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

Contents

1 Top of file
2 droppables
3 cursed_object_at
4 dog_nutrition
5 dog_eat
6 dog_hunger
7 dog_invent
8 dog_goal
9 find_targ
10 find_friends
11 score_targ
12 best_target
13 dog_move
14 could_reach_item
15 can_reach_location
16 wantdoor
17 finish_meating
18 quickmimic

Top of file
&nbsp;/* NetHack 3.6	dogmove.c	$NHDT-Date: 1502753407 2017/08/14 23:30:07 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.63 $ */
&nbsp;/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
&nbsp;/*-Copyright (c) Robert Patrick Rankin, 2012. */
&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;#include "hack.h"
&nbsp;
&nbsp;#include "mfndpos.h"
&nbsp;
&nbsp;extern boolean notonhead;
&nbsp;
&nbsp;STATIC_DCL boolean FDECL(dog_hunger, (struct monst *, struct edog *));
&nbsp;STATIC_DCL int FDECL(dog_invent, (struct monst *, struct edog *, int));
&nbsp;STATIC_DCL int FDECL(dog_goal, (struct monst *, struct edog *, int, int, int));
&nbsp;STATIC_DCL struct monst *FDECL(find_targ, (struct monst *, int, int, int));
&nbsp;STATIC_OVL int FDECL(find_friends, (struct monst *, struct monst *, int));
&nbsp;STATIC_DCL struct monst *FDECL(best_target, (struct monst *));
&nbsp;STATIC_DCL long FDECL(score_targ, (struct monst *, struct monst *));
&nbsp;STATIC_DCL boolean FDECL(can_reach_location, (struct monst *, XCHAR_P,
&nbsp;                                              XCHAR_P, XCHAR_P, XCHAR_P));
&nbsp;STATIC_DCL boolean FDECL(could_reach_item, (struct monst *, XCHAR_P, XCHAR_P));
&nbsp;STATIC_DCL void FDECL(quickmimic, (struct monst *));
&nbsp;
droppables
&nbsp;/* pick a carried item for pet to drop */
&nbsp;struct obj *
&nbsp;droppables(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    struct obj *obj, *wep, dummy, *pickaxe, *unihorn, *key;
&nbsp;
&nbsp;    dummy = zeroobj;
&nbsp;    dummy.otyp = GOLD_PIECE; /* not STRANGE_OBJECT or tools of interest */
&nbsp;    dummy.oartifact = 1; /* so real artifact won't override "don't keep it" */
&nbsp;    pickaxe = unihorn = key = (struct obj *) 0;
&nbsp;    wep = MON_WEP(mon);
&nbsp;
&nbsp;    if (is_animal(mon-&gt;data) || mindless(mon-&gt;data)) {
&nbsp;        /* won't hang on to any objects of these types */
&nbsp;        pickaxe = unihorn = key = &amp;dummy /* act as if already have them */
&nbsp;    } else {
&nbsp;        /* don't hang on to pick-axe if can't use one or don't need one */
&nbsp;        if (!tunnels(mon-&gt;data) || !needspick(mon-&gt;data))
&nbsp;            pickaxe = &amp;dummy
&nbsp;        /* don't hang on to key if can't open doors */
&nbsp;        if (nohands(mon-&gt;data) || verysmall(mon-&gt;data))
&nbsp;            key = &amp;dummy
&nbsp;    }
&nbsp;    if (wep) {
&nbsp;        if (is_pick(wep))
&nbsp;            pickaxe = wep;
&nbsp;        if (wep-&gt;otyp == UNICORN_HORN)
&nbsp;            unihorn = wep;
&nbsp;        /* don't need any wielded check for keys... */
&nbsp;    }
&nbsp;
&nbsp;    for (obj = mon-&gt;minvent; obj; obj = obj-&gt;nobj) {
&nbsp;        switch (obj-&gt;otyp) {
&nbsp;        case DWARVISH_MATTOCK:
&nbsp;            /* reject mattock if couldn't wield it */
&nbsp;            if (which_armor(mon, W_ARMS))
&nbsp;                break;
&nbsp;            /* keep mattock in preference to pick unless pick is already
&nbsp;               wielded or is an artifact and mattock isn't */
&nbsp;            if (pickaxe &amp;&amp; pickaxe-&gt;otyp == PICK_AXE &amp;&amp; pickaxe != wep
&nbsp;                &amp;&amp; (!pickaxe-&gt;oartifact || obj-&gt;oartifact))
&nbsp;                return pickaxe; /* drop the one we earlier decided to keep */
&nbsp;        /*FALLTHRU*/
&nbsp;        case PICK_AXE:
&nbsp;            if (!pickaxe || (obj-&gt;oartifact &amp;&amp; !pickaxe-&gt;oartifact)) {
&nbsp;                if (pickaxe)
&nbsp;                    return pickaxe;
&nbsp;                pickaxe = obj; /* keep this digging tool */
&nbsp;                continue;
&nbsp;            }
&nbsp;            break;
&nbsp;
&nbsp;        case UNICORN_HORN:
&nbsp;            /* reject cursed unicorn horns */
&nbsp;            if (obj-&gt;cursed)
&nbsp;                break;
&nbsp;            /* keep artifact unihorn in preference to ordinary one */
&nbsp;            if (!unihorn || (obj-&gt;oartifact &amp;&amp; !unihorn-&gt;oartifact)) {
&nbsp;                if (unihorn)
&nbsp;                    return unihorn;
&nbsp;                unihorn = obj; /* keep this unicorn horn */
&nbsp;                continue;
&nbsp;            }
&nbsp;            break;
&nbsp;
&nbsp;        case SKELETON_KEY:
&nbsp;            /* keep key in preference to lock-pick */
&nbsp;            if (key &amp;&amp; key-&gt;otyp == LOCK_PICK
&nbsp;                &amp;&amp; (!key-&gt;oartifact || obj-&gt;oartifact))
&nbsp;                return key; /* drop the one we earlier decided to keep */
&nbsp;        /*FALLTHRU*/
&nbsp;        case LOCK_PICK:
&nbsp;            /* keep lock-pick in preference to credit card */
&nbsp;            if (key &amp;&amp; key-&gt;otyp == CREDIT_CARD
&nbsp;                &amp;&amp; (!key-&gt;oartifact || obj-&gt;oartifact))
&nbsp;                return key;
&nbsp;        /*FALLTHRU*/
&nbsp;        case CREDIT_CARD:
&nbsp;            if (!key || (obj-&gt;oartifact &amp;&amp; !key-&gt;oartifact)) {
&nbsp;                if (key)
&nbsp;                    return key;
&nbsp;                key = obj; /* keep this unlocking tool */
&nbsp;                continue;
&nbsp;            }
&nbsp;            break;
&nbsp;
&nbsp;        default:
&nbsp;            break;
&nbsp;        }
&nbsp;
&nbsp;        if (!obj-&gt;owornmask &amp;&amp; obj != wep)
&nbsp;            return obj;
&nbsp;    }
&nbsp;
&nbsp;    return (struct obj *) 0; /* don't drop anything */
&nbsp;}
&nbsp;
&nbsp;static NEARDATA const char nofetch[] = { BALL_CLASS, CHAIN_CLASS, ROCK_CLASS,
&nbsp;                                         0 };
&nbsp;
&nbsp;STATIC_VAR xchar gtyp, gx, gy; /* type and position of dog's current goal */
&nbsp;
&nbsp;STATIC_PTR void FDECL(wantdoor, (int, int, genericptr_t));
&nbsp;
cursed_object_at
&nbsp;boolean
&nbsp;cursed_object_at(x, y)
&nbsp;int x, y;
&nbsp;{
&nbsp;    struct obj *otmp;
&nbsp;
&nbsp;    for (otmp = level.objects[x][y]; otmp; otmp = otmp-&gt;nexthere)
&nbsp;        if (otmp-&gt;cursed)
&nbsp;            return TRUE;
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
dog_nutrition
&nbsp;int
&nbsp;dog_nutrition(mtmp, obj)
&nbsp;struct monst *mtmp;
&nbsp;struct obj *obj;
&nbsp;{
&nbsp;    int nutrit;
&nbsp;
&nbsp;    /*
&nbsp;     * It is arbitrary that the pet takes the same length of time to eat
&nbsp;     * as a human, but gets more nutritional value.
&nbsp;     */
&nbsp;    if (obj-&gt;oclass == FOOD_CLASS) {
&nbsp;        if (obj-&gt;otyp == CORPSE) {
&nbsp;            mtmp-&gt;meating = 3 + (mons[obj-&gt;corpsenm].cwt &gt;&gt; 6);
&nbsp;            nutrit = mons[obj-&gt;corpsenm].cnutrit;
&nbsp;        } else {
&nbsp;            mtmp-&gt;meating = objects[obj-&gt;otyp].oc_delay;
&nbsp;            nutrit = objects[obj-&gt;otyp].oc_nutrition;
&nbsp;        }
&nbsp;        switch (mtmp-&gt;data-&gt;msize) {
&nbsp;        case MZ_TINY:
&nbsp;            nutrit *= 8;
&nbsp;            break;
&nbsp;        case MZ_SMALL:
&nbsp;            nutrit *= 6;
&nbsp;            break;
&nbsp;        default:
&nbsp;        case MZ_MEDIUM:
&nbsp;            nutrit *= 5;
&nbsp;            break;
&nbsp;        case MZ_LARGE:
&nbsp;            nutrit *= 4;
&nbsp;            break;
&nbsp;        case MZ_HUGE:
&nbsp;            nutrit *= 3;
&nbsp;            break;
&nbsp;        case MZ_GIGANTIC:
&nbsp;            nutrit *= 2;
&nbsp;            break;
&nbsp;        }
&nbsp;        if (obj-&gt;oeaten) {
&nbsp;            mtmp-&gt;meating = eaten_stat(mtmp-&gt;meating, obj);
&nbsp;            nutrit = eaten_stat(nutrit, obj);
&nbsp;        }
&nbsp;    } else if (obj-&gt;oclass == COIN_CLASS) {
&nbsp;        mtmp-&gt;meating = (int) (obj-&gt;quan / 2000) + 1;
&nbsp;        if (mtmp-&gt;meating &lt; 0)
&nbsp;            mtmp-&gt;meating = 1;
&nbsp;        nutrit = (int) (obj-&gt;quan / 20);
&nbsp;        if (nutrit &lt; 0)
&nbsp;            nutrit = 0;
&nbsp;    } else {
&nbsp;        /* Unusual pet such as gelatinous cube eating odd stuff.
&nbsp;         * meating made consistent with wild monsters in mon.c.
&nbsp;         * nutrit made consistent with polymorphed player nutrit in
&nbsp;         * eat.c.  (This also applies to pets eating gold.)
&nbsp;         */
&nbsp;        mtmp-&gt;meating = obj-&gt;owt / 20 + 1;
&nbsp;        nutrit = 5 * objects[obj-&gt;otyp].oc_nutrition;
&nbsp;    }
&nbsp;    return nutrit;
&nbsp;}
&nbsp;
dog_eat
&nbsp;/* returns 2 if pet dies, otherwise 1 */
&nbsp;int
&nbsp;dog_eat(mtmp, obj, x, y, devour)
&nbsp;register struct monst *mtmp;
&nbsp;register struct obj *obj; /* if unpaid, then thrown or kicked by hero */
&nbsp;int x, y; /* dog's starting location, might be different from current */
&nbsp;boolean devour;
&nbsp;{
&nbsp;    register struct edog *edog = EDOG(mtmp);
&nbsp;    boolean poly, grow, heal, eyes, slimer, deadmimic;
&nbsp;    int nutrit;
&nbsp;    long oprice;
&nbsp;    char objnambuf[BUFSZ];
&nbsp;
&nbsp;    objnambuf[0] = '\0';
&nbsp;    if (edog-&gt;hungrytime &lt; monstermoves)
&nbsp;        edog-&gt;hungrytime = monstermoves;
&nbsp;    nutrit = dog_nutrition(mtmp, obj);
&nbsp;
&nbsp;    deadmimic = (obj-&gt;otyp == CORPSE &amp;&amp; (obj-&gt;corpsenm == PM_SMALL_MIMIC
&nbsp;                                         || obj-&gt;corpsenm == PM_LARGE_MIMIC
&nbsp;                                         || obj-&gt;corpsenm == PM_GIANT_MIMIC));
&nbsp;    slimer = (obj-&gt;otyp == CORPSE &amp;&amp; obj-&gt;corpsenm == PM_GREEN_SLIME);
&nbsp;    poly = polyfodder(obj);
&nbsp;    grow = mlevelgain(obj);
&nbsp;    heal = mhealup(obj);
&nbsp;    eyes = (obj-&gt;otyp == CARROT);
&nbsp;
&nbsp;    if (devour) {
&nbsp;        if (mtmp-&gt;meating &gt; 1)
&nbsp;            mtmp-&gt;meating /= 2;
&nbsp;        if (nutrit &gt; 1)
&nbsp;            nutrit = (nutrit * 3) / 4;
&nbsp;    }
&nbsp;    edog-&gt;hungrytime += nutrit;
&nbsp;    mtmp-&gt;mconf = 0;
&nbsp;    if (edog-&gt;mhpmax_penalty) {
&nbsp;        /* no longer starving */
&nbsp;        mtmp-&gt;mhpmax += edog-&gt;mhpmax_penalty;
&nbsp;        edog-&gt;mhpmax_penalty = 0;
&nbsp;    }
&nbsp;    if (mtmp-&gt;mflee &amp;&amp; mtmp-&gt;mfleetim &gt; 1)
&nbsp;        mtmp-&gt;mfleetim /= 2;
&nbsp;    if (mtmp-&gt;mtame &lt; 20)
&nbsp;        mtmp-&gt;mtame++;
&nbsp;    if (x != mtmp-&gt;mx || y != mtmp-&gt;my) { /* moved &amp; ate on same turn */
&nbsp;        newsym(x, y);
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;
&nbsp;    /* food items are eaten one at a time; entire stack for other stuff */
&nbsp;    if (obj-&gt;quan &gt; 1L &amp;&amp; obj-&gt;oclass == FOOD_CLASS)
&nbsp;        obj = splitobj(obj, 1L);
&nbsp;    if (obj-&gt;unpaid)
&nbsp;        iflags.suppress_price++;
&nbsp;    if (is_pool(x, y) &amp;&amp; !Underwater) {
&nbsp;        /* Don't print obj */
&nbsp;        /* TODO: Reveal presence of sea monster (especially sharks) */
&nbsp;    } else {
&nbsp;        /* food is at monster's current location, 

<mx,my>;
&nbsp;           <x,y> was monster's location at start of this turn;
&nbsp;           they might be the same but will be different when
&nbsp;           the monster is moving+eating on same turn */
&nbsp;        boolean seeobj = cansee(mtmp-&gt;mx, mtmp-&gt;my),
&nbsp;                sawpet = cansee(x, y) &amp;&amp; mon_visible(mtmp);
&nbsp;
&nbsp;        /* Observe the action if either the food location or the pet
&nbsp;           itself is in view.  When pet which was in view moves to an
&nbsp;           unseen spot to eat the food there, avoid referring to that
&nbsp;           pet as "it".  However, we want "it" if invisible/unsensed
&nbsp;           pet eats visible food. */
&nbsp;        if (sawpet || (seeobj &amp;&amp; canspotmon(mtmp))) {
&nbsp;            if (tunnels(mtmp-&gt;data))
&nbsp;                pline("%s digs in.", noit_Monnam(mtmp));
&nbsp;            else
&nbsp;                pline("%s %s %s.", noit_Monnam(mtmp),
&nbsp;                      devour ? "devours" : "eats", distant_name(obj, doname));
&nbsp;        } else if (seeobj)
&nbsp;            pline("It %s %s.", devour ? "devours" : "eats",
&nbsp;                  distant_name(obj, doname));
&nbsp;    }
&nbsp;    if (obj-&gt;unpaid) {
&nbsp;        Strcpy(objnambuf, xname(obj));
&nbsp;        iflags.suppress_price--;
&nbsp;    }
&nbsp;    /* It's a reward if it's DOGFOOD and the player dropped/threw it.
&nbsp;       We know the player had it if invlet is set. -dlc */
&nbsp;    if (dogfood(mtmp, obj) == DOGFOOD &amp;&amp; obj-&gt;invlet)
&nbsp;#ifdef LINT
&nbsp;        edog-&gt;apport = 0;
&nbsp;#else
&nbsp;        edog-&gt;apport += (int) (200L / ((long) edog-&gt;dropdist + monstermoves
&nbsp;                                       - edog-&gt;droptime));
&nbsp;#endif
&nbsp;    if (mtmp-&gt;data == &amp;mons[PM_RUST_MONSTER] &amp;&amp; obj-&gt;oerodeproof) {
&nbsp;        /* The object's rustproofing is gone now */
&nbsp;        if (obj-&gt;unpaid)
&nbsp;            costly_alteration(obj, COST_DEGRD);
&nbsp;        obj-&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(obj, doname));
&nbsp;        }
&nbsp;    } else if (obj == uball) {
&nbsp;        unpunish();
&nbsp;        delobj(obj); /* we assume this can't be unpaid */
&nbsp;    } else if (obj == uchain) {
&nbsp;        unpunish();
&nbsp;    } else {
&nbsp;        if (obj-&gt;unpaid) {
&nbsp;            /* edible item owned by shop has been thrown or kicked
&nbsp;               by hero and caught by tame or food-tameable monst */
&nbsp;            oprice = unpaid_cost(obj, TRUE);
&nbsp;            pline("That %s will cost you %ld %s.", objnambuf, oprice,
&nbsp;                  currency(oprice));
&nbsp;            /* delobj-&gt;obfree will handle actual shop billing update */
&nbsp;        }
&nbsp;        delobj(obj);
&nbsp;    }
&nbsp;
&nbsp;#if 0 /* pet is eating, so slime recovery is not feasible... */
&nbsp;    /* turning into slime might be cureable */
&nbsp;    if (slimer &amp;&amp; munslime(mtmp, FALSE)) {
&nbsp;        /* but the cure (fire directed at self) might be fatal */
&nbsp;        if (mtmp-&gt;mhp &lt; 1)
&nbsp;            return 2;
&nbsp;        slimer = FALSE; /* sliming is avoided, skip polymorph */
&nbsp;    }
&nbsp;#endif
&nbsp;
&nbsp;    if (poly || slimer) {
&nbsp;        struct permonst *ptr = slimer ? &amp;mons[PM_GREEN_SLIME] : 0;
&nbsp;
&nbsp;        (void) newcham(mtmp, ptr, FALSE, cansee(mtmp-&gt;mx, mtmp-&gt;my));
&nbsp;    }
&nbsp;
&nbsp;    /* limit "instant" growth to prevent potential abuse */
&nbsp;    if (grow &amp;&amp; (int) mtmp-&gt;m_lev &lt; (int) mtmp-&gt;data-&gt;mlevel + 15) {
&nbsp;        if (!grow_up(mtmp, (struct monst *) 0))
&nbsp;            return 2;
&nbsp;    }
&nbsp;    if (heal)
&nbsp;        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;    if ((eyes || heal) &amp;&amp; !mtmp-&gt;mcansee)
&nbsp;        mcureblindness(mtmp, canseemon(mtmp));
&nbsp;    if (deadmimic)
&nbsp;        quickmimic(mtmp);
&nbsp;    return 1;
&nbsp;}
&nbsp;
dog_hunger
&nbsp;/* hunger effects -- returns TRUE on starvation */
&nbsp;STATIC_OVL boolean
&nbsp;dog_hunger(mtmp, edog)
&nbsp;struct monst *mtmp;
&nbsp;struct edog *edog;
&nbsp;{
&nbsp;    if (monstermoves &gt; edog-&gt;hungrytime + 500) {
&nbsp;        if (!carnivorous(mtmp-&gt;data) &amp;&amp; !herbivorous(mtmp-&gt;data)) {
&nbsp;            edog-&gt;hungrytime = monstermoves + 500;
&nbsp;            /* but not too high; it might polymorph */
&nbsp;        } else if (!edog-&gt;mhpmax_penalty) {
&nbsp;            /* starving pets are limited in healing */
&nbsp;            int newmhpmax = mtmp-&gt;mhpmax / 3;
&nbsp;            mtmp-&gt;mconf = 1;
&nbsp;            edog-&gt;mhpmax_penalty = mtmp-&gt;mhpmax - newmhpmax;
&nbsp;            mtmp-&gt;mhpmax = newmhpmax;
&nbsp;            if (mtmp-&gt;mhp &gt; mtmp-&gt;mhpmax)
&nbsp;                mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;            if (mtmp-&gt;mhp &lt; 1)
&nbsp;                goto dog_died;
&nbsp;            if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                pline("%s is confused from hunger.", Monnam(mtmp));
&nbsp;            else if (couldsee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                beg(mtmp);
&nbsp;            else
&nbsp;                You_feel("worried about %s.", y_monnam(mtmp));
&nbsp;            stop_occupation();
&nbsp;        } else if (monstermoves &gt; edog-&gt;hungrytime + 750 || mtmp-&gt;mhp &lt; 1) {
&nbsp;        dog_died:
&nbsp;            if (mtmp-&gt;mleashed &amp;&amp; mtmp != u.usteed)
&nbsp;                Your("leash goes slack.");
&nbsp;            else if (cansee(mtmp-&gt;mx, mtmp-&gt;my))
&nbsp;                pline("%s starves.", Monnam(mtmp));
&nbsp;            else
&nbsp;                You_feel("%s for a moment.",
&nbsp;                         Hallucination ? "bummed" : "sad");
&nbsp;            mondied(mtmp);
&nbsp;            return  TRUE;
&nbsp;        }
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
dog_invent
&nbsp;/* do something with object (drop, pick up, eat) at current position
&nbsp; * returns 1 if object eaten (since that counts as dog's move), 2 if died
&nbsp; */
&nbsp;STATIC_OVL int
&nbsp;dog_invent(mtmp, edog, udist)
&nbsp;register struct monst *mtmp;
&nbsp;register struct edog *edog;
&nbsp;int udist;
&nbsp;{
&nbsp;    register int omx, omy, carryamt = 0;
&nbsp;    struct obj *obj, *otmp;
&nbsp;
&nbsp;    if (mtmp-&gt;msleeping || !mtmp-&gt;mcanmove)
&nbsp;        return 0;
&nbsp;
&nbsp;    omx = mtmp-&gt;mx;
&nbsp;    omy = mtmp-&gt;my;
&nbsp;
&nbsp;    /* If we are carrying something then we drop it (perhaps near @).
&nbsp;     * Note: if apport == 1 then our behaviour is independent of udist.
&nbsp;     * Use udist+1 so steed won't cause divide by zero.
&nbsp;     */
&nbsp;    if (droppables(mtmp)) {
&nbsp;        if (!rn2(udist + 1) || !rn2(edog-&gt;apport))
&nbsp;            if (rn2(10) &lt; edog-&gt;apport) {
&nbsp;                relobj(mtmp, (int) mtmp-&gt;minvis, TRUE);
&nbsp;                if (edog-&gt;apport &gt; 1)
&nbsp;                    edog-&gt;apport--;
&nbsp;                edog-&gt;dropdist = udist; /* hpscdi!jon */
&nbsp;                edog-&gt;droptime = monstermoves;
&nbsp;            }
&nbsp;    } else {
&nbsp;        if ((obj = level.objects[omx][omy]) != 0
&nbsp;            &amp;&amp; !index(nofetch, obj-&gt;oclass)
&nbsp;#ifdef MAIL
&nbsp;            &amp;&amp; obj-&gt;otyp != SCR_MAIL
&nbsp;#endif
&nbsp;            ) {
&nbsp;            int edible = dogfood(mtmp, obj);
&nbsp;
&nbsp;            if ((edible &lt;= CADAVER
&nbsp;                 /* starving pet is more aggressive about eating */
&nbsp;                 || (edog-&gt;mhpmax_penalty &amp;&amp; edible == ACCFOOD))
&nbsp;                &amp;&amp; could_reach_item(mtmp, obj-&gt;ox, obj-&gt;oy))
&nbsp;                return dog_eat(mtmp, obj, omx, omy, FALSE);
&nbsp;
&nbsp;            carryamt = can_carry(mtmp, obj);
&nbsp;            if (carryamt &gt; 0 &amp;&amp; !obj-&gt;cursed
&nbsp;                &amp;&amp; could_reach_item(mtmp, obj-&gt;ox, obj-&gt;oy)) {
&nbsp;                if (rn2(20) &lt; edog-&gt;apport + 3) {
&nbsp;                    if (rn2(udist) || !rn2(edog-&gt;apport)) {
&nbsp;                        otmp = obj;
&nbsp;                        if (carryamt != obj-&gt;quan)
&nbsp;                            otmp = splitobj(obj, carryamt);
&nbsp;                        if (cansee(omx, omy) &amp;&amp; flags.verbose)
&nbsp;                            pline("%s picks up %s.", Monnam(mtmp),
&nbsp;                                  distant_name(otmp, doname));
&nbsp;                        obj_extract_self(otmp);
&nbsp;                        newsym(omx, omy);
&nbsp;                        (void) mpickobj(mtmp, otmp);
&nbsp;                        if (attacktype(mtmp-&gt;data, AT_WEAP)
&nbsp;                            &amp;&amp; mtmp-&gt;weapon_check == NEED_WEAPON) {
&nbsp;                            mtmp-&gt;weapon_check = NEED_HTH_WEAPON;
&nbsp;                            (void) mon_wield_item(mtmp);
&nbsp;                        }
&nbsp;                        m_dowear(mtmp, FALSE);
&nbsp;                    }
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    return 0;
&nbsp;}
&nbsp;
dog_goal
&nbsp;/* set dog's goal -- gtyp, gx, gy;
&nbsp;   returns -1/0/1 (dog's desire to approach player) or -2 (abort move) */
&nbsp;STATIC_OVL int
&nbsp;dog_goal(mtmp, edog, after, udist, whappr)
&nbsp;register struct monst *mtmp;
&nbsp;struct edog *edog;
&nbsp;int after, udist, whappr;
&nbsp;{
&nbsp;    register int omx, omy;
&nbsp;    boolean in_masters_sight, dog_has_minvent;
&nbsp;    register struct obj *obj;
&nbsp;    xchar otyp;
&nbsp;    int appr;
&nbsp;
&nbsp;    /* Steeds don't move on their own will */
&nbsp;    if (mtmp == u.usteed)
&nbsp;        return -2;
&nbsp;
&nbsp;    omx = mtmp-&gt;mx;
&nbsp;    omy = mtmp-&gt;my;
&nbsp;
&nbsp;    in_masters_sight = couldsee(omx, omy);
&nbsp;    dog_has_minvent = (droppables(mtmp) != 0);
&nbsp;
&nbsp;    if (!edog || mtmp-&gt;mleashed) { /* he's not going anywhere... */
&nbsp;        gtyp = APPORT;
&nbsp;        gx = u.ux;
&nbsp;        gy = u.uy;
&nbsp;    } else {
&nbsp;#define DDIST(x, y) (dist2(x, y, omx, omy))
&nbsp;#define SQSRCHRADIUS 5
&nbsp;        int min_x, max_x, min_y, max_y;
&nbsp;        register int nx, ny;
&nbsp;
&nbsp;        gtyp = UNDEF; /* no goal as yet */
&nbsp;        gx = gy = 0;  /* suppress 'used before set' message */
&nbsp;
&nbsp;        if ((min_x = omx - SQSRCHRADIUS) &lt; 1)
&nbsp;            min_x = 1;
&nbsp;        if ((max_x = omx + SQSRCHRADIUS) &gt;= COLNO)
&nbsp;            max_x = COLNO - 1;
&nbsp;        if ((min_y = omy - SQSRCHRADIUS) &lt; 0)
&nbsp;            min_y = 0;
&nbsp;        if ((max_y = omy + SQSRCHRADIUS) &gt;= ROWNO)
&nbsp;            max_y = ROWNO - 1;
&nbsp;
&nbsp;        /* nearby food is the first choice, then other objects */
&nbsp;        for (obj = fobj; obj; obj = obj-&gt;nobj) {
&nbsp;            nx = obj-&gt;ox;
&nbsp;            ny = obj-&gt;oy;
&nbsp;            if (nx &gt;= min_x &amp;&amp; nx &lt;= max_x &amp;&amp; ny &gt;= min_y &amp;&amp; ny &lt;= max_y) {
&nbsp;                otyp = dogfood(mtmp, obj);
&nbsp;                /* skip inferior goals */
&nbsp;                if (otyp &gt; gtyp || otyp == UNDEF)
&nbsp;                    continue;
&nbsp;                /* avoid cursed items unless starving */
&nbsp;                if (cursed_object_at(nx, ny)
&nbsp;                    &amp;&amp; !(edog-&gt;mhpmax_penalty &amp;&amp; otyp &lt; MANFOOD))
&nbsp;                    continue;
&nbsp;                /* skip completely unreachable goals */
&nbsp;                if (!could_reach_item(mtmp, nx, ny)
&nbsp;                    || !can_reach_location(mtmp, mtmp-&gt;mx, mtmp-&gt;my, nx, ny))
&nbsp;                    continue;
&nbsp;                if (otyp &lt; MANFOOD) {
&nbsp;                    if (otyp &lt; gtyp || DDIST(nx, ny) &lt; DDIST(gx, gy)) {
&nbsp;                        gx = nx;
&nbsp;                        gy = ny;
&nbsp;                        gtyp = otyp;
&nbsp;                    }
&nbsp;                } else if (gtyp == UNDEF &amp;&amp; in_masters_sight
&nbsp;                           &amp;&amp; !dog_has_minvent
&nbsp;                           &amp;&amp; (!levl[omx][omy].lit || levl[u.ux][u.uy].lit)
&nbsp;                           &amp;&amp; (otyp == MANFOOD || m_cansee(mtmp, nx, ny))
&nbsp;                           &amp;&amp; edog-&gt;apport &gt; rn2(8)
&nbsp;                           &amp;&amp; can_carry(mtmp, obj) &gt; 0) {
&nbsp;                    gx = nx;
&nbsp;                    gy = ny;
&nbsp;                    gtyp = APPORT;
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* follow player if appropriate */
&nbsp;    if (gtyp == UNDEF || (gtyp != DOGFOOD &amp;&amp; gtyp != APPORT
&nbsp;                          &amp;&amp; monstermoves &lt; edog-&gt;hungrytime)) {
&nbsp;        gx = u.ux;
&nbsp;        gy = u.uy;
&nbsp;        if (after &amp;&amp; udist &lt;= 4 &amp;&amp; gx == u.ux &amp;&amp; gy == u.uy)
&nbsp;            return -2;
&nbsp;        appr = (udist &gt;= 9) ? 1 : (mtmp-&gt;mflee) ? -1 : 0;
&nbsp;        if (udist &gt; 1) {
&nbsp;            if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) || whappr
&nbsp;                || (dog_has_minvent &amp;&amp; rn2(edog-&gt;apport)))
&nbsp;                appr = 1;
&nbsp;        }
&nbsp;        /* if you have dog food it'll follow you more closely */
&nbsp;        if (appr == 0)
&nbsp;            for (obj = invent; obj; obj = obj-&gt;nobj)
&nbsp;                if (dogfood(mtmp, obj) == DOGFOOD) {
&nbsp;                    appr = 1;
&nbsp;                    break;
&nbsp;                }
&nbsp;    } else
&nbsp;        appr = 1; /* gtyp != UNDEF */
&nbsp;    if (mtmp-&gt;mconf)
&nbsp;        appr = 0;
&nbsp;
&nbsp;#define FARAWAY (COLNO + 2) /* position outside screen */
&nbsp;    if (gx == u.ux &amp;&amp; gy == u.uy &amp;&amp; !in_masters_sight) {
&nbsp;        register coord *cp;
&nbsp;
&nbsp;        cp = gettrack(omx, omy);
&nbsp;        if (cp) {
&nbsp;            gx = cp-&gt;x;
&nbsp;            gy = cp-&gt;y;
&nbsp;            if (edog)
&nbsp;                edog-&gt;ogoal.x = 0;
&nbsp;        } else {
&nbsp;            /* assume master hasn't moved far, and reuse previous goal */
&nbsp;            if (edog &amp;&amp; edog-&gt;ogoal.x
&nbsp;                &amp;&amp; (edog-&gt;ogoal.x != omx || edog-&gt;ogoal.y != omy)) {
&nbsp;                gx = edog-&gt;ogoal.x;
&nbsp;                gy = edog-&gt;ogoal.y;
&nbsp;                edog-&gt;ogoal.x = 0;
&nbsp;            } else {
&nbsp;                int fardist = FARAWAY * FARAWAY;
&nbsp;                gx = gy = FARAWAY; /* random */
&nbsp;                do_clear_area(omx, omy, 9, wantdoor, (genericptr_t) &amp;fardist);
&nbsp;
&nbsp;                /* here gx == FARAWAY e.g. when dog is in a vault */
&nbsp;                if (gx == FARAWAY || (gx == omx &amp;&amp; gy == omy)) {
&nbsp;                    gx = u.ux;
&nbsp;                    gy = u.uy;
&nbsp;                } else if (edog) {
&nbsp;                    edog-&gt;ogoal.x = gx;
&nbsp;                    edog-&gt;ogoal.y = gy;
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;    } else if (edog) {
&nbsp;        edog-&gt;ogoal.x = 0;
&nbsp;    }
&nbsp;    return appr;
&nbsp;}
&nbsp;
&nbsp;
find_targ
&nbsp;STATIC_OVL struct monst *
&nbsp;find_targ(mtmp, dx, dy, maxdist)
&nbsp;register struct monst *mtmp;
&nbsp;int dx, dy;
&nbsp;int maxdist;
&nbsp;{
&nbsp;    struct monst *targ = 0;
&nbsp;    int curx = mtmp-&gt;mx, cury = mtmp-&gt;my;
&nbsp;    int dist = 0;
&nbsp;
&nbsp;    /* Walk outwards */
&nbsp;    for ( ; dist &lt; maxdist; ++dist) {
&nbsp;        curx += dx;
&nbsp;        cury += dy;
&nbsp;        if (!isok(curx, cury))
&nbsp;            break;
&nbsp;
&nbsp;        /* FIXME: Check if we hit a wall/door/boulder to
&nbsp;         *        short-circuit unnecessary subsequent checks
&nbsp;         */
&nbsp;
&nbsp;        /* If we can't see up to here, forget it - will this
&nbsp;         * mean pets in corridors don't breathe at monsters
&nbsp;         * in rooms? If so, is that necessarily bad?
&nbsp;         */
&nbsp;        if (!m_cansee(mtmp, curx, cury))
&nbsp;            break;
&nbsp;
&nbsp;        targ = m_at(curx, cury);
&nbsp;
&nbsp;        if (curx == mtmp-&gt;mux &amp;&amp; cury == mtmp-&gt;muy)
&nbsp;            return &amp;youmonst
&nbsp;
&nbsp;        if (targ) {
&nbsp;            /* Is the monster visible to the pet? */
&nbsp;            if ((!targ-&gt;minvis || perceives(mtmp-&gt;data)) &amp;&amp;
&nbsp;                !targ-&gt;mundetected)
&nbsp;                break;
&nbsp;
&nbsp;            /* If the pet can't see it, it assumes it aint there */
&nbsp;            targ = 0;
&nbsp;        }
&nbsp;    }
&nbsp;    return targ;
&nbsp;}
&nbsp;
find_friends
&nbsp;STATIC_OVL int
&nbsp;find_friends(mtmp, mtarg, maxdist)
&nbsp;struct monst *mtmp, *mtarg;
&nbsp;int    maxdist;
&nbsp;{
&nbsp;    struct monst *pal;
&nbsp;    int dx = sgn(mtarg-&gt;mx - mtmp-&gt;mx),
&nbsp;        dy = sgn(mtarg-&gt;my - mtmp-&gt;my);
&nbsp;    int curx = mtarg-&gt;mx, cury = mtarg-&gt;my;
&nbsp;    int dist = distmin(mtarg-&gt;mx, mtarg-&gt;my, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;
&nbsp;    for ( ; dist &lt;= maxdist; ++dist) {
&nbsp;        curx += dx;
&nbsp;        cury += dy;
&nbsp;
&nbsp;        if (!isok(curx, cury))
&nbsp;            return 0;
&nbsp;
&nbsp;        /* If the pet can't see beyond this point, don't
&nbsp;         * check any farther
&nbsp;         */
&nbsp;        if (!m_cansee(mtmp, curx, cury))
&nbsp;            return 0;
&nbsp;
&nbsp;        /* Does pet think you're here? */
&nbsp;        if (mtmp-&gt;mux == curx &amp;&amp; mtmp-&gt;muy == cury)
&nbsp;            return 1;
&nbsp;
&nbsp;        pal = m_at(curx, cury);
&nbsp;
&nbsp;        if (pal) {
&nbsp;            if (pal-&gt;mtame) {
&nbsp;                /* Pet won't notice invisible pets */
&nbsp;                if (!pal-&gt;minvis || perceives(mtmp-&gt;data))
&nbsp;                    return 1;
&nbsp;            } else {
&nbsp;                /* Quest leaders and guardians are always seen */
&nbsp;                if (pal-&gt;data-&gt;msound == MS_LEADER
&nbsp;                    || pal-&gt;data-&gt;msound == MS_GUARDIAN)
&nbsp;                    return 1;
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    return 0;
&nbsp;}
&nbsp;
score_targ
&nbsp;STATIC_OVL long
&nbsp;score_targ(mtmp, mtarg)
&nbsp;struct monst *mtmp, *mtarg;
&nbsp;{
&nbsp;    long score = 0L;
&nbsp;
&nbsp;    /* If the monster is confused, normal scoring is disrupted -
&nbsp;     * anything may happen
&nbsp;     */
&nbsp;
&nbsp;    /* Give 1 in 3 chance of safe breathing even if pet is confused or
&nbsp;     * if you're on the quest start level */
&nbsp;    if (!mtmp-&gt;mconf || !rn2(3) || Is_qstart(&amp;u.uz)) {
&nbsp;        aligntyp align1 = A_NONE, align2 = A_NONE; /* For priests, minions */
&nbsp;        boolean faith1 = TRUE,  faith2 = TRUE;
&nbsp;
&nbsp;        if (mtmp-&gt;isminion)
&nbsp;            align1 = EMIN(mtmp)-&gt;min_align;
&nbsp;        else if (mtmp-&gt;ispriest)
&nbsp;            align1 = EPRI(mtmp)-&gt;shralign;
&nbsp;        else
&nbsp;            faith1 = FALSE;
&nbsp;        if (mtarg-&gt;isminion)
&nbsp;            align2 = EMIN(mtarg)-&gt;min_align; /* MAR */
&nbsp;        else if (mtarg-&gt;ispriest)
&nbsp;            align2 = EPRI(mtarg)-&gt;shralign; /* MAR */
&nbsp;        else
&nbsp;            faith2 = FALSE;
&nbsp;
&nbsp;        /* Never target quest friendlies */
&nbsp;        if (mtarg-&gt;data-&gt;msound == MS_LEADER
&nbsp;            || mtarg-&gt;data-&gt;msound == MS_GUARDIAN)
&nbsp;            return -5000L;
&nbsp;        /* D: Fixed angelic beings using gaze attacks on coaligned priests */
&nbsp;        if (faith1 &amp;&amp; faith2 &amp;&amp; align1 == align2 &amp;&amp; mtarg-&gt;mpeaceful) {
&nbsp;            score -= 5000L;
&nbsp;            return score;
&nbsp;        }
&nbsp;        /* Is monster adjacent? */
&nbsp;        if (distmin(mtmp-&gt;mx, mtmp-&gt;my, mtarg-&gt;mx, mtarg-&gt;my) &lt;= 1) {
&nbsp;            score -= 3000L;
&nbsp;            return score;
&nbsp;        }
&nbsp;        /* Is the monster peaceful or tame? */
&nbsp;        if (/*mtarg-&gt;mpeaceful ||*/ mtarg-&gt;mtame || mtarg == &amp;youmonst) {
&nbsp;            /* Pets will never be targeted */
&nbsp;            score -= 3000L;
&nbsp;            return score;
&nbsp;        }
&nbsp;        /* Is master/pet behind monster? Check up to 15 squares beyond pet. */
&nbsp;        if (find_friends(mtmp, mtarg, 15)) {
&nbsp;            score -= 3000L;
&nbsp;            return score;
&nbsp;        }
&nbsp;        /* Target hostile monsters in preference to peaceful ones */
&nbsp;        if (!mtarg-&gt;mpeaceful)
&nbsp;            score += 10;
&nbsp;        /* Is the monster passive? Don't waste energy on it, if so */
&nbsp;        if (mtarg-&gt;data-&gt;mattk[0].aatyp == AT_NONE)
&nbsp;            score -= 1000;
&nbsp;        /* Even weak pets with breath attacks shouldn't take on very
&nbsp;           low-level monsters. Wasting breath on lichens is ridiculous. */
&nbsp;        if ((mtarg-&gt;m_lev &lt; 2 &amp;&amp; mtmp-&gt;m_lev &gt; 5)
&nbsp;            || (mtmp-&gt;m_lev &gt; 12 &amp;&amp; mtarg-&gt;m_lev &lt; mtmp-&gt;m_lev - 9
&nbsp;                &amp;&amp; u.ulevel &gt; 8 &amp;&amp; mtarg-&gt;m_lev &lt; u.ulevel - 7))
&nbsp;            score -= 25;
&nbsp;        /* And pets will hesitate to attack vastly stronger foes.
&nbsp;           This penalty will be discarded if master's in trouble. */
&nbsp;        if (mtarg-&gt;m_lev &gt; mtmp-&gt;m_lev + 4L)
&nbsp;            score -= (mtarg-&gt;m_lev - mtmp-&gt;m_lev) * 20L;
&nbsp;        /* All things being the same, go for the beefiest monster. This
&nbsp;           bonus should not be large enough to override the pet's aversion
&nbsp;           to attacking much stronger monsters. */
&nbsp;        score += mtarg-&gt;m_lev * 2 + mtarg-&gt;mhp / 3;
&nbsp;    }
&nbsp;    /* Fuzz factor to make things less predictable when very
&nbsp;       similar targets are abundant. */
&nbsp;    score += rnd(5);
&nbsp;    /* Pet may decide not to use ranged attack when confused */
&nbsp;    if (mtmp-&gt;mconf &amp;&amp; !rn2(3))
&nbsp;        score -= 1000;
&nbsp;    return score;
&nbsp;}
&nbsp;
&nbsp;
best_target
&nbsp;STATIC_OVL struct monst *
&nbsp;best_target(mtmp)
&nbsp;struct monst *mtmp;   /* Pet */
&nbsp;{
&nbsp;    int dx, dy;
&nbsp;    long bestscore = -40000L, currscore;
&nbsp;    struct monst *best_targ = 0, *temp_targ = 0;
&nbsp;
&nbsp;    /* Help! */
&nbsp;    if (!mtmp)
&nbsp;        return 0;
&nbsp;
&nbsp;    /* If the pet is blind, it's not going to see any target */
&nbsp;    if (!mtmp-&gt;mcansee)
&nbsp;        return 0;
&nbsp;
&nbsp;    /* Search for any monsters lined up with the pet, within an arbitrary
&nbsp;     * distance from the pet (7 squares, even along diagonals). Monsters
&nbsp;     * are assigned scores and the best score is chosen.
&nbsp;     */
&nbsp;    for (dy = -1; dy &lt; 2; ++dy) {
&nbsp;        for (dx = -1; dx &lt; 2; ++dx) {
&nbsp;            if (!dx &amp;&amp; !dy)
&nbsp;                continue;
&nbsp;            /* Traverse the line to find the first monster within 7
&nbsp;             * squares. Invisible monsters are skipped (if the
&nbsp;             * pet doesn't have see invisible).
&nbsp;             */
&nbsp;            temp_targ = find_targ(mtmp, dx, dy, 7);
&nbsp;
&nbsp;            /* Nothing in this line? */
&nbsp;            if (!temp_targ)
&nbsp;                continue;
&nbsp;
&nbsp;            /* Decide how attractive the target is */
&nbsp;            currscore = score_targ(mtmp, temp_targ);
&nbsp;
&nbsp;            if (currscore &gt; bestscore) {
&nbsp;                bestscore = currscore;
&nbsp;                best_targ = temp_targ;
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /* Filter out targets the pet doesn't like */
&nbsp;    if (bestscore &lt; 0L)
&nbsp;        best_targ = 0;
&nbsp;
&nbsp;    return best_targ;
&nbsp;}
&nbsp;
&nbsp;
dog_move
&nbsp;/* return 0 (no move), 1 (move) or 2 (dead) */
&nbsp;int
&nbsp;dog_move(mtmp, after)
&nbsp;register struct monst *mtmp;
&nbsp;int after; /* this is extra fast monster movement */
&nbsp;{
&nbsp;    int omx, omy; /* original mtmp position */
&nbsp;    int appr, whappr, udist;
&nbsp;    int i, j, k;
&nbsp;    register struct edog *edog = EDOG(mtmp);
&nbsp;    struct obj *obj = (struct obj *) 0;
&nbsp;    xchar otyp;
&nbsp;    boolean has_edog, cursemsg[9], do_eat = FALSE;
&nbsp;    boolean better_with_displacing = FALSE;
&nbsp;    xchar nix, niy;      /* position mtmp is (considering) moving to */
&nbsp;    register int nx, ny; /* temporary coordinates */
&nbsp;    xchar cnt, uncursedcnt, chcnt;
&nbsp;    int chi = -1, nidist, ndist;
&nbsp;    coord poss[9];
&nbsp;    long info[9], allowflags;
&nbsp;#define GDIST(x, y) (dist2(x, y, gx, gy))
&nbsp;
&nbsp;    /*
&nbsp;     * Tame Angels have isminion set and an ispriest structure instead of
&nbsp;     * an edog structure.  Fortunately, guardian Angels need not worry
&nbsp;     * about mundane things like eating and fetching objects, and can
&nbsp;     * spend all their energy defending the player.  (They are the only
&nbsp;     * monsters with other structures that can be tame.)
&nbsp;     */
&nbsp;    has_edog = !mtmp-&gt;isminion;
&nbsp;
&nbsp;    omx = mtmp-&gt;mx;
&nbsp;    omy = mtmp-&gt;my;
&nbsp;    if (has_edog &amp;&amp; dog_hunger(mtmp, edog))
&nbsp;        return 2; /* starved */
&nbsp;
&nbsp;    udist = distu(omx, omy);
&nbsp;    /* Let steeds eat and maybe throw rider during Conflict */
&nbsp;    if (mtmp == u.usteed) {
&nbsp;        if (Conflict &amp;&amp; !resist(mtmp, RING_CLASS, 0, 0)) {
&nbsp;            dismount_steed(DISMOUNT_THROWN);
&nbsp;            return 1;
&nbsp;        }
&nbsp;        udist = 1;
&nbsp;    } else if (!udist)
&nbsp;        /* maybe we tamed him while being swallowed --jgm */
&nbsp;        return 0;
&nbsp;
&nbsp;    nix = omx; /* set before newdogpos */
&nbsp;    niy = omy;
&nbsp;    cursemsg[0] = FALSE; /* lint suppression */
&nbsp;    info[0] = 0;         /* ditto */
&nbsp;
&nbsp;    if (has_edog) {
&nbsp;        j = dog_invent(mtmp, edog, udist);
&nbsp;        if (j == 2)
&nbsp;            return 2; /* died */
&nbsp;        else if (j == 1)
&nbsp;            goto newdogpos; /* eating something */
&nbsp;
&nbsp;        whappr = (monstermoves - edog-&gt;whistletime &lt; 5);
&nbsp;    } else
&nbsp;        whappr = 0;
&nbsp;
&nbsp;    appr = dog_goal(mtmp, has_edog ? edog : (struct edog *) 0, after, udist,
&nbsp;                    whappr);
&nbsp;    if (appr == -2)
&nbsp;        return 0;
&nbsp;
&nbsp;    allowflags = ALLOW_M | ALLOW_TRAPS | ALLOW_SSM | ALLOW_SANCT;
&nbsp;    if (passes_walls(mtmp-&gt;data))
&nbsp;        allowflags |= (ALLOW_ROCK | ALLOW_WALL);
&nbsp;    if (passes_bars(mtmp-&gt;data))
&nbsp;        allowflags |= ALLOW_BARS;
&nbsp;    if (throws_rocks(mtmp-&gt;data))
&nbsp;        allowflags |= ALLOW_ROCK;
&nbsp;    if (is_displacer(mtmp-&gt;data))
&nbsp;        allowflags |= ALLOW_MDISP;
&nbsp;    if (Conflict &amp;&amp; !resist(mtmp, RING_CLASS, 0, 0)) {
&nbsp;        allowflags |= ALLOW_U;
&nbsp;        if (!has_edog) {
&nbsp;            /* Guardian angel refuses to be conflicted; rather,
&nbsp;             * it disappears, angrily, and sends in some nasties
&nbsp;             */
&nbsp;            lose_guardian_angel(mtmp);
&nbsp;            return 2; /* current monster is gone */
&nbsp;        }
&nbsp;    }
&nbsp;#if 0 /* [this is now handled in dochug()] */
&nbsp;    if (!Conflict &amp;&amp; !mtmp-&gt;mconf
&nbsp;        &amp;&amp; mtmp == u.ustuck &amp;&amp; !sticks(youmonst.data)) {
&nbsp;        unstuck(mtmp); /* swallowed case handled above */
&nbsp;        You("get released!");
&nbsp;    }
&nbsp;#endif
&nbsp;    if (!nohands(mtmp-&gt;data) &amp;&amp; !verysmall(mtmp-&gt;data)) {
&nbsp;        allowflags |= OPENDOOR;
&nbsp;        if (monhaskey(mtmp, TRUE))
&nbsp;            allowflags |= UNLOCKDOOR;
&nbsp;        /* note:  the Wizard and Riders can unlock doors without a key;
&nbsp;           they won't use that ability if someone manages to tame them */
&nbsp;    }
&nbsp;    if (is_giant(mtmp-&gt;data))
&nbsp;        allowflags |= BUSTDOOR;
&nbsp;    if (tunnels(mtmp-&gt;data)
&nbsp;        &amp;&amp; !Is_rogue_level(&amp;u.uz)) /* same restriction as m_move() */
&nbsp;        allowflags |= ALLOW_DIG;
&nbsp;    cnt = mfndpos(mtmp, poss, info, allowflags);
&nbsp;
&nbsp;    /* Normally dogs don't step on cursed items, but if they have no
&nbsp;     * other choice they will.  This requires checking ahead of time
&nbsp;     * to see how many uncursed item squares are around.
&nbsp;     */
&nbsp;    uncursedcnt = 0;
&nbsp;    for (i = 0; i &lt; cnt; i++) {
&nbsp;        nx = poss[i].x;
&nbsp;        ny = poss[i].y;
&nbsp;        if (MON_AT(nx, ny) &amp;&amp; !((info[i] &amp; ALLOW_M) || info[i] &amp; ALLOW_MDISP))
&nbsp;            continue;
&nbsp;        if (cursed_object_at(nx, ny))
&nbsp;            continue;
&nbsp;        uncursedcnt++;
&nbsp;    }
&nbsp;
&nbsp;    better_with_displacing = should_displace(mtmp, poss, info, cnt, gx, gy);
&nbsp;
&nbsp;    chcnt = 0;
&nbsp;    chi = -1;
&nbsp;    nidist = GDIST(nix, niy);
&nbsp;
&nbsp;    for (i = 0; i &lt; cnt; i++) {
&nbsp;        nx = poss[i].x;
&nbsp;        ny = poss[i].y;
&nbsp;        cursemsg[i] = FALSE;
&nbsp;
&nbsp;        /* if leashed, we drag him along. */
&nbsp;        if (mtmp-&gt;mleashed &amp;&amp; distu(nx, ny) &gt; 4)
&nbsp;            continue;
&nbsp;
&nbsp;        /* if a guardian, try to stay close by choice */
&nbsp;        if (!has_edog &amp;&amp; (j = distu(nx, ny)) &gt; 16 &amp;&amp; j &gt;= udist)
&nbsp;            continue;
&nbsp;
&nbsp;        if ((info[i] &amp; ALLOW_M) &amp;&amp; MON_AT(nx, ny)) {
&nbsp;            int mstatus;
&nbsp;            register struct monst *mtmp2 = m_at(nx, ny);
&nbsp;
&nbsp;            if ((int) mtmp2-&gt;m_lev &gt;= (int) mtmp-&gt;m_lev + 2
&nbsp;                || (mtmp2-&gt;data == &amp;mons[PM_FLOATING_EYE] &amp;&amp; rn2(10)
&nbsp;                    &amp;&amp; mtmp-&gt;mcansee &amp;&amp; haseyes(mtmp-&gt;data) &amp;&amp; mtmp2-&gt;mcansee
&nbsp;                    &amp;&amp; (perceives(mtmp-&gt;data) || !mtmp2-&gt;minvis))
&nbsp;                || (mtmp2-&gt;data == &amp;mons[PM_GELATINOUS_CUBE] &amp;&amp; rn2(10))
&nbsp;                || (max_passive_dmg(mtmp2, mtmp) &gt;= mtmp-&gt;mhp)
&nbsp;                || ((mtmp-&gt;mhp * 4 &lt; mtmp-&gt;mhpmax
&nbsp;                     || mtmp2-&gt;data-&gt;msound == MS_GUARDIAN
&nbsp;                     || mtmp2-&gt;data-&gt;msound == MS_LEADER) &amp;&amp; mtmp2-&gt;mpeaceful
&nbsp;                    &amp;&amp; !Conflict)
&nbsp;                || (touch_petrifies(mtmp2-&gt;data) &amp;&amp; !resists_ston(mtmp)))
&nbsp;                continue;
&nbsp;
&nbsp;            if (after)
&nbsp;                return 0; /* hit only once each move */
&nbsp;
&nbsp;            notonhead = 0;
&nbsp;            mstatus = mattackm(mtmp, mtmp2);
&nbsp;
&nbsp;            /* aggressor (pet) died */
&nbsp;            if (mstatus &amp; MM_AGR_DIED)
&nbsp;                return 2;
&nbsp;
&nbsp;            if ((mstatus &amp; MM_HIT) &amp;&amp; !(mstatus &amp; MM_DEF_DIED) &amp;&amp; rn2(4)
&nbsp;                &amp;&amp; mtmp2-&gt;mlstmv != monstermoves
&nbsp;                &amp;&amp; !onscary(mtmp-&gt;mx, mtmp-&gt;my, mtmp2)
&nbsp;                /* monnear check needed: long worms hit on tail */
&nbsp;                &amp;&amp; monnear(mtmp2, mtmp-&gt;mx, mtmp-&gt;my)) {
&nbsp;                mstatus = mattackm(mtmp2, mtmp); /* return attack */
&nbsp;                if (mstatus &amp; MM_DEF_DIED)
&nbsp;                    return 2;
&nbsp;            }
&nbsp;            return 0;
&nbsp;        }
&nbsp;        if ((info[i] &amp; ALLOW_MDISP) &amp;&amp; MON_AT(nx, ny)
&nbsp;            &amp;&amp; better_with_displacing &amp;&amp; !undesirable_disp(mtmp, nx, ny)) {
&nbsp;            int mstatus;
&nbsp;            register struct monst *mtmp2 = m_at(nx, ny);
&nbsp;
&nbsp;            mstatus = mdisplacem(mtmp, mtmp2, FALSE); /* displace monster */
&nbsp;            if (mstatus &amp; MM_DEF_DIED)
&nbsp;                return 2;
&nbsp;            return 0;
&nbsp;        }
&nbsp;
&nbsp;        {
&nbsp;            /* Dog avoids harmful traps, but perhaps it has to pass one
&nbsp;             * in order to follow player.  (Non-harmful traps do not
&nbsp;             * have ALLOW_TRAPS in info[].)  The dog only avoids the
&nbsp;             * trap if you've seen it, unlike enemies who avoid traps
&nbsp;             * if they've seen some trap of that type sometime in the
&nbsp;             * past.  (Neither behavior is really realistic.)
&nbsp;             */
&nbsp;            struct trap *trap;
&nbsp;
&nbsp;            if ((info[i] &amp; ALLOW_TRAPS) &amp;&amp; (trap = t_at(nx, ny))) {
&nbsp;                if (mtmp-&gt;mleashed) {
&nbsp;                    if (!Deaf)
&nbsp;                        whimper(mtmp);
&nbsp;                } else {
&nbsp;                    /* 1/40 chance of stepping on it anyway, in case
&nbsp;                     * it has to pass one to follow the player...
&nbsp;                     */
&nbsp;                    if (trap-&gt;tseen &amp;&amp; rn2(40))
&nbsp;                        continue;
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;
&nbsp;        /* dog eschews cursed objects, but likes dog food */
&nbsp;        /* (minion isn't interested; `cursemsg' stays FALSE) */
&nbsp;        if (has_edog)
&nbsp;            for (obj = level.objects[nx][ny]; obj; obj = obj-&gt;nexthere) {
&nbsp;                if (obj-&gt;cursed) {
&nbsp;                    cursemsg[i] = TRUE;
&nbsp;                } else if ((otyp = dogfood(mtmp, obj)) &lt; MANFOOD
&nbsp;                         &amp;&amp; (otyp &lt; ACCFOOD
&nbsp;                             || edog-&gt;hungrytime &lt;= monstermoves)) {
&nbsp;                    /* Note: our dog likes the food so much that he
&nbsp;                     * might eat it even when it conceals a cursed object */
&nbsp;                    nix = nx;
&nbsp;                    niy = ny;
&nbsp;                    chi = i;
&nbsp;                    do_eat = TRUE;
&nbsp;                    cursemsg[i] = FALSE; /* not reluctant */
&nbsp;                    goto newdogpos;
&nbsp;                }
&nbsp;            }
&nbsp;        /* didn't find something to eat; if we saw a cursed item and
&nbsp;           aren't being forced to walk on it, usually keep looking */
&nbsp;        if (cursemsg[i] &amp;&amp; !mtmp-&gt;mleashed &amp;&amp; uncursedcnt &gt; 0
&nbsp;            &amp;&amp; rn2(13 * uncursedcnt))
&nbsp;            continue;
&nbsp;
&nbsp;        /* lessen the chance of backtracking to previous position(s) */
&nbsp;        /* This causes unintended issues for pets trying to follow
&nbsp;           the hero. Thus, only run it if not leashed and &gt;5 tiles
&nbsp;           away. */
&nbsp;        if (!mtmp-&gt;mleashed &amp;&amp;
&nbsp;            distmin(mtmp-&gt;mx, mtmp-&gt;my, u.ux, u.uy) &gt; 5) {
&nbsp;            k = has_edog ? uncursedcnt : cnt;
&nbsp;            for (j = 0; j &lt; MTSZ &amp;&amp; j &lt; k - 1; j++)
&nbsp;                if (nx == mtmp-&gt;mtrack[j].x &amp;&amp;
&nbsp;                    ny == mtmp-&gt;mtrack[j].y)
&nbsp;                    if (rn2(MTSZ * (k - j)))
&nbsp;                        goto nxti;
&nbsp;        }
&nbsp;
&nbsp;        j = ((ndist = GDIST(nx, ny)) - nidist) * appr;
&nbsp;        if ((j == 0 &amp;&amp; !rn2(++chcnt)) || j &lt; 0
&nbsp;            || (j &gt; 0 &amp;&amp; !whappr
&nbsp;                &amp;&amp; ((omx == nix &amp;&amp; omy == niy &amp;&amp; !rn2(3)) || !rn2(12)))) {
&nbsp;            nix = nx;
&nbsp;            niy = ny;
&nbsp;            nidist = ndist;
&nbsp;            if (j &lt; 0)
&nbsp;                chcnt = 0;
&nbsp;            chi = i;
&nbsp;        }
&nbsp;    nxti:
&nbsp;        ;
&nbsp;    }
&nbsp;
&nbsp;    /* Pet hasn't attacked anything but is considering moving -
&nbsp;     * now's the time for ranged attacks. Note that the pet can move
&nbsp;     * after it performs its ranged attack. Should this be changed?
&nbsp;     */
&nbsp;    {
&nbsp;        struct monst *mtarg;
&nbsp;        int hungry = 0;
&nbsp;
&nbsp;        /* How hungry is the pet? */
&nbsp;        if (!mtmp-&gt;isminion) {
&nbsp;            struct edog *dog = EDOG(mtmp);
&nbsp;            hungry = (monstermoves &gt; (dog-&gt;hungrytime + 300));
&nbsp;        }
&nbsp;
&nbsp;        /* Identify the best target in a straight line from the pet;
&nbsp;         * if there is such a target, we'll let the pet attempt an
&nbsp;         * attack.
&nbsp;         */
&nbsp;        mtarg = best_target(mtmp);
&nbsp;
&nbsp;        /* Hungry pets are unlikely to use breath/spit attacks */
&nbsp;        if (mtarg &amp;&amp; (!hungry || !rn2(5))) {
&nbsp;            int mstatus;
&nbsp;
&nbsp;            if (mtarg == &amp;youmonst) {
&nbsp;                if (mattacku(mtmp))
&nbsp;                    return 2;
&nbsp;            } else {
&nbsp;                mstatus = mattackm(mtmp, mtarg);
&nbsp;
&nbsp;                /* Shouldn't happen, really */
&nbsp;                if (mstatus &amp; MM_AGR_DIED)
&nbsp;                    return 2;
&nbsp;
&nbsp;                /* Allow the targeted nasty to strike back - if
&nbsp;                 * the targeted beast doesn't have a ranged attack,
&nbsp;                 * nothing will happen.
&nbsp;                 */
&nbsp;                if ((mstatus &amp; MM_HIT) &amp;&amp; !(mstatus &amp; MM_DEF_DIED)
&nbsp;                    &amp;&amp; rn2(4) &amp;&amp; mtarg != &amp;youmonst) {
&nbsp;
&nbsp;                    /* Can monster see? If it can, it can retaliate
&nbsp;                     * even if the pet is invisible, since it'll see
&nbsp;                     * the direction from which the ranged attack came;
&nbsp;                     * if it's blind or unseeing, it can't retaliate
&nbsp;                     */
&nbsp;                    if (mtarg-&gt;mcansee &amp;&amp; haseyes(mtarg-&gt;data)) {
&nbsp;                        mstatus = mattackm(mtarg, mtmp);
&nbsp;                        if (mstatus &amp; MM_DEF_DIED)
&nbsp;                            return 2;
&nbsp;                    }
&nbsp;                }
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;newdogpos:
&nbsp;    if (nix != omx || niy != omy) {
&nbsp;        boolean wasseen;
&nbsp;
&nbsp;        if (info[chi] &amp; ALLOW_U) {
&nbsp;            if (mtmp-&gt;mleashed) { /* play it safe */
&nbsp;                pline("%s breaks loose of %s leash!", Monnam(mtmp),
&nbsp;                      mhis(mtmp));
&nbsp;                m_unleash(mtmp, FALSE);
&nbsp;            }
&nbsp;            (void) mattacku(mtmp);
&nbsp;            return 0;
&nbsp;        }
&nbsp;        if (!m_in_out_region(mtmp, nix, niy))
&nbsp;            return 1;
&nbsp;        if (m_digweapon_check(mtmp, nix,niy))
&nbsp;            return 0;
&nbsp;
&nbsp;        /* insert a worm_move() if worms ever begin to eat things */
&nbsp;        wasseen = canseemon(mtmp);
&nbsp;        remove_monster(omx, omy);
&nbsp;        place_monster(mtmp, nix, niy);
&nbsp;        if (cursemsg[chi] &amp;&amp; (wasseen || canseemon(mtmp))) {
&nbsp;            /* describe top item of pile, not necessarily cursed item itself;
&nbsp;               don't use glyph_at() here--it would return the pet but we want
&nbsp;               to know whether an object is remembered at this map location */
&nbsp;            struct obj *o = (!Hallucination &amp;&amp; level.flags.hero_memory
&nbsp;                             &amp;&amp; glyph_is_object(levl[nix][niy].glyph))
&nbsp;                               ? vobj_at(nix, niy) : 0;
&nbsp;            const char *what = o ? distant_name(o, doname) : something;
&nbsp;
&nbsp;            pline("%s %s reluctantly over %s.", noit_Monnam(mtmp),
&nbsp;                  vtense((char *) 0, locomotion(mtmp-&gt;data, "step")), what);
&nbsp;        }
&nbsp;        for (j = MTSZ - 1; j &gt; 0; j--)
&nbsp;            mtmp-&gt;mtrack[j] = mtmp-&gt;mtrack[j - 1];
&nbsp;        mtmp-&gt;mtrack[0].x = omx;
&nbsp;        mtmp-&gt;mtrack[0].y = omy;
&nbsp;        /* We have to know if the pet's going to do a combined eat and
&nbsp;         * move before moving it, but it can't eat until after being
&nbsp;         * moved.  Thus the do_eat flag.
&nbsp;         */
&nbsp;        if (do_eat) {
&nbsp;            if (dog_eat(mtmp, obj, omx, omy, FALSE) == 2)
&nbsp;                return 2;
&nbsp;        }
&nbsp;    } else if (mtmp-&gt;mleashed &amp;&amp; distu(omx, omy) &gt; 4) {
&nbsp;        /* an incredible kludge, but the only way to keep pooch near
&nbsp;         * after it spends time eating or in a trap, etc.
&nbsp;         */
&nbsp;        coord cc;
&nbsp;
&nbsp;        nx = sgn(omx - u.ux);
&nbsp;        ny = sgn(omy - u.uy);
&nbsp;        cc.x = u.ux + nx;
&nbsp;        cc.y = u.uy + ny;
&nbsp;        if (goodpos(cc.x, cc.y, mtmp, 0))
&nbsp;            goto dognext;
&nbsp;
&nbsp;        i = xytod(nx, ny);
&nbsp;        for (j = (i + 7) % 8; j &lt; (i + 1) % 8; j++) {
&nbsp;            dtoxy(&amp;cc, j);
&nbsp;            if (goodpos(cc.x, cc.y, mtmp, 0))
&nbsp;                goto dognext;
&nbsp;        }
&nbsp;        for (j = (i + 6) % 8; j &lt; (i + 2) % 8; j++) {
&nbsp;            dtoxy(&amp;cc, j);
&nbsp;            if (goodpos(cc.x, cc.y, mtmp, 0))
&nbsp;                goto dognext;
&nbsp;        }
&nbsp;        cc.x = mtmp-&gt;mx;
&nbsp;        cc.y = mtmp-&gt;my;
&nbsp;    dognext:
&nbsp;        if (!m_in_out_region(mtmp, nix, niy))
&nbsp;            return 1;
&nbsp;        remove_monster(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        place_monster(mtmp, cc.x, cc.y);
&nbsp;        newsym(cc.x, cc.y);
&nbsp;        set_apparxy(mtmp);
&nbsp;    }
&nbsp;    return 1;
&nbsp;}
&nbsp;
could_reach_item
&nbsp;/* check if a monster could pick up objects from a location */
&nbsp;STATIC_OVL boolean
&nbsp;could_reach_item(mon, nx, ny)
&nbsp;struct monst *mon;
&nbsp;xchar nx, ny;
&nbsp;{
&nbsp;    if ((!is_pool(nx, ny) || is_swimmer(mon-&gt;data))
&nbsp;        &amp;&amp; (!is_lava(nx, ny) || likes_lava(mon-&gt;data))
&nbsp;        &amp;&amp; (!sobj_at(BOULDER, nx, ny) || throws_rocks(mon-&gt;data)))
&nbsp;        return TRUE;
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
can_reach_location
&nbsp;/* Hack to prevent a dog from being endlessly stuck near an object that
&nbsp; * it can't reach, such as caught in a teleport scroll niche.  It recursively
&nbsp; * checks to see if the squares in between are good.  The checking could be
&nbsp; * a little smarter; a full check would probably be useful in m_move() too.
&nbsp; * Since the maximum food distance is 5, this should never be more than 5
&nbsp; * calls deep.
&nbsp; */
&nbsp;STATIC_OVL boolean
&nbsp;can_reach_location(mon, mx, my, fx, fy)
&nbsp;struct monst *mon;
&nbsp;xchar mx, my, fx, fy;
&nbsp;{
&nbsp;    int i, j;
&nbsp;    int dist;
&nbsp;
&nbsp;    if (mx == fx &amp;&amp; my == fy)
&nbsp;        return TRUE;
&nbsp;    if (!isok(mx, my))
&nbsp;        return FALSE; /* should not happen */
&nbsp;
&nbsp;    dist = dist2(mx, my, fx, fy);
&nbsp;    for (i = mx - 1; i &lt;= mx + 1; i++) {
&nbsp;        for (j = my - 1; j &lt;= my + 1; j++) {
&nbsp;            if (!isok(i, j))
&nbsp;                continue;
&nbsp;            if (dist2(i, j, fx, fy) &gt;= dist)
&nbsp;                continue;
&nbsp;            if (IS_ROCK(levl[i][j].typ) &amp;&amp; !passes_walls(mon-&gt;data)
&nbsp;                &amp;&amp; (!may_dig(i, j) || !tunnels(mon-&gt;data)))
&nbsp;                continue;
&nbsp;            if (IS_DOOR(levl[i][j].typ)
&nbsp;                &amp;&amp; (levl[i][j].doormask &amp; (D_CLOSED | D_LOCKED)))
&nbsp;                continue;
&nbsp;            if (!could_reach_item(mon, i, j))
&nbsp;                continue;
&nbsp;            if (can_reach_location(mon, i, j, fx, fy))
&nbsp;                return TRUE;
&nbsp;        }
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
wantdoor
&nbsp;/* do_clear_area client */
&nbsp;STATIC_PTR void
&nbsp;wantdoor(x, y, distance)
&nbsp;int x, y;
&nbsp;genericptr_t distance;
&nbsp;{
&nbsp;    int ndist, *dist_ptr = (int *) distance;
&nbsp;
&nbsp;    if (*dist_ptr &gt; (ndist = distu(x, y))) {
&nbsp;        gx = x;
&nbsp;        gy = y;
&nbsp;        *dist_ptr = ndist;
&nbsp;    }
&nbsp;}
&nbsp;
&nbsp;static struct qmchoices {
&nbsp;    int mndx;             /* type of pet, 0 means any  */
&nbsp;    char mlet;            /* symbol of pet, 0 means any */
&nbsp;    unsigned mappearance; /* mimic this */
&nbsp;    uchar m_ap_type;      /* what is the thing it is mimicing? */
&nbsp;} qm[] = {
&nbsp;    /* Things that some pets might be thinking about at the time */
&nbsp;    { PM_LITTLE_DOG, 0, PM_KITTEN, M_AP_MONSTER },
&nbsp;    { PM_DOG, 0, PM_HOUSECAT, M_AP_MONSTER },
&nbsp;    { PM_LARGE_DOG, 0, PM_LARGE_CAT, M_AP_MONSTER },
&nbsp;    { PM_KITTEN, 0, PM_LITTLE_DOG, M_AP_MONSTER },
&nbsp;    { PM_HOUSECAT, 0, PM_DOG, M_AP_MONSTER },
&nbsp;    { PM_LARGE_CAT, 0, PM_LARGE_DOG, M_AP_MONSTER },
&nbsp;    { PM_HOUSECAT, 0, PM_GIANT_RAT, M_AP_MONSTER },
&nbsp;    { 0, S_DOG, SINK,
&nbsp;      M_AP_FURNITURE }, /* sorry, no fire hydrants in NetHack */
&nbsp;    { 0, 0, TRIPE_RATION, M_AP_OBJECT }, /* leave this at end */
&nbsp;};
&nbsp;
finish_meating
&nbsp;void
&nbsp;finish_meating(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    mtmp-&gt;meating = 0;
&nbsp;    if (mtmp-&gt;m_ap_type &amp;&amp; mtmp-&gt;mappearance &amp;&amp; mtmp-&gt;cham == NON_PM) {
&nbsp;        /* was eating a mimic and now appearance needs resetting */
&nbsp;        mtmp-&gt;m_ap_type = 0;
&nbsp;        mtmp-&gt;mappearance = 0;
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;    }
&nbsp;}
&nbsp;
quickmimic
&nbsp;STATIC_OVL void
&nbsp;quickmimic(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    int idx = 0, trycnt = 5, spotted;
&nbsp;    char buf[BUFSZ];
&nbsp;
&nbsp;    if (Protection_from_shape_changers || !mtmp-&gt;meating)
&nbsp;        return;
&nbsp;
&nbsp;    do {
&nbsp;        idx = rn2(SIZE(qm));
&nbsp;        if (qm[idx].mndx != 0 &amp;&amp; monsndx(mtmp-&gt;data) == qm[idx].mndx)
&nbsp;            break;
&nbsp;        if (qm[idx].mlet != 0 &amp;&amp; mtmp-&gt;data-&gt;mlet == qm[idx].mlet)
&nbsp;            break;
&nbsp;        if (qm[idx].mndx == 0 &amp;&amp; qm[idx].mlet == 0)
&nbsp;            break;
&nbsp;    } while (--trycnt &gt; 0);
&nbsp;    if (trycnt == 0)
&nbsp;        idx = SIZE(qm) - 1;
&nbsp;
&nbsp;    Strcpy(buf, mon_nam(mtmp));
&nbsp;    spotted = canspotmon(mtmp);
&nbsp;
&nbsp;    mtmp-&gt;m_ap_type = qm[idx].m_ap_type;
&nbsp;    mtmp-&gt;mappearance = qm[idx].mappearance;
&nbsp;
&nbsp;    if (spotted || cansee(mtmp-&gt;mx, mtmp-&gt;my) || canspotmon(mtmp)) {
&nbsp;        /* this isn't quite right; if sensing a monster without being
&nbsp;           able to see its location, you really shouldn't be told you
&nbsp;           sense it becoming furniture or an object that you can't see
&nbsp;           (on the other hand, perhaps you're sensing a brief glimpse
&nbsp;           of its mind as it changes form) */
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        You("%s %s %sappear%s where %s was!",
&nbsp;            cansee(mtmp-&gt;mx, mtmp-&gt;my) ? "see" : "sense that",
&nbsp;            (mtmp-&gt;m_ap_type == M_AP_FURNITURE)
&nbsp;                ? an(defsyms[mtmp-&gt;mappearance].explanation)
&nbsp;                : (mtmp-&gt;m_ap_type == M_AP_OBJECT
&nbsp;                   &amp;&amp; OBJ_DESCR(objects[mtmp-&gt;mappearance]))
&nbsp;                      ? an(OBJ_DESCR(objects[mtmp-&gt;mappearance]))
&nbsp;                      : (mtmp-&gt;m_ap_type == M_AP_OBJECT
&nbsp;                         &amp;&amp; OBJ_NAME(objects[mtmp-&gt;mappearance]))
&nbsp;                            ? an(OBJ_NAME(objects[mtmp-&gt;mappearance]))
&nbsp;                            : (mtmp-&gt;m_ap_type == M_AP_MONSTER)
&nbsp;                                  ? an(mons[mtmp-&gt;mappearance].mname)
&nbsp;                                  : something,
&nbsp;            cansee(mtmp-&gt;mx, mtmp-&gt;my) ? "" : "has ",
&nbsp;            cansee(mtmp-&gt;mx, mtmp-&gt;my) ? "" : "ed",
&nbsp;            buf);
&nbsp;        display_nhwindow(WIN_MAP, TRUE);
&nbsp;    }
&nbsp;}
&nbsp;
&nbsp;/*dogmove.c*/

</x,y></mx,my>