1 /* NetHack 3.6 makemon.c $NHDT-Date: 1556150377 2019/04/24 23:59:37 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.134 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Robert Patrick Rankin, 2012. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* JNetHack Copyright */
7 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
8 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2019 */
9 /* JNetHack may be freely redistributed. See license for details. */
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))
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, 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 *,
33 #define m_initsgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 3, mmf)
34 #define m_initlgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 10, mmf)
35 #define toostrong(monindx, lev) (mons[monindx].difficulty > lev)
36 #define tooweak(monindx, lev) (mons[monindx].difficulty < lev)
39 is_home_elemental(ptr)
42 if (ptr->mlet == S_ELEMENTAL)
43 switch (monsndx(ptr)) {
44 case PM_AIR_ELEMENTAL:
45 return Is_airlevel(&u.uz);
46 case PM_FIRE_ELEMENTAL:
47 return Is_firelevel(&u.uz);
48 case PM_EARTH_ELEMENTAL:
49 return Is_earthlevel(&u.uz);
50 case PM_WATER_ELEMENTAL:
51 return Is_waterlevel(&u.uz);
57 * Return true if the given monster cannot exist on this elemental level.
63 if (ptr->mlet == S_ELEMENTAL) {
64 return (boolean) !is_home_elemental(ptr);
65 } else if (Is_earthlevel(&u.uz)) {
66 /* no restrictions? */
67 } else if (Is_waterlevel(&u.uz)) {
68 /* just monsters that can swim */
71 } else if (Is_firelevel(&u.uz)) {
72 if (!pm_resistance(ptr, MR_FIRE))
74 } else if (Is_airlevel(&u.uz)) {
75 if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
76 && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
82 /* make a group just like mtmp */
84 m_initgrp(mtmp, x, y, n, mmflags)
89 register int cnt = rnd(n);
91 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
92 /* There is an unresolved problem with several people finding that
93 * the game hangs eating CPU; if interrupted and restored, the level
94 * will be filled with monsters. Of those reports giving system type,
95 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
96 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
97 * problem went away for him and another reporter-to-newsgroup
98 * after adding this debugging code. This has almost got to be a
99 * compiler bug, but until somebody tracks it down and gets it fixed,
100 * might as well go with the "but it went away when I tried to find
106 debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
107 cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
109 /* Tuning: cut down on swarming at low character levels [mrs] */
110 cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
111 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
112 if (cnt != (cnttmp / cntdiv)) {
113 pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
114 (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
119 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
129 if (peace_minded(mtmp->data))
131 /* Don't create groups of peaceful monsters since they'll get
132 * in our way. If the monster has a percentage chance so some
133 * are peaceful and some are not, the result will just be a
136 if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
137 mon = makemon(mtmp->data, mm.x, mm.y, (mmflags | MM_NOGRP));
139 mon->mpeaceful = FALSE;
142 /* Undo the second peace_minded() check in makemon(); if the
143 * monster turned out to be peaceful the first time we
144 * didn't create it at all; we don't want a second check.
153 m_initthrow(mtmp, otyp, oquan)
157 register struct obj *otmp;
159 otmp = mksobj(otyp, TRUE, FALSE);
160 otmp->quan = (long) rn1(oquan, 3);
161 otmp->owt = weight(otmp);
162 if (otyp == ORCISH_ARROW)
163 otmp->opoisoned = TRUE;
164 (void) mpickobj(mtmp, otmp);
169 register struct monst *mtmp;
171 register struct permonst *ptr = mtmp->data;
172 register int mm = monsndx(ptr);
174 int bias, spe2, w1, w2;
176 if (Is_rogue_level(&u.uz))
179 * First a few special cases:
180 * giants get a boulder to throw sometimes
182 * kobolds get darts to throw
183 * centaurs get some sort of bow & arrows or bolts
184 * soldiers get all sorts of things
185 * kops get clubs & cream pies.
190 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
193 if (is_mercenary(ptr)) {
199 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
200 w2 = rn2(2) ? DAGGER : KNIFE;
202 w1 = rn2(2) ? SPEAR : SHORT_SWORD;
205 w1 = rn2(2) ? FLAIL : MACE;
208 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
211 case PM_WATCH_CAPTAIN:
212 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
222 (void) mongets(mtmp, w1);
223 if (!w2 && w1 != DAGGER && !rn2(4))
226 (void) mongets(mtmp, w2);
227 } else if (is_elf(ptr)) {
230 rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
232 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
234 (void) mongets(mtmp, ELVEN_BOOTS);
236 (void) mongets(mtmp, ELVEN_DAGGER);
240 (void) mongets(mtmp, ELVEN_SHIELD);
242 (void) mongets(mtmp, ELVEN_SHORT_SWORD);
243 (void) mongets(mtmp, ELVEN_BOW);
244 m_initthrow(mtmp, ELVEN_ARROW, 12);
247 (void) mongets(mtmp, ELVEN_BROADSWORD);
249 (void) mongets(mtmp, ELVEN_SHIELD);
253 (void) mongets(mtmp, ELVEN_SPEAR);
254 (void) mongets(mtmp, ELVEN_SHIELD);
258 if (mm == PM_ELVENKING) {
259 if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
260 (void) mongets(mtmp, PICK_AXE);
262 (void) mongets(mtmp, CRYSTAL_BALL);
264 } else if (ptr->msound == MS_PRIEST
265 || quest_mon_represents_role(ptr, PM_PRIEST)) {
266 otmp = mksobj(MACE, FALSE, FALSE);
271 (void) mpickobj(mtmp, otmp);
273 } else if (mm == PM_NINJA) { /* extra quest villains */
274 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
275 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
276 } else if (ptr->msound == MS_GUARDIAN) {
277 /* quest "guardians" */
286 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
288 (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET
291 (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
293 (void) mongets(mtmp, POT_HEALING);
299 (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
300 (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
302 (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
304 (void) mongets(mtmp, LEATHER_CLOAK);
306 (void) mongets(mtmp, BOW);
307 m_initthrow(mtmp, ARROW, 12);
311 (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
313 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET
315 (void) mongets(mtmp, BOW);
316 m_initthrow(mtmp, ARROW, 12);
319 (void) mongets(mtmp, CLUB);
320 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
322 (void) mongets(mtmp, LEATHER_GLOVES);
323 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
326 (void) mongets(mtmp, CLUB);
327 (void) mongets(mtmp, LEATHER_ARMOR);
335 /* create minion stuff; can't use mongets */
336 otmp = mksobj(LONG_SWORD, FALSE, FALSE);
338 /* maybe make it special */
339 if (!rn2(20) || is_lord(ptr))
341 artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
343 otmp->oerodeproof = TRUE;
345 otmp->spe = max(otmp->spe, spe2);
346 (void) mpickobj(mtmp, otmp);
348 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
351 otmp->cursed = FALSE;
352 otmp->oerodeproof = TRUE;
354 (void) mpickobj(mtmp, otmp);
359 if (mm == PM_HOBBIT) {
362 (void) mongets(mtmp, DAGGER);
365 (void) mongets(mtmp, ELVEN_DAGGER);
368 (void) mongets(mtmp, SLING);
372 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
374 (void) mongets(mtmp, DWARVISH_CLOAK);
375 } else if (is_dwarf(ptr)) {
377 (void) mongets(mtmp, DWARVISH_CLOAK);
379 (void) mongets(mtmp, IRON_SHOES);
381 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
382 /* note: you can't use a mattock with a shield */
384 (void) mongets(mtmp, DWARVISH_MATTOCK);
386 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
387 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
389 (void) mongets(mtmp, DWARVISH_IRON_HELM);
391 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
393 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
398 /* create Keystone Kops with cream pies to
399 throw. As suggested by KAA. [MRS] */
401 m_initthrow(mtmp, CREAM_PIE, 2);
403 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
407 (void) mongets(mtmp, ORCISH_HELM);
408 switch ((mm != PM_ORC_CAPTAIN) ? mm
409 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
412 (void) mongets(mtmp, SCIMITAR);
414 (void) mongets(mtmp, ORCISH_SHIELD);
416 (void) mongets(mtmp, KNIFE);
418 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
422 (void) mongets(mtmp, ORCISH_CLOAK);
424 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
426 (void) mongets(mtmp, IRON_SHOES);
428 (void) mongets(mtmp, ORCISH_BOW);
429 m_initthrow(mtmp, ORCISH_ARROW, 12);
432 (void) mongets(mtmp, URUK_HAI_SHIELD);
435 if (mm != PM_ORC_SHAMAN && rn2(2))
436 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
442 if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
443 (void) mongets(mtmp, BATTLE_AXE);
445 (void) mongets(mtmp, CLUB);
451 (void) mongets(mtmp, RANSEUR);
454 (void) mongets(mtmp, PARTISAN);
457 (void) mongets(mtmp, GLAIVE);
460 (void) mongets(mtmp, SPETUM);
466 m_initthrow(mtmp, DART, 12);
471 if (ptr == &mons[PM_FOREST_CENTAUR]) {
472 (void) mongets(mtmp, BOW);
473 m_initthrow(mtmp, ARROW, 12);
475 (void) mongets(mtmp, CROSSBOW);
476 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
481 (void) mongets(mtmp, KNIFE);
482 (void) mongets(mtmp, LONG_SWORD);
486 (void) mongets(mtmp, LEATHER_ARMOR);
488 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
491 if (mm == PM_SALAMANDER)
493 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
498 (void) mongets(mtmp, BULLWHIP);
499 (void) mongets(mtmp, BROADSWORD);
502 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
504 case PM_HORNED_DEVIL:
505 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
508 (void) mongets(mtmp, WAN_STRIKING);
511 (void) mongets(mtmp, FLAIL);
514 /* prevent djinn and mail daemons from leaving objects when
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.
526 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
527 switch (rnd(14 - (2 * bias))) {
529 if (strongmonst(ptr))
530 (void) mongets(mtmp, BATTLE_AXE);
532 m_initthrow(mtmp, DART, 12);
535 if (strongmonst(ptr))
536 (void) mongets(mtmp, TWO_HANDED_SWORD);
538 (void) mongets(mtmp, CROSSBOW);
539 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
543 (void) mongets(mtmp, BOW);
544 m_initthrow(mtmp, ARROW, 12);
547 if (strongmonst(ptr))
548 (void) mongets(mtmp, LONG_SWORD);
550 m_initthrow(mtmp, DAGGER, 3);
553 if (strongmonst(ptr))
554 (void) mongets(mtmp, LUCERN_HAMMER);
556 (void) mongets(mtmp, AKLYS);
564 if ((int) mtmp->m_lev > rn2(75))
565 (void) mongets(mtmp, rnd_offensive_item(mtmp));
569 * Makes up money for monster's inventory.
570 * This will change with silver & copper coins
573 mkmonmoney(mtmp, amount)
577 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
580 add_to_minv(mtmp, gold);
585 register struct monst *mtmp;
588 register struct obj *otmp;
589 register struct permonst *ptr = mtmp->data;
591 if (Is_rogue_level(&u.uz))
594 * Soldiers get armour & rations - armour approximates their ac.
595 * Nymphs may get mirror or potion of object detection.
599 if (is_mercenary(ptr)) {
602 switch (monsndx(ptr)) {
621 case PM_WATCH_CAPTAIN:
625 impossible("odd mercenary %d?", monsndx(ptr));
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))
635 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
637 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
638 : STUDDED_LEATHER_ARMOR);
640 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
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);
659 nhUse(mac); /* suppress 'dead increment' from static analyzer */
661 if (ptr == &mons[PM_WATCH_CAPTAIN]) {
662 ; /* better weapon rather than extra gear here */
663 } else if (ptr == &mons[PM_WATCHMAN]) {
664 if (rn2(3)) /* most watchmen carry a whistle */
665 (void) mongets(mtmp, TIN_WHISTLE);
666 } else if (ptr == &mons[PM_GUARD]) {
667 /* if hero teleports out of a vault while being confronted
668 by the vault's guard, there is a shrill whistling sound,
669 so guard evidently carries a cursed whistle */
670 otmp = mksobj(TIN_WHISTLE, TRUE, FALSE);
672 (void) mpickobj(mtmp, otmp);
673 } else { /* soldiers and their officers */
675 (void) mongets(mtmp, K_RATION);
677 (void) mongets(mtmp, C_RATION);
678 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
679 (void) mongets(mtmp, BUGLE);
681 } else if (ptr == &mons[PM_SHOPKEEPER]) {
682 (void) mongets(mtmp, SKELETON_KEY);
684 /* MAJOR fall through ... */
686 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
689 (void) mongets(mtmp, POT_EXTRA_HEALING);
692 (void) mongets(mtmp, POT_HEALING);
695 (void) mongets(mtmp, WAN_STRIKING);
697 } else if (ptr->msound == MS_PRIEST
698 || quest_mon_represents_role(ptr, PM_PRIEST)) {
699 (void) mongets(mtmp, rn2(7) ? ROBE
700 : rn2(3) ? CLOAK_OF_PROTECTION
701 : CLOAK_OF_MAGIC_RESISTANCE);
702 (void) mongets(mtmp, SMALL_SHIELD);
703 mkmonmoney(mtmp, (long) rn1(10, 20));
704 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
705 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
710 (void) mongets(mtmp, MIRROR);
712 (void) mongets(mtmp, POT_OBJECT_DETECTION);
715 if (ptr == &mons[PM_MINOTAUR]) {
716 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
717 (void) mongets(mtmp, WAN_DIGGING);
718 } else if (is_giant(ptr)) {
719 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
720 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
722 otmp->quan = (long) rn1(2, 3);
723 otmp->owt = weight(otmp);
724 (void) mpickobj(mtmp, otmp);
729 if (ptr == &mons[PM_NAZGUL]) {
730 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
732 (void) mpickobj(mtmp, otmp);
736 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
737 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
738 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
739 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
740 rn2(13) ? FALSE : TRUE);
744 otmp->oerodeproof = 1;
745 (void) mpickobj(mtmp, otmp);
750 (void) mongets(mtmp, MUMMY_WRAPPING);
754 struct obj *catcorpse;
756 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
757 /* we used to just set the flag, which resulted in weight()
758 treating the box as being heavier by the weight of a cat;
759 now we include a cat corpse that won't rot; when opening or
760 disclosing the box's contents, the corpse might be revived,
761 otherwise it's given a rot timer; weight is now ordinary */
762 if ((catcorpse = mksobj(CORPSE, TRUE, FALSE)) != 0) {
763 otmp->spe = 1; /* flag for special SchroedingersBox */
764 set_corpsenm(catcorpse, PM_HOUSECAT);
765 (void) stop_timer(ROT_CORPSE, obj_to_any(catcorpse));
766 add_to_container(otmp, catcorpse);
767 otmp->owt = weight(otmp);
769 (void) mpickobj(mtmp, otmp);
773 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
776 /* moved here from m_initweap() because these don't
777 have AT_WEAP so m_initweap() is not called for them */
778 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
779 (void) mongets(mtmp, SPEAR);
780 } else if (ptr == &mons[PM_ASMODEUS]) {
781 (void) mongets(mtmp, WAN_COLD);
782 (void) mongets(mtmp, WAN_FIRE);
786 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
787 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
789 otmp->owt = weight(otmp);
790 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
791 begin_burn(otmp, FALSE);
798 /* ordinary soldiers rarely have access to magic (or gold :-) */
799 if (ptr == &mons[PM_SOLDIER] && rn2(13))
802 if ((int) mtmp->m_lev > rn2(50))
803 (void) mongets(mtmp, rnd_defensive_item(mtmp));
804 if ((int) mtmp->m_lev > rn2(100))
805 (void) mongets(mtmp, rnd_misc_item(mtmp));
806 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
808 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
811 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
815 xchar x, y; /* clone's preferred location or 0 (near mon) */
820 /* may be too weak or have been extinguished for population control */
821 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
822 return (struct monst *) 0;
827 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
828 return (struct monst *) 0;
829 } else if (!isok(x, y)) {
830 return (struct monst *) 0; /* paranoia */
834 if (MON_AT(mm.x, mm.y)) {
835 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
836 return (struct monst *) 0;
840 *m2 = *mon; /* copy condition of old monster */
841 m2->mextra = (struct mextra *) 0;
844 m2->m_id = context.ident++;
846 m2->m_id = context.ident++; /* ident overflowed */
853 m2->minvent = (struct obj *) 0; /* objects don't clone */
854 m2->mleashed = FALSE;
855 /* Max HP the same, but current HP halved for both. The caller
856 * might want to override this by halving the max HP also.
857 * When current HP is odd, the original keeps the extra point.
858 * We know original has more than 1 HP, so both end up with at least 1.
860 m2->mhpmax = mon->mhpmax;
861 m2->mhp = mon->mhp / 2;
864 /* since shopkeepers and guards will only be cloned if they've been
865 * polymorphed away from their original forms, the clone doesn't have
866 * room for the extra information. we also don't want two shopkeepers
867 * around for the same shop.
874 m2->ispriest = FALSE;
875 place_monster(m2, m2->mx, m2->my);
876 if (emits_light(m2->data))
877 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
879 if (has_mname(mon)) {
880 m2 = christen_monst(m2, MNAME(mon));
881 } else if (mon->isshk) {
882 m2 = christen_monst(m2, shkname(mon));
885 /* not all clones caused by player are tame or peaceful */
886 if (!context.mon_moving) {
888 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
889 else if (mon->mpeaceful)
890 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
893 newsym(m2->mx, m2->my); /* display the new monster */
898 *(EMIN(m2)) = *(EMIN(mon));
900 /* because m2 is a copy of mon it is tame but not init'ed.
901 * however, tamedog will not re-tame a tame dog, so m2
902 * must be made non-tame to get initialized properly.
905 if (tamedog(m2, (struct obj *) 0)) {
906 *(EDOG(m2)) = *(EDOG(mon));
916 * Propagate a species
918 * Once a certain number of monsters are created, don't create any more
919 * at random (i.e. make them extinct). The previous (3.2) behavior was
920 * to do this when a certain number had _died_, which didn't make
923 * Returns FALSE propagation unsuccessful
924 * TRUE propagation successful
927 propagate(mndx, tally, ghostly)
933 uchar lim = mbirth_limit(mndx);
934 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
936 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
938 /* if it's unique, don't ever make it again */
939 if ((mons[mndx].geno & G_UNIQ) && mndx != PM_HIGH_PRIEST)
940 mvitals[mndx].mvflags |= G_EXTINCT;
942 if (mvitals[mndx].born < 255 && tally
943 && (!ghostly || (ghostly && result)))
944 mvitals[mndx].born++;
945 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
946 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
948 debugpline1("Automatically extinguished %s.",
949 makeplural(mons[mndx].mname));
951 mvitals[mndx].mvflags |= G_EXTINCT;
952 reset_rndmonst(mndx);
957 /* amount of HP to lose from level drain (or gain from Stormbringer) */
962 struct permonst *ptr = mon->data;
963 int hp = rnd(8); /* default is d8 */
965 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
967 /* draining usually won't be applicable for these critters */
968 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
969 } else if (ptr->mlevel > 49) {
970 /* arbitrary; such monsters won't be involved in draining anyway */
971 hp = 4 + rnd(4); /* 5..8 */
972 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
973 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
975 hp = 4 + rn2(5); /* 4..8 */
976 } else if (!mon->m_lev) {
977 /* level 0 monsters use 1d4 instead of Nd8 */
983 /* set up a new monster's initial level and hit points;
984 used by newcham() as well as by makemon() */
990 struct permonst *ptr = &mons[mndx];
992 mon->m_lev = adj_lev(ptr);
994 mon->mhpmax = mon->mhp = golemhp(mndx);
995 } else if (is_rider(ptr)) {
996 /* we want low HP, but a high mlevel so they can attack well */
997 mon->mhpmax = mon->mhp = d(10, 8);
998 } else if (ptr->mlevel > 49) {
999 /* "special" fixed hp monster
1000 * the hit points are encoded in the mlevel in a somewhat strange
1001 * way to fit in the 50..127 positive range of a signed character
1002 * above the 1..49 that indicate "normal" monster levels */
1003 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
1004 mon->m_lev = mon->mhp / 4; /* approximation */
1005 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
1007 mon->mhpmax = mon->mhp =
1008 (int) (In_endgame(&u.uz)
1010 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
1011 } else if (!mon->m_lev) {
1012 mon->mhpmax = mon->mhp = rnd(4);
1014 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
1015 if (is_home_elemental(ptr))
1016 mon->mhpmax = (mon->mhp *= 3);
1023 struct mextra *mextra;
1025 mextra = (struct mextra *) alloc(sizeof(struct mextra));
1032 mextra->mcorpsenm = NON_PM;
1037 makemon_rnd_goodpos(mon, gpflags, cc)
1047 nx = rn1(COLNO - 3, 2);
1049 good = (!in_mklev && cansee(nx,ny)) ? FALSE
1050 : goodpos(nx, ny, mon, gpflags);
1051 } while ((++tryct < 50) && !good);
1054 /* else go through all map positions, twice, first round
1055 ignoring positions in sight, and pick first good one.
1056 skip first round if we're in special level loader or blind */
1060 int bl = (in_mklev || Blind) ? 1 : 0;
1062 for ( ; bl < 2; bl++) {
1063 for (dx = 0; dx < COLNO; dx++)
1064 for (dy = 0; dy < ROWNO; dy++) {
1065 nx = ((dx + xofs) % (COLNO - 1)) + 1;
1066 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
1067 if (bl == 0 && cansee(nx,ny))
1069 if (goodpos(nx, ny, mon, gpflags))
1072 if (bl == 0 && (!mon || mon->data->mmove)) {
1073 /* all map positions are visible (or not good),
1074 try to pick something logical */
1075 if (dnstair.sx && !rn2(2)) {
1078 } else if (upstair.sx && !rn2(2)) {
1081 } else if (dnladder.sx && !rn2(2)) {
1084 } else if (upladder.sx && !rn2(2)) {
1088 if (goodpos(nx, ny, mon, gpflags))
1102 * called with [x,y] = coordinates;
1103 * [0,0] means anyplace
1104 * [u.ux,u.uy] means: near player (if !in_mklev)
1106 * In case we make a monster group, only return the one at [x,y].
1109 makemon(ptr, x, y, mmflags)
1110 register struct permonst *ptr;
1114 register struct monst *mtmp;
1115 int mndx, mcham, ct, mitem;
1116 boolean anymon = (!ptr);
1117 boolean byyou = (x == u.ux && y == u.uy);
1118 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1119 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1120 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1122 /* if caller wants random location, do it here */
1123 if (x == 0 && y == 0) {
1125 struct monst fakemon;
1127 cc.x = cc.y = 0; /* lint suppression */
1128 fakemon.data = ptr; /* set up for goodpos */
1129 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1131 return (struct monst *) 0;
1134 } else if (byyou && !in_mklev) {
1137 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1141 return (struct monst *) 0;
1144 /* Does monster already exist at the position? */
1146 if ((mmflags & MM_ADJACENTOK) != 0) {
1148 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1152 return (struct monst *) 0;
1154 return (struct monst *) 0;
1158 mndx = monsndx(ptr);
1159 /* if you are to make a specific monster and it has
1160 already been genocided, return */
1161 if (mvitals[mndx].mvflags & G_GENOD)
1162 return (struct monst *) 0;
1163 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1164 debugpline1("Explicitly creating extinct monster %s.",
1168 /* make a random (common) monster that can survive here.
1169 * (the special levels ask for random monsters at specific
1170 * positions, causing mass drowning on the medusa level,
1173 int tryct = 0; /* maybe there are no good choices */
1174 struct monst fakemon;
1177 if (!(ptr = rndmonst())) {
1178 debugpline0("Warning: no monster.");
1179 return (struct monst *) 0; /* no more monsters! */
1181 fakemon.data = ptr; /* set up for goodpos */
1182 } while (++tryct <= 50
1183 /* in Sokoban, don't accept a giant on first try;
1184 after that, boulder carriers are fair game */
1185 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1186 || !goodpos(x, y, &fakemon, gpflags)));
1187 mndx = monsndx(ptr);
1189 (void) propagate(mndx, countbirth, FALSE);
1191 *mtmp = zeromonst; /* clear all entries in structure */
1193 if (mmflags & MM_EGD)
1195 if (mmflags & MM_EPRI)
1197 if (mmflags & MM_ESHK)
1199 if (mmflags & MM_EMIN)
1201 if (mmflags & MM_EDOG)
1203 if (mmflags & MM_ASLEEP)
1204 mtmp->msleeping = 1;
1207 mtmp->m_id = context.ident++;
1209 mtmp->m_id = context.ident++; /* ident overflowed */
1210 set_mon_data(mtmp, ptr); /* mtmp->data = ptr; */
1211 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1212 quest_status.leader_m_id = mtmp->m_id;
1215 /* set up level and hit points */
1216 newmonhp(mtmp, mndx);
1219 mtmp->female = TRUE;
1220 else if (is_male(ptr))
1221 mtmp->female = FALSE;
1222 /* leader and nemesis gender is usually hardcoded in mons[],
1223 but for ones which can be random, it has already been chosen
1224 (in role_init(), for possible use by the quest pager code) */
1225 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1226 mtmp->female = quest_status.ldrgend;
1227 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1228 mtmp->female = quest_status.nemgend;
1230 mtmp->female = rn2(2); /* ignored for neuters */
1232 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1233 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1234 /* quest leader and nemesis both know about all trap types */
1235 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1236 mtmp->mtrapseen = ~0;
1238 place_monster(mtmp, x, y);
1239 mtmp->mcansee = mtmp->mcanmove = TRUE;
1240 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1242 switch (ptr->mlet) {
1244 set_mimic_sym(mtmp);
1250 (void) mkobj_at(0, x, y, TRUE);
1251 (void) hideunder(mtmp);
1255 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1256 mtmp->perminvis = TRUE;
1257 mtmp->minvis = TRUE;
1261 (void) hideunder(mtmp);
1264 mtmp->msleeping = 1;
1268 if (rn2(5) && !u.uhave.amulet)
1269 mtmp->msleeping = 1;
1272 if (Race_if(PM_ELF))
1273 mtmp->mpeaceful = FALSE;
1276 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1277 mtmp->mpeaceful = TRUE;
1280 if (Inhell && is_bat(ptr))
1281 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1284 if ((ct = emits_light(mtmp->data)) > 0)
1285 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1286 monst_to_any(mtmp));
1287 mitem = 0; /* extra inventory item for this monster */
1289 if (mndx == PM_VLAD_THE_IMPALER)
1290 mitem = CANDELABRUM_OF_INVOCATION;
1291 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1292 if (!Protection_from_shape_changers
1293 && (mcham = pm_to_cham(mndx)) != NON_PM) {
1294 /* this is a shapechanger after all */
1296 /* Vlad stays in his normal shape so he can carry the Candelabrum */
1297 if (mndx != PM_VLAD_THE_IMPALER
1298 /* Note: shapechanger's initial form used to be chosen here
1299 with rndmonst(), yielding a monster which was appropriate
1300 to the level's difficulty but ignoring the changer's usual
1301 type selection, so was inappropriate for vampshifters.
1302 Let newcham() pick the shape. */
1303 && newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1304 allow_minvent = FALSE;
1305 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1307 context.no_of_wizards++;
1308 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1310 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1311 mtmp = christen_monst(mtmp, rndghostname());
1312 } else if (mndx == PM_CROESUS) {
1313 mitem = TWO_HANDED_SWORD;
1314 } else if (ptr->msound == MS_NEMESIS) {
1315 mitem = BELL_OF_OPENING;
1316 } else if (mndx == PM_PESTILENCE) {
1317 mitem = POT_SICKNESS;
1319 if (mitem && allow_minvent)
1320 (void) mongets(mtmp, mitem);
1323 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1324 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1325 && !u.uhave.amulet && rn2(5))
1326 mtmp->msleeping = TRUE;
1329 newsym(mtmp->mx, mtmp->my);
1333 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1334 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1336 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1337 mtmp->mpeaceful = mtmp->mtame = FALSE;
1340 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1342 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1344 if (mndx == PM_LONG_WORM
1345 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1348 /* we can now create worms with tails - 11/91 */
1349 initworm(mtmp, rn2(5));
1350 if (count_wsegs(mtmp))
1351 place_worm_tail_randomly(mtmp, x, y);
1353 /* it's possible to create an ordinary monster of some special
1354 types; make sure their extended data is initialized to
1355 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1356 (when they're specified, caller intends to handle this itself) */
1357 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1358 ? !(mmflags & (MM_EPRI | MM_EMIN))
1359 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1364 mtmp->isminion = 1; /* make priest be a roamer */
1365 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1366 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1367 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1371 set_malign(mtmp); /* having finished peaceful changes */
1372 if (anymon && !(mmflags & MM_NOGRP)) {
1373 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1374 m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1375 } else if (ptr->geno & G_LGROUP) {
1377 m_initlgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1379 m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1383 if (allow_minvent) {
1385 m_initweap(mtmp); /* equip with weapons / armor */
1386 m_initinv(mtmp); /* add on a few special items incl. more armor */
1387 m_dowear(mtmp, TRUE);
1389 if (!rn2(100) && is_domestic(ptr)
1390 && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) {
1391 struct obj *otmp = mksobj(SADDLE, TRUE, FALSE);
1392 put_saddle_on_mon(otmp, mtmp);
1396 /* no initial inventory is allowed */
1398 discard_minvent(mtmp);
1399 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1401 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1402 if (ptr->mflags3 & M3_WAITFORU)
1403 mtmp->mstrategy |= STRAT_WAITFORU;
1404 if (ptr->mflags3 & M3_CLOSE)
1405 mtmp->mstrategy |= STRAT_CLOSE;
1406 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1407 mtmp->mstrategy |= STRAT_APPEARMSG;
1410 if (allow_minvent && migrating_objs)
1411 deliver_obj_to_mon(mtmp, 1, DF_NONE); /* in case of waiting items */
1414 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1423 /* There is an implicit limit of 4 for "high priest of <deity>",
1424 * but aligned priests can grow into high priests, thus they aren't
1425 * really limited to 4, so leave the default amount in place for them.
1428 /* assert(MAXMONNO < 255); */
1429 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1432 /* used for wand/scroll/spell of create monster */
1433 /* returns TRUE iff you know monsters have been created */
1435 create_critters(cnt, mptr, neverask)
1437 struct permonst *mptr; /* usually null; used for confused reading */
1443 boolean known = FALSE;
1444 boolean ask = (wizard && !neverask);
1448 if (create_particular()) {
1452 ask = FALSE; /* ESC will shut off prompting */
1455 /* if in water, try to encourage an aquatic monster
1456 by finding and then specifying another wet location */
1457 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1460 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1461 if (mon && canspotmon(mon))
1471 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1473 if (mvitals[mndx].mvflags & G_GONE)
1476 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1478 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1482 * shift the probability of a monster's generation by
1483 * comparing the dungeon alignment and monster alignment.
1484 * return an integer in the range of 0-5.
1488 register struct permonst *ptr;
1490 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1491 static NEARDATA s_level *lev;
1492 register int alshift;
1494 if (oldmoves != moves) {
1495 lev = Is_special(&u.uz);
1498 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1499 default: /* just in case */
1504 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1507 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1510 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1516 static NEARDATA struct {
1518 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1519 } rndmonst_state = { -1, { 0 } };
1521 /* select a random monster type */
1525 register struct permonst *ptr;
1526 register int mndx, ct;
1528 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1531 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1532 int zlevel, minmlev, maxmlev;
1536 rndmonst_state.choice_count = 0;
1537 /* look for first common monster */
1538 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1539 if (!uncommon(mndx))
1541 rndmonst_state.mchoices[mndx] = 0;
1543 if (mndx == SPECIAL_PM) {
1544 /* evidently they've all been exterminated */
1545 debugpline0("rndmonst: no common mons!");
1546 return (struct permonst *) 0;
1547 } /* else `mndx' now ready for use below */
1548 zlevel = level_difficulty();
1549 /* determine the level of the weakest monster to make. */
1550 minmlev = zlevel / 6;
1551 /* determine the level of the strongest monster to make. */
1552 maxmlev = (zlevel + u.ulevel) / 2;
1553 upper = Is_rogue_level(&u.uz);
1554 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1557 * Find out how many monsters exist in the range we have selected.
1559 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1561 rndmonst_state.mchoices[mndx] = 0;
1562 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1564 if (upper && !isupper((uchar) def_monsyms[(int) ptr->mlet].sym))
1566 if (elemlevel && wrong_elem_type(ptr))
1570 if (Inhell && (ptr->geno & G_NOHELL))
1572 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1573 if (ct < 0 || ct > 127)
1574 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1575 rndmonst_state.choice_count += ct;
1576 rndmonst_state.mchoices[mndx] = (char) ct;
1579 * Possible modification: if choice_count is "too low",
1580 * expand minmlev..maxmlev range and try again.
1582 } /* choice_count+mchoices[] recalc */
1584 if (rndmonst_state.choice_count <= 0) {
1585 /* maybe no common mons left, or all are too weak or too strong */
1586 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1587 return (struct permonst *) 0;
1591 * Now, select a monster at random.
1593 ct = rnd(rndmonst_state.choice_count);
1594 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1595 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1598 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1599 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1600 return (struct permonst *) 0;
1605 /* called when you change level (experience or dungeon depth) or when
1606 monster species can no longer be created (genocide or extinction) */
1608 reset_rndmonst(mndx)
1609 int mndx; /* particular species that can no longer be created */
1611 /* cached selection info is out of date */
1612 if (mndx == NON_PM) {
1613 rndmonst_state.choice_count = -1; /* full recalc needed */
1614 } else if (mndx < SPECIAL_PM) {
1615 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1616 rndmonst_state.mchoices[mndx] = 0;
1617 } /* note: safe to ignore extinction of unique monsters */
1620 /* decide whether it's ok to generate a candidate monster by mkclass() */
1622 mk_gen_ok(mndx, mvflagsmask, genomask)
1623 int mndx, mvflagsmask, genomask;
1625 struct permonst *ptr = &mons[mndx];
1627 if (mvitals[mndx].mvflags & mvflagsmask)
1629 if (ptr->geno & genomask)
1631 if (is_placeholder(ptr))
1634 /* special levels might ask for random demon type; reject this one */
1635 if (ptr == &mons[PM_MAIL_DAEMON])
1641 /* Make one of the multiple types of a given monster class.
1642 The second parameter specifies a special casing bit mask
1643 to allow the normal genesis masks to be deactivated.
1644 Returns Null if no monsters in that class can be made. */
1650 return mkclass_aligned(class, spc, A_NONE);
1653 /* mkclass() with alignment restrictions; used by ndemon() */
1655 mkclass_aligned(class, spc, atyp)
1660 register int first, last, num = 0;
1661 int k, nums[SPECIAL_PM + 1]; /* +1: insurance for final return value */
1662 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1664 (void) memset((genericptr_t) nums, 0, sizeof nums);
1665 maxmlev = level_difficulty() >> 1;
1666 if (class < 1 || class >= MAXMCLASSES) {
1667 impossible("mkclass called with bad class!");
1668 return (struct permonst *) 0;
1670 /* Assumption #1: monsters of a given class are contiguous in the
1671 * mons[] array. Player monsters and quest denizens
1672 * are an exception; mkclass() won't pick them.
1673 * SPECIAL_PM is long worm tail and separates the
1674 * regular monsters from the exceptions.
1676 for (first = LOW_PM; first < SPECIAL_PM; first++)
1677 if (mons[first].mlet == class)
1679 if (first == SPECIAL_PM) {
1680 impossible("mkclass found no class %d monsters", class);
1681 return (struct permonst *) 0;
1684 /* Assumption #2: monsters of a given class are presented in ascending
1685 * order of strength.
1687 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++) {
1688 if (atyp != A_NONE && sgn(mons[last].maligntyp) != sgn(atyp))
1690 if (mk_gen_ok(last, G_GONE, mask)) {
1691 /* consider it; don't reject a toostrong() monster if we
1692 don't have anything yet (num==0) or if it is the same
1693 (or lower) difficulty as preceding candidate (non-zero
1694 'num' implies last > first so mons[last-1] is safe);
1695 sometimes accept it even if high difficulty */
1696 if (num && toostrong(last, maxmlev)
1697 && mons[last].difficulty > mons[last - 1].difficulty
1700 if ((k = (mons[last].geno & G_FREQ)) > 0) {
1701 /* skew towards lower value monsters at lower exp. levels
1702 (this used to be done in the next loop, but that didn't
1703 work well when multiple species had the same level and
1704 were followed by one that was past the bias threshold;
1705 cited example was sucubus and incubus, where the bias
1706 against picking the next demon resulted in incubus
1707 being picked nearly twice as often as sucubus);
1708 we need the '+1' in case the entire set is too high
1709 level (really low level hero) */
1710 nums[last] = k + 1 - (adj_lev(&mons[last]) > (u.ulevel * 2));
1716 return (struct permonst *) 0;
1718 /* the hard work has already been done; 'num' should hit 0 before
1719 first reaches last (which is actually one past our last candidate) */
1720 for (num = rnd(num); first < last; first++)
1721 if ((num -= nums[first]) <= 0)
1724 return nums[first] ? &mons[first] : (struct permonst *) 0;
1727 /* like mkclass(), but excludes difficulty considerations; used when
1728 player with polycontrol picks a class instead of a specific type;
1729 genocided types are avoided but extinct ones are acceptable; we don't
1730 check polyok() here--caller accepts some choices !polyok() would reject */
1735 register int first, last, num = 0;
1737 for (first = LOW_PM; first < SPECIAL_PM; first++)
1738 if (mons[first].mlet == class)
1740 if (first == SPECIAL_PM)
1743 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1744 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1745 num += mons[last].geno & G_FREQ;
1749 for (num = rnd(num); num > 0; first++)
1750 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1751 num -= mons[first].geno & G_FREQ;
1752 first--; /* correct an off-by-one error */
1757 /* adjust strength of monsters based on u.uz and u.ulevel */
1760 register struct permonst *ptr;
1764 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1765 /* does not depend on other strengths, but does get stronger
1766 * every time he is killed
1768 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1774 if ((tmp = ptr->mlevel) > 49)
1775 return 50; /* "special" demons/devils */
1776 tmp2 = (level_difficulty() - tmp);
1778 tmp--; /* if mlevel > u.uz decrement tmp */
1780 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1782 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1784 tmp += (tmp2 / 4); /* level as well */
1786 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1788 tmp2 = 49; /* hard upper limit */
1789 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1792 /* monster earned experience and will gain some hit points; it might also
1793 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1795 grow_up(mtmp, victim)
1796 struct monst *mtmp, *victim;
1798 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1800 struct permonst *ptr = mtmp->data;
1802 /* monster died after killing enemy but before calling this function */
1803 /* currently possible if killing a gas spore */
1804 if (DEADMONSTER(mtmp))
1805 return (struct permonst *) 0;
1807 /* note: none of the monsters with special hit point calculations
1808 have both little and big forms */
1809 oldtype = monsndx(ptr);
1810 newtype = little_to_big(oldtype);
1811 if (newtype == PM_PRIEST && mtmp->female)
1812 newtype = PM_PRIESTESS;
1814 /* growth limits differ depending on method of advancement */
1815 if (victim) { /* killed a monster */
1817 * The HP threshold is the maximum number of hit points for the
1818 * current level; once exceeded, a level will be gained.
1819 * Possible bug: if somehow the hit points are already higher
1820 * than that, monster will gain a level without any increase in HP.
1822 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1825 else if (is_golem(ptr)) /* strange creatures */
1826 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1827 else if (is_home_elemental(ptr))
1829 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1830 /* If they can grow up, be sure the level is high enough for that */
1831 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1832 lev_limit = (int) mons[newtype].mlevel;
1833 /* number of hit points to gain; unlike for the player, we put
1834 the limit at the bottom of the next level rather than the top */
1835 max_increase = rnd((int) victim->m_lev + 1);
1836 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1837 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1838 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1840 /* a gain level potion or wraith corpse; always go up a level
1841 unless already at maximum (49 is hard upper limit except
1842 for demon lords, who start at 50 and can't go any higher) */
1843 max_increase = cur_increase = rnd(8);
1844 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1845 lev_limit = 50; /* recalc below */
1848 mtmp->mhpmax += max_increase;
1849 mtmp->mhp += cur_increase;
1850 if (mtmp->mhpmax <= hp_threshold)
1851 return ptr; /* doesn't gain a level */
1853 if (is_mplayer(ptr))
1854 lev_limit = 30; /* same as player */
1855 else if (lev_limit < 5)
1856 lev_limit = 5; /* arbitrary */
1857 else if (lev_limit > 49)
1858 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1860 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1861 ptr = &mons[newtype];
1862 /* new form might force gender change */
1863 fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female;
1865 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1866 if (canspotmon(mtmp))
1868 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1869 an(ptr->mname), mhe(mtmp),
1870 nonliving(ptr) ? "expires" : "dies");
1872 pline("%s
\82ª
\90¬
\92·
\82µ
\82Ä%s
\82É
\82È
\82é
\82Æ%s
\82µ
\82Ü
\82Á
\82½
\81I", mon_nam(mtmp),
1874 nonliving(ptr) ? "
\8fÁ
\82¦
\82Ä" : "
\8e\80\82ñ
\82Å");
1876 set_mon_data(mtmp, ptr); /* keep mvitals[] accurate */
1878 return (struct permonst *) 0;
1879 } else if (canspotmon(mtmp)) {
1884 * Temporary (?) hack to fix growing into opposite gender.
1886 Sprintf(buf, "%s%s",
1887 /* deal with female gnome becoming a gnome lord */
1888 (mtmp->female && !fem) ? "male "
1889 /* or a male gnome becoming a gnome lady
1890 (can't happen with 3.6.0 mons[], but perhaps
1891 slightly less sexist if prepared for it...) */
1892 : (fem && !mtmp->female) ? "female " : "",
1894 pline("%s %s %s.", upstart(y_monnam(mtmp)),
1895 (fem != mtmp->female) ? "changes into"
1896 : humanoid(ptr) ? "becomes"
1899 #else /*
\93ú
\96{
\8cê
\82Å
\82Í
\82Æ
\82è
\82 \82¦
\82¸
\82»
\82±
\82Ü
\82Å
\82Í
\82µ
\82È
\82¢ */
1900 pline("%s
\82Í%s
\82É%s
\81D", Monnam(mtmp),
1902 humanoid(ptr) ? "
\82È
\82Á
\82½" : "
\90¬
\92·
\82µ
\82½");
1905 set_mon_data(mtmp, ptr);
1906 newsym(mtmp->mx, mtmp->my); /* color may change */
1907 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1909 mtmp->female = fem; /* gender might be changing */
1913 if ((int) mtmp->m_lev > lev_limit) {
1914 mtmp->m_lev--; /* undo increment */
1915 /* HP might have been allowed to grow when it shouldn't */
1916 if (mtmp->mhpmax == hp_threshold + 1)
1919 if (mtmp->mhpmax > 50 * 8)
1920 mtmp->mhpmax = 50 * 8; /* absolute limit */
1921 if (mtmp->mhp > mtmp->mhpmax)
1922 mtmp->mhp = mtmp->mhpmax;
1929 register struct monst *mtmp;
1932 register struct obj *otmp;
1937 otmp = mksobj(otyp, TRUE, FALSE);
1939 if (mtmp->data->mlet == S_DEMON) {
1940 /* demons never get blessed objects */
1943 } else if (is_lminion(mtmp)) {
1944 /* lawful minions don't get cursed, bad, or rusting objects */
1945 otmp->cursed = FALSE;
1948 otmp->oerodeproof = TRUE;
1949 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1950 otmp->spe = (3 + rn2(4));
1953 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1956 otmp->lamplit = FALSE;
1957 otmp->blessed = otmp->cursed = FALSE;
1958 } else if (otmp->otyp == BELL_OF_OPENING) {
1959 otmp->blessed = otmp->cursed = FALSE;
1960 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1961 otmp->blessed = FALSE;
1962 otmp->cursed = TRUE;
1965 /* leaders don't tolerate inferior quality battle gear */
1966 if (is_prince(mtmp->data)) {
1967 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1969 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1974 (void) mpickobj(mtmp, otmp); /* might free otmp */
1985 case PM_STRAW_GOLEM:
1987 case PM_PAPER_GOLEM:
1991 case PM_LEATHER_GOLEM:
1997 case PM_FLESH_GOLEM:
2001 case PM_STONE_GOLEM:
2003 case PM_GLASS_GOLEM:
2013 * Alignment vs. yours determines monster's attitude to you.
2014 * (Some "animal" types are co-aligned, but also hungry.)
2018 register struct permonst *ptr;
2020 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
2022 if (always_peaceful(ptr))
2024 if (always_hostile(ptr))
2026 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
2028 if (ptr->msound == MS_NEMESIS)
2031 if (race_peaceful(ptr))
2033 if (race_hostile(ptr))
2036 /* the monster is hostile if its alignment is different from the
2038 if (sgn(mal) != sgn(ual))
2041 /* Negative monster hostile to player with Amulet. */
2042 if (mal < A_NEUTRAL && u.uhave.amulet)
2045 /* minions are hostile to players that have strayed at all */
2047 return (boolean) (u.ualign.record >= 0);
2049 /* Last case: a chance of a co-aligned monster being
2050 * hostile. This chance is greater if the player has strayed
2051 * (u.ualign.record negative) or the monster is not strongly aligned.
2053 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
2055 && !!rn2(2 + abs(mal)));
2058 /* Set malign to have the proper effect on player alignment if monster is
2059 * killed. Negative numbers mean it's bad to kill this monster; positive
2060 * numbers mean it's good. Since there are more hostile monsters than
2061 * peaceful monsters, the penalty for killing a peaceful monster should be
2062 * greater than the bonus for killing a hostile monster to maintain balance.
2064 * it's bad to kill peaceful monsters, potentially worse to kill always-
2065 * peaceful monsters;
2066 * it's never bad to kill a hostile monster, although it may not be good.
2072 schar mal = mtmp->data->maligntyp;
2075 if (mtmp->ispriest || mtmp->isminion) {
2076 /* some monsters have individual alignments; check them */
2077 if (mtmp->ispriest && EPRI(mtmp))
2078 mal = EPRI(mtmp)->shralign;
2079 else if (mtmp->isminion && EMIN(mtmp))
2080 mal = EMIN(mtmp)->min_align;
2081 /* unless alignment is none, set mal to -5,0,5 */
2082 /* (see align.h for valid aligntyp values) */
2087 coaligned = (sgn(mal) == sgn(u.ualign.type));
2088 if (mtmp->data->msound == MS_LEADER) {
2090 } else if (mal == A_NONE) {
2091 if (mtmp->mpeaceful)
2094 mtmp->malign = 20; /* really hostile */
2095 } else if (always_peaceful(mtmp->data)) {
2096 int absmal = abs(mal);
2097 if (mtmp->mpeaceful)
2098 mtmp->malign = -3 * max(5, absmal);
2100 mtmp->malign = 3 * max(5, absmal); /* renegade */
2101 } else if (always_hostile(mtmp->data)) {
2102 int absmal = abs(mal);
2106 mtmp->malign = max(5, absmal);
2107 } else if (coaligned) {
2108 int absmal = abs(mal);
2109 if (mtmp->mpeaceful)
2110 mtmp->malign = -3 * max(3, absmal);
2112 mtmp->malign = max(3, absmal);
2113 } else /* not coaligned and therefore hostile */
2114 mtmp->malign = abs(mal);
2117 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
2123 mtmp->mextra = newmextra();
2124 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
2127 /* release monster's mcorpsenm field; basically a no-op */
2132 if (has_mcorpsenm(mtmp))
2133 MCORPSENM(mtmp) = NON_PM;
2136 static NEARDATA char syms[] = {
2137 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
2138 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
2139 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
2140 S_MIMIC_DEF, S_MIMIC_DEF,
2145 register struct monst *mtmp;
2147 int typ, roomno, rt;
2148 unsigned appear, ap_type;
2153 if (!mtmp || Protection_from_shape_changers)
2157 typ = levl[mx][my].typ;
2158 /* only valid for INSIDE of room */
2159 roomno = levl[mx][my].roomno - ROOMOFFSET;
2161 rt = rooms[roomno].rtype;
2162 #ifdef SPECIALIZATION
2163 else if (IS_ROOM(typ))
2164 rt = OROOM, roomno = 0;
2167 rt = 0; /* roomno < 0 case for GCC_WARN */
2169 if (OBJ_AT(mx, my)) {
2170 ap_type = M_AP_OBJECT;
2171 appear = level.objects[mx][my]->otyp;
2172 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2173 ap_type = M_AP_FURNITURE;
2175 * If there is a wall to the left that connects to this
2176 * location, then the mimic mimics a horizontal closed door.
2177 * This does not allow doors to be in corners of rooms.
2178 * Since rogue has no closed doors, mimic a wall there
2179 * (yes, mimics can end up on this level by various means).
2181 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2182 || levl[mx - 1][my].typ == TLCORNER
2183 || levl[mx - 1][my].typ == TRWALL
2184 || levl[mx - 1][my].typ == BLCORNER
2185 || levl[mx - 1][my].typ == TDWALL
2186 || levl[mx - 1][my].typ == CROSSWALL
2187 || levl[mx - 1][my].typ == TUWALL))
2188 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2190 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2191 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2192 ap_type = M_AP_OBJECT;
2194 } else if (roomno < 0 && !t_at(mx, my)) {
2195 ap_type = M_AP_OBJECT;
2197 } else if (rt == ZOO || rt == VAULT) {
2198 ap_type = M_AP_OBJECT;
2199 appear = GOLD_PIECE;
2200 } else if (rt == DELPHI) {
2202 ap_type = M_AP_OBJECT;
2205 ap_type = M_AP_FURNITURE;
2206 appear = S_fountain;
2208 } else if (rt == TEMPLE) {
2209 ap_type = M_AP_FURNITURE;
2212 * We won't bother with beehives, morgues, barracks, throne rooms
2213 * since they shouldn't contain too many mimics anyway...
2215 } else if (rt >= SHOPBASE) {
2216 s_sym = get_shop_item(rt - SHOPBASE);
2218 ap_type = M_AP_OBJECT;
2221 if (s_sym == RANDOM_CLASS)
2222 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2226 s_sym = syms[rn2((int) sizeof(syms))];
2228 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2229 ap_type = M_AP_FURNITURE;
2230 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2232 ap_type = M_AP_OBJECT;
2233 if (s_sym == S_MIMIC_DEF) {
2234 appear = STRANGE_OBJECT;
2235 } else if (s_sym == COIN_CLASS) {
2236 appear = GOLD_PIECE;
2238 otmp = mkobj((char) s_sym, FALSE);
2239 appear = otmp->otyp;
2240 /* make sure container contents are free'ed */
2241 obfree(otmp, (struct obj *) 0);
2245 mtmp->m_ap_type = ap_type;
2246 mtmp->mappearance = appear;
2247 /* when appearing as an object based on a monster type, pick a shape */
2248 if (ap_type == M_AP_OBJECT
2249 && (appear == STATUE || appear == FIGURINE
2250 || appear == CORPSE || appear == EGG || appear == TIN)) {
2251 int mndx = rndmonnum(),
2252 nocorpse_ndx = (mvitals[mndx].mvflags & G_NOCORPSE) != 0;
2254 if (appear == CORPSE && nocorpse_ndx)
2255 mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
2256 else if ((appear == EGG && !can_be_hatched(mndx))
2257 || (appear == TIN && nocorpse_ndx))
2258 mndx = NON_PM; /* revert to generic egg or empty tin */
2261 MCORPSENM(mtmp) = mndx;
2264 if (does_block(mx, my, &levl[mx][my]))
2265 block_point(mx, my);
2268 /* release monster from bag of tricks; return number of monsters created */
2270 bagotricks(bag, tipping, seencount)
2272 boolean tipping; /* caller emptying entire contents; affects shop handling */
2273 int *seencount; /* secondary output */
2277 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2278 impossible("bad bag o' tricks");
2279 } else if (bag->spe < 1) {
2280 /* if tipping known empty bag, give normal empty container message */
2282 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2284 pline1((tipping && bag->cknown) ? "
\82»
\82ê
\82Í
\8bó
\82¾
\81D" : nothing_happens);
2285 /* now known to be empty if sufficiently discovered */
2286 if (bag->dknown && objects[bag->otyp].oc_name_known)
2290 int creatcnt = 1, seecount = 0;
2292 consume_obj_charge(bag, !tipping);
2297 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2300 if (canspotmon(mtmp))
2303 } while (--creatcnt > 0);
2306 *seencount += seecount;
2308 makeknown(BAG_OF_TRICKS);
2309 } else if (!tipping) {
2311 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2313 pline1(!moncount ? nothing_happens : "
\89½
\82à
\8bN
\82«
\82È
\82©
\82Á
\82½
\82æ
\82¤
\82¾
\81D");