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. */
9 STATIC_VAR NEARDATA struct monst zeromonst;
11 /* this assumes that a human quest leader or nemesis is an archetype
12 of the corresponding role; that isn't so for some roles (tourist
13 for instance) but is for the priests and monks we use it for... */
14 #define quest_mon_represents_role(mptr, role_pm) \
15 (mptr->mlet == S_HUMAN && Role_if(role_pm) \
16 && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
18 STATIC_DCL boolean FDECL(uncommon, (int));
19 STATIC_DCL int FDECL(align_shift, (struct permonst *));
20 STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
21 STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
22 STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int));
23 STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
24 STATIC_DCL void FDECL(m_initweap, (struct monst *));
25 STATIC_DCL void FDECL(m_initinv, (struct monst *));
26 STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *, unsigned, coord *));
28 extern const int monstr[];
30 #define m_initsgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 3)
31 #define m_initlgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 10)
32 #define toostrong(monindx, lev) (monstr[monindx] > lev)
33 #define tooweak(monindx, lev) (monstr[monindx] < lev)
36 is_home_elemental(ptr)
39 if (ptr->mlet == S_ELEMENTAL)
40 switch (monsndx(ptr)) {
41 case PM_AIR_ELEMENTAL:
42 return Is_airlevel(&u.uz);
43 case PM_FIRE_ELEMENTAL:
44 return Is_firelevel(&u.uz);
45 case PM_EARTH_ELEMENTAL:
46 return Is_earthlevel(&u.uz);
47 case PM_WATER_ELEMENTAL:
48 return Is_waterlevel(&u.uz);
54 * Return true if the given monster cannot exist on this elemental level.
60 if (ptr->mlet == S_ELEMENTAL) {
61 return (boolean) !is_home_elemental(ptr);
62 } else if (Is_earthlevel(&u.uz)) {
63 /* no restrictions? */
64 } else if (Is_waterlevel(&u.uz)) {
65 /* just monsters that can swim */
68 } else if (Is_firelevel(&u.uz)) {
69 if (!pm_resistance(ptr, MR_FIRE))
71 } else if (Is_airlevel(&u.uz)) {
72 if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
73 && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
79 /* make a group just like mtmp */
81 m_initgrp(mtmp, x, y, n)
82 register struct monst *mtmp;
86 register int cnt = rnd(n);
88 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
89 /* There is an unresolved problem with several people finding that
90 * the game hangs eating CPU; if interrupted and restored, the level
91 * will be filled with monsters. Of those reports giving system type,
92 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
93 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
94 * problem went away for him and another reporter-to-newsgroup
95 * after adding this debugging code. This has almost got to be a
96 * compiler bug, but until somebody tracks it down and gets it fixed,
97 * might as well go with the "but it went away when I tried to find
103 debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
104 cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
106 /* Tuning: cut down on swarming at low character levels [mrs] */
107 cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
108 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
109 if (cnt != (cnttmp / cntdiv)) {
110 pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
111 (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
116 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
126 if (peace_minded(mtmp->data))
128 /* Don't create groups of peaceful monsters since they'll get
129 * in our way. If the monster has a percentage chance so some
130 * are peaceful and some are not, the result will just be a
133 if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
134 mon = makemon(mtmp->data, mm.x, mm.y, NO_MM_FLAGS);
136 mon->mpeaceful = FALSE;
139 /* Undo the second peace_minded() check in makemon(); if the
140 * monster turned out to be peaceful the first time we
141 * didn't create it at all; we don't want a second check.
150 m_initthrow(mtmp, otyp, oquan)
154 register struct obj *otmp;
156 otmp = mksobj(otyp, TRUE, FALSE);
157 otmp->quan = (long) rn1(oquan, 3);
158 otmp->owt = weight(otmp);
159 if (otyp == ORCISH_ARROW)
160 otmp->opoisoned = TRUE;
161 (void) mpickobj(mtmp, otmp);
166 register struct monst *mtmp;
168 register struct permonst *ptr = mtmp->data;
169 register int mm = monsndx(ptr);
171 int bias, spe2, w1, w2;
173 if (Is_rogue_level(&u.uz))
176 * First a few special cases:
177 * giants get a boulder to throw sometimes
179 * kobolds get darts to throw
180 * centaurs get some sort of bow & arrows or bolts
181 * soldiers get all sorts of things
182 * kops get clubs & cream pies.
187 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
190 if (is_mercenary(ptr)) {
196 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
197 w2 = rn2(2) ? DAGGER : KNIFE;
199 w1 = rn2(2) ? SPEAR : SHORT_SWORD;
202 w1 = rn2(2) ? FLAIL : MACE;
205 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
208 case PM_WATCH_CAPTAIN:
209 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
219 (void) mongets(mtmp, w1);
220 if (!w2 && w1 != DAGGER && !rn2(4))
223 (void) mongets(mtmp, w2);
224 } else if (is_elf(ptr)) {
227 rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
229 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
231 (void) mongets(mtmp, ELVEN_BOOTS);
233 (void) mongets(mtmp, ELVEN_DAGGER);
237 (void) mongets(mtmp, ELVEN_SHIELD);
239 (void) mongets(mtmp, ELVEN_SHORT_SWORD);
240 (void) mongets(mtmp, ELVEN_BOW);
241 m_initthrow(mtmp, ELVEN_ARROW, 12);
244 (void) mongets(mtmp, ELVEN_BROADSWORD);
246 (void) mongets(mtmp, ELVEN_SHIELD);
250 (void) mongets(mtmp, ELVEN_SPEAR);
251 (void) mongets(mtmp, ELVEN_SHIELD);
255 if (mm == PM_ELVENKING) {
256 if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
257 (void) mongets(mtmp, PICK_AXE);
259 (void) mongets(mtmp, CRYSTAL_BALL);
261 } else if (ptr->msound == MS_PRIEST
262 || quest_mon_represents_role(ptr, PM_PRIEST)) {
263 otmp = mksobj(MACE, FALSE, FALSE);
268 (void) mpickobj(mtmp, otmp);
270 } else if (mm == PM_NINJA) { /* extra quest villains */
271 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
272 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
278 /* create minion stuff; can't use mongets */
279 otmp = mksobj(LONG_SWORD, FALSE, FALSE);
281 /* maybe make it special */
282 if (!rn2(20) || is_lord(ptr))
284 artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
286 otmp->oerodeproof = TRUE;
288 otmp->spe = max(otmp->spe, spe2);
289 (void) mpickobj(mtmp, otmp);
291 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
294 otmp->cursed = FALSE;
295 otmp->oerodeproof = TRUE;
297 (void) mpickobj(mtmp, otmp);
302 if (mm == PM_HOBBIT) {
305 (void) mongets(mtmp, DAGGER);
308 (void) mongets(mtmp, ELVEN_DAGGER);
311 (void) mongets(mtmp, SLING);
315 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
317 (void) mongets(mtmp, DWARVISH_CLOAK);
318 } else if (is_dwarf(ptr)) {
320 (void) mongets(mtmp, DWARVISH_CLOAK);
322 (void) mongets(mtmp, IRON_SHOES);
324 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
325 /* note: you can't use a mattock with a shield */
327 (void) mongets(mtmp, DWARVISH_MATTOCK);
329 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
330 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
332 (void) mongets(mtmp, DWARVISH_IRON_HELM);
334 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
336 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
341 /* create Keystone Kops with cream pies to
342 throw. As suggested by KAA. [MRS] */
344 m_initthrow(mtmp, CREAM_PIE, 2);
346 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
350 (void) mongets(mtmp, ORCISH_HELM);
351 switch ((mm != PM_ORC_CAPTAIN) ? mm
352 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
355 (void) mongets(mtmp, SCIMITAR);
357 (void) mongets(mtmp, ORCISH_SHIELD);
359 (void) mongets(mtmp, KNIFE);
361 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
365 (void) mongets(mtmp, ORCISH_CLOAK);
367 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
369 (void) mongets(mtmp, IRON_SHOES);
371 (void) mongets(mtmp, ORCISH_BOW);
372 m_initthrow(mtmp, ORCISH_ARROW, 12);
375 (void) mongets(mtmp, URUK_HAI_SHIELD);
378 if (mm != PM_ORC_SHAMAN && rn2(2))
379 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
385 if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
386 (void) mongets(mtmp, BATTLE_AXE);
388 (void) mongets(mtmp, CLUB);
394 (void) mongets(mtmp, RANSEUR);
397 (void) mongets(mtmp, PARTISAN);
400 (void) mongets(mtmp, GLAIVE);
403 (void) mongets(mtmp, SPETUM);
409 m_initthrow(mtmp, DART, 12);
414 if (ptr == &mons[PM_FOREST_CENTAUR]) {
415 (void) mongets(mtmp, BOW);
416 m_initthrow(mtmp, ARROW, 12);
418 (void) mongets(mtmp, CROSSBOW);
419 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
424 (void) mongets(mtmp, KNIFE);
425 (void) mongets(mtmp, LONG_SWORD);
429 (void) mongets(mtmp, LEATHER_ARMOR);
431 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
434 if (mm == PM_SALAMANDER)
436 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
441 (void) mongets(mtmp, BULLWHIP);
442 (void) mongets(mtmp, BROADSWORD);
445 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
447 case PM_HORNED_DEVIL:
448 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
451 (void) mongets(mtmp, WAN_STRIKING);
454 (void) mongets(mtmp, FLAIL);
457 /* prevent djinn and mail daemons from leaving objects when
465 * Now the general case, some chance of getting some type
466 * of weapon for "normal" monsters. Certain special types
467 * of monsters will get a bonus chance or different selections.
469 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
470 switch (rnd(14 - (2 * bias))) {
472 if (strongmonst(ptr))
473 (void) mongets(mtmp, BATTLE_AXE);
475 m_initthrow(mtmp, DART, 12);
478 if (strongmonst(ptr))
479 (void) mongets(mtmp, TWO_HANDED_SWORD);
481 (void) mongets(mtmp, CROSSBOW);
482 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
486 (void) mongets(mtmp, BOW);
487 m_initthrow(mtmp, ARROW, 12);
490 if (strongmonst(ptr))
491 (void) mongets(mtmp, LONG_SWORD);
493 m_initthrow(mtmp, DAGGER, 3);
496 if (strongmonst(ptr))
497 (void) mongets(mtmp, LUCERN_HAMMER);
499 (void) mongets(mtmp, AKLYS);
507 if ((int) mtmp->m_lev > rn2(75))
508 (void) mongets(mtmp, rnd_offensive_item(mtmp));
512 * Makes up money for monster's inventory.
513 * This will change with silver & copper coins
516 mkmonmoney(mtmp, amount)
520 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
523 add_to_minv(mtmp, gold);
528 register struct monst *mtmp;
531 register struct obj *otmp;
532 register struct permonst *ptr = mtmp->data;
534 if (Is_rogue_level(&u.uz))
537 * Soldiers get armour & rations - armour approximates their ac.
538 * Nymphs may get mirror or potion of object detection.
542 if (is_mercenary(ptr)) {
545 switch (monsndx(ptr)) {
564 case PM_WATCH_CAPTAIN:
568 impossible("odd mercenary %d?", monsndx(ptr));
573 if (mac < -1 && rn2(5))
574 mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
575 : CRYSTAL_PLATE_MAIL);
576 else if (mac < 3 && rn2(5))
578 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
580 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
581 : STUDDED_LEATHER_ARMOR);
583 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
585 if (mac < 10 && rn2(3))
586 mac += 1 + mongets(mtmp, HELMET);
587 else if (mac < 10 && rn2(2))
588 mac += 1 + mongets(mtmp, DENTED_POT);
589 if (mac < 10 && rn2(3))
590 mac += 1 + mongets(mtmp, SMALL_SHIELD);
591 else if (mac < 10 && rn2(2))
592 mac += 2 + mongets(mtmp, LARGE_SHIELD);
593 if (mac < 10 && rn2(3))
594 mac += 1 + mongets(mtmp, LOW_BOOTS);
595 else if (mac < 10 && rn2(2))
596 mac += 2 + mongets(mtmp, HIGH_BOOTS);
597 if (mac < 10 && rn2(3))
598 mac += 1 + mongets(mtmp, LEATHER_GLOVES);
599 else if (mac < 10 && rn2(2))
600 mac += 1 + mongets(mtmp, LEATHER_CLOAK);
602 if (ptr != &mons[PM_GUARD] && ptr != &mons[PM_WATCHMAN]
603 && ptr != &mons[PM_WATCH_CAPTAIN]) {
605 (void) mongets(mtmp, K_RATION);
607 (void) mongets(mtmp, C_RATION);
608 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
609 (void) mongets(mtmp, BUGLE);
610 } else if (ptr == &mons[PM_WATCHMAN] && rn2(3))
611 (void) mongets(mtmp, TIN_WHISTLE);
612 } else if (ptr == &mons[PM_SHOPKEEPER]) {
613 (void) mongets(mtmp, SKELETON_KEY);
615 /* MAJOR fall through ... */
617 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
619 (void) mongets(mtmp, POT_EXTRA_HEALING);
621 (void) mongets(mtmp, POT_HEALING);
623 (void) mongets(mtmp, WAN_STRIKING);
625 } else if (ptr->msound == MS_PRIEST
626 || quest_mon_represents_role(ptr, PM_PRIEST)) {
627 (void) mongets(mtmp, rn2(7) ? ROBE
628 : rn2(3) ? CLOAK_OF_PROTECTION
629 : CLOAK_OF_MAGIC_RESISTANCE);
630 (void) mongets(mtmp, SMALL_SHIELD);
631 mkmonmoney(mtmp, (long) rn1(10, 20));
632 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
633 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
638 (void) mongets(mtmp, MIRROR);
640 (void) mongets(mtmp, POT_OBJECT_DETECTION);
643 if (ptr == &mons[PM_MINOTAUR]) {
644 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
645 (void) mongets(mtmp, WAN_DIGGING);
646 } else if (is_giant(ptr)) {
647 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
648 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
650 otmp->quan = (long) rn1(2, 3);
651 otmp->owt = weight(otmp);
652 (void) mpickobj(mtmp, otmp);
657 if (ptr == &mons[PM_NAZGUL]) {
658 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
660 (void) mpickobj(mtmp, otmp);
664 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
665 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
666 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
667 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
668 rn2(13) ? FALSE : TRUE);
672 otmp->oerodeproof = 1;
673 (void) mpickobj(mtmp, otmp);
678 (void) mongets(mtmp, MUMMY_WRAPPING);
682 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
683 otmp->spe = 1; /* flag for special box */
684 otmp->owt = weight(otmp);
685 (void) mpickobj(mtmp, otmp);
689 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
692 /* moved here from m_initweap() because these don't
693 have AT_WEAP so m_initweap() is not called for them */
694 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
695 (void) mongets(mtmp, SPEAR);
696 } else if (ptr == &mons[PM_ASMODEUS]) {
697 (void) mongets(mtmp, WAN_COLD);
698 (void) mongets(mtmp, WAN_FIRE);
702 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
703 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
705 otmp->owt = weight(otmp);
706 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
707 begin_burn(otmp, FALSE);
714 /* ordinary soldiers rarely have access to magic (or gold :-) */
715 if (ptr == &mons[PM_SOLDIER] && rn2(13))
718 if ((int) mtmp->m_lev > rn2(50))
719 (void) mongets(mtmp, rnd_defensive_item(mtmp));
720 if ((int) mtmp->m_lev > rn2(100))
721 (void) mongets(mtmp, rnd_misc_item(mtmp));
722 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
724 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
727 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
731 xchar x, y; /* clone's preferred location or 0 (near mon) */
736 /* may be too weak or have been extinguished for population control */
737 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
738 return (struct monst *) 0;
743 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
744 return (struct monst *) 0;
745 } else if (!isok(x, y)) {
746 return (struct monst *) 0; /* paranoia */
750 if (MON_AT(mm.x, mm.y)) {
751 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
752 return (struct monst *) 0;
756 *m2 = *mon; /* copy condition of old monster */
757 m2->mextra = (struct mextra *) 0;
760 m2->m_id = context.ident++;
762 m2->m_id = context.ident++; /* ident overflowed */
767 m2->minvent = (struct obj *) 0; /* objects don't clone */
768 m2->mleashed = FALSE;
769 /* Max HP the same, but current HP halved for both. The caller
770 * might want to override this by halving the max HP also.
771 * When current HP is odd, the original keeps the extra point.
773 m2->mhpmax = mon->mhpmax;
774 m2->mhp = mon->mhp / 2;
777 /* since shopkeepers and guards will only be cloned if they've been
778 * polymorphed away from their original forms, the clone doesn't have
779 * room for the extra information. we also don't want two shopkeepers
780 * around for the same shop.
787 m2->ispriest = FALSE;
788 place_monster(m2, m2->mx, m2->my);
789 if (emits_light(m2->data))
790 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
792 if (has_mname(mon)) {
793 m2 = christen_monst(m2, MNAME(mon));
794 } else if (mon->isshk) {
795 m2 = christen_monst(m2, shkname(mon));
798 /* not all clones caused by player are tame or peaceful */
799 if (!context.mon_moving) {
801 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
802 else if (mon->mpeaceful)
803 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
806 newsym(m2->mx, m2->my); /* display the new monster */
811 *(EMIN(m2)) = *(EMIN(mon));
813 /* because m2 is a copy of mon it is tame but not init'ed.
814 * however, tamedog will not re-tame a tame dog, so m2
815 * must be made non-tame to get initialized properly.
818 if (tamedog(m2, (struct obj *) 0)) {
819 *(EDOG(m2)) = *(EDOG(mon));
829 * Propagate a species
831 * Once a certain number of monsters are created, don't create any more
832 * at random (i.e. make them extinct). The previous (3.2) behavior was
833 * to do this when a certain number had _died_, which didn't make
836 * Returns FALSE propagation unsuccessful
837 * TRUE propagation successful
840 propagate(mndx, tally, ghostly)
846 uchar lim = mbirth_limit(mndx);
847 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
849 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
851 /* if it's unique, don't ever make it again */
852 if (mons[mndx].geno & G_UNIQ)
853 mvitals[mndx].mvflags |= G_EXTINCT;
855 if (mvitals[mndx].born < 255 && tally
856 && (!ghostly || (ghostly && result)))
857 mvitals[mndx].born++;
858 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
859 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
861 debugpline1("Automatically extinguished %s.",
862 makeplural(mons[mndx].mname));
864 mvitals[mndx].mvflags |= G_EXTINCT;
865 reset_rndmonst(mndx);
870 /* amount of HP to lose from level drain (or gain from Stormbringer) */
875 struct permonst *ptr = mon->data;
876 int hp = rnd(8); /* default is d8 */
878 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
880 /* draining usually won't be applicable for these critters */
881 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
882 } else if (ptr->mlevel > 49) {
883 /* arbitrary; such monsters won't be involved in draining anyway */
884 hp = 4 + rnd(4); /* 5..8 */
885 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
886 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
888 hp = 4 + rn2(5); /* 4..8 */
889 } else if (!mon->m_lev) {
890 /* level 0 monsters use 1d4 instead of Nd8 */
896 /* set up a new monster's initial level and hit points;
897 used by newcham() as well as by makemon() */
903 struct permonst *ptr = &mons[mndx];
905 mon->m_lev = adj_lev(ptr);
907 mon->mhpmax = mon->mhp = golemhp(mndx);
908 } else if (is_rider(ptr)) {
909 /* we want low HP, but a high mlevel so they can attack well */
910 mon->mhpmax = mon->mhp = d(10, 8);
911 } else if (ptr->mlevel > 49) {
912 /* "special" fixed hp monster
913 * the hit points are encoded in the mlevel in a somewhat strange
914 * way to fit in the 50..127 positive range of a signed character
915 * above the 1..49 that indicate "normal" monster levels */
916 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
917 mon->m_lev = mon->mhp / 4; /* approximation */
918 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
920 mon->mhpmax = mon->mhp =
921 (int) (In_endgame(&u.uz)
923 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
924 } else if (!mon->m_lev) {
925 mon->mhpmax = mon->mhp = rnd(4);
927 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
928 if (is_home_elemental(ptr))
929 mon->mhpmax = (mon->mhp *= 3);
936 struct mextra *mextra;
938 mextra = (struct mextra *) alloc(sizeof(struct mextra));
945 mextra->mcorpsenm = NON_PM;
950 makemon_rnd_goodpos(mon, gpflags, cc)
960 nx = rn1(COLNO - 3, 2);
962 good = (!in_mklev && cansee(nx,ny)) ? FALSE
963 : goodpos(nx, ny, mon, gpflags);
964 } while ((++tryct < 50) && !good);
967 /* else go through all map positions, twice, first round
968 ignoring positions in sight, and pick first good one.
969 skip first round if we're in special level loader or blind */
973 int bl = (in_mklev || Blind) ? 1 : 0;
975 for ( ; bl < 2; bl++) {
976 for (dx = 0; dx < COLNO; dx++)
977 for (dy = 0; dy < ROWNO; dy++) {
978 nx = ((dx + xofs) % (COLNO - 1)) + 1;
979 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
980 if (bl == 0 && cansee(nx,ny))
982 if (goodpos(nx, ny, mon, gpflags))
985 if (bl == 0 && (!mon || mon->data->mmove)) {
986 /* all map positions are visible (or not good),
987 try to pick something logical */
988 if (dnstair.sx && !rn2(2)) {
991 } else if (upstair.sx && !rn2(2)) {
994 } else if (dnladder.sx && !rn2(2)) {
997 } else if (upladder.sx && !rn2(2)) {
1001 if (goodpos(nx, ny, mon, gpflags))
1015 * called with [x,y] = coordinates;
1016 * [0,0] means anyplace
1017 * [u.ux,u.uy] means: near player (if !in_mklev)
1019 * In case we make a monster group, only return the one at [x,y].
1022 makemon(ptr, x, y, mmflags)
1023 register struct permonst *ptr;
1027 register struct monst *mtmp;
1028 int mndx, mcham, ct, mitem;
1029 boolean anymon = (!ptr);
1030 boolean byyou = (x == u.ux && y == u.uy);
1031 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1032 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1033 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1035 /* if caller wants random location, do it here */
1036 if (x == 0 && y == 0) {
1038 struct monst fakemon;
1040 cc.x = cc.y = 0; /* lint suppression */
1041 fakemon.data = ptr; /* set up for goodpos */
1042 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1044 return (struct monst *) 0;
1047 } else if (byyou && !in_mklev) {
1050 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1054 return (struct monst *) 0;
1057 /* Does monster already exist at the position? */
1059 if ((mmflags & MM_ADJACENTOK) != 0) {
1061 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1065 return (struct monst *) 0;
1067 return (struct monst *) 0;
1071 mndx = monsndx(ptr);
1072 /* if you are to make a specific monster and it has
1073 already been genocided, return */
1074 if (mvitals[mndx].mvflags & G_GENOD)
1075 return (struct monst *) 0;
1076 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1077 debugpline1("Explicitly creating extinct monster %s.",
1081 /* make a random (common) monster that can survive here.
1082 * (the special levels ask for random monsters at specific
1083 * positions, causing mass drowning on the medusa level,
1086 int tryct = 0; /* maybe there are no good choices */
1087 struct monst fakemon;
1090 if (!(ptr = rndmonst())) {
1091 debugpline0("Warning: no monster.");
1092 return (struct monst *) 0; /* no more monsters! */
1094 fakemon.data = ptr; /* set up for goodpos */
1095 } while (++tryct <= 50
1096 /* in Sokoban, don't accept a giant on first try;
1097 after that, boulder carriers are fair game */
1098 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1099 || !goodpos(x, y, &fakemon, gpflags)));
1100 mndx = monsndx(ptr);
1102 (void) propagate(mndx, countbirth, FALSE);
1104 *mtmp = zeromonst; /* clear all entries in structure */
1106 if (mmflags & MM_EGD)
1108 if (mmflags & MM_EPRI)
1110 if (mmflags & MM_ESHK)
1112 if (mmflags & MM_EMIN)
1114 if (mmflags & MM_EDOG)
1119 mtmp->m_id = context.ident++;
1121 mtmp->m_id = context.ident++; /* ident overflowed */
1122 set_mon_data(mtmp, ptr, 0);
1123 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1124 quest_status.leader_m_id = mtmp->m_id;
1127 /* set up level and hit points */
1128 newmonhp(mtmp, mndx);
1131 mtmp->female = TRUE;
1132 else if (is_male(ptr))
1133 mtmp->female = FALSE;
1134 /* leader and nemesis gender is usually hardcoded in mons[],
1135 but for ones which can be random, it has already been chosen
1136 (in role_init(), for possible use by the quest pager code) */
1137 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1138 mtmp->female = quest_status.ldrgend;
1139 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1140 mtmp->female = quest_status.nemgend;
1142 mtmp->female = rn2(2); /* ignored for neuters */
1144 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1145 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1146 /* quest leader and nemesis both know about all trap types */
1147 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1148 mtmp->mtrapseen = ~0;
1150 place_monster(mtmp, x, y);
1151 mtmp->mcansee = mtmp->mcanmove = TRUE;
1152 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1154 switch (ptr->mlet) {
1156 set_mimic_sym(mtmp);
1162 (void) mkobj_at(0, x, y, TRUE);
1163 (void) hideunder(mtmp);
1167 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1168 mtmp->perminvis = TRUE;
1169 mtmp->minvis = TRUE;
1173 (void) hideunder(mtmp);
1176 mtmp->msleeping = 1;
1180 if (rn2(5) && !u.uhave.amulet)
1181 mtmp->msleeping = 1;
1184 if (Race_if(PM_ELF))
1185 mtmp->mpeaceful = FALSE;
1188 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1189 mtmp->mpeaceful = TRUE;
1192 if (Inhell && is_bat(ptr))
1193 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1196 if ((ct = emits_light(mtmp->data)) > 0)
1197 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1198 monst_to_any(mtmp));
1199 mitem = 0; /* extra inventory item for this monster */
1201 if (mndx == PM_VLAD_THE_IMPALER)
1202 mitem = CANDELABRUM_OF_INVOCATION;
1203 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1204 if ((mcham = pm_to_cham(mndx)) != NON_PM) {
1205 /* this is a shapechanger after all */
1206 if (Protection_from_shape_changers
1207 || mndx == PM_VLAD_THE_IMPALER) {
1208 ; /* stuck in its natural form (NON_PM) */
1211 /* Note: shapechanger's initial form used to be
1212 chosen here with rndmonst(), yielding a monster
1213 which was appropriate to the level's difficulty
1214 but ignored the changer's usual type selection
1215 so would be inappropriate for vampshifters.
1216 Let newcham() pick the shape. */
1217 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1218 allow_minvent = FALSE;
1220 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1222 context.no_of_wizards++;
1223 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1225 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1226 mtmp = christen_monst(mtmp, rndghostname());
1227 } else if (mndx == PM_CROESUS) {
1228 mitem = TWO_HANDED_SWORD;
1229 } else if (ptr->msound == MS_NEMESIS) {
1230 mitem = BELL_OF_OPENING;
1231 } else if (mndx == PM_PESTILENCE) {
1232 mitem = POT_SICKNESS;
1234 if (mitem && allow_minvent)
1235 (void) mongets(mtmp, mitem);
1238 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1239 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1240 && !u.uhave.amulet && rn2(5))
1241 mtmp->msleeping = TRUE;
1244 newsym(mtmp->mx, mtmp->my);
1248 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1249 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1251 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1252 mtmp->mpeaceful = mtmp->mtame = FALSE;
1255 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1257 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1259 if (mndx == PM_LONG_WORM
1260 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1263 /* we can now create worms with tails - 11/91 */
1264 initworm(mtmp, rn2(5));
1265 if (count_wsegs(mtmp))
1266 place_worm_tail_randomly(mtmp, x, y);
1268 /* it's possible to create an ordinary monster of some special
1269 types; make sure their extended data is initialized to
1270 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1271 (when they're specified, caller intends to handle this itself) */
1272 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1273 ? !(mmflags & (MM_EPRI | MM_EMIN))
1274 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1279 mtmp->isminion = 1; /* make priest be a roamer */
1280 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1281 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1282 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1286 set_malign(mtmp); /* having finished peaceful changes */
1288 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1289 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1290 } else if (ptr->geno & G_LGROUP) {
1292 m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1294 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1298 if (allow_minvent) {
1300 m_initweap(mtmp); /* equip with weapons / armor */
1301 m_initinv(mtmp); /* add on a few special items incl. more armor */
1302 m_dowear(mtmp, TRUE);
1304 /* no initial inventory is allowed */
1306 discard_minvent(mtmp);
1307 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1309 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1310 if (ptr->mflags3 & M3_WAITFORU)
1311 mtmp->mstrategy |= STRAT_WAITFORU;
1312 if (ptr->mflags3 & M3_CLOSE)
1313 mtmp->mstrategy |= STRAT_CLOSE;
1314 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1315 mtmp->mstrategy |= STRAT_APPEARMSG;
1319 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1328 /* assert(MAXMONNO < 255); */
1329 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1332 /* used for wand/scroll/spell of create monster */
1333 /* returns TRUE iff you know monsters have been created */
1335 create_critters(cnt, mptr, neverask)
1337 struct permonst *mptr; /* usually null; used for confused reading */
1343 boolean known = FALSE;
1344 boolean ask = (wizard && !neverask);
1348 if (create_particular()) {
1352 ask = FALSE; /* ESC will shut off prompting */
1355 /* if in water, try to encourage an aquatic monster
1356 by finding and then specifying another wet location */
1357 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1360 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1361 if (mon && canspotmon(mon))
1371 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1373 if (mvitals[mndx].mvflags & G_GONE)
1376 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1378 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1382 * shift the probability of a monster's generation by
1383 * comparing the dungeon alignment and monster alignment.
1384 * return an integer in the range of 0-5.
1388 register struct permonst *ptr;
1390 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1391 static NEARDATA s_level *lev;
1392 register int alshift;
1394 if (oldmoves != moves) {
1395 lev = Is_special(&u.uz);
1398 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1399 default: /* just in case */
1404 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1407 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1410 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1416 static NEARDATA struct {
1418 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1419 } rndmonst_state = { -1, { 0 } };
1421 /* select a random monster type */
1425 register struct permonst *ptr;
1426 register int mndx, ct;
1428 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1431 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1432 int zlevel, minmlev, maxmlev;
1436 rndmonst_state.choice_count = 0;
1437 /* look for first common monster */
1438 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1439 if (!uncommon(mndx))
1441 rndmonst_state.mchoices[mndx] = 0;
1443 if (mndx == SPECIAL_PM) {
1444 /* evidently they've all been exterminated */
1445 debugpline0("rndmonst: no common mons!");
1446 return (struct permonst *) 0;
1447 } /* else `mndx' now ready for use below */
1448 zlevel = level_difficulty();
1449 /* determine the level of the weakest monster to make. */
1450 minmlev = zlevel / 6;
1451 /* determine the level of the strongest monster to make. */
1452 maxmlev = (zlevel + u.ulevel) / 2;
1453 upper = Is_rogue_level(&u.uz);
1454 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1457 * Find out how many monsters exist in the range we have selected.
1459 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1461 rndmonst_state.mchoices[mndx] = 0;
1462 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1464 if (upper && !isupper(def_monsyms[(int) (ptr->mlet)].sym))
1466 if (elemlevel && wrong_elem_type(ptr))
1470 if (Inhell && (ptr->geno & G_NOHELL))
1472 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1473 if (ct < 0 || ct > 127)
1474 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1475 rndmonst_state.choice_count += ct;
1476 rndmonst_state.mchoices[mndx] = (char) ct;
1479 * Possible modification: if choice_count is "too low",
1480 * expand minmlev..maxmlev range and try again.
1482 } /* choice_count+mchoices[] recalc */
1484 if (rndmonst_state.choice_count <= 0) {
1485 /* maybe no common mons left, or all are too weak or too strong */
1486 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1487 return (struct permonst *) 0;
1491 * Now, select a monster at random.
1493 ct = rnd(rndmonst_state.choice_count);
1494 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1495 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1498 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1499 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1500 return (struct permonst *) 0;
1505 /* called when you change level (experience or dungeon depth) or when
1506 monster species can no longer be created (genocide or extinction) */
1508 reset_rndmonst(mndx)
1509 int mndx; /* particular species that can no longer be created */
1511 /* cached selection info is out of date */
1512 if (mndx == NON_PM) {
1513 rndmonst_state.choice_count = -1; /* full recalc needed */
1514 } else if (mndx < SPECIAL_PM) {
1515 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1516 rndmonst_state.mchoices[mndx] = 0;
1517 } /* note: safe to ignore extinction of unique monsters */
1520 /* decide whether it's ok to generate a candidate monster by mkclass() */
1522 mk_gen_ok(mndx, mvflagsmask, genomask)
1523 int mndx, mvflagsmask, genomask;
1525 struct permonst *ptr = &mons[mndx];
1527 if (mvitals[mndx].mvflags & mvflagsmask)
1529 if (ptr->geno & genomask)
1531 if (is_placeholder(ptr))
1534 /* special levels might ask for random demon type; reject this one */
1535 if (ptr == &mons[PM_MAIL_DAEMON])
1541 /* Make one of the multiple types of a given monster class.
1542 * The second parameter specifies a special casing bit mask
1543 * to allow the normal genesis masks to be deactivated.
1544 * Returns Null if no monsters in that class can be made.
1551 register int first, last, num = 0;
1552 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1554 maxmlev = level_difficulty() >> 1;
1555 if (class < 1 || class >= MAXMCLASSES) {
1556 impossible("mkclass called with bad class!");
1557 return (struct permonst *) 0;
1559 /* Assumption #1: monsters of a given class are contiguous in the
1562 for (first = LOW_PM; first < SPECIAL_PM; first++)
1563 if (mons[first].mlet == class)
1565 if (first == SPECIAL_PM)
1566 return (struct permonst *) 0;
1568 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1569 if (mk_gen_ok(last, G_GONE, mask)) {
1571 if (num && toostrong(last, maxmlev)
1572 && monstr[last] != monstr[last - 1] && rn2(2))
1574 num += mons[last].geno & G_FREQ;
1577 return (struct permonst *) 0;
1579 /* Assumption #2: monsters of a given class are presented in ascending
1580 * order of strength.
1582 for (num = rnd(num); num > 0; first++)
1583 if (mk_gen_ok(first, G_GONE, mask)) {
1584 /* skew towards lower value monsters at lower exp. levels */
1585 num -= mons[first].geno & G_FREQ;
1586 if (num && adj_lev(&mons[first]) > (u.ulevel * 2)) {
1587 /* but not when multiple monsters are same level */
1588 if (mons[first].mlevel != mons[first + 1].mlevel)
1592 first--; /* correct an off-by-one error */
1594 return &mons[first];
1597 /* like mkclass(), but excludes difficulty considerations; used when
1598 player with polycontrol picks a class instead of a specific type;
1599 genocided types are avoided but extinct ones are acceptable; we don't
1600 check polyok() here--caller accepts some choices !polyok() would reject */
1605 register int first, last, num = 0;
1607 for (first = LOW_PM; first < SPECIAL_PM; first++)
1608 if (mons[first].mlet == class)
1610 if (first == SPECIAL_PM)
1613 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1614 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1615 num += mons[last].geno & G_FREQ;
1619 for (num = rnd(num); num > 0; first++)
1620 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1621 num -= mons[first].geno & G_FREQ;
1622 first--; /* correct an off-by-one error */
1627 /* adjust strength of monsters based on u.uz and u.ulevel */
1630 register struct permonst *ptr;
1634 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1635 /* does not depend on other strengths, but does get stronger
1636 * every time he is killed
1638 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1644 if ((tmp = ptr->mlevel) > 49)
1645 return 50; /* "special" demons/devils */
1646 tmp2 = (level_difficulty() - tmp);
1648 tmp--; /* if mlevel > u.uz decrement tmp */
1650 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1652 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1654 tmp += (tmp2 / 4); /* level as well */
1656 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1658 tmp2 = 49; /* hard upper limit */
1659 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1662 /* monster earned experience and will gain some hit points; it might also
1663 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1665 grow_up(mtmp, victim)
1666 struct monst *mtmp, *victim;
1668 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1669 struct permonst *ptr = mtmp->data;
1671 /* monster died after killing enemy but before calling this function */
1672 /* currently possible if killing a gas spore */
1674 return (struct permonst *) 0;
1676 /* note: none of the monsters with special hit point calculations
1677 have both little and big forms */
1678 oldtype = monsndx(ptr);
1679 newtype = little_to_big(oldtype);
1680 if (newtype == PM_PRIEST && mtmp->female)
1681 newtype = PM_PRIESTESS;
1683 /* growth limits differ depending on method of advancement */
1684 if (victim) { /* killed a monster */
1686 * The HP threshold is the maximum number of hit points for the
1687 * current level; once exceeded, a level will be gained.
1688 * Possible bug: if somehow the hit points are already higher
1689 * than that, monster will gain a level without any increase in HP.
1691 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1694 else if (is_golem(ptr)) /* strange creatures */
1695 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1696 else if (is_home_elemental(ptr))
1698 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1699 /* If they can grow up, be sure the level is high enough for that */
1700 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1701 lev_limit = (int) mons[newtype].mlevel;
1702 /* number of hit points to gain; unlike for the player, we put
1703 the limit at the bottom of the next level rather than the top */
1704 max_increase = rnd((int) victim->m_lev + 1);
1705 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1706 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1707 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1709 /* a gain level potion or wraith corpse; always go up a level
1710 unless already at maximum (49 is hard upper limit except
1711 for demon lords, who start at 50 and can't go any higher) */
1712 max_increase = cur_increase = rnd(8);
1713 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1714 lev_limit = 50; /* recalc below */
1717 mtmp->mhpmax += max_increase;
1718 mtmp->mhp += cur_increase;
1719 if (mtmp->mhpmax <= hp_threshold)
1720 return ptr; /* doesn't gain a level */
1722 if (is_mplayer(ptr))
1723 lev_limit = 30; /* same as player */
1724 else if (lev_limit < 5)
1725 lev_limit = 5; /* arbitrary */
1726 else if (lev_limit > 49)
1727 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1729 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1730 ptr = &mons[newtype];
1731 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1732 if (canspotmon(mtmp))
1734 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1735 an(ptr->mname), mhe(mtmp),
1736 nonliving(ptr) ? "expires" : "dies");
1738 pline("%s
\82ª
\90¬
\92·
\82µ
\82Ä%s
\82É
\82È
\82é
\82Æ%s
\82µ
\82Ü
\82Á
\82½
\81I", mon_nam(mtmp),
1740 nonliving(ptr) ? "
\8fÁ
\82¦
\82Ä" : "
\8e\80\82ñ
\82Å");
1742 set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
1744 return (struct permonst *) 0;
1745 } else if (canspotmon(mtmp)) {
1747 pline("%s %s %s.", Monnam(mtmp),
1748 humanoid(ptr) ? "becomes" : "grows up into",
1751 pline("%s
\82Í%s
\82É%s
\81D", Monnam(mtmp),
1753 humanoid(ptr) ? "
\82È
\82Á
\82½" : "
\90¬
\92·
\82µ
\82½");
1756 set_mon_data(mtmp, ptr, 1); /* preserve intrinsics */
1757 newsym(mtmp->mx, mtmp->my); /* color may change */
1758 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1761 if ((int) mtmp->m_lev > lev_limit) {
1762 mtmp->m_lev--; /* undo increment */
1763 /* HP might have been allowed to grow when it shouldn't */
1764 if (mtmp->mhpmax == hp_threshold + 1)
1767 if (mtmp->mhpmax > 50 * 8)
1768 mtmp->mhpmax = 50 * 8; /* absolute limit */
1769 if (mtmp->mhp > mtmp->mhpmax)
1770 mtmp->mhp = mtmp->mhpmax;
1777 register struct monst *mtmp;
1780 register struct obj *otmp;
1785 otmp = mksobj(otyp, TRUE, FALSE);
1787 if (mtmp->data->mlet == S_DEMON) {
1788 /* demons never get blessed objects */
1791 } else if (is_lminion(mtmp)) {
1792 /* lawful minions don't get cursed, bad, or rusting objects */
1793 otmp->cursed = FALSE;
1796 otmp->oerodeproof = TRUE;
1797 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1798 otmp->spe = (3 + rn2(4));
1801 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1804 otmp->lamplit = FALSE;
1805 otmp->blessed = otmp->cursed = FALSE;
1806 } else if (otmp->otyp == BELL_OF_OPENING) {
1807 otmp->blessed = otmp->cursed = FALSE;
1808 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1809 otmp->blessed = FALSE;
1810 otmp->cursed = TRUE;
1813 /* leaders don't tolerate inferior quality battle gear */
1814 if (is_prince(mtmp->data)) {
1815 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1817 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1822 (void) mpickobj(mtmp, otmp); /* might free otmp */
1833 case PM_STRAW_GOLEM:
1835 case PM_PAPER_GOLEM:
1839 case PM_LEATHER_GOLEM:
1845 case PM_FLESH_GOLEM:
1849 case PM_STONE_GOLEM:
1851 case PM_GLASS_GOLEM:
1861 * Alignment vs. yours determines monster's attitude to you.
1862 * (Some "animal" types are co-aligned, but also hungry.)
1866 register struct permonst *ptr;
1868 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1870 if (always_peaceful(ptr))
1872 if (always_hostile(ptr))
1874 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1876 if (ptr->msound == MS_NEMESIS)
1879 if (race_peaceful(ptr))
1881 if (race_hostile(ptr))
1884 /* the monster is hostile if its alignment is different from the
1886 if (sgn(mal) != sgn(ual))
1889 /* Negative monster hostile to player with Amulet. */
1890 if (mal < A_NEUTRAL && u.uhave.amulet)
1893 /* minions are hostile to players that have strayed at all */
1895 return (boolean) (u.ualign.record >= 0);
1897 /* Last case: a chance of a co-aligned monster being
1898 * hostile. This chance is greater if the player has strayed
1899 * (u.ualign.record negative) or the monster is not strongly aligned.
1901 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
1903 && !!rn2(2 + abs(mal)));
1906 /* Set malign to have the proper effect on player alignment if monster is
1907 * killed. Negative numbers mean it's bad to kill this monster; positive
1908 * numbers mean it's good. Since there are more hostile monsters than
1909 * peaceful monsters, the penalty for killing a peaceful monster should be
1910 * greater than the bonus for killing a hostile monster to maintain balance.
1912 * it's bad to kill peaceful monsters, potentially worse to kill always-
1913 * peaceful monsters;
1914 * it's never bad to kill a hostile monster, although it may not be good.
1920 schar mal = mtmp->data->maligntyp;
1923 if (mtmp->ispriest || mtmp->isminion) {
1924 /* some monsters have individual alignments; check them */
1925 if (mtmp->ispriest && EPRI(mtmp))
1926 mal = EPRI(mtmp)->shralign;
1927 else if (mtmp->isminion && EMIN(mtmp))
1928 mal = EMIN(mtmp)->min_align;
1929 /* unless alignment is none, set mal to -5,0,5 */
1930 /* (see align.h for valid aligntyp values) */
1935 coaligned = (sgn(mal) == sgn(u.ualign.type));
1936 if (mtmp->data->msound == MS_LEADER) {
1938 } else if (mal == A_NONE) {
1939 if (mtmp->mpeaceful)
1942 mtmp->malign = 20; /* really hostile */
1943 } else if (always_peaceful(mtmp->data)) {
1944 int absmal = abs(mal);
1945 if (mtmp->mpeaceful)
1946 mtmp->malign = -3 * max(5, absmal);
1948 mtmp->malign = 3 * max(5, absmal); /* renegade */
1949 } else if (always_hostile(mtmp->data)) {
1950 int absmal = abs(mal);
1954 mtmp->malign = max(5, absmal);
1955 } else if (coaligned) {
1956 int absmal = abs(mal);
1957 if (mtmp->mpeaceful)
1958 mtmp->malign = -3 * max(3, absmal);
1960 mtmp->malign = max(3, absmal);
1961 } else /* not coaligned and therefore hostile */
1962 mtmp->malign = abs(mal);
1965 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
1971 mtmp->mextra = newmextra();
1972 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
1975 /* release monster's mcorpsenm field; basically a no-op */
1980 if (has_mcorpsenm(mtmp))
1981 MCORPSENM(mtmp) = NON_PM;
1984 static NEARDATA char syms[] = {
1985 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
1986 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
1987 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
1988 S_MIMIC_DEF, S_MIMIC_DEF,
1993 register struct monst *mtmp;
1995 int typ, roomno, rt;
1996 unsigned appear, ap_type;
2005 typ = levl[mx][my].typ;
2006 /* only valid for INSIDE of room */
2007 roomno = levl[mx][my].roomno - ROOMOFFSET;
2009 rt = rooms[roomno].rtype;
2010 #ifdef SPECIALIZATION
2011 else if (IS_ROOM(typ))
2012 rt = OROOM, roomno = 0;
2015 rt = 0; /* roomno < 0 case for GCC_WARN */
2017 if (OBJ_AT(mx, my)) {
2018 ap_type = M_AP_OBJECT;
2019 appear = level.objects[mx][my]->otyp;
2020 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2021 ap_type = M_AP_FURNITURE;
2023 * If there is a wall to the left that connects to this
2024 * location, then the mimic mimics a horizontal closed door.
2025 * This does not allow doors to be in corners of rooms.
2026 * Since rogue has no closed doors, mimic a wall there
2027 * (yes, mimics can end up on this level by various means).
2029 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2030 || levl[mx - 1][my].typ == TLCORNER
2031 || levl[mx - 1][my].typ == TRWALL
2032 || levl[mx - 1][my].typ == BLCORNER
2033 || levl[mx - 1][my].typ == TDWALL
2034 || levl[mx - 1][my].typ == CROSSWALL
2035 || levl[mx - 1][my].typ == TUWALL))
2036 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2038 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2039 if (!mtmp->minvis || See_invisible)
2040 block_point(mx, my); /* vision */
2041 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2042 ap_type = M_AP_OBJECT;
2044 } else if (roomno < 0 && !t_at(mx, my)) {
2045 ap_type = M_AP_OBJECT;
2047 if (!mtmp->minvis || See_invisible)
2048 block_point(mx, my); /* vision */
2049 } else if (rt == ZOO || rt == VAULT) {
2050 ap_type = M_AP_OBJECT;
2051 appear = GOLD_PIECE;
2052 } else if (rt == DELPHI) {
2054 ap_type = M_AP_OBJECT;
2057 ap_type = M_AP_FURNITURE;
2058 appear = S_fountain;
2060 } else if (rt == TEMPLE) {
2061 ap_type = M_AP_FURNITURE;
2064 * We won't bother with beehives, morgues, barracks, throne rooms
2065 * since they shouldn't contain too many mimics anyway...
2067 } else if (rt >= SHOPBASE) {
2068 s_sym = get_shop_item(rt - SHOPBASE);
2070 ap_type = M_AP_OBJECT;
2073 if (s_sym == RANDOM_CLASS)
2074 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2078 s_sym = syms[rn2((int) sizeof(syms))];
2080 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2081 ap_type = M_AP_FURNITURE;
2082 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2084 ap_type = M_AP_OBJECT;
2085 if (s_sym == S_MIMIC_DEF) {
2086 appear = STRANGE_OBJECT;
2087 } else if (s_sym == COIN_CLASS) {
2088 appear = GOLD_PIECE;
2090 otmp = mkobj((char) s_sym, FALSE);
2091 appear = otmp->otyp;
2092 /* make sure container contents are free'ed */
2093 obfree(otmp, (struct obj *) 0);
2097 mtmp->m_ap_type = ap_type;
2098 mtmp->mappearance = appear;
2099 if (ap_type == M_AP_OBJECT && (appear == STATUE || appear == CORPSE
2100 || appear == FIGURINE || appear == EGG)) {
2102 MCORPSENM(mtmp) = rndmonnum();
2103 if (appear == EGG && !can_be_hatched(MCORPSENM(mtmp)))
2104 MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
2108 /* release monster from bag of tricks; return number of monsters created */
2110 bagotricks(bag, tipping, seencount)
2112 boolean tipping; /* caller emptying entire contents; affects shop handling */
2113 int *seencount; /* secondary output */
2117 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2118 impossible("bad bag o' tricks");
2119 } else if (bag->spe < 1) {
2120 /* if tipping known empty bag, give normal empty container message */
2121 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2122 /* now known to be empty if sufficiently discovered */
2123 if (bag->dknown && objects[bag->otyp].oc_name_known)
2127 int creatcnt = 1, seecount = 0;
2129 consume_obj_charge(bag, !tipping);
2134 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2137 if (canspotmon(mtmp))
2140 } while (--creatcnt > 0);
2143 *seencount += seecount;
2145 makeknown(BAG_OF_TRICKS);
2146 } else if (!tipping) {
2147 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");