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