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