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