xref: /netbsd-src/games/hack/hack.mon.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: hack.mon.c,v 1.8 2008/01/28 06:55:41 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.mon.c,v 1.8 2008/01/28 06:55:41 dholland Exp $");
67 #endif				/* not lint */
68 
69 #include <stdlib.h>
70 #include "hack.h"
71 #include "extern.h"
72 #include "hack.mfndpos.h"
73 
74 #ifndef NULL
75 #define	NULL	(char *) 0
76 #endif
77 
78 int             warnlevel;	/* used by movemon and dochugw */
79 long            lastwarntime;
80 int             lastwarnlev;
81 const char           *const warnings[] = {
82 	"white", "pink", "red", "ruby", "purple", "black"
83 };
84 
85 void
86 movemon()
87 {
88 	struct monst   *mtmp;
89 	int             fr;
90 
91 	warnlevel = 0;
92 
93 	while (1) {
94 		/* find a monster that we haven't treated yet */
95 		/*
96 		 * note that mtmp or mtmp->nmon might get killed while mtmp
97 		 * moves, so we cannot just walk down the chain (even new
98 		 * monsters might get created!)
99 		 */
100 		for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
101 			if (mtmp->mlstmv < moves)
102 				goto next_mon;
103 		/* treated all monsters */
104 		break;
105 
106 next_mon:
107 		mtmp->mlstmv = moves;
108 
109 		/* most monsters drown in pools */
110 		{
111 			boolean         inpool, iseel;
112 
113 			inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
114 			iseel = (mtmp->data->mlet == ';');
115 			if (inpool && !iseel) {
116 				if (cansee(mtmp->mx, mtmp->my))
117 					pline("%s drowns.", Monnam(mtmp));
118 				mondead(mtmp);
119 				continue;
120 			}
121 			/* but eels have a difficult time outside */
122 			if (iseel && !inpool) {
123 				if (mtmp->mhp > 1)
124 					mtmp->mhp--;
125 				mtmp->mflee = 1;
126 				mtmp->mfleetim += 2;
127 			}
128 		}
129 		if (mtmp->mblinded && !--mtmp->mblinded)
130 			mtmp->mcansee = 1;
131 		if (mtmp->mfleetim && !--mtmp->mfleetim)
132 			mtmp->mflee = 0;
133 		if (mtmp->mimic)
134 			continue;
135 		if (mtmp->mspeed != MSLOW || !(moves % 2)) {
136 			/* continue if the monster died fighting */
137 			fr = -1;
138 			if (Conflict && cansee(mtmp->mx, mtmp->my)
139 			    && (fr = fightm(mtmp)) == 2)
140 				continue;
141 			if (fr < 0 && dochugw(mtmp))
142 				continue;
143 		}
144 		if (mtmp->mspeed == MFAST && dochugw(mtmp))
145 			continue;
146 	}
147 
148 	warnlevel -= u.ulevel;
149 	if (warnlevel >= SIZE(warnings))
150 		warnlevel = SIZE(warnings) - 1;
151 	if (warnlevel >= 0)
152 		if (warnlevel > lastwarnlev || moves > lastwarntime + 5) {
153 			const char           *rr;
154 			switch (Warning & (LEFT_RING | RIGHT_RING)) {
155 			case LEFT_RING:
156 				rr = "Your left ring glows";
157 				break;
158 			case RIGHT_RING:
159 				rr = "Your right ring glows";
160 				break;
161 			case LEFT_RING | RIGHT_RING:
162 				rr = "Both your rings glow";
163 				break;
164 			default:
165 				rr = "Your fingertips glow";
166 				break;
167 			}
168 			pline("%s %s!", rr, warnings[warnlevel]);
169 			lastwarntime = moves;
170 			lastwarnlev = warnlevel;
171 		}
172 	dmonsfree();		/* remove all dead monsters */
173 }
174 
175 void
176 justswld(mtmp, name)
177 	struct monst   *mtmp;
178 	const char           *name;
179 {
180 
181 	mtmp->mx = u.ux;
182 	mtmp->my = u.uy;
183 	u.ustuck = mtmp;
184 	pmon(mtmp);
185 	kludge("%s swallows you!", name);
186 	more();
187 	seeoff(1);
188 	u.uswallow = 1;
189 	u.uswldtim = 0;
190 	swallowed();
191 }
192 
193 void
194 youswld(mtmp, dam, die, name)
195 	struct monst   *mtmp;
196 	int		dam, die;
197 	const char           *name;
198 {
199 	if (mtmp != u.ustuck)
200 		return;
201 	kludge("%s digests you!", name);
202 	u.uhp -= dam;
203 	if (u.uswldtim++ >= die) {	/* a3 */
204 		pline("It totally digests you!");
205 		u.uhp = -1;
206 	}
207 	if (u.uhp < 1)
208 		done_in_by(mtmp);
209 #if 0
210 	flags.botlx = 1;		/* should we show status line ? */
211 #endif
212 }
213 
214 int
215 dochugw(mtmp)
216 	struct monst   *mtmp;
217 {
218 	int x = mtmp->mx;
219 	int y = mtmp->my;
220 	int dead = dochug(mtmp);
221 	int dd;
222 
223 	if (!dead)		/* monster still alive */
224 		if (Warning)
225 			if (!mtmp->mpeaceful)
226 				if (mtmp->data->mlevel > warnlevel)
227 					if ((dd = dist(mtmp->mx, mtmp->my)) < dist(x, y))
228 						if (dd < 100)
229 							if (!canseemon(mtmp))
230 								warnlevel = mtmp->data->mlevel;
231 	return (dead);
232 }
233 
234 /* returns 1 if monster died moving, 0 otherwise */
235 int
236 dochug(mtmp)
237 	struct monst   *mtmp;
238 {
239 	const struct permonst *mdat;
240 	int tmp = 0, nearby, scared;
241 
242 	if (mtmp->cham && !rn2(6))
243 		(void) newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
244 	mdat = mtmp->data;
245 	if (mdat->mlevel < 0)
246 		panic("bad monster %c (%d)", mdat->mlet, mdat->mlevel);
247 
248 	/* regenerate monsters */
249 	if ((!(moves % 20) || strchr(MREGEN, mdat->mlet)) &&
250 	    mtmp->mhp < mtmp->mhpmax)
251 		mtmp->mhp++;
252 
253 	if (mtmp->mfroz)
254 		return (0);	/* frozen monsters don't do anything */
255 
256 	if (mtmp->msleep) {
257 		/* wake up, or get out of here. */
258 		/* ettins are hard to surprise */
259 		/* Nymphs and Leprechauns do not easily wake up */
260 		if (cansee(mtmp->mx, mtmp->my) &&
261 		    (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
262 		    (!strchr("NL", mdat->mlet) || !rn2(50)) &&
263 		    (Aggravate_monster || strchr("d1", mdat->mlet)
264 		     || (!rn2(7) && !mtmp->mimic)))
265 			mtmp->msleep = 0;
266 		else
267 			return (0);
268 	}
269 	/* not frozen or sleeping: wipe out texts written in the dust */
270 	wipe_engr_at(mtmp->mx, mtmp->my, 1);
271 
272 	/* confused monsters get unconfused with small probability */
273 	if (mtmp->mconf && !rn2(50))
274 		mtmp->mconf = 0;
275 
276 	/* some monsters teleport */
277 	if (mtmp->mflee && strchr("tNL", mdat->mlet) && !rn2(40)) {
278 		rloc(mtmp);
279 		return (0);
280 	}
281 	if (mdat->mmove < rnd(6))
282 		return (0);
283 
284 	/* fleeing monsters might regain courage */
285 	if (mtmp->mflee && !mtmp->mfleetim
286 	    && mtmp->mhp == mtmp->mhpmax && !rn2(25))
287 		mtmp->mflee = 0;
288 
289 	nearby = (dist(mtmp->mx, mtmp->my) < 3);
290 	scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
291 			     sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
292 	if (scared && !mtmp->mflee) {
293 		mtmp->mflee = 1;
294 		mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
295 	}
296 	if (!nearby ||
297 	    mtmp->mflee ||
298 	    mtmp->mconf ||
299 	    (mtmp->minvis && !rn2(3)) ||
300 	    (strchr("BIuy", mdat->mlet) && !rn2(4)) ||
301 	    (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
302 	    (!mtmp->mcansee && !rn2(4)) ||
303 	    mtmp->mpeaceful
304 		) {
305 		tmp = m_move(mtmp, 0);	/* 2: monster died moving */
306 		if (tmp == 2 || (tmp && mdat->mmove <= 12))
307 			return (tmp == 2);
308 	}
309 	if (!strchr("Ea", mdat->mlet) && nearby &&
310 	    !mtmp->mpeaceful && u.uhp > 0 && !scared) {
311 		if (mhitu(mtmp))
312 			return (1);	/* monster died (e.g. 'y' or 'F') */
313 	}
314 	/* extra movement for fast monsters */
315 	if (mdat->mmove - 12 > rnd(12))
316 		tmp = m_move(mtmp, 1);
317 	return (tmp == 2);
318 }
319 
320 int
321 m_move(struct monst *mtmp, int after)
322 {
323 	struct monst   *mtmp2;
324 	int		nx, ny, omx, omy, appr, nearer, cnt, i, j;
325 	xchar           gx, gy, nix, niy, chcnt;
326 	schar           chi;
327 	boolean         likegold = 0, likegems = 0, likeobjs = 0;
328 	char            msym = mtmp->data->mlet;
329 	schar           mmoved = 0;	/* not strictly nec.: chi >= 0 will
330 					 * do */
331 	coord           poss[9];
332 	int             info[9];
333 
334 	if (mtmp->mfroz || mtmp->msleep)
335 		return (0);
336 	if (mtmp->mtrapped) {
337 		i = mintrap(mtmp);
338 		if (i == 2)
339 			return (2);	/* he died */
340 		if (i == 1)
341 			return (0);	/* still in trap, so didnt move */
342 	}
343 	if (mtmp->mhide && o_at(mtmp->mx, mtmp->my) && rn2(10))
344 		return (0);	/* do not leave hiding place */
345 
346 #ifndef NOWORM
347 	if (mtmp->wormno)
348 		goto not_special;
349 #endif	/* NOWORM */
350 
351 	/* my dog gets a special treatment */
352 	if (mtmp->mtame) {
353 		return (dog_move(mtmp, after));
354 	}
355 	/* likewise for shopkeeper */
356 	if (mtmp->isshk) {
357 		mmoved = shk_move(mtmp);
358 		if (mmoved >= 0)
359 			goto postmov;
360 		mmoved = 0;	/* follow player outside shop */
361 	}
362 	/* and for the guard */
363 	if (mtmp->isgd) {
364 		mmoved = gd_move();
365 		goto postmov;
366 	}
367 	/*
368 	 * teleport if that lies in our nature ('t') or when badly wounded
369 	 * ('1')
370 	 */
371 	if ((msym == 't' && !rn2(5))
372 	    || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
373 				|| levl[u.ux][u.uy].typ == STAIRS))) {
374 		if (mtmp->mhp < 7 || (msym == 't' && rn2(2)))
375 			rloc(mtmp);
376 		else
377 			mnexto(mtmp);
378 		mmoved = 1;
379 		goto postmov;
380 	}
381 	/* spit fire ('D') or use a wand ('1') when appropriate */
382 	if (strchr("D1", msym))
383 		inrange(mtmp);
384 
385 	if (msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
386 	    mtmp->mcansee && rn2(5)) {
387 		if (!Confusion)
388 			pline("%s's gaze has confused you!", Monnam(mtmp));
389 		else
390 			pline("You are getting more and more confused.");
391 		if (rn2(3))
392 			mtmp->mcan = 1;
393 		Confusion += d(3, 4);	/* timeout */
394 	}
395 not_special:
396 	if (!mtmp->mflee && u.uswallow && u.ustuck != mtmp)
397 		return (1);
398 	appr = 1;
399 	if (mtmp->mflee)
400 		appr = -1;
401 	if (mtmp->mconf || Invis || !mtmp->mcansee ||
402 	    (strchr("BIy", msym) && !rn2(3)))
403 		appr = 0;
404 	omx = mtmp->mx;
405 	omy = mtmp->my;
406 	gx = u.ux;
407 	gy = u.uy;
408 	if (msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
409 		appr = -1;
410 
411 	/*
412 	 * random criterion for 'smell' or track finding ability should use
413 	 * mtmp->msmell or sth
414 	 */
415 	if (msym == '@' ||
416 	    ('a' <= msym && msym <= 'z')) {
417 		coord          *cp;
418 		schar           mroom;
419 		mroom = inroom(omx, omy);
420 		if (mroom < 0 || mroom != inroom(u.ux, u.uy)) {
421 			cp = gettrack(omx, omy);
422 			if (cp) {
423 				gx = cp->x;
424 				gy = cp->y;
425 			}
426 		}
427 	}
428 	/* look for gold or jewels nearby */
429 	likegold = (strchr("LOD", msym) != NULL);
430 	likegems = (strchr("ODu", msym) != NULL);
431 	likeobjs = mtmp->mhide;
432 #define	SRCHRADIUS	25
433 	{
434 		xchar           mind = SRCHRADIUS;	/* not too far away */
435 		int             dd;
436 		if (likegold) {
437 			struct gold    *gold;
438 			for (gold = fgold; gold; gold = gold->ngold)
439 				if ((dd = DIST(omx, omy, gold->gx, gold->gy)) < mind) {
440 					mind = dd;
441 					gx = gold->gx;
442 					gy = gold->gy;
443 				}
444 		}
445 		if (likegems || likeobjs) {
446 			struct obj     *otmp;
447 			for (otmp = fobj; otmp; otmp = otmp->nobj)
448 				if (likeobjs || otmp->olet == GEM_SYM)
449 					if (msym != 'u' ||
450 					    objects[otmp->otyp].g_val != 0)
451 						if ((dd = DIST(omx, omy, otmp->ox, otmp->oy)) < mind) {
452 							mind = dd;
453 							gx = otmp->ox;
454 							gy = otmp->oy;
455 						}
456 		}
457 		if (mind < SRCHRADIUS && appr == -1) {
458 			if (dist(omx, omy) < 10) {
459 				gx = u.ux;
460 				gy = u.uy;
461 			} else
462 				appr = 1;
463 		}
464 	}
465 	nix = omx;
466 	niy = omy;
467 	cnt = mfndpos(mtmp, poss, info,
468 		      msym == 'u' ? NOTONL :
469 		  (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
470 		      strchr(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
471 	/* ALLOW_ROCK for some monsters ? */
472 	chcnt = 0;
473 	chi = -1;
474 	for (i = 0; i < cnt; i++) {
475 		nx = poss[i].x;
476 		ny = poss[i].y;
477 		for (j = 0; j < MTSZ && j < cnt - 1; j++)
478 			if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
479 				if (rn2(4 * (cnt - j)))
480 					goto nxti;
481 #ifdef STUPID
482 		/* some stupid compilers think that this is too complicated */
483 		{
484 			int             d1 = DIST(nx, ny, gx, gy);
485 			int             d2 = DIST(nix, niy, gx, gy);
486 			nearer = (d1 < d2);
487 		}
488 #else
489 		nearer = (DIST(nx, ny, gx, gy) < DIST(nix, niy, gx, gy));
490 #endif	/* STUPID */
491 		if ((appr == 1 && nearer) || (appr == -1 && !nearer) ||
492 		    !mmoved ||
493 		    (!appr && !rn2(++chcnt))) {
494 			nix = nx;
495 			niy = ny;
496 			chi = i;
497 			mmoved = 1;
498 		}
499 nxti:		;
500 	}
501 	if (mmoved) {
502 		if (info[chi] & ALLOW_M) {
503 			mtmp2 = m_at(nix, niy);
504 			if (mtmp2 == NULL)
505 				panic("error in m_move");
506 			if (hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
507 			    hitmm(mtmp2, mtmp) == 2)
508 				return (2);
509 			return (0);
510 		}
511 		if (info[chi] & ALLOW_U) {
512 			(void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd) + 1);
513 			return (0);
514 		}
515 		mtmp->mx = nix;
516 		mtmp->my = niy;
517 		for (j = MTSZ - 1; j > 0; j--)
518 			mtmp->mtrack[j] = mtmp->mtrack[j - 1];
519 		mtmp->mtrack[0].x = omx;
520 		mtmp->mtrack[0].y = omy;
521 #ifndef NOWORM
522 		if (mtmp->wormno)
523 			worm_move(mtmp);
524 #endif	/* NOWORM */
525 	} else {
526 		if (msym == 'u' && rn2(2)) {
527 			rloc(mtmp);
528 			return (0);
529 		}
530 #ifndef NOWORM
531 		if (mtmp->wormno)
532 			worm_nomove(mtmp);
533 #endif	/* NOWORM */
534 	}
535 postmov:
536 	if (mmoved == 1) {
537 		if (mintrap(mtmp) == 2)	/* he died */
538 			return (2);
539 		if (likegold)
540 			mpickgold(mtmp);
541 		if (likegems)
542 			mpickgems(mtmp);
543 		if (mtmp->mhide)
544 			mtmp->mundetected = 1;
545 	}
546 	pmon(mtmp);
547 	return (mmoved);
548 }
549 
550 void
551 mpickgold(mtmp)
552 	struct monst   *mtmp;
553 {
554 	struct gold    *gold;
555 	while ((gold = g_at(mtmp->mx, mtmp->my)) != NULL) {
556 		mtmp->mgold += gold->amount;
557 		freegold(gold);
558 		if (levl[mtmp->mx][mtmp->my].scrsym == '$')
559 			newsym(mtmp->mx, mtmp->my);
560 	}
561 }
562 
563 void
564 mpickgems(mtmp)
565 	struct monst   *mtmp;
566 {
567 	struct obj     *otmp;
568 	for (otmp = fobj; otmp; otmp = otmp->nobj)
569 		if (otmp->olet == GEM_SYM)
570 			if (otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
571 				if (mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0) {
572 					freeobj(otmp);
573 					mpickobj(mtmp, otmp);
574 					if (levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
575 						newsym(mtmp->mx, mtmp->my);	/* %% */
576 					return;	/* pick only one object */
577 				}
578 }
579 
580 /* return number of acceptable neighbour positions */
581 int
582 mfndpos(mon, poss, info, flag)
583 	struct monst   *mon;
584 	coord           poss[9];
585 	int             info[9], flag;
586 {
587 	int             x, y, nx, ny, cnt = 0, ntyp;
588 	struct monst   *mtmp;
589 	int             nowtyp;
590 	boolean         pool;
591 
592 	x = mon->mx;
593 	y = mon->my;
594 	nowtyp = levl[x][y].typ;
595 
596 	pool = (mon->data->mlet == ';');
597 nexttry:			/* eels prefer the water, but if there is no
598 				 * water nearby, they will crawl over land */
599 	if (mon->mconf) {
600 		flag |= ALLOW_ALL;
601 		flag &= ~NOTONL;
602 	}
603 	for (nx = x - 1; nx <= x + 1; nx++)
604 		for (ny = y - 1; ny <= y + 1; ny++)
605 			if (nx != x || ny != y)
606 				if (isok(nx, ny))
607 					if (!IS_ROCK(ntyp = levl[nx][ny].typ))
608 						if (!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
609 							if ((ntyp == POOL) == pool) {
610 								info[cnt] = 0;
611 								if (nx == u.ux && ny == u.uy) {
612 									if (!(flag & ALLOW_U))
613 										continue;
614 									info[cnt] = ALLOW_U;
615 								} else if ((mtmp = m_at(nx, ny)) != NULL) {
616 									if (!(flag & ALLOW_M))
617 										continue;
618 									info[cnt] = ALLOW_M;
619 									if (mtmp->mtame) {
620 										if (!(flag & ALLOW_TM))
621 											continue;
622 										info[cnt] |= ALLOW_TM;
623 									}
624 								}
625 								if (sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
626 									if (flag & NOGARLIC)
627 										continue;
628 									info[cnt] |= NOGARLIC;
629 								}
630 								if (sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
631 								    (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
632 									if (!(flag & ALLOW_SSM))
633 										continue;
634 									info[cnt] |= ALLOW_SSM;
635 								}
636 								if (sobj_at(ENORMOUS_ROCK, nx, ny)) {
637 									if (!(flag & ALLOW_ROCK))
638 										continue;
639 									info[cnt] |= ALLOW_ROCK;
640 								}
641 								if (!Invis && online(nx, ny)) {
642 									if (flag & NOTONL)
643 										continue;
644 									info[cnt] |= NOTONL;
645 								}
646 								/*
647 								 * we cannot
648 								 * avoid
649 								 * traps of
650 								 * an unknown
651 								 * kind
652 								 */
653 								{
654 									struct trap    *ttmp = t_at(nx, ny);
655 									int             tt;
656 									if (ttmp) {
657 										tt = 1 << ttmp->ttyp;
658 										if (mon->mtrapseen & tt) {
659 											if (!(flag & tt))
660 												continue;
661 											info[cnt] |= tt;
662 										}
663 									}
664 								}
665 								poss[cnt].x = nx;
666 								poss[cnt].y = ny;
667 								cnt++;
668 							}
669 	if (!cnt && pool && nowtyp != POOL) {
670 		pool = FALSE;
671 		goto nexttry;
672 	}
673 	return (cnt);
674 }
675 
676 int
677 dist(x, y)
678 	int             x, y;
679 {
680 	return ((x - u.ux) * (x - u.ux) + (y - u.uy) * (y - u.uy));
681 }
682 
683 void
684 poisoned(string, pname)
685 	const char           *string, *pname;
686 {
687 	int             i;
688 
689 	if (Blind)
690 		pline("It was poisoned.");
691 	else
692 		pline("The %s was poisoned!", string);
693 	if (Poison_resistance) {
694 		pline("The poison doesn't seem to affect you.");
695 		return;
696 	}
697 	i = rn2(10);
698 	if (i == 0) {
699 		u.uhp = -1;
700 		pline("I am afraid the poison was deadly ...");
701 	} else if (i <= 5) {
702 		losestr(rn1(3, 3));
703 	} else {
704 		losehp(rn1(10, 6), pname);
705 	}
706 	if (u.uhp < 1) {
707 		killer = pname;
708 		done("died");
709 	}
710 }
711 
712 void
713 mondead(mtmp)
714 	struct monst   *mtmp;
715 {
716 	relobj(mtmp, 1);
717 	unpmon(mtmp);
718 	relmon(mtmp);
719 	unstuck(mtmp);
720 	if (mtmp->isshk)
721 		shkdead(mtmp);
722 	if (mtmp->isgd)
723 		gddead();
724 #ifndef NOWORM
725 	if (mtmp->wormno)
726 		wormdead(mtmp);
727 #endif	/* NOWORM */
728 	monfree(mtmp);
729 }
730 
731 /* called when monster is moved to larger structure */
732 void
733 replmon(mtmp, mtmp2)
734 	struct monst   *mtmp, *mtmp2;
735 {
736 	relmon(mtmp);
737 	monfree(mtmp);
738 	mtmp2->nmon = fmon;
739 	fmon = mtmp2;
740 	if (u.ustuck == mtmp)
741 		u.ustuck = mtmp2;
742 	if (mtmp2->isshk)
743 		replshk(mtmp, mtmp2);
744 	if (mtmp2->isgd)
745 		replgd(mtmp, mtmp2);
746 }
747 
748 void
749 relmon(mon)
750 	struct monst   *mon;
751 {
752 	struct monst   *mtmp;
753 
754 	if (mon == fmon)
755 		fmon = fmon->nmon;
756 	else {
757 		for (mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon);
758 		mtmp->nmon = mon->nmon;
759 	}
760 }
761 
762 /*
763  * we do not free monsters immediately, in order to have their name available
764  * shortly after their demise
765  */
766 struct monst   *fdmon;		/* chain of dead monsters, need not to be
767 				 * saved */
768 
769 void
770 monfree(mtmp)
771 	struct monst   *mtmp;
772 {
773 	mtmp->nmon = fdmon;
774 	fdmon = mtmp;
775 }
776 
777 void
778 dmonsfree()
779 {
780 	struct monst   *mtmp;
781 	while ((mtmp = fdmon) != NULL) {
782 		fdmon = mtmp->nmon;
783 		free((char *) mtmp);
784 	}
785 }
786 
787 void
788 unstuck(mtmp)
789 	struct monst   *mtmp;
790 {
791 	if (u.ustuck == mtmp) {
792 		if (u.uswallow) {
793 			u.ux = mtmp->mx;
794 			u.uy = mtmp->my;
795 			u.uswallow = 0;
796 			setsee();
797 			docrt();
798 		}
799 		u.ustuck = 0;
800 	}
801 }
802 
803 void
804 killed(mtmp)
805 	struct monst   *mtmp;
806 {
807 #ifdef lint
808 #define	NEW_SCORING
809 #endif	/* lint */
810 	int             tmp, nk, x, y;
811 	const struct permonst *mdat;
812 
813 	if (mtmp->cham)
814 		mtmp->data = PM_CHAMELEON;
815 	mdat = mtmp->data;
816 	if (Blind)
817 		pline("You destroy it!");
818 	else {
819 		pline("You destroy %s!",
820 		      mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
821 	}
822 	if (u.umconf) {
823 		if (!Blind)
824 			pline("Your hands stop glowing blue.");
825 		u.umconf = 0;
826 	}
827 	/* count killed monsters */
828 #define	MAXMONNO	100
829 	nk = 1;			/* in case we cannot find it in mons */
830 	tmp = mdat - mons;	/* strchr in mons array (if not 'd', '@', ...) */
831 	if (tmp >= 0 && tmp < CMNUM + 2) {
832 		u.nr_killed[tmp]++;
833 		if ((nk = u.nr_killed[tmp]) > MAXMONNO &&
834 		    !strchr(fut_geno, mdat->mlet))
835 			charcat(fut_geno, mdat->mlet);
836 	}
837 	/* punish bad behaviour */
838 	if (mdat->mlet == '@')
839 		Telepat = 0, u.uluck -= 2;
840 	if (mtmp->mpeaceful || mtmp->mtame)
841 		u.uluck--;
842 	if (mdat->mlet == 'u')
843 		u.uluck -= 5;
844 	if ((int) u.uluck < LUCKMIN)
845 		u.uluck = LUCKMIN;
846 
847 	/* give experience points */
848 	tmp = 1 + mdat->mlevel * mdat->mlevel;
849 	if (mdat->ac < 3)
850 		tmp += 2 * (7 - mdat->ac);
851 	if (strchr("AcsSDXaeRTVWU&In:P", mdat->mlet))
852 		tmp += 2 * mdat->mlevel;
853 	if (strchr("DeV&P", mdat->mlet))
854 		tmp += (7 * mdat->mlevel);
855 	if (mdat->mlevel > 6)
856 		tmp += 50;
857 	if (mdat->mlet == ';')
858 		tmp += 1000;
859 
860 #ifdef NEW_SCORING
861 	/*
862 	 * ------- recent addition: make nr of points decrease when this is
863 	 * not the first of this kind
864 	 */
865 	{
866 		int             ul = u.ulevel;
867 		int             ml = mdat->mlevel;
868 
869 		if (ul < 14)	/* points are given based on present and
870 				 * future level */
871 			for (tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
872 				if (u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4 << (tmp2 - 1))) / nk
873 				    >= 10 * pow((unsigned) (ul - 1)))
874 					if (++ul == 14)
875 						break;
876 
877 		tmp2 = ml - ul - 1;
878 		tmp = (tmp + ((tmp2 < 0) ? 0 : 4 << tmp2)) / nk;
879 		if (!tmp)
880 			tmp = 1;
881 	}
882 	/* note: ul is not necessarily the future value of u.ulevel */
883 	/* ------- end of recent valuation change ------- */
884 #endif	/* NEW_SCORING */
885 
886 	more_experienced(tmp, 0);
887 	flags.botl = 1;
888 	while (u.ulevel < 14 && u.uexp >= newuexp()) {
889 		pline("Welcome to experience level %u.", ++u.ulevel);
890 		tmp = rnd(10);
891 		if (tmp < 3)
892 			tmp = rnd(10);
893 		u.uhpmax += tmp;
894 		u.uhp += tmp;
895 		flags.botl = 1;
896 	}
897 
898 	/* dispose of monster and make cadaver */
899 	x = mtmp->mx;
900 	y = mtmp->my;
901 	mondead(mtmp);
902 	tmp = mdat->mlet;
903 	if (tmp == 'm') {	/* he killed a minotaur, give him a wand of
904 				 * digging */
905 		/* note: the dead minotaur will be on top of it! */
906 		mksobj_at(WAN_DIGGING, x, y);
907 		/* if(cansee(x,y)) atl(x,y,fobj->olet); */
908 		stackobj(fobj);
909 	} else
910 #ifndef NOWORM
911 	if (tmp == 'w') {
912 		mksobj_at(WORM_TOOTH, x, y);
913 		stackobj(fobj);
914 	} else
915 #endif	/* NOWORM */
916 	if (!letter(tmp) || (!strchr("mw", tmp) && !rn2(3)))
917 		tmp = 0;
918 
919 	if (ACCESSIBLE(levl[x][y].typ))	/* might be mimic in wall or dead eel */
920 		if (x != u.ux || y != u.uy)	/* might be here after
921 						 * swallowed */
922 			if (strchr("NTVm&", mdat->mlet) || rn2(5)) {
923 				struct obj     *obj2 = mkobj_at(tmp, x, y);
924 				if (cansee(x, y))
925 					atl(x, y, obj2->olet);
926 				stackobj(obj2);
927 			}
928 }
929 
930 void
931 kludge(const char *str, const char *arg)
932 {
933 	if (Blind) {
934 		if (*str == '%')
935 			pline(str, "It");
936 		else
937 			pline(str, "it");
938 	} else
939 		pline(str, arg);
940 }
941 
942 void
943 rescham()
944 {				/* force all chameleons to become normal */
945 	struct monst   *mtmp;
946 
947 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
948 		if (mtmp->cham) {
949 			mtmp->cham = 0;
950 			(void) newcham(mtmp, PM_CHAMELEON);
951 		}
952 }
953 
954 int
955 newcham(mtmp, mdat)		/* make a chameleon look like a new monster */
956 /* returns 1 if the monster actually changed */
957 	struct monst   *mtmp;
958 	const struct permonst *mdat;
959 {
960 	int mhp, hpn, hpd;
961 
962 	if (mdat == mtmp->data)
963 		return (0);	/* still the same monster */
964 #ifndef NOWORM
965 	if (mtmp->wormno)
966 		wormdead(mtmp);	/* throw tail away */
967 #endif	/* NOWORM */
968 	if (u.ustuck == mtmp) {
969 		if (u.uswallow) {
970 			u.uswallow = 0;
971 			u.uswldtim = 0;
972 			mnexto(mtmp);
973 			docrt();
974 			prme();
975 		}
976 		u.ustuck = 0;
977 	}
978 	hpn = mtmp->mhp;
979 	hpd = (mtmp->data->mlevel) * 8;
980 	if (!hpd)
981 		hpd = 4;
982 	mtmp->data = mdat;
983 	mhp = (mdat->mlevel) * 8;
984 	/* new hp: same fraction of max as before */
985 	mtmp->mhp = 2 + (hpn * mhp) / hpd;
986 	hpn = mtmp->mhpmax;
987 	mtmp->mhpmax = 2 + (hpn * mhp) / hpd;
988 	mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
989 #ifndef NOWORM
990 	if (mdat->mlet == 'w' && getwn(mtmp))
991 		initworm(mtmp);
992 	/* perhaps we should clear mtmp->mtame here? */
993 #endif	/* NOWORM */
994 	unpmon(mtmp);		/* necessary for 'I' and to force pmon */
995 	pmon(mtmp);
996 	return (1);
997 }
998 
999 void
1000 mnexto(mtmp)			/* Make monster mtmp next to you (if
1001 				 * possible) */
1002 	struct monst   *mtmp;
1003 {
1004 	coord           mm;
1005 	mm = enexto(u.ux, u.uy);
1006 	mtmp->mx = mm.x;
1007 	mtmp->my = mm.y;
1008 	pmon(mtmp);
1009 }
1010 
1011 int
1012 ishuman(mtmp)
1013 	struct monst   *mtmp;
1014 {
1015 	return (mtmp->data->mlet == '@');
1016 }
1017 
1018 void
1019 setmangry(mtmp)
1020 	struct monst   *mtmp;
1021 {
1022 	if (!mtmp->mpeaceful)
1023 		return;
1024 	if (mtmp->mtame)
1025 		return;
1026 	mtmp->mpeaceful = 0;
1027 	if (ishuman(mtmp))
1028 		pline("%s gets angry!", Monnam(mtmp));
1029 }
1030 
1031 /*
1032  * not one hundred procent correct: now a snake may hide under an invisible
1033  * object
1034  */
1035 int
1036 canseemon(mtmp)
1037 	struct monst   *mtmp;
1038 {
1039 	return ((!mtmp->minvis || See_invisible)
1040 		&& (!mtmp->mhide || !o_at(mtmp->mx, mtmp->my))
1041 		&& cansee(mtmp->mx, mtmp->my));
1042 }
1043