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