# Source:NetHack 3.6.1/src/mon.c
Below is the full text to mon.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/mon.c#line123]], for example.

Contents

1 Top of file
2 sanity_check_single_mon
3 mon_sanity_check
4 undead_to_corpse
5 genus
6 pm_to_cham
7 make_corpse
8 minliquid
9 mcalcmove
10 mcalcdistress
11 movemon
12 meatmetal
13 meatobj
14 mpickgold
15 mpickstuff
16 curr_mon_load
17 max_mon_load
18 can_carry
19 mfndpos
20 mm_aggression
21 mm_displacement
22 monnear
23 dmonsfree
24 replmon
25 relmon
26 copy_mextra
27 dealloc_mextra
28 dealloc_monst
29 m_detach
30 mlifesaver
31 lifesaved_monster
32 mondead
33 corpse_chance
34 mondied
35 mongone
36 monstone
37 monkilled
38 unstuck
39 killed
40 xkilled
41 mon_to_stone
42 vamp_stone
43 m_into_limbo
44 mnexto
45 maybe_mnexto
46 mnearto
47 m_respond
48 setmangry
49 wakeup
50 wake_nearby
51 wake_nearto
52 seemimic
53 rescham
54 restartcham
55 restore_cham
56 restrap
57 hideunder
58 hide_monst
59 mon_animal_list
60 pick_animal
61 decide_to_shapeshift
62 pickvampshape
63 isspecmon
64 validspecmon
65 validvamp
66 select_newcham_form
67 accept_newcham_form
68 mgender_from_permonst
69 newcham
70 can_be_hatched
71 egg_type_from_parent
72 dead_species
73 kill_eggs
74 kill_genocided_monsters
75 golemeffects
76 angry_guards
77 pacify_guards
78 mimic_hit_msg
79 usmellmon

Top of file
&nbsp;/* NetHack 3.6	mon.c	$NHDT-Date: 1522540516 2018/03/31 23:55:16 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.250 $ */
&nbsp;/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
&nbsp;/*-Copyright (c) Derek S. Ray, 2015. */
&nbsp;/* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

&nbsp;
&nbsp;/* If you're using precompiled headers, you don't want this either */
&nbsp;#ifdef MICROPORT_BUG
&nbsp;#define MKROOM_H
&nbsp;#endif
&nbsp;
&nbsp;#include "hack.h"
&nbsp;#include "mfndpos.h"
&nbsp;#include 

<ctype.h>
&nbsp;
&nbsp;STATIC_VAR boolean vamp_rise_msg, disintegested;
&nbsp;
&nbsp;STATIC_DCL void FDECL(sanity_check_single_mon, (struct monst *, BOOLEAN_P,
&nbsp;                                                const char *));
&nbsp;STATIC_DCL boolean FDECL(restrap, (struct monst *));
&nbsp;STATIC_DCL long FDECL(mm_aggression, (struct monst *, struct monst *));
&nbsp;STATIC_DCL long FDECL(mm_displacement, (struct monst *, struct monst *));
&nbsp;STATIC_DCL int NDECL(pick_animal);
&nbsp;STATIC_DCL void FDECL(kill_eggs, (struct obj *));
&nbsp;STATIC_DCL int FDECL(pickvampshape, (struct monst *));
&nbsp;STATIC_DCL boolean FDECL(isspecmon, (struct monst *));
&nbsp;STATIC_DCL boolean FDECL(validspecmon, (struct monst *, int));
&nbsp;STATIC_DCL struct permonst *FDECL(accept_newcham_form, (int));
&nbsp;STATIC_DCL struct obj *FDECL(make_corpse, (struct monst *, unsigned));
&nbsp;STATIC_DCL void FDECL(m_detach, (struct monst *, struct permonst *));
&nbsp;STATIC_DCL void FDECL(lifesaved_monster, (struct monst *));
&nbsp;
&nbsp;#define LEVEL_SPECIFIC_NOCORPSE(mdat) \
&nbsp;    (Is_rogue_level(&amp;u.uz)            \
&nbsp;     || (level.flags.graveyard &amp;&amp; is_undead(mdat) &amp;&amp; rn2(3)))
&nbsp;
&nbsp;#if 0
&nbsp;/* part of the original warning code which was replaced in 3.3.1 */
&nbsp;const char *warnings[] = {
&nbsp;    "white", "pink", "red", "ruby", "purple", "black"
&nbsp;};
&nbsp;#endif /* 0 */
&nbsp;
&nbsp;
sanity_check_single_mon
&nbsp;void
&nbsp;sanity_check_single_mon(mtmp, chk_geno, msg)
&nbsp;struct monst *mtmp;
&nbsp;boolean chk_geno;
&nbsp;const char *msg;
&nbsp;{
&nbsp;    if (DEADMONSTER(mtmp))
&nbsp;        return;
&nbsp;    if (mtmp-&gt;data &lt; &amp;mons[LOW_PM] || mtmp-&gt;data &gt;= &amp;mons[NUMMONS]) {
&nbsp;        impossible("illegal mon data %s; mnum=%d (%s)",
&nbsp;                   fmt_ptr((genericptr_t) mtmp-&gt;data), mtmp-&gt;mnum, msg);
&nbsp;    } else {
&nbsp;        int mndx = monsndx(mtmp-&gt;data);
&nbsp;
&nbsp;        if (mtmp-&gt;mnum != mndx) {
&nbsp;            impossible("monster mnum=%d, monsndx=%d (%s)",
&nbsp;                       mtmp-&gt;mnum, mndx, msg);
&nbsp;            mtmp-&gt;mnum = mndx;
&nbsp;        }
&nbsp;        if (chk_geno &amp;&amp; (mvitals[mndx].mvflags &amp; G_GENOD) != 0)
&nbsp;            impossible("genocided %s in play (%s)", mons[mndx].mname, msg);
&nbsp;    }
&nbsp;    if (mtmp-&gt;isshk &amp;&amp; !has_eshk(mtmp))
&nbsp;        impossible("shk without eshk (%s)", msg);
&nbsp;    if (mtmp-&gt;ispriest &amp;&amp; !has_epri(mtmp))
&nbsp;        impossible("priest without epri (%s)", msg);
&nbsp;    if (mtmp-&gt;isgd &amp;&amp; !has_egd(mtmp))
&nbsp;        impossible("guard without egd (%s)", msg);
&nbsp;    if (mtmp-&gt;isminion &amp;&amp; !has_emin(mtmp))
&nbsp;        impossible("minion without emin (%s)", msg);
&nbsp;    /* guardian angel on astral level is tame but has emin rather than edog */
&nbsp;    if (mtmp-&gt;mtame &amp;&amp; !has_edog(mtmp) &amp;&amp; !mtmp-&gt;isminion)
&nbsp;        impossible("pet without edog (%s)", msg);
&nbsp;}
&nbsp;
mon_sanity_check
&nbsp;void
&nbsp;mon_sanity_check()
&nbsp;{
&nbsp;    int x, y;
&nbsp;    struct monst *mtmp, *m;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        sanity_check_single_mon(mtmp, TRUE, "fmon");
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        x = mtmp-&gt;mx, y = mtmp-&gt;my;
&nbsp;        if (!isok(x, y) &amp;&amp; !(mtmp-&gt;isgd &amp;&amp; x == 0 &amp;&amp; y == 0)) {
&nbsp;            impossible("mon (%s) claims to be at &lt;%d,%d&gt;?",
&nbsp;                       fmt_ptr((genericptr_t) mtmp), x, y);
&nbsp;        } else if (mtmp == u.usteed) {
&nbsp;            /* steed is in fmon list but not on the map; its
&nbsp;               <mx,my> coordinates should match hero's location */
&nbsp;            if (x != u.ux || y != u.uy)
&nbsp;                impossible("steed (%s) claims to be at &lt;%d,%d&gt;?",
&nbsp;                           fmt_ptr((genericptr_t) mtmp), x, y);
&nbsp;        } else if (level.monsters[x][y] != mtmp) {
&nbsp;            impossible("mon (%s) at &lt;%d,%d&gt; is not there!",
&nbsp;                       fmt_ptr((genericptr_t) mtmp), x, y);
&nbsp;        } else if (mtmp-&gt;wormno) {
&nbsp;            sanity_check_worm(mtmp);
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    for (x = 0; x &lt; COLNO; x++)
&nbsp;        for (y = 0; y &lt; ROWNO; y++)
&nbsp;            if ((mtmp = level.monsters[x][y]) != 0) {
&nbsp;                for (m = fmon; m; m = m-&gt;nmon)
&nbsp;                    if (m == mtmp)
&nbsp;                        break;
&nbsp;                if (!m)
&nbsp;                    impossible("map mon (%s) at &lt;%d,%d&gt; not in fmon list!",
&nbsp;                               fmt_ptr((genericptr_t) mtmp), x, y);
&nbsp;                else if (mtmp == u.usteed)
&nbsp;                    impossible("steed (%s) is on the map at &lt;%d,%d&gt;!",
&nbsp;                               fmt_ptr((genericptr_t) mtmp), x, y);
&nbsp;                else if ((mtmp-&gt;mx != x || mtmp-&gt;my != y)
&nbsp;                         &amp;&amp; mtmp-&gt;data != &amp;mons[PM_LONG_WORM])
&nbsp;                    impossible("map mon (%s) at &lt;%d,%d&gt; is found at &lt;%d,%d&gt;?",
&nbsp;                               fmt_ptr((genericptr_t) mtmp),
&nbsp;                               mtmp-&gt;mx, mtmp-&gt;my, x, y);
&nbsp;            }
&nbsp;
&nbsp;    for (mtmp = migrating_mons; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        sanity_check_single_mon(mtmp, FALSE, "migr");
&nbsp;    }
&nbsp;}
&nbsp;
&nbsp;
undead_to_corpse
&nbsp;/* convert the monster index of an undead to its living counterpart */
&nbsp;int
&nbsp;undead_to_corpse(mndx)
&nbsp;int mndx;
&nbsp;{
&nbsp;    switch (mndx) {
&nbsp;    case PM_KOBOLD_ZOMBIE:
&nbsp;    case PM_KOBOLD_MUMMY:
&nbsp;        mndx = PM_KOBOLD;
&nbsp;        break;
&nbsp;    case PM_DWARF_ZOMBIE:
&nbsp;    case PM_DWARF_MUMMY:
&nbsp;        mndx = PM_DWARF;
&nbsp;        break;
&nbsp;    case PM_GNOME_ZOMBIE:
&nbsp;    case PM_GNOME_MUMMY:
&nbsp;        mndx = PM_GNOME;
&nbsp;        break;
&nbsp;    case PM_ORC_ZOMBIE:
&nbsp;    case PM_ORC_MUMMY:
&nbsp;        mndx = PM_ORC;
&nbsp;        break;
&nbsp;    case PM_ELF_ZOMBIE:
&nbsp;    case PM_ELF_MUMMY:
&nbsp;        mndx = PM_ELF;
&nbsp;        break;
&nbsp;    case PM_VAMPIRE:
&nbsp;    case PM_VAMPIRE_LORD:
&nbsp;#if 0 /* DEFERRED */
&nbsp;    case PM_VAMPIRE_MAGE:
&nbsp;#endif
&nbsp;    case PM_HUMAN_ZOMBIE:
&nbsp;    case PM_HUMAN_MUMMY:
&nbsp;        mndx = PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_GIANT_ZOMBIE:
&nbsp;    case PM_GIANT_MUMMY:
&nbsp;        mndx = PM_GIANT;
&nbsp;        break;
&nbsp;    case PM_ETTIN_ZOMBIE:
&nbsp;    case PM_ETTIN_MUMMY:
&nbsp;        mndx = PM_ETTIN;
&nbsp;        break;
&nbsp;    default:
&nbsp;        break;
&nbsp;    }
&nbsp;    return mndx;
&nbsp;}
&nbsp;
genus
&nbsp;/* Convert the monster index of some monsters (such as quest guardians)
&nbsp; * to their generic species type.
&nbsp; *
&nbsp; * Return associated character class monster, rather than species
&nbsp; * if mode is 1.
&nbsp; */
&nbsp;int
&nbsp;genus(mndx, mode)
&nbsp;int mndx, mode;
&nbsp;{
&nbsp;    switch (mndx) {
&nbsp;    /* Quest guardians */
&nbsp;    case PM_STUDENT:
&nbsp;        mndx = mode ? PM_ARCHEOLOGIST : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_CHIEFTAIN:
&nbsp;        mndx = mode ? PM_BARBARIAN : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_NEANDERTHAL:
&nbsp;        mndx = mode ? PM_CAVEMAN : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_ATTENDANT:
&nbsp;        mndx = mode ? PM_HEALER : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_PAGE:
&nbsp;        mndx = mode ? PM_KNIGHT : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_ABBOT:
&nbsp;        mndx = mode ? PM_MONK : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_ACOLYTE:
&nbsp;        mndx = mode ? PM_PRIEST : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_HUNTER:
&nbsp;        mndx = mode ? PM_RANGER : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_THUG:
&nbsp;        mndx = mode ? PM_ROGUE : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_ROSHI:
&nbsp;        mndx = mode ? PM_SAMURAI : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_GUIDE:
&nbsp;        mndx = mode ? PM_TOURIST : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_APPRENTICE:
&nbsp;        mndx = mode ? PM_WIZARD : PM_HUMAN;
&nbsp;        break;
&nbsp;    case PM_WARRIOR:
&nbsp;        mndx = mode ? PM_VALKYRIE : PM_HUMAN;
&nbsp;        break;
&nbsp;    default:
&nbsp;        if (mndx &gt;= LOW_PM &amp;&amp; mndx &lt; NUMMONS) {
&nbsp;            struct permonst *ptr = &amp;mons[mndx];
&nbsp;
&nbsp;            if (is_human(ptr))
&nbsp;                mndx = PM_HUMAN;
&nbsp;            else if (is_elf(ptr))
&nbsp;                mndx = PM_ELF;
&nbsp;            else if (is_dwarf(ptr))
&nbsp;                mndx = PM_DWARF;
&nbsp;            else if (is_gnome(ptr))
&nbsp;                mndx = PM_GNOME;
&nbsp;            else if (is_orc(ptr))
&nbsp;                mndx = PM_ORC;
&nbsp;        }
&nbsp;        break;
&nbsp;    }
&nbsp;    return mndx;
&nbsp;}
&nbsp;
pm_to_cham
&nbsp;/* return monster index if chameleon, or NON_PM if not */
&nbsp;int
&nbsp;pm_to_cham(mndx)
&nbsp;int mndx;
&nbsp;{
&nbsp;    int mcham = NON_PM;
&nbsp;
&nbsp;    /*
&nbsp;     * As of 3.6.0 we just check M2_SHAPESHIFTER instead of having a
&nbsp;     * big switch statement with hardcoded shapeshifter types here.
&nbsp;     */
&nbsp;    if (mndx &gt;= LOW_PM &amp;&amp; is_shapeshifter(&amp;mons[mndx]))
&nbsp;        mcham = mndx;
&nbsp;    return mcham;
&nbsp;}
&nbsp;
&nbsp;/* for deciding whether corpse will carry along full monster data */
&nbsp;#define KEEPTRAITS(mon)                                                 \
&nbsp;    ((mon)-&gt;isshk || (mon)-&gt;mtame || unique_corpstat((mon)-&gt;data)       \
&nbsp;     || is_reviver((mon)-&gt;data)                                         \
&nbsp;        /* normally quest leader will be unique, */                     \
&nbsp;        /* but he or she might have been polymorphed  */                \
&nbsp;     || (mon)-&gt;m_id == quest_status.leader_m_id                         \
&nbsp;        /* special cancellation handling for these */                   \
&nbsp;     || (dmgtype((mon)-&gt;data, AD_SEDU) || dmgtype((mon)-&gt;data, AD_SSEX)))
&nbsp;
make_corpse
&nbsp;/* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
&nbsp; * leave corpses.  Monsters which leave "special" corpses should have
&nbsp; * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
&nbsp; * etc....
&nbsp; */
&nbsp;STATIC_OVL struct obj *
&nbsp;make_corpse(mtmp, corpseflags)
&nbsp;register struct monst *mtmp;
&nbsp;unsigned corpseflags;
&nbsp;{
&nbsp;    register struct permonst *mdat = mtmp-&gt;data;
&nbsp;    int num;
&nbsp;    struct obj *obj = (struct obj *) 0;
&nbsp;    struct obj *otmp = (struct obj *) 0;
&nbsp;    int x = mtmp-&gt;mx, y = mtmp-&gt;my;
&nbsp;    int mndx = monsndx(mdat);
&nbsp;    unsigned corpstatflags = corpseflags;
&nbsp;    boolean burythem = ((corpstatflags &amp; CORPSTAT_BURIED) != 0);
&nbsp;
&nbsp;    switch (mndx) {
&nbsp;    case PM_GRAY_DRAGON:
&nbsp;    case PM_SILVER_DRAGON:
&nbsp;#if 0 /* DEFERRED */
&nbsp;    case PM_SHIMMERING_DRAGON:
&nbsp;#endif
&nbsp;    case PM_RED_DRAGON:
&nbsp;    case PM_ORANGE_DRAGON:
&nbsp;    case PM_WHITE_DRAGON:
&nbsp;    case PM_BLACK_DRAGON:
&nbsp;    case PM_BLUE_DRAGON:
&nbsp;    case PM_GREEN_DRAGON:
&nbsp;    case PM_YELLOW_DRAGON:
&nbsp;        /* Make dragon scales.  This assumes that the order of the
&nbsp;           dragons is the same as the order of the scales. */
&nbsp;        if (!rn2(mtmp-&gt;mrevived ? 20 : 3)) {
&nbsp;            num = GRAY_DRAGON_SCALES + monsndx(mdat) - PM_GRAY_DRAGON;
&nbsp;            obj = mksobj_at(num, x, y, FALSE, FALSE);
&nbsp;            obj-&gt;spe = 0;
&nbsp;            obj-&gt;cursed = obj-&gt;blessed = FALSE;
&nbsp;        }
&nbsp;        goto default_1;
&nbsp;    case PM_WHITE_UNICORN:
&nbsp;    case PM_GRAY_UNICORN:
&nbsp;    case PM_BLACK_UNICORN:
&nbsp;        if (mtmp-&gt;mrevived &amp;&amp; rn2(2)) {
&nbsp;            if (canseemon(mtmp))
&nbsp;                pline("%s recently regrown horn crumbles to dust.",
&nbsp;                      s_suffix(Monnam(mtmp)));
&nbsp;        } else {
&nbsp;            obj = mksobj_at(UNICORN_HORN, x, y, TRUE, FALSE);
&nbsp;            if (obj &amp;&amp; mtmp-&gt;mrevived)
&nbsp;                obj-&gt;degraded_horn = 1;
&nbsp;        }
&nbsp;        goto default_1;
&nbsp;    case PM_LONG_WORM:
&nbsp;        (void) mksobj_at(WORM_TOOTH, x, y, TRUE, FALSE);
&nbsp;        goto default_1;
&nbsp;    case PM_VAMPIRE:
&nbsp;    case PM_VAMPIRE_LORD:
&nbsp;        /* include mtmp in the mkcorpstat() call */
&nbsp;        num = undead_to_corpse(mndx);
&nbsp;        corpstatflags |= CORPSTAT_INIT;
&nbsp;        obj = mkcorpstat(CORPSE, mtmp, &amp;mons[num], x, y, corpstatflags);
&nbsp;        obj-&gt;age -= 100; /* this is an *OLD* corpse */
&nbsp;        break;
&nbsp;    case PM_KOBOLD_MUMMY:
&nbsp;    case PM_DWARF_MUMMY:
&nbsp;    case PM_GNOME_MUMMY:
&nbsp;    case PM_ORC_MUMMY:
&nbsp;    case PM_ELF_MUMMY:
&nbsp;    case PM_HUMAN_MUMMY:
&nbsp;    case PM_GIANT_MUMMY:
&nbsp;    case PM_ETTIN_MUMMY:
&nbsp;    case PM_KOBOLD_ZOMBIE:
&nbsp;    case PM_DWARF_ZOMBIE:
&nbsp;    case PM_GNOME_ZOMBIE:
&nbsp;    case PM_ORC_ZOMBIE:
&nbsp;    case PM_ELF_ZOMBIE:
&nbsp;    case PM_HUMAN_ZOMBIE:
&nbsp;    case PM_GIANT_ZOMBIE:
&nbsp;    case PM_ETTIN_ZOMBIE:
&nbsp;        num = undead_to_corpse(mndx);
&nbsp;        corpstatflags |= CORPSTAT_INIT;
&nbsp;        obj = mkcorpstat(CORPSE, mtmp, &amp;mons[num], x, y, corpstatflags);
&nbsp;        obj-&gt;age -= 100; /* this is an *OLD* corpse */
&nbsp;        break;
&nbsp;    case PM_IRON_GOLEM:
&nbsp;        num = d(2, 6);
&nbsp;        while (num--)
&nbsp;            obj = mksobj_at(IRON_CHAIN, x, y, TRUE, FALSE);
&nbsp;        free_mname(mtmp); /* don't christen obj */
&nbsp;        break;
&nbsp;    case PM_GLASS_GOLEM:
&nbsp;        num = d(2, 4); /* very low chance of creating all glass gems */
&nbsp;        while (num--)
&nbsp;            obj = mksobj_at((LAST_GEM + rnd(9)), x, y, TRUE, FALSE);
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    case PM_CLAY_GOLEM:
&nbsp;        obj = mksobj_at(ROCK, x, y, FALSE, FALSE);
&nbsp;        obj-&gt;quan = (long) (rn2(20) + 50);
&nbsp;        obj-&gt;owt = weight(obj);
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    case PM_STONE_GOLEM:
&nbsp;        corpstatflags &amp;= ~CORPSTAT_INIT;
&nbsp;        obj =
&nbsp;            mkcorpstat(STATUE, (struct monst *) 0, mdat, x, y, corpstatflags);
&nbsp;        break;
&nbsp;    case PM_WOOD_GOLEM:
&nbsp;        num = d(2, 4);
&nbsp;        while (num--) {
&nbsp;            obj = mksobj_at(QUARTERSTAFF, x, y, TRUE, FALSE);
&nbsp;        }
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    case PM_LEATHER_GOLEM:
&nbsp;        num = d(2, 4);
&nbsp;        while (num--)
&nbsp;            obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE, FALSE);
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    case PM_GOLD_GOLEM:
&nbsp;        /* Good luck gives more coins */
&nbsp;        obj = mkgold((long) (200 - rnl(101)), x, y);
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    case PM_PAPER_GOLEM:
&nbsp;        num = rnd(4);
&nbsp;        while (num--)
&nbsp;            obj = mksobj_at(SCR_BLANK_PAPER, x, y, TRUE, FALSE);
&nbsp;        free_mname(mtmp);
&nbsp;        break;
&nbsp;    /* expired puddings will congeal into a large blob;
&nbsp;       like dragons, relies on the order remaining consistent */
&nbsp;    case PM_GRAY_OOZE:
&nbsp;    case PM_BROWN_PUDDING:
&nbsp;    case PM_GREEN_SLIME:
&nbsp;    case PM_BLACK_PUDDING:
&nbsp;        /* we have to do this here because most other places
&nbsp;           expect there to be an object coming back; not this one */
&nbsp;        obj = mksobj_at(GLOB_OF_BLACK_PUDDING - (PM_BLACK_PUDDING - mndx),
&nbsp;                        x, y, TRUE, FALSE);
&nbsp;
&nbsp;        while (obj &amp;&amp; (otmp = obj_nexto(obj)) != (struct obj *) 0) {
&nbsp;            pudding_merge_message(obj, otmp);
&nbsp;            obj = obj_meld(&amp;obj, &amp;otmp);
&nbsp;        }
&nbsp;        free_mname(mtmp);
&nbsp;        return obj;
&nbsp;    default_1:
&nbsp;    default:
&nbsp;        if (mvitals[mndx].mvflags &amp; G_NOCORPSE) {
&nbsp;            return (struct obj *) 0;
&nbsp;        } else {
&nbsp;            corpstatflags |= CORPSTAT_INIT;
&nbsp;            /* preserve the unique traits of some creatures */
&nbsp;            obj = mkcorpstat(CORPSE, KEEPTRAITS(mtmp) ? mtmp : 0,
&nbsp;                             mdat, x, y, corpstatflags);
&nbsp;            if (burythem) {
&nbsp;                boolean dealloc;
&nbsp;
&nbsp;                (void) bury_an_obj(obj, &amp;dealloc);
&nbsp;                newsym(x, y);
&nbsp;                return dealloc ? (struct obj *) 0 : obj;
&nbsp;            }
&nbsp;        }
&nbsp;        break;
&nbsp;    }
&nbsp;    /* All special cases should precede the G_NOCORPSE check */
&nbsp;
&nbsp;    if (!obj) return NULL;
&nbsp;
&nbsp;    /* if polymorph or undead turning has killed this monster,
&nbsp;       prevent the same attack beam from hitting its corpse */
&nbsp;    if (context.bypasses)
&nbsp;        bypass_obj(obj);
&nbsp;
&nbsp;    if (has_mname(mtmp))
&nbsp;        obj = oname(obj, MNAME(mtmp));
&nbsp;
&nbsp;    /*  Avoid "It was hidden under a green mold corpse!"
&nbsp;     *  during Blind combat. An unseen monster referred to as "it"
&nbsp;     *  could be killed and leave a corpse.  If a hider then hid
&nbsp;     *  underneath it, you could be told the corpse type of a
&nbsp;     *  monster that you never knew was there without this.
&nbsp;     *  The code in hitmu() substitutes the word "something"
&nbsp;     *  if the corpses obj-&gt;dknown is 0.
&nbsp;     */
&nbsp;    if (Blind &amp;&amp; !sensemon(mtmp))
&nbsp;        obj-&gt;dknown = 0;
&nbsp;
&nbsp;    stackobj(obj);
&nbsp;    newsym(x, y);
&nbsp;    return obj;
&nbsp;}
&nbsp;
minliquid
&nbsp;/* check mtmp and water/lava for compatibility, 0 (survived), 1 (died) */
&nbsp;int
&nbsp;minliquid(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    boolean inpool, inlava, infountain;
&nbsp;
&nbsp;    /* [what about ceiling clingers?] */
&nbsp;    inpool = (is_pool(mtmp-&gt;mx, mtmp-&gt;my)
&nbsp;              &amp;&amp; !(is_flyer(mtmp-&gt;data) || is_floater(mtmp-&gt;data)));
&nbsp;    inlava = (is_lava(mtmp-&gt;mx, mtmp-&gt;my)
&nbsp;              &amp;&amp; !(is_flyer(mtmp-&gt;data) || is_floater(mtmp-&gt;data)));
&nbsp;    infountain = IS_FOUNTAIN(levl[mtmp-&gt;mx][mtmp-&gt;my].typ);
&nbsp;
&nbsp;    /* Flying and levitation keeps our steed out of the liquid */
&nbsp;    /* (but not water-walking or swimming) */
&nbsp;    if (mtmp == u.usteed &amp;&amp; (Flying || Levitation))
&nbsp;        return 0;
&nbsp;
&nbsp;    /* Gremlin multiplying won't go on forever since the hit points
&nbsp;     * keep going down, and when it gets to 1 hit point the clone
&nbsp;     * function will fail.
&nbsp;     */
&nbsp;    if (mtmp-&gt;data == &amp;mons[PM_GREMLIN] &amp;&amp; (inpool || infountain) &amp;&amp; rn2(3)) {
&nbsp;        if (split_mon(mtmp, (struct monst *) 0))
&nbsp;            dryup(mtmp-&gt;mx, mtmp-&gt;my, FALSE);
&nbsp;        if (inpool)
&nbsp;            water_damage_chain(mtmp-&gt;minvent, FALSE);
&nbsp;        return 0;
&nbsp;    } else if (mtmp-&gt;data == &amp;mons[PM_IRON_GOLEM] &amp;&amp; inpool &amp;&amp; !rn2(5)) {
&nbsp;        int dam = d(2, 6);
&nbsp;
&nbsp;        if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;            pline("%s rusts.", Monnam(mtmp));
&nbsp;        mtmp-&gt;mhp -= dam;
&nbsp;        if (mtmp-&gt;mhpmax &gt; dam)
&nbsp;            mtmp-&gt;mhpmax -= dam;
&nbsp;        if (mtmp-&gt;mhp &lt; 1) {
&nbsp;            mondead(mtmp);
&nbsp;            if (mtmp-&gt;mhp &lt; 1)
&nbsp;                return 1;
&nbsp;        }
&nbsp;        water_damage_chain(mtmp-&gt;minvent, FALSE);
&nbsp;        return 0;
&nbsp;    }
&nbsp;
&nbsp;    if (inlava) {
&nbsp;        /*
&nbsp;         * Lava effects much as water effects. Lava likers are able to
&nbsp;         * protect their stuff. Fire resistant monsters can only protect
&nbsp;         * themselves  --ALI
&nbsp;         */
&nbsp;        if (!is_clinger(mtmp-&gt;data) &amp;&amp; !likes_lava(mtmp-&gt;data)) {
&nbsp;            if (!resists_fire(mtmp)) {
&nbsp;                if (cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;                    struct attack *dummy = &amp;mtmp-&gt;data-&gt;mattk[0];
&nbsp;                    const char *how = on_fire(mtmp-&gt;data, dummy);
&nbsp;
&nbsp;                    pline("%s %s.", Monnam(mtmp),
&nbsp;                          !strcmp(how, "boiling") ? "boils away"
&nbsp;                             : !strcmp(how, "melting") ? "melts away"
&nbsp;                                : "burns to a crisp");
&nbsp;                }
&nbsp;                mondead(mtmp);
&nbsp;            } else {
&nbsp;                mtmp-&gt;mhp -= 1;
&nbsp;                if (mtmp-&gt;mhp &lt; 1) {
&nbsp;                    if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                        pline("%s surrenders to the fire.", Monnam(mtmp));
&nbsp;                    mondead(mtmp);
&nbsp;                } else if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                    pline("%s burns slightly.", Monnam(mtmp));
&nbsp;            }
&nbsp;            if (mtmp-&gt;mhp &gt; 0) {
&nbsp;                (void) fire_damage_chain(mtmp-&gt;minvent, FALSE, FALSE,
&nbsp;                                         mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;                (void) rloc(mtmp, FALSE);
&nbsp;                return 0;
&nbsp;            }
&nbsp;            return 1;
&nbsp;        }
&nbsp;    } else if (inpool) {
&nbsp;        /* Most monsters drown in pools.  flooreffects() will take care of
&nbsp;         * water damage to dead monsters' inventory, but survivors need to
&nbsp;         * be handled here.  Swimmers are able to protect their stuff...
&nbsp;         */
&nbsp;        if (!is_clinger(mtmp-&gt;data) &amp;&amp; !is_swimmer(mtmp-&gt;data)
&nbsp;            &amp;&amp; !amphibious(mtmp-&gt;data)) {
&nbsp;            if (cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;                pline("%s drowns.", Monnam(mtmp));
&nbsp;            }
&nbsp;            if (u.ustuck &amp;&amp; u.uswallow &amp;&amp; u.ustuck == mtmp) {
&nbsp;                /* This can happen after a purple worm plucks you off a
&nbsp;                flying steed while you are over water. */
&nbsp;                pline("%s sinks as %s rushes in and flushes you out.",
&nbsp;                      Monnam(mtmp), hliquid("water"));
&nbsp;            }
&nbsp;            mondead(mtmp);
&nbsp;            if (mtmp-&gt;mhp &gt; 0) {
&nbsp;                water_damage_chain(mtmp-&gt;minvent, FALSE);
&nbsp;                (void) rloc(mtmp, FALSE);
&nbsp;                return 0;
&nbsp;            }
&nbsp;            return 1;
&nbsp;        }
&nbsp;    } else {
&nbsp;        /* but eels have a difficult time outside */
&nbsp;        if (mtmp-&gt;data-&gt;mlet == S_EEL &amp;&amp; !Is_waterlevel(&amp;u.uz)) {
&nbsp;            /* as mhp gets lower, the rate of further loss slows down */
&nbsp;            if (mtmp-&gt;mhp &gt; 1 &amp;&amp; rn2(mtmp-&gt;mhp) &gt; rn2(8))
&nbsp;                mtmp-&gt;mhp--;
&nbsp;            monflee(mtmp, 2, FALSE, FALSE);
&nbsp;        }
&nbsp;    }
&nbsp;    return 0;
&nbsp;}
&nbsp;
mcalcmove
&nbsp;int
&nbsp;mcalcmove(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    int mmove = mon-&gt;data-&gt;mmove;
&nbsp;    int mmove_adj;
&nbsp;
&nbsp;    /* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
&nbsp;     *       MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
&nbsp;     *       both adjustments have negligible effect on higher speeds.
&nbsp;     */
&nbsp;    if (mon-&gt;mspeed == MSLOW)
&nbsp;        mmove = (2 * mmove + 1) / 3;
&nbsp;    else if (mon-&gt;mspeed == MFAST)
&nbsp;        mmove = (4 * mmove + 2) / 3;
&nbsp;
&nbsp;    if (mon == u.usteed &amp;&amp; u.ugallop &amp;&amp; context.mv) {
&nbsp;        /* increase movement by a factor of 1.5; also increase variance of
&nbsp;           movement speed (if it's naturally 24, we don't want it to always
&nbsp;           become 36) */
&nbsp;        mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
&nbsp;    }
&nbsp;
&nbsp;    /* Randomly round the monster's speed to a multiple of NORMAL_SPEED. This
&nbsp;       makes it impossible for the player to predict when they'll get a free
&nbsp;       turn (thus preventing exploits like "melee kiting"), while retaining
&nbsp;       guarantees about shopkeepers not being outsped by a normal-speed player,
&nbsp;       normal-speed players being unable to open up a gap when fleeing a
&nbsp;       normal-speed monster, etc.*/
&nbsp;    mmove_adj = mmove % NORMAL_SPEED;
&nbsp;    mmove -= mmove_adj;
&nbsp;    if (rn2(NORMAL_SPEED) &lt; mmove_adj)
&nbsp;        mmove += NORMAL_SPEED;
&nbsp;
&nbsp;    return mmove;
&nbsp;}
&nbsp;
mcalcdistress
&nbsp;/* actions that happen once per ``turn'', regardless of each
&nbsp;   individual monster's metabolism; some of these might need to
&nbsp;   be reclassified to occur more in proportion with movement rate */
&nbsp;void
&nbsp;mcalcdistress()
&nbsp;{
&nbsp;    struct monst *mtmp;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;
&nbsp;        /* must check non-moving monsters once/turn in case
&nbsp;         * they managed to end up in liquid */
&nbsp;        if (mtmp-&gt;data-&gt;mmove == 0) {
&nbsp;            if (vision_full_recalc)
&nbsp;                vision_recalc(0);
&nbsp;            if (minliquid(mtmp))
&nbsp;                continue;
&nbsp;        }
&nbsp;
&nbsp;        /* regenerate hit points */
&nbsp;        mon_regen(mtmp, FALSE);
&nbsp;
&nbsp;        /* possibly polymorph shapechangers and lycanthropes */
&nbsp;        if (mtmp-&gt;cham &gt;= LOW_PM)
&nbsp;            decide_to_shapeshift(mtmp, (canspotmon(mtmp)
&nbsp;                                        || (u.uswallow &amp;&amp; mtmp == u.ustuck))
&nbsp;                                          ? SHIFT_MSG : 0);
&nbsp;        were_change(mtmp);
&nbsp;
&nbsp;        /* gradually time out temporary problems */
&nbsp;        if (mtmp-&gt;mblinded &amp;&amp; !--mtmp-&gt;mblinded)
&nbsp;            mtmp-&gt;mcansee = 1;
&nbsp;        if (mtmp-&gt;mfrozen &amp;&amp; !--mtmp-&gt;mfrozen)
&nbsp;            mtmp-&gt;mcanmove = 1;
&nbsp;        if (mtmp-&gt;mfleetim &amp;&amp; !--mtmp-&gt;mfleetim)
&nbsp;            mtmp-&gt;mflee = 0;
&nbsp;
&nbsp;        /* FIXME: mtmp-&gt;mlstmv ought to be updated here */
&nbsp;    }
&nbsp;}
&nbsp;
movemon
&nbsp;int
&nbsp;movemon()
&nbsp;{
&nbsp;    register struct monst *mtmp, *nmtmp;
&nbsp;    register boolean somebody_can_move = FALSE;
&nbsp;
&nbsp;    /*
&nbsp;     * Some of you may remember the former assertion here that
&nbsp;     * because of deaths and other actions, a simple one-pass
&nbsp;     * algorithm wasn't possible for movemon.  Deaths are no longer
&nbsp;     * removed to the separate list fdmon; they are simply left in
&nbsp;     * the chain with hit points &lt;= 0, to be cleaned up at the end
&nbsp;     * of the pass.
&nbsp;     *
&nbsp;     * The only other actions which cause monsters to be removed from
&nbsp;     * the chain are level migrations and losedogs().  I believe losedogs()
&nbsp;     * is a cleanup routine not associated with monster movements, and
&nbsp;     * monsters can only affect level migrations on themselves, not others
&nbsp;     * (hence the fetching of nmon before moving the monster).  Currently,
&nbsp;     * monsters can jump into traps, read cursed scrolls of teleportation,
&nbsp;     * and drink cursed potions of raise level to change levels.  These are
&nbsp;     * all reflexive at this point.  Should one monster be able to level
&nbsp;     * teleport another, this scheme would have problems.
&nbsp;     */
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = nmtmp) {
&nbsp;        /* end monster movement early if hero is flagged to leave the level */
&nbsp;        if (u.utotype
&nbsp;#ifdef SAFERHANGUP
&nbsp;            /* or if the program has lost contact with the user */
&nbsp;            || program_state.done_hup
&nbsp;#endif
&nbsp;            ) {
&nbsp;            somebody_can_move = FALSE;
&nbsp;            break;
&nbsp;        }
&nbsp;        nmtmp = mtmp-&gt;nmon;
&nbsp;        /* one dead monster needs to perform a move after death:
&nbsp;           vault guard whose temporary corridor is still on the map */
&nbsp;        if (mtmp-&gt;isgd &amp;&amp; !mtmp-&gt;mx &amp;&amp; mtmp-&gt;mhp &lt;= 0)
&nbsp;            (void) gd_move(mtmp);
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;
&nbsp;        /* Find a monster that we have not treated yet. */
&nbsp;        if (mtmp-&gt;movement &lt; NORMAL_SPEED)
&nbsp;            continue;
&nbsp;
&nbsp;        mtmp-&gt;movement -= NORMAL_SPEED;
&nbsp;        if (mtmp-&gt;movement &gt;= NORMAL_SPEED)
&nbsp;            somebody_can_move = TRUE;
&nbsp;
&nbsp;        if (vision_full_recalc)
&nbsp;            vision_recalc(0); /* vision! */
&nbsp;
&nbsp;        /* reset obj bypasses before next monster moves */
&nbsp;        if (context.bypasses)
&nbsp;            clear_bypasses();
&nbsp;        clear_splitobjs();
&nbsp;        if (minliquid(mtmp))
&nbsp;            continue;
&nbsp;
&nbsp;        if (is_hider(mtmp-&gt;data)) {
&nbsp;            /* unwatched mimics and piercers may hide again  [MRS] */
&nbsp;            if (restrap(mtmp))
&nbsp;                continue;
&nbsp;            if (mtmp-&gt;m_ap_type == M_AP_FURNITURE
&nbsp;                || mtmp-&gt;m_ap_type == M_AP_OBJECT)
&nbsp;                continue;
&nbsp;            if (mtmp-&gt;mundetected)
&nbsp;                continue;
&nbsp;        } else if (mtmp-&gt;data-&gt;mlet == S_EEL &amp;&amp; !mtmp-&gt;mundetected
&nbsp;                   &amp;&amp; (mtmp-&gt;mflee || distu(mtmp-&gt;mx, mtmp-&gt;my) &gt; 2)
&nbsp;                   &amp;&amp; !canseemon(mtmp) &amp;&amp; !rn2(4)) {
&nbsp;            /* some eels end up stuck in isolated pools, where they
&nbsp;               can't--or at least won't--move, so they never reach
&nbsp;               their post-move chance to re-hide */
&nbsp;            if (hideunder(mtmp))
&nbsp;                continue;
&nbsp;        }
&nbsp;
&nbsp;        /* continue if the monster died fighting */
&nbsp;        if (Conflict &amp;&amp; !mtmp-&gt;iswiz &amp;&amp; mtmp-&gt;mcansee) {
&nbsp;            /* Note:
&nbsp;             *  Conflict does not take effect in the first round.
&nbsp;             *  Therefore, A monster when stepping into the area will
&nbsp;             *  get to swing at you.
&nbsp;             *
&nbsp;             *  The call to fightm() must be _last_.  The monster might
&nbsp;             *  have died if it returns 1.
&nbsp;             */
&nbsp;            if (couldsee(mtmp-&gt;mx, mtmp-&gt;my)
&nbsp;                &amp;&amp; (distu(mtmp-&gt;mx, mtmp-&gt;my) &lt;= BOLT_LIM * BOLT_LIM)
&nbsp;                &amp;&amp; fightm(mtmp))
&nbsp;                continue; /* mon might have died */
&nbsp;        }
&nbsp;        if (dochugw(mtmp)) /* otherwise just move the monster */
&nbsp;            continue;
&nbsp;    }
&nbsp;
&nbsp;    if (any_light_source())
&nbsp;        vision_full_recalc = 1; /* in case a mon moved with a light source */
&nbsp;    /* reset obj bypasses after last monster has moved */
&nbsp;    if (context.bypasses)
&nbsp;        clear_bypasses();
&nbsp;    clear_splitobjs();
&nbsp;    /* remove dead monsters; dead vault guard will be left at &lt;0,0&gt;
&nbsp;       if temporary corridor out of vault hasn't been removed yet */
&nbsp;    dmonsfree();
&nbsp;
&nbsp;    /* a monster may have levteleported player -dlc */
&nbsp;    if (u.utotype) {
&nbsp;        deferred_goto();
&nbsp;        /* changed levels, so these monsters are dormant */
&nbsp;        somebody_can_move = FALSE;
&nbsp;    }
&nbsp;
&nbsp;    return somebody_can_move;
&nbsp;}
&nbsp;
&nbsp;#define mstoning(obj)                                       \
&nbsp;    (ofood(obj) &amp;&amp; (touch_petrifies(&amp;mons[(obj)-&gt;corpsenm]) \
&nbsp;                    || (obj)-&gt;corpsenm == PM_MEDUSA))
&nbsp;
meatmetal
&nbsp;/*
&nbsp; * Maybe eat a metallic object (not just gold).
&nbsp; * Return value: 0 =&gt; nothing happened, 1 =&gt; monster ate something,
&nbsp; * 2 =&gt; monster died (it must have grown into a genocided form, but
&nbsp; * that can't happen at present because nothing which eats objects
&nbsp; * has young and old forms).
&nbsp; */
&nbsp;int
&nbsp;meatmetal(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    register struct obj *otmp;
&nbsp;    struct permonst *ptr;
&nbsp;    int poly, grow, heal, mstone;
&nbsp;
&nbsp;    /* If a pet, eating is handled separately, in dog.c */
&nbsp;    if (mtmp-&gt;mtame)
&nbsp;        return 0;
&nbsp;
&nbsp;    /* Eats topmost metal object if it is there */
&nbsp;    for (otmp = level.objects[mtmp-&gt;mx][mtmp-&gt;my]; otmp;
&nbsp;         otmp = otmp-&gt;nexthere) {
&nbsp;        /* Don't eat indigestible/choking/inappropriate objects */
&nbsp;        if ((mtmp-&gt;data == &amp;mons[PM_RUST_MONSTER] &amp;&amp; !is_rustprone(otmp))
&nbsp;            || (otmp-&gt;otyp == AMULET_OF_STRANGULATION)
&nbsp;            || (otmp-&gt;otyp == RIN_SLOW_DIGESTION))
&nbsp;            continue;
&nbsp;        if (is_metallic(otmp) &amp;&amp; !obj_resists(otmp, 5, 95)
&nbsp;            &amp;&amp; touch_artifact(otmp, mtmp)) {
&nbsp;            if (mtmp-&gt;data == &amp;mons[PM_RUST_MONSTER] &amp;&amp; otmp-&gt;oerodeproof) {
&nbsp;                if (canseemon(mtmp) &amp;&amp; flags.verbose) {
&nbsp;                    pline("%s eats %s!", Monnam(mtmp),
&nbsp;                          distant_name(otmp, doname));
&nbsp;                }
&nbsp;                /* The object's rustproofing is gone now */
&nbsp;                otmp-&gt;oerodeproof = 0;
&nbsp;                mtmp-&gt;mstun = 1;
&nbsp;                if (canseemon(mtmp) &amp;&amp; flags.verbose) {
&nbsp;                    pline("%s spits %s out in disgust!", Monnam(mtmp),
&nbsp;                          distant_name(otmp, doname));
&nbsp;                }
&nbsp;            } else {
&nbsp;                if (cansee(mtmp-&gt;mx, mtmp-&gt;my) &amp;&amp; flags.verbose)
&nbsp;                    pline("%s eats %s!", Monnam(mtmp),
&nbsp;                          distant_name(otmp, doname));
&nbsp;                else if (flags.verbose)
&nbsp;                    You_hear("a crunching sound.");
&nbsp;                mtmp-&gt;meating = otmp-&gt;owt / 2 + 1;
&nbsp;                /* Heal up to the object's weight in hp */
&nbsp;                if (mtmp-&gt;mhp &lt; mtmp-&gt;mhpmax) {
&nbsp;                    mtmp-&gt;mhp += objects[otmp-&gt;otyp].oc_weight;
&nbsp;                    if (mtmp-&gt;mhp &gt; mtmp-&gt;mhpmax)
&nbsp;                        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;                }
&nbsp;                if (otmp == uball) {
&nbsp;                    unpunish();
&nbsp;                    delobj(otmp);
&nbsp;                } else if (otmp == uchain) {
&nbsp;                    unpunish(); /* frees uchain */
&nbsp;                } else {
&nbsp;                    poly = polyfodder(otmp);
&nbsp;                    grow = mlevelgain(otmp);
&nbsp;                    heal = mhealup(otmp);
&nbsp;                    mstone = mstoning(otmp);
&nbsp;                    delobj(otmp);
&nbsp;                    ptr = mtmp-&gt;data;
&nbsp;                    if (poly) {
&nbsp;                        if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
&nbsp;                            ptr = mtmp-&gt;data;
&nbsp;                    } else if (grow) {
&nbsp;                        ptr = grow_up(mtmp, (struct monst *) 0);
&nbsp;                    } else if (mstone) {
&nbsp;                        if (poly_when_stoned(ptr)) {
&nbsp;                            mon_to_stone(mtmp);
&nbsp;                            ptr = mtmp-&gt;data;
&nbsp;                        } else if (!resists_ston(mtmp)) {
&nbsp;                            if (canseemon(mtmp))
&nbsp;                                pline("%s turns to stone!", Monnam(mtmp));
&nbsp;                            monstone(mtmp);
&nbsp;                            ptr = (struct permonst *) 0;
&nbsp;                        }
&nbsp;                    } else if (heal) {
&nbsp;                        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;                    }
&nbsp;                    if (!ptr)
&nbsp;                        return 2; /* it died */
&nbsp;                }
&nbsp;                /* Left behind a pile? */
&nbsp;                if (rnd(25) &lt; 3)
&nbsp;                    (void) mksobj_at(ROCK, mtmp-&gt;mx, mtmp-&gt;my, TRUE, FALSE);
&nbsp;                newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;                return 1;
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    return 0;
&nbsp;}
&nbsp;
meatobj
&nbsp;/* monster eats a pile of objects */
&nbsp;int
&nbsp;meatobj(mtmp) /* for gelatinous cubes */
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    struct obj *otmp, *otmp2;
&nbsp;    struct permonst *ptr, *original_ptr = mtmp-&gt;data;
&nbsp;    int poly, grow, heal, eyes, count = 0, ecount = 0;
&nbsp;    char buf[BUFSZ];
&nbsp;
&nbsp;    buf[0] = '\0';
&nbsp;    /* If a pet, eating is handled separately, in dog.c */
&nbsp;    if (mtmp-&gt;mtame)
&nbsp;        return 0;
&nbsp;
&nbsp;    /* eat organic objects, including cloth and wood, if present;
&nbsp;       engulf others, except huge rocks and metal attached to player
&nbsp;       [despite comment at top, doesn't assume that eater is a g.cube] */
&nbsp;    for (otmp = level.objects[mtmp-&gt;mx][mtmp-&gt;my]; otmp; otmp = otmp2) {
&nbsp;        otmp2 = otmp-&gt;nexthere;
&nbsp;
&nbsp;        /* touch sensitive items */
&nbsp;        if (otmp-&gt;otyp == CORPSE &amp;&amp; is_rider(&amp;mons[otmp-&gt;corpsenm])) {
&nbsp;            /* Rider corpse isn't just inedible; can't engulf it either */
&nbsp;            (void) revive_corpse(otmp);
&nbsp;
&nbsp;        /* untouchable (or inaccessible) items */
&nbsp;        } else if ((otmp-&gt;otyp == CORPSE
&nbsp;                    &amp;&amp; touch_petrifies(&amp;mons[otmp-&gt;corpsenm])
&nbsp;                    &amp;&amp; !resists_ston(mtmp))
&nbsp;                   /* don't engulf boulders and statues or ball&amp;chain */
&nbsp;                   || otmp-&gt;oclass == ROCK_CLASS
&nbsp;                   || otmp == uball || otmp == uchain
&nbsp;                   /* normally mtmp won't have stepped onto scare monster
&nbsp;                      scroll, but if it does, don't eat or engulf that
&nbsp;                      (note: scrolls inside eaten containers will still
&nbsp;                      become engulfed) */
&nbsp;                   || otmp-&gt;otyp == SCR_SCARE_MONSTER) {
&nbsp;            /* do nothing--neither eaten nor engulfed */
&nbsp;            continue;
&nbsp;
&nbsp;        /* inedible items -- engulf these */
&nbsp;        } else if (!is_organic(otmp) || obj_resists(otmp, 5, 95)
&nbsp;                   || !touch_artifact(otmp, mtmp)
&nbsp;                   /* redundant due to non-organic composition but
&nbsp;                      included for emphasis */
&nbsp;                   || (otmp-&gt;otyp == AMULET_OF_STRANGULATION
&nbsp;                       || otmp-&gt;otyp == RIN_SLOW_DIGESTION)
&nbsp;                   /* cockatrice corpses handled above; this
&nbsp;                      touch_petrifies() check catches eggs */
&nbsp;                   || ((otmp-&gt;otyp == CORPSE || otmp-&gt;otyp == EGG
&nbsp;                        || otmp-&gt;globby)
&nbsp;                       &amp;&amp; ((touch_petrifies(&amp;mons[otmp-&gt;corpsenm])
&nbsp;                            &amp;&amp; !resists_ston(mtmp))
&nbsp;                           || (otmp-&gt;corpsenm == PM_GREEN_SLIME
&nbsp;                               &amp;&amp; !slimeproof(mtmp-&gt;data))))) {
&nbsp;            /* engulf */
&nbsp;            ++ecount;
&nbsp;            if (ecount == 1)
&nbsp;                Sprintf(buf, "%s engulfs %s.", Monnam(mtmp),
&nbsp;                        distant_name(otmp, doname));
&nbsp;            else if (ecount == 2)
&nbsp;                Sprintf(buf, "%s engulfs several objects.", Monnam(mtmp));
&nbsp;            obj_extract_self(otmp);
&nbsp;            (void) mpickobj(mtmp, otmp); /* slurp */
&nbsp;
&nbsp;        /* lastly, edible items; yum! */
&nbsp;        } else {
&nbsp;            /* devour */
&nbsp;            ++count;
&nbsp;            if (cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;                if (flags.verbose)
&nbsp;                    pline("%s eats %s!", Monnam(mtmp),
&nbsp;                          distant_name(otmp, doname));
&nbsp;                /* give this one even if !verbose */
&nbsp;                if (otmp-&gt;oclass == SCROLL_CLASS
&nbsp;                    &amp;&amp; !strcmpi(OBJ_DESCR(objects[otmp-&gt;otyp]), "YUM YUM"))
&nbsp;                    pline("Yum%c", otmp-&gt;blessed ? '!' : '.');
&nbsp;            } else {
&nbsp;                if (flags.verbose)
&nbsp;                    You_hear("a slurping sound.");
&nbsp;            }
&nbsp;            /* Heal up to the object's weight in hp */
&nbsp;            if (mtmp-&gt;mhp &lt; mtmp-&gt;mhpmax) {
&nbsp;                mtmp-&gt;mhp += objects[otmp-&gt;otyp].oc_weight;
&nbsp;                if (mtmp-&gt;mhp &gt; mtmp-&gt;mhpmax)
&nbsp;                    mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;            }
&nbsp;            if (Has_contents(otmp)) {
&nbsp;                register struct obj *otmp3;
&nbsp;
&nbsp;                /* contents of eaten containers become engulfed; this
&nbsp;                   is arbitrary, but otherwise g.cubes are too powerful */
&nbsp;                while ((otmp3 = otmp-&gt;cobj) != 0) {
&nbsp;                    obj_extract_self(otmp3);
&nbsp;                    if (otmp-&gt;otyp == ICE_BOX &amp;&amp; otmp3-&gt;otyp == CORPSE) {
&nbsp;                        otmp3-&gt;age = monstermoves - otmp3-&gt;age;
&nbsp;                        start_corpse_timeout(otmp3);
&nbsp;                    }
&nbsp;                    (void) mpickobj(mtmp, otmp3);
&nbsp;                }
&nbsp;            }
&nbsp;            poly = polyfodder(otmp);
&nbsp;            grow = mlevelgain(otmp);
&nbsp;            heal = mhealup(otmp);
&nbsp;            eyes = (otmp-&gt;otyp == CARROT);
&nbsp;            delobj(otmp); /* munch */
&nbsp;            ptr = mtmp-&gt;data;
&nbsp;            if (poly) {
&nbsp;                if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
&nbsp;                    ptr = mtmp-&gt;data;
&nbsp;            } else if (grow) {
&nbsp;                ptr = grow_up(mtmp, (struct monst *) 0);
&nbsp;            } else if (heal) {
&nbsp;                mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;            }
&nbsp;            if ((eyes || heal) &amp;&amp; !mtmp-&gt;mcansee)
&nbsp;                mcureblindness(mtmp, canseemon(mtmp));
&nbsp;            /* in case it polymorphed or died */
&nbsp;            if (ptr != original_ptr)
&nbsp;                return !ptr ? 2 : 1;
&nbsp;        }
&nbsp;
&nbsp;        /* Engulf &amp; devour is instant, so don't set meating */
&nbsp;        if (mtmp-&gt;minvis)
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;
&nbsp;    if (ecount &gt; 0) {
&nbsp;        if (cansee(mtmp-&gt;mx, mtmp-&gt;my) &amp;&amp; flags.verbose &amp;&amp; buf[0])
&nbsp;            pline1(buf);
&nbsp;        else if (flags.verbose)
&nbsp;            You_hear("%s slurping sound%s.",
&nbsp;                     (ecount == 1) ? "a" : "several", plur(ecount));
&nbsp;    }
&nbsp;    return (count &gt; 0 || ecount &gt; 0) ? 1 : 0;
&nbsp;}
&nbsp;
mpickgold
&nbsp;void
&nbsp;mpickgold(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    register struct obj *gold;
&nbsp;    int mat_idx;
&nbsp;
&nbsp;    if ((gold = g_at(mtmp-&gt;mx, mtmp-&gt;my)) != 0) {
&nbsp;        mat_idx = objects[gold-&gt;otyp].oc_material;
&nbsp;        obj_extract_self(gold);
&nbsp;        add_to_minv(mtmp, gold);
&nbsp;        if (cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;            if (flags.verbose &amp;&amp; !mtmp-&gt;isgd)
&nbsp;                pline("%s picks up some %s.", Monnam(mtmp),
&nbsp;                      mat_idx == GOLD ? "gold" : "money");
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
mpickstuff
&nbsp;boolean
&nbsp;mpickstuff(mtmp, str)
&nbsp;register struct monst *mtmp;
&nbsp;register const char *str;
&nbsp;{
&nbsp;    register struct obj *otmp, *otmp2, *otmp3;
&nbsp;    int carryamt = 0;
&nbsp;
&nbsp;    /* prevent shopkeepers from leaving the door of their shop */
&nbsp;    if (mtmp-&gt;isshk &amp;&amp; inhishop(mtmp))
&nbsp;        return FALSE;
&nbsp;
&nbsp;    for (otmp = level.objects[mtmp-&gt;mx][mtmp-&gt;my]; otmp; otmp = otmp2) {
&nbsp;        otmp2 = otmp-&gt;nexthere;
&nbsp;        /* Nymphs take everything.  Most monsters don't pick up corpses. */
&nbsp;        if (!str ? searches_for_item(mtmp, otmp)
&nbsp;                 : !!(index(str, otmp-&gt;oclass))) {
&nbsp;            if (otmp-&gt;otyp == CORPSE &amp;&amp; mtmp-&gt;data-&gt;mlet != S_NYMPH
&nbsp;                /* let a handful of corpse types thru to can_carry() */
&nbsp;                &amp;&amp; !touch_petrifies(&amp;mons[otmp-&gt;corpsenm])
&nbsp;                &amp;&amp; otmp-&gt;corpsenm != PM_LIZARD
&nbsp;                &amp;&amp; !acidic(&amp;mons[otmp-&gt;corpsenm]))
&nbsp;                continue;
&nbsp;            if (!touch_artifact(otmp, mtmp))
&nbsp;                continue;
&nbsp;            carryamt = can_carry(mtmp, otmp);
&nbsp;            if (carryamt == 0)
&nbsp;                continue;
&nbsp;            if (is_pool(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                continue;
&nbsp;            /* handle cases where the critter can only get some */
&nbsp;            otmp3 = otmp;
&nbsp;            if (carryamt != otmp-&gt;quan) {
&nbsp;                otmp3 = splitobj(otmp, carryamt);
&nbsp;            }
&nbsp;            if (cansee(mtmp-&gt;mx, mtmp-&gt;my) &amp;&amp; flags.verbose)
&nbsp;                pline("%s picks up %s.", Monnam(mtmp),
&nbsp;                      (distu(mtmp-&gt;mx, mtmp-&gt;my) &lt;= 5)
&nbsp;                          ? doname(otmp3)
&nbsp;                          : distant_name(otmp3, doname));
&nbsp;            obj_extract_self(otmp3);      /* remove from floor */
&nbsp;            (void) mpickobj(mtmp, otmp3); /* may merge and free otmp3 */
&nbsp;            m_dowear(mtmp, FALSE);
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;            return TRUE; /* pick only one object */
&nbsp;        }
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
curr_mon_load
&nbsp;int
&nbsp;curr_mon_load(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    int curload = 0;
&nbsp;    struct obj *obj;
&nbsp;
&nbsp;    for (obj = mtmp-&gt;minvent; obj; obj = obj-&gt;nobj) {
&nbsp;        if (obj-&gt;otyp != BOULDER || !throws_rocks(mtmp-&gt;data))
&nbsp;            curload += obj-&gt;owt;
&nbsp;    }
&nbsp;
&nbsp;    return curload;
&nbsp;}
&nbsp;
max_mon_load
&nbsp;int
&nbsp;max_mon_load(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    long maxload;
&nbsp;
&nbsp;    /* Base monster carrying capacity is equal to human maximum
&nbsp;     * carrying capacity, or half human maximum if not strong.
&nbsp;     * (for a polymorphed player, the value used would be the
&nbsp;     * non-polymorphed carrying capacity instead of max/half max).
&nbsp;     * This is then modified by the ratio between the monster weights
&nbsp;     * and human weights.  Corpseless monsters are given a capacity
&nbsp;     * proportional to their size instead of weight.
&nbsp;     */
&nbsp;    if (!mtmp-&gt;data-&gt;cwt)
&nbsp;        maxload = (MAX_CARR_CAP * (long) mtmp-&gt;data-&gt;msize) / MZ_HUMAN;
&nbsp;    else if (!strongmonst(mtmp-&gt;data)
&nbsp;             || (strongmonst(mtmp-&gt;data) &amp;&amp; (mtmp-&gt;data-&gt;cwt &gt; WT_HUMAN)))
&nbsp;        maxload = (MAX_CARR_CAP * (long) mtmp-&gt;data-&gt;cwt) / WT_HUMAN;
&nbsp;    else
&nbsp;        maxload = MAX_CARR_CAP; /*strong monsters w/cwt &lt;= WT_HUMAN*/
&nbsp;
&nbsp;    if (!strongmonst(mtmp-&gt;data))
&nbsp;        maxload /= 2;
&nbsp;
&nbsp;    if (maxload &lt; 1)
&nbsp;        maxload = 1;
&nbsp;
&nbsp;    return (int) maxload;
&nbsp;}
&nbsp;
can_carry
&nbsp;/* for restricting monsters' object-pickup.
&nbsp; *
&nbsp; * to support the new pet behavior, this now returns the max # of objects
&nbsp; * that a given monster could pick up from a pile. frequently this will be
&nbsp; * otmp-&gt;quan, but special cases for 'only one' now exist so.
&nbsp; *
&nbsp; * this will probably cause very amusing behavior with pets and gold coins.
&nbsp; *
&nbsp; * TODO: allow picking up 2-N objects from a pile of N based on weight.
&nbsp; *       Change from 'int' to 'long' to accomate big stacks of gold.
&nbsp; *       Right now we fake it by reporting a partial quantity, but the
&nbsp; *       likesgold handling m_move results in picking up the whole stack.
&nbsp; */
&nbsp;int
&nbsp;can_carry(mtmp, otmp)
&nbsp;struct monst *mtmp;
&nbsp;struct obj *otmp;
&nbsp;{
&nbsp;    int iquan, otyp = otmp-&gt;otyp, newload = otmp-&gt;owt;
&nbsp;    struct permonst *mdat = mtmp-&gt;data;
&nbsp;    short nattk = 0;
&nbsp;
&nbsp;    if (notake(mdat))
&nbsp;        return 0; /* can't carry anything */
&nbsp;
&nbsp;    if (otyp == CORPSE &amp;&amp; touch_petrifies(&amp;mons[otmp-&gt;corpsenm])
&nbsp;        &amp;&amp; !(mtmp-&gt;misc_worn_check &amp; W_ARMG) &amp;&amp; !resists_ston(mtmp))
&nbsp;        return 0;
&nbsp;    if (otyp == CORPSE &amp;&amp; is_rider(&amp;mons[otmp-&gt;corpsenm]))
&nbsp;        return 0;
&nbsp;    if (objects[otyp].oc_material == SILVER &amp;&amp; mon_hates_silver(mtmp)
&nbsp;        &amp;&amp; (otyp != BELL_OF_OPENING || !is_covetous(mdat)))
&nbsp;        return 0;
&nbsp;
&nbsp;    /* hostile monsters who like gold will pick up the whole stack;
&nbsp;       tame mosnters with hands will pick up the partial stack */
&nbsp;    iquan = (otmp-&gt;quan &gt; (long) LARGEST_INT)
&nbsp;               ? 20000 + rn2(LARGEST_INT - 20000 + 1)
&nbsp;               : (int) otmp-&gt;quan;
&nbsp;
&nbsp;    /* monsters without hands can't pick up multiple objects at once
&nbsp;     * unless they have an engulfing attack
&nbsp;     *
&nbsp;     * ...dragons, of course, can always carry gold pieces and gems somehow
&nbsp;     */
&nbsp;    if (iquan &gt; 1) {
&nbsp;        boolean glomper = FALSE;
&nbsp;
&nbsp;        if (mtmp-&gt;data-&gt;mlet == S_DRAGON
&nbsp;            &amp;&amp; (otmp-&gt;oclass == COIN_CLASS
&nbsp;                || otmp-&gt;oclass == GEM_CLASS))
&nbsp;            glomper = TRUE;
&nbsp;        else
&nbsp;            for (nattk = 0; nattk &lt; NATTK; nattk++)
&nbsp;                if (mtmp-&gt;data-&gt;mattk[nattk].aatyp == AT_ENGL) {
&nbsp;                    glomper = TRUE;
&nbsp;                    break;
&nbsp;                }
&nbsp;        if ((mtmp-&gt;data-&gt;mflags1 &amp; M1_NOHANDS) &amp;&amp; !glomper)
&nbsp;            return 1;
&nbsp;    }
&nbsp;
&nbsp;    /* steeds don't pick up stuff (to avoid shop abuse) */
&nbsp;    if (mtmp == u.usteed)
&nbsp;        return 0;
&nbsp;    if (mtmp-&gt;isshk)
&nbsp;        return iquan; /* no limit */
&nbsp;    if (mtmp-&gt;mpeaceful &amp;&amp; !mtmp-&gt;mtame)
&nbsp;        return 0;
&nbsp;    /* otherwise players might find themselves obligated to violate
&nbsp;     * their alignment if the monster takes something they need
&nbsp;     */
&nbsp;
&nbsp;    /* special--boulder throwers carry unlimited amounts of boulders */
&nbsp;    if (throws_rocks(mdat) &amp;&amp; otyp == BOULDER)
&nbsp;        return iquan;
&nbsp;
&nbsp;    /* nymphs deal in stolen merchandise, but not boulders or statues */
&nbsp;    if (mdat-&gt;mlet == S_NYMPH)
&nbsp;        return (otmp-&gt;oclass == ROCK_CLASS) ? 0 : iquan;
&nbsp;
&nbsp;    if (curr_mon_load(mtmp) + newload &gt; max_mon_load(mtmp))
&nbsp;        return 0;
&nbsp;
&nbsp;    return iquan;
&nbsp;}
&nbsp;
mfndpos
&nbsp;/* return number of acceptable neighbour positions */
&nbsp;int
&nbsp;mfndpos(mon, poss, info, flag)
&nbsp;struct monst *mon;
&nbsp;coord *poss; /* coord poss[9] */
&nbsp;long *info;  /* long info[9] */
&nbsp;long flag;
&nbsp;{
&nbsp;    struct permonst *mdat = mon-&gt;data;
&nbsp;    register struct trap *ttmp;
&nbsp;    xchar x, y, nx, ny;
&nbsp;    int cnt = 0;
&nbsp;    uchar ntyp;
&nbsp;    uchar nowtyp;
&nbsp;    boolean wantpool, poolok, lavaok, nodiag;
&nbsp;    boolean rockok = FALSE, treeok = FALSE, thrudoor;
&nbsp;    int maxx, maxy;
&nbsp;    boolean poisongas_ok, in_poisongas;
&nbsp;    NhRegion *gas_reg;
&nbsp;    int gas_glyph = cmap_to_glyph(S_poisoncloud);
&nbsp;
&nbsp;    x = mon-&gt;mx;
&nbsp;    y = mon-&gt;my;
&nbsp;    nowtyp = levl[x][y].typ;
&nbsp;
&nbsp;    nodiag = NODIAG(mdat - mons);
&nbsp;    wantpool = mdat-&gt;mlet == S_EEL;
&nbsp;    poolok = (is_flyer(mdat) || is_clinger(mdat)
&nbsp;              || (is_swimmer(mdat) &amp;&amp; !wantpool));
&nbsp;    lavaok = (is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat));
&nbsp;    thrudoor = ((flag &amp; (ALLOW_WALL | BUSTDOOR)) != 0L);
&nbsp;    poisongas_ok = ((nonliving(mdat) || is_vampshifter(mon)
&nbsp;                     || breathless(mdat)) || resists_poison(mon));
&nbsp;    in_poisongas = ((gas_reg = visible_region_at(x,y)) != 0
&nbsp;                    &amp;&amp; gas_reg-&gt;glyph == gas_glyph);
&nbsp;
&nbsp;    if (flag &amp; ALLOW_DIG) {
&nbsp;        struct obj *mw_tmp;
&nbsp;
&nbsp;        /* need to be specific about what can currently be dug */
&nbsp;        if (!needspick(mdat)) {
&nbsp;            rockok = treeok = TRUE;
&nbsp;        } else if ((mw_tmp = MON_WEP(mon)) &amp;&amp; mw_tmp-&gt;cursed
&nbsp;                   &amp;&amp; mon-&gt;weapon_check == NO_WEAPON_WANTED) {
&nbsp;            rockok = is_pick(mw_tmp);
&nbsp;            treeok = is_axe(mw_tmp);
&nbsp;        } else {
&nbsp;            rockok = (m_carrying(mon, PICK_AXE)
&nbsp;                      || (m_carrying(mon, DWARVISH_MATTOCK)
&nbsp;                          &amp;&amp; !which_armor(mon, W_ARMS)));
&nbsp;            treeok = (m_carrying(mon, AXE) || (m_carrying(mon, BATTLE_AXE)
&nbsp;                                               &amp;&amp; !which_armor(mon, W_ARMS)));
&nbsp;        }
&nbsp;        if (rockok || treeok)
&nbsp;            thrudoor = TRUE;
&nbsp;    }
&nbsp;
&nbsp;nexttry: /* eels prefer the water, but if there is no water nearby,
&nbsp;            they will crawl over land */
&nbsp;    if (mon-&gt;mconf) {
&nbsp;        flag |= ALLOW_ALL;
&nbsp;        flag &amp;= ~NOTONL;
&nbsp;    }
&nbsp;    if (!mon-&gt;mcansee)
&nbsp;        flag |= ALLOW_SSM;
&nbsp;    maxx = min(x + 1, COLNO - 1);
&nbsp;    maxy = min(y + 1, ROWNO - 1);
&nbsp;    for (nx = max(1, x - 1); nx &lt;= maxx; nx++)
&nbsp;        for (ny = max(0, y - 1); ny &lt;= maxy; ny++) {
&nbsp;            if (nx == x &amp;&amp; ny == y)
&nbsp;                continue;
&nbsp;            ntyp = levl[nx][ny].typ;
&nbsp;            if (IS_ROCK(ntyp)
&nbsp;                &amp;&amp; !((flag &amp; ALLOW_WALL) &amp;&amp; may_passwall(nx, ny))
&nbsp;                &amp;&amp; !((IS_TREE(ntyp) ? treeok : rockok) &amp;&amp; may_dig(nx, ny)))
&nbsp;                continue;
&nbsp;            /* KMH -- Added iron bars */
&nbsp;            if (ntyp == IRONBARS &amp;&amp; !(flag &amp; ALLOW_BARS))
&nbsp;                continue;
&nbsp;            if (IS_DOOR(ntyp) &amp;&amp; !(amorphous(mdat) || can_fog(mon))
&nbsp;                &amp;&amp; (((levl[nx][ny].doormask &amp; D_CLOSED) &amp;&amp; !(flag &amp; OPENDOOR))
&nbsp;                    || ((levl[nx][ny].doormask &amp; D_LOCKED)
&nbsp;                        &amp;&amp; !(flag &amp; UNLOCKDOOR))) &amp;&amp; !thrudoor)
&nbsp;                continue;
&nbsp;            /* avoid poison gas? */
&nbsp;            if (!poisongas_ok &amp;&amp; !in_poisongas
&nbsp;                &amp;&amp; (gas_reg = visible_region_at(nx,ny)) != 0
&nbsp;                &amp;&amp; gas_reg-&gt;glyph == gas_glyph)
&nbsp;                continue;
&nbsp;            /* first diagonal checks (tight squeezes handled below) */
&nbsp;            if (nx != x &amp;&amp; ny != y
&nbsp;                &amp;&amp; (nodiag
&nbsp;                    || (IS_DOOR(nowtyp) &amp;&amp; (levl[x][y].doormask &amp; ~D_BROKEN))
&nbsp;                    || (IS_DOOR(ntyp) &amp;&amp; (levl[nx][ny].doormask &amp; ~D_BROKEN))
&nbsp;                    || ((IS_DOOR(nowtyp) || IS_DOOR(ntyp))
&nbsp;                        &amp;&amp; Is_rogue_level(&amp;u.uz))
&nbsp;                    /* mustn't pass between adjacent long worm segments,
&nbsp;                       but can attack that way */
&nbsp;                    || (m_at(x, ny) &amp;&amp; m_at(nx, y) &amp;&amp; worm_cross(x, y, nx, ny)
&nbsp;                        &amp;&amp; !m_at(nx, ny) &amp;&amp; (nx != u.ux || ny != u.uy))))
&nbsp;                continue;
&nbsp;            if ((is_pool(nx, ny) == wantpool || poolok)
&nbsp;                &amp;&amp; (lavaok || !is_lava(nx, ny))) {
&nbsp;                int dispx, dispy;
&nbsp;                boolean monseeu = (mon-&gt;mcansee
&nbsp;                                   &amp;&amp; (!Invis || perceives(mdat)));
&nbsp;                boolean checkobj = OBJ_AT(nx, ny);
&nbsp;
&nbsp;                /* Displacement also displaces the Elbereth/scare monster,
&nbsp;                 * as long as you are visible.
&nbsp;                 */
&nbsp;                if (Displaced &amp;&amp; monseeu &amp;&amp; mon-&gt;mux == nx &amp;&amp; mon-&gt;muy == ny) {
&nbsp;                    dispx = u.ux;
&nbsp;                    dispy = u.uy;
&nbsp;                } else {
&nbsp;                    dispx = nx;
&nbsp;                    dispy = ny;
&nbsp;                }
&nbsp;
&nbsp;                info[cnt] = 0;
&nbsp;                if (onscary(dispx, dispy, mon)) {
&nbsp;                    if (!(flag &amp; ALLOW_SSM))
&nbsp;                        continue;
&nbsp;                    info[cnt] |= ALLOW_SSM;
&nbsp;                }
&nbsp;                if ((nx == u.ux &amp;&amp; ny == u.uy)
&nbsp;                    || (nx == mon-&gt;mux &amp;&amp; ny == mon-&gt;muy)) {
&nbsp;                    if (nx == u.ux &amp;&amp; ny == u.uy) {
&nbsp;                        /* If it's right next to you, it found you,
&nbsp;                         * displaced or no.  We must set mux and muy
&nbsp;                         * right now, so when we return we can tell
&nbsp;                         * that the ALLOW_U means to attack _you_ and
&nbsp;                         * not the image.
&nbsp;                         */
&nbsp;                        mon-&gt;mux = u.ux;
&nbsp;                        mon-&gt;muy = u.uy;
&nbsp;                    }
&nbsp;                    if (!(flag &amp; ALLOW_U))
&nbsp;                        continue;
&nbsp;                    info[cnt] |= ALLOW_U;
&nbsp;                } else {
&nbsp;                    if (MON_AT(nx, ny)) {
&nbsp;                        struct monst *mtmp2 = m_at(nx, ny);
&nbsp;                        long mmflag = flag | mm_aggression(mon, mtmp2);
&nbsp;
&nbsp;                        if (mmflag &amp; ALLOW_M) {
&nbsp;                            info[cnt] |= ALLOW_M;
&nbsp;                            if (mtmp2-&gt;mtame) {
&nbsp;                                if (!(mmflag &amp; ALLOW_TM))
&nbsp;                                    continue;
&nbsp;                                info[cnt] |= ALLOW_TM;
&nbsp;                            }
&nbsp;                        } else {
&nbsp;                            mmflag = flag | mm_displacement(mon, mtmp2);
&nbsp;                            if (!(mmflag &amp; ALLOW_MDISP))
&nbsp;                                continue;
&nbsp;                            info[cnt] |= ALLOW_MDISP;
&nbsp;                        }
&nbsp;                    }
&nbsp;                    /* Note: ALLOW_SANCT only prevents movement, not
&nbsp;                       attack, into a temple. */
&nbsp;                    if (level.flags.has_temple &amp;&amp; *in_rooms(nx, ny, TEMPLE)
&nbsp;                        &amp;&amp; !*in_rooms(x, y, TEMPLE)
&nbsp;                        &amp;&amp; in_your_sanctuary((struct monst *) 0, nx, ny)) {
&nbsp;                        if (!(flag &amp; ALLOW_SANCT))
&nbsp;                            continue;
&nbsp;                        info[cnt] |= ALLOW_SANCT;
&nbsp;                    }
&nbsp;                }
&nbsp;                if (checkobj &amp;&amp; sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
&nbsp;                    if (flag &amp; NOGARLIC)
&nbsp;                        continue;
&nbsp;                    info[cnt] |= NOGARLIC;
&nbsp;                }
&nbsp;                if (checkobj &amp;&amp; sobj_at(BOULDER, nx, ny)) {
&nbsp;                    if (!(flag &amp; ALLOW_ROCK))
&nbsp;                        continue;
&nbsp;                    info[cnt] |= ALLOW_ROCK;
&nbsp;                }
&nbsp;                if (monseeu &amp;&amp; onlineu(nx, ny)) {
&nbsp;                    if (flag &amp; NOTONL)
&nbsp;                        continue;
&nbsp;                    info[cnt] |= NOTONL;
&nbsp;                }
&nbsp;                /* check for diagonal tight squeeze */
&nbsp;                if (nx != x &amp;&amp; ny != y &amp;&amp; bad_rock(mdat, x, ny)
&nbsp;                    &amp;&amp; bad_rock(mdat, nx, y) &amp;&amp; cant_squeeze_thru(mon))
&nbsp;                    continue;
&nbsp;                /* The monster avoids a particular type of trap if it's
&nbsp;                 * familiar with the trap type.  Pets get ALLOW_TRAPS
&nbsp;                 * and checking is done in dogmove.c.  In either case,
&nbsp;                 * "harmless" traps are neither avoided nor marked in info[].
&nbsp;                 */
&nbsp;                if ((ttmp = t_at(nx, ny)) != 0) {
&nbsp;                    if (ttmp-&gt;ttyp &gt;= TRAPNUM || ttmp-&gt;ttyp == 0) {
&nbsp;                        impossible(
&nbsp;                         "A monster looked at a very strange trap of type %d.",
&nbsp;                                   ttmp-&gt;ttyp);
&nbsp;                            continue;
&nbsp;                    }
&nbsp;                    if ((ttmp-&gt;ttyp != RUST_TRAP
&nbsp;                         || mdat == &amp;mons[PM_IRON_GOLEM])
&nbsp;                        &amp;&amp; ttmp-&gt;ttyp != STATUE_TRAP
&nbsp;                        &amp;&amp; ((ttmp-&gt;ttyp != PIT &amp;&amp; ttmp-&gt;ttyp != SPIKED_PIT
&nbsp;                             &amp;&amp; ttmp-&gt;ttyp != TRAPDOOR &amp;&amp; ttmp-&gt;ttyp != HOLE)
&nbsp;                            || (!is_flyer(mdat) &amp;&amp; !is_floater(mdat)
&nbsp;                                &amp;&amp; !is_clinger(mdat)) || Sokoban)
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != SLP_GAS_TRAP || !resists_sleep(mon))
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != BEAR_TRAP
&nbsp;                            || (mdat-&gt;msize &gt; MZ_SMALL &amp;&amp; !amorphous(mdat)
&nbsp;                                &amp;&amp; !is_flyer(mdat) &amp;&amp; !is_floater(mdat)
&nbsp;                                &amp;&amp; !is_whirly(mdat) &amp;&amp; !unsolid(mdat)))
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != FIRE_TRAP || !resists_fire(mon))
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != SQKY_BOARD || !is_flyer(mdat))
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != WEB
&nbsp;                            || (!amorphous(mdat) &amp;&amp; !webmaker(mdat)
&nbsp;                                &amp;&amp; !is_whirly(mdat) &amp;&amp; !unsolid(mdat)))
&nbsp;                        &amp;&amp; (ttmp-&gt;ttyp != ANTI_MAGIC || !resists_magm(mon))) {
&nbsp;                        if (!(flag &amp; ALLOW_TRAPS)) {
&nbsp;                            if (mon-&gt;mtrapseen &amp; (1L &lt;&lt; (ttmp-&gt;ttyp - 1)))
&nbsp;                                continue;
&nbsp;                        }
&nbsp;                        info[cnt] |= ALLOW_TRAPS;
&nbsp;                    }
&nbsp;                }
&nbsp;                poss[cnt].x = nx;
&nbsp;                poss[cnt].y = ny;
&nbsp;                cnt++;
&nbsp;            }
&nbsp;        }
&nbsp;    if (!cnt &amp;&amp; wantpool &amp;&amp; !is_pool(x, y)) {
&nbsp;        wantpool = FALSE;
&nbsp;        goto nexttry;
&nbsp;    }
&nbsp;    return cnt;
&nbsp;}
&nbsp;
mm_aggression
&nbsp;/* Monster against monster special attacks; for the specified monster
&nbsp;   combinations, this allows one monster to attack another adjacent one
&nbsp;   in the absence of Conflict.  There is no provision for targetting
&nbsp;   other monsters; just hand to hand fighting when they happen to be
&nbsp;   next to each other. */
&nbsp;STATIC_OVL long
&nbsp;mm_aggression(magr, mdef)
&nbsp;struct monst *magr, /* monster that is currently deciding where to move */
&nbsp;             *mdef; /* another monster which is next to it */
&nbsp;{
&nbsp;    /* supposedly purple worms are attracted to shrieking because they
&nbsp;       like to eat shriekers, so attack the latter when feasible */
&nbsp;    if (magr-&gt;data == &amp;mons[PM_PURPLE_WORM]
&nbsp;        &amp;&amp; mdef-&gt;data == &amp;mons[PM_SHRIEKER])
&nbsp;        return ALLOW_M | ALLOW_TM;
&nbsp;    /* Various other combinations such as dog vs cat, cat vs rat, and
&nbsp;       elf vs orc have been suggested.  For the time being we don't
&nbsp;       support those. */
&nbsp;    return 0L;
&nbsp;}
&nbsp;
mm_displacement
&nbsp;/* Monster displacing another monster out of the way */
&nbsp;STATIC_OVL long
&nbsp;mm_displacement(magr, mdef)
&nbsp;struct monst *magr, /* monster that is currently deciding where to move */
&nbsp;             *mdef; /* another monster which is next to it */
&nbsp;{
&nbsp;    struct permonst *pa = magr-&gt;data, *pd = mdef-&gt;data;
&nbsp;
&nbsp;    /* if attacker can't barge through, there's nothing to do;
&nbsp;       or if defender can barge through too, don't let attacker
&nbsp;       do so, otherwise they might just end up swapping places
&nbsp;       again when defender gets its chance to move */
&nbsp;    if ((pa-&gt;mflags3 &amp; M3_DISPLACES) != 0 &amp;&amp; (pd-&gt;mflags3 &amp; M3_DISPLACES) == 0
&nbsp;        /* no displacing grid bugs diagonally */
&nbsp;        &amp;&amp; !(magr-&gt;mx != mdef-&gt;mx &amp;&amp; magr-&gt;my != mdef-&gt;my
&nbsp;             &amp;&amp; NODIAG(monsndx(pd)))
&nbsp;        /* no displacing trapped monsters or multi-location longworms */
&nbsp;        &amp;&amp; !mdef-&gt;mtrapped &amp;&amp; (!mdef-&gt;wormno || !count_wsegs(mdef))
&nbsp;        /* riders can move anything; others, same size or smaller only */
&nbsp;        &amp;&amp; (is_rider(pa) || pa-&gt;msize &gt;= pd-&gt;msize))
&nbsp;        return ALLOW_MDISP;
&nbsp;    return 0L;
&nbsp;}
&nbsp;
monnear
&nbsp;/* Is the square close enough for the monster to move or attack into? */
&nbsp;boolean
&nbsp;monnear(mon, x, y)
&nbsp;struct monst *mon;
&nbsp;int x, y;
&nbsp;{
&nbsp;    int distance = dist2(mon-&gt;mx, mon-&gt;my, x, y);
&nbsp;
&nbsp;    if (distance == 2 &amp;&amp; NODIAG(mon-&gt;data - mons))
&nbsp;        return 0;
&nbsp;    return (boolean) (distance &lt; 3);
&nbsp;}
&nbsp;
dmonsfree
&nbsp;/* really free dead monsters */
&nbsp;void
&nbsp;dmonsfree()
&nbsp;{
&nbsp;    struct monst **mtmp, *freetmp;
&nbsp;    int count = 0;
&nbsp;
&nbsp;    for (mtmp = &amp;fmon *mtmp;) {
&nbsp;        freetmp = *mtmp;
&nbsp;        if (freetmp-&gt;mhp &lt;= 0 &amp;&amp; !freetmp-&gt;isgd) {
&nbsp;            *mtmp = freetmp-&gt;nmon;
&nbsp;            freetmp-&gt;nmon = NULL;
&nbsp;            dealloc_monst(freetmp);
&nbsp;            count++;
&nbsp;        } else
&nbsp;            mtmp = &amp;(freetmp-&gt;nmon);
&nbsp;    }
&nbsp;
&nbsp;    if (count != iflags.purge_monsters)
&nbsp;        impossible("dmonsfree: %d removed doesn't match %d pending",
&nbsp;                   count, iflags.purge_monsters);
&nbsp;    iflags.purge_monsters = 0;
&nbsp;}
&nbsp;
replmon
&nbsp;/* called when monster is moved to larger structure */
&nbsp;void
&nbsp;replmon(mtmp, mtmp2)
&nbsp;struct monst *mtmp, *mtmp2;
&nbsp;{
&nbsp;    struct obj *otmp;
&nbsp;
&nbsp;    /* transfer the monster's inventory */
&nbsp;    for (otmp = mtmp2-&gt;minvent; otmp; otmp = otmp-&gt;nobj) {
&nbsp;        if (otmp-&gt;where != OBJ_MINVENT || otmp-&gt;ocarry != mtmp)
&nbsp;            impossible("replmon: minvent inconsistency");
&nbsp;        otmp-&gt;ocarry = mtmp2;
&nbsp;    }
&nbsp;    mtmp-&gt;minvent = 0;
&nbsp;
&nbsp;    /* remove the old monster from the map and from `fmon' list */
&nbsp;    relmon(mtmp, (struct monst **) 0);
&nbsp;
&nbsp;    /* finish adding its replacement */
&nbsp;    if (mtmp != u.usteed) /* don't place steed onto the map */
&nbsp;        place_monster(mtmp2, mtmp2-&gt;mx, mtmp2-&gt;my);
&nbsp;    if (mtmp2-&gt;wormno)      /* update level.monsters[wseg-&gt;wx][wseg-&gt;wy] */
&nbsp;        place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
&nbsp;    if (emits_light(mtmp2-&gt;data)) {
&nbsp;        /* since this is so rare, we don't have any `mon_move_light_source' */
&nbsp;        new_light_source(mtmp2-&gt;mx, mtmp2-&gt;my, emits_light(mtmp2-&gt;data),
&nbsp;                         LS_MONSTER, monst_to_any(mtmp2));
&nbsp;        /* here we rely on fact that `mtmp' hasn't actually been deleted */
&nbsp;        del_light_source(LS_MONSTER, monst_to_any(mtmp));
&nbsp;    }
&nbsp;    mtmp2-&gt;nmon = fmon;
&nbsp;    fmon = mtmp2;
&nbsp;    if (u.ustuck == mtmp)
&nbsp;        u.ustuck = mtmp2;
&nbsp;    if (u.usteed == mtmp)
&nbsp;        u.usteed = mtmp2;
&nbsp;    if (mtmp2-&gt;isshk)
&nbsp;        replshk(mtmp, mtmp2);
&nbsp;
&nbsp;    /* discard the old monster */
&nbsp;    dealloc_monst(mtmp);
&nbsp;}
&nbsp;
relmon
&nbsp;/* release mon from the display and the map's monster list,
&nbsp;   maybe transfer it to one of the other monster lists */
&nbsp;void
&nbsp;relmon(mon, monst_list)
&nbsp;struct monst *mon;
&nbsp;struct monst **monst_list; /* &amp;migrating_mons or &amp;mydogs or null */
&nbsp;{
&nbsp;    struct monst *mtmp;
&nbsp;    boolean unhide = (monst_list != 0);
&nbsp;    int mx = mon-&gt;mx, my = mon-&gt;my;
&nbsp;
&nbsp;    if (!fmon)
&nbsp;        panic("relmon: no fmon available.");
&nbsp;
&nbsp;    if (unhide) {
&nbsp;        /* can't remain hidden across level changes (exception: wizard
&nbsp;           clone can continue imitating some other monster form); also,
&nbsp;           might be imitating a boulder so need line-of-sight unblocking */
&nbsp;        mon-&gt;mundetected = 0;
&nbsp;        if (mon-&gt;m_ap_type &amp;&amp; mon-&gt;m_ap_type != M_AP_MONSTER)
&nbsp;            seemimic(mon);
&nbsp;    }
&nbsp;
&nbsp;    if (mon-&gt;wormno)
&nbsp;        remove_worm(mon);
&nbsp;    else
&nbsp;        remove_monster(mx, my);
&nbsp;
&nbsp;    if (mon == fmon) {
&nbsp;        fmon = fmon-&gt;nmon;
&nbsp;    } else {
&nbsp;        for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon)
&nbsp;            if (mtmp-&gt;nmon == mon)
&nbsp;                break;
&nbsp;
&nbsp;        if (mtmp)
&nbsp;            mtmp-&gt;nmon = mon-&gt;nmon;
&nbsp;        else
&nbsp;            panic("relmon: mon not in list.");
&nbsp;    }
&nbsp;
&nbsp;    if (unhide) {
&nbsp;        newsym(mx, my);
&nbsp;        /* insert into mydogs or migrating_mons */
&nbsp;        mon-&gt;nmon = *monst_list;
&nbsp;        *monst_list = mon;
&nbsp;    } else {
&nbsp;        /* orphan has no next monster */
&nbsp;        mon-&gt;nmon = 0;
&nbsp;    }
&nbsp;}
&nbsp;
copy_mextra
&nbsp;void
&nbsp;copy_mextra(mtmp2, mtmp1)
&nbsp;struct monst *mtmp2, *mtmp1;
&nbsp;{
&nbsp;    if (!mtmp2 || !mtmp1 || !mtmp1-&gt;mextra)
&nbsp;        return;
&nbsp;
&nbsp;    if (!mtmp2-&gt;mextra)
&nbsp;        mtmp2-&gt;mextra = newmextra();
&nbsp;    if (MNAME(mtmp1)) {
&nbsp;        new_mname(mtmp2, (int) strlen(MNAME(mtmp1)) + 1);
&nbsp;        Strcpy(MNAME(mtmp2), MNAME(mtmp1));
&nbsp;    }
&nbsp;    if (EGD(mtmp1)) {
&nbsp;        if (!EGD(mtmp2))
&nbsp;            newegd(mtmp2);
&nbsp;        *EGD(mtmp2) = *EGD(mtmp1);
&nbsp;    }
&nbsp;    if (EPRI(mtmp1)) {
&nbsp;        if (!EPRI(mtmp2))
&nbsp;            newepri(mtmp2);
&nbsp;        *EPRI(mtmp2) = *EPRI(mtmp1);
&nbsp;    }
&nbsp;    if (ESHK(mtmp1)) {
&nbsp;        if (!ESHK(mtmp2))
&nbsp;            neweshk(mtmp2);
&nbsp;        *ESHK(mtmp2) = *ESHK(mtmp1);
&nbsp;    }
&nbsp;    if (EMIN(mtmp1)) {
&nbsp;        if (!EMIN(mtmp2))
&nbsp;            newemin(mtmp2);
&nbsp;        *EMIN(mtmp2) = *EMIN(mtmp1);
&nbsp;    }
&nbsp;    if (EDOG(mtmp1)) {
&nbsp;        if (!EDOG(mtmp2))
&nbsp;            newedog(mtmp2);
&nbsp;        *EDOG(mtmp2) = *EDOG(mtmp1);
&nbsp;    }
&nbsp;    if (has_mcorpsenm(mtmp1))
&nbsp;        MCORPSENM(mtmp2) = MCORPSENM(mtmp1);
&nbsp;}
&nbsp;
dealloc_mextra
&nbsp;void
&nbsp;dealloc_mextra(m)
&nbsp;struct monst *m;
&nbsp;{
&nbsp;    struct mextra *x = m-&gt;mextra;
&nbsp;
&nbsp;    if (x) {
&nbsp;        if (x-&gt;mname)
&nbsp;            free((genericptr_t) x-&gt;mname);
&nbsp;        if (x-&gt;egd)
&nbsp;            free((genericptr_t) x-&gt;egd);
&nbsp;        if (x-&gt;epri)
&nbsp;            free((genericptr_t) x-&gt;epri);
&nbsp;        if (x-&gt;eshk)
&nbsp;            free((genericptr_t) x-&gt;eshk);
&nbsp;        if (x-&gt;emin)
&nbsp;            free((genericptr_t) x-&gt;emin);
&nbsp;        if (x-&gt;edog)
&nbsp;            free((genericptr_t) x-&gt;edog);
&nbsp;        /* [no action needed for x-&gt;mcorpsenm] */
&nbsp;
&nbsp;        free((genericptr_t) x);
&nbsp;        m-&gt;mextra = (struct mextra *) 0;
&nbsp;    }
&nbsp;}
&nbsp;
dealloc_monst
&nbsp;void
&nbsp;dealloc_monst(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    if (mon-&gt;nmon)
&nbsp;        panic("dealloc_monst with nmon");
&nbsp;    if (mon-&gt;mextra)
&nbsp;        dealloc_mextra(mon);
&nbsp;    free((genericptr_t) mon);
&nbsp;}
&nbsp;
m_detach
&nbsp;/* remove effects of mtmp from other data structures */
&nbsp;STATIC_OVL void
&nbsp;m_detach(mtmp, mptr)
&nbsp;struct monst *mtmp;
&nbsp;struct permonst *mptr; /* reflects mtmp-&gt;data _prior_ to mtmp's death */
&nbsp;{
&nbsp;    if (mtmp == context.polearm.hitmon)
&nbsp;        context.polearm.hitmon = 0;
&nbsp;    if (mtmp-&gt;mleashed)
&nbsp;        m_unleash(mtmp, FALSE);
&nbsp;    /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
&nbsp;    mtmp-&gt;mtrapped = 0;
&nbsp;    mtmp-&gt;mhp = 0; /* simplify some tests: force mhp to 0 */
&nbsp;    relobj(mtmp, 0, FALSE);
&nbsp;    remove_monster(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    if (emits_light(mptr))
&nbsp;        del_light_source(LS_MONSTER, monst_to_any(mtmp));
&nbsp;    if (mtmp-&gt;m_ap_type)
&nbsp;        seemimic(mtmp);
&nbsp;    newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    unstuck(mtmp);
&nbsp;    fill_pit(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;
&nbsp;    if (mtmp-&gt;isshk)
&nbsp;        shkgone(mtmp);
&nbsp;    if (mtmp-&gt;wormno)
&nbsp;        wormgone(mtmp);
&nbsp;    iflags.purge_monsters++;
&nbsp;}
&nbsp;
mlifesaver
&nbsp;/* find the worn amulet of life saving which will save a monster */
&nbsp;struct obj *
&nbsp;mlifesaver(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    if (!nonliving(mon-&gt;data) || is_vampshifter(mon)) {
&nbsp;        struct obj *otmp = which_armor(mon, W_AMUL);
&nbsp;
&nbsp;        if (otmp &amp;&amp; otmp-&gt;otyp == AMULET_OF_LIFE_SAVING)
&nbsp;            return otmp;
&nbsp;    }
&nbsp;    return (struct obj *) 0;
&nbsp;}
&nbsp;
lifesaved_monster
&nbsp;STATIC_OVL void
&nbsp;lifesaved_monster(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    boolean surviver;
&nbsp;    struct obj *lifesave = mlifesaver(mtmp);
&nbsp;
&nbsp;    if (lifesave) {
&nbsp;        /* not canseemon; amulets are on the head, so you don't want
&nbsp;         * to show this for a long worm with only a tail visible.
&nbsp;         * Nor do you check invisibility, because glowing and
&nbsp;         * disintegrating amulets are always visible. */
&nbsp;        if (cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;            pline("But wait...");
&nbsp;            pline("%s medallion begins to glow!", s_suffix(Monnam(mtmp)));
&nbsp;            makeknown(AMULET_OF_LIFE_SAVING);
&nbsp;            /* amulet is visible, but monster might not be */
&nbsp;            if (canseemon(mtmp)) {
&nbsp;                if (attacktype(mtmp-&gt;data, AT_EXPL)
&nbsp;                    || attacktype(mtmp-&gt;data, AT_BOOM))
&nbsp;                    pline("%s reconstitutes!", Monnam(mtmp));
&nbsp;                else
&nbsp;                    pline("%s looks much better!", Monnam(mtmp));
&nbsp;            }
&nbsp;            pline_The("medallion crumbles to dust!");
&nbsp;        }
&nbsp;        m_useup(mtmp, lifesave);
&nbsp;
&nbsp;        surviver = !(mvitals[monsndx(mtmp-&gt;data)].mvflags &amp; G_GENOD);
&nbsp;        mtmp-&gt;mcanmove = 1;
&nbsp;        mtmp-&gt;mfrozen = 0;
&nbsp;        if (mtmp-&gt;mtame &amp;&amp; !mtmp-&gt;isminion) {
&nbsp;            wary_dog(mtmp, !surviver);
&nbsp;        }
&nbsp;        if (mtmp-&gt;mhpmax &lt;= 0)
&nbsp;            mtmp-&gt;mhpmax = 10;
&nbsp;        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;        if (surviver)
&nbsp;            return;
&nbsp;
&nbsp;        /* genocided monster can't be life-saved */
&nbsp;        if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;            pline("Unfortunately, %s is still genocided...", mon_nam(mtmp));
&nbsp;        mtmp-&gt;mhp = 0;
&nbsp;    }
&nbsp;}
&nbsp;
mondead
&nbsp;void
&nbsp;mondead(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    struct permonst *mptr;
&nbsp;    int tmp;
&nbsp;
&nbsp;    mtmp-&gt;mhp = 0; /* in case caller hasn't done this */
&nbsp;    lifesaved_monster(mtmp);
&nbsp;    if (mtmp-&gt;mhp &gt; 0)
&nbsp;        return;
&nbsp;
&nbsp;    if (is_vampshifter(mtmp)) {
&nbsp;        int mndx = mtmp-&gt;cham;
&nbsp;        int x = mtmp-&gt;mx, y = mtmp-&gt;my;
&nbsp;
&nbsp;        /* this only happens if shapeshifted */
&nbsp;        if (mndx &gt;= LOW_PM &amp;&amp; mndx != monsndx(mtmp-&gt;data)
&nbsp;            &amp;&amp; !(mvitals[mndx].mvflags &amp; G_GENOD)) {
&nbsp;            char buf[BUFSZ];
&nbsp;            boolean in_door = (amorphous(mtmp-&gt;data)
&nbsp;                               &amp;&amp; closed_door(mtmp-&gt;mx, mtmp-&gt;my)),
&nbsp;                /* alternate message phrasing for some monster types */
&nbsp;                spec_mon = (nonliving(mtmp-&gt;data)
&nbsp;                            || noncorporeal(mtmp-&gt;data)
&nbsp;                            || amorphous(mtmp-&gt;data)),
&nbsp;                spec_death = (disintegested /* disintegrated or digested */
&nbsp;                              || noncorporeal(mtmp-&gt;data)
&nbsp;                              || amorphous(mtmp-&gt;data));
&nbsp;
&nbsp;            /* construct a format string before transformation;
&nbsp;               will be capitalized when used, expects one %s arg */
&nbsp;            Sprintf(buf, "%s suddenly %s and rises as %%s!",
&nbsp;                    x_monnam(mtmp, ARTICLE_THE,
&nbsp;                             spec_mon ? (char *) 0 : "seemingly dead",
&nbsp;                             SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
&nbsp;                                 | SUPPRESS_INVISIBLE | SUPPRESS_IT,
&nbsp;                             FALSE),
&nbsp;                    spec_death ? "reconstitutes" : "transforms");
&nbsp;            mtmp-&gt;mcanmove = 1;
&nbsp;            mtmp-&gt;mfrozen = 0;
&nbsp;            if (mtmp-&gt;mhpmax &lt;= 0)
&nbsp;                mtmp-&gt;mhpmax = 10;
&nbsp;            mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;            /* this can happen if previously a fog cloud */
&nbsp;            if (u.uswallow &amp;&amp; (mtmp == u.ustuck))
&nbsp;                expels(mtmp, mtmp-&gt;data, FALSE);
&nbsp;            if (in_door) {
&nbsp;                coord new_xy;
&nbsp;
&nbsp;                if (enexto(&amp;new_xy, mtmp-&gt;mx, mtmp-&gt;my, &amp;mons[mndx])) {
&nbsp;                    rloc_to(mtmp, new_xy.x, new_xy.y);
&nbsp;                }
&nbsp;            }
&nbsp;            newcham(mtmp, &amp;mons[mndx], FALSE, FALSE);
&nbsp;            if (mtmp-&gt;data == &amp;mons[mndx])
&nbsp;                mtmp-&gt;cham = NON_PM;
&nbsp;            else
&nbsp;                mtmp-&gt;cham = mndx;
&nbsp;            if (canspotmon(mtmp)) {
&nbsp;                /* was using a_monnam(mtmp) but that's weird if mtmp is named:
&nbsp;                   "Dracula suddenly transforms and rises as Dracula" */
&nbsp;                pline(upstart(buf), an(mtmp-&gt;data-&gt;mname));
&nbsp;                vamp_rise_msg = TRUE;
&nbsp;            }
&nbsp;            newsym(x, y);
&nbsp;            return;
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* dead vault guard is actually kept at coordinate &lt;0,0&gt; until
&nbsp;       his temporary corridor to/from the vault has been removed;
&nbsp;       need to do this after life-saving and before m_detach() */
&nbsp;    if (mtmp-&gt;isgd &amp;&amp; !grddead(mtmp))
&nbsp;        return;
&nbsp;
&nbsp;    /* Player is thrown from his steed when it dies */
&nbsp;    if (mtmp == u.usteed)
&nbsp;        dismount_steed(DISMOUNT_GENERIC);
&nbsp;
&nbsp;    mptr = mtmp-&gt;data; /* save this for m_detach() */
&nbsp;    /* restore chameleon, lycanthropes to true form at death */
&nbsp;    if (mtmp-&gt;cham &gt;= LOW_PM) {
&nbsp;        set_mon_data(mtmp, &amp;mons[mtmp-&gt;cham], -1);
&nbsp;        mtmp-&gt;cham = NON_PM;
&nbsp;    } else if (mtmp-&gt;data == &amp;mons[PM_WEREJACKAL])
&nbsp;        set_mon_data(mtmp, &amp;mons[PM_HUMAN_WEREJACKAL], -1);
&nbsp;    else if (mtmp-&gt;data == &amp;mons[PM_WEREWOLF])
&nbsp;        set_mon_data(mtmp, &amp;mons[PM_HUMAN_WEREWOLF], -1);
&nbsp;    else if (mtmp-&gt;data == &amp;mons[PM_WERERAT])
&nbsp;        set_mon_data(mtmp, &amp;mons[PM_HUMAN_WERERAT], -1);
&nbsp;
&nbsp;    /*
&nbsp;     * mvitals[].died does double duty as total number of dead monsters
&nbsp;     * and as experience factor for the player killing more monsters.
&nbsp;     * this means that a dragon dying by other means reduces the
&nbsp;     * experience the player gets for killing a dragon directly; this
&nbsp;     * is probably not too bad, since the player likely finagled the
&nbsp;     * first dead dragon via ring of conflict or pets, and extinguishing
&nbsp;     * based on only player kills probably opens more avenues of abuse
&nbsp;     * for rings of conflict and such.
&nbsp;     */
&nbsp;    tmp = monsndx(mtmp-&gt;data);
&nbsp;    if (mvitals[tmp].died &lt; 255)
&nbsp;        mvitals[tmp].died++;
&nbsp;
&nbsp;    /* if it's a (possibly polymorphed) quest leader, mark him as dead */
&nbsp;    if (mtmp-&gt;m_id == quest_status.leader_m_id)
&nbsp;        quest_status.leader_is_dead = TRUE;
&nbsp;#ifdef MAIL
&nbsp;    /* if the mail daemon dies, no more mail delivery.  -3. */
&nbsp;    if (tmp == PM_MAIL_DAEMON)
&nbsp;        mvitals[tmp].mvflags |= G_GENOD;
&nbsp;#endif
&nbsp;
&nbsp;    if (mtmp-&gt;data-&gt;mlet == S_KOP) {
&nbsp;        /* Dead Kops may come back. */
&nbsp;        switch (rnd(5)) {
&nbsp;        case 1: /* returns near the stairs */
&nbsp;            (void) makemon(mtmp-&gt;data, xdnstair, ydnstair, NO_MM_FLAGS);
&nbsp;            break;
&nbsp;        case 2: /* randomly */
&nbsp;            (void) makemon(mtmp-&gt;data, 0, 0, NO_MM_FLAGS);
&nbsp;            break;
&nbsp;        default:
&nbsp;            break;
&nbsp;        }
&nbsp;    }
&nbsp;    if (mtmp-&gt;iswiz)
&nbsp;        wizdead();
&nbsp;    if (mtmp-&gt;data-&gt;msound == MS_NEMESIS)
&nbsp;        nemdead();
&nbsp;    if (mtmp-&gt;data == &amp;mons[PM_MEDUSA])
&nbsp;        u.uachieve.killed_medusa = 1;
&nbsp;    if (glyph_is_invisible(levl[mtmp-&gt;mx][mtmp-&gt;my].glyph))
&nbsp;        unmap_object(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    m_detach(mtmp, mptr);
&nbsp;}
&nbsp;
corpse_chance
&nbsp;/* TRUE if corpse might be dropped, magr may die if mon was swallowed */
&nbsp;boolean
&nbsp;corpse_chance(mon, magr, was_swallowed)
&nbsp;struct monst *mon;
&nbsp;struct monst *magr;    /* killer, if swallowed */
&nbsp;boolean was_swallowed; /* digestion */
&nbsp;{
&nbsp;    struct permonst *mdat = mon-&gt;data;
&nbsp;    int i, tmp;
&nbsp;
&nbsp;    if (mdat == &amp;mons[PM_VLAD_THE_IMPALER] || mdat-&gt;mlet == S_LICH) {
&nbsp;        if (cansee(mon-&gt;mx, mon-&gt;my) &amp;&amp; !was_swallowed)
&nbsp;            pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
&nbsp;        return FALSE;
&nbsp;    }
&nbsp;
&nbsp;    /* Gas spores always explode upon death */
&nbsp;    for (i = 0; i &lt; NATTK; i++) {
&nbsp;        if (mdat-&gt;mattk[i].aatyp == AT_BOOM) {
&nbsp;            if (mdat-&gt;mattk[i].damn)
&nbsp;                tmp = d((int) mdat-&gt;mattk[i].damn, (int) mdat-&gt;mattk[i].damd);
&nbsp;            else if (mdat-&gt;mattk[i].damd)
&nbsp;                tmp = d((int) mdat-&gt;mlevel + 1, (int) mdat-&gt;mattk[i].damd);
&nbsp;            else
&nbsp;                tmp = 0;
&nbsp;            if (was_swallowed &amp;&amp; magr) {
&nbsp;                if (magr == &amp;youmonst) {
&nbsp;                    There("is an explosion in your %s!", body_part(STOMACH));
&nbsp;                    Sprintf(killer.name, "%s explosion",
&nbsp;                            s_suffix(mdat-&gt;mname));
&nbsp;                    losehp(Maybe_Half_Phys(tmp), killer.name, KILLED_BY_AN);
&nbsp;                } else {
&nbsp;                    You_hear("an explosion.");
&nbsp;                    magr-&gt;mhp -= tmp;
&nbsp;                    if (magr-&gt;mhp &lt; 1)
&nbsp;                        mondied(magr);
&nbsp;                    if (magr-&gt;mhp &lt; 1) { /* maybe lifesaved */
&nbsp;                        if (canspotmon(magr))
&nbsp;                            pline("%s rips open!", Monnam(magr));
&nbsp;                    } else if (canseemon(magr))
&nbsp;                        pline("%s seems to have indigestion.", Monnam(magr));
&nbsp;                }
&nbsp;
&nbsp;                return FALSE;
&nbsp;            }
&nbsp;
&nbsp;            Sprintf(killer.name, "%s explosion", s_suffix(mdat-&gt;mname));
&nbsp;            killer.format = KILLED_BY_AN;
&nbsp;            explode(mon-&gt;mx, mon-&gt;my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS);
&nbsp;            killer.name[0] = '\0';
&nbsp;            killer.format = 0;
&nbsp;            return FALSE;
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* must duplicate this below check in xkilled() since it results in
&nbsp;     * creating no objects as well as no corpse
&nbsp;     */
&nbsp;    if (LEVEL_SPECIFIC_NOCORPSE(mdat))
&nbsp;        return FALSE;
&nbsp;
&nbsp;    if (((bigmonst(mdat) || mdat == &amp;mons[PM_LIZARD]) &amp;&amp; !mon-&gt;mcloned)
&nbsp;        || is_golem(mdat) || is_mplayer(mdat) || is_rider(mdat) || mon-&gt;isshk)
&nbsp;        return TRUE;
&nbsp;    tmp = 2 + ((mdat-&gt;geno &amp; G_FREQ) &lt; 2) + verysmall(mdat);
&nbsp;    return (boolean) !rn2(tmp);
&nbsp;}
&nbsp;
mondied
&nbsp;/* drop (perhaps) a cadaver and remove monster */
&nbsp;void
&nbsp;mondied(mdef)
&nbsp;register struct monst *mdef;
&nbsp;{
&nbsp;    mondead(mdef);
&nbsp;    if (mdef-&gt;mhp &gt; 0)
&nbsp;        return; /* lifesaved */
&nbsp;
&nbsp;    if (corpse_chance(mdef, (struct monst *) 0, FALSE)
&nbsp;        &amp;&amp; (accessible(mdef-&gt;mx, mdef-&gt;my) || is_pool(mdef-&gt;mx, mdef-&gt;my)))
&nbsp;        (void) make_corpse(mdef, CORPSTAT_NONE);
&nbsp;}
&nbsp;
mongone
&nbsp;/* monster disappears, not dies */
&nbsp;void
&nbsp;mongone(mdef)
&nbsp;struct monst *mdef;
&nbsp;{
&nbsp;    mdef-&gt;mhp = 0; /* can skip some inventory bookkeeping */
&nbsp;
&nbsp;    /* dead vault guard is actually kept at coordinate &lt;0,0&gt; until
&nbsp;       his temporary corridor to/from the vault has been removed */
&nbsp;    if (mdef-&gt;isgd &amp;&amp; !grddead(mdef))
&nbsp;        return;
&nbsp;    /* hero is thrown from his steed when it disappears */
&nbsp;    if (mdef == u.usteed)
&nbsp;        dismount_steed(DISMOUNT_GENERIC);
&nbsp;    /* stuck to you? release */
&nbsp;    unstuck(mdef);
&nbsp;    /* drop special items like the Amulet so that a dismissed Kop or nurse
&nbsp;       can't remove them from the game */
&nbsp;    mdrop_special_objs(mdef);
&nbsp;    /* release rest of monster's inventory--it is removed from game */
&nbsp;    discard_minvent(mdef);
&nbsp;    m_detach(mdef, mdef-&gt;data);
&nbsp;}
&nbsp;
monstone
&nbsp;/* drop a statue or rock and remove monster */
&nbsp;void
&nbsp;monstone(mdef)
&nbsp;struct monst *mdef;
&nbsp;{
&nbsp;    struct obj *otmp, *obj, *oldminvent;
&nbsp;    xchar x = mdef-&gt;mx, y = mdef-&gt;my;
&nbsp;    boolean wasinside = FALSE;
&nbsp;
&nbsp;    if (!vamp_stone(mdef)) /* vampshifter reverts to vampire */
&nbsp;        return;
&nbsp;
&nbsp;    /* we have to make the statue before calling mondead, to be able to
&nbsp;     * put inventory in it, and we have to check for lifesaving before
&nbsp;     * making the statue....
&nbsp;     */
&nbsp;    mdef-&gt;mhp = 0; /* in case caller hasn't done this */
&nbsp;    lifesaved_monster(mdef);
&nbsp;    if (mdef-&gt;mhp &gt; 0)
&nbsp;        return;
&nbsp;
&nbsp;    mdef-&gt;mtrapped = 0; /* (see m_detach) */
&nbsp;
&nbsp;    if ((int) mdef-&gt;data-&gt;msize &gt; MZ_TINY
&nbsp;        || !rn2(2 + ((int) (mdef-&gt;data-&gt;geno &amp; G_FREQ) &gt; 2))) {
&nbsp;        oldminvent = 0;
&nbsp;        /* some objects may end up outside the statue */
&nbsp;        while ((obj = mdef-&gt;minvent) != 0) {
&nbsp;            obj_extract_self(obj);
&nbsp;            if (obj-&gt;owornmask)
&nbsp;                update_mon_intrinsics(mdef, obj, FALSE, TRUE);
&nbsp;            obj_no_longer_held(obj);
&nbsp;            if (obj-&gt;owornmask &amp; W_WEP)
&nbsp;                setmnotwielded(mdef, obj);
&nbsp;            obj-&gt;owornmask = 0L;
&nbsp;            if (obj-&gt;otyp == BOULDER
&nbsp;#if 0 /* monsters don't carry statues */
&nbsp;                ||  (obj-&gt;otyp == STATUE
&nbsp;                     &amp;&amp; mons[obj-&gt;corpsenm].msize &gt;= mdef-&gt;data-&gt;msize)
&nbsp;#endif
&nbsp;                /* invocation tools resist even with 0% resistance */
&nbsp;                || obj_resists(obj, 0, 0)) {
&nbsp;                if (flooreffects(obj, x, y, "fall"))
&nbsp;                    continue;
&nbsp;                place_object(obj, x, y);
&nbsp;            } else {
&nbsp;                if (obj-&gt;lamplit)
&nbsp;                    end_burn(obj, TRUE);
&nbsp;                obj-&gt;nobj = oldminvent;
&nbsp;                oldminvent = obj;
&nbsp;            }
&nbsp;        }
&nbsp;        /* defer statue creation until after inventory removal
&nbsp;           so that saved monster traits won't retain any stale
&nbsp;           item-conferred attributes */
&nbsp;        otmp = mkcorpstat(STATUE, mdef, mdef-&gt;data, x, y, CORPSTAT_NONE);
&nbsp;        if (has_mname(mdef))
&nbsp;            otmp = oname(otmp, MNAME(mdef));
&nbsp;        while ((obj = oldminvent) != 0) {
&nbsp;            oldminvent = obj-&gt;nobj;
&nbsp;            (void) add_to_container(otmp, obj);
&nbsp;        }
&nbsp;        /* Archeologists should not break unique statues */
&nbsp;        if (mdef-&gt;data-&gt;geno &amp; G_UNIQ)
&nbsp;            otmp-&gt;spe = 1;
&nbsp;        otmp-&gt;owt = weight(otmp);
&nbsp;    } else
&nbsp;        otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
&nbsp;
&nbsp;    stackobj(otmp);
&nbsp;    /* mondead() already does this, but we must do it before the newsym */
&nbsp;    if (glyph_is_invisible(levl[x][y].glyph))
&nbsp;        unmap_object(x, y);
&nbsp;    if (cansee(x, y))
&nbsp;        newsym(x, y);
&nbsp;    /* We don't currently trap the hero in the statue in this case but we
&nbsp;     * could */
&nbsp;    if (u.uswallow &amp;&amp; u.ustuck == mdef)
&nbsp;        wasinside = TRUE;
&nbsp;    mondead(mdef);
&nbsp;    if (wasinside) {
&nbsp;        if (is_animal(mdef-&gt;data))
&nbsp;            You("%s through an opening in the new %s.",
&nbsp;                locomotion(youmonst.data, "jump"), xname(otmp));
&nbsp;    }
&nbsp;}
&nbsp;
monkilled
&nbsp;/* another monster has killed the monster mdef */
&nbsp;void
&nbsp;monkilled(mdef, fltxt, how)
&nbsp;struct monst *mdef;
&nbsp;const char *fltxt;
&nbsp;int how;
&nbsp;{
&nbsp;    boolean be_sad = FALSE; /* true if unseen pet is killed */
&nbsp;
&nbsp;    if ((mdef-&gt;wormno ? worm_known(mdef) : cansee(mdef-&gt;mx, mdef-&gt;my))
&nbsp;        &amp;&amp; fltxt)
&nbsp;        pline("%s is %s%s%s!", Monnam(mdef),
&nbsp;              nonliving(mdef-&gt;data) ? "destroyed" : "killed",
&nbsp;              *fltxt ? " by the " : "", fltxt);
&nbsp;    else
&nbsp;        be_sad = (mdef-&gt;mtame != 0);
&nbsp;
&nbsp;    /* no corpses if digested or disintegrated */
&nbsp;    disintegested = (how == AD_DGST || how == -AD_RBRE);
&nbsp;    if (disintegested)
&nbsp;        mondead(mdef);
&nbsp;    else
&nbsp;        mondied(mdef);
&nbsp;
&nbsp;    if (be_sad &amp;&amp; mdef-&gt;mhp &lt;= 0)
&nbsp;        You("have a sad feeling for a moment, then it passes.");
&nbsp;}
&nbsp;
unstuck
&nbsp;void
&nbsp;unstuck(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (u.ustuck == mtmp) {
&nbsp;        if (u.uswallow) {
&nbsp;            u.ux = mtmp-&gt;mx;
&nbsp;            u.uy = mtmp-&gt;my;
&nbsp;            u.uswallow = 0;
&nbsp;            u.uswldtim = 0;
&nbsp;            if (Punished &amp;&amp; uchain-&gt;where != OBJ_FLOOR)
&nbsp;                placebc();
&nbsp;            vision_full_recalc = 1;
&nbsp;            docrt();
&nbsp;            /* prevent swallower (mtmp might have just poly'd into something
&nbsp;               without an engulf attack) from immediately re-engulfing */
&nbsp;            if (attacktype(mtmp-&gt;data, AT_ENGL) &amp;&amp; !mtmp-&gt;mspec_used)
&nbsp;                mtmp-&gt;mspec_used = rnd(2);
&nbsp;        }
&nbsp;        u.ustuck = 0;
&nbsp;    }
&nbsp;}
&nbsp;
killed
&nbsp;void
&nbsp;killed(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    xkilled(mtmp, XKILL_GIVEMSG);
&nbsp;}
&nbsp;
xkilled
&nbsp;/* the player has killed the monster mtmp */
&nbsp;void
&nbsp;xkilled(mtmp, xkill_flags)
&nbsp;struct monst *mtmp;
&nbsp;int xkill_flags; /* 1: suppress message, 2: suppress corpse, 4: pacifist */
&nbsp;{
&nbsp;    int tmp, mndx, x = mtmp-&gt;mx, y = mtmp-&gt;my;
&nbsp;    struct permonst *mdat;
&nbsp;    struct obj *otmp;
&nbsp;    struct trap *t;
&nbsp;    boolean wasinside = u.uswallow &amp;&amp; (u.ustuck == mtmp),
&nbsp;            burycorpse = FALSE,
&nbsp;            nomsg = (xkill_flags &amp; XKILL_NOMSG) != 0,
&nbsp;            nocorpse = (xkill_flags &amp; XKILL_NOCORPSE) != 0,
&nbsp;            noconduct = (xkill_flags &amp; XKILL_NOCONDUCT) != 0;
&nbsp;
&nbsp;    mtmp-&gt;mhp = 0; /* caller will usually have already done this */
&nbsp;    if (!noconduct) /* KMH, conduct */
&nbsp;        u.uconduct.killer++;
&nbsp;
&nbsp;    if (!nomsg) {
&nbsp;        boolean namedpet = has_mname(mtmp) &amp;&amp; !Hallucination;
&nbsp;
&nbsp;        You("%s %s!",
&nbsp;            nonliving(mtmp-&gt;data) ? "destroy" : "kill",
&nbsp;            !(wasinside || canspotmon(mtmp)) ? "it"
&nbsp;              : !mtmp-&gt;mtame ? mon_nam(mtmp)
&nbsp;                : x_monnam(mtmp, namedpet ? ARTICLE_NONE : ARTICLE_THE,
&nbsp;                           "poor", namedpet ? SUPPRESS_SADDLE : 0, FALSE));
&nbsp;    }
&nbsp;
&nbsp;    if (mtmp-&gt;mtrapped &amp;&amp; (t = t_at(x, y)) != 0
&nbsp;        &amp;&amp; (t-&gt;ttyp == PIT || t-&gt;ttyp == SPIKED_PIT)) {
&nbsp;        if (sobj_at(BOULDER, x, y))
&nbsp;            nocorpse = TRUE; /* Prevent corpses/treasure being created
&nbsp;                                "on top" of boulder that is about to fall in.
&nbsp;                                This is out of order, but cannot be helped
&nbsp;                                unless this whole routine is rearranged. */
&nbsp;        if (m_carrying(mtmp, BOULDER))
&nbsp;            burycorpse = TRUE;
&nbsp;    }
&nbsp;
&nbsp;    /* your pet knows who just killed it...watch out */
&nbsp;    if (mtmp-&gt;mtame &amp;&amp; !mtmp-&gt;isminion)
&nbsp;        EDOG(mtmp)-&gt;killed_by_u = 1;
&nbsp;
&nbsp;    if (wasinside &amp;&amp; thrownobj &amp;&amp; thrownobj != uball) {
&nbsp;        /* thrown object has killed hero's engulfer; add it to mon's
&nbsp;           inventory now so that it will be placed with mon's other
&nbsp;           stuff prior to lookhere/autopickup when hero is expelled
&nbsp;           below (as a side-effect, this missile has immunity from
&nbsp;           being consumed [for this shot/throw only]) */
&nbsp;        mpickobj(mtmp, thrownobj);
&nbsp;        /* let throwing code know that missile has been disposed of */
&nbsp;        thrownobj = 0;
&nbsp;    }
&nbsp;
&nbsp;    vamp_rise_msg = FALSE; /* might get set in mondead(); only checked below */
&nbsp;    disintegested = nocorpse; /* alternate vamp_rise message needed if true */
&nbsp;    /* dispose of monster and make cadaver */
&nbsp;    if (stoned)
&nbsp;        monstone(mtmp);
&nbsp;    else
&nbsp;        mondead(mtmp);
&nbsp;    disintegested = FALSE; /* reset */
&nbsp;
&nbsp;    if (mtmp-&gt;mhp &gt; 0) { /* monster lifesaved */
&nbsp;        /* Cannot put the non-visible lifesaving message in
&nbsp;         * lifesaved_monster() since the message appears only when _you_
&nbsp;         * kill it (as opposed to visible lifesaving which always appears).
&nbsp;         */
&nbsp;        stoned = FALSE;
&nbsp;        if (!cansee(x, y) &amp;&amp; !vamp_rise_msg)
&nbsp;            pline("Maybe not...");
&nbsp;        return;
&nbsp;    }
&nbsp;
&nbsp;    mdat = mtmp-&gt;data; /* note: mondead can change mtmp-&gt;data */
&nbsp;    mndx = monsndx(mdat);
&nbsp;
&nbsp;    if (stoned) {
&nbsp;        stoned = FALSE;
&nbsp;        goto cleanup;
&nbsp;    }
&nbsp;
&nbsp;    if (nocorpse || LEVEL_SPECIFIC_NOCORPSE(mdat))
&nbsp;        goto cleanup;
&nbsp;
&nbsp;#ifdef MAIL
&nbsp;    if (mdat == &amp;mons[PM_MAIL_DAEMON]) {
&nbsp;        stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
&nbsp;    }
&nbsp;#endif
&nbsp;    if (accessible(x, y) || is_pool(x, y)) {
&nbsp;        struct obj *cadaver;
&nbsp;        int otyp;
&nbsp;
&nbsp;        /* illogical but traditional "treasure drop" */
&nbsp;        if (!rn2(6) &amp;&amp; !(mvitals[mndx].mvflags &amp; G_NOCORPSE)
&nbsp;            /* no extra item from swallower or steed */
&nbsp;            &amp;&amp; (x != u.ux || y != u.uy)
&nbsp;            /* no extra item from kops--too easy to abuse */
&nbsp;            &amp;&amp; mdat-&gt;mlet != S_KOP
&nbsp;            /* no items from cloned monsters */
&nbsp;            &amp;&amp; !mtmp-&gt;mcloned) {
&nbsp;            otmp = mkobj(RANDOM_CLASS, TRUE);
&nbsp;            /* don't create large objects from small monsters */
&nbsp;            otyp = otmp-&gt;otyp;
&nbsp;            if (mdat-&gt;msize &lt; MZ_HUMAN &amp;&amp; otyp != FIGURINE
&nbsp;                /* oc_big is also oc_bimanual and oc_bulky */
&nbsp;                &amp;&amp; (otmp-&gt;owt &gt; 30 || objects[otyp].oc_big)) {
&nbsp;                delobj(otmp);
&nbsp;            } else if (!flooreffects(otmp, x, y, nomsg ? "" : "fall")) {
&nbsp;                place_object(otmp, x, y);
&nbsp;                stackobj(otmp);
&nbsp;            }
&nbsp;        }
&nbsp;        /* corpse--none if hero was inside the monster */
&nbsp;        if (!wasinside &amp;&amp; corpse_chance(mtmp, (struct monst *) 0, FALSE)) {
&nbsp;            cadaver = make_corpse(mtmp, burycorpse ? CORPSTAT_BURIED
&nbsp;                                                   : CORPSTAT_NONE);
&nbsp;            if (burycorpse &amp;&amp; cadaver &amp;&amp; cansee(x, y) &amp;&amp; !mtmp-&gt;minvis
&nbsp;                &amp;&amp; cadaver-&gt;where == OBJ_BURIED &amp;&amp; !nomsg) {
&nbsp;                pline("%s corpse ends up buried.", s_suffix(Monnam(mtmp)));
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    if (wasinside)
&nbsp;        spoteffects(TRUE); /* poor man's expels() */
&nbsp;    /* monster is gone, corpse or other object might now be visible */
&nbsp;    newsym(x, y);
&nbsp;
&nbsp;cleanup:
&nbsp;    /* punish bad behaviour */
&nbsp;    if (is_human(mdat)
&nbsp;        &amp;&amp; (!always_hostile(mdat) &amp;&amp; mtmp-&gt;malign &lt;= 0)
&nbsp;        &amp;&amp; (mndx &lt; PM_ARCHEOLOGIST || mndx &gt; PM_WIZARD)
&nbsp;        &amp;&amp; u.ualign.type != A_CHAOTIC) {
&nbsp;        HTelepat &amp;= ~INTRINSIC;
&nbsp;        change_luck(-2);
&nbsp;        You("murderer!");
&nbsp;        if (Blind &amp;&amp; !Blind_telepat)
&nbsp;            see_monsters(); /* Can't sense monsters any more. */
&nbsp;    }
&nbsp;    if ((mtmp-&gt;mpeaceful &amp;&amp; !rn2(2)) || mtmp-&gt;mtame)
&nbsp;        change_luck(-1);
&nbsp;    if (is_unicorn(mdat) &amp;&amp; sgn(u.ualign.type) == sgn(mdat-&gt;maligntyp)) {
&nbsp;        change_luck(-5);
&nbsp;        You_feel("guilty...");
&nbsp;    }
&nbsp;
&nbsp;    /* give experience points */
&nbsp;    tmp = experience(mtmp, (int) mvitals[mndx].died);
&nbsp;    more_experienced(tmp, 0);
&nbsp;    newexplevel(); /* will decide if you go up */
&nbsp;
&nbsp;    /* adjust alignment points */
&nbsp;    if (mtmp-&gt;m_id == quest_status.leader_m_id) { /* REAL BAD! */
&nbsp;        adjalign(-(u.ualign.record + (int) ALIGNLIM / 2));
&nbsp;        pline("That was %sa bad idea...",
&nbsp;              u.uevent.qcompleted ? "probably " : "");
&nbsp;    } else if (mdat-&gt;msound == MS_NEMESIS) { /* Real good! */
&nbsp;        adjalign((int) (ALIGNLIM / 4));
&nbsp;    } else if (mdat-&gt;msound == MS_GUARDIAN) { /* Bad */
&nbsp;        adjalign(-(int) (ALIGNLIM / 8));
&nbsp;        if (!Hallucination)
&nbsp;            pline("That was probably a bad idea...");
&nbsp;        else
&nbsp;            pline("Whoopsie-daisy!");
&nbsp;    } else if (mtmp-&gt;ispriest) {
&nbsp;        adjalign((p_coaligned(mtmp)) ? -2 : 2);
&nbsp;        /* cancel divine protection for killing your priest */
&nbsp;        if (p_coaligned(mtmp))
&nbsp;            u.ublessed = 0;
&nbsp;        if (mdat-&gt;maligntyp == A_NONE)
&nbsp;            adjalign((int) (ALIGNLIM / 4)); /* BIG bonus */
&nbsp;    } else if (mtmp-&gt;mtame) {
&nbsp;        adjalign(-15); /* bad!! */
&nbsp;        /* your god is mighty displeased... */
&nbsp;        if (!Hallucination)
&nbsp;            You_hear("the rumble of distant thunder...");
&nbsp;        else
&nbsp;            You_hear("the studio audience applaud!");
&nbsp;    } else if (mtmp-&gt;mpeaceful)
&nbsp;        adjalign(-5);
&nbsp;
&nbsp;    /* malign was already adjusted for u.ualign.type and randomization */
&nbsp;    adjalign(mtmp-&gt;malign);
&nbsp;}
&nbsp;
mon_to_stone
&nbsp;/* changes the monster into a stone monster of the same type
&nbsp;   this should only be called when poly_when_stoned() is true */
&nbsp;void
&nbsp;mon_to_stone(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (mtmp-&gt;data-&gt;mlet == S_GOLEM) {
&nbsp;        /* it's a golem, and not a stone golem */
&nbsp;        if (canseemon(mtmp))
&nbsp;            pline("%s solidifies...", Monnam(mtmp));
&nbsp;        if (newcham(mtmp, &amp;mons[PM_STONE_GOLEM], FALSE, FALSE)) {
&nbsp;            if (canseemon(mtmp))
&nbsp;                pline("Now it's %s.", an(mtmp-&gt;data-&gt;mname));
&nbsp;        } else {
&nbsp;            if (canseemon(mtmp))
&nbsp;                pline("... and returns to normal.");
&nbsp;        }
&nbsp;    } else
&nbsp;        impossible("Can't polystone %s!", a_monnam(mtmp));
&nbsp;}
&nbsp;
vamp_stone
&nbsp;boolean
&nbsp;vamp_stone(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (is_vampshifter(mtmp)) {
&nbsp;        int mndx = mtmp-&gt;cham;
&nbsp;        int x = mtmp-&gt;mx, y = mtmp-&gt;my;
&nbsp;
&nbsp;        /* this only happens if shapeshifted */
&nbsp;        if (mndx &gt;= LOW_PM &amp;&amp; mndx != monsndx(mtmp-&gt;data)
&nbsp;            &amp;&amp; !(mvitals[mndx].mvflags &amp; G_GENOD)) {
&nbsp;            char buf[BUFSZ];
&nbsp;            boolean in_door = (amorphous(mtmp-&gt;data)
&nbsp;                               &amp;&amp; closed_door(mtmp-&gt;mx, mtmp-&gt;my));
&nbsp;
&nbsp;            /* construct a format string before transformation */
&nbsp;            Sprintf(buf, "The lapidifying %s %s %s",
&nbsp;                    x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
&nbsp;                             (SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
&nbsp;                              | SUPPRESS_INVISIBLE | SUPPRESS_IT), FALSE),
&nbsp;                    amorphous(mtmp-&gt;data) ? "coalesces on the"
&nbsp;                       : is_flyer(mtmp-&gt;data) ? "drops to the"
&nbsp;                          : "writhes on the",
&nbsp;                    surface(x,y));
&nbsp;            mtmp-&gt;mcanmove = 1;
&nbsp;            mtmp-&gt;mfrozen = 0;
&nbsp;            if (mtmp-&gt;mhpmax &lt;= 0)
&nbsp;                mtmp-&gt;mhpmax = 10;
&nbsp;            mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;            /* this can happen if previously a fog cloud */
&nbsp;            if (u.uswallow &amp;&amp; (mtmp == u.ustuck))
&nbsp;                expels(mtmp, mtmp-&gt;data, FALSE);
&nbsp;            if (in_door) {
&nbsp;                coord new_xy;
&nbsp;
&nbsp;                if (enexto(&amp;new_xy, mtmp-&gt;mx, mtmp-&gt;my, &amp;mons[mndx])) {
&nbsp;                    rloc_to(mtmp, new_xy.x, new_xy.y);
&nbsp;                }
&nbsp;            }
&nbsp;            if (canspotmon(mtmp)) {
&nbsp;                pline("%s!", buf);
&nbsp;                display_nhwindow(WIN_MESSAGE, FALSE);
&nbsp;            }
&nbsp;            newcham(mtmp, &amp;mons[mndx], FALSE, FALSE);
&nbsp;            if (mtmp-&gt;data == &amp;mons[mndx])
&nbsp;                mtmp-&gt;cham = NON_PM;
&nbsp;            else
&nbsp;                mtmp-&gt;cham = mndx;
&nbsp;            if (canspotmon(mtmp)) {
&nbsp;                pline("%s rises from the %s with renewed agility!",
&nbsp;                      Amonnam(mtmp), surface(mtmp-&gt;mx, mtmp-&gt;my));
&nbsp;            }
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;            return FALSE;   /* didn't petrify */
&nbsp;        }
&nbsp;    }
&nbsp;    return TRUE;
&nbsp;}
&nbsp;
m_into_limbo
&nbsp;/* drop monster into "limbo" - that is, migrate to the current level */
&nbsp;void
&nbsp;m_into_limbo(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    unstuck(mtmp);
&nbsp;    mdrop_special_objs(mtmp);
&nbsp;    migrate_to_level(mtmp, ledger_no(&amp;u.uz), MIGR_APPROX_XY, NULL);
&nbsp;}
&nbsp;
mnexto
&nbsp;/* make monster mtmp next to you (if possible);
&nbsp;   might place monst on far side of a wall or boulder */
&nbsp;void
&nbsp;mnexto(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    coord mm;
&nbsp;    boolean couldspot = canspotmon(mtmp);
&nbsp;
&nbsp;    if (mtmp == u.usteed) {
&nbsp;        /* Keep your steed in sync with you instead */
&nbsp;        mtmp-&gt;mx = u.ux;
&nbsp;        mtmp-&gt;my = u.uy;
&nbsp;        return;
&nbsp;    }
&nbsp;
&nbsp;    if (!enexto(&amp;mm, u.ux, u.uy, mtmp-&gt;data)) {
&nbsp;        m_into_limbo(mtmp);
&nbsp;        return;
&nbsp;    }
&nbsp;    if (!isok(mm.x, mm.y))
&nbsp;        return;
&nbsp;    rloc_to(mtmp, mm.x, mm.y);
&nbsp;    if (!in_mklev &amp;&amp; (mtmp-&gt;mstrategy &amp; STRAT_APPEARMSG)) {
&nbsp;        mtmp-&gt;mstrategy &amp;= ~STRAT_APPEARMSG; /* one chance only */
&nbsp;        if (!couldspot &amp;&amp; canspotmon(mtmp))
&nbsp;            pline("%s suddenly %s!", Amonnam(mtmp),
&nbsp;                  !Blind ? "appears" : "arrives");
&nbsp;    }
&nbsp;    return;
&nbsp;}
&nbsp;
maybe_mnexto
&nbsp;/* like mnexto() but requires destination to be directly accessible */
&nbsp;void
&nbsp;maybe_mnexto(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    coord mm;
&nbsp;    struct permonst *ptr = mtmp-&gt;data;
&nbsp;    boolean diagok = !NODIAG(ptr - mons);
&nbsp;    int tryct = 20;
&nbsp;
&nbsp;    do {
&nbsp;        if (!enexto(&amp;mm, u.ux, u.uy, ptr))
&nbsp;            return;
&nbsp;        if (couldsee(mm.x, mm.y)
&nbsp;            /* don't move grid bugs diagonally */
&nbsp;            &amp;&amp; (diagok || mm.x == mtmp-&gt;mx || mm.y == mtmp-&gt;my)) {
&nbsp;            rloc_to(mtmp, mm.x, mm.y);
&nbsp;            return;
&nbsp;        }
&nbsp;    } while (--tryct &gt; 0);
&nbsp;}
&nbsp;
mnearto
&nbsp;/* mnearto()
&nbsp; * Put monster near (or at) location if possible.
&nbsp; * Returns:
&nbsp; *  true if relocation was successful
&nbsp; *  false otherwise
&nbsp; */
&nbsp;boolean
&nbsp;mnearto(mtmp, x, y, move_other)
&nbsp;register struct monst *mtmp;
&nbsp;xchar x, y;
&nbsp;boolean move_other; /* make sure mtmp gets to x, y! so move m_at(x, y) */
&nbsp;{
&nbsp;    struct monst *othermon = (struct monst *) 0;
&nbsp;    xchar newx, newy;
&nbsp;    coord mm;
&nbsp;
&nbsp;    if (mtmp-&gt;mx == x &amp;&amp; mtmp-&gt;my == y)
&nbsp;        return TRUE;
&nbsp;
&nbsp;    if (move_other &amp;&amp; (othermon = m_at(x, y)) != 0) {
&nbsp;        if (othermon-&gt;wormno)
&nbsp;            remove_worm(othermon);
&nbsp;        else
&nbsp;            remove_monster(x, y);
&nbsp;    }
&nbsp;
&nbsp;    newx = x;
&nbsp;    newy = y;
&nbsp;    if (!goodpos(newx, newy, mtmp, 0)) {
&nbsp;        /* Actually we have real problems if enexto ever fails.
&nbsp;         * Migrating_mons that need to be placed will cause
&nbsp;         * no end of trouble.
&nbsp;         */
&nbsp;        if (!enexto(&amp;mm, newx, newy, mtmp-&gt;data))
&nbsp;            return FALSE;
&nbsp;        if (!isok(mm.x,mm.y))
&nbsp;            return FALSE;
&nbsp;        newx = mm.x;
&nbsp;        newy = mm.y;
&nbsp;    }
&nbsp;    rloc_to(mtmp, newx, newy);
&nbsp;
&nbsp;    if (move_other &amp;&amp; othermon) {
&nbsp;        xchar oldx = othermon-&gt;mx, oldy = othermon-&gt;my;
&nbsp;
&nbsp;        othermon-&gt;mx = othermon-&gt;my = 0;
&nbsp;        (void) mnearto(othermon, x, y, FALSE);
&nbsp;        if (othermon-&gt;mx == 0 &amp;&amp; othermon-&gt;my == 0) {
&nbsp;            /* reloc failed */
&nbsp;            othermon-&gt;mx = oldx;
&nbsp;            othermon-&gt;my = oldy;
&nbsp;            m_into_limbo(othermon);
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    return TRUE;
&nbsp;}
&nbsp;
m_respond
&nbsp;/* monster responds to player action; not the same as a passive attack;
&nbsp;   assumes reason for response has been tested, and response _must_ be made */
&nbsp;void
&nbsp;m_respond(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (mtmp-&gt;data-&gt;msound == MS_SHRIEK) {
&nbsp;        if (!Deaf) {
&nbsp;            pline("%s shrieks.", Monnam(mtmp));
&nbsp;            stop_occupation();
&nbsp;        }
&nbsp;        if (!rn2(10)) {
&nbsp;            if (!rn2(13))
&nbsp;                (void) makemon(&amp;mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
&nbsp;            else
&nbsp;                (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
&nbsp;        }
&nbsp;        aggravate();
&nbsp;    }
&nbsp;    if (mtmp-&gt;data == &amp;mons[PM_MEDUSA]) {
&nbsp;        register int i;
&nbsp;
&nbsp;        for (i = 0; i &lt; NATTK; i++)
&nbsp;            if (mtmp-&gt;data-&gt;mattk[i].aatyp == AT_GAZE) {
&nbsp;                (void) gazemu(mtmp, &amp;mtmp-&gt;data-&gt;mattk[i]);
&nbsp;                break;
&nbsp;            }
&nbsp;    }
&nbsp;}
&nbsp;
setmangry
&nbsp;/* Called whenever the player attacks mtmp; also called in other situations
&nbsp;   where mtmp gets annoyed at the player. Handles mtmp getting annoyed at the
&nbsp;   attack and any ramifications that might have. Useful also in situations
&nbsp;   where mtmp was already hostile; it checks for situations where the player
&nbsp;   shouldn't be attacking and any ramifications /that/ might have. */
&nbsp;void
&nbsp;setmangry(mtmp, via_attack)
&nbsp;struct monst *mtmp;
&nbsp;boolean via_attack;
&nbsp;{
&nbsp;    if (via_attack &amp;&amp; sengr_at("Elbereth", u.ux, u.uy, TRUE)) {
&nbsp;        You_feel("like a hypocrite.");
&nbsp;        /* AIS: Yes, I know alignment penalties and bonuses aren't balanced
&nbsp;           at the moment. This is about correct relative to other "small"
&nbsp;           penalties; it should be fairly large, as attacking while standing
&nbsp;           on an Elbereth means that you're requesting peace and then
&nbsp;           violating your own request. I know 5 isn't actually large, but
&nbsp;           it's intentionally larger than the 1s and 2s that are normally
&nbsp;           given for this sort of thing. */
&nbsp;        adjalign(-5);
&nbsp;
&nbsp;        if (!Blind)
&nbsp;            pline("The engraving beneath you fades.");
&nbsp;        del_engr_at(u.ux, u.uy);
&nbsp;    }
&nbsp;
&nbsp;    /* AIS: Should this be in both places, or just in wakeup()? */
&nbsp;    mtmp-&gt;mstrategy &amp;= ~STRAT_WAITMASK;
&nbsp;    if (!mtmp-&gt;mpeaceful)
&nbsp;        return;
&nbsp;    if (mtmp-&gt;mtame)
&nbsp;        return;
&nbsp;    mtmp-&gt;mpeaceful = 0;
&nbsp;    if (mtmp-&gt;ispriest) {
&nbsp;        if (p_coaligned(mtmp))
&nbsp;            adjalign(-5); /* very bad */
&nbsp;        else
&nbsp;            adjalign(2);
&nbsp;    } else
&nbsp;        adjalign(-1); /* attacking peaceful monsters is bad */
&nbsp;    if (couldsee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;        if (humanoid(mtmp-&gt;data) || mtmp-&gt;isshk || mtmp-&gt;isgd)
&nbsp;            pline("%s gets angry!", Monnam(mtmp));
&nbsp;        else if (flags.verbose &amp;&amp; !Deaf)
&nbsp;            growl(mtmp);
&nbsp;    }
&nbsp;
&nbsp;    /* attacking your own quest leader will anger his or her guardians */
&nbsp;    if (!context.mon_moving /* should always be the case here */
&nbsp;        &amp;&amp; mtmp-&gt;data == &amp;mons[quest_info(MS_LEADER)]) {
&nbsp;        struct monst *mon;
&nbsp;        struct permonst *q_guardian = &amp;mons[quest_info(MS_GUARDIAN)];
&nbsp;        int got_mad = 0;
&nbsp;
&nbsp;        /* guardians will sense this attack even if they can't see it */
&nbsp;        for (mon = fmon; mon; mon = mon-&gt;nmon) {
&nbsp;            if (DEADMONSTER(mon))
&nbsp;                continue;
&nbsp;            if (mon-&gt;data == q_guardian &amp;&amp; mon-&gt;mpeaceful) {
&nbsp;                mon-&gt;mpeaceful = 0;
&nbsp;                if (canseemon(mon))
&nbsp;                    ++got_mad;
&nbsp;            }
&nbsp;        }
&nbsp;        if (got_mad &amp;&amp; !Hallucination) {
&nbsp;            const char *who = q_guardian-&gt;mname;
&nbsp;
&nbsp;            if (got_mad &gt; 1)
&nbsp;                who = makeplural(who);
&nbsp;            pline_The("%s %s to be angry too...",
&nbsp;                      who, vtense(who, "appear"));
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* make other peaceful monsters react */
&nbsp;    if (!context.mon_moving) {
&nbsp;        static const char *const Exclam[] = {
&nbsp;            "Gasp!", "Uh-oh.", "Oh my!", "What?", "Why?",
&nbsp;        };
&nbsp;        struct monst *mon;
&nbsp;        int mndx = monsndx(mtmp-&gt;data);
&nbsp;
&nbsp;        for (mon = fmon; mon; mon = mon-&gt;nmon) {
&nbsp;            if (DEADMONSTER(mon))
&nbsp;                continue;
&nbsp;            if (mon == mtmp) /* the mpeaceful test catches this since mtmp */
&nbsp;                continue;    /* is no longer peaceful, but be explicit...  */
&nbsp;
&nbsp;            if (!mindless(mon-&gt;data) &amp;&amp; mon-&gt;mpeaceful
&nbsp;                &amp;&amp; couldsee(mon-&gt;mx, mon-&gt;my) &amp;&amp; !mon-&gt;msleeping
&nbsp;                &amp;&amp; mon-&gt;mcansee &amp;&amp; m_canseeu(mon)) {
&nbsp;                boolean exclaimed = FALSE;
&nbsp;
&nbsp;                if (humanoid(mon-&gt;data) || mon-&gt;isshk || mon-&gt;ispriest) {
&nbsp;                    if (is_watch(mon-&gt;data)) {
&nbsp;                        verbalize("Halt!  You're under arrest!");
&nbsp;                        (void) angry_guards(!!Deaf);
&nbsp;                    } else {
&nbsp;                        if (!rn2(5)) {
&nbsp;                            verbalize("%s", Exclam[mon-&gt;m_id % SIZE(Exclam)]);
&nbsp;                            exclaimed = TRUE;
&nbsp;                        }
&nbsp;                        /* shopkeepers and temple priests might gasp in
&nbsp;                           surprise, but they won't become angry here */
&nbsp;                        if (mon-&gt;isshk || mon-&gt;ispriest)
&nbsp;                            continue;
&nbsp;
&nbsp;                        if (mon-&gt;data-&gt;mlevel &lt; rn2(10)) {
&nbsp;                            monflee(mon, rn2(50) + 25, TRUE, !exclaimed);
&nbsp;                            exclaimed = TRUE;
&nbsp;                        }
&nbsp;                        if (mon-&gt;mtame) {
&nbsp;                            /* mustn't set mpeaceful to 0 as below;
&nbsp;                               perhaps reduce tameness? */
&nbsp;                        } else {
&nbsp;                            mon-&gt;mpeaceful = 0;
&nbsp;                            adjalign(-1);
&nbsp;                            if (!exclaimed)
&nbsp;                                pline("%s gets angry!", Monnam(mon));
&nbsp;                        }
&nbsp;                    }
&nbsp;                } else if (mon-&gt;data-&gt;mlet == mtmp-&gt;data-&gt;mlet
&nbsp;                           &amp;&amp; big_little_match(mndx, monsndx(mon-&gt;data))
&nbsp;                           &amp;&amp; !rn2(3)) {
&nbsp;                    if (!rn2(4)) {
&nbsp;                        growl(mon);
&nbsp;                        exclaimed = TRUE;
&nbsp;                    }
&nbsp;                    if (rn2(6))
&nbsp;                        monflee(mon, rn2(25) + 15, TRUE, !exclaimed);
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
wakeup
&nbsp;/* wake up a monster, possibly making it angry in the process */
&nbsp;void
&nbsp;wakeup(mtmp, via_attack)
&nbsp;register struct monst *mtmp;
&nbsp;boolean via_attack;
&nbsp;{
&nbsp;    mtmp-&gt;msleeping = 0;
&nbsp;    if (mtmp-&gt;m_ap_type) {
&nbsp;        seemimic(mtmp);
&nbsp;    } else if (context.forcefight &amp;&amp; !context.mon_moving
&nbsp;               &amp;&amp; mtmp-&gt;mundetected) {
&nbsp;        mtmp-&gt;mundetected = 0;
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;    finish_meating(mtmp);
&nbsp;    if (via_attack)
&nbsp;        setmangry(mtmp, TRUE);
&nbsp;}
&nbsp;
wake_nearby
&nbsp;/* Wake up nearby monsters without angering them. */
&nbsp;void
&nbsp;wake_nearby()
&nbsp;{
&nbsp;    register struct monst *mtmp;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        if (distu(mtmp-&gt;mx, mtmp-&gt;my) &lt; u.ulevel * 20) {
&nbsp;            mtmp-&gt;msleeping = 0;
&nbsp;            if (!unique_corpstat(mtmp-&gt;data))
&nbsp;                mtmp-&gt;mstrategy &amp;= ~STRAT_WAITMASK;
&nbsp;            if (mtmp-&gt;mtame) {
&nbsp;                if (!mtmp-&gt;isminion)
&nbsp;                    EDOG(mtmp)-&gt;whistletime = moves;
&nbsp;                /* Clear mtrack. This is to fix up a pet who is
&nbsp;                   stuck "fleeing" its master. */
&nbsp;                memset(mtmp-&gt;mtrack, 0, sizeof(mtmp-&gt;mtrack));
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
wake_nearto
&nbsp;/* Wake up monsters near some particular location. */
&nbsp;void
&nbsp;wake_nearto(x, y, distance)
&nbsp;register int x, y, distance;
&nbsp;{
&nbsp;    register struct monst *mtmp;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        if (distance == 0 || dist2(mtmp-&gt;mx, mtmp-&gt;my, x, y) &lt; distance) {
&nbsp;            mtmp-&gt;msleeping = 0;
&nbsp;            if (!unique_corpstat(mtmp-&gt;data))
&nbsp;                mtmp-&gt;mstrategy &amp;= ~STRAT_WAITMASK;
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
seemimic
&nbsp;/* NOTE: we must check for mimicry before calling this routine */
&nbsp;void
&nbsp;seemimic(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    boolean is_blocker_appear = (is_lightblocker_mappear(mtmp));
&nbsp;
&nbsp;    if (has_mcorpsenm(mtmp))
&nbsp;        freemcorpsenm(mtmp);
&nbsp;
&nbsp;    mtmp-&gt;m_ap_type = M_AP_NOTHING;
&nbsp;    mtmp-&gt;mappearance = 0;
&nbsp;
&nbsp;    /*
&nbsp;     *  Discovered mimics don't block light.
&nbsp;     */
&nbsp;    if (is_blocker_appear
&nbsp;        &amp;&amp; !does_block(mtmp-&gt;mx, mtmp-&gt;my, &amp;levl[mtmp-&gt;mx][mtmp-&gt;my]))
&nbsp;        unblock_point(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;
&nbsp;    newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;}
&nbsp;
rescham
&nbsp;/* force all chameleons to become normal */
&nbsp;void
&nbsp;rescham()
&nbsp;{
&nbsp;    register struct monst *mtmp;
&nbsp;    int mcham;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        mcham = (int) mtmp-&gt;cham;
&nbsp;        if (mcham &gt;= LOW_PM) {
&nbsp;            (void) newcham(mtmp, &amp;mons[mcham], FALSE, FALSE);
&nbsp;            mtmp-&gt;cham = NON_PM;
&nbsp;        }
&nbsp;        if (is_were(mtmp-&gt;data) &amp;&amp; mtmp-&gt;data-&gt;mlet != S_HUMAN)
&nbsp;            new_were(mtmp);
&nbsp;        if (mtmp-&gt;m_ap_type &amp;&amp; cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;            seemimic(mtmp);
&nbsp;            /* we pretend that the mimic doesn't
&nbsp;               know that it has been unmasked */
&nbsp;            mtmp-&gt;msleeping = 1;
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
restartcham
&nbsp;/* Let the chameleons change again -dgk */
&nbsp;void
&nbsp;restartcham()
&nbsp;{
&nbsp;    register struct monst *mtmp;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        mtmp-&gt;cham = pm_to_cham(monsndx(mtmp-&gt;data));
&nbsp;        if (mtmp-&gt;data-&gt;mlet == S_MIMIC &amp;&amp; mtmp-&gt;msleeping
&nbsp;            &amp;&amp; cansee(mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;            set_mimic_sym(mtmp);
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
restore_cham
&nbsp;/* called when restoring a monster from a saved level; protection
&nbsp;   against shape-changing might be different now than it was at the
&nbsp;   time the level was saved. */
&nbsp;void
&nbsp;restore_cham(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    int mcham;
&nbsp;
&nbsp;    if (Protection_from_shape_changers) {
&nbsp;        mcham = (int) mon-&gt;cham;
&nbsp;        if (mcham &gt;= LOW_PM) {
&nbsp;            mon-&gt;cham = NON_PM;
&nbsp;            (void) newcham(mon, &amp;mons[mcham], FALSE, FALSE);
&nbsp;        } else if (is_were(mon-&gt;data) &amp;&amp; !is_human(mon-&gt;data)) {
&nbsp;            new_were(mon);
&nbsp;        }
&nbsp;    } else if (mon-&gt;cham == NON_PM) {
&nbsp;        mon-&gt;cham = pm_to_cham(monsndx(mon-&gt;data));
&nbsp;    }
&nbsp;}
&nbsp;
restrap
&nbsp;/* unwatched hiders may hide again; if so, returns True */
&nbsp;STATIC_OVL boolean
&nbsp;restrap(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    struct trap *t;
&nbsp;
&nbsp;    if (mtmp-&gt;mcan || mtmp-&gt;m_ap_type || cansee(mtmp-&gt;mx, mtmp-&gt;my)
&nbsp;        || rn2(3) || mtmp == u.ustuck
&nbsp;        /* can't hide while trapped except in pits */
&nbsp;        || (mtmp-&gt;mtrapped &amp;&amp; (t = t_at(mtmp-&gt;mx, mtmp-&gt;my)) != 0
&nbsp;            &amp;&amp; !(t-&gt;ttyp == PIT || t-&gt;ttyp == SPIKED_PIT))
&nbsp;        || (sensemon(mtmp) &amp;&amp; distu(mtmp-&gt;mx, mtmp-&gt;my) &lt;= 2))
&nbsp;        return FALSE;
&nbsp;
&nbsp;    if (mtmp-&gt;data-&gt;mlet == S_MIMIC) {
&nbsp;        set_mimic_sym(mtmp);
&nbsp;        return TRUE;
&nbsp;    } else if (levl[mtmp-&gt;mx][mtmp-&gt;my].typ == ROOM) {
&nbsp;        mtmp-&gt;mundetected = 1;
&nbsp;        return TRUE;
&nbsp;    }
&nbsp;
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
hideunder
&nbsp;/* monster/hero tries to hide under something at the current location */
&nbsp;boolean
&nbsp;hideunder(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    struct trap *t;
&nbsp;    boolean undetected = FALSE, is_u = (mtmp == &amp;youmonst);
&nbsp;    xchar x = is_u ? u.ux : mtmp-&gt;mx, y = is_u ? u.uy : mtmp-&gt;my;
&nbsp;
&nbsp;    if (mtmp == u.ustuck) {
&nbsp;        ; /* can't hide if holding you or held by you */
&nbsp;    } else if (is_u ? (u.utrap &amp;&amp; u.utraptype != TT_PIT)
&nbsp;                    : (mtmp-&gt;mtrapped &amp;&amp; (t = t_at(x, y)) != 0
&nbsp;                       &amp;&amp; !(t-&gt;ttyp == PIT || t-&gt;ttyp == SPIKED_PIT))) {
&nbsp;        ; /* can't hide while stuck in a non-pit trap */
&nbsp;    } else if (mtmp-&gt;data-&gt;mlet == S_EEL) {
&nbsp;        undetected = (is_pool(x, y) &amp;&amp; !Is_waterlevel(&amp;u.uz));
&nbsp;    } else if (hides_under(mtmp-&gt;data) &amp;&amp; OBJ_AT(x, y)) {
&nbsp;        struct obj *otmp = level.objects[x][y];
&nbsp;
&nbsp;        /* most monsters won't hide under cockatrice corpse */
&nbsp;        if (otmp-&gt;nexthere || otmp-&gt;otyp != CORPSE
&nbsp;            || (mtmp == &amp;youmonst ? Stone_resistance : resists_ston(mtmp))
&nbsp;            || !touch_petrifies(&amp;mons[otmp-&gt;corpsenm]))
&nbsp;            undetected = TRUE;
&nbsp;    }
&nbsp;
&nbsp;    if (is_u)
&nbsp;        u.uundetected = undetected;
&nbsp;    else
&nbsp;        mtmp-&gt;mundetected = undetected;
&nbsp;    return undetected;
&nbsp;}
&nbsp;
hide_monst
&nbsp;/* called when returning to a previously visited level */
&nbsp;void
&nbsp;hide_monst(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    boolean hider_under = hides_under(mon-&gt;data) || mon-&gt;data-&gt;mlet == S_EEL;
&nbsp;
&nbsp;    if ((is_hider(mon-&gt;data) || hider_under)
&nbsp;        &amp;&amp; !(mon-&gt;mundetected || mon-&gt;m_ap_type)) {
&nbsp;        xchar x = mon-&gt;mx, y = mon-&gt;my;
&nbsp;        char save_viz = viz_array[y][x];
&nbsp;
&nbsp;        /* override vision, forcing hero to be unable to see monster's spot */
&nbsp;        viz_array[y][x] &amp;= ~(IN_SIGHT | COULD_SEE);
&nbsp;        if (is_hider(mon-&gt;data))
&nbsp;            (void) restrap(mon);
&nbsp;        /* try again if mimic missed its 1/3 chance to hide */
&nbsp;        if (mon-&gt;data-&gt;mlet == S_MIMIC &amp;&amp; !mon-&gt;m_ap_type)
&nbsp;            (void) restrap(mon);
&nbsp;        if (hider_under)
&nbsp;            (void) hideunder(mon);
&nbsp;        viz_array[y][x] = save_viz;
&nbsp;    }
&nbsp;}
&nbsp;
&nbsp;static short *animal_list = 0; /* list of PM values for animal monsters */
&nbsp;static int animal_list_count;
&nbsp;
mon_animal_list
&nbsp;void
&nbsp;mon_animal_list(construct)
&nbsp;boolean construct;
&nbsp;{
&nbsp;    if (construct) {
&nbsp;        short animal_temp[SPECIAL_PM];
&nbsp;        int i, n;
&nbsp;
&nbsp;        /* if (animal_list) impossible("animal_list already exists"); */
&nbsp;
&nbsp;        for (n = 0, i = LOW_PM; i &lt; SPECIAL_PM; i++)
&nbsp;            if (is_animal(&amp;mons[i]))
&nbsp;                animal_temp[n++] = i;
&nbsp;        /* if (n == 0) animal_temp[n++] = NON_PM; */
&nbsp;
&nbsp;        animal_list = (short *) alloc(n * sizeof *animal_list);
&nbsp;        (void) memcpy((genericptr_t) animal_list, (genericptr_t) animal_temp,
&nbsp;                      n * sizeof *animal_list);
&nbsp;        animal_list_count = n;
&nbsp;    } else { /* release */
&nbsp;        if (animal_list)
&nbsp;            free((genericptr_t) animal_list), animal_list = 0;
&nbsp;        animal_list_count = 0;
&nbsp;    }
&nbsp;}
&nbsp;
pick_animal
&nbsp;STATIC_OVL int
&nbsp;pick_animal()
&nbsp;{
&nbsp;    int res;
&nbsp;
&nbsp;    if (!animal_list)
&nbsp;        mon_animal_list(TRUE);
&nbsp;
&nbsp;    res = animal_list[rn2(animal_list_count)];
&nbsp;    /* rogue level should use monsters represented by uppercase letters
&nbsp;       only, but since chameleons aren't generated there (not uppercase!)
&nbsp;       we don't perform a lot of retries */
&nbsp;    if (Is_rogue_level(&amp;u.uz) &amp;&amp; !isupper((uchar) mons[res].mlet))
&nbsp;        res = animal_list[rn2(animal_list_count)];
&nbsp;    return res;
&nbsp;}
&nbsp;
decide_to_shapeshift
&nbsp;void
&nbsp;decide_to_shapeshift(mon, shiftflags)
&nbsp;struct monst *mon;
&nbsp;int shiftflags;
&nbsp;{
&nbsp;    struct permonst *ptr = 0;
&nbsp;    int mndx;
&nbsp;    unsigned was_female = mon-&gt;female;
&nbsp;    boolean msg = FALSE, dochng = FALSE;
&nbsp;
&nbsp;    if ((shiftflags &amp; SHIFT_MSG)
&nbsp;        || ((shiftflags &amp; SHIFT_SEENMSG) &amp;&amp; sensemon(mon)))
&nbsp;        msg = TRUE;
&nbsp;
&nbsp;    if (!is_vampshifter(mon)) {
&nbsp;        /* regular shapeshifter */
&nbsp;        if (!rn2(6))
&nbsp;            dochng = TRUE;
&nbsp;    } else {
&nbsp;        /* The vampire has to be in good health (mhp) to maintain
&nbsp;         * its shifted form.
&nbsp;         *
&nbsp;         * If we're shifted and getting low on hp, maybe shift back, or
&nbsp;         * if we're a fog cloud at full hp, maybe pick a different shape.
&nbsp;         * If we're not already shifted and in good health, maybe shift.
&nbsp;         */
&nbsp;        if (mon-&gt;data-&gt;mlet != S_VAMPIRE) {
&nbsp;            if ((mon-&gt;mhp &lt;= (mon-&gt;mhpmax + 5) / 6) &amp;&amp; rn2(4)
&nbsp;                &amp;&amp; mon-&gt;cham &gt;= LOW_PM) {
&nbsp;                ptr = &amp;mons[mon-&gt;cham];
&nbsp;                dochng = TRUE;
&nbsp;            } else if (mon-&gt;data == &amp;mons[PM_FOG_CLOUD]
&nbsp;                     &amp;&amp; mon-&gt;mhp == mon-&gt;mhpmax &amp;&amp; !rn2(4)
&nbsp;                     &amp;&amp; (!canseemon(mon)
&nbsp;                         || distu(mon-&gt;mx, mon-&gt;my) &gt; BOLT_LIM * BOLT_LIM)) {
&nbsp;                /* if a fog cloud, maybe change to wolf or vampire bat;
&nbsp;                   those are more likely to take damage--at least when
&nbsp;                   tame--and then switch back to vampire; they'll also
&nbsp;                   switch to fog cloud if they encounter a closed door */
&nbsp;                mndx = pickvampshape(mon);
&nbsp;                if (mndx &gt;= LOW_PM) {
&nbsp;                    ptr = &amp;mons[mndx];
&nbsp;                    dochng = (ptr != mon-&gt;data);
&nbsp;                }
&nbsp;            }
&nbsp;        } else {
&nbsp;            if (mon-&gt;mhp &gt;= 9 * mon-&gt;mhpmax / 10 &amp;&amp; !rn2(6)
&nbsp;                &amp;&amp; (!canseemon(mon)
&nbsp;                    || distu(mon-&gt;mx, mon-&gt;my) &gt; BOLT_LIM * BOLT_LIM))
&nbsp;                dochng = TRUE; /* 'ptr' stays Null */
&nbsp;        }
&nbsp;    }
&nbsp;    if (dochng) {
&nbsp;        if (newcham(mon, ptr, FALSE, msg) &amp;&amp; is_vampshifter(mon)) {
&nbsp;            /* for vampshift, override the 10% chance for sex change */
&nbsp;            ptr = mon-&gt;data;
&nbsp;            if (!is_male(ptr) &amp;&amp; !is_female(ptr) &amp;&amp; !is_neuter(ptr))
&nbsp;                mon-&gt;female = was_female;
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
pickvampshape
&nbsp;STATIC_OVL int
&nbsp;pickvampshape(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    int mndx = mon-&gt;cham, wolfchance = 10;
&nbsp;    /* avoid picking monsters with lowercase display symbols ('d' for wolf
&nbsp;       and 'v' for fog cloud) on rogue level*/
&nbsp;    boolean uppercase_only = Is_rogue_level(&amp;u.uz);
&nbsp;
&nbsp;    switch (mndx) {
&nbsp;    case PM_VLAD_THE_IMPALER:
&nbsp;        /* ensure Vlad can keep carrying the Candelabrum */
&nbsp;        if (mon_has_special(mon))
&nbsp;            break; /* leave mndx as is */
&nbsp;        wolfchance = 3;
&nbsp;    /*FALLTHRU*/
&nbsp;    case PM_VAMPIRE_LORD: /* vampire lord or Vlad can become wolf */
&nbsp;        if (!rn2(wolfchance) &amp;&amp; !uppercase_only) {
&nbsp;            mndx = PM_WOLF;
&nbsp;            break;
&nbsp;        }
&nbsp;    /*FALLTHRU*/
&nbsp;    case PM_VAMPIRE: /* any vampire can become fog or bat */
&nbsp;        mndx = (!rn2(4) &amp;&amp; !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
&nbsp;        break;
&nbsp;    }
&nbsp;    return mndx;
&nbsp;}
&nbsp;
isspecmon
&nbsp;/* nonshapechangers who warrant special polymorph handling */
&nbsp;STATIC_OVL boolean
&nbsp;isspecmon(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    return (mon-&gt;isshk || mon-&gt;ispriest || mon-&gt;isgd
&nbsp;            || mon-&gt;m_id == quest_status.leader_m_id);
&nbsp;}
&nbsp;
validspecmon
&nbsp;/* restrict certain special monsters (shopkeepers, aligned priests,
&nbsp;   vault guards) to forms that allow them to behave sensibly (catching
&nbsp;   gold, speaking?) so that they don't need too much extra code */
&nbsp;STATIC_OVL boolean
&nbsp;validspecmon(mon, mndx)
&nbsp;struct monst *mon;
&nbsp;int mndx;
&nbsp;{
&nbsp;    if (mndx == NON_PM)
&nbsp;        return TRUE; /* caller wants random */
&nbsp;
&nbsp;    if (!accept_newcham_form(mndx))
&nbsp;        return FALSE; /* geno'd or !polyok */
&nbsp;
&nbsp;    if (isspecmon(mon)) {
&nbsp;        struct permonst *ptr = &amp;mons[mndx];
&nbsp;
&nbsp;        /* reject notake because object manipulation is expected
&nbsp;           and nohead because speech capability is expected */
&nbsp;        if (notake(ptr) || !has_head(ptr))
&nbsp;            return FALSE;
&nbsp;        /* [should we check ptr-&gt;msound here too?] */
&nbsp;    }
&nbsp;    return TRUE; /* potential new form is ok */
&nbsp;}
&nbsp;
validvamp
&nbsp;/* prevent wizard mode user from specifying invalid vampshifter shape */
&nbsp;boolean
&nbsp;validvamp(mon, mndx_p, monclass)
&nbsp;struct monst *mon;
&nbsp;int *mndx_p, monclass;
&nbsp;{
&nbsp;    /* simplify caller's usage */
&nbsp;    if (!is_vampshifter(mon))
&nbsp;        return validspecmon(mon, *mndx_p);
&nbsp;
&nbsp;    if (*mndx_p == PM_VAMPIRE || *mndx_p == PM_VAMPIRE_LORD
&nbsp;        || *mndx_p == PM_VLAD_THE_IMPALER) {
&nbsp;        /* player picked some type of vampire; use mon's self */
&nbsp;        *mndx_p = mon-&gt;cham;
&nbsp;        return TRUE;
&nbsp;    }
&nbsp;    if (mon-&gt;cham == PM_VLAD_THE_IMPALER &amp;&amp; mon_has_special(mon)) {
&nbsp;        /* Vlad with Candelabrum; override choice, then accept it */
&nbsp;        *mndx_p = PM_VLAD_THE_IMPALER;
&nbsp;        return TRUE;
&nbsp;    }
&nbsp;    /* basic vampires can't become wolves; any can become fog or bat
&nbsp;       (we don't enforce upper-case only for rogue level here) */
&nbsp;    if (*mndx_p == PM_WOLF)
&nbsp;        return (boolean) (mon-&gt;cham != PM_VAMPIRE);
&nbsp;    if (*mndx_p == PM_FOG_CLOUD || *mndx_p == PM_VAMPIRE_BAT)
&nbsp;        return TRUE;
&nbsp;
&nbsp;    /* if we get here, specific type was no good; try by class */
&nbsp;    switch (monclass) {
&nbsp;    case S_VAMPIRE:
&nbsp;        *mndx_p = mon-&gt;cham;
&nbsp;        break;
&nbsp;    case S_BAT:
&nbsp;        *mndx_p = PM_VAMPIRE_BAT;
&nbsp;        break;
&nbsp;    case S_VORTEX:
&nbsp;        *mndx_p = PM_FOG_CLOUD;
&nbsp;        break;
&nbsp;    case S_DOG:
&nbsp;        if (mon-&gt;cham != PM_VAMPIRE) {
&nbsp;            *mndx_p = PM_WOLF;
&nbsp;            break;
&nbsp;        }
&nbsp;    /*FALLTHRU*/
&nbsp;    default:
&nbsp;        *mndx_p = NON_PM;
&nbsp;        break;
&nbsp;    }
&nbsp;    return (boolean) (*mndx_p != NON_PM);
&nbsp;}
&nbsp;
select_newcham_form
&nbsp;int
&nbsp;select_newcham_form(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    int mndx = NON_PM, tryct;
&nbsp;
&nbsp;    switch (mon-&gt;cham) {
&nbsp;    case PM_SANDESTIN:
&nbsp;        if (rn2(7))
&nbsp;            mndx = pick_nasty();
&nbsp;        break;
&nbsp;    case PM_DOPPELGANGER:
&nbsp;        if (!rn2(7)) {
&nbsp;            mndx = pick_nasty();
&nbsp;        } else if (rn2(3)) { /* role monsters */
&nbsp;            mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
&nbsp;        } else if (!rn2(3)) { /* quest guardians */
&nbsp;            mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
&nbsp;            /* avoid own role's guardian */
&nbsp;            if (mndx == urole.guardnum)
&nbsp;                mndx = NON_PM;
&nbsp;        } else { /* general humanoids */
&nbsp;            tryct = 5;
&nbsp;            do {
&nbsp;                mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
&nbsp;                if (humanoid(&amp;mons[mndx]) &amp;&amp; polyok(&amp;mons[mndx]))
&nbsp;                    break;
&nbsp;            } while (--tryct &gt; 0);
&nbsp;            if (!tryct)
&nbsp;                mndx = NON_PM;
&nbsp;        }
&nbsp;        break;
&nbsp;    case PM_CHAMELEON:
&nbsp;        if (!rn2(3))
&nbsp;            mndx = pick_animal();
&nbsp;        break;
&nbsp;    case PM_VLAD_THE_IMPALER:
&nbsp;    case PM_VAMPIRE_LORD:
&nbsp;    case PM_VAMPIRE:
&nbsp;        mndx = pickvampshape(mon);
&nbsp;        break;
&nbsp;    case NON_PM: /* ordinary */
&nbsp;      {
&nbsp;        struct obj *m_armr = which_armor(mon, W_ARM);
&nbsp;
&nbsp;        if (m_armr &amp;&amp; Is_dragon_scales(m_armr))
&nbsp;            mndx = (int) (Dragon_scales_to_pm(m_armr) - mons);
&nbsp;        else if (m_armr &amp;&amp; Is_dragon_mail(m_armr))
&nbsp;            mndx = (int) (Dragon_mail_to_pm(m_armr) - mons);
&nbsp;      }
&nbsp;        break;
&nbsp;    }
&nbsp;
&nbsp;    /* for debugging: allow control of polymorphed monster */
&nbsp;    if (wizard &amp;&amp; iflags.mon_polycontrol) {
&nbsp;        char pprompt[BUFSZ], buf[BUFSZ] = DUMMY;
&nbsp;        int monclass;
&nbsp;
&nbsp;        Sprintf(pprompt, "Change %s @ %s into what kind of monster?",
&nbsp;                noit_mon_nam(mon),
&nbsp;                coord_desc((int) mon-&gt;mx, (int) mon-&gt;my, buf,
&nbsp;                           (iflags.getpos_coords != GPCOORDS_NONE)
&nbsp;                              ? iflags.getpos_coords : GPCOORDS_MAP));
&nbsp;        tryct = 5;
&nbsp;        do {
&nbsp;            monclass = 0;
&nbsp;            getlin(pprompt, buf);
&nbsp;            mungspaces(buf);
&nbsp;            /* for ESC, take form selected above (might be NON_PM) */
&nbsp;            if (*buf == '\033')
&nbsp;                break;
&nbsp;            /* for "*", use NON_PM to pick an arbitrary shape below */
&nbsp;            if (!strcmp(buf, "*") || !strcmp(buf, "random")) {
&nbsp;                mndx = NON_PM;
&nbsp;                break;
&nbsp;            }
&nbsp;            mndx = name_to_mon(buf);
&nbsp;            if (mndx == NON_PM) {
&nbsp;                /* didn't get a type, so check whether it's a class
&nbsp;                   (single letter or text match with def_monsyms[]) */
&nbsp;                monclass = name_to_monclass(buf, &amp;mndx);
&nbsp;                if (monclass &amp;&amp; mndx == NON_PM)
&nbsp;                    mndx = mkclass_poly(monclass);
&nbsp;            }
&nbsp;            if (mndx &gt;= LOW_PM) {
&nbsp;                /* got a specific type of monster; use it if we can */
&nbsp;                if (validvamp(mon, &amp;mndx, monclass))
&nbsp;                    break;
&nbsp;                /* can't; revert to random in case we exhaust tryct */
&nbsp;                mndx = NON_PM;
&nbsp;            }
&nbsp;
&nbsp;            pline("It can't become that.");
&nbsp;        } while (--tryct &gt; 0);
&nbsp;        if (!tryct)
&nbsp;            pline1(thats_enough_tries);
&nbsp;        if (is_vampshifter(mon) &amp;&amp; !validvamp(mon, &amp;mndx, monclass))
&nbsp;            mndx = pickvampshape(mon); /* don't resort to arbitrary */
&nbsp;    }
&nbsp;
&nbsp;    /* if no form was specified above, pick one at random now */
&nbsp;    if (mndx == NON_PM) {
&nbsp;        tryct = 50;
&nbsp;        do {
&nbsp;            mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
&nbsp;        } while (--tryct &gt; 0 &amp;&amp; !validspecmon(mon, mndx)
&nbsp;                 /* try harder to select uppercase monster on rogue level */
&nbsp;                 &amp;&amp; (tryct &gt; 40 &amp;&amp; Is_rogue_level(&amp;u.uz)
&nbsp;                     &amp;&amp; !isupper((uchar) mons[mndx].mlet)));
&nbsp;    }
&nbsp;    return mndx;
&nbsp;}
&nbsp;
accept_newcham_form
&nbsp;/* this used to be inline within newcham() but monpolycontrol needs it too */
&nbsp;STATIC_OVL struct permonst *
&nbsp;accept_newcham_form(mndx)
&nbsp;int mndx;
&nbsp;{
&nbsp;    struct permonst *mdat;
&nbsp;
&nbsp;    if (mndx == NON_PM)
&nbsp;        return 0;
&nbsp;    mdat = &amp;mons[mndx];
&nbsp;    if ((mvitals[mndx].mvflags &amp; G_GENOD) != 0)
&nbsp;        return 0;
&nbsp;    if (is_placeholder(mdat))
&nbsp;        return 0;
&nbsp;    /* select_newcham_form() might deliberately pick a player
&nbsp;       character type (random selection never does) which
&nbsp;       polyok() rejects, so we need a special case here */
&nbsp;    if (is_mplayer(mdat))
&nbsp;        return mdat;
&nbsp;    /* polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops */
&nbsp;    return polyok(mdat) ? mdat : 0;
&nbsp;}
&nbsp;
mgender_from_permonst
&nbsp;void
&nbsp;mgender_from_permonst(mtmp, mdat)
&nbsp;struct monst *mtmp;
&nbsp;struct permonst *mdat;
&nbsp;{
&nbsp;    if (is_male(mdat)) {
&nbsp;        if (mtmp-&gt;female)
&nbsp;            mtmp-&gt;female = FALSE;
&nbsp;    } else if (is_female(mdat)) {
&nbsp;        if (!mtmp-&gt;female)
&nbsp;            mtmp-&gt;female = TRUE;
&nbsp;    } else if (!is_neuter(mdat)) {
&nbsp;        if (!rn2(10))
&nbsp;            mtmp-&gt;female = !mtmp-&gt;female;
&nbsp;    }
&nbsp;}
&nbsp;
newcham
&nbsp;/* make a chameleon take on another shape, or a polymorph target
&nbsp;   (possibly self-inflicted) become a different monster;
&nbsp;   returns 1 if it actually changes form */
&nbsp;int
&nbsp;newcham(mtmp, mdat, polyspot, msg)
&nbsp;struct monst *mtmp;
&nbsp;struct permonst *mdat;
&nbsp;boolean polyspot; /* change is the result of wand or spell of polymorph */
&nbsp;boolean msg;      /* "The oldmon turns into a newmon!" */
&nbsp;{
&nbsp;    int hpn, hpd;
&nbsp;    int mndx, tryct;
&nbsp;    struct permonst *olddata = mtmp-&gt;data;
&nbsp;    char *p, oldname[BUFSZ], l_oldname[BUFSZ], newname[BUFSZ];
&nbsp;
&nbsp;    /* Riders are immune to polymorph and green slime
&nbsp;       (but apparent Rider might actually be a doppelganger) */
&nbsp;    if (mtmp-&gt;cham == NON_PM) { /* not a shapechanger */
&nbsp;        if (is_rider(olddata))
&nbsp;            return 0;
&nbsp;        /* make Nazgul and erinyes immune too, to reduce chance of
&nbsp;           anomalous extinction feedback during final disclsoure */
&nbsp;        if (mbirth_limit(monsndx(olddata)) &lt; MAXMONNO)
&nbsp;            return 0;
&nbsp;    }
&nbsp;
&nbsp;    if (msg) {
&nbsp;        /* like Monnam() but never mention saddle */
&nbsp;        Strcpy(oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
&nbsp;                                 SUPPRESS_SADDLE, FALSE));
&nbsp;        oldname[0] = highc(oldname[0]);
&nbsp;    }
&nbsp;    /* we need this one whether msg is true or not */
&nbsp;    Strcpy(l_oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
&nbsp;                               has_mname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE));
&nbsp;
&nbsp;    /* mdat = 0 -&gt; caller wants a random monster shape */
&nbsp;    if (mdat == 0) {
&nbsp;        /* select_newcham_form() loops when resorting to random but
&nbsp;           it doesn't always pick that so we still retry here too */
&nbsp;        tryct = 20;
&nbsp;        do {
&nbsp;            mndx = select_newcham_form(mtmp);
&nbsp;            mdat = accept_newcham_form(mndx);
&nbsp;            /* for the first several tries we require upper-case on
&nbsp;               the rogue level (after that, we take whatever we get) */
&nbsp;            if (tryct &gt; 15 &amp;&amp; Is_rogue_level(&amp;u.uz)
&nbsp;                &amp;&amp; mdat &amp;&amp; !isupper((uchar) mdat-&gt;mlet))
&nbsp;                mdat = 0;
&nbsp;            if (mdat)
&nbsp;                break;
&nbsp;        } while (--tryct &gt; 0);
&nbsp;        if (!tryct)
&nbsp;            return 0;
&nbsp;    } else if (mvitals[monsndx(mdat)].mvflags &amp; G_GENOD)
&nbsp;        return 0; /* passed in mdat is genocided */
&nbsp;
&nbsp;    if (mdat == olddata)
&nbsp;        return 0; /* still the same monster */
&nbsp;
&nbsp;    mgender_from_permonst(mtmp, mdat);
&nbsp;    /* Endgame mplayers start out as "Foo the Bar", but some of the
&nbsp;     * titles are inappropriate when polymorphed, particularly into
&nbsp;     * the opposite sex.  Player characters don't use ranks when
&nbsp;     * polymorphed, so dropping rank for mplayers seems reasonable.
&nbsp;     */
&nbsp;    if (In_endgame(&amp;u.uz) &amp;&amp; is_mplayer(olddata)
&nbsp;        &amp;&amp; has_mname(mtmp) &amp;&amp; (p = strstr(MNAME(mtmp), " the ")) != 0)
&nbsp;        *p = '\0';
&nbsp;
&nbsp;    if (mtmp-&gt;wormno) { /* throw tail away */
&nbsp;        wormgone(mtmp);
&nbsp;        place_monster(mtmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;    if (mtmp-&gt;m_ap_type &amp;&amp; mdat-&gt;mlet != S_MIMIC)
&nbsp;        seemimic(mtmp); /* revert to normal monster */
&nbsp;
&nbsp;    /* (this code used to try to adjust the monster's health based on
&nbsp;       a normal one of its type but there are too many special cases
&nbsp;       which need to handled in order to do that correctly, so just
&nbsp;       give the new form the same proportion of HP as its old one had) */
&nbsp;    hpn = mtmp-&gt;mhp;
&nbsp;    hpd = mtmp-&gt;mhpmax;
&nbsp;    /* set level and hit points */
&nbsp;    newmonhp(mtmp, monsndx(mdat));
&nbsp;    /* new hp: same fraction of max as before */
&nbsp;#ifndef LINT
&nbsp;    mtmp-&gt;mhp = (int) (((long) hpn * (long) mtmp-&gt;mhp) / (long) hpd);
&nbsp;#endif
&nbsp;    /* sanity check (potential overflow) */
&nbsp;    if (mtmp-&gt;mhp &lt; 0 || mtmp-&gt;mhp &gt; mtmp-&gt;mhpmax)
&nbsp;        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;    /* unlikely but not impossible; a 1HD creature with 1HP that changes
&nbsp;       into a 0HD creature will require this statement */
&nbsp;    if (!mtmp-&gt;mhp)
&nbsp;        mtmp-&gt;mhp = 1;
&nbsp;
&nbsp;    /* take on the new form... */
&nbsp;    set_mon_data(mtmp, mdat, 0);
&nbsp;
&nbsp;    if (emits_light(olddata) != emits_light(mtmp-&gt;data)) {
&nbsp;        /* used to give light, now doesn't, or vice versa,
&nbsp;           or light's range has changed */
&nbsp;        if (emits_light(olddata))
&nbsp;            del_light_source(LS_MONSTER, monst_to_any(mtmp));
&nbsp;        if (emits_light(mtmp-&gt;data))
&nbsp;            new_light_source(mtmp-&gt;mx, mtmp-&gt;my, emits_light(mtmp-&gt;data),
&nbsp;                             LS_MONSTER, monst_to_any(mtmp));
&nbsp;    }
&nbsp;    if (!mtmp-&gt;perminvis || pm_invisible(olddata))
&nbsp;        mtmp-&gt;perminvis = pm_invisible(mdat);
&nbsp;    mtmp-&gt;minvis = mtmp-&gt;invis_blkd ? 0 : mtmp-&gt;perminvis;
&nbsp;    if (mtmp-&gt;mundetected)
&nbsp;        (void) hideunder(mtmp);
&nbsp;    if (u.ustuck == mtmp) {
&nbsp;        if (u.uswallow) {
&nbsp;            if (!attacktype(mdat, AT_ENGL)) {
&nbsp;                /* Does mdat care? */
&nbsp;                if (!noncorporeal(mdat) &amp;&amp; !amorphous(mdat)
&nbsp;                    &amp;&amp; !is_whirly(mdat) &amp;&amp; (mdat != &amp;mons[PM_YELLOW_LIGHT])) {
&nbsp;                    char msgtrail[BUFSZ];
&nbsp;
&nbsp;                    if (is_vampshifter(mtmp)) {
&nbsp;                        Sprintf(msgtrail, " which was a shapeshifted %s",
&nbsp;                                noname_monnam(mtmp, ARTICLE_NONE));
&nbsp;                    } else if (is_animal(mdat)) {
&nbsp;                        Strcpy(msgtrail, "'s stomach");
&nbsp;                    } else {
&nbsp;                        msgtrail[0] = '\0';
&nbsp;                    }
&nbsp;
&nbsp;                    /* Do this even if msg is FALSE */
&nbsp;                    You("%s %s%s!",
&nbsp;                        (amorphous(olddata) || is_whirly(olddata))
&nbsp;                            ? "emerge from" : "break out of",
&nbsp;                        l_oldname, msgtrail);
&nbsp;                    msg = FALSE; /* message has been given */
&nbsp;                    mtmp-&gt;mhp = 1; /* almost dead */
&nbsp;                }
&nbsp;                expels(mtmp, olddata, FALSE);
&nbsp;            } else {
&nbsp;                /* update swallow glyphs for new monster */
&nbsp;                swallowed(0);
&nbsp;            }
&nbsp;        } else if (!sticks(mdat) &amp;&amp; !sticks(youmonst.data))
&nbsp;            unstuck(mtmp);
&nbsp;    }
&nbsp;
&nbsp;#ifndef DCC30_BUG
&nbsp;    if (mdat == &amp;mons[PM_LONG_WORM] &amp;&amp; (mtmp-&gt;wormno = get_wormno()) != 0) {
&nbsp;#else
&nbsp;    /* DICE 3.0 doesn't like assigning and comparing mtmp-&gt;wormno in the
&nbsp;     * same expression.
&nbsp;     */
&nbsp;    if (mdat == &amp;mons[PM_LONG_WORM]
&nbsp;        &amp;&amp; (mtmp-&gt;wormno = get_wormno(), mtmp-&gt;wormno != 0)) {
&nbsp;#endif
&nbsp;        /* we can now create worms with tails - 11/91 */
&nbsp;        initworm(mtmp, rn2(5));
&nbsp;        if (count_wsegs(mtmp))
&nbsp;            place_worm_tail_randomly(mtmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;
&nbsp;    newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;
&nbsp;    if (msg) {
&nbsp;        Strcpy(newname, noname_monnam(mtmp, ARTICLE_A));
&nbsp;        /* oldname was capitalized above; newname will be lower case */
&nbsp;        if (!strcmpi(newname, "it")) { /* can't see or sense it now */
&nbsp;            if (!!strcmpi(oldname, "it")) /* could see or sense it before */
&nbsp;                pline("%s disappears!", oldname);
&nbsp;            (void) usmellmon(mdat);
&nbsp;        } else { /* can see or sense it now */
&nbsp;            if (!strcmpi(oldname, "it")) /* couldn't see or sense it before */
&nbsp;                pline("%s appears!", upstart(newname));
&nbsp;            else
&nbsp;                pline("%s turns into %s!", oldname, newname);
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* when polymorph trap/wand/potion produces a vampire, turn in into
&nbsp;       a full-fledged vampshifter unless shape-changing is blocked */
&nbsp;    if (mtmp-&gt;cham == NON_PM &amp;&amp; mdat-&gt;mlet == S_VAMPIRE
&nbsp;        &amp;&amp; !Protection_from_shape_changers)
&nbsp;        mtmp-&gt;cham = pm_to_cham(monsndx(mdat));
&nbsp;
&nbsp;    possibly_unwield(mtmp, polyspot); /* might lose use of weapon */
&nbsp;    mon_break_armor(mtmp, polyspot);
&nbsp;    if (!(mtmp-&gt;misc_worn_check &amp; W_ARMG))
&nbsp;        mselftouch(mtmp, "No longer petrify-resistant, ",
&nbsp;                   !context.mon_moving);
&nbsp;    m_dowear(mtmp, FALSE);
&nbsp;
&nbsp;    /* This ought to re-test can_carry() on each item in the inventory
&nbsp;     * rather than just checking ex-giants &amp; boulders, but that'd be
&nbsp;     * pretty expensive to perform.  If implemented, then perhaps
&nbsp;     * minvent should be sorted in order to drop heaviest items first.
&nbsp;     */
&nbsp;    /* former giants can't continue carrying boulders */
&nbsp;    if (mtmp-&gt;minvent &amp;&amp; !throws_rocks(mdat)) {
&nbsp;        register struct obj *otmp, *otmp2;
&nbsp;
&nbsp;        for (otmp = mtmp-&gt;minvent; otmp; otmp = otmp2) {
&nbsp;            otmp2 = otmp-&gt;nobj;
&nbsp;            if (otmp-&gt;otyp == BOULDER) {
&nbsp;                /* this keeps otmp from being polymorphed in the
&nbsp;                   same zap that the monster that held it is polymorphed */
&nbsp;                if (polyspot)
&nbsp;                    bypass_obj(otmp);
&nbsp;                obj_extract_self(otmp);
&nbsp;                /* probably ought to give some "drop" message here */
&nbsp;                if (flooreffects(otmp, mtmp-&gt;mx, mtmp-&gt;my, ""))
&nbsp;                    continue;
&nbsp;                place_object(otmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    return 1;
&nbsp;}
&nbsp;
&nbsp;/* sometimes an egg will be special */
&nbsp;#define BREEDER_EGG (!rn2(77))
&nbsp;
can_be_hatched
&nbsp;/*
&nbsp; * Determine if the given monster number can be hatched from an egg.
&nbsp; * Return the monster number to use as the egg's corpsenm.  Return
&nbsp; * NON_PM if the given monster can't be hatched.
&nbsp; */
&nbsp;int
&nbsp;can_be_hatched(mnum)
&nbsp;int mnum;
&nbsp;{
&nbsp;    /* ranger quest nemesis has the oviparous bit set, making it
&nbsp;       be possible to wish for eggs of that unique monster; turn
&nbsp;       such into ordinary eggs rather than forbidding them outright */
&nbsp;    if (mnum == PM_SCORPIUS)
&nbsp;        mnum = PM_SCORPION;
&nbsp;
&nbsp;    mnum = little_to_big(mnum);
&nbsp;    /*
&nbsp;     * Queen bees lay killer bee eggs (usually), but killer bees don't
&nbsp;     * grow into queen bees.  Ditto for [winged-]gargoyles.
&nbsp;     */
&nbsp;    if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE
&nbsp;        || (lays_eggs(&amp;mons[mnum])
&nbsp;            &amp;&amp; (BREEDER_EGG
&nbsp;                || (mnum != PM_QUEEN_BEE &amp;&amp; mnum != PM_WINGED_GARGOYLE))))
&nbsp;        return mnum;
&nbsp;    return NON_PM;
&nbsp;}
&nbsp;
egg_type_from_parent
&nbsp;/* type of egg laid by #sit; usually matches parent */
&nbsp;int
&nbsp;egg_type_from_parent(mnum, force_ordinary)
&nbsp;int mnum; /* parent monster; caller must handle lays_eggs() check */
&nbsp;boolean force_ordinary;
&nbsp;{
&nbsp;    if (force_ordinary || !BREEDER_EGG) {
&nbsp;        if (mnum == PM_QUEEN_BEE)
&nbsp;            mnum = PM_KILLER_BEE;
&nbsp;        else if (mnum == PM_WINGED_GARGOYLE)
&nbsp;            mnum = PM_GARGOYLE;
&nbsp;    }
&nbsp;    return mnum;
&nbsp;}
&nbsp;
dead_species
&nbsp;/* decide whether an egg of the indicated monster type is viable;
&nbsp;   also used to determine whether an egg or tin can be created... */
&nbsp;boolean
&nbsp;dead_species(m_idx, egg)
&nbsp;int m_idx;
&nbsp;boolean egg;
&nbsp;{
&nbsp;    int alt_idx;
&nbsp;
&nbsp;    /* generic eggs are unhatchable and have corpsenm of NON_PM */
&nbsp;    if (m_idx &lt; LOW_PM)
&nbsp;        return TRUE;
&nbsp;    /*
&nbsp;     * For monsters with both baby and adult forms, genociding either
&nbsp;     * form kills all eggs of that monster.  Monsters with more than
&nbsp;     * two forms (small-&gt;large-&gt;giant mimics) are more or less ignored;
&nbsp;     * fortunately, none of them have eggs.  Species extinction due to
&nbsp;     * overpopulation does not kill eggs.
&nbsp;     */
&nbsp;    alt_idx = egg ? big_to_little(m_idx) : m_idx;
&nbsp;    return (boolean) ((mvitals[m_idx].mvflags &amp; G_GENOD) != 0
&nbsp;                      || (mvitals[alt_idx].mvflags &amp; G_GENOD) != 0);
&nbsp;}
&nbsp;
kill_eggs
&nbsp;/* kill off any eggs of genocided monsters */
&nbsp;STATIC_OVL void
&nbsp;kill_eggs(obj_list)
&nbsp;struct obj *obj_list;
&nbsp;{
&nbsp;    struct obj *otmp;
&nbsp;
&nbsp;    for (otmp = obj_list; otmp; otmp = otmp-&gt;nobj)
&nbsp;        if (otmp-&gt;otyp == EGG) {
&nbsp;            if (dead_species(otmp-&gt;corpsenm, TRUE)) {
&nbsp;                /*
&nbsp;                 * It seems we could also just catch this when
&nbsp;                 * it attempted to hatch, so we wouldn't have to
&nbsp;                 * search all of the objlists.. or stop all
&nbsp;                 * hatch timers based on a corpsenm.
&nbsp;                 */
&nbsp;                kill_egg(otmp);
&nbsp;            }
&nbsp;#if 0 /* not used */
&nbsp;        } else if (otmp-&gt;otyp == TIN) {
&nbsp;            if (dead_species(otmp-&gt;corpsenm, FALSE))
&nbsp;                otmp-&gt;corpsenm = NON_PM; /* empty tin */
&nbsp;        } else if (otmp-&gt;otyp == CORPSE) {
&nbsp;            if (dead_species(otmp-&gt;corpsenm, FALSE))
&nbsp;                ; /* not yet implemented... */
&nbsp;#endif
&nbsp;        } else if (Has_contents(otmp)) {
&nbsp;            kill_eggs(otmp-&gt;cobj);
&nbsp;        }
&nbsp;}
&nbsp;
kill_genocided_monsters
&nbsp;/* kill all members of genocided species */
&nbsp;void
&nbsp;kill_genocided_monsters()
&nbsp;{
&nbsp;    struct monst *mtmp, *mtmp2;
&nbsp;    boolean kill_cham;
&nbsp;    int mndx;
&nbsp;
&nbsp;    /*
&nbsp;     * Called during genocide, and again upon level change.  The latter
&nbsp;     * catches up with any migrating monsters as they finally arrive at
&nbsp;     * their intended destinations, so possessions get deposited there.
&nbsp;     *
&nbsp;     * Chameleon handling:
&nbsp;     *  1) if chameleons have been genocided, destroy them
&nbsp;     *     regardless of current form;
&nbsp;     *  2) otherwise, force every chameleon which is imitating
&nbsp;     *     any genocided species to take on a new form.
&nbsp;     */
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp2) {
&nbsp;        mtmp2 = mtmp-&gt;nmon;
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        mndx = monsndx(mtmp-&gt;data);
&nbsp;        kill_cham = (mtmp-&gt;cham &gt;= LOW_PM
&nbsp;                     &amp;&amp; (mvitals[mtmp-&gt;cham].mvflags &amp; G_GENOD));
&nbsp;        if ((mvitals[mndx].mvflags &amp; G_GENOD) || kill_cham) {
&nbsp;            if (mtmp-&gt;cham &gt;= LOW_PM &amp;&amp; !kill_cham)
&nbsp;                (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
&nbsp;            else
&nbsp;                mondead(mtmp);
&nbsp;        }
&nbsp;        if (mtmp-&gt;minvent)
&nbsp;            kill_eggs(mtmp-&gt;minvent);
&nbsp;    }
&nbsp;
&nbsp;    kill_eggs(invent);
&nbsp;    kill_eggs(fobj);
&nbsp;    kill_eggs(migrating_objs);
&nbsp;    kill_eggs(level.buriedobjlist);
&nbsp;}
&nbsp;
golemeffects
&nbsp;void
&nbsp;golemeffects(mon, damtype, dam)
&nbsp;register struct monst *mon;
&nbsp;int damtype, dam;
&nbsp;{
&nbsp;    int heal = 0, slow = 0;
&nbsp;
&nbsp;    if (mon-&gt;data == &amp;mons[PM_FLESH_GOLEM]) {
&nbsp;        if (damtype == AD_ELEC)
&nbsp;            heal = (dam + 5) / 6;
&nbsp;        else if (damtype == AD_FIRE || damtype == AD_COLD)
&nbsp;            slow = 1;
&nbsp;    } else if (mon-&gt;data == &amp;mons[PM_IRON_GOLEM]) {
&nbsp;        if (damtype == AD_ELEC)
&nbsp;            slow = 1;
&nbsp;        else if (damtype == AD_FIRE)
&nbsp;            heal = dam;
&nbsp;    } else {
&nbsp;        return;
&nbsp;    }
&nbsp;    if (slow) {
&nbsp;        if (mon-&gt;mspeed != MSLOW)
&nbsp;            mon_adjust_speed(mon, -1, (struct obj *) 0);
&nbsp;    }
&nbsp;    if (heal) {
&nbsp;        if (mon-&gt;mhp &lt; mon-&gt;mhpmax) {
&nbsp;            mon-&gt;mhp += heal;
&nbsp;            if (mon-&gt;mhp &gt; mon-&gt;mhpmax)
&nbsp;                mon-&gt;mhp = mon-&gt;mhpmax;
&nbsp;            if (cansee(mon-&gt;mx, mon-&gt;my))
&nbsp;                pline("%s seems healthier.", Monnam(mon));
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
angry_guards
&nbsp;boolean
&nbsp;angry_guards(silent)
&nbsp;boolean silent;
&nbsp;{
&nbsp;    struct monst *mtmp;
&nbsp;    int ct = 0, nct = 0, sct = 0, slct = 0;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        if (is_watch(mtmp-&gt;data) &amp;&amp; mtmp-&gt;mpeaceful) {
&nbsp;            ct++;
&nbsp;            if (cansee(mtmp-&gt;mx, mtmp-&gt;my) &amp;&amp; mtmp-&gt;mcanmove) {
&nbsp;                if (distu(mtmp-&gt;mx, mtmp-&gt;my) == 2)
&nbsp;                    nct++;
&nbsp;                else
&nbsp;                    sct++;
&nbsp;            }
&nbsp;            if (mtmp-&gt;msleeping || mtmp-&gt;mfrozen) {
&nbsp;                slct++;
&nbsp;                mtmp-&gt;msleeping = mtmp-&gt;mfrozen = 0;
&nbsp;            }
&nbsp;            mtmp-&gt;mpeaceful = 0;
&nbsp;        }
&nbsp;    }
&nbsp;    if (ct) {
&nbsp;        if (!silent) { /* do we want pline msgs? */
&nbsp;            if (slct)
&nbsp;                pline_The("guard%s wake%s up!", slct &gt; 1 ? "s" : "",
&nbsp;                          slct == 1 ? "s" : "");
&nbsp;            if (nct || sct) {
&nbsp;                if (nct)
&nbsp;                    pline_The("guard%s get%s angry!", nct == 1 ? "" : "s",
&nbsp;                              nct == 1 ? "s" : "");
&nbsp;                else if (!Blind)
&nbsp;                    You_see("%sangry guard%s approaching!",
&nbsp;                            sct == 1 ? "an " : "", sct &gt; 1 ? "s" : "");
&nbsp;            } else
&nbsp;                You_hear("the shrill sound of a guard's whistle.");
&nbsp;        }
&nbsp;        return TRUE;
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
pacify_guards
&nbsp;void
&nbsp;pacify_guards()
&nbsp;{
&nbsp;    struct monst *mtmp;
&nbsp;
&nbsp;    for (mtmp = fmon; mtmp; mtmp = mtmp-&gt;nmon) {
&nbsp;        if (DEADMONSTER(mtmp))
&nbsp;            continue;
&nbsp;        if (is_watch(mtmp-&gt;data))
&nbsp;            mtmp-&gt;mpeaceful = 1;
&nbsp;    }
&nbsp;}
&nbsp;
mimic_hit_msg
&nbsp;void
&nbsp;mimic_hit_msg(mtmp, otyp)
&nbsp;struct monst *mtmp;
&nbsp;short otyp;
&nbsp;{
&nbsp;    short ap = mtmp-&gt;mappearance;
&nbsp;
&nbsp;    switch (mtmp-&gt;m_ap_type) {
&nbsp;    case M_AP_NOTHING:
&nbsp;    case M_AP_FURNITURE:
&nbsp;    case M_AP_MONSTER:
&nbsp;        break;
&nbsp;    case M_AP_OBJECT:
&nbsp;        if (otyp == SPE_HEALING || otyp == SPE_EXTRA_HEALING) {
&nbsp;            pline("%s seems a more vivid %s than before.",
&nbsp;                  The(simple_typename(ap)),
&nbsp;                  c_obj_colors[objects[ap].oc_color]);
&nbsp;        }
&nbsp;        break;
&nbsp;    }
&nbsp;}
&nbsp;
usmellmon
&nbsp;boolean
&nbsp;usmellmon(mdat)
&nbsp;struct permonst *mdat;
&nbsp;{
&nbsp;    int mndx;
&nbsp;    boolean nonspecific = FALSE;
&nbsp;    boolean msg_given = FALSE;
&nbsp;
&nbsp;    if (mdat) {
&nbsp;        if (!olfaction(youmonst.data))
&nbsp;            return FALSE;
&nbsp;        mndx = monsndx(mdat);
&nbsp;        switch (mndx) {
&nbsp;        case PM_ROTHE:
&nbsp;        case PM_MINOTAUR:
&nbsp;            You("notice a bovine smell.");
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        case PM_CAVEMAN:
&nbsp;        case PM_CAVEWOMAN:
&nbsp;        case PM_BARBARIAN:
&nbsp;        case PM_NEANDERTHAL:
&nbsp;            You("smell body odor.");
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        /*
&nbsp;        case PM_PESTILENCE:
&nbsp;        case PM_FAMINE:
&nbsp;        case PM_DEATH:
&nbsp;            break;
&nbsp;        */
&nbsp;        case PM_HORNED_DEVIL:
&nbsp;        case PM_BALROG:
&nbsp;        case PM_ASMODEUS:
&nbsp;        case PM_DISPATER:
&nbsp;        case PM_YEENOGHU:
&nbsp;        case PM_ORCUS:
&nbsp;            break;
&nbsp;        case PM_HUMAN_WEREJACKAL:
&nbsp;        case PM_HUMAN_WERERAT:
&nbsp;        case PM_HUMAN_WEREWOLF:
&nbsp;        case PM_WEREJACKAL:
&nbsp;        case PM_WERERAT:
&nbsp;        case PM_WEREWOLF:
&nbsp;        case PM_OWLBEAR:
&nbsp;            You("detect an odor reminiscent of an animal's den.");
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        /*
&nbsp;        case PM_PURPLE_WORM:
&nbsp;            break;
&nbsp;        */
&nbsp;        case PM_STEAM_VORTEX:
&nbsp;            You("smell steam.");
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        case PM_GREEN_SLIME:
&nbsp;            pline("%s stinks.", Something);
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        case PM_VIOLET_FUNGUS:
&nbsp;        case PM_SHRIEKER:
&nbsp;            You("smell mushrooms.");
&nbsp;            msg_given = TRUE;
&nbsp;            break;
&nbsp;        /* These are here to avoid triggering the
&nbsp;           nonspecific treatment through the default case below*/
&nbsp;        case PM_WHITE_UNICORN:
&nbsp;        case PM_GRAY_UNICORN:
&nbsp;        case PM_BLACK_UNICORN:
&nbsp;        case PM_JELLYFISH:
&nbsp;            break;
&nbsp;        default:
&nbsp;            nonspecific = TRUE;
&nbsp;            break;
&nbsp;        }
&nbsp;
&nbsp;        if (nonspecific)
&nbsp;            switch (mdat-&gt;mlet) {
&nbsp;            case S_DOG:
&nbsp;                You("notice a dog smell.");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_DRAGON:
&nbsp;                You("smell a dragon!");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_FUNGUS:
&nbsp;                pline("%s smells moldy.", Something);
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_UNICORN:
&nbsp;                You("detect a%s odor reminiscent of a stable.",
&nbsp;                    (mndx == PM_PONY) ? "n" : " strong");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_ZOMBIE:
&nbsp;                You("smell rotting flesh.");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_EEL:
&nbsp;                You("smell fish.");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            case S_ORC:
&nbsp;                if (maybe_polyd(is_orc(youmonst.data), Race_if(PM_ORC)))
&nbsp;                    You("notice an attractive smell.");
&nbsp;                else
&nbsp;                    pline("A foul stench makes you feel a little nauseated.");
&nbsp;                msg_given = TRUE;
&nbsp;                break;
&nbsp;            default:
&nbsp;                break;
&nbsp;            }
&nbsp;    }
&nbsp;    return msg_given ? TRUE : FALSE;
&nbsp;}
&nbsp;
&nbsp;/*mon.c*/

</mx,my></ctype.h>