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