MFC:
[dragonfly.git] / games / larn / diag.c
blob63b1a47d5e930bbc0728348e40e0489890f9fc74
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 $ */
3 /* $DragonFly: src/games/larn/diag.c,v 1.5 2006/08/26 17:05:05 pavalos Exp $ */
4 #include <sys/types.h>
5 #include <sys/times.h>
6 #include <sys/stat.h>
7 #include "header.h"
8 extern int rmst,maxitm,lasttime;
9 static struct tms cputime;
11 ***************************
12 DIAG -- dungeon diagnostics
13 ***************************
15 subroutine to print out data for debugging
18 static void greedy(void);
19 static void fsorry(void);
20 static void fcheat(void);
21 #ifdef EXTRA
22 static int dcount(int);
23 static void drawdiagscreen(void);
25 static int rndcount[16];
27 int
28 diag(void)
30 int i,j;
31 int hit,dam;
32 cursors(); lwclose();
33 if (lcreat(diagfile) < 0) /* open the diagnostic file */
35 lcreat((char*)0); lprcat("\ndiagnostic failure\n"); return(-1);
38 write(1,"\nDiagnosing . . .\n",18);
39 lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
41 /* for the character attributes */
43 lprintf("\n\nPlayer attributes:\n\nHit points: %2d(%2d)",(long)c[HP],(long)c[HPMAX]);
44 lprintf("\ngold: %d Experience: %d Character level: %d Level in caverns: %d",
45 (long)c[GOLD],(long)c[EXPERIENCE],(long)c[LEVEL],(long)level);
46 lprintf("\nTotal types of monsters: %d",(long)MAXMONST+8);
48 lprcat("\f\nHere's the dungeon:\n\n");
50 i=level;
51 for (j=0; j<MAXLEVEL+MAXVLEVEL; j++)
53 newcavelevel(j);
54 lprintf("\nMaze for level %s:\n",levelname[level]);
55 diagdrawscreen();
57 newcavelevel(i);
59 lprcat("\f\nNow for the monster data:\n\n");
60 lprcat(" Monster Name LEV AC DAM ATT DEF GOLD HP EXP \n");
61 lprcat("--------------------------------------------------------------------------\n");
62 for (i=0; i<=MAXMONST+8; i++)
64 lprintf("%19s %2d %3d ",monster[i].name,(long)monster[i].level,(long)monster[i].armorclass);
65 lprintf(" %3d %3d %3d ",(long)monster[i].damage,(long)monster[i].attack,(long)monster[i].defense);
66 lprintf("%6d %3d %6d\n",(long)monster[i].gold,(long)monster[i].hitpoints,(long)monster[i].experience);
69 lprcat("\n\nHere's a Table for the to hit percentages\n");
70 lprcat("\n We will be assuming that players level = 2 * monster level");
71 lprcat("\n and that the players dexterity and strength are 16.");
72 lprcat("\n to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
73 lprcat("\n damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
74 lprcat("\n to hit: if rnd(22) < to hit then player hits\n");
75 lprcat("\n Each entry is as follows: to hit / damage / number hits to kill\n");
76 lprcat("\n monster WC = 4 WC = 20 WC = 40");
77 lprcat("\n---------------------------------------------------------------");
78 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",
83 monster[i].name,
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++) lprintf("%20s\n",&potionhide[i][1]);
91 lprcat("\n\nHere's the list of available scrolls:\n\n");
92 for (i=0; i<MAXSCROLL; i++) lprintf("%20s\n",&scrollhide[i][1]);
93 lprcat("\n\nHere's the spell list:\n\n");
94 lprcat("spell name description\n");
95 lprcat("-------------------------------------------------------------------------------------------\n\n");
96 for (j=0; j<SPNUM; j++)
98 lprc(' '); lprcat(spelcode[j]);
99 lprintf(" %21s %s\n",spelname[j],speldescript[j]);
102 lprcat("\n\nFor the c[] array:\n");
103 for (j=0; j<100; j+=10)
105 lprintf("\nc[%2d] = ",(long)j); for (i=0; i<9; i++) lprintf("%5d ",(long)c[i+j]);
108 lprcat("\n\nTest of random number generator ----------------");
109 lprcat("\n for 25,000 calls divided into 16 slots\n\n");
111 for (i=0; i<16; i++) rndcount[i]=0;
112 for (i=0; i<25000; i++) rndcount[rund(16)]++;
113 for (i=0; i<16; i++) { lprintf(" %5d",(long)rndcount[i]); if (i==7) lprc('\n'); }
115 lprcat("\n\n"); lwclose();
116 lcreat((char*)0); lprcat("Done Diagnosing . . .");
117 return(0);
120 subroutine to count the number of occurrences of an object
122 static int
123 dcount(int l)
125 int i,j,p;
126 int k;
127 k=0;
128 for (i=0; i<MAXX; i++)
129 for (j=0; j<MAXY; j++)
130 for (p=0; p<MAXLEVEL; p++)
131 if (cell[p*MAXX*MAXY+i*MAXY+j].item == l) k++;
132 return(k);
136 subroutine to draw the whole screen as the player knows it
138 static void
139 diagdrawscreen(void)
141 int i,j,k;
143 for (i=0; i<MAXY; i++)
145 /* for the east west walls of this line */
147 for (j=0; j<MAXX; j++) if (k=mitem[j][i]) lprc(monstnamelist[k]); else
148 lprc(objnamelist[item[j][i]]);
149 lprc('\n');
152 #endif
155 to save the game in a file
157 static time_t zzz=0;
160 savegame(char *fname)
162 int i,k;
163 struct sphere *sp;
164 struct stat statbuf;
165 nosignal=1; lflush(); savelevel();
166 ointerest();
167 if (lcreat(fname) < 0)
169 lcreat((char*)0); lprintf("\nCan't open file <%s> to save game\n",fname);
170 nosignal=0; return(-1);
173 set_score_output();
174 lwrite((char*)beenhere,MAXLEVEL+MAXVLEVEL);
175 for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
176 if (beenhere[k])
177 lwrite((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
178 times(&cputime); /* get cpu time */
179 c[CPUTIME] += (cputime.tms_utime+cputime.tms_stime)/60;
180 lwrite((char*)&c[0],100*sizeof(long));
181 lprint((long)gtime); lprc(level);
182 lprc(playerx); lprc(playery);
183 lwrite((char*)iven,26); lwrite((char*)ivenarg,26*sizeof(short));
184 for (k=0; k<MAXSCROLL; k++) lprc(scrollname[k][0]);
185 for (k=0; k<MAXPOTION; k++) lprc(potionname[k][0]);
186 lwrite((char*)spelknow,SPNUM); lprc(wizard);
187 lprc(rmst); /* random monster generation counter */
188 for (i=0; i<90; i++) lprc(itm_[i].qty);
189 lwrite((char*)course,25); lprc(cheat); lprc(VERSION);
190 for (i=0; i<MAXMONST; i++) lprc(monster[i].genocided); /* genocide info */
191 for (sp=spheres; sp; sp=sp->p)
192 lwrite((char*)sp,sizeof(struct sphere)); /* save spheres of annihilation */
193 time(&zzz); lprint((long)(zzz-initialtime));
194 lwrite((char*)&zzz,sizeof(long));
195 if (fstat(lfd,&statbuf)< 0) lprint(0L);
196 else lprint((long)statbuf.st_ino); /* inode # */
197 lwclose(); lastmonst[0] = 0;
198 #ifndef VT100
199 setscroll();
200 #endif /* VT100 */
201 lcreat((char*)0); nosignal=0;
202 return(0);
205 void
206 restoregame(char *fname)
208 int i,k;
209 struct sphere *sp,*sp2;
210 struct stat filetimes;
211 cursors(); lprcat("\nRestoring . . ."); lflush();
212 if (lopen(fname) <= 0)
214 lcreat((char*)0); lprintf("\nCan't open file <%s>to restore game\n",fname);
215 nap(2000); c[GOLD]=c[BANKACCOUNT]=0; died(-265); return;
218 lrfill((char*)beenhere,MAXLEVEL+MAXVLEVEL);
219 for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
220 if (beenhere[k])
221 lrfill((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
223 lrfill((char*)&c[0],100*sizeof(long)); gtime = lrint_x();
224 level = c[CAVELEVEL] = lgetc();
225 playerx = lgetc(); playery = lgetc();
226 lrfill((char*)iven,26); lrfill((char*)ivenarg,26*sizeof(short));
227 for (k=0; k<MAXSCROLL; k++) scrollname[k] = lgetc() ? scrollhide[k] : "";
228 for (k=0; k<MAXPOTION; k++) potionname[k] = lgetc() ? potionhide[k] : "";
229 lrfill((char*)spelknow,SPNUM); wizard = lgetc();
230 rmst = lgetc(); /* random monster creation flag */
232 for (i=0; i<90; i++) itm_[i].qty = lgetc();
233 lrfill((char*)course,25); cheat = lgetc();
234 if (VERSION != lgetc()) /* version number */
236 cheat=1;
237 lprcat("Sorry, But your save file is for an older version of larn\n");
238 nap(2000); c[GOLD]=c[BANKACCOUNT]=0; died(-266); return;
241 for (i=0; i<MAXMONST; i++) monster[i].genocided=lgetc(); /* genocide info */
242 for (sp=0,i=0; i<c[SPHCAST]; i++)
244 sp2 = sp;
245 sp = (struct sphere *)malloc(sizeof(struct sphere));
246 if (sp==0) { write(2,"Can't malloc() for sphere space\n",32); break; }
247 lrfill((char*)sp,sizeof(struct sphere)); /* get spheres of annihilation */
248 sp->p=0; /* null out pointer */
249 if (i==0) spheres=sp; /* beginning of list */
250 else sp2->p = sp;
253 time(&zzz);
254 initialtime = zzz-lrint_x();
255 fstat(fd,&filetimes); /* get the creation and modification time of file */
256 lrfill((char*)&zzz,sizeof(long)); zzz += 6;
257 if (filetimes.st_ctime > zzz) fsorry(); /* file create time */
258 else if (filetimes.st_mtime > zzz) fsorry(); /* file modify time */
259 if (c[HP]<0) { died(284); return; } /* died a post mortem death */
261 oldx = oldy = 0;
262 i = lrint_x(); /* inode # */
263 if (i && (filetimes.st_ino!=(unsigned)i)) fsorry();
264 lrclose();
265 if (strcmp(fname,ckpfile) == 0)
267 if (lappend(fname) < 0) fcheat(); else { lprc(' '); lwclose(); }
268 lcreat((char*)0);
270 else if (unlink(fname) < 0) fcheat(); /* can't unlink save file */
271 /* for the greedy cheater checker */
272 for (k=0; k<6; k++) if (c[k]>99) greedy();
273 if (c[HPMAX]>999 || c[SPELLMAX]>125) greedy();
274 if (c[LEVEL]==25 && c[EXPERIENCE]>skill[24]) /* if patch up lev 25 player */
276 long tmp;
277 tmp = c[EXPERIENCE]-skill[24]; /* amount to go up */
278 c[EXPERIENCE] = skill[24];
279 raiseexperience((long)tmp);
281 getlevel(); lasttime=gtime;
285 subroutine to not allow greedy cheaters
287 static void
288 greedy(void)
290 #if WIZID
291 if (wizard) return;
292 #endif
294 lprcat("\n\nI am so sorry, but your character is a little TOO good! Since this\n");
295 lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
296 lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
297 lprcat("to continue.\n"); nap(5000); c[GOLD]=c[BANKACCOUNT]=0; died(-267); return;
301 subroutine to not allow altered save files and terminate the attempted
302 restart
304 static void
305 fsorry(void)
307 lprcat("\nSorry, but your savefile has been altered.\n");
308 lprcat("However, seeing as I am a good sport, I will let you play.\n");
309 lprcat("Be advised though, you won't be placed on the normal scoreboard.");
310 cheat = 1; nap(4000);
314 subroutine to not allow game if save file can't be deleted
316 static void
317 fcheat(void)
319 #if WIZID
320 if (wizard) return;
321 #endif
323 lprcat("\nSorry, but your savefile can't be deleted. This can only mean\n");
324 lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
325 lprcat("is in. Since this is unfair to the rest of the larn community, I\n");
326 lprcat("cannot let you play this game.\n");
327 nap(5000); c[GOLD]=c[BANKACCOUNT]=0; died(-268); return;