OSDN Git Service

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