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