1 /* NetHack 3.6 makemon.c $NHDT-Date: 1495237801 2017/05/19 23:50:01 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.116 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Robert Patrick Rankin, 2012. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* JNetHack Copyright */
7 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
8 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2018 */
9 /* JNetHack may be freely redistributed. See license for details. */
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));
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 extern const int monstr[];
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)
41 is_home_elemental(ptr)
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);
59 * Return true if the given monster cannot exist on this elemental level.
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 */
73 } else if (Is_firelevel(&u.uz)) {
74 if (!pm_resistance(ptr, MR_FIRE))
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))
84 /* make a group just like mtmp */
86 m_initgrp(mtmp, x, y, n)
87 register struct monst *mtmp;
91 register int cnt = rnd(n);
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
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);
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);
121 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
131 if (peace_minded(mtmp->data))
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
138 if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
139 mon = makemon(mtmp->data, mm.x, mm.y, NO_MM_FLAGS);
141 mon->mpeaceful = FALSE;
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.
155 m_initthrow(mtmp, otyp, oquan)
159 register struct obj *otmp;
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);
171 register struct monst *mtmp;
173 register struct permonst *ptr = mtmp->data;
174 register int mm = monsndx(ptr);
176 int bias, spe2, w1, w2;
178 if (Is_rogue_level(&u.uz))
181 * First a few special cases:
182 * giants get a boulder to throw sometimes
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.
192 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
195 if (is_mercenary(ptr)) {
201 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
202 w2 = rn2(2) ? DAGGER : KNIFE;
204 w1 = rn2(2) ? SPEAR : SHORT_SWORD;
207 w1 = rn2(2) ? FLAIL : MACE;
210 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
213 case PM_WATCH_CAPTAIN:
214 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
224 (void) mongets(mtmp, w1);
225 if (!w2 && w1 != DAGGER && !rn2(4))
228 (void) mongets(mtmp, w2);
229 } else if (is_elf(ptr)) {
232 rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
234 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
236 (void) mongets(mtmp, ELVEN_BOOTS);
238 (void) mongets(mtmp, ELVEN_DAGGER);
242 (void) mongets(mtmp, ELVEN_SHIELD);
244 (void) mongets(mtmp, ELVEN_SHORT_SWORD);
245 (void) mongets(mtmp, ELVEN_BOW);
246 m_initthrow(mtmp, ELVEN_ARROW, 12);
249 (void) mongets(mtmp, ELVEN_BROADSWORD);
251 (void) mongets(mtmp, ELVEN_SHIELD);
255 (void) mongets(mtmp, ELVEN_SPEAR);
256 (void) mongets(mtmp, ELVEN_SHIELD);
260 if (mm == PM_ELVENKING) {
261 if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
262 (void) mongets(mtmp, PICK_AXE);
264 (void) mongets(mtmp, CRYSTAL_BALL);
266 } else if (ptr->msound == MS_PRIEST
267 || quest_mon_represents_role(ptr, PM_PRIEST)) {
268 otmp = mksobj(MACE, FALSE, FALSE);
273 (void) mpickobj(mtmp, otmp);
275 } else if (mm == PM_NINJA) { /* extra quest villains */
276 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
277 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
278 } else if (ptr->msound == MS_GUARDIAN) {
279 /* quest "guardians" */
288 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
290 (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET : LEATHER_CLOAK);
292 (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
294 (void) mongets(mtmp, POT_HEALING);
300 (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
301 (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
303 (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
305 (void) mongets(mtmp, LEATHER_CLOAK);
307 (void) mongets(mtmp, BOW);
308 m_initthrow(mtmp, ARROW, 12);
312 (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
314 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
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_GUARD] && ptr != &mons[PM_WATCHMAN]
662 && ptr != &mons[PM_WATCH_CAPTAIN]) {
664 (void) mongets(mtmp, K_RATION);
666 (void) mongets(mtmp, C_RATION);
667 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
668 (void) mongets(mtmp, BUGLE);
669 } else if (ptr == &mons[PM_WATCHMAN] && rn2(3))
670 (void) mongets(mtmp, TIN_WHISTLE);
671 } else if (ptr == &mons[PM_SHOPKEEPER]) {
672 (void) mongets(mtmp, SKELETON_KEY);
674 /* MAJOR fall through ... */
676 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
679 (void) mongets(mtmp, POT_EXTRA_HEALING);
682 (void) mongets(mtmp, POT_HEALING);
685 (void) mongets(mtmp, WAN_STRIKING);
687 } else if (ptr->msound == MS_PRIEST
688 || quest_mon_represents_role(ptr, PM_PRIEST)) {
689 (void) mongets(mtmp, rn2(7) ? ROBE
690 : rn2(3) ? CLOAK_OF_PROTECTION
691 : CLOAK_OF_MAGIC_RESISTANCE);
692 (void) mongets(mtmp, SMALL_SHIELD);
693 mkmonmoney(mtmp, (long) rn1(10, 20));
694 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
695 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
700 (void) mongets(mtmp, MIRROR);
702 (void) mongets(mtmp, POT_OBJECT_DETECTION);
705 if (ptr == &mons[PM_MINOTAUR]) {
706 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
707 (void) mongets(mtmp, WAN_DIGGING);
708 } else if (is_giant(ptr)) {
709 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
710 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
712 otmp->quan = (long) rn1(2, 3);
713 otmp->owt = weight(otmp);
714 (void) mpickobj(mtmp, otmp);
719 if (ptr == &mons[PM_NAZGUL]) {
720 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
722 (void) mpickobj(mtmp, otmp);
726 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
727 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
728 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
729 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
730 rn2(13) ? FALSE : TRUE);
734 otmp->oerodeproof = 1;
735 (void) mpickobj(mtmp, otmp);
740 (void) mongets(mtmp, MUMMY_WRAPPING);
744 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
745 otmp->spe = 1; /* flag for special box */
746 otmp->owt = weight(otmp);
747 (void) mpickobj(mtmp, otmp);
751 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
754 /* moved here from m_initweap() because these don't
755 have AT_WEAP so m_initweap() is not called for them */
756 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
757 (void) mongets(mtmp, SPEAR);
758 } else if (ptr == &mons[PM_ASMODEUS]) {
759 (void) mongets(mtmp, WAN_COLD);
760 (void) mongets(mtmp, WAN_FIRE);
764 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
765 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
767 otmp->owt = weight(otmp);
768 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
769 begin_burn(otmp, FALSE);
776 /* ordinary soldiers rarely have access to magic (or gold :-) */
777 if (ptr == &mons[PM_SOLDIER] && rn2(13))
780 if ((int) mtmp->m_lev > rn2(50))
781 (void) mongets(mtmp, rnd_defensive_item(mtmp));
782 if ((int) mtmp->m_lev > rn2(100))
783 (void) mongets(mtmp, rnd_misc_item(mtmp));
784 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
786 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
789 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
793 xchar x, y; /* clone's preferred location or 0 (near mon) */
798 /* may be too weak or have been extinguished for population control */
799 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
800 return (struct monst *) 0;
805 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
806 return (struct monst *) 0;
807 } else if (!isok(x, y)) {
808 return (struct monst *) 0; /* paranoia */
812 if (MON_AT(mm.x, mm.y)) {
813 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
814 return (struct monst *) 0;
818 *m2 = *mon; /* copy condition of old monster */
819 m2->mextra = (struct mextra *) 0;
822 m2->m_id = context.ident++;
824 m2->m_id = context.ident++; /* ident overflowed */
829 m2->minvent = (struct obj *) 0; /* objects don't clone */
830 m2->mleashed = FALSE;
831 /* Max HP the same, but current HP halved for both. The caller
832 * might want to override this by halving the max HP also.
833 * When current HP is odd, the original keeps the extra point.
834 * We know original has more than 1 HP, so both end up with at least 1.
836 m2->mhpmax = mon->mhpmax;
837 m2->mhp = mon->mhp / 2;
840 /* since shopkeepers and guards will only be cloned if they've been
841 * polymorphed away from their original forms, the clone doesn't have
842 * room for the extra information. we also don't want two shopkeepers
843 * around for the same shop.
850 m2->ispriest = FALSE;
851 place_monster(m2, m2->mx, m2->my);
852 if (emits_light(m2->data))
853 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
855 if (has_mname(mon)) {
856 m2 = christen_monst(m2, MNAME(mon));
857 } else if (mon->isshk) {
858 m2 = christen_monst(m2, shkname(mon));
861 /* not all clones caused by player are tame or peaceful */
862 if (!context.mon_moving) {
864 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
865 else if (mon->mpeaceful)
866 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
869 newsym(m2->mx, m2->my); /* display the new monster */
874 *(EMIN(m2)) = *(EMIN(mon));
876 /* because m2 is a copy of mon it is tame but not init'ed.
877 * however, tamedog will not re-tame a tame dog, so m2
878 * must be made non-tame to get initialized properly.
881 if (tamedog(m2, (struct obj *) 0)) {
882 *(EDOG(m2)) = *(EDOG(mon));
892 * Propagate a species
894 * Once a certain number of monsters are created, don't create any more
895 * at random (i.e. make them extinct). The previous (3.2) behavior was
896 * to do this when a certain number had _died_, which didn't make
899 * Returns FALSE propagation unsuccessful
900 * TRUE propagation successful
903 propagate(mndx, tally, ghostly)
909 uchar lim = mbirth_limit(mndx);
910 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
912 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
914 /* if it's unique, don't ever make it again */
915 if ((mons[mndx].geno & G_UNIQ) && mndx != PM_HIGH_PRIEST)
916 mvitals[mndx].mvflags |= G_EXTINCT;
918 if (mvitals[mndx].born < 255 && tally
919 && (!ghostly || (ghostly && result)))
920 mvitals[mndx].born++;
921 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
922 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
924 debugpline1("Automatically extinguished %s.",
925 makeplural(mons[mndx].mname));
927 mvitals[mndx].mvflags |= G_EXTINCT;
928 reset_rndmonst(mndx);
933 /* amount of HP to lose from level drain (or gain from Stormbringer) */
938 struct permonst *ptr = mon->data;
939 int hp = rnd(8); /* default is d8 */
941 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
943 /* draining usually won't be applicable for these critters */
944 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
945 } else if (ptr->mlevel > 49) {
946 /* arbitrary; such monsters won't be involved in draining anyway */
947 hp = 4 + rnd(4); /* 5..8 */
948 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
949 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
951 hp = 4 + rn2(5); /* 4..8 */
952 } else if (!mon->m_lev) {
953 /* level 0 monsters use 1d4 instead of Nd8 */
959 /* set up a new monster's initial level and hit points;
960 used by newcham() as well as by makemon() */
966 struct permonst *ptr = &mons[mndx];
968 mon->m_lev = adj_lev(ptr);
970 mon->mhpmax = mon->mhp = golemhp(mndx);
971 } else if (is_rider(ptr)) {
972 /* we want low HP, but a high mlevel so they can attack well */
973 mon->mhpmax = mon->mhp = d(10, 8);
974 } else if (ptr->mlevel > 49) {
975 /* "special" fixed hp monster
976 * the hit points are encoded in the mlevel in a somewhat strange
977 * way to fit in the 50..127 positive range of a signed character
978 * above the 1..49 that indicate "normal" monster levels */
979 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
980 mon->m_lev = mon->mhp / 4; /* approximation */
981 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
983 mon->mhpmax = mon->mhp =
984 (int) (In_endgame(&u.uz)
986 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
987 } else if (!mon->m_lev) {
988 mon->mhpmax = mon->mhp = rnd(4);
990 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
991 if (is_home_elemental(ptr))
992 mon->mhpmax = (mon->mhp *= 3);
999 struct mextra *mextra;
1001 mextra = (struct mextra *) alloc(sizeof(struct mextra));
1008 mextra->mcorpsenm = NON_PM;
1013 makemon_rnd_goodpos(mon, gpflags, cc)
1023 nx = rn1(COLNO - 3, 2);
1025 good = (!in_mklev && cansee(nx,ny)) ? FALSE
1026 : goodpos(nx, ny, mon, gpflags);
1027 } while ((++tryct < 50) && !good);
1030 /* else go through all map positions, twice, first round
1031 ignoring positions in sight, and pick first good one.
1032 skip first round if we're in special level loader or blind */
1036 int bl = (in_mklev || Blind) ? 1 : 0;
1038 for ( ; bl < 2; bl++) {
1039 for (dx = 0; dx < COLNO; dx++)
1040 for (dy = 0; dy < ROWNO; dy++) {
1041 nx = ((dx + xofs) % (COLNO - 1)) + 1;
1042 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
1043 if (bl == 0 && cansee(nx,ny))
1045 if (goodpos(nx, ny, mon, gpflags))
1048 if (bl == 0 && (!mon || mon->data->mmove)) {
1049 /* all map positions are visible (or not good),
1050 try to pick something logical */
1051 if (dnstair.sx && !rn2(2)) {
1054 } else if (upstair.sx && !rn2(2)) {
1057 } else if (dnladder.sx && !rn2(2)) {
1060 } else if (upladder.sx && !rn2(2)) {
1064 if (goodpos(nx, ny, mon, gpflags))
1078 * called with [x,y] = coordinates;
1079 * [0,0] means anyplace
1080 * [u.ux,u.uy] means: near player (if !in_mklev)
1082 * In case we make a monster group, only return the one at [x,y].
1085 makemon(ptr, x, y, mmflags)
1086 register struct permonst *ptr;
1090 register struct monst *mtmp;
1091 int mndx, mcham, ct, mitem;
1092 boolean anymon = (!ptr);
1093 boolean byyou = (x == u.ux && y == u.uy);
1094 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1095 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1096 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1098 /* if caller wants random location, do it here */
1099 if (x == 0 && y == 0) {
1101 struct monst fakemon;
1103 cc.x = cc.y = 0; /* lint suppression */
1104 fakemon.data = ptr; /* set up for goodpos */
1105 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1107 return (struct monst *) 0;
1110 } else if (byyou && !in_mklev) {
1113 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1117 return (struct monst *) 0;
1120 /* Does monster already exist at the position? */
1122 if ((mmflags & MM_ADJACENTOK) != 0) {
1124 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1128 return (struct monst *) 0;
1130 return (struct monst *) 0;
1134 mndx = monsndx(ptr);
1135 /* if you are to make a specific monster and it has
1136 already been genocided, return */
1137 if (mvitals[mndx].mvflags & G_GENOD)
1138 return (struct monst *) 0;
1139 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1140 debugpline1("Explicitly creating extinct monster %s.",
1144 /* make a random (common) monster that can survive here.
1145 * (the special levels ask for random monsters at specific
1146 * positions, causing mass drowning on the medusa level,
1149 int tryct = 0; /* maybe there are no good choices */
1150 struct monst fakemon;
1153 if (!(ptr = rndmonst())) {
1154 debugpline0("Warning: no monster.");
1155 return (struct monst *) 0; /* no more monsters! */
1157 fakemon.data = ptr; /* set up for goodpos */
1158 } while (++tryct <= 50
1159 /* in Sokoban, don't accept a giant on first try;
1160 after that, boulder carriers are fair game */
1161 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1162 || !goodpos(x, y, &fakemon, gpflags)));
1163 mndx = monsndx(ptr);
1165 (void) propagate(mndx, countbirth, FALSE);
1167 *mtmp = zeromonst; /* clear all entries in structure */
1169 if (mmflags & MM_EGD)
1171 if (mmflags & MM_EPRI)
1173 if (mmflags & MM_ESHK)
1175 if (mmflags & MM_EMIN)
1177 if (mmflags & MM_EDOG)
1182 mtmp->m_id = context.ident++;
1184 mtmp->m_id = context.ident++; /* ident overflowed */
1185 set_mon_data(mtmp, ptr, 0);
1186 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1187 quest_status.leader_m_id = mtmp->m_id;
1190 /* set up level and hit points */
1191 newmonhp(mtmp, mndx);
1194 mtmp->female = TRUE;
1195 else if (is_male(ptr))
1196 mtmp->female = FALSE;
1197 /* leader and nemesis gender is usually hardcoded in mons[],
1198 but for ones which can be random, it has already been chosen
1199 (in role_init(), for possible use by the quest pager code) */
1200 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1201 mtmp->female = quest_status.ldrgend;
1202 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1203 mtmp->female = quest_status.nemgend;
1205 mtmp->female = rn2(2); /* ignored for neuters */
1207 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1208 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1209 /* quest leader and nemesis both know about all trap types */
1210 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1211 mtmp->mtrapseen = ~0;
1213 place_monster(mtmp, x, y);
1214 mtmp->mcansee = mtmp->mcanmove = TRUE;
1215 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1217 switch (ptr->mlet) {
1219 set_mimic_sym(mtmp);
1225 (void) mkobj_at(0, x, y, TRUE);
1226 (void) hideunder(mtmp);
1230 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1231 mtmp->perminvis = TRUE;
1232 mtmp->minvis = TRUE;
1236 (void) hideunder(mtmp);
1239 mtmp->msleeping = 1;
1243 if (rn2(5) && !u.uhave.amulet)
1244 mtmp->msleeping = 1;
1247 if (Race_if(PM_ELF))
1248 mtmp->mpeaceful = FALSE;
1251 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1252 mtmp->mpeaceful = TRUE;
1255 if (Inhell && is_bat(ptr))
1256 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1259 if ((ct = emits_light(mtmp->data)) > 0)
1260 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1261 monst_to_any(mtmp));
1262 mitem = 0; /* extra inventory item for this monster */
1264 if (mndx == PM_VLAD_THE_IMPALER)
1265 mitem = CANDELABRUM_OF_INVOCATION;
1266 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1267 if ((mcham = pm_to_cham(mndx)) != NON_PM) {
1268 /* this is a shapechanger after all */
1269 if (Protection_from_shape_changers
1270 || mndx == PM_VLAD_THE_IMPALER) {
1271 ; /* stuck in its natural form (NON_PM) */
1274 /* Note: shapechanger's initial form used to be
1275 chosen here with rndmonst(), yielding a monster
1276 which was appropriate to the level's difficulty
1277 but ignored the changer's usual type selection
1278 so would be inappropriate for vampshifters.
1279 Let newcham() pick the shape. */
1280 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1281 allow_minvent = FALSE;
1283 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1285 context.no_of_wizards++;
1286 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1288 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1289 mtmp = christen_monst(mtmp, rndghostname());
1290 } else if (mndx == PM_CROESUS) {
1291 mitem = TWO_HANDED_SWORD;
1292 } else if (ptr->msound == MS_NEMESIS) {
1293 mitem = BELL_OF_OPENING;
1294 } else if (mndx == PM_PESTILENCE) {
1295 mitem = POT_SICKNESS;
1297 if (mitem && allow_minvent)
1298 (void) mongets(mtmp, mitem);
1301 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1302 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1303 && !u.uhave.amulet && rn2(5))
1304 mtmp->msleeping = TRUE;
1307 newsym(mtmp->mx, mtmp->my);
1311 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1312 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1314 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1315 mtmp->mpeaceful = mtmp->mtame = FALSE;
1318 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1320 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1322 if (mndx == PM_LONG_WORM
1323 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1326 /* we can now create worms with tails - 11/91 */
1327 initworm(mtmp, rn2(5));
1328 if (count_wsegs(mtmp))
1329 place_worm_tail_randomly(mtmp, x, y);
1331 /* it's possible to create an ordinary monster of some special
1332 types; make sure their extended data is initialized to
1333 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1334 (when they're specified, caller intends to handle this itself) */
1335 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1336 ? !(mmflags & (MM_EPRI | MM_EMIN))
1337 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1342 mtmp->isminion = 1; /* make priest be a roamer */
1343 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1344 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1345 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1349 set_malign(mtmp); /* having finished peaceful changes */
1351 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1352 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1353 } else if (ptr->geno & G_LGROUP) {
1355 m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1357 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1361 if (allow_minvent) {
1363 m_initweap(mtmp); /* equip with weapons / armor */
1364 m_initinv(mtmp); /* add on a few special items incl. more armor */
1365 m_dowear(mtmp, TRUE);
1367 if (!rn2(100) && is_domestic(ptr)
1368 && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) {
1369 struct obj *otmp = mksobj(SADDLE, TRUE, FALSE);
1370 put_saddle_on_mon(otmp, mtmp);
1374 /* no initial inventory is allowed */
1376 discard_minvent(mtmp);
1377 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1379 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1380 if (ptr->mflags3 & M3_WAITFORU)
1381 mtmp->mstrategy |= STRAT_WAITFORU;
1382 if (ptr->mflags3 & M3_CLOSE)
1383 mtmp->mstrategy |= STRAT_CLOSE;
1384 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1385 mtmp->mstrategy |= STRAT_APPEARMSG;
1389 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1398 /* There is an implicit limit of 4 for "high priest of <deity>",
1399 * but aligned priests can grow into high priests, thus they aren't
1400 * really limited to 4, so leave the default amount in place for them.
1403 /* assert(MAXMONNO < 255); */
1404 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1407 /* used for wand/scroll/spell of create monster */
1408 /* returns TRUE iff you know monsters have been created */
1410 create_critters(cnt, mptr, neverask)
1412 struct permonst *mptr; /* usually null; used for confused reading */
1418 boolean known = FALSE;
1419 boolean ask = (wizard && !neverask);
1423 if (create_particular()) {
1427 ask = FALSE; /* ESC will shut off prompting */
1430 /* if in water, try to encourage an aquatic monster
1431 by finding and then specifying another wet location */
1432 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1435 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1436 if (mon && canspotmon(mon))
1446 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1448 if (mvitals[mndx].mvflags & G_GONE)
1451 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1453 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1457 * shift the probability of a monster's generation by
1458 * comparing the dungeon alignment and monster alignment.
1459 * return an integer in the range of 0-5.
1463 register struct permonst *ptr;
1465 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1466 static NEARDATA s_level *lev;
1467 register int alshift;
1469 if (oldmoves != moves) {
1470 lev = Is_special(&u.uz);
1473 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1474 default: /* just in case */
1479 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1482 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1485 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1491 static NEARDATA struct {
1493 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1494 } rndmonst_state = { -1, { 0 } };
1496 /* select a random monster type */
1500 register struct permonst *ptr;
1501 register int mndx, ct;
1503 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1506 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1507 int zlevel, minmlev, maxmlev;
1511 rndmonst_state.choice_count = 0;
1512 /* look for first common monster */
1513 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1514 if (!uncommon(mndx))
1516 rndmonst_state.mchoices[mndx] = 0;
1518 if (mndx == SPECIAL_PM) {
1519 /* evidently they've all been exterminated */
1520 debugpline0("rndmonst: no common mons!");
1521 return (struct permonst *) 0;
1522 } /* else `mndx' now ready for use below */
1523 zlevel = level_difficulty();
1524 /* determine the level of the weakest monster to make. */
1525 minmlev = zlevel / 6;
1526 /* determine the level of the strongest monster to make. */
1527 maxmlev = (zlevel + u.ulevel) / 2;
1528 upper = Is_rogue_level(&u.uz);
1529 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1532 * Find out how many monsters exist in the range we have selected.
1534 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1536 rndmonst_state.mchoices[mndx] = 0;
1537 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1539 if (upper && !isupper((uchar) def_monsyms[(int) ptr->mlet].sym))
1541 if (elemlevel && wrong_elem_type(ptr))
1545 if (Inhell && (ptr->geno & G_NOHELL))
1547 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1548 if (ct < 0 || ct > 127)
1549 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1550 rndmonst_state.choice_count += ct;
1551 rndmonst_state.mchoices[mndx] = (char) ct;
1554 * Possible modification: if choice_count is "too low",
1555 * expand minmlev..maxmlev range and try again.
1557 } /* choice_count+mchoices[] recalc */
1559 if (rndmonst_state.choice_count <= 0) {
1560 /* maybe no common mons left, or all are too weak or too strong */
1561 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1562 return (struct permonst *) 0;
1566 * Now, select a monster at random.
1568 ct = rnd(rndmonst_state.choice_count);
1569 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1570 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1573 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1574 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1575 return (struct permonst *) 0;
1580 /* called when you change level (experience or dungeon depth) or when
1581 monster species can no longer be created (genocide or extinction) */
1583 reset_rndmonst(mndx)
1584 int mndx; /* particular species that can no longer be created */
1586 /* cached selection info is out of date */
1587 if (mndx == NON_PM) {
1588 rndmonst_state.choice_count = -1; /* full recalc needed */
1589 } else if (mndx < SPECIAL_PM) {
1590 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1591 rndmonst_state.mchoices[mndx] = 0;
1592 } /* note: safe to ignore extinction of unique monsters */
1595 /* decide whether it's ok to generate a candidate monster by mkclass() */
1597 mk_gen_ok(mndx, mvflagsmask, genomask)
1598 int mndx, mvflagsmask, genomask;
1600 struct permonst *ptr = &mons[mndx];
1602 if (mvitals[mndx].mvflags & mvflagsmask)
1604 if (ptr->geno & genomask)
1606 if (is_placeholder(ptr))
1609 /* special levels might ask for random demon type; reject this one */
1610 if (ptr == &mons[PM_MAIL_DAEMON])
1616 /* Make one of the multiple types of a given monster class.
1617 * The second parameter specifies a special casing bit mask
1618 * to allow the normal genesis masks to be deactivated.
1619 * Returns Null if no monsters in that class can be made.
1626 register int first, last, num = 0;
1627 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1629 maxmlev = level_difficulty() >> 1;
1630 if (class < 1 || class >= MAXMCLASSES) {
1631 impossible("mkclass called with bad class!");
1632 return (struct permonst *) 0;
1634 /* Assumption #1: monsters of a given class are contiguous in the
1637 for (first = LOW_PM; first < SPECIAL_PM; first++)
1638 if (mons[first].mlet == class)
1640 if (first == SPECIAL_PM)
1641 return (struct permonst *) 0;
1643 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1644 if (mk_gen_ok(last, G_GONE, mask)) {
1646 if (num && toostrong(last, maxmlev)
1647 && monstr[last] != monstr[last - 1] && rn2(2))
1649 num += mons[last].geno & G_FREQ;
1652 return (struct permonst *) 0;
1654 /* Assumption #2: monsters of a given class are presented in ascending
1655 * order of strength.
1657 for (num = rnd(num); num > 0; first++)
1658 if (mk_gen_ok(first, G_GONE, mask)) {
1659 /* skew towards lower value monsters at lower exp. levels */
1660 num -= mons[first].geno & G_FREQ;
1661 if (num && adj_lev(&mons[first]) > (u.ulevel * 2)) {
1662 /* but not when multiple monsters are same level */
1663 if (mons[first].mlevel != mons[first + 1].mlevel)
1667 first--; /* correct an off-by-one error */
1669 return &mons[first];
1672 /* like mkclass(), but excludes difficulty considerations; used when
1673 player with polycontrol picks a class instead of a specific type;
1674 genocided types are avoided but extinct ones are acceptable; we don't
1675 check polyok() here--caller accepts some choices !polyok() would reject */
1680 register int first, last, num = 0;
1682 for (first = LOW_PM; first < SPECIAL_PM; first++)
1683 if (mons[first].mlet == class)
1685 if (first == SPECIAL_PM)
1688 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1689 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1690 num += mons[last].geno & G_FREQ;
1694 for (num = rnd(num); num > 0; first++)
1695 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1696 num -= mons[first].geno & G_FREQ;
1697 first--; /* correct an off-by-one error */
1702 /* adjust strength of monsters based on u.uz and u.ulevel */
1705 register struct permonst *ptr;
1709 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1710 /* does not depend on other strengths, but does get stronger
1711 * every time he is killed
1713 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1719 if ((tmp = ptr->mlevel) > 49)
1720 return 50; /* "special" demons/devils */
1721 tmp2 = (level_difficulty() - tmp);
1723 tmp--; /* if mlevel > u.uz decrement tmp */
1725 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1727 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1729 tmp += (tmp2 / 4); /* level as well */
1731 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1733 tmp2 = 49; /* hard upper limit */
1734 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1737 /* monster earned experience and will gain some hit points; it might also
1738 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1740 grow_up(mtmp, victim)
1741 struct monst *mtmp, *victim;
1743 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1745 struct permonst *ptr = mtmp->data;
1747 /* monster died after killing enemy but before calling this function */
1748 /* currently possible if killing a gas spore */
1750 return (struct permonst *) 0;
1752 /* note: none of the monsters with special hit point calculations
1753 have both little and big forms */
1754 oldtype = monsndx(ptr);
1755 newtype = little_to_big(oldtype);
1756 if (newtype == PM_PRIEST && mtmp->female)
1757 newtype = PM_PRIESTESS;
1759 /* growth limits differ depending on method of advancement */
1760 if (victim) { /* killed a monster */
1762 * The HP threshold is the maximum number of hit points for the
1763 * current level; once exceeded, a level will be gained.
1764 * Possible bug: if somehow the hit points are already higher
1765 * than that, monster will gain a level without any increase in HP.
1767 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1770 else if (is_golem(ptr)) /* strange creatures */
1771 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1772 else if (is_home_elemental(ptr))
1774 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1775 /* If they can grow up, be sure the level is high enough for that */
1776 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1777 lev_limit = (int) mons[newtype].mlevel;
1778 /* number of hit points to gain; unlike for the player, we put
1779 the limit at the bottom of the next level rather than the top */
1780 max_increase = rnd((int) victim->m_lev + 1);
1781 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1782 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1783 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1785 /* a gain level potion or wraith corpse; always go up a level
1786 unless already at maximum (49 is hard upper limit except
1787 for demon lords, who start at 50 and can't go any higher) */
1788 max_increase = cur_increase = rnd(8);
1789 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1790 lev_limit = 50; /* recalc below */
1793 mtmp->mhpmax += max_increase;
1794 mtmp->mhp += cur_increase;
1795 if (mtmp->mhpmax <= hp_threshold)
1796 return ptr; /* doesn't gain a level */
1798 if (is_mplayer(ptr))
1799 lev_limit = 30; /* same as player */
1800 else if (lev_limit < 5)
1801 lev_limit = 5; /* arbitrary */
1802 else if (lev_limit > 49)
1803 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1805 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1806 ptr = &mons[newtype];
1807 /* new form might force gender change */
1808 fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female;
1810 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1811 if (canspotmon(mtmp))
1813 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1814 an(ptr->mname), mhe(mtmp),
1815 nonliving(ptr) ? "expires" : "dies");
1817 pline("%s
\82ª
\90¬
\92·
\82µ
\82Ä%s
\82É
\82È
\82é
\82Æ%s
\82µ
\82Ü
\82Á
\82½
\81I", mon_nam(mtmp),
1819 nonliving(ptr) ? "
\8fÁ
\82¦
\82Ä" : "
\8e\80\82ñ
\82Å");
1821 set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
1823 return (struct permonst *) 0;
1824 } else if (canspotmon(mtmp)) {
1829 * Temporary (?) hack to fix growing into opposite gender.
1831 Sprintf(buf, "%s%s",
1832 /* deal with female gnome becoming a gnome lord */
1833 (mtmp->female && !fem) ? "male "
1834 /* or a male gnome becoming a gnome lady
1835 (can't happen with 3.6.0 mons[], but perhaps
1836 slightly less sexist if prepared for it...) */
1837 : (fem && !mtmp->female) ? "female " : "",
1839 pline("%s %s %s.", Monnam(mtmp),
1840 (fem != mtmp->female) ? "changes into"
1841 : humanoid(ptr) ? "becomes"
1844 #else /*
\93ú
\96{
\8cê
\82Å
\82Í
\82Æ
\82è
\82 \82¦
\82¸
\82»
\82±
\82Ü
\82Å
\82Í
\82µ
\82È
\82¢ */
1845 pline("%s
\82Í%s
\82É%s
\81D", Monnam(mtmp),
1847 humanoid(ptr) ? "
\82È
\82Á
\82½" : "
\90¬
\92·
\82µ
\82½");
1850 set_mon_data(mtmp, ptr, 1); /* preserve intrinsics */
1851 newsym(mtmp->mx, mtmp->my); /* color may change */
1852 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1854 mtmp->female = fem; /* gender might be changing */
1858 if ((int) mtmp->m_lev > lev_limit) {
1859 mtmp->m_lev--; /* undo increment */
1860 /* HP might have been allowed to grow when it shouldn't */
1861 if (mtmp->mhpmax == hp_threshold + 1)
1864 if (mtmp->mhpmax > 50 * 8)
1865 mtmp->mhpmax = 50 * 8; /* absolute limit */
1866 if (mtmp->mhp > mtmp->mhpmax)
1867 mtmp->mhp = mtmp->mhpmax;
1874 register struct monst *mtmp;
1877 register struct obj *otmp;
1882 otmp = mksobj(otyp, TRUE, FALSE);
1884 if (mtmp->data->mlet == S_DEMON) {
1885 /* demons never get blessed objects */
1888 } else if (is_lminion(mtmp)) {
1889 /* lawful minions don't get cursed, bad, or rusting objects */
1890 otmp->cursed = FALSE;
1893 otmp->oerodeproof = TRUE;
1894 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1895 otmp->spe = (3 + rn2(4));
1898 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1901 otmp->lamplit = FALSE;
1902 otmp->blessed = otmp->cursed = FALSE;
1903 } else if (otmp->otyp == BELL_OF_OPENING) {
1904 otmp->blessed = otmp->cursed = FALSE;
1905 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1906 otmp->blessed = FALSE;
1907 otmp->cursed = TRUE;
1910 /* leaders don't tolerate inferior quality battle gear */
1911 if (is_prince(mtmp->data)) {
1912 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1914 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1919 (void) mpickobj(mtmp, otmp); /* might free otmp */
1930 case PM_STRAW_GOLEM:
1932 case PM_PAPER_GOLEM:
1936 case PM_LEATHER_GOLEM:
1942 case PM_FLESH_GOLEM:
1946 case PM_STONE_GOLEM:
1948 case PM_GLASS_GOLEM:
1958 * Alignment vs. yours determines monster's attitude to you.
1959 * (Some "animal" types are co-aligned, but also hungry.)
1963 register struct permonst *ptr;
1965 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1967 if (always_peaceful(ptr))
1969 if (always_hostile(ptr))
1971 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1973 if (ptr->msound == MS_NEMESIS)
1976 if (race_peaceful(ptr))
1978 if (race_hostile(ptr))
1981 /* the monster is hostile if its alignment is different from the
1983 if (sgn(mal) != sgn(ual))
1986 /* Negative monster hostile to player with Amulet. */
1987 if (mal < A_NEUTRAL && u.uhave.amulet)
1990 /* minions are hostile to players that have strayed at all */
1992 return (boolean) (u.ualign.record >= 0);
1994 /* Last case: a chance of a co-aligned monster being
1995 * hostile. This chance is greater if the player has strayed
1996 * (u.ualign.record negative) or the monster is not strongly aligned.
1998 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
2000 && !!rn2(2 + abs(mal)));
2003 /* Set malign to have the proper effect on player alignment if monster is
2004 * killed. Negative numbers mean it's bad to kill this monster; positive
2005 * numbers mean it's good. Since there are more hostile monsters than
2006 * peaceful monsters, the penalty for killing a peaceful monster should be
2007 * greater than the bonus for killing a hostile monster to maintain balance.
2009 * it's bad to kill peaceful monsters, potentially worse to kill always-
2010 * peaceful monsters;
2011 * it's never bad to kill a hostile monster, although it may not be good.
2017 schar mal = mtmp->data->maligntyp;
2020 if (mtmp->ispriest || mtmp->isminion) {
2021 /* some monsters have individual alignments; check them */
2022 if (mtmp->ispriest && EPRI(mtmp))
2023 mal = EPRI(mtmp)->shralign;
2024 else if (mtmp->isminion && EMIN(mtmp))
2025 mal = EMIN(mtmp)->min_align;
2026 /* unless alignment is none, set mal to -5,0,5 */
2027 /* (see align.h for valid aligntyp values) */
2032 coaligned = (sgn(mal) == sgn(u.ualign.type));
2033 if (mtmp->data->msound == MS_LEADER) {
2035 } else if (mal == A_NONE) {
2036 if (mtmp->mpeaceful)
2039 mtmp->malign = 20; /* really hostile */
2040 } else if (always_peaceful(mtmp->data)) {
2041 int absmal = abs(mal);
2042 if (mtmp->mpeaceful)
2043 mtmp->malign = -3 * max(5, absmal);
2045 mtmp->malign = 3 * max(5, absmal); /* renegade */
2046 } else if (always_hostile(mtmp->data)) {
2047 int absmal = abs(mal);
2051 mtmp->malign = max(5, absmal);
2052 } else if (coaligned) {
2053 int absmal = abs(mal);
2054 if (mtmp->mpeaceful)
2055 mtmp->malign = -3 * max(3, absmal);
2057 mtmp->malign = max(3, absmal);
2058 } else /* not coaligned and therefore hostile */
2059 mtmp->malign = abs(mal);
2062 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
2068 mtmp->mextra = newmextra();
2069 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
2072 /* release monster's mcorpsenm field; basically a no-op */
2077 if (has_mcorpsenm(mtmp))
2078 MCORPSENM(mtmp) = NON_PM;
2081 static NEARDATA char syms[] = {
2082 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
2083 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
2084 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
2085 S_MIMIC_DEF, S_MIMIC_DEF,
2090 register struct monst *mtmp;
2092 int typ, roomno, rt;
2093 unsigned appear, ap_type;
2098 if (!mtmp || Protection_from_shape_changers)
2102 typ = levl[mx][my].typ;
2103 /* only valid for INSIDE of room */
2104 roomno = levl[mx][my].roomno - ROOMOFFSET;
2106 rt = rooms[roomno].rtype;
2107 #ifdef SPECIALIZATION
2108 else if (IS_ROOM(typ))
2109 rt = OROOM, roomno = 0;
2112 rt = 0; /* roomno < 0 case for GCC_WARN */
2114 if (OBJ_AT(mx, my)) {
2115 ap_type = M_AP_OBJECT;
2116 appear = level.objects[mx][my]->otyp;
2117 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2118 ap_type = M_AP_FURNITURE;
2120 * If there is a wall to the left that connects to this
2121 * location, then the mimic mimics a horizontal closed door.
2122 * This does not allow doors to be in corners of rooms.
2123 * Since rogue has no closed doors, mimic a wall there
2124 * (yes, mimics can end up on this level by various means).
2126 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2127 || levl[mx - 1][my].typ == TLCORNER
2128 || levl[mx - 1][my].typ == TRWALL
2129 || levl[mx - 1][my].typ == BLCORNER
2130 || levl[mx - 1][my].typ == TDWALL
2131 || levl[mx - 1][my].typ == CROSSWALL
2132 || levl[mx - 1][my].typ == TUWALL))
2133 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2135 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2136 if (!mtmp->minvis || See_invisible)
2137 block_point(mx, my); /* vision */
2138 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2139 ap_type = M_AP_OBJECT;
2141 } else if (roomno < 0 && !t_at(mx, my)) {
2142 ap_type = M_AP_OBJECT;
2144 if (!mtmp->minvis || See_invisible)
2145 block_point(mx, my); /* vision */
2146 } else if (rt == ZOO || rt == VAULT) {
2147 ap_type = M_AP_OBJECT;
2148 appear = GOLD_PIECE;
2149 } else if (rt == DELPHI) {
2151 ap_type = M_AP_OBJECT;
2154 ap_type = M_AP_FURNITURE;
2155 appear = S_fountain;
2157 } else if (rt == TEMPLE) {
2158 ap_type = M_AP_FURNITURE;
2161 * We won't bother with beehives, morgues, barracks, throne rooms
2162 * since they shouldn't contain too many mimics anyway...
2164 } else if (rt >= SHOPBASE) {
2165 s_sym = get_shop_item(rt - SHOPBASE);
2167 ap_type = M_AP_OBJECT;
2170 if (s_sym == RANDOM_CLASS)
2171 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2175 s_sym = syms[rn2((int) sizeof(syms))];
2177 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2178 ap_type = M_AP_FURNITURE;
2179 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2181 ap_type = M_AP_OBJECT;
2182 if (s_sym == S_MIMIC_DEF) {
2183 appear = STRANGE_OBJECT;
2184 } else if (s_sym == COIN_CLASS) {
2185 appear = GOLD_PIECE;
2187 otmp = mkobj((char) s_sym, FALSE);
2188 appear = otmp->otyp;
2189 /* make sure container contents are free'ed */
2190 obfree(otmp, (struct obj *) 0);
2194 mtmp->m_ap_type = ap_type;
2195 mtmp->mappearance = appear;
2196 if (ap_type == M_AP_OBJECT && (appear == STATUE || appear == CORPSE
2197 || appear == FIGURINE || appear == EGG)) {
2199 MCORPSENM(mtmp) = rndmonnum();
2200 if (appear == EGG && !can_be_hatched(MCORPSENM(mtmp)))
2201 MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
2205 /* release monster from bag of tricks; return number of monsters created */
2207 bagotricks(bag, tipping, seencount)
2209 boolean tipping; /* caller emptying entire contents; affects shop handling */
2210 int *seencount; /* secondary output */
2214 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2215 impossible("bad bag o' tricks");
2216 } else if (bag->spe < 1) {
2217 /* if tipping known empty bag, give normal empty container message */
2219 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2221 pline1((tipping && bag->cknown) ? "
\82»
\82ê
\82Í
\8bó
\82¾
\81D" : nothing_happens);
2222 /* now known to be empty if sufficiently discovered */
2223 if (bag->dknown && objects[bag->otyp].oc_name_known)
2227 int creatcnt = 1, seecount = 0;
2229 consume_obj_charge(bag, !tipping);
2234 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2237 if (canspotmon(mtmp))
2240 } while (--creatcnt > 0);
2243 *seencount += seecount;
2245 makeknown(BAG_OF_TRICKS);
2246 } else if (!tipping) {
2248 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2250 pline1(!moncount ? nothing_happens : "
\89½
\82à
\8bN
\82«
\82È
\82©
\82Á
\82½
\82æ
\82¤
\82¾
\81D");