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