1 /* diag.c Larn is copyrighted 1986 by Noah Morgan. */
2 /* $FreeBSD: src/games/larn/diag.c,v 1.5 1999/11/16 02:57:21 billf Exp $ */
7 extern int rmst, maxitm, lasttime;
9 static void greedy(void);
10 static void fsorry(void);
11 static void fcheat(void);
13 static struct tms cputime;
16 ***************************
17 DIAG -- dungeon diagnostics
18 ***************************
20 subroutine to print out data for debugging
23 static int dcount(int);
24 static void drawdiagscreen(void);
26 static int rndcount[16];
35 if (lcreat(diagfile) < 0) { /* open the diagnostic file */
37 lprcat("\ndiagnostic failure\n");
41 write(1, "\nDiagnosing . . .\n", 18);
42 lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
44 /* for the character attributes */
46 lprintf("\n\nPlayer attributes:\n\nHit points: %2d(%2d)", (long)c[HP], (long)c[HPMAX]);
47 lprintf("\ngold: %d Experience: %d Character level: %d Level in caverns: %d",
48 (long)c[GOLD], (long)c[EXPERIENCE], (long)c[LEVEL], (long)level);
49 lprintf("\nTotal types of monsters: %d", (long)MAXMONST + 8);
51 lprcat("\f\nHere's the dungeon:\n\n");
54 for (j = 0; j < MAXLEVEL + MAXVLEVEL; j++) {
56 lprintf("\nMaze for level %s:\n", levelname[level]);
61 lprcat("\f\nNow for the monster data:\n\n");
62 lprcat(" Monster Name LEV AC DAM ATT DEF GOLD HP EXP \n");
63 lprcat("--------------------------------------------------------------------------\n");
64 for (i = 0; i <= MAXMONST + 8; i++) {
65 lprintf("%19s %2d %3d ", monster[i].name, (long)monster[i].level, (long)monster[i].armorclass);
66 lprintf(" %3d %3d %3d ", (long)monster[i].damage, (long)monster[i].attack, (long)monster[i].defense);
67 lprintf("%6d %3d %6d\n", (long)monster[i].gold, (long)monster[i].hitpoints, (long)monster[i].experience);
70 lprcat("\n\nHere's a Table for the to hit percentages\n");
71 lprcat("\n We will be assuming that players level = 2 * monster level");
72 lprcat("\n and that the players dexterity and strength are 16.");
73 lprcat("\n to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
74 lprcat("\n damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
75 lprcat("\n to hit: if rnd(22) < to hit then player hits\n");
76 lprcat("\n Each entry is as follows: to hit / damage / number hits to kill\n");
77 lprcat("\n monster WC = 4 WC = 20 WC = 40");
78 lprcat("\n---------------------------------------------------------------");
79 for (i = 0; i <= MAXMONST + 8; i++) {
80 hit = 2 * monster[i].armorclass + 2 * monster[i].level + 16;
81 dam = 16 - c[HARDGAME];
82 lprintf("\n%20s %2d/%2d/%2d %2d/%2d/%2d %2d/%2d/%2d",
84 (long)(hit / 2), (long)max(0, dam + 2), (long)(monster[i].hitpoints / (dam + 2) + 1),
85 (long)((hit + 2) / 2), (long)max(0, dam + 10), (long)(monster[i].hitpoints / (dam + 10) + 1),
86 (long)((hit + 5) / 2), (long)max(0, dam + 20), (long)(monster[i].hitpoints / (dam + 20) + 1));
89 lprcat("\n\nHere's the list of available potions:\n\n");
90 for (i = 0; i < MAXPOTION; i++)
91 lprintf("%20s\n", &potionhide[i][1]);
92 lprcat("\n\nHere's the list of available scrolls:\n\n");
93 for (i = 0; i < MAXSCROLL; i++)
94 lprintf("%20s\n", &scrollhide[i][1]);
95 lprcat("\n\nHere's the spell list:\n\n");
96 lprcat("spell name description\n");
97 lprcat("-------------------------------------------------------------------------------------------\n\n");
98 for (j = 0; j < SPNUM; j++) {
101 lprintf(" %21s %s\n", spelname[j], speldescript[j]);
104 lprcat("\n\nFor the c[] array:\n");
105 for (j = 0; j < 100; j += 10) {
106 lprintf("\nc[%2d] = ", (long)j);
107 for (i = 0; i < 9; i++)
108 lprintf("%5d ", (long)c[i + j]);
111 lprcat("\n\nTest of random number generator ----------------");
112 lprcat("\n for 25,000 calls divided into 16 slots\n\n");
114 for (i = 0; i < 16; i++)
116 for (i = 0; i < 25000; i++)
117 rndcount[rund(16)]++;
118 for (i = 0; i < 16; i++) {
119 lprintf(" %5d", (long)rndcount[i]);
127 lprcat("Done Diagnosing . . .");
131 subroutine to count the number of occurrences of an object
139 for (i = 0; i < MAXX; i++)
140 for (j = 0; j < MAXY; j++)
141 for (p = 0; p < MAXLEVEL; p++)
142 if (cell[p * MAXX * MAXY + i * MAXY + j].item == l)
148 subroutine to draw the whole screen as the player knows it
155 /* for the east west walls of this line */
156 for (i = 0; i < MAXY; i++) {
157 for (j = 0; j < MAXX; j++)
159 lprc(monstnamelist[k]);
161 lprc(objnamelist[item[j][i]]);
168 to save the game in a file
170 static time_t zzz = 0;
173 savegame(char *fname)
183 if (lcreat(fname) < 0) {
185 lprintf("\nCan't open file <%s> to save game\n", fname);
191 lwrite((char *)beenhere, MAXLEVEL + MAXVLEVEL);
192 for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
194 lwrite((char *)&cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);
195 times(&cputime); /* get cpu time */
196 c[CPUTIME] += (cputime.tms_utime + cputime.tms_stime) / 60;
197 lwrite((char *)&c[0], 100 * sizeof(long));
202 lwrite((char *)iven, 26);
203 lwrite((char *)ivenarg, 26 * sizeof(short));
204 for (k = 0; k < MAXSCROLL; k++)
205 lprc(scrollname[k][0]);
206 for (k = 0; k < MAXPOTION; k++)
207 lprc(potionname[k][0]);
208 lwrite((char *)spelknow, SPNUM);
210 lprc(rmst); /* random monster generation counter */
211 for (i = 0; i < 90; i++)
213 lwrite((char *)course, 25);
216 for (i = 0; i < MAXMONST; i++)
217 lprc(monster[i].genocided); /* genocide info */
218 /* save spheres of annihilation */
219 for (sp = spheres; sp; sp = sp->p)
220 lwrite((char *)sp, sizeof(struct sphere));
222 lprint((long)(zzz - initialtime));
223 lwrite((char *)&zzz, sizeof(long));
224 if (fstat(io_outfd, &statbuf) < 0)
227 lprint((long)statbuf.st_ino); /* inode # */
239 restoregame(char *fname)
242 struct sphere *sp, *sp2;
243 struct stat filetimes;
245 lprcat("\nRestoring . . .");
247 if (lopen(fname) <= 0) {
249 lprintf("\nCan't open file <%s>to restore game\n", fname);
251 c[GOLD] = c[BANKACCOUNT] = 0;
255 lrfill((char *)beenhere, MAXLEVEL + MAXVLEVEL);
256 for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
258 lrfill((char *)&cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);
260 lrfill((char *)&c[0], 100 * sizeof(long));
262 level = c[CAVELEVEL] = lgetc();
265 lrfill((char *)iven, 26);
266 lrfill((char *)ivenarg, 26 * sizeof(short));
267 for (k = 0; k < MAXSCROLL; k++)
268 scrollname[k] = lgetc() ? scrollhide[k] : "";
269 for (k = 0; k < MAXPOTION; k++)
270 potionname[k] = lgetc() ? potionhide[k] : "";
271 lrfill((char *)spelknow, SPNUM);
273 rmst = lgetc(); /* random monster creation flag */
275 for (i = 0; i < 90; i++)
276 itm_[i].qty = lgetc();
277 lrfill((char *)course, 25);
279 if (VERSION != lgetc()) { /* version number */
281 lprcat("Sorry, But your save file is for an older version of larn\n");
283 c[GOLD] = c[BANKACCOUNT] = 0;
288 for (i = 0; i < MAXMONST; i++)
289 monster[i].genocided = lgetc(); /* genocide info */
290 for (sp = NULL, i = 0; i < c[SPHCAST]; i++) {
292 sp = malloc(sizeof(struct sphere));
294 write(2, "Can't malloc() for sphere space\n", 32);
297 /* get spheres of annihilation */
298 lrfill((char *)sp, sizeof(struct sphere));
299 sp->p = 0; /* null out pointer */
301 spheres = sp; /* beginning of list */
307 initialtime = zzz - lrint_x();
308 /* get the creation and modification time of file */
309 fstat(io_infd, &filetimes);
310 lrfill((char *)&zzz, sizeof(long));
312 if (filetimes.st_ctime > zzz) /* file create time */
314 else if (filetimes.st_mtime > zzz) /* file modify time */
319 } /* died a post mortem death */
322 i = lrint_x(); /* inode # */
323 if (i && (filetimes.st_ino != (unsigned)i))
326 if (strcmp(fname, ckpfile) == 0) {
327 if (lappend(fname) < 0)
334 } else if (unlink(fname) < 0)
335 fcheat(); /* can't unlink save file */
336 /* for the greedy cheater checker */
337 for (k = 0; k < 6; k++)
340 if (c[HPMAX] > 999 || c[SPELLMAX] > 125)
342 /* if patch up lev 25 player */
343 if (c[LEVEL] == 25 && c[EXPERIENCE] > skill[24]) {
345 tmp = c[EXPERIENCE] - skill[24]; /* amount to go up */
346 c[EXPERIENCE] = skill[24];
347 raiseexperience((long)tmp);
354 subroutine to not allow greedy cheaters
364 lprcat("\n\nI am so sorry, but your character is a little TOO good! Since this\n");
365 lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
366 lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
367 lprcat("to continue.\n");
369 c[GOLD] = c[BANKACCOUNT] = 0;
374 subroutine to not allow altered save files and terminate the attempted
380 lprcat("\nSorry, but your savefile has been altered.\n");
381 lprcat("However, seeing as I am a good sport, I will let you play.\n");
382 lprcat("Be advised though, you won't be placed on the normal scoreboard.");
388 subroutine to not allow game if save file can't be deleted
398 lprcat("\nSorry, but your savefile can't be deleted. This can only mean\n");
399 lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
400 lprcat("is in. Since this is unfair to the rest of the larn community, I\n");
401 lprcat("cannot let you play this game.\n");
403 c[GOLD] = c[BANKACCOUNT] = 0;