1 /* NetHack 3.6 makemon.c $NHDT-Date: 1450451931 2015/12/18 15:18:51 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.106 $ */
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 nhUse(mac
); /* suppress 'dead increment' from static analyzer */
604 if (ptr
!= &mons
[PM_GUARD
] && ptr
!= &mons
[PM_WATCHMAN
]
605 && ptr
!= &mons
[PM_WATCH_CAPTAIN
]) {
607 (void) mongets(mtmp
, K_RATION
);
609 (void) mongets(mtmp
, C_RATION
);
610 if (ptr
!= &mons
[PM_SOLDIER
] && !rn2(3))
611 (void) mongets(mtmp
, BUGLE
);
612 } else if (ptr
== &mons
[PM_WATCHMAN
] && rn2(3))
613 (void) mongets(mtmp
, TIN_WHISTLE
);
614 } else if (ptr
== &mons
[PM_SHOPKEEPER
]) {
615 (void) mongets(mtmp
, SKELETON_KEY
);
617 /* MAJOR fall through ... */
619 (void) mongets(mtmp
, WAN_MAGIC_MISSILE
);
621 (void) mongets(mtmp
, POT_EXTRA_HEALING
);
623 (void) mongets(mtmp
, POT_HEALING
);
625 (void) mongets(mtmp
, WAN_STRIKING
);
627 } else if (ptr
->msound
== MS_PRIEST
628 || quest_mon_represents_role(ptr
, PM_PRIEST
)) {
629 (void) mongets(mtmp
, rn2(7) ? ROBE
630 : rn2(3) ? CLOAK_OF_PROTECTION
631 : CLOAK_OF_MAGIC_RESISTANCE
);
632 (void) mongets(mtmp
, SMALL_SHIELD
);
633 mkmonmoney(mtmp
, (long) rn1(10, 20));
634 } else if (quest_mon_represents_role(ptr
, PM_MONK
)) {
635 (void) mongets(mtmp
, rn2(11) ? ROBE
: CLOAK_OF_MAGIC_RESISTANCE
);
640 (void) mongets(mtmp
, MIRROR
);
642 (void) mongets(mtmp
, POT_OBJECT_DETECTION
);
645 if (ptr
== &mons
[PM_MINOTAUR
]) {
646 if (!rn2(3) || (in_mklev
&& Is_earthlevel(&u
.uz
)))
647 (void) mongets(mtmp
, WAN_DIGGING
);
648 } else if (is_giant(ptr
)) {
649 for (cnt
= rn2((int) (mtmp
->m_lev
/ 2)); cnt
; cnt
--) {
650 otmp
= mksobj(rnd_class(DILITHIUM_CRYSTAL
, LUCKSTONE
- 1),
652 otmp
->quan
= (long) rn1(2, 3);
653 otmp
->owt
= weight(otmp
);
654 (void) mpickobj(mtmp
, otmp
);
659 if (ptr
== &mons
[PM_NAZGUL
]) {
660 otmp
= mksobj(RIN_INVISIBILITY
, FALSE
, FALSE
);
662 (void) mpickobj(mtmp
, otmp
);
666 if (ptr
== &mons
[PM_MASTER_LICH
] && !rn2(13))
667 (void) mongets(mtmp
, (rn2(7) ? ATHAME
: WAN_NOTHING
));
668 else if (ptr
== &mons
[PM_ARCH_LICH
] && !rn2(3)) {
669 otmp
= mksobj(rn2(3) ? ATHAME
: QUARTERSTAFF
, TRUE
,
670 rn2(13) ? FALSE
: TRUE
);
674 otmp
->oerodeproof
= 1;
675 (void) mpickobj(mtmp
, otmp
);
680 (void) mongets(mtmp
, MUMMY_WRAPPING
);
684 otmp
= mksobj(LARGE_BOX
, FALSE
, FALSE
);
685 otmp
->spe
= 1; /* flag for special box */
686 otmp
->owt
= weight(otmp
);
687 (void) mpickobj(mtmp
, otmp
);
691 mkmonmoney(mtmp
, (long) d(level_difficulty(), 30));
694 /* moved here from m_initweap() because these don't
695 have AT_WEAP so m_initweap() is not called for them */
696 if (ptr
== &mons
[PM_ICE_DEVIL
] && !rn2(4)) {
697 (void) mongets(mtmp
, SPEAR
);
698 } else if (ptr
== &mons
[PM_ASMODEUS
]) {
699 (void) mongets(mtmp
, WAN_COLD
);
700 (void) mongets(mtmp
, WAN_FIRE
);
704 if (!rn2((In_mines(&u
.uz
) && in_mklev
) ? 20 : 60)) {
705 otmp
= mksobj(rn2(4) ? TALLOW_CANDLE
: WAX_CANDLE
, TRUE
, FALSE
);
707 otmp
->owt
= weight(otmp
);
708 if (!mpickobj(mtmp
, otmp
) && !levl
[mtmp
->mx
][mtmp
->my
].lit
)
709 begin_burn(otmp
, FALSE
);
716 /* ordinary soldiers rarely have access to magic (or gold :-) */
717 if (ptr
== &mons
[PM_SOLDIER
] && rn2(13))
720 if ((int) mtmp
->m_lev
> rn2(50))
721 (void) mongets(mtmp
, rnd_defensive_item(mtmp
));
722 if ((int) mtmp
->m_lev
> rn2(100))
723 (void) mongets(mtmp
, rnd_misc_item(mtmp
));
724 if (likes_gold(ptr
) && !findgold(mtmp
->minvent
) && !rn2(5))
726 (long) d(level_difficulty(), mtmp
->minvent
? 5 : 10));
729 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
733 xchar x
, y
; /* clone's preferred location or 0 (near mon) */
738 /* may be too weak or have been extinguished for population control */
739 if (mon
->mhp
<= 1 || (mvitals
[monsndx(mon
->data
)].mvflags
& G_EXTINCT
))
740 return (struct monst
*) 0;
745 if (!enexto(&mm
, mm
.x
, mm
.y
, mon
->data
) || MON_AT(mm
.x
, mm
.y
))
746 return (struct monst
*) 0;
747 } else if (!isok(x
, y
)) {
748 return (struct monst
*) 0; /* paranoia */
752 if (MON_AT(mm
.x
, mm
.y
)) {
753 if (!enexto(&mm
, mm
.x
, mm
.y
, mon
->data
) || MON_AT(mm
.x
, mm
.y
))
754 return (struct monst
*) 0;
758 *m2
= *mon
; /* copy condition of old monster */
759 m2
->mextra
= (struct mextra
*) 0;
762 m2
->m_id
= context
.ident
++;
764 m2
->m_id
= context
.ident
++; /* ident overflowed */
769 m2
->minvent
= (struct obj
*) 0; /* objects don't clone */
770 m2
->mleashed
= FALSE
;
771 /* Max HP the same, but current HP halved for both. The caller
772 * might want to override this by halving the max HP also.
773 * When current HP is odd, the original keeps the extra point.
775 m2
->mhpmax
= mon
->mhpmax
;
776 m2
->mhp
= mon
->mhp
/ 2;
779 /* since shopkeepers and guards will only be cloned if they've been
780 * polymorphed away from their original forms, the clone doesn't have
781 * room for the extra information. we also don't want two shopkeepers
782 * around for the same shop.
789 m2
->ispriest
= FALSE
;
790 place_monster(m2
, m2
->mx
, m2
->my
);
791 if (emits_light(m2
->data
))
792 new_light_source(m2
->mx
, m2
->my
, emits_light(m2
->data
), LS_MONSTER
,
794 if (has_mname(mon
)) {
795 m2
= christen_monst(m2
, MNAME(mon
));
796 } else if (mon
->isshk
) {
797 m2
= christen_monst(m2
, shkname(mon
));
800 /* not all clones caused by player are tame or peaceful */
801 if (!context
.mon_moving
) {
803 m2
->mtame
= rn2(max(2 + u
.uluck
, 2)) ? mon
->mtame
: 0;
804 else if (mon
->mpeaceful
)
805 m2
->mpeaceful
= rn2(max(2 + u
.uluck
, 2)) ? 1 : 0;
808 newsym(m2
->mx
, m2
->my
); /* display the new monster */
813 *(EMIN(m2
)) = *(EMIN(mon
));
815 /* because m2 is a copy of mon it is tame but not init'ed.
816 * however, tamedog will not re-tame a tame dog, so m2
817 * must be made non-tame to get initialized properly.
820 if (tamedog(m2
, (struct obj
*) 0)) {
821 *(EDOG(m2
)) = *(EDOG(mon
));
831 * Propagate a species
833 * Once a certain number of monsters are created, don't create any more
834 * at random (i.e. make them extinct). The previous (3.2) behavior was
835 * to do this when a certain number had _died_, which didn't make
838 * Returns FALSE propagation unsuccessful
839 * TRUE propagation successful
842 propagate(mndx
, tally
, ghostly
)
848 uchar lim
= mbirth_limit(mndx
);
849 boolean gone
= (mvitals
[mndx
].mvflags
& G_GONE
) != 0; /* geno'd|extinct */
851 result
= (((int) mvitals
[mndx
].born
< lim
) && !gone
) ? TRUE
: FALSE
;
853 /* if it's unique, don't ever make it again */
854 if (mons
[mndx
].geno
& G_UNIQ
)
855 mvitals
[mndx
].mvflags
|= G_EXTINCT
;
857 if (mvitals
[mndx
].born
< 255 && tally
858 && (!ghostly
|| (ghostly
&& result
)))
859 mvitals
[mndx
].born
++;
860 if ((int) mvitals
[mndx
].born
>= lim
&& !(mons
[mndx
].geno
& G_NOGEN
)
861 && !(mvitals
[mndx
].mvflags
& G_EXTINCT
)) {
863 debugpline1("Automatically extinguished %s.",
864 makeplural(mons
[mndx
].mname
));
866 mvitals
[mndx
].mvflags
|= G_EXTINCT
;
867 reset_rndmonst(mndx
);
872 /* amount of HP to lose from level drain (or gain from Stormbringer) */
877 struct permonst
*ptr
= mon
->data
;
878 int hp
= rnd(8); /* default is d8 */
880 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
882 /* draining usually won't be applicable for these critters */
883 hp
= golemhp(monsndx(ptr
)) / (int) ptr
->mlevel
;
884 } else if (ptr
->mlevel
> 49) {
885 /* arbitrary; such monsters won't be involved in draining anyway */
886 hp
= 4 + rnd(4); /* 5..8 */
887 } else if (ptr
->mlet
== S_DRAGON
&& monsndx(ptr
) >= PM_GRAY_DRAGON
) {
888 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
890 hp
= 4 + rn2(5); /* 4..8 */
891 } else if (!mon
->m_lev
) {
892 /* level 0 monsters use 1d4 instead of Nd8 */
898 /* set up a new monster's initial level and hit points;
899 used by newcham() as well as by makemon() */
905 struct permonst
*ptr
= &mons
[mndx
];
907 mon
->m_lev
= adj_lev(ptr
);
909 mon
->mhpmax
= mon
->mhp
= golemhp(mndx
);
910 } else if (is_rider(ptr
)) {
911 /* we want low HP, but a high mlevel so they can attack well */
912 mon
->mhpmax
= mon
->mhp
= d(10, 8);
913 } else if (ptr
->mlevel
> 49) {
914 /* "special" fixed hp monster
915 * the hit points are encoded in the mlevel in a somewhat strange
916 * way to fit in the 50..127 positive range of a signed character
917 * above the 1..49 that indicate "normal" monster levels */
918 mon
->mhpmax
= mon
->mhp
= 2 * (ptr
->mlevel
- 6);
919 mon
->m_lev
= mon
->mhp
/ 4; /* approximation */
920 } else if (ptr
->mlet
== S_DRAGON
&& mndx
>= PM_GRAY_DRAGON
) {
922 mon
->mhpmax
= mon
->mhp
=
923 (int) (In_endgame(&u
.uz
)
925 : (4 * mon
->m_lev
+ d((int) mon
->m_lev
, 4)));
926 } else if (!mon
->m_lev
) {
927 mon
->mhpmax
= mon
->mhp
= rnd(4);
929 mon
->mhpmax
= mon
->mhp
= d((int) mon
->m_lev
, 8);
930 if (is_home_elemental(ptr
))
931 mon
->mhpmax
= (mon
->mhp
*= 3);
938 struct mextra
*mextra
;
940 mextra
= (struct mextra
*) alloc(sizeof(struct mextra
));
947 mextra
->mcorpsenm
= NON_PM
;
952 makemon_rnd_goodpos(mon
, gpflags
, cc
)
962 nx
= rn1(COLNO
- 3, 2);
964 good
= (!in_mklev
&& cansee(nx
,ny
)) ? FALSE
965 : goodpos(nx
, ny
, mon
, gpflags
);
966 } while ((++tryct
< 50) && !good
);
969 /* else go through all map positions, twice, first round
970 ignoring positions in sight, and pick first good one.
971 skip first round if we're in special level loader or blind */
975 int bl
= (in_mklev
|| Blind
) ? 1 : 0;
977 for ( ; bl
< 2; bl
++) {
978 for (dx
= 0; dx
< COLNO
; dx
++)
979 for (dy
= 0; dy
< ROWNO
; dy
++) {
980 nx
= ((dx
+ xofs
) % (COLNO
- 1)) + 1;
981 ny
= ((dy
+ yofs
) % (ROWNO
- 1)) + 1;
982 if (bl
== 0 && cansee(nx
,ny
))
984 if (goodpos(nx
, ny
, mon
, gpflags
))
987 if (bl
== 0 && (!mon
|| mon
->data
->mmove
)) {
988 /* all map positions are visible (or not good),
989 try to pick something logical */
990 if (dnstair
.sx
&& !rn2(2)) {
993 } else if (upstair
.sx
&& !rn2(2)) {
996 } else if (dnladder
.sx
&& !rn2(2)) {
999 } else if (upladder
.sx
&& !rn2(2)) {
1003 if (goodpos(nx
, ny
, mon
, gpflags
))
1017 * called with [x,y] = coordinates;
1018 * [0,0] means anyplace
1019 * [u.ux,u.uy] means: near player (if !in_mklev)
1021 * In case we make a monster group, only return the one at [x,y].
1024 makemon(ptr
, x
, y
, mmflags
)
1025 register struct permonst
*ptr
;
1029 register struct monst
*mtmp
;
1030 int mndx
, mcham
, ct
, mitem
;
1031 boolean anymon
= (!ptr
);
1032 boolean byyou
= (x
== u
.ux
&& y
== u
.uy
);
1033 boolean allow_minvent
= ((mmflags
& NO_MINVENT
) == 0);
1034 boolean countbirth
= ((mmflags
& MM_NOCOUNTBIRTH
) == 0);
1035 unsigned gpflags
= (mmflags
& MM_IGNOREWATER
) ? MM_IGNOREWATER
: 0;
1037 /* if caller wants random location, do it here */
1038 if (x
== 0 && y
== 0) {
1040 struct monst fakemon
;
1042 cc
.x
= cc
.y
= 0; /* lint suppression */
1043 fakemon
.data
= ptr
; /* set up for goodpos */
1044 if (!makemon_rnd_goodpos(ptr
? &fakemon
: (struct monst
*)0,
1046 return (struct monst
*) 0;
1049 } else if (byyou
&& !in_mklev
) {
1052 if (enexto_core(&bypos
, u
.ux
, u
.uy
, ptr
, gpflags
)) {
1056 return (struct monst
*) 0;
1059 /* Does monster already exist at the position? */
1061 if ((mmflags
& MM_ADJACENTOK
) != 0) {
1063 if (enexto_core(&bypos
, x
, y
, ptr
, gpflags
)) {
1067 return (struct monst
*) 0;
1069 return (struct monst
*) 0;
1073 mndx
= monsndx(ptr
);
1074 /* if you are to make a specific monster and it has
1075 already been genocided, return */
1076 if (mvitals
[mndx
].mvflags
& G_GENOD
)
1077 return (struct monst
*) 0;
1078 if (wizard
&& (mvitals
[mndx
].mvflags
& G_EXTINCT
)) {
1079 debugpline1("Explicitly creating extinct monster %s.",
1083 /* make a random (common) monster that can survive here.
1084 * (the special levels ask for random monsters at specific
1085 * positions, causing mass drowning on the medusa level,
1088 int tryct
= 0; /* maybe there are no good choices */
1089 struct monst fakemon
;
1092 if (!(ptr
= rndmonst())) {
1093 debugpline0("Warning: no monster.");
1094 return (struct monst
*) 0; /* no more monsters! */
1096 fakemon
.data
= ptr
; /* set up for goodpos */
1097 } while (++tryct
<= 50
1098 /* in Sokoban, don't accept a giant on first try;
1099 after that, boulder carriers are fair game */
1100 && ((tryct
== 1 && throws_rocks(ptr
) && In_sokoban(&u
.uz
))
1101 || !goodpos(x
, y
, &fakemon
, gpflags
)));
1102 mndx
= monsndx(ptr
);
1104 (void) propagate(mndx
, countbirth
, FALSE
);
1106 *mtmp
= zeromonst
; /* clear all entries in structure */
1108 if (mmflags
& MM_EGD
)
1110 if (mmflags
& MM_EPRI
)
1112 if (mmflags
& MM_ESHK
)
1114 if (mmflags
& MM_EMIN
)
1116 if (mmflags
& MM_EDOG
)
1121 mtmp
->m_id
= context
.ident
++;
1123 mtmp
->m_id
= context
.ident
++; /* ident overflowed */
1124 set_mon_data(mtmp
, ptr
, 0);
1125 if (ptr
->msound
== MS_LEADER
&& quest_info(MS_LEADER
) == mndx
)
1126 quest_status
.leader_m_id
= mtmp
->m_id
;
1129 /* set up level and hit points */
1130 newmonhp(mtmp
, mndx
);
1133 mtmp
->female
= TRUE
;
1134 else if (is_male(ptr
))
1135 mtmp
->female
= FALSE
;
1136 /* leader and nemesis gender is usually hardcoded in mons[],
1137 but for ones which can be random, it has already been chosen
1138 (in role_init(), for possible use by the quest pager code) */
1139 else if (ptr
->msound
== MS_LEADER
&& quest_info(MS_LEADER
) == mndx
)
1140 mtmp
->female
= quest_status
.ldrgend
;
1141 else if (ptr
->msound
== MS_NEMESIS
&& quest_info(MS_NEMESIS
) == mndx
)
1142 mtmp
->female
= quest_status
.nemgend
;
1144 mtmp
->female
= rn2(2); /* ignored for neuters */
1146 if (In_sokoban(&u
.uz
) && !mindless(ptr
)) /* know about traps here */
1147 mtmp
->mtrapseen
= (1L << (PIT
- 1)) | (1L << (HOLE
- 1));
1148 /* quest leader and nemesis both know about all trap types */
1149 if (ptr
->msound
== MS_LEADER
|| ptr
->msound
== MS_NEMESIS
)
1150 mtmp
->mtrapseen
= ~0;
1152 place_monster(mtmp
, x
, y
);
1153 mtmp
->mcansee
= mtmp
->mcanmove
= TRUE
;
1154 mtmp
->mpeaceful
= (mmflags
& MM_ANGRY
) ? FALSE
: peace_minded(ptr
);
1156 switch (ptr
->mlet
) {
1158 set_mimic_sym(mtmp
);
1164 (void) mkobj_at(0, x
, y
, TRUE
);
1165 (void) hideunder(mtmp
);
1169 if (mndx
== PM_STALKER
|| mndx
== PM_BLACK_LIGHT
) {
1170 mtmp
->perminvis
= TRUE
;
1171 mtmp
->minvis
= TRUE
;
1175 (void) hideunder(mtmp
);
1178 mtmp
->msleeping
= 1;
1182 if (rn2(5) && !u
.uhave
.amulet
)
1183 mtmp
->msleeping
= 1;
1186 if (Race_if(PM_ELF
))
1187 mtmp
->mpeaceful
= FALSE
;
1190 if (is_unicorn(ptr
) && sgn(u
.ualign
.type
) == sgn(ptr
->maligntyp
))
1191 mtmp
->mpeaceful
= TRUE
;
1194 if (Inhell
&& is_bat(ptr
))
1195 mon_adjust_speed(mtmp
, 2, (struct obj
*) 0);
1198 if ((ct
= emits_light(mtmp
->data
)) > 0)
1199 new_light_source(mtmp
->mx
, mtmp
->my
, ct
, LS_MONSTER
,
1200 monst_to_any(mtmp
));
1201 mitem
= 0; /* extra inventory item for this monster */
1203 if (mndx
== PM_VLAD_THE_IMPALER
)
1204 mitem
= CANDELABRUM_OF_INVOCATION
;
1205 mtmp
->cham
= NON_PM
; /* default is "not a shapechanger" */
1206 if ((mcham
= pm_to_cham(mndx
)) != NON_PM
) {
1207 /* this is a shapechanger after all */
1208 if (Protection_from_shape_changers
1209 || mndx
== PM_VLAD_THE_IMPALER
) {
1210 ; /* stuck in its natural form (NON_PM) */
1213 /* Note: shapechanger's initial form used to be
1214 chosen here with rndmonst(), yielding a monster
1215 which was appropriate to the level's difficulty
1216 but ignored the changer's usual type selection
1217 so would be inappropriate for vampshifters.
1218 Let newcham() pick the shape. */
1219 if (newcham(mtmp
, (struct permonst
*) 0, FALSE
, FALSE
))
1220 allow_minvent
= FALSE
;
1222 } else if (mndx
== PM_WIZARD_OF_YENDOR
) {
1224 context
.no_of_wizards
++;
1225 if (context
.no_of_wizards
== 1 && Is_earthlevel(&u
.uz
))
1227 } else if (mndx
== PM_GHOST
&& !(mmflags
& MM_NONAME
)) {
1228 mtmp
= christen_monst(mtmp
, rndghostname());
1229 } else if (mndx
== PM_CROESUS
) {
1230 mitem
= TWO_HANDED_SWORD
;
1231 } else if (ptr
->msound
== MS_NEMESIS
) {
1232 mitem
= BELL_OF_OPENING
;
1233 } else if (mndx
== PM_PESTILENCE
) {
1234 mitem
= POT_SICKNESS
;
1236 if (mitem
&& allow_minvent
)
1237 (void) mongets(mtmp
, mitem
);
1240 if ((is_ndemon(ptr
) || mndx
== PM_WUMPUS
1241 || mndx
== PM_LONG_WORM
|| mndx
== PM_GIANT_EEL
)
1242 && !u
.uhave
.amulet
&& rn2(5))
1243 mtmp
->msleeping
= TRUE
;
1246 newsym(mtmp
->mx
, mtmp
->my
);
1250 if (is_dprince(ptr
) && ptr
->msound
== MS_BRIBE
) {
1251 mtmp
->mpeaceful
= mtmp
->minvis
= mtmp
->perminvis
= 1;
1253 if (uwep
&& uwep
->oartifact
== ART_EXCALIBUR
)
1254 mtmp
->mpeaceful
= mtmp
->mtame
= FALSE
;
1257 if (mndx
== PM_LONG_WORM
&& (mtmp
->wormno
= get_wormno()) != 0)
1259 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1261 if (mndx
== PM_LONG_WORM
1262 && (mtmp
->wormno
= get_wormno(), mtmp
->wormno
!= 0))
1265 /* we can now create worms with tails - 11/91 */
1266 initworm(mtmp
, rn2(5));
1267 if (count_wsegs(mtmp
))
1268 place_worm_tail_randomly(mtmp
, x
, y
);
1270 /* it's possible to create an ordinary monster of some special
1271 types; make sure their extended data is initialized to
1272 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1273 (when they're specified, caller intends to handle this itself) */
1274 if ((mndx
== PM_ALIGNED_PRIEST
|| mndx
== PM_HIGH_PRIEST
)
1275 ? !(mmflags
& (MM_EPRI
| MM_EMIN
))
1276 : (mndx
== PM_ANGEL
&& !(mmflags
& MM_EMIN
) && !rn2(3))) {
1281 mtmp
->isminion
= 1; /* make priest be a roamer */
1282 eminp
->min_align
= rn2(3) - 1; /* no A_NONE */
1283 eminp
->renegade
= (boolean
) ((mmflags
& MM_ANGRY
) ? 1 : !rn2(3));
1284 mtmp
->mpeaceful
= (eminp
->min_align
== u
.ualign
.type
)
1288 set_malign(mtmp
); /* having finished peaceful changes */
1290 if ((ptr
->geno
& G_SGROUP
) && rn2(2)) {
1291 m_initsgrp(mtmp
, mtmp
->mx
, mtmp
->my
);
1292 } else if (ptr
->geno
& G_LGROUP
) {
1294 m_initlgrp(mtmp
, mtmp
->mx
, mtmp
->my
);
1296 m_initsgrp(mtmp
, mtmp
->mx
, mtmp
->my
);
1300 if (allow_minvent
) {
1302 m_initweap(mtmp
); /* equip with weapons / armor */
1303 m_initinv(mtmp
); /* add on a few special items incl. more armor */
1304 m_dowear(mtmp
, TRUE
);
1306 /* no initial inventory is allowed */
1308 discard_minvent(mtmp
);
1309 mtmp
->minvent
= (struct obj
*) 0; /* caller expects this */
1311 if (ptr
->mflags3
&& !(mmflags
& MM_NOWAIT
)) {
1312 if (ptr
->mflags3
& M3_WAITFORU
)
1313 mtmp
->mstrategy
|= STRAT_WAITFORU
;
1314 if (ptr
->mflags3
& M3_CLOSE
)
1315 mtmp
->mstrategy
|= STRAT_CLOSE
;
1316 if (ptr
->mflags3
& (M3_WAITMASK
| M3_COVETOUS
))
1317 mtmp
->mstrategy
|= STRAT_APPEARMSG
;
1321 newsym(mtmp
->mx
, mtmp
->my
); /* make sure the mon shows up */
1330 /* assert(MAXMONNO < 255); */
1331 return (mndx
== PM_NAZGUL
? 9 : mndx
== PM_ERINYS
? 3 : MAXMONNO
);
1334 /* used for wand/scroll/spell of create monster */
1335 /* returns TRUE iff you know monsters have been created */
1337 create_critters(cnt
, mptr
, neverask
)
1339 struct permonst
*mptr
; /* usually null; used for confused reading */
1345 boolean known
= FALSE
;
1346 boolean ask
= (wizard
&& !neverask
);
1350 if (create_particular()) {
1354 ask
= FALSE
; /* ESC will shut off prompting */
1357 /* if in water, try to encourage an aquatic monster
1358 by finding and then specifying another wet location */
1359 if (!mptr
&& u
.uinwater
&& enexto(&c
, x
, y
, &mons
[PM_GIANT_EEL
]))
1362 mon
= makemon(mptr
, x
, y
, NO_MM_FLAGS
);
1363 if (mon
&& canspotmon(mon
))
1373 if (mons
[mndx
].geno
& (G_NOGEN
| G_UNIQ
))
1375 if (mvitals
[mndx
].mvflags
& G_GONE
)
1378 return (boolean
) (mons
[mndx
].maligntyp
> A_NEUTRAL
);
1380 return (boolean
) ((mons
[mndx
].geno
& G_HELL
) != 0);
1384 * shift the probability of a monster's generation by
1385 * comparing the dungeon alignment and monster alignment.
1386 * return an integer in the range of 0-5.
1390 register struct permonst
*ptr
;
1392 static NEARDATA
long oldmoves
= 0L; /* != 1, starting value of moves */
1393 static NEARDATA s_level
*lev
;
1394 register int alshift
;
1396 if (oldmoves
!= moves
) {
1397 lev
= Is_special(&u
.uz
);
1400 switch ((lev
) ? lev
->flags
.align
: dungeons
[u
.uz
.dnum
].flags
.align
) {
1401 default: /* just in case */
1406 alshift
= (ptr
->maligntyp
+ 20) / (2 * ALIGNWEIGHT
);
1409 alshift
= (20 - abs(ptr
->maligntyp
)) / ALIGNWEIGHT
;
1412 alshift
= (-(ptr
->maligntyp
- 20)) / (2 * ALIGNWEIGHT
);
1418 static NEARDATA
struct {
1420 char mchoices
[SPECIAL_PM
]; /* value range is 0..127 */
1421 } rndmonst_state
= { -1, { 0 } };
1423 /* select a random monster type */
1427 register struct permonst
*ptr
;
1428 register int mndx
, ct
;
1430 if (u
.uz
.dnum
== quest_dnum
&& rn2(7) && (ptr
= qt_montype()) != 0)
1433 if (rndmonst_state
.choice_count
< 0) { /* need to recalculate */
1434 int zlevel
, minmlev
, maxmlev
;
1438 rndmonst_state
.choice_count
= 0;
1439 /* look for first common monster */
1440 for (mndx
= LOW_PM
; mndx
< SPECIAL_PM
; mndx
++) {
1441 if (!uncommon(mndx
))
1443 rndmonst_state
.mchoices
[mndx
] = 0;
1445 if (mndx
== SPECIAL_PM
) {
1446 /* evidently they've all been exterminated */
1447 debugpline0("rndmonst: no common mons!");
1448 return (struct permonst
*) 0;
1449 } /* else `mndx' now ready for use below */
1450 zlevel
= level_difficulty();
1451 /* determine the level of the weakest monster to make. */
1452 minmlev
= zlevel
/ 6;
1453 /* determine the level of the strongest monster to make. */
1454 maxmlev
= (zlevel
+ u
.ulevel
) / 2;
1455 upper
= Is_rogue_level(&u
.uz
);
1456 elemlevel
= In_endgame(&u
.uz
) && !Is_astralevel(&u
.uz
);
1459 * Find out how many monsters exist in the range we have selected.
1461 for ( ; mndx
< SPECIAL_PM
; mndx
++) { /* (`mndx' initialized above) */
1463 rndmonst_state
.mchoices
[mndx
] = 0;
1464 if (tooweak(mndx
, minmlev
) || toostrong(mndx
, maxmlev
))
1466 if (upper
&& !isupper(def_monsyms
[(int) (ptr
->mlet
)].sym
))
1468 if (elemlevel
&& wrong_elem_type(ptr
))
1472 if (Inhell
&& (ptr
->geno
& G_NOHELL
))
1474 ct
= (int) (ptr
->geno
& G_FREQ
) + align_shift(ptr
);
1475 if (ct
< 0 || ct
> 127)
1476 panic("rndmonst: bad count [#%d: %d]", mndx
, ct
);
1477 rndmonst_state
.choice_count
+= ct
;
1478 rndmonst_state
.mchoices
[mndx
] = (char) ct
;
1481 * Possible modification: if choice_count is "too low",
1482 * expand minmlev..maxmlev range and try again.
1484 } /* choice_count+mchoices[] recalc */
1486 if (rndmonst_state
.choice_count
<= 0) {
1487 /* maybe no common mons left, or all are too weak or too strong */
1488 debugpline1("rndmonst: choice_count=%d", rndmonst_state
.choice_count
);
1489 return (struct permonst
*) 0;
1493 * Now, select a monster at random.
1495 ct
= rnd(rndmonst_state
.choice_count
);
1496 for (mndx
= LOW_PM
; mndx
< SPECIAL_PM
; mndx
++)
1497 if ((ct
-= (int) rndmonst_state
.mchoices
[mndx
]) <= 0)
1500 if (mndx
== SPECIAL_PM
|| uncommon(mndx
)) { /* shouldn't happen */
1501 impossible("rndmonst: bad `mndx' [#%d]", mndx
);
1502 return (struct permonst
*) 0;
1507 /* called when you change level (experience or dungeon depth) or when
1508 monster species can no longer be created (genocide or extinction) */
1510 reset_rndmonst(mndx
)
1511 int mndx
; /* particular species that can no longer be created */
1513 /* cached selection info is out of date */
1514 if (mndx
== NON_PM
) {
1515 rndmonst_state
.choice_count
= -1; /* full recalc needed */
1516 } else if (mndx
< SPECIAL_PM
) {
1517 rndmonst_state
.choice_count
-= rndmonst_state
.mchoices
[mndx
];
1518 rndmonst_state
.mchoices
[mndx
] = 0;
1519 } /* note: safe to ignore extinction of unique monsters */
1522 /* decide whether it's ok to generate a candidate monster by mkclass() */
1524 mk_gen_ok(mndx
, mvflagsmask
, genomask
)
1525 int mndx
, mvflagsmask
, genomask
;
1527 struct permonst
*ptr
= &mons
[mndx
];
1529 if (mvitals
[mndx
].mvflags
& mvflagsmask
)
1531 if (ptr
->geno
& genomask
)
1533 if (is_placeholder(ptr
))
1536 /* special levels might ask for random demon type; reject this one */
1537 if (ptr
== &mons
[PM_MAIL_DAEMON
])
1543 /* Make one of the multiple types of a given monster class.
1544 * The second parameter specifies a special casing bit mask
1545 * to allow the normal genesis masks to be deactivated.
1546 * Returns Null if no monsters in that class can be made.
1553 register int first
, last
, num
= 0;
1554 int maxmlev
, mask
= (G_NOGEN
| G_UNIQ
) & ~spc
;
1556 maxmlev
= level_difficulty() >> 1;
1557 if (class < 1 || class >= MAXMCLASSES
) {
1558 impossible("mkclass called with bad class!");
1559 return (struct permonst
*) 0;
1561 /* Assumption #1: monsters of a given class are contiguous in the
1564 for (first
= LOW_PM
; first
< SPECIAL_PM
; first
++)
1565 if (mons
[first
].mlet
== class)
1567 if (first
== SPECIAL_PM
)
1568 return (struct permonst
*) 0;
1570 for (last
= first
; last
< SPECIAL_PM
&& mons
[last
].mlet
== class; last
++)
1571 if (mk_gen_ok(last
, G_GONE
, mask
)) {
1573 if (num
&& toostrong(last
, maxmlev
)
1574 && monstr
[last
] != monstr
[last
- 1] && rn2(2))
1576 num
+= mons
[last
].geno
& G_FREQ
;
1579 return (struct permonst
*) 0;
1581 /* Assumption #2: monsters of a given class are presented in ascending
1582 * order of strength.
1584 for (num
= rnd(num
); num
> 0; first
++)
1585 if (mk_gen_ok(first
, G_GONE
, mask
)) {
1586 /* skew towards lower value monsters at lower exp. levels */
1587 num
-= mons
[first
].geno
& G_FREQ
;
1588 if (num
&& adj_lev(&mons
[first
]) > (u
.ulevel
* 2)) {
1589 /* but not when multiple monsters are same level */
1590 if (mons
[first
].mlevel
!= mons
[first
+ 1].mlevel
)
1594 first
--; /* correct an off-by-one error */
1596 return &mons
[first
];
1599 /* like mkclass(), but excludes difficulty considerations; used when
1600 player with polycontrol picks a class instead of a specific type;
1601 genocided types are avoided but extinct ones are acceptable; we don't
1602 check polyok() here--caller accepts some choices !polyok() would reject */
1607 register int first
, last
, num
= 0;
1609 for (first
= LOW_PM
; first
< SPECIAL_PM
; first
++)
1610 if (mons
[first
].mlet
== class)
1612 if (first
== SPECIAL_PM
)
1615 for (last
= first
; last
< SPECIAL_PM
&& mons
[last
].mlet
== class; last
++)
1616 if (mk_gen_ok(last
, G_GENOD
, (G_NOGEN
| G_UNIQ
)))
1617 num
+= mons
[last
].geno
& G_FREQ
;
1621 for (num
= rnd(num
); num
> 0; first
++)
1622 if (mk_gen_ok(first
, G_GENOD
, (G_NOGEN
| G_UNIQ
)))
1623 num
-= mons
[first
].geno
& G_FREQ
;
1624 first
--; /* correct an off-by-one error */
1629 /* adjust strength of monsters based on u.uz and u.ulevel */
1632 register struct permonst
*ptr
;
1636 if (ptr
== &mons
[PM_WIZARD_OF_YENDOR
]) {
1637 /* does not depend on other strengths, but does get stronger
1638 * every time he is killed
1640 tmp
= ptr
->mlevel
+ mvitals
[PM_WIZARD_OF_YENDOR
].died
;
1646 if ((tmp
= ptr
->mlevel
) > 49)
1647 return 50; /* "special" demons/devils */
1648 tmp2
= (level_difficulty() - tmp
);
1650 tmp
--; /* if mlevel > u.uz decrement tmp */
1652 tmp
+= (tmp2
/ 5); /* else increment 1 per five diff */
1654 tmp2
= (u
.ulevel
- ptr
->mlevel
); /* adjust vs. the player */
1656 tmp
+= (tmp2
/ 4); /* level as well */
1658 tmp2
= (3 * ((int) ptr
->mlevel
)) / 2; /* crude upper limit */
1660 tmp2
= 49; /* hard upper limit */
1661 return ((tmp
> tmp2
) ? tmp2
: (tmp
> 0 ? tmp
: 0)); /* 0 lower limit */
1664 /* monster earned experience and will gain some hit points; it might also
1665 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1667 grow_up(mtmp
, victim
)
1668 struct monst
*mtmp
, *victim
;
1670 int oldtype
, newtype
, max_increase
, cur_increase
, lev_limit
, hp_threshold
;
1671 struct permonst
*ptr
= mtmp
->data
;
1673 /* monster died after killing enemy but before calling this function */
1674 /* currently possible if killing a gas spore */
1676 return (struct permonst
*) 0;
1678 /* note: none of the monsters with special hit point calculations
1679 have both little and big forms */
1680 oldtype
= monsndx(ptr
);
1681 newtype
= little_to_big(oldtype
);
1682 if (newtype
== PM_PRIEST
&& mtmp
->female
)
1683 newtype
= PM_PRIESTESS
;
1685 /* growth limits differ depending on method of advancement */
1686 if (victim
) { /* killed a monster */
1688 * The HP threshold is the maximum number of hit points for the
1689 * current level; once exceeded, a level will be gained.
1690 * Possible bug: if somehow the hit points are already higher
1691 * than that, monster will gain a level without any increase in HP.
1693 hp_threshold
= mtmp
->m_lev
* 8; /* normal limit */
1696 else if (is_golem(ptr
)) /* strange creatures */
1697 hp_threshold
= ((mtmp
->mhpmax
/ 10) + 1) * 10 - 1;
1698 else if (is_home_elemental(ptr
))
1700 lev_limit
= 3 * (int) ptr
->mlevel
/ 2; /* same as adj_lev() */
1701 /* If they can grow up, be sure the level is high enough for that */
1702 if (oldtype
!= newtype
&& mons
[newtype
].mlevel
> lev_limit
)
1703 lev_limit
= (int) mons
[newtype
].mlevel
;
1704 /* number of hit points to gain; unlike for the player, we put
1705 the limit at the bottom of the next level rather than the top */
1706 max_increase
= rnd((int) victim
->m_lev
+ 1);
1707 if (mtmp
->mhpmax
+ max_increase
> hp_threshold
+ 1)
1708 max_increase
= max((hp_threshold
+ 1) - mtmp
->mhpmax
, 0);
1709 cur_increase
= (max_increase
> 1) ? rn2(max_increase
) : 0;
1711 /* a gain level potion or wraith corpse; always go up a level
1712 unless already at maximum (49 is hard upper limit except
1713 for demon lords, who start at 50 and can't go any higher) */
1714 max_increase
= cur_increase
= rnd(8);
1715 hp_threshold
= 0; /* smaller than `mhpmax + max_increase' */
1716 lev_limit
= 50; /* recalc below */
1719 mtmp
->mhpmax
+= max_increase
;
1720 mtmp
->mhp
+= cur_increase
;
1721 if (mtmp
->mhpmax
<= hp_threshold
)
1722 return ptr
; /* doesn't gain a level */
1724 if (is_mplayer(ptr
))
1725 lev_limit
= 30; /* same as player */
1726 else if (lev_limit
< 5)
1727 lev_limit
= 5; /* arbitrary */
1728 else if (lev_limit
> 49)
1729 lev_limit
= (ptr
->mlevel
> 49 ? 50 : 49);
1731 if ((int) ++mtmp
->m_lev
>= mons
[newtype
].mlevel
&& newtype
!= oldtype
) {
1732 ptr
= &mons
[newtype
];
1733 if (mvitals
[newtype
].mvflags
& G_GENOD
) { /* allow G_EXTINCT */
1734 if (canspotmon(mtmp
))
1735 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp
),
1736 an(ptr
->mname
), mhe(mtmp
),
1737 nonliving(ptr
) ? "expires" : "dies");
1738 set_mon_data(mtmp
, ptr
, -1); /* keep mvitals[] accurate */
1740 return (struct permonst
*) 0;
1741 } else if (canspotmon(mtmp
)) {
1742 pline("%s %s %s.", Monnam(mtmp
),
1743 humanoid(ptr
) ? "becomes" : "grows up into",
1746 set_mon_data(mtmp
, ptr
, 1); /* preserve intrinsics */
1747 newsym(mtmp
->mx
, mtmp
->my
); /* color may change */
1748 lev_limit
= (int) mtmp
->m_lev
; /* never undo increment */
1751 if ((int) mtmp
->m_lev
> lev_limit
) {
1752 mtmp
->m_lev
--; /* undo increment */
1753 /* HP might have been allowed to grow when it shouldn't */
1754 if (mtmp
->mhpmax
== hp_threshold
+ 1)
1757 if (mtmp
->mhpmax
> 50 * 8)
1758 mtmp
->mhpmax
= 50 * 8; /* absolute limit */
1759 if (mtmp
->mhp
> mtmp
->mhpmax
)
1760 mtmp
->mhp
= mtmp
->mhpmax
;
1767 register struct monst
*mtmp
;
1770 register struct obj
*otmp
;
1775 otmp
= mksobj(otyp
, TRUE
, FALSE
);
1777 if (mtmp
->data
->mlet
== S_DEMON
) {
1778 /* demons never get blessed objects */
1781 } else if (is_lminion(mtmp
)) {
1782 /* lawful minions don't get cursed, bad, or rusting objects */
1783 otmp
->cursed
= FALSE
;
1786 otmp
->oerodeproof
= TRUE
;
1787 } else if (is_mplayer(mtmp
->data
) && is_sword(otmp
)) {
1788 otmp
->spe
= (3 + rn2(4));
1791 if (otmp
->otyp
== CANDELABRUM_OF_INVOCATION
) {
1794 otmp
->lamplit
= FALSE
;
1795 otmp
->blessed
= otmp
->cursed
= FALSE
;
1796 } else if (otmp
->otyp
== BELL_OF_OPENING
) {
1797 otmp
->blessed
= otmp
->cursed
= FALSE
;
1798 } else if (otmp
->otyp
== SPE_BOOK_OF_THE_DEAD
) {
1799 otmp
->blessed
= FALSE
;
1800 otmp
->cursed
= TRUE
;
1803 /* leaders don't tolerate inferior quality battle gear */
1804 if (is_prince(mtmp
->data
)) {
1805 if (otmp
->oclass
== WEAPON_CLASS
&& otmp
->spe
< 1)
1807 else if (otmp
->oclass
== ARMOR_CLASS
&& otmp
->spe
< 0)
1812 (void) mpickobj(mtmp
, otmp
); /* might free otmp */
1823 case PM_STRAW_GOLEM
:
1825 case PM_PAPER_GOLEM
:
1829 case PM_LEATHER_GOLEM
:
1835 case PM_FLESH_GOLEM
:
1839 case PM_STONE_GOLEM
:
1841 case PM_GLASS_GOLEM
:
1851 * Alignment vs. yours determines monster's attitude to you.
1852 * (Some "animal" types are co-aligned, but also hungry.)
1856 register struct permonst
*ptr
;
1858 aligntyp mal
= ptr
->maligntyp
, ual
= u
.ualign
.type
;
1860 if (always_peaceful(ptr
))
1862 if (always_hostile(ptr
))
1864 if (ptr
->msound
== MS_LEADER
|| ptr
->msound
== MS_GUARDIAN
)
1866 if (ptr
->msound
== MS_NEMESIS
)
1869 if (race_peaceful(ptr
))
1871 if (race_hostile(ptr
))
1874 /* the monster is hostile if its alignment is different from the
1876 if (sgn(mal
) != sgn(ual
))
1879 /* Negative monster hostile to player with Amulet. */
1880 if (mal
< A_NEUTRAL
&& u
.uhave
.amulet
)
1883 /* minions are hostile to players that have strayed at all */
1885 return (boolean
) (u
.ualign
.record
>= 0);
1887 /* Last case: a chance of a co-aligned monster being
1888 * hostile. This chance is greater if the player has strayed
1889 * (u.ualign.record negative) or the monster is not strongly aligned.
1891 return (boolean
) (!!rn2(16 + (u
.ualign
.record
< -15 ? -15
1893 && !!rn2(2 + abs(mal
)));
1896 /* Set malign to have the proper effect on player alignment if monster is
1897 * killed. Negative numbers mean it's bad to kill this monster; positive
1898 * numbers mean it's good. Since there are more hostile monsters than
1899 * peaceful monsters, the penalty for killing a peaceful monster should be
1900 * greater than the bonus for killing a hostile monster to maintain balance.
1902 * it's bad to kill peaceful monsters, potentially worse to kill always-
1903 * peaceful monsters;
1904 * it's never bad to kill a hostile monster, although it may not be good.
1910 schar mal
= mtmp
->data
->maligntyp
;
1913 if (mtmp
->ispriest
|| mtmp
->isminion
) {
1914 /* some monsters have individual alignments; check them */
1915 if (mtmp
->ispriest
&& EPRI(mtmp
))
1916 mal
= EPRI(mtmp
)->shralign
;
1917 else if (mtmp
->isminion
&& EMIN(mtmp
))
1918 mal
= EMIN(mtmp
)->min_align
;
1919 /* unless alignment is none, set mal to -5,0,5 */
1920 /* (see align.h for valid aligntyp values) */
1925 coaligned
= (sgn(mal
) == sgn(u
.ualign
.type
));
1926 if (mtmp
->data
->msound
== MS_LEADER
) {
1928 } else if (mal
== A_NONE
) {
1929 if (mtmp
->mpeaceful
)
1932 mtmp
->malign
= 20; /* really hostile */
1933 } else if (always_peaceful(mtmp
->data
)) {
1934 int absmal
= abs(mal
);
1935 if (mtmp
->mpeaceful
)
1936 mtmp
->malign
= -3 * max(5, absmal
);
1938 mtmp
->malign
= 3 * max(5, absmal
); /* renegade */
1939 } else if (always_hostile(mtmp
->data
)) {
1940 int absmal
= abs(mal
);
1944 mtmp
->malign
= max(5, absmal
);
1945 } else if (coaligned
) {
1946 int absmal
= abs(mal
);
1947 if (mtmp
->mpeaceful
)
1948 mtmp
->malign
= -3 * max(3, absmal
);
1950 mtmp
->malign
= max(3, absmal
);
1951 } else /* not coaligned and therefore hostile */
1952 mtmp
->malign
= abs(mal
);
1955 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
1961 mtmp
->mextra
= newmextra();
1962 MCORPSENM(mtmp
) = NON_PM
; /* not initialized yet */
1965 /* release monster's mcorpsenm field; basically a no-op */
1970 if (has_mcorpsenm(mtmp
))
1971 MCORPSENM(mtmp
) = NON_PM
;
1974 static NEARDATA
char syms
[] = {
1975 MAXOCLASSES
, MAXOCLASSES
+ 1, RING_CLASS
, WAND_CLASS
, WEAPON_CLASS
,
1976 FOOD_CLASS
, COIN_CLASS
, SCROLL_CLASS
, POTION_CLASS
, ARMOR_CLASS
,
1977 AMULET_CLASS
, TOOL_CLASS
, ROCK_CLASS
, GEM_CLASS
, SPBOOK_CLASS
,
1978 S_MIMIC_DEF
, S_MIMIC_DEF
,
1983 register struct monst
*mtmp
;
1985 int typ
, roomno
, rt
;
1986 unsigned appear
, ap_type
;
1991 if (!mtmp
|| Protection_from_shape_changers
)
1995 typ
= levl
[mx
][my
].typ
;
1996 /* only valid for INSIDE of room */
1997 roomno
= levl
[mx
][my
].roomno
- ROOMOFFSET
;
1999 rt
= rooms
[roomno
].rtype
;
2000 #ifdef SPECIALIZATION
2001 else if (IS_ROOM(typ
))
2002 rt
= OROOM
, roomno
= 0;
2005 rt
= 0; /* roomno < 0 case for GCC_WARN */
2007 if (OBJ_AT(mx
, my
)) {
2008 ap_type
= M_AP_OBJECT
;
2009 appear
= level
.objects
[mx
][my
]->otyp
;
2010 } else if (IS_DOOR(typ
) || IS_WALL(typ
) || typ
== SDOOR
|| typ
== SCORR
) {
2011 ap_type
= M_AP_FURNITURE
;
2013 * If there is a wall to the left that connects to this
2014 * location, then the mimic mimics a horizontal closed door.
2015 * This does not allow doors to be in corners of rooms.
2016 * Since rogue has no closed doors, mimic a wall there
2017 * (yes, mimics can end up on this level by various means).
2019 if (mx
!= 0 && (levl
[mx
- 1][my
].typ
== HWALL
2020 || levl
[mx
- 1][my
].typ
== TLCORNER
2021 || levl
[mx
- 1][my
].typ
== TRWALL
2022 || levl
[mx
- 1][my
].typ
== BLCORNER
2023 || levl
[mx
- 1][my
].typ
== TDWALL
2024 || levl
[mx
- 1][my
].typ
== CROSSWALL
2025 || levl
[mx
- 1][my
].typ
== TUWALL
))
2026 appear
= Is_rogue_level(&u
.uz
) ? S_hwall
: S_hcdoor
;
2028 appear
= Is_rogue_level(&u
.uz
) ? S_vwall
: S_vcdoor
;
2029 if (!mtmp
->minvis
|| See_invisible
)
2030 block_point(mx
, my
); /* vision */
2031 } else if (level
.flags
.is_maze_lev
&& !In_sokoban(&u
.uz
) && rn2(2)) {
2032 ap_type
= M_AP_OBJECT
;
2034 } else if (roomno
< 0 && !t_at(mx
, my
)) {
2035 ap_type
= M_AP_OBJECT
;
2037 if (!mtmp
->minvis
|| See_invisible
)
2038 block_point(mx
, my
); /* vision */
2039 } else if (rt
== ZOO
|| rt
== VAULT
) {
2040 ap_type
= M_AP_OBJECT
;
2041 appear
= GOLD_PIECE
;
2042 } else if (rt
== DELPHI
) {
2044 ap_type
= M_AP_OBJECT
;
2047 ap_type
= M_AP_FURNITURE
;
2048 appear
= S_fountain
;
2050 } else if (rt
== TEMPLE
) {
2051 ap_type
= M_AP_FURNITURE
;
2054 * We won't bother with beehives, morgues, barracks, throne rooms
2055 * since they shouldn't contain too many mimics anyway...
2057 } else if (rt
>= SHOPBASE
) {
2058 s_sym
= get_shop_item(rt
- SHOPBASE
);
2060 ap_type
= M_AP_OBJECT
;
2063 if (s_sym
== RANDOM_CLASS
)
2064 s_sym
= syms
[rn2((int) sizeof(syms
) - 2) + 2];
2068 s_sym
= syms
[rn2((int) sizeof(syms
))];
2070 if (s_sym
== MAXOCLASSES
|| s_sym
== MAXOCLASSES
+ 1) {
2071 ap_type
= M_AP_FURNITURE
;
2072 appear
= (s_sym
== MAXOCLASSES
) ? S_upstair
: S_dnstair
;
2074 ap_type
= M_AP_OBJECT
;
2075 if (s_sym
== S_MIMIC_DEF
) {
2076 appear
= STRANGE_OBJECT
;
2077 } else if (s_sym
== COIN_CLASS
) {
2078 appear
= GOLD_PIECE
;
2080 otmp
= mkobj((char) s_sym
, FALSE
);
2081 appear
= otmp
->otyp
;
2082 /* make sure container contents are free'ed */
2083 obfree(otmp
, (struct obj
*) 0);
2087 mtmp
->m_ap_type
= ap_type
;
2088 mtmp
->mappearance
= appear
;
2089 if (ap_type
== M_AP_OBJECT
&& (appear
== STATUE
|| appear
== CORPSE
2090 || appear
== FIGURINE
|| appear
== EGG
)) {
2092 MCORPSENM(mtmp
) = rndmonnum();
2093 if (appear
== EGG
&& !can_be_hatched(MCORPSENM(mtmp
)))
2094 MCORPSENM(mtmp
) = NON_PM
; /* revert to generic egg */
2098 /* release monster from bag of tricks; return number of monsters created */
2100 bagotricks(bag
, tipping
, seencount
)
2102 boolean tipping
; /* caller emptying entire contents; affects shop handling */
2103 int *seencount
; /* secondary output */
2107 if (!bag
|| bag
->otyp
!= BAG_OF_TRICKS
) {
2108 impossible("bad bag o' tricks");
2109 } else if (bag
->spe
< 1) {
2110 /* if tipping known empty bag, give normal empty container message */
2111 pline1((tipping
&& bag
->cknown
) ? "It's empty." : nothing_happens
);
2112 /* now known to be empty if sufficiently discovered */
2113 if (bag
->dknown
&& objects
[bag
->otyp
].oc_name_known
)
2117 int creatcnt
= 1, seecount
= 0;
2119 consume_obj_charge(bag
, !tipping
);
2124 mtmp
= makemon((struct permonst
*) 0, u
.ux
, u
.uy
, NO_MM_FLAGS
);
2127 if (canspotmon(mtmp
))
2130 } while (--creatcnt
> 0);
2133 *seencount
+= seecount
;
2135 makeknown(BAG_OF_TRICKS
);
2136 } else if (!tipping
) {
2137 pline1(!moncount
? nothing_happens
: "Nothing seems to happen.");