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