OSDN Git Service

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