OSDN Git Service

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