home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / src / makemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  40.9 KB  |  1,567 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)makemon.c    3.1    93/06/26    */
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "epri.h"
  7. #include "emin.h"
  8. #ifdef REINCARNATION
  9. # include <ctype.h>
  10. #endif
  11.  
  12. STATIC_VAR NEARDATA struct monst zeromonst;
  13.  
  14. #define uncommon(ptr) \
  15.     (((ptr)->geno & (G_GENOD | G_EXTINCT | G_NOGEN | G_UNIQ)) || \
  16.      (!Inhell ? ((ptr)->geno & G_HELL) : ((ptr)->maligntyp > A_NEUTRAL)))
  17.  
  18. #ifdef OVL0
  19. static boolean NDECL(cmavail);
  20. static int FDECL(align_shift, (struct permonst *));
  21. #endif /* OVL0 */
  22. STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
  23. STATIC_DCL void FDECL(m_initgrp,(struct monst *,int,int,int));
  24. STATIC_DCL void FDECL(m_initthrow,(struct monst *,int,int));
  25. STATIC_DCL void FDECL(m_initweap,(struct monst *));
  26. #ifdef OVL1
  27. static void FDECL(m_initinv,(struct monst *));
  28. #endif /* OVL1 */
  29.  
  30. extern int monstr[];
  31.  
  32. #define m_initsgrp(mtmp, x, y)    m_initgrp(mtmp, x, y, 3)
  33. #define m_initlgrp(mtmp, x, y)    m_initgrp(mtmp, x, y, 10)
  34. #define toostrong(monindx, lev) (monstr[monindx] > lev)
  35. #define tooweak(monindx, lev)    (monstr[monindx] < lev)
  36.  
  37. #ifdef OVLB
  38. boolean
  39. is_home_elemental(ptr)
  40. register struct permonst *ptr;
  41. {
  42.     if (ptr->mlet == S_ELEMENTAL)
  43.         switch (monsndx(ptr)) {
  44.         case PM_AIR_ELEMENTAL: return Is_airlevel(&u.uz);
  45.         case PM_FIRE_ELEMENTAL: return Is_firelevel(&u.uz);
  46.         case PM_EARTH_ELEMENTAL: return Is_earthlevel(&u.uz);
  47.         case PM_WATER_ELEMENTAL: return Is_waterlevel(&u.uz);
  48.         }
  49.     return FALSE;
  50. }
  51.  
  52. /*
  53.  * Return true if the given monster cannot exist on this elemental level.
  54.  */
  55. STATIC_OVL boolean
  56. wrong_elem_type(ptr)
  57.     register struct permonst *ptr;
  58. {
  59.     if (ptr->mlet == S_ELEMENTAL) {
  60.     return((boolean)(!is_home_elemental(ptr)));
  61.     } else if (Is_earthlevel(&u.uz)) {
  62.     /* no restrictions? */
  63.     } else if (Is_waterlevel(&u.uz)) {
  64.     /* just monsters that can swim */
  65.     if(!is_swimmer(ptr)) return TRUE;
  66.     } else if (Is_firelevel(&u.uz)) {
  67.     if(!resists_fire(ptr)) return TRUE;
  68.     } else if (Is_airlevel(&u.uz)) {
  69.     if(!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
  70.        && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
  71.         return TRUE;
  72.     }
  73.     return FALSE;
  74. }
  75.  
  76. STATIC_OVL void
  77. m_initgrp(mtmp, x, y, n)    /* make a group just like mtmp */
  78. register struct monst *mtmp;
  79. register int x, y, n;
  80. {
  81.     coord mm;
  82.     register int cnt = rnd(n);
  83.     struct monst *mon;
  84.  
  85. /*
  86.  *    Temporary kludge to cut down on swarming at lower character levels
  87.  *    till we can get this game a little more balanced. [mrs]
  88.  */
  89.     cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
  90.     if(!cnt) cnt++;
  91.  
  92.     mm.x = x;
  93.     mm.y = y;
  94.     while(cnt--) {
  95.         if (peace_minded(mtmp->data)) continue;
  96.         /* Don't create groups of peaceful monsters since they'll get
  97.          * in our way.  If the monster has a percentage chance so some
  98.          * are peaceful and some are not, the result will just be a
  99.          * smaller group.
  100.          */
  101.         if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
  102.             mon = makemon(mtmp->data, mm.x, mm.y);
  103.             mon->mpeaceful = FALSE;
  104.             set_malign(mon);
  105.             /* Undo the second peace_minded() check in makemon(); if the
  106.              * monster turned out to be peaceful the first time we
  107.              * didn't create it at all; we don't want a second check.
  108.              */
  109.         }
  110.     }
  111. }
  112.  
  113. STATIC_OVL
  114. void
  115. m_initthrow(mtmp,otyp,oquan)
  116. struct monst *mtmp;
  117. int otyp,oquan;
  118. {
  119.     register struct obj *otmp;
  120.  
  121.     otmp = mksobj(otyp, TRUE, FALSE);
  122.     otmp->quan = (long) rn1(oquan, 3);
  123.     otmp->owt = weight(otmp);
  124.     if (otyp == ORCISH_ARROW) otmp->opoisoned = TRUE;
  125.     mpickobj(mtmp, otmp);
  126. }
  127.  
  128. #endif /* OVLB */
  129. #ifdef OVL2
  130.  
  131. STATIC_OVL void
  132. m_initweap(mtmp)
  133. register struct monst *mtmp;
  134. {
  135.     register struct permonst *ptr = mtmp->data;
  136.     register int mm = monsndx(ptr);
  137. #ifdef REINCARNATION
  138.     if (Is_rogue_level(&u.uz)) return;
  139. #endif
  140. /*
  141.  *    first a few special cases:
  142.  *
  143.  *        giants get a boulder to throw sometimes.
  144.  *        ettins get clubs
  145.  *        kobolds get darts to throw
  146.  *        centaurs get some sort of bow & arrows or bolts
  147.  *        soldiers get all sorts of things.
  148.  *        kops get clubs & cream pies.
  149.  */
  150.     switch (mtmp->data->mlet) {
  151.         case S_GIANT:
  152.         if (rn2(2)) (void)mongets(mtmp, (ptr != &mons[PM_ETTIN]) ?
  153.                     BOULDER : CLUB);
  154.         break;
  155.         case S_HUMAN:
  156.         if(is_mercenary(ptr)) {
  157.             int w1 = 0, w2 = 0;
  158.             switch (mm) {
  159.  
  160.             case PM_WATCHMAN:
  161. #ifdef ARMY
  162.             case PM_SOLDIER:
  163. #endif
  164.               if (!rn2(3)) {
  165.                   w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
  166.                   w2 = rn2(2) ? DAGGER : KNIFE;
  167.               } else w1 = rn2(2) ? SPEAR : SHORT_SWORD;
  168.               break;
  169. #ifdef ARMY
  170.             case PM_SERGEANT:
  171.               w1 = rn2(2) ? FLAIL : MACE;
  172.               break;
  173.             case PM_LIEUTENANT:
  174.               w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
  175.               break;
  176.             case PM_CAPTAIN:
  177. #endif
  178.             case PM_WATCH_CAPTAIN:
  179.               w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
  180.               break;
  181.             default:
  182.               if (!rn2(4)) w1 = DAGGER;
  183.               if (!rn2(7)) w2 = SPEAR;
  184.               break;
  185.             }
  186.             if (w1) (void)mongets(mtmp, w1);
  187.             if (!w2 && w1 != DAGGER && !rn2(4)) w2 = KNIFE;
  188.             if (w2) (void)mongets(mtmp, w2);
  189.         } else if (is_elf(ptr)) {
  190.             if (rn2(2))
  191.             (void) mongets(mtmp,
  192.                    rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
  193.             if (rn2(2)) (void)mongets(mtmp, ELVEN_LEATHER_HELM);
  194.             else if (!rn2(4)) (void)mongets(mtmp, ELVEN_BOOTS);
  195.             if (rn2(2)) (void)mongets(mtmp, ELVEN_DAGGER);
  196.             switch (rn2(3)) {
  197.             case 0:
  198.                 if (!rn2(4)) (void)mongets(mtmp, ELVEN_SHIELD);
  199.                 if (rn2(3)) (void)mongets(mtmp, ELVEN_SHORT_SWORD);
  200.                 (void)mongets(mtmp, ELVEN_BOW);
  201.                 m_initthrow(mtmp, ELVEN_ARROW, 12);
  202.                 break;
  203.             case 1:
  204.                 (void)mongets(mtmp, ELVEN_BROADSWORD);
  205.                 if (rn2(2)) (void)mongets(mtmp, ELVEN_SHIELD);
  206.                 break;
  207.             case 2:
  208.                 if (rn2(2)) {
  209.                 (void)mongets(mtmp, ELVEN_SPEAR);
  210.                 (void)mongets(mtmp, ELVEN_SHIELD);
  211.                 }
  212.                 break;
  213.             }
  214.             if(mtmp->data == &mons[PM_ELVENKING])
  215.             (void)mongets(mtmp, PICK_AXE);
  216.         }
  217.         break;
  218.  
  219.         case S_ANGEL:
  220.         {
  221.             int spe2;
  222.             /* create minion stuff; can't use mongets */
  223.             struct obj *otmp = mksobj(LONG_SWORD, FALSE, FALSE);
  224.  
  225.             /* maybe make it special */
  226.             if(!rn2(20) || is_lord(mtmp->data))
  227.             otmp = oname(otmp, artiname(
  228.                 rn2(2) ? ART_DEMONBANE : ART_SUNSWORD), 0);
  229.             bless(otmp);
  230.             otmp->oerodeproof = TRUE;
  231.             spe2 = rn2(4);
  232.             otmp->spe = max(otmp->spe, spe2);
  233.             mpickobj(mtmp, otmp);
  234.  
  235.             otmp = mksobj(!rn2(4) || is_lord(mtmp->data) ?
  236.                   SHIELD_OF_REFLECTION : LARGE_SHIELD,
  237.                   FALSE, FALSE);
  238.             otmp->cursed = FALSE;
  239.             otmp->oerodeproof = TRUE;
  240.             otmp->spe = 0;
  241.             mpickobj(mtmp, otmp);
  242.         }
  243.         break;
  244.  
  245.         case S_HUMANOID:
  246.         if (mm == PM_HOBBIT) {
  247.             switch (rn2(3)) {
  248.             case 0:
  249.                 (void)mongets(mtmp, DAGGER);
  250.                 break;
  251.             case 1:
  252.                 (void)mongets(mtmp, ELVEN_DAGGER);
  253.                 break;
  254.             case 2:
  255.                 (void)mongets(mtmp, SLING);
  256.                 break;
  257.               }
  258.             if (!rn2(10)) (void)mongets(mtmp, ELVEN_MITHRIL_COAT);
  259.             if (!rn2(10)) (void)mongets(mtmp, DWARVISH_CLOAK);
  260.         } else if (is_dwarf(ptr)) {
  261.             if (rn2(7)) (void)mongets(mtmp, DWARVISH_CLOAK);
  262.             if (rn2(7)) (void)mongets(mtmp, IRON_SHOES);
  263.             if (!rn2(4)) {
  264.             (void)mongets(mtmp, DWARVISH_SHORT_SWORD);
  265.             /* note: you can't use a mattock with a shield */
  266.             if (rn2(2)) (void)mongets(mtmp, DWARVISH_MATTOCK);
  267.             else {
  268.                 (void)mongets(mtmp, AXE);
  269.                 (void)mongets(mtmp, DWARVISH_ROUNDSHIELD);
  270.             }
  271.             (void)mongets(mtmp, DWARVISH_IRON_HELM);
  272.             if (!rn2(3))
  273.                 (void)mongets(mtmp, DWARVISH_MITHRIL_COAT);
  274.             } else {
  275.             (void)mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
  276.             }
  277.         }
  278.         break;
  279. # ifdef KOPS
  280.         case S_KOP:        /* create Keystone Kops with cream pies to
  281.                  * throw. As suggested by KAA.       [MRS]
  282.                  */
  283.         if (!rn2(4)) m_initthrow(mtmp, CREAM_PIE, 2);
  284.         if (!rn2(3)) (void)mongets(mtmp,(rn2(2)) ? CLUB : RUBBER_HOSE);
  285.         break;
  286. # endif
  287.         case S_ORC:
  288.         if(rn2(2)) (void)mongets(mtmp, ORCISH_HELM);
  289.         switch (mm != PM_ORC_CAPTAIN ? mm :
  290.             rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
  291.             case PM_MORDOR_ORC:
  292.             if(!rn2(3)) (void)mongets(mtmp, SCIMITAR);
  293.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHIELD);
  294.             if(!rn2(3)) (void)mongets(mtmp, KNIFE);
  295.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_CHAIN_MAIL);
  296.             break;
  297.             case PM_URUK_HAI:
  298.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_CLOAK);
  299.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHORT_SWORD);
  300.             if(!rn2(3)) (void)mongets(mtmp, IRON_SHOES);
  301.             if(!rn2(3)) {
  302.                 (void)mongets(mtmp, ORCISH_BOW);
  303.                 m_initthrow(mtmp, ORCISH_ARROW, 12);
  304.             }
  305.             if(!rn2(3)) (void)mongets(mtmp, URUK_HAI_SHIELD);
  306.             break;
  307.             default:
  308.             if (mm != PM_ORC_SHAMAN && rn2(2))
  309.               (void)mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
  310.                            ? ORCISH_DAGGER : SCIMITAR);
  311.         }
  312.         break;
  313.         case S_OGRE:
  314.         if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
  315.             (void) mongets(mtmp, BATTLE_AXE);
  316.         break;
  317.         case S_KOBOLD:
  318.         if (!rn2(4)) m_initthrow(mtmp, DART, 12);
  319.         break;
  320.  
  321.         case S_CENTAUR:
  322.         if (rn2(2)) {
  323.             if(ptr == &mons[PM_FOREST_CENTAUR]) {
  324.             (void)mongets(mtmp, BOW);
  325.             m_initthrow(mtmp, ARROW, 12);
  326.             } else {
  327.             (void)mongets(mtmp, CROSSBOW);
  328.             m_initthrow(mtmp, CROSSBOW_BOLT, 12);
  329.             }
  330.         }
  331.         break;
  332.         case S_WRAITH:
  333.         (void)mongets(mtmp, KNIFE);
  334.         (void)mongets(mtmp, LONG_SWORD);
  335.         break;
  336.         case S_ZOMBIE:
  337.         if (!rn2(4)) (void)mongets(mtmp, LEATHER_ARMOR);
  338.         if (!rn2(4))
  339.             (void)mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
  340.         break;
  341.         case S_DEMON:
  342.         switch (mm) {
  343.             case PM_BALROG:
  344.             (void)mongets(mtmp, BULLWHIP);
  345.             (void)mongets(mtmp, BROADSWORD);
  346.             break;
  347.             case PM_ORCUS:
  348.             (void)mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
  349.             break;
  350.             case PM_HORNED_DEVIL:
  351.             (void)mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
  352.             break;
  353.             case PM_ICE_DEVIL:
  354.             if (!rn2(4)) (void)mongets(mtmp, SPEAR);
  355.             break;
  356.             case PM_ASMODEUS:
  357.             (void)mongets(mtmp, WAN_COLD);
  358.             break;
  359.             case PM_DISPATER:
  360.             (void)mongets(mtmp, WAN_STRIKING);
  361.             break;
  362.             case PM_YEENOGHU:
  363.             (void)mongets(mtmp, FLAIL);
  364.             break;
  365.         }
  366.         /* prevent djinnis and mail daemons from leaving objects when
  367.          * they vanish
  368.          */
  369.         if (!is_demon(ptr)) break;
  370.         /* fall thru */
  371. /*
  372.  *    Now the general case, Some chance of getting some type
  373.  *    of weapon for "normal" monsters.  Certain special types
  374.  *    of monsters will get a bonus chance or different selections.
  375.  */
  376.         default:
  377.           {
  378.         int bias;
  379.         
  380.         bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
  381.         switch(rnd(14 - (2 * bias))) {
  382.             case 1:
  383.             if(strongmonst(ptr)) (void) mongets(mtmp, BATTLE_AXE);
  384.             else m_initthrow(mtmp, DART, 12);
  385.             break;
  386.             case 2:
  387.             if(strongmonst(ptr))
  388.                 (void) mongets(mtmp, TWO_HANDED_SWORD);
  389.             else {
  390.                 (void) mongets(mtmp, CROSSBOW);
  391.                 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
  392.             }
  393.             break;
  394.             case 3:
  395.             (void) mongets(mtmp, BOW);
  396.             m_initthrow(mtmp, ARROW, 12);
  397.             break;
  398.             case 4:
  399.             if(strongmonst(ptr)) (void) mongets(mtmp, LONG_SWORD);
  400.             else m_initthrow(mtmp, DAGGER, 3);
  401.             break;
  402.             case 5:
  403.             if(strongmonst(ptr))
  404.                 (void) mongets(mtmp, LUCERN_HAMMER);
  405.             else (void) mongets(mtmp, AKLYS);
  406.             break;
  407.             default:
  408.             break;
  409.         }
  410.           }
  411.           break;
  412.     }
  413. #ifdef MUSE
  414.     if ((int) mtmp->m_lev > rn2(75))
  415.         (void) mongets(mtmp, rnd_offensive_item(mtmp));
  416. #endif
  417. }
  418.  
  419. #endif /* OVL2 */
  420. #ifdef OVL1
  421.  
  422. static void
  423. m_initinv(mtmp)
  424. register struct    monst    *mtmp;
  425. {
  426.     register int cnt;
  427.     register struct obj *otmp;
  428.     register struct permonst *ptr = mtmp->data;
  429. #ifdef REINCARNATION
  430.     if (Is_rogue_level(&u.uz)) return;
  431. #endif
  432. /*
  433.  *    Soldiers get armour & rations - armour approximates their ac.
  434.  *    Nymphs may get mirror or potion of object detection.
  435.  */
  436.     switch(ptr->mlet) {
  437.  
  438.         case S_HUMAN:
  439.         if(is_mercenary(ptr)) {
  440.             register int mac;
  441. #ifdef MUSE
  442.             switch(monsndx(ptr)) {
  443.             case PM_GUARD: mac = -1; break;
  444. # ifdef ARMY
  445.             case PM_SOLDIER: mac = 3; break;
  446.             case PM_SERGEANT: mac = 0; break;
  447.             case PM_LIEUTENANT: mac = -2; break;
  448.             case PM_CAPTAIN: mac = -3; break;
  449. # endif
  450.             case PM_WATCHMAN: mac = 3; break;
  451.             case PM_WATCH_CAPTAIN: mac = -2; break;
  452.             default: impossible("odd mercenary %d?", monsndx(ptr));
  453.                 mac = 0;
  454.                 break;
  455.             }
  456. #else
  457.             mac = ptr->ac;
  458. #endif
  459.  
  460.             if (mac < -1 && rn2(5))
  461.             mac += 7 + mongets(mtmp, (rn2(5)) ?
  462.                        PLATE_MAIL : CRYSTAL_PLATE_MAIL);
  463.             else if (mac < 3 && rn2(5))
  464.             mac += 6 + mongets(mtmp, (rn2(3)) ?
  465.                        SPLINT_MAIL : BANDED_MAIL);
  466.             else if (rn2(5))
  467.             mac += 3 + mongets(mtmp, (rn2(3)) ?
  468.                        RING_MAIL : STUDDED_LEATHER_ARMOR);
  469.             else
  470.             mac += 2 + mongets(mtmp, LEATHER_ARMOR);
  471.  
  472.             if (mac < 10 && rn2(3))
  473.             mac += 1 + mongets(mtmp, HELMET);
  474.             else if (mac < 10 && rn2(2))
  475.             mac += 1 + mongets(mtmp, DENTED_POT);
  476.             if (mac < 10 && rn2(3))
  477.             mac += 1 + mongets(mtmp, SMALL_SHIELD);
  478.             else if (mac < 10 && rn2(2))
  479.             mac += 2 + mongets(mtmp, LARGE_SHIELD);
  480.             if (mac < 10 && rn2(3))
  481.             mac += 1 + mongets(mtmp, LOW_BOOTS);
  482.             else if (mac < 10 && rn2(2))
  483.             mac += 2 + mongets(mtmp, HIGH_BOOTS);
  484.             if (mac < 10 && rn2(3))
  485.             mac += 1 + mongets(mtmp, LEATHER_GLOVES);
  486.             else if (mac < 10 && rn2(2))
  487.             mac += 1 + mongets(mtmp, ELVEN_CLOAK);
  488.  
  489. #ifndef MUSE
  490.             if (mac != 10 && rn2(5)) {    /* make up the difference */
  491.             otmp = mksobj(RIN_PROTECTION, FALSE, FALSE);
  492.             otmp->spe = (10 - mac + rn2(3) - rn2(3));
  493.             if(otmp->spe < 0) curse(otmp);
  494.             mpickobj(mtmp, otmp);
  495.             }
  496. #endif
  497. #ifdef ARMY
  498.             if(ptr != &mons[PM_GUARD] &&
  499.             ptr != &mons[PM_WATCHMAN] &&
  500.             ptr != &mons[PM_WATCH_CAPTAIN]) {
  501.             if (!rn2(3)) (void) mongets(mtmp, K_RATION);
  502.             if (!rn2(2)) (void) mongets(mtmp, C_RATION);
  503. # ifdef MUSE
  504.             if (ptr != &mons[PM_SOLDIER] && !rn2(3))
  505.                 (void) mongets(mtmp, BUGLE);
  506. # endif
  507.             } else
  508. #endif
  509.                if (ptr == &mons[PM_WATCHMAN] && rn2(3))
  510.                 (void) mongets(mtmp, TIN_WHISTLE);
  511.         } else if (ptr == &mons[PM_SHOPKEEPER]) {
  512.             (void) mongets(mtmp,SKELETON_KEY);
  513.         }
  514.         break;
  515.  
  516.         case S_NYMPH:
  517.         if(!rn2(2)) (void) mongets(mtmp, MIRROR);
  518.         if(!rn2(2)) (void) mongets(mtmp, POT_OBJECT_DETECTION);
  519.         break;
  520.  
  521.         case S_GIANT:
  522.         if (ptr == &mons[PM_MINOTAUR])
  523.             (void) mongets(mtmp, WAN_DIGGING);
  524.         else if (is_giant(ptr)) {
  525.             for(cnt = rn2((int)(mtmp->m_lev / 2)); cnt; cnt--) {
  526.                 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL,LUCKSTONE-1),FALSE,FALSE);
  527.                 otmp->quan = (long) rn1(2, 3);
  528.                 otmp->owt = weight(otmp);
  529.                 mpickobj(mtmp, otmp);
  530.             }
  531.         }
  532.         break;
  533.         case S_WRAITH:
  534.         if (ptr == &mons[PM_NAZGUL]) {
  535.             otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
  536.             curse(otmp);
  537.             mpickobj(mtmp, otmp);
  538.         }
  539.         break;
  540.         case S_QUANTMECH:
  541.         if (!rn2(20)) {
  542.             struct obj *cat;
  543.  
  544.             otmp = mksobj(LARGE_BOX, FALSE, FALSE);
  545.     /* actually, whether this is a corpse or a live cat shouldn't
  546.        really be decided until the box is opened... */
  547.             cat = mksobj(CORPSE, FALSE, FALSE);
  548.             cat->corpsenm = PM_HOUSECAT;
  549.             cat->owt = weight(cat);
  550.             cat = oname(cat, "Schroedinger's Cat", FALSE);
  551.             cat->nobj = otmp->cobj;
  552.             otmp->cobj = cat;
  553.             otmp->owt = weight(otmp);
  554.             mpickobj(mtmp, otmp);
  555.         }
  556.         break;
  557.         case S_LEPRECHAUN:
  558.         mtmp->mgold = (long) d(level_difficulty(), 30);
  559.         break;
  560.         default:
  561.         break;
  562.     }
  563.  
  564. #ifdef ARMY    /* ordinary soldiers rarely have access to magic (or gold :-) */
  565.     if (ptr == &mons[PM_SOLDIER] && rn2(13)) return;
  566. #endif
  567. #ifdef MUSE
  568.     if ((int) mtmp->m_lev > rn2(50))
  569.         (void) mongets(mtmp, rnd_defensive_item(mtmp));
  570.     if ((int) mtmp->m_lev > rn2(100))
  571.         (void) mongets(mtmp, rnd_misc_item(mtmp));
  572. #endif
  573.     if (likes_gold(ptr) && !mtmp->mgold && !rn2(5))
  574.         mtmp->mgold =
  575.               (long) d(level_difficulty(), mtmp->minvent ? 5 : 10);
  576. }
  577.  
  578. /*
  579.  * called with [x,y] = coordinates;
  580.  *    [0,0] means anyplace
  581.  *    [u.ux,u.uy] means: near player (if !in_mklev)
  582.  *
  583.  *    In case we make a monster group, only return the one at [x,y].
  584.  */
  585. struct monst *
  586. makemon(ptr, x, y)
  587. register struct permonst *ptr;
  588. register int    x, y;
  589. {
  590.     register struct monst *mtmp;
  591.     register int    ct;
  592.     boolean anything = (!ptr);
  593.     boolean byyou = (x == u.ux && y == u.uy);
  594.  
  595.     /* if caller wants random location, do it here */
  596.     if(x == 0 && y == 0) {
  597.         int tryct = 0;    /* careful with bigrooms */
  598.         do {
  599.             x = rn1(COLNO-3,2);
  600.             y = rn2(ROWNO);
  601.         } while(!goodpos(x, y, (struct monst *)0, ptr) ||
  602.             (!in_mklev && tryct++ < 50 && cansee(x, y)));
  603.     } else if (byyou && !in_mklev) {
  604.         coord bypos;
  605.  
  606.         if(enexto(&bypos, u.ux, u.uy, ptr)) {
  607.             x = bypos.x;
  608.             y = bypos.y;
  609.         } else
  610.             return((struct monst *)0);
  611.     }
  612.  
  613.     /* if a monster already exists at the position, return */
  614.     if(MON_AT(x, y))
  615.         return((struct monst *) 0);
  616.  
  617.     if(ptr){
  618.         /* if you are to make a specific monster and it has
  619.            already been genocided, return */
  620.         if(ptr->geno & G_GENOD) return((struct monst *) 0);
  621.     } else {
  622.         /* make a random (common) monster that can survive here.
  623.          * (the special levels ask for random monsters at specific
  624.          * positions, causing mass drowning on the medusa level,
  625.          * for instance.)
  626.          */
  627.         int tryct = 0;    /* maybe there are no good choices */
  628.         do {
  629.             if(!(ptr = rndmonst())) {
  630. #ifdef DEBUG
  631.                 pline("Warning: no monster.");
  632. #endif
  633.                 return((struct monst *) 0);    /* no more monsters! */
  634.             }
  635.         } while(!goodpos(x, y, (struct monst *)0, ptr) && tryct++ < 50);
  636.     }
  637.     /* if it's unique, don't ever make it again */
  638.     if (ptr->geno & G_UNIQ) ptr->geno |= G_EXTINCT;
  639.  
  640.     mtmp = newmonst(ptr->pxlth);
  641.     *mtmp = zeromonst;        /* clear all entries in structure */
  642.     for(ct = 0; ct < ptr->pxlth; ct++)
  643.         ((char *) &(mtmp->mextra[0]))[ct] = 0;
  644.     mtmp->nmon = fmon;
  645.     fmon = mtmp;
  646.     mtmp->m_id = flags.ident++;
  647.     mtmp->data = ptr;
  648.     mtmp->mxlth = ptr->pxlth;
  649.  
  650.     mtmp->m_lev = adj_lev(ptr);
  651.     if (is_golem(ptr))
  652.         mtmp->mhpmax = mtmp->mhp = golemhp(monsndx(ptr));
  653.     else if (is_rider(ptr)) {
  654.         /* We want low HP, but a high mlevel so they can attack well */
  655.         mtmp->mhpmax = mtmp->mhp = d(10,8);
  656.     } else if(ptr->mlevel > 49) {
  657.         /* "special" fixed hp monster
  658.          * the hit points are encoded in the mlevel in a somewhat strange
  659.          * way to fit in the 50..127 positive range of a signed character
  660.          * above the 1..49 that indicate "normal" monster levels */
  661.         mtmp->mhpmax = mtmp->mhp = 2*(ptr->mlevel - 6);
  662.         mtmp->m_lev = mtmp->mhp / 4;    /* approximation */
  663.     } else if((ptr->mlet == S_DRAGON) && (ptr >= &mons[PM_GRAY_DRAGON]))
  664.         mtmp->mhpmax = mtmp->mhp = mtmp->m_lev*8;
  665.     else if(!mtmp->m_lev) mtmp->mhpmax = mtmp->mhp = rnd(4);
  666.     else if(is_home_elemental(ptr))
  667.         mtmp->mhpmax = mtmp->mhp = 3 * d((int)mtmp->m_lev, 8);
  668.     else mtmp->mhpmax = mtmp->mhp = d((int)mtmp->m_lev, 8);
  669.  
  670.     if (is_female(ptr)) mtmp->female = TRUE;
  671.     else if (is_male(ptr)) mtmp->female = FALSE;
  672.     else mtmp->female = rn2(2);    /* ignored for neuters */
  673.  
  674.     place_monster(mtmp, x, y);
  675.     mtmp->mcansee = mtmp->mcanmove = TRUE;
  676.     mtmp->mpeaceful = peace_minded(ptr);
  677.  
  678.     switch(ptr->mlet) {
  679.         case S_MIMIC:
  680.             set_mimic_sym(mtmp);
  681.             break;
  682.         case S_SPIDER:
  683.         case S_SNAKE:
  684.             if(in_mklev)
  685.                 if(x && y)
  686.                 (void) mkobj_at(0, x, y, TRUE);
  687.             if(hides_under(ptr) && OBJ_AT(x, y))
  688.                 mtmp->mundetected = TRUE;
  689.             break;
  690.         case S_STALKER:
  691.         case S_EEL:
  692.             mtmp->minvis = TRUE;
  693.             break;
  694.         case S_LEPRECHAUN:
  695.             mtmp->msleep = TRUE;
  696.             break;
  697.         case S_JABBERWOCK:
  698.         case S_NYMPH:
  699.             if(rn2(5) && !u.uhave.amulet) mtmp->msleep = TRUE;
  700.             break;
  701.         case S_ORC:
  702.             if(pl_character[0] == 'E') mtmp->mpeaceful = FALSE;
  703.             break;
  704.         case S_UNICORN:
  705.             if (sgn(u.ualign.type) == sgn(ptr->maligntyp))
  706.                 mtmp->mpeaceful = TRUE;
  707.             break;
  708.     }
  709.     if (ptr == &mons[PM_CHAMELEON]) {
  710.         /* If you're protected with a ring, don't create
  711.          * any shape-changing chameleons -dgk
  712.          */
  713.         if (Protection_from_shape_changers)
  714.             mtmp->cham = FALSE;
  715.         else {
  716.             mtmp->cham = TRUE;
  717.             (void) newcham(mtmp, rndmonst());
  718.         }
  719.     } else if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
  720.         mtmp->iswiz = TRUE;
  721.         flags.no_of_wizards++;
  722.     } else if (ptr == &mons[PM_VLAD_THE_IMPALER])
  723.         (void) mongets(mtmp, CANDELABRUM_OF_INVOCATION);
  724. #ifdef MULDGN
  725.     else if (ptr->msound == MS_NEMESIS)
  726.         (void) mongets(mtmp, BELL_OF_OPENING);
  727. #else
  728.     else if (ptr == &mons[PM_MEDUSA])
  729.         (void) mongets(mtmp, BELL_OF_OPENING);
  730. #endif
  731.  
  732.     if(in_mklev) {
  733.         if(((is_ndemon(ptr)) ||
  734.             (ptr == &mons[PM_WUMPUS]) ||
  735.             (ptr == &mons[PM_LONG_WORM]) ||
  736.             (ptr == &mons[PM_GIANT_EEL])) && !u.uhave.amulet && rn2(5))
  737.             mtmp->msleep = TRUE;
  738.     } else {
  739.         if(byyou) {
  740.             newsym(mtmp->mx,mtmp->my);
  741.             set_apparxy(mtmp);
  742.         }
  743.     }
  744.     if(is_dprince(ptr)) {
  745.         mtmp->mpeaceful = mtmp->minvis = TRUE;
  746.         if (uwep && uwep->oartifact == ART_EXCALIBUR)
  747.         mtmp->mpeaceful = mtmp->mtame = FALSE;
  748.     }
  749.     if ( (ptr == &mons[PM_LONG_WORM]) && (mtmp->wormno = get_wormno()) ) {
  750.         /* we can now create worms with tails - 11/91 */
  751.         initworm(mtmp, rn2(5));
  752.         if (count_wsegs(mtmp)) place_worm_tail_randomly(mtmp, x, y);
  753.     }
  754.     set_malign(mtmp);        /* having finished peaceful changes */
  755.     if(anything) {
  756.         if((ptr->geno & G_SGROUP) && rn2(2))
  757.         m_initsgrp(mtmp, mtmp->mx, mtmp->my);
  758.         else if(ptr->geno & G_LGROUP) {
  759.             if(rn2(3))  m_initlgrp(mtmp, mtmp->mx, mtmp->my);
  760.             else        m_initsgrp(mtmp, mtmp->mx, mtmp->my);
  761.         }
  762.     }
  763.  
  764.     if(is_armed(ptr))
  765.         m_initweap(mtmp);    /* equip with weapons / armor */
  766.     m_initinv(mtmp);    /* add on a few special items incl. more armor */
  767. #ifdef MUSE
  768.     m_dowear(mtmp, TRUE);
  769. #endif
  770.  
  771.     if (!in_mklev)
  772.         newsym(mtmp->mx,mtmp->my);    /* make sure the mon shows up */
  773.  
  774.     return(mtmp);
  775. }
  776.  
  777. boolean
  778. enexto(cc, xx, yy, mdat)
  779. coord *cc;
  780. register xchar xx, yy;
  781. struct permonst *mdat;
  782. {
  783.     register xchar x,y;
  784.     coord foo[15], *tfoo;
  785.     int range, i;
  786.     int xmin, xmax, ymin, ymax;
  787.  
  788.     tfoo = foo;
  789.     range = 1;
  790.     do {    /* full kludge action. */
  791.         xmin = max(1, xx-range);
  792.         xmax = min(COLNO-1, xx+range);
  793.         ymin = max(0, yy-range);
  794.         ymax = min(ROWNO-1, yy+range);
  795.  
  796.         for(x = xmin; x <= xmax; x++)
  797.             if(goodpos(x, ymin, (struct monst *)0, mdat)) {
  798.                 tfoo->x = x;
  799. #ifdef MAC_MPW32
  800.                 ( tfoo ) -> y = ymin ;
  801.                 tfoo ++ ;
  802. #else
  803.                 (tfoo++)->y = ymin;
  804. #endif
  805.                 if(tfoo == &foo[15]) goto foofull;
  806.             }
  807.         for(x = xmin; x <= xmax; x++)
  808.             if(goodpos(x, ymax, (struct monst *)0, mdat)) {
  809.                 tfoo->x = x;
  810. #ifdef MAC_MPW32
  811.                 ( tfoo ) -> y = ymax ;
  812.                 tfoo ++ ;
  813. #else
  814.                 (tfoo++)->y = ymax;
  815. #endif
  816.                 if(tfoo == &foo[15]) goto foofull;
  817.             }
  818.         for(y = ymin+1; y < ymax; y++)
  819.             if(goodpos(xmin, y, (struct monst *)0, mdat)) {
  820.                 tfoo->x = xmin;
  821. #ifdef MAC_MPW32
  822.                 ( tfoo ) -> y = y ;
  823.                 tfoo ++ ;
  824. #else
  825.                 (tfoo++)->y = y;
  826. #endif
  827.                 if(tfoo == &foo[15]) goto foofull;
  828.             }
  829.         for(y = ymin+1; y < ymax; y++)
  830.             if(goodpos(xmax, y, (struct monst *)0, mdat)) {
  831.                 tfoo->x = xmax;
  832. #ifdef MAC_MPW32
  833.                 ( tfoo ) -> y = y ;
  834.                 tfoo ++ ;
  835. #else
  836.                 (tfoo++)->y = y;
  837. #endif
  838.                 if(tfoo == &foo[15]) goto foofull;
  839.             }
  840.         range++;
  841.         if(range > ROWNO && range > COLNO) return FALSE;
  842.     } while(tfoo == foo);
  843. foofull:
  844.     i = rn2((int)(tfoo - foo));
  845.     cc->x = foo[i].x;
  846.     cc->y = foo[i].y;
  847.     return TRUE;
  848. }
  849.  
  850. int
  851. goodpos(x, y, mtmp, mdat)
  852. int x,y;
  853. struct monst *mtmp;    /* existing monster being moved, if any */
  854. struct permonst *mdat;
  855. {
  856.     struct monst *mtmp2;
  857.  
  858.     if (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 || MON_AT(x, y))
  859.         return 0;
  860.  
  861.     /* in many cases, we're trying to create a new monster, which
  862.      * can't go on top of the player or any existing monster.
  863.      * however, occasionally we are relocating engravings or objects,
  864.      * which could be colocated and thus get restricted a bit too much.
  865.      * oh well.
  866.      */
  867.     if (x == u.ux && y == u.uy) return 0;
  868.     if ((mtmp2 = m_at(x, y)) && mtmp != mtmp2) return 0;
  869.  
  870.     if (mdat) {
  871.         if (IS_POOL(levl[x][y].typ))
  872.         if (mdat == &playermon &&
  873.             (HLevitation || Wwalking || Amphibious))
  874.             return 1;
  875.         else    return (is_flyer(mdat) || is_swimmer(mdat));
  876.         if (levl[x][y].typ == LAVAPOOL)
  877.         if (mdat == &playermon && (HLevitation))
  878.             return 1;
  879.         else return
  880.             (is_flyer(mdat) || (mdat == &mons[PM_FIRE_ELEMENTAL]));
  881.         if (passes_walls(mdat) && may_passwall(x,y)) return 1;
  882.     }
  883.     if (!ACCESSIBLE(levl[x][y].typ)) return 0;
  884.     if (closed_door(x, y) && (!mdat || !amorphous(mdat)))
  885.         return 0;
  886.     if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat)))
  887.         return 0;
  888.     return 1;
  889. }
  890.  
  891. #endif /* OVL1 */
  892. #ifdef OVLB
  893.  
  894. /*
  895.  * rloc_to()
  896.  *
  897.  * Pulls a monster from its current position and places a monster at
  898.  * a new x and y.  If oldx is 0, then the monster was not in the levels.monsters
  899.  * array.  However, if oldx is 0, oldy may still have a value because mtmp is a
  900.  * migrating_mon.  Worm tails are always placed randomly around the head of
  901.  * the worm.
  902.  */
  903.  
  904. void
  905. rloc_to(mtmp, x, y)
  906.     struct monst *mtmp;
  907.     register int x, y;
  908. {
  909.     register int oldx = mtmp->mx, oldy = mtmp->my;
  910.  
  911.     if(x == mtmp->mx && y == mtmp->my) /* that was easy */
  912.         return;
  913.  
  914.     if (oldx) {                /* "pick up" monster */
  915.         if(mtmp->wormno)
  916.         remove_worm(mtmp);
  917.         else {
  918.         remove_monster(oldx, oldy);
  919.         newsym(oldx, oldy);        /* update old location */
  920.         }
  921.     }
  922.  
  923.     place_monster(mtmp, x, y);        /* put monster down */
  924.  
  925.     if(mtmp->wormno)            /* now put down tail */
  926.         place_worm_tail_randomly(mtmp, x, y);
  927.  
  928.     if(u.ustuck == mtmp){
  929.         if(u.uswallow) {
  930.             u.ux = x;
  931.             u.uy = y;
  932.             docrt();
  933.         } else    u.ustuck = 0;
  934.     }
  935.  
  936.     newsym(x, y);                /* update new location */
  937.     set_apparxy(mtmp);            /* orient monster */
  938. }
  939.  
  940. #endif /* OVLB */
  941. #ifdef OVL2
  942.  
  943. void
  944. rloc(mtmp)
  945.     struct monst *mtmp;
  946. {
  947.     register int x = xupstair, y = yupstair, trycount;
  948.  
  949.     /* if the wiz teleports away to heal, try the up staircase,
  950.        to block the player's escaping before he's healed */
  951.     if (!mtmp->iswiz || !goodpos(x, y, mtmp, mtmp->data)) {
  952.         trycount = 0;
  953.         do {
  954.         x = rn1(COLNO-3,2);
  955.         y = rn2(ROWNO);
  956.         } while(!goodpos(x,y,mtmp,mtmp->data) && ++trycount < 1000);
  957.         /* last ditch attempt to find a good place */
  958.         if (trycount >= 1000) {
  959.         for (x = 2; x < COLNO - 1; x++)
  960.             for (y = 0; y < ROWNO; y++)
  961.             if (goodpos(x,y,mtmp,mtmp->data))
  962.                 goto found_atlast;
  963.         /* level either full of monsters or somehow faulty */
  964.         impossible("rloc(): couldn't relocate monster");
  965.         return;
  966.         }
  967.     }
  968. found_atlast:;
  969.     rloc_to(mtmp, x, y);
  970. }
  971.  
  972. void
  973. rloc_shk(mtmp)   /* to be used when teleporting a shopkeeper */
  974. struct monst *mtmp;
  975. {
  976.     register int x, y, ox, oy, trycount;
  977.  
  978.     if(!mtmp->isshk) return;
  979.     trycount = 0;
  980.     do {
  981.         x = rn1(COLNO-3,2);
  982.         y = rn2(ROWNO);
  983.     } while(!goodpos(x,y,mtmp,mtmp->data) && ++trycount < 1000);
  984.     /* last ditch attempt to find a good place */
  985.     if (trycount >= 1000) {
  986.         for (x = 2; x < COLNO - 1; x++)
  987.             for (y = 0; y < ROWNO; y++)
  988.             if (goodpos(x,y,mtmp,mtmp->data))
  989.                 goto found_ok;
  990.         /* this really shouldn't happen - after all, shopkeeper's
  991.            original position should always be available */
  992.         impossible("rloc_shk(): couldn't relocate shopkeeper");
  993.         return;
  994.     }
  995. found_ok:;
  996.     ox = mtmp->mx;
  997.     oy = mtmp->my;
  998.     rloc_to(mtmp, x, y);
  999.     make_angry_shk(mtmp, ox, oy);
  1000. }
  1001.  
  1002. #endif /* OVL2 */
  1003. #ifdef OVLB
  1004.  
  1005. void
  1006. vloc(mtmp)
  1007. struct monst *mtmp;
  1008. {
  1009.     register struct mkroom *croom = search_special(VAULT);
  1010.     coord c;
  1011.  
  1012.     if(croom && somexy(croom, &c) && goodpos(c.x, c.y, mtmp, mtmp->data)) {
  1013.         rloc_to(mtmp, c.x, c.y);
  1014.         return;
  1015.     }
  1016.     rloc(mtmp);
  1017. }
  1018.  
  1019. #endif /* OVLB */
  1020. #ifdef OVL0
  1021.  
  1022. static boolean
  1023. cmavail()    /* return TRUE if "common" monsters can be generated */
  1024. {
  1025.     struct permonst *ptr;
  1026.  
  1027.     for(ptr = &mons[0]; ptr != &mons[NUMMONS]; ptr++)
  1028.        if(!uncommon(ptr))  return TRUE;
  1029.  
  1030.     return FALSE;
  1031. }
  1032.  
  1033. /*
  1034.  *    shift the probability of a monster's generation by
  1035.  *    comparing the dungeon alignment and monster alignment.
  1036.  *    return an integer in the range of 0-5.
  1037.  */
  1038. static int
  1039. align_shift(ptr)
  1040. register struct permonst *ptr;
  1041. {
  1042.     static NEARDATA long oldmoves = 0L;    /* != 1, starting value of moves */
  1043.     static NEARDATA s_level *lev;
  1044.     register int alshift;
  1045.  
  1046.     if(oldmoves != moves) {
  1047.     lev = Is_special(&u.uz);
  1048.     oldmoves = moves;
  1049.     }
  1050.     switch((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
  1051.     default:    /* just in case */
  1052.     case AM_NONE:    alshift = 0;
  1053.             break;
  1054.     case AM_LAWFUL:    alshift = (ptr->maligntyp+20)/(2*ALIGNWEIGHT);
  1055.             break;
  1056.     case AM_NEUTRAL:    alshift = (20 - abs(ptr->maligntyp))/ALIGNWEIGHT;
  1057.             break;
  1058.     case AM_CHAOTIC:    alshift = (-(ptr->maligntyp-20))/(2*ALIGNWEIGHT);
  1059.             break;
  1060.     }
  1061.     return alshift;
  1062. }
  1063.  
  1064. struct    permonst *
  1065. rndmonst()        /* select a random monster */
  1066. {
  1067.     register struct permonst *ptr;
  1068.     register int i, ct;
  1069.     register int zlevel;
  1070.     static NEARDATA int minmlev, maxmlev, accept;
  1071.     static NEARDATA long oldmoves = 0L;    /* != 1, starting value of moves */
  1072. #ifdef REINCARNATION
  1073.     static NEARDATA boolean upper;
  1074. #endif
  1075.     static NEARDATA boolean elemlevel;
  1076.  
  1077. #ifdef MULDGN
  1078.     if(u.uz.dnum == quest_dnum && (ptr = qt_montype())) return(ptr);
  1079. #endif
  1080.     if(oldmoves != moves) {        /* must recalculate accept */
  1081.         oldmoves = moves;
  1082.         zlevel = level_difficulty();
  1083.         if(!cmavail()) {
  1084. #ifdef DEBUG
  1085.         pline("cmavail() fails!");
  1086. #endif
  1087.         return((struct permonst *) 0);
  1088.         }
  1089.  
  1090.         /* determine the level of the weakest monster to make. */
  1091.         minmlev = zlevel/6;
  1092.         /* determine the level of the strongest monster to make. */
  1093.         maxmlev = (zlevel + u.ulevel)>>1;
  1094. #ifdef REINCARNATION
  1095.         upper = Is_rogue_level(&u.uz);
  1096. #endif
  1097.         elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
  1098. /*
  1099.  *    Find out how many monsters exist in the range we have selected.
  1100.  */
  1101.         accept = 0;
  1102.         for(ct = 0, ptr = &mons[0] ; ptr != &mons[NUMMONS]; ct++, ptr++) {
  1103.         if(tooweak(ct, minmlev) || toostrong(ct, maxmlev))
  1104.             continue;
  1105. #ifdef REINCARNATION
  1106.         if(upper && !isupper(def_monsyms[(int)ptr->mlet])) continue;
  1107. #endif
  1108.         if(elemlevel && wrong_elem_type(ptr)) continue;
  1109.         if(uncommon(ptr)) continue;
  1110.         accept += (ptr->geno & G_FREQ);
  1111.         accept += align_shift(ptr);
  1112.         }
  1113.     }
  1114.  
  1115.     if(!accept) {
  1116. #ifdef DEBUG
  1117.         pline("no accept!");
  1118. #endif
  1119.         return((struct permonst *) 0);
  1120.     }
  1121. /*
  1122.  *    Now, select a monster at random.
  1123.  */
  1124.     ct = rnd(accept);
  1125.     for(i = 0,ptr = &mons[0]; ptr != &mons[NUMMONS] && ct > 0; i++,ptr++) {
  1126.         if(tooweak(i, minmlev) || toostrong(i, maxmlev))
  1127.             continue;
  1128. #ifdef REINCARNATION
  1129.         if(upper & !isupper(def_monsyms[(int)ptr->mlet])) continue;
  1130. #endif
  1131.         if(elemlevel && wrong_elem_type(ptr)) continue;
  1132.         if(uncommon(ptr)) continue;
  1133.         ct -= (ptr->geno & G_FREQ);
  1134.         ct -= align_shift(ptr);
  1135.     }
  1136.     if(ct > 0) {
  1137. #ifdef DEBUG
  1138.         pline("no count!");
  1139. #endif
  1140.         return((struct permonst *) 0);
  1141.     }
  1142.     return(--ptr);    /* subtract extra increment */
  1143. }
  1144.  
  1145. #endif /* OVL0 */
  1146. #ifdef OVL1
  1147.  
  1148. /*    The routine below is used to make one of the multiple types
  1149.  *    of a given monster class.  The second parameter specifies a
  1150.  *    special casing bit mask to allow any of the normal genesis
  1151.  *    masks to be deactivated.  Returns 0 if no monsters
  1152.  *    in that class can be made.
  1153.  */
  1154.  
  1155. struct permonst *
  1156. mkclass(class,spc)
  1157. char    class;
  1158. int    spc;
  1159. {
  1160.     register int    first, last, num = 0;
  1161.     int maxmlev, mask = (G_GENOD | G_EXTINCT | G_NOGEN | G_UNIQ) & ~spc;
  1162.  
  1163.     maxmlev = level_difficulty() >> 1;
  1164.     if(class < 1 || class >= MAXMCLASSES) {
  1165.         impossible("mkclass called with bad class!");
  1166.         return((struct permonst *) 0);
  1167.     }
  1168. /*    Assumption #1:    monsters of a given class are contiguous in the
  1169.  *            mons[] array.
  1170.  */
  1171.     for(first = 0; first < NUMMONS; first++)
  1172.         if (mons[first].mlet == class) break;
  1173.     if (first == NUMMONS) return((struct permonst *) 0);
  1174.  
  1175.     for(last = first; last < NUMMONS && mons[last].mlet == class; last++)
  1176.         if(!(mons[last].geno & mask)) {
  1177.         /* consider it */
  1178.         if(num && toostrong(last, maxmlev) &&
  1179.            monstr[last] != monstr[last-1] && rn2(2)) break;
  1180.         num += mons[last].geno & G_FREQ;
  1181.         }
  1182.  
  1183.     if(!num) return((struct permonst *) 0);
  1184.  
  1185. /*    Assumption #2:    monsters of a given class are presented in ascending
  1186.  *            order of strength.
  1187.  */
  1188.     for(num = rnd(num); num > 0; first++)
  1189.         if(!(mons[first].geno & mask)) {
  1190.         /* skew towards lower value monsters at lower exp. levels */
  1191.         num -= mons[first].geno & G_FREQ;
  1192.         if (num && adj_lev(&mons[first]) > (u.ulevel*2)) {
  1193.             /* but not when multiple monsters are same level */
  1194.             if (mons[first].mlevel != mons[first+1].mlevel)
  1195.             num--;
  1196.         }
  1197.         }
  1198.     first--; /* correct an off-by-one error */
  1199.  
  1200.     return(&mons[first]);
  1201. }
  1202.  
  1203. int
  1204. adj_lev(ptr)    /* adjust strength of monsters based on u.uz and u.ulevel */
  1205. register struct permonst *ptr;
  1206. {
  1207.     int    tmp, tmp2;
  1208.  
  1209.     if((tmp = ptr->mlevel) > 49) return(50); /* "special" demons/devils */
  1210.     tmp2 = (level_difficulty() - tmp);
  1211.     if(tmp2 < 0) tmp--;        /* if mlevel > u.uz decrement tmp */
  1212.     else tmp += (tmp2 / 5);        /* else increment 1 per five diff */
  1213.  
  1214.     tmp2 = (u.ulevel - ptr->mlevel);    /* adjust vs. the player */
  1215.     if(tmp2 > 0) tmp += (tmp2 / 4);        /* level as well */
  1216.  
  1217.     tmp2 = (3 * ((int) ptr->mlevel))/ 2;    /* crude upper limit */
  1218.     return((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
  1219. }
  1220.  
  1221. #endif /* OVL1 */
  1222. #ifdef OVLB
  1223.  
  1224. struct permonst *
  1225. grow_up(mtmp,victim)        /* mon mtmp "grows up" to a bigger version. */
  1226. register struct monst *mtmp;
  1227. register struct monst *victim;
  1228. {
  1229.     register int newtype;
  1230.     register struct permonst *ptr = mtmp->data;
  1231.  
  1232.     if (victim) {
  1233.         if (ptr->mlevel >= 50 || is_golem(ptr) || is_home_elemental(ptr)
  1234.             || is_mplayer(ptr))
  1235.         /* doesn't grow up, has strange hp calculation so might be
  1236.          * weakened by tests below */
  1237.         return ptr;
  1238.  
  1239.         mtmp->mhpmax = mtmp->mhpmax + (1 + rn2((int)victim->m_lev+1));
  1240.         if (mtmp->mhpmax <= (8 * (int)mtmp->m_lev)
  1241.             || (mtmp->m_lev == 0 && mtmp->mhpmax <= 4))
  1242.         /* not ready to grow up */
  1243.         return ptr;
  1244.     }
  1245. #ifdef MUSE
  1246.     /* else it's a gain level potion; always go up a level */
  1247.     else {
  1248.         int foo=rnd(8);
  1249.  
  1250.         mtmp->mhp += foo;
  1251.         mtmp->mhpmax += foo;
  1252.     }
  1253. #endif
  1254.  
  1255.     newtype = little_to_big(monsndx(ptr));
  1256.     if ((int) (++mtmp->m_lev) >= mons[newtype].mlevel
  1257.                     && newtype != monsndx(ptr)) {
  1258.         if (mons[newtype].geno & G_GENOD) { /* allow G_EXTINCT */
  1259.             pline("As %s grows up into %s, %s dies!",
  1260.                 mon_nam(mtmp),
  1261.                 an(mons[newtype].mname),
  1262.                 mon_nam(mtmp));
  1263.             mondied(mtmp);
  1264.             return (struct permonst *)0;
  1265.         }
  1266.         mtmp->data = &mons[newtype];
  1267.         newsym(mtmp->mx, mtmp->my);    /* color may change */
  1268.         mtmp->m_lev = mons[newtype].mlevel;
  1269.     }
  1270.     if (newtype == monsndx(ptr) && victim &&
  1271.         (int) mtmp->m_lev > (3*(int)mtmp->data->mlevel) / 2)
  1272.         mtmp->m_lev = (3*(int)mtmp->data->mlevel) / 2;
  1273.     if (mtmp->m_lev > 0) {
  1274.         if (mtmp->mhp > (int) mtmp->m_lev * 8)
  1275.         mtmp->mhp = mtmp->m_lev * 8;
  1276.         if (mtmp->mhpmax > (int) mtmp->m_lev * 8)
  1277.         mtmp->mhpmax = mtmp->m_lev * 8;
  1278.     }
  1279.     return(mtmp->data);
  1280. }
  1281.  
  1282. #endif /* OVLB */
  1283. #ifdef OVL1
  1284.  
  1285. int
  1286. mongets(mtmp, otyp)
  1287. register struct monst *mtmp;
  1288. register int otyp;
  1289. {
  1290.     register struct obj *otmp;
  1291.  
  1292. #ifdef MUSE
  1293.     if (!otyp) return 0;
  1294. #endif
  1295.     if((otmp = (otyp) ? mksobj(otyp,TRUE,FALSE) : mkobj((char)otyp,FALSE))) {
  1296.         if (mtmp->data->mlet == S_DEMON) {
  1297.         /* demons always get cursed objects */
  1298.         curse(otmp);
  1299.         } else if(is_lminion(mtmp->data)) {
  1300.         /* lawful minions don't get cursed, bad, or rusting objects */
  1301.         otmp->cursed = FALSE;
  1302.         if(otmp->spe < 0) otmp->spe = 0;
  1303.         otmp->oerodeproof = TRUE;
  1304.         } else if(is_mplayer(mtmp->data) && is_sword(otmp))
  1305.             otmp->spe = (3 + rn2(4));
  1306.  
  1307.         if(otmp->otyp == CANDELABRUM_OF_INVOCATION) {
  1308.         otmp->spe = 0;
  1309.         otmp->age = 0L;
  1310.         otmp->lamplit = FALSE;
  1311.         otmp->blessed = otmp->cursed = FALSE;
  1312.         } else if (otmp->otyp == BELL_OF_OPENING) {
  1313.         otmp->blessed = otmp->cursed = FALSE;
  1314.         } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
  1315.         otmp->blessed = FALSE;
  1316.         otmp->cursed = TRUE;
  1317.         }
  1318.  
  1319.         mpickobj(mtmp, otmp);
  1320.         return(otmp->spe);
  1321.     } else return(0);
  1322. }
  1323.  
  1324. #endif /* OVL1 */
  1325. #ifdef OVLB
  1326.  
  1327. int
  1328. golemhp(type)
  1329. int type;
  1330. {
  1331.     switch(type) {
  1332.         case PM_STRAW_GOLEM: return 20;
  1333.         case PM_ROPE_GOLEM: return 30;
  1334.         case PM_LEATHER_GOLEM: return 40;
  1335.         case PM_WOOD_GOLEM: return 50;
  1336.         case PM_FLESH_GOLEM: return 40;
  1337.         case PM_CLAY_GOLEM: return 50;
  1338.         case PM_STONE_GOLEM: return 60;
  1339.         case PM_IRON_GOLEM: return 80;
  1340.         default: return 0;
  1341.     }
  1342. }
  1343.  
  1344. #endif /* OVLB */
  1345. #ifdef OVL1
  1346.  
  1347. /*
  1348.  *    Alignment vs. yours determines monster's attitude to you.
  1349.  *    ( some "animal" types are co-aligned, but also hungry )
  1350.  */
  1351. boolean
  1352. peace_minded(ptr)
  1353. register struct permonst *ptr;
  1354. {
  1355.     aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
  1356.  
  1357.     if (always_peaceful(ptr)) return TRUE;
  1358.     if (always_hostile(ptr)) return FALSE;
  1359. #ifdef MULDGN
  1360.     if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
  1361.         return TRUE;
  1362.     if (ptr->msound == MS_NEMESIS)    return FALSE;
  1363. #endif
  1364.  
  1365.     /* the monster is hostile if its alignment is different from the
  1366.      * player's */
  1367.     if (sgn(mal) != sgn(ual)) return FALSE;
  1368.  
  1369.     /* Negative monster hostile to player with Amulet. */
  1370.     if (mal < A_NEUTRAL && u.uhave.amulet) return FALSE;
  1371.  
  1372.     /* minions are hostile to players that have strayed at all */
  1373.     if (is_minion(ptr)) return((boolean)(u.ualign.record >= 0));
  1374.  
  1375.     /* Last case:  a chance of a co-aligned monster being
  1376.      * hostile.  This chance is greater if the player has strayed
  1377.      * (u.ualign.record negative) or the monster is not strongly aligned.
  1378.      */
  1379.     return((boolean)(!!rn2(16 + (u.ualign.record < -15 ? -15 : u.ualign.record)) &&
  1380.         !!rn2(2 + abs(mal))));
  1381. }
  1382.  
  1383. /* Set malign to have the proper effect on player alignment if monster is
  1384.  * killed.  Negative numbers mean it's bad to kill this monster; positive
  1385.  * numbers mean it's good.  Since there are more hostile monsters than
  1386.  * peaceful monsters, the penalty for killing a peaceful monster should be
  1387.  * greater than the bonus for killing a hostile monster to maintain balance.
  1388.  * Rules:
  1389.  *   it's bad to kill peaceful monsters, potentially worse to kill always-
  1390.  *    peaceful monsters
  1391.  *   it's never bad to kill a hostile monster, although it may not be good
  1392.  */
  1393. void
  1394. set_malign(mtmp)
  1395. struct monst *mtmp;
  1396. {
  1397.     schar mal = mtmp->data->maligntyp;
  1398.     boolean coaligned;
  1399.  
  1400.     if (mtmp->ispriest || mtmp->isminion) {
  1401.         /* some monsters have individual alignments; check them */
  1402.         if (mtmp->ispriest)
  1403.             mal = EPRI(mtmp)->shralign;
  1404.         else if (mtmp->isminion)
  1405.             mal = EMIN(mtmp)->min_align;
  1406.         /* unless alignment is none, set mal to -5,0,5 */
  1407.         /* (see align.h for valid aligntyp values)     */
  1408.         if(mal != A_NONE)
  1409.             mal *= 5;
  1410.     }
  1411.  
  1412.     coaligned = (sgn(mal) == sgn(u.ualign.type));
  1413. #ifdef MULDGN
  1414.     if (mtmp->data->msound == MS_LEADER) {
  1415.         mtmp->malign = -20;
  1416.     } else
  1417. #endif
  1418.           if (mal == A_NONE) {
  1419.         if (mtmp->mpeaceful)
  1420.             mtmp->malign = 0;
  1421.         else
  1422.             mtmp->malign = 20;    /* really hostile */
  1423.     } else if (always_peaceful(mtmp->data)) {
  1424.         int absmal = abs(mal);
  1425.         if (mtmp->mpeaceful)
  1426.             mtmp->malign = -3*max(5,absmal);
  1427.         else
  1428.             mtmp->malign = 3*max(5,absmal); /* renegade */
  1429.     } else if (always_hostile(mtmp->data)) {
  1430.         int absmal = abs(mal);
  1431.         if (coaligned)
  1432.             mtmp->malign = 0;
  1433.         else
  1434.             mtmp->malign = max(5,absmal);
  1435.     } else if (coaligned) {
  1436.         int absmal = abs(mal);
  1437.         if (mtmp->mpeaceful)
  1438.             mtmp->malign = -3*max(3,absmal);
  1439.         else    /* renegade */
  1440.             mtmp->malign = max(3,absmal);
  1441.     } else    /* not coaligned and therefore hostile */
  1442.         mtmp->malign = abs(mal);
  1443. }
  1444.  
  1445. #endif /* OVL1 */
  1446. #ifdef OVLB
  1447.  
  1448. static NEARDATA char syms[] = {
  1449.     MAXOCLASSES, MAXOCLASSES+1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
  1450.     FOOD_CLASS, GOLD_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
  1451.     AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
  1452.     S_MIMIC_DEF, S_MIMIC_DEF, S_MIMIC_DEF,
  1453. };
  1454.  
  1455. void
  1456. set_mimic_sym(mtmp)        /* KAA, modified by ERS */
  1457. register struct monst *mtmp;
  1458. {
  1459.     int typ, roomno, rt;
  1460.     unsigned appear, ap_type;
  1461.     int s_sym;
  1462.     struct obj *otmp;
  1463.     int mx, my;
  1464.  
  1465.     if (!mtmp) return;
  1466.     mx = mtmp->mx; my = mtmp->my;
  1467.     typ = levl[mx][my].typ;
  1468.                     /* only valid for INSIDE of room */
  1469.     roomno = levl[mx][my].roomno - ROOMOFFSET;
  1470.     if (roomno >= 0)
  1471.         rt = rooms[roomno].rtype;
  1472. #ifdef SPECIALIZATION
  1473.     else if (IS_ROOM(typ))
  1474.         rt = OROOM,  roomno = 0;
  1475. #endif
  1476.     else    rt = 0;    /* roomno < 0 case for GCC_WARN */
  1477.  
  1478.     if (OBJ_AT(mx, my)) {
  1479.         ap_type = M_AP_OBJECT;
  1480.         appear = level.objects[mx][my]->otyp;
  1481.     } else if (IS_DOOR(typ) || IS_WALL(typ) ||
  1482.            typ == SDOOR || typ == SCORR) {
  1483.         ap_type = M_AP_FURNITURE;
  1484.         /*
  1485.          *  If there is a wall to the left that connects to this
  1486.          *  location, then the mimic mimics a horizontal closed door.
  1487.          *  This does not allow doors to be in corners of rooms.
  1488.          */
  1489.         if (mx != 0 &&
  1490.             (levl[mx-1][my].typ == HWALL    ||
  1491.              levl[mx-1][my].typ == TLCORNER ||
  1492.              levl[mx-1][my].typ == TRWALL   ||
  1493.              levl[mx-1][my].typ == BLCORNER ||
  1494.              levl[mx-1][my].typ == TDWALL   ||
  1495.              levl[mx-1][my].typ == CROSSWALL||
  1496.              levl[mx-1][my].typ == TUWALL    ))
  1497.             appear = S_hcdoor;
  1498.         else
  1499.             appear = S_vcdoor;
  1500.  
  1501.         if(!mtmp->minvis || See_invisible)
  1502.             block_point(mx,my);    /* vision */
  1503.     } else if (level.flags.is_maze_lev && rn2(2)) {
  1504.         ap_type = M_AP_OBJECT;
  1505.         appear = STATUE;
  1506.     } else if (roomno < 0) {
  1507.         ap_type = M_AP_OBJECT;
  1508.         appear = BOULDER;
  1509.         if(!mtmp->minvis || See_invisible)
  1510.             block_point(mx,my);    /* vision */
  1511.     } else if (rt == ZOO || rt == VAULT) {
  1512.         ap_type = M_AP_OBJECT;
  1513.         appear = GOLD_PIECE;
  1514.     } else if (rt == DELPHI) {
  1515.         if (rn2(2)) {
  1516.             ap_type = M_AP_OBJECT;
  1517.             appear = STATUE;
  1518.         } else {
  1519.             ap_type = M_AP_FURNITURE;
  1520.             appear = S_fountain;
  1521.         }
  1522.     } else if (rt == TEMPLE) {
  1523.         ap_type = M_AP_FURNITURE;
  1524.         appear = S_altar;
  1525.     /*
  1526.      * We won't bother with beehives, morgues, barracks, throne rooms
  1527.      * since they shouldn't contain too many mimics anyway...
  1528.      */
  1529.     } else if (rt >= SHOPBASE) {
  1530.         s_sym = get_shop_item(rt - SHOPBASE);
  1531.         if (s_sym < 0) {
  1532.             ap_type = M_AP_OBJECT;
  1533.             appear = -s_sym;
  1534.         } else {
  1535.             if (s_sym == RANDOM_CLASS)
  1536.                 s_sym = syms[rn2((int)sizeof(syms)-2) + 2];
  1537.             goto assign_sym;
  1538.         }
  1539.     } else {
  1540.         s_sym = syms[rn2((int)sizeof(syms))];
  1541. assign_sym:
  1542.         if (s_sym >= MAXOCLASSES) {
  1543.             ap_type = M_AP_FURNITURE;
  1544.             appear = s_sym == MAXOCLASSES ? S_upstair : S_dnstair;
  1545.         } else if (s_sym == GOLD_CLASS) {
  1546.             ap_type = M_AP_OBJECT;
  1547.             appear = GOLD_PIECE;
  1548.         } else {
  1549.             ap_type = M_AP_OBJECT;
  1550.             if (s_sym == S_MIMIC_DEF) {
  1551.                 appear = STRANGE_OBJECT;
  1552.             } else {
  1553.                 otmp = mkobj( (char) s_sym, FALSE );
  1554.                 appear = otmp->otyp;
  1555.                 /* make sure container contents are free'ed */
  1556.                 obfree(otmp, (struct obj *) 0);
  1557.             }
  1558.         }
  1559.     }
  1560.     mtmp->m_ap_type = ap_type;
  1561.     mtmp->mappearance = appear;
  1562. }
  1563.  
  1564. #endif /* OVLB */
  1565.  
  1566. /*makemon.c*/
  1567.