1 /* NetHack 3.6 makemon.c $NHDT-Date: 1449269917 2015/12/04 22:58:37 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.105 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* JNetHack Copyright */
6 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016 */
8 /* JNetHack may be freely redistributed. See license for details. */
14 STATIC_VAR NEARDATA struct monst zeromonst;
16 /* this assumes that a human quest leader or nemesis is an archetype
17 of the corresponding role; that isn't so for some roles (tourist
18 for instance) but is for the priests and monks we use it for... */
19 #define quest_mon_represents_role(mptr, role_pm) \
20 (mptr->mlet == S_HUMAN && Role_if(role_pm) \
21 && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
23 STATIC_DCL boolean FDECL(uncommon, (int));
24 STATIC_DCL int FDECL(align_shift, (struct permonst *));
25 STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
26 STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
27 STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int));
28 STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
29 STATIC_DCL void FDECL(m_initweap, (struct monst *));
30 STATIC_DCL void FDECL(m_initinv, (struct monst *));
31 STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *, unsigned, coord *));
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);
283 /* create minion stuff; can't use mongets */
284 otmp = mksobj(LONG_SWORD, FALSE, FALSE);
286 /* maybe make it special */
287 if (!rn2(20) || is_lord(ptr))
289 artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
291 otmp->oerodeproof = TRUE;
293 otmp->spe = max(otmp->spe, spe2);
294 (void) mpickobj(mtmp, otmp);
296 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
299 otmp->cursed = FALSE;
300 otmp->oerodeproof = TRUE;
302 (void) mpickobj(mtmp, otmp);
307 if (mm == PM_HOBBIT) {
310 (void) mongets(mtmp, DAGGER);
313 (void) mongets(mtmp, ELVEN_DAGGER);
316 (void) mongets(mtmp, SLING);
320 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
322 (void) mongets(mtmp, DWARVISH_CLOAK);
323 } else if (is_dwarf(ptr)) {
325 (void) mongets(mtmp, DWARVISH_CLOAK);
327 (void) mongets(mtmp, IRON_SHOES);
329 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
330 /* note: you can't use a mattock with a shield */
332 (void) mongets(mtmp, DWARVISH_MATTOCK);
334 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
335 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
337 (void) mongets(mtmp, DWARVISH_IRON_HELM);
339 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
341 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
346 /* create Keystone Kops with cream pies to
347 throw. As suggested by KAA. [MRS] */
349 m_initthrow(mtmp, CREAM_PIE, 2);
351 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
355 (void) mongets(mtmp, ORCISH_HELM);
356 switch ((mm != PM_ORC_CAPTAIN) ? mm
357 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
360 (void) mongets(mtmp, SCIMITAR);
362 (void) mongets(mtmp, ORCISH_SHIELD);
364 (void) mongets(mtmp, KNIFE);
366 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
370 (void) mongets(mtmp, ORCISH_CLOAK);
372 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
374 (void) mongets(mtmp, IRON_SHOES);
376 (void) mongets(mtmp, ORCISH_BOW);
377 m_initthrow(mtmp, ORCISH_ARROW, 12);
380 (void) mongets(mtmp, URUK_HAI_SHIELD);
383 if (mm != PM_ORC_SHAMAN && rn2(2))
384 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
390 if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
391 (void) mongets(mtmp, BATTLE_AXE);
393 (void) mongets(mtmp, CLUB);
399 (void) mongets(mtmp, RANSEUR);
402 (void) mongets(mtmp, PARTISAN);
405 (void) mongets(mtmp, GLAIVE);
408 (void) mongets(mtmp, SPETUM);
414 m_initthrow(mtmp, DART, 12);
419 if (ptr == &mons[PM_FOREST_CENTAUR]) {
420 (void) mongets(mtmp, BOW);
421 m_initthrow(mtmp, ARROW, 12);
423 (void) mongets(mtmp, CROSSBOW);
424 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
429 (void) mongets(mtmp, KNIFE);
430 (void) mongets(mtmp, LONG_SWORD);
434 (void) mongets(mtmp, LEATHER_ARMOR);
436 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
439 if (mm == PM_SALAMANDER)
441 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
446 (void) mongets(mtmp, BULLWHIP);
447 (void) mongets(mtmp, BROADSWORD);
450 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
452 case PM_HORNED_DEVIL:
453 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
456 (void) mongets(mtmp, WAN_STRIKING);
459 (void) mongets(mtmp, FLAIL);
462 /* prevent djinn and mail daemons from leaving objects when
470 * Now the general case, some chance of getting some type
471 * of weapon for "normal" monsters. Certain special types
472 * of monsters will get a bonus chance or different selections.
474 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
475 switch (rnd(14 - (2 * bias))) {
477 if (strongmonst(ptr))
478 (void) mongets(mtmp, BATTLE_AXE);
480 m_initthrow(mtmp, DART, 12);
483 if (strongmonst(ptr))
484 (void) mongets(mtmp, TWO_HANDED_SWORD);
486 (void) mongets(mtmp, CROSSBOW);
487 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
491 (void) mongets(mtmp, BOW);
492 m_initthrow(mtmp, ARROW, 12);
495 if (strongmonst(ptr))
496 (void) mongets(mtmp, LONG_SWORD);
498 m_initthrow(mtmp, DAGGER, 3);
501 if (strongmonst(ptr))
502 (void) mongets(mtmp, LUCERN_HAMMER);
504 (void) mongets(mtmp, AKLYS);
512 if ((int) mtmp->m_lev > rn2(75))
513 (void) mongets(mtmp, rnd_offensive_item(mtmp));
517 * Makes up money for monster's inventory.
518 * This will change with silver & copper coins
521 mkmonmoney(mtmp, amount)
525 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
528 add_to_minv(mtmp, gold);
533 register struct monst *mtmp;
536 register struct obj *otmp;
537 register struct permonst *ptr = mtmp->data;
539 if (Is_rogue_level(&u.uz))
542 * Soldiers get armour & rations - armour approximates their ac.
543 * Nymphs may get mirror or potion of object detection.
547 if (is_mercenary(ptr)) {
550 switch (monsndx(ptr)) {
569 case PM_WATCH_CAPTAIN:
573 impossible("odd mercenary %d?", monsndx(ptr));
578 if (mac < -1 && rn2(5))
579 mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
580 : CRYSTAL_PLATE_MAIL);
581 else if (mac < 3 && rn2(5))
583 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
585 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
586 : STUDDED_LEATHER_ARMOR);
588 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
590 if (mac < 10 && rn2(3))
591 mac += 1 + mongets(mtmp, HELMET);
592 else if (mac < 10 && rn2(2))
593 mac += 1 + mongets(mtmp, DENTED_POT);
594 if (mac < 10 && rn2(3))
595 mac += 1 + mongets(mtmp, SMALL_SHIELD);
596 else if (mac < 10 && rn2(2))
597 mac += 2 + mongets(mtmp, LARGE_SHIELD);
598 if (mac < 10 && rn2(3))
599 mac += 1 + mongets(mtmp, LOW_BOOTS);
600 else if (mac < 10 && rn2(2))
601 mac += 2 + mongets(mtmp, HIGH_BOOTS);
602 if (mac < 10 && rn2(3))
603 mac += 1 + mongets(mtmp, LEATHER_GLOVES);
604 else if (mac < 10 && rn2(2))
605 mac += 1 + mongets(mtmp, LEATHER_CLOAK);
607 if (ptr != &mons[PM_GUARD] && ptr != &mons[PM_WATCHMAN]
608 && ptr != &mons[PM_WATCH_CAPTAIN]) {
610 (void) mongets(mtmp, K_RATION);
612 (void) mongets(mtmp, C_RATION);
613 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
614 (void) mongets(mtmp, BUGLE);
615 } else if (ptr == &mons[PM_WATCHMAN] && rn2(3))
616 (void) mongets(mtmp, TIN_WHISTLE);
617 } else if (ptr == &mons[PM_SHOPKEEPER]) {
618 (void) mongets(mtmp, SKELETON_KEY);
620 /* MAJOR fall through ... */
622 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
624 (void) mongets(mtmp, POT_EXTRA_HEALING);
626 (void) mongets(mtmp, POT_HEALING);
628 (void) mongets(mtmp, WAN_STRIKING);
630 } else if (ptr->msound == MS_PRIEST
631 || quest_mon_represents_role(ptr, PM_PRIEST)) {
632 (void) mongets(mtmp, rn2(7) ? ROBE
633 : rn2(3) ? CLOAK_OF_PROTECTION
634 : CLOAK_OF_MAGIC_RESISTANCE);
635 (void) mongets(mtmp, SMALL_SHIELD);
636 mkmonmoney(mtmp, (long) rn1(10, 20));
637 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
638 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
643 (void) mongets(mtmp, MIRROR);
645 (void) mongets(mtmp, POT_OBJECT_DETECTION);
648 if (ptr == &mons[PM_MINOTAUR]) {
649 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
650 (void) mongets(mtmp, WAN_DIGGING);
651 } else if (is_giant(ptr)) {
652 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
653 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
655 otmp->quan = (long) rn1(2, 3);
656 otmp->owt = weight(otmp);
657 (void) mpickobj(mtmp, otmp);
662 if (ptr == &mons[PM_NAZGUL]) {
663 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
665 (void) mpickobj(mtmp, otmp);
669 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
670 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
671 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
672 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
673 rn2(13) ? FALSE : TRUE);
677 otmp->oerodeproof = 1;
678 (void) mpickobj(mtmp, otmp);
683 (void) mongets(mtmp, MUMMY_WRAPPING);
687 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
688 otmp->spe = 1; /* flag for special box */
689 otmp->owt = weight(otmp);
690 (void) mpickobj(mtmp, otmp);
694 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
697 /* moved here from m_initweap() because these don't
698 have AT_WEAP so m_initweap() is not called for them */
699 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
700 (void) mongets(mtmp, SPEAR);
701 } else if (ptr == &mons[PM_ASMODEUS]) {
702 (void) mongets(mtmp, WAN_COLD);
703 (void) mongets(mtmp, WAN_FIRE);
707 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
708 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
710 otmp->owt = weight(otmp);
711 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
712 begin_burn(otmp, FALSE);
719 /* ordinary soldiers rarely have access to magic (or gold :-) */
720 if (ptr == &mons[PM_SOLDIER] && rn2(13))
723 if ((int) mtmp->m_lev > rn2(50))
724 (void) mongets(mtmp, rnd_defensive_item(mtmp));
725 if ((int) mtmp->m_lev > rn2(100))
726 (void) mongets(mtmp, rnd_misc_item(mtmp));
727 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
729 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
732 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
736 xchar x, y; /* clone's preferred location or 0 (near mon) */
741 /* may be too weak or have been extinguished for population control */
742 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
743 return (struct monst *) 0;
748 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
749 return (struct monst *) 0;
750 } else if (!isok(x, y)) {
751 return (struct monst *) 0; /* paranoia */
755 if (MON_AT(mm.x, mm.y)) {
756 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
757 return (struct monst *) 0;
761 *m2 = *mon; /* copy condition of old monster */
762 m2->mextra = (struct mextra *) 0;
765 m2->m_id = context.ident++;
767 m2->m_id = context.ident++; /* ident overflowed */
772 m2->minvent = (struct obj *) 0; /* objects don't clone */
773 m2->mleashed = FALSE;
774 /* Max HP the same, but current HP halved for both. The caller
775 * might want to override this by halving the max HP also.
776 * When current HP is odd, the original keeps the extra point.
778 m2->mhpmax = mon->mhpmax;
779 m2->mhp = mon->mhp / 2;
782 /* since shopkeepers and guards will only be cloned if they've been
783 * polymorphed away from their original forms, the clone doesn't have
784 * room for the extra information. we also don't want two shopkeepers
785 * around for the same shop.
792 m2->ispriest = FALSE;
793 place_monster(m2, m2->mx, m2->my);
794 if (emits_light(m2->data))
795 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
797 if (has_mname(mon)) {
798 m2 = christen_monst(m2, MNAME(mon));
799 } else if (mon->isshk) {
800 m2 = christen_monst(m2, shkname(mon));
803 /* not all clones caused by player are tame or peaceful */
804 if (!context.mon_moving) {
806 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
807 else if (mon->mpeaceful)
808 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
811 newsym(m2->mx, m2->my); /* display the new monster */
816 *(EMIN(m2)) = *(EMIN(mon));
818 /* because m2 is a copy of mon it is tame but not init'ed.
819 * however, tamedog will not re-tame a tame dog, so m2
820 * must be made non-tame to get initialized properly.
823 if (tamedog(m2, (struct obj *) 0)) {
824 *(EDOG(m2)) = *(EDOG(mon));
834 * Propagate a species
836 * Once a certain number of monsters are created, don't create any more
837 * at random (i.e. make them extinct). The previous (3.2) behavior was
838 * to do this when a certain number had _died_, which didn't make
841 * Returns FALSE propagation unsuccessful
842 * TRUE propagation successful
845 propagate(mndx, tally, ghostly)
851 uchar lim = mbirth_limit(mndx);
852 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
854 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
856 /* if it's unique, don't ever make it again */
857 if (mons[mndx].geno & G_UNIQ)
858 mvitals[mndx].mvflags |= G_EXTINCT;
860 if (mvitals[mndx].born < 255 && tally
861 && (!ghostly || (ghostly && result)))
862 mvitals[mndx].born++;
863 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
864 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
866 debugpline1("Automatically extinguished %s.",
867 makeplural(mons[mndx].mname));
869 mvitals[mndx].mvflags |= G_EXTINCT;
870 reset_rndmonst(mndx);
875 /* amount of HP to lose from level drain (or gain from Stormbringer) */
880 struct permonst *ptr = mon->data;
881 int hp = rnd(8); /* default is d8 */
883 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
885 /* draining usually won't be applicable for these critters */
886 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
887 } else if (ptr->mlevel > 49) {
888 /* arbitrary; such monsters won't be involved in draining anyway */
889 hp = 4 + rnd(4); /* 5..8 */
890 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
891 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
893 hp = 4 + rn2(5); /* 4..8 */
894 } else if (!mon->m_lev) {
895 /* level 0 monsters use 1d4 instead of Nd8 */
901 /* set up a new monster's initial level and hit points;
902 used by newcham() as well as by makemon() */
908 struct permonst *ptr = &mons[mndx];
910 mon->m_lev = adj_lev(ptr);
912 mon->mhpmax = mon->mhp = golemhp(mndx);
913 } else if (is_rider(ptr)) {
914 /* we want low HP, but a high mlevel so they can attack well */
915 mon->mhpmax = mon->mhp = d(10, 8);
916 } else if (ptr->mlevel > 49) {
917 /* "special" fixed hp monster
918 * the hit points are encoded in the mlevel in a somewhat strange
919 * way to fit in the 50..127 positive range of a signed character
920 * above the 1..49 that indicate "normal" monster levels */
921 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
922 mon->m_lev = mon->mhp / 4; /* approximation */
923 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
925 mon->mhpmax = mon->mhp =
926 (int) (In_endgame(&u.uz)
928 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
929 } else if (!mon->m_lev) {
930 mon->mhpmax = mon->mhp = rnd(4);
932 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
933 if (is_home_elemental(ptr))
934 mon->mhpmax = (mon->mhp *= 3);
941 struct mextra *mextra;
943 mextra = (struct mextra *) alloc(sizeof(struct mextra));
950 mextra->mcorpsenm = NON_PM;
955 makemon_rnd_goodpos(mon, gpflags, cc)
965 nx = rn1(COLNO - 3, 2);
967 good = (!in_mklev && cansee(nx,ny)) ? FALSE
968 : goodpos(nx, ny, mon, gpflags);
969 } while ((++tryct < 50) && !good);
972 /* else go through all map positions, twice, first round
973 ignoring positions in sight, and pick first good one.
974 skip first round if we're in special level loader or blind */
978 int bl = (in_mklev || Blind) ? 1 : 0;
980 for ( ; bl < 2; bl++) {
981 for (dx = 0; dx < COLNO; dx++)
982 for (dy = 0; dy < ROWNO; dy++) {
983 nx = ((dx + xofs) % (COLNO - 1)) + 1;
984 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
985 if (bl == 0 && cansee(nx,ny))
987 if (goodpos(nx, ny, mon, gpflags))
990 if (bl == 0 && (!mon || mon->data->mmove)) {
991 /* all map positions are visible (or not good),
992 try to pick something logical */
993 if (dnstair.sx && !rn2(2)) {
996 } else if (upstair.sx && !rn2(2)) {
999 } else if (dnladder.sx && !rn2(2)) {
1002 } else if (upladder.sx && !rn2(2)) {
1006 if (goodpos(nx, ny, mon, gpflags))
1020 * called with [x,y] = coordinates;
1021 * [0,0] means anyplace
1022 * [u.ux,u.uy] means: near player (if !in_mklev)
1024 * In case we make a monster group, only return the one at [x,y].
1027 makemon(ptr, x, y, mmflags)
1028 register struct permonst *ptr;
1032 register struct monst *mtmp;
1033 int mndx, mcham, ct, mitem;
1034 boolean anymon = (!ptr);
1035 boolean byyou = (x == u.ux && y == u.uy);
1036 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1037 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1038 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1040 /* if caller wants random location, do it here */
1041 if (x == 0 && y == 0) {
1043 struct monst fakemon;
1045 cc.x = cc.y = 0; /* lint suppression */
1046 fakemon.data = ptr; /* set up for goodpos */
1047 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1049 return (struct monst *) 0;
1052 } else if (byyou && !in_mklev) {
1055 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1059 return (struct monst *) 0;
1062 /* Does monster already exist at the position? */
1064 if ((mmflags & MM_ADJACENTOK) != 0) {
1066 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1070 return (struct monst *) 0;
1072 return (struct monst *) 0;
1076 mndx = monsndx(ptr);
1077 /* if you are to make a specific monster and it has
1078 already been genocided, return */
1079 if (mvitals[mndx].mvflags & G_GENOD)
1080 return (struct monst *) 0;
1081 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1082 debugpline1("Explicitly creating extinct monster %s.",
1086 /* make a random (common) monster that can survive here.
1087 * (the special levels ask for random monsters at specific
1088 * positions, causing mass drowning on the medusa level,
1091 int tryct = 0; /* maybe there are no good choices */
1092 struct monst fakemon;
1095 if (!(ptr = rndmonst())) {
1096 debugpline0("Warning: no monster.");
1097 return (struct monst *) 0; /* no more monsters! */
1099 fakemon.data = ptr; /* set up for goodpos */
1100 } while (++tryct <= 50
1101 /* in Sokoban, don't accept a giant on first try;
1102 after that, boulder carriers are fair game */
1103 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1104 || !goodpos(x, y, &fakemon, gpflags)));
1105 mndx = monsndx(ptr);
1107 (void) propagate(mndx, countbirth, FALSE);
1109 *mtmp = zeromonst; /* clear all entries in structure */
1111 if (mmflags & MM_EGD)
1113 if (mmflags & MM_EPRI)
1115 if (mmflags & MM_ESHK)
1117 if (mmflags & MM_EMIN)
1119 if (mmflags & MM_EDOG)
1124 mtmp->m_id = context.ident++;
1126 mtmp->m_id = context.ident++; /* ident overflowed */
1127 set_mon_data(mtmp, ptr, 0);
1128 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1129 quest_status.leader_m_id = mtmp->m_id;
1132 /* set up level and hit points */
1133 newmonhp(mtmp, mndx);
1136 mtmp->female = TRUE;
1137 else if (is_male(ptr))
1138 mtmp->female = FALSE;
1139 /* leader and nemesis gender is usually hardcoded in mons[],
1140 but for ones which can be random, it has already been chosen
1141 (in role_init(), for possible use by the quest pager code) */
1142 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1143 mtmp->female = quest_status.ldrgend;
1144 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1145 mtmp->female = quest_status.nemgend;
1147 mtmp->female = rn2(2); /* ignored for neuters */
1149 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1150 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1151 /* quest leader and nemesis both know about all trap types */
1152 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1153 mtmp->mtrapseen = ~0;
1155 place_monster(mtmp, x, y);
1156 mtmp->mcansee = mtmp->mcanmove = TRUE;
1157 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1159 switch (ptr->mlet) {
1161 set_mimic_sym(mtmp);
1167 (void) mkobj_at(0, x, y, TRUE);
1168 (void) hideunder(mtmp);
1172 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1173 mtmp->perminvis = TRUE;
1174 mtmp->minvis = TRUE;
1178 (void) hideunder(mtmp);
1181 mtmp->msleeping = 1;
1185 if (rn2(5) && !u.uhave.amulet)
1186 mtmp->msleeping = 1;
1189 if (Race_if(PM_ELF))
1190 mtmp->mpeaceful = FALSE;
1193 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1194 mtmp->mpeaceful = TRUE;
1197 if (Inhell && is_bat(ptr))
1198 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1201 if ((ct = emits_light(mtmp->data)) > 0)
1202 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1203 monst_to_any(mtmp));
1204 mitem = 0; /* extra inventory item for this monster */
1206 if (mndx == PM_VLAD_THE_IMPALER)
1207 mitem = CANDELABRUM_OF_INVOCATION;
1208 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1209 if ((mcham = pm_to_cham(mndx)) != NON_PM) {
1210 /* this is a shapechanger after all */
1211 if (Protection_from_shape_changers
1212 || mndx == PM_VLAD_THE_IMPALER) {
1213 ; /* stuck in its natural form (NON_PM) */
1216 /* Note: shapechanger's initial form used to be
1217 chosen here with rndmonst(), yielding a monster
1218 which was appropriate to the level's difficulty
1219 but ignored the changer's usual type selection
1220 so would be inappropriate for vampshifters.
1221 Let newcham() pick the shape. */
1222 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1223 allow_minvent = FALSE;
1225 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1227 context.no_of_wizards++;
1228 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1230 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1231 mtmp = christen_monst(mtmp, rndghostname());
1232 } else if (mndx == PM_CROESUS) {
1233 mitem = TWO_HANDED_SWORD;
1234 } else if (ptr->msound == MS_NEMESIS) {
1235 mitem = BELL_OF_OPENING;
1236 } else if (mndx == PM_PESTILENCE) {
1237 mitem = POT_SICKNESS;
1239 if (mitem && allow_minvent)
1240 (void) mongets(mtmp, mitem);
1243 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1244 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1245 && !u.uhave.amulet && rn2(5))
1246 mtmp->msleeping = TRUE;
1249 newsym(mtmp->mx, mtmp->my);
1253 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1254 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1256 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1257 mtmp->mpeaceful = mtmp->mtame = FALSE;
1260 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1262 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1264 if (mndx == PM_LONG_WORM
1265 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1268 /* we can now create worms with tails - 11/91 */
1269 initworm(mtmp, rn2(5));
1270 if (count_wsegs(mtmp))
1271 place_worm_tail_randomly(mtmp, x, y);
1273 /* it's possible to create an ordinary monster of some special
1274 types; make sure their extended data is initialized to
1275 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1276 (when they're specified, caller intends to handle this itself) */
1277 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1278 ? !(mmflags & (MM_EPRI | MM_EMIN))
1279 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1284 mtmp->isminion = 1; /* make priest be a roamer */
1285 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1286 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1287 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1291 set_malign(mtmp); /* having finished peaceful changes */
1293 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1294 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1295 } else if (ptr->geno & G_LGROUP) {
1297 m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1299 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1303 if (allow_minvent) {
1305 m_initweap(mtmp); /* equip with weapons / armor */
1306 m_initinv(mtmp); /* add on a few special items incl. more armor */
1307 m_dowear(mtmp, TRUE);
1309 /* no initial inventory is allowed */
1311 discard_minvent(mtmp);
1312 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1314 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1315 if (ptr->mflags3 & M3_WAITFORU)
1316 mtmp->mstrategy |= STRAT_WAITFORU;
1317 if (ptr->mflags3 & M3_CLOSE)
1318 mtmp->mstrategy |= STRAT_CLOSE;
1319 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1320 mtmp->mstrategy |= STRAT_APPEARMSG;
1324 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1333 /* assert(MAXMONNO < 255); */
1334 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1337 /* used for wand/scroll/spell of create monster */
1338 /* returns TRUE iff you know monsters have been created */
1340 create_critters(cnt, mptr, neverask)
1342 struct permonst *mptr; /* usually null; used for confused reading */
1348 boolean known = FALSE;
1349 boolean ask = (wizard && !neverask);
1353 if (create_particular()) {
1357 ask = FALSE; /* ESC will shut off prompting */
1360 /* if in water, try to encourage an aquatic monster
1361 by finding and then specifying another wet location */
1362 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1365 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1366 if (mon && canspotmon(mon))
1376 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1378 if (mvitals[mndx].mvflags & G_GONE)
1381 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1383 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1387 * shift the probability of a monster's generation by
1388 * comparing the dungeon alignment and monster alignment.
1389 * return an integer in the range of 0-5.
1393 register struct permonst *ptr;
1395 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1396 static NEARDATA s_level *lev;
1397 register int alshift;
1399 if (oldmoves != moves) {
1400 lev = Is_special(&u.uz);
1403 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1404 default: /* just in case */
1409 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1412 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1415 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1421 static NEARDATA struct {
1423 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1424 } rndmonst_state = { -1, { 0 } };
1426 /* select a random monster type */
1430 register struct permonst *ptr;
1431 register int mndx, ct;
1433 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1436 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1437 int zlevel, minmlev, maxmlev;
1441 rndmonst_state.choice_count = 0;
1442 /* look for first common monster */
1443 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1444 if (!uncommon(mndx))
1446 rndmonst_state.mchoices[mndx] = 0;
1448 if (mndx == SPECIAL_PM) {
1449 /* evidently they've all been exterminated */
1450 debugpline0("rndmonst: no common mons!");
1451 return (struct permonst *) 0;
1452 } /* else `mndx' now ready for use below */
1453 zlevel = level_difficulty();
1454 /* determine the level of the weakest monster to make. */
1455 minmlev = zlevel / 6;
1456 /* determine the level of the strongest monster to make. */
1457 maxmlev = (zlevel + u.ulevel) / 2;
1458 upper = Is_rogue_level(&u.uz);
1459 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1462 * Find out how many monsters exist in the range we have selected.
1464 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1466 rndmonst_state.mchoices[mndx] = 0;
1467 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1469 if (upper && !isupper(def_monsyms[(int) (ptr->mlet)].sym))
1471 if (elemlevel && wrong_elem_type(ptr))
1475 if (Inhell && (ptr->geno & G_NOHELL))
1477 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1478 if (ct < 0 || ct > 127)
1479 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1480 rndmonst_state.choice_count += ct;
1481 rndmonst_state.mchoices[mndx] = (char) ct;
1484 * Possible modification: if choice_count is "too low",
1485 * expand minmlev..maxmlev range and try again.
1487 } /* choice_count+mchoices[] recalc */
1489 if (rndmonst_state.choice_count <= 0) {
1490 /* maybe no common mons left, or all are too weak or too strong */
1491 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1492 return (struct permonst *) 0;
1496 * Now, select a monster at random.
1498 ct = rnd(rndmonst_state.choice_count);
1499 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1500 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1503 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1504 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1505 return (struct permonst *) 0;
1510 /* called when you change level (experience or dungeon depth) or when
1511 monster species can no longer be created (genocide or extinction) */
1513 reset_rndmonst(mndx)
1514 int mndx; /* particular species that can no longer be created */
1516 /* cached selection info is out of date */
1517 if (mndx == NON_PM) {
1518 rndmonst_state.choice_count = -1; /* full recalc needed */
1519 } else if (mndx < SPECIAL_PM) {
1520 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1521 rndmonst_state.mchoices[mndx] = 0;
1522 } /* note: safe to ignore extinction of unique monsters */
1525 /* decide whether it's ok to generate a candidate monster by mkclass() */
1527 mk_gen_ok(mndx, mvflagsmask, genomask)
1528 int mndx, mvflagsmask, genomask;
1530 struct permonst *ptr = &mons[mndx];
1532 if (mvitals[mndx].mvflags & mvflagsmask)
1534 if (ptr->geno & genomask)
1536 if (is_placeholder(ptr))
1539 /* special levels might ask for random demon type; reject this one */
1540 if (ptr == &mons[PM_MAIL_DAEMON])
1546 /* Make one of the multiple types of a given monster class.
1547 * The second parameter specifies a special casing bit mask
1548 * to allow the normal genesis masks to be deactivated.
1549 * Returns Null if no monsters in that class can be made.
1556 register int first, last, num = 0;
1557 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1559 maxmlev = level_difficulty() >> 1;
1560 if (class < 1 || class >= MAXMCLASSES) {
1561 impossible("mkclass called with bad class!");
1562 return (struct permonst *) 0;
1564 /* Assumption #1: monsters of a given class are contiguous in the
1567 for (first = LOW_PM; first < SPECIAL_PM; first++)
1568 if (mons[first].mlet == class)
1570 if (first == SPECIAL_PM)
1571 return (struct permonst *) 0;
1573 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1574 if (mk_gen_ok(last, G_GONE, mask)) {
1576 if (num && toostrong(last, maxmlev)
1577 && monstr[last] != monstr[last - 1] && rn2(2))
1579 num += mons[last].geno & G_FREQ;
1582 return (struct permonst *) 0;
1584 /* Assumption #2: monsters of a given class are presented in ascending
1585 * order of strength.
1587 for (num = rnd(num); num > 0; first++)
1588 if (mk_gen_ok(first, G_GONE, mask)) {
1589 /* skew towards lower value monsters at lower exp. levels */
1590 num -= mons[first].geno & G_FREQ;
1591 if (num && adj_lev(&mons[first]) > (u.ulevel * 2)) {
1592 /* but not when multiple monsters are same level */
1593 if (mons[first].mlevel != mons[first + 1].mlevel)
1597 first--; /* correct an off-by-one error */
1599 return &mons[first];
1602 /* like mkclass(), but excludes difficulty considerations; used when
1603 player with polycontrol picks a class instead of a specific type;
1604 genocided types are avoided but extinct ones are acceptable; we don't
1605 check polyok() here--caller accepts some choices !polyok() would reject */
1610 register int first, last, num = 0;
1612 for (first = LOW_PM; first < SPECIAL_PM; first++)
1613 if (mons[first].mlet == class)
1615 if (first == SPECIAL_PM)
1618 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1619 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1620 num += mons[last].geno & G_FREQ;
1624 for (num = rnd(num); num > 0; first++)
1625 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1626 num -= mons[first].geno & G_FREQ;
1627 first--; /* correct an off-by-one error */
1632 /* adjust strength of monsters based on u.uz and u.ulevel */
1635 register struct permonst *ptr;
1639 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1640 /* does not depend on other strengths, but does get stronger
1641 * every time he is killed
1643 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1649 if ((tmp = ptr->mlevel) > 49)
1650 return 50; /* "special" demons/devils */
1651 tmp2 = (level_difficulty() - tmp);
1653 tmp--; /* if mlevel > u.uz decrement tmp */
1655 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1657 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1659 tmp += (tmp2 / 4); /* level as well */
1661 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1663 tmp2 = 49; /* hard upper limit */
1664 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1667 /* monster earned experience and will gain some hit points; it might also
1668 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1670 grow_up(mtmp, victim)
1671 struct monst *mtmp, *victim;
1673 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1674 struct permonst *ptr = mtmp->data;
1676 /* monster died after killing enemy but before calling this function */
1677 /* currently possible if killing a gas spore */
1679 return (struct permonst *) 0;
1681 /* note: none of the monsters with special hit point calculations
1682 have both little and big forms */
1683 oldtype = monsndx(ptr);
1684 newtype = little_to_big(oldtype);
1685 if (newtype == PM_PRIEST && mtmp->female)
1686 newtype = PM_PRIESTESS;
1688 /* growth limits differ depending on method of advancement */
1689 if (victim) { /* killed a monster */
1691 * The HP threshold is the maximum number of hit points for the
1692 * current level; once exceeded, a level will be gained.
1693 * Possible bug: if somehow the hit points are already higher
1694 * than that, monster will gain a level without any increase in HP.
1696 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1699 else if (is_golem(ptr)) /* strange creatures */
1700 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1701 else if (is_home_elemental(ptr))
1703 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1704 /* If they can grow up, be sure the level is high enough for that */
1705 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1706 lev_limit = (int) mons[newtype].mlevel;
1707 /* number of hit points to gain; unlike for the player, we put
1708 the limit at the bottom of the next level rather than the top */
1709 max_increase = rnd((int) victim->m_lev + 1);
1710 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1711 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1712 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1714 /* a gain level potion or wraith corpse; always go up a level
1715 unless already at maximum (49 is hard upper limit except
1716 for demon lords, who start at 50 and can't go any higher) */
1717 max_increase = cur_increase = rnd(8);
1718 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1719 lev_limit = 50; /* recalc below */
1722 mtmp->mhpmax += max_increase;
1723 mtmp->mhp += cur_increase;
1724 if (mtmp->mhpmax <= hp_threshold)
1725 return ptr; /* doesn't gain a level */
1727 if (is_mplayer(ptr))
1728 lev_limit = 30; /* same as player */
1729 else if (lev_limit < 5)
1730 lev_limit = 5; /* arbitrary */
1731 else if (lev_limit > 49)
1732 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1734 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1735 ptr = &mons[newtype];
1736 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1737 if (canspotmon(mtmp))
1739 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1740 an(ptr->mname), mhe(mtmp),
1741 nonliving(ptr) ? "expires" : "dies");
1743 pline("%s
\82ª
\90¬
\92·
\82µ
\82Ä%s
\82É
\82È
\82é
\82Æ%s
\82µ
\82Ü
\82Á
\82½
\81I", mon_nam(mtmp),
1745 nonliving(ptr) ? "
\8fÁ
\82¦
\82Ä" : "
\8e\80\82ñ
\82Å");
1747 set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
1749 return (struct permonst *) 0;
1750 } else if (canspotmon(mtmp)) {
1752 pline("%s %s %s.", Monnam(mtmp),
1753 humanoid(ptr) ? "becomes" : "grows up into",
1756 pline("%s
\82Í%s
\82É%s
\81D", Monnam(mtmp),
1758 humanoid(ptr) ? "
\82È
\82Á
\82½" : "
\90¬
\92·
\82µ
\82½");
1761 set_mon_data(mtmp, ptr, 1); /* preserve intrinsics */
1762 newsym(mtmp->mx, mtmp->my); /* color may change */
1763 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1766 if ((int) mtmp->m_lev > lev_limit) {
1767 mtmp->m_lev--; /* undo increment */
1768 /* HP might have been allowed to grow when it shouldn't */
1769 if (mtmp->mhpmax == hp_threshold + 1)
1772 if (mtmp->mhpmax > 50 * 8)
1773 mtmp->mhpmax = 50 * 8; /* absolute limit */
1774 if (mtmp->mhp > mtmp->mhpmax)
1775 mtmp->mhp = mtmp->mhpmax;
1782 register struct monst *mtmp;
1785 register struct obj *otmp;
1790 otmp = mksobj(otyp, TRUE, FALSE);
1792 if (mtmp->data->mlet == S_DEMON) {
1793 /* demons never get blessed objects */
1796 } else if (is_lminion(mtmp)) {
1797 /* lawful minions don't get cursed, bad, or rusting objects */
1798 otmp->cursed = FALSE;
1801 otmp->oerodeproof = TRUE;
1802 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1803 otmp->spe = (3 + rn2(4));
1806 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1809 otmp->lamplit = FALSE;
1810 otmp->blessed = otmp->cursed = FALSE;
1811 } else if (otmp->otyp == BELL_OF_OPENING) {
1812 otmp->blessed = otmp->cursed = FALSE;
1813 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1814 otmp->blessed = FALSE;
1815 otmp->cursed = TRUE;
1818 /* leaders don't tolerate inferior quality battle gear */
1819 if (is_prince(mtmp->data)) {
1820 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1822 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1827 (void) mpickobj(mtmp, otmp); /* might free otmp */
1838 case PM_STRAW_GOLEM:
1840 case PM_PAPER_GOLEM:
1844 case PM_LEATHER_GOLEM:
1850 case PM_FLESH_GOLEM:
1854 case PM_STONE_GOLEM:
1856 case PM_GLASS_GOLEM:
1866 * Alignment vs. yours determines monster's attitude to you.
1867 * (Some "animal" types are co-aligned, but also hungry.)
1871 register struct permonst *ptr;
1873 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1875 if (always_peaceful(ptr))
1877 if (always_hostile(ptr))
1879 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1881 if (ptr->msound == MS_NEMESIS)
1884 if (race_peaceful(ptr))
1886 if (race_hostile(ptr))
1889 /* the monster is hostile if its alignment is different from the
1891 if (sgn(mal) != sgn(ual))
1894 /* Negative monster hostile to player with Amulet. */
1895 if (mal < A_NEUTRAL && u.uhave.amulet)
1898 /* minions are hostile to players that have strayed at all */
1900 return (boolean) (u.ualign.record >= 0);
1902 /* Last case: a chance of a co-aligned monster being
1903 * hostile. This chance is greater if the player has strayed
1904 * (u.ualign.record negative) or the monster is not strongly aligned.
1906 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
1908 && !!rn2(2 + abs(mal)));
1911 /* Set malign to have the proper effect on player alignment if monster is
1912 * killed. Negative numbers mean it's bad to kill this monster; positive
1913 * numbers mean it's good. Since there are more hostile monsters than
1914 * peaceful monsters, the penalty for killing a peaceful monster should be
1915 * greater than the bonus for killing a hostile monster to maintain balance.
1917 * it's bad to kill peaceful monsters, potentially worse to kill always-
1918 * peaceful monsters;
1919 * it's never bad to kill a hostile monster, although it may not be good.
1925 schar mal = mtmp->data->maligntyp;
1928 if (mtmp->ispriest || mtmp->isminion) {
1929 /* some monsters have individual alignments; check them */
1930 if (mtmp->ispriest && EPRI(mtmp))
1931 mal = EPRI(mtmp)->shralign;
1932 else if (mtmp->isminion && EMIN(mtmp))
1933 mal = EMIN(mtmp)->min_align;
1934 /* unless alignment is none, set mal to -5,0,5 */
1935 /* (see align.h for valid aligntyp values) */
1940 coaligned = (sgn(mal) == sgn(u.ualign.type));
1941 if (mtmp->data->msound == MS_LEADER) {
1943 } else if (mal == A_NONE) {
1944 if (mtmp->mpeaceful)
1947 mtmp->malign = 20; /* really hostile */
1948 } else if (always_peaceful(mtmp->data)) {
1949 int absmal = abs(mal);
1950 if (mtmp->mpeaceful)
1951 mtmp->malign = -3 * max(5, absmal);
1953 mtmp->malign = 3 * max(5, absmal); /* renegade */
1954 } else if (always_hostile(mtmp->data)) {
1955 int absmal = abs(mal);
1959 mtmp->malign = max(5, absmal);
1960 } else if (coaligned) {
1961 int absmal = abs(mal);
1962 if (mtmp->mpeaceful)
1963 mtmp->malign = -3 * max(3, absmal);
1965 mtmp->malign = max(3, absmal);
1966 } else /* not coaligned and therefore hostile */
1967 mtmp->malign = abs(mal);
1970 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
1976 mtmp->mextra = newmextra();
1977 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
1980 /* release monster's mcorpsenm field; basically a no-op */
1985 if (has_mcorpsenm(mtmp))
1986 MCORPSENM(mtmp) = NON_PM;
1989 static NEARDATA char syms[] = {
1990 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
1991 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
1992 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
1993 S_MIMIC_DEF, S_MIMIC_DEF,
1998 register struct monst *mtmp;
2000 int typ, roomno, rt;
2001 unsigned appear, ap_type;
2010 typ = levl[mx][my].typ;
2011 /* only valid for INSIDE of room */
2012 roomno = levl[mx][my].roomno - ROOMOFFSET;
2014 rt = rooms[roomno].rtype;
2015 #ifdef SPECIALIZATION
2016 else if (IS_ROOM(typ))
2017 rt = OROOM, roomno = 0;
2020 rt = 0; /* roomno < 0 case for GCC_WARN */
2022 if (OBJ_AT(mx, my)) {
2023 ap_type = M_AP_OBJECT;
2024 appear = level.objects[mx][my]->otyp;
2025 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2026 ap_type = M_AP_FURNITURE;
2028 * If there is a wall to the left that connects to this
2029 * location, then the mimic mimics a horizontal closed door.
2030 * This does not allow doors to be in corners of rooms.
2031 * Since rogue has no closed doors, mimic a wall there
2032 * (yes, mimics can end up on this level by various means).
2034 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2035 || levl[mx - 1][my].typ == TLCORNER
2036 || levl[mx - 1][my].typ == TRWALL
2037 || levl[mx - 1][my].typ == BLCORNER
2038 || levl[mx - 1][my].typ == TDWALL
2039 || levl[mx - 1][my].typ == CROSSWALL
2040 || levl[mx - 1][my].typ == TUWALL))
2041 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2043 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2044 if (!mtmp->minvis || See_invisible)
2045 block_point(mx, my); /* vision */
2046 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2047 ap_type = M_AP_OBJECT;
2049 } else if (roomno < 0 && !t_at(mx, my)) {
2050 ap_type = M_AP_OBJECT;
2052 if (!mtmp->minvis || See_invisible)
2053 block_point(mx, my); /* vision */
2054 } else if (rt == ZOO || rt == VAULT) {
2055 ap_type = M_AP_OBJECT;
2056 appear = GOLD_PIECE;
2057 } else if (rt == DELPHI) {
2059 ap_type = M_AP_OBJECT;
2062 ap_type = M_AP_FURNITURE;
2063 appear = S_fountain;
2065 } else if (rt == TEMPLE) {
2066 ap_type = M_AP_FURNITURE;
2069 * We won't bother with beehives, morgues, barracks, throne rooms
2070 * since they shouldn't contain too many mimics anyway...
2072 } else if (rt >= SHOPBASE) {
2073 s_sym = get_shop_item(rt - SHOPBASE);
2075 ap_type = M_AP_OBJECT;
2078 if (s_sym == RANDOM_CLASS)
2079 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2083 s_sym = syms[rn2((int) sizeof(syms))];
2085 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2086 ap_type = M_AP_FURNITURE;
2087 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2089 ap_type = M_AP_OBJECT;
2090 if (s_sym == S_MIMIC_DEF) {
2091 appear = STRANGE_OBJECT;
2092 } else if (s_sym == COIN_CLASS) {
2093 appear = GOLD_PIECE;
2095 otmp = mkobj((char) s_sym, FALSE);
2096 appear = otmp->otyp;
2097 /* make sure container contents are free'ed */
2098 obfree(otmp, (struct obj *) 0);
2102 mtmp->m_ap_type = ap_type;
2103 mtmp->mappearance = appear;
2104 if (ap_type == M_AP_OBJECT && (appear == STATUE || appear == CORPSE
2105 || appear == FIGURINE || appear == EGG)) {
2107 MCORPSENM(mtmp) = rndmonnum();
2108 if (appear == EGG && !can_be_hatched(MCORPSENM(mtmp)))
2109 MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
2113 /* release monster from bag of tricks; return number of monsters created */
2115 bagotricks(bag, tipping, seencount)
2117 boolean tipping; /* caller emptying entire contents; affects shop handling */
2118 int *seencount; /* secondary output */
2122 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2123 impossible("bad bag o' tricks");
2124 } else if (bag->spe < 1) {
2125 /* if tipping known empty bag, give normal empty container message */
2127 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2129 pline1((tipping && bag->cknown) ? "
\82»
\82ê
\82Í
\8bó
\82¾
\81D" : nothing_happens);
2130 /* now known to be empty if sufficiently discovered */
2131 if (bag->dknown && objects[bag->otyp].oc_name_known)
2135 int creatcnt = 1, seecount = 0;
2137 consume_obj_charge(bag, !tipping);
2142 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2145 if (canspotmon(mtmp))
2148 } while (--creatcnt > 0);
2151 *seencount += seecount;
2153 makeknown(BAG_OF_TRICKS);
2154 } else if (!tipping) {
2156 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2158 pline1(!moncount ? nothing_happens : "
\89½
\82à
\8bN
\82«
\82È
\82©
\82Á
\82½
\82æ
\82¤
\82¾
\81D");