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

Contents

1 Top of file
2 wrong_elem_type
3 m_initgrp
4 m_initthrow
5 m_initweap
6 mkmonmoney
7 m_initinv
8 clone_mon
9 propagate
10 monhp_per_lvl
11 newmonhp
12 newmextra
13 makemon_rnd_goodpos
14 makemon
15 mbirth_limit
16 create_critters
17 uncommon
18 align_shift
19 rndmonst
20 reset_rndmonst
21 mk_gen_ok
22 mkclass
23 mkclass_poly
24 adj_lev
25 grow_up
26 mongets
27 golemhp
28 peace_minded
29 set_malign
30 newmcorpsenm
31 freemcorpsenm
32 set_mimic_sym
33 bagotricks

Top of file
&nbsp;/* NetHack 3.6	makemon.c	$NHDT-Date: 1495237801 2017/05/19 23:50:01 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.116 $ */
&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 

<ctype.h>
&nbsp;
&nbsp;/* this assumes that a human quest leader or nemesis is an archetype
&nbsp;   of the corresponding role; that isn't so for some roles (tourist
&nbsp;   for instance) but is for the priests and monks we use it for... */
&nbsp;#define quest_mon_represents_role(mptr, role_pm) \
&nbsp;    (mptr-&gt;mlet == S_HUMAN &amp;&amp; Role_if(role_pm)   \
&nbsp;     &amp;&amp; (mptr-&gt;msound == MS_LEADER || mptr-&gt;msound == MS_NEMESIS))
&nbsp;
&nbsp;STATIC_DCL boolean FDECL(uncommon, (int));
&nbsp;STATIC_DCL int FDECL(align_shift, (struct permonst *));
&nbsp;STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
&nbsp;STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
&nbsp;STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int));
&nbsp;STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
&nbsp;STATIC_DCL void FDECL(m_initweap, (struct monst *));
&nbsp;STATIC_DCL void FDECL(m_initinv, (struct monst *));
&nbsp;STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *,
&nbsp;                                               unsigned, coord *));
&nbsp;
&nbsp;extern const int monstr[];
&nbsp;
&nbsp;#define m_initsgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 3)
&nbsp;#define m_initlgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 10)
&nbsp;#define toostrong(monindx, lev) (monstr[monindx] &gt; lev)
&nbsp;#define tooweak(monindx, lev) (monstr[monindx] &lt; lev)
&nbsp;
&nbsp;boolean
&nbsp;is_home_elemental(ptr)
&nbsp;struct permonst *ptr;
&nbsp;{
&nbsp;    if (ptr-&gt;mlet == S_ELEMENTAL)
&nbsp;        switch (monsndx(ptr)) {
&nbsp;        case PM_AIR_ELEMENTAL:
&nbsp;            return Is_airlevel(&amp;u.uz);
&nbsp;        case PM_FIRE_ELEMENTAL:
&nbsp;            return Is_firelevel(&amp;u.uz);
&nbsp;        case PM_EARTH_ELEMENTAL:
&nbsp;            return Is_earthlevel(&amp;u.uz);
&nbsp;        case PM_WATER_ELEMENTAL:
&nbsp;            return Is_waterlevel(&amp;u.uz);
&nbsp;        }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
wrong_elem_type
&nbsp;/*
&nbsp; * Return true if the given monster cannot exist on this elemental level.
&nbsp; */
&nbsp;STATIC_OVL boolean
&nbsp;wrong_elem_type(ptr)
&nbsp;struct permonst *ptr;
&nbsp;{
&nbsp;    if (ptr-&gt;mlet == S_ELEMENTAL) {
&nbsp;        return (boolean) !is_home_elemental(ptr);
&nbsp;    } else if (Is_earthlevel(&amp;u.uz)) {
&nbsp;        /* no restrictions? */
&nbsp;    } else if (Is_waterlevel(&amp;u.uz)) {
&nbsp;        /* just monsters that can swim */
&nbsp;        if (!is_swimmer(ptr))
&nbsp;            return TRUE;
&nbsp;    } else if (Is_firelevel(&amp;u.uz)) {
&nbsp;        if (!pm_resistance(ptr, MR_FIRE))
&nbsp;            return TRUE;
&nbsp;    } else if (Is_airlevel(&amp;u.uz)) {
&nbsp;        if (!(is_flyer(ptr) &amp;&amp; ptr-&gt;mlet != S_TRAPPER) &amp;&amp; !is_floater(ptr)
&nbsp;            &amp;&amp; !amorphous(ptr) &amp;&amp; !noncorporeal(ptr) &amp;&amp; !is_whirly(ptr))
&nbsp;            return TRUE;
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
m_initgrp
&nbsp;/* make a group just like mtmp */
&nbsp;STATIC_OVL void
&nbsp;m_initgrp(mtmp, x, y, n)
&nbsp;register struct monst *mtmp;
&nbsp;register int x, y, n;
&nbsp;{
&nbsp;    coord mm;
&nbsp;    register int cnt = rnd(n);
&nbsp;    struct monst *mon;
&nbsp;#if defined(__GNUC__) &amp;&amp; (defined(HPUX) || defined(DGUX))
&nbsp;    /* There is an unresolved problem with several people finding that
&nbsp;     * the game hangs eating CPU; if interrupted and restored, the level
&nbsp;     * will be filled with monsters.  Of those reports giving system type,
&nbsp;     * there were two DG/UX and two HP-UX, all using gcc as the compiler.
&nbsp;     * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
&nbsp;     * problem went away for him and another reporter-to-newsgroup
&nbsp;     * after adding this debugging code.  This has almost got to be a
&nbsp;     * compiler bug, but until somebody tracks it down and gets it fixed,
&nbsp;     * might as well go with the "but it went away when I tried to find
&nbsp;     * it" code.
&nbsp;     */
&nbsp;    int cnttmp, cntdiv;
&nbsp;
&nbsp;    cnttmp = cnt;
&nbsp;    debugpline4("init group call &lt;%d,%d&gt;, n=%d, cnt=%d.", x, y, n, cnt);
&nbsp;    cntdiv = ((u.ulevel &lt; 3) ? 4 : (u.ulevel &lt; 5) ? 2 : 1);
&nbsp;#endif
&nbsp;    /* Tuning: cut down on swarming at low character levels [mrs] */
&nbsp;    cnt /= (u.ulevel &lt; 3) ? 4 : (u.ulevel &lt; 5) ? 2 : 1;
&nbsp;#if defined(__GNUC__) &amp;&amp; (defined(HPUX) || defined(DGUX))
&nbsp;    if (cnt != (cnttmp / cntdiv)) {
&nbsp;        pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
&nbsp;              (u.ulevel &lt; 3) ? 4 : (u.ulevel &lt; 5) ? 2 : 1, cnttmp, cntdiv);
&nbsp;    }
&nbsp;#endif
&nbsp;    if (!cnt)
&nbsp;        cnt++;
&nbsp;#if defined(__GNUC__) &amp;&amp; (defined(HPUX) || defined(DGUX))
&nbsp;    if (cnt &lt; 0)
&nbsp;        cnt = 1;
&nbsp;    if (cnt &gt; 10)
&nbsp;        cnt = 10;
&nbsp;#endif
&nbsp;
&nbsp;    mm.x = x;
&nbsp;    mm.y = y;
&nbsp;    while (cnt--) {
&nbsp;        if (peace_minded(mtmp-&gt;data))
&nbsp;            continue;
&nbsp;        /* Don't create groups of peaceful monsters since they'll get
&nbsp;         * in our way.  If the monster has a percentage chance so some
&nbsp;         * are peaceful and some are not, the result will just be a
&nbsp;         * smaller group.
&nbsp;         */
&nbsp;        if (enexto(&amp;mm, mm.x, mm.y, mtmp-&gt;data)) {
&nbsp;            mon = makemon(mtmp-&gt;data, mm.x, mm.y, NO_MM_FLAGS);
&nbsp;            if (mon) {
&nbsp;                mon-&gt;mpeaceful = FALSE;
&nbsp;                mon-&gt;mavenge = 0;
&nbsp;                set_malign(mon);
&nbsp;                /* Undo the second peace_minded() check in makemon(); if the
&nbsp;                 * monster turned out to be peaceful the first time we
&nbsp;                 * didn't create it at all; we don't want a second check.
&nbsp;                 */
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;}
&nbsp;
m_initthrow
&nbsp;STATIC_OVL
&nbsp;void
&nbsp;m_initthrow(mtmp, otyp, oquan)
&nbsp;struct monst *mtmp;
&nbsp;int otyp, oquan;
&nbsp;{
&nbsp;    register struct obj *otmp;
&nbsp;
&nbsp;    otmp = mksobj(otyp, TRUE, FALSE);
&nbsp;    otmp-&gt;quan = (long) rn1(oquan, 3);
&nbsp;    otmp-&gt;owt = weight(otmp);
&nbsp;    if (otyp == ORCISH_ARROW)
&nbsp;        otmp-&gt;opoisoned = TRUE;
&nbsp;    (void) mpickobj(mtmp, otmp);
&nbsp;}
&nbsp;
m_initweap
&nbsp;STATIC_OVL void
&nbsp;m_initweap(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    register struct permonst *ptr = mtmp-&gt;data;
&nbsp;    register int mm = monsndx(ptr);
&nbsp;    struct obj *otmp;
&nbsp;    int bias, spe2, w1, w2;
&nbsp;
&nbsp;    if (Is_rogue_level(&amp;u.uz))
&nbsp;        return;
&nbsp;    /*
&nbsp;     *  First a few special cases:
&nbsp;     *          giants get a boulder to throw sometimes
&nbsp;     *          ettins get clubs
&nbsp;     *          kobolds get darts to throw
&nbsp;     *          centaurs get some sort of bow &amp; arrows or bolts
&nbsp;     *          soldiers get all sorts of things
&nbsp;     *          kops get clubs &amp; cream pies.
&nbsp;     */
&nbsp;    switch (ptr-&gt;mlet) {
&nbsp;    case S_GIANT:
&nbsp;        if (rn2(2))
&nbsp;            (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
&nbsp;        break;
&nbsp;    case S_HUMAN:
&nbsp;        if (is_mercenary(ptr)) {
&nbsp;            w1 = w2 = 0;
&nbsp;            switch (mm) {
&nbsp;            case PM_WATCHMAN:
&nbsp;            case PM_SOLDIER:
&nbsp;                if (!rn2(3)) {
&nbsp;                    w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
&nbsp;                    w2 = rn2(2) ? DAGGER : KNIFE;
&nbsp;                } else
&nbsp;                    w1 = rn2(2) ? SPEAR : SHORT_SWORD;
&nbsp;                break;
&nbsp;            case PM_SERGEANT:
&nbsp;                w1 = rn2(2) ? FLAIL : MACE;
&nbsp;                break;
&nbsp;            case PM_LIEUTENANT:
&nbsp;                w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
&nbsp;                break;
&nbsp;            case PM_CAPTAIN:
&nbsp;            case PM_WATCH_CAPTAIN:
&nbsp;                w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
&nbsp;                break;
&nbsp;            default:
&nbsp;                if (!rn2(4))
&nbsp;                    w1 = DAGGER;
&nbsp;                if (!rn2(7))
&nbsp;                    w2 = SPEAR;
&nbsp;                break;
&nbsp;            }
&nbsp;            if (w1)
&nbsp;                (void) mongets(mtmp, w1);
&nbsp;            if (!w2 &amp;&amp; w1 != DAGGER &amp;&amp; !rn2(4))
&nbsp;                w2 = KNIFE;
&nbsp;            if (w2)
&nbsp;                (void) mongets(mtmp, w2);
&nbsp;        } else if (is_elf(ptr)) {
&nbsp;            if (rn2(2))
&nbsp;                (void) mongets(mtmp,
&nbsp;                               rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
&nbsp;            if (rn2(2))
&nbsp;                (void) mongets(mtmp, ELVEN_LEATHER_HELM);
&nbsp;            else if (!rn2(4))
&nbsp;                (void) mongets(mtmp, ELVEN_BOOTS);
&nbsp;            if (rn2(2))
&nbsp;                (void) mongets(mtmp, ELVEN_DAGGER);
&nbsp;            switch (rn2(3)) {
&nbsp;            case 0:
&nbsp;                if (!rn2(4))
&nbsp;                    (void) mongets(mtmp, ELVEN_SHIELD);
&nbsp;                if (rn2(3))
&nbsp;                    (void) mongets(mtmp, ELVEN_SHORT_SWORD);
&nbsp;                (void) mongets(mtmp, ELVEN_BOW);
&nbsp;                m_initthrow(mtmp, ELVEN_ARROW, 12);
&nbsp;                break;
&nbsp;            case 1:
&nbsp;                (void) mongets(mtmp, ELVEN_BROADSWORD);
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, ELVEN_SHIELD);
&nbsp;                break;
&nbsp;            case 2:
&nbsp;                if (rn2(2)) {
&nbsp;                    (void) mongets(mtmp, ELVEN_SPEAR);
&nbsp;                    (void) mongets(mtmp, ELVEN_SHIELD);
&nbsp;                }
&nbsp;                break;
&nbsp;            }
&nbsp;            if (mm == PM_ELVENKING) {
&nbsp;                if (rn2(3) || (in_mklev &amp;&amp; Is_earthlevel(&amp;u.uz)))
&nbsp;                    (void) mongets(mtmp, PICK_AXE);
&nbsp;                if (!rn2(50))
&nbsp;                    (void) mongets(mtmp, CRYSTAL_BALL);
&nbsp;            }
&nbsp;        } else if (ptr-&gt;msound == MS_PRIEST
&nbsp;                   || quest_mon_represents_role(ptr, PM_PRIEST)) {
&nbsp;            otmp = mksobj(MACE, FALSE, FALSE);
&nbsp;            if (otmp) {
&nbsp;                otmp-&gt;spe = rnd(3);
&nbsp;                if (!rn2(2))
&nbsp;                    curse(otmp);
&nbsp;                (void) mpickobj(mtmp, otmp);
&nbsp;            }
&nbsp;        } else if (mm == PM_NINJA) { /* extra quest villains */
&nbsp;            (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
&nbsp;            (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
&nbsp;        } else if (ptr-&gt;msound == MS_GUARDIAN) {
&nbsp;            /* quest "guardians" */
&nbsp;            switch (mm) {
&nbsp;            case PM_STUDENT:
&nbsp;            case PM_ATTENDANT:
&nbsp;            case PM_ABBOT:
&nbsp;            case PM_ACOLYTE:
&nbsp;            case PM_GUIDE:
&nbsp;            case PM_APPRENTICE:
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
&nbsp;                if (rn2(5))
&nbsp;                    (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET : LEATHER_CLOAK);
&nbsp;                if (rn2(3))
&nbsp;                    (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
&nbsp;                if (rn2(3))
&nbsp;                    (void) mongets(mtmp, POT_HEALING);
&nbsp;                break;
&nbsp;            case PM_CHIEFTAIN:
&nbsp;            case PM_PAGE:
&nbsp;            case PM_ROSHI:
&nbsp;            case PM_WARRIOR:
&nbsp;                (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
&nbsp;                (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
&nbsp;                if (!rn2(3))
&nbsp;                    (void) mongets(mtmp, LEATHER_CLOAK);
&nbsp;                if (!rn2(3)) {
&nbsp;                    (void) mongets(mtmp, BOW);
&nbsp;                    m_initthrow(mtmp, ARROW, 12);
&nbsp;                }
&nbsp;                break;
&nbsp;            case PM_HUNTER:
&nbsp;                (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
&nbsp;                (void) mongets(mtmp, BOW);
&nbsp;                m_initthrow(mtmp, ARROW, 12);
&nbsp;                break;
&nbsp;            case PM_THUG:
&nbsp;                (void) mongets(mtmp, CLUB);
&nbsp;                (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, LEATHER_GLOVES);
&nbsp;                (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
&nbsp;                break;
&nbsp;            case PM_NEANDERTHAL:
&nbsp;                (void) mongets(mtmp, CLUB);
&nbsp;                (void) mongets(mtmp, LEATHER_ARMOR);
&nbsp;                break;
&nbsp;            }
&nbsp;        }
&nbsp;        break;
&nbsp;
&nbsp;    case S_ANGEL:
&nbsp;        if (humanoid(ptr)) {
&nbsp;            /* create minion stuff; can't use mongets */
&nbsp;            otmp = mksobj(LONG_SWORD, FALSE, FALSE);
&nbsp;
&nbsp;            /* maybe make it special */
&nbsp;            if (!rn2(20) || is_lord(ptr))
&nbsp;                otmp = oname(otmp,
&nbsp;                             artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
&nbsp;            bless(otmp);
&nbsp;            otmp-&gt;oerodeproof = TRUE;
&nbsp;            spe2 = rn2(4);
&nbsp;            otmp-&gt;spe = max(otmp-&gt;spe, spe2);
&nbsp;            (void) mpickobj(mtmp, otmp);
&nbsp;
&nbsp;            otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
&nbsp;                                                  : LARGE_SHIELD,
&nbsp;                          FALSE, FALSE);
&nbsp;            otmp-&gt;cursed = FALSE;
&nbsp;            otmp-&gt;oerodeproof = TRUE;
&nbsp;            otmp-&gt;spe = 0;
&nbsp;            (void) mpickobj(mtmp, otmp);
&nbsp;        }
&nbsp;        break;
&nbsp;
&nbsp;    case S_HUMANOID:
&nbsp;        if (mm == PM_HOBBIT) {
&nbsp;            switch (rn2(3)) {
&nbsp;            case 0:
&nbsp;                (void) mongets(mtmp, DAGGER);
&nbsp;                break;
&nbsp;            case 1:
&nbsp;                (void) mongets(mtmp, ELVEN_DAGGER);
&nbsp;                break;
&nbsp;            case 2:
&nbsp;                (void) mongets(mtmp, SLING);
&nbsp;                break;
&nbsp;            }
&nbsp;            if (!rn2(10))
&nbsp;                (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
&nbsp;            if (!rn2(10))
&nbsp;                (void) mongets(mtmp, DWARVISH_CLOAK);
&nbsp;        } else if (is_dwarf(ptr)) {
&nbsp;            if (rn2(7))
&nbsp;                (void) mongets(mtmp, DWARVISH_CLOAK);
&nbsp;            if (rn2(7))
&nbsp;                (void) mongets(mtmp, IRON_SHOES);
&nbsp;            if (!rn2(4)) {
&nbsp;                (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
&nbsp;                /* note: you can't use a mattock with a shield */
&nbsp;                if (rn2(2))
&nbsp;                    (void) mongets(mtmp, DWARVISH_MATTOCK);
&nbsp;                else {
&nbsp;                    (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
&nbsp;                    (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
&nbsp;                }
&nbsp;                (void) mongets(mtmp, DWARVISH_IRON_HELM);
&nbsp;                if (!rn2(3))
&nbsp;                    (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
&nbsp;            } else {
&nbsp;                (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
&nbsp;            }
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_KOP:
&nbsp;        /* create Keystone Kops with cream pies to
&nbsp;           throw. As suggested by KAA.     [MRS] */
&nbsp;        if (!rn2(4))
&nbsp;            m_initthrow(mtmp, CREAM_PIE, 2);
&nbsp;        if (!rn2(3))
&nbsp;            (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
&nbsp;        break;
&nbsp;    case S_ORC:
&nbsp;        if (rn2(2))
&nbsp;            (void) mongets(mtmp, ORCISH_HELM);
&nbsp;        switch ((mm != PM_ORC_CAPTAIN) ? mm
&nbsp;                : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
&nbsp;        case PM_MORDOR_ORC:
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, SCIMITAR);
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, ORCISH_SHIELD);
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, KNIFE);
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
&nbsp;            break;
&nbsp;        case PM_URUK_HAI:
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, ORCISH_CLOAK);
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, ORCISH_SHORT_SWORD);
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, IRON_SHOES);
&nbsp;            if (!rn2(3)) {
&nbsp;                (void) mongets(mtmp, ORCISH_BOW);
&nbsp;                m_initthrow(mtmp, ORCISH_ARROW, 12);
&nbsp;            }
&nbsp;            if (!rn2(3))
&nbsp;                (void) mongets(mtmp, URUK_HAI_SHIELD);
&nbsp;            break;
&nbsp;        default:
&nbsp;            if (mm != PM_ORC_SHAMAN &amp;&amp; rn2(2))
&nbsp;                (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
&nbsp;                                         ? ORCISH_DAGGER
&nbsp;                                         : SCIMITAR);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_OGRE:
&nbsp;        if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
&nbsp;            (void) mongets(mtmp, BATTLE_AXE);
&nbsp;        else
&nbsp;            (void) mongets(mtmp, CLUB);
&nbsp;        break;
&nbsp;    case S_TROLL:
&nbsp;        if (!rn2(2))
&nbsp;            switch (rn2(4)) {
&nbsp;            case 0:
&nbsp;                (void) mongets(mtmp, RANSEUR);
&nbsp;                break;
&nbsp;            case 1:
&nbsp;                (void) mongets(mtmp, PARTISAN);
&nbsp;                break;
&nbsp;            case 2:
&nbsp;                (void) mongets(mtmp, GLAIVE);
&nbsp;                break;
&nbsp;            case 3:
&nbsp;                (void) mongets(mtmp, SPETUM);
&nbsp;                break;
&nbsp;            }
&nbsp;        break;
&nbsp;    case S_KOBOLD:
&nbsp;        if (!rn2(4))
&nbsp;            m_initthrow(mtmp, DART, 12);
&nbsp;        break;
&nbsp;
&nbsp;    case S_CENTAUR:
&nbsp;        if (rn2(2)) {
&nbsp;            if (ptr == &amp;mons[PM_FOREST_CENTAUR]) {
&nbsp;                (void) mongets(mtmp, BOW);
&nbsp;                m_initthrow(mtmp, ARROW, 12);
&nbsp;            } else {
&nbsp;                (void) mongets(mtmp, CROSSBOW);
&nbsp;                m_initthrow(mtmp, CROSSBOW_BOLT, 12);
&nbsp;            }
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_WRAITH:
&nbsp;        (void) mongets(mtmp, KNIFE);
&nbsp;        (void) mongets(mtmp, LONG_SWORD);
&nbsp;        break;
&nbsp;    case S_ZOMBIE:
&nbsp;        if (!rn2(4))
&nbsp;            (void) mongets(mtmp, LEATHER_ARMOR);
&nbsp;        if (!rn2(4))
&nbsp;            (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
&nbsp;        break;
&nbsp;    case S_LIZARD:
&nbsp;        if (mm == PM_SALAMANDER)
&nbsp;            (void) mongets(mtmp,
&nbsp;                           (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
&nbsp;        break;
&nbsp;    case S_DEMON:
&nbsp;        switch (mm) {
&nbsp;        case PM_BALROG:
&nbsp;            (void) mongets(mtmp, BULLWHIP);
&nbsp;            (void) mongets(mtmp, BROADSWORD);
&nbsp;            break;
&nbsp;        case PM_ORCUS:
&nbsp;            (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
&nbsp;            break;
&nbsp;        case PM_HORNED_DEVIL:
&nbsp;            (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
&nbsp;            break;
&nbsp;        case PM_DISPATER:
&nbsp;            (void) mongets(mtmp, WAN_STRIKING);
&nbsp;            break;
&nbsp;        case PM_YEENOGHU:
&nbsp;            (void) mongets(mtmp, FLAIL);
&nbsp;            break;
&nbsp;        }
&nbsp;        /* prevent djinn and mail daemons from leaving objects when
&nbsp;         * they vanish
&nbsp;         */
&nbsp;        if (!is_demon(ptr))
&nbsp;            break;
&nbsp;        /*FALLTHRU*/
&nbsp;    default:
&nbsp;        /*
&nbsp;         * Now the general case, some chance of getting some type
&nbsp;         * of weapon for "normal" monsters.  Certain special types
&nbsp;         * of monsters will get a bonus chance or different selections.
&nbsp;         */
&nbsp;        bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
&nbsp;        switch (rnd(14 - (2 * bias))) {
&nbsp;        case 1:
&nbsp;            if (strongmonst(ptr))
&nbsp;                (void) mongets(mtmp, BATTLE_AXE);
&nbsp;            else
&nbsp;                m_initthrow(mtmp, DART, 12);
&nbsp;            break;
&nbsp;        case 2:
&nbsp;            if (strongmonst(ptr))
&nbsp;                (void) mongets(mtmp, TWO_HANDED_SWORD);
&nbsp;            else {
&nbsp;                (void) mongets(mtmp, CROSSBOW);
&nbsp;                m_initthrow(mtmp, CROSSBOW_BOLT, 12);
&nbsp;            }
&nbsp;            break;
&nbsp;        case 3:
&nbsp;            (void) mongets(mtmp, BOW);
&nbsp;            m_initthrow(mtmp, ARROW, 12);
&nbsp;            break;
&nbsp;        case 4:
&nbsp;            if (strongmonst(ptr))
&nbsp;                (void) mongets(mtmp, LONG_SWORD);
&nbsp;            else
&nbsp;                m_initthrow(mtmp, DAGGER, 3);
&nbsp;            break;
&nbsp;        case 5:
&nbsp;            if (strongmonst(ptr))
&nbsp;                (void) mongets(mtmp, LUCERN_HAMMER);
&nbsp;            else
&nbsp;                (void) mongets(mtmp, AKLYS);
&nbsp;            break;
&nbsp;        default:
&nbsp;            break;
&nbsp;        }
&nbsp;        break;
&nbsp;    }
&nbsp;
&nbsp;    if ((int) mtmp-&gt;m_lev &gt; rn2(75))
&nbsp;        (void) mongets(mtmp, rnd_offensive_item(mtmp));
&nbsp;}
&nbsp;
mkmonmoney
&nbsp;/*
&nbsp; *   Makes up money for monster's inventory.
&nbsp; *   This will change with silver &amp; copper coins
&nbsp; */
&nbsp;void
&nbsp;mkmonmoney(mtmp, amount)
&nbsp;struct monst *mtmp;
&nbsp;long amount;
&nbsp;{
&nbsp;    struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
&nbsp;
&nbsp;    gold-&gt;quan = amount;
&nbsp;    add_to_minv(mtmp, gold);
&nbsp;}
&nbsp;
m_initinv
&nbsp;STATIC_OVL void
&nbsp;m_initinv(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    register int cnt;
&nbsp;    register struct obj *otmp;
&nbsp;    register struct permonst *ptr = mtmp-&gt;data;
&nbsp;
&nbsp;    if (Is_rogue_level(&amp;u.uz))
&nbsp;        return;
&nbsp;    /*
&nbsp;     *  Soldiers get armour &amp; rations - armour approximates their ac.
&nbsp;     *  Nymphs may get mirror or potion of object detection.
&nbsp;     */
&nbsp;    switch (ptr-&gt;mlet) {
&nbsp;    case S_HUMAN:
&nbsp;        if (is_mercenary(ptr)) {
&nbsp;            register int mac;
&nbsp;
&nbsp;            switch (monsndx(ptr)) {
&nbsp;            case PM_GUARD:
&nbsp;                mac = -1;
&nbsp;                break;
&nbsp;            case PM_SOLDIER:
&nbsp;                mac = 3;
&nbsp;                break;
&nbsp;            case PM_SERGEANT:
&nbsp;                mac = 0;
&nbsp;                break;
&nbsp;            case PM_LIEUTENANT:
&nbsp;                mac = -2;
&nbsp;                break;
&nbsp;            case PM_CAPTAIN:
&nbsp;                mac = -3;
&nbsp;                break;
&nbsp;            case PM_WATCHMAN:
&nbsp;                mac = 3;
&nbsp;                break;
&nbsp;            case PM_WATCH_CAPTAIN:
&nbsp;                mac = -2;
&nbsp;                break;
&nbsp;            default:
&nbsp;                impossible("odd mercenary %d?", monsndx(ptr));
&nbsp;                mac = 0;
&nbsp;                break;
&nbsp;            }
&nbsp;
&nbsp;            if (mac &lt; -1 &amp;&amp; rn2(5))
&nbsp;                mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
&nbsp;                                                  : CRYSTAL_PLATE_MAIL);
&nbsp;            else if (mac &lt; 3 &amp;&amp; rn2(5))
&nbsp;                mac +=
&nbsp;                    6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
&nbsp;            else if (rn2(5))
&nbsp;                mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
&nbsp;                                                  : STUDDED_LEATHER_ARMOR);
&nbsp;            else
&nbsp;                mac += 2 + mongets(mtmp, LEATHER_ARMOR);
&nbsp;
&nbsp;            if (mac &lt; 10 &amp;&amp; rn2(3))
&nbsp;                mac += 1 + mongets(mtmp, HELMET);
&nbsp;            else if (mac &lt; 10 &amp;&amp; rn2(2))
&nbsp;                mac += 1 + mongets(mtmp, DENTED_POT);
&nbsp;            if (mac &lt; 10 &amp;&amp; rn2(3))
&nbsp;                mac += 1 + mongets(mtmp, SMALL_SHIELD);
&nbsp;            else if (mac &lt; 10 &amp;&amp; rn2(2))
&nbsp;                mac += 2 + mongets(mtmp, LARGE_SHIELD);
&nbsp;            if (mac &lt; 10 &amp;&amp; rn2(3))
&nbsp;                mac += 1 + mongets(mtmp, LOW_BOOTS);
&nbsp;            else if (mac &lt; 10 &amp;&amp; rn2(2))
&nbsp;                mac += 2 + mongets(mtmp, HIGH_BOOTS);
&nbsp;            if (mac &lt; 10 &amp;&amp; rn2(3))
&nbsp;                mac += 1 + mongets(mtmp, LEATHER_GLOVES);
&nbsp;            else if (mac &lt; 10 &amp;&amp; rn2(2))
&nbsp;                mac += 1 + mongets(mtmp, LEATHER_CLOAK);
&nbsp;
&nbsp;            nhUse(mac); /* suppress 'dead increment' from static analyzer */
&nbsp;
&nbsp;            if (ptr != &amp;mons[PM_GUARD] &amp;&amp; ptr != &amp;mons[PM_WATCHMAN]
&nbsp;                &amp;&amp; ptr != &amp;mons[PM_WATCH_CAPTAIN]) {
&nbsp;                if (!rn2(3))
&nbsp;                    (void) mongets(mtmp, K_RATION);
&nbsp;                if (!rn2(2))
&nbsp;                    (void) mongets(mtmp, C_RATION);
&nbsp;                if (ptr != &amp;mons[PM_SOLDIER] &amp;&amp; !rn2(3))
&nbsp;                    (void) mongets(mtmp, BUGLE);
&nbsp;            } else if (ptr == &amp;mons[PM_WATCHMAN] &amp;&amp; rn2(3))
&nbsp;                (void) mongets(mtmp, TIN_WHISTLE);
&nbsp;        } else if (ptr == &amp;mons[PM_SHOPKEEPER]) {
&nbsp;            (void) mongets(mtmp, SKELETON_KEY);
&nbsp;            switch (rn2(4)) {
&nbsp;            /* MAJOR fall through ... */
&nbsp;            case 0:
&nbsp;                (void) mongets(mtmp, WAN_MAGIC_MISSILE);
&nbsp;                /*FALLTHRU*/
&nbsp;            case 1:
&nbsp;                (void) mongets(mtmp, POT_EXTRA_HEALING);
&nbsp;                /*FALLTHRU*/
&nbsp;            case 2:
&nbsp;                (void) mongets(mtmp, POT_HEALING);
&nbsp;                /*FALLTHRU*/
&nbsp;            case 3:
&nbsp;                (void) mongets(mtmp, WAN_STRIKING);
&nbsp;            }
&nbsp;        } else if (ptr-&gt;msound == MS_PRIEST
&nbsp;                   || quest_mon_represents_role(ptr, PM_PRIEST)) {
&nbsp;            (void) mongets(mtmp, rn2(7) ? ROBE
&nbsp;                                        : rn2(3) ? CLOAK_OF_PROTECTION
&nbsp;                                                 : CLOAK_OF_MAGIC_RESISTANCE);
&nbsp;            (void) mongets(mtmp, SMALL_SHIELD);
&nbsp;            mkmonmoney(mtmp, (long) rn1(10, 20));
&nbsp;        } else if (quest_mon_represents_role(ptr, PM_MONK)) {
&nbsp;            (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_NYMPH:
&nbsp;        if (!rn2(2))
&nbsp;            (void) mongets(mtmp, MIRROR);
&nbsp;        if (!rn2(2))
&nbsp;            (void) mongets(mtmp, POT_OBJECT_DETECTION);
&nbsp;        break;
&nbsp;    case S_GIANT:
&nbsp;        if (ptr == &amp;mons[PM_MINOTAUR]) {
&nbsp;            if (!rn2(3) || (in_mklev &amp;&amp; Is_earthlevel(&amp;u.uz)))
&nbsp;                (void) mongets(mtmp, WAN_DIGGING);
&nbsp;        } else if (is_giant(ptr)) {
&nbsp;            for (cnt = rn2((int) (mtmp-&gt;m_lev / 2)); cnt; cnt--) {
&nbsp;                otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
&nbsp;                              FALSE, FALSE);
&nbsp;                otmp-&gt;quan = (long) rn1(2, 3);
&nbsp;                otmp-&gt;owt = weight(otmp);
&nbsp;                (void) mpickobj(mtmp, otmp);
&nbsp;            }
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_WRAITH:
&nbsp;        if (ptr == &amp;mons[PM_NAZGUL]) {
&nbsp;            otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
&nbsp;            curse(otmp);
&nbsp;            (void) mpickobj(mtmp, otmp);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_LICH:
&nbsp;        if (ptr == &amp;mons[PM_MASTER_LICH] &amp;&amp; !rn2(13))
&nbsp;            (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
&nbsp;        else if (ptr == &amp;mons[PM_ARCH_LICH] &amp;&amp; !rn2(3)) {
&nbsp;            otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
&nbsp;                          rn2(13) ? FALSE : TRUE);
&nbsp;            if (otmp-&gt;spe &lt; 2)
&nbsp;                otmp-&gt;spe = rnd(3);
&nbsp;            if (!rn2(4))
&nbsp;                otmp-&gt;oerodeproof = 1;
&nbsp;            (void) mpickobj(mtmp, otmp);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_MUMMY:
&nbsp;        if (rn2(7))
&nbsp;            (void) mongets(mtmp, MUMMY_WRAPPING);
&nbsp;        break;
&nbsp;    case S_QUANTMECH:
&nbsp;        if (!rn2(20)) {
&nbsp;            otmp = mksobj(LARGE_BOX, FALSE, FALSE);
&nbsp;            otmp-&gt;spe = 1; /* flag for special box */
&nbsp;            otmp-&gt;owt = weight(otmp);
&nbsp;            (void) mpickobj(mtmp, otmp);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_LEPRECHAUN:
&nbsp;        mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
&nbsp;        break;
&nbsp;    case S_DEMON:
&nbsp;        /* moved here from m_initweap() because these don't
&nbsp;           have AT_WEAP so m_initweap() is not called for them */
&nbsp;        if (ptr == &amp;mons[PM_ICE_DEVIL] &amp;&amp; !rn2(4)) {
&nbsp;            (void) mongets(mtmp, SPEAR);
&nbsp;        } else if (ptr == &amp;mons[PM_ASMODEUS]) {
&nbsp;            (void) mongets(mtmp, WAN_COLD);
&nbsp;            (void) mongets(mtmp, WAN_FIRE);
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_GNOME:
&nbsp;        if (!rn2((In_mines(&amp;u.uz) &amp;&amp; in_mklev) ? 20 : 60)) {
&nbsp;            otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
&nbsp;            otmp-&gt;quan = 1;
&nbsp;            otmp-&gt;owt = weight(otmp);
&nbsp;            if (!mpickobj(mtmp, otmp) &amp;&amp; !levl[mtmp-&gt;mx][mtmp-&gt;my].lit)
&nbsp;                begin_burn(otmp, FALSE);
&nbsp;        }
&nbsp;        break;
&nbsp;    default:
&nbsp;        break;
&nbsp;    }
&nbsp;
&nbsp;    /* ordinary soldiers rarely have access to magic (or gold :-) */
&nbsp;    if (ptr == &amp;mons[PM_SOLDIER] &amp;&amp; rn2(13))
&nbsp;        return;
&nbsp;
&nbsp;    if ((int) mtmp-&gt;m_lev &gt; rn2(50))
&nbsp;        (void) mongets(mtmp, rnd_defensive_item(mtmp));
&nbsp;    if ((int) mtmp-&gt;m_lev &gt; rn2(100))
&nbsp;        (void) mongets(mtmp, rnd_misc_item(mtmp));
&nbsp;    if (likes_gold(ptr) &amp;&amp; !findgold(mtmp-&gt;minvent) &amp;&amp; !rn2(5))
&nbsp;        mkmonmoney(mtmp,
&nbsp;                   (long) d(level_difficulty(), mtmp-&gt;minvent ? 5 : 10));
&nbsp;}
&nbsp;
clone_mon
&nbsp;/* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
&nbsp;struct monst *
&nbsp;clone_mon(mon, x, y)
&nbsp;struct monst *mon;
&nbsp;xchar x, y; /* clone's preferred location or 0 (near mon) */
&nbsp;{
&nbsp;    coord mm;
&nbsp;    struct monst *m2;
&nbsp;
&nbsp;    /* may be too weak or have been extinguished for population control */
&nbsp;    if (mon-&gt;mhp &lt;= 1 || (mvitals[monsndx(mon-&gt;data)].mvflags &amp; G_EXTINCT))
&nbsp;        return (struct monst *) 0;
&nbsp;
&nbsp;    if (x == 0) {
&nbsp;        mm.x = mon-&gt;mx;
&nbsp;        mm.y = mon-&gt;my;
&nbsp;        if (!enexto(&amp;mm, mm.x, mm.y, mon-&gt;data) || MON_AT(mm.x, mm.y))
&nbsp;            return (struct monst *) 0;
&nbsp;    } else if (!isok(x, y)) {
&nbsp;        return (struct monst *) 0; /* paranoia */
&nbsp;    } else {
&nbsp;        mm.x = x;
&nbsp;        mm.y = y;
&nbsp;        if (MON_AT(mm.x, mm.y)) {
&nbsp;            if (!enexto(&amp;mm, mm.x, mm.y, mon-&gt;data) || MON_AT(mm.x, mm.y))
&nbsp;                return (struct monst *) 0;
&nbsp;        }
&nbsp;    }
&nbsp;    m2 = newmonst();
&nbsp;    *m2 = *mon; /* copy condition of old monster */
&nbsp;    m2-&gt;mextra = (struct mextra *) 0;
&nbsp;    m2-&gt;nmon = fmon;
&nbsp;    fmon = m2;
&nbsp;    m2-&gt;m_id = context.ident++;
&nbsp;    if (!m2-&gt;m_id)
&nbsp;        m2-&gt;m_id = context.ident++; /* ident overflowed */
&nbsp;    m2-&gt;mx = mm.x;
&nbsp;    m2-&gt;my = mm.y;
&nbsp;
&nbsp;    m2-&gt;mcloned = 1;
&nbsp;    m2-&gt;minvent = (struct obj *) 0; /* objects don't clone */
&nbsp;    m2-&gt;mleashed = FALSE;
&nbsp;    /* Max HP the same, but current HP halved for both.  The caller
&nbsp;     * might want to override this by halving the max HP also.
&nbsp;     * When current HP is odd, the original keeps the extra point.
&nbsp;     * We know original has more than 1 HP, so both end up with at least 1.
&nbsp;     */
&nbsp;    m2-&gt;mhpmax = mon-&gt;mhpmax;
&nbsp;    m2-&gt;mhp = mon-&gt;mhp / 2;
&nbsp;    mon-&gt;mhp -= m2-&gt;mhp;
&nbsp;
&nbsp;    /* since shopkeepers and guards will only be cloned if they've been
&nbsp;     * polymorphed away from their original forms, the clone doesn't have
&nbsp;     * room for the extra information.  we also don't want two shopkeepers
&nbsp;     * around for the same shop.
&nbsp;     */
&nbsp;    if (mon-&gt;isshk)
&nbsp;        m2-&gt;isshk = FALSE;
&nbsp;    if (mon-&gt;isgd)
&nbsp;        m2-&gt;isgd = FALSE;
&nbsp;    if (mon-&gt;ispriest)
&nbsp;        m2-&gt;ispriest = FALSE;
&nbsp;    place_monster(m2, m2-&gt;mx, m2-&gt;my);
&nbsp;    if (emits_light(m2-&gt;data))
&nbsp;        new_light_source(m2-&gt;mx, m2-&gt;my, emits_light(m2-&gt;data), LS_MONSTER,
&nbsp;                         monst_to_any(m2));
&nbsp;    if (has_mname(mon)) {
&nbsp;        m2 = christen_monst(m2, MNAME(mon));
&nbsp;    } else if (mon-&gt;isshk) {
&nbsp;        m2 = christen_monst(m2, shkname(mon));
&nbsp;    }
&nbsp;
&nbsp;    /* not all clones caused by player are tame or peaceful */
&nbsp;    if (!context.mon_moving) {
&nbsp;        if (mon-&gt;mtame)
&nbsp;            m2-&gt;mtame = rn2(max(2 + u.uluck, 2)) ? mon-&gt;mtame : 0;
&nbsp;        else if (mon-&gt;mpeaceful)
&nbsp;            m2-&gt;mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
&nbsp;    }
&nbsp;
&nbsp;    newsym(m2-&gt;mx, m2-&gt;my); /* display the new monster */
&nbsp;    if (m2-&gt;mtame) {
&nbsp;        if (mon-&gt;isminion) {
&nbsp;            newemin(m2);
&nbsp;            if (EMIN(mon))
&nbsp;                *(EMIN(m2)) = *(EMIN(mon));
&nbsp;        } else {
&nbsp;            /* because m2 is a copy of mon it is tame but not init'ed.
&nbsp;             * however, tamedog will not re-tame a tame dog, so m2
&nbsp;             * must be made non-tame to get initialized properly.
&nbsp;             */
&nbsp;            m2-&gt;mtame = 0;
&nbsp;            if (tamedog(m2, (struct obj *) 0)) {
&nbsp;                *(EDOG(m2)) = *(EDOG(mon));
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    set_malign(m2);
&nbsp;
&nbsp;    return m2;
&nbsp;}
&nbsp;
propagate
&nbsp;/*
&nbsp; * Propagate a species
&nbsp; *
&nbsp; * Once a certain number of monsters are created, don't create any more
&nbsp; * at random (i.e. make them extinct).  The previous (3.2) behavior was
&nbsp; * to do this when a certain number had _died_, which didn't make
&nbsp; * much sense.
&nbsp; *
&nbsp; * Returns FALSE propagation unsuccessful
&nbsp; *         TRUE  propagation successful
&nbsp; */
&nbsp;boolean
&nbsp;propagate(mndx, tally, ghostly)
&nbsp;int mndx;
&nbsp;boolean tally;
&nbsp;boolean ghostly;
&nbsp;{
&nbsp;    boolean result;
&nbsp;    uchar lim = mbirth_limit(mndx);
&nbsp;    boolean gone = (mvitals[mndx].mvflags &amp; G_GONE) != 0; /* geno'd|extinct */
&nbsp;
&nbsp;    result = (((int) mvitals[mndx].born &lt; lim) &amp;&amp; !gone) ? TRUE : FALSE;
&nbsp;
&nbsp;    /* if it's unique, don't ever make it again */
&nbsp;    if ((mons[mndx].geno &amp; G_UNIQ) &amp;&amp; mndx != PM_HIGH_PRIEST)
&nbsp;        mvitals[mndx].mvflags |= G_EXTINCT;
&nbsp;
&nbsp;    if (mvitals[mndx].born &lt; 255 &amp;&amp; tally
&nbsp;        &amp;&amp; (!ghostly || (ghostly &amp;&amp; result)))
&nbsp;        mvitals[mndx].born++;
&nbsp;    if ((int) mvitals[mndx].born &gt;= lim &amp;&amp; !(mons[mndx].geno &amp; G_NOGEN)
&nbsp;        &amp;&amp; !(mvitals[mndx].mvflags &amp; G_EXTINCT)) {
&nbsp;        if (wizard) {
&nbsp;            debugpline1("Automatically extinguished %s.",
&nbsp;                        makeplural(mons[mndx].mname));
&nbsp;        }
&nbsp;        mvitals[mndx].mvflags |= G_EXTINCT;
&nbsp;        reset_rndmonst(mndx);
&nbsp;    }
&nbsp;    return result;
&nbsp;}
&nbsp;
monhp_per_lvl
&nbsp;/* amount of HP to lose from level drain (or gain from Stormbringer) */
&nbsp;int
&nbsp;monhp_per_lvl(mon)
&nbsp;struct monst *mon;
&nbsp;{
&nbsp;    struct permonst *ptr = mon-&gt;data;
&nbsp;    int hp = rnd(8); /* default is d8 */
&nbsp;
&nbsp;    /* like newmonhp, but home elementals are ignored, riders use normal d8 */
&nbsp;    if (is_golem(ptr)) {
&nbsp;        /* draining usually won't be applicable for these critters */
&nbsp;        hp = golemhp(monsndx(ptr)) / (int) ptr-&gt;mlevel;
&nbsp;    } else if (ptr-&gt;mlevel &gt; 49) {
&nbsp;        /* arbitrary; such monsters won't be involved in draining anyway */
&nbsp;        hp = 4 + rnd(4); /* 5..8 */
&nbsp;    } else if (ptr-&gt;mlet == S_DRAGON &amp;&amp; monsndx(ptr) &gt;= PM_GRAY_DRAGON) {
&nbsp;        /* adult dragons; newmonhp() uses In_endgame(&amp;u.uz) ? 8 : 4 + rnd(4)
&nbsp;         */
&nbsp;        hp = 4 + rn2(5); /* 4..8 */
&nbsp;    } else if (!mon-&gt;m_lev) {
&nbsp;        /* level 0 monsters use 1d4 instead of Nd8 */
&nbsp;        hp = rnd(4);
&nbsp;    }
&nbsp;    return hp;
&nbsp;}
&nbsp;
newmonhp
&nbsp;/* set up a new monster's initial level and hit points;
&nbsp;   used by newcham() as well as by makemon() */
&nbsp;void
&nbsp;newmonhp(mon, mndx)
&nbsp;struct monst *mon;
&nbsp;int mndx;
&nbsp;{
&nbsp;    struct permonst *ptr = &amp;mons[mndx];
&nbsp;
&nbsp;    mon-&gt;m_lev = adj_lev(ptr);
&nbsp;    if (is_golem(ptr)) {
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp = golemhp(mndx);
&nbsp;    } else if (is_rider(ptr)) {
&nbsp;        /* we want low HP, but a high mlevel so they can attack well */
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp = d(10, 8);
&nbsp;    } else if (ptr-&gt;mlevel &gt; 49) {
&nbsp;        /* "special" fixed hp monster
&nbsp;         * the hit points are encoded in the mlevel in a somewhat strange
&nbsp;         * way to fit in the 50..127 positive range of a signed character
&nbsp;         * above the 1..49 that indicate "normal" monster levels */
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp = 2 * (ptr-&gt;mlevel - 6);
&nbsp;        mon-&gt;m_lev = mon-&gt;mhp / 4; /* approximation */
&nbsp;    } else if (ptr-&gt;mlet == S_DRAGON &amp;&amp; mndx &gt;= PM_GRAY_DRAGON) {
&nbsp;        /* adult dragons */
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp =
&nbsp;            (int) (In_endgame(&amp;u.uz)
&nbsp;                       ? (8 * mon-&gt;m_lev)
&nbsp;                       : (4 * mon-&gt;m_lev + d((int) mon-&gt;m_lev, 4)));
&nbsp;    } else if (!mon-&gt;m_lev) {
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp = rnd(4);
&nbsp;    } else {
&nbsp;        mon-&gt;mhpmax = mon-&gt;mhp = d((int) mon-&gt;m_lev, 8);
&nbsp;        if (is_home_elemental(ptr))
&nbsp;            mon-&gt;mhpmax = (mon-&gt;mhp *= 3);
&nbsp;    }
&nbsp;}
&nbsp;
newmextra
&nbsp;struct mextra *
&nbsp;newmextra()
&nbsp;{
&nbsp;    struct mextra *mextra;
&nbsp;
&nbsp;    mextra = (struct mextra *) alloc(sizeof(struct mextra));
&nbsp;    mextra-&gt;mname = 0;
&nbsp;    mextra-&gt;egd = 0;
&nbsp;    mextra-&gt;epri = 0;
&nbsp;    mextra-&gt;eshk = 0;
&nbsp;    mextra-&gt;emin = 0;
&nbsp;    mextra-&gt;edog = 0;
&nbsp;    mextra-&gt;mcorpsenm = NON_PM;
&nbsp;    return mextra;
&nbsp;}
&nbsp;
makemon_rnd_goodpos
&nbsp;boolean
&nbsp;makemon_rnd_goodpos(mon, gpflags, cc)
&nbsp;struct monst *mon;
&nbsp;unsigned gpflags;
&nbsp;coord *cc;
&nbsp;{
&nbsp;    int tryct = 0;
&nbsp;    int nx,ny;
&nbsp;    boolean good;
&nbsp;
&nbsp;    do {
&nbsp;        nx = rn1(COLNO - 3, 2);
&nbsp;        ny = rn2(ROWNO);
&nbsp;        good = (!in_mklev &amp;&amp; cansee(nx,ny)) ? FALSE
&nbsp;                                            : goodpos(nx, ny, mon, gpflags);
&nbsp;    } while ((++tryct &lt; 50) &amp;&amp; !good);
&nbsp;
&nbsp;    if (!good) {
&nbsp;        /* else go through all map positions, twice, first round
&nbsp;           ignoring positions in sight, and pick first good one.
&nbsp;           skip first round if we're in special level loader or blind */
&nbsp;        int xofs = nx;
&nbsp;        int yofs = ny;
&nbsp;        int dx,dy;
&nbsp;        int bl = (in_mklev || Blind) ? 1 : 0;
&nbsp;
&nbsp;        for ( ; bl &lt; 2; bl++) {
&nbsp;            for (dx = 0; dx &lt; COLNO; dx++)
&nbsp;                for (dy = 0; dy &lt; ROWNO; dy++) {
&nbsp;                    nx = ((dx + xofs) % (COLNO - 1)) + 1;
&nbsp;                    ny = ((dy + yofs) % (ROWNO - 1)) + 1;
&nbsp;                    if (bl == 0 &amp;&amp; cansee(nx,ny))
&nbsp;                        continue;
&nbsp;                    if (goodpos(nx, ny, mon, gpflags))
&nbsp;                        goto gotgood;
&nbsp;                }
&nbsp;            if (bl == 0 &amp;&amp; (!mon || mon-&gt;data-&gt;mmove)) {
&nbsp;                /* all map positions are visible (or not good),
&nbsp;                   try to pick something logical */
&nbsp;                if (dnstair.sx &amp;&amp; !rn2(2)) {
&nbsp;                    nx = dnstair.sx;
&nbsp;                    ny = dnstair.sy;
&nbsp;                } else if (upstair.sx &amp;&amp; !rn2(2)) {
&nbsp;                    nx = upstair.sx;
&nbsp;                    ny = upstair.sy;
&nbsp;                } else if (dnladder.sx &amp;&amp; !rn2(2)) {
&nbsp;                    nx = dnladder.sx;
&nbsp;                    ny = dnladder.sy;
&nbsp;                } else if (upladder.sx &amp;&amp; !rn2(2)) {
&nbsp;                    nx = upladder.sx;
&nbsp;                    ny = upladder.sy;
&nbsp;                }
&nbsp;                if (goodpos(nx, ny, mon, gpflags))
&nbsp;                    goto gotgood;
&nbsp;            }
&nbsp;        }
&nbsp;    } else {
&nbsp;    gotgood:
&nbsp;        cc-&gt;x = nx;
&nbsp;        cc-&gt;y = ny;
&nbsp;        return TRUE;
&nbsp;    }
&nbsp;    return FALSE;
&nbsp;}
&nbsp;
makemon
&nbsp;/*
&nbsp; * called with [x,y] = coordinates;
&nbsp; *      [0,0] means anyplace
&nbsp; *      [u.ux,u.uy] means: near player (if !in_mklev)
&nbsp; *
&nbsp; *      In case we make a monster group, only return the one at [x,y].
&nbsp; */
&nbsp;struct monst *
&nbsp;makemon(ptr, x, y, mmflags)
&nbsp;register struct permonst *ptr;
&nbsp;register int x, y;
&nbsp;int mmflags;
&nbsp;{
&nbsp;    register struct monst *mtmp;
&nbsp;    int mndx, mcham, ct, mitem;
&nbsp;    boolean anymon = (!ptr);
&nbsp;    boolean byyou = (x == u.ux &amp;&amp; y == u.uy);
&nbsp;    boolean allow_minvent = ((mmflags &amp; NO_MINVENT) == 0);
&nbsp;    boolean countbirth = ((mmflags &amp; MM_NOCOUNTBIRTH) == 0);
&nbsp;    unsigned gpflags = (mmflags &amp; MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
&nbsp;
&nbsp;    /* if caller wants random location, do it here */
&nbsp;    if (x == 0 &amp;&amp; y == 0) {
&nbsp;        coord cc;
&nbsp;        struct monst fakemon;
&nbsp;
&nbsp;        cc.x = cc.y = 0; /* lint suppression */
&nbsp;        fakemon.data = ptr; /* set up for goodpos */
&nbsp;        if (!makemon_rnd_goodpos(ptr ? &amp;fakemon : (struct monst *)0,
&nbsp;                                 gpflags, &amp;cc))
&nbsp;            return (struct monst *) 0;
&nbsp;        x = cc.x;
&nbsp;        y = cc.y;
&nbsp;    } else if (byyou &amp;&amp; !in_mklev) {
&nbsp;        coord bypos;
&nbsp;
&nbsp;        if (enexto_core(&amp;bypos, u.ux, u.uy, ptr, gpflags)) {
&nbsp;            x = bypos.x;
&nbsp;            y = bypos.y;
&nbsp;        } else
&nbsp;            return (struct monst *) 0;
&nbsp;    }
&nbsp;
&nbsp;    /* Does monster already exist at the position? */
&nbsp;    if (MON_AT(x, y)) {
&nbsp;        if ((mmflags &amp; MM_ADJACENTOK) != 0) {
&nbsp;            coord bypos;
&nbsp;            if (enexto_core(&amp;bypos, x, y, ptr, gpflags)) {
&nbsp;                x = bypos.x;
&nbsp;                y = bypos.y;
&nbsp;            } else
&nbsp;                return (struct monst *) 0;
&nbsp;        } else
&nbsp;            return (struct monst *) 0;
&nbsp;    }
&nbsp;
&nbsp;    if (ptr) {
&nbsp;        mndx = monsndx(ptr);
&nbsp;        /* if you are to make a specific monster and it has
&nbsp;           already been genocided, return */
&nbsp;        if (mvitals[mndx].mvflags &amp; G_GENOD)
&nbsp;            return (struct monst *) 0;
&nbsp;        if (wizard &amp;&amp; (mvitals[mndx].mvflags &amp; G_EXTINCT)) {
&nbsp;            debugpline1("Explicitly creating extinct monster %s.",
&nbsp;                        mons[mndx].mname);
&nbsp;        }
&nbsp;    } else {
&nbsp;        /* make a random (common) monster that can survive here.
&nbsp;         * (the special levels ask for random monsters at specific
&nbsp;         * positions, causing mass drowning on the medusa level,
&nbsp;         * for instance.)
&nbsp;         */
&nbsp;        int tryct = 0; /* maybe there are no good choices */
&nbsp;        struct monst fakemon;
&nbsp;
&nbsp;        do {
&nbsp;            if (!(ptr = rndmonst())) {
&nbsp;                debugpline0("Warning: no monster.");
&nbsp;                return (struct monst *) 0; /* no more monsters! */
&nbsp;            }
&nbsp;            fakemon.data = ptr; /* set up for goodpos */
&nbsp;        } while (++tryct &lt;= 50
&nbsp;                 /* in Sokoban, don't accept a giant on first try;
&nbsp;                    after that, boulder carriers are fair game */
&nbsp;                 &amp;&amp; ((tryct == 1 &amp;&amp; throws_rocks(ptr) &amp;&amp; In_sokoban(&amp;u.uz))
&nbsp;                     || !goodpos(x, y, &amp;fakemon, gpflags)));
&nbsp;        mndx = monsndx(ptr);
&nbsp;    }
&nbsp;    (void) propagate(mndx, countbirth, FALSE);
&nbsp;    mtmp = newmonst();
&nbsp;    *mtmp = zeromonst; /* clear all entries in structure */
&nbsp;
&nbsp;    if (mmflags &amp; MM_EGD)
&nbsp;        newegd(mtmp);
&nbsp;    if (mmflags &amp; MM_EPRI)
&nbsp;        newepri(mtmp);
&nbsp;    if (mmflags &amp; MM_ESHK)
&nbsp;        neweshk(mtmp);
&nbsp;    if (mmflags &amp; MM_EMIN)
&nbsp;        newemin(mtmp);
&nbsp;    if (mmflags &amp; MM_EDOG)
&nbsp;        newedog(mtmp);
&nbsp;
&nbsp;    mtmp-&gt;nmon = fmon;
&nbsp;    fmon = mtmp;
&nbsp;    mtmp-&gt;m_id = context.ident++;
&nbsp;    if (!mtmp-&gt;m_id)
&nbsp;        mtmp-&gt;m_id = context.ident++; /* ident overflowed */
&nbsp;    set_mon_data(mtmp, ptr, 0);
&nbsp;    if (ptr-&gt;msound == MS_LEADER &amp;&amp; quest_info(MS_LEADER) == mndx)
&nbsp;        quest_status.leader_m_id = mtmp-&gt;m_id;
&nbsp;    mtmp-&gt;mnum = mndx;
&nbsp;
&nbsp;    /* set up level and hit points */
&nbsp;    newmonhp(mtmp, mndx);
&nbsp;
&nbsp;    if (is_female(ptr))
&nbsp;        mtmp-&gt;female = TRUE;
&nbsp;    else if (is_male(ptr))
&nbsp;        mtmp-&gt;female = FALSE;
&nbsp;    /* leader and nemesis gender is usually hardcoded in mons[],
&nbsp;       but for ones which can be random, it has already been chosen
&nbsp;       (in role_init(), for possible use by the quest pager code) */
&nbsp;    else if (ptr-&gt;msound == MS_LEADER &amp;&amp; quest_info(MS_LEADER) == mndx)
&nbsp;        mtmp-&gt;female = quest_status.ldrgend;
&nbsp;    else if (ptr-&gt;msound == MS_NEMESIS &amp;&amp; quest_info(MS_NEMESIS) == mndx)
&nbsp;        mtmp-&gt;female = quest_status.nemgend;
&nbsp;    else
&nbsp;        mtmp-&gt;female = rn2(2); /* ignored for neuters */
&nbsp;
&nbsp;    if (In_sokoban(&amp;u.uz) &amp;&amp; !mindless(ptr)) /* know about traps here */
&nbsp;        mtmp-&gt;mtrapseen = (1L &lt;&lt; (PIT - 1)) | (1L &lt;&lt; (HOLE - 1));
&nbsp;    /* quest leader and nemesis both know about all trap types */
&nbsp;    if (ptr-&gt;msound == MS_LEADER || ptr-&gt;msound == MS_NEMESIS)
&nbsp;        mtmp-&gt;mtrapseen = ~0;
&nbsp;
&nbsp;    place_monster(mtmp, x, y);
&nbsp;    mtmp-&gt;mcansee = mtmp-&gt;mcanmove = TRUE;
&nbsp;    mtmp-&gt;mpeaceful = (mmflags &amp; MM_ANGRY) ? FALSE : peace_minded(ptr);
&nbsp;
&nbsp;    switch (ptr-&gt;mlet) {
&nbsp;    case S_MIMIC:
&nbsp;        set_mimic_sym(mtmp);
&nbsp;        break;
&nbsp;    case S_SPIDER:
&nbsp;    case S_SNAKE:
&nbsp;        if (in_mklev)
&nbsp;            if (x &amp;&amp; y)
&nbsp;                (void) mkobj_at(0, x, y, TRUE);
&nbsp;        (void) hideunder(mtmp);
&nbsp;        break;
&nbsp;    case S_LIGHT:
&nbsp;    case S_ELEMENTAL:
&nbsp;        if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
&nbsp;            mtmp-&gt;perminvis = TRUE;
&nbsp;            mtmp-&gt;minvis = TRUE;
&nbsp;        }
&nbsp;        break;
&nbsp;    case S_EEL:
&nbsp;        (void) hideunder(mtmp);
&nbsp;        break;
&nbsp;    case S_LEPRECHAUN:
&nbsp;        mtmp-&gt;msleeping = 1;
&nbsp;        break;
&nbsp;    case S_JABBERWOCK:
&nbsp;    case S_NYMPH:
&nbsp;        if (rn2(5) &amp;&amp; !u.uhave.amulet)
&nbsp;            mtmp-&gt;msleeping = 1;
&nbsp;        break;
&nbsp;    case S_ORC:
&nbsp;        if (Race_if(PM_ELF))
&nbsp;            mtmp-&gt;mpeaceful = FALSE;
&nbsp;        break;
&nbsp;    case S_UNICORN:
&nbsp;        if (is_unicorn(ptr) &amp;&amp; sgn(u.ualign.type) == sgn(ptr-&gt;maligntyp))
&nbsp;            mtmp-&gt;mpeaceful = TRUE;
&nbsp;        break;
&nbsp;    case S_BAT:
&nbsp;        if (Inhell &amp;&amp; is_bat(ptr))
&nbsp;            mon_adjust_speed(mtmp, 2, (struct obj *) 0);
&nbsp;        break;
&nbsp;    }
&nbsp;    if ((ct = emits_light(mtmp-&gt;data)) &gt; 0)
&nbsp;        new_light_source(mtmp-&gt;mx, mtmp-&gt;my, ct, LS_MONSTER,
&nbsp;                         monst_to_any(mtmp));
&nbsp;    mitem = 0; /* extra inventory item for this monster */
&nbsp;
&nbsp;    if (mndx == PM_VLAD_THE_IMPALER)
&nbsp;        mitem = CANDELABRUM_OF_INVOCATION;
&nbsp;    mtmp-&gt;cham = NON_PM; /* default is "not a shapechanger" */
&nbsp;    if ((mcham = pm_to_cham(mndx)) != NON_PM) {
&nbsp;        /* this is a shapechanger after all */
&nbsp;        if (Protection_from_shape_changers
&nbsp;            || mndx == PM_VLAD_THE_IMPALER) {
&nbsp;            ; /* stuck in its natural form (NON_PM) */
&nbsp;        } else {
&nbsp;            mtmp-&gt;cham = mcham;
&nbsp;            /* Note: shapechanger's initial form used to be
&nbsp;               chosen here with rndmonst(), yielding a monster
&nbsp;               which was appropriate to the level's difficulty
&nbsp;               but ignored the changer's usual type selection
&nbsp;               so would be inappropriate for vampshifters.
&nbsp;               Let newcham() pick the shape. */
&nbsp;            if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
&nbsp;                allow_minvent = FALSE;
&nbsp;        }
&nbsp;    } else if (mndx == PM_WIZARD_OF_YENDOR) {
&nbsp;        mtmp-&gt;iswiz = TRUE;
&nbsp;        context.no_of_wizards++;
&nbsp;        if (context.no_of_wizards == 1 &amp;&amp; Is_earthlevel(&amp;u.uz))
&nbsp;            mitem = SPE_DIG;
&nbsp;    } else if (mndx == PM_GHOST &amp;&amp; !(mmflags &amp; MM_NONAME)) {
&nbsp;        mtmp = christen_monst(mtmp, rndghostname());
&nbsp;    } else if (mndx == PM_CROESUS) {
&nbsp;        mitem = TWO_HANDED_SWORD;
&nbsp;    } else if (ptr-&gt;msound == MS_NEMESIS) {
&nbsp;        mitem = BELL_OF_OPENING;
&nbsp;    } else if (mndx == PM_PESTILENCE) {
&nbsp;        mitem = POT_SICKNESS;
&nbsp;    }
&nbsp;    if (mitem &amp;&amp; allow_minvent)
&nbsp;        (void) mongets(mtmp, mitem);
&nbsp;
&nbsp;    if (in_mklev) {
&nbsp;        if ((is_ndemon(ptr) || mndx == PM_WUMPUS
&nbsp;             || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
&nbsp;            &amp;&amp; !u.uhave.amulet &amp;&amp; rn2(5))
&nbsp;            mtmp-&gt;msleeping = TRUE;
&nbsp;    } else {
&nbsp;        if (byyou) {
&nbsp;            newsym(mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;            set_apparxy(mtmp);
&nbsp;        }
&nbsp;    }
&nbsp;    if (is_dprince(ptr) &amp;&amp; ptr-&gt;msound == MS_BRIBE) {
&nbsp;        mtmp-&gt;mpeaceful = mtmp-&gt;minvis = mtmp-&gt;perminvis = 1;
&nbsp;        mtmp-&gt;mavenge = 0;
&nbsp;        if (uwep &amp;&amp; uwep-&gt;oartifact == ART_EXCALIBUR)
&nbsp;            mtmp-&gt;mpeaceful = mtmp-&gt;mtame = FALSE;
&nbsp;    }
&nbsp;#ifndef DCC30_BUG
&nbsp;    if (mndx == 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;    if (mndx == PM_LONG_WORM
&nbsp;        &amp;&amp; (mtmp-&gt;wormno = get_wormno(), mtmp-&gt;wormno != 0))
&nbsp;#endif
&nbsp;    {
&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, x, y);
&nbsp;    }
&nbsp;    /* it's possible to create an ordinary monster of some special
&nbsp;       types; make sure their extended data is initialized to
&nbsp;       something sensible if caller hasn't specified MM_EPRI|MM_EMIN
&nbsp;       (when they're specified, caller intends to handle this itself) */
&nbsp;    if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
&nbsp;            ? !(mmflags &amp; (MM_EPRI | MM_EMIN))
&nbsp;            : (mndx == PM_ANGEL &amp;&amp; !(mmflags &amp; MM_EMIN) &amp;&amp; !rn2(3))) {
&nbsp;        struct emin *eminp;
&nbsp;        newemin(mtmp);
&nbsp;        eminp = EMIN(mtmp);
&nbsp;
&nbsp;        mtmp-&gt;isminion = 1;            /* make priest be a roamer */
&nbsp;        eminp-&gt;min_align = rn2(3) - 1; /* no A_NONE */
&nbsp;        eminp-&gt;renegade = (boolean) ((mmflags &amp; MM_ANGRY) ? 1 : !rn2(3));
&nbsp;        mtmp-&gt;mpeaceful = (eminp-&gt;min_align == u.ualign.type)
&nbsp;                              ? !eminp-&gt;renegade
&nbsp;                              : eminp-&gt;renegade;
&nbsp;    }
&nbsp;    set_malign(mtmp); /* having finished peaceful changes */
&nbsp;    if (anymon) {
&nbsp;        if ((ptr-&gt;geno &amp; G_SGROUP) &amp;&amp; rn2(2)) {
&nbsp;            m_initsgrp(mtmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        } else if (ptr-&gt;geno &amp; G_LGROUP) {
&nbsp;            if (rn2(3))
&nbsp;                m_initlgrp(mtmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;            else
&nbsp;                m_initsgrp(mtmp, mtmp-&gt;mx, mtmp-&gt;my);
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    if (allow_minvent) {
&nbsp;        if (is_armed(ptr))
&nbsp;            m_initweap(mtmp); /* equip with weapons / armor */
&nbsp;        m_initinv(mtmp); /* add on a few special items incl. more armor */
&nbsp;        m_dowear(mtmp, TRUE);
&nbsp;
&nbsp;        if (!rn2(100) &amp;&amp; is_domestic(ptr)
&nbsp;            &amp;&amp; can_saddle(mtmp) &amp;&amp; !which_armor(mtmp, W_SADDLE)) {
&nbsp;            struct obj *otmp = mksobj(SADDLE, TRUE, FALSE);
&nbsp;            put_saddle_on_mon(otmp, mtmp);
&nbsp;        }
&nbsp;
&nbsp;    } else {
&nbsp;        /* no initial inventory is allowed */
&nbsp;        if (mtmp-&gt;minvent)
&nbsp;            discard_minvent(mtmp);
&nbsp;        mtmp-&gt;minvent = (struct obj *) 0; /* caller expects this */
&nbsp;    }
&nbsp;    if (ptr-&gt;mflags3 &amp;&amp; !(mmflags &amp; MM_NOWAIT)) {
&nbsp;        if (ptr-&gt;mflags3 &amp; M3_WAITFORU)
&nbsp;            mtmp-&gt;mstrategy |= STRAT_WAITFORU;
&nbsp;        if (ptr-&gt;mflags3 &amp; M3_CLOSE)
&nbsp;            mtmp-&gt;mstrategy |= STRAT_CLOSE;
&nbsp;        if (ptr-&gt;mflags3 &amp; (M3_WAITMASK | M3_COVETOUS))
&nbsp;            mtmp-&gt;mstrategy |= STRAT_APPEARMSG;
&nbsp;    }
&nbsp;
&nbsp;    if (!in_mklev)
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my); /* make sure the mon shows up */
&nbsp;
&nbsp;    return mtmp;
&nbsp;}
&nbsp;
mbirth_limit
&nbsp;int
&nbsp;mbirth_limit(mndx)
&nbsp;int mndx;
&nbsp;{
&nbsp;    /* There is an implicit limit of 4 for "high priest of <deity>",
&nbsp;     * but aligned priests can grow into high priests, thus they aren't
&nbsp;     * really limited to 4, so leave the default amount in place for them.
&nbsp;     */
&nbsp;
&nbsp;    /* assert(MAXMONNO &lt; 255); */
&nbsp;    return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
&nbsp;}
&nbsp;
create_critters
&nbsp;/* used for wand/scroll/spell of create monster */
&nbsp;/* returns TRUE iff you know monsters have been created */
&nbsp;boolean
&nbsp;create_critters(cnt, mptr, neverask)
&nbsp;int cnt;
&nbsp;struct permonst *mptr; /* usually null; used for confused reading */
&nbsp;boolean neverask;
&nbsp;{
&nbsp;    coord c;
&nbsp;    int x, y;
&nbsp;    struct monst *mon;
&nbsp;    boolean known = FALSE;
&nbsp;    boolean ask = (wizard &amp;&amp; !neverask);
&nbsp;
&nbsp;    while (cnt--) {
&nbsp;        if (ask) {
&nbsp;            if (create_particular()) {
&nbsp;                known = TRUE;
&nbsp;                continue;
&nbsp;            } else
&nbsp;                ask = FALSE; /* ESC will shut off prompting */
&nbsp;        }
&nbsp;        x = u.ux, y = u.uy;
&nbsp;        /* if in water, try to encourage an aquatic monster
&nbsp;           by finding and then specifying another wet location */
&nbsp;        if (!mptr &amp;&amp; u.uinwater &amp;&amp; enexto(&amp;c, x, y, &amp;mons[PM_GIANT_EEL]))
&nbsp;            x = c.x, y = c.y;
&nbsp;
&nbsp;        mon = makemon(mptr, x, y, NO_MM_FLAGS);
&nbsp;        if (mon &amp;&amp; canspotmon(mon))
&nbsp;            known = TRUE;
&nbsp;    }
&nbsp;    return known;
&nbsp;}
&nbsp;
uncommon
&nbsp;STATIC_OVL boolean
&nbsp;uncommon(mndx)
&nbsp;int mndx;
&nbsp;{
&nbsp;    if (mons[mndx].geno &amp; (G_NOGEN | G_UNIQ))
&nbsp;        return TRUE;
&nbsp;    if (mvitals[mndx].mvflags &amp; G_GONE)
&nbsp;        return TRUE;
&nbsp;    if (Inhell)
&nbsp;        return (boolean) (mons[mndx].maligntyp &gt; A_NEUTRAL);
&nbsp;    else
&nbsp;        return (boolean) ((mons[mndx].geno &amp; G_HELL) != 0);
&nbsp;}
&nbsp;
align_shift
&nbsp;/*
&nbsp; *      shift the probability of a monster's generation by
&nbsp; *      comparing the dungeon alignment and monster alignment.
&nbsp; *      return an integer in the range of 0-5.
&nbsp; */
&nbsp;STATIC_OVL int
&nbsp;align_shift(ptr)
&nbsp;register struct permonst *ptr;
&nbsp;{
&nbsp;    static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
&nbsp;    static NEARDATA s_level *lev;
&nbsp;    register int alshift;
&nbsp;
&nbsp;    if (oldmoves != moves) {
&nbsp;        lev = Is_special(&amp;u.uz);
&nbsp;        oldmoves = moves;
&nbsp;    }
&nbsp;    switch ((lev) ? lev-&gt;flags.align : dungeons[u.uz.dnum].flags.align) {
&nbsp;    default: /* just in case */
&nbsp;    case AM_NONE:
&nbsp;        alshift = 0;
&nbsp;        break;
&nbsp;    case AM_LAWFUL:
&nbsp;        alshift = (ptr-&gt;maligntyp + 20) / (2 * ALIGNWEIGHT);
&nbsp;        break;
&nbsp;    case AM_NEUTRAL:
&nbsp;        alshift = (20 - abs(ptr-&gt;maligntyp)) / ALIGNWEIGHT;
&nbsp;        break;
&nbsp;    case AM_CHAOTIC:
&nbsp;        alshift = (-(ptr-&gt;maligntyp - 20)) / (2 * ALIGNWEIGHT);
&nbsp;        break;
&nbsp;    }
&nbsp;    return alshift;
&nbsp;}
&nbsp;
&nbsp;static NEARDATA struct {
&nbsp;    int choice_count;
&nbsp;    char mchoices[SPECIAL_PM]; /* value range is 0..127 */
&nbsp;} rndmonst_state = { -1, { 0 } };
&nbsp;
rndmonst
&nbsp;/* select a random monster type */
&nbsp;struct permonst *
&nbsp;rndmonst()
&nbsp;{
&nbsp;    register struct permonst *ptr;
&nbsp;    register int mndx, ct;
&nbsp;
&nbsp;    if (u.uz.dnum == quest_dnum &amp;&amp; rn2(7) &amp;&amp; (ptr = qt_montype()) != 0)
&nbsp;        return ptr;
&nbsp;
&nbsp;    if (rndmonst_state.choice_count &lt; 0) { /* need to recalculate */
&nbsp;        int zlevel, minmlev, maxmlev;
&nbsp;        boolean elemlevel;
&nbsp;        boolean upper;
&nbsp;
&nbsp;        rndmonst_state.choice_count = 0;
&nbsp;        /* look for first common monster */
&nbsp;        for (mndx = LOW_PM; mndx &lt; SPECIAL_PM; mndx++) {
&nbsp;            if (!uncommon(mndx))
&nbsp;                break;
&nbsp;            rndmonst_state.mchoices[mndx] = 0;
&nbsp;        }
&nbsp;        if (mndx == SPECIAL_PM) {
&nbsp;            /* evidently they've all been exterminated */
&nbsp;            debugpline0("rndmonst: no common mons!");
&nbsp;            return (struct permonst *) 0;
&nbsp;        } /* else `mndx' now ready for use below */
&nbsp;        zlevel = level_difficulty();
&nbsp;        /* determine the level of the weakest monster to make. */
&nbsp;        minmlev = zlevel / 6;
&nbsp;        /* determine the level of the strongest monster to make. */
&nbsp;        maxmlev = (zlevel + u.ulevel) / 2;
&nbsp;        upper = Is_rogue_level(&amp;u.uz);
&nbsp;        elemlevel = In_endgame(&amp;u.uz) &amp;&amp; !Is_astralevel(&amp;u.uz);
&nbsp;
&nbsp;        /*
&nbsp;         * Find out how many monsters exist in the range we have selected.
&nbsp;         */
&nbsp;        for ( ; mndx &lt; SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
&nbsp;            ptr = &amp;mons[mndx];
&nbsp;            rndmonst_state.mchoices[mndx] = 0;
&nbsp;            if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
&nbsp;                continue;
&nbsp;            if (upper &amp;&amp; !isupper((uchar) def_monsyms[(int) ptr-&gt;mlet].sym))
&nbsp;                continue;
&nbsp;            if (elemlevel &amp;&amp; wrong_elem_type(ptr))
&nbsp;                continue;
&nbsp;            if (uncommon(mndx))
&nbsp;                continue;
&nbsp;            if (Inhell &amp;&amp; (ptr-&gt;geno &amp; G_NOHELL))
&nbsp;                continue;
&nbsp;            ct = (int) (ptr-&gt;geno &amp; G_FREQ) + align_shift(ptr);
&nbsp;            if (ct &lt; 0 || ct &gt; 127)
&nbsp;                panic("rndmonst: bad count [#%d: %d]", mndx, ct);
&nbsp;            rndmonst_state.choice_count += ct;
&nbsp;            rndmonst_state.mchoices[mndx] = (char) ct;
&nbsp;        }
&nbsp;        /*
&nbsp;         *      Possible modification:  if choice_count is "too low",
&nbsp;         *      expand minmlev..maxmlev range and try again.
&nbsp;         */
&nbsp;    } /* choice_count+mchoices[] recalc */
&nbsp;
&nbsp;    if (rndmonst_state.choice_count &lt;= 0) {
&nbsp;        /* maybe no common mons left, or all are too weak or too strong */
&nbsp;        debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
&nbsp;        return (struct permonst *) 0;
&nbsp;    }
&nbsp;
&nbsp;    /*
&nbsp;     *  Now, select a monster at random.
&nbsp;     */
&nbsp;    ct = rnd(rndmonst_state.choice_count);
&nbsp;    for (mndx = LOW_PM; mndx &lt; SPECIAL_PM; mndx++)
&nbsp;        if ((ct -= (int) rndmonst_state.mchoices[mndx]) &lt;= 0)
&nbsp;            break;
&nbsp;
&nbsp;    if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
&nbsp;        impossible("rndmonst: bad `mndx' [#%d]", mndx);
&nbsp;        return (struct permonst *) 0;
&nbsp;    }
&nbsp;    return &amp;mons[mndx];
&nbsp;}
&nbsp;
reset_rndmonst
&nbsp;/* called when you change level (experience or dungeon depth) or when
&nbsp;   monster species can no longer be created (genocide or extinction) */
&nbsp;void
&nbsp;reset_rndmonst(mndx)
&nbsp;int mndx; /* particular species that can no longer be created */
&nbsp;{
&nbsp;    /* cached selection info is out of date */
&nbsp;    if (mndx == NON_PM) {
&nbsp;        rndmonst_state.choice_count = -1; /* full recalc needed */
&nbsp;    } else if (mndx &lt; SPECIAL_PM) {
&nbsp;        rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
&nbsp;        rndmonst_state.mchoices[mndx] = 0;
&nbsp;    } /* note: safe to ignore extinction of unique monsters */
&nbsp;}
&nbsp;
mk_gen_ok
&nbsp;/* decide whether it's ok to generate a candidate monster by mkclass() */
&nbsp;STATIC_OVL boolean
&nbsp;mk_gen_ok(mndx, mvflagsmask, genomask)
&nbsp;int mndx, mvflagsmask, genomask;
&nbsp;{
&nbsp;    struct permonst *ptr = &amp;mons[mndx];
&nbsp;
&nbsp;    if (mvitals[mndx].mvflags &amp; mvflagsmask)
&nbsp;        return FALSE;
&nbsp;    if (ptr-&gt;geno &amp; genomask)
&nbsp;        return FALSE;
&nbsp;    if (is_placeholder(ptr))
&nbsp;        return FALSE;
&nbsp;#ifdef MAIL
&nbsp;    /* special levels might ask for random demon type; reject this one */
&nbsp;    if (ptr == &amp;mons[PM_MAIL_DAEMON])
&nbsp;        return FALSE;
&nbsp;#endif
&nbsp;    return TRUE;
&nbsp;}
&nbsp;
mkclass
&nbsp;/* Make one of the multiple types of a given monster class.
&nbsp; * The second parameter specifies a special casing bit mask
&nbsp; * to allow the normal genesis masks to be deactivated.
&nbsp; * Returns Null if no monsters in that class can be made.
&nbsp; */
&nbsp;struct permonst *
&nbsp;mkclass(class, spc)
&nbsp;char class;
&nbsp;int spc;
&nbsp;{
&nbsp;    register int first, last, num = 0;
&nbsp;    int maxmlev, mask = (G_NOGEN | G_UNIQ) &amp; ~spc;
&nbsp;
&nbsp;    maxmlev = level_difficulty() &gt;&gt; 1;
&nbsp;    if (class &lt; 1 || class &gt;= MAXMCLASSES) {
&nbsp;        impossible("mkclass called with bad class!");
&nbsp;        return (struct permonst *) 0;
&nbsp;    }
&nbsp;    /*  Assumption #1:  monsters of a given class are contiguous in the
&nbsp;     *                  mons[] array.
&nbsp;     */
&nbsp;    for (first = LOW_PM; first &lt; SPECIAL_PM; first++)
&nbsp;        if (mons[first].mlet == class)
&nbsp;            break;
&nbsp;    if (first == SPECIAL_PM)
&nbsp;        return (struct permonst *) 0;
&nbsp;
&nbsp;    for (last = first; last &lt; SPECIAL_PM &amp;&amp; mons[last].mlet == class; last++)
&nbsp;        if (mk_gen_ok(last, G_GONE, mask)) {
&nbsp;            /* consider it */
&nbsp;            if (num &amp;&amp; toostrong(last, maxmlev)
&nbsp;                &amp;&amp; monstr[last] != monstr[last - 1] &amp;&amp; rn2(2))
&nbsp;                break;
&nbsp;            num += mons[last].geno &amp; G_FREQ;
&nbsp;        }
&nbsp;    if (!num)
&nbsp;        return (struct permonst *) 0;
&nbsp;
&nbsp;    /*  Assumption #2:  monsters of a given class are presented in ascending
&nbsp;     *                  order of strength.
&nbsp;     */
&nbsp;    for (num = rnd(num); num &gt; 0; first++)
&nbsp;        if (mk_gen_ok(first, G_GONE, mask)) {
&nbsp;            /* skew towards lower value monsters at lower exp. levels */
&nbsp;            num -= mons[first].geno &amp; G_FREQ;
&nbsp;            if (num &amp;&amp; adj_lev(&amp;mons[first]) &gt; (u.ulevel * 2)) {
&nbsp;                /* but not when multiple monsters are same level */
&nbsp;                if (mons[first].mlevel != mons[first + 1].mlevel)
&nbsp;                    num--;
&nbsp;            }
&nbsp;        }
&nbsp;    first--; /* correct an off-by-one error */
&nbsp;
&nbsp;    return &amp;mons[first];
&nbsp;}
&nbsp;
mkclass_poly
&nbsp;/* like mkclass(), but excludes difficulty considerations; used when
&nbsp;   player with polycontrol picks a class instead of a specific type;
&nbsp;   genocided types are avoided but extinct ones are acceptable; we don't
&nbsp;   check polyok() here--caller accepts some choices !polyok() would reject */
&nbsp;int
&nbsp;mkclass_poly(class)
&nbsp;int class;
&nbsp;{
&nbsp;    register int first, last, num = 0;
&nbsp;
&nbsp;    for (first = LOW_PM; first &lt; SPECIAL_PM; first++)
&nbsp;        if (mons[first].mlet == class)
&nbsp;            break;
&nbsp;    if (first == SPECIAL_PM)
&nbsp;        return NON_PM;
&nbsp;
&nbsp;    for (last = first; last &lt; SPECIAL_PM &amp;&amp; mons[last].mlet == class; last++)
&nbsp;        if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
&nbsp;            num += mons[last].geno &amp; G_FREQ;
&nbsp;    if (!num)
&nbsp;        return NON_PM;
&nbsp;
&nbsp;    for (num = rnd(num); num &gt; 0; first++)
&nbsp;        if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
&nbsp;            num -= mons[first].geno &amp; G_FREQ;
&nbsp;    first--; /* correct an off-by-one error */
&nbsp;
&nbsp;    return first;
&nbsp;}
&nbsp;
adj_lev
&nbsp;/* adjust strength of monsters based on u.uz and u.ulevel */
&nbsp;int
&nbsp;adj_lev(ptr)
&nbsp;register struct permonst *ptr;
&nbsp;{
&nbsp;    int tmp, tmp2;
&nbsp;
&nbsp;    if (ptr == &amp;mons[PM_WIZARD_OF_YENDOR]) {
&nbsp;        /* does not depend on other strengths, but does get stronger
&nbsp;         * every time he is killed
&nbsp;         */
&nbsp;        tmp = ptr-&gt;mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
&nbsp;        if (tmp &gt; 49)
&nbsp;            tmp = 49;
&nbsp;        return tmp;
&nbsp;    }
&nbsp;
&nbsp;    if ((tmp = ptr-&gt;mlevel) &gt; 49)
&nbsp;        return 50; /* "special" demons/devils */
&nbsp;    tmp2 = (level_difficulty() - tmp);
&nbsp;    if (tmp2 &lt; 0)
&nbsp;        tmp--; /* if mlevel &gt; u.uz decrement tmp */
&nbsp;    else
&nbsp;        tmp += (tmp2 / 5); /* else increment 1 per five diff */
&nbsp;
&nbsp;    tmp2 = (u.ulevel - ptr-&gt;mlevel); /* adjust vs. the player */
&nbsp;    if (tmp2 &gt; 0)
&nbsp;        tmp += (tmp2 / 4); /* level as well */
&nbsp;
&nbsp;    tmp2 = (3 * ((int) ptr-&gt;mlevel)) / 2; /* crude upper limit */
&nbsp;    if (tmp2 &gt; 49)
&nbsp;        tmp2 = 49;                                      /* hard upper limit */
&nbsp;    return ((tmp &gt; tmp2) ? tmp2 : (tmp &gt; 0 ? tmp : 0)); /* 0 lower limit */
&nbsp;}
&nbsp;
grow_up
&nbsp;/* monster earned experience and will gain some hit points; it might also
&nbsp;   grow into a bigger monster (baby to adult, soldier to officer, etc) */
&nbsp;struct permonst *
&nbsp;grow_up(mtmp, victim)
&nbsp;struct monst *mtmp, *victim;
&nbsp;{
&nbsp;    int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
&nbsp;    unsigned fem;
&nbsp;    struct permonst *ptr = mtmp-&gt;data;
&nbsp;
&nbsp;    /* monster died after killing enemy but before calling this function */
&nbsp;    /* currently possible if killing a gas spore */
&nbsp;    if (mtmp-&gt;mhp &lt;= 0)
&nbsp;        return (struct permonst *) 0;
&nbsp;
&nbsp;    /* note:  none of the monsters with special hit point calculations
&nbsp;       have both little and big forms */
&nbsp;    oldtype = monsndx(ptr);
&nbsp;    newtype = little_to_big(oldtype);
&nbsp;    if (newtype == PM_PRIEST &amp;&amp; mtmp-&gt;female)
&nbsp;        newtype = PM_PRIESTESS;
&nbsp;
&nbsp;    /* growth limits differ depending on method of advancement */
&nbsp;    if (victim) {                       /* killed a monster */
&nbsp;        /*
&nbsp;         * The HP threshold is the maximum number of hit points for the
&nbsp;         * current level; once exceeded, a level will be gained.
&nbsp;         * Possible bug: if somehow the hit points are already higher
&nbsp;         * than that, monster will gain a level without any increase in HP.
&nbsp;         */
&nbsp;        hp_threshold = mtmp-&gt;m_lev * 8; /* normal limit */
&nbsp;        if (!mtmp-&gt;m_lev)
&nbsp;            hp_threshold = 4;
&nbsp;        else if (is_golem(ptr)) /* strange creatures */
&nbsp;            hp_threshold = ((mtmp-&gt;mhpmax / 10) + 1) * 10 - 1;
&nbsp;        else if (is_home_elemental(ptr))
&nbsp;            hp_threshold *= 3;
&nbsp;        lev_limit = 3 * (int) ptr-&gt;mlevel / 2; /* same as adj_lev() */
&nbsp;        /* If they can grow up, be sure the level is high enough for that */
&nbsp;        if (oldtype != newtype &amp;&amp; mons[newtype].mlevel &gt; lev_limit)
&nbsp;            lev_limit = (int) mons[newtype].mlevel;
&nbsp;        /* number of hit points to gain; unlike for the player, we put
&nbsp;           the limit at the bottom of the next level rather than the top */
&nbsp;        max_increase = rnd((int) victim-&gt;m_lev + 1);
&nbsp;        if (mtmp-&gt;mhpmax + max_increase &gt; hp_threshold + 1)
&nbsp;            max_increase = max((hp_threshold + 1) - mtmp-&gt;mhpmax, 0);
&nbsp;        cur_increase = (max_increase &gt; 1) ? rn2(max_increase) : 0;
&nbsp;    } else {
&nbsp;        /* a gain level potion or wraith corpse; always go up a level
&nbsp;           unless already at maximum (49 is hard upper limit except
&nbsp;           for demon lords, who start at 50 and can't go any higher) */
&nbsp;        max_increase = cur_increase = rnd(8);
&nbsp;        hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
&nbsp;        lev_limit = 50;   /* recalc below */
&nbsp;    }
&nbsp;
&nbsp;    mtmp-&gt;mhpmax += max_increase;
&nbsp;    mtmp-&gt;mhp += cur_increase;
&nbsp;    if (mtmp-&gt;mhpmax &lt;= hp_threshold)
&nbsp;        return ptr; /* doesn't gain a level */
&nbsp;
&nbsp;    if (is_mplayer(ptr))
&nbsp;        lev_limit = 30; /* same as player */
&nbsp;    else if (lev_limit &lt; 5)
&nbsp;        lev_limit = 5; /* arbitrary */
&nbsp;    else if (lev_limit &gt; 49)
&nbsp;        lev_limit = (ptr-&gt;mlevel &gt; 49 ? 50 : 49);
&nbsp;
&nbsp;    if ((int) ++mtmp-&gt;m_lev &gt;= mons[newtype].mlevel &amp;&amp; newtype != oldtype) {
&nbsp;        ptr = &amp;mons[newtype];
&nbsp;        /* new form might force gender change */
&nbsp;        fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp-&gt;female;
&nbsp;
&nbsp;        if (mvitals[newtype].mvflags &amp; G_GENOD) { /* allow G_EXTINCT */
&nbsp;            if (canspotmon(mtmp))
&nbsp;                pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
&nbsp;                      an(ptr-&gt;mname), mhe(mtmp),
&nbsp;                      nonliving(ptr) ? "expires" : "dies");
&nbsp;            set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
&nbsp;            mondied(mtmp);
&nbsp;            return (struct permonst *) 0;
&nbsp;        } else if (canspotmon(mtmp)) {
&nbsp;            char buf[BUFSZ];
&nbsp;
&nbsp;            /* 3.6.1:
&nbsp;             * Temporary (?) hack to fix growing into opposite gender.
&nbsp;             */
&nbsp;            Sprintf(buf, "%s%s",
&nbsp;                    /* deal with female gnome becoming a gnome lord */
&nbsp;                    (mtmp-&gt;female &amp;&amp; !fem) ? "male "
&nbsp;                        /* or a male gnome becoming a gnome lady
&nbsp;                           (can't happen with 3.6.0 mons[], but perhaps
&nbsp;                           slightly less sexist if prepared for it...) */
&nbsp;                      : (fem &amp;&amp; !mtmp-&gt;female) ? "female " : "",
&nbsp;                    ptr-&gt;mname);
&nbsp;            pline("%s %s %s.", Monnam(mtmp),
&nbsp;                  (fem != mtmp-&gt;female) ? "changes into"
&nbsp;                                        : humanoid(ptr) ? "becomes"
&nbsp;                                                        : "grows up into",
&nbsp;                  an(buf));
&nbsp;        }
&nbsp;        set_mon_data(mtmp, ptr, 1);    /* preserve intrinsics */
&nbsp;        newsym(mtmp-&gt;mx, mtmp-&gt;my);    /* color may change */
&nbsp;        lev_limit = (int) mtmp-&gt;m_lev; /* never undo increment */
&nbsp;
&nbsp;        mtmp-&gt;female = fem; /* gender might be changing */
&nbsp;    }
&nbsp;
&nbsp;    /* sanity checks */
&nbsp;    if ((int) mtmp-&gt;m_lev &gt; lev_limit) {
&nbsp;        mtmp-&gt;m_lev--; /* undo increment */
&nbsp;        /* HP might have been allowed to grow when it shouldn't */
&nbsp;        if (mtmp-&gt;mhpmax == hp_threshold + 1)
&nbsp;            mtmp-&gt;mhpmax--;
&nbsp;    }
&nbsp;    if (mtmp-&gt;mhpmax &gt; 50 * 8)
&nbsp;        mtmp-&gt;mhpmax = 50 * 8; /* absolute limit */
&nbsp;    if (mtmp-&gt;mhp &gt; mtmp-&gt;mhpmax)
&nbsp;        mtmp-&gt;mhp = mtmp-&gt;mhpmax;
&nbsp;
&nbsp;    return ptr;
&nbsp;}
&nbsp;
mongets
&nbsp;int
&nbsp;mongets(mtmp, otyp)
&nbsp;register struct monst *mtmp;
&nbsp;int otyp;
&nbsp;{
&nbsp;    register struct obj *otmp;
&nbsp;    int spe;
&nbsp;
&nbsp;    if (!otyp)
&nbsp;        return 0;
&nbsp;    otmp = mksobj(otyp, TRUE, FALSE);
&nbsp;    if (otmp) {
&nbsp;        if (mtmp-&gt;data-&gt;mlet == S_DEMON) {
&nbsp;            /* demons never get blessed objects */
&nbsp;            if (otmp-&gt;blessed)
&nbsp;                curse(otmp);
&nbsp;        } else if (is_lminion(mtmp)) {
&nbsp;            /* lawful minions don't get cursed, bad, or rusting objects */
&nbsp;            otmp-&gt;cursed = FALSE;
&nbsp;            if (otmp-&gt;spe &lt; 0)
&nbsp;                otmp-&gt;spe = 0;
&nbsp;            otmp-&gt;oerodeproof = TRUE;
&nbsp;        } else if (is_mplayer(mtmp-&gt;data) &amp;&amp; is_sword(otmp)) {
&nbsp;            otmp-&gt;spe = (3 + rn2(4));
&nbsp;        }
&nbsp;
&nbsp;        if (otmp-&gt;otyp == CANDELABRUM_OF_INVOCATION) {
&nbsp;            otmp-&gt;spe = 0;
&nbsp;            otmp-&gt;age = 0L;
&nbsp;            otmp-&gt;lamplit = FALSE;
&nbsp;            otmp-&gt;blessed = otmp-&gt;cursed = FALSE;
&nbsp;        } else if (otmp-&gt;otyp == BELL_OF_OPENING) {
&nbsp;            otmp-&gt;blessed = otmp-&gt;cursed = FALSE;
&nbsp;        } else if (otmp-&gt;otyp == SPE_BOOK_OF_THE_DEAD) {
&nbsp;            otmp-&gt;blessed = FALSE;
&nbsp;            otmp-&gt;cursed = TRUE;
&nbsp;        }
&nbsp;
&nbsp;        /* leaders don't tolerate inferior quality battle gear */
&nbsp;        if (is_prince(mtmp-&gt;data)) {
&nbsp;            if (otmp-&gt;oclass == WEAPON_CLASS &amp;&amp; otmp-&gt;spe &lt; 1)
&nbsp;                otmp-&gt;spe = 1;
&nbsp;            else if (otmp-&gt;oclass == ARMOR_CLASS &amp;&amp; otmp-&gt;spe &lt; 0)
&nbsp;                otmp-&gt;spe = 0;
&nbsp;        }
&nbsp;
&nbsp;        spe = otmp-&gt;spe;
&nbsp;        (void) mpickobj(mtmp, otmp); /* might free otmp */
&nbsp;        return spe;
&nbsp;    }
&nbsp;    return 0;
&nbsp;}
&nbsp;
golemhp
&nbsp;int
&nbsp;golemhp(type)
&nbsp;int type;
&nbsp;{
&nbsp;    switch (type) {
&nbsp;    case PM_STRAW_GOLEM:
&nbsp;        return 20;
&nbsp;    case PM_PAPER_GOLEM:
&nbsp;        return 20;
&nbsp;    case PM_ROPE_GOLEM:
&nbsp;        return 30;
&nbsp;    case PM_LEATHER_GOLEM:
&nbsp;        return 40;
&nbsp;    case PM_GOLD_GOLEM:
&nbsp;        return 40;
&nbsp;    case PM_WOOD_GOLEM:
&nbsp;        return 50;
&nbsp;    case PM_FLESH_GOLEM:
&nbsp;        return 40;
&nbsp;    case PM_CLAY_GOLEM:
&nbsp;        return 50;
&nbsp;    case PM_STONE_GOLEM:
&nbsp;        return 60;
&nbsp;    case PM_GLASS_GOLEM:
&nbsp;        return 60;
&nbsp;    case PM_IRON_GOLEM:
&nbsp;        return 80;
&nbsp;    default:
&nbsp;        return 0;
&nbsp;    }
&nbsp;}
&nbsp;
peace_minded
&nbsp;/*
&nbsp; *      Alignment vs. yours determines monster's attitude to you.
&nbsp; *      (Some "animal" types are co-aligned, but also hungry.)
&nbsp; */
&nbsp;boolean
&nbsp;peace_minded(ptr)
&nbsp;register struct permonst *ptr;
&nbsp;{
&nbsp;    aligntyp mal = ptr-&gt;maligntyp, ual = u.ualign.type;
&nbsp;
&nbsp;    if (always_peaceful(ptr))
&nbsp;        return TRUE;
&nbsp;    if (always_hostile(ptr))
&nbsp;        return FALSE;
&nbsp;    if (ptr-&gt;msound == MS_LEADER || ptr-&gt;msound == MS_GUARDIAN)
&nbsp;        return TRUE;
&nbsp;    if (ptr-&gt;msound == MS_NEMESIS)
&nbsp;        return FALSE;
&nbsp;
&nbsp;    if (race_peaceful(ptr))
&nbsp;        return TRUE;
&nbsp;    if (race_hostile(ptr))
&nbsp;        return FALSE;
&nbsp;
&nbsp;    /* the monster is hostile if its alignment is different from the
&nbsp;     * player's */
&nbsp;    if (sgn(mal) != sgn(ual))
&nbsp;        return FALSE;
&nbsp;
&nbsp;    /* Negative monster hostile to player with Amulet. */
&nbsp;    if (mal &lt; A_NEUTRAL &amp;&amp; u.uhave.amulet)
&nbsp;        return FALSE;
&nbsp;
&nbsp;    /* minions are hostile to players that have strayed at all */
&nbsp;    if (is_minion(ptr))
&nbsp;        return (boolean) (u.ualign.record &gt;= 0);
&nbsp;
&nbsp;    /* Last case:  a chance of a co-aligned monster being
&nbsp;     * hostile.  This chance is greater if the player has strayed
&nbsp;     * (u.ualign.record negative) or the monster is not strongly aligned.
&nbsp;     */
&nbsp;    return (boolean) (!!rn2(16 + (u.ualign.record &lt; -15 ? -15
&nbsp;                                                        : u.ualign.record))
&nbsp;                      &amp;&amp; !!rn2(2 + abs(mal)));
&nbsp;}
&nbsp;
set_malign
&nbsp;/* Set malign to have the proper effect on player alignment if monster is
&nbsp; * killed.  Negative numbers mean it's bad to kill this monster; positive
&nbsp; * numbers mean it's good.  Since there are more hostile monsters than
&nbsp; * peaceful monsters, the penalty for killing a peaceful monster should be
&nbsp; * greater than the bonus for killing a hostile monster to maintain balance.
&nbsp; * Rules:
&nbsp; *   it's bad to kill peaceful monsters, potentially worse to kill always-
&nbsp; *      peaceful monsters;
&nbsp; *   it's never bad to kill a hostile monster, although it may not be good.
&nbsp; */
&nbsp;void
&nbsp;set_malign(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    schar mal = mtmp-&gt;data-&gt;maligntyp;
&nbsp;    boolean coaligned;
&nbsp;
&nbsp;    if (mtmp-&gt;ispriest || mtmp-&gt;isminion) {
&nbsp;        /* some monsters have individual alignments; check them */
&nbsp;        if (mtmp-&gt;ispriest &amp;&amp; EPRI(mtmp))
&nbsp;            mal = EPRI(mtmp)-&gt;shralign;
&nbsp;        else if (mtmp-&gt;isminion &amp;&amp; EMIN(mtmp))
&nbsp;            mal = EMIN(mtmp)-&gt;min_align;
&nbsp;        /* unless alignment is none, set mal to -5,0,5 */
&nbsp;        /* (see align.h for valid aligntyp values)     */
&nbsp;        if (mal != A_NONE)
&nbsp;            mal *= 5;
&nbsp;    }
&nbsp;
&nbsp;    coaligned = (sgn(mal) == sgn(u.ualign.type));
&nbsp;    if (mtmp-&gt;data-&gt;msound == MS_LEADER) {
&nbsp;        mtmp-&gt;malign = -20;
&nbsp;    } else if (mal == A_NONE) {
&nbsp;        if (mtmp-&gt;mpeaceful)
&nbsp;            mtmp-&gt;malign = 0;
&nbsp;        else
&nbsp;            mtmp-&gt;malign = 20; /* really hostile */
&nbsp;    } else if (always_peaceful(mtmp-&gt;data)) {
&nbsp;        int absmal = abs(mal);
&nbsp;        if (mtmp-&gt;mpeaceful)
&nbsp;            mtmp-&gt;malign = -3 * max(5, absmal);
&nbsp;        else
&nbsp;            mtmp-&gt;malign = 3 * max(5, absmal); /* renegade */
&nbsp;    } else if (always_hostile(mtmp-&gt;data)) {
&nbsp;        int absmal = abs(mal);
&nbsp;        if (coaligned)
&nbsp;            mtmp-&gt;malign = 0;
&nbsp;        else
&nbsp;            mtmp-&gt;malign = max(5, absmal);
&nbsp;    } else if (coaligned) {
&nbsp;        int absmal = abs(mal);
&nbsp;        if (mtmp-&gt;mpeaceful)
&nbsp;            mtmp-&gt;malign = -3 * max(3, absmal);
&nbsp;        else /* renegade */
&nbsp;            mtmp-&gt;malign = max(3, absmal);
&nbsp;    } else /* not coaligned and therefore hostile */
&nbsp;        mtmp-&gt;malign = abs(mal);
&nbsp;}
&nbsp;
newmcorpsenm
&nbsp;/* allocate a new mcorpsenm field for a monster; only need mextra itself */
&nbsp;void
&nbsp;newmcorpsenm(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (!mtmp-&gt;mextra)
&nbsp;        mtmp-&gt;mextra = newmextra();
&nbsp;    MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
&nbsp;}
&nbsp;
freemcorpsenm
&nbsp;/* release monster's mcorpsenm field; basically a no-op */
&nbsp;void
&nbsp;freemcorpsenm(mtmp)
&nbsp;struct monst *mtmp;
&nbsp;{
&nbsp;    if (has_mcorpsenm(mtmp))
&nbsp;        MCORPSENM(mtmp) = NON_PM;
&nbsp;}
&nbsp;
&nbsp;static NEARDATA char syms[] = {
&nbsp;    MAXOCLASSES,  MAXOCLASSES + 1, RING_CLASS,   WAND_CLASS,   WEAPON_CLASS,
&nbsp;    FOOD_CLASS,   COIN_CLASS,      SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
&nbsp;    AMULET_CLASS, TOOL_CLASS,      ROCK_CLASS,   GEM_CLASS,    SPBOOK_CLASS,
&nbsp;    S_MIMIC_DEF,  S_MIMIC_DEF,
&nbsp;};
&nbsp;
set_mimic_sym
&nbsp;void
&nbsp;set_mimic_sym(mtmp)
&nbsp;register struct monst *mtmp;
&nbsp;{
&nbsp;    int typ, roomno, rt;
&nbsp;    unsigned appear, ap_type;
&nbsp;    int s_sym;
&nbsp;    struct obj *otmp;
&nbsp;    int mx, my;
&nbsp;
&nbsp;    if (!mtmp || Protection_from_shape_changers)
&nbsp;        return;
&nbsp;    mx = mtmp-&gt;mx;
&nbsp;    my = mtmp-&gt;my;
&nbsp;    typ = levl[mx][my].typ;
&nbsp;    /* only valid for INSIDE of room */
&nbsp;    roomno = levl[mx][my].roomno - ROOMOFFSET;
&nbsp;    if (roomno &gt;= 0)
&nbsp;        rt = rooms[roomno].rtype;
&nbsp;#ifdef SPECIALIZATION
&nbsp;    else if (IS_ROOM(typ))
&nbsp;        rt = OROOM, roomno = 0;
&nbsp;#endif
&nbsp;    else
&nbsp;        rt = 0; /* roomno &lt; 0 case for GCC_WARN */
&nbsp;
&nbsp;    if (OBJ_AT(mx, my)) {
&nbsp;        ap_type = M_AP_OBJECT;
&nbsp;        appear = level.objects[mx][my]-&gt;otyp;
&nbsp;    } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
&nbsp;        ap_type = M_AP_FURNITURE;
&nbsp;        /*
&nbsp;         *  If there is a wall to the left that connects to this
&nbsp;         *  location, then the mimic mimics a horizontal closed door.
&nbsp;         *  This does not allow doors to be in corners of rooms.
&nbsp;         *  Since rogue has no closed doors, mimic a wall there
&nbsp;         *  (yes, mimics can end up on this level by various means).
&nbsp;         */
&nbsp;        if (mx != 0 &amp;&amp; (levl[mx - 1][my].typ == HWALL
&nbsp;                        || levl[mx - 1][my].typ == TLCORNER
&nbsp;                        || levl[mx - 1][my].typ == TRWALL
&nbsp;                        || levl[mx - 1][my].typ == BLCORNER
&nbsp;                        || levl[mx - 1][my].typ == TDWALL
&nbsp;                        || levl[mx - 1][my].typ == CROSSWALL
&nbsp;                        || levl[mx - 1][my].typ == TUWALL))
&nbsp;            appear = Is_rogue_level(&amp;u.uz) ? S_hwall : S_hcdoor;
&nbsp;        else
&nbsp;            appear = Is_rogue_level(&amp;u.uz) ? S_vwall : S_vcdoor;
&nbsp;        if (!mtmp-&gt;minvis || See_invisible)
&nbsp;            block_point(mx, my); /* vision */
&nbsp;    } else if (level.flags.is_maze_lev &amp;&amp; !In_sokoban(&amp;u.uz) &amp;&amp; rn2(2)) {
&nbsp;        ap_type = M_AP_OBJECT;
&nbsp;        appear = STATUE;
&nbsp;    } else if (roomno &lt; 0 &amp;&amp; !t_at(mx, my)) {
&nbsp;        ap_type = M_AP_OBJECT;
&nbsp;        appear = BOULDER;
&nbsp;        if (!mtmp-&gt;minvis || See_invisible)
&nbsp;            block_point(mx, my); /* vision */
&nbsp;    } else if (rt == ZOO || rt == VAULT) {
&nbsp;        ap_type = M_AP_OBJECT;
&nbsp;        appear = GOLD_PIECE;
&nbsp;    } else if (rt == DELPHI) {
&nbsp;        if (rn2(2)) {
&nbsp;            ap_type = M_AP_OBJECT;
&nbsp;            appear = STATUE;
&nbsp;        } else {
&nbsp;            ap_type = M_AP_FURNITURE;
&nbsp;            appear = S_fountain;
&nbsp;        }
&nbsp;    } else if (rt == TEMPLE) {
&nbsp;        ap_type = M_AP_FURNITURE;
&nbsp;        appear = S_altar;
&nbsp;        /*
&nbsp;         * We won't bother with beehives, morgues, barracks, throne rooms
&nbsp;         * since they shouldn't contain too many mimics anyway...
&nbsp;         */
&nbsp;    } else if (rt &gt;= SHOPBASE) {
&nbsp;        s_sym = get_shop_item(rt - SHOPBASE);
&nbsp;        if (s_sym &lt; 0) {
&nbsp;            ap_type = M_AP_OBJECT;
&nbsp;            appear = -s_sym;
&nbsp;        } else {
&nbsp;            if (s_sym == RANDOM_CLASS)
&nbsp;                s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
&nbsp;            goto assign_sym;
&nbsp;        }
&nbsp;    } else {
&nbsp;        s_sym = syms[rn2((int) sizeof(syms))];
&nbsp;    assign_sym:
&nbsp;        if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
&nbsp;            ap_type = M_AP_FURNITURE;
&nbsp;            appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
&nbsp;        } else {
&nbsp;            ap_type = M_AP_OBJECT;
&nbsp;            if (s_sym == S_MIMIC_DEF) {
&nbsp;                appear = STRANGE_OBJECT;
&nbsp;            } else if (s_sym == COIN_CLASS) {
&nbsp;                appear = GOLD_PIECE;
&nbsp;            } else {
&nbsp;                otmp = mkobj((char) s_sym, FALSE);
&nbsp;                appear = otmp-&gt;otyp;
&nbsp;                /* make sure container contents are free'ed */
&nbsp;                obfree(otmp, (struct obj *) 0);
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;    mtmp-&gt;m_ap_type = ap_type;
&nbsp;    mtmp-&gt;mappearance = appear;
&nbsp;    if (ap_type == M_AP_OBJECT &amp;&amp; (appear == STATUE || appear == CORPSE
&nbsp;                                   || appear == FIGURINE || appear == EGG)) {
&nbsp;        newmcorpsenm(mtmp);
&nbsp;        MCORPSENM(mtmp) = rndmonnum();
&nbsp;        if (appear == EGG &amp;&amp; !can_be_hatched(MCORPSENM(mtmp)))
&nbsp;            MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
&nbsp;    }
&nbsp;}
&nbsp;
bagotricks
&nbsp;/* release monster from bag of tricks; return number of monsters created */
&nbsp;int
&nbsp;bagotricks(bag, tipping, seencount)
&nbsp;struct obj *bag;
&nbsp;boolean tipping; /* caller emptying entire contents; affects shop handling */
&nbsp;int *seencount;  /* secondary output */
&nbsp;{
&nbsp;    int moncount = 0;
&nbsp;
&nbsp;    if (!bag || bag-&gt;otyp != BAG_OF_TRICKS) {
&nbsp;        impossible("bad bag o' tricks");
&nbsp;    } else if (bag-&gt;spe &lt; 1) {
&nbsp;        /* if tipping known empty bag, give normal empty container message */
&nbsp;        pline1((tipping &amp;&amp; bag-&gt;cknown) ? "It's empty." : nothing_happens);
&nbsp;        /* now known to be empty if sufficiently discovered */
&nbsp;        if (bag-&gt;dknown &amp;&amp; objects[bag-&gt;otyp].oc_name_known)
&nbsp;            bag-&gt;cknown = 1;
&nbsp;    } else {
&nbsp;        struct monst *mtmp;
&nbsp;        int creatcnt = 1, seecount = 0;
&nbsp;
&nbsp;        consume_obj_charge(bag, !tipping);
&nbsp;
&nbsp;        if (!rn2(23))
&nbsp;            creatcnt += rnd(7);
&nbsp;        do {
&nbsp;            mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
&nbsp;            if (mtmp) {
&nbsp;                ++moncount;
&nbsp;                if (canspotmon(mtmp))
&nbsp;                    ++seecount;
&nbsp;            }
&nbsp;        } while (--creatcnt &gt; 0);
&nbsp;        if (seecount) {
&nbsp;            if (seencount)
&nbsp;                *seencount += seecount;
&nbsp;            if (bag-&gt;dknown)
&nbsp;                makeknown(BAG_OF_TRICKS);
&nbsp;        } else if (!tipping) {
&nbsp;            pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
&nbsp;        }
&nbsp;    }
&nbsp;    return moncount;
&nbsp;}
&nbsp;
&nbsp;/*makemon.c*/

</deity></ctype.h>