]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/diag.c
1 /* $NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $ */
3 /* diag.c Larn is copyrighted 1986 by Noah Morgan. */
6 __RCSID("$NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $");
10 #include <sys/times.h>
18 static void greedy(void);
19 static void fsorry(void);
20 static void fcheat(void);
22 static struct tms cputime
;
25 ***************************
26 DIAG -- dungeon diagnostics
27 ***************************
29 subroutine to print out data for debugging
32 static int rndcount
[16];
40 if (lcreat(diagfile
) < 0) { /* open the diagnostic file */
42 lprcat("\ndiagnostic failure\n");
45 write(1, "\nDiagnosing . . .\n", 18);
46 lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
48 /* for the character attributes */
50 lprintf("\n\nPlayer attributes:\n\nHit points: %2ld(%2ld)", (long) c
[HP
], (long) c
[HPMAX
]);
51 lprintf("\ngold: %ld Experience: %ld Character level: %ld Level in caverns: %ld",
52 (long) c
[GOLD
], (long) c
[EXPERIENCE
], (long) c
[LEVEL
], (long) level
);
53 lprintf("\nTotal types of monsters: %ld", (long) MAXMONST
+ 8);
55 lprcat("\f\nHere's the dungeon:\n\n");
58 for (j
= 0; j
< MAXLEVEL
+ MAXVLEVEL
; j
++) {
60 lprintf("\nMaze for level %s:\n", levelname
[level
]);
65 lprcat("\f\nNow for the monster data:\n\n");
66 lprcat(" Monster Name LEV AC DAM ATT DEF GOLD HP EXP \n");
67 lprcat("--------------------------------------------------------------------------\n");
68 for (i
= 0; i
<= MAXMONST
+ 8; i
++) {
69 lprintf("%19s %2ld %3ld ", monster
[i
].name
, (long) monster
[i
].level
, (long) monster
[i
].armorclass
);
70 lprintf(" %3ld %3ld %3ld ", (long) monster
[i
].damage
, (long) monster
[i
].attack
, (long) monster
[i
].defense
);
71 lprintf("%6ld %3ld %6ld\n", (long) monster
[i
].gold
, (long) monster
[i
].hitpoints
, (long) monster
[i
].experience
);
74 lprcat("\n\nHere's a Table for the to hit percentages\n");
75 lprcat("\n We will be assuming that players level = 2 * monster level");
76 lprcat("\n and that the players dexterity and strength are 16.");
77 lprcat("\n to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
78 lprcat("\n damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
79 lprcat("\n to hit: if rnd(22) < to hit then player hits\n");
80 lprcat("\n Each entry is as follows: to hit / damage / number hits to kill\n");
81 lprcat("\n monster WC = 4 WC = 20 WC = 40");
82 lprcat("\n---------------------------------------------------------------");
83 for (i
= 0; i
<= MAXMONST
+ 8; i
++) {
84 hit
= 2 * monster
[i
].armorclass
+ 2 * monster
[i
].level
+ 16;
85 dam
= 16 - c
[HARDGAME
];
86 lprintf("\n%20s %2ld/%2ld/%2ld %2ld/%2ld/%2ld %2ld/%2ld/%2ld",
88 (long) (hit
/ 2), (long) max(0, dam
+ 2), (long) (monster
[i
].hitpoints
/ (dam
+ 2) + 1),
89 (long) ((hit
+ 2) / 2), (long) max(0, dam
+ 10), (long) (monster
[i
].hitpoints
/ (dam
+ 10) + 1),
90 (long) ((hit
+ 5) / 2), (long) max(0, dam
+ 20), (long) (monster
[i
].hitpoints
/ (dam
+ 20) + 1));
93 lprcat("\n\nHere's the list of available potions:\n\n");
94 for (i
= 0; i
< MAXPOTION
; i
++)
95 lprintf("%20s\n", &potionhide
[i
][1]);
96 lprcat("\n\nHere's the list of available scrolls:\n\n");
97 for (i
= 0; i
< MAXSCROLL
; i
++)
98 lprintf("%20s\n", &scrollhide
[i
][1]);
99 lprcat("\n\nHere's the spell list:\n\n");
100 lprcat("spell name description\n");
101 lprcat("-------------------------------------------------------------------------------------------\n\n");
102 for (j
= 0; j
< SPNUM
; j
++) {
105 lprintf(" %21s %s\n", spelname
[j
], speldescript
[j
]);
108 lprcat("\n\nFor the c[] array:\n");
109 for (j
= 0; j
< 100; j
+= 10) {
110 lprintf("\nc[%2ld] = ", (long) j
);
111 for (i
= 0; i
< 9; i
++)
112 lprintf("%5ld ", (long) c
[i
+ j
]);
115 lprcat("\n\nTest of random number generator ----------------");
116 lprcat("\n for 25,000 calls divided into 16 slots\n\n");
118 for (i
= 0; i
< 16; i
++)
120 for (i
= 0; i
< 25000; i
++)
121 rndcount
[rund(16)]++;
122 for (i
= 0; i
< 16; i
++) {
123 lprintf(" %5ld", (long) rndcount
[i
]);
131 lprcat("Done Diagnosing . . .");
135 subroutine to count the number of occurrences of an object
144 for (i
= 0; i
< MAXX
; i
++)
145 for (j
= 0; j
< MAXY
; j
++)
146 for (p
= 0; p
< MAXLEVEL
; p
++)
147 if (cell
[p
* MAXX
* MAXY
+ i
* MAXY
+ j
].item
== l
)
153 subroutine to draw the whole screen as the player knows it
160 for (i
= 0; i
< MAXY
; i
++)
161 /* for the east west walls of this line */
163 for (j
= 0; j
< MAXX
; j
++)
165 lprc(monstnamelist
[k
]);
167 lprc(objnamelist
[item
[j
][i
]]);
175 to save the game in a file
177 static time_t zzz
= 0;
179 savegame(char *fname
)
189 if (lcreat(fname
) < 0) {
191 lprintf("\nCan't open file <%s> to save game\n", fname
);
196 lwrite((char *) beenhere
, MAXLEVEL
+ MAXVLEVEL
);
197 for (k
= 0; k
< MAXLEVEL
+ MAXVLEVEL
; k
++)
199 lwrite((char *) &cell
[k
* MAXX
* MAXY
], sizeof(struct cel
) * MAXY
* MAXX
);
200 times(&cputime
); /* get cpu time */
201 c
[CPUTIME
] += (cputime
.tms_utime
+ cputime
.tms_stime
) / 60;
202 lwrite((char *) &c
[0], 100 * sizeof(long));
203 lprint((long) gltime
);
207 lwrite((char *) iven
, 26);
208 lwrite((char *) ivenarg
, 26 * sizeof(short));
209 for (k
= 0; k
< MAXSCROLL
; k
++)
210 lprc(scrollname
[k
][0]);
211 for (k
= 0; k
< MAXPOTION
; k
++)
212 lprc(potionname
[k
][0]);
213 lwrite((char *) spelknow
, SPNUM
);
215 lprc(rmst
); /* random monster generation counter */
216 for (i
= 0; i
< 90; i
++)
218 lwrite((char *) course
, 25);
221 for (i
= 0; i
< MAXMONST
; i
++)
222 lprc(monster
[i
].genocided
); /* genocide info */
223 for (sp
= spheres
; sp
; sp
= sp
->p
)
224 lwrite((char *) sp
, sizeof(struct sphere
)); /* save spheres of
227 lprint((long) (zzz
- initialtime
));
228 lwrite((char *) &zzz
, sizeof(long));
229 if (fstat(io_outfd
, &statbuf
) < 0)
232 lprint((long) statbuf
.st_ino
); /* inode # */
244 restoregame(char *fname
)
247 struct sphere
*sp
, *sp2
;
248 struct stat filetimes
;
250 lprcat("\nRestoring . . .");
252 if (lopen(fname
) <= 0) {
254 lprintf("\nCan't open file <%s>to restore game\n", fname
);
256 c
[GOLD
] = c
[BANKACCOUNT
] = 0;
260 lrfill((char *) beenhere
, MAXLEVEL
+ MAXVLEVEL
);
261 for (k
= 0; k
< MAXLEVEL
+ MAXVLEVEL
; k
++)
263 lrfill((char *) &cell
[k
* MAXX
* MAXY
], sizeof(struct cel
) * MAXY
* MAXX
);
265 lrfill((char *) &c
[0], 100 * sizeof(long));
266 gltime
= larn_lrint();
267 level
= c
[CAVELEVEL
] = lgetc();
270 lrfill((char *) iven
, 26);
271 lrfill((char *) ivenarg
, 26 * sizeof(short));
272 for (k
= 0; k
< MAXSCROLL
; k
++)
273 scrollname
[k
] = lgetc() ? scrollhide
[k
] : "";
274 for (k
= 0; k
< MAXPOTION
; k
++)
275 potionname
[k
] = lgetc() ? potionhide
[k
] : "";
276 lrfill((char *) spelknow
, SPNUM
);
278 rmst
= lgetc(); /* random monster creation flag */
280 for (i
= 0; i
< 90; i
++)
281 itm
[i
].qty
= lgetc();
282 lrfill((char *) course
, 25);
284 if (VERSION
!= lgetc()) { /* version number */
286 lprcat("Sorry, But your save file is for an older version of larn\n");
288 c
[GOLD
] = c
[BANKACCOUNT
] = 0;
292 for (i
= 0; i
< MAXMONST
; i
++)
293 monster
[i
].genocided
= lgetc(); /* genocide info */
294 for (sp
= 0, i
= 0; i
< c
[SPHCAST
]; i
++) {
296 sp
= (struct sphere
*) malloc(sizeof(struct sphere
));
298 write(2, "Can't malloc() for sphere space\n", 32);
301 lrfill((char *) sp
, sizeof(struct sphere
)); /* get spheres of
303 sp
->p
= 0; /* null out pointer */
305 spheres
= sp
; /* beginning of list */
311 initialtime
= zzz
- larn_lrint();
312 /* get the creation and modification time of file */
313 fstat(io_infd
, &filetimes
);
314 lrfill((char *) &zzz
, sizeof(long));
316 if (filetimes
.st_ctime
> zzz
)
317 fsorry(); /* file create time */
318 else if (filetimes
.st_mtime
> zzz
)
319 fsorry(); /* file modify time */
323 } /* died a post mortem death */
325 /* XXX the following will break on 64-bit inode numbers */
326 i
= larn_lrint(); /* inode # */
327 if (i
&& (filetimes
.st_ino
!= (ino_t
) i
))
330 if (strcmp(fname
, ckpfile
) == 0) {
331 if (lappend(fname
) < 0)
338 } else if (unlink(fname
) < 0)
339 fcheat(); /* can't unlink save file */
340 /* for the greedy cheater checker */
341 for (k
= 0; k
< 6; k
++)
344 if (c
[HPMAX
] > 999 || c
[SPELLMAX
] > 125)
346 if (c
[LEVEL
] == 25 && c
[EXPERIENCE
] > skill
[24]) { /* if patch up lev 25
349 tmp
= c
[EXPERIENCE
] - skill
[24]; /* amount to go up */
350 c
[EXPERIENCE
] = skill
[24];
351 raiseexperience((long) tmp
);
358 subroutine to not allow greedy cheaters
368 lprcat("\n\nI am so sorry, but your character is a little TOO good! Since this\n");
369 lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
370 lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
371 lprcat("to continue.\n");
373 c
[GOLD
] = c
[BANKACCOUNT
] = 0;
379 subroutine to not allow altered save files and terminate the attempted
385 lprcat("\nSorry, but your savefile has been altered.\n");
386 lprcat("However, seeing as I am a good sport, I will let you play.\n");
387 lprcat("Be advised though, you won't be placed on the normal scoreboard.");
393 subroutine to not allow game if save file can't be deleted
403 lprcat("\nSorry, but your savefile can't be deleted. This can only mean\n");
404 lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
405 lprcat("is in. Since this is unfair to the rest of the larn community, I\n");
406 lprcat("cannot let you play this game.\n");
408 c
[GOLD
] = c
[BANKACCOUNT
] = 0;