Wikihack
Advertisement

Below is the full text to mon.c from the source code of NetHack 3.2.0. To link to a particular line, write [[NetHack 3.2.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.
1.    /*	SCCS Id: @(#)mon.c	3.2	96/03/07	*/
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(corpse_chance,(struct monst *));
16.   STATIC_DCL boolean FDECL(restrap,(struct monst *));
17.   #ifdef OVL2
18.   static void FDECL(kill_eggs, (struct obj *));
19.   #endif
20.   
21.   #ifdef OVLB
22.   STATIC_OVL NEARDATA struct monst *fdmon;	/* dead monster list */
23.   #else
24.   STATIC_DCL NEARDATA struct monst *fdmon;
25.   #endif
26.   
27.   #ifdef OVL1
28.   #define warnDelay 10
29.   long lastwarntime;
30.   int lastwarnlev;
31.   
32.   const char *warnings[] = {
33.   	"white", "pink", "red", "ruby", "purple", "black"
34.   };
35.   
36.   static void NDECL(warn_effects);
37.   
38.   #endif /* OVL1 */
39.   
40.   
41.   #ifdef OVLB
42.   static struct obj *FDECL(make_corpse,(struct monst *));
43.   static void FDECL(m_detach, (struct monst *,struct permonst *));
44.   
45.   /* convert the monster index of an undead to its living counterpart */
46.   int
47.   undead_to_corpse(mndx)
48.   int mndx;
49.   {
50.   	switch (mndx) {
51.   	case PM_KOBOLD_ZOMBIE:
52.   	case PM_KOBOLD_MUMMY:	mndx = PM_KOBOLD;  break;
53.   	case PM_GNOME_ZOMBIE:
54.   	case PM_GNOME_MUMMY:	mndx = PM_GNOME;  break;
55.   	case PM_ORC_ZOMBIE:
56.   	case PM_ORC_MUMMY:	mndx = PM_ORC;  break;
57.   	case PM_ELF_ZOMBIE:
58.   	case PM_ELF_MUMMY:	mndx = PM_ELF;  break;
59.   	case PM_VAMPIRE:
60.   	case PM_VAMPIRE_LORD:
61.   	case PM_HUMAN_ZOMBIE:
62.   	case PM_HUMAN_MUMMY:	mndx = PM_HUMAN;  break;
63.   	case PM_GIANT_ZOMBIE:
64.   	case PM_GIANT_MUMMY:	mndx = PM_GIANT;  break;
65.   	case PM_ETTIN_ZOMBIE:
66.   	case PM_ETTIN_MUMMY:	mndx = PM_ETTIN;  break;
67.   	default:  break;
68.   	}
69.   	return mndx;
70.   }
71.   
72.   /* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
73.    * leave corpses.  Monsters which leave "special" corpses should have
74.    * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
75.    * etc....
76.    */
77.   static struct obj *
78.   make_corpse(mtmp)
79.   register struct monst *mtmp;
80.   {
81.   	register struct permonst *mdat = mtmp->data;
82.   	int num;
83.   	struct obj *obj = (struct obj *)0;
84.   	int x = mtmp->mx, y = mtmp->my;
85.   	int mndx = monsndx(mdat);
86.   
87.   	switch(mndx) {
88.   	    case PM_GRAY_DRAGON:
89.   	    case PM_RED_DRAGON:
90.   	    case PM_ORANGE_DRAGON:
91.   	    case PM_WHITE_DRAGON:
92.   	    case PM_BLACK_DRAGON:
93.   	    case PM_BLUE_DRAGON:
94.   	    case PM_GREEN_DRAGON:
95.   	    case PM_YELLOW_DRAGON:
96.   		/* Make dragon scales.  This assumes that the order of the */
97.   		/* dragons is the same as the order of the scales.	   */
98.   		if (!rn2(3)) {
99.   		    obj = mksobj_at(GRAY_DRAGON_SCALES +
100.  				    monsndx(mdat)-PM_GRAY_DRAGON, x, y, FALSE);
101.  		    obj->spe = 0;
102.  		    obj->cursed = obj->blessed = FALSE;
103.  		}
104.  		goto default_1;
105.  
106.  	    case PM_WHITE_UNICORN:
107.  	    case PM_GRAY_UNICORN:
108.  	    case PM_BLACK_UNICORN:
109.  		(void) mksobj_at(UNICORN_HORN, x, y, TRUE);
110.  		goto default_1;
111.  	    case PM_LONG_WORM:
112.  		(void) mksobj_at(WORM_TOOTH, x, y, TRUE);
113.  		goto default_1;
114.  	    case PM_KOBOLD_MUMMY:
115.  	    case PM_GNOME_MUMMY:
116.  	    case PM_ORC_MUMMY:
117.  	    case PM_ELF_MUMMY:
118.  	    case PM_VAMPIRE:
119.  	    case PM_VAMPIRE_LORD:
120.  	    case PM_HUMAN_MUMMY:
121.  	    case PM_GIANT_MUMMY:
122.  	    case PM_ETTIN_MUMMY:
123.  	    case PM_KOBOLD_ZOMBIE:
124.  	    case PM_GNOME_ZOMBIE:
125.  	    case PM_ORC_ZOMBIE:
126.  	    case PM_ELF_ZOMBIE:
127.  	    case PM_HUMAN_ZOMBIE:
128.  	    case PM_GIANT_ZOMBIE:
129.  	    case PM_ETTIN_ZOMBIE:
130.  		num = undead_to_corpse(mndx);
131.  		obj = mkcorpstat(CORPSE, &mons[num], x, y, TRUE);
132.  		obj->age -= 100;		/* this is an *OLD* corpse */
133.  		break;
134.  	    case PM_IRON_GOLEM:
135.  		num = d(2,6);
136.  		while (num--)
137.  			obj = mksobj_at(IRON_CHAIN, x, y, TRUE);
138.  		mtmp->mnamelth = 0;
139.  		break;
140.  	    case PM_CLAY_GOLEM:
141.  		obj = mksobj_at(ROCK, x, y, FALSE);
142.  		obj->quan = (long)(rn2(20) + 50);
143.  		obj->owt = weight(obj);
144.  		mtmp->mnamelth = 0;
145.  		break;
146.  	    case PM_STONE_GOLEM:
147.  		obj = mkcorpstat(STATUE, mdat, x, y, FALSE);
148.  		break;
149.  	    case PM_WOOD_GOLEM:
150.  		num = d(2,4);
151.  		while(num--) {
152.  			obj = mksobj_at(QUARTERSTAFF, x, y, TRUE);
153.  			if (obj && obj->oartifact) {	/* don't allow this */
154.  				artifact_exists(obj, ONAME(obj), FALSE);
155.  				Strcpy(ONAME(obj), "");  obj->onamelth = 0;
156.  			}
157.  		}
158.  		mtmp->mnamelth = 0;
159.  		break;
160.  	    case PM_LEATHER_GOLEM:
161.  		num = d(2,4);
162.  		while(num--)
163.  			obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE);
164.  		mtmp->mnamelth = 0;
165.  		break;
166.  	    default_1:
167.  	    default:
168.  		if (mvitals[mndx].mvflags & G_NOCORPSE)
169.  			return (struct obj *)0;
170.  		else obj = mkcorpstat(CORPSE, mdat, x, y, TRUE);
171.  		break;
172.  	}
173.  	/* All special cases should precede the G_NOCORPSE check */
174.  
175.  	if (mtmp->mnamelth)
176.  	    obj = oname(obj, NAME(mtmp));
177.  	stackobj(obj);
178.  	newsym(x, y);
179.  	return obj;
180.  }
181.  
182.  #endif /* OVLB */
183.  #ifdef OVL1
184.  
185.  static void
186.  warn_effects()
187.  {
188.      if (warnlevel == 100) {
189.  	if(!Blind &&
190.  	    (warnlevel > lastwarnlev || moves > lastwarntime + warnDelay)) {
191.  	    Your("%s %s!", aobjnam(uwep, "glow"),
192.  		hcolor(light_blue));
193.  	    lastwarnlev = warnlevel;
194.  	    lastwarntime = moves;
195.  	}
196.  	warnlevel = 0;
197.  	return;
198.      }
199.  
200.      if(warnlevel >= SIZE(warnings))
201.  	warnlevel = SIZE(warnings)-1;
202.      if(!Blind && warnlevel >= 0)
203.  	if(warnlevel > lastwarnlev || moves > lastwarntime + warnDelay) {
204.  	    register const char *rr;
205.  
206.  	    lastwarntime = moves;
207.  	    lastwarnlev = warnlevel;
208.  	    switch((int) (Warning & (LEFT_RING | RIGHT_RING))) {
209.  	    case LEFT_RING:
210.  		rr = Hallucination ? "left mood ring glows" : "left ring glows";
211.  		break;
212.  	    case RIGHT_RING:
213.  		rr = Hallucination ? "right mood ring glows"
214.  			: "right ring glows";
215.  		break;
216.  	    case LEFT_RING | RIGHT_RING:
217.  		rr = Hallucination ? "mood rings glow" : "rings both glow";
218.  		break;
219.  	    default:
220.  		if (Hallucination)
221.  		    Your("spider-sense is tingling...");
222.  		else
223.  		    You_feel("apprehensive as you sense a %s flash.",
224.  			warnings[warnlevel]);
225.  		return;
226.  	    }
227.  	    Your("%s %s!", rr, hcolor(warnings[warnlevel]));
228.  	}
229.  }
230.  
231.  /* check mtmp and water for compatibility, 0 (survived), 1 (drowned) */
232.  int
233.  minwater(mtmp)
234.  register struct monst *mtmp;
235.  {
236.      boolean inpool, infountain;
237.  
238.      inpool = is_pool(mtmp->mx,mtmp->my) &&
239.  	     !is_flyer(mtmp->data) && !is_floater(mtmp->data);
240.      infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
241.  
242.      /* Gremlin multiplying won't go on forever since the hit points
243.       * keep going down, and when it gets to 1 hit point the clone
244.       * function will fail.
245.       */
246.      if (mtmp->data == &mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
247.  	struct monst *mtmp2 = clone_mon(mtmp);
248.  
249.  	if (mtmp2) {
250.  	    mtmp2->mhpmax = (mtmp->mhpmax /= 2);
251.  	    if(cansee(mtmp->mx,mtmp->my))
252.  		pline("%s multiplies.", Monnam(mtmp));
253.  	    dryup(mtmp->mx,mtmp->my);
254.  	}
255.  	if (inpool) water_damage(mtmp->minvent, FALSE, FALSE);
256.  	return (0);
257.      }
258.      if (inpool) {
259.  	/* Most monsters drown in pools.  flooreffects() will take care of
260.  	 * water damage to dead monsters' inventory, but survivors need to
261.  	 * be handled here.  Swimmers are able to protect their stuff...
262.  	 */
263.  	if (!is_clinger(mtmp->data)
264.  	    && !is_swimmer(mtmp->data) && !amphibious(mtmp->data)) {
265.  	    if (cansee(mtmp->mx,mtmp->my))
266.  		pline("%s drowns.", Monnam(mtmp));
267.  	    mondead(mtmp);
268.  	    if (mtmp->mhp > 0) {
269.  		rloc(mtmp);
270.  		water_damage(mtmp->minvent, FALSE, FALSE);
271.  		return 0;
272.  	    }
273.  	    return (1);
274.  	}
275.      } else {
276.  	/* but eels have a difficult time outside */
277.  	if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)) {
278.  	    if(mtmp->mhp > 1) mtmp->mhp--;
279.  	    mtmp->mflee = 1;
280.  	    mtmp->mfleetim += 2;
281.  	}
282.      }
283.      return (0);
284.  }
285.  
286.  void
287.  movemon()
288.  {
289.      register struct monst *mtmp;
290.      register boolean tametype = TRUE;
291.  
292.      warnlevel = 0;
293.  
294.      while(1) {
295.  	/* Find a monster that we have not treated yet.
296.  	 * Note that mtmp or mtmp->nmon might get killed
297.  	 * while mtmp moves, so we cannot just walk down the
298.  	 * chain (even new monsters might get created!)
299.  	 *
300.  	 * Do tame monsters first.  Necessary so that when the tame
301.  	 * monster attacks something, the something gets a chance to
302.  	 * attack the tame monster back (which it's permitted to do
303.  	 * only if it hasn't made its move yet).
304.  	 */
305.  	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
306.  	    if(mtmp->mlstmv < monstermoves &&
307.  	       ((mtmp->mtame>0) == tametype)) goto next_mon;
308.  	if(tametype) {
309.  	    /* checked all tame monsters, now do other ones */
310.  	    tametype = FALSE;
311.  	    continue;
312.  	}
313.  	/* treated all monsters */
314.  	break;
315.  
316.      next_mon:
317.  	mtmp->mlstmv = monstermoves;
318.  
319.  	if(mtmp->mhp <= 0) {
320.  	    impossible("Monster with zero hp?");
321.  	    mtmp->mhp = mtmp->mhpmax = 1; /* repair */
322.  	}
323.  	if (minwater(mtmp)) continue;
324.  
325.  	if(mtmp->mblinded && !--mtmp->mblinded)
326.  	    mtmp->mcansee = 1;
327.  	if(mtmp->mfrozen && !--mtmp->mfrozen)
328.  	    mtmp->mcanmove = 1;
329.  	if(mtmp->mfleetim && !--mtmp->mfleetim)
330.  	    mtmp->mflee = 0;
331.  	if (is_hider(mtmp->data) &&
332.  		(mtmp->mundetected ||
333.  		 restrap(mtmp) ||	/* maybe hide again */
334.  		 (mtmp->m_ap_type == M_AP_FURNITURE ||
335.  					mtmp->m_ap_type == M_AP_OBJECT))) {
336.  	    mon_regen(mtmp, TRUE);	/* heal while resting */
337.  	    continue;
338.  	}
339.  	if(mtmp->mspeed != MSLOW || !(moves%2)) {
340.  	    /* continue if the monster died fighting */
341.  	    if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
342.  		/* Note:
343.  		 *  Conflict does not take effect in the first round.
344.  		 *  Therefore, A monster when stepping into the area will
345.  		 *  get to swing at you.
346.  		 *
347.  		 *  The call to fightm() must be _last_.  The monster might
348.  		 *  have died if it returns 1.
349.  		 */
350.  		if (couldsee(mtmp->mx,mtmp->my) &&
351.  		    (distu(mtmp->mx,mtmp->my) <= BOLT_LIM*BOLT_LIM) &&
352.  								fightm(mtmp))
353.  		     continue;	/* mon might have died */
354.  	    }
355.  	    if(dochugw(mtmp))
356.  		/* otherwise just move the monster */
357.  		continue;
358.  	}
359.  	if(mtmp->mspeed == MFAST && dochugw(mtmp))
360.  	    continue;
361.      }
362.      if(warnlevel > 0)
363.  	warn_effects();
364.  
365.      if (any_light_source())
366.  	vision_full_recalc = 1;	/* in case a mon moved with a light source */
367.      if (fdmon) dmonsfree();	/* remove all dead monsters */
368.  }
369.  
370.  #endif /* OVL1 */
371.  #ifdef OVLB
372.  
373.  #define mstoning(obj)	(ofood(obj) && ((obj)->corpsenm == PM_COCKATRICE || \
374.  					(obj)->corpsenm == PM_MEDUSA))
375.  
376.  /*
377.   * Maybe eat a metallic object (not just gold).
378.   * Return value: 0 => nothing happened, 1 => monster ate something,
379.   * 2 => monster died (it must have grown into a genocided form, but
380.   * that can't happen at present because nothing which eats objects
381.   * has young and old forms).
382.   */
383.  int
384.  meatgold(mtmp)
385.  	register struct monst *mtmp;
386.  {
387.  	register struct obj *otmp;
388.  	struct permonst *ptr;
389.  	int poly, grow, heal, mstone;
390.  
391.  	/* If a pet, eating is handled separately, in dog.c */
392.  	if (mtmp->mtame) return 0;
393.  
394.  	/* Eats topmost metal object if it is there */
395.  	for (otmp = level.objects[mtmp->mx][mtmp->my];
396.  						    otmp; otmp = otmp->nexthere)
397.  	    if (is_metallic(otmp) && !obj_resists(otmp, 5, 95) &&
398.  		touch_artifact(otmp,mtmp)) {
399.  		if (mtmp->data == &mons[PM_RUST_MONSTER] && otmp->oerodeproof) {
400.  		    if (canseemon(mtmp) && flags.verbose) {
401.  			pline("%s eats %s!",
402.  				Monnam(mtmp),
403.  				distant_name(otmp,doname));
404.  		    }
405.  		    /* The object's rustproofing is gone now */
406.  		    otmp->oerodeproof = 0;
407.  		    mtmp->mstun = 1;
408.  		    pline("%s spits %s out in disgust!",
409.  		    	Monnam(mtmp),
410.  		    	distant_name(otmp,doname));
411.  		} else {
412.  		    if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
413.  			pline("%s eats %s!", Monnam(mtmp),
414.  				distant_name(otmp,doname));
415.  		    else if (flags.soundok && flags.verbose)
416.  			You_hear("a crunching sound.");
417.  		    mtmp->meating = otmp->owt/2 + 1;
418.  		    /* Heal up to the object's weight in hp */
419.  		    if (mtmp->mhp < mtmp->mhpmax) {
420.  			mtmp->mhp += objects[otmp->otyp].oc_weight;
421.  			if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
422.  		    }
423.  		    if(otmp == uball) {
424.  			unpunish();
425.  			delobj(otmp);
426.  		    } else if (otmp == uchain) {
427.  			unpunish();	/* frees uchain */
428.  		    } else {
429.  			poly = polyfodder(otmp);
430.  			grow = mlevelgain(otmp);
431.  			heal = mhealup(otmp);
432.  			mstone = mstoning(otmp);
433.  			delobj(otmp);
434.  			ptr = mtmp->data;
435.  			if (poly) {
436.  			    if (newcham(mtmp, (struct permonst *)0))
437.  				ptr = mtmp->data;
438.  			} else if (grow) {
439.  			    ptr = grow_up(mtmp, (struct monst *)0);
440.  			} else if (mstone) {
441.  			    if (poly_when_stoned(ptr)) {
442.  			    	mon_to_stone(mtmp);
443.  			    	ptr = mtmp->data;
444.  			    } else if (!resists_ston(mtmp)) {
445.  				if (canseemon(mtmp))
446.  				    pline("%s turns to stone!", Monnam(mtmp));
447.  				monstone(mtmp);
448.  				ptr = (struct permonst *)0;
449.  			    }
450.  			} else if (heal) {
451.  			    mtmp->mhp = mtmp->mhpmax;
452.  			}
453.  			if (!ptr) return 2;		 /* it died */
454.  		    }
455.  		    /* Left behind a pile? */
456.  		    if(rnd(25) < 3) (void) mksobj_at(ROCK, mtmp->mx, mtmp->my, TRUE);
457.  		    newsym(mtmp->mx, mtmp->my);
458.  		    return 1;
459.  		}
460.  	    }
461.  	return 0;
462.  }
463.  
464.  int
465.  meatobj(mtmp)		/* for gelatinous cubes */
466.  	register struct monst *mtmp;
467.  {
468.  	register struct obj *otmp, *otmp2;
469.  	struct permonst *ptr;
470.  	int poly, grow, heal, count = 0;
471.  
472.  	/* If a pet, eating is handled separately, in dog.c */
473.  	if (mtmp->mtame) return 0;
474.  
475.  	/* Eats organic objects, including cloth and wood, if there */
476.  	/* Engulfs others, except huge rocks and metal attached to player */
477.  	for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
478.  	    otmp2 = otmp->nexthere;
479.  	    if (is_organic(otmp) && !obj_resists(otmp, 5, 95) &&
480.  		    touch_artifact(otmp,mtmp)) {
481.  		if (otmp->otyp == CORPSE && otmp->corpsenm == PM_COCKATRICE &&
482.  			!resists_ston(mtmp))
483.  		    continue;
484.  		++count;
485.  		if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
486.  		    pline("%s eats %s!", Monnam(mtmp),
487.  			    distant_name(otmp, doname));
488.  		else if (flags.soundok && flags.verbose)
489.  		    You_hear("a slurping sound.");
490.  		/* Heal up to the object's weight in hp */
491.  		if (mtmp->mhp < mtmp->mhpmax) {
492.  		    mtmp->mhp += objects[otmp->otyp].oc_weight;
493.  		    if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
494.  		}
495.  		if (Has_contents(otmp)) {
496.  		    register struct obj *otmp3;
497.  		    /* contents of eaten containers become engulfed; this
498.  		       is arbitrary, but otherwise g.cubes are too powerful */
499.  		    while ((otmp3 = otmp->cobj) != 0) {
500.  			obj_extract_self(otmp3);
501.  			if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) {
502.  			    otmp3->age = monstermoves - otmp3->age;
503.  			    start_corpse_timeout(otmp3);
504.  			}
505.  			mpickobj(mtmp, otmp3);
506.  		    }
507.  		}
508.  		poly = polyfodder(otmp);
509.  		grow = mlevelgain(otmp);
510.  		heal = mhealup(otmp);
511.  		delobj(otmp);		/* munch */
512.  		ptr = mtmp->data;
513.  		if (poly) {
514.  		    if (newcham(mtmp, (struct permonst *)0)) ptr = mtmp->data;
515.  		} else if (grow) {
516.  		    ptr = grow_up(mtmp, (struct monst *)0);
517.  		} else if (heal) {
518.  		    mtmp->mhp = mtmp->mhpmax;
519.  		}
520.  		/* in case it polymorphed or died */
521.  		if (ptr != &mons[PM_GELATINOUS_CUBE])
522.  		    return !ptr ? 2 : 1;
523.  	    } else if (otmp->oclass != ROCK_CLASS &&
524.  				    otmp != uball && otmp != uchain) {
525.  		if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
526.  		    pline("%s engulfs %s.", Monnam(mtmp),
527.  			    distant_name(otmp,doname));
528.  		obj_extract_self(otmp);
529.  		mpickobj(mtmp, otmp);	/* slurp */
530.  	    }
531.  	    /* Engulf & devour is instant, so don't set meating */
532.  	    if (mtmp->minvis) newsym(mtmp->mx, mtmp->my);
533.  	}
534.  	return (count > 0) ? 1 : 0;
535.  }
536.  
537.  void
538.  mpickgold(mtmp)
539.  	register struct monst *mtmp;
540.  {
541.      register struct obj *gold;
542.  
543.      if ((gold = g_at(mtmp->mx, mtmp->my)) != 0) {
544.  	mtmp->mgold += gold->quan;
545.  	delobj(gold);
546.  	if (cansee(mtmp->mx, mtmp->my) ) {
547.  	    if (flags.verbose && !mtmp->isgd)
548.  		pline("%s picks up some gold.", Monnam(mtmp));
549.  	    newsym(mtmp->mx, mtmp->my);
550.  	}
551.      }
552.  }
553.  #endif /* OVLB */
554.  #ifdef OVL2
555.  
556.  boolean
557.  mpickstuff(mtmp, str)
558.  	register struct monst *mtmp;
559.  	register const char *str;
560.  {
561.  	register struct obj *otmp, *otmp2;
562.  
563.  /*	prevent shopkeepers from leaving the door of their shop */
564.  	if(mtmp->isshk && inhishop(mtmp)) return FALSE;
565.  
566.  	for(otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
567.  	    otmp2 = otmp->nexthere;
568.  /*	Nymphs take everything.  Most monsters don't pick up corpses. */
569.  	    if (!str ? searches_for_item(mtmp,otmp) :
570.  		  !!(index(str, otmp->oclass))) {
571.  		if (otmp->otyp == CORPSE && (
572.  		    is_rider(&mons[otmp->corpsenm]) ||
573.  		    (otmp->corpsenm == PM_COCKATRICE
574.  			&& !(mtmp->misc_worn_check & W_ARMG)) ||
575.  		    (mtmp->data->mlet != S_NYMPH
576.  			&& otmp->corpsenm != PM_COCKATRICE
577.  			&& otmp->corpsenm != PM_LIZARD
578.  			&& !acidic(&mons[otmp->corpsenm]))
579.  		   ))
580.  			continue;
581.  		if (!touch_artifact(otmp,mtmp)) continue;
582.  		if (!can_carry(mtmp,otmp)) continue;
583.  		if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
584.  			pline("%s picks up %s.", Monnam(mtmp),
585.  			      (distu(mtmp->my, mtmp->my) <= 5) ?
586.  				doname(otmp) : distant_name(otmp, doname));
587.  		obj_extract_self(otmp);
588.  		mpickobj(mtmp, otmp);
589.  		m_dowear(mtmp, FALSE);
590.  		newsym(mtmp->mx, mtmp->my);
591.  		if (otmp->otyp == BOULDER)
592.  		    unblock_point(otmp->ox,otmp->oy);	/* vision */
593.  		return TRUE;			/* pick only one object */
594.  	    }
595.  	}
596.  	return FALSE;
597.  }
598.  
599.  #endif /* OVL2 */
600.  #ifdef OVL0
601.  
602.  int
603.  curr_mon_load(mtmp)
604.  register struct monst *mtmp;
605.  {
606.  	register int curload = 0;
607.  	register struct obj *obj;
608.  
609.  	for(obj = mtmp->minvent; obj; obj = obj->nobj) {
610.  		if(obj->otyp != BOULDER || !throws_rocks(mtmp->data))
611.  			curload += obj->owt;
612.  	}
613.  
614.  	return curload;
615.  }
616.  
617.  int
618.  max_mon_load(mtmp)
619.  register struct monst *mtmp;
620.  {
621.  	register long maxload;
622.  
623.  	/* Base monster carrying capacity is equal to human maximum
624.  	 * carrying capacity, or half human maximum if not strong.
625.  	 * (for a polymorphed player, the value used would be the
626.  	 * non-polymorphed carrying capacity instead of max/half max).
627.  	 * This is then modified by the ratio between the monster weights
628.  	 * and human weights.  Corpseless monsters are given a capacity
629.  	 * proportional to their size instead of weight.
630.  	 */
631.  	if (!mtmp->data->cwt)
632.  		maxload = (MAX_CARR_CAP * (long)mtmp->data->msize) / MZ_HUMAN;
633.  	else if (!strongmonst(mtmp->data)
634.  		|| (strongmonst(mtmp->data) && (mtmp->data->cwt > WT_HUMAN)))
635.  		maxload = (MAX_CARR_CAP * (long)mtmp->data->cwt) / WT_HUMAN;
636.  	else	maxload = MAX_CARR_CAP; /*strong monsters w/cwt <= WT_HUMAN*/
637.  
638.  	if (!strongmonst(mtmp->data)) maxload /= 2;
639.  
640.  	if (maxload < 1) maxload = 1;
641.  
642.  	return (int) maxload;
643.  }
644.  
645.  /* for restricting monsters' object-pickup */
646.  boolean
647.  can_carry(mtmp,otmp)
648.  struct monst *mtmp;
649.  struct obj *otmp;
650.  {
651.  	register int newload = otmp->owt;
652.  
653.  	if (otmp->otyp == CORPSE && otmp->corpsenm == PM_COCKATRICE &&
654.  		!(mtmp->misc_worn_check & W_ARMG) &&
655.  		!resists_ston(mtmp))
656.  	    return FALSE;
657.  
658.  	if (mtmp->isshk) return(TRUE); /* no limit */
659.  	if (mtmp->mpeaceful && !mtmp->mtame) return(FALSE);
660.  	/* otherwise players might find themselves obligated to violate
661.  	 * their alignment if the monster takes something they need
662.  	 */
663.  
664.  	/* special--boulder throwers carry unlimited amounts of boulders */
665.  	if (throws_rocks(mtmp->data) && otmp->otyp == BOULDER)
666.  		return(TRUE);
667.  
668.  	/* nymphs deal in stolen merchandise, but not boulders or statues */
669.  	if (mtmp->data->mlet == S_NYMPH)
670.  		return((boolean)(!(otmp->oclass == ROCK_CLASS)));
671.  
672.  	if(curr_mon_load(mtmp) + newload > max_mon_load(mtmp)) return(FALSE);
673.  
674.  	return(TRUE);
675.  }
676.  
677.  /* return number of acceptable neighbour positions */
678.  int
679.  mfndpos(mon, poss, info, flag)
680.  	register struct monst *mon;
681.  	coord *poss;	/* coord poss[9] */
682.  	long *info;	/* long info[9] */
683.  	long flag;
684.  {
685.  	struct permonst *mdat = mon->data;
686.  	register xchar x,y,nx,ny;
687.  	register int cnt = 0;
688.  	register uchar ntyp;
689.  	uchar nowtyp;
690.  	boolean wantpool,poolok,lavaok,nodiag;
691.  	int maxx, maxy;
692.  
693.  	x = mon->mx;
694.  	y = mon->my;
695.  	nowtyp = levl[x][y].typ;
696.  
697.  	nodiag = (mdat == &mons[PM_GRID_BUG]);
698.  	wantpool = mdat->mlet == S_EEL;
699.  	poolok = is_flyer(mdat) || is_clinger(mdat) ||
700.  		 (is_swimmer(mdat) && !wantpool);
701.  	lavaok = is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat);
702.  
703.  nexttry:	/* eels prefer the water, but if there is no water nearby,
704.  		   they will crawl over land */
705.  	if(mon->mconf) {
706.  		flag |= ALLOW_ALL;
707.  		flag &= ~NOTONL;
708.  	}
709.  	if(!mon->mcansee)
710.  		flag |= ALLOW_SSM;
711.  	maxx = min(x+1,COLNO-1);
712.  	maxy = min(y+1,ROWNO-1);
713.  	for(nx = max(1,x-1); nx <= maxx; nx++)
714.  	  for(ny = max(0,y-1); ny <= maxy; ny++) {
715.  	    if(nx == x && ny == y) continue;
716.  	    if(IS_ROCK(ntyp = levl[nx][ny].typ) &&
717.  	       !((flag & ALLOW_WALL) && may_passwall(nx,ny)) &&
718.  	       !((flag & ALLOW_DIG) && may_dig(nx,ny))) continue;
719.  	    if(IS_DOOR(ntyp) && !amorphous(mdat) &&
720.  	       ((levl[nx][ny].doormask & D_CLOSED && !(flag & OPENDOOR)) ||
721.  		(levl[nx][ny].doormask & D_LOCKED && !(flag & UNLOCKDOOR))
722.  	       ) && !(flag & (ALLOW_WALL|ALLOW_DIG|BUSTDOOR))) continue;
723.  	    if(nx != x && ny != y && (nodiag ||
724.  #ifdef REINCARNATION
725.  	       ((IS_DOOR(nowtyp) &&
726.  		 ((levl[x][y].doormask & ~D_BROKEN) || Is_rogue_level(&u.uz))) ||
727.  		(IS_DOOR(ntyp) &&
728.  		 ((levl[nx][ny].doormask & ~D_BROKEN) || Is_rogue_level(&u.uz))))
729.  #else
730.  	       ((IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN)) ||
731.  		(IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN)))
732.  #endif
733.  	       ))
734.  		continue;
735.  	    if((is_pool(nx,ny) == wantpool || poolok) &&
736.  	       (lavaok || !is_lava(nx,ny))) {
737.  		int dispx, dispy;
738.  		boolean monseeu = (mon->mcansee && (!Invis || perceives(mdat)));
739.  		boolean checkobj = OBJ_AT(nx,ny);
740.  
741.  		/* Displacement also displaces the Elbereth/scare monster,
742.  		 * as long as you are visible.
743.  		 */
744.  		if(Displaced && monseeu && (mon->mux==nx) && (mon->muy==ny)) {
745.  		    dispx = u.ux;
746.  		    dispy = u.uy;
747.  		} else {
748.  		    dispx = nx;
749.  		    dispy = ny;
750.  		}
751.  
752.  		info[cnt] = 0;
753.  		if(((checkobj || Displaced) &&
754.  		    sobj_at(SCR_SCARE_MONSTER, dispx, dispy))
755.  #ifdef ELBERETH
756.  		       || sengr_at("Elbereth", dispx, dispy)
757.  #endif
758.  		       || (mdat->mlet == S_VAMPIRE &&
759.  			   IS_ALTAR(levl[dispx][dispy].typ))
760.  		       ) {
761.  		    if(!(flag & ALLOW_SSM)) continue;
762.  		    info[cnt] |= ALLOW_SSM;
763.  		}
764.  		if((nx == u.ux && ny == u.uy) ||
765.  		   (nx == mon->mux && ny == mon->muy)) {
766.  			if (nx == u.ux && ny == u.uy) {
767.  				/* If it's right next to you, it found you,
768.  				 * displaced or no.  We must set mux and muy
769.  				 * right now, so when we return we can tell
770.  				 * that the ALLOW_U means to attack _you_ and
771.  				 * not the image.
772.  				 */
773.  				mon->mux = u.ux;
774.  				mon->muy = u.uy;
775.  			}
776.  			if(!(flag & ALLOW_U)) continue;
777.  			info[cnt] |= ALLOW_U;
778.  		} else {
779.  			if(MON_AT(nx, ny)) {
780.  				if(!(flag & ALLOW_M)) continue;
781.  				info[cnt] |= ALLOW_M;
782.  				if((m_at(nx,ny))->mtame) {
783.  					if(!(flag & ALLOW_TM)) continue;
784.  					info[cnt] |= ALLOW_TM;
785.  				}
786.  			}
787.  			/* Note: ALLOW_SANCT only prevents movement, not */
788.  			/* attack, into a temple. */
789.  			if(level.flags.has_temple &&
790.  			   *in_rooms(nx, ny, TEMPLE) &&
791.  			   !*in_rooms(x, y, TEMPLE) &&
792.  			   in_your_sanctuary(nx, ny)){
793.  				if(!(flag & ALLOW_SANCT)) continue;
794.  				info[cnt] |= ALLOW_SANCT;
795.  			}
796.  		}
797.  		if(checkobj && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
798.  			if(flag & NOGARLIC) continue;
799.  			info[cnt] |= NOGARLIC;
800.  		}
801.  		if(checkobj && sobj_at(BOULDER, nx, ny)) {
802.  			if(!(flag & ALLOW_ROCK)) continue;
803.  			info[cnt] |= ALLOW_ROCK;
804.  		}
805.  		if (monseeu && onlineu(nx,ny)) {
806.  			if(flag & NOTONL) continue;
807.  			info[cnt] |= NOTONL;
808.  		}
809.  		if (nx != x && ny != y && bad_rock(mdat, x, ny)
810.  			    && bad_rock(mdat, nx, y)
811.  			    && (bigmonst(mdat) || (curr_mon_load(mon) > 600)))
812.  			continue;
813.  		/* The monster avoids a particular type of trap if it's familiar
814.  		 * with the trap type.  Pets get ALLOW_TRAPS and checking is
815.  		 * done in dogmove.c.  In either case, "harmless" traps are
816.  		 * neither avoided nor marked in info[].
817.  		 */
818.  		{ register struct trap *ttmp = t_at(nx, ny);
819.  		    if(ttmp) {
820.  			if(ttmp->ttyp >= TRAPNUM || ttmp->ttyp == 0)  {
821.  impossible("A monster looked at a very strange trap of type %d.", ttmp->ttyp);
822.  			    continue;
823.  			}
824.  			if ((ttmp->ttyp != RUST_TRAP
825.  					|| mdat == &mons[PM_IRON_GOLEM])
826.  				&& ttmp->ttyp != STATUE_TRAP
827.  				&& ((ttmp->ttyp != PIT
828.  				    && ttmp->ttyp != SPIKED_PIT
829.  				    && ttmp->ttyp != TRAPDOOR
830.  				    && ttmp->ttyp != HOLE)
831.  				      || (!is_flyer(mdat) && !is_clinger(mdat)))
832.  				&& (ttmp->ttyp != SLP_GAS_TRAP ||
833.  				    !resists_sleep(mon))
834.  				&& (ttmp->ttyp != BEAR_TRAP ||
835.  				    (mdat->msize > MZ_SMALL &&
836.  				     !amorphous(mdat) && !is_flyer(mdat)))
837.  				&& (ttmp->ttyp != FIRE_TRAP ||
838.  				    !resists_fire(mon))
839.  				&& (ttmp->ttyp != SQKY_BOARD || !is_flyer(mdat))
840.  				&& (ttmp->ttyp != WEB || (!amorphous(mdat) &&
841.  				    mdat->mlet != S_SPIDER))
842.  			) {
843.  			    if (!(flag & ALLOW_TRAPS)) {
844.  				if (mon->mtrapseen & (1L << (ttmp->ttyp - 1)))
845.  				    continue;
846.  			    }
847.  			    info[cnt] |= ALLOW_TRAPS;
848.  			}
849.  		    }
850.  		}
851.  		poss[cnt].x = nx;
852.  		poss[cnt].y = ny;
853.  		cnt++;
854.  	    }
855.  	}
856.  	if(!cnt && wantpool && !is_pool(x,y)) {
857.  		wantpool = FALSE;
858.  		goto nexttry;
859.  	}
860.  	return(cnt);
861.  }
862.  
863.  #endif /* OVL0 */
864.  #ifdef OVL1
865.  
866.  boolean
867.  monnear(mon, x, y)
868.  register struct monst *mon;
869.  register int x,y;
870.  /* Is the square close enough for the monster to move or attack into? */
871.  {
872.  	register int distance = dist2(mon->mx, mon->my, x, y);
873.  	if (distance==2 && mon->data==&mons[PM_GRID_BUG]) return 0;
874.  	return((boolean)(distance < 3));
875.  }
876.  
877.  /* really free dead monsters */
878.  void
879.  dmonsfree()
880.  {
881.  	register struct monst *mtmp;
882.  
883.  	while ((mtmp = fdmon) != 0) {
884.  		fdmon = mtmp->nmon;
885.  		dealloc_monst(mtmp);
886.  	}
887.  }
888.  
889.  #endif /* OVL1 */
890.  #ifdef OVLB
891.  
892.  /* we do not free monsters immediately, in order to have their name
893.     available shortly after their demise */
894.  void
895.  monfree(mtmp)
896.  register struct monst *mtmp;
897.  {
898.  #if 0	/* can't do this; make_corpse() needs the coordinates */
899.  	mtmp->mx = mtmp->my = 0;	/* not on the map any more */
900.  #endif
901.  	mtmp->mhp = 0;			/* not living any more */
902.  	mtmp->nmon = fdmon;
903.  	fdmon = mtmp;
904.  }
905.  
906.  /* called when monster is moved to larger structure */
907.  void
908.  replmon(mtmp, mtmp2)
909.  register struct monst *mtmp, *mtmp2;
910.  {
911.      struct obj *otmp;
912.  
913.      /* transfer the monster's inventory */
914.      for (otmp = mtmp2->minvent; otmp; otmp = otmp->nobj) {
915.  #ifdef DEBUG
916.  	if (otmp->where != OBJ_MINVENT || otmp->ocarry != mtmp)
917.  	    panic("replmon: minvent inconsistency");
918.  #endif
919.  	otmp->ocarry = mtmp2;
920.      }
921.  
922.      relmon(mtmp);
923.      monfree(mtmp);
924.      place_monster(mtmp2, mtmp2->mx, mtmp2->my);
925.      if (mtmp2->wormno)	    /* update level.monsters[wseg->wx][wseg->wy] */
926.  	place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
927.      if (emits_light(mtmp2->data)) {
928.  	/* since this is so rare, we don't have any `mon_move_light_source' */
929.  	new_light_source(mtmp2->mx, mtmp2->my,
930.  			 emits_light(mtmp2->data),
931.  			 LS_MONSTER, (genericptr_t)mtmp2);
932.  	/* here we rely on the fact that `mtmp' hasn't actually been deleted */
933.  	del_light_source(LS_MONSTER, (genericptr_t)mtmp);
934.      }
935.      mtmp2->nmon = fmon;
936.      fmon = mtmp2;
937.      if (u.ustuck == mtmp) u.ustuck = mtmp2;
938.      if (mtmp2->isshk) replshk(mtmp,mtmp2);
939.  }
940.  
941.  /* release mon from display and monster list */
942.  void
943.  relmon(mon)
944.  register struct monst *mon;
945.  {
946.  	register struct monst *mtmp;
947.  
948.  	if (fmon == (struct monst *)0)  panic ("relmon: no fmon available.");
949.  
950.  	remove_monster(mon->mx, mon->my);
951.  
952.  	if(mon == fmon) fmon = fmon->nmon;
953.  	else {
954.  		for(mtmp = fmon; mtmp && mtmp->nmon != mon; mtmp = mtmp->nmon) ;
955.  		if(mtmp)    mtmp->nmon = mon->nmon;
956.  		else	    panic("relmon: mon not in list.");
957.  	}
958.  }
959.  
960.  /* remove effects of mtmp from other data structures */
961.  static void
962.  m_detach(mtmp, mptr)
963.  struct monst *mtmp;
964.  struct permonst *mptr;	/* reflects mtmp->data _prior_ to mtmp's death */
965.  {
966.  	if(mtmp->mleashed) m_unleash(mtmp);
967.  	    /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
968.  	mtmp->mtrapped = 0;
969.  	mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */
970.  	relobj(mtmp, 0, FALSE);
971.  	relmon(mtmp);
972.  	if (emits_light(mptr))
973.  	    del_light_source(LS_MONSTER, (genericptr_t)mtmp);
974.  	newsym(mtmp->mx,mtmp->my);
975.  	unstuck(mtmp);
976.  	fill_pit(mtmp->mx, mtmp->my);
977.  
978.  	if(mtmp->isshk) shkgone(mtmp);
979.  	if(mtmp->wormno) wormgone(mtmp);
980.  }
981.  
982.  static void FDECL(lifesaved_monster, (struct monst *));
983.  
984.  static void
985.  lifesaved_monster(mtmp)
986.  struct monst *mtmp;
987.  {
988.  	struct obj *lifesave;
989.  
990.  	if (!nonliving(mtmp->data) && (lifesave = which_armor(mtmp, W_AMUL))
991.  			&& lifesave->otyp == AMULET_OF_LIFE_SAVING) {
992.  		/* not canseemon; amulets are on the head, so you don't want */
993.  		/* to show this for a long worm with only a tail visible. */
994.  		/* Nor do you check invisibility, because glowing and disinte- */
995.  		/* grating amulets are always visible. */
996.  		if (cansee(mtmp->mx, mtmp->my)) {
997.  			pline("But wait...");
998.  			pline("%s medallion begins to glow!",
999.  				s_suffix(Monnam(mtmp)));
1000. 			makeknown(AMULET_OF_LIFE_SAVING);
1001. 			pline("%s looks much better!", Monnam(mtmp));
1002. 			pline_The("medallion crumbles to dust!");
1003. 		}
1004. 		m_useup(mtmp, lifesave);
1005. 		if (mtmp->mhpmax <= 0) mtmp->mhpmax = 10;
1006. 		mtmp->mhp = mtmp->mhpmax;
1007. 		mtmp->mcanmove = 1;
1008. 		mtmp->mfrozen = 0;
1009. 		if (mtmp->mtame && !mtmp->isminion) {
1010. 			struct edog *edog = EDOG(mtmp);
1011. 			if (edog->hungrytime < moves+500)
1012. 				edog->hungrytime = moves+500;
1013. 		}
1014. 		if (mvitals[monsndx(mtmp->data)].mvflags & G_GENOD)
1015. 			pline("Unfortunately %s is still genocided...",
1016. 				mon_nam(mtmp));
1017. 		else
1018. 			return;
1019. 	}
1020. 	mtmp->mhp = 0;
1021. }
1022. 
1023. void
1024. mondead(mtmp)
1025. register struct monst *mtmp;
1026. {
1027. 	struct permonst *mptr;
1028. 	int tmp, lim;
1029. 
1030. 	if(mtmp->isgd) {
1031. 		/* if we're going to abort the death, it *must* be before
1032. 		 * the m_detach or there will be relmon problems later */
1033. 		if(!grddead(mtmp)) return;
1034. 	}
1035. 	lifesaved_monster(mtmp);
1036. 	if (mtmp->mhp > 0) return;
1037. 
1038. 	mptr = mtmp->data;		/* save this for m_detach() */
1039. 	/* restore chameleon, lycanthropes to true form at death */
1040. 	if (mtmp->cham)
1041. 	    set_mon_data(mtmp, &mons[PM_CHAMELEON], -1);
1042. 	else if (mtmp->data == &mons[PM_WEREJACKAL])
1043. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1044. 	else if (mtmp->data == &mons[PM_WEREWOLF])
1045. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1046. 	else if (mtmp->data == &mons[PM_WERERAT])
1047. 	    set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1048. 
1049. 	/* if MAXMONNO monsters of a given type have died, and it
1050. 	 * can be done, extinguish that monster.
1051. 	 *
1052. 	 * mvitals[].died does double duty as total number of dead monsters
1053. 	 * and as experience factor for the player killing more monsters.
1054. 	 * this means that a dragon dying by other means reduces the
1055. 	 * experience the player gets for killing a dragon directly; this
1056. 	 * is probably not too bad, since the player likely finagled the
1057. 	 * first dead dragon via ring of conflict or pets, and extinguishing
1058. 	 * based on only player kills probably opens more avenues of abuse
1059. 	 * for rings of conflict and such.
1060. 	 */
1061. 	tmp = monsndx(mtmp->data);
1062. 	if (mvitals[tmp].died < 255) mvitals[tmp].died++;
1063. 	lim = (tmp == PM_NAZGUL ? 9 : tmp == PM_ERINYS ? 3 : MAXMONNO);
1064. 	if ((int) mvitals[tmp].died > lim && !(mons[tmp].geno & G_NOGEN) &&
1065. 		!(mvitals[tmp].mvflags & G_EXTINCT)) {
1066. #ifdef DEBUG
1067. 		pline("Automatically extinguished %s.",
1068. 					makeplural(mons[tmp].mname));
1069. #endif
1070. 		mvitals[tmp].mvflags |= G_EXTINCT;
1071. 		reset_rndmonst(tmp);
1072. 	}
1073. #ifdef MAIL
1074. 	/* if the mail daemon dies, no more mail delivery.  -3. */
1075. 	else if (tmp == PM_MAIL_DAEMON) mvitals[tmp].mvflags |= G_GENOD;
1076. #endif
1077. 
1078. #ifdef KOPS
1079. 	if (mtmp->data->mlet == S_KOP) {
1080. 	    /* Dead Kops may come back. */
1081. 	    switch(rnd(5)) {
1082. 		case 1:	     /* returns near the stairs */
1083. 			(void) makemon(mtmp->data,xdnstair,ydnstair);
1084. 			break;
1085. 		case 2:	     /* randomly */
1086. 			(void) makemon(mtmp->data,0,0);
1087. 			break;
1088. 		default:
1089. 			break;
1090. 	    }
1091. 	}
1092. #endif
1093. 	if(mtmp->iswiz) wizdead();
1094. 	if(mtmp->data->msound == MS_NEMESIS) nemdead();
1095. 	m_detach(mtmp, mptr);
1096. 	monfree(mtmp);
1097. }
1098. 
1099. static boolean
1100. corpse_chance(mon)
1101. struct monst *mon;
1102. {
1103. 	struct permonst *mdat = mon->data;
1104. 
1105. 	if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1106. 		if (cansee(mon->mx, mon->my))
1107. 			pline("%s%ss body crumbles into dust.", Monnam(mon),
1108. 				canseemon(mon) ? "'" : "");
1109. 		return FALSE;
1110. 	}
1111. 
1112. 	/* must duplicate this below check in xkilled() since it results in
1113. 	 * creating no objects as well as no corpse
1114. 	 */
1115. 	if (
1116. #ifdef REINCARNATION
1117. 		 Is_rogue_level(&u.uz) ||
1118. #endif
1119. 	   (level.flags.graveyard && is_undead(mdat) && rn2(3)))
1120. 		return FALSE;
1121. 
1122. 	if (bigmonst(mdat) || mdat == &mons[PM_LIZARD]
1123. 		   || is_golem(mdat)
1124. 		   || is_mplayer(mdat)
1125. 		   || is_rider(mdat))
1126. 		return TRUE;
1127. 	return (boolean) (!rn2((int)
1128. 		(2 + ((int)(mdat->geno & G_FREQ)<2) + verysmall(mdat))));
1129. }
1130. 
1131. /* drop (perhaps) a cadaver and remove monster */
1132. void
1133. mondied(mdef)
1134. register struct monst *mdef;
1135. {
1136. 	mondead(mdef);
1137. 	if (mdef->mhp > 0) return;	/* lifesaved */
1138. 
1139. 	if (corpse_chance(mdef))
1140. 		(void) make_corpse(mdef);
1141. }
1142. 
1143. /* monster disappears, not dies */
1144. void
1145. mongone(mdef)
1146. register struct monst *mdef;
1147. {
1148. 	discard_minvent(mdef);	/* release monster's inventory */
1149. 	mdef->mgold = 0L;
1150. 	m_detach(mdef, mdef->data);
1151. 	monfree(mdef);
1152. }
1153. 
1154. /* drop a statue or rock and remove monster */
1155. void
1156. monstone(mdef)
1157. register struct monst *mdef;
1158. {
1159. 	struct obj *otmp, *obj;
1160. 	xchar x = mdef->mx, y = mdef->my;
1161. 
1162. 	/* we have to make the statue before calling mondead, to be able to
1163. 	 * put inventory in it, and we have to check for lifesaving before
1164. 	 * making the statue....
1165. 	 */
1166. 	lifesaved_monster(mdef);
1167. 	if (mdef->mhp > 0) return;
1168. 
1169. 	mdef->mtrapped = 0;	/* (see m_detach) */
1170. 
1171. 	if((int)mdef->data->msize > MZ_TINY ||
1172. 	   !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
1173. 		otmp = mk_named_object(STATUE, mdef->data, x, y,
1174. 				       mdef->mnamelth ? NAME(mdef) : (char *)0);
1175. 		/* some objects may end up outside the statue */
1176. 		while ((obj = mdef->minvent) != 0) {
1177. 		    obj_extract_self(obj);
1178. 		    obj->owornmask = 0L;
1179. 		    if (obj->otyp == BOULDER ||
1180. #if 0				/* monsters don't carry statues */
1181.      (obj->otyp == STATUE && mons[obj->corpsenm].msize >= mdef->data->msize) ||
1182. #endif
1183. 				obj_resists(obj, 0, 0)) {
1184. 			if (flooreffects(obj, x, y, "fall")) continue;
1185. 			place_object(obj, x, y);
1186. 		    } else {
1187. 			if (obj->lamplit) end_burn(obj, TRUE);
1188. 			add_to_container(otmp, obj);
1189. 		    }
1190. 		}
1191. 		if (mdef->mgold) {
1192. 			struct obj *au;
1193. 			au = mksobj(GOLD_PIECE, FALSE, FALSE);
1194. 			au->quan = mdef->mgold;
1195. 			au->owt = weight(au);
1196. 			add_to_container(otmp, au);
1197. 			mdef->mgold = 0;
1198. 		}
1199. 		otmp->owt = weight(otmp);
1200. 	} else
1201. 		otmp = mksobj_at(ROCK, x, y, TRUE);
1202. 
1203. 	stackobj(otmp);
1204. 	if (cansee(x, y)) newsym(x,y);
1205. 	mondead(mdef);
1206. }
1207. 
1208. /* another monster has killed the monster mdef */
1209. void
1210. monkilled(mdef, fltxt, how)
1211. register struct monst *mdef;
1212. const char *fltxt;
1213. int how;
1214. {
1215. 	boolean be_sad = FALSE;		/* true if unseen pet is killed */
1216. 
1217. 	if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
1218. 		&& fltxt)
1219. 	    pline("%s is %s%s%s!", Monnam(mdef),
1220. 			nonliving(mdef->data) ? "destroyed" : "killed",
1221. 		    *fltxt ? " by the " : "",
1222. 		    fltxt
1223. 		 );
1224. 	else
1225. 	    be_sad = (mdef->mtame != 0);
1226. 
1227. 	/* no corpses if digested or disintegrated */
1228. 	if(how == AD_DGST || how == -AD_RBRE)
1229. 	    mondead(mdef);
1230. 	else
1231. 	    mondied(mdef);
1232. 
1233. 	if (be_sad && mdef->mhp <= 0)
1234. 	    You("have a sad feeling for a moment, then it passes.");
1235. }
1236. 
1237. void
1238. unstuck(mtmp)
1239. register struct monst *mtmp;
1240. {
1241. 	if(u.ustuck == mtmp) {
1242. 		if(u.uswallow){
1243. 			u.ux = mtmp->mx;
1244. 			u.uy = mtmp->my;
1245. 			u.uswallow = 0;
1246. 			u.uswldtim = 0;
1247. 			if (Punished) placebc();
1248. 			vision_full_recalc = 1;
1249. 			docrt();
1250. 		}
1251. 		u.ustuck = 0;
1252. 	}
1253. }
1254. 
1255. void
1256. killed(mtmp)
1257. register struct monst *mtmp;
1258. {
1259. 	xkilled(mtmp, 1);
1260. }
1261. 
1262. /* the player has killed the monster mtmp */
1263. void
1264. xkilled(mtmp, dest)
1265. 	register struct monst *mtmp;
1266. /*
1267.  * Dest=1, normal; dest=0, don't print message; dest=2, don't drop corpse
1268.  * either; dest=3, message but no corpse
1269.  */
1270. 	int	dest;
1271. {
1272. 	register int tmp, x = mtmp->mx, y = mtmp->my;
1273. 	register struct permonst *mdat;
1274. 	int mndx;
1275. 	register struct obj *otmp;
1276. 	register struct trap *t;
1277. 	boolean redisp = FALSE;
1278. 	boolean wasinside = u.uswallow && (u.ustuck == mtmp);
1279. 
1280. 	if (dest & 1) {
1281. 	    if(!wasinside && !canspotmon(mtmp))
1282. 		You("destroy it!");
1283. 	    else {
1284. 		You("destroy %s!",
1285. 			mtmp->mtame ? x_monnam(mtmp, 0, "poor", 0)
1286. 			: mon_nam(mtmp));
1287. 	    }
1288. 	}
1289. 
1290. 	if (mtmp->mtrapped &&
1291. 	    ((t = t_at(x, y)) && (t->ttyp == PIT || t->ttyp == SPIKED_PIT)) &&
1292. 	    sobj_at(BOULDER, x, y))
1293. 		dest ^= 2; /*
1294. 			    * Prevent corpses/treasure being created "on top"
1295. 			    * of the boulder that is about to fall in. This is
1296. 			    * out of order, but cannot be helped unless this
1297. 			    * whole routine is rearranged.
1298. 			    */
1299. 
1300. 	/* dispose of monster and make cadaver */
1301. 	if(stoned) monstone(mtmp);
1302. 	else mondead(mtmp);
1303. 
1304. 	if (mtmp->mhp > 0) { /* monster lifesaved */
1305. 		/* Cannot put the non-visible lifesaving message in
1306. 		 * lifesaved_monster() since the message appears only when you
1307. 		 * kill it (as opposed to visible lifesaving which always
1308. 		 * appears).
1309. 		 */
1310. 		if (!cansee(x,y)) pline("Maybe not...");
1311. 		return;
1312. 	}
1313. 
1314. 	mdat = mtmp->data; /* note: mondead can change mtmp->data */
1315. 	mndx = monsndx(mdat);
1316. 
1317. 	if (stoned) {
1318. 		stoned = FALSE;
1319. 		goto cleanup;
1320. 	}
1321. 
1322. 	if((dest & 2)
1323. #ifdef REINCARNATION
1324. 		 || Is_rogue_level(&u.uz)
1325. #endif
1326. 	   || (level.flags.graveyard && is_undead(mdat) && rn2(3)))
1327. 		goto cleanup;
1328. 
1329. #ifdef MAIL
1330. 	if(mdat == &mons[PM_MAIL_DAEMON]) {
1331. 		stackobj(mksobj_at(SCR_MAIL, x, y, FALSE));
1332. 		redisp = TRUE;
1333. 	}
1334. #endif
1335. 	if(!accessible(x, y)) {
1336. 	    /* might be mimic in wall or dead eel or in a pool or lava */
1337. 	    redisp = TRUE;
1338. 	    if(wasinside) spoteffects();
1339. 	} else if(x != u.ux || y != u.uy) {
1340. 		/* might be here after swallowed */
1341. 		if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
1342. #ifdef KOPS
1343. 					&& mdat->mlet != S_KOP
1344. #endif
1345. 							) {
1346. 			int typ;
1347. 
1348. 			otmp = mkobj_at(RANDOM_CLASS, x, y, TRUE);
1349. 			/* Don't create large objects from small monsters */
1350. 			typ = otmp->otyp;
1351. 			if (mdat->msize < MZ_HUMAN && typ != FOOD_RATION
1352. 			    && typ != LEASH
1353. 			    && typ != FIGURINE
1354. 			    && (otmp->owt > 3 ||
1355. 				objects[typ].oc_big /*oc_bimanual/oc_bulky*/ ||
1356. 				objects[typ].oc_wepcat == WEP_SPEAR ||
1357. 				objects[typ].oc_wepcat == WEP_POLEARM ||
1358. 				typ == MORNING_STAR)) {
1359. 			    delobj(otmp);
1360. 			} else redisp = TRUE;
1361. 		}
1362. 		/* Whether or not it always makes a corpse is, in theory,
1363. 		 * different from whether or not the corpse is "special";
1364. 		 * if we want both, we have to specify it explicitly.
1365. 		 */
1366. 		if (corpse_chance(mtmp))
1367. 			(void) make_corpse(mtmp);
1368. 	}
1369. 	if(redisp) newsym(x,y);
1370. cleanup:
1371. 	/* punish bad behaviour */
1372. 	if(is_human(mdat) && (!always_hostile(mdat) && mtmp->malign <= 0) &&
1373. 	   (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD) &&
1374. 	   u.ualign.type != A_CHAOTIC) {
1375. 		HTelepat &= ~INTRINSIC;
1376. 		change_luck(-2);
1377. 		if (Blind && !Telepat)
1378. 		    see_monsters(); /* Can't sense monsters any more. */
1379. 	}
1380. 	if((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)	change_luck(-1);
1381. 	if (mdat->mlet == S_UNICORN &&
1382. 				sgn(u.ualign.type) == sgn(mdat->maligntyp))
1383. 		change_luck(-5);
1384. 
1385. 	/* give experience points */
1386. 	tmp = experience(mtmp, (int)mvitals[mndx].died + 1);
1387. 	more_experienced(tmp, 0);
1388. 	newexplevel();		/* will decide if you go up */
1389. 
1390. 	/* adjust alignment points */
1391. 	if (mdat->msound == MS_LEADER)		/* REAL BAD! */
1392. 	    adjalign(-(u.ualign.record+(int)ALIGNLIM/2));
1393. 	else if (mdat->msound == MS_NEMESIS)	/* Real good! */
1394. 	    adjalign((int)(ALIGNLIM/4));
1395. 	else if (mdat->msound == MS_GUARDIAN)	/* Bad */
1396. 	    adjalign(-(int)(ALIGNLIM/8));
1397. 	else if (mtmp->ispriest) {
1398. 		adjalign((p_coaligned(mtmp)) ? -2 : 2);
1399. 		/* cancel divine protection for killing your priest */
1400. 		if (p_coaligned(mtmp)) u.ublessed = 0;
1401. 		if (mdat->maligntyp == A_NONE)
1402. 			adjalign((int)(ALIGNLIM / 4));		/* BIG bonus */
1403. 	} else if (mtmp->mtame)
1404. 		adjalign(-15);	/* bad!! */
1405. 	else if (mtmp->mpeaceful)
1406. 		adjalign(-5);
1407. 
1408. 	/* malign was already adjusted for u.ualign.type and randomization */
1409. 	adjalign(mtmp->malign);
1410. }
1411. 
1412. /* changes the monster into a stone monster of the same type */
1413. /* this should only be called when poly_when_stoned() is true */
1414. void
1415. mon_to_stone(mtmp)
1416.     register struct monst *mtmp;
1417. {
1418.     if(mtmp->data->mlet == S_GOLEM) {
1419. 	/* it's a golem, and not a stone golem */
1420. 	if(canseemon(mtmp))
1421. 	    pline("%s solidifies...", Monnam(mtmp));
1422. 	(void) newcham(mtmp, &mons[PM_STONE_GOLEM]);
1423. 	if(canseemon(mtmp))
1424. 	    pline("Now it's %s.", an(mtmp->data->mname));
1425.     } else
1426. 	impossible("Can't polystone %s!", a_monnam(mtmp));
1427. }
1428. 
1429. void
1430. mnexto(mtmp)	/* Make monster mtmp next to you (if possible) */
1431. 	struct monst *mtmp;
1432. {
1433. 	coord mm;
1434. 
1435. 	if(!enexto(&mm, u.ux, u.uy, mtmp->data)) return;
1436. 
1437. 	rloc_to(mtmp, mm.x, mm.y);
1438. }
1439. 
1440. /* mnearto()
1441.  * Put monster near (or at) location if possible.
1442.  * Returns:
1443.  *	1 - if a monster was moved from x, y to put mtmp at x, y.
1444.  *	0 - in most cases.
1445.  */
1446. boolean
1447. mnearto(mtmp,x,y,move_other)
1448. register struct monst *mtmp;
1449. xchar x, y;
1450. boolean move_other;	/* make sure mtmp gets to x, y! so move m_at(x, y) */
1451. {
1452. 	struct monst *othermon = (struct monst *)0;
1453. 	xchar newx, newy;
1454. 	coord mm;
1455. 
1456. 	if ((mtmp->mx == x) && (mtmp->my == y)) return(FALSE);
1457. 
1458. 	if (move_other && (othermon = m_at(x, y))) {
1459. 		if (othermon->wormno)
1460. 			remove_worm(othermon);
1461. 		else
1462. 			remove_monster(x, y);
1463. 	}
1464. 
1465. 	newx = x;
1466. 	newy = y;
1467. 
1468. 	if (!goodpos(newx, newy, mtmp, mtmp->data)) {
1469. 		/* actually we have real problems if enexto ever fails.
1470. 		 * migrating_mons that need to be placed will cause
1471. 		 * no end of trouble.
1472. 		 */
1473. 		if (!enexto(&mm, newx, newy, mtmp->data)) return(FALSE);
1474. 		newx = mm.x; newy = mm.y;
1475. 	}
1476. 
1477. 	rloc_to(mtmp, newx, newy);
1478. 
1479. 	if (move_other && othermon) {
1480. 	    othermon->mx = othermon->my = 0;
1481. 	    (void) mnearto(othermon, x, y, FALSE);
1482. 	    if ((othermon->mx != x) || (othermon->my != y))
1483. 		return(TRUE);
1484. 	}
1485. 
1486. 	return(FALSE);
1487. }
1488. 
1489. 
1490. static const char *poiseff[] = {
1491. 
1492. 	" feel weaker", "r brain is on fire",
1493. 	"r judgement is impaired", "r muscles won't obey you",
1494. 	" feel very sick", " break out in hives"
1495. };
1496. 
1497. void
1498. poisontell(typ)
1499. 
1500. 	int	typ;
1501. {
1502. 	pline("You%s.", poiseff[typ]);
1503. }
1504. 
1505. void
1506. poisoned(string, typ, pname, fatal)
1507. register const char *string, *pname;
1508. register int  typ, fatal;
1509. {
1510. 	register int i, plural;
1511. 	boolean thrown_weapon = !strncmp(string, "poison", 6);
1512. 		/* admittedly a kludge... */
1513. 
1514. 	if(strcmp(string, "blast") && !thrown_weapon) {
1515. 	    /* 'blast' has already given a 'poison gas' message */
1516. 	    /* so have "poison arrow", "poison dart", etc... */
1517. 	    plural = (string[strlen(string) - 1] == 's')? 1 : 0;
1518. 	    /* avoid "The" Orcus's sting was poisoned... */
1519. 	    pline("%s%s %s poisoned!", isupper(*string) ? "" : "The ",
1520. 			string, plural ? "were" : "was");
1521. 	}
1522. 
1523. 	if(Poison_resistance) {
1524. 		if(!strcmp(string, "blast")) shieldeff(u.ux, u.uy);
1525. 		pline_The("poison doesn't seem to affect you.");
1526. 		return;
1527. 	}
1528. 	i = rn2(fatal + 20*thrown_weapon);
1529. 	if(i == 0 && typ != A_CHA) {
1530. 		u.uhp = -1;
1531. 		pline_The("poison was deadly...");
1532. 	} else if(i <= 5) {
1533. 		pline("You%s!", poiseff[typ]);
1534. 		(void) adjattrib(typ, thrown_weapon ? -1 : -rn1(3,3), TRUE);
1535. 	} else {
1536. 		i = thrown_weapon ? rnd(6) : rn1(10,6);
1537. 		if(Half_physical_damage) i = (i+1) / 2;
1538. 		losehp(i, pname, KILLED_BY_AN);
1539. 	}
1540. 	if(u.uhp < 1) {
1541. 		killer_format = KILLED_BY_AN;
1542. 		killer = pname;
1543. 		done(POISONING);
1544. 	}
1545. 	(void) encumber_msg();
1546. }
1547. 
1548. /* monster responds to player action; not the same as a passive attack */
1549. /* assumes reason for response has been tested, and response _must_ be made */
1550. void
1551. m_respond(mtmp)
1552. register struct monst *mtmp;
1553. {
1554.     if(mtmp->data->msound == MS_SHRIEK) {
1555. 	if(flags.soundok) {
1556. 	    pline("%s shrieks.", Monnam(mtmp));
1557. 	    stop_occupation();
1558. 	}
1559. 	if (!rn2(10)) {
1560. 	    if (!rn2(13))
1561. 		(void) makemon(&mons[PM_PURPLE_WORM], 0, 0);
1562. 	    else
1563. 		(void) makemon((struct permonst *)0, 0, 0);
1564. 
1565. 	}
1566. 	aggravate();
1567.     }
1568.     if(mtmp->data == &mons[PM_MEDUSA] && !mtmp->mcan) {
1569. 	register int i;
1570. 	for(i = 0; i < NATTK; i++)
1571. 	     if(mtmp->data->mattk[i].aatyp == AT_GAZE) {
1572. 		 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
1573. 		 break;
1574. 	     }
1575.     }
1576. }
1577. 
1578. #endif /* OVLB */
1579. #ifdef OVL2
1580. 
1581. void
1582. setmangry(mtmp)
1583. register struct monst *mtmp;
1584. {
1585. 	mtmp->mstrategy &= ~STRAT_WAITMASK;
1586. 	if(!mtmp->mpeaceful) return;
1587. 	if(mtmp->mtame) return;
1588. 	mtmp->mpeaceful = 0;
1589. 	if(mtmp->ispriest) {
1590. 		if(p_coaligned(mtmp)) adjalign(-5); /* very bad */
1591. 		else adjalign(2);
1592. 	} else
1593. 		adjalign(-1);		/* attacking peaceful monsters is bad */
1594. 	if(humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
1595. 		pline("%s gets angry!", Monnam(mtmp));
1596. 	else if (flags.verbose && flags.soundok) growl(mtmp);
1597. 
1598. 	/* attacking your own quest leader will anger his or her guardians */
1599. 	if (!flags.mon_moving &&	/* should always be the case here */
1600. 		mtmp->data == &mons[quest_info(MS_LEADER)]) {
1601. 	    struct monst *mon;
1602. 	    struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
1603. 	    int got_mad = 0;
1604. 
1605. 	    /* guardians will sense this attack even if they can't see it */
1606. 	    for (mon = fmon; mon; mon = mon->nmon)
1607. 		if (mon->data == q_guardian && mon->mpeaceful) {
1608. 		    mon->mpeaceful = 0;
1609. 		    if (canseemon(mon)) ++got_mad;
1610. 		}
1611. 	    if (got_mad && !Hallucination)
1612. 		pline_The("%s appear%s to be angry too...",
1613. 		      got_mad == 1 ? q_guardian->mname :
1614. 				    makeplural(q_guardian->mname),
1615. 		      got_mad == 1 ? "s" : "");
1616. 	}
1617. }
1618. 
1619. void
1620. wakeup(mtmp)
1621. register struct monst *mtmp;
1622. {
1623. 	mtmp->msleep = 0;
1624. 	mtmp->meating = 0;	/* assume there's no salvagable food left */
1625. 	setmangry(mtmp);
1626. 	if(mtmp->m_ap_type) seemimic(mtmp);
1627. }
1628. 
1629. /* Wake up nearby monsters. */
1630. void
1631. wake_nearby()
1632. {
1633. 	register struct monst *mtmp;
1634. 
1635. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1636. 	    if (distu(mtmp->mx,mtmp->my) < u.ulevel*20) {
1637. 		if(mtmp->msleep)  mtmp->msleep = 0;
1638. 		if(mtmp->mtame)   EDOG(mtmp)->whistletime = moves;
1639. 	    }
1640. 	}
1641. }
1642. 
1643. /* Wake up monsters near some particular location. */
1644. void
1645. wake_nearto(x, y, distance)
1646. register int x, y, distance;
1647. {
1648. 	register struct monst *mtmp;
1649. 
1650. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1651. 	    if (mtmp->msleep && (distance == 0 ||
1652. 				 dist2(mtmp->mx, mtmp->my, x, y) < distance))
1653. 		mtmp->msleep = 0;
1654. 	}
1655. }
1656. 
1657. /* NOTE: we must check for mimicry before calling this routine */
1658. void
1659. seemimic(mtmp)
1660. register struct monst *mtmp;
1661. {
1662. 	/*
1663. 	 *  Discovered mimics don't block light.
1664. 	 */
1665. 	if ((mtmp->m_ap_type == M_AP_FURNITURE &&
1666. 		(mtmp->mappearance==S_hcdoor || mtmp->mappearance==S_vcdoor))||
1667. 	    (mtmp->m_ap_type == M_AP_OBJECT && mtmp->mappearance == BOULDER))
1668. 	    unblock_point(mtmp->mx,mtmp->my);
1669. 
1670. 	mtmp->m_ap_type = M_AP_NOTHING;
1671. 	mtmp->mappearance = 0;
1672. 	newsym(mtmp->mx,mtmp->my);
1673. }
1674. 
1675. /* force all chameleons to become normal */
1676. void
1677. rescham()
1678. {
1679. 	register struct monst *mtmp;
1680. 
1681. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1682. 		if(mtmp->cham) {
1683. 			mtmp->cham = 0;
1684. 			(void) newcham(mtmp, &mons[PM_CHAMELEON]);
1685. 		}
1686. 		if(is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
1687. 			new_were(mtmp);
1688. 		if(mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
1689. 			seemimic(mtmp);
1690. 			/* we pretend that the mimic doesn't */
1691. 			/* know that it has been unmasked.   */
1692. 			mtmp->msleep = 1;
1693. 		}
1694. 	}
1695. }
1696. 
1697. /* Let the chameleons change again -dgk */
1698. void
1699. restartcham()
1700. {
1701. 	register struct monst *mtmp;
1702. 
1703. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1704. 		if (mtmp->data == &mons[PM_CHAMELEON])
1705. 			mtmp->cham = 1;
1706. 		if(mtmp->data->mlet == S_MIMIC && mtmp->msleep &&
1707. 				cansee(mtmp->mx, mtmp->my)) {
1708. 			set_mimic_sym(mtmp);
1709. 			newsym(mtmp->mx,mtmp->my);
1710. 		}
1711. 	}
1712. }
1713. 
1714. /* unwatched hiders may hide again; if so, a 1 is returned.  */
1715. STATIC_OVL boolean
1716. restrap(mtmp)
1717. register struct monst *mtmp;
1718. {
1719. 	if(mtmp->cham || mtmp->mcan || mtmp->m_ap_type ||
1720. 	   cansee(mtmp->mx, mtmp->my) || rn2(3) || (mtmp == u.ustuck))
1721. 		return(FALSE);
1722. 
1723. 	if(mtmp->data->mlet == S_MIMIC) {
1724. 		set_mimic_sym(mtmp);
1725. 		return(TRUE);
1726. 	} else
1727. 	    if(levl[mtmp->mx][mtmp->my].typ == ROOM)  {
1728. 		mtmp->mundetected = 1;
1729. 		return(TRUE);
1730. 	    }
1731. 
1732. 	return(FALSE);
1733. }
1734. 
1735. /* make a chameleon look like a new monster; returns 1 if it actually changed */
1736. int
1737. newcham(mtmp, mdat)
1738. register struct monst *mtmp;
1739. register struct permonst *mdat;
1740. {
1741. 	register int mhp, hpn, hpd;
1742. 	int mndx, tryct;
1743. 	struct permonst *olddata = mtmp->data;
1744. 
1745. 	/* mdat = 0 -> caller wants a random monster shape */
1746. 	tryct = 0;
1747. 	if(mdat == 0) {
1748. 		while (++tryct < 100) {
1749. 			mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
1750. 			mdat = &mons[mndx];
1751. 			/* polyok rules out all M2_PNAME and M2_WERE's */
1752. 			if (!is_human(mdat) && polyok(mdat)
1753. 					&& !is_placeholder(mdat)
1754. 					&& !(mvitals[mndx].mvflags & G_GENOD))
1755. 				break;
1756. 		}
1757. 		if (tryct >= 100) return(0); /* Should never happen */
1758. 	}
1759. 
1760. 	if(is_male(mdat)) {
1761. 		if(mtmp->female) mtmp->female = FALSE;
1762. 	} else if (is_female(mdat)) {
1763. 		if(!mtmp->female) mtmp->female = TRUE;
1764. 	} else if (!is_neuter(mdat)) {
1765. 		if(!rn2(10)) mtmp->female = !mtmp->female;
1766. 	}
1767. 
1768. 	if (In_endgame(&u.uz) && is_mplayer(olddata)) {
1769. 		/* mplayers start out as "Foo the Bar", but some of the
1770. 		 * titles are inappropriate when polymorphed, particularly
1771. 		 * into the opposite sex.  players don't use ranks when
1772. 		 * polymorphed, so dropping the rank for mplayers seems
1773. 		 * reasonable.
1774. 		 */
1775. 		char *p = index(NAME(mtmp), ' ');
1776. 		if (p) {
1777. 			*p = '\0';
1778. 			mtmp->mnamelth = p - NAME(mtmp) + 1;
1779. 		}
1780. 	}
1781. 
1782. 	if(mdat == mtmp->data) return(0);	/* still the same monster */
1783. 
1784. 	if(mtmp->wormno) {			/* throw tail away */
1785. 		wormgone(mtmp);
1786. 		place_monster(mtmp, mtmp->mx, mtmp->my);
1787. 	}
1788. 
1789. 	hpn = mtmp->mhp;
1790. 	hpd = (mtmp->m_lev < 50) ? ((int)mtmp->m_lev)*8 : mdat->mlevel;
1791. 	if(!hpd) hpd = 4;
1792. 
1793. 	mtmp->m_lev = adj_lev(mdat);		/* new monster level */
1794. 
1795. 	mhp = (mtmp->m_lev < 50) ? ((int)mtmp->m_lev)*8 : mdat->mlevel;
1796. 	if(!mhp) mhp = 4;
1797. 
1798. 	/* new hp: same fraction of max as before */
1799. #ifndef LINT
1800. 	mtmp->mhp = (int)(((long)hpn*(long)mhp)/(long)hpd);
1801. #endif
1802. 	if(mtmp->mhp < 0) mtmp->mhp = hpn;	/* overflow */
1803. /* Unlikely but not impossible; a 1HD creature with 1HP that changes into a
1804.    0HD creature will require this statement */
1805. 	if (!mtmp->mhp) mtmp->mhp = 1;
1806. 
1807. /* and the same for maximum hit points */
1808. 	hpn = mtmp->mhpmax;
1809. #ifndef LINT
1810. 	mtmp->mhpmax = (int)(((long)hpn*(long)mhp)/(long)hpd);
1811. #endif
1812. 	if(mtmp->mhpmax < 0) mtmp->mhpmax = hpn;	/* overflow */
1813. 	if (!mtmp->mhpmax) mtmp->mhpmax = 1;
1814. 
1815. 	/* take on the new form... */
1816. 	set_mon_data(mtmp, mdat, 0);
1817. 
1818. 	if (emits_light(olddata) != emits_light(mtmp->data)) {
1819. 	    /* used to give light, now doesn't, or vice versa,
1820. 	       or light's range has changed */
1821. 	    if (emits_light(olddata))
1822. 		del_light_source(LS_MONSTER, (genericptr_t)mtmp);
1823. 	    if (emits_light(mtmp->data))
1824. 		new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
1825. 				 LS_MONSTER, (genericptr_t)mtmp);
1826. 	}
1827. 	mtmp->perminvis = pm_invisible(mdat);
1828. 	mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
1829. 	if (!(hides_under(mdat) && OBJ_AT(mtmp->mx, mtmp->my)) &&
1830. 			!(mdat->mlet == S_EEL && is_pool(mtmp->mx, mtmp->my)))
1831. 		mtmp->mundetected = 0;
1832. 	if (u.ustuck == mtmp) {
1833. 		if(u.uswallow) {
1834. 			if(!attacktype(mdat,AT_ENGL)) {
1835. 				/* Does mdat care? */
1836. 				if (!noncorporeal(mdat) && !amorphous(mdat) &&
1837. 				    !is_whirly(mdat) &&
1838. 				    (mdat != &mons[PM_YELLOW_LIGHT])) {
1839. 					You("break out of %s%s!", mon_nam(mtmp),
1840. 					    (is_animal(mdat)?
1841. 					    "'s stomach" : ""));
1842. 					mtmp->mhp = 1;  /* almost dead */
1843. 				}
1844. 				expels(mtmp, olddata, FALSE);
1845. 			}
1846. 		} else if (!sticks(mdat) && !sticks(uasmon))
1847. 			unstuck(mtmp);
1848. 	}
1849. 
1850. #ifndef DCC30_BUG
1851. 	if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
1852. #else
1853. 	/* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1854. 	 * same expression.
1855. 	 */
1856. 	if (mdat == &mons[PM_LONG_WORM] &&
1857. 		(mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
1858. #endif
1859. 	    /* we can now create worms with tails - 11/91 */
1860. 	    initworm(mtmp, rn2(5));
1861. 	    if (count_wsegs(mtmp))
1862. 		place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
1863. 	}
1864. 
1865. 	newsym(mtmp->mx,mtmp->my);
1866. 
1867. 	mon_break_armor(mtmp);
1868. 	if (!(mtmp->misc_worn_check & W_ARMG))
1869. 	    mselftouch(mtmp, "No longer petrify-resistant, ",
1870. 			!flags.mon_moving);
1871. 	possibly_unwield(mtmp);
1872. 	m_dowear(mtmp, FALSE);
1873. 
1874. 	/* This ought to re-test can_carry() on each item in the inventory
1875. 	 * rather than just checking ex-giants & boulders, but that'd be
1876. 	 * pretty expensive to perform.  If implemented, then perhaps
1877. 	 * minvent should be sorted in order to drop heaviest items first.
1878. 	 */
1879. 	/* former giants can't continue carrying boulders */
1880. 	if (mtmp->minvent && !throws_rocks(mdat)) {
1881. 	    register struct obj *otmp, *otmp2;
1882. 
1883. 	    for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
1884. 		otmp2 = otmp->nobj;
1885. 		if (otmp->otyp == BOULDER) {
1886. 		    obj_extract_self(otmp);
1887. 		    /* probably ought to give some "drop" message here */
1888. 		    if (flooreffects(otmp, mtmp->mx, mtmp->my, "")) continue;
1889. 		    place_object(otmp, mtmp->mx, mtmp->my);
1890. 		}
1891. 	    }
1892. 	}
1893. 
1894. 	return(1);
1895. }
1896. 
1897. /* sometimes an egg will be special */
1898. #define BREEDER_EGG (!rn2(77))
1899. 
1900. /*
1901.  * Determine if the given monster number can be hatched from an egg.
1902.  * Return the monster number to use as the egg's corpsenm.  Return
1903.  * NON_PM if the given monster can't be hatched.
1904.  */
1905. int
1906. can_be_hatched(mnum)
1907. int mnum;
1908. {
1909.     mnum = little_to_big(mnum);
1910.     /*
1911.      * Queen bees lay killer bee eggs (usually), but killer bees don't
1912.      * grow into queen bees.  Ditto for [winged-]gargoyles.
1913.      */
1914.     if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE ||
1915. 	    (lays_eggs(&mons[mnum]) && (BREEDER_EGG ||
1916. 		(mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
1917. 	return mnum;
1918.     return NON_PM;
1919. }
1920. 
1921. /* type of egg laid by #sit; usually matches parent */
1922. int
1923. egg_type_from_parent(mnum, force_ordinary)
1924. int mnum;	/* parent monster; caller must handle lays_eggs() check */
1925. boolean force_ordinary;
1926. {
1927.     if (force_ordinary || !BREEDER_EGG) {
1928. 	if (mnum == PM_QUEEN_BEE) mnum = PM_KILLER_BEE;
1929. 	else if (mnum == PM_WINGED_GARGOYLE) mnum = PM_GARGOYLE;
1930.     }
1931.     return mnum;
1932. }
1933. 
1934. /* decide whether an egg of the indicated monster type is viable; */
1935. /* also used to determine whether an egg or tin can be created... */
1936. boolean
1937. dead_species(m_idx, egg)
1938. int m_idx;
1939. boolean egg;
1940. {
1941. 	/*
1942. 	 * For monsters with both baby and adult forms, genociding either
1943. 	 * form kills all eggs of that monster.  Monsters with more than
1944. 	 * two forms (small->large->giant mimics) are more or less ignored;
1945. 	 * fortunately, none of them have eggs.  Species extinction due to
1946. 	 * overpopulation does not kill eggs.
1947. 	 */
1948. 	return (boolean)
1949. 		(m_idx >= LOW_PM &&
1950. 		 ((mvitals[m_idx].mvflags & G_GENOD) != 0 ||
1951. 		  (egg &&
1952. 		   (mvitals[big_to_little(m_idx)].mvflags & G_GENOD) != 0)));
1953. }
1954. 
1955. /* kill off any eggs of genocided monsters */
1956. static void
1957. kill_eggs(obj_list)
1958. struct obj *obj_list;
1959. {
1960. 	struct obj *otmp;
1961. 
1962. 	for (otmp = obj_list; otmp; otmp = otmp->nobj)
1963. 	    if (otmp->otyp == EGG) {
1964. 		if (dead_species(otmp->corpsenm, TRUE)) {
1965. 		    /*
1966. 		     * It seems we could also just catch this when
1967. 		     * it attempted to hatch, so we wouldn't have to
1968. 		     * search all of the objlists.. or stop all
1969. 		     * hatch timers based on a corpsenm.
1970. 		     */
1971. 		    kill_egg(otmp);
1972. 		}
1973. #if 0	/* not used */
1974. 	    } else if (otmp->otyp == TIN) {
1975. 		if (dead_species(otmp->corpsenm, FALSE))
1976. 		    otmp->corpsenm = NON_PM;	/* empty tin */
1977. 	    } else if (otmp->otyp == CORPSE) {
1978. 		if (dead_species(otmp->corpsenm, FALSE))
1979. 		    ;		/* not yet implemented... */
1980. #endif
1981. 	    } else if (Has_contents(otmp)) {
1982. 		kill_eggs(otmp->cobj);
1983. 	    }
1984. }
1985. 
1986. /* kill all members of genocided species */
1987. void
1988. kill_genocided_monsters()
1989. {
1990. 	struct monst *mtmp, *mtmp2;
1991. 	boolean kill_chameleons;
1992. 	int mndx;
1993. 
1994. 	kill_chameleons = (mvitals[PM_CHAMELEON].mvflags & G_GENOD) != 0;
1995. 	/*
1996. 	 * Called during genocide, and again upon level change.  The latter
1997. 	 * catches up with any migrating monsters as they finally arrive at
1998. 	 * their intended destinations, so possessions get deposited there.
1999. 	 *
2000. 	 * Chameleon handling:
2001. 	 *	1) if chameleons have been genocided, destroy them
2002. 	 *	   regardless of current form;
2003. 	 *	2) otherwise, force every chameleon which is imitating
2004. 	 *	   any genocided species to take on a new form.
2005. 	 */
2006. 	for (mtmp = fmon; mtmp; mtmp = mtmp2) {
2007. 	    mtmp2 = mtmp->nmon;
2008. 	    mndx = monsndx(mtmp->data);
2009. 	    if ((mvitals[mndx].mvflags & G_GENOD) ||
2010. 		    (mtmp->cham && kill_chameleons)) {
2011. 		if (mtmp->cham && !kill_chameleons)
2012. 		    (void) newcham(mtmp, (struct permonst *)0);
2013. 		else
2014. 		    mondead(mtmp);
2015. 	    }
2016. 	    if (mtmp->minvent) kill_eggs(mtmp->minvent);
2017. 	}
2018. 
2019. 	kill_eggs(invent);
2020. 	kill_eggs(fobj);
2021. 	kill_eggs(level.buriedobjlist);
2022. }
2023. 
2024. #endif /* OVL2 */
2025. #ifdef OVLB
2026. 
2027. void
2028. golemeffects(mon, damtype, dam)
2029. register struct monst *mon;
2030. int damtype, dam;
2031. {
2032. 	int heal=0, slow=0;
2033. 
2034. 	if (mon->data != &mons[PM_FLESH_GOLEM]
2035. 					&& mon->data != &mons[PM_IRON_GOLEM])
2036. 		return;
2037. 
2038. 	if (mon->data == &mons[PM_FLESH_GOLEM]) {
2039. 		if (damtype == AD_ELEC) heal = dam / 6;
2040. 		else if (damtype == AD_FIRE || damtype == AD_COLD) slow = 1;
2041. 	} else {
2042. 		if (damtype == AD_ELEC) slow = 1;
2043. 		else if (damtype == AD_FIRE) heal = dam;
2044. 	}
2045. 	if (slow) {
2046. 		if (mon->mspeed != MSLOW) {
2047. 			if (mon->mspeed == MFAST) mon->mspeed = 0;
2048. 			else mon->mspeed = MSLOW;
2049. 			if (cansee(mon->mx, mon->my))
2050. 				pline("%s seems to be moving slower.",
2051. 					Monnam(mon));
2052. 		}
2053. 	}
2054. 	if (heal) {
2055. 		if (mon->mhp < mon->mhpmax) {
2056. 			mon->mhp += dam;
2057. 			if (mon->mhp > mon->mhpmax) mon->mhp = mon->mhpmax;
2058. 			if (cansee(mon->mx, mon->my))
2059. 				pline("%s seems healthier.", Monnam(mon));
2060. 		}
2061. 	}
2062. }
2063. 
2064. boolean
2065. angry_guards(silent)
2066. register boolean silent;
2067. {
2068. 	register struct monst *mtmp;
2069. 	register int ct = 0, nct = 0, sct = 0, slct = 0;
2070. 
2071. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2072. 		if((mtmp->data == &mons[PM_WATCHMAN] ||
2073. 			       mtmp->data == &mons[PM_WATCH_CAPTAIN])
2074. 					&& mtmp->mpeaceful) {
2075. 			ct++;
2076. 			if(cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
2077. 				if (distu(mtmp->mx, mtmp->my) == 2) nct++;
2078. 				else sct++;
2079. 			}
2080. 			if(mtmp->msleep || mtmp->mfrozen) {
2081. 				slct++;
2082. 				mtmp->msleep = mtmp->mfrozen = 0;
2083. 			}
2084. 			mtmp->mpeaceful = 0;
2085. 		}
2086. 	}
2087. 	if(ct) {
2088. 	    if(!silent) { /* do we want pline msgs? */
2089. 		if(slct) pline_The("guard%s wake%s up!",
2090. 				 slct > 1 ? "s" : "", slct == 1 ? "s" : "");
2091. 		if(nct || sct) {
2092. 			if(nct) pline_The("guard%s get%s angry!",
2093. 				nct == 1 ? "" : "s", nct == 1 ? "s" : "");
2094. 			else if(!Blind)
2095. 				You("see %sangry guard%s approaching!",
2096. 				  sct == 1 ? "an " : "", sct > 1 ? "s" : "");
2097. 		} else if(flags.soundok)
2098. 			You_hear("the shrill sound of a guard's whistle.");
2099. 	    }
2100. 	    return(TRUE);
2101. 	}
2102. 	return(FALSE);
2103. }
2104. 
2105. void
2106. pacify_guards()
2107. {
2108. 	register struct monst *mtmp;
2109. 
2110. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2111. 	    if (mtmp->data == &mons[PM_WATCHMAN] ||
2112. 		mtmp->data == &mons[PM_WATCH_CAPTAIN])
2113. 	    mtmp->mpeaceful = 1;
2114. 	}
2115. }
2116. #endif /* OVLB */
2117. 
2118. /*mon.c*/
Advertisement