summaryrefslogtreecommitdiffstats
path: root/hack/hack.dog.c
blob: a76e7ffe94fdb377e92712f8e051e2a219360d78 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*	$NetBSD: hack.dog.c,v 1.12 2011/08/07 06:03:45 dholland Exp $	*/

/*
 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
 * Amsterdam
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Stichting Centrum voor Wiskunde en
 * Informatica, nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior
 * written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: hack.dog.c,v 1.12 2011/08/07 06:03:45 dholland Exp $");
#endif				/* not lint */

#include "hack.h"
#include "extern.h"
#include "hack.mfndpos.h"
#include "def.edog.h"
#include "def.mkroom.h"

const struct permonst li_dog =
{"little dog", 'd', 2, 18, 6, 1, 6, sizeof(struct edog)};
const struct permonst dog =
{"dog", 'd', 4, 16, 5, 1, 6, sizeof(struct edog)};
const struct permonst la_dog =
{"large dog", 'd', 6, 15, 4, 2, 4, sizeof(struct edog)};

static void initedog(struct monst *);
static int dogfood(struct obj *);

void
makedog(void)
{
	struct monst   *mtmp = makemon(&li_dog, u.ux, u.uy);
	if (!mtmp)
		return;		/* dogs were genocided */
	initedog(mtmp);
}

static void
initedog(struct monst *mtmp)
{
	mtmp->mtame = mtmp->mpeaceful = 1;
	EDOG(mtmp)->hungrytime = 1000 + moves;
	EDOG(mtmp)->eattime = 0;
	EDOG(mtmp)->droptime = 0;
	EDOG(mtmp)->dropdist = 10000;
	EDOG(mtmp)->apport = 10;
	EDOG(mtmp)->whistletime = 0;
}

/* attach the monsters that went down (or up) together with @ */
struct monst   *mydogs = 0;
struct monst   *fallen_down = 0;/* monsters that fell through a trapdoor */
/* they will appear on the next level @ goes to, even if he goes up! */

void
losedogs(void)
{
	struct monst   *mtmp;
	while ((mtmp = mydogs) != NULL) {
		mydogs = mtmp->nmon;
		mtmp->nmon = fmon;
		fmon = mtmp;
		mnexto(mtmp);
	}
	while ((mtmp = fallen_down) != NULL) {
		fallen_down = mtmp->nmon;
		mtmp->nmon = fmon;
		fmon = mtmp;
		rloc(mtmp);
	}
}

void
keepdogs(void)
{
	struct monst   *mtmp;
	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
		if (dist(mtmp->mx, mtmp->my) < 3 && follower(mtmp)
		    && !mtmp->msleep && !mtmp->mfroz) {
			relmon(mtmp);
			mtmp->nmon = mydogs;
			mydogs = mtmp;
			unpmon(mtmp);
			keepdogs();	/* we destroyed the link, so use
					 * recursion */
			return;	/* (admittedly somewhat primitive) */
		}
}

void
fall_down(struct monst *mtmp)
{
	relmon(mtmp);
	mtmp->nmon = fallen_down;
	fallen_down = mtmp;
	unpmon(mtmp);
	mtmp->mtame = 0;
}

/* return quality of food; the lower the better */
#define	DOGFOOD	0
#define	CADAVER	1
#define	ACCFOOD	2
#define	MANFOOD	3
#define	APPORT	4
#define	POISON	5
#define	UNDEF	6
static int
dogfood(struct obj *obj)
{
	switch (obj->olet) {
	case FOOD_SYM:
		return (
			(obj->otyp == TRIPE_RATION) ? DOGFOOD :
			(obj->otyp < CARROT) ? ACCFOOD :
			(obj->otyp < CORPSE) ? MANFOOD :
			(poisonous(obj) || obj->age + 50 <= moves ||
			 obj->otyp == DEAD_COCKATRICE)
			? POISON : CADAVER
			);
	default:
		if (!obj->cursed)
			return (APPORT);
		/* FALLTHROUGH */
	case BALL_SYM:
	case CHAIN_SYM:
	case ROCK_SYM:
		return (UNDEF);
	}
}

/* return 0 (no move), 1 (move) or 2 (dead) */
int
dog_move(struct monst *mtmp, int after)
{
	int             nx, ny, omx, omy, appr, nearer, j;
	int             udist, chi = 0, i, whappr;
	struct monst   *mtmp2;
	const struct permonst *mdat = mtmp->data;
	struct edog    *edog = EDOG(mtmp);
	struct obj     *obj;
	struct trap    *trap;
	xchar           cnt, chcnt, nix, niy;
	schar           dogroom, uroom;
	xchar           gx = 0, gy = 0, gtyp, otyp;	/* current goal */
	coord           poss[9];
	int             info[9];
#define GDIST(x,y) ((x-gx)*(x-gx) + (y-gy)*(y-gy))
#define DDIST(x,y) ((x-omx)*(x-omx) + (y-omy)*(y-omy))

	if (moves <= edog->eattime)
		return (0);	/* dog is still eating */
	omx = mtmp->mx;
	omy = mtmp->my;
	whappr = (moves - EDOG(mtmp)->whistletime < 5);
	if (moves > edog->hungrytime + 500 && !mtmp->mconf) {
		mtmp->mconf = 1;
		mtmp->mhpmax /= 3;
		if (mtmp->mhp > mtmp->mhpmax)
			mtmp->mhp = mtmp->mhpmax;
		if (cansee(omx, omy))
			pline("%s is confused from hunger.", Monnam(mtmp));
		else
			pline("You feel worried about %s.", monnam(mtmp));
	} else if (moves > edog->hungrytime + 750 || mtmp->mhp < 1) {
		if (cansee(omx, omy))
			pline("%s dies from hunger.", Monnam(mtmp));
		else
			pline("You have a sad feeling for a moment, then it passes.");
		mondied(mtmp);
		return (2);
	}
	dogroom = inroom(omx, omy);
	uroom = inroom(u.ux, u.uy);
	udist = dist(omx, omy);

	/* maybe we tamed him while being swallowed --jgm */
	if (!udist)
		return (0);

	/* if we are carrying sth then we drop it (perhaps near @) */
	/* Note: if apport == 1 then our behaviour is independent of udist */
	if (mtmp->minvent) {
		if (!rn2(udist) || !rn2((int) edog->apport))
			if ((unsigned) rn2(10) < edog->apport) {
				relobj(mtmp, (int) mtmp->minvis);
				if (edog->apport > 1)
					edog->apport--;
				edog->dropdist = udist;	/* hpscdi!jon */
				edog->droptime = moves;
			}
	} else {
		if ((obj = o_at(omx, omy)) != NULL)
			if (!strchr("0_", obj->olet)) {
				if ((otyp = dogfood(obj)) <= CADAVER) {
					nix = omx;
					niy = omy;
					goto eatobj;
				}
				if (obj->owt < 10 * mtmp->data->mlevel)
					if ((unsigned) rn2(20) < edog->apport + 3)
						if (rn2(udist) || !rn2((int) edog->apport)) {
							freeobj(obj);
							unpobj(obj);
							/*
							 * if(levl[omx][omy].s
							 * crsym ==
							 * obj->olet)
							 * newsym(omx,omy);
							 */
							mpickobj(mtmp, obj);
						}
			}
	}

	/* first we look for food */
	gtyp = UNDEF;		/* no goal as yet */
#ifdef LINT
	gx = gy = 0;		/* suppress 'used before set' message */
#endif	/* LINT */
	for (obj = fobj; obj; obj = obj->nobj) {
		otyp = dogfood(obj);
		if (otyp > gtyp || otyp == UNDEF)
			continue;
		if (inroom(obj->ox, obj->oy) != dogroom)
			continue;
		if (otyp < MANFOOD &&
		    (dogroom >= 0 || DDIST(obj->ox, obj->oy) < 10)) {
			if (otyp < gtyp || (otyp == gtyp &&
				 DDIST(obj->ox, obj->oy) < DDIST(gx, gy))) {
				gx = obj->ox;
				gy = obj->oy;
				gtyp = otyp;
			}
		} else if (gtyp == UNDEF && dogroom >= 0 &&
			   uroom == dogroom &&
			   !mtmp->minvent && edog->apport > (unsigned)rn2(8)) {
			gx = obj->ox;
			gy = obj->oy;
			gtyp = APPORT;
		}
	}
	if (gtyp == UNDEF ||
	  (gtyp != DOGFOOD && gtyp != APPORT && moves < edog->hungrytime)) {
		if (dogroom < 0 || dogroom == uroom) {
			gx = u.ux;
			gy = u.uy;
#ifndef QUEST
		} else {
			int             tmp = rooms[dogroom].fdoor;
			cnt = rooms[dogroom].doorct;

			gx = gy = FAR;	/* random, far away */
			while (cnt--) {
				if (dist(gx, gy) >
				    dist(doors[tmp].x, doors[tmp].y)) {
					gx = doors[tmp].x;
					gy = doors[tmp].y;
				}
				tmp++;
			}
			/* here gx == FAR e.g. when dog is in a vault */
			if (gx == FAR || (gx == omx && gy == omy)) {
				gx = u.ux;
				gy = u.uy;
			}
#endif	/* QUEST */
		}
		appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
		if (after && udist <= 4 && gx == u.ux && gy == u.uy)
			return (0);
		if (udist > 1) {
			if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) ||
			    whappr ||
			    (mtmp->minvent && rn2((int) edog->apport)))
				appr = 1;
		}
		/* if you have dog food he'll follow you more closely */
		if (appr == 0) {
			obj = invent;
			while (obj) {
				if (obj->otyp == TRIPE_RATION) {
					appr = 1;
					break;
				}
				obj = obj->nobj;
			}
		}
	} else
		appr = 1;	/* gtyp != UNDEF */
	if (mtmp->mconf)
		appr = 0;

	if (gx == u.ux && gy == u.uy && (dogroom != uroom || dogroom < 0)) {
		coord          *cp;
		cp = gettrack(omx, omy);
		if (cp) {
			gx = cp->x;
			gy = cp->y;
		}
	}
	nix = omx;
	niy = omy;
	cnt = mfndpos(mtmp, poss, info, ALLOW_M | ALLOW_TRAPS);
	chcnt = 0;
	chi = -1;
	for (i = 0; i < cnt; i++) {
		nx = poss[i].x;
		ny = poss[i].y;
		if (info[i] & ALLOW_M) {
			mtmp2 = m_at(nx, ny);
			if (mtmp2 == NULL)
				panic("error in dog_move");
			if (mtmp2->data->mlevel >= mdat->mlevel + 2 ||
			    mtmp2->data->mlet == 'c')
				continue;
			if (after)
				return (0);	/* hit only once each move */

			if (hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
			    mtmp2->mlstmv != moves &&
			    hitmm(mtmp2, mtmp) == 2)
				return (2);
			return (0);
		}
		/* dog avoids traps */
		/* but perhaps we have to pass a trap in order to follow @ */
		if ((info[i] & ALLOW_TRAPS) && (trap = t_at(nx, ny))) {
			if (!trap->tseen && rn2(40))
				continue;
			if (rn2(10))
				continue;
		}
		/* dog eschewes cursed objects */
		/* but likes dog food */
		obj = fobj;
		while (obj) {
			if (obj->ox != nx || obj->oy != ny)
				goto nextobj;
			if (obj->cursed)
				goto nxti;
			if (obj->olet == FOOD_SYM &&
			    (otyp = dogfood(obj)) < MANFOOD &&
			    (otyp < ACCFOOD || edog->hungrytime <= moves)) {
				/*
				 * Note: our dog likes the food so much that
				 * he might eat it even when it conceals a
				 * cursed object
				 */
				nix = nx;
				niy = ny;
				chi = i;
		eatobj:
				edog->eattime =
					moves + obj->quan * objects[obj->otyp].oc_delay;
				if (edog->hungrytime < moves)
					edog->hungrytime = moves;
				edog->hungrytime +=
					5 * obj->quan * objects[obj->otyp].nutrition;
				mtmp->mconf = 0;
				if (cansee(nix, niy))
					pline("%s ate %s.", Monnam(mtmp), doname(obj));
				/* perhaps this was a reward */
				if (otyp != CADAVER)
					edog->apport += 200 / (edog->dropdist + moves - edog->droptime);
				delobj(obj);
				goto newdogpos;
			}
	nextobj:
			obj = obj->nobj;
		}

		for (j = 0; j < MTSZ && j < cnt - 1; j++)
			if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
				if (rn2(4 * (cnt - j)))
					goto nxti;

		/*
		 * Some stupid C compilers cannot compute the whole
		 * expression at once.
		 */
		nearer = GDIST(nx, ny);
		nearer -= GDIST(nix, niy);
		nearer *= appr;
		if ((nearer == 0 && !rn2(++chcnt)) || nearer < 0 ||
		    (nearer > 0 && !whappr &&
		     ((omx == nix && omy == niy && !rn2(3))
		      || !rn2(12))
		     )) {
			nix = nx;
			niy = ny;
			if (nearer < 0)
				chcnt = 0;
			chi = i;
		}
nxti:		;
	}
newdogpos:
	if (nix != omx || niy != omy) {
		if (info[chi] & ALLOW_U) {
			(void) hitu(mtmp, d(mdat->damn, mdat->damd) + 1);
			return (0);
		}
		mtmp->mx = nix;
		mtmp->my = niy;
		for (j = MTSZ - 1; j > 0; j--)
			mtmp->mtrack[j] = mtmp->mtrack[j - 1];
		mtmp->mtrack[0].x = omx;
		mtmp->mtrack[0].y = omy;
	}
	if (mintrap(mtmp) == 2)	/* he died */
		return (2);
	pmon(mtmp);
	return (1);
}

/* return roomnumber or -1 */
int
inroom(xchar x, xchar y)
{
#ifndef QUEST
	int pos = 0;

	while (rooms[pos].hx >= 0) {
		if (rooms[pos].hx >= x - 1 && rooms[pos].lx <= x + 1 &&
		    rooms[pos].hy >= y - 1 && rooms[pos].ly <= y + 1)
			return pos;
		pos++;
	}
#endif	/* QUEST */
	return (-1);		/* not in room or on door */
}

int
tamedog(struct monst *mtmp, struct obj *obj)
{
	struct monst   *mtmp2;

	if (flags.moonphase == FULL_MOON && night() && rn2(6))
		return (0);

	/* If we cannot tame him, at least he's no longer afraid. */
	mtmp->mflee = 0;
	mtmp->mfleetim = 0;
	if (mtmp->mtame || mtmp->mfroz ||
#ifndef NOWORM
	    mtmp->wormno ||
#endif	/* NOWORM */
	    mtmp->isshk || mtmp->isgd || strchr(" &@12", mtmp->data->mlet))
		return (0);	/* no tame long worms? */
	if (obj) {
		if (dogfood(obj) >= MANFOOD)
			return (0);
		if (cansee(mtmp->mx, mtmp->my)) {
			pline("%s devours the %s.", Monnam(mtmp),
			      objects[obj->otyp].oc_name);
		}
		obfree(obj, (struct obj *) 0);
	}
	mtmp2 = newmonst(sizeof(struct edog) + mtmp->mnamelth);
	*mtmp2 = *mtmp;
	mtmp2->mxlth = sizeof(struct edog);
	if (mtmp->mnamelth)
		(void) strcpy(NAME(mtmp2), NAME(mtmp));
	initedog(mtmp2);
	replmon(mtmp, mtmp2);
	return (1);
}