Source:NetHack 3.6.2/src/makemon.c

From NetHackWiki
Jump to navigation Jump to search

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

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.

1. /* NetHack 3.6	makemon.c	$NHDT-Date: 1556150377 2019/04/24 23:59:37 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.134 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2012. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. #include <ctype.h>
9. 
10. /* this assumes that a human quest leader or nemesis is an archetype
11.    of the corresponding role; that isn't so for some roles (tourist
12.    for instance) but is for the priests and monks we use it for... */
13. #define quest_mon_represents_role(mptr, role_pm) \
14.     (mptr->mlet == S_HUMAN && Role_if(role_pm)   \
15.      && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
16. 
17. STATIC_DCL boolean FDECL(uncommon, (int));
18. STATIC_DCL int FDECL(align_shift, (struct permonst *));
19. STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
20. STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
21. STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int, int));
22. STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
23. STATIC_DCL void FDECL(m_initweap, (struct monst *));
24. STATIC_DCL void FDECL(m_initinv, (struct monst *));
25. STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *,
26.                                                unsigned, coord *));
27. 
28. #define m_initsgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 3, mmf)
29. #define m_initlgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 10, mmf)
30. #define toostrong(monindx, lev) (mons[monindx].difficulty > lev)
31. #define tooweak(monindx, lev) (mons[monindx].difficulty < lev)
32. 
33. boolean
34. is_home_elemental(ptr)
35. struct permonst *ptr;
36. {
37.     if (ptr->mlet == S_ELEMENTAL)
38.         switch (monsndx(ptr)) {
39.         case PM_AIR_ELEMENTAL:
40.             return Is_airlevel(&u.uz);
41.         case PM_FIRE_ELEMENTAL:
42.             return Is_firelevel(&u.uz);
43.         case PM_EARTH_ELEMENTAL:
44.             return Is_earthlevel(&u.uz);
45.         case PM_WATER_ELEMENTAL:
46.             return Is_waterlevel(&u.uz);
47.         }
48.     return FALSE;
49. }
50. 
51. /*
52.  * Return true if the given monster cannot exist on this elemental level.
53.  */
54. STATIC_OVL boolean
55. wrong_elem_type(ptr)
56. struct permonst *ptr;
57. {
58.     if (ptr->mlet == S_ELEMENTAL) {
59.         return (boolean) !is_home_elemental(ptr);
60.     } else if (Is_earthlevel(&u.uz)) {
61.         /* no restrictions? */
62.     } else if (Is_waterlevel(&u.uz)) {
63.         /* just monsters that can swim */
64.         if (!is_swimmer(ptr))
65.             return TRUE;
66.     } else if (Is_firelevel(&u.uz)) {
67.         if (!pm_resistance(ptr, MR_FIRE))
68.             return TRUE;
69.     } else if (Is_airlevel(&u.uz)) {
70.         if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
71.             && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
72.             return TRUE;
73.     }
74.     return FALSE;
75. }
76. 
77. /* make a group just like mtmp */
78. STATIC_OVL void
79. m_initgrp(mtmp, x, y, n, mmflags)
80. struct monst *mtmp;
81. int x, y, n, mmflags;
82. {
83.     coord mm;
84.     register int cnt = rnd(n);
85.     struct monst *mon;
86. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
87.     /* There is an unresolved problem with several people finding that
88.      * the game hangs eating CPU; if interrupted and restored, the level
89.      * will be filled with monsters.  Of those reports giving system type,
90.      * there were two DG/UX and two HP-UX, all using gcc as the compiler.
91.      * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
92.      * problem went away for him and another reporter-to-newsgroup
93.      * after adding this debugging code.  This has almost got to be a
94.      * compiler bug, but until somebody tracks it down and gets it fixed,
95.      * might as well go with the "but it went away when I tried to find
96.      * it" code.
97.      */
98.     int cnttmp, cntdiv;
99. 
100.     cnttmp = cnt;
101.     debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
102.     cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
103. #endif
104.     /* Tuning: cut down on swarming at low character levels [mrs] */
105.     cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
106. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
107.     if (cnt != (cnttmp / cntdiv)) {
108.         pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
109.               (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
110.     }
111. #endif
112.     if (!cnt)
113.         cnt++;
114. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
115.     if (cnt < 0)
116.         cnt = 1;
117.     if (cnt > 10)
118.         cnt = 10;
119. #endif
120. 
121.     mm.x = x;
122.     mm.y = y;
123.     while (cnt--) {
124.         if (peace_minded(mtmp->data))
125.             continue;
126.         /* Don't create groups of peaceful monsters since they'll get
127.          * in our way.  If the monster has a percentage chance so some
128.          * are peaceful and some are not, the result will just be a
129.          * smaller group.
130.          */
131.         if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
132.             mon = makemon(mtmp->data, mm.x, mm.y, (mmflags | MM_NOGRP));
133.             if (mon) {
134.                 mon->mpeaceful = FALSE;
135.                 mon->mavenge = 0;
136.                 set_malign(mon);
137.                 /* Undo the second peace_minded() check in makemon(); if the
138.                  * monster turned out to be peaceful the first time we
139.                  * didn't create it at all; we don't want a second check.
140.                  */
141.             }
142.         }
143.     }
144. }
145. 
146. STATIC_OVL
147. void
148. m_initthrow(mtmp, otyp, oquan)
149. struct monst *mtmp;
150. int otyp, oquan;
151. {
152.     register struct obj *otmp;
153. 
154.     otmp = mksobj(otyp, TRUE, FALSE);
155.     otmp->quan = (long) rn1(oquan, 3);
156.     otmp->owt = weight(otmp);
157.     if (otyp == ORCISH_ARROW)
158.         otmp->opoisoned = TRUE;
159.     (void) mpickobj(mtmp, otmp);
160. }
161. 
162. STATIC_OVL void
163. m_initweap(mtmp)
164. register struct monst *mtmp;
165. {
166.     register struct permonst *ptr = mtmp->data;
167.     register int mm = monsndx(ptr);
168.     struct obj *otmp;
169.     int bias, spe2, w1, w2;
170. 
171.     if (Is_rogue_level(&u.uz))
172.         return;
173.     /*
174.      *  First a few special cases:
175.      *          giants get a boulder to throw sometimes
176.      *          ettins get clubs
177.      *          kobolds get darts to throw
178.      *          centaurs get some sort of bow & arrows or bolts
179.      *          soldiers get all sorts of things
180.      *          kops get clubs & cream pies.
181.      */
182.     switch (ptr->mlet) {
183.     case S_GIANT:
184.         if (rn2(2))
185.             (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
186.         break;
187.     case S_HUMAN:
188.         if (is_mercenary(ptr)) {
189.             w1 = w2 = 0;
190.             switch (mm) {
191.             case PM_WATCHMAN:
192.             case PM_SOLDIER:
193.                 if (!rn2(3)) {
194.                     w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
195.                     w2 = rn2(2) ? DAGGER : KNIFE;
196.                 } else
197.                     w1 = rn2(2) ? SPEAR : SHORT_SWORD;
198.                 break;
199.             case PM_SERGEANT:
200.                 w1 = rn2(2) ? FLAIL : MACE;
201.                 break;
202.             case PM_LIEUTENANT:
203.                 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
204.                 break;
205.             case PM_CAPTAIN:
206.             case PM_WATCH_CAPTAIN:
207.                 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
208.                 break;
209.             default:
210.                 if (!rn2(4))
211.                     w1 = DAGGER;
212.                 if (!rn2(7))
213.                     w2 = SPEAR;
214.                 break;
215.             }
216.             if (w1)
217.                 (void) mongets(mtmp, w1);
218.             if (!w2 && w1 != DAGGER && !rn2(4))
219.                 w2 = KNIFE;
220.             if (w2)
221.                 (void) mongets(mtmp, w2);
222.         } else if (is_elf(ptr)) {
223.             if (rn2(2))
224.                 (void) mongets(mtmp,
225.                                rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
226.             if (rn2(2))
227.                 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
228.             else if (!rn2(4))
229.                 (void) mongets(mtmp, ELVEN_BOOTS);
230.             if (rn2(2))
231.                 (void) mongets(mtmp, ELVEN_DAGGER);
232.             switch (rn2(3)) {
233.             case 0:
234.                 if (!rn2(4))
235.                     (void) mongets(mtmp, ELVEN_SHIELD);
236.                 if (rn2(3))
237.                     (void) mongets(mtmp, ELVEN_SHORT_SWORD);
238.                 (void) mongets(mtmp, ELVEN_BOW);
239.                 m_initthrow(mtmp, ELVEN_ARROW, 12);
240.                 break;
241.             case 1:
242.                 (void) mongets(mtmp, ELVEN_BROADSWORD);
243.                 if (rn2(2))
244.                     (void) mongets(mtmp, ELVEN_SHIELD);
245.                 break;
246.             case 2:
247.                 if (rn2(2)) {
248.                     (void) mongets(mtmp, ELVEN_SPEAR);
249.                     (void) mongets(mtmp, ELVEN_SHIELD);
250.                 }
251.                 break;
252.             }
253.             if (mm == PM_ELVENKING) {
254.                 if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
255.                     (void) mongets(mtmp, PICK_AXE);
256.                 if (!rn2(50))
257.                     (void) mongets(mtmp, CRYSTAL_BALL);
258.             }
259.         } else if (ptr->msound == MS_PRIEST
260.                    || quest_mon_represents_role(ptr, PM_PRIEST)) {
261.             otmp = mksobj(MACE, FALSE, FALSE);
262.             if (otmp) {
263.                 otmp->spe = rnd(3);
264.                 if (!rn2(2))
265.                     curse(otmp);
266.                 (void) mpickobj(mtmp, otmp);
267.             }
268.         } else if (mm == PM_NINJA) { /* extra quest villains */
269.             (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
270.             (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
271.         } else if (ptr->msound == MS_GUARDIAN) {
272.             /* quest "guardians" */
273.             switch (mm) {
274.             case PM_STUDENT:
275.             case PM_ATTENDANT:
276.             case PM_ABBOT:
277.             case PM_ACOLYTE:
278.             case PM_GUIDE:
279.             case PM_APPRENTICE:
280.                 if (rn2(2))
281.                     (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
282.                 if (rn2(5))
283.                     (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET
284.                                                 : LEATHER_CLOAK);
285.                 if (rn2(3))
286.                     (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
287.                 if (rn2(3))
288.                     (void) mongets(mtmp, POT_HEALING);
289.                 break;
290.             case PM_CHIEFTAIN:
291.             case PM_PAGE:
292.             case PM_ROSHI:
293.             case PM_WARRIOR:
294.                 (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
295.                 (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
296.                 if (rn2(2))
297.                     (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
298.                 if (!rn2(3))
299.                     (void) mongets(mtmp, LEATHER_CLOAK);
300.                 if (!rn2(3)) {
301.                     (void) mongets(mtmp, BOW);
302.                     m_initthrow(mtmp, ARROW, 12);
303.                 }
304.                 break;
305.             case PM_HUNTER:
306.                 (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
307.                 if (rn2(2))
308.                     (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET
309.                                                 : LEATHER_ARMOR);
310.                 (void) mongets(mtmp, BOW);
311.                 m_initthrow(mtmp, ARROW, 12);
312.                 break;
313.             case PM_THUG:
314.                 (void) mongets(mtmp, CLUB);
315.                 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
316.                 if (rn2(2))
317.                     (void) mongets(mtmp, LEATHER_GLOVES);
318.                 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
319.                 break;
320.             case PM_NEANDERTHAL:
321.                 (void) mongets(mtmp, CLUB);
322.                 (void) mongets(mtmp, LEATHER_ARMOR);
323.                 break;
324.             }
325.         }
326.         break;
327. 
328.     case S_ANGEL:
329.         if (humanoid(ptr)) {
330.             /* create minion stuff; can't use mongets */
331.             otmp = mksobj(LONG_SWORD, FALSE, FALSE);
332. 
333.             /* maybe make it special */
334.             if (!rn2(20) || is_lord(ptr))
335.                 otmp = oname(otmp,
336.                              artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
337.             bless(otmp);
338.             otmp->oerodeproof = TRUE;
339.             spe2 = rn2(4);
340.             otmp->spe = max(otmp->spe, spe2);
341.             (void) mpickobj(mtmp, otmp);
342. 
343.             otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
344.                                                   : LARGE_SHIELD,
345.                           FALSE, FALSE);
346.             otmp->cursed = FALSE;
347.             otmp->oerodeproof = TRUE;
348.             otmp->spe = 0;
349.             (void) mpickobj(mtmp, otmp);
350.         }
351.         break;
352. 
353.     case S_HUMANOID:
354.         if (mm == PM_HOBBIT) {
355.             switch (rn2(3)) {
356.             case 0:
357.                 (void) mongets(mtmp, DAGGER);
358.                 break;
359.             case 1:
360.                 (void) mongets(mtmp, ELVEN_DAGGER);
361.                 break;
362.             case 2:
363.                 (void) mongets(mtmp, SLING);
364.                 break;
365.             }
366.             if (!rn2(10))
367.                 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
368.             if (!rn2(10))
369.                 (void) mongets(mtmp, DWARVISH_CLOAK);
370.         } else if (is_dwarf(ptr)) {
371.             if (rn2(7))
372.                 (void) mongets(mtmp, DWARVISH_CLOAK);
373.             if (rn2(7))
374.                 (void) mongets(mtmp, IRON_SHOES);
375.             if (!rn2(4)) {
376.                 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
377.                 /* note: you can't use a mattock with a shield */
378.                 if (rn2(2))
379.                     (void) mongets(mtmp, DWARVISH_MATTOCK);
380.                 else {
381.                     (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
382.                     (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
383.                 }
384.                 (void) mongets(mtmp, DWARVISH_IRON_HELM);
385.                 if (!rn2(3))
386.                     (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
387.             } else {
388.                 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
389.             }
390.         }
391.         break;
392.     case S_KOP:
393.         /* create Keystone Kops with cream pies to
394.            throw. As suggested by KAA.     [MRS] */
395.         if (!rn2(4))
396.             m_initthrow(mtmp, CREAM_PIE, 2);
397.         if (!rn2(3))
398.             (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
399.         break;
400.     case S_ORC:
401.         if (rn2(2))
402.             (void) mongets(mtmp, ORCISH_HELM);
403.         switch ((mm != PM_ORC_CAPTAIN) ? mm
404.                 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
405.         case PM_MORDOR_ORC:
406.             if (!rn2(3))
407.                 (void) mongets(mtmp, SCIMITAR);
408.             if (!rn2(3))
409.                 (void) mongets(mtmp, ORCISH_SHIELD);
410.             if (!rn2(3))
411.                 (void) mongets(mtmp, KNIFE);
412.             if (!rn2(3))
413.                 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
414.             break;
415.         case PM_URUK_HAI:
416.             if (!rn2(3))
417.                 (void) mongets(mtmp, ORCISH_CLOAK);
418.             if (!rn2(3))
419.                 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
420.             if (!rn2(3))
421.                 (void) mongets(mtmp, IRON_SHOES);
422.             if (!rn2(3)) {
423.                 (void) mongets(mtmp, ORCISH_BOW);
424.                 m_initthrow(mtmp, ORCISH_ARROW, 12);
425.             }
426.             if (!rn2(3))
427.                 (void) mongets(mtmp, URUK_HAI_SHIELD);
428.             break;
429.         default:
430.             if (mm != PM_ORC_SHAMAN && rn2(2))
431.                 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
432.                                          ? ORCISH_DAGGER
433.                                          : SCIMITAR);
434.         }
435.         break;
436.     case S_OGRE:
437.         if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
438.             (void) mongets(mtmp, BATTLE_AXE);
439.         else
440.             (void) mongets(mtmp, CLUB);
441.         break;
442.     case S_TROLL:
443.         if (!rn2(2))
444.             switch (rn2(4)) {
445.             case 0:
446.                 (void) mongets(mtmp, RANSEUR);
447.                 break;
448.             case 1:
449.                 (void) mongets(mtmp, PARTISAN);
450.                 break;
451.             case 2:
452.                 (void) mongets(mtmp, GLAIVE);
453.                 break;
454.             case 3:
455.                 (void) mongets(mtmp, SPETUM);
456.                 break;
457.             }
458.         break;
459.     case S_KOBOLD:
460.         if (!rn2(4))
461.             m_initthrow(mtmp, DART, 12);
462.         break;
463. 
464.     case S_CENTAUR:
465.         if (rn2(2)) {
466.             if (ptr == &mons[PM_FOREST_CENTAUR]) {
467.                 (void) mongets(mtmp, BOW);
468.                 m_initthrow(mtmp, ARROW, 12);
469.             } else {
470.                 (void) mongets(mtmp, CROSSBOW);
471.                 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
472.             }
473.         }
474.         break;
475.     case S_WRAITH:
476.         (void) mongets(mtmp, KNIFE);
477.         (void) mongets(mtmp, LONG_SWORD);
478.         break;
479.     case S_ZOMBIE:
480.         if (!rn2(4))
481.             (void) mongets(mtmp, LEATHER_ARMOR);
482.         if (!rn2(4))
483.             (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
484.         break;
485.     case S_LIZARD:
486.         if (mm == PM_SALAMANDER)
487.             (void) mongets(mtmp,
488.                            (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
489.         break;
490.     case S_DEMON:
491.         switch (mm) {
492.         case PM_BALROG:
493.             (void) mongets(mtmp, BULLWHIP);
494.             (void) mongets(mtmp, BROADSWORD);
495.             break;
496.         case PM_ORCUS:
497.             (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
498.             break;
499.         case PM_HORNED_DEVIL:
500.             (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
501.             break;
502.         case PM_DISPATER:
503.             (void) mongets(mtmp, WAN_STRIKING);
504.             break;
505.         case PM_YEENOGHU:
506.             (void) mongets(mtmp, FLAIL);
507.             break;
508.         }
509.         /* prevent djinn and mail daemons from leaving objects when
510.          * they vanish
511.          */
512.         if (!is_demon(ptr))
513.             break;
514.         /*FALLTHRU*/
515.     default:
516.         /*
517.          * Now the general case, some chance of getting some type
518.          * of weapon for "normal" monsters.  Certain special types
519.          * of monsters will get a bonus chance or different selections.
520.          */
521.         bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
522.         switch (rnd(14 - (2 * bias))) {
523.         case 1:
524.             if (strongmonst(ptr))
525.                 (void) mongets(mtmp, BATTLE_AXE);
526.             else
527.                 m_initthrow(mtmp, DART, 12);
528.             break;
529.         case 2:
530.             if (strongmonst(ptr))
531.                 (void) mongets(mtmp, TWO_HANDED_SWORD);
532.             else {
533.                 (void) mongets(mtmp, CROSSBOW);
534.                 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
535.             }
536.             break;
537.         case 3:
538.             (void) mongets(mtmp, BOW);
539.             m_initthrow(mtmp, ARROW, 12);
540.             break;
541.         case 4:
542.             if (strongmonst(ptr))
543.                 (void) mongets(mtmp, LONG_SWORD);
544.             else
545.                 m_initthrow(mtmp, DAGGER, 3);
546.             break;
547.         case 5:
548.             if (strongmonst(ptr))
549.                 (void) mongets(mtmp, LUCERN_HAMMER);
550.             else
551.                 (void) mongets(mtmp, AKLYS);
552.             break;
553.         default:
554.             break;
555.         }
556.         break;
557.     }
558. 
559.     if ((int) mtmp->m_lev > rn2(75))
560.         (void) mongets(mtmp, rnd_offensive_item(mtmp));
561. }
562. 
563. /*
564.  *   Makes up money for monster's inventory.
565.  *   This will change with silver & copper coins
566.  */
567. void
568. mkmonmoney(mtmp, amount)
569. struct monst *mtmp;
570. long amount;
571. {
572.     struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
573. 
574.     gold->quan = amount;
575.     add_to_minv(mtmp, gold);
576. }
577. 
578. STATIC_OVL void
579. m_initinv(mtmp)
580. register struct monst *mtmp;
581. {
582.     register int cnt;
583.     register struct obj *otmp;
584.     register struct permonst *ptr = mtmp->data;
585. 
586.     if (Is_rogue_level(&u.uz))
587.         return;
588.     /*
589.      *  Soldiers get armour & rations - armour approximates their ac.
590.      *  Nymphs may get mirror or potion of object detection.
591.      */
592.     switch (ptr->mlet) {
593.     case S_HUMAN:
594.         if (is_mercenary(ptr)) {
595.             register int mac;
596. 
597.             switch (monsndx(ptr)) {
598.             case PM_GUARD:
599.                 mac = -1;
600.                 break;
601.             case PM_SOLDIER:
602.                 mac = 3;
603.                 break;
604.             case PM_SERGEANT:
605.                 mac = 0;
606.                 break;
607.             case PM_LIEUTENANT:
608.                 mac = -2;
609.                 break;
610.             case PM_CAPTAIN:
611.                 mac = -3;
612.                 break;
613.             case PM_WATCHMAN:
614.                 mac = 3;
615.                 break;
616.             case PM_WATCH_CAPTAIN:
617.                 mac = -2;
618.                 break;
619.             default:
620.                 impossible("odd mercenary %d?", monsndx(ptr));
621.                 mac = 0;
622.                 break;
623.             }
624. 
625.             if (mac < -1 && rn2(5))
626.                 mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
627.                                                   : CRYSTAL_PLATE_MAIL);
628.             else if (mac < 3 && rn2(5))
629.                 mac +=
630.                     6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
631.             else if (rn2(5))
632.                 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
633.                                                   : STUDDED_LEATHER_ARMOR);
634.             else
635.                 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
636. 
637.             if (mac < 10 && rn2(3))
638.                 mac += 1 + mongets(mtmp, HELMET);
639.             else if (mac < 10 && rn2(2))
640.                 mac += 1 + mongets(mtmp, DENTED_POT);
641.             if (mac < 10 && rn2(3))
642.                 mac += 1 + mongets(mtmp, SMALL_SHIELD);
643.             else if (mac < 10 && rn2(2))
644.                 mac += 2 + mongets(mtmp, LARGE_SHIELD);
645.             if (mac < 10 && rn2(3))
646.                 mac += 1 + mongets(mtmp, LOW_BOOTS);
647.             else if (mac < 10 && rn2(2))
648.                 mac += 2 + mongets(mtmp, HIGH_BOOTS);
649.             if (mac < 10 && rn2(3))
650.                 mac += 1 + mongets(mtmp, LEATHER_GLOVES);
651.             else if (mac < 10 && rn2(2))
652.                 mac += 1 + mongets(mtmp, LEATHER_CLOAK);
653. 
654.             nhUse(mac); /* suppress 'dead increment' from static analyzer */
655. 
656.             if (ptr == &mons[PM_WATCH_CAPTAIN]) {
657.                 ; /* better weapon rather than extra gear here */
658.             } else if (ptr == &mons[PM_WATCHMAN]) {
659.                 if (rn2(3)) /* most watchmen carry a whistle */
660.                     (void) mongets(mtmp, TIN_WHISTLE);
661.             } else if (ptr == &mons[PM_GUARD]) {
662.                 /* if hero teleports out of a vault while being confronted
663.                    by the vault's guard, there is a shrill whistling sound,
664.                    so guard evidently carries a cursed whistle */
665.                 otmp = mksobj(TIN_WHISTLE, TRUE, FALSE);
666.                 curse(otmp);
667.                 (void) mpickobj(mtmp, otmp);
668.             } else { /* soldiers and their officers */
669.                 if (!rn2(3))
670.                     (void) mongets(mtmp, K_RATION);
671.                 if (!rn2(2))
672.                     (void) mongets(mtmp, C_RATION);
673.                 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
674.                     (void) mongets(mtmp, BUGLE);
675.             }
676.         } else if (ptr == &mons[PM_SHOPKEEPER]) {
677.             (void) mongets(mtmp, SKELETON_KEY);
678.             switch (rn2(4)) {
679.             /* MAJOR fall through ... */
680.             case 0:
681.                 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
682.                 /*FALLTHRU*/
683.             case 1:
684.                 (void) mongets(mtmp, POT_EXTRA_HEALING);
685.                 /*FALLTHRU*/
686.             case 2:
687.                 (void) mongets(mtmp, POT_HEALING);
688.                 /*FALLTHRU*/
689.             case 3:
690.                 (void) mongets(mtmp, WAN_STRIKING);
691.             }
692.         } else if (ptr->msound == MS_PRIEST
693.                    || quest_mon_represents_role(ptr, PM_PRIEST)) {
694.             (void) mongets(mtmp, rn2(7) ? ROBE
695.                                         : rn2(3) ? CLOAK_OF_PROTECTION
696.                                                  : CLOAK_OF_MAGIC_RESISTANCE);
697.             (void) mongets(mtmp, SMALL_SHIELD);
698.             mkmonmoney(mtmp, (long) rn1(10, 20));
699.         } else if (quest_mon_represents_role(ptr, PM_MONK)) {
700.             (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
701.         }
702.         break;
703.     case S_NYMPH:
704.         if (!rn2(2))
705.             (void) mongets(mtmp, MIRROR);
706.         if (!rn2(2))
707.             (void) mongets(mtmp, POT_OBJECT_DETECTION);
708.         break;
709.     case S_GIANT:
710.         if (ptr == &mons[PM_MINOTAUR]) {
711.             if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
712.                 (void) mongets(mtmp, WAN_DIGGING);
713.         } else if (is_giant(ptr)) {
714.             for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
715.                 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
716.                               FALSE, FALSE);
717.                 otmp->quan = (long) rn1(2, 3);
718.                 otmp->owt = weight(otmp);
719.                 (void) mpickobj(mtmp, otmp);
720.             }
721.         }
722.         break;
723.     case S_WRAITH:
724.         if (ptr == &mons[PM_NAZGUL]) {
725.             otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
726.             curse(otmp);
727.             (void) mpickobj(mtmp, otmp);
728.         }
729.         break;
730.     case S_LICH:
731.         if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
732.             (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
733.         else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
734.             otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
735.                           rn2(13) ? FALSE : TRUE);
736.             if (otmp->spe < 2)
737.                 otmp->spe = rnd(3);
738.             if (!rn2(4))
739.                 otmp->oerodeproof = 1;
740.             (void) mpickobj(mtmp, otmp);
741.         }
742.         break;
743.     case S_MUMMY:
744.         if (rn2(7))
745.             (void) mongets(mtmp, MUMMY_WRAPPING);
746.         break;
747.     case S_QUANTMECH:
748.         if (!rn2(20)) {
749.             struct obj *catcorpse;
750. 
751.             otmp = mksobj(LARGE_BOX, FALSE, FALSE);
752.             /* we used to just set the flag, which resulted in weight()
753.                treating the box as being heavier by the weight of a cat;
754.                now we include a cat corpse that won't rot; when opening or
755.                disclosing the box's contents, the corpse might be revived,
756.                otherwise it's given a rot timer; weight is now ordinary */
757.             if ((catcorpse = mksobj(CORPSE, TRUE, FALSE)) != 0) {
758.                 otmp->spe = 1; /* flag for special SchroedingersBox */
759.                 set_corpsenm(catcorpse, PM_HOUSECAT);
760.                 (void) stop_timer(ROT_CORPSE, obj_to_any(catcorpse));
761.                 add_to_container(otmp, catcorpse);
762.                 otmp->owt = weight(otmp);
763.             }
764.             (void) mpickobj(mtmp, otmp);
765.         }
766.         break;
767.     case S_LEPRECHAUN:
768.         mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
769.         break;
770.     case S_DEMON:
771.         /* moved here from m_initweap() because these don't
772.            have AT_WEAP so m_initweap() is not called for them */
773.         if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
774.             (void) mongets(mtmp, SPEAR);
775.         } else if (ptr == &mons[PM_ASMODEUS]) {
776.             (void) mongets(mtmp, WAN_COLD);
777.             (void) mongets(mtmp, WAN_FIRE);
778.         }
779.         break;
780.     case S_GNOME:
781.         if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
782.             otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
783.             otmp->quan = 1;
784.             otmp->owt = weight(otmp);
785.             if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
786.                 begin_burn(otmp, FALSE);
787.         }
788.         break;
789.     default:
790.         break;
791.     }
792. 
793.     /* ordinary soldiers rarely have access to magic (or gold :-) */
794.     if (ptr == &mons[PM_SOLDIER] && rn2(13))
795.         return;
796. 
797.     if ((int) mtmp->m_lev > rn2(50))
798.         (void) mongets(mtmp, rnd_defensive_item(mtmp));
799.     if ((int) mtmp->m_lev > rn2(100))
800.         (void) mongets(mtmp, rnd_misc_item(mtmp));
801.     if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
802.         mkmonmoney(mtmp,
803.                    (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
804. }
805. 
806. /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
807. struct monst *
808. clone_mon(mon, x, y)
809. struct monst *mon;
810. xchar x, y; /* clone's preferred location or 0 (near mon) */
811. {
812.     coord mm;
813.     struct monst *m2;
814. 
815.     /* may be too weak or have been extinguished for population control */
816.     if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
817.         return (struct monst *) 0;
818. 
819.     if (x == 0) {
820.         mm.x = mon->mx;
821.         mm.y = mon->my;
822.         if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
823.             return (struct monst *) 0;
824.     } else if (!isok(x, y)) {
825.         return (struct monst *) 0; /* paranoia */
826.     } else {
827.         mm.x = x;
828.         mm.y = y;
829.         if (MON_AT(mm.x, mm.y)) {
830.             if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
831.                 return (struct monst *) 0;
832.         }
833.     }
834.     m2 = newmonst();
835.     *m2 = *mon; /* copy condition of old monster */
836.     m2->mextra = (struct mextra *) 0;
837.     m2->nmon = fmon;
838.     fmon = m2;
839.     m2->m_id = context.ident++;
840.     if (!m2->m_id)
841.         m2->m_id = context.ident++; /* ident overflowed */
842.     m2->mx = mm.x;
843.     m2->my = mm.y;
844. 
845.     m2->mundetected = 0;
846.     m2->mtrapped = 0;
847.     m2->mcloned = 1;
848.     m2->minvent = (struct obj *) 0; /* objects don't clone */
849.     m2->mleashed = FALSE;
850.     /* Max HP the same, but current HP halved for both.  The caller
851.      * might want to override this by halving the max HP also.
852.      * When current HP is odd, the original keeps the extra point.
853.      * We know original has more than 1 HP, so both end up with at least 1.
854.      */
855.     m2->mhpmax = mon->mhpmax;
856.     m2->mhp = mon->mhp / 2;
857.     mon->mhp -= m2->mhp;
858. 
859.     /* since shopkeepers and guards will only be cloned if they've been
860.      * polymorphed away from their original forms, the clone doesn't have
861.      * room for the extra information.  we also don't want two shopkeepers
862.      * around for the same shop.
863.      */
864.     if (mon->isshk)
865.         m2->isshk = FALSE;
866.     if (mon->isgd)
867.         m2->isgd = FALSE;
868.     if (mon->ispriest)
869.         m2->ispriest = FALSE;
870.     place_monster(m2, m2->mx, m2->my);
871.     if (emits_light(m2->data))
872.         new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
873.                          monst_to_any(m2));
874.     if (has_mname(mon)) {
875.         m2 = christen_monst(m2, MNAME(mon));
876.     } else if (mon->isshk) {
877.         m2 = christen_monst(m2, shkname(mon));
878.     }
879. 
880.     /* not all clones caused by player are tame or peaceful */
881.     if (!context.mon_moving) {
882.         if (mon->mtame)
883.             m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
884.         else if (mon->mpeaceful)
885.             m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
886.     }
887. 
888.     newsym(m2->mx, m2->my); /* display the new monster */
889.     if (m2->mtame) {
890.         if (mon->isminion) {
891.             newemin(m2);
892.             if (EMIN(mon))
893.                 *(EMIN(m2)) = *(EMIN(mon));
894.         } else {
895.             /* because m2 is a copy of mon it is tame but not init'ed.
896.              * however, tamedog will not re-tame a tame dog, so m2
897.              * must be made non-tame to get initialized properly.
898.              */
899.             m2->mtame = 0;
900.             if (tamedog(m2, (struct obj *) 0)) {
901.                 *(EDOG(m2)) = *(EDOG(mon));
902.             }
903.         }
904.     }
905.     set_malign(m2);
906. 
907.     return m2;
908. }
909. 
910. /*
911.  * Propagate a species
912.  *
913.  * Once a certain number of monsters are created, don't create any more
914.  * at random (i.e. make them extinct).  The previous (3.2) behavior was
915.  * to do this when a certain number had _died_, which didn't make
916.  * much sense.
917.  *
918.  * Returns FALSE propagation unsuccessful
919.  *         TRUE  propagation successful
920.  */
921. boolean
922. propagate(mndx, tally, ghostly)
923. int mndx;
924. boolean tally;
925. boolean ghostly;
926. {
927.     boolean result;
928.     uchar lim = mbirth_limit(mndx);
929.     boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
930. 
931.     result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
932. 
933.     /* if it's unique, don't ever make it again */
934.     if ((mons[mndx].geno & G_UNIQ) && mndx != PM_HIGH_PRIEST)
935.         mvitals[mndx].mvflags |= G_EXTINCT;
936. 
937.     if (mvitals[mndx].born < 255 && tally
938.         && (!ghostly || (ghostly && result)))
939.         mvitals[mndx].born++;
940.     if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
941.         && !(mvitals[mndx].mvflags & G_EXTINCT)) {
942.         if (wizard) {
943.             debugpline1("Automatically extinguished %s.",
944.                         makeplural(mons[mndx].mname));
945.         }
946.         mvitals[mndx].mvflags |= G_EXTINCT;
947.         reset_rndmonst(mndx);
948.     }
949.     return result;
950. }
951. 
952. /* amount of HP to lose from level drain (or gain from Stormbringer) */
953. int
954. monhp_per_lvl(mon)
955. struct monst *mon;
956. {
957.     struct permonst *ptr = mon->data;
958.     int hp = rnd(8); /* default is d8 */
959. 
960.     /* like newmonhp, but home elementals are ignored, riders use normal d8 */
961.     if (is_golem(ptr)) {
962.         /* draining usually won't be applicable for these critters */
963.         hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
964.     } else if (ptr->mlevel > 49) {
965.         /* arbitrary; such monsters won't be involved in draining anyway */
966.         hp = 4 + rnd(4); /* 5..8 */
967.     } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
968.         /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
969.          */
970.         hp = 4 + rn2(5); /* 4..8 */
971.     } else if (!mon->m_lev) {
972.         /* level 0 monsters use 1d4 instead of Nd8 */
973.         hp = rnd(4);
974.     }
975.     return hp;
976. }
977. 
978. /* set up a new monster's initial level and hit points;
979.    used by newcham() as well as by makemon() */
980. void
981. newmonhp(mon, mndx)
982. struct monst *mon;
983. int mndx;
984. {
985.     struct permonst *ptr = &mons[mndx];
986. 
987.     mon->m_lev = adj_lev(ptr);
988.     if (is_golem(ptr)) {
989.         mon->mhpmax = mon->mhp = golemhp(mndx);
990.     } else if (is_rider(ptr)) {
991.         /* we want low HP, but a high mlevel so they can attack well */
992.         mon->mhpmax = mon->mhp = d(10, 8);
993.     } else if (ptr->mlevel > 49) {
994.         /* "special" fixed hp monster
995.          * the hit points are encoded in the mlevel in a somewhat strange
996.          * way to fit in the 50..127 positive range of a signed character
997.          * above the 1..49 that indicate "normal" monster levels */
998.         mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
999.         mon->m_lev = mon->mhp / 4; /* approximation */
1000.     } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
1001.         /* adult dragons */
1002.         mon->mhpmax = mon->mhp =
1003.             (int) (In_endgame(&u.uz)
1004.                        ? (8 * mon->m_lev)
1005.                        : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
1006.     } else if (!mon->m_lev) {
1007.         mon->mhpmax = mon->mhp = rnd(4);
1008.     } else {
1009.         mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
1010.         if (is_home_elemental(ptr))
1011.             mon->mhpmax = (mon->mhp *= 3);
1012.     }
1013. }
1014. 
1015. struct mextra *
1016. newmextra()
1017. {
1018.     struct mextra *mextra;
1019. 
1020.     mextra = (struct mextra *) alloc(sizeof(struct mextra));
1021.     mextra->mname = 0;
1022.     mextra->egd = 0;
1023.     mextra->epri = 0;
1024.     mextra->eshk = 0;
1025.     mextra->emin = 0;
1026.     mextra->edog = 0;
1027.     mextra->mcorpsenm = NON_PM;
1028.     return mextra;
1029. }
1030. 
1031. boolean
1032. makemon_rnd_goodpos(mon, gpflags, cc)
1033. struct monst *mon;
1034. unsigned gpflags;
1035. coord *cc;
1036. {
1037.     int tryct = 0;
1038.     int nx,ny;
1039.     boolean good;
1040. 
1041.     do {
1042.         nx = rn1(COLNO - 3, 2);
1043.         ny = rn2(ROWNO);
1044.         good = (!in_mklev && cansee(nx,ny)) ? FALSE
1045.                                             : goodpos(nx, ny, mon, gpflags);
1046.     } while ((++tryct < 50) && !good);
1047. 
1048.     if (!good) {
1049.         /* else go through all map positions, twice, first round
1050.            ignoring positions in sight, and pick first good one.
1051.            skip first round if we're in special level loader or blind */
1052.         int xofs = nx;
1053.         int yofs = ny;
1054.         int dx,dy;
1055.         int bl = (in_mklev || Blind) ? 1 : 0;
1056. 
1057.         for ( ; bl < 2; bl++) {
1058.             for (dx = 0; dx < COLNO; dx++)
1059.                 for (dy = 0; dy < ROWNO; dy++) {
1060.                     nx = ((dx + xofs) % (COLNO - 1)) + 1;
1061.                     ny = ((dy + yofs) % (ROWNO - 1)) + 1;
1062.                     if (bl == 0 && cansee(nx,ny))
1063.                         continue;
1064.                     if (goodpos(nx, ny, mon, gpflags))
1065.                         goto gotgood;
1066.                 }
1067.             if (bl == 0 && (!mon || mon->data->mmove)) {
1068.                 /* all map positions are visible (or not good),
1069.                    try to pick something logical */
1070.                 if (dnstair.sx && !rn2(2)) {
1071.                     nx = dnstair.sx;
1072.                     ny = dnstair.sy;
1073.                 } else if (upstair.sx && !rn2(2)) {
1074.                     nx = upstair.sx;
1075.                     ny = upstair.sy;
1076.                 } else if (dnladder.sx && !rn2(2)) {
1077.                     nx = dnladder.sx;
1078.                     ny = dnladder.sy;
1079.                 } else if (upladder.sx && !rn2(2)) {
1080.                     nx = upladder.sx;
1081.                     ny = upladder.sy;
1082.                 }
1083.                 if (goodpos(nx, ny, mon, gpflags))
1084.                     goto gotgood;
1085.             }
1086.         }
1087.     } else {
1088.  gotgood:
1089.         cc->x = nx;
1090.         cc->y = ny;
1091.         return TRUE;
1092.     }
1093.     return FALSE;
1094. }
1095. 
1096. /*
1097.  * called with [x,y] = coordinates;
1098.  *      [0,0] means anyplace
1099.  *      [u.ux,u.uy] means: near player (if !in_mklev)
1100.  *
1101.  *      In case we make a monster group, only return the one at [x,y].
1102.  */
1103. struct monst *
1104. makemon(ptr, x, y, mmflags)
1105. register struct permonst *ptr;
1106. register int x, y;
1107. int mmflags;
1108. {
1109.     register struct monst *mtmp;
1110.     int mndx, mcham, ct, mitem;
1111.     boolean anymon = (!ptr);
1112.     boolean byyou = (x == u.ux && y == u.uy);
1113.     boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1114.     boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1115.     unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1116. 
1117.     /* if caller wants random location, do it here */
1118.     if (x == 0 && y == 0) {
1119.         coord cc;
1120.         struct monst fakemon;
1121. 
1122.         cc.x = cc.y = 0; /* lint suppression */
1123.         fakemon.data = ptr; /* set up for goodpos */
1124.         if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1125.                                  gpflags, &cc))
1126.             return (struct monst *) 0;
1127.         x = cc.x;
1128.         y = cc.y;
1129.     } else if (byyou && !in_mklev) {
1130.         coord bypos;
1131. 
1132.         if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1133.             x = bypos.x;
1134.             y = bypos.y;
1135.         } else
1136.             return (struct monst *) 0;
1137.     }
1138. 
1139.     /* Does monster already exist at the position? */
1140.     if (MON_AT(x, y)) {
1141.         if ((mmflags & MM_ADJACENTOK) != 0) {
1142.             coord bypos;
1143.             if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1144.                 x = bypos.x;
1145.                 y = bypos.y;
1146.             } else
1147.                 return (struct monst *) 0;
1148.         } else
1149.             return (struct monst *) 0;
1150.     }
1151. 
1152.     if (ptr) {
1153.         mndx = monsndx(ptr);
1154.         /* if you are to make a specific monster and it has
1155.            already been genocided, return */
1156.         if (mvitals[mndx].mvflags & G_GENOD)
1157.             return (struct monst *) 0;
1158.         if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1159.             debugpline1("Explicitly creating extinct monster %s.",
1160.                         mons[mndx].mname);
1161.         }
1162.     } else {
1163.         /* make a random (common) monster that can survive here.
1164.          * (the special levels ask for random monsters at specific
1165.          * positions, causing mass drowning on the medusa level,
1166.          * for instance.)
1167.          */
1168.         int tryct = 0; /* maybe there are no good choices */
1169.         struct monst fakemon;
1170. 
1171.         do {
1172.             if (!(ptr = rndmonst())) {
1173.                 debugpline0("Warning: no monster.");
1174.                 return (struct monst *) 0; /* no more monsters! */
1175.             }
1176.             fakemon.data = ptr; /* set up for goodpos */
1177.         } while (++tryct <= 50
1178.                  /* in Sokoban, don't accept a giant on first try;
1179.                     after that, boulder carriers are fair game */
1180.                  && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1181.                      || !goodpos(x, y, &fakemon, gpflags)));
1182.         mndx = monsndx(ptr);
1183.     }
1184.     (void) propagate(mndx, countbirth, FALSE);
1185.     mtmp = newmonst();
1186.     *mtmp = zeromonst; /* clear all entries in structure */
1187. 
1188.     if (mmflags & MM_EGD)
1189.         newegd(mtmp);
1190.     if (mmflags & MM_EPRI)
1191.         newepri(mtmp);
1192.     if (mmflags & MM_ESHK)
1193.         neweshk(mtmp);
1194.     if (mmflags & MM_EMIN)
1195.         newemin(mtmp);
1196.     if (mmflags & MM_EDOG)
1197.         newedog(mtmp);
1198.     if (mmflags & MM_ASLEEP)
1199.         mtmp->msleeping = 1;
1200.     mtmp->nmon = fmon;
1201.     fmon = mtmp;
1202.     mtmp->m_id = context.ident++;
1203.     if (!mtmp->m_id)
1204.         mtmp->m_id = context.ident++; /* ident overflowed */
1205.     set_mon_data(mtmp, ptr); /* mtmp->data = ptr; */
1206.     if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1207.         quest_status.leader_m_id = mtmp->m_id;
1208.     mtmp->mnum = mndx;
1209. 
1210.     /* set up level and hit points */
1211.     newmonhp(mtmp, mndx);
1212. 
1213.     if (is_female(ptr))
1214.         mtmp->female = TRUE;
1215.     else if (is_male(ptr))
1216.         mtmp->female = FALSE;
1217.     /* leader and nemesis gender is usually hardcoded in mons[],
1218.        but for ones which can be random, it has already been chosen
1219.        (in role_init(), for possible use by the quest pager code) */
1220.     else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1221.         mtmp->female = quest_status.ldrgend;
1222.     else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1223.         mtmp->female = quest_status.nemgend;
1224.     else
1225.         mtmp->female = rn2(2); /* ignored for neuters */
1226. 
1227.     if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1228.         mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1229.     /* quest leader and nemesis both know about all trap types */
1230.     if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1231.         mtmp->mtrapseen = ~0;
1232. 
1233.     place_monster(mtmp, x, y);
1234.     mtmp->mcansee = mtmp->mcanmove = TRUE;
1235.     mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1236. 
1237.     switch (ptr->mlet) {
1238.     case S_MIMIC:
1239.         set_mimic_sym(mtmp);
1240.         break;
1241.     case S_SPIDER:
1242.     case S_SNAKE:
1243.         if (in_mklev)
1244.             if (x && y)
1245.                 (void) mkobj_at(0, x, y, TRUE);
1246.         (void) hideunder(mtmp);
1247.         break;
1248.     case S_LIGHT:
1249.     case S_ELEMENTAL:
1250.         if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1251.             mtmp->perminvis = TRUE;
1252.             mtmp->minvis = TRUE;
1253.         }
1254.         break;
1255.     case S_EEL:
1256.         (void) hideunder(mtmp);
1257.         break;
1258.     case S_LEPRECHAUN:
1259.         mtmp->msleeping = 1;
1260.         break;
1261.     case S_JABBERWOCK:
1262.     case S_NYMPH:
1263.         if (rn2(5) && !u.uhave.amulet)
1264.             mtmp->msleeping = 1;
1265.         break;
1266.     case S_ORC:
1267.         if (Race_if(PM_ELF))
1268.             mtmp->mpeaceful = FALSE;
1269.         break;
1270.     case S_UNICORN:
1271.         if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1272.             mtmp->mpeaceful = TRUE;
1273.         break;
1274.     case S_BAT:
1275.         if (Inhell && is_bat(ptr))
1276.             mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1277.         break;
1278.     }
1279.     if ((ct = emits_light(mtmp->data)) > 0)
1280.         new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1281.                          monst_to_any(mtmp));
1282.     mitem = 0; /* extra inventory item for this monster */
1283. 
1284.     if (mndx == PM_VLAD_THE_IMPALER)
1285.         mitem = CANDELABRUM_OF_INVOCATION;
1286.     mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1287.     if (!Protection_from_shape_changers
1288.         && (mcham = pm_to_cham(mndx)) != NON_PM) {
1289.         /* this is a shapechanger after all */
1290.         mtmp->cham = mcham;
1291.         /* Vlad stays in his normal shape so he can carry the Candelabrum */
1292.         if (mndx != PM_VLAD_THE_IMPALER
1293.             /* Note:  shapechanger's initial form used to be chosen here
1294.                with rndmonst(), yielding a monster which was appropriate
1295.                to the level's difficulty but ignoring the changer's usual
1296.                type selection, so was inappropriate for vampshifters.
1297.                Let newcham() pick the shape. */
1298.             && newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1299.             allow_minvent = FALSE;
1300.     } else if (mndx == PM_WIZARD_OF_YENDOR) {
1301.         mtmp->iswiz = TRUE;
1302.         context.no_of_wizards++;
1303.         if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1304.             mitem = SPE_DIG;
1305.     } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1306.         mtmp = christen_monst(mtmp, rndghostname());
1307.     } else if (mndx == PM_CROESUS) {
1308.         mitem = TWO_HANDED_SWORD;
1309.     } else if (ptr->msound == MS_NEMESIS) {
1310.         mitem = BELL_OF_OPENING;
1311.     } else if (mndx == PM_PESTILENCE) {
1312.         mitem = POT_SICKNESS;
1313.     }
1314.     if (mitem && allow_minvent)
1315.         (void) mongets(mtmp, mitem);
1316. 
1317.     if (in_mklev) {
1318.         if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1319.              || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1320.             && !u.uhave.amulet && rn2(5))
1321.             mtmp->msleeping = TRUE;
1322.     } else {
1323.         if (byyou) {
1324.             newsym(mtmp->mx, mtmp->my);
1325.             set_apparxy(mtmp);
1326.         }
1327.     }
1328.     if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1329.         mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1330.         mtmp->mavenge = 0;
1331.         if (uwep && uwep->oartifact == ART_EXCALIBUR)
1332.             mtmp->mpeaceful = mtmp->mtame = FALSE;
1333.     }
1334. #ifndef DCC30_BUG
1335.     if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1336. #else
1337.     /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1338.        same expression. */
1339.     if (mndx == PM_LONG_WORM
1340.         && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1341. #endif
1342.     {
1343.         /* we can now create worms with tails - 11/91 */
1344.         initworm(mtmp, rn2(5));
1345.         if (count_wsegs(mtmp))
1346.             place_worm_tail_randomly(mtmp, x, y);
1347.     }
1348.     /* it's possible to create an ordinary monster of some special
1349.        types; make sure their extended data is initialized to
1350.        something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1351.        (when they're specified, caller intends to handle this itself) */
1352.     if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1353.             ? !(mmflags & (MM_EPRI | MM_EMIN))
1354.             : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1355.         struct emin *eminp;
1356.         newemin(mtmp);
1357.         eminp = EMIN(mtmp);
1358. 
1359.         mtmp->isminion = 1;            /* make priest be a roamer */
1360.         eminp->min_align = rn2(3) - 1; /* no A_NONE */
1361.         eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1362.         mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1363.                               ? !eminp->renegade
1364.                               : eminp->renegade;
1365.     }
1366.     set_malign(mtmp); /* having finished peaceful changes */
1367.     if (anymon && !(mmflags & MM_NOGRP)) {
1368.         if ((ptr->geno & G_SGROUP) && rn2(2)) {
1369.             m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1370.         } else if (ptr->geno & G_LGROUP) {
1371.             if (rn2(3))
1372.                 m_initlgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1373.             else
1374.                 m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1375.         }
1376.     }
1377. 
1378.     if (allow_minvent) {
1379.         if (is_armed(ptr))
1380.             m_initweap(mtmp); /* equip with weapons / armor */
1381.         m_initinv(mtmp); /* add on a few special items incl. more armor */
1382.         m_dowear(mtmp, TRUE);
1383. 
1384.         if (!rn2(100) && is_domestic(ptr)
1385.             && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) {
1386.             struct obj *otmp = mksobj(SADDLE, TRUE, FALSE);
1387.             put_saddle_on_mon(otmp, mtmp);
1388.         }
1389. 
1390.     } else {
1391.         /* no initial inventory is allowed */
1392.         if (mtmp->minvent)
1393.             discard_minvent(mtmp);
1394.         mtmp->minvent = (struct obj *) 0; /* caller expects this */
1395.     }
1396.     if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1397.         if (ptr->mflags3 & M3_WAITFORU)
1398.             mtmp->mstrategy |= STRAT_WAITFORU;
1399.         if (ptr->mflags3 & M3_CLOSE)
1400.             mtmp->mstrategy |= STRAT_CLOSE;
1401.         if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1402.             mtmp->mstrategy |= STRAT_APPEARMSG;
1403.     }
1404. 
1405.     if (allow_minvent && migrating_objs)
1406.         deliver_obj_to_mon(mtmp, 1, DF_NONE); /* in case of waiting items */
1407. 
1408.     if (!in_mklev)
1409.         newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1410. 
1411.     return mtmp;
1412. }
1413. 
1414. int
1415. mbirth_limit(mndx)
1416. int mndx;
1417. {
1418.     /* There is an implicit limit of 4 for "high priest of <deity>",
1419.      * but aligned priests can grow into high priests, thus they aren't
1420.      * really limited to 4, so leave the default amount in place for them.
1421.      */
1422. 
1423.     /* assert(MAXMONNO < 255); */
1424.     return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1425. }
1426. 
1427. /* used for wand/scroll/spell of create monster */
1428. /* returns TRUE iff you know monsters have been created */
1429. boolean
1430. create_critters(cnt, mptr, neverask)
1431. int cnt;
1432. struct permonst *mptr; /* usually null; used for confused reading */
1433. boolean neverask;
1434. {
1435.     coord c;
1436.     int x, y;
1437.     struct monst *mon;
1438.     boolean known = FALSE;
1439.     boolean ask = (wizard && !neverask);
1440. 
1441.     while (cnt--) {
1442.         if (ask) {
1443.             if (create_particular()) {
1444.                 known = TRUE;
1445.                 continue;
1446.             } else
1447.                 ask = FALSE; /* ESC will shut off prompting */
1448.         }
1449.         x = u.ux, y = u.uy;
1450.         /* if in water, try to encourage an aquatic monster
1451.            by finding and then specifying another wet location */
1452.         if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1453.             x = c.x, y = c.y;
1454. 
1455.         mon = makemon(mptr, x, y, NO_MM_FLAGS);
1456.         if (mon && canspotmon(mon))
1457.             known = TRUE;
1458.     }
1459.     return known;
1460. }
1461. 
1462. STATIC_OVL boolean
1463. uncommon(mndx)
1464. int mndx;
1465. {
1466.     if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1467.         return TRUE;
1468.     if (mvitals[mndx].mvflags & G_GONE)
1469.         return TRUE;
1470.     if (Inhell)
1471.         return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1472.     else
1473.         return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1474. }
1475. 
1476. /*
1477.  *      shift the probability of a monster's generation by
1478.  *      comparing the dungeon alignment and monster alignment.
1479.  *      return an integer in the range of 0-5.
1480.  */
1481. STATIC_OVL int
1482. align_shift(ptr)
1483. register struct permonst *ptr;
1484. {
1485.     static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1486.     static NEARDATA s_level *lev;
1487.     register int alshift;
1488. 
1489.     if (oldmoves != moves) {
1490.         lev = Is_special(&u.uz);
1491.         oldmoves = moves;
1492.     }
1493.     switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1494.     default: /* just in case */
1495.     case AM_NONE:
1496.         alshift = 0;
1497.         break;
1498.     case AM_LAWFUL:
1499.         alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1500.         break;
1501.     case AM_NEUTRAL:
1502.         alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1503.         break;
1504.     case AM_CHAOTIC:
1505.         alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1506.         break;
1507.     }
1508.     return alshift;
1509. }
1510. 
1511. static NEARDATA struct {
1512.     int choice_count;
1513.     char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1514. } rndmonst_state = { -1, { 0 } };
1515. 
1516. /* select a random monster type */
1517. struct permonst *
1518. rndmonst()
1519. {
1520.     register struct permonst *ptr;
1521.     register int mndx, ct;
1522. 
1523.     if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1524.         return ptr;
1525. 
1526.     if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1527.         int zlevel, minmlev, maxmlev;
1528.         boolean elemlevel;
1529.         boolean upper;
1530. 
1531.         rndmonst_state.choice_count = 0;
1532.         /* look for first common monster */
1533.         for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1534.             if (!uncommon(mndx))
1535.                 break;
1536.             rndmonst_state.mchoices[mndx] = 0;
1537.         }
1538.         if (mndx == SPECIAL_PM) {
1539.             /* evidently they've all been exterminated */
1540.             debugpline0("rndmonst: no common mons!");
1541.             return (struct permonst *) 0;
1542.         } /* else `mndx' now ready for use below */
1543.         zlevel = level_difficulty();
1544.         /* determine the level of the weakest monster to make. */
1545.         minmlev = zlevel / 6;
1546.         /* determine the level of the strongest monster to make. */
1547.         maxmlev = (zlevel + u.ulevel) / 2;
1548.         upper = Is_rogue_level(&u.uz);
1549.         elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1550. 
1551.         /*
1552.          * Find out how many monsters exist in the range we have selected.
1553.          */
1554.         for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1555.             ptr = &mons[mndx];
1556.             rndmonst_state.mchoices[mndx] = 0;
1557.             if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1558.                 continue;
1559.             if (upper && !isupper((uchar) def_monsyms[(int) ptr->mlet].sym))
1560.                 continue;
1561.             if (elemlevel && wrong_elem_type(ptr))
1562.                 continue;
1563.             if (uncommon(mndx))
1564.                 continue;
1565.             if (Inhell && (ptr->geno & G_NOHELL))
1566.                 continue;
1567.             ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1568.             if (ct < 0 || ct > 127)
1569.                 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1570.             rndmonst_state.choice_count += ct;
1571.             rndmonst_state.mchoices[mndx] = (char) ct;
1572.         }
1573.         /*
1574.          *      Possible modification:  if choice_count is "too low",
1575.          *      expand minmlev..maxmlev range and try again.
1576.          */
1577.     } /* choice_count+mchoices[] recalc */
1578. 
1579.     if (rndmonst_state.choice_count <= 0) {
1580.         /* maybe no common mons left, or all are too weak or too strong */
1581.         debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1582.         return (struct permonst *) 0;
1583.     }
1584. 
1585.     /*
1586.      *  Now, select a monster at random.
1587.      */
1588.     ct = rnd(rndmonst_state.choice_count);
1589.     for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1590.         if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1591.             break;
1592. 
1593.     if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1594.         impossible("rndmonst: bad `mndx' [#%d]", mndx);
1595.         return (struct permonst *) 0;
1596.     }
1597.     return &mons[mndx];
1598. }
1599. 
1600. /* called when you change level (experience or dungeon depth) or when
1601.    monster species can no longer be created (genocide or extinction) */
1602. void
1603. reset_rndmonst(mndx)
1604. int mndx; /* particular species that can no longer be created */
1605. {
1606.     /* cached selection info is out of date */
1607.     if (mndx == NON_PM) {
1608.         rndmonst_state.choice_count = -1; /* full recalc needed */
1609.     } else if (mndx < SPECIAL_PM) {
1610.         rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1611.         rndmonst_state.mchoices[mndx] = 0;
1612.     } /* note: safe to ignore extinction of unique monsters */
1613. }
1614. 
1615. /* decide whether it's ok to generate a candidate monster by mkclass() */
1616. STATIC_OVL boolean
1617. mk_gen_ok(mndx, mvflagsmask, genomask)
1618. int mndx, mvflagsmask, genomask;
1619. {
1620.     struct permonst *ptr = &mons[mndx];
1621. 
1622.     if (mvitals[mndx].mvflags & mvflagsmask)
1623.         return FALSE;
1624.     if (ptr->geno & genomask)
1625.         return FALSE;
1626.     if (is_placeholder(ptr))
1627.         return FALSE;
1628. #ifdef MAIL
1629.     /* special levels might ask for random demon type; reject this one */
1630.     if (ptr == &mons[PM_MAIL_DAEMON])
1631.         return FALSE;
1632. #endif
1633.     return TRUE;
1634. }
1635. 
1636. /* Make one of the multiple types of a given monster class.
1637.    The second parameter specifies a special casing bit mask
1638.    to allow the normal genesis masks to be deactivated.
1639.    Returns Null if no monsters in that class can be made. */
1640. struct permonst *
1641. mkclass(class, spc)
1642. char class;
1643. int spc;
1644. {
1645.     return mkclass_aligned(class, spc, A_NONE);
1646. }
1647. 
1648. /* mkclass() with alignment restrictions; used by ndemon() */
1649. struct permonst *
1650. mkclass_aligned(class, spc, atyp)
1651. char class;
1652. int spc;
1653. aligntyp atyp;
1654. {
1655.     register int first, last, num = 0;
1656.     int k, nums[SPECIAL_PM + 1]; /* +1: insurance for final return value */
1657.     int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1658. 
1659.     (void) memset((genericptr_t) nums, 0, sizeof nums);
1660.     maxmlev = level_difficulty() >> 1;
1661.     if (class < 1 || class >= MAXMCLASSES) {
1662.         impossible("mkclass called with bad class!");
1663.         return (struct permonst *) 0;
1664.     }
1665.     /*  Assumption #1:  monsters of a given class are contiguous in the
1666.      *                  mons[] array.  Player monsters and quest denizens
1667.      *                  are an exception; mkclass() won't pick them.
1668.      *                  SPECIAL_PM is long worm tail and separates the
1669.      *                  regular monsters from the exceptions.
1670.      */
1671.     for (first = LOW_PM; first < SPECIAL_PM; first++)
1672.         if (mons[first].mlet == class)
1673.             break;
1674.     if (first == SPECIAL_PM) {
1675.         impossible("mkclass found no class %d monsters", class);
1676.         return (struct permonst *) 0;
1677.     }
1678. 
1679.     /*  Assumption #2:  monsters of a given class are presented in ascending
1680.      *                  order of strength.
1681.      */
1682.     for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++) {
1683.         if (atyp != A_NONE && sgn(mons[last].maligntyp) != sgn(atyp))
1684.             continue;
1685.         if (mk_gen_ok(last, G_GONE, mask)) {
1686.             /* consider it; don't reject a toostrong() monster if we
1687.                don't have anything yet (num==0) or if it is the same
1688.                (or lower) difficulty as preceding candidate (non-zero
1689.                'num' implies last > first so mons[last-1] is safe);
1690.                sometimes accept it even if high difficulty */
1691.             if (num && toostrong(last, maxmlev)
1692.                 && mons[last].difficulty > mons[last - 1].difficulty
1693.                 && rn2(2))
1694.                 break;
1695.             if ((k = (mons[last].geno & G_FREQ)) > 0) {
1696.                 /* skew towards lower value monsters at lower exp. levels
1697.                    (this used to be done in the next loop, but that didn't
1698.                    work well when multiple species had the same level and
1699.                    were followed by one that was past the bias threshold;
1700.                    cited example was sucubus and incubus, where the bias
1701.                    against picking the next demon resulted in incubus
1702.                    being picked nearly twice as often as sucubus);
1703.                    we need the '+1' in case the entire set is too high
1704.                    level (really low level hero) */
1705.                 nums[last] = k + 1 - (adj_lev(&mons[last]) > (u.ulevel * 2));
1706.                 num += nums[last];
1707.             }
1708.         }
1709.     }
1710.     if (!num)
1711.         return (struct permonst *) 0;
1712. 
1713.     /* the hard work has already been done; 'num' should hit 0 before
1714.        first reaches last (which is actually one past our last candidate) */
1715.     for (num = rnd(num); first < last; first++)
1716.         if ((num -= nums[first]) <= 0)
1717.             break;
1718. 
1719.     return nums[first] ? &mons[first] : (struct permonst *) 0;
1720. }
1721. 
1722. /* like mkclass(), but excludes difficulty considerations; used when
1723.    player with polycontrol picks a class instead of a specific type;
1724.    genocided types are avoided but extinct ones are acceptable; we don't
1725.    check polyok() here--caller accepts some choices !polyok() would reject */
1726. int
1727. mkclass_poly(class)
1728. int class;
1729. {
1730.     register int first, last, num = 0;
1731. 
1732.     for (first = LOW_PM; first < SPECIAL_PM; first++)
1733.         if (mons[first].mlet == class)
1734.             break;
1735.     if (first == SPECIAL_PM)
1736.         return NON_PM;
1737. 
1738.     for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1739.         if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1740.             num += mons[last].geno & G_FREQ;
1741.     if (!num)
1742.         return NON_PM;
1743. 
1744.     for (num = rnd(num); num > 0; first++)
1745.         if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1746.             num -= mons[first].geno & G_FREQ;
1747.     first--; /* correct an off-by-one error */
1748. 
1749.     return first;
1750. }
1751. 
1752. /* adjust strength of monsters based on u.uz and u.ulevel */
1753. int
1754. adj_lev(ptr)
1755. register struct permonst *ptr;
1756. {
1757.     int tmp, tmp2;
1758. 
1759.     if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1760.         /* does not depend on other strengths, but does get stronger
1761.          * every time he is killed
1762.          */
1763.         tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1764.         if (tmp > 49)
1765.             tmp = 49;
1766.         return tmp;
1767.     }
1768. 
1769.     if ((tmp = ptr->mlevel) > 49)
1770.         return 50; /* "special" demons/devils */
1771.     tmp2 = (level_difficulty() - tmp);
1772.     if (tmp2 < 0)
1773.         tmp--; /* if mlevel > u.uz decrement tmp */
1774.     else
1775.         tmp += (tmp2 / 5); /* else increment 1 per five diff */
1776. 
1777.     tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1778.     if (tmp2 > 0)
1779.         tmp += (tmp2 / 4); /* level as well */
1780. 
1781.     tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1782.     if (tmp2 > 49)
1783.         tmp2 = 49;                                      /* hard upper limit */
1784.     return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1785. }
1786. 
1787. /* monster earned experience and will gain some hit points; it might also
1788.    grow into a bigger monster (baby to adult, soldier to officer, etc) */
1789. struct permonst *
1790. grow_up(mtmp, victim)
1791. struct monst *mtmp, *victim;
1792. {
1793.     int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1794.     unsigned fem;
1795.     struct permonst *ptr = mtmp->data;
1796. 
1797.     /* monster died after killing enemy but before calling this function */
1798.     /* currently possible if killing a gas spore */
1799.     if (DEADMONSTER(mtmp))
1800.         return (struct permonst *) 0;
1801. 
1802.     /* note:  none of the monsters with special hit point calculations
1803.        have both little and big forms */
1804.     oldtype = monsndx(ptr);
1805.     newtype = little_to_big(oldtype);
1806.     if (newtype == PM_PRIEST && mtmp->female)
1807.         newtype = PM_PRIESTESS;
1808. 
1809.     /* growth limits differ depending on method of advancement */
1810.     if (victim) {                       /* killed a monster */
1811.         /*
1812.          * The HP threshold is the maximum number of hit points for the
1813.          * current level; once exceeded, a level will be gained.
1814.          * Possible bug: if somehow the hit points are already higher
1815.          * than that, monster will gain a level without any increase in HP.
1816.          */
1817.         hp_threshold = mtmp->m_lev * 8; /* normal limit */
1818.         if (!mtmp->m_lev)
1819.             hp_threshold = 4;
1820.         else if (is_golem(ptr)) /* strange creatures */
1821.             hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1822.         else if (is_home_elemental(ptr))
1823.             hp_threshold *= 3;
1824.         lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1825.         /* If they can grow up, be sure the level is high enough for that */
1826.         if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1827.             lev_limit = (int) mons[newtype].mlevel;
1828.         /* number of hit points to gain; unlike for the player, we put
1829.            the limit at the bottom of the next level rather than the top */
1830.         max_increase = rnd((int) victim->m_lev + 1);
1831.         if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1832.             max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1833.         cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1834.     } else {
1835.         /* a gain level potion or wraith corpse; always go up a level
1836.            unless already at maximum (49 is hard upper limit except
1837.            for demon lords, who start at 50 and can't go any higher) */
1838.         max_increase = cur_increase = rnd(8);
1839.         hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1840.         lev_limit = 50;   /* recalc below */
1841.     }
1842. 
1843.     mtmp->mhpmax += max_increase;
1844.     mtmp->mhp += cur_increase;
1845.     if (mtmp->mhpmax <= hp_threshold)
1846.         return ptr; /* doesn't gain a level */
1847. 
1848.     if (is_mplayer(ptr))
1849.         lev_limit = 30; /* same as player */
1850.     else if (lev_limit < 5)
1851.         lev_limit = 5; /* arbitrary */
1852.     else if (lev_limit > 49)
1853.         lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1854. 
1855.     if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1856.         ptr = &mons[newtype];
1857.         /* new form might force gender change */
1858.         fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female;
1859. 
1860.         if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1861.             if (canspotmon(mtmp))
1862.                 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1863.                       an(ptr->mname), mhe(mtmp),
1864.                       nonliving(ptr) ? "expires" : "dies");
1865.             set_mon_data(mtmp, ptr); /* keep mvitals[] accurate */
1866.             mondied(mtmp);
1867.             return (struct permonst *) 0;
1868.         } else if (canspotmon(mtmp)) {
1869.             char buf[BUFSZ];
1870. 
1871.             /* 3.6.1:
1872.              * Temporary (?) hack to fix growing into opposite gender.
1873.              */
1874.             Sprintf(buf, "%s%s",
1875.                     /* deal with female gnome becoming a gnome lord */
1876.                     (mtmp->female && !fem) ? "male "
1877.                         /* or a male gnome becoming a gnome lady
1878.                            (can't happen with 3.6.0 mons[], but perhaps
1879.                            slightly less sexist if prepared for it...) */
1880.                       : (fem && !mtmp->female) ? "female " : "",
1881.                     ptr->mname);
1882.             pline("%s %s %s.", upstart(y_monnam(mtmp)),
1883.                   (fem != mtmp->female) ? "changes into"
1884.                                         : humanoid(ptr) ? "becomes"
1885.                                                         : "grows up into",
1886.                   an(buf));
1887.         }
1888.         set_mon_data(mtmp, ptr);
1889.         newsym(mtmp->mx, mtmp->my);    /* color may change */
1890.         lev_limit = (int) mtmp->m_lev; /* never undo increment */
1891. 
1892.         mtmp->female = fem; /* gender might be changing */
1893.     }
1894. 
1895.     /* sanity checks */
1896.     if ((int) mtmp->m_lev > lev_limit) {
1897.         mtmp->m_lev--; /* undo increment */
1898.         /* HP might have been allowed to grow when it shouldn't */
1899.         if (mtmp->mhpmax == hp_threshold + 1)
1900.             mtmp->mhpmax--;
1901.     }
1902.     if (mtmp->mhpmax > 50 * 8)
1903.         mtmp->mhpmax = 50 * 8; /* absolute limit */
1904.     if (mtmp->mhp > mtmp->mhpmax)
1905.         mtmp->mhp = mtmp->mhpmax;
1906. 
1907.     return ptr;
1908. }
1909. 
1910. int
1911. mongets(mtmp, otyp)
1912. register struct monst *mtmp;
1913. int otyp;
1914. {
1915.     register struct obj *otmp;
1916.     int spe;
1917. 
1918.     if (!otyp)
1919.         return 0;
1920.     otmp = mksobj(otyp, TRUE, FALSE);
1921.     if (otmp) {
1922.         if (mtmp->data->mlet == S_DEMON) {
1923.             /* demons never get blessed objects */
1924.             if (otmp->blessed)
1925.                 curse(otmp);
1926.         } else if (is_lminion(mtmp)) {
1927.             /* lawful minions don't get cursed, bad, or rusting objects */
1928.             otmp->cursed = FALSE;
1929.             if (otmp->spe < 0)
1930.                 otmp->spe = 0;
1931.             otmp->oerodeproof = TRUE;
1932.         } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1933.             otmp->spe = (3 + rn2(4));
1934.         }
1935. 
1936.         if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1937.             otmp->spe = 0;
1938.             otmp->age = 0L;
1939.             otmp->lamplit = FALSE;
1940.             otmp->blessed = otmp->cursed = FALSE;
1941.         } else if (otmp->otyp == BELL_OF_OPENING) {
1942.             otmp->blessed = otmp->cursed = FALSE;
1943.         } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1944.             otmp->blessed = FALSE;
1945.             otmp->cursed = TRUE;
1946.         }
1947. 
1948.         /* leaders don't tolerate inferior quality battle gear */
1949.         if (is_prince(mtmp->data)) {
1950.             if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1951.                 otmp->spe = 1;
1952.             else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1953.                 otmp->spe = 0;
1954.         }
1955. 
1956.         spe = otmp->spe;
1957.         (void) mpickobj(mtmp, otmp); /* might free otmp */
1958.         return spe;
1959.     }
1960.     return 0;
1961. }
1962. 
1963. int
1964. golemhp(type)
1965. int type;
1966. {
1967.     switch (type) {
1968.     case PM_STRAW_GOLEM:
1969.         return 20;
1970.     case PM_PAPER_GOLEM:
1971.         return 20;
1972.     case PM_ROPE_GOLEM:
1973.         return 30;
1974.     case PM_LEATHER_GOLEM:
1975.         return 40;
1976.     case PM_GOLD_GOLEM:
1977.         return 40;
1978.     case PM_WOOD_GOLEM:
1979.         return 50;
1980.     case PM_FLESH_GOLEM:
1981.         return 40;
1982.     case PM_CLAY_GOLEM:
1983.         return 50;
1984.     case PM_STONE_GOLEM:
1985.         return 60;
1986.     case PM_GLASS_GOLEM:
1987.         return 60;
1988.     case PM_IRON_GOLEM:
1989.         return 80;
1990.     default:
1991.         return 0;
1992.     }
1993. }
1994. 
1995. /*
1996.  *      Alignment vs. yours determines monster's attitude to you.
1997.  *      (Some "animal" types are co-aligned, but also hungry.)
1998.  */
1999. boolean
2000. peace_minded(ptr)
2001. register struct permonst *ptr;
2002. {
2003.     aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
2004. 
2005.     if (always_peaceful(ptr))
2006.         return TRUE;
2007.     if (always_hostile(ptr))
2008.         return FALSE;
2009.     if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
2010.         return TRUE;
2011.     if (ptr->msound == MS_NEMESIS)
2012.         return FALSE;
2013. 
2014.     if (race_peaceful(ptr))
2015.         return TRUE;
2016.     if (race_hostile(ptr))
2017.         return FALSE;
2018. 
2019.     /* the monster is hostile if its alignment is different from the
2020.      * player's */
2021.     if (sgn(mal) != sgn(ual))
2022.         return FALSE;
2023. 
2024.     /* Negative monster hostile to player with Amulet. */
2025.     if (mal < A_NEUTRAL && u.uhave.amulet)
2026.         return FALSE;
2027. 
2028.     /* minions are hostile to players that have strayed at all */
2029.     if (is_minion(ptr))
2030.         return (boolean) (u.ualign.record >= 0);
2031. 
2032.     /* Last case:  a chance of a co-aligned monster being
2033.      * hostile.  This chance is greater if the player has strayed
2034.      * (u.ualign.record negative) or the monster is not strongly aligned.
2035.      */
2036.     return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
2037.                                                         : u.ualign.record))
2038.                       && !!rn2(2 + abs(mal)));
2039. }
2040. 
2041. /* Set malign to have the proper effect on player alignment if monster is
2042.  * killed.  Negative numbers mean it's bad to kill this monster; positive
2043.  * numbers mean it's good.  Since there are more hostile monsters than
2044.  * peaceful monsters, the penalty for killing a peaceful monster should be
2045.  * greater than the bonus for killing a hostile monster to maintain balance.
2046.  * Rules:
2047.  *   it's bad to kill peaceful monsters, potentially worse to kill always-
2048.  *      peaceful monsters;
2049.  *   it's never bad to kill a hostile monster, although it may not be good.
2050.  */
2051. void
2052. set_malign(mtmp)
2053. struct monst *mtmp;
2054. {
2055.     schar mal = mtmp->data->maligntyp;
2056.     boolean coaligned;
2057. 
2058.     if (mtmp->ispriest || mtmp->isminion) {
2059.         /* some monsters have individual alignments; check them */
2060.         if (mtmp->ispriest && EPRI(mtmp))
2061.             mal = EPRI(mtmp)->shralign;
2062.         else if (mtmp->isminion && EMIN(mtmp))
2063.             mal = EMIN(mtmp)->min_align;
2064.         /* unless alignment is none, set mal to -5,0,5 */
2065.         /* (see align.h for valid aligntyp values)     */
2066.         if (mal != A_NONE)
2067.             mal *= 5;
2068.     }
2069. 
2070.     coaligned = (sgn(mal) == sgn(u.ualign.type));
2071.     if (mtmp->data->msound == MS_LEADER) {
2072.         mtmp->malign = -20;
2073.     } else if (mal == A_NONE) {
2074.         if (mtmp->mpeaceful)
2075.             mtmp->malign = 0;
2076.         else
2077.             mtmp->malign = 20; /* really hostile */
2078.     } else if (always_peaceful(mtmp->data)) {
2079.         int absmal = abs(mal);
2080.         if (mtmp->mpeaceful)
2081.             mtmp->malign = -3 * max(5, absmal);
2082.         else
2083.             mtmp->malign = 3 * max(5, absmal); /* renegade */
2084.     } else if (always_hostile(mtmp->data)) {
2085.         int absmal = abs(mal);
2086.         if (coaligned)
2087.             mtmp->malign = 0;
2088.         else
2089.             mtmp->malign = max(5, absmal);
2090.     } else if (coaligned) {
2091.         int absmal = abs(mal);
2092.         if (mtmp->mpeaceful)
2093.             mtmp->malign = -3 * max(3, absmal);
2094.         else /* renegade */
2095.             mtmp->malign = max(3, absmal);
2096.     } else /* not coaligned and therefore hostile */
2097.         mtmp->malign = abs(mal);
2098. }
2099. 
2100. /* allocate a new mcorpsenm field for a monster; only need mextra itself */
2101. void
2102. newmcorpsenm(mtmp)
2103. struct monst *mtmp;
2104. {
2105.     if (!mtmp->mextra)
2106.         mtmp->mextra = newmextra();
2107.     MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
2108. }
2109. 
2110. /* release monster's mcorpsenm field; basically a no-op */
2111. void
2112. freemcorpsenm(mtmp)
2113. struct monst *mtmp;
2114. {
2115.     if (has_mcorpsenm(mtmp))
2116.         MCORPSENM(mtmp) = NON_PM;
2117. }
2118. 
2119. static NEARDATA char syms[] = {
2120.     MAXOCLASSES,  MAXOCLASSES + 1, RING_CLASS,   WAND_CLASS,   WEAPON_CLASS,
2121.     FOOD_CLASS,   COIN_CLASS,      SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
2122.     AMULET_CLASS, TOOL_CLASS,      ROCK_CLASS,   GEM_CLASS,    SPBOOK_CLASS,
2123.     S_MIMIC_DEF,  S_MIMIC_DEF,
2124. };
2125. 
2126. void
2127. set_mimic_sym(mtmp)
2128. register struct monst *mtmp;
2129. {
2130.     int typ, roomno, rt;
2131.     unsigned appear, ap_type;
2132.     int s_sym;
2133.     struct obj *otmp;
2134.     int mx, my;
2135. 
2136.     if (!mtmp || Protection_from_shape_changers)
2137.         return;
2138.     mx = mtmp->mx;
2139.     my = mtmp->my;
2140.     typ = levl[mx][my].typ;
2141.     /* only valid for INSIDE of room */
2142.     roomno = levl[mx][my].roomno - ROOMOFFSET;
2143.     if (roomno >= 0)
2144.         rt = rooms[roomno].rtype;
2145. #ifdef SPECIALIZATION
2146.     else if (IS_ROOM(typ))
2147.         rt = OROOM, roomno = 0;
2148. #endif
2149.     else
2150.         rt = 0; /* roomno < 0 case for GCC_WARN */
2151. 
2152.     if (OBJ_AT(mx, my)) {
2153.         ap_type = M_AP_OBJECT;
2154.         appear = level.objects[mx][my]->otyp;
2155.     } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2156.         ap_type = M_AP_FURNITURE;
2157.         /*
2158.          *  If there is a wall to the left that connects to this
2159.          *  location, then the mimic mimics a horizontal closed door.
2160.          *  This does not allow doors to be in corners of rooms.
2161.          *  Since rogue has no closed doors, mimic a wall there
2162.          *  (yes, mimics can end up on this level by various means).
2163.          */
2164.         if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2165.                         || levl[mx - 1][my].typ == TLCORNER
2166.                         || levl[mx - 1][my].typ == TRWALL
2167.                         || levl[mx - 1][my].typ == BLCORNER
2168.                         || levl[mx - 1][my].typ == TDWALL
2169.                         || levl[mx - 1][my].typ == CROSSWALL
2170.                         || levl[mx - 1][my].typ == TUWALL))
2171.             appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2172.         else
2173.             appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2174.     } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2175.         ap_type = M_AP_OBJECT;
2176.         appear = STATUE;
2177.     } else if (roomno < 0 && !t_at(mx, my)) {
2178.         ap_type = M_AP_OBJECT;
2179.         appear = BOULDER;
2180.     } else if (rt == ZOO || rt == VAULT) {
2181.         ap_type = M_AP_OBJECT;
2182.         appear = GOLD_PIECE;
2183.     } else if (rt == DELPHI) {
2184.         if (rn2(2)) {
2185.             ap_type = M_AP_OBJECT;
2186.             appear = STATUE;
2187.         } else {
2188.             ap_type = M_AP_FURNITURE;
2189.             appear = S_fountain;
2190.         }
2191.     } else if (rt == TEMPLE) {
2192.         ap_type = M_AP_FURNITURE;
2193.         appear = S_altar;
2194.         /*
2195.          * We won't bother with beehives, morgues, barracks, throne rooms
2196.          * since they shouldn't contain too many mimics anyway...
2197.          */
2198.     } else if (rt >= SHOPBASE) {
2199.         s_sym = get_shop_item(rt - SHOPBASE);
2200.         if (s_sym < 0) {
2201.             ap_type = M_AP_OBJECT;
2202.             appear = -s_sym;
2203.         } else {
2204.             if (s_sym == RANDOM_CLASS)
2205.                 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2206.             goto assign_sym;
2207.         }
2208.     } else {
2209.         s_sym = syms[rn2((int) sizeof(syms))];
2210.  assign_sym:
2211.         if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2212.             ap_type = M_AP_FURNITURE;
2213.             appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2214.         } else {
2215.             ap_type = M_AP_OBJECT;
2216.             if (s_sym == S_MIMIC_DEF) {
2217.                 appear = STRANGE_OBJECT;
2218.             } else if (s_sym == COIN_CLASS) {
2219.                 appear = GOLD_PIECE;
2220.             } else {
2221.                 otmp = mkobj((char) s_sym, FALSE);
2222.                 appear = otmp->otyp;
2223.                 /* make sure container contents are free'ed */
2224.                 obfree(otmp, (struct obj *) 0);
2225.             }
2226.         }
2227.     }
2228.     mtmp->m_ap_type = ap_type;
2229.     mtmp->mappearance = appear;
2230.     /* when appearing as an object based on a monster type, pick a shape */
2231.     if (ap_type == M_AP_OBJECT
2232.         && (appear == STATUE || appear == FIGURINE
2233.             || appear == CORPSE || appear == EGG || appear == TIN)) {
2234.         int mndx = rndmonnum(),
2235.             nocorpse_ndx = (mvitals[mndx].mvflags & G_NOCORPSE) != 0;
2236. 
2237.         if (appear == CORPSE && nocorpse_ndx)
2238.             mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
2239.         else if ((appear == EGG && !can_be_hatched(mndx))
2240.                  || (appear == TIN && nocorpse_ndx))
2241.             mndx = NON_PM; /* revert to generic egg or empty tin */
2242. 
2243.         newmcorpsenm(mtmp);
2244.         MCORPSENM(mtmp) = mndx;
2245.     }
2246. 
2247.     if (does_block(mx, my, &levl[mx][my]))
2248.         block_point(mx, my);
2249. }
2250. 
2251. /* release monster from bag of tricks; return number of monsters created */
2252. int
2253. bagotricks(bag, tipping, seencount)
2254. struct obj *bag;
2255. boolean tipping; /* caller emptying entire contents; affects shop handling */
2256. int *seencount;  /* secondary output */
2257. {
2258.     int moncount = 0;
2259. 
2260.     if (!bag || bag->otyp != BAG_OF_TRICKS) {
2261.         impossible("bad bag o' tricks");
2262.     } else if (bag->spe < 1) {
2263.         /* if tipping known empty bag, give normal empty container message */
2264.         pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2265.         /* now known to be empty if sufficiently discovered */
2266.         if (bag->dknown && objects[bag->otyp].oc_name_known)
2267.             bag->cknown = 1;
2268.     } else {
2269.         struct monst *mtmp;
2270.         int creatcnt = 1, seecount = 0;
2271. 
2272.         consume_obj_charge(bag, !tipping);
2273. 
2274.         if (!rn2(23))
2275.             creatcnt += rnd(7);
2276.         do {
2277.             mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2278.             if (mtmp) {
2279.                 ++moncount;
2280.                 if (canspotmon(mtmp))
2281.                     ++seecount;
2282.             }
2283.         } while (--creatcnt > 0);
2284.         if (seecount) {
2285.             if (seencount)
2286.                 *seencount += seecount;
2287.             if (bag->dknown)
2288.                 makeknown(BAG_OF_TRICKS);
2289.         } else if (!tipping) {
2290.             pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2291.         }
2292.     }
2293.     return moncount;
2294. }
2295. 
2296. /*makemon.c*/