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