/*	diag.c		*/
static char copyright[]="Larn is copyrighted 1986 by Noah Morgan.\n";
#include <sys/types.h>
#include <sys/times.h>
#include <sys/stat.h>
#include "header.h"
extern long int initialtime;
extern short playerx,playery,oldx,oldy;
extern char level,cheat,VERSION;
extern short hitp[MAXX][MAXY];
extern int c[],skill[],cbak[],gtime,rmst,maxitm,lasttime;
extern char item[MAXX][MAXY],know[MAXX][MAXY],nosignal;
extern struct cel *cell;
extern struct _itm itm[];
extern struct monst monster[];
extern char objnamelist[],*scrollname[],*potionname[],spelknow[SPNUM],wizard;
extern char mitem[MAXX][MAXY],lastmonst[],course[],*diagfile;
extern char iarg[MAXX][MAXY],beenhere[],iven[],ivenarg[];
extern char *spelcode[],*spelname[],*speldescript[],monstnamelist[],*levelname[];
char lgetc();
static struct stat filetimes;
static struct tms cputime;
extern char *savefilename;		/*	this is the filename for the save file */
extern char *ckpfile;
/*
	***************************
	DIAG -- dungeon diagnostics
	***************************

	subroutine to print out data for debugging
 */
#ifdef EXTRA
static int l,m,n,e,w,s,rndcount[16];
diag()
	{
	register int i,j,k;
	int hit,dam;
	cursor(1,21);  lwclose();
	if (lcreat(diagfile) < 0)	/*	open the diagnostic file	*/
		{
		lcreat(0); lprcat("\ndiagnostic failure\n"); return(-1);
		}

	write(1,"\nDiagnosing . . .\n",18);
	lprcat("\n\nBeginning of DIAG diagnostics ----------\n");

/*	for the character attributes	*/

	lprintf("\n\nPlayer attributes:\n\nHit points: %2d(%2d)",c[HP],c[HPMAX]);
	lprintf("\ngold: %d  Experience: %d  Character level: %d  Level in caverns: %d",
		c[GOLD],c[EXPERIENCE],c[LEVEL],level);
	lprintf("\nTotal types of monsters: %d",MAXMONST+8);

	lprcat("\f\nHere's the dungeon:\n\n");

	i=level;
	for (j=0; j<MAXLEVEL+MAXVLEVEL; j++)
		{
		newcavelevel(j);
		lprintf("\nMaze for level %s:\n",levelname[level]);
		diagdrawscreen();
		}
	newcavelevel(i);

	lprcat("\f\nNow for the monster data:\n\n");
	lprcat("   Monster Name      LEV  AC   DAM  ATT  DEF    GOLD   HP     EXP   \n");
	lprcat("--------------------------------------------------------------------------\n");
	for (i=0; i<=MAXMONST+8; i++)
		{
		lprintf("%19s  %2d  %3d ",monster[i].name,monster[i].level,monster[i].armorclass);
		lprintf(" %3d  %3d  %3d  ",monster[i].damage,monster[i].attack,monster[i].defense);
		lprintf("%6d  %3d   %6d\n",monster[i].gold,monster[i].hitpoints,monster[i].experience);
		}

	lprcat("\n\nHere's a Table for the to hit percentages\n");
	lprcat("\n     We will be assuming that players level = 2 * monster level");
	lprcat("\n     and that the players dexterity and strength are 16.");
	lprcat("\n    to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
	lprcat("\n    damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
	lprcat("\n    to hit:  if rnd(22) < to hit  then player hits\n");
	lprcat("\n    Each entry is as follows:  to hit / damage / number hits to kill\n");
	lprcat("\n          monster     WC = 4         WC = 20        WC = 40");
	lprcat("\n---------------------------------------------------------------");
	for (i=0; i<=MAXMONST+8; i++)
		{
		hit = 2*monster[i].armorclass+2*monster[i].level+16;
		dam = 16 - c[HARDGAME];
		lprintf("\n%20s   %2d/%2d/%2d       %2d/%2d/%2d       %2d/%2d/%2d",
					monster[i].name,
					hit/2,max(0,dam+2),monster[i].hitpoints/(dam+2)+1,
					(hit+2)/2,max(0,dam+10),monster[i].hitpoints/(dam+10)+1,
					(hit+5)/2,max(0,dam+20),monster[i].hitpoints/(dam+20)+1);
		}

	lprcat("\n\nHere's the list of available potions:\n\n");
	for (i=0; i<MAXPOTION; i++)	lprintf("%20s\n",&potionname[i][1]);
	lprcat("\n\nHere's the list of available scrolls:\n\n");
	for (i=0; i<MAXSCROLL; i++)	lprintf("%20s\n",&scrollname[i][1]);
	lprcat("\n\nHere's the spell list:\n\n");
	lprcat("spell          name           desctiption\n");
	lprcat("-------------------------------------------------------------------------------------------\n\n");
	for (j=0; j<SPNUM; j++)
		{
		lprc(' ');	lprcat(spelcode[j]);
		lprintf(" %21s  %s\n",spelname[j],speldescript[j]); 
		}

	lprcat("\n\nFor the c[] array:\n");
	for (j=0; j<100; j+=10)
		{
		lprintf("\nc[%2d] = ",j); for (i=0; i<9; i++) lprintf("%5d ",c[i+j]);
		}

	lprcat("\n\nTest of random number generator ----------------");
	lprcat("\n    for 25,000 calls divided into 16 slots\n\n");

	for (i=0; i<16; i++)  rndcount[i]=0;
	for (i=0; i<25000; i++)	rndcount[rund(16)]++;
	for (i=0; i<16; i++)  { lprintf("  %5d",rndcount[i]); if (i==7) lprc('\n'); }

	lprcat("\n\n");			lwclose();
	lcreat(0);		lprcat("Done Diagnosing . . .");
	}
/*
	subroutine to count the number of occurrances of an object
 */
dcount(l)
	int l;
	{
	register int i,j,p;
	int k;
	k=0;
	for (i=0; i<MAXX; i++)
		for (j=0; j<MAXY; j++)
			for (p=0; p<MAXLEVEL; p++)
				if (cell[p*MAXX*MAXY+i*MAXY+j].item == l) k++;
	return(k);
	}

/*
	subroutine to draw the whole screen as the player knows it
 */
diagdrawscreen()
	{
	register int i,j,k;

	for (i=0; i<MAXY; i++)

/*	for the east west walls of this line	*/
		{
		for (j=0; j<MAXX; j++)	if (k=mitem[j][i]) lprc(monstnamelist[k]); else
								lprc(objnamelist[item[j][i]]);
		lprc('\n');
		}
	}
#endif

/*
	to save the game in a file
 */
static long int zzz=0;
savegame(fname)
	char *fname;
	{
	register int i,j,k;
	nosignal=1;  lflush();	savelevel();
	ointerest();
	if (lcreat(fname) < 0)
		{
		lcreat(0); lprintf("\nCan't open file <%s> to save game\n",fname);
		nosignal=0;  return(-1);
		}

	lwrite(beenhere,MAXLEVEL+MAXVLEVEL);
	for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
		if (beenhere[k])	lwrite(&cell[k*MAXX*MAXY],6*MAXY*MAXX);
	times(&cputime);	/* get cpu time */
	c[CPUTIME] += (cputime.tms_utime+cputime.tms_stime)/60;
	lwrite(&c[0],100*sizeof(int));
	lprint(gtime);		lprc(level);
	lprc(playerx);		lprc(playery);
	lwrite(iven,26);	lwrite(ivenarg,26);
	for (k=0; k<MAXSCROLL; k++)  lprc(scrollname[k][0]);
	for (k=0; k<MAXPOTION; k++)  lprc(potionname[k][0]);
	lwrite(spelknow,SPNUM);		 lprc(wizard);
	lprc(rmst);		/*	random monster generation counter */
	for (i=0; i<90; i++)	lprc(itm[i].qty);
	lwrite(course,25);			lprc(cheat);		lprc(VERSION);
	time(&zzz);			lprint(zzz-initialtime);
	lwrite(&zzz,8);
	lwclose();	lastmonst[0] = 0;
	lcreat(0);  nosignal=0;
	}

restoregame(fname)
	char *fname;
	{
	register int i,j,k;
	cursor(1,21); lprcat("\nRestoring game . . .");  lflush();
	if (lopen(fname) <= 0)
		{
		lcreat(0); lprintf("\nCan't open file <%s>to restore game\n",fname);
		nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-265);
		}

	lrfill(beenhere,MAXLEVEL+MAXVLEVEL);
	for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
		if (beenhere[k])	lrfill(&cell[k*MAXX*MAXY],6*MAXY*MAXX);

	lrfill(&c[0],400);	gtime = lrint();	level = c[CAVELEVEL] = lgetc();
	playerx = lgetc();		playery = lgetc();
	lrfill(iven,26);		lrfill(ivenarg,26);
	for (k=0; k<MAXSCROLL; k++)  scrollname[k][0] = lgetc();
	for (k=0; k<MAXPOTION; k++)  potionname[k][0] = lgetc();
	lrfill(spelknow,SPNUM);		wizard = lgetc();
	rmst = lgetc();			/*	random monster creation flag */
	for (i=0; i<90; i++)	itm[i].qty = lgetc();
	lrfill(course,25);			cheat = lgetc();
	if (VERSION != lgetc())		/*  version number  */
		{
		lprintf("Sorry, But your save file is for an older version of larn\n");
		nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-266);
		}
	time(&zzz);
	initialtime = zzz-lrint();
	stat(fname,&filetimes);	/*	get the creation and modification time of file	*/
	lrfill(&zzz,8);	zzz += 6;
	if (filetimes.st_ctime > zzz) fsorry();	/*	file create time	*/
	else if (filetimes.st_mtime > zzz) fsorry(); /*	file modify time	*/
	if (c[HP]<0) died(284);	/* died a post mortem death */

	oldx = oldy = 0;
	lrclose();
	if (strcmp(fname,ckpfile) == 0)
		{
		if (lappend(fname) < 0) fcheat();  else { lprc(' '); lwclose(); }
		lcreat(0);
		}
	else if (unlink(fname) < 0) fcheat(); /* cant unlink save file */
/*	for the greedy cheater checker	*/
	for (k=0; k<6; k++) if (c[k]>99) greedy();
	if (c[HPMAX]>999 || c[SPELLMAX]>125) greedy();
	if (c[LEVEL]==25 && c[EXPERIENCE]>skill[24]) /* if patch up lev 25 player */
		{
		int tmp;
		tmp = c[EXPERIENCE]-skill[24]; /* amount to go up */
		c[EXPERIENCE] = skill[24];
		raiseexperience(tmp);
		}
	getlevel();  lasttime=gtime;
	}

/*
	subroutine to not allow greedy cheaters
 */
greedy()
	{
#ifdef WIZZARD
	if (wizard) return;
#endif

	lprcat("\n\nI am so sorry, but your character is a little TOO good!  Since this\n");
	lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
	lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
	lprcat("to continue.\n"); nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-267);
	}

/*
	subroutine to not allow altered save files and terminate the attempted
	restart
 */
fsorry()
	{
	lprcat("\nSorry, but your savefile has been altered.\n");
	lprcat("However, seeing as I am a good sport, I will let you play.\n");
	lprcat("Be advised though, you won't be placed on the normal scoreboard.");
	cheat = 1;	nap(4000);
	}

/*
	subroutine to not allow game if save file cant be deleted
 */
fcheat()
	{
#ifdef WIZZARD
	if (wizard) return;
#endif

	lprcat("\nSorry, but your savefile can't be deleted.  This can only mean\n");
	lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
	lprcat("is in.  Since this is unfair to the rest of the larn community, I\n");
	lprcat("cannot let you play this game.\n");
	nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-268);
	}
