xref: /csrg-svn/games/hack/hack.mon.c (revision 43466)
141247Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
241247Sbostic /* hack.mon.c - version 1.0.3 */
341247Sbostic 
441247Sbostic #include "hack.h"
541247Sbostic #include "hack.mfndpos.h"
6*43466Sbostic 
7*43466Sbostic #ifndef NULL
841247Sbostic #define	NULL	(char *) 0
9*43466Sbostic #endif
10*43466Sbostic 
1141247Sbostic extern struct monst *makemon();
1241247Sbostic extern struct obj *mkobj_at();
1341247Sbostic 
1441247Sbostic int warnlevel;		/* used by movemon and dochugw */
1541247Sbostic long lastwarntime;
1641247Sbostic int lastwarnlev;
1741247Sbostic char *warnings[] = {
1841247Sbostic 	"white", "pink", "red", "ruby", "purple", "black"
1941247Sbostic };
2041247Sbostic 
movemon()2141247Sbostic movemon()
2241247Sbostic {
2341247Sbostic 	register struct monst *mtmp;
2441247Sbostic 	register int fr;
2541247Sbostic 
2641247Sbostic 	warnlevel = 0;
2741247Sbostic 
2841247Sbostic 	while(1) {
2941247Sbostic 		/* find a monster that we haven't treated yet */
3041247Sbostic 		/* note that mtmp or mtmp->nmon might get killed
3141247Sbostic 		   while mtmp moves, so we cannot just walk down the
3241247Sbostic 		   chain (even new monsters might get created!) */
3341247Sbostic 		for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
3441247Sbostic 			if(mtmp->mlstmv < moves) goto next_mon;
3541247Sbostic 		/* treated all monsters */
3641247Sbostic 		break;
3741247Sbostic 
3841247Sbostic 	next_mon:
3941247Sbostic 		mtmp->mlstmv = moves;
4041247Sbostic 
4141247Sbostic 		/* most monsters drown in pools */
4241247Sbostic 		{ boolean inpool, iseel;
4341247Sbostic 
4441247Sbostic 		  inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
4541247Sbostic 		  iseel = (mtmp->data->mlet == ';');
4641247Sbostic 		  if(inpool && !iseel) {
4741247Sbostic 			if(cansee(mtmp->mx,mtmp->my))
4841247Sbostic 			    pline("%s drowns.", Monnam(mtmp));
4941247Sbostic 			mondead(mtmp);
5041247Sbostic 			continue;
5141247Sbostic 		  }
5241247Sbostic 		/* but eels have a difficult time outside */
5341247Sbostic 		  if(iseel && !inpool) {
5441247Sbostic 			if(mtmp->mhp > 1) mtmp->mhp--;
5541247Sbostic 			mtmp->mflee = 1;
5641247Sbostic 			mtmp->mfleetim += 2;
5741247Sbostic 		  }
5841247Sbostic 		}
5941247Sbostic 		if(mtmp->mblinded && !--mtmp->mblinded)
6041247Sbostic 			mtmp->mcansee = 1;
6141247Sbostic 		if(mtmp->mfleetim && !--mtmp->mfleetim)
6241247Sbostic 			mtmp->mflee = 0;
6341247Sbostic 		if(mtmp->mimic) continue;
6441247Sbostic 		if(mtmp->mspeed != MSLOW || !(moves%2)){
6541247Sbostic 			/* continue if the monster died fighting */
6641247Sbostic 			fr = -1;
6741247Sbostic 			if(Conflict && cansee(mtmp->mx,mtmp->my)
6841247Sbostic 				&& (fr = fightm(mtmp)) == 2)
6941247Sbostic 				continue;
7041247Sbostic 			if(fr<0 && dochugw(mtmp))
7141247Sbostic 				continue;
7241247Sbostic 		}
7341247Sbostic 		if(mtmp->mspeed == MFAST && dochugw(mtmp))
7441247Sbostic 			continue;
7541247Sbostic 	}
7641247Sbostic 
7741247Sbostic 	warnlevel -= u.ulevel;
7841247Sbostic 	if(warnlevel >= SIZE(warnings))
7941247Sbostic 		warnlevel = SIZE(warnings)-1;
8041247Sbostic 	if(warnlevel >= 0)
8141247Sbostic 	if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
8241247Sbostic 	    register char *rr;
8341247Sbostic 	    switch(Warning & (LEFT_RING | RIGHT_RING)){
8441247Sbostic 	    case LEFT_RING:
8541247Sbostic 		rr = "Your left ring glows";
8641247Sbostic 		break;
8741247Sbostic 	    case RIGHT_RING:
8841247Sbostic 		rr = "Your right ring glows";
8941247Sbostic 		break;
9041247Sbostic 	    case LEFT_RING | RIGHT_RING:
9141247Sbostic 		rr = "Both your rings glow";
9241247Sbostic 		break;
9341247Sbostic 	    default:
9441247Sbostic 		rr = "Your fingertips glow";
9541247Sbostic 		break;
9641247Sbostic 	    }
9741247Sbostic 	    pline("%s %s!", rr, warnings[warnlevel]);
9841247Sbostic 	    lastwarntime = moves;
9941247Sbostic 	    lastwarnlev = warnlevel;
10041247Sbostic 	}
10141247Sbostic 
10241247Sbostic 	dmonsfree();	/* remove all dead monsters */
10341247Sbostic }
10441247Sbostic 
justswld(mtmp,name)10541247Sbostic justswld(mtmp,name)
10641247Sbostic register struct monst *mtmp;
10741247Sbostic char *name;
10841247Sbostic {
10941247Sbostic 
11041247Sbostic 	mtmp->mx = u.ux;
11141247Sbostic 	mtmp->my = u.uy;
11241247Sbostic 	u.ustuck = mtmp;
11341247Sbostic 	pmon(mtmp);
11441247Sbostic 	kludge("%s swallows you!",name);
11541247Sbostic 	more();
11641247Sbostic 	seeoff(1);
11741247Sbostic 	u.uswallow = 1;
11841247Sbostic 	u.uswldtim = 0;
11941247Sbostic 	swallowed();
12041247Sbostic }
12141247Sbostic 
youswld(mtmp,dam,die,name)12241247Sbostic youswld(mtmp,dam,die,name)
12341247Sbostic register struct monst *mtmp;
12441247Sbostic register dam,die;
12541247Sbostic char *name;
12641247Sbostic {
12741247Sbostic 	if(mtmp != u.ustuck) return;
12841247Sbostic 	kludge("%s digests you!",name);
12941247Sbostic 	u.uhp -= dam;
13041247Sbostic 	if(u.uswldtim++ >= die){	/* a3 */
13141247Sbostic 		pline("It totally digests you!");
13241247Sbostic 		u.uhp = -1;
13341247Sbostic 	}
13441247Sbostic 	if(u.uhp < 1) done_in_by(mtmp);
13541247Sbostic 	/* flags.botlx = 1;		/* should we show status line ? */
13641247Sbostic }
13741247Sbostic 
dochugw(mtmp)13841247Sbostic dochugw(mtmp) register struct monst *mtmp; {
13941247Sbostic register x = mtmp->mx;
14041247Sbostic register y = mtmp->my;
14141247Sbostic register d = dochug(mtmp);
14241247Sbostic register dd;
14341247Sbostic 	if(!d)		/* monster still alive */
14441247Sbostic 	if(Warning)
14541247Sbostic 	if(!mtmp->mpeaceful)
14641247Sbostic 	if(mtmp->data->mlevel > warnlevel)
14741247Sbostic 	if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
14841247Sbostic 	if(dd < 100)
14941247Sbostic 	if(!canseemon(mtmp))
15041247Sbostic 		warnlevel = mtmp->data->mlevel;
15141247Sbostic 	return(d);
15241247Sbostic }
15341247Sbostic 
15441247Sbostic /* returns 1 if monster died moving, 0 otherwise */
dochug(mtmp)15541247Sbostic dochug(mtmp)
15641247Sbostic register struct monst *mtmp;
15741247Sbostic {
15841247Sbostic 	register struct permonst *mdat;
15941247Sbostic 	register tmp, nearby, scared;
16041247Sbostic 
16141247Sbostic 	if(mtmp->cham && !rn2(6))
16241247Sbostic 		(void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
16341247Sbostic 	mdat = mtmp->data;
16441247Sbostic 	if(mdat->mlevel < 0)
16541247Sbostic 		panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
16641247Sbostic 
16741247Sbostic 	/* regenerate monsters */
16841247Sbostic 	if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
16941247Sbostic 	    mtmp->mhp < mtmp->mhpmax)
17041247Sbostic 		mtmp->mhp++;
17141247Sbostic 
17241247Sbostic 	if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
17341247Sbostic 
17441247Sbostic 	if(mtmp->msleep) {
17541247Sbostic 		/* wake up, or get out of here. */
17641247Sbostic 		/* ettins are hard to surprise */
17741247Sbostic 		/* Nymphs and Leprechauns do not easily wake up */
17841247Sbostic 		if(cansee(mtmp->mx,mtmp->my) &&
17941247Sbostic 			(!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
18041247Sbostic 			(!index("NL",mdat->mlet) || !rn2(50)) &&
18141247Sbostic 			(Aggravate_monster || index("d1", mdat->mlet)
18241247Sbostic 				|| (!rn2(7) && !mtmp->mimic)))
18341247Sbostic 			mtmp->msleep = 0;
18441247Sbostic 		else return(0);
18541247Sbostic 	}
18641247Sbostic 
18741247Sbostic 	/* not frozen or sleeping: wipe out texts written in the dust */
18841247Sbostic 	wipe_engr_at(mtmp->mx, mtmp->my, 1);
18941247Sbostic 
19041247Sbostic 	/* confused monsters get unconfused with small probability */
19141247Sbostic 	if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
19241247Sbostic 
19341247Sbostic 	/* some monsters teleport */
19441247Sbostic 	if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
19541247Sbostic 		rloc(mtmp);
19641247Sbostic 		return(0);
19741247Sbostic 	}
19841247Sbostic 	if(mdat->mmove < rnd(6)) return(0);
19941247Sbostic 
20041247Sbostic 	/* fleeing monsters might regain courage */
20141247Sbostic 	if(mtmp->mflee && !mtmp->mfleetim
20241247Sbostic 	    && mtmp->mhp == mtmp->mhpmax && !rn2(25))
20341247Sbostic 		mtmp->mflee = 0;
20441247Sbostic 
20541247Sbostic 	nearby = (dist(mtmp->mx, mtmp->my) < 3);
20641247Sbostic 	scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
20741247Sbostic 			sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
20841247Sbostic 	if(scared && !mtmp->mflee) {
20941247Sbostic 		mtmp->mflee = 1;
21041247Sbostic 		mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
21141247Sbostic 	}
21241247Sbostic 
21341247Sbostic 	if(!nearby ||
21441247Sbostic 		mtmp->mflee ||
21541247Sbostic 		mtmp->mconf ||
21641247Sbostic 		(mtmp->minvis && !rn2(3)) ||
21741247Sbostic 		(index("BIuy", mdat->mlet) && !rn2(4)) ||
21841247Sbostic 		(mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
21941247Sbostic 		(!mtmp->mcansee && !rn2(4)) ||
22041247Sbostic 		mtmp->mpeaceful
22141247Sbostic 	   ) {
22241247Sbostic 		tmp = m_move(mtmp,0);	/* 2: monster died moving */
22341247Sbostic 		if(tmp == 2 || (tmp && mdat->mmove <= 12))
22441247Sbostic 			return(tmp == 2);
22541247Sbostic 	}
22641247Sbostic 
22741247Sbostic 	if(!index("Ea", mdat->mlet) && nearby &&
22841247Sbostic 	 !mtmp->mpeaceful && u.uhp > 0 && !scared) {
22941247Sbostic 		if(mhitu(mtmp))
23041247Sbostic 			return(1);	/* monster died (e.g. 'y' or 'F') */
23141247Sbostic 	}
23241247Sbostic 	/* extra movement for fast monsters */
23341247Sbostic 	if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
23441247Sbostic 	return(tmp == 2);
23541247Sbostic }
23641247Sbostic 
m_move(mtmp,after)23741247Sbostic m_move(mtmp,after)
23841247Sbostic register struct monst *mtmp;
23941247Sbostic {
24041247Sbostic 	register struct monst *mtmp2;
24141247Sbostic 	register nx,ny,omx,omy,appr,nearer,cnt,i,j;
24241247Sbostic 	xchar gx,gy,nix,niy,chcnt;
24341247Sbostic 	schar chi;
24441247Sbostic 	boolean likegold, likegems, likeobjs;
24541247Sbostic 	char msym = mtmp->data->mlet;
24641247Sbostic 	schar mmoved = 0;	/* not strictly nec.: chi >= 0 will do */
24741247Sbostic 	coord poss[9];
24841247Sbostic 	int info[9];
24941247Sbostic 
25041247Sbostic 	if(mtmp->mfroz || mtmp->msleep)
25141247Sbostic 		return(0);
25241247Sbostic 	if(mtmp->mtrapped) {
25341247Sbostic 		i = mintrap(mtmp);
25441247Sbostic 		if(i == 2) return(2);	/* he died */
25541247Sbostic 		if(i == 1) return(0);	/* still in trap, so didnt move */
25641247Sbostic 	}
25741247Sbostic 	if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
25841247Sbostic 		return(0);		/* do not leave hiding place */
25941247Sbostic 
26041247Sbostic #ifndef NOWORM
26141247Sbostic 	if(mtmp->wormno)
26241247Sbostic 		goto not_special;
26341247Sbostic #endif NOWORM
26441247Sbostic 
26541247Sbostic 	/* my dog gets a special treatment */
26641247Sbostic 	if(mtmp->mtame) {
26741247Sbostic 		return( dog_move(mtmp, after) );
26841247Sbostic 	}
26941247Sbostic 
27041247Sbostic 	/* likewise for shopkeeper */
27141247Sbostic 	if(mtmp->isshk) {
27241247Sbostic 		mmoved = shk_move(mtmp);
27341247Sbostic 		if(mmoved >= 0)
27441247Sbostic 			goto postmov;
27541247Sbostic 		mmoved = 0;		/* follow player outside shop */
27641247Sbostic 	}
27741247Sbostic 
27841247Sbostic 	/* and for the guard */
27941247Sbostic 	if(mtmp->isgd) {
28041247Sbostic 		mmoved = gd_move();
28141247Sbostic 		goto postmov;
28241247Sbostic 	}
28341247Sbostic 
28441247Sbostic /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
28541247Sbostic 	if((msym == 't' && !rn2(5))
28641247Sbostic 	|| (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
28741247Sbostic 		|| levl[u.ux][u.uy].typ == STAIRS))) {
28841247Sbostic 		if(mtmp->mhp < 7 || (msym == 't' && rn2(2)))
28941247Sbostic 			rloc(mtmp);
29041247Sbostic 		else
29141247Sbostic 			mnexto(mtmp);
29241247Sbostic 		mmoved = 1;
29341247Sbostic 		goto postmov;
29441247Sbostic 	}
29541247Sbostic 
29641247Sbostic 	/* spit fire ('D') or use a wand ('1') when appropriate */
29741247Sbostic 	if(index("D1", msym))
29841247Sbostic 		inrange(mtmp);
29941247Sbostic 
30041247Sbostic 	if(msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
30141247Sbostic 	    mtmp->mcansee && rn2(5)) {
30241247Sbostic 		if(!Confusion)
30341247Sbostic 			pline("%s's gaze has confused you!", Monnam(mtmp));
30441247Sbostic 		else
30541247Sbostic 			pline("You are getting more and more confused.");
30641247Sbostic 		if(rn2(3)) mtmp->mcan = 1;
30741247Sbostic 		Confusion += d(3,4);		/* timeout */
30841247Sbostic 	}
30941247Sbostic not_special:
31041247Sbostic 	if(!mtmp->mflee && u.uswallow && u.ustuck != mtmp) return(1);
31141247Sbostic 	appr = 1;
31241247Sbostic 	if(mtmp->mflee) appr = -1;
31341247Sbostic 	if(mtmp->mconf || Invis ||  !mtmp->mcansee ||
31441247Sbostic 		(index("BIy", msym) && !rn2(3)))
31541247Sbostic 		appr = 0;
31641247Sbostic 	omx = mtmp->mx;
31741247Sbostic 	omy = mtmp->my;
31841247Sbostic 	gx = u.ux;
31941247Sbostic 	gy = u.uy;
32041247Sbostic 	if(msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
32141247Sbostic 		appr = -1;
32241247Sbostic 
32341247Sbostic 	/* random criterion for 'smell' or track finding ability
32441247Sbostic 	   should use mtmp->msmell or sth
32541247Sbostic 	 */
32641247Sbostic 	if(msym == '@' ||
32741247Sbostic 	  ('a' <= msym && msym <= 'z')) {
32841247Sbostic 	extern coord *gettrack();
32941247Sbostic 	register coord *cp;
33041247Sbostic 	schar mroom;
33141247Sbostic 		mroom = inroom(omx,omy);
33241247Sbostic 		if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
33341247Sbostic 		    cp = gettrack(omx,omy);
33441247Sbostic 		    if(cp){
33541247Sbostic 			gx = cp->x;
33641247Sbostic 			gy = cp->y;
33741247Sbostic 		    }
33841247Sbostic 		}
33941247Sbostic 	}
34041247Sbostic 
34141247Sbostic 	/* look for gold or jewels nearby */
34241247Sbostic 	likegold = (index("LOD", msym) != NULL);
34341247Sbostic 	likegems = (index("ODu", msym) != NULL);
34441247Sbostic 	likeobjs = mtmp->mhide;
34541247Sbostic #define	SRCHRADIUS	25
34641247Sbostic 	{ xchar mind = SRCHRADIUS;		/* not too far away */
34741247Sbostic 	  register int dd;
34841247Sbostic 	  if(likegold){
34941247Sbostic 		register struct gold *gold;
35041247Sbostic 		for(gold = fgold; gold; gold = gold->ngold)
35141247Sbostic 		  if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
35241247Sbostic 		    mind = dd;
35341247Sbostic 		    gx = gold->gx;
35441247Sbostic 		    gy = gold->gy;
35541247Sbostic 		}
35641247Sbostic 	  }
35741247Sbostic 	  if(likegems || likeobjs){
35841247Sbostic 		register struct obj *otmp;
35941247Sbostic 		for(otmp = fobj; otmp; otmp = otmp->nobj)
36041247Sbostic 		if(likeobjs || otmp->olet == GEM_SYM)
36141247Sbostic 		if(msym != 'u' ||
36241247Sbostic 			objects[otmp->otyp].g_val != 0)
36341247Sbostic 		if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
36441247Sbostic 		    mind = dd;
36541247Sbostic 		    gx = otmp->ox;
36641247Sbostic 		    gy = otmp->oy;
36741247Sbostic 		}
36841247Sbostic 	  }
36941247Sbostic 	  if(mind < SRCHRADIUS && appr == -1) {
37041247Sbostic 		if(dist(omx,omy) < 10) {
37141247Sbostic 		    gx = u.ux;
37241247Sbostic 		    gy = u.uy;
37341247Sbostic 		} else
37441247Sbostic 		    appr = 1;
37541247Sbostic 	  }
37641247Sbostic 	}
37741247Sbostic 	nix = omx;
37841247Sbostic 	niy = omy;
37941247Sbostic 	cnt = mfndpos(mtmp,poss,info,
38041247Sbostic 		msym == 'u' ? NOTONL :
38141247Sbostic 		(msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
38241247Sbostic 		index(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
38341247Sbostic 		/* ALLOW_ROCK for some monsters ? */
38441247Sbostic 	chcnt = 0;
38541247Sbostic 	chi = -1;
38641247Sbostic 	for(i=0; i<cnt; i++) {
38741247Sbostic 		nx = poss[i].x;
38841247Sbostic 		ny = poss[i].y;
38941247Sbostic 		for(j=0; j<MTSZ && j<cnt-1; j++)
39041247Sbostic 			if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
39141247Sbostic 				if(rn2(4*(cnt-j))) goto nxti;
39241247Sbostic #ifdef STUPID
39341247Sbostic 		/* some stupid compilers think that this is too complicated */
39441247Sbostic 		{ int d1 = DIST(nx,ny,gx,gy);
39541247Sbostic 		  int d2 = DIST(nix,niy,gx,gy);
39641247Sbostic 		  nearer = (d1 < d2);
39741247Sbostic 		}
39841247Sbostic #else
39941247Sbostic 		nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
40041247Sbostic #endif STUPID
40141247Sbostic 		if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
40241247Sbostic 			!mmoved ||
40341247Sbostic 			(!appr && !rn2(++chcnt))){
40441247Sbostic 			nix = nx;
40541247Sbostic 			niy = ny;
40641247Sbostic 			chi = i;
40741247Sbostic 			mmoved = 1;
40841247Sbostic 		}
40941247Sbostic 	nxti:	;
41041247Sbostic 	}
41141247Sbostic 	if(mmoved){
41241247Sbostic 		if(info[chi] & ALLOW_M){
41341247Sbostic 			mtmp2 = m_at(nix,niy);
41441247Sbostic 			if(hitmm(mtmp,mtmp2) == 1 && rn2(4) &&
41541247Sbostic 			  hitmm(mtmp2,mtmp) == 2) return(2);
41641247Sbostic 			return(0);
41741247Sbostic 		}
41841247Sbostic 		if(info[chi] & ALLOW_U){
41941247Sbostic 		  (void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
42041247Sbostic 		  return(0);
42141247Sbostic 		}
42241247Sbostic 		mtmp->mx = nix;
42341247Sbostic 		mtmp->my = niy;
42441247Sbostic 		for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
42541247Sbostic 		mtmp->mtrack[0].x = omx;
42641247Sbostic 		mtmp->mtrack[0].y = omy;
42741247Sbostic #ifndef NOWORM
42841247Sbostic 		if(mtmp->wormno) worm_move(mtmp);
42941247Sbostic #endif NOWORM
43041247Sbostic 	} else {
43141247Sbostic 		if(msym == 'u' && rn2(2)){
43241247Sbostic 			rloc(mtmp);
43341247Sbostic 			return(0);
43441247Sbostic 		}
43541247Sbostic #ifndef NOWORM
43641247Sbostic 		if(mtmp->wormno) worm_nomove(mtmp);
43741247Sbostic #endif NOWORM
43841247Sbostic 	}
43941247Sbostic postmov:
44041247Sbostic 	if(mmoved == 1) {
44141247Sbostic 		if(mintrap(mtmp) == 2)	/* he died */
44241247Sbostic 			return(2);
44341247Sbostic 		if(likegold) mpickgold(mtmp);
44441247Sbostic 		if(likegems) mpickgems(mtmp);
44541247Sbostic 		if(mtmp->mhide) mtmp->mundetected = 1;
44641247Sbostic 	}
44741247Sbostic 	pmon(mtmp);
44841247Sbostic 	return(mmoved);
44941247Sbostic }
45041247Sbostic 
mpickgold(mtmp)45141247Sbostic mpickgold(mtmp) register struct monst *mtmp; {
45241247Sbostic register struct gold *gold;
45341247Sbostic 	while(gold = g_at(mtmp->mx, mtmp->my)){
45441247Sbostic 		mtmp->mgold += gold->amount;
45541247Sbostic 		freegold(gold);
45641247Sbostic 		if(levl[mtmp->mx][mtmp->my].scrsym == '$')
45741247Sbostic 			newsym(mtmp->mx, mtmp->my);
45841247Sbostic 	}
45941247Sbostic }
46041247Sbostic 
mpickgems(mtmp)46141247Sbostic mpickgems(mtmp) register struct monst *mtmp; {
46241247Sbostic register struct obj *otmp;
46341247Sbostic 	for(otmp = fobj; otmp; otmp = otmp->nobj)
46441247Sbostic 	if(otmp->olet == GEM_SYM)
46541247Sbostic 	if(otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
46641247Sbostic 	if(mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0){
46741247Sbostic 		freeobj(otmp);
46841247Sbostic 		mpickobj(mtmp, otmp);
46941247Sbostic 		if(levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
47041247Sbostic 			newsym(mtmp->mx, mtmp->my);	/* %% */
47141247Sbostic 		return;	/* pick only one object */
47241247Sbostic 	}
47341247Sbostic }
47441247Sbostic 
47541247Sbostic /* return number of acceptable neighbour positions */
mfndpos(mon,poss,info,flag)47641247Sbostic mfndpos(mon,poss,info,flag)
47741247Sbostic register struct monst *mon;
47841247Sbostic coord poss[9];
47941247Sbostic int info[9], flag;
48041247Sbostic {
48141247Sbostic 	register int x,y,nx,ny,cnt = 0,ntyp;
48241247Sbostic 	register struct monst *mtmp;
48341247Sbostic 	int nowtyp;
48441247Sbostic 	boolean pool;
48541247Sbostic 
48641247Sbostic 	x = mon->mx;
48741247Sbostic 	y = mon->my;
48841247Sbostic 	nowtyp = levl[x][y].typ;
48941247Sbostic 
49041247Sbostic 	pool = (mon->data->mlet == ';');
49141247Sbostic nexttry:	/* eels prefer the water, but if there is no water nearby,
49241247Sbostic 		   they will crawl over land */
49341247Sbostic 	if(mon->mconf) {
49441247Sbostic 		flag |= ALLOW_ALL;
49541247Sbostic 		flag &= ~NOTONL;
49641247Sbostic 	}
49741247Sbostic 	for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++)
49841247Sbostic 	if(nx != x || ny != y) if(isok(nx,ny))
49941247Sbostic 	if(!IS_ROCK(ntyp = levl[nx][ny].typ))
50041247Sbostic 	if(!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
50141247Sbostic 	if((ntyp == POOL) == pool) {
50241247Sbostic 		info[cnt] = 0;
50341247Sbostic 		if(nx == u.ux && ny == u.uy){
50441247Sbostic 			if(!(flag & ALLOW_U)) continue;
50541247Sbostic 			info[cnt] = ALLOW_U;
50641247Sbostic 		} else if(mtmp = m_at(nx,ny)){
50741247Sbostic 			if(!(flag & ALLOW_M)) continue;
50841247Sbostic 			info[cnt] = ALLOW_M;
50941247Sbostic 			if(mtmp->mtame){
51041247Sbostic 				if(!(flag & ALLOW_TM)) continue;
51141247Sbostic 				info[cnt] |= ALLOW_TM;
51241247Sbostic 			}
51341247Sbostic 		}
51441247Sbostic 		if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
51541247Sbostic 			if(flag & NOGARLIC) continue;
51641247Sbostic 			info[cnt] |= NOGARLIC;
51741247Sbostic 		}
51841247Sbostic 		if(sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
51941247Sbostic 		   (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
52041247Sbostic 			if(!(flag & ALLOW_SSM)) continue;
52141247Sbostic 			info[cnt] |= ALLOW_SSM;
52241247Sbostic 		}
52341247Sbostic 		if(sobj_at(ENORMOUS_ROCK, nx, ny)) {
52441247Sbostic 			if(!(flag & ALLOW_ROCK)) continue;
52541247Sbostic 			info[cnt] |= ALLOW_ROCK;
52641247Sbostic 		}
52741247Sbostic 		if(!Invis && online(nx,ny)){
52841247Sbostic 			if(flag & NOTONL) continue;
52941247Sbostic 			info[cnt] |= NOTONL;
53041247Sbostic 		}
53141247Sbostic 		/* we cannot avoid traps of an unknown kind */
53241247Sbostic 		{ register struct trap *ttmp = t_at(nx, ny);
53341247Sbostic 		  register int tt;
53441247Sbostic 			if(ttmp) {
53541247Sbostic 				tt = 1 << ttmp->ttyp;
53641247Sbostic 				if(mon->mtrapseen & tt){
53741247Sbostic 					if(!(flag & tt)) continue;
53841247Sbostic 					info[cnt] |= tt;
53941247Sbostic 				}
54041247Sbostic 			}
54141247Sbostic 		}
54241247Sbostic 		poss[cnt].x = nx;
54341247Sbostic 		poss[cnt].y = ny;
54441247Sbostic 		cnt++;
54541247Sbostic 	}
54641247Sbostic 	if(!cnt && pool && nowtyp != POOL) {
54741247Sbostic 		pool = FALSE;
54841247Sbostic 		goto nexttry;
54941247Sbostic 	}
55041247Sbostic 	return(cnt);
55141247Sbostic }
55241247Sbostic 
dist(x,y)55341247Sbostic dist(x,y) int x,y; {
55441247Sbostic 	return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
55541247Sbostic }
55641247Sbostic 
poisoned(string,pname)55741247Sbostic poisoned(string, pname)
55841247Sbostic register char *string, *pname;
55941247Sbostic {
56041247Sbostic 	register int i;
56141247Sbostic 
56241247Sbostic 	if(Blind) pline("It was poisoned.");
56341247Sbostic 	else pline("The %s was poisoned!",string);
56441247Sbostic 	if(Poison_resistance) {
56541247Sbostic 		pline("The poison doesn't seem to affect you.");
56641247Sbostic 		return;
56741247Sbostic 	}
56841247Sbostic 	i = rn2(10);
56941247Sbostic 	if(i == 0) {
57041247Sbostic 		u.uhp = -1;
57141247Sbostic 		pline("I am afraid the poison was deadly ...");
57241247Sbostic 	} else if(i <= 5) {
57341247Sbostic 		losestr(rn1(3,3));
57441247Sbostic 	} else {
57541247Sbostic 		losehp(rn1(10,6), pname);
57641247Sbostic 	}
57741247Sbostic 	if(u.uhp < 1) {
57841247Sbostic 		killer = pname;
57941247Sbostic 		done("died");
58041247Sbostic 	}
58141247Sbostic }
58241247Sbostic 
mondead(mtmp)58341247Sbostic mondead(mtmp)
58441247Sbostic register struct monst *mtmp;
58541247Sbostic {
58641247Sbostic 	relobj(mtmp,1);
58741247Sbostic 	unpmon(mtmp);
58841247Sbostic 	relmon(mtmp);
58941247Sbostic 	unstuck(mtmp);
59041247Sbostic 	if(mtmp->isshk) shkdead(mtmp);
59141247Sbostic 	if(mtmp->isgd) gddead();
59241247Sbostic #ifndef NOWORM
59341247Sbostic 	if(mtmp->wormno) wormdead(mtmp);
59441247Sbostic #endif NOWORM
59541247Sbostic 	monfree(mtmp);
59641247Sbostic }
59741247Sbostic 
59841247Sbostic /* called when monster is moved to larger structure */
replmon(mtmp,mtmp2)59941247Sbostic replmon(mtmp,mtmp2)
60041247Sbostic register struct monst *mtmp, *mtmp2;
60141247Sbostic {
60241247Sbostic 	relmon(mtmp);
60341247Sbostic 	monfree(mtmp);
60441247Sbostic 	mtmp2->nmon = fmon;
60541247Sbostic 	fmon = mtmp2;
60641247Sbostic 	if(u.ustuck == mtmp) u.ustuck = mtmp2;
60741247Sbostic 	if(mtmp2->isshk) replshk(mtmp,mtmp2);
60841247Sbostic 	if(mtmp2->isgd) replgd(mtmp,mtmp2);
60941247Sbostic }
61041247Sbostic 
relmon(mon)61141247Sbostic relmon(mon)
61241247Sbostic register struct monst *mon;
61341247Sbostic {
61441247Sbostic 	register struct monst *mtmp;
61541247Sbostic 
61641247Sbostic 	if(mon == fmon) fmon = fmon->nmon;
61741247Sbostic 	else {
61841247Sbostic 		for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
61941247Sbostic 		mtmp->nmon = mon->nmon;
62041247Sbostic 	}
62141247Sbostic }
62241247Sbostic 
62341247Sbostic /* we do not free monsters immediately, in order to have their name
62441247Sbostic    available shortly after their demise */
62541247Sbostic struct monst *fdmon;	/* chain of dead monsters, need not to be saved */
62641247Sbostic 
monfree(mtmp)62741247Sbostic monfree(mtmp) register struct monst *mtmp; {
62841247Sbostic 	mtmp->nmon = fdmon;
62941247Sbostic 	fdmon = mtmp;
63041247Sbostic }
63141247Sbostic 
dmonsfree()63241247Sbostic dmonsfree(){
63341247Sbostic register struct monst *mtmp;
63441247Sbostic 	while(mtmp = fdmon){
63541247Sbostic 		fdmon = mtmp->nmon;
63641247Sbostic 		free((char *) mtmp);
63741247Sbostic 	}
63841247Sbostic }
63941247Sbostic 
unstuck(mtmp)64041247Sbostic unstuck(mtmp)
64141247Sbostic register struct monst *mtmp;
64241247Sbostic {
64341247Sbostic 	if(u.ustuck == mtmp) {
64441247Sbostic 		if(u.uswallow){
64541247Sbostic 			u.ux = mtmp->mx;
64641247Sbostic 			u.uy = mtmp->my;
64741247Sbostic 			u.uswallow = 0;
64841247Sbostic 			setsee();
64941247Sbostic 			docrt();
65041247Sbostic 		}
65141247Sbostic 		u.ustuck = 0;
65241247Sbostic 	}
65341247Sbostic }
65441247Sbostic 
killed(mtmp)65541247Sbostic killed(mtmp)
65641247Sbostic register struct monst *mtmp;
65741247Sbostic {
65841247Sbostic #ifdef lint
65941247Sbostic #define	NEW_SCORING
66041247Sbostic #endif lint
66141247Sbostic 	register int tmp,tmp2,nk,x,y;
66241247Sbostic 	register struct permonst *mdat;
66341247Sbostic 	extern long newuexp();
66441247Sbostic 
66541247Sbostic 	if(mtmp->cham) mtmp->data = PM_CHAMELEON;
66641247Sbostic 	mdat = mtmp->data;
66741247Sbostic 	if(Blind) pline("You destroy it!");
66841247Sbostic 	else {
66941247Sbostic 		pline("You destroy %s!",
67041247Sbostic 			mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
67141247Sbostic 	}
67241247Sbostic 	if(u.umconf) {
67341247Sbostic 		if(!Blind) pline("Your hands stop glowing blue.");
67441247Sbostic 		u.umconf = 0;
67541247Sbostic 	}
67641247Sbostic 
67741247Sbostic 	/* count killed monsters */
67841247Sbostic #define	MAXMONNO	100
67941247Sbostic 	nk = 1;		      /* in case we cannot find it in mons */
68041247Sbostic 	tmp = mdat - mons;    /* index in mons array (if not 'd', '@', ...) */
68141247Sbostic 	if(tmp >= 0 && tmp < CMNUM+2) {
68241247Sbostic 	    extern char fut_geno[];
68341247Sbostic 	    u.nr_killed[tmp]++;
68441247Sbostic 	    if((nk = u.nr_killed[tmp]) > MAXMONNO &&
68541247Sbostic 		!index(fut_geno, mdat->mlet))
68641247Sbostic 		    charcat(fut_geno,  mdat->mlet);
68741247Sbostic 	}
68841247Sbostic 
68941247Sbostic 	/* punish bad behaviour */
69041247Sbostic 	if(mdat->mlet == '@') Telepat = 0, u.uluck -= 2;
69141247Sbostic 	if(mtmp->mpeaceful || mtmp->mtame) u.uluck--;
69241247Sbostic 	if(mdat->mlet == 'u') u.uluck -= 5;
69341247Sbostic 	if((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN;
69441247Sbostic 
69541247Sbostic 	/* give experience points */
69641247Sbostic 	tmp = 1 + mdat->mlevel * mdat->mlevel;
69741247Sbostic 	if(mdat->ac < 3) tmp += 2*(7 - mdat->ac);
69841247Sbostic 	if(index("AcsSDXaeRTVWU&In:P", mdat->mlet))
69941247Sbostic 		tmp += 2*mdat->mlevel;
70041247Sbostic 	if(index("DeV&P",mdat->mlet)) tmp += (7*mdat->mlevel);
70141247Sbostic 	if(mdat->mlevel > 6) tmp += 50;
70241247Sbostic 	if(mdat->mlet == ';') tmp += 1000;
70341247Sbostic 
70441247Sbostic #ifdef NEW_SCORING
70541247Sbostic 	/* ------- recent addition: make nr of points decrease
70641247Sbostic 		   when this is not the first of this kind */
70741247Sbostic 	{ int ul = u.ulevel;
70841247Sbostic 	  int ml = mdat->mlevel;
70941247Sbostic 
71041247Sbostic 	if(ul < 14)    /* points are given based on present and future level */
71141247Sbostic 	    for(tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
71241247Sbostic 		if(u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4<<(tmp2-1)))/nk
71341247Sbostic 		    >= 10*pow((unsigned)(ul-1)))
71441247Sbostic 			if(++ul == 14) break;
71541247Sbostic 
71641247Sbostic 	tmp2 = ml - ul -1;
71741247Sbostic 	tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
71841247Sbostic 	if(!tmp) tmp = 1;
71941247Sbostic 	}
72041247Sbostic 	/* note: ul is not necessarily the future value of u.ulevel */
72141247Sbostic 	/* ------- end of recent valuation change ------- */
72241247Sbostic #endif NEW_SCORING
72341247Sbostic 
72441247Sbostic 	more_experienced(tmp,0);
72541247Sbostic 	flags.botl = 1;
72641247Sbostic 	while(u.ulevel < 14 && u.uexp >= newuexp()){
72741247Sbostic 		pline("Welcome to experience level %u.", ++u.ulevel);
72841247Sbostic 		tmp = rnd(10);
72941247Sbostic 		if(tmp < 3) tmp = rnd(10);
73041247Sbostic 		u.uhpmax += tmp;
73141247Sbostic 		u.uhp += tmp;
73241247Sbostic 		flags.botl = 1;
73341247Sbostic 	}
73441247Sbostic 
73541247Sbostic 	/* dispose of monster and make cadaver */
73641247Sbostic 	x = mtmp->mx;	y = mtmp->my;
73741247Sbostic 	mondead(mtmp);
73841247Sbostic 	tmp = mdat->mlet;
73941247Sbostic 	if(tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
74041247Sbostic 			/* note: the dead minotaur will be on top of it! */
74141247Sbostic 		mksobj_at(WAN_DIGGING, x, y);
74241247Sbostic 		/* if(cansee(x,y)) atl(x,y,fobj->olet); */
74341247Sbostic 		stackobj(fobj);
74441247Sbostic 	} else
74541247Sbostic #ifndef NOWORM
74641247Sbostic 	if(tmp == 'w') {
74741247Sbostic 		mksobj_at(WORM_TOOTH, x, y);
74841247Sbostic 		stackobj(fobj);
74941247Sbostic 	} else
75041247Sbostic #endif	NOWORM
75141247Sbostic 	if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
75241247Sbostic 
75341247Sbostic 	if(ACCESSIBLE(levl[x][y].typ))	/* might be mimic in wall or dead eel*/
75441247Sbostic 	    if(x != u.ux || y != u.uy)	/* might be here after swallowed */
75541247Sbostic 		if(index("NTVm&",mdat->mlet) || rn2(5)) {
75641247Sbostic 		register struct obj *obj2 = mkobj_at(tmp,x,y);
75741247Sbostic 		if(cansee(x,y))
75841247Sbostic 			atl(x,y,obj2->olet);
75941247Sbostic 		stackobj(obj2);
76041247Sbostic 	}
76141247Sbostic }
76241247Sbostic 
kludge(str,arg)76341247Sbostic kludge(str,arg)
76441247Sbostic register char *str,*arg;
76541247Sbostic {
76641247Sbostic 	if(Blind) {
76741247Sbostic 		if(*str == '%') pline(str,"It");
76841247Sbostic 		else pline(str,"it");
76941247Sbostic 	} else pline(str,arg);
77041247Sbostic }
77141247Sbostic 
rescham()77241247Sbostic rescham()	/* force all chameleons to become normal */
77341247Sbostic {
77441247Sbostic 	register struct monst *mtmp;
77541247Sbostic 
77641247Sbostic 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
77741247Sbostic 		if(mtmp->cham) {
77841247Sbostic 			mtmp->cham = 0;
77941247Sbostic 			(void) newcham(mtmp, PM_CHAMELEON);
78041247Sbostic 		}
78141247Sbostic }
78241247Sbostic 
newcham(mtmp,mdat)78341247Sbostic newcham(mtmp,mdat)	/* make a chameleon look like a new monster */
78441247Sbostic 			/* returns 1 if the monster actually changed */
78541247Sbostic register struct monst *mtmp;
78641247Sbostic register struct permonst *mdat;
78741247Sbostic {
78841247Sbostic 	register mhp, hpn, hpd;
78941247Sbostic 
79041247Sbostic 	if(mdat == mtmp->data) return(0);	/* still the same monster */
79141247Sbostic #ifndef NOWORM
79241247Sbostic 	if(mtmp->wormno) wormdead(mtmp);	/* throw tail away */
79341247Sbostic #endif NOWORM
79441247Sbostic 	if (u.ustuck == mtmp) {
79541247Sbostic 		if (u.uswallow) {
79641247Sbostic 			u.uswallow = 0;
79741247Sbostic 			u.uswldtim = 0;
79841247Sbostic 			mnexto (mtmp);
79941247Sbostic 			docrt ();
80041247Sbostic 			prme ();
80141247Sbostic 		}
80241247Sbostic 		u.ustuck = 0;
80341247Sbostic 	}
80441247Sbostic 	hpn = mtmp->mhp;
80541247Sbostic 	hpd = (mtmp->data->mlevel)*8;
80641247Sbostic 	if(!hpd) hpd = 4;
80741247Sbostic 	mtmp->data = mdat;
80841247Sbostic 	mhp = (mdat->mlevel)*8;
80941247Sbostic 	/* new hp: same fraction of max as before */
81041247Sbostic 	mtmp->mhp = 2 + (hpn*mhp)/hpd;
81141247Sbostic 	hpn = mtmp->mhpmax;
81241247Sbostic 	mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
81341247Sbostic 	mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
81441247Sbostic #ifndef NOWORM
81541247Sbostic 	if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
81641247Sbostic 			/* perhaps we should clear mtmp->mtame here? */
81741247Sbostic #endif NOWORM
81841247Sbostic 	unpmon(mtmp);	/* necessary for 'I' and to force pmon */
81941247Sbostic 	pmon(mtmp);
82041247Sbostic 	return(1);
82141247Sbostic }
82241247Sbostic 
82341247Sbostic mnexto(mtmp)	/* Make monster mtmp next to you (if possible) */
82441247Sbostic struct monst *mtmp;
82541247Sbostic {
82641247Sbostic 	extern coord enexto();
82741247Sbostic 	coord mm;
82841247Sbostic 	mm = enexto(u.ux, u.uy);
82941247Sbostic 	mtmp->mx = mm.x;
83041247Sbostic 	mtmp->my = mm.y;
83141247Sbostic 	pmon(mtmp);
83241247Sbostic }
83341247Sbostic 
ishuman(mtmp)83441247Sbostic ishuman(mtmp) register struct monst *mtmp; {
83541247Sbostic 	return(mtmp->data->mlet == '@');
83641247Sbostic }
83741247Sbostic 
setmangry(mtmp)83841247Sbostic setmangry(mtmp) register struct monst *mtmp; {
83941247Sbostic 	if(!mtmp->mpeaceful) return;
84041247Sbostic 	if(mtmp->mtame) return;
84141247Sbostic 	mtmp->mpeaceful = 0;
84241247Sbostic 	if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
84341247Sbostic }
84441247Sbostic 
84541247Sbostic /* not one hundred procent correct: now a snake may hide under an
84641247Sbostic    invisible object */
canseemon(mtmp)84741247Sbostic canseemon(mtmp)
84841247Sbostic register struct monst *mtmp;
84941247Sbostic {
85041247Sbostic 	return((!mtmp->minvis || See_invisible)
85141247Sbostic 		&& (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
85241247Sbostic 		&& cansee(mtmp->mx, mtmp->my));
85341247Sbostic }
854