Home | History | Annotate | Line # | Download | only in larn
diag.c revision 1.3
      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 
    148 /*
    150 	to save the game in a file
    151  */
    152 static long int zzz=0;
    153 savegame(fname)
    154 	char *fname;
    155 	{
    156 	register int i,k;
    157 	register struct sphere *sp;
    158 	struct stat statbuf;
    159 	nosignal=1;  lflush();	savelevel();
    160 	ointerest();
    161 	if (lcreat(fname) < 0)
    162 		{
    163 		lcreat((char*)0); lprintf("\nCan't open file <%s> to save game\n",fname);
    164 		nosignal=0;  return(-1);
    165 		}
    166 
    167 	set_score_output();
    168 	lwrite((char*)beenhere,MAXLEVEL+MAXVLEVEL);
    169 	for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
    170 		if (beenhere[k])
    171 			lwrite((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
    172 	times(&cputime);	/* get cpu time */
    173 	c[CPUTIME] += (cputime.tms_utime+cputime.tms_stime)/60;
    174 	lwrite((char*)&c[0],100*sizeof(long));
    175 	lprint((long)gtime);		lprc(level);
    176 	lprc(playerx);		lprc(playery);
    177 	lwrite((char*)iven,26);	lwrite((char*)ivenarg,26*sizeof(short));
    178 	for (k=0; k<MAXSCROLL; k++)  lprc(scrollname[k][0]);
    179 	for (k=0; k<MAXPOTION; k++)  lprc(potionname[k][0]);
    180 	lwrite((char*)spelknow,SPNUM);		 lprc(wizard);
    181 	lprc(rmst);		/*	random monster generation counter */
    182 	for (i=0; i<90; i++)	lprc(itm[i].qty);
    183 	lwrite((char*)course,25);			lprc(cheat);		lprc(VERSION);
    184 	for (i=0; i<MAXMONST; i++) lprc(monster[i].genocided); /* genocide info */
    185 	for (sp=spheres; sp; sp=sp->p)
    186 		lwrite((char*)sp,sizeof(struct sphere));	/* save spheres of annihilation */
    187 	time(&zzz);			lprint((long)(zzz-initialtime));
    188 	lwrite((char*)&zzz,sizeof(long));
    189 	if (fstat(lfd,&statbuf)< 0) lprint(0L);
    190 	else lprint((long)statbuf.st_ino); /* inode # */
    191 	lwclose();	lastmonst[0] = 0;
    192 #ifndef VT100
    193 	setscroll();
    194 #endif VT100
    195 	lcreat((char*)0);  nosignal=0;
    196 	return(0);
    197 	}
    198 
    199 restoregame(fname)
    200 	char *fname;
    201 	{
    202 	register int i,k;
    203 	register struct sphere *sp,*sp2;
    204 	struct stat filetimes;
    205 	cursors(); lprcat("\nRestoring . . .");  lflush();
    206 	if (lopen(fname) <= 0)
    207 		{
    208 		lcreat((char*)0); lprintf("\nCan't open file <%s>to restore game\n",fname);
    209 		nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-265); return;
    210 		}
    211 
    212 	lrfill((char*)beenhere,MAXLEVEL+MAXVLEVEL);
    213 	for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
    214 		if (beenhere[k])
    215 			lrfill((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
    216 
    217 	lrfill((char*)&c[0],100*sizeof(long));	gtime = lrint();
    218 	level = c[CAVELEVEL] = lgetc();
    219 	playerx = lgetc();		playery = lgetc();
    220 	lrfill((char*)iven,26);		lrfill((char*)ivenarg,26*sizeof(short));
    221 	for (k=0; k<MAXSCROLL; k++)  scrollname[k] = lgetc() ? scrollhide[k] : "";
    222 	for (k=0; k<MAXPOTION; k++)  potionname[k] = lgetc() ? potionhide[k] : "";
    223 	lrfill((char*)spelknow,SPNUM);		wizard = lgetc();
    224 	rmst = lgetc();			/*	random monster creation flag */
    225 
    226 	for (i=0; i<90; i++)	itm[i].qty = lgetc();
    227 	lrfill((char*)course,25);			cheat = lgetc();
    228 	if (VERSION != lgetc())		/*  version number  */
    229 		{
    230 		cheat=1;
    231 		lprcat("Sorry, But your save file is for an older version of larn\n");
    232 		nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-266); return;
    233 		}
    234 
    235 	for (i=0; i<MAXMONST; i++) monster[i].genocided=lgetc(); /* genocide info */
    236 	for (sp=0,i=0; i<c[SPHCAST]; i++)
    237 		{
    238 		sp2 = sp;
    239 		sp = (struct sphere *)malloc(sizeof(struct sphere));
    240 		if (sp==0) { write(2,"Can't malloc() for sphere space\n",32); break; }
    241 		lrfill((char*)sp,sizeof(struct sphere));	/* get spheres of annihilation */
    242 		sp->p=0;	/* null out pointer */
    243 		if (i==0) spheres=sp;	/* beginning of list */
    244 			else sp2->p = sp;
    245 		}
    246 
    247 	time(&zzz);
    248 	initialtime = zzz-lrint();
    249 	fstat(fd,&filetimes);	/*	get the creation and modification time of file	*/
    250 	lrfill((char*)&zzz,sizeof(long));	zzz += 6;
    251 	if (filetimes.st_ctime > zzz) fsorry();	/*	file create time	*/
    252 	else if (filetimes.st_mtime > zzz) fsorry(); /*	file modify time	*/
    253 	if (c[HP]<0) { died(284); return; }	/* died a post mortem death */
    254 
    255 	oldx = oldy = 0;
    256 	i = lrint();  /* inode # */
    257 	if (i && (filetimes.st_ino!=i)) fsorry();
    258 	lrclose();
    259 	if (strcmp(fname,ckpfile) == 0)
    260 		{
    261 		if (lappend(fname) < 0) fcheat();  else { lprc(' '); lwclose(); }
    262 		lcreat((char*)0);
    263 		}
    264 	else if (unlink(fname) < 0) fcheat(); /* can't unlink save file */
    265 /*	for the greedy cheater checker	*/
    266 	for (k=0; k<6; k++) if (c[k]>99) greedy();
    267 	if (c[HPMAX]>999 || c[SPELLMAX]>125) greedy();
    268 	if (c[LEVEL]==25 && c[EXPERIENCE]>skill[24]) /* if patch up lev 25 player */
    269 		{
    270 		long tmp;
    271 		tmp = c[EXPERIENCE]-skill[24]; /* amount to go up */
    272 		c[EXPERIENCE] = skill[24];
    273 		raiseexperience((long)tmp);
    274 		}
    275 	getlevel();  lasttime=gtime;
    276 	}
    277 
    278 /*
    279 	subroutine to not allow greedy cheaters
    280  */
    281 greedy()
    282 	{
    283 #if WIZID
    284 	if (wizard) return;
    285 #endif
    286 
    287 	lprcat("\n\nI am so sorry, but your character is a little TOO good!  Since this\n");
    288 	lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
    289 	lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
    290 	lprcat("to continue.\n"); nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-267); return;
    291 	}
    292 
    293 /*
    294 	subroutine to not allow altered save files and terminate the attempted
    295 	restart
    296  */
    297 fsorry()
    298 	{
    299 	lprcat("\nSorry, but your savefile has been altered.\n");
    300 	lprcat("However, seeing as I am a good sport, I will let you play.\n");
    301 	lprcat("Be advised though, you won't be placed on the normal scoreboard.");
    302 	cheat = 1;	nap(4000);
    303 	}
    304 
    305 /*
    306 	subroutine to not allow game if save file can't be deleted
    307  */
    308 fcheat()
    309 	{
    310 #if WIZID
    311 	if (wizard) return;
    312 #endif
    313 
    314 	lprcat("\nSorry, but your savefile can't be deleted.  This can only mean\n");
    315 	lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
    316 	lprcat("is in.  Since this is unfair to the rest of the larn community, I\n");
    317 	lprcat("cannot let you play this game.\n");
    318 	nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-268); return;
    319 	}
    320