OSDN Git Service

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