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