OSDN Git Service

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