summaryrefslogtreecommitdiffstats
path: root/larn/diag.c
blob: 0f00a6f1677c8bb5a2dac1afdf415d6f5e8ad9dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*	$NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $	*/

/* diag.c		Larn is copyrighted 1986 by Noah Morgan. */
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: diag.c,v 1.13 2012/06/19 05:30:43 dholland Exp $");
#endif				/* not lint */

#include <sys/types.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "header.h"
#include "extern.h"

static void greedy(void);
static void fsorry(void);
static void fcheat(void);

static struct tms cputime;
time_t time(time_t *t);

/*
	***************************
	DIAG -- dungeon diagnostics
	***************************

	subroutine to print out data for debugging
 */
#ifdef EXTRA
static int      rndcount[16];
void
diag()
{
	int    i, j;
	int             hit, dam;
	cursors();
	lwclose();
	if (lcreat(diagfile) < 0) {	/* open the diagnostic file	 */
		lcreat((char *) 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: %2ld(%2ld)", (long) c[HP], (long) c[HPMAX]);
	lprintf("\ngold: %ld  Experience: %ld  Character level: %ld  Level in caverns: %ld",
	(long) c[GOLD], (long) c[EXPERIENCE], (long) c[LEVEL], (long) level);
	lprintf("\nTotal types of monsters: %ld", (long) 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  %2ld  %3ld ", monster[i].name, (long) monster[i].level, (long) monster[i].armorclass);
		lprintf(" %3ld  %3ld  %3ld  ", (long) monster[i].damage, (long) monster[i].attack, (long) monster[i].defense);
		lprintf("%6ld  %3ld   %6ld\n", (long) monster[i].gold, (long) monster[i].hitpoints, (long) 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   %2ld/%2ld/%2ld       %2ld/%2ld/%2ld       %2ld/%2ld/%2ld",
			monster[i].name,
			(long) (hit / 2), (long) max(0, dam + 2), (long) (monster[i].hitpoints / (dam + 2) + 1),
			(long) ((hit + 2) / 2), (long) max(0, dam + 10), (long) (monster[i].hitpoints / (dam + 10) + 1),
			(long) ((hit + 5) / 2), (long) max(0, dam + 20), (long) (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", &potionhide[i][1]);
	lprcat("\n\nHere's the list of available scrolls:\n\n");
	for (i = 0; i < MAXSCROLL; i++)
		lprintf("%20s\n", &scrollhide[i][1]);
	lprcat("\n\nHere's the spell list:\n\n");
	lprcat("spell          name           description\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[%2ld] = ", (long) j);
		for (i = 0; i < 9; i++)
			lprintf("%5ld ", (long) 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("  %5ld", (long) rndcount[i]);
		if (i == 7)
			lprc('\n');
	}

	lprcat("\n\n");
	lwclose();
	lcreat((char *) 0);
	lprcat("Done Diagnosing . . .");
	return (0);
}
/*
	subroutine to count the number of occurrences of an object
 */
int
dcount(l)
	int l;
{
	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
 */
void
diagdrawscreen()
{
	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 time_t   zzz = 0;
int
savegame(char *fname)
{
	int    i, k;
	struct sphere *sp;
	struct stat     statbuf;

	nosignal = 1;
	lflush();
	savelevel();
	ointerest();
	if (lcreat(fname) < 0) {
		lcreat((char *) 0);
		lprintf("\nCan't open file <%s> to save game\n", fname);
		nosignal = 0;
		return (-1);
	}
	set_score_output();
	lwrite((char *) beenhere, MAXLEVEL + MAXVLEVEL);
	for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
		if (beenhere[k])
			lwrite((char *) &cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);
	times(&cputime);	/* get cpu time */
	c[CPUTIME] += (cputime.tms_utime + cputime.tms_stime) / 60;
	lwrite((char *) &c[0], 100 * sizeof(long));
	lprint((long) gltime);
	lprc(level);
	lprc(playerx);
	lprc(playery);
	lwrite((char *) iven, 26);
	lwrite((char *) ivenarg, 26 * sizeof(short));
	for (k = 0; k < MAXSCROLL; k++)
		lprc(scrollname[k][0]);
	for (k = 0; k < MAXPOTION; k++)
		lprc(potionname[k][0]);
	lwrite((char *) spelknow, SPNUM);
	lprc(wizard);
	lprc(rmst);		/* random monster generation counter */
	for (i = 0; i < 90; i++)
		lprc(itm[i].qty);
	lwrite((char *) course, 25);
	lprc(cheat);
	lprc(VERSION);
	for (i = 0; i < MAXMONST; i++)
		lprc(monster[i].genocided);	/* genocide info */
	for (sp = spheres; sp; sp = sp->p)
		lwrite((char *) sp, sizeof(struct sphere));	/* save spheres of
								 * annihilation */
	time(&zzz);
	lprint((long) (zzz - initialtime));
	lwrite((char *) &zzz, sizeof(long));
	if (fstat(io_outfd, &statbuf) < 0)
		lprint(0L);
	else
		lprint((long) statbuf.st_ino);	/* inode # */
	lwclose();
	lastmonst[0] = 0;
#ifndef VT100
	setscroll();
#endif	/* VT100 */
	lcreat((char *) 0);
	nosignal = 0;
	return (0);
}

void
restoregame(char *fname)
{
	int    i, k;
	struct sphere *sp, *sp2;
	struct stat     filetimes;
	cursors();
	lprcat("\nRestoring . . .");
	lflush();
	if (lopen(fname) <= 0) {
		lcreat((char *) 0);
		lprintf("\nCan't open file <%s>to restore game\n", fname);
		nap(2000);
		c[GOLD] = c[BANKACCOUNT] = 0;
		died(-265);
		return;
	}
	lrfill((char *) beenhere, MAXLEVEL + MAXVLEVEL);
	for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
		if (beenhere[k])
			lrfill((char *) &cell[k * MAXX * MAXY], sizeof(struct cel) * MAXY * MAXX);

	lrfill((char *) &c[0], 100 * sizeof(long));
	gltime = larn_lrint();
	level = c[CAVELEVEL] = lgetc();
	playerx = lgetc();
	playery = lgetc();
	lrfill((char *) iven, 26);
	lrfill((char *) ivenarg, 26 * sizeof(short));
	for (k = 0; k < MAXSCROLL; k++)
		scrollname[k] = lgetc() ? scrollhide[k] : "";
	for (k = 0; k < MAXPOTION; k++)
		potionname[k] = lgetc() ? potionhide[k] : "";
	lrfill((char *) spelknow, SPNUM);
	wizard = lgetc();
	rmst = lgetc();		/* random monster creation flag */

	for (i = 0; i < 90; i++)
		itm[i].qty = lgetc();
	lrfill((char *) course, 25);
	cheat = lgetc();
	if (VERSION != lgetc()) {	/* version number  */
		cheat = 1;
		lprcat("Sorry, But your save file is for an older version of larn\n");
		nap(2000);
		c[GOLD] = c[BANKACCOUNT] = 0;
		died(-266);
		return;
	}
	for (i = 0; i < MAXMONST; i++)
		monster[i].genocided = lgetc();	/* genocide info */
	for (sp = 0, i = 0; i < c[SPHCAST]; i++) {
		sp2 = sp;
		sp = (struct sphere *) malloc(sizeof(struct sphere));
		if (sp == 0) {
			write(2, "Can't malloc() for sphere space\n", 32);
			break;
		}
		lrfill((char *) sp, sizeof(struct sphere));	/* get spheres of
								 * annihilation */
		sp->p = 0;	/* null out pointer */
		if (i == 0)
			spheres = sp;	/* beginning of list */
		else
			sp2->p = sp;
	}

	time(&zzz);
	initialtime = zzz - larn_lrint();
	/* get the creation and modification time of file */
	fstat(io_infd, &filetimes);
	lrfill((char *) &zzz, sizeof(long));
	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);
		return;
	}			/* died a post mortem death */
	oldx = oldy = 0;
	/* XXX the following will break on 64-bit inode numbers */
	i = larn_lrint();		/* inode # */
	if (i && (filetimes.st_ino != (ino_t) i))
		fsorry();
	lrclose();
	if (strcmp(fname, ckpfile) == 0) {
		if (lappend(fname) < 0)
			fcheat();
		else {
			lprc(' ');
			lwclose();
		}
		lcreat((char *) 0);
	} else if (unlink(fname) < 0)
		fcheat();	/* can't 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 */
		long            tmp;
		tmp = c[EXPERIENCE] - skill[24];	/* amount to go up */
		c[EXPERIENCE] = skill[24];
		raiseexperience((long) tmp);
	}
	getlevel();
	lasttime = gltime;
}

/*
	subroutine to not allow greedy cheaters
 */
static void
greedy(void)
{
#if WIZID
	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);
	return;
}

/*
	subroutine to not allow altered save files and terminate the attempted
	restart
 */
static void
fsorry(void)
{
	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 can't be deleted
 */
static void
fcheat(void)
{
#if WIZID
	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);
	return;
}