OSDN Git Service

ba40f1938c840bf509c11b867f36a291d54aa206
[jnethack/source.git] / src / mon.c
1 /* NetHack 3.6  mon.c   $NHDT-Date: 1449269918 2015/12/04 22:58:38 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.199 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 /* JNetHack Copyright */
6 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000  */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016            */
8 /* JNetHack may be freely redistributed.  See license for details. */
9
10 /* If you're using precompiled headers, you don't want this either */
11 #ifdef MICROPORT_BUG
12 #define MKROOM_H
13 #endif
14
15 #include "hack.h"
16 #include "mfndpos.h"
17 #include <ctype.h>
18
19 STATIC_VAR boolean vamp_rise_msg;
20
21 STATIC_DCL void FDECL(sanity_check_single_mon, (struct monst *, const char *));
22 STATIC_DCL boolean FDECL(restrap, (struct monst *));
23 STATIC_DCL long FDECL(mm_aggression, (struct monst *, struct monst *));
24 STATIC_DCL long FDECL(mm_displacement, (struct monst *, struct monst *));
25 STATIC_DCL int NDECL(pick_animal);
26 STATIC_DCL void FDECL(kill_eggs, (struct obj *));
27 STATIC_DCL int FDECL(pickvampshape, (struct monst *));
28 STATIC_DCL boolean FDECL(isspecmon, (struct monst *));
29 STATIC_DCL boolean FDECL(validspecmon, (struct monst *, int));
30 STATIC_DCL boolean FDECL(validvamp, (struct monst *, int *, int));
31 STATIC_DCL struct permonst *FDECL(accept_newcham_form, (int));
32 STATIC_DCL struct obj *FDECL(make_corpse, (struct monst *, unsigned));
33 STATIC_DCL void FDECL(m_detach, (struct monst *, struct permonst *));
34 STATIC_DCL void FDECL(lifesaved_monster, (struct monst *));
35
36 #define LEVEL_SPECIFIC_NOCORPSE(mdat) \
37     (Is_rogue_level(&u.uz)            \
38      || (level.flags.graveyard && is_undead(mdat) && rn2(3)))
39
40 #if 0
41 /* part of the original warning code which was replaced in 3.3.1 */
42 const char *warnings[] = {
43 /*JP
44     "white", "pink", "red", "ruby", "purple", "black"
45 */
46     "\94\92\82¢", "\83s\83\93\83N\90F\82Ì", "\90Ô\82¢", "\83\8b\83r\81[\90F\82Ì", "\8e\87\82Ì", "\8d\95\82¢"
47 };
48 #endif /* 0 */
49
50
51 void
52 sanity_check_single_mon(mtmp, msg)
53 struct monst *mtmp;
54 const char *msg;
55 {
56     if (DEADMONSTER(mtmp))
57         return;
58     if (mtmp->data < &mons[LOW_PM] || mtmp->data >= &mons[NUMMONS])
59         impossible("illegal mon data (%s)", msg);
60 }
61
62 void
63 mon_sanity_check()
64 {
65     int x,y;
66     struct monst *mtmp = fmon;
67
68     while (mtmp) {
69         sanity_check_single_mon(mtmp, "fmon");
70         mtmp = mtmp->nmon;
71     }
72     for (x = 0; x < COLNO; x++)
73         for (y = 0; y < ROWNO; y++)
74             if ((mtmp = m_at(x,y)) != 0)
75                 sanity_check_single_mon(mtmp, "m_at");
76
77     mtmp = migrating_mons;
78     while (mtmp) {
79         sanity_check_single_mon(mtmp, "migr");
80         mtmp = mtmp->nmon;
81     }
82 }
83
84
85 /* convert the monster index of an undead to its living counterpart */
86 int
87 undead_to_corpse(mndx)
88 int mndx;
89 {
90     switch (mndx) {
91     case PM_KOBOLD_ZOMBIE:
92     case PM_KOBOLD_MUMMY:
93         mndx = PM_KOBOLD;
94         break;
95     case PM_DWARF_ZOMBIE:
96     case PM_DWARF_MUMMY:
97         mndx = PM_DWARF;
98         break;
99     case PM_GNOME_ZOMBIE:
100     case PM_GNOME_MUMMY:
101         mndx = PM_GNOME;
102         break;
103     case PM_ORC_ZOMBIE:
104     case PM_ORC_MUMMY:
105         mndx = PM_ORC;
106         break;
107     case PM_ELF_ZOMBIE:
108     case PM_ELF_MUMMY:
109         mndx = PM_ELF;
110         break;
111     case PM_VAMPIRE:
112     case PM_VAMPIRE_LORD:
113 #if 0 /* DEFERRED */
114     case PM_VAMPIRE_MAGE:
115 #endif
116     case PM_HUMAN_ZOMBIE:
117     case PM_HUMAN_MUMMY:
118         mndx = PM_HUMAN;
119         break;
120     case PM_GIANT_ZOMBIE:
121     case PM_GIANT_MUMMY:
122         mndx = PM_GIANT;
123         break;
124     case PM_ETTIN_ZOMBIE:
125     case PM_ETTIN_MUMMY:
126         mndx = PM_ETTIN;
127         break;
128     default:
129         break;
130     }
131     return mndx;
132 }
133
134 /* Convert the monster index of some monsters (such as quest guardians)
135  * to their generic species type.
136  *
137  * Return associated character class monster, rather than species
138  * if mode is 1.
139  */
140 int
141 genus(mndx, mode)
142 int mndx, mode;
143 {
144     switch (mndx) {
145     /* Quest guardians */
146     case PM_STUDENT:
147         mndx = mode ? PM_ARCHEOLOGIST : PM_HUMAN;
148         break;
149     case PM_CHIEFTAIN:
150         mndx = mode ? PM_BARBARIAN : PM_HUMAN;
151         break;
152     case PM_NEANDERTHAL:
153         mndx = mode ? PM_CAVEMAN : PM_HUMAN;
154         break;
155     case PM_ATTENDANT:
156         mndx = mode ? PM_HEALER : PM_HUMAN;
157         break;
158     case PM_PAGE:
159         mndx = mode ? PM_KNIGHT : PM_HUMAN;
160         break;
161     case PM_ABBOT:
162         mndx = mode ? PM_MONK : PM_HUMAN;
163         break;
164     case PM_ACOLYTE:
165         mndx = mode ? PM_PRIEST : PM_HUMAN;
166         break;
167     case PM_HUNTER:
168         mndx = mode ? PM_RANGER : PM_HUMAN;
169         break;
170     case PM_THUG:
171         mndx = mode ? PM_ROGUE : PM_HUMAN;
172         break;
173     case PM_ROSHI:
174         mndx = mode ? PM_SAMURAI : PM_HUMAN;
175         break;
176     case PM_GUIDE:
177         mndx = mode ? PM_TOURIST : PM_HUMAN;
178         break;
179     case PM_APPRENTICE:
180         mndx = mode ? PM_WIZARD : PM_HUMAN;
181         break;
182     case PM_WARRIOR:
183         mndx = mode ? PM_VALKYRIE : PM_HUMAN;
184         break;
185     default:
186         if (mndx >= LOW_PM && mndx < NUMMONS) {
187             struct permonst *ptr = &mons[mndx];
188
189             if (is_human(ptr))
190                 mndx = PM_HUMAN;
191             else if (is_elf(ptr))
192                 mndx = PM_ELF;
193             else if (is_dwarf(ptr))
194                 mndx = PM_DWARF;
195             else if (is_gnome(ptr))
196                 mndx = PM_GNOME;
197             else if (is_orc(ptr))
198                 mndx = PM_ORC;
199         }
200         break;
201     }
202     return mndx;
203 }
204
205 /* return monster index if chameleon, or NON_PM if not */
206 int
207 pm_to_cham(mndx)
208 int mndx;
209 {
210     int mcham = NON_PM;
211
212     /*
213      * As of 3.6.0 we just check M2_SHAPESHIFTER instead of having a
214      * big switch statement with hardcoded shapeshifter types here.
215      */
216     if (mndx >= LOW_PM && is_shapeshifter(&mons[mndx]))
217         mcham = mndx;
218     return mcham;
219 }
220
221 /* for deciding whether corpse will carry along full monster data */
222 #define KEEPTRAITS(mon)                                                 \
223     ((mon)->isshk || (mon)->mtame || unique_corpstat((mon)->data)       \
224      || is_reviver((mon)->data)                                         \
225         /* normally quest leader will be unique, */                     \
226         /* but he or she might have been polymorphed  */                \
227      || (mon)->m_id == quest_status.leader_m_id                         \
228         /* special cancellation handling for these */                   \
229      || (dmgtype((mon)->data, AD_SEDU) || dmgtype((mon)->data, AD_SSEX)))
230
231 /* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
232  * leave corpses.  Monsters which leave "special" corpses should have
233  * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
234  * etc....
235  */
236 STATIC_OVL struct obj *
237 make_corpse(mtmp, corpseflags)
238 register struct monst *mtmp;
239 unsigned corpseflags;
240 {
241     register struct permonst *mdat = mtmp->data;
242     int num;
243     struct obj *obj = (struct obj *) 0;
244     struct obj *otmp = (struct obj *) 0;
245     int x = mtmp->mx, y = mtmp->my;
246     int mndx = monsndx(mdat);
247     unsigned corpstatflags = corpseflags;
248     boolean burythem = ((corpstatflags & CORPSTAT_BURIED) != 0);
249
250     switch (mndx) {
251     case PM_GRAY_DRAGON:
252     case PM_SILVER_DRAGON:
253 #if 0 /* DEFERRED */
254     case PM_SHIMMERING_DRAGON:
255 #endif
256     case PM_RED_DRAGON:
257     case PM_ORANGE_DRAGON:
258     case PM_WHITE_DRAGON:
259     case PM_BLACK_DRAGON:
260     case PM_BLUE_DRAGON:
261     case PM_GREEN_DRAGON:
262     case PM_YELLOW_DRAGON:
263         /* Make dragon scales.  This assumes that the order of the
264            dragons is the same as the order of the scales. */
265         if (!rn2(mtmp->mrevived ? 20 : 3)) {
266             num = GRAY_DRAGON_SCALES + monsndx(mdat) - PM_GRAY_DRAGON;
267             obj = mksobj_at(num, x, y, FALSE, FALSE);
268             obj->spe = 0;
269             obj->cursed = obj->blessed = FALSE;
270         }
271         goto default_1;
272     case PM_WHITE_UNICORN:
273     case PM_GRAY_UNICORN:
274     case PM_BLACK_UNICORN:
275         if (mtmp->mrevived && rn2(2)) {
276             if (canseemon(mtmp))
277 /*JP
278                 pline("%s recently regrown horn crumbles to dust.",
279 */
280                 pline("\8dÅ\8bß\8dÄ\90\82µ\82½%s\82Ì\8ap\82Í\95²\81X\82É\82È\82Á\82½\81D",
281                       s_suffix(Monnam(mtmp)));
282         } else {
283             obj = mksobj_at(UNICORN_HORN, x, y, TRUE, FALSE);
284             if (obj && mtmp->mrevived)
285                 obj->degraded_horn = 1;
286         }
287         goto default_1;
288     case PM_LONG_WORM:
289         (void) mksobj_at(WORM_TOOTH, x, y, TRUE, FALSE);
290         goto default_1;
291     case PM_VAMPIRE:
292     case PM_VAMPIRE_LORD:
293         /* include mtmp in the mkcorpstat() call */
294         num = undead_to_corpse(mndx);
295         corpstatflags |= CORPSTAT_INIT;
296         obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
297         obj->age -= 100; /* this is an *OLD* corpse */
298         break;
299     case PM_KOBOLD_MUMMY:
300     case PM_DWARF_MUMMY:
301     case PM_GNOME_MUMMY:
302     case PM_ORC_MUMMY:
303     case PM_ELF_MUMMY:
304     case PM_HUMAN_MUMMY:
305     case PM_GIANT_MUMMY:
306     case PM_ETTIN_MUMMY:
307     case PM_KOBOLD_ZOMBIE:
308     case PM_DWARF_ZOMBIE:
309     case PM_GNOME_ZOMBIE:
310     case PM_ORC_ZOMBIE:
311     case PM_ELF_ZOMBIE:
312     case PM_HUMAN_ZOMBIE:
313     case PM_GIANT_ZOMBIE:
314     case PM_ETTIN_ZOMBIE:
315         num = undead_to_corpse(mndx);
316         corpstatflags |= CORPSTAT_INIT;
317         obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
318         obj->age -= 100; /* this is an *OLD* corpse */
319         break;
320     case PM_IRON_GOLEM:
321         num = d(2, 6);
322         while (num--)
323             obj = mksobj_at(IRON_CHAIN, x, y, TRUE, FALSE);
324         free_mname(mtmp); /* don't christen obj */
325         break;
326     case PM_GLASS_GOLEM:
327         num = d(2, 4); /* very low chance of creating all glass gems */
328         while (num--)
329             obj = mksobj_at((LAST_GEM + rnd(9)), x, y, TRUE, FALSE);
330         free_mname(mtmp);
331         break;
332     case PM_CLAY_GOLEM:
333         obj = mksobj_at(ROCK, x, y, FALSE, FALSE);
334         obj->quan = (long) (rn2(20) + 50);
335         obj->owt = weight(obj);
336         free_mname(mtmp);
337         break;
338     case PM_STONE_GOLEM:
339         corpstatflags &= ~CORPSTAT_INIT;
340         obj =
341             mkcorpstat(STATUE, (struct monst *) 0, mdat, x, y, corpstatflags);
342         break;
343     case PM_WOOD_GOLEM:
344         num = d(2, 4);
345         while (num--) {
346             obj = mksobj_at(QUARTERSTAFF, x, y, TRUE, FALSE);
347         }
348         free_mname(mtmp);
349         break;
350     case PM_LEATHER_GOLEM:
351         num = d(2, 4);
352         while (num--)
353             obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE, FALSE);
354         free_mname(mtmp);
355         break;
356     case PM_GOLD_GOLEM:
357         /* Good luck gives more coins */
358         obj = mkgold((long) (200 - rnl(101)), x, y);
359         free_mname(mtmp);
360         break;
361     case PM_PAPER_GOLEM:
362         num = rnd(4);
363         while (num--)
364             obj = mksobj_at(SCR_BLANK_PAPER, x, y, TRUE, FALSE);
365         free_mname(mtmp);
366         break;
367     /* expired puddings will congeal into a large blob;
368        like dragons, relies on the order remaining consistent */
369     case PM_GRAY_OOZE:
370     case PM_BROWN_PUDDING:
371     case PM_GREEN_SLIME:
372     case PM_BLACK_PUDDING:
373         /* we have to do this here because most other places
374            expect there to be an object coming back; not this one */
375         obj = mksobj_at(GLOB_OF_BLACK_PUDDING - (PM_BLACK_PUDDING - mndx),
376                         x, y, TRUE, FALSE);
377
378         while (obj && (otmp = obj_nexto(obj)) != (struct obj *) 0) {
379             pudding_merge_message(obj, otmp);
380             obj = obj_meld(&obj, &otmp);
381         }
382         free_mname(mtmp);
383         return obj;
384         break;
385     default_1:
386     default:
387         if (mvitals[mndx].mvflags & G_NOCORPSE) {
388             return (struct obj *) 0;
389         } else {
390             corpstatflags |= CORPSTAT_INIT;
391             /* preserve the unique traits of some creatures */
392             obj = mkcorpstat(CORPSE, KEEPTRAITS(mtmp) ? mtmp : 0,
393                              mdat, x, y, corpstatflags);
394             if (burythem) {
395                 boolean dealloc;
396
397                 (void) bury_an_obj(obj, &dealloc);
398                 newsym(x, y);
399                 return dealloc ? (struct obj *) 0 : obj;
400             }
401         }
402         break;
403     }
404     /* All special cases should precede the G_NOCORPSE check */
405
406     if (!obj) return NULL;
407
408     /* if polymorph or undead turning has killed this monster,
409        prevent the same attack beam from hitting its corpse */
410     if (context.bypasses)
411         bypass_obj(obj);
412
413     if (has_mname(mtmp))
414         obj = oname(obj, MNAME(mtmp));
415
416     /*  Avoid "It was hidden under a green mold corpse!"
417      *  during Blind combat. An unseen monster referred to as "it"
418      *  could be killed and leave a corpse.  If a hider then hid
419      *  underneath it, you could be told the corpse type of a
420      *  monster that you never knew was there without this.
421      *  The code in hitmu() substitutes the word "something"
422      *  if the corpses obj->dknown is 0.
423      */
424     if (Blind && !sensemon(mtmp))
425         obj->dknown = 0;
426
427     stackobj(obj);
428     newsym(x, y);
429     return obj;
430 }
431
432 /* check mtmp and water/lava for compatibility, 0 (survived), 1 (died) */
433 int
434 minliquid(mtmp)
435 register struct monst *mtmp;
436 {
437     boolean inpool, inlava, infountain;
438
439     /* [what about ceiling clingers?] */
440     inpool = (is_pool(mtmp->mx, mtmp->my)
441               && !(is_flyer(mtmp->data) || is_floater(mtmp->data)));
442     inlava = (is_lava(mtmp->mx, mtmp->my)
443               && !(is_flyer(mtmp->data) || is_floater(mtmp->data)));
444     infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
445
446     /* Flying and levitation keeps our steed out of the liquid */
447     /* (but not water-walking or swimming) */
448     if (mtmp == u.usteed && (Flying || Levitation))
449         return 0;
450
451     /* Gremlin multiplying won't go on forever since the hit points
452      * keep going down, and when it gets to 1 hit point the clone
453      * function will fail.
454      */
455     if (mtmp->data == &mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
456         if (split_mon(mtmp, (struct monst *) 0))
457             dryup(mtmp->mx, mtmp->my, FALSE);
458         if (inpool)
459             water_damage_chain(mtmp->minvent, FALSE);
460         return 0;
461     } else if (mtmp->data == &mons[PM_IRON_GOLEM] && inpool && !rn2(5)) {
462         int dam = d(2, 6);
463
464         if (cansee(mtmp->mx, mtmp->my))
465 /*JP
466             pline("%s rusts.", Monnam(mtmp));
467 */
468             pline("%s\82Í\8eK\82Ñ\82½\81D", Monnam(mtmp));
469         mtmp->mhp -= dam;
470         if (mtmp->mhpmax > dam)
471             mtmp->mhpmax -= dam;
472         if (mtmp->mhp < 1) {
473             mondead(mtmp);
474             if (mtmp->mhp < 1)
475                 return 1;
476         }
477         water_damage_chain(mtmp->minvent, FALSE);
478         return 0;
479     }
480
481     if (inlava) {
482         /*
483          * Lava effects much as water effects. Lava likers are able to
484          * protect their stuff. Fire resistant monsters can only protect
485          * themselves  --ALI
486          */
487         if (!is_clinger(mtmp->data) && !likes_lava(mtmp->data)) {
488             if (!resists_fire(mtmp)) {
489                 if (cansee(mtmp->mx, mtmp->my))
490 #if 0 /*JP*/
491                     pline("%s %s.", Monnam(mtmp),
492                           mtmp->data == &mons[PM_WATER_ELEMENTAL]
493                               ? "boils away"
494                               : "burns to a crisp");
495 #else
496                     pline("%s\82Í%s\82½\81D", Monnam(mtmp),
497                           mtmp->data == &mons[PM_WATER_ELEMENTAL]
498                               ? "\95¦\93«\82µ"
499                               : "\94R\82¦\82Ä\83p\83\8a\83p\83\8a\82É\82È\82Á");
500 #endif
501                 mondead(mtmp);
502             } else {
503                 if (--mtmp->mhp < 1) {
504                     if (cansee(mtmp->mx, mtmp->my))
505 /*JP
506                         pline("%s surrenders to the fire.", Monnam(mtmp));
507 */
508                         pline("%s\82Í\89\8a\82É\93Û\82Ü\82ê\82½\81D", Monnam(mtmp));
509                     mondead(mtmp);
510                 } else if (cansee(mtmp->mx, mtmp->my))
511 /*JP
512                     pline("%s burns slightly.", Monnam(mtmp));
513 */
514                     pline("%s\82Í\82¿\82å\82Á\82Æ\8fÅ\82°\82½\81D", Monnam(mtmp));
515             }
516             if (mtmp->mhp > 0) {
517                 (void) fire_damage_chain(mtmp->minvent, FALSE, FALSE,
518                                          mtmp->mx, mtmp->my);
519                 (void) rloc(mtmp, FALSE);
520                 return 0;
521             }
522             return 1;
523         }
524     } else if (inpool) {
525         /* Most monsters drown in pools.  flooreffects() will take care of
526          * water damage to dead monsters' inventory, but survivors need to
527          * be handled here.  Swimmers are able to protect their stuff...
528          */
529         if (!is_clinger(mtmp->data) && !is_swimmer(mtmp->data)
530             && !amphibious(mtmp->data)) {
531             if (cansee(mtmp->mx, mtmp->my)) {
532 /*JP
533                 pline("%s drowns.", Monnam(mtmp));
534 */
535                 pline("%s\82Í\93M\82ê\82½\81D", Monnam(mtmp));
536             }
537             if (u.ustuck && u.uswallow && u.ustuck == mtmp) {
538                 /* This can happen after a purple worm plucks you off a
539                 flying steed while you are over water. */
540 /*JP
541                 pline("%s sinks as water rushes in and flushes you out.",
542 */
543                 pline("%s\82Í\90\85\97¬\82É\92¾\82Ý\81D\82 \82È\82½\82ð\93f\82«\8fo\82µ\82½\81D",
544                       Monnam(mtmp));
545             }
546             mondead(mtmp);
547             if (mtmp->mhp > 0) {
548                 water_damage_chain(mtmp->minvent, FALSE);
549                 (void) rloc(mtmp, FALSE);
550                 return 0;
551             }
552             return 1;
553         }
554     } else {
555         /* but eels have a difficult time outside */
556         if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)) {
557             /* as mhp gets lower, the rate of further loss slows down */
558             if (mtmp->mhp > 1 && rn2(mtmp->mhp) > rn2(8))
559                 mtmp->mhp--;
560             monflee(mtmp, 2, FALSE, FALSE);
561         }
562     }
563     return 0;
564 }
565
566 int
567 mcalcmove(mon)
568 struct monst *mon;
569 {
570     int mmove = mon->data->mmove;
571
572     /* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
573      *       MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
574      *       both adjustments have negligible effect on higher speeds.
575      */
576     if (mon->mspeed == MSLOW)
577         mmove = (2 * mmove + 1) / 3;
578     else if (mon->mspeed == MFAST)
579         mmove = (4 * mmove + 2) / 3;
580
581     if (mon == u.usteed) {
582         if (u.ugallop && context.mv) {
583             /* average movement is 1.50 times normal */
584             mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
585         }
586     } else if (mmove) {
587         /* vary movement points allocated to slightly reduce predictability;
588            random increment (avg +2) exceeds random decrement (avg +1) by
589            a small amount; normal speed monsters will occasionally get an
590            extra move and slow ones won't be quite as slow */
591         mmove += rn2(5) - rn2(3); /* + 0..4 - 0..2, average net +1 */
592         if (mmove < 1)
593             mmove = 1;
594     }
595
596     return mmove;
597 }
598
599 /* actions that happen once per ``turn'', regardless of each
600    individual monster's metabolism; some of these might need to
601    be reclassified to occur more in proportion with movement rate */
602 void
603 mcalcdistress()
604 {
605     struct monst *mtmp;
606
607     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
608         if (DEADMONSTER(mtmp))
609             continue;
610
611         /* must check non-moving monsters once/turn in case
612          * they managed to end up in liquid */
613         if (mtmp->data->mmove == 0) {
614             if (vision_full_recalc)
615                 vision_recalc(0);
616             if (minliquid(mtmp))
617                 continue;
618         }
619
620         /* regenerate hit points */
621         mon_regen(mtmp, FALSE);
622
623         /* possibly polymorph shapechangers and lycanthropes */
624         if (mtmp->cham >= LOW_PM) {
625             if (is_vampshifter(mtmp) || mtmp->data->mlet == S_VAMPIRE)
626                 decide_to_shapeshift(mtmp, 0);
627             else if (!rn2(6))
628                 (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
629         }
630         were_change(mtmp);
631
632         /* gradually time out temporary problems */
633         if (mtmp->mblinded && !--mtmp->mblinded)
634             mtmp->mcansee = 1;
635         if (mtmp->mfrozen && !--mtmp->mfrozen)
636             mtmp->mcanmove = 1;
637         if (mtmp->mfleetim && !--mtmp->mfleetim)
638             mtmp->mflee = 0;
639
640         /* FIXME: mtmp->mlstmv ought to be updated here */
641     }
642 }
643
644 int
645 movemon()
646 {
647     register struct monst *mtmp, *nmtmp;
648     register boolean somebody_can_move = FALSE;
649
650     /*
651      * Some of you may remember the former assertion here that
652      * because of deaths and other actions, a simple one-pass
653      * algorithm wasn't possible for movemon.  Deaths are no longer
654      * removed to the separate list fdmon; they are simply left in
655      * the chain with hit points <= 0, to be cleaned up at the end
656      * of the pass.
657      *
658      * The only other actions which cause monsters to be removed from
659      * the chain are level migrations and losedogs().  I believe losedogs()
660      * is a cleanup routine not associated with monster movements, and
661      * monsters can only affect level migrations on themselves, not others
662      * (hence the fetching of nmon before moving the monster).  Currently,
663      * monsters can jump into traps, read cursed scrolls of teleportation,
664      * and drink cursed potions of raise level to change levels.  These are
665      * all reflexive at this point.  Should one monster be able to level
666      * teleport another, this scheme would have problems.
667      */
668
669     for (mtmp = fmon; mtmp; mtmp = nmtmp) {
670         /* end monster movement early if hero is flagged to leave the level */
671         if (u.utotype
672 #ifdef SAFERHANGUP
673             /* or if the program has lost contact with the user */
674             || program_state.done_hup
675 #endif
676             ) {
677             somebody_can_move = FALSE;
678             break;
679         }
680         nmtmp = mtmp->nmon;
681         /* one dead monster needs to perform a move after death:
682            vault guard whose temporary corridor is still on the map */
683         if (mtmp->isgd && !mtmp->mx && mtmp->mhp <= 0)
684             (void) gd_move(mtmp);
685         if (DEADMONSTER(mtmp))
686             continue;
687
688         /* Find a monster that we have not treated yet. */
689         if (mtmp->movement < NORMAL_SPEED)
690             continue;
691
692         mtmp->movement -= NORMAL_SPEED;
693         if (mtmp->movement >= NORMAL_SPEED)
694             somebody_can_move = TRUE;
695
696         if (vision_full_recalc)
697             vision_recalc(0); /* vision! */
698
699         /* reset obj bypasses before next monster moves */
700         if (context.bypasses)
701             clear_bypasses();
702         clear_splitobjs();
703         if (minliquid(mtmp))
704             continue;
705
706         if (is_hider(mtmp->data)) {
707             /* unwatched mimics and piercers may hide again  [MRS] */
708             if (restrap(mtmp))
709                 continue;
710             if (mtmp->m_ap_type == M_AP_FURNITURE
711                 || mtmp->m_ap_type == M_AP_OBJECT)
712                 continue;
713             if (mtmp->mundetected)
714                 continue;
715         } else if (mtmp->data->mlet == S_EEL && !mtmp->mundetected
716                    && (mtmp->mflee || distu(mtmp->mx, mtmp->my) > 2)
717                    && !canseemon(mtmp) && !rn2(4)) {
718             /* some eels end up stuck in isolated pools, where they
719                can't--or at least won't--move, so they never reach
720                their post-move chance to re-hide */
721             if (hideunder(mtmp))
722                 continue;
723         }
724
725         /* continue if the monster died fighting */
726         if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
727             /* Note:
728              *  Conflict does not take effect in the first round.
729              *  Therefore, A monster when stepping into the area will
730              *  get to swing at you.
731              *
732              *  The call to fightm() must be _last_.  The monster might
733              *  have died if it returns 1.
734              */
735             if (couldsee(mtmp->mx, mtmp->my)
736                 && (distu(mtmp->mx, mtmp->my) <= BOLT_LIM * BOLT_LIM)
737                 && fightm(mtmp))
738                 continue; /* mon might have died */
739         }
740         if (dochugw(mtmp)) /* otherwise just move the monster */
741             continue;
742     }
743
744     if (any_light_source())
745         vision_full_recalc = 1; /* in case a mon moved with a light source */
746     /* reset obj bypasses after last monster has moved */
747     if (context.bypasses)
748         clear_bypasses();
749     clear_splitobjs();
750     /* remove dead monsters; dead vault guard will be left at <0,0>
751        if temporary corridor out of vault hasn't been removed yet */
752     dmonsfree();
753
754     /* a monster may have levteleported player -dlc */
755     if (u.utotype) {
756         deferred_goto();
757         /* changed levels, so these monsters are dormant */
758         somebody_can_move = FALSE;
759     }
760
761     return somebody_can_move;
762 }
763
764 #define mstoning(obj)                                       \
765     (ofood(obj) && (touch_petrifies(&mons[(obj)->corpsenm]) \
766                     || (obj)->corpsenm == PM_MEDUSA))
767
768 /*
769  * Maybe eat a metallic object (not just gold).
770  * Return value: 0 => nothing happened, 1 => monster ate something,
771  * 2 => monster died (it must have grown into a genocided form, but
772  * that can't happen at present because nothing which eats objects
773  * has young and old forms).
774  */
775 int
776 meatmetal(mtmp)
777 register struct monst *mtmp;
778 {
779     register struct obj *otmp;
780     struct permonst *ptr;
781     int poly, grow, heal, mstone;
782
783     /* If a pet, eating is handled separately, in dog.c */
784     if (mtmp->mtame)
785         return 0;
786
787     /* Eats topmost metal object if it is there */
788     for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp;
789          otmp = otmp->nexthere) {
790         /* Don't eat indigestible/choking/inappropriate objects */
791         if ((mtmp->data == &mons[PM_RUST_MONSTER] && !is_rustprone(otmp))
792             || (otmp->otyp == AMULET_OF_STRANGULATION)
793             || (otmp->otyp == RIN_SLOW_DIGESTION))
794             continue;
795         if (is_metallic(otmp) && !obj_resists(otmp, 5, 95)
796             && touch_artifact(otmp, mtmp)) {
797             if (mtmp->data == &mons[PM_RUST_MONSTER] && otmp->oerodeproof) {
798                 if (canseemon(mtmp) && flags.verbose) {
799 #if 0 /*JP*/
800                     pline("%s eats %s!", Monnam(mtmp),
801                           distant_name(otmp, doname));
802 #else
803                     pline("%s\82Í%s\82ð\90H\82×\82Ä\82¢\82é\81I", Monnam(mtmp),
804                           distant_name(otmp,doname));
805 #endif
806                 }
807                 /* The object's rustproofing is gone now */
808                 otmp->oerodeproof = 0;
809                 mtmp->mstun = 1;
810                 if (canseemon(mtmp) && flags.verbose) {
811 #if 0 /*JP*/
812                     pline("%s spits %s out in disgust!", Monnam(mtmp),
813                           distant_name(otmp, doname));
814 #else
815                     pline("%s\82Í%s\82ð\83y\83b\82Æ\93f\82«\8fo\82µ\82½\81I", Monnam(mtmp),
816                           distant_name(otmp,doname));
817 #endif
818                 }
819             } else {
820                 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
821 #if 0 /*JP*/
822                     pline("%s eats %s!", Monnam(mtmp),
823                           distant_name(otmp, doname));
824 #else
825                     pline("%s\82Í%s\82ð\90H\82×\82Ä\82¢\82é\81I", Monnam(mtmp),
826                           distant_name(otmp,doname));
827 #endif
828                 else if (flags.verbose)
829 /*JP
830                     You_hear("a crunching sound.");
831 */
832                     You_hear("\83o\83\8a\83o\83\8a\82Æ\90H\82×\82é\89¹\82ð\95·\82¢\82½\81D");
833                 mtmp->meating = otmp->owt / 2 + 1;
834                 /* Heal up to the object's weight in hp */
835                 if (mtmp->mhp < mtmp->mhpmax) {
836                     mtmp->mhp += objects[otmp->otyp].oc_weight;
837                     if (mtmp->mhp > mtmp->mhpmax)
838                         mtmp->mhp = mtmp->mhpmax;
839                 }
840                 if (otmp == uball) {
841                     unpunish();
842                     delobj(otmp);
843                 } else if (otmp == uchain) {
844                     unpunish(); /* frees uchain */
845                 } else {
846                     poly = polyfodder(otmp);
847                     grow = mlevelgain(otmp);
848                     heal = mhealup(otmp);
849                     mstone = mstoning(otmp);
850                     delobj(otmp);
851                     ptr = mtmp->data;
852                     if (poly) {
853                         if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
854                             ptr = mtmp->data;
855                     } else if (grow) {
856                         ptr = grow_up(mtmp, (struct monst *) 0);
857                     } else if (mstone) {
858                         if (poly_when_stoned(ptr)) {
859                             mon_to_stone(mtmp);
860                             ptr = mtmp->data;
861                         } else if (!resists_ston(mtmp)) {
862                             if (canseemon(mtmp))
863 /*JP
864                                 pline("%s turns to stone!", Monnam(mtmp));
865 */
866                                 pline("%s\82Í\90Î\82É\82È\82Á\82½\81I", Monnam(mtmp));
867                             monstone(mtmp);
868                             ptr = (struct permonst *) 0;
869                         }
870                     } else if (heal) {
871                         mtmp->mhp = mtmp->mhpmax;
872                     }
873                     if (!ptr)
874                         return 2; /* it died */
875                 }
876                 /* Left behind a pile? */
877                 if (rnd(25) < 3)
878                     (void) mksobj_at(ROCK, mtmp->mx, mtmp->my, TRUE, FALSE);
879                 newsym(mtmp->mx, mtmp->my);
880                 return 1;
881             }
882         }
883     }
884     return 0;
885 }
886
887 /* monster eats a pile of objects */
888 int
889 meatobj(mtmp) /* for gelatinous cubes */
890 struct monst *mtmp;
891 {
892     register struct obj *otmp, *otmp2;
893     struct permonst *ptr, *original_ptr = mtmp->data;
894     int poly, grow, heal, count = 0, ecount = 0;
895     char buf[BUFSZ];
896
897     buf[0] = '\0';
898     /* If a pet, eating is handled separately, in dog.c */
899     if (mtmp->mtame)
900         return 0;
901
902     /* eat organic objects, including cloth and wood, if present;
903        engulf others, except huge rocks and metal attached to player
904        [despite comment at top, doesn't assume that eater is a g.cube] */
905     for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
906         otmp2 = otmp->nexthere;
907
908         /* touch sensitive items */
909         if (otmp->otyp == CORPSE && is_rider(&mons[otmp->corpsenm])) {
910             /* Rider corpse isn't just inedible; can't engulf it either */
911             (void) revive_corpse(otmp);
912
913         /* untouchable (or inaccessible) items */
914         } else if ((otmp->otyp == CORPSE
915                     && touch_petrifies(&mons[otmp->corpsenm])
916                     && !resists_ston(mtmp))
917                    /* don't engulf boulders and statues or ball&chain */
918                    || otmp->oclass == ROCK_CLASS
919                    || otmp == uball || otmp == uchain) {
920             /* do nothing--neither eaten nor engulfed */
921             continue;
922
923         /* inedible items -- engulf these */
924         } else if (!is_organic(otmp) || obj_resists(otmp, 5, 95)
925                    || !touch_artifact(otmp, mtmp)
926                    /* redundant due to non-organic composition but
927                       included for emphasis */
928                    || (otmp->otyp == AMULET_OF_STRANGULATION
929                        || otmp->otyp == RIN_SLOW_DIGESTION)
930                    /* cockatrice corpses handled above; this
931                       touch_petrifies() check catches eggs */
932                    || ((otmp->otyp == CORPSE || otmp->otyp == EGG)
933                        && ((touch_petrifies(&mons[otmp->corpsenm])
934                             && !resists_ston(mtmp))
935                            || (otmp->corpsenm == PM_GREEN_SLIME
936                                && !slimeproof(mtmp->data))))) {
937             /* engulf */
938             ++ecount;
939             if (ecount == 1)
940 #if 0 /*JP*/
941                 Sprintf(buf, "%s engulfs %s.", Monnam(mtmp),
942                         distant_name(otmp, doname));
943 #else
944                 Sprintf(buf, "%s\82Í%s\82ð\88ù\82Ý\8d\9e\82ñ\82¾\81D", Monnam(mtmp),
945                         distant_name(otmp,doname));
946 #endif
947             else if (ecount == 2)
948 /*JP
949                 Sprintf(buf, "%s engulfs several objects.", Monnam(mtmp));
950 */
951                 Sprintf(buf, "%s\82Í\82¢\82­\82Â\82©\82Ì\95¨\82ð\88ù\82Ý\8d\9e\82ñ\82¾\81D", Monnam(mtmp));
952             obj_extract_self(otmp);
953             (void) mpickobj(mtmp, otmp); /* slurp */
954
955         /* lastly, edible items; yum! */
956         } else {
957             /* devour */
958             ++count;
959             if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
960 #if 0 /*JP*/
961                 pline("%s eats %s!", Monnam(mtmp),
962                       distant_name(otmp, doname));
963 #else
964                 pline("%s\82Í%s\82ð\90H\82×\82Ä\82¢\82é\81I", Monnam(mtmp),
965                       distant_name(otmp, doname));
966 #endif
967             else if (flags.verbose)
968 /*JP
969                 You_hear("a slurping sound.");
970 */
971                 You_hear("\82²\82­\82ñ\82Æ\88ù\82Ý\8d\9e\82Þ\89¹\82ð\95·\82¢\82½\81D");
972             /* Heal up to the object's weight in hp */
973             if (mtmp->mhp < mtmp->mhpmax) {
974                 mtmp->mhp += objects[otmp->otyp].oc_weight;
975                 if (mtmp->mhp > mtmp->mhpmax)
976                     mtmp->mhp = mtmp->mhpmax;
977             }
978             if (Has_contents(otmp)) {
979                 register struct obj *otmp3;
980
981                 /* contents of eaten containers become engulfed; this
982                    is arbitrary, but otherwise g.cubes are too powerful */
983                 while ((otmp3 = otmp->cobj) != 0) {
984                     obj_extract_self(otmp3);
985                     if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) {
986                         otmp3->age = monstermoves - otmp3->age;
987                         start_corpse_timeout(otmp3);
988                     }
989                     (void) mpickobj(mtmp, otmp3);
990                 }
991             }
992             poly = polyfodder(otmp);
993             grow = mlevelgain(otmp);
994             heal = mhealup(otmp);
995             delobj(otmp); /* munch */
996             ptr = mtmp->data;
997             if (poly) {
998                 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
999                     ptr = mtmp->data;
1000             } else if (grow) {
1001                 ptr = grow_up(mtmp, (struct monst *) 0);
1002             } else if (heal) {
1003                 mtmp->mhp = mtmp->mhpmax;
1004             }
1005             /* in case it polymorphed or died */
1006             if (ptr != original_ptr)
1007                 return !ptr ? 2 : 1;
1008         }
1009
1010         /* Engulf & devour is instant, so don't set meating */
1011         if (mtmp->minvis)
1012             newsym(mtmp->mx, mtmp->my);
1013     }
1014
1015     if (ecount > 0) {
1016         if (cansee(mtmp->mx, mtmp->my) && flags.verbose && buf[0])
1017             pline1(buf);
1018         else if (flags.verbose)
1019 #if 0 /*JP*/
1020             You_hear("%s slurping sound%s.",
1021                      (ecount == 1) ? "a" : "several", plur(ecount));
1022 #else
1023             You_hear("\83Y\83\8b\83Y\83\8b\82Æ\82¢\82¤\89¹\82ð\95·\82¢\82½\81D");
1024 #endif
1025     }
1026     return (count > 0 || ecount > 0) ? 1 : 0;
1027 }
1028
1029 void
1030 mpickgold(mtmp)
1031 register struct monst *mtmp;
1032 {
1033     register struct obj *gold;
1034     int mat_idx;
1035
1036     if ((gold = g_at(mtmp->mx, mtmp->my)) != 0) {
1037         mat_idx = objects[gold->otyp].oc_material;
1038         obj_extract_self(gold);
1039         add_to_minv(mtmp, gold);
1040         if (cansee(mtmp->mx, mtmp->my)) {
1041             if (flags.verbose && !mtmp->isgd)
1042 #if 0 /*JP*/
1043                 pline("%s picks up some %s.", Monnam(mtmp),
1044                       mat_idx == GOLD ? "gold" : "money");
1045 #else
1046                 pline("%s\82Í\82¨\8bà\82ð\8fE\82Á\82½\81D", Monnam(mtmp));
1047 #endif
1048             newsym(mtmp->mx, mtmp->my);
1049         }
1050     }
1051 }
1052
1053 boolean
1054 mpickstuff(mtmp, str)
1055 register struct monst *mtmp;
1056 register const char *str;
1057 {
1058     register struct obj *otmp, *otmp2, *otmp3;
1059     int carryamt = 0;
1060
1061     /* prevent shopkeepers from leaving the door of their shop */
1062     if (mtmp->isshk && inhishop(mtmp))
1063         return FALSE;
1064
1065     for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
1066         otmp2 = otmp->nexthere;
1067         /* Nymphs take everything.  Most monsters don't pick up corpses. */
1068         if (!str ? searches_for_item(mtmp, otmp)
1069                  : !!(index(str, otmp->oclass))) {
1070             if (otmp->otyp == CORPSE && mtmp->data->mlet != S_NYMPH
1071                 /* let a handful of corpse types thru to can_carry() */
1072                 && !touch_petrifies(&mons[otmp->corpsenm])
1073                 && otmp->corpsenm != PM_LIZARD
1074                 && !acidic(&mons[otmp->corpsenm]))
1075                 continue;
1076             if (!touch_artifact(otmp, mtmp))
1077                 continue;
1078             carryamt = can_carry(mtmp, otmp);
1079             if (carryamt == 0)
1080                 continue;
1081             if (is_pool(mtmp->mx, mtmp->my))
1082                 continue;
1083             /* handle cases where the critter can only get some */
1084             otmp3 = otmp;
1085             if (carryamt != otmp->quan) {
1086                 otmp3 = splitobj(otmp, carryamt);
1087             }
1088             if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
1089 #if 0 /*JP*/
1090                 pline("%s picks up %s.", Monnam(mtmp),
1091                       (distu(mtmp->mx, mtmp->my) <= 5)
1092                           ? doname(otmp3)
1093                           : distant_name(otmp3, doname));
1094 #else
1095                 pline("%s\82Í%s\82ð\8fE\82Á\82½\81D", Monnam(mtmp),
1096                       (distu(mtmp->mx, mtmp->my) <= 5)
1097                           ? doname(otmp3)
1098                           : distant_name(otmp3, doname));
1099 #endif
1100             obj_extract_self(otmp3);      /* remove from floor */
1101             (void) mpickobj(mtmp, otmp3); /* may merge and free otmp3 */
1102             m_dowear(mtmp, FALSE);
1103             newsym(mtmp->mx, mtmp->my);
1104             return TRUE; /* pick only one object */
1105         }
1106     }
1107     return FALSE;
1108 }
1109
1110 int
1111 curr_mon_load(mtmp)
1112 struct monst *mtmp;
1113 {
1114     int curload = 0;
1115     struct obj *obj;
1116
1117     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1118         if (obj->otyp != BOULDER || !throws_rocks(mtmp->data))
1119             curload += obj->owt;
1120     }
1121
1122     return curload;
1123 }
1124
1125 int
1126 max_mon_load(mtmp)
1127 struct monst *mtmp;
1128 {
1129     long maxload;
1130
1131     /* Base monster carrying capacity is equal to human maximum
1132      * carrying capacity, or half human maximum if not strong.
1133      * (for a polymorphed player, the value used would be the
1134      * non-polymorphed carrying capacity instead of max/half max).
1135      * This is then modified by the ratio between the monster weights
1136      * and human weights.  Corpseless monsters are given a capacity
1137      * proportional to their size instead of weight.
1138      */
1139     if (!mtmp->data->cwt)
1140         maxload = (MAX_CARR_CAP * (long) mtmp->data->msize) / MZ_HUMAN;
1141     else if (!strongmonst(mtmp->data)
1142              || (strongmonst(mtmp->data) && (mtmp->data->cwt > WT_HUMAN)))
1143         maxload = (MAX_CARR_CAP * (long) mtmp->data->cwt) / WT_HUMAN;
1144     else
1145         maxload = MAX_CARR_CAP; /*strong monsters w/cwt <= WT_HUMAN*/
1146
1147     if (!strongmonst(mtmp->data))
1148         maxload /= 2;
1149
1150     if (maxload < 1)
1151         maxload = 1;
1152
1153     return (int) maxload;
1154 }
1155
1156 /* for restricting monsters' object-pickup.
1157  *
1158  * to support the new pet behavior, this now returns the max # of objects
1159  * that a given monster could pick up from a pile. frequently this will be
1160  * otmp->quan, but special cases for 'only one' now exist so.
1161  *
1162  * this will probably cause very amusing behavior with pets and gold coins.
1163  *
1164  * TODO: allow picking up 2-N objects from a pile of N based on weight.
1165  *       Change from 'int' to 'long' to accomate big stacks of gold.
1166  *       Right now we fake it by reporting a partial quantity, but the
1167  *       likesgold handling m_move results in picking up the whole stack.
1168  */
1169 int
1170 can_carry(mtmp, otmp)
1171 struct monst *mtmp;
1172 struct obj *otmp;
1173 {
1174     int iquan, otyp = otmp->otyp, newload = otmp->owt;
1175     struct permonst *mdat = mtmp->data;
1176     short nattk = 0;
1177
1178     if (notake(mdat))
1179         return 0; /* can't carry anything */
1180
1181     if (otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm])
1182         && !(mtmp->misc_worn_check & W_ARMG) && !resists_ston(mtmp))
1183         return 0;
1184     if (otyp == CORPSE && is_rider(&mons[otmp->corpsenm]))
1185         return 0;
1186     if (objects[otyp].oc_material == SILVER && mon_hates_silver(mtmp)
1187         && (otyp != BELL_OF_OPENING || !is_covetous(mdat)))
1188         return 0;
1189
1190     /* hostile monsters who like gold will pick up the whole stack;
1191        tame mosnters with hands will pick up the partial stack */
1192     iquan = (otmp->quan > (long) LARGEST_INT)
1193                ? 20000 + rn2(LARGEST_INT - 20000 + 1)
1194                : (int) otmp->quan;
1195
1196     /* monsters without hands can't pick up multiple objects at once
1197      * unless they have an engulfing attack
1198      *
1199      * ...dragons, of course, can always carry gold pieces and gems somehow
1200      */
1201     if (iquan > 1) {
1202         boolean glomper = FALSE;
1203
1204         if (mtmp->data->mlet == S_DRAGON
1205             && (otmp->oclass == COIN_CLASS
1206                 || otmp->oclass == GEM_CLASS))
1207             glomper = TRUE;
1208         else
1209             for (nattk = 0; nattk < NATTK; nattk++)
1210                 if (mtmp->data->mattk[nattk].aatyp == AT_ENGL) {
1211                     glomper = TRUE;
1212                     break;
1213                 }
1214         if ((mtmp->data->mflags1 & M1_NOHANDS) && !glomper)
1215             return 1;
1216     }
1217
1218     /* steeds don't pick up stuff (to avoid shop abuse) */
1219     if (mtmp == u.usteed)
1220         return 0;
1221     if (mtmp->isshk)
1222         return iquan; /* no limit */
1223     if (mtmp->mpeaceful && !mtmp->mtame)
1224         return 0;
1225     /* otherwise players might find themselves obligated to violate
1226      * their alignment if the monster takes something they need
1227      */
1228
1229     /* special--boulder throwers carry unlimited amounts of boulders */
1230     if (throws_rocks(mdat) && otyp == BOULDER)
1231         return iquan;
1232
1233     /* nymphs deal in stolen merchandise, but not boulders or statues */
1234     if (mdat->mlet == S_NYMPH)
1235         return (otmp->oclass == ROCK_CLASS) ? 0 : iquan;
1236
1237     if (curr_mon_load(mtmp) + newload > max_mon_load(mtmp))
1238         return 0;
1239
1240     return iquan;
1241 }
1242
1243 /* return number of acceptable neighbour positions */
1244 int
1245 mfndpos(mon, poss, info, flag)
1246 struct monst *mon;
1247 coord *poss; /* coord poss[9] */
1248 long *info;  /* long info[9] */
1249 long flag;
1250 {
1251     struct permonst *mdat = mon->data;
1252     register struct trap *ttmp;
1253     xchar x, y, nx, ny;
1254     int cnt = 0;
1255     uchar ntyp;
1256     uchar nowtyp;
1257     boolean wantpool, poolok, lavaok, nodiag;
1258     boolean rockok = FALSE, treeok = FALSE, thrudoor;
1259     int maxx, maxy;
1260
1261     x = mon->mx;
1262     y = mon->my;
1263     nowtyp = levl[x][y].typ;
1264
1265     nodiag = NODIAG(mdat - mons);
1266     wantpool = mdat->mlet == S_EEL;
1267     poolok = (is_flyer(mdat) || is_clinger(mdat)
1268               || (is_swimmer(mdat) && !wantpool));
1269     lavaok = (is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat));
1270     thrudoor = ((flag & (ALLOW_WALL | BUSTDOOR)) != 0L);
1271     if (flag & ALLOW_DIG) {
1272         struct obj *mw_tmp;
1273
1274         /* need to be specific about what can currently be dug */
1275         if (!needspick(mdat)) {
1276             rockok = treeok = TRUE;
1277         } else if ((mw_tmp = MON_WEP(mon)) && mw_tmp->cursed
1278                    && mon->weapon_check == NO_WEAPON_WANTED) {
1279             rockok = is_pick(mw_tmp);
1280             treeok = is_axe(mw_tmp);
1281         } else {
1282             rockok = (m_carrying(mon, PICK_AXE)
1283                       || (m_carrying(mon, DWARVISH_MATTOCK)
1284                           && !which_armor(mon, W_ARMS)));
1285             treeok = (m_carrying(mon, AXE) || (m_carrying(mon, BATTLE_AXE)
1286                                                && !which_armor(mon, W_ARMS)));
1287         }
1288         if (rockok || treeok)
1289             thrudoor = TRUE;
1290     }
1291
1292 nexttry: /* eels prefer the water, but if there is no water nearby,
1293             they will crawl over land */
1294     if (mon->mconf) {
1295         flag |= ALLOW_ALL;
1296         flag &= ~NOTONL;
1297     }
1298     if (!mon->mcansee)
1299         flag |= ALLOW_SSM;
1300     maxx = min(x + 1, COLNO - 1);
1301     maxy = min(y + 1, ROWNO - 1);
1302     for (nx = max(1, x - 1); nx <= maxx; nx++)
1303         for (ny = max(0, y - 1); ny <= maxy; ny++) {
1304             if (nx == x && ny == y)
1305                 continue;
1306             ntyp = levl[nx][ny].typ;
1307             if (IS_ROCK(ntyp)
1308                 && !((flag & ALLOW_WALL) && may_passwall(nx, ny))
1309                 && !((IS_TREE(ntyp) ? treeok : rockok) && may_dig(nx, ny)))
1310                 continue;
1311             /* KMH -- Added iron bars */
1312             if (ntyp == IRONBARS && !(flag & ALLOW_BARS))
1313                 continue;
1314             if (IS_DOOR(ntyp) && !(amorphous(mdat) || can_fog(mon))
1315                 && (((levl[nx][ny].doormask & D_CLOSED) && !(flag & OPENDOOR))
1316                     || ((levl[nx][ny].doormask & D_LOCKED)
1317                         && !(flag & UNLOCKDOOR))) && !thrudoor)
1318                 continue;
1319             /* first diagonal checks (tight squeezes handled below) */
1320             if (nx != x && ny != y
1321                 && (nodiag
1322                     || (IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN))
1323                     || (IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN))
1324                     || ((IS_DOOR(nowtyp) || IS_DOOR(ntyp))
1325                         && Is_rogue_level(&u.uz))
1326                     /* mustn't pass between adjacent long worm segments,
1327                        but can attack that way */
1328                     || (m_at(x, ny) && m_at(nx, y) && worm_cross(x, y, nx, ny)
1329                         && !m_at(nx, ny) && (nx != u.ux || ny != u.uy))))
1330                 continue;
1331             if ((is_pool(nx, ny) == wantpool || poolok)
1332                 && (lavaok || !is_lava(nx, ny))) {
1333                 int dispx, dispy;
1334                 boolean monseeu = (mon->mcansee
1335                                    && (!Invis || perceives(mdat)));
1336                 boolean checkobj = OBJ_AT(nx, ny);
1337
1338                 /* Displacement also displaces the Elbereth/scare monster,
1339                  * as long as you are visible.
1340                  */
1341                 if (Displaced && monseeu && mon->mux == nx && mon->muy == ny) {
1342                     dispx = u.ux;
1343                     dispy = u.uy;
1344                 } else {
1345                     dispx = nx;
1346                     dispy = ny;
1347                 }
1348
1349                 info[cnt] = 0;
1350                 if (onscary(dispx, dispy, mon)) {
1351                     if (!(flag & ALLOW_SSM))
1352                         continue;
1353                     info[cnt] |= ALLOW_SSM;
1354                 }
1355                 if ((nx == u.ux && ny == u.uy)
1356                     || (nx == mon->mux && ny == mon->muy)) {
1357                     if (nx == u.ux && ny == u.uy) {
1358                         /* If it's right next to you, it found you,
1359                          * displaced or no.  We must set mux and muy
1360                          * right now, so when we return we can tell
1361                          * that the ALLOW_U means to attack _you_ and
1362                          * not the image.
1363                          */
1364                         mon->mux = u.ux;
1365                         mon->muy = u.uy;
1366                     }
1367                     if (!(flag & ALLOW_U))
1368                         continue;
1369                     info[cnt] |= ALLOW_U;
1370                 } else {
1371                     if (MON_AT(nx, ny)) {
1372                         struct monst *mtmp2 = m_at(nx, ny);
1373                         long mmflag = flag | mm_aggression(mon, mtmp2);
1374
1375                         if (mmflag & ALLOW_M) {
1376                             info[cnt] |= ALLOW_M;
1377                             if (mtmp2->mtame) {
1378                                 if (!(mmflag & ALLOW_TM))
1379                                     continue;
1380                                 info[cnt] |= ALLOW_TM;
1381                             }
1382                         } else {
1383                             mmflag = flag | mm_displacement(mon, mtmp2);
1384                             if (!(mmflag & ALLOW_MDISP))
1385                                 continue;
1386                             info[cnt] |= ALLOW_MDISP;
1387                         }
1388                     }
1389                     /* Note: ALLOW_SANCT only prevents movement, not
1390                        attack, into a temple. */
1391                     if (level.flags.has_temple && *in_rooms(nx, ny, TEMPLE)
1392                         && !*in_rooms(x, y, TEMPLE)
1393                         && in_your_sanctuary((struct monst *) 0, nx, ny)) {
1394                         if (!(flag & ALLOW_SANCT))
1395                             continue;
1396                         info[cnt] |= ALLOW_SANCT;
1397                     }
1398                 }
1399                 if (checkobj && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
1400                     if (flag & NOGARLIC)
1401                         continue;
1402                     info[cnt] |= NOGARLIC;
1403                 }
1404                 if (checkobj && sobj_at(BOULDER, nx, ny)) {
1405                     if (!(flag & ALLOW_ROCK))
1406                         continue;
1407                     info[cnt] |= ALLOW_ROCK;
1408                 }
1409                 if (monseeu && onlineu(nx, ny)) {
1410                     if (flag & NOTONL)
1411                         continue;
1412                     info[cnt] |= NOTONL;
1413                 }
1414                 /* check for diagonal tight squeeze */
1415                 if (nx != x && ny != y && bad_rock(mdat, x, ny)
1416                     && bad_rock(mdat, nx, y) && cant_squeeze_thru(mon))
1417                     continue;
1418                 /* The monster avoids a particular type of trap if it's
1419                  * familiar with the trap type.  Pets get ALLOW_TRAPS
1420                  * and checking is done in dogmove.c.  In either case,
1421                  * "harmless" traps are neither avoided nor marked in info[].
1422                  */
1423                 if ((ttmp = t_at(nx, ny)) != 0) {
1424                     if (ttmp->ttyp >= TRAPNUM || ttmp->ttyp == 0) {
1425                         impossible(
1426                          "A monster looked at a very strange trap of type %d.",
1427                                    ttmp->ttyp);
1428                             continue;
1429                     }
1430                     if ((ttmp->ttyp != RUST_TRAP
1431                          || mdat == &mons[PM_IRON_GOLEM])
1432                         && ttmp->ttyp != STATUE_TRAP
1433                         && ((ttmp->ttyp != PIT && ttmp->ttyp != SPIKED_PIT
1434                              && ttmp->ttyp != TRAPDOOR && ttmp->ttyp != HOLE)
1435                             || (!is_flyer(mdat) && !is_floater(mdat)
1436                                 && !is_clinger(mdat)) || Sokoban)
1437                         && (ttmp->ttyp != SLP_GAS_TRAP || !resists_sleep(mon))
1438                         && (ttmp->ttyp != BEAR_TRAP
1439                             || (mdat->msize > MZ_SMALL && !amorphous(mdat)
1440                                 && !is_flyer(mdat) && !is_floater(mdat)
1441                                 && !is_whirly(mdat) && !unsolid(mdat)))
1442                         && (ttmp->ttyp != FIRE_TRAP || !resists_fire(mon))
1443                         && (ttmp->ttyp != SQKY_BOARD || !is_flyer(mdat))
1444                         && (ttmp->ttyp != WEB
1445                             || (!amorphous(mdat) && !webmaker(mdat)))
1446                         && (ttmp->ttyp != ANTI_MAGIC || !resists_magm(mon))) {
1447                         if (!(flag & ALLOW_TRAPS)) {
1448                             if (mon->mtrapseen & (1L << (ttmp->ttyp - 1)))
1449                                 continue;
1450                         }
1451                         info[cnt] |= ALLOW_TRAPS;
1452                     }
1453                 }
1454                 poss[cnt].x = nx;
1455                 poss[cnt].y = ny;
1456                 cnt++;
1457             }
1458         }
1459     if (!cnt && wantpool && !is_pool(x, y)) {
1460         wantpool = FALSE;
1461         goto nexttry;
1462     }
1463     return cnt;
1464 }
1465
1466 /* Monster against monster special attacks; for the specified monster
1467    combinations, this allows one monster to attack another adjacent one
1468    in the absence of Conflict.  There is no provision for targetting
1469    other monsters; just hand to hand fighting when they happen to be
1470    next to each other. */
1471 STATIC_OVL long
1472 mm_aggression(magr, mdef)
1473 struct monst *magr, /* monster that is currently deciding where to move */
1474              *mdef; /* another monster which is next to it */
1475 {
1476     /* supposedly purple worms are attracted to shrieking because they
1477        like to eat shriekers, so attack the latter when feasible */
1478     if (magr->data == &mons[PM_PURPLE_WORM]
1479         && mdef->data == &mons[PM_SHRIEKER])
1480         return ALLOW_M | ALLOW_TM;
1481     /* Various other combinations such as dog vs cat, cat vs rat, and
1482        elf vs orc have been suggested.  For the time being we don't
1483        support those. */
1484     return 0L;
1485 }
1486
1487 /* Monster displacing another monster out of the way */
1488 STATIC_OVL long
1489 mm_displacement(magr, mdef)
1490 struct monst *magr, /* monster that is currently deciding where to move */
1491              *mdef; /* another monster which is next to it */
1492 {
1493     struct permonst *pa = magr->data, *pd = mdef->data;
1494
1495     /* if attacker can't barge through, there's nothing to do;
1496        or if defender can barge through too, don't let attacker
1497        do so, otherwise they might just end up swapping places
1498        again when defender gets its chance to move */
1499     if ((pa->mflags3 & M3_DISPLACES) != 0 && (pd->mflags3 & M3_DISPLACES) == 0
1500         /* no displacing grid bugs diagonally */
1501         && !(magr->mx != mdef->mx && magr->my != mdef->my
1502              && NODIAG(monsndx(pd)))
1503         /* no displacing trapped monsters or multi-location longworms */
1504         && !mdef->mtrapped && (!mdef->wormno || !count_wsegs(mdef))
1505         /* riders can move anything; others, same size or smaller only */
1506         && (is_rider(pa) || pa->msize >= pd->msize))
1507         return ALLOW_MDISP;
1508     return 0L;
1509 }
1510
1511 /* Is the square close enough for the monster to move or attack into? */
1512 boolean
1513 monnear(mon, x, y)
1514 struct monst *mon;
1515 int x, y;
1516 {
1517     int distance = dist2(mon->mx, mon->my, x, y);
1518
1519     if (distance == 2 && NODIAG(mon->data - mons))
1520         return 0;
1521     return (boolean) (distance < 3);
1522 }
1523
1524 /* really free dead monsters */
1525 void
1526 dmonsfree()
1527 {
1528     struct monst **mtmp, *freetmp;
1529     int count = 0;
1530
1531     for (mtmp = &fmon; *mtmp;) {
1532         freetmp = *mtmp;
1533         if (freetmp->mhp <= 0 && !freetmp->isgd) {
1534             *mtmp = freetmp->nmon;
1535             freetmp->nmon = NULL;
1536             dealloc_monst(freetmp);
1537             count++;
1538         } else
1539             mtmp = &(freetmp->nmon);
1540     }
1541
1542     if (count != iflags.purge_monsters)
1543         impossible("dmonsfree: %d removed doesn't match %d pending",
1544                    count, iflags.purge_monsters);
1545     iflags.purge_monsters = 0;
1546 }
1547
1548 /* called when monster is moved to larger structure */
1549 void
1550 replmon(mtmp, mtmp2)
1551 struct monst *mtmp, *mtmp2;
1552 {
1553     struct obj *otmp;
1554
1555     /* transfer the monster's inventory */
1556     for (otmp = mtmp2->minvent; otmp; otmp = otmp->nobj) {
1557         if (otmp->where != OBJ_MINVENT || otmp->ocarry != mtmp)
1558             impossible("replmon: minvent inconsistency");
1559         otmp->ocarry = mtmp2;
1560     }
1561     mtmp->minvent = 0;
1562
1563     /* remove the old monster from the map and from `fmon' list */
1564     relmon(mtmp, (struct monst **) 0);
1565
1566     /* finish adding its replacement */
1567     if (mtmp != u.usteed) /* don't place steed onto the map */
1568         place_monster(mtmp2, mtmp2->mx, mtmp2->my);
1569     if (mtmp2->wormno)      /* update level.monsters[wseg->wx][wseg->wy] */
1570         place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
1571     if (emits_light(mtmp2->data)) {
1572         /* since this is so rare, we don't have any `mon_move_light_source' */
1573         new_light_source(mtmp2->mx, mtmp2->my, emits_light(mtmp2->data),
1574                          LS_MONSTER, monst_to_any(mtmp2));
1575         /* here we rely on fact that `mtmp' hasn't actually been deleted */
1576         del_light_source(LS_MONSTER, monst_to_any(mtmp));
1577     }
1578     mtmp2->nmon = fmon;
1579     fmon = mtmp2;
1580     if (u.ustuck == mtmp)
1581         u.ustuck = mtmp2;
1582     if (u.usteed == mtmp)
1583         u.usteed = mtmp2;
1584     if (mtmp2->isshk)
1585         replshk(mtmp, mtmp2);
1586
1587     /* discard the old monster */
1588     dealloc_monst(mtmp);
1589 }
1590
1591 /* release mon from the display and the map's monster list,
1592    maybe transfer it to one of the other monster lists */
1593 void
1594 relmon(mon, monst_list)
1595 struct monst *mon;
1596 struct monst **monst_list; /* &migrating_mons or &mydogs or null */
1597 {
1598     struct monst *mtmp;
1599     boolean unhide = (monst_list != 0);
1600     int mx = mon->mx, my = mon->my;
1601
1602     if (!fmon)
1603         panic("relmon: no fmon available.");
1604
1605     if (unhide) {
1606         /* can't remain hidden across level changes (exception: wizard
1607            clone can continue imitating some other monster form); also,
1608            might be imitating a boulder so need line-of-sight unblocking */
1609         mon->mundetected = 0;
1610         if (mon->m_ap_type && mon->m_ap_type != M_AP_MONSTER)
1611             seemimic(mon);
1612     }
1613
1614     remove_monster(mx, my);
1615
1616     if (mon == fmon) {
1617         fmon = fmon->nmon;
1618     } else {
1619         for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
1620             if (mtmp->nmon == mon)
1621                 break;
1622
1623         if (mtmp)
1624             mtmp->nmon = mon->nmon;
1625         else
1626             panic("relmon: mon not in list.");
1627     }
1628
1629     if (unhide) {
1630         newsym(mx, my);
1631         /* insert into mydogs or migrating_mons */
1632         mon->nmon = *monst_list;
1633         *monst_list = mon;
1634     } else {
1635         /* orphan has no next monster */
1636         mon->nmon = 0;
1637     }
1638 }
1639
1640 void
1641 copy_mextra(mtmp2, mtmp1)
1642 struct monst *mtmp2, *mtmp1;
1643 {
1644     if (!mtmp2 || !mtmp1 || !mtmp1->mextra)
1645         return;
1646
1647     if (!mtmp2->mextra)
1648         mtmp2->mextra = newmextra();
1649     if (MNAME(mtmp1)) {
1650         new_mname(mtmp2, (int) strlen(MNAME(mtmp1)) + 1);
1651         Strcpy(MNAME(mtmp2), MNAME(mtmp1));
1652     }
1653     if (EGD(mtmp1)) {
1654         if (!EGD(mtmp2))
1655             newegd(mtmp2);
1656         (void) memcpy((genericptr_t) EGD(mtmp2), (genericptr_t) EGD(mtmp1),
1657                       sizeof (struct egd));
1658     }
1659     if (EPRI(mtmp1)) {
1660         if (!EPRI(mtmp2))
1661             newepri(mtmp2);
1662         (void) memcpy((genericptr_t) EPRI(mtmp2), (genericptr_t) EPRI(mtmp1),
1663                       sizeof (struct epri));
1664     }
1665     if (ESHK(mtmp1)) {
1666         if (!ESHK(mtmp2))
1667             neweshk(mtmp2);
1668         (void) memcpy((genericptr_t) ESHK(mtmp2), (genericptr_t) ESHK(mtmp1),
1669                       sizeof (struct eshk));
1670     }
1671     if (EMIN(mtmp1)) {
1672         if (!EMIN(mtmp2))
1673             newemin(mtmp2);
1674         (void) memcpy((genericptr_t) EMIN(mtmp2), (genericptr_t) EMIN(mtmp1),
1675                       sizeof (struct emin));
1676     }
1677     if (EDOG(mtmp1)) {
1678         if (!EDOG(mtmp2))
1679             newedog(mtmp2);
1680         (void) memcpy((genericptr_t) EDOG(mtmp2), (genericptr_t) EDOG(mtmp1),
1681                       sizeof (struct edog));
1682     }
1683     if (has_mcorpsenm(mtmp1))
1684         MCORPSENM(mtmp2) = MCORPSENM(mtmp1);
1685 }
1686
1687 void
1688 dealloc_mextra(m)
1689 struct monst *m;
1690 {
1691     struct mextra *x = m->mextra;
1692
1693     if (x) {
1694         if (x->mname)
1695             free((genericptr_t) x->mname);
1696         if (x->egd)
1697             free((genericptr_t) x->egd);
1698         if (x->epri)
1699             free((genericptr_t) x->epri);
1700         if (x->eshk)
1701             free((genericptr_t) x->eshk);
1702         if (x->emin)
1703             free((genericptr_t) x->emin);
1704         if (x->edog)
1705             free((genericptr_t) x->edog);
1706         /* [no action needed for x->mcorpsenm] */
1707
1708         free((genericptr_t) x);
1709         m->mextra = (struct mextra *) 0;
1710     }
1711 }
1712
1713 void
1714 dealloc_monst(mon)
1715 struct monst *mon;
1716 {
1717     if (mon->nmon)
1718         panic("dealloc_monst with nmon");
1719     if (mon->mextra)
1720         dealloc_mextra(mon);
1721     free((genericptr_t) mon);
1722 }
1723
1724 /* remove effects of mtmp from other data structures */
1725 STATIC_OVL void
1726 m_detach(mtmp, mptr)
1727 struct monst *mtmp;
1728 struct permonst *mptr; /* reflects mtmp->data _prior_ to mtmp's death */
1729 {
1730     if (mtmp == context.polearm.hitmon)
1731         context.polearm.hitmon = 0;
1732     if (mtmp->mleashed)
1733         m_unleash(mtmp, FALSE);
1734     /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
1735     mtmp->mtrapped = 0;
1736     mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */
1737     relobj(mtmp, 0, FALSE);
1738     remove_monster(mtmp->mx, mtmp->my);
1739     if (emits_light(mptr))
1740         del_light_source(LS_MONSTER, monst_to_any(mtmp));
1741     newsym(mtmp->mx, mtmp->my);
1742     unstuck(mtmp);
1743     fill_pit(mtmp->mx, mtmp->my);
1744
1745     if (mtmp->isshk)
1746         shkgone(mtmp);
1747     if (mtmp->wormno)
1748         wormgone(mtmp);
1749     iflags.purge_monsters++;
1750 }
1751
1752 /* find the worn amulet of life saving which will save a monster */
1753 struct obj *
1754 mlifesaver(mon)
1755 struct monst *mon;
1756 {
1757     if (!nonliving(mon->data) || is_vampshifter(mon)) {
1758         struct obj *otmp = which_armor(mon, W_AMUL);
1759
1760         if (otmp && otmp->otyp == AMULET_OF_LIFE_SAVING)
1761             return otmp;
1762     }
1763     return (struct obj *) 0;
1764 }
1765
1766 STATIC_OVL void
1767 lifesaved_monster(mtmp)
1768 struct monst *mtmp;
1769 {
1770     boolean surviver;
1771     struct obj *lifesave = mlifesaver(mtmp);
1772
1773     if (lifesave) {
1774         /* not canseemon; amulets are on the head, so you don't want
1775          * to show this for a long worm with only a tail visible.
1776          * Nor do you check invisibility, because glowing and
1777          * disintegrating amulets are always visible. */
1778         if (cansee(mtmp->mx, mtmp->my)) {
1779 /*JP
1780             pline("But wait...");
1781 */
1782                         pline("\82µ\82©\82µ\81D\81D\81D");
1783 /*JP
1784             pline("%s medallion begins to glow!", s_suffix(Monnam(mtmp)));
1785 */
1786             pline("%s\82Ì\83\81\83_\83\8a\83I\83\93\82ª\8bP\82«\82Í\82\82ß\82½\81I", Monnam(mtmp));
1787             makeknown(AMULET_OF_LIFE_SAVING);
1788             /* amulet is visible, but monster might not be */
1789             if (canseemon(mtmp)) {
1790                 if (attacktype(mtmp->data, AT_EXPL)
1791                     || attacktype(mtmp->data, AT_BOOM))
1792 /*JP
1793                     pline("%s reconstitutes!", Monnam(mtmp));
1794 */
1795                     pline("%s\82Í\8dÄ\8d\\90¬\82³\82ê\82½\81I", Monnam(mtmp));
1796                 else
1797 /*JP
1798                     pline("%s looks much better!", Monnam(mtmp));
1799 */
1800                     pline("%s\82Í\82·\82Á\82©\82è\89ñ\95\9c\82µ\82½\82æ\82¤\82¾\81I", Monnam(mtmp));
1801             }
1802 /*JP
1803             pline_The("medallion crumbles to dust!");
1804 */
1805             pline_The("\83\81\83_\83\8a\83I\83\93\82Í\82±\82È\82²\82È\82É\82­\82¾\82¯\82Ä\82µ\82Ü\82Á\82½\81I");
1806         }
1807         m_useup(mtmp, lifesave);
1808
1809         surviver = !(mvitals[monsndx(mtmp->data)].mvflags & G_GENOD);
1810         mtmp->mcanmove = 1;
1811         mtmp->mfrozen = 0;
1812         if (mtmp->mtame && !mtmp->isminion) {
1813             wary_dog(mtmp, !surviver);
1814         }
1815         if (mtmp->mhpmax <= 0)
1816             mtmp->mhpmax = 10;
1817         mtmp->mhp = mtmp->mhpmax;
1818         if (surviver)
1819             return;
1820
1821         /* genocided monster can't be life-saved */
1822         if (cansee(mtmp->mx, mtmp->my))
1823 /*JP
1824             pline("Unfortunately, %s is still genocided...", mon_nam(mtmp));
1825 */
1826             pline("\8ec\94O\82È\82ª\82ç%s\82Í\8bs\8eE\82³\82ê\82Ä\82¢\82é\81D\81D\81D", mon_nam(mtmp));
1827     }
1828     mtmp->mhp = 0;
1829 }
1830
1831 void
1832 mondead(mtmp)
1833 register struct monst *mtmp;
1834 {
1835     struct permonst *mptr;
1836     int tmp;
1837
1838     lifesaved_monster(mtmp);
1839     if (mtmp->mhp > 0)
1840         return;
1841
1842     if (is_vampshifter(mtmp)) {
1843         int mndx = mtmp->cham;
1844         int x = mtmp->mx, y = mtmp->my;
1845
1846         /* this only happens if shapeshifted */
1847         if (mndx >= LOW_PM && mndx != monsndx(mtmp->data)) {
1848             char buf[BUFSZ];
1849             boolean in_door = (amorphous(mtmp->data)
1850                                && closed_door(mtmp->mx, mtmp->my)),
1851                 /* alternate message phrasing for some monster types */
1852                 spec_mon = (nonliving(mtmp->data)
1853                             || noncorporeal(mtmp->data)
1854                             || amorphous(mtmp->data));
1855
1856             /* construct a format string before transformation */
1857             Sprintf(buf, "The %s%s suddenly %s and rises as %%s!",
1858                     spec_mon ? "" : "seemingly dead ",
1859                     x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1860                              SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
1861                                  | SUPPRESS_INVISIBLE | SUPPRESS_IT,
1862                              FALSE),
1863                     spec_mon ? "reconstitutes" : "transforms");
1864             mtmp->mcanmove = 1;
1865             mtmp->mfrozen = 0;
1866             if (mtmp->mhpmax <= 0)
1867                 mtmp->mhpmax = 10;
1868             mtmp->mhp = mtmp->mhpmax;
1869             /* this can happen if previously a fog cloud */
1870             if (u.uswallow && (mtmp == u.ustuck))
1871                 expels(mtmp, mtmp->data, FALSE);
1872             if (in_door) {
1873                 coord new_xy;
1874
1875                 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
1876                     rloc_to(mtmp, new_xy.x, new_xy.y);
1877                 }
1878             }
1879             newcham(mtmp, &mons[mndx], FALSE, FALSE);
1880             if (mtmp->data == &mons[mndx])
1881                 mtmp->cham = NON_PM;
1882             else
1883                 mtmp->cham = mndx;
1884             if (canspotmon(mtmp)) {
1885                 pline(buf, a_monnam(mtmp));
1886                 vamp_rise_msg = TRUE;
1887             }
1888             newsym(x, y);
1889             return;
1890         }
1891     }
1892
1893     /* dead vault guard is actually kept at coordinate <0,0> until
1894        his temporary corridor to/from the vault has been removed;
1895        need to do this after life-saving and before m_detach() */
1896     if (mtmp->isgd && !grddead(mtmp))
1897         return;
1898
1899     /* Player is thrown from his steed when it dies */
1900     if (mtmp == u.usteed)
1901         dismount_steed(DISMOUNT_GENERIC);
1902
1903     mptr = mtmp->data; /* save this for m_detach() */
1904     /* restore chameleon, lycanthropes to true form at death */
1905     if (mtmp->cham >= LOW_PM) {
1906         set_mon_data(mtmp, &mons[mtmp->cham], -1);
1907         mtmp->cham = NON_PM;
1908     } else if (mtmp->data == &mons[PM_WEREJACKAL])
1909         set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1910     else if (mtmp->data == &mons[PM_WEREWOLF])
1911         set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1912     else if (mtmp->data == &mons[PM_WERERAT])
1913         set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1914
1915     /* if MAXMONNO monsters of a given type have died, and it
1916      * can be done, extinguish that monster.
1917      *
1918      * mvitals[].died does double duty as total number of dead monsters
1919      * and as experience factor for the player killing more monsters.
1920      * this means that a dragon dying by other means reduces the
1921      * experience the player gets for killing a dragon directly; this
1922      * is probably not too bad, since the player likely finagled the
1923      * first dead dragon via ring of conflict or pets, and extinguishing
1924      * based on only player kills probably opens more avenues of abuse
1925      * for rings of conflict and such.
1926      */
1927     tmp = monsndx(mtmp->data);
1928     if (mvitals[tmp].died < 255)
1929         mvitals[tmp].died++;
1930
1931     /* if it's a (possibly polymorphed) quest leader, mark him as dead */
1932     if (mtmp->m_id == quest_status.leader_m_id)
1933         quest_status.leader_is_dead = TRUE;
1934 #ifdef MAIL
1935     /* if the mail daemon dies, no more mail delivery.  -3. */
1936     if (tmp == PM_MAIL_DAEMON)
1937         mvitals[tmp].mvflags |= G_GENOD;
1938 #endif
1939
1940     if (mtmp->data->mlet == S_KOP) {
1941         /* Dead Kops may come back. */
1942         switch (rnd(5)) {
1943         case 1: /* returns near the stairs */
1944             (void) makemon(mtmp->data, xdnstair, ydnstair, NO_MM_FLAGS);
1945             break;
1946         case 2: /* randomly */
1947             (void) makemon(mtmp->data, 0, 0, NO_MM_FLAGS);
1948             break;
1949         default:
1950             break;
1951         }
1952     }
1953     if (mtmp->iswiz)
1954         wizdead();
1955     if (mtmp->data->msound == MS_NEMESIS)
1956         nemdead();
1957     if (mtmp->data == &mons[PM_MEDUSA])
1958         u.uachieve.killed_medusa = 1;
1959     if (glyph_is_invisible(levl[mtmp->mx][mtmp->my].glyph))
1960         unmap_object(mtmp->mx, mtmp->my);
1961     m_detach(mtmp, mptr);
1962 }
1963
1964 /* TRUE if corpse might be dropped, magr may die if mon was swallowed */
1965 boolean
1966 corpse_chance(mon, magr, was_swallowed)
1967 struct monst *mon;
1968 struct monst *magr;    /* killer, if swallowed */
1969 boolean was_swallowed; /* digestion */
1970 {
1971     struct permonst *mdat = mon->data;
1972     int i, tmp;
1973
1974     if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1975         if (cansee(mon->mx, mon->my) && !was_swallowed)
1976 /*JP
1977             pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
1978 */
1979             pline("%s\82Ì\91Ì\82Í\95²\81X\82É\82È\82Á\82½\81D", Monnam(mon));
1980         return FALSE;
1981     }
1982
1983     /* Gas spores always explode upon death */
1984     for (i = 0; i < NATTK; i++) {
1985         if (mdat->mattk[i].aatyp == AT_BOOM) {
1986             if (mdat->mattk[i].damn)
1987                 tmp = d((int) mdat->mattk[i].damn, (int) mdat->mattk[i].damd);
1988             else if (mdat->mattk[i].damd)
1989                 tmp = d((int) mdat->mlevel + 1, (int) mdat->mattk[i].damd);
1990             else
1991                 tmp = 0;
1992             if (was_swallowed && magr) {
1993                 if (magr == &youmonst) {
1994 /*JP
1995                     There("is an explosion in your %s!", body_part(STOMACH));
1996 */
1997                     pline("%s\82Ì\92\86\82Å\94\9a\94­\82ª\8bN\82«\82½\81I", body_part(STOMACH));
1998 #if 0 /*JP*/
1999                     Sprintf(killer.name, "%s explosion",
2000                             s_suffix(mdat->mname));
2001 #else
2002                     Sprintf(killer.name, "%s\82Ì\94\9a\94­\82Å", mdat->mname);
2003 #endif
2004                     losehp(Maybe_Half_Phys(tmp), killer.name, KILLED_BY_AN);
2005                 } else {
2006 /*JP
2007                     You_hear("an explosion.");
2008 */
2009                     You_hear("\94\9a\94­\89¹\82ð\95·\82¢\82½\81D");
2010                     magr->mhp -= tmp;
2011                     if (magr->mhp < 1)
2012                         mondied(magr);
2013                     if (magr->mhp < 1) { /* maybe lifesaved */
2014                         if (canspotmon(magr))
2015 /*JP
2016                             pline("%s rips open!", Monnam(magr));
2017 */
2018                             pline("%s\82Í\83r\83\8a\82Á\82Æ\94j\82ê\82½\81I", Monnam(magr));
2019                     } else if (canseemon(magr))
2020 /*JP
2021                         pline("%s seems to have indigestion.", Monnam(magr));
2022 */
2023                         pline("%s\82Í\8fÁ\89»\95s\97Ç\82Ì\82æ\82¤\82¾\81D", Monnam(magr));
2024                 }
2025
2026                 return FALSE;
2027             }
2028
2029 /*JP
2030             Sprintf(killer.name, "%s explosion", s_suffix(mdat->mname));
2031 */
2032             Sprintf(killer.name, "%s\82Ì\94\9a\94­\82Å", mdat->mname);
2033             killer.format = KILLED_BY_AN;
2034             explode(mon->mx, mon->my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS);
2035             return FALSE;
2036         }
2037     }
2038
2039     /* must duplicate this below check in xkilled() since it results in
2040      * creating no objects as well as no corpse
2041      */
2042     if (LEVEL_SPECIFIC_NOCORPSE(mdat))
2043         return FALSE;
2044
2045     if (((bigmonst(mdat) || mdat == &mons[PM_LIZARD]) && !mon->mcloned)
2046         || is_golem(mdat) || is_mplayer(mdat) || is_rider(mdat))
2047         return TRUE;
2048     tmp = 2 + ((mdat->geno & G_FREQ) < 2) + verysmall(mdat);
2049     return (boolean) !rn2(tmp);
2050 }
2051
2052 /* drop (perhaps) a cadaver and remove monster */
2053 void
2054 mondied(mdef)
2055 register struct monst *mdef;
2056 {
2057     mondead(mdef);
2058     if (mdef->mhp > 0)
2059         return; /* lifesaved */
2060
2061     if (corpse_chance(mdef, (struct monst *) 0, FALSE)
2062         && (accessible(mdef->mx, mdef->my) || is_pool(mdef->mx, mdef->my)))
2063         (void) make_corpse(mdef, CORPSTAT_NONE);
2064 }
2065
2066 /* monster disappears, not dies */
2067 void
2068 mongone(mdef)
2069 struct monst *mdef;
2070 {
2071     mdef->mhp = 0; /* can skip some inventory bookkeeping */
2072
2073     /* dead vault guard is actually kept at coordinate <0,0> until
2074        his temporary corridor to/from the vault has been removed */
2075     if (mdef->isgd && !grddead(mdef))
2076         return;
2077     /* hero is thrown from his steed when it disappears */
2078     if (mdef == u.usteed)
2079         dismount_steed(DISMOUNT_GENERIC);
2080     /* drop special items like the Amulet so that a dismissed Kop or nurse
2081        can't remove them from the game */
2082     mdrop_special_objs(mdef);
2083     /* release rest of monster's inventory--it is removed from game */
2084     discard_minvent(mdef);
2085     m_detach(mdef, mdef->data);
2086 }
2087
2088 /* drop a statue or rock and remove monster */
2089 void
2090 monstone(mdef)
2091 struct monst *mdef;
2092 {
2093     struct obj *otmp, *obj, *oldminvent;
2094     xchar x = mdef->mx, y = mdef->my;
2095     boolean wasinside = FALSE;
2096
2097     /* we have to make the statue before calling mondead, to be able to
2098      * put inventory in it, and we have to check for lifesaving before
2099      * making the statue....
2100      */
2101     lifesaved_monster(mdef);
2102     if (mdef->mhp > 0)
2103         return;
2104
2105     mdef->mtrapped = 0; /* (see m_detach) */
2106
2107     if ((int) mdef->data->msize > MZ_TINY
2108         || !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
2109         oldminvent = 0;
2110         /* some objects may end up outside the statue */
2111         while ((obj = mdef->minvent) != 0) {
2112             obj_extract_self(obj);
2113             if (obj->owornmask)
2114                 update_mon_intrinsics(mdef, obj, FALSE, TRUE);
2115             obj_no_longer_held(obj);
2116             if (obj->owornmask & W_WEP)
2117                 setmnotwielded(mdef, obj);
2118             obj->owornmask = 0L;
2119             if (obj->otyp == BOULDER
2120 #if 0 /* monsters don't carry statues */
2121                 ||  (obj->otyp == STATUE
2122                      && mons[obj->corpsenm].msize >= mdef->data->msize)
2123 #endif
2124                 /* invocation tools resist even with 0% resistance */
2125                 || obj_resists(obj, 0, 0)) {
2126 /*JP
2127                 if (flooreffects(obj, x, y, "fall"))
2128 */
2129                 if (flooreffects(obj, x, y, "\97\8e\82¿\82é"))
2130                     continue;
2131                 place_object(obj, x, y);
2132             } else {
2133                 if (obj->lamplit)
2134                     end_burn(obj, TRUE);
2135                 obj->nobj = oldminvent;
2136                 oldminvent = obj;
2137             }
2138         }
2139         /* defer statue creation until after inventory removal
2140            so that saved monster traits won't retain any stale
2141            item-conferred attributes */
2142         otmp = mkcorpstat(STATUE, mdef, mdef->data, x, y, CORPSTAT_NONE);
2143         if (has_mname(mdef))
2144             otmp = oname(otmp, MNAME(mdef));
2145         while ((obj = oldminvent) != 0) {
2146             oldminvent = obj->nobj;
2147             (void) add_to_container(otmp, obj);
2148         }
2149         /* Archeologists should not break unique statues */
2150         if (mdef->data->geno & G_UNIQ)
2151             otmp->spe = 1;
2152         otmp->owt = weight(otmp);
2153     } else
2154         otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
2155
2156     stackobj(otmp);
2157     /* mondead() already does this, but we must do it before the newsym */
2158     if (glyph_is_invisible(levl[x][y].glyph))
2159         unmap_object(x, y);
2160     if (cansee(x, y))
2161         newsym(x, y);
2162     /* We don't currently trap the hero in the statue in this case but we
2163      * could */
2164     if (u.uswallow && u.ustuck == mdef)
2165         wasinside = TRUE;
2166     mondead(mdef);
2167     if (wasinside) {
2168         if (is_animal(mdef->data))
2169 #if 0 /*JP*/
2170             You("%s through an opening in the new %s.",
2171                 locomotion(youmonst.data, "jump"), xname(otmp));
2172 #else
2173             You("\90V\82µ\82­\82Å\82«\82½%s\82©\82ç%s\81D",
2174                 xname(otmp), jumpedthrough(youmonst.data, "\94ò\82Ñ\8fo\82½"));
2175 #endif
2176     }
2177 }
2178
2179 /* another monster has killed the monster mdef */
2180 void
2181 monkilled(mdef, fltxt, how)
2182 struct monst *mdef;
2183 const char *fltxt;
2184 int how;
2185 {
2186     boolean be_sad = FALSE; /* true if unseen pet is killed */
2187
2188     if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
2189         && fltxt)
2190 #if 0 /*JP*/
2191         pline("%s is %s%s%s!", Monnam(mdef),
2192               nonliving(mdef->data) ? "destroyed" : "killed",
2193               *fltxt ? " by the " : "", fltxt);
2194 #else
2195         {
2196             if(*fltxt)
2197                 pline("%s\82Í%s\82É\82æ\82Á\82Ä%s\81I", Monnam(mdef), fltxt,
2198                       nonliving(mdef->data) ? "\93|\82³\82ê\82½" : "\8eE\82³\82ê\82½");
2199             else
2200                 pline("%s\82Í%s\81I", Monnam(mdef), 
2201                       nonliving(mdef->data) ? "\93|\82³\82ê\82½" : "\8eE\82³\82ê\82½");
2202         }
2203 #endif
2204     else
2205         be_sad = (mdef->mtame != 0);
2206
2207     /* no corpses if digested or disintegrated */
2208     if (how == AD_DGST || how == -AD_RBRE)
2209         mondead(mdef);
2210     else
2211         mondied(mdef);
2212
2213     if (be_sad && mdef->mhp <= 0)
2214 /*JP
2215         You("have a sad feeling for a moment, then it passes.");
2216 */
2217         You("\94ß\82µ\82¢\8bC\8e\9d\82É\82¨\82»\82í\82ê\82½\82ª\81C\82·\82®\82É\89ß\82¬\82³\82Á\82½\81D");
2218 }
2219
2220 void
2221 unstuck(mtmp)
2222 struct monst *mtmp;
2223 {
2224     if (u.ustuck == mtmp) {
2225         if (u.uswallow) {
2226             u.ux = mtmp->mx;
2227             u.uy = mtmp->my;
2228             u.uswallow = 0;
2229             u.uswldtim = 0;
2230             if (Punished && uchain->where != OBJ_FLOOR)
2231                 placebc();
2232             vision_full_recalc = 1;
2233             docrt();
2234         }
2235         u.ustuck = 0;
2236     }
2237 }
2238
2239 void
2240 killed(mtmp)
2241 struct monst *mtmp;
2242 {
2243     xkilled(mtmp, 1);
2244 }
2245
2246 /* the player has killed the monster mtmp */
2247 void
2248 xkilled(mtmp, dest)
2249 struct monst *mtmp;
2250 int dest; /* dest==1, normal; dest==0, don't print message; dest==2, don't
2251              drop corpse either; dest==3, message but no corpse */
2252 {
2253     int tmp, mndx, x = mtmp->mx, y = mtmp->my;
2254     struct permonst *mdat;
2255     struct obj *otmp;
2256     struct trap *t;
2257     boolean wasinside = u.uswallow && (u.ustuck == mtmp);
2258     boolean burycorpse = FALSE;
2259
2260     /* KMH, conduct */
2261     u.uconduct.killer++;
2262
2263     if (dest & 1) {
2264 #if 0 /*JP*/
2265         const char *verb = nonliving(mtmp->data) ? "destroy" : "kill";
2266 #endif
2267
2268         if (!wasinside && !canspotmon(mtmp))
2269 /*JP
2270             You("%s it!", verb);
2271 */
2272             You("\89½\8eÒ\82©\82ð\93|\82µ\82½\81I");
2273         else {
2274 #if 0 /*JP*/
2275             You("%s %s!", verb,
2276                 !mtmp->mtame
2277                     ? mon_nam(mtmp)
2278                     : x_monnam(mtmp,
2279                                (has_mname(mtmp)) ? ARTICLE_NONE : ARTICLE_THE,
2280                                "poor",
2281                                (has_mname(mtmp)) ? SUPPRESS_SADDLE : 0,
2282                                FALSE));
2283 #else
2284             You("%s%s\82ð\93|\82µ\82½\81I",
2285                 mtmp->mtame ? "\82©\82í\82¢\82»\82¤\82È" : "",
2286                 mon_nam(mtmp));
2287 #endif
2288         }
2289     }
2290
2291     if (mtmp->mtrapped && (t = t_at(x, y)) != 0
2292         && (t->ttyp == PIT || t->ttyp == SPIKED_PIT)) {
2293         if (sobj_at(BOULDER, x, y))
2294             dest |= 2; /*
2295         * Prevent corpses/treasure being created "on top"
2296         * of the boulder that is about to fall in. This is
2297         * out of order, but cannot be helped unless this
2298         * whole routine is rearranged.
2299         */
2300         if (m_carrying(mtmp, BOULDER))
2301             burycorpse = TRUE;
2302     }
2303
2304     /* your pet knows who just killed it...watch out */
2305     if (mtmp->mtame && !mtmp->isminion)
2306         EDOG(mtmp)->killed_by_u = 1;
2307
2308     if (wasinside && thrownobj && thrownobj != uball) {
2309         /* thrown object has killed hero's engulfer; add it to mon's
2310            inventory now so that it will be placed with mon's other
2311            stuff prior to lookhere/autopickup when hero is expelled
2312            below (as a side-effect, this missile has immunity from
2313            being consumed [for this shot/throw only]) */
2314         mpickobj(mtmp, thrownobj);
2315         /* let throwing code know that missile has been disposed of */
2316         thrownobj = 0;
2317     }
2318
2319     vamp_rise_msg = FALSE; /* might get set in mondead() */
2320     /* dispose of monster and make cadaver */
2321     if (stoned)
2322         monstone(mtmp);
2323     else
2324         mondead(mtmp);
2325
2326     if (mtmp->mhp > 0) { /* monster lifesaved */
2327         /* Cannot put the non-visible lifesaving message in
2328          * lifesaved_monster() since the message appears only when you
2329          * kill it (as opposed to visible lifesaving which always
2330          * appears).
2331          */
2332         stoned = FALSE;
2333         if (!cansee(x, y) && !vamp_rise_msg)
2334 /*JP
2335             pline("Maybe not...");
2336 */
2337             pline("\82¢\82â\81C\88á\82¤\82©\82à\81D\81D\81D");
2338         return;
2339     }
2340
2341     mdat = mtmp->data; /* note: mondead can change mtmp->data */
2342     mndx = monsndx(mdat);
2343
2344     if (stoned) {
2345         stoned = FALSE;
2346         goto cleanup;
2347     }
2348
2349     if ((dest & 2) || LEVEL_SPECIFIC_NOCORPSE(mdat))
2350         goto cleanup;
2351
2352 #ifdef MAIL
2353     if (mdat == &mons[PM_MAIL_DAEMON]) {
2354         stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
2355     }
2356 #endif
2357     if (accessible(x, y) || is_pool(x, y)) {
2358         struct obj *cadaver;
2359         int otyp;
2360
2361         /* illogical but traditional "treasure drop" */
2362         if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
2363             /* no extra item from swallower or steed */
2364             && (x != u.ux || y != u.uy)
2365             /* no extra item from kops--too easy to abuse */
2366             && mdat->mlet != S_KOP
2367             /* no items from cloned monsters */
2368             && !mtmp->mcloned) {
2369             otmp = mkobj(RANDOM_CLASS, TRUE);
2370             /* don't create large objects from small monsters */
2371             otyp = otmp->otyp;
2372             if (mdat->msize < MZ_HUMAN && otyp != FIGURINE
2373                 /* oc_big is also oc_bimanual and oc_bulky */
2374                 && (otmp->owt > 30 || objects[otyp].oc_big)) {
2375                 delobj(otmp);
2376             } else if (!flooreffects(otmp, x, y, (dest & 1) ? "fall" : "")) {
2377                 place_object(otmp, x, y);
2378                 stackobj(otmp);
2379             }
2380         }
2381         /* corpse--none if hero was inside the monster */
2382         if (!wasinside && corpse_chance(mtmp, (struct monst *) 0, FALSE)) {
2383             cadaver = make_corpse(mtmp, burycorpse ? CORPSTAT_BURIED
2384                                                    : CORPSTAT_NONE);
2385             if (burycorpse && cadaver && cansee(x, y) && !mtmp->minvis
2386                 && cadaver->where == OBJ_BURIED && (dest & 1)) {
2387                 pline("%s corpse ends up buried.", s_suffix(Monnam(mtmp)));
2388             }
2389         }
2390     }
2391     if (wasinside)
2392         spoteffects(TRUE); /* poor man's expels() */
2393     /* monster is gone, corpse or other object might now be visible */
2394     newsym(x, y);
2395
2396 cleanup:
2397     /* punish bad behaviour */
2398     if (is_human(mdat) && (!always_hostile(mdat) && mtmp->malign <= 0)
2399         && (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD)
2400         && u.ualign.type != A_CHAOTIC) {
2401         HTelepat &= ~INTRINSIC;
2402         change_luck(-2);
2403 /*JP
2404         You("murderer!");
2405 */
2406         You("\8eE\90l\8bS\82¾\81I");
2407         if (Blind && !Blind_telepat)
2408             see_monsters(); /* Can't sense monsters any more. */
2409     }
2410     if ((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)
2411         change_luck(-1);
2412     if (is_unicorn(mdat) && sgn(u.ualign.type) == sgn(mdat->maligntyp)) {
2413         change_luck(-5);
2414 /*JP
2415         You_feel("guilty...");
2416 */
2417         You("\8dß\82ð\8a´\82\82½\81D\81D\81D");
2418     }
2419
2420     /* give experience points */
2421     tmp = experience(mtmp, (int) mvitals[mndx].died);
2422     more_experienced(tmp, 0);
2423     newexplevel(); /* will decide if you go up */
2424
2425     /* adjust alignment points */
2426     if (mtmp->m_id == quest_status.leader_m_id) { /* REAL BAD! */
2427         adjalign(-(u.ualign.record + (int) ALIGNLIM / 2));
2428 #if 0 /*JP*/
2429         pline("That was %sa bad idea...",
2430               u.uevent.qcompleted ? "probably " : "");
2431 #else
2432         pline("%s\82æ\82­\82È\82¢\8ds\88×\82¾\82Á\82½\81D\81D\81D",
2433               u.uevent.qcompleted ? "\82½\82Ô\82ñ" : "");
2434 #endif
2435     } else if (mdat->msound == MS_NEMESIS) { /* Real good! */
2436         adjalign((int) (ALIGNLIM / 4));
2437     } else if (mdat->msound == MS_GUARDIAN) { /* Bad */
2438         adjalign(-(int) (ALIGNLIM / 8));
2439         if (!Hallucination)
2440 /*JP
2441             pline("That was probably a bad idea...");
2442 */
2443             pline("\82æ\82­\82È\82¢\8ds\88×\82¾\82Á\82½\81D\81D\81D");
2444         else
2445 /*JP
2446             pline("Whoopsie-daisy!");
2447 */
2448             pline("\83V\83\93\83W\83}\83b\83^\81[\81I");
2449     } else if (mtmp->ispriest) {
2450         adjalign((p_coaligned(mtmp)) ? -2 : 2);
2451         /* cancel divine protection for killing your priest */
2452         if (p_coaligned(mtmp))
2453             u.ublessed = 0;
2454         if (mdat->maligntyp == A_NONE)
2455             adjalign((int) (ALIGNLIM / 4)); /* BIG bonus */
2456     } else if (mtmp->mtame) {
2457         adjalign(-15); /* bad!! */
2458         /* your god is mighty displeased... */
2459         if (!Hallucination)
2460 /*JP
2461             You_hear("the rumble of distant thunder...");
2462 */
2463             You_hear("\89\93\82­\82Å\97\8b\96Â\82ð\95·\82¢\82½\81D\81D\81D");
2464         else
2465 /*JP
2466             You_hear("the studio audience applaud!");
2467 */
2468             pline("\92®\8fO\82Ì\8a\85\8dÑ\82ð\97\81\82Ñ\82½\81I");
2469     } else if (mtmp->mpeaceful)
2470         adjalign(-5);
2471
2472     /* malign was already adjusted for u.ualign.type and randomization */
2473     adjalign(mtmp->malign);
2474 }
2475
2476 /* changes the monster into a stone monster of the same type
2477    this should only be called when poly_when_stoned() is true */
2478 void
2479 mon_to_stone(mtmp)
2480 struct monst *mtmp;
2481 {
2482     if (mtmp->data->mlet == S_GOLEM) {
2483         /* it's a golem, and not a stone golem */
2484         if (canseemon(mtmp))
2485 /*JP
2486             pline("%s solidifies...", Monnam(mtmp));
2487 */
2488             pline("%s\82Í\8bÃ\8cÅ\82µ\82½\81D\81D\81D", Monnam(mtmp));
2489         if (newcham(mtmp, &mons[PM_STONE_GOLEM], FALSE, FALSE)) {
2490             if (canseemon(mtmp))
2491 /*JP
2492                 pline("Now it's %s.", an(mtmp->data->mname));
2493 */
2494                 pline("\82È\82ñ\82Æ%s\82É\82È\82Á\82Ä\82µ\82Ü\82Á\82½\81D", mtmp->data->mname);
2495         } else {
2496             if (canseemon(mtmp))
2497 /*JP
2498                 pline("... and returns to normal.");
2499 */
2500                 pline("\81D\81D\81D\82»\82µ\82Ä\95\81\92Ê\82É\96ß\82Á\82½\81D");
2501         }
2502     } else
2503         impossible("Can't polystone %s!", a_monnam(mtmp));
2504 }
2505
2506 /* make monster mtmp next to you (if possible);
2507    might place monst on far side of a wall or boulder */
2508 void
2509 mnexto(mtmp)
2510 struct monst *mtmp;
2511 {
2512     coord mm;
2513     boolean couldspot = canspotmon(mtmp);
2514
2515     if (mtmp == u.usteed) {
2516         /* Keep your steed in sync with you instead */
2517         mtmp->mx = u.ux;
2518         mtmp->my = u.uy;
2519         return;
2520     }
2521
2522     if (!enexto(&mm, u.ux, u.uy, mtmp->data))
2523         return;
2524     rloc_to(mtmp, mm.x, mm.y);
2525     if (!in_mklev && (mtmp->mstrategy & STRAT_APPEARMSG)) {
2526         mtmp->mstrategy &= ~STRAT_APPEARMSG; /* one chance only */
2527         if (!couldspot && canspotmon(mtmp))
2528             pline("%s suddenly %s!", Amonnam(mtmp),
2529                   !Blind ? "appears" : "arrives");
2530     }
2531     return;
2532 }
2533
2534 /* like mnexto() but requires destination to be directly accessible */
2535 void
2536 maybe_mnexto(mtmp)
2537 struct monst *mtmp;
2538 {
2539     coord mm;
2540     struct permonst *ptr = mtmp->data;
2541     boolean diagok = !NODIAG(ptr - mons);
2542     int tryct = 20;
2543
2544     do {
2545         if (!enexto(&mm, u.ux, u.uy, ptr))
2546             return;
2547         if (couldsee(mm.x, mm.y)
2548             /* don't move grid bugs diagonally */
2549             && (diagok || mm.x == mtmp->mx || mm.y == mtmp->my)) {
2550             rloc_to(mtmp, mm.x, mm.y);
2551             return;
2552         }
2553     } while (--tryct > 0);
2554 }
2555
2556 /* mnearto()
2557  * Put monster near (or at) location if possible.
2558  * Returns:
2559  *      1 - if a monster was moved from x, y to put mtmp at x, y.
2560  *      0 - in most cases.
2561  */
2562 boolean
2563 mnearto(mtmp, x, y, move_other)
2564 register struct monst *mtmp;
2565 xchar x, y;
2566 boolean move_other; /* make sure mtmp gets to x, y! so move m_at(x, y) */
2567 {
2568     struct monst *othermon = (struct monst *) 0;
2569     xchar newx, newy;
2570     coord mm;
2571
2572     if (mtmp->mx == x && mtmp->my == y)
2573         return FALSE;
2574
2575     if (move_other && (othermon = m_at(x, y)) != 0) {
2576         if (othermon->wormno)
2577             remove_worm(othermon);
2578         else
2579             remove_monster(x, y);
2580     }
2581
2582     newx = x;
2583     newy = y;
2584     if (!goodpos(newx, newy, mtmp, 0)) {
2585         /* Actually we have real problems if enexto ever fails.
2586          * Migrating_mons that need to be placed will cause
2587          * no end of trouble.
2588          */
2589         if (!enexto(&mm, newx, newy, mtmp->data))
2590             return FALSE;
2591         newx = mm.x;
2592         newy = mm.y;
2593     }
2594     rloc_to(mtmp, newx, newy);
2595
2596     if (move_other && othermon) {
2597         xchar oldx = othermon->mx, oldy = othermon->my;
2598
2599         othermon->mx = othermon->my = 0;
2600         (void) mnearto(othermon, x, y, FALSE);
2601         if (othermon->mx == 0 && othermon->my == 0) {
2602             /* reloc failed, dump monster into "limbo"
2603                (aka migrate to current level) */
2604             othermon->mx = oldx;
2605             othermon->my = oldy;
2606             mdrop_special_objs(othermon);
2607             migrate_to_level(othermon, ledger_no(&u.uz), MIGR_APPROX_XY, NULL);
2608         }
2609     }
2610
2611     return FALSE;
2612 }
2613
2614 /* monster responds to player action; not the same as a passive attack;
2615    assumes reason for response has been tested, and response _must_ be made */
2616 void
2617 m_respond(mtmp)
2618 struct monst *mtmp;
2619 {
2620     if (mtmp->data->msound == MS_SHRIEK) {
2621         if (!Deaf) {
2622 /*JP
2623             pline("%s shrieks.", Monnam(mtmp));
2624 */
2625             pline("%s\82Í\8bà\90Ø\82è\90º\82ð\82 \82°\82½\81D", Monnam(mtmp));
2626             stop_occupation();
2627         }
2628         if (!rn2(10)) {
2629             if (!rn2(13))
2630                 (void) makemon(&mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
2631             else
2632                 (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
2633         }
2634         aggravate();
2635     }
2636     if (mtmp->data == &mons[PM_MEDUSA]) {
2637         register int i;
2638
2639         for (i = 0; i < NATTK; i++)
2640             if (mtmp->data->mattk[i].aatyp == AT_GAZE) {
2641                 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
2642                 break;
2643             }
2644     }
2645 }
2646
2647 void
2648 setmangry(mtmp)
2649 struct monst *mtmp;
2650 {
2651     mtmp->mstrategy &= ~STRAT_WAITMASK;
2652     if (!mtmp->mpeaceful)
2653         return;
2654     if (mtmp->mtame)
2655         return;
2656     mtmp->mpeaceful = 0;
2657     if (mtmp->ispriest) {
2658         if (p_coaligned(mtmp))
2659             adjalign(-5); /* very bad */
2660         else
2661             adjalign(2);
2662     } else
2663         adjalign(-1); /* attacking peaceful monsters is bad */
2664     if (couldsee(mtmp->mx, mtmp->my)) {
2665         if (humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
2666 /*JP
2667             pline("%s gets angry!", Monnam(mtmp));
2668 */
2669             pline("%s\82Í\93{\82Á\82½\81I", Monnam(mtmp));
2670         else if (flags.verbose && !Deaf)
2671             growl(mtmp);
2672     }
2673
2674     /* attacking your own quest leader will anger his or her guardians */
2675     if (!context.mon_moving /* should always be the case here */
2676         && mtmp->data == &mons[quest_info(MS_LEADER)]) {
2677         struct monst *mon;
2678         struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
2679         int got_mad = 0;
2680
2681         /* guardians will sense this attack even if they can't see it */
2682         for (mon = fmon; mon; mon = mon->nmon) {
2683             if (DEADMONSTER(mon))
2684                 continue;
2685             if (mon->data == q_guardian && mon->mpeaceful) {
2686                 mon->mpeaceful = 0;
2687                 if (canseemon(mon))
2688                     ++got_mad;
2689             }
2690         }
2691         if (got_mad && !Hallucination)
2692 #if 0 /*JP*/
2693             pline_The("%s appear%s to be angry too...",
2694                       got_mad == 1 ? q_guardian->mname
2695                                    : makeplural(q_guardian->mname),
2696                       got_mad == 1 ? "s" : "");
2697 #else
2698             pline("%s\82à\93{\82Á\82½\81D\81D\81D", q_guardian->mname);
2699 #endif
2700     }
2701 }
2702
2703 /* wake up a monster, usually making it angry in the process */
2704 void
2705 wakeup(mtmp)
2706 register struct monst *mtmp;
2707 {
2708     mtmp->msleeping = 0;
2709     finish_meating(mtmp);
2710     setmangry(mtmp);
2711     if (mtmp->m_ap_type) {
2712         seemimic(mtmp);
2713     } else if (context.forcefight && !context.mon_moving
2714                && mtmp->mundetected) {
2715         mtmp->mundetected = 0;
2716         newsym(mtmp->mx, mtmp->my);
2717     }
2718 }
2719
2720 /* Wake up nearby monsters without angering them. */
2721 void
2722 wake_nearby()
2723 {
2724     register struct monst *mtmp;
2725
2726     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2727         if (DEADMONSTER(mtmp))
2728             continue;
2729         if (distu(mtmp->mx, mtmp->my) < u.ulevel * 20) {
2730             mtmp->msleeping = 0;
2731             if (!unique_corpstat(mtmp->data))
2732                 mtmp->mstrategy &= ~STRAT_WAITMASK;
2733             if (mtmp->mtame && !mtmp->isminion)
2734                 EDOG(mtmp)->whistletime = moves;
2735         }
2736     }
2737 }
2738
2739 /* Wake up monsters near some particular location. */
2740 void
2741 wake_nearto(x, y, distance)
2742 register int x, y, distance;
2743 {
2744     register struct monst *mtmp;
2745
2746     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2747         if (DEADMONSTER(mtmp))
2748             continue;
2749         if (distance == 0 || dist2(mtmp->mx, mtmp->my, x, y) < distance) {
2750             mtmp->msleeping = 0;
2751             if (!unique_corpstat(mtmp->data))
2752                 mtmp->mstrategy &= ~STRAT_WAITMASK;
2753         }
2754     }
2755 }
2756
2757 /* NOTE: we must check for mimicry before calling this routine */
2758 void
2759 seemimic(mtmp)
2760 register struct monst *mtmp;
2761 {
2762     boolean is_blocker_appear = (is_door_mappear(mtmp)
2763                                  || is_obj_mappear(mtmp, BOULDER));
2764
2765     if (has_mcorpsenm(mtmp))
2766         freemcorpsenm(mtmp);
2767
2768     mtmp->m_ap_type = M_AP_NOTHING;
2769     mtmp->mappearance = 0;
2770
2771     /*
2772      *  Discovered mimics don't block light.
2773      */
2774     if (is_blocker_appear
2775         && !does_block(mtmp->mx, mtmp->my, &levl[mtmp->mx][mtmp->my]))
2776         unblock_point(mtmp->mx, mtmp->my);
2777
2778     newsym(mtmp->mx, mtmp->my);
2779 }
2780
2781 /* force all chameleons to become normal */
2782 void
2783 rescham()
2784 {
2785     register struct monst *mtmp;
2786     int mcham;
2787
2788     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2789         if (DEADMONSTER(mtmp))
2790             continue;
2791         mcham = (int) mtmp->cham;
2792         if (mcham >= LOW_PM) {
2793             (void) newcham(mtmp, &mons[mcham], FALSE, FALSE);
2794             mtmp->cham = NON_PM;
2795         }
2796         if (is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
2797             new_were(mtmp);
2798         if (mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
2799             seemimic(mtmp);
2800             /* we pretend that the mimic doesn't
2801                know that it has been unmasked */
2802             mtmp->msleeping = 1;
2803         }
2804     }
2805 }
2806
2807 /* Let the chameleons change again -dgk */
2808 void
2809 restartcham()
2810 {
2811     register struct monst *mtmp;
2812
2813     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2814         if (DEADMONSTER(mtmp))
2815             continue;
2816         mtmp->cham = pm_to_cham(monsndx(mtmp->data));
2817         if (mtmp->data->mlet == S_MIMIC && mtmp->msleeping
2818             && cansee(mtmp->mx, mtmp->my)) {
2819             set_mimic_sym(mtmp);
2820             newsym(mtmp->mx, mtmp->my);
2821         }
2822     }
2823 }
2824
2825 /* called when restoring a monster from a saved level; protection
2826    against shape-changing might be different now than it was at the
2827    time the level was saved. */
2828 void
2829 restore_cham(mon)
2830 struct monst *mon;
2831 {
2832     int mcham;
2833
2834     if (Protection_from_shape_changers) {
2835         mcham = (int) mon->cham;
2836         if (mcham >= LOW_PM) {
2837             mon->cham = NON_PM;
2838             (void) newcham(mon, &mons[mcham], FALSE, FALSE);
2839         } else if (is_were(mon->data) && !is_human(mon->data)) {
2840             new_were(mon);
2841         }
2842     } else if (mon->cham == NON_PM) {
2843         mon->cham = pm_to_cham(monsndx(mon->data));
2844     }
2845 }
2846
2847 /* unwatched hiders may hide again; if so, returns True */
2848 STATIC_OVL boolean
2849 restrap(mtmp)
2850 register struct monst *mtmp;
2851 {
2852     struct trap *t;
2853
2854     if (mtmp->mcan || mtmp->m_ap_type || cansee(mtmp->mx, mtmp->my)
2855         || rn2(3) || mtmp == u.ustuck
2856         /* can't hide while trapped except in pits */
2857         || (mtmp->mtrapped && (t = t_at(mtmp->mx, mtmp->my)) != 0
2858             && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))
2859         || (sensemon(mtmp) && distu(mtmp->mx, mtmp->my) <= 2))
2860         return FALSE;
2861
2862     if (mtmp->data->mlet == S_MIMIC) {
2863         set_mimic_sym(mtmp);
2864         return TRUE;
2865     } else if (levl[mtmp->mx][mtmp->my].typ == ROOM) {
2866         mtmp->mundetected = 1;
2867         return TRUE;
2868     }
2869
2870     return FALSE;
2871 }
2872
2873 /* monster/hero tries to hide under something at the current location */
2874 boolean
2875 hideunder(mtmp)
2876 struct monst *mtmp;
2877 {
2878     struct trap *t;
2879     boolean undetected = FALSE, is_u = (mtmp == &youmonst);
2880     xchar x = is_u ? u.ux : mtmp->mx, y = is_u ? u.uy : mtmp->my;
2881
2882     if (mtmp == u.ustuck) {
2883         ; /* can't hide if holding you or held by you */
2884     } else if (is_u ? (u.utrap && u.utraptype != TT_PIT)
2885                     : (mtmp->mtrapped && (t = t_at(x, y)) != 0
2886                        && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))) {
2887         ; /* can't hide while stuck in a non-pit trap */
2888     } else if (mtmp->data->mlet == S_EEL) {
2889         undetected = (is_pool(x, y) && !Is_waterlevel(&u.uz));
2890     } else if (hides_under(mtmp->data) && OBJ_AT(x, y)) {
2891         struct obj *otmp = level.objects[x][y];
2892
2893         /* most monsters won't hide under cockatrice corpse */
2894         if (otmp->nexthere || otmp->otyp != CORPSE
2895             || (mtmp == &youmonst ? Stone_resistance : resists_ston(mtmp))
2896             || !touch_petrifies(&mons[otmp->corpsenm]))
2897             undetected = TRUE;
2898     }
2899
2900     if (is_u)
2901         u.uundetected = undetected;
2902     else
2903         mtmp->mundetected = undetected;
2904     return undetected;
2905 }
2906
2907 /* called when returning to a previously visited level */
2908 void
2909 hide_monst(mon)
2910 struct monst *mon;
2911 {
2912     boolean hider_under = hides_under(mon->data) || mon->data->mlet == S_EEL;
2913
2914     if ((is_hider(mon->data) || hider_under)
2915         && !(mon->mundetected || mon->m_ap_type)) {
2916         xchar x = mon->mx, y = mon->my;
2917         char save_viz = viz_array[y][x];
2918
2919         /* override vision, forcing hero to be unable to see monster's spot */
2920         viz_array[y][x] &= ~(IN_SIGHT | COULD_SEE);
2921         if (is_hider(mon->data))
2922             (void) restrap(mon);
2923         /* try again if mimic missed its 1/3 chance to hide */
2924         if (mon->data->mlet == S_MIMIC && !mon->m_ap_type)
2925             (void) restrap(mon);
2926         if (hider_under)
2927             (void) hideunder(mon);
2928         viz_array[y][x] = save_viz;
2929     }
2930 }
2931
2932 static short *animal_list = 0; /* list of PM values for animal monsters */
2933 static int animal_list_count;
2934
2935 void
2936 mon_animal_list(construct)
2937 boolean construct;
2938 {
2939     if (construct) {
2940         short animal_temp[SPECIAL_PM];
2941         int i, n;
2942
2943         /* if (animal_list) impossible("animal_list already exists"); */
2944
2945         for (n = 0, i = LOW_PM; i < SPECIAL_PM; i++)
2946             if (is_animal(&mons[i]))
2947                 animal_temp[n++] = i;
2948         /* if (n == 0) animal_temp[n++] = NON_PM; */
2949
2950         animal_list = (short *) alloc(n * sizeof *animal_list);
2951         (void) memcpy((genericptr_t) animal_list, (genericptr_t) animal_temp,
2952                       n * sizeof *animal_list);
2953         animal_list_count = n;
2954     } else { /* release */
2955         if (animal_list)
2956             free((genericptr_t) animal_list), animal_list = 0;
2957         animal_list_count = 0;
2958     }
2959 }
2960
2961 STATIC_OVL int
2962 pick_animal()
2963 {
2964     int res;
2965
2966     if (!animal_list)
2967         mon_animal_list(TRUE);
2968
2969     res = animal_list[rn2(animal_list_count)];
2970     /* rogue level should use monsters represented by uppercase letters
2971        only, but since chameleons aren't generated there (not uppercase!)
2972        we don't perform a lot of retries */
2973     if (Is_rogue_level(&u.uz) && !isupper((uchar) mons[res].mlet))
2974         res = animal_list[rn2(animal_list_count)];
2975     return res;
2976 }
2977
2978 void
2979 decide_to_shapeshift(mon, shiftflags)
2980 struct monst *mon;
2981 int shiftflags;
2982 {
2983     struct permonst *ptr;
2984     unsigned was_female = mon->female;
2985     boolean msg = FALSE;
2986
2987     if ((shiftflags & SHIFT_MSG)
2988         || ((shiftflags & SHIFT_SEENMSG) && sensemon(mon)))
2989         msg = TRUE;
2990
2991     if (is_vampshifter(mon)) {
2992         /* The vampire has to be in good health (mhp) to maintain
2993          * its shifted form.
2994              *
2995          * If we're shifted and getting low on hp, maybe shift back.
2996          * If we're not already shifted and in good health, maybe shift.
2997          */
2998         if ((mon->mhp <= mon->mhpmax / 6) && rn2(4) && (mon->cham >= LOW_PM))
2999             (void) newcham(mon, &mons[mon->cham], FALSE, msg);
3000     } else if (mon->data->mlet == S_VAMPIRE && mon->cham == NON_PM && !rn2(6)
3001                && (mon->mhp > mon->mhpmax - ((mon->mhpmax / 10) + 1))) {
3002         (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
3003     }
3004     /* override the 10% chance for sex change */
3005     ptr = mon->data;
3006     if (!is_male(ptr) && !is_female(ptr) && !is_neuter(ptr))
3007         mon->female = was_female;
3008 }
3009
3010 STATIC_OVL int
3011 pickvampshape(mon)
3012 struct monst *mon;
3013 {
3014     int mndx = mon->cham;
3015     /* avoid picking monsters with lowercase display symbols ('d' for wolf
3016        and 'v' for fog cloud) on rogue level*/
3017     boolean uppercase_only = Is_rogue_level(&u.uz);
3018
3019     switch (mndx) {
3020     case PM_VLAD_THE_IMPALER:
3021         /* ensure Vlad can keep carrying the Candelabrum */
3022         if (mon_has_special(mon))
3023             break; /* leave mndx as is */
3024     /*FALLTHRU*/
3025     case PM_VAMPIRE_LORD: /* vampire lord or Vlad can become wolf */
3026         if (!rn2(10) && !uppercase_only) {
3027             mndx = PM_WOLF;
3028             break;
3029         }
3030     /*FALLTHRU*/
3031     case PM_VAMPIRE: /* any vampire can become fog or bat */
3032         mndx = (!rn2(4) && !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
3033         break;
3034     }
3035     return mndx;
3036 }
3037
3038 /* nonshapechangers who warrant special polymorph handling */
3039 STATIC_OVL boolean
3040 isspecmon(mon)
3041 struct monst *mon;
3042 {
3043     return (mon->isshk || mon->ispriest || mon->isgd
3044             || mon->m_id == quest_status.leader_m_id);
3045 }
3046
3047 /* restrict certain special monsters (shopkeepers, aligned priests,
3048    vault guards) to forms that allow them to behave sensibly (catching
3049    gold, speaking?) so that they don't need too much extra code */
3050 STATIC_OVL boolean
3051 validspecmon(mon, mndx)
3052 struct monst *mon;
3053 int mndx;
3054 {
3055     if (mndx == NON_PM)
3056         return TRUE; /* caller wants random */
3057
3058     if (!accept_newcham_form(mndx))
3059         return FALSE; /* geno'd or !polyok */
3060
3061     if (isspecmon(mon)) {
3062         struct permonst *ptr = &mons[mndx];
3063
3064         /* reject notake because object manipulation is expected
3065            and nohead because speech capability is expected */
3066         if (notake(ptr) || !has_head(ptr))
3067             return FALSE;
3068         /* [should we check ptr->msound here too?] */
3069     }
3070     return TRUE; /* potential new form is ok */
3071 }
3072
3073 /* prevent wizard mode user from specifying invalid vampshifter shape */
3074 STATIC_OVL boolean
3075 validvamp(mon, mndx_p, monclass)
3076 struct monst *mon;
3077 int *mndx_p, monclass;
3078 {
3079     /* simplify caller's usage */
3080     if (!is_vampshifter(mon))
3081         return validspecmon(mon, *mndx_p);
3082
3083     if (*mndx_p == PM_VAMPIRE || *mndx_p == PM_VAMPIRE_LORD
3084         || *mndx_p == PM_VLAD_THE_IMPALER) {
3085         /* player picked some type of vampire; use mon's self */
3086         *mndx_p = mon->cham;
3087         return TRUE;
3088     }
3089     if (mon->cham == PM_VLAD_THE_IMPALER && mon_has_special(mon)) {
3090         /* Vlad with Candelabrum; override choice, then accept it */
3091         *mndx_p = PM_VLAD_THE_IMPALER;
3092         return TRUE;
3093     }
3094     /* basic vampires can't become wolves; any can become fog or bat
3095        (we don't enforce upper-case only for rogue level here) */
3096     if (*mndx_p == PM_WOLF)
3097         return (boolean) (mon->cham != PM_VAMPIRE);
3098     if (*mndx_p == PM_FOG_CLOUD || *mndx_p == PM_VAMPIRE_BAT)
3099         return TRUE;
3100
3101     /* if we get here, specific type was no good; try by class */
3102     switch (monclass) {
3103     case S_VAMPIRE:
3104         *mndx_p = mon->cham;
3105         break;
3106     case S_BAT:
3107         *mndx_p = PM_VAMPIRE_BAT;
3108         break;
3109     case S_VORTEX:
3110         *mndx_p = PM_FOG_CLOUD;
3111         break;
3112     case S_DOG:
3113         if (mon->cham != PM_VAMPIRE) {
3114             *mndx_p = PM_WOLF;
3115             break;
3116         }
3117     /*FALLTHRU*/
3118     default:
3119         *mndx_p = NON_PM;
3120         break;
3121     }
3122     return (boolean) (*mndx_p != NON_PM);
3123 }
3124
3125 int
3126 select_newcham_form(mon)
3127 struct monst *mon;
3128 {
3129     int mndx = NON_PM, tryct;
3130
3131     switch (mon->cham) {
3132     case PM_SANDESTIN:
3133         if (rn2(7))
3134             mndx = pick_nasty();
3135         break;
3136     case PM_DOPPELGANGER:
3137         if (!rn2(7)) {
3138             mndx = pick_nasty();
3139         } else if (rn2(3)) { /* role monsters */
3140             mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
3141         } else if (!rn2(3)) { /* quest guardians */
3142             mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
3143             /* avoid own role's guardian */
3144             if (mndx == urole.guardnum)
3145                 mndx = NON_PM;
3146         } else { /* general humanoids */
3147             tryct = 5;
3148             do {
3149                 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3150                 if (humanoid(&mons[mndx]) && polyok(&mons[mndx]))
3151                     break;
3152             } while (--tryct > 0);
3153             if (!tryct)
3154                 mndx = NON_PM;
3155         }
3156         break;
3157     case PM_CHAMELEON:
3158         if (!rn2(3))
3159             mndx = pick_animal();
3160         break;
3161     case PM_VLAD_THE_IMPALER:
3162     case PM_VAMPIRE_LORD:
3163     case PM_VAMPIRE:
3164         mndx = pickvampshape(mon);
3165         break;
3166     case NON_PM: /* ordinary */
3167       {
3168         struct obj *m_armr = which_armor(mon, W_ARM);
3169
3170         if (m_armr && Is_dragon_scales(m_armr))
3171             mndx = (int) (Dragon_scales_to_pm(m_armr) - mons);
3172         else if (m_armr && Is_dragon_mail(m_armr))
3173             mndx = (int) (Dragon_mail_to_pm(m_armr) - mons);
3174       }
3175         break;
3176     }
3177     /* for debugging: allow control of polymorphed monster */
3178     if (wizard && iflags.mon_polycontrol) {
3179         char pprompt[BUFSZ], buf[BUFSZ];
3180         int monclass;
3181
3182         Sprintf(pprompt, "Change %s @ <%d,%d> into what kind of monster?",
3183                 noit_mon_nam(mon), (int) mon->mx, (int) mon->my);
3184         tryct = 5;
3185         do {
3186             monclass = 0;
3187             getlin(pprompt, buf);
3188             mungspaces(buf);
3189             /* for ESC, take form selected above (might be NON_PM) */
3190             if (*buf == '\033')
3191                 break;
3192             /* for "*", use NON_PM to pick an arbitrary shape below */
3193             if (!strcmp(buf, "*") || !strcmp(buf, "random")) {
3194                 mndx = NON_PM;
3195                 break;
3196             }
3197             mndx = name_to_mon(buf);
3198             if (mndx == NON_PM) {
3199                 /* didn't get a type, so check whether it's a class
3200                    (single letter or text match with def_monsyms[]) */
3201                 monclass = name_to_monclass(buf, &mndx);
3202                 if (monclass && mndx == NON_PM)
3203                     mndx = mkclass_poly(monclass);
3204             }
3205             if (mndx >= LOW_PM) {
3206                 /* got a specific type of monster; use it if we can */
3207                 if (validvamp(mon, &mndx, monclass))
3208                     break;
3209                 /* can't; revert to random in case we exhaust tryct */
3210                 mndx = NON_PM;
3211             }
3212
3213             pline("It can't become that.");
3214         } while (--tryct > 0);
3215         if (!tryct)
3216             pline1(thats_enough_tries);
3217         if (is_vampshifter(mon) && !validvamp(mon, &mndx, monclass))
3218             mndx = pickvampshape(mon); /* don't resort to arbitrary */
3219     }
3220
3221     /* if no form was specified above, pick one at random now */
3222     if (mndx == NON_PM) {
3223         tryct = 50;
3224         do {
3225             mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3226         } while (--tryct > 0 && !validspecmon(mon, mndx)
3227                  /* try harder to select uppercase monster on rogue level */
3228                  && (tryct > 40 && Is_rogue_level(&u.uz)
3229                      && !isupper((uchar) mons[mndx].mlet)));
3230     }
3231     return mndx;
3232 }
3233
3234 /* this used to be inline within newcham() but monpolycontrol needs it too */
3235 STATIC_OVL struct permonst *
3236 accept_newcham_form(mndx)
3237 int mndx;
3238 {
3239     struct permonst *mdat;
3240
3241     if (mndx == NON_PM)
3242         return 0;
3243     mdat = &mons[mndx];
3244     if ((mvitals[mndx].mvflags & G_GENOD) != 0)
3245         return 0;
3246     if (is_placeholder(mdat))
3247         return 0;
3248     /* select_newcham_form() might deliberately pick a player
3249        character type (random selection never does) which
3250        polyok() rejects, so we need a special case here */
3251     if (is_mplayer(mdat))
3252         return mdat;
3253     /* polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops */
3254     return polyok(mdat) ? mdat : 0;
3255 }
3256
3257 void
3258 mgender_from_permonst(mtmp, mdat)
3259 struct monst *mtmp;
3260 struct permonst *mdat;
3261 {
3262     if (is_male(mdat)) {
3263         if (mtmp->female)
3264             mtmp->female = FALSE;
3265     } else if (is_female(mdat)) {
3266         if (!mtmp->female)
3267             mtmp->female = TRUE;
3268     } else if (!is_neuter(mdat)) {
3269         if (!rn2(10))
3270             mtmp->female = !mtmp->female;
3271     }
3272 }
3273
3274 /* make a chameleon take on another shape, or a polymorph target
3275    (possibly self-inflicted) become a different monster;
3276    returns 1 if it actually changes form */
3277 int
3278 newcham(mtmp, mdat, polyspot, msg)
3279 struct monst *mtmp;
3280 struct permonst *mdat;
3281 boolean polyspot; /* change is the result of wand or spell of polymorph */
3282 boolean msg;      /* "The oldmon turns into a newmon!" */
3283 {
3284     int hpn, hpd;
3285     int mndx, tryct;
3286     struct permonst *olddata = mtmp->data;
3287     char oldname[BUFSZ], newname[BUFSZ];
3288
3289     /* Riders are immune to polymorph and green slime */
3290     if (is_rider(mtmp->data))
3291         return 0;
3292
3293     if (msg) {
3294         /* like Monnam() but never mention saddle */
3295         Strcpy(oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3296                                  SUPPRESS_SADDLE, FALSE));
3297         oldname[0] = highc(oldname[0]);
3298     }
3299
3300     /* mdat = 0 -> caller wants a random monster shape */
3301     if (mdat == 0) {
3302         /* select_newcham_form() loops when resorting to random but
3303            it doesn't always pick that so we still retry here too */
3304         tryct = 20;
3305         do {
3306             mndx = select_newcham_form(mtmp);
3307             mdat = accept_newcham_form(mndx);
3308             /* for the first several tries we require upper-case on
3309                the rogue level (after that, we take whatever we get) */
3310             if (tryct > 15 && Is_rogue_level(&u.uz)
3311                 && mdat && !isupper((uchar) mdat->mlet))
3312                 mdat = 0;
3313             if (mdat)
3314                 break;
3315         } while (--tryct > 0);
3316         if (!tryct)
3317             return 0;
3318     } else if (mvitals[monsndx(mdat)].mvflags & G_GENOD)
3319         return 0; /* passed in mdat is genocided */
3320
3321     mgender_from_permonst(mtmp, mdat);
3322
3323     if (In_endgame(&u.uz) && is_mplayer(olddata) && has_mname(mtmp)) {
3324         /* mplayers start out as "Foo the Bar", but some of the
3325          * titles are inappropriate when polymorphed, particularly
3326          * into the opposite sex.  players don't use ranks when
3327          * polymorphed, so dropping the rank for mplayers seems
3328          * reasonable.
3329          */
3330         char *p = index(MNAME(mtmp), ' ');
3331
3332         if (p)
3333             *p = '\0';
3334     }
3335
3336     if (mdat == mtmp->data)
3337         return 0; /* still the same monster */
3338
3339     if (mtmp->wormno) { /* throw tail away */
3340         wormgone(mtmp);
3341         place_monster(mtmp, mtmp->mx, mtmp->my);
3342     }
3343     if (mtmp->m_ap_type && mdat->mlet != S_MIMIC)
3344         seemimic(mtmp); /* revert to normal monster */
3345
3346     /* (this code used to try to adjust the monster's health based on
3347        a normal one of its type but there are too many special cases
3348        which need to handled in order to do that correctly, so just
3349        give the new form the same proportion of HP as its old one had) */
3350     hpn = mtmp->mhp;
3351     hpd = mtmp->mhpmax;
3352     /* set level and hit points */
3353     newmonhp(mtmp, monsndx(mdat));
3354     /* new hp: same fraction of max as before */
3355 #ifndef LINT
3356     mtmp->mhp = (int) (((long) hpn * (long) mtmp->mhp) / (long) hpd);
3357 #endif
3358     /* sanity check (potential overflow) */
3359     if (mtmp->mhp < 0 || mtmp->mhp > mtmp->mhpmax)
3360         mtmp->mhp = mtmp->mhpmax;
3361     /* unlikely but not impossible; a 1HD creature with 1HP that changes
3362        into a 0HD creature will require this statement */
3363     if (!mtmp->mhp)
3364         mtmp->mhp = 1;
3365
3366     /* take on the new form... */
3367     set_mon_data(mtmp, mdat, 0);
3368
3369     if (emits_light(olddata) != emits_light(mtmp->data)) {
3370         /* used to give light, now doesn't, or vice versa,
3371            or light's range has changed */
3372         if (emits_light(olddata))
3373             del_light_source(LS_MONSTER, monst_to_any(mtmp));
3374         if (emits_light(mtmp->data))
3375             new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
3376                              LS_MONSTER, monst_to_any(mtmp));
3377     }
3378     if (!mtmp->perminvis || pm_invisible(olddata))
3379         mtmp->perminvis = pm_invisible(mdat);
3380     mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
3381     if (mtmp->mundetected)
3382         (void) hideunder(mtmp);
3383     if (u.ustuck == mtmp) {
3384         if (u.uswallow) {
3385             if (!attacktype(mdat, AT_ENGL)) {
3386                 /* Does mdat care? */
3387                 if (!noncorporeal(mdat) && !amorphous(mdat)
3388                     && !is_whirly(mdat) && (mdat != &mons[PM_YELLOW_LIGHT])) {
3389 #if 0 /*JP*/
3390                     You("break out of %s%s!", mon_nam(mtmp),
3391                         (is_animal(mdat) ? "'s stomach" : ""));
3392 #else
3393                     You("%s%s\82ð\94j\82è\8fo\82½\81I", mon_nam(mtmp),
3394                         (is_animal(mdat) ? "\82Ì\88Ý\91Ü" : ""));
3395 #endif
3396                     mtmp->mhp = 1; /* almost dead */
3397                 }
3398                 expels(mtmp, olddata, FALSE);
3399             } else {
3400                 /* update swallow glyphs for new monster */
3401                 swallowed(0);
3402             }
3403         } else if (!sticks(mdat) && !sticks(youmonst.data))
3404             unstuck(mtmp);
3405     }
3406
3407 #ifndef DCC30_BUG
3408     if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
3409 #else
3410     /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
3411      * same expression.
3412      */
3413     if (mdat == &mons[PM_LONG_WORM]
3414         && (mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
3415 #endif
3416         /* we can now create worms with tails - 11/91 */
3417         initworm(mtmp, rn2(5));
3418         if (count_wsegs(mtmp))
3419             place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
3420     }
3421
3422     newsym(mtmp->mx, mtmp->my);
3423
3424     if (msg) {
3425         char *save_mname = 0;
3426
3427         if (has_mname(mtmp)) {
3428             save_mname = MNAME(mtmp);
3429             MNAME(mtmp) = (char *) 0;
3430         }
3431 #if 0 /*JP*/
3432         Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3433                             ? "slime"
3434                             : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3435                                        SUPPRESS_SADDLE, FALSE));
3436 #else
3437         Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3438                             ? "\83X\83\89\83C\83\80"
3439                             : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3440                                        SUPPRESS_SADDLE, FALSE));
3441 #endif
3442 /*JP
3443         if (!strcmpi(oldname, "it") && !strcmpi(newname, "it"))
3444 */
3445         if (!strcmpi(oldname, "\89½\8eÒ\82©") && !strcmpi(newname, "\89½\8eÒ\82©"))
3446             (void) usmellmon(mdat);
3447         else
3448 /*JP
3449             pline("%s turns into %s!", oldname, newname);
3450 */
3451             pline("%s\82Í%s\82É\82È\82Á\82½\81I", oldname, newname);
3452         if (save_mname)
3453             MNAME(mtmp) = save_mname;
3454     }
3455
3456     possibly_unwield(mtmp, polyspot); /* might lose use of weapon */
3457     mon_break_armor(mtmp, polyspot);
3458     if (!(mtmp->misc_worn_check & W_ARMG))
3459 #if 0 /*JP*/
3460         mselftouch(mtmp, "No longer petrify-resistant, ",
3461                    !context.mon_moving);
3462 #else
3463         mselftouch(mtmp, "\90Î\89»\82Ö\82Ì\92ï\8dR\97Í\82ª\82È\82­\82È\82Á\82Ä\81C",
3464                    !context.mon_moving);
3465 #endif
3466     m_dowear(mtmp, FALSE);
3467
3468     /* This ought to re-test can_carry() on each item in the inventory
3469      * rather than just checking ex-giants & boulders, but that'd be
3470      * pretty expensive to perform.  If implemented, then perhaps
3471      * minvent should be sorted in order to drop heaviest items first.
3472      */
3473     /* former giants can't continue carrying boulders */
3474     if (mtmp->minvent && !throws_rocks(mdat)) {
3475         register struct obj *otmp, *otmp2;
3476
3477         for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
3478             otmp2 = otmp->nobj;
3479             if (otmp->otyp == BOULDER) {
3480                 /* this keeps otmp from being polymorphed in the
3481                    same zap that the monster that held it is polymorphed */
3482                 if (polyspot)
3483                     bypass_obj(otmp);
3484                 obj_extract_self(otmp);
3485                 /* probably ought to give some "drop" message here */
3486                 if (flooreffects(otmp, mtmp->mx, mtmp->my, ""))
3487                     continue;
3488                 place_object(otmp, mtmp->mx, mtmp->my);
3489             }
3490         }
3491     }
3492
3493     return 1;
3494 }
3495
3496 /* sometimes an egg will be special */
3497 #define BREEDER_EGG (!rn2(77))
3498
3499 /*
3500  * Determine if the given monster number can be hatched from an egg.
3501  * Return the monster number to use as the egg's corpsenm.  Return
3502  * NON_PM if the given monster can't be hatched.
3503  */
3504 int
3505 can_be_hatched(mnum)
3506 int mnum;
3507 {
3508     /* ranger quest nemesis has the oviparous bit set, making it
3509        be possible to wish for eggs of that unique monster; turn
3510        such into ordinary eggs rather than forbidding them outright */
3511     if (mnum == PM_SCORPIUS)
3512         mnum = PM_SCORPION;
3513
3514     mnum = little_to_big(mnum);
3515     /*
3516      * Queen bees lay killer bee eggs (usually), but killer bees don't
3517      * grow into queen bees.  Ditto for [winged-]gargoyles.
3518      */
3519     if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE
3520         || (lays_eggs(&mons[mnum])
3521             && (BREEDER_EGG
3522                 || (mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
3523         return mnum;
3524     return NON_PM;
3525 }
3526
3527 /* type of egg laid by #sit; usually matches parent */
3528 int
3529 egg_type_from_parent(mnum, force_ordinary)
3530 int mnum; /* parent monster; caller must handle lays_eggs() check */
3531 boolean force_ordinary;
3532 {
3533     if (force_ordinary || !BREEDER_EGG) {
3534         if (mnum == PM_QUEEN_BEE)
3535             mnum = PM_KILLER_BEE;
3536         else if (mnum == PM_WINGED_GARGOYLE)
3537             mnum = PM_GARGOYLE;
3538     }
3539     return mnum;
3540 }
3541
3542 /* decide whether an egg of the indicated monster type is viable;
3543    also used to determine whether an egg or tin can be created... */
3544 boolean
3545 dead_species(m_idx, egg)
3546 int m_idx;
3547 boolean egg;
3548 {
3549     int alt_idx;
3550
3551     /* generic eggs are unhatchable and have corpsenm of NON_PM */
3552     if (m_idx < LOW_PM)
3553         return TRUE;
3554     /*
3555      * For monsters with both baby and adult forms, genociding either
3556      * form kills all eggs of that monster.  Monsters with more than
3557      * two forms (small->large->giant mimics) are more or less ignored;
3558      * fortunately, none of them have eggs.  Species extinction due to
3559      * overpopulation does not kill eggs.
3560      */
3561     alt_idx = egg ? big_to_little(m_idx) : m_idx;
3562     return (boolean) ((mvitals[m_idx].mvflags & G_GENOD) != 0
3563                       || (mvitals[alt_idx].mvflags & G_GENOD) != 0);
3564 }
3565
3566 /* kill off any eggs of genocided monsters */
3567 STATIC_OVL void
3568 kill_eggs(obj_list)
3569 struct obj *obj_list;
3570 {
3571     struct obj *otmp;
3572
3573     for (otmp = obj_list; otmp; otmp = otmp->nobj)
3574         if (otmp->otyp == EGG) {
3575             if (dead_species(otmp->corpsenm, TRUE)) {
3576                 /*
3577                  * It seems we could also just catch this when
3578                  * it attempted to hatch, so we wouldn't have to
3579                  * search all of the objlists.. or stop all
3580                  * hatch timers based on a corpsenm.
3581                  */
3582                 kill_egg(otmp);
3583             }
3584 #if 0 /* not used */
3585         } else if (otmp->otyp == TIN) {
3586             if (dead_species(otmp->corpsenm, FALSE))
3587                 otmp->corpsenm = NON_PM; /* empty tin */
3588         } else if (otmp->otyp == CORPSE) {
3589             if (dead_species(otmp->corpsenm, FALSE))
3590                 ; /* not yet implemented... */
3591 #endif
3592         } else if (Has_contents(otmp)) {
3593             kill_eggs(otmp->cobj);
3594         }
3595 }
3596
3597 /* kill all members of genocided species */
3598 void
3599 kill_genocided_monsters()
3600 {
3601     struct monst *mtmp, *mtmp2;
3602     boolean kill_cham;
3603     int mndx;
3604
3605     /*
3606      * Called during genocide, and again upon level change.  The latter
3607      * catches up with any migrating monsters as they finally arrive at
3608      * their intended destinations, so possessions get deposited there.
3609      *
3610      * Chameleon handling:
3611      *  1) if chameleons have been genocided, destroy them
3612      *     regardless of current form;
3613      *  2) otherwise, force every chameleon which is imitating
3614      *     any genocided species to take on a new form.
3615      */
3616     for (mtmp = fmon; mtmp; mtmp = mtmp2) {
3617         mtmp2 = mtmp->nmon;
3618         if (DEADMONSTER(mtmp))
3619             continue;
3620         mndx = monsndx(mtmp->data);
3621         kill_cham = (mtmp->cham >= LOW_PM
3622                      && (mvitals[mtmp->cham].mvflags & G_GENOD));
3623         if ((mvitals[mndx].mvflags & G_GENOD) || kill_cham) {
3624             if (mtmp->cham >= LOW_PM && !kill_cham)
3625                 (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
3626             else
3627                 mondead(mtmp);
3628         }
3629         if (mtmp->minvent)
3630             kill_eggs(mtmp->minvent);
3631     }
3632
3633     kill_eggs(invent);
3634     kill_eggs(fobj);
3635     kill_eggs(migrating_objs);
3636     kill_eggs(level.buriedobjlist);
3637 }
3638
3639 void
3640 golemeffects(mon, damtype, dam)
3641 register struct monst *mon;
3642 int damtype, dam;
3643 {
3644     int heal = 0, slow = 0;
3645
3646     if (mon->data == &mons[PM_FLESH_GOLEM]) {
3647         if (damtype == AD_ELEC)
3648             heal = (dam + 5) / 6;
3649         else if (damtype == AD_FIRE || damtype == AD_COLD)
3650             slow = 1;
3651     } else if (mon->data == &mons[PM_IRON_GOLEM]) {
3652         if (damtype == AD_ELEC)
3653             slow = 1;
3654         else if (damtype == AD_FIRE)
3655             heal = dam;
3656     } else {
3657         return;
3658     }
3659     if (slow) {
3660         if (mon->mspeed != MSLOW)
3661             mon_adjust_speed(mon, -1, (struct obj *) 0);
3662     }
3663     if (heal) {
3664         if (mon->mhp < mon->mhpmax) {
3665             mon->mhp += heal;
3666             if (mon->mhp > mon->mhpmax)
3667                 mon->mhp = mon->mhpmax;
3668             if (cansee(mon->mx, mon->my))
3669 /*JP
3670                 pline("%s seems healthier.", Monnam(mon));
3671 */
3672                 pline("%s\82Í\8c³\8bC\82É\82È\82Á\82½\82æ\82¤\82É\8c©\82¦\82é\81D", Monnam(mon));
3673         }
3674     }
3675 }
3676
3677 boolean
3678 angry_guards(silent)
3679 boolean silent;
3680 {
3681     struct monst *mtmp;
3682     int ct = 0, nct = 0, sct = 0, slct = 0;
3683
3684     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3685         if (DEADMONSTER(mtmp))
3686             continue;
3687         if (is_watch(mtmp->data) && mtmp->mpeaceful) {
3688             ct++;
3689             if (cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
3690                 if (distu(mtmp->mx, mtmp->my) == 2)
3691                     nct++;
3692                 else
3693                     sct++;
3694             }
3695             if (mtmp->msleeping || mtmp->mfrozen) {
3696                 slct++;
3697                 mtmp->msleeping = mtmp->mfrozen = 0;
3698             }
3699             mtmp->mpeaceful = 0;
3700         }
3701     }
3702     if (ct) {
3703         if (!silent) { /* do we want pline msgs? */
3704             if (slct)
3705 #if 0 /*JP*/
3706                 pline_The("guard%s wake%s up!", slct > 1 ? "s" : "",
3707                           slct == 1 ? "s" : "");
3708 #else
3709                 pline("\94Ô\95º\82Í\96Ú\82ð\8ao\82Ü\82µ\82½\81I");
3710 #endif
3711             if (nct || sct) {
3712                 if (nct)
3713 #if 0 /*JP*/
3714                     pline_The("guard%s get%s angry!", nct == 1 ? "" : "s",
3715                               nct == 1 ? "s" : "");
3716 #else
3717                     pline("\94Ô\95º\82Í\93{\82Á\82½\81I");
3718 #endif
3719                 else if (!Blind)
3720 #if 0 /*JP*/
3721                     You_see("%sangry guard%s approaching!",
3722                             sct == 1 ? "an " : "", sct > 1 ? "s" : "");
3723 #else
3724                     You("\93{\82Á\82½\94Ô\95º\82ª\8bß\8añ\82Á\82Ä\82­\82é\82Ì\82ð\8c©\82½\81I");
3725 #endif
3726             } else
3727 /*JP
3728                 You_hear("the shrill sound of a guard's whistle.");
3729 */
3730                 You_hear("\94Ô\95º\82Ì\93J\82ª\82¯\82½\82½\82Ü\82µ\82­\96Â\82é\82Ì\82ð\95·\82¢\82½\81D");
3731         }
3732         return TRUE;
3733     }
3734     return FALSE;
3735 }
3736
3737 void
3738 pacify_guards()
3739 {
3740     struct monst *mtmp;
3741
3742     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3743         if (DEADMONSTER(mtmp))
3744             continue;
3745         if (is_watch(mtmp->data))
3746             mtmp->mpeaceful = 1;
3747     }
3748 }
3749
3750 void
3751 mimic_hit_msg(mtmp, otyp)
3752 struct monst *mtmp;
3753 short otyp;
3754 {
3755     short ap = mtmp->mappearance;
3756
3757     switch (mtmp->m_ap_type) {
3758     case M_AP_NOTHING:
3759     case M_AP_FURNITURE:
3760     case M_AP_MONSTER:
3761         break;
3762     case M_AP_OBJECT:
3763         if (otyp == SPE_HEALING || otyp == SPE_EXTRA_HEALING) {
3764 #if 0 /*JP*/
3765             pline("%s seems a more vivid %s than before.",
3766                   The(simple_typename(ap)),
3767                   c_obj_colors[objects[ap].oc_color]);
3768 #else
3769             pline("%s\82Í\88È\91O\82æ\82è\82æ\82è\91N\82â\82©\82È%s\82É\82È\82Á\82½\82æ\82¤\82¾\81D",
3770                   The(simple_typename(ap)),
3771                   c_obj_colors[objects[ap].oc_color]);
3772 #endif
3773         }
3774         break;
3775     }
3776 }
3777
3778 boolean
3779 usmellmon(mdat)
3780 struct permonst *mdat;
3781 {
3782     int mndx;
3783     boolean nonspecific = FALSE;
3784     boolean msg_given = FALSE;
3785
3786     if (mdat) {
3787         if (!olfaction(youmonst.data))
3788             return FALSE;
3789         mndx = monsndx(mdat);
3790         switch (mndx) {
3791         case PM_ROTHE:
3792         case PM_MINOTAUR:
3793             You("notice a bovine smell.");
3794             msg_given = TRUE;
3795             break;
3796         case PM_CAVEMAN:
3797         case PM_CAVEWOMAN:
3798         case PM_BARBARIAN:
3799         case PM_NEANDERTHAL:
3800             You("smell body odor.");
3801             msg_given = TRUE;
3802             break;
3803         /*
3804         case PM_PESTILENCE:
3805         case PM_FAMINE:
3806         case PM_DEATH:
3807             break;
3808         */
3809         case PM_HORNED_DEVIL:
3810         case PM_BALROG:
3811         case PM_ASMODEUS:
3812         case PM_DISPATER:
3813         case PM_YEENOGHU:
3814         case PM_ORCUS:
3815             break;
3816         case PM_HUMAN_WEREJACKAL:
3817         case PM_HUMAN_WERERAT:
3818         case PM_HUMAN_WEREWOLF:
3819         case PM_WEREJACKAL:
3820         case PM_WERERAT:
3821         case PM_WEREWOLF:
3822         case PM_OWLBEAR:
3823             You("detect an odor reminiscent of an animal's den.");
3824             msg_given = TRUE;
3825             break;
3826         /*
3827         case PM_PURPLE_WORM:
3828             break;
3829         */
3830         case PM_STEAM_VORTEX:
3831             You("smell steam.");
3832             msg_given = TRUE;
3833             break;
3834         case PM_GREEN_SLIME:
3835             pline("%s stinks.", Something);
3836             msg_given = TRUE;
3837             break;
3838         case PM_VIOLET_FUNGUS:
3839         case PM_SHRIEKER:
3840             You("smell mushrooms.");
3841             msg_given = TRUE;
3842             break;
3843         /* These are here to avoid triggering the
3844            nonspecific treatment through the default case below*/
3845         case PM_WHITE_UNICORN:
3846         case PM_GRAY_UNICORN:
3847         case PM_BLACK_UNICORN:
3848         case PM_JELLYFISH:
3849             break;
3850         default:
3851             nonspecific = TRUE;
3852             break;
3853         }
3854
3855         if (nonspecific)
3856             switch (mdat->mlet) {
3857             case S_DOG:
3858                 You("notice a dog smell.");
3859                 msg_given = TRUE;
3860                 break;
3861             case S_DRAGON:
3862                 You("smell a dragon!");
3863                 msg_given = TRUE;
3864                 break;
3865             case S_FUNGUS:
3866                 pline("%s smells moldy.", Something);
3867                 msg_given = TRUE;
3868                 break;
3869             case S_UNICORN:
3870                 You("detect a%s odor reminiscent of a stable.",
3871                     (mndx == PM_PONY) ? "n" : " strong");
3872                 msg_given = TRUE;
3873                 break;
3874             case S_ZOMBIE:
3875                 You("smell rotting flesh.");
3876                 msg_given = TRUE;
3877                 break;
3878             case S_EEL:
3879                 You("smell fish.");
3880                 msg_given = TRUE;
3881                 break;
3882             case S_ORC:
3883                 if (maybe_polyd(is_orc(youmonst.data), Race_if(PM_ORC)))
3884                     You("notice an attractive smell.");
3885                 else
3886                     pline("A foul stench makes you feel a little nauseated.");
3887                 msg_given = TRUE;
3888                 break;
3889             default:
3890                 break;
3891             }
3892     }
3893     return msg_given ? TRUE : FALSE;
3894 }
3895
3896 /*mon.c*/