]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/diag.c
cgram: adjust style to survive lint's strict bool mode
[bsdgames-darwin.git] / larn / diag.c
1 /* $NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $ */
2
3 /* diag.c Larn is copyrighted 1986 by Noah Morgan. */
4 #include <sys/cdefs.h>
5 #ifndef lint
6 __RCSID("$NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $");
7 #endif /* not lint */
8
9 #include <sys/types.h>
10 #include <sys/times.h>
11 #include <sys/stat.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "header.h"
16 #include "extern.h"
17
18 static void greedy(void);
19 static void fsorry(void);
20 static void fcheat(void);
21
22 static struct tms cputime;
23 time_t time(time_t *t);
24
25 /*
26 ***************************
27 DIAG -- dungeon diagnostics
28 ***************************
29
30 subroutine to print out data for debugging
31 */
32 #ifdef EXTRA
33 static int rndcount[16];
34 void
35 diag()
36 {
37 int i, j;
38 int hit, dam;
39 cursors();
40 lwclose();
41 if (lcreat(diagfile) < 0) { /* open the diagnostic file */
42 lcreat((char *) 0);
43 lprcat("\ndiagnostic failure\n");
44 return (-1);
45 }
46 write(1, "\nDiagnosing . . .\n", 18);
47 lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
48
49 /* for the character attributes */
50
51 lprintf("\n\nPlayer attributes:\n\nHit points: %2ld(%2ld)", (long) c[HP], (long) c[HPMAX]);
52 lprintf("\ngold: %ld Experience: %ld Character level: %ld Level in caverns: %ld",
53 (long) c[GOLD], (long) c[EXPERIENCE], (long) c[LEVEL], (long) level);
54 lprintf("\nTotal types of monsters: %ld", (long) MAXMONST + 8);
55
56 lprcat("\f\nHere's the dungeon:\n\n");
57
58 i = level;
59 for (j = 0; j < MAXLEVEL + MAXVLEVEL; j++) {
60 newcavelevel(j);
61 lprintf("\nMaze for level %s:\n", levelname[level]);
62 diagdrawscreen();
63 }
64 newcavelevel(i);
65
66 lprcat("\f\nNow for the monster data:\n\n");
67 lprcat(" Monster Name LEV AC DAM ATT DEF GOLD HP EXP \n");
68 lprcat("--------------------------------------------------------------------------\n");
69 for (i = 0; i <= MAXMONST + 8; i++) {
70 lprintf("%19s %2ld %3ld ", monster[i].name, (long) monster[i].level, (long) monster[i].armorclass);
71 lprintf(" %3ld %3ld %3ld ", (long) monster[i].damage, (long) monster[i].attack, (long) monster[i].defense);
72 lprintf("%6ld %3ld %6ld\n", (long) monster[i].gold, (long) monster[i].hitpoints, (long) monster[i].experience);
73 }
74
75 lprcat("\n\nHere's a Table for the to hit percentages\n");
76 lprcat("\n We will be assuming that players level = 2 * monster level");
77 lprcat("\n and that the players dexterity and strength are 16.");
78 lprcat("\n to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
79 lprcat("\n damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
80 lprcat("\n to hit: if rnd(22) < to hit then player hits\n");
81 lprcat("\n Each entry is as follows: to hit / damage / number hits to kill\n");
82 lprcat("\n monster WC = 4 WC = 20 WC = 40");
83 lprcat("\n---------------------------------------------------------------");
84 for (i = 0; i <= MAXMONST + 8; i++) {
85 hit = 2 * monster[i].armorclass + 2 * monster[i].level + 16;
86 dam = 16 - c[HARDGAME];
87 lprintf("\n%20s %2ld/%2ld/%2ld %2ld/%2ld/%2ld %2ld/%2ld/%2ld",
88 monster[i].name,
89 (long) (hit / 2), (long) max(0, dam + 2), (long) (monster[i].hitpoints / (dam + 2) + 1),
90 (long) ((hit + 2) / 2), (long) max(0, dam + 10), (long) (monster[i].hitpoints / (dam + 10) + 1),
91 (long) ((hit + 5) / 2), (long) max(0, dam + 20), (long) (monster[i].hitpoints / (dam + 20) + 1));
92 }
93
94 lprcat("\n\nHere's the list of available potions:\n\n");
95 for (i = 0; i < MAXPOTION; i++)
96 lprintf("%20s\n", &potionhide[i][1]);
97 lprcat("\n\nHere's the list of available scrolls:\n\n");
98 for (i = 0; i < MAXSCROLL; i++)
99 lprintf("%20s\n", &scrollhide[i][1]);
100 lprcat("\n\nHere's the spell list:\n\n");
101 lprcat("spell name description\n");
102 lprcat("-------------------------------------------------------------------------------------------\n\n");
103 for (j = 0; j < SPNUM; j++) {
104 lprc(' ');
105 lprcat(spelcode[j]);
106 lprintf(" %21s %s\n", spelname[j], speldescript[j]);
107 }
108
109 lprcat("\n\nFor the c[] array:\n");
110 for (j = 0; j < 100; j += 10) {
111 lprintf("\nc[%2ld] = ", (long) j);
112 for (i = 0; i < 9; i++)
113 lprintf("%5ld ", (long) c[i + j]);
114 }
115
116 lprcat("\n\nTest of random number generator ----------------");
117 lprcat("\n for 25,000 calls divided into 16 slots\n\n");
118
119 for (i = 0; i < 16; i++)
120 rndcount[i] = 0;
121 for (i = 0; i < 25000; i++)
122 rndcount[rund(16)]++;
123 for (i = 0; i < 16; i++) {
124 lprintf(" %5ld", (long) rndcount[i]);
125 if (i == 7)
126 lprc('\n');
127 }
128
129 lprcat("\n\n");
130 lwclose();
131 lcreat((char *) 0);
132 lprcat("Done Diagnosing . . .");
133 return (0);
134 }
135 /*
136 subroutine to count the number of occurrences of an object
137 */
138 int
139 dcount(l)
140 int l;
141 {
142 int i, j, p;
143 int k;
144 k = 0;
145 for (i = 0; i < MAXX; i++)
146 for (j = 0; j < MAXY; j++)
147 for (p = 0; p < MAXLEVEL; p++)
148 if (cell[p * MAXX * MAXY + i * MAXY + j].item == l)
149 k++;
150 return (k);
151 }
152
153 /*
154 subroutine to draw the whole screen as the player knows it
155 */
156 void
157 diagdrawscreen()
158 {
159 int i, j, k;
160
161 for (i = 0; i < MAXY; i++)
162 /* for the east west walls of this line */
163 {
164 for (j = 0; j < MAXX; j++)
165 if (k = mitem[j][i])
166 lprc(monstnamelist[k]);
167 else
168 lprc(objnamelist[item[j][i]]);
169 lprc('\n');
170 }
171 }
172 #endif
173
174
175 /*
176 to save the game in a file
177 */
178 static time_t zzz = 0;
179 int
180 savegame(char *fname)
181 {
182 int i, k;
183 struct sphere *sp;
184 struct stat statbuf;
185
186 nosignal = 1;
187 lflush();
188 savelevel();
189 ointerest();
190 if (lcreat(fname) < 0) {
191 lcreat((char *) 0);
192 lprintf("\nCan't open file <%s> to save game\n", fname);
193 nosignal = 0;
194 return (-1);
195 }
196 set_score_output();
197 lwrite((char *) beenhere, MAXLEVEL + MAXVLEVEL);
198 for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
199 if (beenhere[k])
200 lwrite((char *) &cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);
201 times(&cputime); /* get cpu time */
202 c[CPUTIME] += (cputime.tms_utime + cputime.tms_stime) / 60;
203 lwrite((char *) &c[0], 100 * sizeof(long));
204 lprint((long) gltime);
205 lprc(level);
206 lprc(playerx);
207 lprc(playery);
208 lwrite((char *) iven, 26);
209 lwrite((char *) ivenarg, 26 * sizeof(short));
210 for (k = 0; k < MAXSCROLL; k++)
211 lprc(scrollname[k][0]);
212 for (k = 0; k < MAXPOTION; k++)
213 lprc(potionname[k][0]);
214 lwrite((char *) spelknow, SPNUM);
215 lprc(wizard);
216 lprc(rmst); /* random monster generation counter */
217 for (i = 0; i < 90; i++)
218 lprc(itm[i].qty);
219 lwrite((char *) course, 25);
220 lprc(cheat);
221 lprc(VERSION);
222 for (i = 0; i < MAXMONST; i++)
223 lprc(monster[i].genocided); /* genocide info */
224 for (sp = spheres; sp; sp = sp->p)
225 lwrite((char *) sp, sizeof(struct sphere)); /* save spheres of
226 * annihilation */
227 time(&zzz);
228 lprint((long) (zzz - initialtime));
229 lwrite((char *) &zzz, sizeof(long));
230 if (fstat(io_outfd, &statbuf) < 0)
231 lprint(0L);
232 else
233 lprint((long) statbuf.st_ino); /* inode # */
234 lwclose();
235 lastmonst[0] = 0;
236 #ifndef VT100
237 setscroll();
238 #endif /* VT100 */
239 lcreat((char *) 0);
240 nosignal = 0;
241 return (0);
242 }
243
244 void
245 restoregame(char *fname)
246 {
247 int i, k;
248 struct sphere *sp, *sp2;
249 struct stat filetimes;
250 cursors();
251 lprcat("\nRestoring . . .");
252 lflush();
253 if (lopen(fname) <= 0) {
254 lcreat((char *) 0);
255 lprintf("\nCan't open file <%s>to restore game\n", fname);
256 nap(2000);
257 c[GOLD] = c[BANKACCOUNT] = 0;
258 died(-265);
259 return;
260 }
261 lrfill((char *) beenhere, MAXLEVEL + MAXVLEVEL);
262 for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
263 if (beenhere[k])
264 lrfill((char *) &cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);
265
266 lrfill((char *) &c[0], 100 * sizeof(long));
267 gltime = larn_lrint();
268 level = c[CAVELEVEL] = lgetc();
269 playerx = lgetc();
270 playery = lgetc();
271 lrfill((char *) iven, 26);
272 lrfill((char *) ivenarg, 26 * sizeof(short));
273 for (k = 0; k < MAXSCROLL; k++)
274 scrollname[k] = lgetc() ? scrollhide[k] : "";
275 for (k = 0; k < MAXPOTION; k++)
276 potionname[k] = lgetc() ? potionhide[k] : "";
277 lrfill((char *) spelknow, SPNUM);
278 wizard = lgetc();
279 rmst = lgetc(); /* random monster creation flag */
280
281 for (i = 0; i < 90; i++)
282 itm[i].qty = lgetc();
283 lrfill((char *) course, 25);
284 cheat = lgetc();
285 if (VERSION != lgetc()) { /* version number */
286 cheat = 1;
287 lprcat("Sorry, But your save file is for an older version of larn\n");
288 nap(2000);
289 c[GOLD] = c[BANKACCOUNT] = 0;
290 died(-266);
291 return;
292 }
293 for (i = 0; i < MAXMONST; i++)
294 monster[i].genocided = lgetc(); /* genocide info */
295 for (sp = 0, i = 0; i < c[SPHCAST]; i++) {
296 sp2 = sp;
297 sp = (struct sphere *) malloc(sizeof(struct sphere));
298 if (sp == 0) {
299 write(2, "Can't malloc() for sphere space\n", 32);
300 break;
301 }
302 lrfill((char *) sp, sizeof(struct sphere)); /* get spheres of
303 * annihilation */
304 sp->p = 0; /* null out pointer */
305 if (i == 0)
306 spheres = sp; /* beginning of list */
307 else
308 sp2->p = sp;
309 }
310
311 time(&zzz);
312 initialtime = zzz - larn_lrint();
313 /* get the creation and modification time of file */
314 fstat(io_infd, &filetimes);
315 lrfill((char *) &zzz, sizeof(long));
316 zzz += 6;
317 if (filetimes.st_ctime > zzz)
318 fsorry(); /* file create time */
319 else if (filetimes.st_mtime > zzz)
320 fsorry(); /* file modify time */
321 if (c[HP] < 0) {
322 died(284);
323 return;
324 } /* died a post mortem death */
325 oldx = oldy = 0;
326 /* XXX the following will break on 64-bit inode numbers */
327 i = larn_lrint(); /* inode # */
328 if (i && (filetimes.st_ino != (ino_t) i))
329 fsorry();
330 lrclose();
331 if (strcmp(fname, ckpfile) == 0) {
332 if (lappend(fname) < 0)
333 fcheat();
334 else {
335 lprc(' ');
336 lwclose();
337 }
338 lcreat((char *) 0);
339 } else if (unlink(fname) < 0)
340 fcheat(); /* can't unlink save file */
341 /* for the greedy cheater checker */
342 for (k = 0; k < 6; k++)
343 if (c[k] > 99)
344 greedy();
345 if (c[HPMAX] > 999 || c[SPELLMAX] > 125)
346 greedy();
347 if (c[LEVEL] == 25 && c[EXPERIENCE] > skill[24]) { /* if patch up lev 25
348 * player */
349 long tmp;
350 tmp = c[EXPERIENCE] - skill[24]; /* amount to go up */
351 c[EXPERIENCE] = skill[24];
352 raiseexperience((long) tmp);
353 }
354 getlevel();
355 lasttime = gltime;
356 }
357
358 /*
359 subroutine to not allow greedy cheaters
360 */
361 static void
362 greedy(void)
363 {
364 #if WIZID
365 if (wizard)
366 return;
367 #endif
368
369 lprcat("\n\nI am so sorry, but your character is a little TOO good! Since this\n");
370 lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
371 lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
372 lprcat("to continue.\n");
373 nap(5000);
374 c[GOLD] = c[BANKACCOUNT] = 0;
375 died(-267);
376 return;
377 }
378
379 /*
380 subroutine to not allow altered save files and terminate the attempted
381 restart
382 */
383 static void
384 fsorry(void)
385 {
386 lprcat("\nSorry, but your savefile has been altered.\n");
387 lprcat("However, seeing as I am a good sport, I will let you play.\n");
388 lprcat("Be advised though, you won't be placed on the normal scoreboard.");
389 cheat = 1;
390 nap(4000);
391 }
392
393 /*
394 subroutine to not allow game if save file can't be deleted
395 */
396 static void
397 fcheat(void)
398 {
399 #if WIZID
400 if (wizard)
401 return;
402 #endif
403
404 lprcat("\nSorry, but your savefile can't be deleted. This can only mean\n");
405 lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
406 lprcat("is in. Since this is unfair to the rest of the larn community, I\n");
407 lprcat("cannot let you play this game.\n");
408 nap(5000);
409 c[GOLD] = c[BANKACCOUNT] = 0;
410 died(-268);
411 return;
412 }