Source:NetHack 3.4.0/mon.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to mon.c from the source code of NetHack 3.4.0. To link to a particular line, write [[NetHack 3.4.0/mon.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)mon.c	3.4	2002/03/09	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    /* If you're using precompiled headers, you don't want this either */
6.    #ifdef MICROPORT_BUG
7.    #define MKROOM_H
8.    #endif
9.    
10.   #include "hack.h"
11.   #include "mfndpos.h"
12.   #include "edog.h"
13.   #include <ctype.h>
14.   
15.   STATIC_DCL boolean FDECL(restrap,(struct monst *));
16.   STATIC_DCL long FDECL(mm_aggression, (struct monst *,struct monst *));
17.   #ifdef OVL2
18.   STATIC_DCL int NDECL(pick_animal);
19.   STATIC_DCL int FDECL(select_newcham_form, (struct monst *));
20.   STATIC_DCL void FDECL(kill_eggs, (struct obj *));
21.   #endif
22.   
23.   #ifdef REINCARNATION
24.   #define LEVEL_SPECIFIC_NOCORPSE(mdat) \
25.   	 (Is_rogue_level(&u.uz) || \
26.   	   (level.flags.graveyard && is_undead(mdat) && rn2(3)))
27.   #else
28.   #define LEVEL_SPECIFIC_NOCORPSE(mdat) \
29.   	   (level.flags.graveyard && is_undead(mdat) && rn2(3))
30.   #endif
31.   
32.   
33.   #if 0
34.   /* part of the original warning code which was replaced in 3.3.1 */
35.   #ifdef OVL1
36.   #define warnDelay 10
37.   long lastwarntime;
38.   int lastwarnlev;
39.   
40.   const char *warnings[] = {
41.   	"white", "pink", "red", "ruby", "purple", "black"
42.   };
43.   
44.   STATIC_DCL void NDECL(warn_effects);
45.   #endif /* OVL1 */
46.   #endif /* 0 */
47.   
48.   #ifndef OVLB
49.   STATIC_VAR short cham_to_pm[];
50.   #else
51.   STATIC_DCL struct obj *FDECL(make_corpse,(struct monst *));
52.   STATIC_DCL void FDECL(m_detach, (struct monst *, struct permonst *));
53.   STATIC_DCL void FDECL(lifesaved_monster, (struct monst *));
54.   
55.   /* convert the monster index of an undead to its living counterpart */
56.   int
57.   undead_to_corpse(mndx)
58.   int mndx;
59.   {
60.   	switch (mndx) {
61.   	case PM_KOBOLD_ZOMBIE:
62.   	case PM_KOBOLD_MUMMY:	mndx = PM_KOBOLD;  break;
63.   	case PM_DWARF_ZOMBIE:
64.   	case PM_DWARF_MUMMY:	mndx = PM_DWARF;  break;
65.   	case PM_GNOME_ZOMBIE:
66.   	case PM_GNOME_MUMMY:	mndx = PM_GNOME;  break;
67.   	case PM_ORC_ZOMBIE:
68.   	case PM_ORC_MUMMY:	mndx = PM_ORC;  break;
69.   	case PM_ELF_ZOMBIE:
70.   	case PM_ELF_MUMMY:	mndx = PM_ELF;  break;
71.   	case PM_VAMPIRE:
72.   	case PM_VAMPIRE_LORD:
73.   #if 0	/* DEFERRED */
74.   	case PM_VAMPIRE_MAGE:
75.   #endif
76.   	case PM_HUMAN_ZOMBIE:
77.   	case PM_HUMAN_MUMMY:	mndx = PM_HUMAN;  break;
78.   	case PM_GIANT_ZOMBIE:
79.   	case PM_GIANT_MUMMY:	mndx = PM_GIANT;  break;
80.   	case PM_ETTIN_ZOMBIE:
81.   	case PM_ETTIN_MUMMY:	mndx = PM_ETTIN;  break;
82.   	default:  break;
83.   	}
84.   	return mndx;
85.   }
86.   
87.   /* convert monster index to chameleon index */
88.   int
89.   pm_to_cham(mndx)
90.   int mndx;
91.   {
92.   	int mcham;
93.   
94.   	switch (mndx) {
95.   	case PM_CHAMELEON:	mcham = CHAM_CHAMELEON; break;
96.   	case PM_DOPPELGANGER:	mcham = CHAM_DOPPELGANGER; break;
97.   	case PM_SANDESTIN:	mcham = CHAM_SANDESTIN; break;
98.   	default: mcham = CHAM_ORDINARY; break;
99.   	}
100.  	return mcham;
101.  }
102.  
103.  /* convert chameleon index to monster index */
104.  STATIC_VAR short cham_to_pm[] = {
105.  		NON_PM,		/* placeholder for CHAM_ORDINARY */
106.  		PM_CHAMELEON,
107.  		PM_DOPPELGANGER,
108.  		PM_SANDESTIN,
109.  };
110.  
111.  #define KEEPTRAITS(mon)	(mon->isshk || mon->mtame || \
112.  			 (mon->data->geno & G_UNIQ) || is_reviver(mon->data) || \
113.  			 (mon->m_id == quest_status.leader_m_id))
114.  
115.  /* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
116.   * leave corpses.  Monsters which leave "special" corpses should have
117.   * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
118.   * etc....
119.   */
120.  STATIC_OVL struct obj *
121.  make_corpse(mtmp)
122.  register struct monst *mtmp;
123.  {
124.  	register struct permonst *mdat = mtmp->data;
125.  	int num;
126.  	struct obj *obj = (struct obj *)0;
127.  	int x = mtmp->mx, y = mtmp->my;
128.  	int mndx = monsndx(mdat);
129.  
130.  	switch(mndx) {
131.  	    case PM_GRAY_DRAGON:
132.  	    case PM_SILVER_DRAGON:
133.  #if 0	/* DEFERRED */
134.  	    case PM_SHIMMERING_DRAGON:
135.  #endif
136.  	    case PM_RED_DRAGON:
137.  	    case PM_ORANGE_DRAGON:
138.  	    case PM_WHITE_DRAGON:
139.  	    case PM_BLACK_DRAGON:
140.  	    case PM_BLUE_DRAGON:
141.  	    case PM_GREEN_DRAGON:
142.  	    case PM_YELLOW_DRAGON:
143.  		/* Make dragon scales.  This assumes that the order of the */
144.  		/* dragons is the same as the order of the scales.	   */
145.  		if (!rn2(mtmp->mrevived ? 20 : 3)) {
146.  		    num = GRAY_DRAGON_SCALES + monsndx(mdat) - PM_GRAY_DRAGON;
147.  		    obj = mksobj_at(num, x, y, FALSE, FALSE);
148.  		    obj->spe = 0;
149.  		    obj->cursed = obj->blessed = FALSE;
150.  		}
151.  		goto default_1;
152.  
153.  	    case PM_WHITE_UNICORN:
154.  	    case PM_GRAY_UNICORN:
155.  	    case PM_BLACK_UNICORN:
156.  		if (mtmp->mrevived && rn2(20)) {
157.  			if (canseemon(mtmp))
158.  			   pline("%s recently regrown horn crumbles to dust.",
159.  				s_suffix(Monnam(mtmp)));
160.  		} else
161.  			(void) mksobj_at(UNICORN_HORN, x, y, TRUE, FALSE);
162.  		goto default_1;
163.  	    case PM_LONG_WORM:
164.  		(void) mksobj_at(WORM_TOOTH, x, y, TRUE, FALSE);
165.  		goto default_1;
166.  	    case PM_VAMPIRE:
167.  	    case PM_VAMPIRE_LORD:
168.  		/* include mtmp in the mkcorpstat() call */
169.  		num = undead_to_corpse(mndx);
170.  		obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, TRUE);
171.  		obj->age -= 100;		/* this is an *OLD* corpse */
172.  		break;
173.  	    case PM_KOBOLD_MUMMY:
174.  	    case PM_DWARF_MUMMY:
175.  	    case PM_GNOME_MUMMY:
176.  	    case PM_ORC_MUMMY:
177.  	    case PM_ELF_MUMMY:
178.  	    case PM_HUMAN_MUMMY:
179.  	    case PM_GIANT_MUMMY:
180.  	    case PM_ETTIN_MUMMY:
181.  	    case PM_KOBOLD_ZOMBIE:
182.  	    case PM_DWARF_ZOMBIE:
183.  	    case PM_GNOME_ZOMBIE:
184.  	    case PM_ORC_ZOMBIE:
185.  	    case PM_ELF_ZOMBIE:
186.  	    case PM_HUMAN_ZOMBIE:
187.  	    case PM_GIANT_ZOMBIE:
188.  	    case PM_ETTIN_ZOMBIE:
189.  		num = undead_to_corpse(mndx);
190.  		obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, TRUE);
191.  		obj->age -= 100;		/* this is an *OLD* corpse */
192.  		break;
193.  	    case PM_IRON_GOLEM:
194.  		num = d(2,6);
195.  		while (num--)
196.  			obj = mksobj_at(IRON_CHAIN, x, y, TRUE, FALSE);
197.  		mtmp->mnamelth = 0;
198.  		break;
199.  	    case PM_GLASS_GOLEM:
200.  		num = d(2,4);   /* very low chance of creating all glass gems */
201.  		while (num--)
202.  			obj = mksobj_at((LAST_GEM + rnd(9)), x, y, TRUE, FALSE);
203.  		mtmp->mnamelth = 0;
204.  		break;
205.  	    case PM_CLAY_GOLEM:
206.  		obj = mksobj_at(ROCK, x, y, FALSE, FALSE);
207.  		obj->quan = (long)(rn2(20) + 50);
208.  		obj->owt = weight(obj);
209.  		mtmp->mnamelth = 0;
210.  		break;
211.  	    case PM_STONE_GOLEM:
212.  		obj = mkcorpstat(STATUE, (struct monst *)0,
213.  			mdat, x, y, FALSE);
214.  		break;
215.  	    case PM_WOOD_GOLEM:
216.  		num = d(2,4);
217.  		while(num--) {
218.  			obj = mksobj_at(QUARTERSTAFF, x, y, TRUE, FALSE);
219.  		}
220.  		mtmp->mnamelth = 0;
221.  		break;
222.  	    case PM_LEATHER_GOLEM:
223.  		num = d(2,4);
224.  		while(num--)
225.  			obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE, FALSE);
226.  		mtmp->mnamelth = 0;
227.  		break;
228.  	    case PM_GOLD_GOLEM:
229.  		/* Good luck gives more coins */
230.  		obj = mkgold((long)(200 - rnl(101)), x, y);
231.  		mtmp->mnamelth = 0;
232.  		break;
233.  	    case PM_PAPER_GOLEM:
234.  		num = rnd(4);
235.  		while (num--)
236.  			obj = mksobj_at(SCR_BLANK_PAPER, x, y, TRUE, FALSE);
237.  		mtmp->mnamelth = 0;
238.  		break;
239.  	    default_1:
240.  	    default:
241.  		if (mvitals[mndx].mvflags & G_NOCORPSE)
242.  		    return (struct obj *)0;
243.  		else	/* preserve the unique traits of some creatures */
244.  		    obj = mkcorpstat(CORPSE, KEEPTRAITS(mtmp) ? mtmp : 0,
245.  				     mdat, x, y, TRUE);
246.  		break;
247.  	}
248.  	/* All special cases should precede the G_NOCORPSE check */
249.  
250.  	/* if polymorph or undead turning has killed this monster,
251.  	   prevent the same attack beam from hitting its corpse */
252.  	if (flags.bypasses) bypass_obj(obj);
253.  
254.  	if (mtmp->mnamelth)
255.  	    obj = oname(obj, NAME(mtmp));
256.  
257.  #ifdef INVISIBLE_OBJECTS
258.  	/* Invisible monster ==> invisible corpse */
259.  	obj->oinvis = mtmp->minvis;
260.  #endif
261.  
262.  	stackobj(obj);
263.  	newsym(x, y);
264.  	return obj;
265.  }
266.  
267.  #endif /* OVLB */
268.  #ifdef OVL1
269.  
270.  #if 0
271.  /* part of the original warning code which was replaced in 3.3.1 */
272.  STATIC_OVL void
273.  warn_effects()
274.  {
275.      if (warnlevel == 100) {
276.  	if(!Blind && uwep &&
277.  	    (warnlevel > lastwarnlev || moves > lastwarntime + warnDelay)) {
278.  	    Your("%s %s!", aobjnam(uwep, "glow"),
279.  		hcolor(light_blue));
280.  	    lastwarnlev = warnlevel;
281.  	    lastwarntime = moves;
282.  	}
283.  	warnlevel = 0;
284.  	return;
285.      }
286.  
287.      if (warnlevel >= SIZE(warnings))
288.  	warnlevel = SIZE(warnings)-1;
289.      if (!Blind &&
290.  	    (warnlevel > lastwarnlev || moves > lastwarntime + warnDelay)) {
291.  	const char *which, *what, *how;
292.  	long rings = (EWarning & (LEFT_RING|RIGHT_RING));
293.  
294.  	if (rings) {
295.  	    what = Hallucination ? "mood ring" : "ring";
296.  	    how = "glows";	/* singular verb */
297.  	    if (rings == LEFT_RING) {
298.  		which = "left ";
299.  	    } else if (rings == RIGHT_RING) {
300.  		which = "right ";
301.  	    } else {		/* both */
302.  		which = "";
303.  		what = (const char *) makeplural(what);
304.  		how = "glow";	/* plural verb */
305.  	    }
306.  	    Your("%s%s %s %s!", which, what, how, hcolor(warnings[warnlevel]));
307.  	} else {
308.  	    if (Hallucination)
309.  		Your("spider-sense is tingling...");
310.  	    else
311.  		You_feel("apprehensive as you sense a %s flash.",
312.  		    warnings[warnlevel]);
313.  	}
314.  
315.  	lastwarntime = moves;
316.  	lastwarnlev = warnlevel;
317.      }
318.  }
319.  #endif /* 0 */
320.  
321.  /* check mtmp and water/lava for compatibility, 0 (survived), 1 (died) */
322.  int
323.  minliquid(mtmp)
324.  register struct monst *mtmp;
325.  {
326.      boolean inpool, inlava, infountain;
327.  
328.      inpool = is_pool(mtmp->mx,mtmp->my) &&
329.  	     !is_flyer(mtmp->data) && !is_floater(mtmp->data);
330.      inlava = is_lava(mtmp->mx,mtmp->my) &&
331.  	     !is_flyer(mtmp->data) && !is_floater(mtmp->data);
332.      infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
333.  
334.  #ifdef STEED
335.  	/* Flying and levitation keeps our steed out of the liquid */
336.  	/* (but not water-walking or swimming) */
337.  	if (mtmp == u.usteed && (Flying || Levitation))
338.  		return (0);
339.  #endif
340.  
341.      /* Gremlin multiplying won't go on forever since the hit points
342.       * keep going down, and when it gets to 1 hit point the clone
343.       * function will fail.
344.       */
345.      if (mtmp->data == &mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
346.  	if (split_mon(mtmp, (struct monst *)0))
347.  	    dryup(mtmp->mx, mtmp->my, FALSE);
348.  	if (inpool) water_damage(mtmp->minvent, FALSE, FALSE);
349.  	return (0);
350.      } else if (mtmp->data == &mons[PM_IRON_GOLEM] && inpool && !rn2(5)) {
351.  	int dam = d(2,6);
352.  	if (cansee(mtmp->mx,mtmp->my))
353.  	    pline("%s rusts.", Monnam(mtmp));
354.  	mtmp->mhp -= dam;
355.  	if (mtmp->mhpmax > dam) mtmp->mhpmax -= dam;
356.  	if (mtmp->mhp < 1) {
357.  	    mondead(mtmp);
358.  	    if (mtmp->mhp < 1) return (1);
359.  	}
360.  	water_damage(mtmp->minvent, FALSE, FALSE);
361.  	return (0);
362.      }
363.  
364.      if (inlava) {
365.  	/*
366.  	 * Lava effects much as water effects. Lava likers are able to
367.  	 * protect their stuff. Fire resistant monsters can only protect
368.  	 * themselves  --ALI
369.  	 */
370.  	if (!is_clinger(mtmp->data) && !likes_lava(mtmp->data)) {
371.  	    if (!resists_fire(mtmp)) {
372.  		if (cansee(mtmp->mx,mtmp->my))
373.  		    pline("%s burns to a crisp.", Monnam(mtmp));
374.  		mondead(mtmp);
375.  	    }
376.  	    else {
377.  		if (--mtmp->mhp < 1) {
378.  		    if (cansee(mtmp->mx,mtmp->my))
379.  			pline("%s surrenders to the fire.", Monnam(mtmp));
380.  		    mondead(mtmp);
381.  		}
382.  		else if (cansee(mtmp->mx,mtmp->my))
383.  		    pline("%s burns slightly.", Monnam(mtmp));
384.  	    }
385.  	    if (mtmp->mhp > 0) {
386.  		(void) fire_damage(mtmp->minvent, FALSE, FALSE,
387.  						mtmp->mx, mtmp->my);
388.  		rloc(mtmp);
389.  		return 0;
390.  	    }
391.  	    return (1);
392.  	}
393.      } else if (inpool) {
394.  	/* Most monsters drown in pools.  flooreffects() will take care of
395.  	 * water damage to dead monsters' inventory, but survivors need to
396.  	 * be handled here.  Swimmers are able to protect their stuff...
397.  	 */
398.  	if (!is_clinger(mtmp->data)
399.  	    && !is_swimmer(mtmp->data) && !amphibious(mtmp->data)) {
400.  	    if (cansee(mtmp->mx,mtmp->my)) {
401.  		    pline("%s drowns.", Monnam(mtmp));
402.  	    }
403.  	    mondead(mtmp);
404.  	    if (mtmp->mhp > 0) {
405.  		rloc(mtmp);
406.  		water_damage(mtmp->minvent, FALSE, FALSE);
407.  		return 0;
408.  	    }
409.  	    return (1);
410.  	}
411.      } else {
412.  	/* but eels have a difficult time outside */
413.  	if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)) {
414.  	    if(mtmp->mhp > 1) mtmp->mhp--;
415.  	    monflee(mtmp, 2, FALSE, FALSE);
416.  	}
417.      }
418.      return (0);
419.  }
420.  
421.  
422.  int
423.  mcalcmove(mon)
424.  struct monst *mon;
425.  {
426.      int mmove = mon->data->mmove;
427.  
428.      /* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
429.       *	     MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
430.       *	     both adjustments have negligible effect on higher speeds.
431.       */
432.      if (mon->mspeed == MSLOW)
433.  	mmove = (2 * mmove + 1) / 3;
434.      else if (mon->mspeed == MFAST)
435.  	mmove = (4 * mmove + 2) / 3;
436.  
437.  #ifdef STEED
438.      if (mon == u.usteed) {
439.  	if (u.ugallop && flags.mv) {
440.  	    /* average movement is 1.50 times normal */
441.  	    mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
442.  	}
443.      }
444.  #endif
445.  
446.      return mmove;
447.  }
448.  
449.  /* actions that happen once per ``turn, regardless of each
450.     individual monster's metabolism; some of these might need to
451.     be reclassified to occur more in proportion with movement rate */
452.  void
453.  mcalcdistress()
454.  {
455.      struct monst *mtmp;
456.  
457.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
458.  	if (DEADMONSTER(mtmp)) continue;
459.  
460.  	/* regenerate hit points */
461.  	mon_regen(mtmp, FALSE);
462.  
463.  	/* possibly polymorph shapechangers and lycanthropes */
464.  	if (mtmp->cham && !rn2(6))
465.  	    (void) newcham(mtmp, (struct permonst *)0, FALSE);
466.  	were_change(mtmp);
467.  
468.  	/* gradually time out temporary problems */
469.  	if (mtmp->mblinded && !--mtmp->mblinded)
470.  	    mtmp->mcansee = 1;
471.  	if (mtmp->mfrozen && !--mtmp->mfrozen)
472.  	    mtmp->mcanmove = 1;
473.  	if (mtmp->mfleetim && !--mtmp->mfleetim)
474.  	    mtmp->mflee = 0;
475.  
476.  	/* FIXME: mtmp->mlstmv ought to be updated here */
477.      }
478.  }
479.  
480.  int
481.  movemon()
482.  {
483.      register struct monst *mtmp, *nmtmp;
484.      register boolean somebody_can_move = FALSE;
485.  #if 0
486.      /* part of the original warning code which was replaced in 3.3.1 */
487.      warnlevel = 0;
488.  #endif
489.  
490.      /*
491.      Some of you may remember the former assertion here that
492.      because of deaths and other actions, a simple one-pass
493.      algorithm wasn't possible for movemon.  Deaths are no longer
494.      removed to the separate list fdmon; they are simply left in
495.      the chain with hit points <= 0, to be cleaned up at the end
496.      of the pass.
497.  
498.      The only other actions which cause monsters to be removed from
499.      the chain are level migrations and losedogs().  I believe losedogs()
500.      is a cleanup routine not associated with monster movements, and
501.      monsters can only affect level migrations on themselves, not others
502.      (hence the fetching of nmon before moving the monster).  Currently,
503.      monsters can jump into traps, read cursed scrolls of teleportation,
504.      and drink cursed potions of raise level to change levels.  These are
505.      all reflexive at this point.  Should one monster be able to level
506.      teleport another, this scheme would have problems.
507.      */
508.  
509.      for(mtmp = fmon; mtmp; mtmp = nmtmp) {
510.  	nmtmp = mtmp->nmon;
511.  
512.  	/* Find a monster that we have not treated yet.	 */
513.  	if(DEADMONSTER(mtmp))
514.  	    continue;
515.  	if(mtmp->movement < NORMAL_SPEED)
516.  	    continue;
517.  
518.  	mtmp->movement -= NORMAL_SPEED;
519.  	if (mtmp->movement >= NORMAL_SPEED)
520.  	    somebody_can_move = TRUE;
521.  
522.  	if (vision_full_recalc) vision_recalc(0);	/* vision! */
523.  
524.  	if (minliquid(mtmp)) continue;
525.  
526.  	if (is_hider(mtmp->data)) {
527.  	    /* unwatched mimics and piercers may hide again  [MRS] */
528.  	    if(restrap(mtmp))   continue;
529.  	    if(mtmp->m_ap_type == M_AP_FURNITURE ||
530.  				mtmp->m_ap_type == M_AP_OBJECT)
531.  		    continue;
532.  	    if(mtmp->mundetected) continue;
533.  	}
534.  
535.  	/* continue if the monster died fighting */
536.  	if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
537.  	    /* Note:
538.  	     *  Conflict does not take effect in the first round.
539.  	     *  Therefore, A monster when stepping into the area will
540.  	     *  get to swing at you.
541.  	     *
542.  	     *  The call to fightm() must be _last_.  The monster might
543.  	     *  have died if it returns 1.
544.  	     */
545.  	    if (couldsee(mtmp->mx,mtmp->my) &&
546.  		(distu(mtmp->mx,mtmp->my) <= BOLT_LIM*BOLT_LIM) &&
547.  							fightm(mtmp))
548.  		continue;	/* mon might have died */
549.  	}
550.  	if(dochugw(mtmp))		/* otherwise just move the monster */
551.  	    continue;
552.      }
553.  #if 0
554.      /* part of the original warning code which was replaced in 3.3.1 */
555.      if(warnlevel > 0)
556.  	warn_effects();
557.  #endif
558.  
559.      if (any_light_source())
560.  	vision_full_recalc = 1;	/* in case a mon moved with a light source */
561.      dmonsfree();	/* remove all dead monsters */
562.  
563.      /* a monster may have levteleported player -dlc */
564.      if (u.utotype) {
565.  	deferred_goto();
566.  	/* changed levels, so these monsters are dormant */
567.  	somebody_can_move = FALSE;
568.      }
569.  
570.      return somebody_can_move;
571.  }
572.  
573.  #endif /* OVL1 */
574.  #ifdef OVLB
575.  
576.  #define mstoning(obj)	(ofood(obj) && \
577.  					(touch_petrifies(&mons[(obj)->corpsenm]) || \
578.  					(obj)->corpsenm == PM_MEDUSA))
579.  
580.  /*
581.   * Maybe eat a metallic object (not just gold).
582.   * Return value: 0 => nothing happened, 1 => monster ate something,
583.   * 2 => monster died (it must have grown into a genocided form, but
584.   * that can't happen at present because nothing which eats objects
585.   * has young and old forms).
586.   */
587.  int
588.  meatmetal(mtmp)
589.  	register struct monst *mtmp;
590.  {
591.  	register struct obj *otmp;
592.  	struct permonst *ptr;
593.  	int poly, grow, heal, mstone;
594.  
595.  	/* If a pet, eating is handled separately, in dog.c */
596.  	if (mtmp->mtame) return 0;
597.  
598.  	/* Eats topmost metal object if it is there */
599.  	for (otmp = level.objects[mtmp->mx][mtmp->my];
600.  						otmp; otmp = otmp->nexthere) {
601.  	    if (mtmp->data == &mons[PM_RUST_MONSTER] && !is_rustprone(otmp))
602.  		continue;
603.  	    if (is_metallic(otmp) && !obj_resists(otmp, 5, 95) &&
604.  		touch_artifact(otmp,mtmp)) {
605.  		if (mtmp->data == &mons[PM_RUST_MONSTER] && otmp->oerodeproof) {
606.  		    if (canseemon(mtmp) && flags.verbose) {
607.  			pline("%s eats %s!",
608.  				Monnam(mtmp),
609.  				distant_name(otmp,doname));
610.  		    }
611.  		    /* The object's rustproofing is gone now */
612.  		    otmp->oerodeproof = 0;
613.  		    mtmp->mstun = 1;
614.  		    if (canseemon(mtmp) && flags.verbose) {
615.  			pline("%s spits %s out in disgust!",
616.  			      Monnam(mtmp), distant_name(otmp,doname));
617.  		    }
618.  		/* KMH -- Don't eat undigestable/choking objects */
619.  		} else if (otmp->otyp != AMULET_OF_STRANGULATION &&
620.  				otmp->otyp != RIN_SLOW_DIGESTION) {
621.  		    if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
622.  			pline("%s eats %s!", Monnam(mtmp),
623.  				distant_name(otmp,doname));
624.  		    else if (flags.soundok && flags.verbose)
625.  			You_hear("a crunching sound.");
626.  		    mtmp->meating = otmp->owt/2 + 1;
627.  		    /* Heal up to the object's weight in hp */
628.  		    if (mtmp->mhp < mtmp->mhpmax) {
629.  			mtmp->mhp += objects[otmp->otyp].oc_weight;
630.  			if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
631.  		    }
632.  		    if(otmp == uball) {
633.  			unpunish();
634.  			delobj(otmp);
635.  		    } else if (otmp == uchain) {
636.  			unpunish();	/* frees uchain */
637.  		    } else {
638.  			poly = polyfodder(otmp);
639.  			grow = mlevelgain(otmp);
640.  			heal = mhealup(otmp);
641.  			mstone = mstoning(otmp);
642.  			delobj(otmp);
643.  			ptr = mtmp->data;
644.  			if (poly) {
645.  			    if (newcham(mtmp, (struct permonst *)0, FALSE))
646.  				ptr = mtmp->data;
647.  			} else if (grow) {
648.  			    ptr = grow_up(mtmp, (struct monst *)0);
649.  			} else if (mstone) {
650.  			    if (poly_when_stoned(ptr)) {
651.  				mon_to_stone(mtmp);
652.  				ptr = mtmp->data;
653.  			    } else if (!resists_ston(mtmp)) {
654.  				if (canseemon(mtmp))
655.  				    pline("%s turns to stone!", Monnam(mtmp));
656.  				monstone(mtmp);
657.  				ptr = (struct permonst *)0;
658.  			    }
659.  			} else if (heal) {
660.  			    mtmp->mhp = mtmp->mhpmax;
661.  			}
662.  			if (!ptr) return 2;		 /* it died */
663.  		    }
664.  		    /* Left behind a pile? */
665.  		    if (rnd(25) < 3)
666.  			(void)mksobj_at(ROCK, mtmp->mx, mtmp->my, TRUE, FALSE);
667.  		    newsym(mtmp->mx, mtmp->my);
668.  		    return 1;
669.  		}
670.  	    }
671.  	}
672.  	return 0;
673.  }
674.  
675.  int
676.  meatobj(mtmp)		/* for gelatinous cubes */
677.  	register struct monst *mtmp;
678.  {
679.  	register struct obj *otmp, *otmp2;
680.  	struct permonst *ptr;
681.  	int poly, grow, heal, count = 0, ecount = 0;
682.  	char buf[BUFSZ];
683.  
684.  	buf[0] = '\0';
685.  	/* If a pet, eating is handled separately, in dog.c */
686.  	if (mtmp->mtame) return 0;
687.  
688.  	/* Eats organic objects, including cloth and wood, if there */
689.  	/* Engulfs others, except huge rocks and metal attached to player */
690.  	for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
691.  	    otmp2 = otmp->nexthere;
692.  	    if (is_organic(otmp) && !obj_resists(otmp, 5, 95) &&
693.  		    touch_artifact(otmp,mtmp)) {
694.  		if (otmp->otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm]) &&
695.  			!resists_ston(mtmp))
696.  		    continue;
697.  		if (otmp->otyp == AMULET_OF_STRANGULATION ||
698.  				otmp->otyp == RIN_SLOW_DIGESTION)
699.  		    continue;
700.  		++count;
701.  		if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
702.  		    pline("%s eats %s!", Monnam(mtmp),
703.  			    distant_name(otmp, doname));
704.  		else if (flags.soundok && flags.verbose)
705.  		    You_hear("a slurping sound.");
706.  		/* Heal up to the object's weight in hp */
707.  		if (mtmp->mhp < mtmp->mhpmax) {
708.  		    mtmp->mhp += objects[otmp->otyp].oc_weight;
709.  		    if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
710.  		}
711.  		if (Has_contents(otmp)) {
712.  		    register struct obj *otmp3;
713.  		    /* contents of eaten containers become engulfed; this
714.  		       is arbitrary, but otherwise g.cubes are too powerful */
715.  		    while ((otmp3 = otmp->cobj) != 0) {
716.  			obj_extract_self(otmp3);
717.  			if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) {
718.  			    otmp3->age = monstermoves - otmp3->age;
719.  			    start_corpse_timeout(otmp3);
720.  			}
721.  			(void) mpickobj(mtmp, otmp3);
722.  		    }
723.  		}
724.  		poly = polyfodder(otmp);
725.  		grow = mlevelgain(otmp);
726.  		heal = mhealup(otmp);
727.  		delobj(otmp);		/* munch */
728.  		ptr = mtmp->data;
729.  		if (poly) {
730.  		    if (newcham(mtmp, (struct permonst *)0, FALSE)) ptr = mtmp->data;
731.  		} else if (grow) {
732.  		    ptr = grow_up(mtmp, (struct monst *)0);
733.  		} else if (heal) {
734.  		    mtmp->mhp = mtmp->mhpmax;
735.  		}
736.  		/* in case it polymorphed or died */
737.  		if (ptr != &mons[PM_GELATINOUS_CUBE])
738.  		    return !ptr ? 2 : 1;
739.  	    } else if (otmp->oclass != ROCK_CLASS &&
740.  				    otmp != uball && otmp != uchain) {
741.  		++ecount;
742.  		if (ecount == 1) {
743.  			Sprintf(buf, "%s engulfs %s.", Monnam(mtmp),
744.  			    distant_name(otmp,doname));
745.  		} else if (ecount == 2)
746.  			Sprintf(buf, "%s engulfs several objects.", Monnam(mtmp));
747.  		obj_extract_self(otmp);
748.  		(void) mpickobj(mtmp, otmp);	/* slurp */
749.  	    }
750.  	    /* Engulf & devour is instant, so don't set meating */
751.  	    if (mtmp->minvis) newsym(mtmp->mx, mtmp->my);
752.  	}
753.  	if (ecount > 0) {
754.  	    if (cansee(mtmp->mx, mtmp->my) && flags.verbose && buf[0])
755.  		pline("%s", buf);
756.  	    else if (flags.soundok && flags.verbose)
757.  	    	You_hear("%s slurping sound%s.",
758.  			ecount == 1 ? "a" : "several",
759.  			ecount == 1 ? "" : "s");
760.  	}
761.  	return ((count > 0) || (ecount > 0)) ? 1 : 0;
762.  }
763.  
764.  void
765.  mpickgold(mtmp)
766.  	register struct monst *mtmp;
767.  {
768.      register struct obj *gold;
769.  
770.      if ((gold = g_at(mtmp->mx, mtmp->my)) != 0) {
771.  #ifndef GOLDOBJ
772.  	mtmp->mgold += gold->quan;
773.  	delobj(gold);
774.  	if (cansee(mtmp->mx, mtmp->my) ) {
775.  	    if (flags.verbose && !mtmp->isgd)
776.  		pline("%s picks up some gold.", Monnam(mtmp));
777.  #else
778.          obj_extract_self(gold);
779.          add_to_minv(mtmp, gold);
780.  	if (cansee(mtmp->mx, mtmp->my) ) {
781.  	    if (flags.verbose && !mtmp->isgd)
782.  		pline("%s picks up some money.", Monnam(mtmp));
783.  #endif
784.  	    newsym(mtmp->mx, mtmp->my);
785.  	}
786.      }
787.  }
788.  #endif /* OVLB */
789.  #ifdef OVL2
790.  
791.  boolean
792.  mpickstuff(mtmp, str)
793.  	register struct monst *mtmp;
794.  	register const char *str;
795.  {
796.  	register struct obj *otmp, *otmp2;
797.  
798.  /*	prevent shopkeepers from leaving the door of their shop */
799.  	if(mtmp->isshk && inhishop(mtmp)) return FALSE;
800.  
801.  	for(otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
802.  	    otmp2 = otmp->nexthere;
803.  /*	Nymphs take everything.  Most monsters don't pick up corpses. */
804.  	    if (!str ? searches_for_item(mtmp,otmp) :
805.  		  !!(index(str, otmp->oclass))) {
806.  		if (otmp->otyp == CORPSE && mtmp->data->mlet != S_NYMPH &&
807.  			/* let a handful of corpse types thru to can_carry() */
808.  			!touch_petrifies(&mons[otmp->corpsenm]) &&
809.  			otmp->corpsenm != PM_LIZARD &&
810.  			!acidic(&mons[otmp->corpsenm])) continue;
811.  		if (!touch_artifact(otmp,mtmp)) continue;
812.  		if (!can_carry(mtmp,otmp)) continue;
813.  		if (is_pool(mtmp->mx,mtmp->my)) continue;
814.  #ifdef INVISIBLE_OBJECTS
815.  		if (otmp->oinvis && !perceives(mtmp->data)) continue;
816.  #endif
817.  		if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
818.  			pline("%s picks up %s.", Monnam(mtmp),
819.  			      (distu(mtmp->my, mtmp->my) <= 5) ?
820.  				doname(otmp) : distant_name(otmp, doname));
821.  		obj_extract_self(otmp);
822.  		/* unblock point after extract, before pickup */
823.  		if (otmp->otyp == BOULDER)
824.  		    unblock_point(otmp->ox,otmp->oy);	/* vision */
825.  		(void) mpickobj(mtmp, otmp);	/* may merge and free otmp */
826.  		m_dowear(mtmp, FALSE);
827.  		newsym(mtmp->mx, mtmp->my);
828.  		return TRUE;			/* pick only one object */
829.  	    }
830.  	}
831.  	return FALSE;
832.  }
833.  
834.  #endif /* OVL2 */
835.  #ifdef OVL0
836.  
837.  int
838.  curr_mon_load(mtmp)
839.  register struct monst *mtmp;
840.  {
841.  	register int curload = 0;
842.  	register struct obj *obj;
843.  
844.  	for(obj = mtmp->minvent; obj; obj = obj->nobj) {
845.  		if(obj->otyp != BOULDER || !throws_rocks(mtmp->data))
846.  			curload += obj->owt;
847.  	}
848.  
849.  	return curload;
850.  }
851.  
852.  int
853.  max_mon_load(mtmp)
854.  register struct monst *mtmp;
855.  {
856.  	register long maxload;
857.  
858.  	/* Base monster carrying capacity is equal to human maximum
859.  	 * carrying capacity, or half human maximum if not strong.
860.  	 * (for a polymorphed player, the value used would be the
861.  	 * non-polymorphed carrying capacity instead of max/half max).
862.  	 * This is then modified by the ratio between the monster weights
863.  	 * and human weights.  Corpseless monsters are given a capacity
864.  	 * proportional to their size instead of weight.
865.  	 */
866.  	if (!mtmp->data->cwt)
867.  		maxload = (MAX_CARR_CAP * (long)mtmp->data->msize) / MZ_HUMAN;
868.  	else if (!strongmonst(mtmp->data)
869.  		|| (strongmonst(mtmp->data) && (mtmp->data->cwt > WT_HUMAN)))
870.  		maxload = (MAX_CARR_CAP * (long)mtmp->data->cwt) / WT_HUMAN;
871.  	else	maxload = MAX_CARR_CAP; /*strong monsters w/cwt <= WT_HUMAN*/
872.  
873.  	if (!strongmonst(mtmp->data)) maxload /= 2;
874.  
875.  	if (maxload < 1) maxload = 1;
876.  
877.  	return (int) maxload;
878.  }
879.  
880.  /* for restricting monsters' object-pickup */
881.  boolean
882.  can_carry(mtmp,otmp)
883.  struct monst *mtmp;
884.  struct obj *otmp;
885.  {
886.  	int otyp = otmp->otyp, newload = otmp->owt;
887.  	struct permonst *mdat = mtmp->data;
888.  
889.  	if (notake(mdat)) return FALSE;		/* can't carry anything */
890.  
891.  	if (otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm]) &&
892.  		!(mtmp->misc_worn_check & W_ARMG) && !resists_ston(mtmp))
893.  	    return FALSE;
894.  	if (otyp == CORPSE && is_rider(&mons[otmp->corpsenm]))
895.  	    return FALSE;
896.  	if (objects[otyp].oc_material == SILVER && hates_silver(mdat) &&
897.  		(otyp != BELL_OF_OPENING || !is_covetous(mdat)))
898.  	    return FALSE;
899.  
900.  #ifdef STEED
901.  	/* Steeds don't pick up stuff (to avoid shop abuse) */
902.  	if (mtmp == u.usteed) return (FALSE);
903.  #endif
904.  	if (mtmp->isshk) return(TRUE); /* no limit */
905.  	if (mtmp->mpeaceful && !mtmp->mtame) return(FALSE);
906.  	/* otherwise players might find themselves obligated to violate
907.  	 * their alignment if the monster takes something they need
908.  	 */
909.  
910.  	/* special--boulder throwers carry unlimited amounts of boulders */
911.  	if (throws_rocks(mdat) && otyp == BOULDER)
912.  		return(TRUE);
913.  
914.  	/* nymphs deal in stolen merchandise, but not boulders or statues */
915.  	if (mdat->mlet == S_NYMPH)
916.  		return (boolean)(otmp->oclass != ROCK_CLASS);
917.  
918.  	if (curr_mon_load(mtmp) + newload > max_mon_load(mtmp)) return FALSE;
919.  
920.  	return(TRUE);
921.  }
922.  
923.  /* return number of acceptable neighbour positions */
924.  int
925.  mfndpos(mon, poss, info, flag)
926.  	register struct monst *mon;
927.  	coord *poss;	/* coord poss[9] */
928.  	long *info;	/* long info[9] */
929.  	long flag;
930.  {
931.  	struct permonst *mdat = mon->data;
932.  	register xchar x,y,nx,ny;
933.  	register int cnt = 0;
934.  	register uchar ntyp;
935.  	uchar nowtyp;
936.  	boolean wantpool,poolok,lavaok,nodiag;
937.  	int maxx, maxy;
938.  
939.  	x = mon->mx;
940.  	y = mon->my;
941.  	nowtyp = levl[x][y].typ;
942.  
943.  	nodiag = (mdat == &mons[PM_GRID_BUG]);
944.  	wantpool = mdat->mlet == S_EEL;
945.  	poolok = is_flyer(mdat) || is_clinger(mdat) ||
946.  		 (is_swimmer(mdat) && !wantpool);
947.  	lavaok = is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat);
948.  
949.  nexttry:	/* eels prefer the water, but if there is no water nearby,
950.  		   they will crawl over land */
951.  	if(mon->mconf) {
952.  		flag |= ALLOW_ALL;
953.  		flag &= ~NOTONL;
954.  	}
955.  	if(!mon->mcansee)
956.  		flag |= ALLOW_SSM;
957.  	maxx = min(x+1,COLNO-1);
958.  	maxy = min(y+1,ROWNO-1);
959.  	for(nx = max(1,x-1); nx <= maxx; nx++)
960.  	  for(ny = max(0,y-1); ny <= maxy; ny++) {
961.  	    if(nx == x && ny == y) continue;
962.  	    if(IS_ROCK(ntyp = levl[nx][ny].typ) &&
963.  	       !((flag & ALLOW_WALL) && may_passwall(nx,ny)) &&
964.  	       !((flag & ALLOW_DIG) && may_dig(nx,ny))) continue;
965.  	    /* KMH -- Added iron bars */
966.  	    if (ntyp == IRONBARS &&
967.  	    	!((flag & ALLOW_WALL) && may_passwall(nx,ny))) continue;
968.  	    if(IS_DOOR(ntyp) && !amorphous(mdat) &&
969.  	       ((levl[nx][ny].doormask & D_CLOSED && !(flag & OPENDOOR)) ||
970.  		(levl[nx][ny].doormask & D_LOCKED && !(flag & UNLOCKDOOR))
971.  	       ) && !(flag & (ALLOW_WALL|ALLOW_DIG|BUSTDOOR))) continue;
972.  	    if(nx != x && ny != y && (nodiag ||
973.  #ifdef REINCARNATION
974.  	       ((IS_DOOR(nowtyp) &&
975.  		 ((levl[x][y].doormask & ~D_BROKEN) || Is_rogue_level(&u.uz))) ||
976.  		(IS_DOOR(ntyp) &&
977.  		 ((levl[nx][ny].doormask & ~D_BROKEN) || Is_rogue_level(&u.uz))))
978.  #else
979.  	       ((IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN)) ||
980.  		(IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN)))
981.  #endif
982.  	       ))
983.  		continue;
984.  	    if((is_pool(nx,ny) == wantpool || poolok) &&
985.  	       (lavaok || !is_lava(nx,ny))) {
986.  		int dispx, dispy;
987.  		boolean monseeu = (mon->mcansee && (!Invis || perceives(mdat)));
988.  		boolean checkobj = OBJ_AT(nx,ny);
989.  
990.  		/* Displacement also displaces the Elbereth/scare monster,
991.  		 * as long as you are visible.
992.  		 */
993.  		if(Displaced && monseeu && (mon->mux==nx) && (mon->muy==ny)) {
994.  		    dispx = u.ux;
995.  		    dispy = u.uy;
996.  		} else {
997.  		    dispx = nx;
998.  		    dispy = ny;
999.  		}
1000. 
1001. 		info[cnt] = 0;
1002. 		if ((checkobj || Displaced) && onscary(dispx, dispy, mon)) {
1003. 		    if(!(flag & ALLOW_SSM)) continue;
1004. 		    info[cnt] |= ALLOW_SSM;
1005. 		}
1006. 		if((nx == u.ux && ny == u.uy) ||
1007. 		   (nx == mon->mux && ny == mon->muy)) {
1008. 			if (nx == u.ux && ny == u.uy) {
1009. 				/* If it's right next to you, it found you,
1010. 				 * displaced or no.  We must set mux and muy
1011. 				 * right now, so when we return we can tell
1012. 				 * that the ALLOW_U means to attack _you_ and
1013. 				 * not the image.
1014. 				 */
1015. 				mon->mux = u.ux;
1016. 				mon->muy = u.uy;
1017. 			}
1018. 			if(!(flag & ALLOW_U)) continue;
1019. 			info[cnt] |= ALLOW_U;
1020. 		} else {
1021. 			if(MON_AT(nx, ny)) {
1022. 				struct monst *mtmp2 = m_at(nx, ny);
1023. 				long mmflag = flag | mm_aggression(mon, mtmp2);
1024. 
1025. 				if (!(mmflag & ALLOW_M)) continue;
1026. 				info[cnt] |= ALLOW_M;
1027. 				if (mtmp2->mtame) {
1028. 					if (!(mmflag & ALLOW_TM)) continue;
1029. 					info[cnt] |= ALLOW_TM;
1030. 				}
1031. 			}
1032. 			/* Note: ALLOW_SANCT only prevents movement, not */
1033. 			/* attack, into a temple. */
1034. 			if(level.flags.has_temple &&
1035. 			   *in_rooms(nx, ny, TEMPLE) &&
1036. 			   !*in_rooms(x, y, TEMPLE) &&
1037. 			   in_your_sanctuary((struct monst *)0, nx, ny)) {
1038. 				if(!(flag & ALLOW_SANCT)) continue;
1039. 				info[cnt] |= ALLOW_SANCT;
1040. 			}
1041. 		}
1042. 		if(checkobj && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
1043. 			if(flag & NOGARLIC) continue;
1044. 			info[cnt] |= NOGARLIC;
1045. 		}
1046. 		if(checkobj && sobj_at(BOULDER, nx, ny)) {
1047. 			if(!(flag & ALLOW_ROCK)) continue;
1048. 			info[cnt] |= ALLOW_ROCK;
1049. 		}
1050. 		if (monseeu && onlineu(nx,ny)) {
1051. 			if(flag & NOTONL) continue;
1052. 			info[cnt] |= NOTONL;
1053. 		}
1054. 		if (nx != x && ny != y && bad_rock(mdat, x, ny)
1055. 			    && bad_rock(mdat, nx, y)
1056. 			    && (bigmonst(mdat) || (curr_mon_load(mon) > 600)))
1057. 			continue;
1058. 		/* The monster avoids a particular type of trap if it's familiar
1059. 		 * with the trap type.  Pets get ALLOW_TRAPS and checking is
1060. 		 * done in dogmove.c.  In either case, "harmless" traps are
1061. 		 * neither avoided nor marked in info[].
1062. 		 */
1063. 		{ register struct trap *ttmp = t_at(nx, ny);
1064. 		    if(ttmp) {
1065. 			if(ttmp->ttyp >= TRAPNUM || ttmp->ttyp == 0)  {
1066. impossible("A monster looked at a very strange trap of type %d.", ttmp->ttyp);
1067. 			    continue;
1068. 			}
1069. 			if ((ttmp->ttyp != RUST_TRAP
1070. 					|| mdat == &mons[PM_IRON_GOLEM])
1071. 				&& ttmp->ttyp != STATUE_TRAP
1072. 				&& ((ttmp->ttyp != PIT
1073. 				    && ttmp->ttyp != SPIKED_PIT
1074. 				    && ttmp->ttyp != TRAPDOOR
1075. 				    && ttmp->ttyp != HOLE)
1076. 				      || (!is_flyer(mdat)
1077. 				    && !is_floater(mdat)
1078. 				    && !is_clinger(mdat))
1079. 				      || In_sokoban(&u.uz))
1080. 				&& (ttmp->ttyp != SLP_GAS_TRAP ||
1081. 				    !resists_sleep(mon))
1082. 				&& (ttmp->ttyp != BEAR_TRAP ||
1083. 				    (mdat->msize > MZ_SMALL &&
1084. 				     !amorphous(mdat) && !is_flyer(mdat)))
1085. 				&& (ttmp->ttyp != FIRE_TRAP ||
1086. 				    !resists_fire(mon))
1087. 				&& (ttmp->ttyp != SQKY_BOARD || !is_flyer(mdat))
1088. 				&& (ttmp->ttyp != WEB || (!amorphous(mdat) &&
1089. 				    !webmaker(mdat)))
1090. 			) {
1091. 			    if (!(flag & ALLOW_TRAPS)) {
1092. 				if (mon->mtrapseen & (1L << (ttmp->ttyp - 1)))
1093. 				    continue;
1094. 			    }
1095. 			    info[cnt] |= ALLOW_TRAPS;
1096. 			}
1097. 		    }
1098. 		}
1099. 		poss[cnt].x = nx;
1100. 		poss[cnt].y = ny;
1101. 		cnt++;
1102. 	    }
1103. 	}
1104. 	if(!cnt && wantpool && !is_pool(x,y)) {
1105. 		wantpool = FALSE;
1106. 		goto nexttry;
1107. 	}
1108. 	return(cnt);
1109. }
1110. 
1111. #endif /* OVL0 */
1112. #ifdef OVL1
1113. 
1114. /* Monster against monster special attacks; for the specified monster
1115.    combinations, this allows one monster to attack another adjacent one
1116.    in the absence of Conflict.  There is no provision for targetting
1117.    other monsters; just hand to hand fighting when they happen to be
1118.    next to each other. */
1119. STATIC_OVL long
1120. mm_aggression(magr, mdef)
1121. struct monst *magr,	/* monster that is currently deciding where to move */
1122. 	     *mdef;	/* another monster which is next to it */
1123. {
1124. 	/* supposedly purple worms are attracted to shrieking because they
1125. 	   like to eat shriekers, so attack the latter when feasible */
1126. 	if (magr->data == &mons[PM_PURPLE_WORM] &&
1127. 		mdef->data == &mons[PM_SHRIEKER])
1128. 	    return ALLOW_M|ALLOW_TM;
1129. 	/* Various other combinations such as dog vs cat, cat vs rat, and
1130. 	   elf vs orc have been suggested.  For the time being we don't
1131. 	   support those. */
1132. 	return 0L;
1133. }
1134. 
1135. boolean
1136. monnear(mon, x, y)
1137. register struct monst *mon;
1138. register int x,y;
1139. /* Is the square close enough for the monster to move or attack into? */
1140. {
1141. 	register int distance = dist2(mon->mx, mon->my, x, y);
1142. 	if (distance==2 && mon->data==&mons[PM_GRID_BUG]) return 0;
1143. 	return((boolean)(distance < 3));
1144. }
1145. 
1146. /* really free dead monsters */
1147. void
1148. dmonsfree()
1149. {
1150.     struct monst **mtmp;
1151.     int count = 0;
1152. 
1153.     for (mtmp = &fmon; *mtmp;) {
1154. 	if ((*mtmp)->mhp <= 0) {
1155. 	    struct monst *freetmp = *mtmp;
1156. 	    *mtmp = (*mtmp)->nmon;
1157. 	    dealloc_monst(freetmp);
1158. 	    count++;
1159. 	} else
1160. 	    mtmp = &(*mtmp)->nmon;
1161.     }
1162. 
1163.     if (count != iflags.purge_monsters)
1164. 	impossible("dmonsfree: %d removed doesn't match %d pending",
1165. 		   count, iflags.purge_monsters);
1166.     iflags.purge_monsters = 0;
1167. }
1168. 
1169. #endif /* OVL1 */
1170. #ifdef OVLB
1171. 
1172. /* called when monster is moved to larger structure */
1173. void
1174. replmon(mtmp, mtmp2)
1175. register struct monst *mtmp, *mtmp2;
1176. {
1177.     struct obj *otmp;
1178. 
1179.     /* transfer the monster's inventory */
1180.     for (otmp = mtmp2->minvent; otmp; otmp = otmp->nobj) {
1181. #ifdef DEBUG
1182. 	if (otmp->where != OBJ_MINVENT || otmp->ocarry != mtmp)
1183. 	    panic("replmon: minvent inconsistency");
1184. #endif
1185. 	otmp->ocarry = mtmp2;
1186.     }
1187.     mtmp->minvent = 0;
1188. 
1189.     /* remove the old monster from the map and from `fmon' list */
1190.     relmon(mtmp);
1191. 
1192.     /* finish adding its replacement */
1193. #ifdef STEED
1194.     if (mtmp == u.usteed) ; else	/* don't place steed onto the map */
1195. #endif
1196.     place_monster(mtmp2, mtmp2->mx, mtmp2->my);
1197.     if (mtmp2->wormno)	    /* update level.monsters[wseg->wx][wseg->wy] */
1198. 	place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
1199.     if (emits_light(mtmp2->data)) {
1200. 	/* since this is so rare, we don't have any `mon_move_light_source' */
1201. 	new_light_source(mtmp2->mx, mtmp2->my,
1202. 			 emits_light(mtmp2->data),
1203. 			 LS_MONSTER, (genericptr_t)mtmp2);
1204. 	/* here we rely on the fact that `mtmp' hasn't actually been deleted */
1205. 	del_light_source(LS_MONSTER, (genericptr_t)mtmp);
1206.     }
1207.     mtmp2->nmon = fmon;
1208.     fmon = mtmp2;
1209.     if (u.ustuck == mtmp) u.ustuck = mtmp2;
1210. #ifdef STEED
1211.     if (u.usteed == mtmp) u.usteed = mtmp2;
1212. #endif
1213.     if (mtmp2->isshk) replshk(mtmp,mtmp2);
1214. 
1215.     /* discard the old monster */
1216.     dealloc_monst(mtmp);
1217. }
1218. 
1219. /* release mon from display and monster list */
1220. void
1221. relmon(mon)
1222. register struct monst *mon;
1223. {
1224. 	register struct monst *mtmp;
1225. 
1226. 	if (fmon == (struct monst *)0)  panic ("relmon: no fmon available.");
1227. 
1228. 	remove_monster(mon->mx, mon->my);
1229. 
1230. 	if(mon == fmon) fmon = fmon->nmon;
1231. 	else {
1232. 		for(mtmp = fmon; mtmp && mtmp->nmon != mon; mtmp = mtmp->nmon) ;
1233. 		if(mtmp)    mtmp->nmon = mon->nmon;
1234. 		else	    panic("relmon: mon not in list.");
1235. 	}
1236. }
1237. 
1238. /* remove effects of mtmp from other data structures */
1239. STATIC_OVL void
1240. m_detach(mtmp, mptr)
1241. struct monst *mtmp;
1242. struct permonst *mptr;	/* reflects mtmp->data _prior_ to mtmp's death */
1243. {
1244. 	if (mtmp->mleashed) m_unleash(mtmp, FALSE);
1245. 	    /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
1246. 	mtmp->mtrapped = 0;
1247. 	mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */
1248. 	relobj(mtmp, 0, FALSE);
1249. 	remove_monster(mtmp->mx, mtmp->my);
1250. 	if (emits_light(mptr))
1251. 	    del_light_source(LS_MONSTER, (genericptr_t)mtmp);
1252. 	newsym(mtmp->mx,mtmp->my);
1253. 	unstuck(mtmp);
1254. 	fill_pit(mtmp->mx, mtmp->my);
1255. 
1256. 	if(mtmp->isshk) shkgone(mtmp);
1257. 	if(mtmp->wormno) wormgone(mtmp);
1258. 	iflags.purge_monsters++;
1259. }
1260. 
1261. /* find the worn amulet of life saving which will save a monster */
1262. struct obj *
1263. mlifesaver(mon)
1264. struct monst *mon;
1265. {
1266. 	if (!nonliving(mon->data)) {
1267. 	    struct obj *otmp = which_armor(mon, W_AMUL);
1268. 
1269. 	    if (otmp && otmp->otyp == AMULET_OF_LIFE_SAVING)
1270. 		return otmp;
1271. 	}
1272. 	return (struct obj *)0;
1273. }
1274. 
1275. STATIC_OVL void
1276. lifesaved_monster(mtmp)
1277. struct monst *mtmp;
1278. {
1279. 	struct obj *lifesave = mlifesaver(mtmp);
1280. 
1281. 	if (lifesave) {
1282. 		/* not canseemon; amulets are on the head, so you don't want */
1283. 		/* to show this for a long worm with only a tail visible. */
1284. 		/* Nor do you check invisibility, because glowing and disinte- */
1285. 		/* grating amulets are always visible. */
1286. 		if (cansee(mtmp->mx, mtmp->my)) {
1287. 			pline("But wait...");
1288. 			pline("%s medallion begins to glow!",
1289. 				s_suffix(Monnam(mtmp)));
1290. 			makeknown(AMULET_OF_LIFE_SAVING);
1291. 			pline("%s looks much better!", Monnam(mtmp));
1292. 			pline_The("medallion crumbles to dust!");
1293. 		}
1294. 		m_useup(mtmp, lifesave);
1295. 		mtmp->mcanmove = 1;
1296. 		mtmp->mfrozen = 0;
1297. 		if (mtmp->mtame && !mtmp->isminion) {
1298. 			struct edog *edog = EDOG(mtmp);
1299. 			if (edog->hungrytime < moves+500)
1300. 				edog->hungrytime = moves+500;
1301. 			if (edog->mhpmax_penalty) {
1302. 				/* was starving */
1303. 				mtmp->mhpmax += edog->mhpmax_penalty;
1304. 				edog->mhpmax_penalty = 0;
1305. 			}
1306. 			wary_dog(mtmp, FALSE);
1307. 		}
1308. 		if (mtmp->mhpmax <= 0) mtmp->mhpmax = 10;
1309. 		mtmp->mhp = mtmp->mhpmax;
1310. 		if (mvitals[monsndx(mtmp->data)].mvflags & G_GENOD) {
1311. 			if (cansee(mtmp->mx, mtmp->my))
1312. 			    pline("Unfortunately %s is still genocided...",
1313. 				mon_nam(mtmp));
1314. 		} else
1315. 			return;
1316. 	}
1317. 	mtmp->mhp = 0;
1318. }
1319. 
1320. void
1321. mondead(mtmp)
1322. register struct monst *mtmp;
1323. {
1324. 	struct permonst *mptr;
1325. 	int tmp;
1326. 
1327. 	if(mtmp->isgd) {
1328. 		/* if we're going to abort the death, it *must* be before
1329. 		 * the m_detach or there will be relmon problems later */
1330. 		if(!grddead(mtmp)) return;
1331. 	}
1332. 	lifesaved_monster(mtmp);
1333. 	if (mtmp->mhp > 0) return;
1334. 
1335. #ifdef STEED
1336. 	/* Player is thrown from his steed when it dies */
1337. 	if (mtmp == u.usteed)
1338. 		dismount_steed(DISMOUNT_GENERIC);
1339. #endif
1340. 
1341. 	mptr = mtmp->data;		/* save this for m_detach() */
1342. 	/* restore chameleon, lycanthropes to true form at death */
1343. 	if (mtmp->cham)
1344. 	    set_mon_data(mtmp, &mons[cham_to_pm[mtmp->cham]], -1);
1345. 	else if (mtmp->data == &mons[PM_WEREJACKAL])
1346. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1347. 	else if (mtmp->data == &mons[PM_WEREWOLF])
1348. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1349. 	else if (mtmp->data == &mons[PM_WERERAT])
1350. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1351. 
1352. 	/* if MAXMONNO monsters of a given type have died, and it
1353. 	 * can be done, extinguish that monster.
1354. 	 *
1355. 	 * mvitals[].died does double duty as total number of dead monsters
1356. 	 * and as experience factor for the player killing more monsters.
1357. 	 * this means that a dragon dying by other means reduces the
1358. 	 * experience the player gets for killing a dragon directly; this
1359. 	 * is probably not too bad, since the player likely finagled the
1360. 	 * first dead dragon via ring of conflict or pets, and extinguishing
1361. 	 * based on only player kills probably opens more avenues of abuse
1362. 	 * for rings of conflict and such.
1363. 	 */
1364. 	tmp = monsndx(mtmp->data);
1365. 	if (mvitals[tmp].died < 255) mvitals[tmp].died++;
1366. 
1367. 	/* if it's a (possibly polymorphed) quest leader, mark him as dead */
1368. 	if (mtmp->m_id == quest_status.leader_m_id)
1369. 	    quest_status.leader_is_dead = TRUE;
1370. #ifdef MAIL
1371. 	/* if the mail daemon dies, no more mail delivery.  -3. */
1372. 	if (tmp == PM_MAIL_DAEMON) mvitals[tmp].mvflags |= G_GENOD;
1373. #endif
1374. 
1375. #ifdef KOPS
1376. 	if (mtmp->data->mlet == S_KOP) {
1377. 	    /* Dead Kops may come back. */
1378. 	    switch(rnd(5)) {
1379. 		case 1:	     /* returns near the stairs */
1380. 			(void) makemon(mtmp->data,xdnstair,ydnstair,NO_MM_FLAGS);
1381. 			break;
1382. 		case 2:	     /* randomly */
1383. 			(void) makemon(mtmp->data,0,0,NO_MM_FLAGS);
1384. 			break;
1385. 		default:
1386. 			break;
1387. 	    }
1388. 	}
1389. #endif
1390. 	if(mtmp->iswiz) wizdead();
1391. 	if(mtmp->data->msound == MS_NEMESIS) nemdead();
1392. 	if(glyph_is_invisible(levl[mtmp->mx][mtmp->my].glyph))
1393. 	    unmap_object(mtmp->mx, mtmp->my);
1394. 	m_detach(mtmp, mptr);
1395. }
1396. 
1397. /* TRUE if corpse might be dropped, magr may die if mon was swallowed */
1398. boolean
1399. corpse_chance(mon, magr, was_swallowed)
1400. struct monst *mon;
1401. struct monst *magr;			/* killer, if swallowed */
1402. boolean was_swallowed;			/* digestion */
1403. {
1404. 	struct permonst *mdat = mon->data;
1405. 	int i, tmp;
1406. 
1407. 	if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1408. 	    if (cansee(mon->mx, mon->my) && !was_swallowed)
1409. 		pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
1410. 	    return FALSE;
1411. 	}
1412. 
1413. 	/* Gas spores always explode upon death */
1414. 	for(i = 0; i < NATTK; i++) {
1415. 	    if (mdat->mattk[i].aatyp == AT_BOOM) {
1416. 	    	if (mdat->mattk[i].damn)
1417. 	    	    tmp = d((int)mdat->mattk[i].damn,
1418. 	    	    		(int)mdat->mattk[i].damd);
1419. 	    	else if(mdat->mattk[i].damd)
1420. 	    	    tmp = d((int)mdat->mlevel+1, (int)mdat->mattk[i].damd);
1421. 	    	else tmp = 0;
1422. 		if (Half_physical_damage) tmp = (tmp+1) / 2;
1423. 		if (was_swallowed && magr) {
1424. 		    if (magr == &youmonst) {
1425. 			There("is an explosion in your %s!",
1426. 			      body_part(STOMACH));
1427. 			Sprintf(killer_buf, "%s explosion",
1428. 				s_suffix(mdat->mname));
1429. 			losehp(tmp, killer_buf, KILLED_BY_AN);
1430. 		    } else {
1431. 			if (flags.soundok) You_hear("an explosion.");
1432. 			magr->mhp -= tmp;
1433. 			if (magr->mhp < 1) mondied(magr);
1434. 			if (magr->mhp < 1) { /* maybe lifesaved */
1435. 			    if (canspotmon(magr))
1436. 				pline("%s rips open!", Monnam(magr));
1437. 			} else if (canseemon(magr))
1438. 			    pline("%s seems to have indigestion.",
1439. 				  Monnam(magr));
1440. 		    }
1441. 
1442. 		    return FALSE;
1443. 		}
1444. 
1445. 	    	Sprintf(killer_buf, "%s explosion", s_suffix(mdat->mname));
1446. 	    	killer = killer_buf;
1447. 	    	killer_format = KILLED_BY_AN;
1448. 	    	explode(mon->mx, mon->my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS); 
1449. 	    	return (FALSE);
1450. 	    }
1451.   	}
1452. 
1453. 	/* must duplicate this below check in xkilled() since it results in
1454. 	 * creating no objects as well as no corpse
1455. 	 */
1456. 	if (LEVEL_SPECIFIC_NOCORPSE(mdat))
1457. 		return FALSE;
1458. 
1459. 	if (bigmonst(mdat) || mdat == &mons[PM_LIZARD]
1460. 		   || is_golem(mdat)
1461. 		   || is_mplayer(mdat)
1462. 		   || is_rider(mdat))
1463. 		return TRUE;
1464. 	return (boolean) (!rn2((int)
1465. 		(2 + ((int)(mdat->geno & G_FREQ)<2) + verysmall(mdat))));
1466. }
1467. 
1468. /* drop (perhaps) a cadaver and remove monster */
1469. void
1470. mondied(mdef)
1471. register struct monst *mdef;
1472. {
1473. 	mondead(mdef);
1474. 	if (mdef->mhp > 0) return;	/* lifesaved */
1475. 
1476. 	if (corpse_chance(mdef, (struct monst *)0, FALSE))
1477. 		(void) make_corpse(mdef);
1478. }
1479. 
1480. /* monster disappears, not dies */
1481. void
1482. mongone(mdef)
1483. register struct monst *mdef;
1484. {
1485. #ifdef STEED
1486. 	/* Player is thrown from his steed when it disappears */
1487. 	if (mdef == u.usteed)
1488. 		dismount_steed(DISMOUNT_GENERIC);
1489. #endif
1490. 
1491. 	discard_minvent(mdef);	/* release monster's inventory */
1492. #ifndef GOLDOBJ
1493. 	mdef->mgold = 0L;
1494. #endif
1495. 	m_detach(mdef, mdef->data);
1496. }
1497. 
1498. /* drop a statue or rock and remove monster */
1499. void
1500. monstone(mdef)
1501. register struct monst *mdef;
1502. {
1503. 	struct obj *otmp, *obj;
1504. 	xchar x = mdef->mx, y = mdef->my;
1505. 	boolean wasinside = FALSE;
1506. 
1507. 	/* we have to make the statue before calling mondead, to be able to
1508. 	 * put inventory in it, and we have to check for lifesaving before
1509. 	 * making the statue....
1510. 	 */
1511. 	lifesaved_monster(mdef);
1512. 	if (mdef->mhp > 0) return;
1513. 
1514. 	mdef->mtrapped = 0;	/* (see m_detach) */
1515. 
1516. 	if ((int)mdef->data->msize > MZ_TINY ||
1517. 		    !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
1518. 		otmp = mkcorpstat(STATUE, KEEPTRAITS(mdef) ? mdef : 0,
1519. 				  mdef->data, x, y, FALSE);
1520. 		if (mdef->mnamelth) otmp = oname(otmp, NAME(mdef));
1521. 		/* some objects may end up outside the statue */
1522. 		while ((obj = mdef->minvent) != 0) {
1523. 		    obj_extract_self(obj);
1524. 		    obj_no_longer_held(obj);
1525. 		    if (obj->owornmask & W_WEP)
1526. 			setmnotwielded(mdef,obj);
1527. 		    obj->owornmask = 0L;
1528. 		    if (obj->otyp == BOULDER ||
1529. #if 0				/* monsters don't carry statues */
1530.      (obj->otyp == STATUE && mons[obj->corpsenm].msize >= mdef->data->msize) ||
1531. #endif
1532. 				obj_resists(obj, 0, 0)) {
1533. 			if (flooreffects(obj, x, y, "fall")) continue;
1534. 			place_object(obj, x, y);
1535. 		    } else {
1536. 			if (obj->lamplit) end_burn(obj, TRUE);
1537. 			(void) add_to_container(otmp, obj);
1538. 		    }
1539. 		}
1540. #ifndef GOLDOBJ
1541. 		if (mdef->mgold) {
1542. 			struct obj *au;
1543. 			au = mksobj(GOLD_PIECE, FALSE, FALSE);
1544. 			au->quan = mdef->mgold;
1545. 			au->owt = weight(au);
1546. 			(void) add_to_container(otmp, au);
1547. 			mdef->mgold = 0;
1548. 		}
1549. #endif
1550. 		/* Archeologists should not break unique statues */
1551. 		if (mdef->data->geno & G_UNIQ)
1552. 			otmp->spe = 1;
1553. 		otmp->owt = weight(otmp);
1554. 	} else
1555. 		otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
1556. 
1557. 	stackobj(otmp);
1558. 	/* mondead() already does this, but we must do it before the newsym */
1559. 	if(glyph_is_invisible(levl[x][y].glyph))
1560. 	    unmap_object(x, y);
1561. 	if (cansee(x, y)) newsym(x,y);
1562. 	/* We don't currently trap the hero in the statue in this case but we could */
1563. 	if (u.uswallow && u.ustuck == mdef) wasinside = TRUE;
1564. 	mondead(mdef);
1565. 	if (wasinside) {
1566. 		if (is_animal(mdef->data))
1567. 			You("%s through an opening in the new %s.",
1568. 				locomotion(youmonst.data, "jump"),
1569. 				xname(otmp));
1570. 	}
1571. }
1572. 
1573. /* another monster has killed the monster mdef */
1574. void
1575. monkilled(mdef, fltxt, how)
1576. register struct monst *mdef;
1577. const char *fltxt;
1578. int how;
1579. {
1580. 	boolean be_sad = FALSE;		/* true if unseen pet is killed */
1581. 
1582. 	if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
1583. 		&& fltxt)
1584. 	    pline("%s is %s%s%s!", Monnam(mdef),
1585. 			nonliving(mdef->data) ? "destroyed" : "killed",
1586. 		    *fltxt ? " by the " : "",
1587. 		    fltxt
1588. 		 );
1589. 	else
1590. 	    be_sad = (mdef->mtame != 0);
1591. 
1592. 	/* no corpses if digested or disintegrated */
1593. 	if(how == AD_DGST || how == -AD_RBRE)
1594. 	    mondead(mdef);
1595. 	else
1596. 	    mondied(mdef);
1597. 
1598. 	if (be_sad && mdef->mhp <= 0)
1599. 	    You("have a sad feeling for a moment, then it passes.");
1600. }
1601. 
1602. void
1603. unstuck(mtmp)
1604. register struct monst *mtmp;
1605. {
1606. 	if(u.ustuck == mtmp) {
1607. 		if(u.uswallow){
1608. 			u.ux = mtmp->mx;
1609. 			u.uy = mtmp->my;
1610. 			u.uswallow = 0;
1611. 			u.uswldtim = 0;
1612. 			if (Punished) placebc();
1613. 			vision_full_recalc = 1;
1614. 			docrt();
1615. 		}
1616. 		u.ustuck = 0;
1617. 	}
1618. }
1619. 
1620. void
1621. killed(mtmp)
1622. register struct monst *mtmp;
1623. {
1624. 	xkilled(mtmp, 1);
1625. }
1626. 
1627. /* the player has killed the monster mtmp */
1628. void
1629. xkilled(mtmp, dest)
1630. 	register struct monst *mtmp;
1631. /*
1632.  * Dest=1, normal; dest=0, don't print message; dest=2, don't drop corpse
1633.  * either; dest=3, message but no corpse
1634.  */
1635. 	int	dest;
1636. {
1637. 	register int tmp, x = mtmp->mx, y = mtmp->my;
1638. 	register struct permonst *mdat;
1639. 	int mndx;
1640. 	register struct obj *otmp;
1641. 	register struct trap *t;
1642. 	boolean redisp = FALSE;
1643. 	boolean wasinside = u.uswallow && (u.ustuck == mtmp);
1644. 
1645. 
1646. 	/* KMH, conduct */
1647. 	u.uconduct.killer++;
1648. 
1649. 	if (dest & 1) {
1650. 	    const char *verb = nonliving(mtmp->data) ? "destroy" : "kill";
1651. 
1652. 	    if (!wasinside && !canspotmon(mtmp))
1653. 		You("%s it!", verb);
1654. 	    else {
1655. 		You("%s %s!", verb,
1656. 		    !mtmp->mtame ? mon_nam(mtmp) :
1657. 			x_monnam(mtmp,
1658. 				 mtmp->mnamelth ? ARTICLE_NONE : ARTICLE_THE,
1659. 				 "poor",
1660. 				 mtmp->mnamelth ? SUPPRESS_SADDLE : 0,
1661. 				 FALSE));
1662. 	    }
1663. 	}
1664. 
1665. 	if (mtmp->mtrapped && (t = t_at(x, y)) != 0 &&
1666. 		(t->ttyp == PIT || t->ttyp == SPIKED_PIT) &&
1667. 		sobj_at(BOULDER, x, y))
1668. 	    dest |= 2;     /*
1669. 			    * Prevent corpses/treasure being created "on top"
1670. 			    * of the boulder that is about to fall in. This is
1671. 			    * out of order, but cannot be helped unless this
1672. 			    * whole routine is rearranged.
1673. 			    */
1674. 
1675. 	/* your pet knows who just killed it...watch out */
1676. 	if (mtmp->mtame && !mtmp->isminion) EDOG(mtmp)->killed_by_u = 1;
1677. 
1678. 	/* dispose of monster and make cadaver */
1679. 	if(stoned) monstone(mtmp);
1680. 	else mondead(mtmp);
1681. 
1682. 	if (mtmp->mhp > 0) { /* monster lifesaved */
1683. 		/* Cannot put the non-visible lifesaving message in
1684. 		 * lifesaved_monster() since the message appears only when you
1685. 		 * kill it (as opposed to visible lifesaving which always
1686. 		 * appears).
1687. 		 */
1688. 		stoned = FALSE;
1689. 		if (!cansee(x,y)) pline("Maybe not...");
1690. 		return;
1691. 	}
1692. 
1693. 	mdat = mtmp->data; /* note: mondead can change mtmp->data */
1694. 	mndx = monsndx(mdat);
1695. 
1696. 	if (stoned) {
1697. 		stoned = FALSE;
1698. 		goto cleanup;
1699. 	}
1700. 
1701. 	if((dest & 2) || LEVEL_SPECIFIC_NOCORPSE(mdat))
1702. 		goto cleanup;
1703. 
1704. #ifdef MAIL
1705. 	if(mdat == &mons[PM_MAIL_DAEMON]) {
1706. 		stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
1707. 		redisp = TRUE;
1708. 	}
1709. #endif
1710. 	if(!accessible(x, y) && !is_pool(x, y)) {
1711. 	    /* might be mimic in wall or corpse in lava */
1712. 	    redisp = TRUE;
1713. 	    if(wasinside) spoteffects(TRUE);
1714. 	} else if(x != u.ux || y != u.uy) {
1715. 		/* might be here after swallowed */
1716. 		if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
1717. #ifdef KOPS
1718. 					&& mdat->mlet != S_KOP
1719. #endif
1720. 							) {
1721. 			int typ;
1722. 
1723. 			otmp = mkobj_at(RANDOM_CLASS, x, y, TRUE);
1724. 			/* Don't create large objects from small monsters */
1725. 			typ = otmp->otyp;
1726. 			if (mdat->msize < MZ_HUMAN && typ != FOOD_RATION
1727. 			    && typ != LEASH
1728. 			    && typ != FIGURINE
1729. 			    && (otmp->owt > 3 ||
1730. 				objects[typ].oc_big /*oc_bimanual/oc_bulky*/ ||
1731. 				is_spear(otmp) || is_pole(otmp) ||
1732. 				typ == MORNING_STAR)) {
1733. 			    delobj(otmp);
1734. 			} else redisp = TRUE;
1735. 		}
1736. 		/* Whether or not it always makes a corpse is, in theory,
1737. 		 * different from whether or not the corpse is "special";
1738. 		 * if we want both, we have to specify it explicitly.
1739. 		 */
1740. 		if (corpse_chance(mtmp, (struct monst *)0, FALSE))
1741. 			(void) make_corpse(mtmp);
1742. 	}
1743. 	if(redisp) newsym(x,y);
1744. cleanup:
1745. 	/* punish bad behaviour */
1746. 	if(is_human(mdat) && (!always_hostile(mdat) && mtmp->malign <= 0) &&
1747. 	   (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD) &&
1748. 	   u.ualign.type != A_CHAOTIC) {
1749. 		HTelepat &= ~INTRINSIC;
1750. 		change_luck(-2);
1751. 		You("murderer!");
1752. 		if (Blind && !Blind_telepat)
1753. 		    see_monsters(); /* Can't sense monsters any more. */
1754. 	}
1755. 	if((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)	change_luck(-1);
1756. 	if (is_unicorn(mdat) &&
1757. 				sgn(u.ualign.type) == sgn(mdat->maligntyp)) {
1758. 		change_luck(-5);
1759. 		You_feel("guilty...");
1760. 	}
1761. 
1762. 	/* give experience points */
1763. 	tmp = experience(mtmp, (int)mvitals[mndx].died + 1);
1764. 	more_experienced(tmp, 0);
1765. 	newexplevel();		/* will decide if you go up */
1766. 
1767. 	/* adjust alignment points */
1768. 	if (mtmp->m_id == quest_status.leader_m_id) {		/* REAL BAD! */
1769. 	    adjalign(-(u.ualign.record+(int)ALIGNLIM/2));
1770. 	    pline("That was %sa bad idea...",
1771. 	    		u.uevent.qcompleted ? "probably " : "");
1772. 	} else if (mdat->msound == MS_NEMESIS)	/* Real good! */
1773. 	    adjalign((int)(ALIGNLIM/4));
1774. 	else if (mdat->msound == MS_GUARDIAN) {	/* Bad */
1775. 	    adjalign(-(int)(ALIGNLIM/8));
1776. 	    if (!Hallucination) pline("That was probably a bad idea...");
1777. 	    else pline("Whoopsie-daisy!");
1778. 	}else if (mtmp->ispriest) {
1779. 		adjalign((p_coaligned(mtmp)) ? -2 : 2);
1780. 		/* cancel divine protection for killing your priest */
1781. 		if (p_coaligned(mtmp)) u.ublessed = 0;
1782. 		if (mdat->maligntyp == A_NONE)
1783. 			adjalign((int)(ALIGNLIM / 4));		/* BIG bonus */
1784. 	} else if (mtmp->mtame) {
1785. 		adjalign(-15);	/* bad!! */
1786. 		/* your god is mighty displeased... */
1787. 		if (!Hallucination) You_hear("the rumble of distant thunder...");
1788. 		else You_hear("the studio audience applaud!");
1789. 	} else if (mtmp->mpeaceful)
1790. 		adjalign(-5);
1791. 
1792. 	/* malign was already adjusted for u.ualign.type and randomization */
1793. 	adjalign(mtmp->malign);
1794. }
1795. 
1796. /* changes the monster into a stone monster of the same type */
1797. /* this should only be called when poly_when_stoned() is true */
1798. void
1799. mon_to_stone(mtmp)
1800.     register struct monst *mtmp;
1801. {
1802.     if(mtmp->data->mlet == S_GOLEM) {
1803. 	/* it's a golem, and not a stone golem */
1804. 	if(canseemon(mtmp))
1805. 	    pline("%s solidifies...", Monnam(mtmp));
1806. 	if (newcham(mtmp, &mons[PM_STONE_GOLEM], FALSE)) {
1807. 	    if(canseemon(mtmp))
1808. 		pline("Now it's %s.", an(mtmp->data->mname));
1809. 	} else {
1810. 	    if(canseemon(mtmp))
1811. 		pline("... and returns to normal.");
1812. 	}
1813.     } else
1814. 	impossible("Can't polystone %s!", a_monnam(mtmp));
1815. }
1816. 
1817. void
1818. mnexto(mtmp)	/* Make monster mtmp next to you (if possible) */
1819. 	struct monst *mtmp;
1820. {
1821. 	coord mm;
1822. 
1823. #ifdef STEED
1824. 	if (mtmp == u.usteed) {
1825. 		/* Keep your steed in sync with you instead */
1826. 		mtmp->mx = u.ux;
1827. 		mtmp->my = u.uy;
1828. 		return;
1829. 	}
1830. #endif
1831. 
1832. 	if(!enexto(&mm, u.ux, u.uy, mtmp->data)) return;
1833. 	rloc_to(mtmp, mm.x, mm.y);
1834. 	return;
1835. }
1836. 
1837. /* mnearto()
1838.  * Put monster near (or at) location if possible.
1839.  * Returns:
1840.  *	1 - if a monster was moved from x, y to put mtmp at x, y.
1841.  *	0 - in most cases.
1842.  */
1843. boolean
1844. mnearto(mtmp,x,y,move_other)
1845. register struct monst *mtmp;
1846. xchar x, y;
1847. boolean move_other;	/* make sure mtmp gets to x, y! so move m_at(x, y) */
1848. {
1849. 	struct monst *othermon = (struct monst *)0;
1850. 	xchar newx, newy;
1851. 	coord mm;
1852. 
1853. 	if ((mtmp->mx == x) && (mtmp->my == y)) return(FALSE);
1854. 
1855. 	if (move_other && (othermon = m_at(x, y))) {
1856. 		if (othermon->wormno)
1857. 			remove_worm(othermon);
1858. 		else
1859. 			remove_monster(x, y);
1860. 	}
1861. 
1862. 	newx = x;
1863. 	newy = y;
1864. 
1865. 	if (!goodpos(newx, newy, mtmp)) {
1866. 		/* actually we have real problems if enexto ever fails.
1867. 		 * migrating_mons that need to be placed will cause
1868. 		 * no end of trouble.
1869. 		 */
1870. 		if (!enexto(&mm, newx, newy, mtmp->data)) return(FALSE);
1871. 		newx = mm.x; newy = mm.y;
1872. 	}
1873. 
1874. 	rloc_to(mtmp, newx, newy);
1875. 
1876. 	if (move_other && othermon) {
1877. 	    othermon->mx = othermon->my = 0;
1878. 	    (void) mnearto(othermon, x, y, FALSE);
1879. 	    if ((othermon->mx != x) || (othermon->my != y))
1880. 		return(TRUE);
1881. 	}
1882. 
1883. 	return(FALSE);
1884. }
1885. 
1886. 
1887. static const char *poiseff[] = {
1888. 
1889. 	" feel weaker", "r brain is on fire",
1890. 	"r judgement is impaired", "r muscles won't obey you",
1891. 	" feel very sick", " break out in hives"
1892. };
1893. 
1894. void
1895. poisontell(typ)
1896. 
1897. 	int	typ;
1898. {
1899. 	pline("You%s.", poiseff[typ]);
1900. }
1901. 
1902. void
1903. poisoned(string, typ, pname, fatal)
1904. register const char *string, *pname;
1905. register int  typ, fatal;
1906. {
1907. 	register int i, plural;
1908. 	boolean thrown_weapon = !strncmp(string, "poison", 6);
1909. 		/* admittedly a kludge... */
1910. 
1911. 	if(strcmp(string, "blast") && !thrown_weapon) {
1912. 	    /* 'blast' has already given a 'poison gas' message */
1913. 	    /* so have "poison arrow", "poison dart", etc... */
1914. 	    plural = (string[strlen(string) - 1] == 's')? 1 : 0;
1915. 	    /* avoid "The" Orcus's sting was poisoned... */
1916. 	    pline("%s%s %s poisoned!", isupper(*string) ? "" : "The ",
1917. 			string, plural ? "were" : "was");
1918. 	}
1919. 
1920. 	if(Poison_resistance) {
1921. 		if(!strcmp(string, "blast")) shieldeff(u.ux, u.uy);
1922. 		pline_The("poison doesn't seem to affect you.");
1923. 		return;
1924. 	}
1925. 	i = rn2(fatal + 20*thrown_weapon);
1926. 	if(i == 0 && typ != A_CHA) {
1927. 		u.uhp = -1;
1928. 		pline_The("poison was deadly...");
1929. 	} else if(i <= 5) {
1930. 		/* Check that a stat change was made */
1931. 		if (adjattrib(typ, thrown_weapon ? -1 : -rn1(3,3), 1))
1932.   		    pline("You%s!", poiseff[typ]);
1933. 	} else {
1934. 		i = thrown_weapon ? rnd(6) : rn1(10,6);
1935. 		if(Half_physical_damage) i = (i+1) / 2;
1936. 		losehp(i, pname, KILLED_BY_AN);
1937. 	}
1938. 	if(u.uhp < 1) {
1939. 		killer_format = KILLED_BY_AN;
1940. 		killer = pname;
1941. 		/* "Poisoned by a poisoned ___" is redundant */
1942. 		done(thrown_weapon ? DIED : POISONING);
1943. 	}
1944. 	(void) encumber_msg();
1945. }
1946. 
1947. /* monster responds to player action; not the same as a passive attack */
1948. /* assumes reason for response has been tested, and response _must_ be made */
1949. void
1950. m_respond(mtmp)
1951. register struct monst *mtmp;
1952. {
1953.     if(mtmp->data->msound == MS_SHRIEK) {
1954. 	if(flags.soundok) {
1955. 	    pline("%s shrieks.", Monnam(mtmp));
1956. 	    stop_occupation();
1957. 	}
1958. 	if (!rn2(10)) {
1959. 	    if (!rn2(13))
1960. 		(void) makemon(&mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
1961. 	    else
1962. 		(void) makemon((struct permonst *)0, 0, 0, NO_MM_FLAGS);
1963. 
1964. 	}
1965. 	aggravate();
1966.     }
1967.     if(mtmp->data == &mons[PM_MEDUSA]) {
1968. 	register int i;
1969. 	for(i = 0; i < NATTK; i++)
1970. 	     if(mtmp->data->mattk[i].aatyp == AT_GAZE) {
1971. 		 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
1972. 		 break;
1973. 	     }
1974.     }
1975. }
1976. 
1977. #endif /* OVLB */
1978. #ifdef OVL2
1979. 
1980. void
1981. setmangry(mtmp)
1982. register struct monst *mtmp;
1983. {
1984. 	mtmp->mstrategy &= ~STRAT_WAITMASK;
1985. 	if(!mtmp->mpeaceful) return;
1986. 	if(mtmp->mtame) return;
1987. 	mtmp->mpeaceful = 0;
1988. 	if(mtmp->ispriest) {
1989. 		if(p_coaligned(mtmp)) adjalign(-5); /* very bad */
1990. 		else adjalign(2);
1991. 	} else
1992. 		adjalign(-1);		/* attacking peaceful monsters is bad */
1993. 	if (couldsee(mtmp->mx, mtmp->my)) {
1994. 		if (humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
1995. 		    pline("%s gets angry!", Monnam(mtmp));
1996. 		else if (flags.verbose && flags.soundok) growl(mtmp);
1997. 	}
1998. 
1999. 	/* attacking your own quest leader will anger his or her guardians */
2000. 	if (!flags.mon_moving &&	/* should always be the case here */
2001. 		mtmp->data == &mons[quest_info(MS_LEADER)]) {
2002. 	    struct monst *mon;
2003. 	    struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
2004. 	    int got_mad = 0;
2005. 
2006. 	    /* guardians will sense this attack even if they can't see it */
2007. 	    for (mon = fmon; mon; mon = mon->nmon)
2008. 		if (!DEADMONSTER(mon) && mon->data == q_guardian && mon->mpeaceful) {
2009. 		    mon->mpeaceful = 0;
2010. 		    if (canseemon(mon)) ++got_mad;
2011. 		}
2012. 	    if (got_mad && !Hallucination)
2013. 		pline_The("%s appear%s to be angry too...",
2014. 		      got_mad == 1 ? q_guardian->mname :
2015. 				    makeplural(q_guardian->mname),
2016. 		      got_mad == 1 ? "s" : "");
2017. 	}
2018. }
2019. 
2020. void
2021. wakeup(mtmp)
2022. register struct monst *mtmp;
2023. {
2024. 	mtmp->msleeping = 0;
2025. 	mtmp->meating = 0;	/* assume there's no salvagable food left */
2026. 	setmangry(mtmp);
2027. 	if(mtmp->m_ap_type) seemimic(mtmp);
2028. }
2029. 
2030. /* Wake up nearby monsters. */
2031. void
2032. wake_nearby()
2033. {
2034. 	register struct monst *mtmp;
2035. 
2036. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2037. 	    if (!DEADMONSTER(mtmp) && distu(mtmp->mx,mtmp->my) < u.ulevel*20) {
2038. 		mtmp->msleeping = 0;
2039. 		if (mtmp->mtame && !mtmp->isminion)
2040. 		    EDOG(mtmp)->whistletime = moves;
2041. 	    }
2042. 	}
2043. }
2044. 
2045. /* Wake up monsters near some particular location. */
2046. void
2047. wake_nearto(x, y, distance)
2048. register int x, y, distance;
2049. {
2050. 	register struct monst *mtmp;
2051. 
2052. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2053. 	    if (!DEADMONSTER(mtmp) && mtmp->msleeping && (distance == 0 ||
2054. 				 dist2(mtmp->mx, mtmp->my, x, y) < distance))
2055. 		mtmp->msleeping = 0;
2056. 	}
2057. }
2058. 
2059. /* NOTE: we must check for mimicry before calling this routine */
2060. void
2061. seemimic(mtmp)
2062. register struct monst *mtmp;
2063. {
2064. 	/*
2065. 	 *  Discovered mimics don't block light.
2066. 	 */
2067. 	if ((mtmp->m_ap_type == M_AP_FURNITURE &&
2068. 		(mtmp->mappearance==S_hcdoor || mtmp->mappearance==S_vcdoor))||
2069. 	    (mtmp->m_ap_type == M_AP_OBJECT && mtmp->mappearance == BOULDER))
2070. 	    unblock_point(mtmp->mx,mtmp->my);
2071. 
2072. 	mtmp->m_ap_type = M_AP_NOTHING;
2073. 	mtmp->mappearance = 0;
2074. 	newsym(mtmp->mx,mtmp->my);
2075. }
2076. 
2077. /* force all chameleons to become normal */
2078. void
2079. rescham()
2080. {
2081. 	register struct monst *mtmp;
2082. 	int mcham;
2083. 
2084. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2085. 		if (DEADMONSTER(mtmp)) continue;
2086. 		mcham = (int) mtmp->cham;
2087. 		if (mcham) {
2088. 			mtmp->cham = CHAM_ORDINARY;
2089. 			(void) newcham(mtmp, &mons[cham_to_pm[mcham]], FALSE);
2090. 		}
2091. 		if(is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
2092. 			new_were(mtmp);
2093. 		if(mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
2094. 			seemimic(mtmp);
2095. 			/* we pretend that the mimic doesn't */
2096. 			/* know that it has been unmasked.   */
2097. 			mtmp->msleeping = 1;
2098. 		}
2099. 	}
2100. }
2101. 
2102. /* Let the chameleons change again -dgk */
2103. void
2104. restartcham()
2105. {
2106. 	register struct monst *mtmp;
2107. 
2108. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2109. 		if (DEADMONSTER(mtmp)) continue;
2110. 		mtmp->cham = pm_to_cham(monsndx(mtmp->data));
2111. 		if (mtmp->data->mlet == S_MIMIC && mtmp->msleeping &&
2112. 				cansee(mtmp->mx, mtmp->my)) {
2113. 			set_mimic_sym(mtmp);
2114. 			newsym(mtmp->mx,mtmp->my);
2115. 		}
2116. 	}
2117. }
2118. 
2119. /* called when restoring a monster from a saved level; protection
2120.    against shape-changing might be different now than it was at the
2121.    time the level was saved. */
2122. void
2123. restore_cham(mon)
2124. struct monst *mon;
2125. {
2126. 	int mcham;
2127. 
2128. 	if (Protection_from_shape_changers) {
2129. 	    mcham = (int) mon->cham;
2130. 	    if (mcham) {
2131. 		mon->cham = CHAM_ORDINARY;
2132. 		(void) newcham(mon, &mons[cham_to_pm[mcham]], FALSE);
2133. 	    } else if (is_were(mon->data) && !is_human(mon->data)) {
2134. 		new_were(mon);
2135. 	    }
2136. 	} else if (mon->cham == CHAM_ORDINARY) {
2137. 	    mon->cham = pm_to_cham(monsndx(mon->data));
2138. 	}
2139. }
2140. 
2141. /* unwatched hiders may hide again; if so, a 1 is returned.  */
2142. STATIC_OVL boolean
2143. restrap(mtmp)
2144. register struct monst *mtmp;
2145. {
2146. 	if(mtmp->cham || mtmp->mcan || mtmp->m_ap_type ||
2147. 	   cansee(mtmp->mx, mtmp->my) || rn2(3) || (mtmp == u.ustuck) ||
2148. 	   (sensemon(mtmp) && distu(mtmp->mx, mtmp->my) <= 2))
2149. 		return(FALSE);
2150. 
2151. 	if(mtmp->data->mlet == S_MIMIC) {
2152. 		set_mimic_sym(mtmp);
2153. 		return(TRUE);
2154. 	} else
2155. 	    if(levl[mtmp->mx][mtmp->my].typ == ROOM)  {
2156. 		mtmp->mundetected = 1;
2157. 		return(TRUE);
2158. 	    }
2159. 
2160. 	return(FALSE);
2161. }
2162. 
2163. short *animal_list = 0;		/* list of PM values for animal monsters */
2164. int animal_list_count;
2165. 
2166. void
2167. mon_animal_list(construct)
2168. boolean construct;
2169. {
2170. 	if (construct) {
2171. 	    short animal_temp[SPECIAL_PM];
2172. 	    int i, n;
2173. 
2174. 	 /* if (animal_list) impossible("animal_list already exists"); */
2175. 
2176. 	    for (n = 0, i = LOW_PM; i < SPECIAL_PM; i++)
2177. 		if (is_animal(&mons[i])) animal_temp[n++] = i;
2178. 	 /* if (n == 0) animal_temp[n++] = NON_PM; */
2179. 
2180. 	    animal_list = (short *)alloc(n * sizeof *animal_list);
2181. 	    (void) memcpy((genericptr_t)animal_list,
2182. 			  (genericptr_t)animal_temp,
2183. 			  n * sizeof *animal_list);
2184. 	    animal_list_count = n;
2185. 	} else {	/* release */
2186. 	    if (animal_list) free((genericptr_t)animal_list), animal_list = 0;
2187. 	    animal_list_count = 0;
2188. 	}
2189. }
2190. 
2191. STATIC_OVL int
2192. pick_animal()
2193. {
2194. 	if (!animal_list) mon_animal_list(TRUE);
2195. 
2196. 	return animal_list[rn2(animal_list_count)];
2197. }
2198. 
2199. STATIC_OVL int
2200. select_newcham_form(mon)
2201. struct monst *mon;
2202. {
2203. 	int mndx = NON_PM;
2204. 
2205. 	switch (mon->cham) {
2206. 	    case CHAM_SANDESTIN:
2207. 		if (rn2(7)) mndx = pick_nasty();
2208. 		break;
2209. 	    case CHAM_DOPPELGANGER:
2210. 		if (!rn2(7)) mndx = pick_nasty();
2211. 		else if (rn2(3)) mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1,
2212. 					    PM_ARCHEOLOGIST);
2213. 		break;
2214. 	    case CHAM_CHAMELEON:
2215. 		if (!rn2(3)) mndx = pick_animal();
2216. 		break;
2217. 	    case CHAM_ORDINARY:
2218. 	      {
2219. 		struct obj *m_armr = which_armor(mon, W_ARM);
2220. 
2221. 		if (m_armr && Is_dragon_scales(m_armr))
2222. 		    mndx = Dragon_scales_to_pm(m_armr) - mons;
2223. 		else if (m_armr && Is_dragon_mail(m_armr))
2224. 		    mndx = Dragon_mail_to_pm(m_armr) - mons;
2225. 	      }
2226. 		break;
2227. 	}
2228. #ifdef WIZARD
2229. 	/* For debugging only: allow control of polymorphed monster; not saved */
2230. 	if (wizard && iflags.mon_polycontrol) {
2231. 		char pprompt[BUFSZ], buf[BUFSZ];
2232. 		int tries = 0;
2233. 		do {
2234. 			Sprintf(pprompt,
2235. 				"Change %s into what kind of monster? [type the name]",
2236. 				mon_nam(mon));
2237. 			getlin(pprompt,buf);
2238. 			mndx = name_to_mon(buf);
2239. 			if (mndx < LOW_PM)
2240. 				You("cannot polymorph %s into that.", mon_nam(mon));
2241. 			else break;
2242. 		} while(++tries < 5);
2243. 		if (tries==5) pline(thats_enough_tries);
2244. 	}
2245. #endif /*WIZARD*/
2246. 	if (mndx == NON_PM) mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
2247. 	return mndx;
2248. }
2249. 
2250. /* make a chameleon look like a new monster; returns 1 if it actually changed */
2251. int
2252. newcham(mtmp, mdat, polyspot)
2253. struct monst *mtmp;
2254. struct permonst *mdat;
2255. boolean polyspot;	/* change is the result of wand or spell of polymorph */
2256. {
2257. 	int mhp, hpn, hpd;
2258. 	int mndx, tryct;
2259. 	struct permonst *olddata = mtmp->data;
2260. 
2261. 	/* mdat = 0 -> caller wants a random monster shape */
2262. 	tryct = 0;
2263. 	if (mdat == 0) {
2264. 	    while (++tryct <= 100) {
2265. 		mndx = select_newcham_form(mtmp);
2266. 		mdat = &mons[mndx];
2267. 		if ((mvitals[mndx].mvflags & G_GENOD) != 0 ||
2268. 			is_placeholder(mdat)) continue;
2269. 		/* polyok rules out all M2_PNAME and M2_WERE's;
2270. 		   select_newcham_form might deliberately pick a player
2271. 		   character type, so we can't arbitrarily rule out all
2272. 		   human forms any more */
2273. 		if (is_mplayer(mdat) || (!is_human(mdat) && polyok(mdat)))
2274. 		    break;
2275. 	    }
2276. 	    if (tryct > 100) return 0;	/* Should never happen */
2277. 	} else if (mvitals[monsndx(mdat)].mvflags & G_GENOD)
2278. 	    return(0);	/* passed in mdat is genocided */
2279. 
2280. 	if(is_male(mdat)) {
2281. 		if(mtmp->female) mtmp->female = FALSE;
2282. 	} else if (is_female(mdat)) {
2283. 		if(!mtmp->female) mtmp->female = TRUE;
2284. 	} else if (!is_neuter(mdat)) {
2285. 		if(!rn2(10)) mtmp->female = !mtmp->female;
2286. 	}
2287. 
2288. 	if (In_endgame(&u.uz) && is_mplayer(olddata)) {
2289. 		/* mplayers start out as "Foo the Bar", but some of the
2290. 		 * titles are inappropriate when polymorphed, particularly
2291. 		 * into the opposite sex.  players don't use ranks when
2292. 		 * polymorphed, so dropping the rank for mplayers seems
2293. 		 * reasonable.
2294. 		 */
2295. 		char *p = index(NAME(mtmp), ' ');
2296. 		if (p) {
2297. 			*p = '\0';
2298. 			mtmp->mnamelth = p - NAME(mtmp) + 1;
2299. 		}
2300. 	}
2301. 
2302. 	if(mdat == mtmp->data) return(0);	/* still the same monster */
2303. 
2304. 	if(mtmp->wormno) {			/* throw tail away */
2305. 		wormgone(mtmp);
2306. 		place_monster(mtmp, mtmp->mx, mtmp->my);
2307. 	}
2308. 
2309. 	hpn = mtmp->mhp;
2310. 	hpd = (mtmp->m_lev < 50) ? ((int)mtmp->m_lev)*8 : mdat->mlevel;
2311. 	if(!hpd) hpd = 4;
2312. 
2313. 	mtmp->m_lev = adj_lev(mdat);		/* new monster level */
2314. 
2315. 	mhp = (mtmp->m_lev < 50) ? ((int)mtmp->m_lev)*8 : mdat->mlevel;
2316. 	if(!mhp) mhp = 4;
2317. 
2318. 	/* new hp: same fraction of max as before */
2319. #ifndef LINT
2320. 	mtmp->mhp = (int)(((long)hpn*(long)mhp)/(long)hpd);
2321. #endif
2322. 	if(mtmp->mhp < 0) mtmp->mhp = hpn;	/* overflow */
2323. /* Unlikely but not impossible; a 1HD creature with 1HP that changes into a
2324.    0HD creature will require this statement */
2325. 	if (!mtmp->mhp) mtmp->mhp = 1;
2326. 
2327. /* and the same for maximum hit points */
2328. 	hpn = mtmp->mhpmax;
2329. #ifndef LINT
2330. 	mtmp->mhpmax = (int)(((long)hpn*(long)mhp)/(long)hpd);
2331. #endif
2332. 	if(mtmp->mhpmax < 0) mtmp->mhpmax = hpn;	/* overflow */
2333. 	if (!mtmp->mhpmax) mtmp->mhpmax = 1;
2334. 
2335. 	/* take on the new form... */
2336. 	set_mon_data(mtmp, mdat, 0);
2337. 
2338. 	if (emits_light(olddata) != emits_light(mtmp->data)) {
2339. 	    /* used to give light, now doesn't, or vice versa,
2340. 	       or light's range has changed */
2341. 	    if (emits_light(olddata))
2342. 		del_light_source(LS_MONSTER, (genericptr_t)mtmp);
2343. 	    if (emits_light(mtmp->data))
2344. 		new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
2345. 				 LS_MONSTER, (genericptr_t)mtmp);
2346. 	}
2347. 	if (!mtmp->perminvis || pm_invisible(olddata))
2348. 	    mtmp->perminvis = pm_invisible(mdat);
2349. 	mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
2350. 	if (!(hides_under(mdat) && OBJ_AT(mtmp->mx, mtmp->my)) &&
2351. 			!(mdat->mlet == S_EEL && is_pool(mtmp->mx, mtmp->my)))
2352. 		mtmp->mundetected = 0;
2353. 	if (u.ustuck == mtmp) {
2354. 		if(u.uswallow) {
2355. 			if(!attacktype(mdat,AT_ENGL)) {
2356. 				/* Does mdat care? */
2357. 				if (!noncorporeal(mdat) && !amorphous(mdat) &&
2358. 				    !is_whirly(mdat) &&
2359. 				    (mdat != &mons[PM_YELLOW_LIGHT])) {
2360. 					You("break out of %s%s!", mon_nam(mtmp),
2361. 					    (is_animal(mdat)?
2362. 					    "'s stomach" : ""));
2363. 					mtmp->mhp = 1;  /* almost dead */
2364. 				}
2365. 				expels(mtmp, olddata, FALSE);
2366. 			}
2367. 		} else if (!sticks(mdat) && !sticks(youmonst.data))
2368. 			unstuck(mtmp);
2369. 	}
2370. 
2371. #ifndef DCC30_BUG
2372. 	if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
2373. #else
2374. 	/* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
2375. 	 * same expression.
2376. 	 */
2377. 	if (mdat == &mons[PM_LONG_WORM] &&
2378. 		(mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
2379. #endif
2380. 	    /* we can now create worms with tails - 11/91 */
2381. 	    initworm(mtmp, rn2(5));
2382. 	    if (count_wsegs(mtmp))
2383. 		place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
2384. 	}
2385. 
2386. 	newsym(mtmp->mx,mtmp->my);
2387. 
2388. 	mon_break_armor(mtmp, polyspot);
2389. 	if (!(mtmp->misc_worn_check & W_ARMG))
2390. 	    mselftouch(mtmp, "No longer petrify-resistant, ",
2391. 			!flags.mon_moving);
2392. 	possibly_unwield(mtmp);
2393. 	m_dowear(mtmp, FALSE);
2394. 
2395. 	/* This ought to re-test can_carry() on each item in the inventory
2396. 	 * rather than just checking ex-giants & boulders, but that'd be
2397. 	 * pretty expensive to perform.  If implemented, then perhaps
2398. 	 * minvent should be sorted in order to drop heaviest items first.
2399. 	 */
2400. 	/* former giants can't continue carrying boulders */
2401. 	if (mtmp->minvent && !throws_rocks(mdat)) {
2402. 	    register struct obj *otmp, *otmp2;
2403. 
2404. 	    for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
2405. 		otmp2 = otmp->nobj;
2406. 		if (otmp->otyp == BOULDER) {
2407. 		    /* this keeps otmp from being polymorphed in the
2408. 		       same zap that the monster that held it is polymorphed */
2409. 		    if (polyspot) bypass_obj(otmp);
2410. 		    obj_extract_self(otmp);
2411. 		    /* probably ought to give some "drop" message here */
2412. 		    if (flooreffects(otmp, mtmp->mx, mtmp->my, "")) continue;
2413. 		    place_object(otmp, mtmp->mx, mtmp->my);
2414. 		}
2415. 	    }
2416. 	}
2417. 
2418. 	return(1);
2419. }
2420. 
2421. /* sometimes an egg will be special */
2422. #define BREEDER_EGG (!rn2(77))
2423. 
2424. /*
2425.  * Determine if the given monster number can be hatched from an egg.
2426.  * Return the monster number to use as the egg's corpsenm.  Return
2427.  * NON_PM if the given monster can't be hatched.
2428.  */
2429. int
2430. can_be_hatched(mnum)
2431. int mnum;
2432. {
2433.     mnum = little_to_big(mnum);
2434.     /*
2435.      * Queen bees lay killer bee eggs (usually), but killer bees don't
2436.      * grow into queen bees.  Ditto for [winged-]gargoyles.
2437.      */
2438.     if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE ||
2439. 	    (lays_eggs(&mons[mnum]) && (BREEDER_EGG ||
2440. 		(mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
2441. 	return mnum;
2442.     return NON_PM;
2443. }
2444. 
2445. /* type of egg laid by #sit; usually matches parent */
2446. int
2447. egg_type_from_parent(mnum, force_ordinary)
2448. int mnum;	/* parent monster; caller must handle lays_eggs() check */
2449. boolean force_ordinary;
2450. {
2451.     if (force_ordinary || !BREEDER_EGG) {
2452. 	if (mnum == PM_QUEEN_BEE) mnum = PM_KILLER_BEE;
2453. 	else if (mnum == PM_WINGED_GARGOYLE) mnum = PM_GARGOYLE;
2454.     }
2455.     return mnum;
2456. }
2457. 
2458. /* decide whether an egg of the indicated monster type is viable; */
2459. /* also used to determine whether an egg or tin can be created... */
2460. boolean
2461. dead_species(m_idx, egg)
2462. int m_idx;
2463. boolean egg;
2464. {
2465. 	/*
2466. 	 * For monsters with both baby and adult forms, genociding either
2467. 	 * form kills all eggs of that monster.  Monsters with more than
2468. 	 * two forms (small->large->giant mimics) are more or less ignored;
2469. 	 * fortunately, none of them have eggs.  Species extinction due to
2470. 	 * overpopulation does not kill eggs.
2471. 	 */
2472. 	return (boolean)
2473. 		(m_idx >= LOW_PM &&
2474. 		 ((mvitals[m_idx].mvflags & G_GENOD) != 0 ||
2475. 		  (egg &&
2476. 		   (mvitals[big_to_little(m_idx)].mvflags & G_GENOD) != 0)));
2477. }
2478. 
2479. /* kill off any eggs of genocided monsters */
2480. STATIC_OVL void
2481. kill_eggs(obj_list)
2482. struct obj *obj_list;
2483. {
2484. 	struct obj *otmp;
2485. 
2486. 	for (otmp = obj_list; otmp; otmp = otmp->nobj)
2487. 	    if (otmp->otyp == EGG) {
2488. 		if (dead_species(otmp->corpsenm, TRUE)) {
2489. 		    /*
2490. 		     * It seems we could also just catch this when
2491. 		     * it attempted to hatch, so we wouldn't have to
2492. 		     * search all of the objlists.. or stop all
2493. 		     * hatch timers based on a corpsenm.
2494. 		     */
2495. 		    kill_egg(otmp);
2496. 		}
2497. #if 0	/* not used */
2498. 	    } else if (otmp->otyp == TIN) {
2499. 		if (dead_species(otmp->corpsenm, FALSE))
2500. 		    otmp->corpsenm = NON_PM;	/* empty tin */
2501. 	    } else if (otmp->otyp == CORPSE) {
2502. 		if (dead_species(otmp->corpsenm, FALSE))
2503. 		    ;		/* not yet implemented... */
2504. #endif
2505. 	    } else if (Has_contents(otmp)) {
2506. 		kill_eggs(otmp->cobj);
2507. 	    }
2508. }
2509. 
2510. /* kill all members of genocided species */
2511. void
2512. kill_genocided_monsters()
2513. {
2514. 	struct monst *mtmp, *mtmp2;
2515. 	boolean kill_cham[CHAM_MAX_INDX+1];
2516. 	int mndx;
2517. 
2518. 	kill_cham[CHAM_ORDINARY] = FALSE;	/* (this is mndx==0) */
2519. 	for (mndx = 1; mndx <= CHAM_MAX_INDX; mndx++)
2520. 	  kill_cham[mndx] = (mvitals[cham_to_pm[mndx]].mvflags & G_GENOD) != 0;
2521. 	/*
2522. 	 * Called during genocide, and again upon level change.  The latter
2523. 	 * catches up with any migrating monsters as they finally arrive at
2524. 	 * their intended destinations, so possessions get deposited there.
2525. 	 *
2526. 	 * Chameleon handling:
2527. 	 *	1) if chameleons have been genocided, destroy them
2528. 	 *	   regardless of current form;
2529. 	 *	2) otherwise, force every chameleon which is imitating
2530. 	 *	   any genocided species to take on a new form.
2531. 	 */
2532. 	for (mtmp = fmon; mtmp; mtmp = mtmp2) {
2533. 	    mtmp2 = mtmp->nmon;
2534. 	    if (DEADMONSTER(mtmp)) continue;
2535. 	    mndx = monsndx(mtmp->data);
2536. 	    if ((mvitals[mndx].mvflags & G_GENOD) || kill_cham[mtmp->cham]) {
2537. 		if (mtmp->cham && !kill_cham[mtmp->cham])
2538. 		    (void) newcham(mtmp, (struct permonst *)0, FALSE);
2539. 		else
2540. 		    mondead(mtmp);
2541. 	    }
2542. 	    if (mtmp->minvent) kill_eggs(mtmp->minvent);
2543. 	}
2544. 
2545. 	kill_eggs(invent);
2546. 	kill_eggs(fobj);
2547. 	kill_eggs(level.buriedobjlist);
2548. }
2549. 
2550. #endif /* OVL2 */
2551. #ifdef OVLB
2552. 
2553. void
2554. golemeffects(mon, damtype, dam)
2555. register struct monst *mon;
2556. int damtype, dam;
2557. {
2558.     int heal = 0, slow = 0;
2559. 
2560.     if (mon->data == &mons[PM_FLESH_GOLEM]) {
2561. 	if (damtype == AD_ELEC) heal = dam / 6;
2562. 	else if (damtype == AD_FIRE || damtype == AD_COLD) slow = 1;
2563.     } else if (mon->data == &mons[PM_IRON_GOLEM]) {
2564. 	if (damtype == AD_ELEC) slow = 1;
2565. 	else if (damtype == AD_FIRE) heal = dam;
2566.     } else {
2567. 	return;
2568.     }
2569.     if (slow) {
2570. 	if (mon->mspeed != MSLOW)
2571. 	    mon_adjust_speed(mon, -1, (struct obj *)0);
2572.     }
2573.     if (heal) {
2574. 	if (mon->mhp < mon->mhpmax) {
2575. 	    mon->mhp += dam;
2576. 	    if (mon->mhp > mon->mhpmax) mon->mhp = mon->mhpmax;
2577. 	    if (cansee(mon->mx, mon->my))
2578. 		pline("%s seems healthier.", Monnam(mon));
2579. 	}
2580.     }
2581. }
2582. 
2583. boolean
2584. angry_guards(silent)
2585. register boolean silent;
2586. {
2587. 	register struct monst *mtmp;
2588. 	register int ct = 0, nct = 0, sct = 0, slct = 0;
2589. 
2590. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2591. 		if (DEADMONSTER(mtmp)) continue;
2592. 		if((mtmp->data == &mons[PM_WATCHMAN] ||
2593. 			       mtmp->data == &mons[PM_WATCH_CAPTAIN])
2594. 					&& mtmp->mpeaceful) {
2595. 			ct++;
2596. 			if(cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
2597. 				if (distu(mtmp->mx, mtmp->my) == 2) nct++;
2598. 				else sct++;
2599. 			}
2600. 			if (mtmp->msleeping || mtmp->mfrozen) {
2601. 				slct++;
2602. 				mtmp->msleeping = mtmp->mfrozen = 0;
2603. 			}
2604. 			mtmp->mpeaceful = 0;
2605. 		}
2606. 	}
2607. 	if(ct) {
2608. 	    if(!silent) { /* do we want pline msgs? */
2609. 		if(slct) pline_The("guard%s wake%s up!",
2610. 				 slct > 1 ? "s" : "", slct == 1 ? "s" : "");
2611. 		if(nct || sct) {
2612. 			if(nct) pline_The("guard%s get%s angry!",
2613. 				nct == 1 ? "" : "s", nct == 1 ? "s" : "");
2614. 			else if(!Blind)
2615. 				You("see %sangry guard%s approaching!",
2616. 				  sct == 1 ? "an " : "", sct > 1 ? "s" : "");
2617. 		} else if(flags.soundok)
2618. 			You_hear("the shrill sound of a guard's whistle.");
2619. 	    }
2620. 	    return(TRUE);
2621. 	}
2622. 	return(FALSE);
2623. }
2624. 
2625. void
2626. pacify_guards()
2627. {
2628. 	register struct monst *mtmp;
2629. 
2630. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2631. 	    if (DEADMONSTER(mtmp)) continue;
2632. 	    if (mtmp->data == &mons[PM_WATCHMAN] ||
2633. 		mtmp->data == &mons[PM_WATCH_CAPTAIN])
2634. 	    mtmp->mpeaceful = 1;
2635. 	}
2636. }
2637. #endif /* OVLB */
2638. 
2639. /*mon.c*/