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