OSDN Git Service

fix #36659
[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Á\82Æ\82Ü\82Á\82½\81D\81D\81D");
1783 /*JP
1784             pline("%s medallion begins to glow!", s_suffix(Monnam(mtmp)));
1785 */
1786             pline("%s\82Ì\96\82\8f\9c\82¯\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("\96\82\8f\9c\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 #if 0 /*JP*/
1858             Sprintf(buf, "The %s%s suddenly %s and rises as %%s!",
1859                     spec_mon ? "" : "seemingly dead ",
1860                     x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1861                              SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
1862                                  | SUPPRESS_INVISIBLE | SUPPRESS_IT,
1863                              FALSE),
1864                     spec_mon ? "reconstitutes" : "transforms");
1865 #else
1866             Sprintf(buf, "%s%s\82Í\93Ë\91R%s\81C%%s\82Æ\82µ\82Ä\91h\82Á\82½\81I",
1867                     spec_mon ? "" : "\8e\80\82ñ\82¾\82æ\82¤\82É\8ev\82í\82ê\82½",
1868                     x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1869                              SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
1870                                  | SUPPRESS_INVISIBLE | SUPPRESS_IT,
1871                              FALSE),
1872                     spec_mon ? "\8dÄ\8d\\90¬\82³\82ê" : "\95Ï\89»\82µ");
1873 #endif
1874             mtmp->mcanmove = 1;
1875             mtmp->mfrozen = 0;
1876             if (mtmp->mhpmax <= 0)
1877                 mtmp->mhpmax = 10;
1878             mtmp->mhp = mtmp->mhpmax;
1879             /* this can happen if previously a fog cloud */
1880             if (u.uswallow && (mtmp == u.ustuck))
1881                 expels(mtmp, mtmp->data, FALSE);
1882             if (in_door) {
1883                 coord new_xy;
1884
1885                 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
1886                     rloc_to(mtmp, new_xy.x, new_xy.y);
1887                 }
1888             }
1889             newcham(mtmp, &mons[mndx], FALSE, FALSE);
1890             if (mtmp->data == &mons[mndx])
1891                 mtmp->cham = NON_PM;
1892             else
1893                 mtmp->cham = mndx;
1894             if (canspotmon(mtmp)) {
1895                 pline(buf, a_monnam(mtmp));
1896                 vamp_rise_msg = TRUE;
1897             }
1898             newsym(x, y);
1899             return;
1900         }
1901     }
1902
1903     /* dead vault guard is actually kept at coordinate <0,0> until
1904        his temporary corridor to/from the vault has been removed;
1905        need to do this after life-saving and before m_detach() */
1906     if (mtmp->isgd && !grddead(mtmp))
1907         return;
1908
1909     /* Player is thrown from his steed when it dies */
1910     if (mtmp == u.usteed)
1911         dismount_steed(DISMOUNT_GENERIC);
1912
1913     mptr = mtmp->data; /* save this for m_detach() */
1914     /* restore chameleon, lycanthropes to true form at death */
1915     if (mtmp->cham >= LOW_PM) {
1916         set_mon_data(mtmp, &mons[mtmp->cham], -1);
1917         mtmp->cham = NON_PM;
1918     } else if (mtmp->data == &mons[PM_WEREJACKAL])
1919         set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1920     else if (mtmp->data == &mons[PM_WEREWOLF])
1921         set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1922     else if (mtmp->data == &mons[PM_WERERAT])
1923         set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1924
1925     /* if MAXMONNO monsters of a given type have died, and it
1926      * can be done, extinguish that monster.
1927      *
1928      * mvitals[].died does double duty as total number of dead monsters
1929      * and as experience factor for the player killing more monsters.
1930      * this means that a dragon dying by other means reduces the
1931      * experience the player gets for killing a dragon directly; this
1932      * is probably not too bad, since the player likely finagled the
1933      * first dead dragon via ring of conflict or pets, and extinguishing
1934      * based on only player kills probably opens more avenues of abuse
1935      * for rings of conflict and such.
1936      */
1937     tmp = monsndx(mtmp->data);
1938     if (mvitals[tmp].died < 255)
1939         mvitals[tmp].died++;
1940
1941     /* if it's a (possibly polymorphed) quest leader, mark him as dead */
1942     if (mtmp->m_id == quest_status.leader_m_id)
1943         quest_status.leader_is_dead = TRUE;
1944 #ifdef MAIL
1945     /* if the mail daemon dies, no more mail delivery.  -3. */
1946     if (tmp == PM_MAIL_DAEMON)
1947         mvitals[tmp].mvflags |= G_GENOD;
1948 #endif
1949
1950     if (mtmp->data->mlet == S_KOP) {
1951         /* Dead Kops may come back. */
1952         switch (rnd(5)) {
1953         case 1: /* returns near the stairs */
1954             (void) makemon(mtmp->data, xdnstair, ydnstair, NO_MM_FLAGS);
1955             break;
1956         case 2: /* randomly */
1957             (void) makemon(mtmp->data, 0, 0, NO_MM_FLAGS);
1958             break;
1959         default:
1960             break;
1961         }
1962     }
1963     if (mtmp->iswiz)
1964         wizdead();
1965     if (mtmp->data->msound == MS_NEMESIS)
1966         nemdead();
1967     if (mtmp->data == &mons[PM_MEDUSA])
1968         u.uachieve.killed_medusa = 1;
1969     if (glyph_is_invisible(levl[mtmp->mx][mtmp->my].glyph))
1970         unmap_object(mtmp->mx, mtmp->my);
1971     m_detach(mtmp, mptr);
1972 }
1973
1974 /* TRUE if corpse might be dropped, magr may die if mon was swallowed */
1975 boolean
1976 corpse_chance(mon, magr, was_swallowed)
1977 struct monst *mon;
1978 struct monst *magr;    /* killer, if swallowed */
1979 boolean was_swallowed; /* digestion */
1980 {
1981     struct permonst *mdat = mon->data;
1982     int i, tmp;
1983
1984     if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1985         if (cansee(mon->mx, mon->my) && !was_swallowed)
1986 /*JP
1987             pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
1988 */
1989             pline("%s\82Ì\91Ì\82Í\95²\81X\82É\82È\82Á\82½\81D", Monnam(mon));
1990         return FALSE;
1991     }
1992
1993     /* Gas spores always explode upon death */
1994     for (i = 0; i < NATTK; i++) {
1995         if (mdat->mattk[i].aatyp == AT_BOOM) {
1996             if (mdat->mattk[i].damn)
1997                 tmp = d((int) mdat->mattk[i].damn, (int) mdat->mattk[i].damd);
1998             else if (mdat->mattk[i].damd)
1999                 tmp = d((int) mdat->mlevel + 1, (int) mdat->mattk[i].damd);
2000             else
2001                 tmp = 0;
2002             if (was_swallowed && magr) {
2003                 if (magr == &youmonst) {
2004 /*JP
2005                     There("is an explosion in your %s!", body_part(STOMACH));
2006 */
2007                     pline("%s\82Ì\92\86\82Å\94\9a\94­\82ª\8bN\82«\82½\81I", body_part(STOMACH));
2008 #if 0 /*JP*/
2009                     Sprintf(killer.name, "%s explosion",
2010                             s_suffix(mdat->mname));
2011 #else
2012                     Sprintf(killer.name, "%s\82Ì\94\9a\94­\82Å", mdat->mname);
2013 #endif
2014                     losehp(Maybe_Half_Phys(tmp), killer.name, KILLED_BY_AN);
2015                 } else {
2016 /*JP
2017                     You_hear("an explosion.");
2018 */
2019                     You_hear("\94\9a\94­\89¹\82ð\95·\82¢\82½\81D");
2020                     magr->mhp -= tmp;
2021                     if (magr->mhp < 1)
2022                         mondied(magr);
2023                     if (magr->mhp < 1) { /* maybe lifesaved */
2024                         if (canspotmon(magr))
2025 /*JP
2026                             pline("%s rips open!", Monnam(magr));
2027 */
2028                             pline("%s\82Í\83r\83\8a\82Á\82Æ\94j\82ê\82½\81I", Monnam(magr));
2029                     } else if (canseemon(magr))
2030 /*JP
2031                         pline("%s seems to have indigestion.", Monnam(magr));
2032 */
2033                         pline("%s\82Í\8fÁ\89»\95s\97Ç\82Ì\82æ\82¤\82¾\81D", Monnam(magr));
2034                 }
2035
2036                 return FALSE;
2037             }
2038
2039 /*JP
2040             Sprintf(killer.name, "%s explosion", s_suffix(mdat->mname));
2041 */
2042             Sprintf(killer.name, "%s\82Ì\94\9a\94­\82Å", mdat->mname);
2043             killer.format = KILLED_BY_AN;
2044             explode(mon->mx, mon->my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS);
2045             return FALSE;
2046         }
2047     }
2048
2049     /* must duplicate this below check in xkilled() since it results in
2050      * creating no objects as well as no corpse
2051      */
2052     if (LEVEL_SPECIFIC_NOCORPSE(mdat))
2053         return FALSE;
2054
2055     if (((bigmonst(mdat) || mdat == &mons[PM_LIZARD]) && !mon->mcloned)
2056         || is_golem(mdat) || is_mplayer(mdat) || is_rider(mdat))
2057         return TRUE;
2058     tmp = 2 + ((mdat->geno & G_FREQ) < 2) + verysmall(mdat);
2059     return (boolean) !rn2(tmp);
2060 }
2061
2062 /* drop (perhaps) a cadaver and remove monster */
2063 void
2064 mondied(mdef)
2065 register struct monst *mdef;
2066 {
2067     mondead(mdef);
2068     if (mdef->mhp > 0)
2069         return; /* lifesaved */
2070
2071     if (corpse_chance(mdef, (struct monst *) 0, FALSE)
2072         && (accessible(mdef->mx, mdef->my) || is_pool(mdef->mx, mdef->my)))
2073         (void) make_corpse(mdef, CORPSTAT_NONE);
2074 }
2075
2076 /* monster disappears, not dies */
2077 void
2078 mongone(mdef)
2079 struct monst *mdef;
2080 {
2081     mdef->mhp = 0; /* can skip some inventory bookkeeping */
2082
2083     /* dead vault guard is actually kept at coordinate <0,0> until
2084        his temporary corridor to/from the vault has been removed */
2085     if (mdef->isgd && !grddead(mdef))
2086         return;
2087     /* hero is thrown from his steed when it disappears */
2088     if (mdef == u.usteed)
2089         dismount_steed(DISMOUNT_GENERIC);
2090     /* drop special items like the Amulet so that a dismissed Kop or nurse
2091        can't remove them from the game */
2092     mdrop_special_objs(mdef);
2093     /* release rest of monster's inventory--it is removed from game */
2094     discard_minvent(mdef);
2095     m_detach(mdef, mdef->data);
2096 }
2097
2098 /* drop a statue or rock and remove monster */
2099 void
2100 monstone(mdef)
2101 struct monst *mdef;
2102 {
2103     struct obj *otmp, *obj, *oldminvent;
2104     xchar x = mdef->mx, y = mdef->my;
2105     boolean wasinside = FALSE;
2106
2107     /* we have to make the statue before calling mondead, to be able to
2108      * put inventory in it, and we have to check for lifesaving before
2109      * making the statue....
2110      */
2111     lifesaved_monster(mdef);
2112     if (mdef->mhp > 0)
2113         return;
2114
2115     mdef->mtrapped = 0; /* (see m_detach) */
2116
2117     if ((int) mdef->data->msize > MZ_TINY
2118         || !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
2119         oldminvent = 0;
2120         /* some objects may end up outside the statue */
2121         while ((obj = mdef->minvent) != 0) {
2122             obj_extract_self(obj);
2123             if (obj->owornmask)
2124                 update_mon_intrinsics(mdef, obj, FALSE, TRUE);
2125             obj_no_longer_held(obj);
2126             if (obj->owornmask & W_WEP)
2127                 setmnotwielded(mdef, obj);
2128             obj->owornmask = 0L;
2129             if (obj->otyp == BOULDER
2130 #if 0 /* monsters don't carry statues */
2131                 ||  (obj->otyp == STATUE
2132                      && mons[obj->corpsenm].msize >= mdef->data->msize)
2133 #endif
2134                 /* invocation tools resist even with 0% resistance */
2135                 || obj_resists(obj, 0, 0)) {
2136 /*JP
2137                 if (flooreffects(obj, x, y, "fall"))
2138 */
2139                 if (flooreffects(obj, x, y, "\97\8e\82¿\82é"))
2140                     continue;
2141                 place_object(obj, x, y);
2142             } else {
2143                 if (obj->lamplit)
2144                     end_burn(obj, TRUE);
2145                 obj->nobj = oldminvent;
2146                 oldminvent = obj;
2147             }
2148         }
2149         /* defer statue creation until after inventory removal
2150            so that saved monster traits won't retain any stale
2151            item-conferred attributes */
2152         otmp = mkcorpstat(STATUE, mdef, mdef->data, x, y, CORPSTAT_NONE);
2153         if (has_mname(mdef))
2154             otmp = oname(otmp, MNAME(mdef));
2155         while ((obj = oldminvent) != 0) {
2156             oldminvent = obj->nobj;
2157             (void) add_to_container(otmp, obj);
2158         }
2159         /* Archeologists should not break unique statues */
2160         if (mdef->data->geno & G_UNIQ)
2161             otmp->spe = 1;
2162         otmp->owt = weight(otmp);
2163     } else
2164         otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
2165
2166     stackobj(otmp);
2167     /* mondead() already does this, but we must do it before the newsym */
2168     if (glyph_is_invisible(levl[x][y].glyph))
2169         unmap_object(x, y);
2170     if (cansee(x, y))
2171         newsym(x, y);
2172     /* We don't currently trap the hero in the statue in this case but we
2173      * could */
2174     if (u.uswallow && u.ustuck == mdef)
2175         wasinside = TRUE;
2176     mondead(mdef);
2177     if (wasinside) {
2178         if (is_animal(mdef->data))
2179 #if 0 /*JP*/
2180             You("%s through an opening in the new %s.",
2181                 locomotion(youmonst.data, "jump"), xname(otmp));
2182 #else
2183             You("\90V\82µ\82­\82Å\82«\82½%s\82©\82ç%s\81D",
2184                 xname(otmp), jumpedthrough(youmonst.data, "\94ò\82Ñ\8fo\82½"));
2185 #endif
2186     }
2187 }
2188
2189 /* another monster has killed the monster mdef */
2190 void
2191 monkilled(mdef, fltxt, how)
2192 struct monst *mdef;
2193 const char *fltxt;
2194 int how;
2195 {
2196     boolean be_sad = FALSE; /* true if unseen pet is killed */
2197
2198     if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
2199         && fltxt)
2200 #if 0 /*JP*/
2201         pline("%s is %s%s%s!", Monnam(mdef),
2202               nonliving(mdef->data) ? "destroyed" : "killed",
2203               *fltxt ? " by the " : "", fltxt);
2204 #else
2205         {
2206             if(*fltxt)
2207                 pline("%s\82Í%s\82É\82æ\82Á\82Ä%s\81I", Monnam(mdef), fltxt,
2208                       nonliving(mdef->data) ? "\93|\82³\82ê\82½" : "\8eE\82³\82ê\82½");
2209             else
2210                 pline("%s\82Í%s\81I", Monnam(mdef), 
2211                       nonliving(mdef->data) ? "\93|\82³\82ê\82½" : "\8eE\82³\82ê\82½");
2212         }
2213 #endif
2214     else
2215         be_sad = (mdef->mtame != 0);
2216
2217     /* no corpses if digested or disintegrated */
2218     if (how == AD_DGST || how == -AD_RBRE)
2219         mondead(mdef);
2220     else
2221         mondied(mdef);
2222
2223     if (be_sad && mdef->mhp <= 0)
2224 /*JP
2225         You("have a sad feeling for a moment, then it passes.");
2226 */
2227         You("\94ß\82µ\82¢\8bC\8e\9d\82É\82¨\82»\82í\82ê\82½\82ª\81C\82·\82®\82É\89ß\82¬\82³\82Á\82½\81D");
2228 }
2229
2230 void
2231 unstuck(mtmp)
2232 struct monst *mtmp;
2233 {
2234     if (u.ustuck == mtmp) {
2235         if (u.uswallow) {
2236             u.ux = mtmp->mx;
2237             u.uy = mtmp->my;
2238             u.uswallow = 0;
2239             u.uswldtim = 0;
2240             if (Punished && uchain->where != OBJ_FLOOR)
2241                 placebc();
2242             vision_full_recalc = 1;
2243             docrt();
2244         }
2245         u.ustuck = 0;
2246     }
2247 }
2248
2249 void
2250 killed(mtmp)
2251 struct monst *mtmp;
2252 {
2253     xkilled(mtmp, 1);
2254 }
2255
2256 /* the player has killed the monster mtmp */
2257 void
2258 xkilled(mtmp, dest)
2259 struct monst *mtmp;
2260 int dest; /* dest==1, normal; dest==0, don't print message; dest==2, don't
2261              drop corpse either; dest==3, message but no corpse */
2262 {
2263     int tmp, mndx, x = mtmp->mx, y = mtmp->my;
2264     struct permonst *mdat;
2265     struct obj *otmp;
2266     struct trap *t;
2267     boolean wasinside = u.uswallow && (u.ustuck == mtmp);
2268     boolean burycorpse = FALSE;
2269
2270     /* KMH, conduct */
2271     u.uconduct.killer++;
2272
2273     if (dest & 1) {
2274 #if 0 /*JP*/
2275         const char *verb = nonliving(mtmp->data) ? "destroy" : "kill";
2276 #endif
2277
2278         if (!wasinside && !canspotmon(mtmp))
2279 /*JP
2280             You("%s it!", verb);
2281 */
2282             You("\89½\8eÒ\82©\82ð\93|\82µ\82½\81I");
2283         else {
2284 #if 0 /*JP*/
2285             You("%s %s!", verb,
2286                 !mtmp->mtame
2287                     ? mon_nam(mtmp)
2288                     : x_monnam(mtmp,
2289                                (has_mname(mtmp)) ? ARTICLE_NONE : ARTICLE_THE,
2290                                "poor",
2291                                (has_mname(mtmp)) ? SUPPRESS_SADDLE : 0,
2292                                FALSE));
2293 #else
2294             You("%s%s\82ð\93|\82µ\82½\81I",
2295                 mtmp->mtame ? "\82©\82í\82¢\82»\82¤\82È" : "",
2296                 mon_nam(mtmp));
2297 #endif
2298         }
2299     }
2300
2301     if (mtmp->mtrapped && (t = t_at(x, y)) != 0
2302         && (t->ttyp == PIT || t->ttyp == SPIKED_PIT)) {
2303         if (sobj_at(BOULDER, x, y))
2304             dest |= 2; /*
2305         * Prevent corpses/treasure being created "on top"
2306         * of the boulder that is about to fall in. This is
2307         * out of order, but cannot be helped unless this
2308         * whole routine is rearranged.
2309         */
2310         if (m_carrying(mtmp, BOULDER))
2311             burycorpse = TRUE;
2312     }
2313
2314     /* your pet knows who just killed it...watch out */
2315     if (mtmp->mtame && !mtmp->isminion)
2316         EDOG(mtmp)->killed_by_u = 1;
2317
2318     if (wasinside && thrownobj && thrownobj != uball) {
2319         /* thrown object has killed hero's engulfer; add it to mon's
2320            inventory now so that it will be placed with mon's other
2321            stuff prior to lookhere/autopickup when hero is expelled
2322            below (as a side-effect, this missile has immunity from
2323            being consumed [for this shot/throw only]) */
2324         mpickobj(mtmp, thrownobj);
2325         /* let throwing code know that missile has been disposed of */
2326         thrownobj = 0;
2327     }
2328
2329     vamp_rise_msg = FALSE; /* might get set in mondead() */
2330     /* dispose of monster and make cadaver */
2331     if (stoned)
2332         monstone(mtmp);
2333     else
2334         mondead(mtmp);
2335
2336     if (mtmp->mhp > 0) { /* monster lifesaved */
2337         /* Cannot put the non-visible lifesaving message in
2338          * lifesaved_monster() since the message appears only when you
2339          * kill it (as opposed to visible lifesaving which always
2340          * appears).
2341          */
2342         stoned = FALSE;
2343         if (!cansee(x, y) && !vamp_rise_msg)
2344 /*JP
2345             pline("Maybe not...");
2346 */
2347             pline("\82¢\82â\81C\88á\82¤\82©\82à\81D\81D\81D");
2348         return;
2349     }
2350
2351     mdat = mtmp->data; /* note: mondead can change mtmp->data */
2352     mndx = monsndx(mdat);
2353
2354     if (stoned) {
2355         stoned = FALSE;
2356         goto cleanup;
2357     }
2358
2359     if ((dest & 2) || LEVEL_SPECIFIC_NOCORPSE(mdat))
2360         goto cleanup;
2361
2362 #ifdef MAIL
2363     if (mdat == &mons[PM_MAIL_DAEMON]) {
2364         stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
2365     }
2366 #endif
2367     if (accessible(x, y) || is_pool(x, y)) {
2368         struct obj *cadaver;
2369         int otyp;
2370
2371         /* illogical but traditional "treasure drop" */
2372         if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
2373             /* no extra item from swallower or steed */
2374             && (x != u.ux || y != u.uy)
2375             /* no extra item from kops--too easy to abuse */
2376             && mdat->mlet != S_KOP
2377             /* no items from cloned monsters */
2378             && !mtmp->mcloned) {
2379             otmp = mkobj(RANDOM_CLASS, TRUE);
2380             /* don't create large objects from small monsters */
2381             otyp = otmp->otyp;
2382             if (mdat->msize < MZ_HUMAN && otyp != FIGURINE
2383                 /* oc_big is also oc_bimanual and oc_bulky */
2384                 && (otmp->owt > 30 || objects[otyp].oc_big)) {
2385                 delobj(otmp);
2386 #if 0 /*JP*/
2387             } else if (!flooreffects(otmp, x, y, (dest & 1) ? "fall" : "")) {
2388 #else
2389             } else if (!flooreffects(otmp, x, y, (dest & 1) ? "\97\8e\82¿\82é" : "")) {
2390 #endif
2391                 place_object(otmp, x, y);
2392                 stackobj(otmp);
2393             }
2394         }
2395         /* corpse--none if hero was inside the monster */
2396         if (!wasinside && corpse_chance(mtmp, (struct monst *) 0, FALSE)) {
2397             cadaver = make_corpse(mtmp, burycorpse ? CORPSTAT_BURIED
2398                                                    : CORPSTAT_NONE);
2399             if (burycorpse && cadaver && cansee(x, y) && !mtmp->minvis
2400                 && cadaver->where == OBJ_BURIED && (dest & 1)) {
2401 /*JP
2402                 pline("%s corpse ends up buried.", s_suffix(Monnam(mtmp)));
2403 */
2404                 pline("%s\82Ì\8e\80\91Ì\82Í\96\84\82Ü\82Á\82Ä\82µ\82Ü\82Á\82½\81D", s_suffix(Monnam(mtmp)));
2405             }
2406         }
2407     }
2408     if (wasinside)
2409         spoteffects(TRUE); /* poor man's expels() */
2410     /* monster is gone, corpse or other object might now be visible */
2411     newsym(x, y);
2412
2413 cleanup:
2414     /* punish bad behaviour */
2415     if (is_human(mdat) && (!always_hostile(mdat) && mtmp->malign <= 0)
2416         && (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD)
2417         && u.ualign.type != A_CHAOTIC) {
2418         HTelepat &= ~INTRINSIC;
2419         change_luck(-2);
2420 /*JP
2421         You("murderer!");
2422 */
2423         You("\8eE\90l\8bS\82¾\81I");
2424         if (Blind && !Blind_telepat)
2425             see_monsters(); /* Can't sense monsters any more. */
2426     }
2427     if ((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)
2428         change_luck(-1);
2429     if (is_unicorn(mdat) && sgn(u.ualign.type) == sgn(mdat->maligntyp)) {
2430         change_luck(-5);
2431 /*JP
2432         You_feel("guilty...");
2433 */
2434         You("\8dß\82ð\8a´\82\82½\81D\81D\81D");
2435     }
2436
2437     /* give experience points */
2438     tmp = experience(mtmp, (int) mvitals[mndx].died);
2439     more_experienced(tmp, 0);
2440     newexplevel(); /* will decide if you go up */
2441
2442     /* adjust alignment points */
2443     if (mtmp->m_id == quest_status.leader_m_id) { /* REAL BAD! */
2444         adjalign(-(u.ualign.record + (int) ALIGNLIM / 2));
2445 #if 0 /*JP*/
2446         pline("That was %sa bad idea...",
2447               u.uevent.qcompleted ? "probably " : "");
2448 #else
2449         pline("%s\82æ\82­\82È\82¢\8ds\88×\82¾\82Á\82½\81D\81D\81D",
2450               u.uevent.qcompleted ? "\82½\82Ô\82ñ" : "");
2451 #endif
2452     } else if (mdat->msound == MS_NEMESIS) { /* Real good! */
2453         adjalign((int) (ALIGNLIM / 4));
2454     } else if (mdat->msound == MS_GUARDIAN) { /* Bad */
2455         adjalign(-(int) (ALIGNLIM / 8));
2456         if (!Hallucination)
2457 /*JP
2458             pline("That was probably a bad idea...");
2459 */
2460             pline("\82æ\82­\82È\82¢\8ds\88×\82¾\82Á\82½\81D\81D\81D");
2461         else
2462 /*JP
2463             pline("Whoopsie-daisy!");
2464 */
2465             pline("\83V\83\93\83W\83}\83b\83^\81[\81I");
2466     } else if (mtmp->ispriest) {
2467         adjalign((p_coaligned(mtmp)) ? -2 : 2);
2468         /* cancel divine protection for killing your priest */
2469         if (p_coaligned(mtmp))
2470             u.ublessed = 0;
2471         if (mdat->maligntyp == A_NONE)
2472             adjalign((int) (ALIGNLIM / 4)); /* BIG bonus */
2473     } else if (mtmp->mtame) {
2474         adjalign(-15); /* bad!! */
2475         /* your god is mighty displeased... */
2476         if (!Hallucination)
2477 /*JP
2478             You_hear("the rumble of distant thunder...");
2479 */
2480             You_hear("\89\93\82­\82Å\97\8b\96Â\82ð\95·\82¢\82½\81D\81D\81D");
2481         else
2482 /*JP
2483             You_hear("the studio audience applaud!");
2484 */
2485             pline("\92®\8fO\82Ì\8a\85\8dÑ\82ð\97\81\82Ñ\82½\81I");
2486     } else if (mtmp->mpeaceful)
2487         adjalign(-5);
2488
2489     /* malign was already adjusted for u.ualign.type and randomization */
2490     adjalign(mtmp->malign);
2491 }
2492
2493 /* changes the monster into a stone monster of the same type
2494    this should only be called when poly_when_stoned() is true */
2495 void
2496 mon_to_stone(mtmp)
2497 struct monst *mtmp;
2498 {
2499     if (mtmp->data->mlet == S_GOLEM) {
2500         /* it's a golem, and not a stone golem */
2501         if (canseemon(mtmp))
2502 /*JP
2503             pline("%s solidifies...", Monnam(mtmp));
2504 */
2505             pline("%s\82Í\8bÃ\8cÅ\82µ\82½\81D\81D\81D", Monnam(mtmp));
2506         if (newcham(mtmp, &mons[PM_STONE_GOLEM], FALSE, FALSE)) {
2507             if (canseemon(mtmp))
2508 /*JP
2509                 pline("Now it's %s.", an(mtmp->data->mname));
2510 */
2511                 pline("\82È\82ñ\82Æ%s\82É\82È\82Á\82Ä\82µ\82Ü\82Á\82½\81D", mtmp->data->mname);
2512         } else {
2513             if (canseemon(mtmp))
2514 /*JP
2515                 pline("... and returns to normal.");
2516 */
2517                 pline("\81D\81D\81D\82»\82µ\82Ä\95\81\92Ê\82É\96ß\82Á\82½\81D");
2518         }
2519     } else
2520         impossible("Can't polystone %s!", a_monnam(mtmp));
2521 }
2522
2523 /* make monster mtmp next to you (if possible);
2524    might place monst on far side of a wall or boulder */
2525 void
2526 mnexto(mtmp)
2527 struct monst *mtmp;
2528 {
2529     coord mm;
2530     boolean couldspot = canspotmon(mtmp);
2531
2532     if (mtmp == u.usteed) {
2533         /* Keep your steed in sync with you instead */
2534         mtmp->mx = u.ux;
2535         mtmp->my = u.uy;
2536         return;
2537     }
2538
2539     if (!enexto(&mm, u.ux, u.uy, mtmp->data))
2540         return;
2541     rloc_to(mtmp, mm.x, mm.y);
2542     if (!in_mklev && (mtmp->mstrategy & STRAT_APPEARMSG)) {
2543         mtmp->mstrategy &= ~STRAT_APPEARMSG; /* one chance only */
2544         if (!couldspot && canspotmon(mtmp))
2545 #if 0 /*JP*/
2546             pline("%s suddenly %s!", Amonnam(mtmp),
2547                   !Blind ? "appears" : "arrives");
2548 #else
2549             pline("\93Ë\91R%s\82ª\8c»\82ê\82½\81I", Amonnam(mtmp));
2550 #endif
2551     }
2552     return;
2553 }
2554
2555 /* like mnexto() but requires destination to be directly accessible */
2556 void
2557 maybe_mnexto(mtmp)
2558 struct monst *mtmp;
2559 {
2560     coord mm;
2561     struct permonst *ptr = mtmp->data;
2562     boolean diagok = !NODIAG(ptr - mons);
2563     int tryct = 20;
2564
2565     do {
2566         if (!enexto(&mm, u.ux, u.uy, ptr))
2567             return;
2568         if (couldsee(mm.x, mm.y)
2569             /* don't move grid bugs diagonally */
2570             && (diagok || mm.x == mtmp->mx || mm.y == mtmp->my)) {
2571             rloc_to(mtmp, mm.x, mm.y);
2572             return;
2573         }
2574     } while (--tryct > 0);
2575 }
2576
2577 /* mnearto()
2578  * Put monster near (or at) location if possible.
2579  * Returns:
2580  *      1 - if a monster was moved from x, y to put mtmp at x, y.
2581  *      0 - in most cases.
2582  */
2583 boolean
2584 mnearto(mtmp, x, y, move_other)
2585 register struct monst *mtmp;
2586 xchar x, y;
2587 boolean move_other; /* make sure mtmp gets to x, y! so move m_at(x, y) */
2588 {
2589     struct monst *othermon = (struct monst *) 0;
2590     xchar newx, newy;
2591     coord mm;
2592
2593     if (mtmp->mx == x && mtmp->my == y)
2594         return FALSE;
2595
2596     if (move_other && (othermon = m_at(x, y)) != 0) {
2597         if (othermon->wormno)
2598             remove_worm(othermon);
2599         else
2600             remove_monster(x, y);
2601     }
2602
2603     newx = x;
2604     newy = y;
2605     if (!goodpos(newx, newy, mtmp, 0)) {
2606         /* Actually we have real problems if enexto ever fails.
2607          * Migrating_mons that need to be placed will cause
2608          * no end of trouble.
2609          */
2610         if (!enexto(&mm, newx, newy, mtmp->data))
2611             return FALSE;
2612         newx = mm.x;
2613         newy = mm.y;
2614     }
2615     rloc_to(mtmp, newx, newy);
2616
2617     if (move_other && othermon) {
2618         xchar oldx = othermon->mx, oldy = othermon->my;
2619
2620         othermon->mx = othermon->my = 0;
2621         (void) mnearto(othermon, x, y, FALSE);
2622         if (othermon->mx == 0 && othermon->my == 0) {
2623             /* reloc failed, dump monster into "limbo"
2624                (aka migrate to current level) */
2625             othermon->mx = oldx;
2626             othermon->my = oldy;
2627             mdrop_special_objs(othermon);
2628             migrate_to_level(othermon, ledger_no(&u.uz), MIGR_APPROX_XY, NULL);
2629         }
2630     }
2631
2632     return FALSE;
2633 }
2634
2635 /* monster responds to player action; not the same as a passive attack;
2636    assumes reason for response has been tested, and response _must_ be made */
2637 void
2638 m_respond(mtmp)
2639 struct monst *mtmp;
2640 {
2641     if (mtmp->data->msound == MS_SHRIEK) {
2642         if (!Deaf) {
2643 /*JP
2644             pline("%s shrieks.", Monnam(mtmp));
2645 */
2646             pline("%s\82Í\8bà\90Ø\82è\90º\82ð\82 \82°\82½\81D", Monnam(mtmp));
2647             stop_occupation();
2648         }
2649         if (!rn2(10)) {
2650             if (!rn2(13))
2651                 (void) makemon(&mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
2652             else
2653                 (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
2654         }
2655         aggravate();
2656     }
2657     if (mtmp->data == &mons[PM_MEDUSA]) {
2658         register int i;
2659
2660         for (i = 0; i < NATTK; i++)
2661             if (mtmp->data->mattk[i].aatyp == AT_GAZE) {
2662                 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
2663                 break;
2664             }
2665     }
2666 }
2667
2668 void
2669 setmangry(mtmp)
2670 struct monst *mtmp;
2671 {
2672     mtmp->mstrategy &= ~STRAT_WAITMASK;
2673     if (!mtmp->mpeaceful)
2674         return;
2675     if (mtmp->mtame)
2676         return;
2677     mtmp->mpeaceful = 0;
2678     if (mtmp->ispriest) {
2679         if (p_coaligned(mtmp))
2680             adjalign(-5); /* very bad */
2681         else
2682             adjalign(2);
2683     } else
2684         adjalign(-1); /* attacking peaceful monsters is bad */
2685     if (couldsee(mtmp->mx, mtmp->my)) {
2686         if (humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
2687 /*JP
2688             pline("%s gets angry!", Monnam(mtmp));
2689 */
2690             pline("%s\82Í\93{\82Á\82½\81I", Monnam(mtmp));
2691         else if (flags.verbose && !Deaf)
2692             growl(mtmp);
2693     }
2694
2695     /* attacking your own quest leader will anger his or her guardians */
2696     if (!context.mon_moving /* should always be the case here */
2697         && mtmp->data == &mons[quest_info(MS_LEADER)]) {
2698         struct monst *mon;
2699         struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
2700         int got_mad = 0;
2701
2702         /* guardians will sense this attack even if they can't see it */
2703         for (mon = fmon; mon; mon = mon->nmon) {
2704             if (DEADMONSTER(mon))
2705                 continue;
2706             if (mon->data == q_guardian && mon->mpeaceful) {
2707                 mon->mpeaceful = 0;
2708                 if (canseemon(mon))
2709                     ++got_mad;
2710             }
2711         }
2712         if (got_mad && !Hallucination)
2713 #if 0 /*JP*/
2714             pline_The("%s appear%s to be angry too...",
2715                       got_mad == 1 ? q_guardian->mname
2716                                    : makeplural(q_guardian->mname),
2717                       got_mad == 1 ? "s" : "");
2718 #else
2719             pline("%s\82à\93{\82Á\82½\81D\81D\81D", q_guardian->mname);
2720 #endif
2721     }
2722 }
2723
2724 /* wake up a monster, usually making it angry in the process */
2725 void
2726 wakeup(mtmp)
2727 register struct monst *mtmp;
2728 {
2729     mtmp->msleeping = 0;
2730     finish_meating(mtmp);
2731     setmangry(mtmp);
2732     if (mtmp->m_ap_type) {
2733         seemimic(mtmp);
2734     } else if (context.forcefight && !context.mon_moving
2735                && mtmp->mundetected) {
2736         mtmp->mundetected = 0;
2737         newsym(mtmp->mx, mtmp->my);
2738     }
2739 }
2740
2741 /* Wake up nearby monsters without angering them. */
2742 void
2743 wake_nearby()
2744 {
2745     register struct monst *mtmp;
2746
2747     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2748         if (DEADMONSTER(mtmp))
2749             continue;
2750         if (distu(mtmp->mx, mtmp->my) < u.ulevel * 20) {
2751             mtmp->msleeping = 0;
2752             if (!unique_corpstat(mtmp->data))
2753                 mtmp->mstrategy &= ~STRAT_WAITMASK;
2754             if (mtmp->mtame && !mtmp->isminion)
2755                 EDOG(mtmp)->whistletime = moves;
2756         }
2757     }
2758 }
2759
2760 /* Wake up monsters near some particular location. */
2761 void
2762 wake_nearto(x, y, distance)
2763 register int x, y, distance;
2764 {
2765     register struct monst *mtmp;
2766
2767     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2768         if (DEADMONSTER(mtmp))
2769             continue;
2770         if (distance == 0 || dist2(mtmp->mx, mtmp->my, x, y) < distance) {
2771             mtmp->msleeping = 0;
2772             if (!unique_corpstat(mtmp->data))
2773                 mtmp->mstrategy &= ~STRAT_WAITMASK;
2774         }
2775     }
2776 }
2777
2778 /* NOTE: we must check for mimicry before calling this routine */
2779 void
2780 seemimic(mtmp)
2781 register struct monst *mtmp;
2782 {
2783     boolean is_blocker_appear = (is_door_mappear(mtmp)
2784                                  || is_obj_mappear(mtmp, BOULDER));
2785
2786     if (has_mcorpsenm(mtmp))
2787         freemcorpsenm(mtmp);
2788
2789     mtmp->m_ap_type = M_AP_NOTHING;
2790     mtmp->mappearance = 0;
2791
2792     /*
2793      *  Discovered mimics don't block light.
2794      */
2795     if (is_blocker_appear
2796         && !does_block(mtmp->mx, mtmp->my, &levl[mtmp->mx][mtmp->my]))
2797         unblock_point(mtmp->mx, mtmp->my);
2798
2799     newsym(mtmp->mx, mtmp->my);
2800 }
2801
2802 /* force all chameleons to become normal */
2803 void
2804 rescham()
2805 {
2806     register struct monst *mtmp;
2807     int mcham;
2808
2809     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2810         if (DEADMONSTER(mtmp))
2811             continue;
2812         mcham = (int) mtmp->cham;
2813         if (mcham >= LOW_PM) {
2814             (void) newcham(mtmp, &mons[mcham], FALSE, FALSE);
2815             mtmp->cham = NON_PM;
2816         }
2817         if (is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
2818             new_were(mtmp);
2819         if (mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
2820             seemimic(mtmp);
2821             /* we pretend that the mimic doesn't
2822                know that it has been unmasked */
2823             mtmp->msleeping = 1;
2824         }
2825     }
2826 }
2827
2828 /* Let the chameleons change again -dgk */
2829 void
2830 restartcham()
2831 {
2832     register struct monst *mtmp;
2833
2834     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2835         if (DEADMONSTER(mtmp))
2836             continue;
2837         mtmp->cham = pm_to_cham(monsndx(mtmp->data));
2838         if (mtmp->data->mlet == S_MIMIC && mtmp->msleeping
2839             && cansee(mtmp->mx, mtmp->my)) {
2840             set_mimic_sym(mtmp);
2841             newsym(mtmp->mx, mtmp->my);
2842         }
2843     }
2844 }
2845
2846 /* called when restoring a monster from a saved level; protection
2847    against shape-changing might be different now than it was at the
2848    time the level was saved. */
2849 void
2850 restore_cham(mon)
2851 struct monst *mon;
2852 {
2853     int mcham;
2854
2855     if (Protection_from_shape_changers) {
2856         mcham = (int) mon->cham;
2857         if (mcham >= LOW_PM) {
2858             mon->cham = NON_PM;
2859             (void) newcham(mon, &mons[mcham], FALSE, FALSE);
2860         } else if (is_were(mon->data) && !is_human(mon->data)) {
2861             new_were(mon);
2862         }
2863     } else if (mon->cham == NON_PM) {
2864         mon->cham = pm_to_cham(monsndx(mon->data));
2865     }
2866 }
2867
2868 /* unwatched hiders may hide again; if so, returns True */
2869 STATIC_OVL boolean
2870 restrap(mtmp)
2871 register struct monst *mtmp;
2872 {
2873     struct trap *t;
2874
2875     if (mtmp->mcan || mtmp->m_ap_type || cansee(mtmp->mx, mtmp->my)
2876         || rn2(3) || mtmp == u.ustuck
2877         /* can't hide while trapped except in pits */
2878         || (mtmp->mtrapped && (t = t_at(mtmp->mx, mtmp->my)) != 0
2879             && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))
2880         || (sensemon(mtmp) && distu(mtmp->mx, mtmp->my) <= 2))
2881         return FALSE;
2882
2883     if (mtmp->data->mlet == S_MIMIC) {
2884         set_mimic_sym(mtmp);
2885         return TRUE;
2886     } else if (levl[mtmp->mx][mtmp->my].typ == ROOM) {
2887         mtmp->mundetected = 1;
2888         return TRUE;
2889     }
2890
2891     return FALSE;
2892 }
2893
2894 /* monster/hero tries to hide under something at the current location */
2895 boolean
2896 hideunder(mtmp)
2897 struct monst *mtmp;
2898 {
2899     struct trap *t;
2900     boolean undetected = FALSE, is_u = (mtmp == &youmonst);
2901     xchar x = is_u ? u.ux : mtmp->mx, y = is_u ? u.uy : mtmp->my;
2902
2903     if (mtmp == u.ustuck) {
2904         ; /* can't hide if holding you or held by you */
2905     } else if (is_u ? (u.utrap && u.utraptype != TT_PIT)
2906                     : (mtmp->mtrapped && (t = t_at(x, y)) != 0
2907                        && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))) {
2908         ; /* can't hide while stuck in a non-pit trap */
2909     } else if (mtmp->data->mlet == S_EEL) {
2910         undetected = (is_pool(x, y) && !Is_waterlevel(&u.uz));
2911     } else if (hides_under(mtmp->data) && OBJ_AT(x, y)) {
2912         struct obj *otmp = level.objects[x][y];
2913
2914         /* most monsters won't hide under cockatrice corpse */
2915         if (otmp->nexthere || otmp->otyp != CORPSE
2916             || (mtmp == &youmonst ? Stone_resistance : resists_ston(mtmp))
2917             || !touch_petrifies(&mons[otmp->corpsenm]))
2918             undetected = TRUE;
2919     }
2920
2921     if (is_u)
2922         u.uundetected = undetected;
2923     else
2924         mtmp->mundetected = undetected;
2925     return undetected;
2926 }
2927
2928 /* called when returning to a previously visited level */
2929 void
2930 hide_monst(mon)
2931 struct monst *mon;
2932 {
2933     boolean hider_under = hides_under(mon->data) || mon->data->mlet == S_EEL;
2934
2935     if ((is_hider(mon->data) || hider_under)
2936         && !(mon->mundetected || mon->m_ap_type)) {
2937         xchar x = mon->mx, y = mon->my;
2938         char save_viz = viz_array[y][x];
2939
2940         /* override vision, forcing hero to be unable to see monster's spot */
2941         viz_array[y][x] &= ~(IN_SIGHT | COULD_SEE);
2942         if (is_hider(mon->data))
2943             (void) restrap(mon);
2944         /* try again if mimic missed its 1/3 chance to hide */
2945         if (mon->data->mlet == S_MIMIC && !mon->m_ap_type)
2946             (void) restrap(mon);
2947         if (hider_under)
2948             (void) hideunder(mon);
2949         viz_array[y][x] = save_viz;
2950     }
2951 }
2952
2953 static short *animal_list = 0; /* list of PM values for animal monsters */
2954 static int animal_list_count;
2955
2956 void
2957 mon_animal_list(construct)
2958 boolean construct;
2959 {
2960     if (construct) {
2961         short animal_temp[SPECIAL_PM];
2962         int i, n;
2963
2964         /* if (animal_list) impossible("animal_list already exists"); */
2965
2966         for (n = 0, i = LOW_PM; i < SPECIAL_PM; i++)
2967             if (is_animal(&mons[i]))
2968                 animal_temp[n++] = i;
2969         /* if (n == 0) animal_temp[n++] = NON_PM; */
2970
2971         animal_list = (short *) alloc(n * sizeof *animal_list);
2972         (void) memcpy((genericptr_t) animal_list, (genericptr_t) animal_temp,
2973                       n * sizeof *animal_list);
2974         animal_list_count = n;
2975     } else { /* release */
2976         if (animal_list)
2977             free((genericptr_t) animal_list), animal_list = 0;
2978         animal_list_count = 0;
2979     }
2980 }
2981
2982 STATIC_OVL int
2983 pick_animal()
2984 {
2985     int res;
2986
2987     if (!animal_list)
2988         mon_animal_list(TRUE);
2989
2990     res = animal_list[rn2(animal_list_count)];
2991     /* rogue level should use monsters represented by uppercase letters
2992        only, but since chameleons aren't generated there (not uppercase!)
2993        we don't perform a lot of retries */
2994     if (Is_rogue_level(&u.uz) && !isupper((uchar) mons[res].mlet))
2995         res = animal_list[rn2(animal_list_count)];
2996     return res;
2997 }
2998
2999 void
3000 decide_to_shapeshift(mon, shiftflags)
3001 struct monst *mon;
3002 int shiftflags;
3003 {
3004     struct permonst *ptr;
3005     unsigned was_female = mon->female;
3006     boolean msg = FALSE;
3007
3008     if ((shiftflags & SHIFT_MSG)
3009         || ((shiftflags & SHIFT_SEENMSG) && sensemon(mon)))
3010         msg = TRUE;
3011
3012     if (is_vampshifter(mon)) {
3013         /* The vampire has to be in good health (mhp) to maintain
3014          * its shifted form.
3015              *
3016          * If we're shifted and getting low on hp, maybe shift back.
3017          * If we're not already shifted and in good health, maybe shift.
3018          */
3019         if ((mon->mhp <= mon->mhpmax / 6) && rn2(4) && (mon->cham >= LOW_PM))
3020             (void) newcham(mon, &mons[mon->cham], FALSE, msg);
3021     } else if (mon->data->mlet == S_VAMPIRE && mon->cham == NON_PM && !rn2(6)
3022                && (mon->mhp > mon->mhpmax - ((mon->mhpmax / 10) + 1))) {
3023         (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
3024     }
3025     /* override the 10% chance for sex change */
3026     ptr = mon->data;
3027     if (!is_male(ptr) && !is_female(ptr) && !is_neuter(ptr))
3028         mon->female = was_female;
3029 }
3030
3031 STATIC_OVL int
3032 pickvampshape(mon)
3033 struct monst *mon;
3034 {
3035     int mndx = mon->cham;
3036     /* avoid picking monsters with lowercase display symbols ('d' for wolf
3037        and 'v' for fog cloud) on rogue level*/
3038     boolean uppercase_only = Is_rogue_level(&u.uz);
3039
3040     switch (mndx) {
3041     case PM_VLAD_THE_IMPALER:
3042         /* ensure Vlad can keep carrying the Candelabrum */
3043         if (mon_has_special(mon))
3044             break; /* leave mndx as is */
3045     /*FALLTHRU*/
3046     case PM_VAMPIRE_LORD: /* vampire lord or Vlad can become wolf */
3047         if (!rn2(10) && !uppercase_only) {
3048             mndx = PM_WOLF;
3049             break;
3050         }
3051     /*FALLTHRU*/
3052     case PM_VAMPIRE: /* any vampire can become fog or bat */
3053         mndx = (!rn2(4) && !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
3054         break;
3055     }
3056     return mndx;
3057 }
3058
3059 /* nonshapechangers who warrant special polymorph handling */
3060 STATIC_OVL boolean
3061 isspecmon(mon)
3062 struct monst *mon;
3063 {
3064     return (mon->isshk || mon->ispriest || mon->isgd
3065             || mon->m_id == quest_status.leader_m_id);
3066 }
3067
3068 /* restrict certain special monsters (shopkeepers, aligned priests,
3069    vault guards) to forms that allow them to behave sensibly (catching
3070    gold, speaking?) so that they don't need too much extra code */
3071 STATIC_OVL boolean
3072 validspecmon(mon, mndx)
3073 struct monst *mon;
3074 int mndx;
3075 {
3076     if (mndx == NON_PM)
3077         return TRUE; /* caller wants random */
3078
3079     if (!accept_newcham_form(mndx))
3080         return FALSE; /* geno'd or !polyok */
3081
3082     if (isspecmon(mon)) {
3083         struct permonst *ptr = &mons[mndx];
3084
3085         /* reject notake because object manipulation is expected
3086            and nohead because speech capability is expected */
3087         if (notake(ptr) || !has_head(ptr))
3088             return FALSE;
3089         /* [should we check ptr->msound here too?] */
3090     }
3091     return TRUE; /* potential new form is ok */
3092 }
3093
3094 /* prevent wizard mode user from specifying invalid vampshifter shape */
3095 STATIC_OVL boolean
3096 validvamp(mon, mndx_p, monclass)
3097 struct monst *mon;
3098 int *mndx_p, monclass;
3099 {
3100     /* simplify caller's usage */
3101     if (!is_vampshifter(mon))
3102         return validspecmon(mon, *mndx_p);
3103
3104     if (*mndx_p == PM_VAMPIRE || *mndx_p == PM_VAMPIRE_LORD
3105         || *mndx_p == PM_VLAD_THE_IMPALER) {
3106         /* player picked some type of vampire; use mon's self */
3107         *mndx_p = mon->cham;
3108         return TRUE;
3109     }
3110     if (mon->cham == PM_VLAD_THE_IMPALER && mon_has_special(mon)) {
3111         /* Vlad with Candelabrum; override choice, then accept it */
3112         *mndx_p = PM_VLAD_THE_IMPALER;
3113         return TRUE;
3114     }
3115     /* basic vampires can't become wolves; any can become fog or bat
3116        (we don't enforce upper-case only for rogue level here) */
3117     if (*mndx_p == PM_WOLF)
3118         return (boolean) (mon->cham != PM_VAMPIRE);
3119     if (*mndx_p == PM_FOG_CLOUD || *mndx_p == PM_VAMPIRE_BAT)
3120         return TRUE;
3121
3122     /* if we get here, specific type was no good; try by class */
3123     switch (monclass) {
3124     case S_VAMPIRE:
3125         *mndx_p = mon->cham;
3126         break;
3127     case S_BAT:
3128         *mndx_p = PM_VAMPIRE_BAT;
3129         break;
3130     case S_VORTEX:
3131         *mndx_p = PM_FOG_CLOUD;
3132         break;
3133     case S_DOG:
3134         if (mon->cham != PM_VAMPIRE) {
3135             *mndx_p = PM_WOLF;
3136             break;
3137         }
3138     /*FALLTHRU*/
3139     default:
3140         *mndx_p = NON_PM;
3141         break;
3142     }
3143     return (boolean) (*mndx_p != NON_PM);
3144 }
3145
3146 int
3147 select_newcham_form(mon)
3148 struct monst *mon;
3149 {
3150     int mndx = NON_PM, tryct;
3151
3152     switch (mon->cham) {
3153     case PM_SANDESTIN:
3154         if (rn2(7))
3155             mndx = pick_nasty();
3156         break;
3157     case PM_DOPPELGANGER:
3158         if (!rn2(7)) {
3159             mndx = pick_nasty();
3160         } else if (rn2(3)) { /* role monsters */
3161             mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
3162         } else if (!rn2(3)) { /* quest guardians */
3163             mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
3164             /* avoid own role's guardian */
3165             if (mndx == urole.guardnum)
3166                 mndx = NON_PM;
3167         } else { /* general humanoids */
3168             tryct = 5;
3169             do {
3170                 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3171                 if (humanoid(&mons[mndx]) && polyok(&mons[mndx]))
3172                     break;
3173             } while (--tryct > 0);
3174             if (!tryct)
3175                 mndx = NON_PM;
3176         }
3177         break;
3178     case PM_CHAMELEON:
3179         if (!rn2(3))
3180             mndx = pick_animal();
3181         break;
3182     case PM_VLAD_THE_IMPALER:
3183     case PM_VAMPIRE_LORD:
3184     case PM_VAMPIRE:
3185         mndx = pickvampshape(mon);
3186         break;
3187     case NON_PM: /* ordinary */
3188       {
3189         struct obj *m_armr = which_armor(mon, W_ARM);
3190
3191         if (m_armr && Is_dragon_scales(m_armr))
3192             mndx = (int) (Dragon_scales_to_pm(m_armr) - mons);
3193         else if (m_armr && Is_dragon_mail(m_armr))
3194             mndx = (int) (Dragon_mail_to_pm(m_armr) - mons);
3195       }
3196         break;
3197     }
3198     /* for debugging: allow control of polymorphed monster */
3199     if (wizard && iflags.mon_polycontrol) {
3200         char pprompt[BUFSZ], buf[BUFSZ];
3201         int monclass;
3202
3203         Sprintf(pprompt, "Change %s @ <%d,%d> into what kind of monster?",
3204                 noit_mon_nam(mon), (int) mon->mx, (int) mon->my);
3205         tryct = 5;
3206         do {
3207             monclass = 0;
3208             getlin(pprompt, buf);
3209             mungspaces(buf);
3210             /* for ESC, take form selected above (might be NON_PM) */
3211             if (*buf == '\033')
3212                 break;
3213             /* for "*", use NON_PM to pick an arbitrary shape below */
3214             if (!strcmp(buf, "*") || !strcmp(buf, "random")) {
3215                 mndx = NON_PM;
3216                 break;
3217             }
3218             mndx = name_to_mon(buf);
3219             if (mndx == NON_PM) {
3220                 /* didn't get a type, so check whether it's a class
3221                    (single letter or text match with def_monsyms[]) */
3222                 monclass = name_to_monclass(buf, &mndx);
3223                 if (monclass && mndx == NON_PM)
3224                     mndx = mkclass_poly(monclass);
3225             }
3226             if (mndx >= LOW_PM) {
3227                 /* got a specific type of monster; use it if we can */
3228                 if (validvamp(mon, &mndx, monclass))
3229                     break;
3230                 /* can't; revert to random in case we exhaust tryct */
3231                 mndx = NON_PM;
3232             }
3233
3234             pline("It can't become that.");
3235         } while (--tryct > 0);
3236         if (!tryct)
3237             pline1(thats_enough_tries);
3238         if (is_vampshifter(mon) && !validvamp(mon, &mndx, monclass))
3239             mndx = pickvampshape(mon); /* don't resort to arbitrary */
3240     }
3241
3242     /* if no form was specified above, pick one at random now */
3243     if (mndx == NON_PM) {
3244         tryct = 50;
3245         do {
3246             mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3247         } while (--tryct > 0 && !validspecmon(mon, mndx)
3248                  /* try harder to select uppercase monster on rogue level */
3249                  && (tryct > 40 && Is_rogue_level(&u.uz)
3250                      && !isupper((uchar) mons[mndx].mlet)));
3251     }
3252     return mndx;
3253 }
3254
3255 /* this used to be inline within newcham() but monpolycontrol needs it too */
3256 STATIC_OVL struct permonst *
3257 accept_newcham_form(mndx)
3258 int mndx;
3259 {
3260     struct permonst *mdat;
3261
3262     if (mndx == NON_PM)
3263         return 0;
3264     mdat = &mons[mndx];
3265     if ((mvitals[mndx].mvflags & G_GENOD) != 0)
3266         return 0;
3267     if (is_placeholder(mdat))
3268         return 0;
3269     /* select_newcham_form() might deliberately pick a player
3270        character type (random selection never does) which
3271        polyok() rejects, so we need a special case here */
3272     if (is_mplayer(mdat))
3273         return mdat;
3274     /* polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops */
3275     return polyok(mdat) ? mdat : 0;
3276 }
3277
3278 void
3279 mgender_from_permonst(mtmp, mdat)
3280 struct monst *mtmp;
3281 struct permonst *mdat;
3282 {
3283     if (is_male(mdat)) {
3284         if (mtmp->female)
3285             mtmp->female = FALSE;
3286     } else if (is_female(mdat)) {
3287         if (!mtmp->female)
3288             mtmp->female = TRUE;
3289     } else if (!is_neuter(mdat)) {
3290         if (!rn2(10))
3291             mtmp->female = !mtmp->female;
3292     }
3293 }
3294
3295 /* make a chameleon take on another shape, or a polymorph target
3296    (possibly self-inflicted) become a different monster;
3297    returns 1 if it actually changes form */
3298 int
3299 newcham(mtmp, mdat, polyspot, msg)
3300 struct monst *mtmp;
3301 struct permonst *mdat;
3302 boolean polyspot; /* change is the result of wand or spell of polymorph */
3303 boolean msg;      /* "The oldmon turns into a newmon!" */
3304 {
3305     int hpn, hpd;
3306     int mndx, tryct;
3307     struct permonst *olddata = mtmp->data;
3308     char oldname[BUFSZ], newname[BUFSZ];
3309
3310     /* Riders are immune to polymorph and green slime */
3311     if (is_rider(mtmp->data))
3312         return 0;
3313
3314     if (msg) {
3315         /* like Monnam() but never mention saddle */
3316         Strcpy(oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3317                                  SUPPRESS_SADDLE, FALSE));
3318         oldname[0] = highc(oldname[0]);
3319     }
3320
3321     /* mdat = 0 -> caller wants a random monster shape */
3322     if (mdat == 0) {
3323         /* select_newcham_form() loops when resorting to random but
3324            it doesn't always pick that so we still retry here too */
3325         tryct = 20;
3326         do {
3327             mndx = select_newcham_form(mtmp);
3328             mdat = accept_newcham_form(mndx);
3329             /* for the first several tries we require upper-case on
3330                the rogue level (after that, we take whatever we get) */
3331             if (tryct > 15 && Is_rogue_level(&u.uz)
3332                 && mdat && !isupper((uchar) mdat->mlet))
3333                 mdat = 0;
3334             if (mdat)
3335                 break;
3336         } while (--tryct > 0);
3337         if (!tryct)
3338             return 0;
3339     } else if (mvitals[monsndx(mdat)].mvflags & G_GENOD)
3340         return 0; /* passed in mdat is genocided */
3341
3342     mgender_from_permonst(mtmp, mdat);
3343
3344     if (In_endgame(&u.uz) && is_mplayer(olddata) && has_mname(mtmp)) {
3345         /* mplayers start out as "Foo the Bar", but some of the
3346          * titles are inappropriate when polymorphed, particularly
3347          * into the opposite sex.  players don't use ranks when
3348          * polymorphed, so dropping the rank for mplayers seems
3349          * reasonable.
3350          */
3351         char *p = index(MNAME(mtmp), ' ');
3352
3353         if (p)
3354             *p = '\0';
3355     }
3356
3357     if (mdat == mtmp->data)
3358         return 0; /* still the same monster */
3359
3360     if (mtmp->wormno) { /* throw tail away */
3361         wormgone(mtmp);
3362         place_monster(mtmp, mtmp->mx, mtmp->my);
3363     }
3364     if (mtmp->m_ap_type && mdat->mlet != S_MIMIC)
3365         seemimic(mtmp); /* revert to normal monster */
3366
3367     /* (this code used to try to adjust the monster's health based on
3368        a normal one of its type but there are too many special cases
3369        which need to handled in order to do that correctly, so just
3370        give the new form the same proportion of HP as its old one had) */
3371     hpn = mtmp->mhp;
3372     hpd = mtmp->mhpmax;
3373     /* set level and hit points */
3374     newmonhp(mtmp, monsndx(mdat));
3375     /* new hp: same fraction of max as before */
3376 #ifndef LINT
3377     mtmp->mhp = (int) (((long) hpn * (long) mtmp->mhp) / (long) hpd);
3378 #endif
3379     /* sanity check (potential overflow) */
3380     if (mtmp->mhp < 0 || mtmp->mhp > mtmp->mhpmax)
3381         mtmp->mhp = mtmp->mhpmax;
3382     /* unlikely but not impossible; a 1HD creature with 1HP that changes
3383        into a 0HD creature will require this statement */
3384     if (!mtmp->mhp)
3385         mtmp->mhp = 1;
3386
3387     /* take on the new form... */
3388     set_mon_data(mtmp, mdat, 0);
3389
3390     if (emits_light(olddata) != emits_light(mtmp->data)) {
3391         /* used to give light, now doesn't, or vice versa,
3392            or light's range has changed */
3393         if (emits_light(olddata))
3394             del_light_source(LS_MONSTER, monst_to_any(mtmp));
3395         if (emits_light(mtmp->data))
3396             new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
3397                              LS_MONSTER, monst_to_any(mtmp));
3398     }
3399     if (!mtmp->perminvis || pm_invisible(olddata))
3400         mtmp->perminvis = pm_invisible(mdat);
3401     mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
3402     if (mtmp->mundetected)
3403         (void) hideunder(mtmp);
3404     if (u.ustuck == mtmp) {
3405         if (u.uswallow) {
3406             if (!attacktype(mdat, AT_ENGL)) {
3407                 /* Does mdat care? */
3408                 if (!noncorporeal(mdat) && !amorphous(mdat)
3409                     && !is_whirly(mdat) && (mdat != &mons[PM_YELLOW_LIGHT])) {
3410 #if 0 /*JP*/
3411                     You("break out of %s%s!", mon_nam(mtmp),
3412                         (is_animal(mdat) ? "'s stomach" : ""));
3413 #else
3414                     You("%s%s\82ð\94j\82è\8fo\82½\81I", mon_nam(mtmp),
3415                         (is_animal(mdat) ? "\82Ì\88Ý\91Ü" : ""));
3416 #endif
3417                     mtmp->mhp = 1; /* almost dead */
3418                 }
3419                 expels(mtmp, olddata, FALSE);
3420             } else {
3421                 /* update swallow glyphs for new monster */
3422                 swallowed(0);
3423             }
3424         } else if (!sticks(mdat) && !sticks(youmonst.data))
3425             unstuck(mtmp);
3426     }
3427
3428 #ifndef DCC30_BUG
3429     if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
3430 #else
3431     /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
3432      * same expression.
3433      */
3434     if (mdat == &mons[PM_LONG_WORM]
3435         && (mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
3436 #endif
3437         /* we can now create worms with tails - 11/91 */
3438         initworm(mtmp, rn2(5));
3439         if (count_wsegs(mtmp))
3440             place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
3441     }
3442
3443     newsym(mtmp->mx, mtmp->my);
3444
3445     if (msg) {
3446         char *save_mname = 0;
3447
3448         if (has_mname(mtmp)) {
3449             save_mname = MNAME(mtmp);
3450             MNAME(mtmp) = (char *) 0;
3451         }
3452 #if 0 /*JP*/
3453         Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3454                             ? "slime"
3455                             : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3456                                        SUPPRESS_SADDLE, FALSE));
3457 #else
3458         Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3459                             ? "\83X\83\89\83C\83\80"
3460                             : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3461                                        SUPPRESS_SADDLE, FALSE));
3462 #endif
3463 /*JP
3464         if (!strcmpi(oldname, "it") && !strcmpi(newname, "it"))
3465 */
3466         if (!strcmpi(oldname, "\89½\8eÒ\82©") && !strcmpi(newname, "\89½\8eÒ\82©"))
3467             (void) usmellmon(mdat);
3468         else
3469 /*JP
3470             pline("%s turns into %s!", oldname, newname);
3471 */
3472             pline("%s\82Í%s\82É\82È\82Á\82½\81I", oldname, newname);
3473         if (save_mname)
3474             MNAME(mtmp) = save_mname;
3475     }
3476
3477     possibly_unwield(mtmp, polyspot); /* might lose use of weapon */
3478     mon_break_armor(mtmp, polyspot);
3479     if (!(mtmp->misc_worn_check & W_ARMG))
3480 #if 0 /*JP*/
3481         mselftouch(mtmp, "No longer petrify-resistant, ",
3482                    !context.mon_moving);
3483 #else
3484         mselftouch(mtmp, "\90Î\89»\82Ö\82Ì\92ï\8dR\97Í\82ª\82È\82­\82È\82Á\82Ä\81C",
3485                    !context.mon_moving);
3486 #endif
3487     m_dowear(mtmp, FALSE);
3488
3489     /* This ought to re-test can_carry() on each item in the inventory
3490      * rather than just checking ex-giants & boulders, but that'd be
3491      * pretty expensive to perform.  If implemented, then perhaps
3492      * minvent should be sorted in order to drop heaviest items first.
3493      */
3494     /* former giants can't continue carrying boulders */
3495     if (mtmp->minvent && !throws_rocks(mdat)) {
3496         register struct obj *otmp, *otmp2;
3497
3498         for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
3499             otmp2 = otmp->nobj;
3500             if (otmp->otyp == BOULDER) {
3501                 /* this keeps otmp from being polymorphed in the
3502                    same zap that the monster that held it is polymorphed */
3503                 if (polyspot)
3504                     bypass_obj(otmp);
3505                 obj_extract_self(otmp);
3506                 /* probably ought to give some "drop" message here */
3507                 if (flooreffects(otmp, mtmp->mx, mtmp->my, ""))
3508                     continue;
3509                 place_object(otmp, mtmp->mx, mtmp->my);
3510             }
3511         }
3512     }
3513
3514     return 1;
3515 }
3516
3517 /* sometimes an egg will be special */
3518 #define BREEDER_EGG (!rn2(77))
3519
3520 /*
3521  * Determine if the given monster number can be hatched from an egg.
3522  * Return the monster number to use as the egg's corpsenm.  Return
3523  * NON_PM if the given monster can't be hatched.
3524  */
3525 int
3526 can_be_hatched(mnum)
3527 int mnum;
3528 {
3529     /* ranger quest nemesis has the oviparous bit set, making it
3530        be possible to wish for eggs of that unique monster; turn
3531        such into ordinary eggs rather than forbidding them outright */
3532     if (mnum == PM_SCORPIUS)
3533         mnum = PM_SCORPION;
3534
3535     mnum = little_to_big(mnum);
3536     /*
3537      * Queen bees lay killer bee eggs (usually), but killer bees don't
3538      * grow into queen bees.  Ditto for [winged-]gargoyles.
3539      */
3540     if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE
3541         || (lays_eggs(&mons[mnum])
3542             && (BREEDER_EGG
3543                 || (mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
3544         return mnum;
3545     return NON_PM;
3546 }
3547
3548 /* type of egg laid by #sit; usually matches parent */
3549 int
3550 egg_type_from_parent(mnum, force_ordinary)
3551 int mnum; /* parent monster; caller must handle lays_eggs() check */
3552 boolean force_ordinary;
3553 {
3554     if (force_ordinary || !BREEDER_EGG) {
3555         if (mnum == PM_QUEEN_BEE)
3556             mnum = PM_KILLER_BEE;
3557         else if (mnum == PM_WINGED_GARGOYLE)
3558             mnum = PM_GARGOYLE;
3559     }
3560     return mnum;
3561 }
3562
3563 /* decide whether an egg of the indicated monster type is viable;
3564    also used to determine whether an egg or tin can be created... */
3565 boolean
3566 dead_species(m_idx, egg)
3567 int m_idx;
3568 boolean egg;
3569 {
3570     int alt_idx;
3571
3572     /* generic eggs are unhatchable and have corpsenm of NON_PM */
3573     if (m_idx < LOW_PM)
3574         return TRUE;
3575     /*
3576      * For monsters with both baby and adult forms, genociding either
3577      * form kills all eggs of that monster.  Monsters with more than
3578      * two forms (small->large->giant mimics) are more or less ignored;
3579      * fortunately, none of them have eggs.  Species extinction due to
3580      * overpopulation does not kill eggs.
3581      */
3582     alt_idx = egg ? big_to_little(m_idx) : m_idx;
3583     return (boolean) ((mvitals[m_idx].mvflags & G_GENOD) != 0
3584                       || (mvitals[alt_idx].mvflags & G_GENOD) != 0);
3585 }
3586
3587 /* kill off any eggs of genocided monsters */
3588 STATIC_OVL void
3589 kill_eggs(obj_list)
3590 struct obj *obj_list;
3591 {
3592     struct obj *otmp;
3593
3594     for (otmp = obj_list; otmp; otmp = otmp->nobj)
3595         if (otmp->otyp == EGG) {
3596             if (dead_species(otmp->corpsenm, TRUE)) {
3597                 /*
3598                  * It seems we could also just catch this when
3599                  * it attempted to hatch, so we wouldn't have to
3600                  * search all of the objlists.. or stop all
3601                  * hatch timers based on a corpsenm.
3602                  */
3603                 kill_egg(otmp);
3604             }
3605 #if 0 /* not used */
3606         } else if (otmp->otyp == TIN) {
3607             if (dead_species(otmp->corpsenm, FALSE))
3608                 otmp->corpsenm = NON_PM; /* empty tin */
3609         } else if (otmp->otyp == CORPSE) {
3610             if (dead_species(otmp->corpsenm, FALSE))
3611                 ; /* not yet implemented... */
3612 #endif
3613         } else if (Has_contents(otmp)) {
3614             kill_eggs(otmp->cobj);
3615         }
3616 }
3617
3618 /* kill all members of genocided species */
3619 void
3620 kill_genocided_monsters()
3621 {
3622     struct monst *mtmp, *mtmp2;
3623     boolean kill_cham;
3624     int mndx;
3625
3626     /*
3627      * Called during genocide, and again upon level change.  The latter
3628      * catches up with any migrating monsters as they finally arrive at
3629      * their intended destinations, so possessions get deposited there.
3630      *
3631      * Chameleon handling:
3632      *  1) if chameleons have been genocided, destroy them
3633      *     regardless of current form;
3634      *  2) otherwise, force every chameleon which is imitating
3635      *     any genocided species to take on a new form.
3636      */
3637     for (mtmp = fmon; mtmp; mtmp = mtmp2) {
3638         mtmp2 = mtmp->nmon;
3639         if (DEADMONSTER(mtmp))
3640             continue;
3641         mndx = monsndx(mtmp->data);
3642         kill_cham = (mtmp->cham >= LOW_PM
3643                      && (mvitals[mtmp->cham].mvflags & G_GENOD));
3644         if ((mvitals[mndx].mvflags & G_GENOD) || kill_cham) {
3645             if (mtmp->cham >= LOW_PM && !kill_cham)
3646                 (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
3647             else
3648                 mondead(mtmp);
3649         }
3650         if (mtmp->minvent)
3651             kill_eggs(mtmp->minvent);
3652     }
3653
3654     kill_eggs(invent);
3655     kill_eggs(fobj);
3656     kill_eggs(migrating_objs);
3657     kill_eggs(level.buriedobjlist);
3658 }
3659
3660 void
3661 golemeffects(mon, damtype, dam)
3662 register struct monst *mon;
3663 int damtype, dam;
3664 {
3665     int heal = 0, slow = 0;
3666
3667     if (mon->data == &mons[PM_FLESH_GOLEM]) {
3668         if (damtype == AD_ELEC)
3669             heal = (dam + 5) / 6;
3670         else if (damtype == AD_FIRE || damtype == AD_COLD)
3671             slow = 1;
3672     } else if (mon->data == &mons[PM_IRON_GOLEM]) {
3673         if (damtype == AD_ELEC)
3674             slow = 1;
3675         else if (damtype == AD_FIRE)
3676             heal = dam;
3677     } else {
3678         return;
3679     }
3680     if (slow) {
3681         if (mon->mspeed != MSLOW)
3682             mon_adjust_speed(mon, -1, (struct obj *) 0);
3683     }
3684     if (heal) {
3685         if (mon->mhp < mon->mhpmax) {
3686             mon->mhp += heal;
3687             if (mon->mhp > mon->mhpmax)
3688                 mon->mhp = mon->mhpmax;
3689             if (cansee(mon->mx, mon->my))
3690 /*JP
3691                 pline("%s seems healthier.", Monnam(mon));
3692 */
3693                 pline("%s\82Í\8c³\8bC\82É\82È\82Á\82½\82æ\82¤\82É\8c©\82¦\82é\81D", Monnam(mon));
3694         }
3695     }
3696 }
3697
3698 boolean
3699 angry_guards(silent)
3700 boolean silent;
3701 {
3702     struct monst *mtmp;
3703     int ct = 0, nct = 0, sct = 0, slct = 0;
3704
3705     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3706         if (DEADMONSTER(mtmp))
3707             continue;
3708         if (is_watch(mtmp->data) && mtmp->mpeaceful) {
3709             ct++;
3710             if (cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
3711                 if (distu(mtmp->mx, mtmp->my) == 2)
3712                     nct++;
3713                 else
3714                     sct++;
3715             }
3716             if (mtmp->msleeping || mtmp->mfrozen) {
3717                 slct++;
3718                 mtmp->msleeping = mtmp->mfrozen = 0;
3719             }
3720             mtmp->mpeaceful = 0;
3721         }
3722     }
3723     if (ct) {
3724         if (!silent) { /* do we want pline msgs? */
3725             if (slct)
3726 #if 0 /*JP*/
3727                 pline_The("guard%s wake%s up!", slct > 1 ? "s" : "",
3728                           slct == 1 ? "s" : "");
3729 #else
3730                 pline("\94Ô\95º\82Í\96Ú\82ð\8ao\82Ü\82µ\82½\81I");
3731 #endif
3732             if (nct || sct) {
3733                 if (nct)
3734 #if 0 /*JP*/
3735                     pline_The("guard%s get%s angry!", nct == 1 ? "" : "s",
3736                               nct == 1 ? "s" : "");
3737 #else
3738                     pline("\94Ô\95º\82Í\93{\82Á\82½\81I");
3739 #endif
3740                 else if (!Blind)
3741 #if 0 /*JP*/
3742                     You_see("%sangry guard%s approaching!",
3743                             sct == 1 ? "an " : "", sct > 1 ? "s" : "");
3744 #else
3745                     You("\93{\82Á\82½\94Ô\95º\82ª\8bß\8añ\82Á\82Ä\82­\82é\82Ì\82ð\8c©\82½\81I");
3746 #endif
3747             } else
3748 /*JP
3749                 You_hear("the shrill sound of a guard's whistle.");
3750 */
3751                 You_hear("\94Ô\95º\82Ì\93J\82ª\82¯\82½\82½\82Ü\82µ\82­\96Â\82é\82Ì\82ð\95·\82¢\82½\81D");
3752         }
3753         return TRUE;
3754     }
3755     return FALSE;
3756 }
3757
3758 void
3759 pacify_guards()
3760 {
3761     struct monst *mtmp;
3762
3763     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3764         if (DEADMONSTER(mtmp))
3765             continue;
3766         if (is_watch(mtmp->data))
3767             mtmp->mpeaceful = 1;
3768     }
3769 }
3770
3771 void
3772 mimic_hit_msg(mtmp, otyp)
3773 struct monst *mtmp;
3774 short otyp;
3775 {
3776     short ap = mtmp->mappearance;
3777
3778     switch (mtmp->m_ap_type) {
3779     case M_AP_NOTHING:
3780     case M_AP_FURNITURE:
3781     case M_AP_MONSTER:
3782         break;
3783     case M_AP_OBJECT:
3784         if (otyp == SPE_HEALING || otyp == SPE_EXTRA_HEALING) {
3785 #if 0 /*JP*/
3786             pline("%s seems a more vivid %s than before.",
3787                   The(simple_typename(ap)),
3788                   c_obj_colors[objects[ap].oc_color]);
3789 #else
3790             pline("%s\82Í\88È\91O\82æ\82è\82æ\82è\91N\82â\82©\82È%s\82É\82È\82Á\82½\82æ\82¤\82¾\81D",
3791                   The(simple_typename(ap)),
3792                   c_obj_colors[objects[ap].oc_color]);
3793 #endif
3794         }
3795         break;
3796     }
3797 }
3798
3799 boolean
3800 usmellmon(mdat)
3801 struct permonst *mdat;
3802 {
3803     int mndx;
3804     boolean nonspecific = FALSE;
3805     boolean msg_given = FALSE;
3806
3807     if (mdat) {
3808         if (!olfaction(youmonst.data))
3809             return FALSE;
3810         mndx = monsndx(mdat);
3811         switch (mndx) {
3812         case PM_ROTHE:
3813         case PM_MINOTAUR:
3814 /*JP
3815             You("notice a bovine smell.");
3816 */
3817             You("\8b\8d\82Ì\82æ\82¤\82È\82É\82¨\82¢\82É\8bC\95t\82¢\82½\81D");
3818             msg_given = TRUE;
3819             break;
3820         case PM_CAVEMAN:
3821         case PM_CAVEWOMAN:
3822         case PM_BARBARIAN:
3823         case PM_NEANDERTHAL:
3824 /*JP
3825             You("smell body odor.");
3826 */
3827             pline("\91Ì\8fL\82Ì\82æ\82¤\82È\82É\82¨\82¢\82ª\82µ\82½\81D");
3828             msg_given = TRUE;
3829             break;
3830         /*
3831         case PM_PESTILENCE:
3832         case PM_FAMINE:
3833         case PM_DEATH:
3834             break;
3835         */
3836         case PM_HORNED_DEVIL:
3837         case PM_BALROG:
3838         case PM_ASMODEUS:
3839         case PM_DISPATER:
3840         case PM_YEENOGHU:
3841         case PM_ORCUS:
3842             break;
3843         case PM_HUMAN_WEREJACKAL:
3844         case PM_HUMAN_WERERAT:
3845         case PM_HUMAN_WEREWOLF:
3846         case PM_WEREJACKAL:
3847         case PM_WERERAT:
3848         case PM_WEREWOLF:
3849         case PM_OWLBEAR:
3850 /*JP
3851             You("detect an odor reminiscent of an animal's den.");
3852 */
3853             pline("\93®\95¨\82Ì\82Ë\82®\82ç\82ð\8ev\82¢\8fo\82·\82æ\82¤\82È\82É\82¨\82¢\82ª\82µ\82½\81D");
3854             msg_given = TRUE;
3855             break;
3856         /*
3857         case PM_PURPLE_WORM:
3858             break;
3859         */
3860         case PM_STEAM_VORTEX:
3861 /*JP
3862             You("smell steam.");
3863 */
3864             pline("\8fö\8bC\82Ì\82É\82¨\82¢\82ª\82µ\82½\81D");
3865             msg_given = TRUE;
3866             break;
3867         case PM_GREEN_SLIME:
3868 /*JP
3869             pline("%s stinks.", Something);
3870 */
3871             pline("\88«\8fL\82ª\82µ\82½\81D");
3872             msg_given = TRUE;
3873             break;
3874         case PM_VIOLET_FUNGUS:
3875         case PM_SHRIEKER:
3876 /*JP
3877             You("smell mushrooms.");
3878 */
3879             pline("\82«\82Ì\82±\82Ì\82É\82¨\82¢\82ª\82µ\82½\81D");
3880             msg_given = TRUE;
3881             break;
3882         /* These are here to avoid triggering the
3883            nonspecific treatment through the default case below*/
3884         case PM_WHITE_UNICORN:
3885         case PM_GRAY_UNICORN:
3886         case PM_BLACK_UNICORN:
3887         case PM_JELLYFISH:
3888             break;
3889         default:
3890             nonspecific = TRUE;
3891             break;
3892         }
3893
3894         if (nonspecific)
3895             switch (mdat->mlet) {
3896             case S_DOG:
3897 /*JP
3898                 You("notice a dog smell.");
3899 */
3900                 You("\8c¢\82Ì\82É\82¨\82¢\82É\8bC\95t\82¢\82½\81D");
3901                 msg_given = TRUE;
3902                 break;
3903             case S_DRAGON:
3904 /*JP
3905                 You("smell a dragon!");
3906 */
3907                 pline("\83h\83\89\83S\83\93\82Ì\82É\82¨\82¢\82ª\82·\82é\81I");
3908                 msg_given = TRUE;
3909                 break;
3910             case S_FUNGUS:
3911 /*JP
3912                 pline("%s smells moldy.", Something);
3913 */
3914                 pline("\89½\82©\91Û\82Ì\82æ\82¤\82È\82É\82¨\82¢\82ª\82·\82é\81D");
3915                 msg_given = TRUE;
3916                 break;
3917             case S_UNICORN:
3918 #if 0 /*JP*/
3919                 You("detect a%s odor reminiscent of a stable.",
3920                     (mndx == PM_PONY) ? "n" : " strong");
3921 #else
3922                 pline("\94n\8f¬\89®\82ð\8ev\82¢\8fo\82·\82æ\82¤\82È%s\82É\82¨\82¢\82ª\82µ\82½\81D",
3923                     (mndx == PM_PONY) ? "" : "\8b­\82¢");
3924 #endif
3925                 msg_given = TRUE;
3926                 break;
3927             case S_ZOMBIE:
3928 /*JP
3929                 You("smell rotting flesh.");
3930 */
3931                 pline("\95\85\82Á\82½\93÷\82Ì\82É\82¨\82¢\82ª\82µ\82½\81D");
3932                 msg_given = TRUE;
3933                 break;
3934             case S_EEL:
3935 /*JP
3936                 You("smell fish.");
3937 */
3938                 pline("\8b\9b\82Ì\82É\82¨\82¢\82ª\82µ\82½\81D");
3939                 msg_given = TRUE;
3940                 break;
3941             case S_ORC:
3942                 if (maybe_polyd(is_orc(youmonst.data), Race_if(PM_ORC)))
3943 /*JP
3944                     You("notice an attractive smell.");
3945 */
3946                     You("\96£\97Í\93I\82È\82É\82¨\82¢\82É\8bC\95t\82¢\82½\81D");
3947                 else
3948 /*JP
3949                     pline("A foul stench makes you feel a little nauseated.");
3950 */
3951                     pline("\82Þ\82©\82Â\82­\82æ\82¤\82È\88«\8fL\82Å\8f­\82µ\8bC\95ª\82ª\88«\82­\82È\82Á\82½\81D");
3952                 msg_given = TRUE;
3953                 break;
3954             default:
3955                 break;
3956             }
3957     }
3958     return msg_given ? TRUE : FALSE;
3959 }
3960
3961 /*mon.c*/