make rank() static again
[NetHack.git] / src / makemon.c
bloba7e52bf47c741288504ab08835677b5eabdaf74e
1 /* NetHack 3.7 makemon.c $NHDT-Date: 1720128166 2024/07/04 21:22:46 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.249 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Robert Patrick Rankin, 2012. */
4 /* NetHack may be freely redistributed. See license for details. */
6 #include "hack.h"
8 /* this assumes that a human quest leader or nemesis is an archetype
9 of the corresponding role; that isn't so for some roles (tourist
10 for instance) but is for the priests and monks we use it for... */
11 #define quest_mon_represents_role(mptr, role_pm) \
12 (mptr->mlet == S_HUMAN && Role_if(role_pm) \
13 && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
15 staticfn boolean uncommon(int);
16 staticfn int align_shift(struct permonst *);
17 staticfn int temperature_shift(struct permonst *);
18 staticfn boolean mk_gen_ok(int, unsigned, unsigned);
19 staticfn boolean wrong_elem_type(struct permonst *);
20 staticfn void m_initgrp(struct monst *, coordxy, coordxy, int, mmflags_nht);
21 staticfn void m_initthrow(struct monst *, int, int);
22 staticfn void m_initweap(struct monst *);
23 staticfn void m_initinv(struct monst *);
24 staticfn boolean makemon_rnd_goodpos(struct monst *, mmflags_nht, coord *);
25 staticfn void init_mextra(struct mextra *);
27 #define m_initsgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 3, mmf)
28 #define m_initlgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 10, mmf)
30 boolean
31 is_home_elemental(struct permonst *ptr)
33 if (ptr->mlet == S_ELEMENTAL) {
34 switch (monsndx(ptr)) {
35 case PM_AIR_ELEMENTAL:
36 return Is_airlevel(&u.uz);
37 case PM_FIRE_ELEMENTAL:
38 return Is_firelevel(&u.uz);
39 case PM_EARTH_ELEMENTAL:
40 return Is_earthlevel(&u.uz);
41 case PM_WATER_ELEMENTAL:
42 return Is_waterlevel(&u.uz);
43 default:
44 break;
47 return FALSE;
51 * Return true if the given monster cannot exist on this elemental level.
53 staticfn boolean
54 wrong_elem_type(struct permonst *ptr)
56 if (ptr->mlet == S_ELEMENTAL) {
57 return (boolean) !is_home_elemental(ptr);
58 } else if (Is_earthlevel(&u.uz)) {
59 /* no restrictions? */
60 } else if (Is_waterlevel(&u.uz)) {
61 /* just monsters that can swim */
62 if (!is_swimmer(ptr))
63 return TRUE;
64 } else if (Is_firelevel(&u.uz)) {
65 if (!pm_resistance(ptr, MR_FIRE))
66 return TRUE;
67 } else if (Is_airlevel(&u.uz)) {
68 if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
69 && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
70 return TRUE;
72 return FALSE;
75 /* make a group just like mtmp */
76 staticfn void
77 m_initgrp(
78 struct monst *mtmp,
79 coordxy x, coordxy y, int n,
80 mmflags_nht mmflags)
82 coord mm;
83 int cnt = rnd(n);
84 struct monst *mon;
85 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
86 /* There is an unresolved problem with several people finding that
87 * the game hangs eating CPU; if interrupted and restored, the level
88 * will be filled with monsters. Of those reports giving system type,
89 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
90 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
91 * problem went away for him and another reporter-to-newsgroup
92 * after adding this debugging code. This has almost got to be a
93 * compiler bug, but until somebody tracks it down and gets it fixed,
94 * might as well go with the "but it went away when I tried to find
95 * it" code.
97 int cnttmp, cntdiv;
99 cnttmp = cnt;
100 debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
101 cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
102 #endif
103 /* Tuning: cut down on swarming at low character levels [mrs] */
104 cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
105 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
106 if (cnt != (cnttmp / cntdiv)) {
107 pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
108 (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
110 #endif
111 if (!cnt)
112 cnt++;
113 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
114 if (cnt < 0)
115 cnt = 1;
116 if (cnt > 10)
117 cnt = 10;
118 #endif
120 mm.x = x;
121 mm.y = y;
122 while (cnt--) {
123 if (peace_minded(mtmp->data))
124 continue;
125 /* Don't create groups of peaceful monsters since they'll get
126 * in our way. If the monster has a percentage chance so some
127 * are peaceful and some are not, the result will just be a
128 * smaller group.
130 if (enexto_gpflags(&mm, mm.x, mm.y, mtmp->data, mmflags)) {
131 mon = makemon(mtmp->data, mm.x, mm.y, (mmflags | MM_NOGRP));
132 if (mon) {
133 mon->mpeaceful = FALSE;
134 mon->mavenge = 0;
135 set_malign(mon);
136 /* Undo the second peace_minded() check in makemon(); if the
137 * monster turned out to be peaceful the first time we
138 * didn't create it at all; we don't want a second check.
145 staticfn void
146 m_initthrow(struct monst *mtmp, int otyp, int oquan)
148 struct obj *otmp;
150 otmp = mksobj(otyp, TRUE, FALSE);
151 otmp->quan = (long) rn1(oquan, 3);
152 otmp->owt = weight(otmp);
153 if (otyp == ORCISH_ARROW)
154 otmp->opoisoned = TRUE;
155 (void) mpickobj(mtmp, otmp);
158 staticfn void
159 m_initweap(struct monst *mtmp)
161 struct permonst *ptr = mtmp->data;
162 int mm = monsndx(ptr);
163 struct obj *otmp;
164 int bias, w1, w2;
166 if (Is_rogue_level(&u.uz))
167 return;
169 * First a few special cases:
170 * giants get a boulder to throw sometimes
171 * ettins get clubs
172 * kobolds get darts to throw
173 * centaurs get some sort of bow & arrows or bolts
174 * soldiers get all sorts of things
175 * kops get clubs & cream pies.
177 switch (ptr->mlet) {
178 case S_GIANT:
179 if (rn2(2))
180 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
181 if ((mm != PM_ETTIN) && !rn2(5))
182 (void) mongets(mtmp, rn2(2) ? TWO_HANDED_SWORD : BATTLE_AXE);
183 break;
184 case S_HUMAN:
185 if (is_mercenary(ptr)) {
186 w1 = w2 = 0;
187 switch (mm) {
188 case PM_WATCHMAN:
189 case PM_SOLDIER:
190 if (!rn2(3)) {
191 /* lance and dwarvish mattock used to be in midst of
192 the polearms but use different skills from polearms
193 and aren't appropriates choices for human soldiers */
194 do {
195 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
196 } while (objects[w1].oc_skill != P_POLEARMS);
197 w2 = rn2(2) ? DAGGER : KNIFE;
198 } else
199 w1 = rn2(2) ? SPEAR : SHORT_SWORD;
200 break;
201 case PM_SERGEANT:
202 w1 = rn2(2) ? FLAIL : MACE;
203 break;
204 case PM_LIEUTENANT:
205 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
206 break;
207 case PM_CAPTAIN:
208 case PM_WATCH_CAPTAIN:
209 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
210 break;
211 default:
212 if (!rn2(4))
213 w1 = DAGGER;
214 if (!rn2(7))
215 w2 = SPEAR;
216 break;
218 if (w1)
219 (void) mongets(mtmp, w1);
220 if (!w2 && w1 != DAGGER && !rn2(4))
221 w2 = KNIFE;
222 if (w2)
223 (void) mongets(mtmp, w2);
224 } else if (is_elf(ptr)) {
225 if (rn2(2))
226 (void) mongets(mtmp,
227 rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
228 if (rn2(2))
229 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
230 else if (!rn2(4))
231 (void) mongets(mtmp, ELVEN_BOOTS);
232 if (rn2(2))
233 (void) mongets(mtmp, ELVEN_DAGGER);
234 switch (rn2(3)) {
235 case 0:
236 if (!rn2(4))
237 (void) mongets(mtmp, ELVEN_SHIELD);
238 if (rn2(3))
239 (void) mongets(mtmp, ELVEN_SHORT_SWORD);
240 (void) mongets(mtmp, ELVEN_BOW);
241 m_initthrow(mtmp, ELVEN_ARROW, 12);
242 break;
243 case 1:
244 (void) mongets(mtmp, ELVEN_BROADSWORD);
245 if (rn2(2))
246 (void) mongets(mtmp, ELVEN_SHIELD);
247 break;
248 case 2:
249 if (rn2(2)) {
250 (void) mongets(mtmp, ELVEN_SPEAR);
251 (void) mongets(mtmp, ELVEN_SHIELD);
253 break;
255 if (mm == PM_ELVEN_MONARCH) {
256 if (rn2(3) || (gi.in_mklev && Is_earthlevel(&u.uz)))
257 (void) mongets(mtmp, PICK_AXE);
258 if (!rn2(50))
259 (void) mongets(mtmp, CRYSTAL_BALL);
261 } else if (ptr->msound == MS_PRIEST
262 || quest_mon_represents_role(ptr, PM_CLERIC)) {
263 otmp = mksobj(MACE, FALSE, FALSE);
264 otmp->spe = rnd(3);
265 if (!rn2(2))
266 curse(otmp);
267 (void) mpickobj(mtmp, otmp);
268 } else if (mm == PM_NINJA) { /* extra quest villains */
269 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
270 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
271 } else if (ptr->msound == MS_GUARDIAN) {
272 /* quest "guardians" */
273 switch (mm) {
274 case PM_STUDENT:
275 case PM_ATTENDANT:
276 case PM_ABBOT:
277 case PM_ACOLYTE:
278 case PM_GUIDE:
279 case PM_APPRENTICE:
280 if (rn2(2))
281 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
282 if (rn2(5))
283 (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET
284 : LEATHER_CLOAK);
285 if (rn2(3))
286 (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
287 if (rn2(3))
288 (void) mongets(mtmp, POT_HEALING);
289 break;
290 case PM_CHIEFTAIN:
291 case PM_PAGE:
292 case PM_ROSHI:
293 case PM_WARRIOR:
294 (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
295 (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
296 if (rn2(2))
297 (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
298 if (!rn2(3))
299 (void) mongets(mtmp, LEATHER_CLOAK);
300 if (!rn2(3)) {
301 (void) mongets(mtmp, BOW);
302 m_initthrow(mtmp, ARROW, 12);
304 break;
305 case PM_HUNTER:
306 (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
307 if (rn2(2))
308 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET
309 : LEATHER_ARMOR);
310 (void) mongets(mtmp, BOW);
311 m_initthrow(mtmp, ARROW, 12);
312 break;
313 case PM_THUG:
314 (void) mongets(mtmp, CLUB);
315 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
316 if (rn2(2))
317 (void) mongets(mtmp, LEATHER_GLOVES);
318 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
319 break;
320 case PM_NEANDERTHAL:
321 (void) mongets(mtmp, CLUB);
322 (void) mongets(mtmp, LEATHER_ARMOR);
323 break;
326 break;
328 case S_ANGEL:
329 if (humanoid(ptr)) {
330 /* create minion stuff; bypass mongets */
331 int typ = rn2(3) ? LONG_SWORD : MACE;
332 const char *nam = (typ == LONG_SWORD) ? "Sunsword" : "Demonbane";
334 otmp = mksobj(typ, FALSE, FALSE);
335 /* maybe promote weapon to an artifact */
336 if ((!rn2(20) || is_lord(ptr))
337 && sgn(mtmp->isminion ? EMIN(mtmp)->min_align
338 : ptr->maligntyp) == A_LAWFUL)
339 otmp = oname(otmp, nam, ONAME_RANDOM); /* randomly created */
340 /* enhance the weapon */
341 bless(otmp);
342 otmp->oerodeproof = TRUE;
343 /* make long sword be +0 to +3, mace be +3 to +6 to compensate
344 for being significantly weaker against large opponents */
345 otmp->spe = rn2(4);
346 if (typ == MACE)
347 otmp->spe += 3;
348 (void) mpickobj(mtmp, otmp);
350 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
351 : LARGE_SHIELD,
352 FALSE, FALSE);
353 /* uncurse(otmp); -- mksobj(,FALSE,) item is always uncursed */
354 otmp->oerodeproof = TRUE;
355 otmp->spe = 0;
356 (void) mpickobj(mtmp, otmp);
358 break;
360 case S_HUMANOID:
361 if (mm == PM_HOBBIT) {
362 switch (rn2(3)) {
363 case 0:
364 (void) mongets(mtmp, DAGGER);
365 break;
366 case 1:
367 (void) mongets(mtmp, ELVEN_DAGGER);
368 break;
369 case 2:
370 (void) mongets(mtmp, SLING);
371 m_initthrow(mtmp, !rn2(4) ? FLINT : ROCK, 6);
372 break;
374 if (!rn2(10))
375 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
376 if (!rn2(10))
377 (void) mongets(mtmp, DWARVISH_CLOAK);
378 } else if (is_dwarf(ptr)) {
379 if (rn2(7))
380 (void) mongets(mtmp, DWARVISH_CLOAK);
381 if (rn2(7))
382 (void) mongets(mtmp, IRON_SHOES);
383 if (!rn2(4)) {
384 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
385 /* note: you can't use a mattock with a shield */
386 if (rn2(2))
387 (void) mongets(mtmp, DWARVISH_MATTOCK);
388 else {
389 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
390 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
392 (void) mongets(mtmp, DWARVISH_IRON_HELM);
393 if (!rn2(3))
394 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
395 } else {
396 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
399 break;
400 case S_KOP:
401 /* create Keystone Kops with cream pies to
402 throw. As suggested by KAA. [MRS] */
403 if (!rn2(4))
404 m_initthrow(mtmp, CREAM_PIE, 2);
405 if (!rn2(3))
406 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
407 break;
408 case S_ORC:
409 if (rn2(2))
410 (void) mongets(mtmp, ORCISH_HELM);
411 switch ((mm != PM_ORC_CAPTAIN) ? mm
412 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
413 case PM_MORDOR_ORC:
414 if (!rn2(3))
415 (void) mongets(mtmp, SCIMITAR);
416 if (!rn2(3))
417 (void) mongets(mtmp, ORCISH_SHIELD);
418 if (!rn2(3))
419 (void) mongets(mtmp, KNIFE);
420 if (!rn2(3))
421 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
422 break;
423 case PM_URUK_HAI:
424 if (!rn2(3))
425 (void) mongets(mtmp, ORCISH_CLOAK);
426 if (!rn2(3))
427 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
428 if (!rn2(3))
429 (void) mongets(mtmp, IRON_SHOES);
430 if (!rn2(3)) {
431 (void) mongets(mtmp, ORCISH_BOW);
432 m_initthrow(mtmp, ORCISH_ARROW, 12);
434 if (!rn2(3))
435 (void) mongets(mtmp, URUK_HAI_SHIELD);
436 break;
437 default:
438 if (mm != PM_ORC_SHAMAN && rn2(2))
439 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
440 ? ORCISH_DAGGER
441 : SCIMITAR);
443 break;
444 case S_OGRE:
445 if (!rn2(mm == PM_OGRE_TYRANT ? 3 : mm == PM_OGRE_LEADER ? 6 : 12))
446 (void) mongets(mtmp, BATTLE_AXE);
447 else
448 (void) mongets(mtmp, CLUB);
449 break;
450 case S_TROLL:
451 if (!rn2(2))
452 switch (rn2(4)) {
453 case 0:
454 (void) mongets(mtmp, RANSEUR);
455 break;
456 case 1:
457 (void) mongets(mtmp, PARTISAN);
458 break;
459 case 2:
460 (void) mongets(mtmp, GLAIVE);
461 break;
462 case 3:
463 (void) mongets(mtmp, SPETUM);
464 break;
466 break;
467 case S_KOBOLD:
468 if (!rn2(4))
469 m_initthrow(mtmp, DART, 12);
470 break;
472 case S_CENTAUR:
473 if (rn2(2)) {
474 if (ptr == &mons[PM_FOREST_CENTAUR]) {
475 (void) mongets(mtmp, BOW);
476 m_initthrow(mtmp, ARROW, 12);
477 } else {
478 (void) mongets(mtmp, CROSSBOW);
479 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
482 break;
483 case S_WRAITH:
484 (void) mongets(mtmp, KNIFE);
485 (void) mongets(mtmp, LONG_SWORD);
486 break;
487 case S_ZOMBIE:
488 if (!rn2(4))
489 (void) mongets(mtmp, LEATHER_ARMOR);
490 if (!rn2(4))
491 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
492 break;
493 case S_LIZARD:
494 if (mm == PM_SALAMANDER)
495 (void) mongets(mtmp,
496 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
497 break;
498 case S_DEMON:
499 switch (mm) {
500 case PM_BALROG:
501 (void) mongets(mtmp, BULLWHIP);
502 (void) mongets(mtmp, BROADSWORD);
503 break;
504 case PM_ORCUS:
505 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
506 break;
507 case PM_HORNED_DEVIL:
508 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
509 break;
510 case PM_DISPATER:
511 (void) mongets(mtmp, WAN_STRIKING);
512 break;
513 case PM_YEENOGHU:
514 (void) mongets(mtmp, FLAIL);
515 break;
517 /* prevent djinn and mail daemons from leaving objects when
518 * they vanish
520 if (!is_demon(ptr))
521 break;
522 FALLTHROUGH;
523 /*FALLTHRU*/
524 default:
526 * Now the general case, some chance of getting some type
527 * of weapon for "normal" monsters. Certain special types
528 * of monsters will get a bonus chance or different selections.
530 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
531 switch (rnd(14 - (2 * bias))) {
532 case 1:
533 if (strongmonst(ptr))
534 (void) mongets(mtmp, BATTLE_AXE);
535 else
536 m_initthrow(mtmp, DART, 12);
537 break;
538 case 2:
539 if (strongmonst(ptr))
540 (void) mongets(mtmp, TWO_HANDED_SWORD);
541 else {
542 (void) mongets(mtmp, CROSSBOW);
543 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
545 break;
546 case 3:
547 (void) mongets(mtmp, BOW);
548 m_initthrow(mtmp, ARROW, 12);
549 break;
550 case 4:
551 if (strongmonst(ptr))
552 (void) mongets(mtmp, LONG_SWORD);
553 else
554 m_initthrow(mtmp, DAGGER, 3);
555 break;
556 case 5:
557 if (strongmonst(ptr))
558 (void) mongets(mtmp, LUCERN_HAMMER);
559 else
560 (void) mongets(mtmp, AKLYS);
561 break;
562 default:
563 break;
565 break;
568 if ((int) mtmp->m_lev > rn2(75))
569 (void) mongets(mtmp, rnd_offensive_item(mtmp));
572 /* create a new stack of gold in monster's inventory */
573 void
574 mkmonmoney(struct monst *mtmp, long amount)
576 /* mk_mplayer() passes rn2(1000) so the amount might be 0 */
577 if (amount > 0L) {
578 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
580 gold->quan = amount;
581 gold->owt = weight(gold);
582 add_to_minv(mtmp, gold);
586 staticfn void
587 m_initinv(struct monst *mtmp)
589 int cnt;
590 struct obj *otmp;
591 struct permonst *ptr = mtmp->data;
593 if (Is_rogue_level(&u.uz))
594 return;
596 * Soldiers get armour & rations - armour approximates their ac.
597 * Nymphs may get mirror or potion of object detection.
599 switch (ptr->mlet) {
600 case S_HUMAN:
601 if (is_mercenary(ptr)) {
602 int mac;
604 switch (monsndx(ptr)) {
605 case PM_GUARD:
606 mac = -1;
607 break;
608 case PM_SOLDIER:
609 mac = 3;
610 break;
611 case PM_SERGEANT:
612 mac = 0;
613 break;
614 case PM_LIEUTENANT:
615 mac = -2;
616 break;
617 case PM_CAPTAIN:
618 mac = -3;
619 break;
620 case PM_WATCHMAN:
621 mac = 3;
622 break;
623 case PM_WATCH_CAPTAIN:
624 mac = -2;
625 break;
626 default:
627 impossible("odd mercenary %d?", monsndx(ptr));
628 mac = 0;
629 break;
632 #define add_ac(otmp) \
633 if (otmp) { mac += ARM_BONUS(otmp); } \
634 otmp = (struct obj *) 0;
636 /* round 1: give them body armor */
637 if (mac < -1 && rn2(5))
638 otmp = mongets(mtmp, (rn2(5)) ? PLATE_MAIL
639 : CRYSTAL_PLATE_MAIL);
640 else if (mac < 3 && rn2(5))
641 otmp = mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
642 else if (rn2(5))
643 otmp = mongets(mtmp, (rn2(3)) ? RING_MAIL
644 : STUDDED_LEATHER_ARMOR);
645 else
646 otmp = mongets(mtmp, LEATHER_ARMOR);
647 add_ac(otmp);
649 /* round 2: helmets */
650 if (mac < 10 && rn2(3))
651 otmp = mongets(mtmp, HELMET);
652 else if (mac < 10 && rn2(2))
653 otmp = mongets(mtmp, DENTED_POT);
654 add_ac(otmp);
656 /* round 3: shields */
657 if (mac < 10 && rn2(3))
658 otmp = mongets(mtmp, SMALL_SHIELD);
659 else if (mac < 10 && rn2(2))
660 otmp = mongets(mtmp, LARGE_SHIELD);
661 add_ac(otmp);
663 /* round 4: boots */
664 if (mac < 10 && rn2(3))
665 otmp = mongets(mtmp, LOW_BOOTS);
666 else if (mac < 10 && rn2(2))
667 otmp = mongets(mtmp, HIGH_BOOTS);
668 add_ac(otmp);
670 /* round 5: gloves + cloak */
671 if (mac < 10 && rn2(3))
672 otmp = mongets(mtmp, LEATHER_GLOVES);
673 else if (mac < 10 && rn2(2))
674 otmp = mongets(mtmp, LEATHER_CLOAK);
675 add_ac(otmp); /* not technically needed */
677 #undef add_ac
678 nhUse(mac); /* suppress 'dead increment' from static analyzer */
680 if (ptr == &mons[PM_WATCH_CAPTAIN]) {
681 ; /* better weapon rather than extra gear here */
682 } else if (ptr == &mons[PM_WATCHMAN]) {
683 if (rn2(3)) /* most watchmen carry a whistle */
684 (void) mongets(mtmp, TIN_WHISTLE);
685 } else if (ptr == &mons[PM_GUARD]) {
686 /* if hero teleports out of a vault while being confronted
687 by the vault's guard, there is a shrill whistling sound,
688 so guard evidently carries a cursed whistle */
689 otmp = mksobj(TIN_WHISTLE, TRUE, FALSE);
690 curse(otmp);
691 (void) mpickobj(mtmp, otmp);
692 } else { /* soldiers and their officers */
693 if (!rn2(3))
694 (void) mongets(mtmp, K_RATION);
695 if (!rn2(2))
696 (void) mongets(mtmp, C_RATION);
697 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
698 (void) mongets(mtmp, BUGLE);
700 } else if (ptr == &mons[PM_SHOPKEEPER]) {
701 (void) mongets(mtmp, SKELETON_KEY);
702 switch (rn2(4)) {
703 /* MAJOR fall through ... */
704 case 0:
705 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
706 FALLTHROUGH;
707 /*FALLTHRU*/
708 case 1:
709 (void) mongets(mtmp, POT_EXTRA_HEALING);
710 FALLTHROUGH;
711 /*FALLTHRU*/
712 case 2:
713 (void) mongets(mtmp, POT_HEALING);
714 FALLTHROUGH;
715 /*FALLTHRU*/
716 case 3:
717 (void) mongets(mtmp, WAN_STRIKING);
719 } else if (ptr->msound == MS_PRIEST
720 || quest_mon_represents_role(ptr, PM_CLERIC)) {
721 (void) mongets(mtmp, rn2(7) ? ROBE
722 : rn2(3) ? CLOAK_OF_PROTECTION
723 : CLOAK_OF_MAGIC_RESISTANCE);
724 (void) mongets(mtmp, SMALL_SHIELD);
725 mkmonmoney(mtmp, (long) rn1(10, 20));
726 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
727 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
729 break;
730 case S_NYMPH:
731 if (!rn2(2))
732 (void) mongets(mtmp, MIRROR);
733 if (!rn2(2))
734 (void) mongets(mtmp, POT_OBJECT_DETECTION);
735 break;
736 case S_GIANT:
737 if (ptr == &mons[PM_MINOTAUR]) {
738 if (!rn2(3) || (gi.in_mklev && Is_earthlevel(&u.uz)))
739 (void) mongets(mtmp, WAN_DIGGING);
740 } else if (is_giant(ptr)) {
741 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
742 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
743 FALSE, FALSE);
744 otmp->quan = (long) rn1(2, 3);
745 otmp->owt = weight(otmp);
746 (void) mpickobj(mtmp, otmp);
749 break;
750 case S_WRAITH:
751 if (ptr == &mons[PM_NAZGUL]) {
752 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
753 curse(otmp);
754 (void) mpickobj(mtmp, otmp);
756 break;
757 case S_LICH:
758 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
759 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
760 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
761 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
762 rn2(13) ? FALSE : TRUE);
763 if (otmp->spe < 2)
764 otmp->spe = rnd(3);
765 if (!rn2(4))
766 otmp->oerodeproof = 1;
767 (void) mpickobj(mtmp, otmp);
769 break;
770 case S_MUMMY:
771 if (rn2(7))
772 (void) mongets(mtmp, MUMMY_WRAPPING);
773 break;
774 case S_QUANTMECH:
775 if (!rn2(20) && ptr == &mons[PM_QUANTUM_MECHANIC]) {
776 struct obj *catcorpse;
778 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
779 /* we used to just set the flag, which resulted in weight()
780 treating the box as being heavier by the weight of a cat;
781 now we include a cat corpse that won't rot; when opening or
782 disclosing the box's contents, the corpse might be revived,
783 otherwise it's given a rot timer; weight is now ordinary */
784 if ((catcorpse = mksobj(CORPSE, TRUE, FALSE)) != 0) {
785 otmp->spe = 1; /* flag for special SchroedingersBox */
786 set_corpsenm(catcorpse, PM_HOUSECAT);
787 (void) stop_timer(ROT_CORPSE, obj_to_any(catcorpse));
788 add_to_container(otmp, catcorpse);
789 otmp->owt = weight(otmp);
791 (void) mpickobj(mtmp, otmp);
793 break;
794 case S_LEPRECHAUN:
795 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
796 break;
797 case S_DEMON:
798 /* moved here from m_initweap() because these don't
799 have AT_WEAP so m_initweap() is not called for them */
800 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
801 (void) mongets(mtmp, SPEAR);
802 } else if (ptr == &mons[PM_ASMODEUS]) {
803 (void) mongets(mtmp, WAN_COLD);
804 (void) mongets(mtmp, WAN_FIRE);
806 break;
807 case S_GNOME:
808 if (!rn2((In_mines(&u.uz) && gi.in_mklev) ? 20 : 60)) {
809 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
810 otmp->quan = 1;
811 otmp->owt = weight(otmp);
812 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
813 begin_burn(otmp, FALSE);
815 break;
816 default:
817 break;
820 /* ordinary soldiers rarely have access to magic (or gold :-) */
821 if (ptr == &mons[PM_SOLDIER] && rn2(13))
822 return;
824 if ((int) mtmp->m_lev > rn2(50))
825 (void) mongets(mtmp, rnd_defensive_item(mtmp));
826 if ((int) mtmp->m_lev > rn2(100))
827 (void) mongets(mtmp, rnd_misc_item(mtmp));
828 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
829 mkmonmoney(mtmp,
830 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
833 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
834 struct monst *
835 clone_mon(
836 struct monst *mon,
837 coordxy x, coordxy y) /* clone's preferred location or 0 (near mon) */
839 coord mm;
840 struct monst *m2;
842 /* may be too weak or have been extinguished for population control */
843 if (mon->mhp <= 1
844 || (svm.mvitals[monsndx(mon->data)].mvflags & G_EXTINCT) != 0)
845 return (struct monst *) 0;
847 if (x == 0) {
848 mm.x = mon->mx;
849 mm.y = mon->my;
850 } else {
851 mm.x = x;
852 mm.y = y;
854 if (!isok(mm.x, mm.y)) { /* paranoia */
855 impossible("clone_mon trying to create a monster at <%d,%d>?",
856 mm.x, mm.y);
857 return (struct monst *) 0;
859 if (MON_AT(mm.x, mm.y)) { /* (always True for the x==0 case) */
860 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
861 return (struct monst *) 0;
864 m2 = newmonst();
865 *m2 = *mon; /* copy condition of old monster */
866 m2->mextra = (struct mextra *) 0;
867 m2->nmon = fmon;
868 fmon = m2;
869 m2->m_id = next_ident();
870 m2->mx = mm.x;
871 m2->my = mm.y;
873 m2->mundetected = 0;
874 m2->mtrapped = 0;
875 m2->mcloned = 1;
876 m2->minvent = (struct obj *) 0; /* objects don't clone */
877 m2->mleashed = 0;
878 /* Max HP the same, but current HP halved for both. The caller
879 * might want to override this by halving the max HP also.
880 * When current HP is odd, the original keeps the extra point.
881 * We know original has more than 1 HP, so both end up with at least 1.
883 m2->mhpmax = mon->mhpmax;
884 m2->mhp = mon->mhp / 2;
885 mon->mhp -= m2->mhp;
887 /* clone doesn't have mextra so mustn't retain special monster flags */
888 m2->isshk = 0;
889 m2->isgd = 0;
890 m2->ispriest = 0;
891 /* ms->isminion handled below */
893 /* clone shouldn't be reluctant to move on spots 'parent' just moved on */
894 mon_track_clear(m2);
896 place_monster(m2, m2->mx, m2->my);
897 if (emits_light(m2->data))
898 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
899 monst_to_any(m2));
900 /* if 'parent' is named, give the clone the same name */
901 if (has_mgivenname(mon)) {
902 m2 = christen_monst(m2, MGIVENNAME(mon));
903 } else if (mon->isshk) {
904 m2 = christen_monst(m2, shkname(mon));
907 /* not all clones caused by player are tame or peaceful */
908 if (!svc.context.mon_moving && mon->mpeaceful) {
909 if (mon->mtame)
910 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
911 else if (mon->mpeaceful)
912 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
914 /* if guardian angel could be cloned (maybe after polymorph?),
915 m2 could be both isminion and mtame; isminion takes precedence */
916 if (m2->isminion) {
917 int atyp;
919 newemin(m2);
920 assert(has_emin(m2) && has_emin(mon));
921 *EMIN(m2) = *EMIN(mon);
922 /* renegade when same alignment as hero but not peaceful or
923 when peaceful while being different alignment from hero */
924 atyp = EMIN(m2)->min_align;
925 EMIN(m2)->renegade = (atyp != u.ualign.type) ^ !m2->mpeaceful;
926 } else if (m2->mtame) {
927 /* Because m2 is a copy of mon it is tame but not init'ed.
928 However, tamedog() will not re-tame a tame dog, so m2
929 must be made non-tame to get initialized properly. */
930 m2->mtame = 0;
931 if (tamedog(m2, (struct obj *) 0, FALSE)) {
932 assert(has_edog(m2) && has_edog(mon));
933 *EDOG(m2) = *EDOG(mon);
935 /* [TODO? some (most? all?) edog fields probably should be
936 reinitialized rather that retain the 'parent's values] */
938 set_malign(m2);
939 newsym(m2->mx, m2->my); /* display the new monster */
941 return m2;
945 * Propagate a species
947 * Once a certain number of monsters are created, don't create any more
948 * at random (i.e. make them extinct). The previous (3.2) behavior was
949 * to do this when a certain number had _died_, which didn't make
950 * much sense.
952 * Returns FALSE propagation unsuccessful
953 * TRUE propagation successful
955 boolean
956 propagate(int mndx, boolean tally, boolean ghostly)
958 boolean gone, result;
959 int lim = mbirth_limit(mndx);
961 gone = (svm.mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
962 result = ((int) svm.mvitals[mndx].born < lim && !gone) ? TRUE : FALSE;
964 /* if it's unique, don't ever make it again */
965 if ((mons[mndx].geno & G_UNIQ) != 0 && mndx != PM_HIGH_CLERIC)
966 svm.mvitals[mndx].mvflags |= G_EXTINCT;
968 if (svm.mvitals[mndx].born < 255 && tally && (!ghostly || result))
969 svm.mvitals[mndx].born++;
970 if ((int) svm.mvitals[mndx].born >= lim
971 && !(mons[mndx].geno & G_NOGEN)
972 && !(svm.mvitals[mndx].mvflags & G_EXTINCT)) {
973 if (wizard) {
974 debugpline1("Automatically extinguished %s.",
975 makeplural(mons[mndx].pmnames[NEUTRAL]));
977 svm.mvitals[mndx].mvflags |= G_EXTINCT;
979 return result;
982 /* amount of HP to lose from level drain (or gain from Stormbringer) */
984 monhp_per_lvl(struct monst *mon)
986 struct permonst *ptr = mon->data;
987 int hp = rnd(8); /* default is d8 */
989 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
990 if (is_golem(ptr)) {
991 /* draining usually won't be applicable for these critters */
992 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
993 } else if (ptr->mlevel > 49) {
994 /* arbitrary; such monsters won't be involved in draining anyway */
995 hp = 4 + rnd(4); /* 5..8 */
996 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
997 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
999 hp = 4 + rn2(5); /* 4..8 */
1000 } else if (!mon->m_lev) {
1001 /* level 0 monsters use 1d4 instead of Nd8 */
1002 hp = rnd(4);
1004 return hp;
1007 /* set up a new monster's initial level and hit points;
1008 used by newcham() as well as by makemon() */
1009 void
1010 newmonhp(struct monst *mon, int mndx)
1012 struct permonst *ptr = &mons[mndx];
1013 int basehp = 0;
1015 mon->m_lev = adj_lev(ptr);
1016 if (is_golem(ptr)) {
1017 /* golems have a fixed amount of HP, varying by golem type */
1018 mon->mhpmax = mon->mhp = golemhp(mndx);
1019 } else if (is_rider(ptr)) {
1020 /* we want low HP, but a high mlevel so they can attack well */
1021 basehp = 10; /* minimum is 1 per false (weaker) level */
1022 mon->mhpmax = mon->mhp = d(basehp, 8);
1023 } else if (ptr->mlevel > 49) {
1024 /* "special" fixed hp monster
1025 * the hit points are encoded in the mlevel in a somewhat strange
1026 * way to fit in the 50..127 positive range of a signed character
1027 * above the 1..49 that indicate "normal" monster levels */
1028 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
1029 mon->m_lev = mon->mhp / 4; /* approximation */
1030 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
1031 /* adult dragons; N*(4+rnd(4)) before endgame, N*8 once there */
1032 basehp = (int) mon->m_lev; /* not really applicable; isolates cast */
1033 mon->mhpmax = mon->mhp = In_endgame(&u.uz) ? (8 * basehp)
1034 : (4 * basehp + d(basehp, 4));
1035 } else if (!mon->m_lev) {
1036 basehp = 1; /* minimum is 1, increased to 2 below */
1037 mon->mhpmax = mon->mhp = rnd(4);
1038 } else {
1039 basehp = (int) mon->m_lev; /* minimum possible is one per level */
1040 mon->mhpmax = mon->mhp = d(basehp, 8);
1041 if (is_home_elemental(ptr))
1042 mon->mhpmax = (mon->mhp *= 3); /* leave 'basehp' as-is */
1045 /* if d(X,8) rolled a 1 all X times, give a boost;
1046 most beneficial for level 0 and level 1 monsters, making mhpmax
1047 and starting mhp always be at least 2 */
1048 if (mon->mhpmax == basehp) {
1049 mon->mhpmax += 1;
1050 mon->mhp = mon->mhpmax;
1054 static const struct mextra zeromextra = DUMMY;
1056 staticfn void
1057 init_mextra(struct mextra *mex)
1059 *mex = zeromextra;
1060 mex->mcorpsenm = NON_PM;
1063 struct mextra *
1064 newmextra(void)
1066 struct mextra *mextra;
1068 mextra = (struct mextra *) alloc(sizeof (struct mextra));
1069 init_mextra(mextra);
1070 return mextra;
1073 staticfn boolean
1074 makemon_rnd_goodpos(
1075 struct monst *mon,
1076 mmflags_nht gpflags,
1077 coord *cc) /* output */
1079 int tryct = 0;
1080 coordxy nx, ny;
1081 boolean good;
1083 gpflags |= GP_AVOID_MONPOS;
1084 do {
1085 nx = rn1(COLNO - 3, 2);
1086 ny = rn2(ROWNO);
1087 good = (!gi.in_mklev && cansee(nx,ny)) ? FALSE
1088 : goodpos(nx, ny, mon, gpflags);
1089 } while ((++tryct < 50) && !good);
1091 if (!good) {
1092 /* else go through all map positions, twice, first round
1093 ignoring positions in sight, and pick first good one.
1094 skip first round if we're in special level loader or blind */
1095 coordxy xofs = nx;
1096 coordxy yofs = ny;
1097 coordxy dx,dy;
1098 int bl = (gi.in_mklev || Blind) ? 1 : 0;
1100 for ( ; bl < 2; bl++) {
1101 if (!bl)
1102 gpflags &= ~GP_CHECKSCARY; /* perhaps should be a 3rd pass */
1103 for (dx = 0; dx < COLNO; dx++)
1104 for (dy = 0; dy < ROWNO; dy++) {
1105 nx = ((dx + xofs) % (COLNO - 1)) + 1;
1106 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
1107 if (bl == 0 && cansee(nx,ny))
1108 continue;
1109 if (goodpos(nx, ny, mon, gpflags))
1110 goto gotgood;
1112 if (bl == 0 && (!mon || mon->data->mmove)) {
1113 stairway *stway = gs.stairs;
1114 /* all map positions are visible (or not good),
1115 try to pick something logical */
1116 while (stway) {
1117 if (stway->tolev.dnum == u.uz.dnum && !rn2(2)) {
1118 nx = stway->sx;
1119 ny = stway->sy;
1120 break;
1122 stway = stway->next;
1124 if (goodpos(nx, ny, mon, gpflags))
1125 goto gotgood;
1128 } else {
1129 gotgood:
1130 cc->x = nx;
1131 cc->y = ny;
1132 return TRUE;
1134 return FALSE;
1138 * called with [x,y] = coordinates;
1139 * [0,0] means anyplace
1140 * [u.ux,u.uy] means: near player (if !gi.in_mklev)
1142 * In case we make a monster group, only return the one at [x,y].
1144 struct monst *
1145 makemon(
1146 struct permonst *ptr,
1147 coordxy x, coordxy y,
1148 mmflags_nht mmflags)
1150 struct monst *mtmp;
1151 struct monst fakemon;
1152 coord cc;
1153 int mndx, mcham, ct, mitem;
1154 boolean femaleok, maleok,
1155 anymon = !ptr,
1156 byyou = u_at(x, y),
1157 allow_minvent = ((mmflags & NO_MINVENT) == 0),
1158 countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0),
1159 allowtail = ((mmflags & MM_NOTAIL) == 0);
1160 mmflags_nht gpflags = (((mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0)
1161 | GP_CHECKSCARY | GP_AVOID_MONPOS);
1163 fakemon = cg.zeromonst;
1164 cc.x = cc.y = 0;
1166 if (iflags.debug_mongen || (!svl.level.flags.rndmongen && !ptr))
1167 return (struct monst *) 0;
1169 /* if caller wants random location, do it here */
1170 if (x == 0 && y == 0) {
1171 fakemon.data = ptr; /* set up for goodpos */
1172 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *) 0,
1173 gpflags, &cc))
1174 return (struct monst *) 0;
1175 x = cc.x;
1176 y = cc.y;
1177 } else if (byyou && !gi.in_mklev) {
1178 if (!enexto_core(&cc, u.ux, u.uy, ptr, gpflags)
1179 && !enexto_core(&cc, u.ux, u.uy, ptr, gpflags & ~GP_CHECKSCARY))
1180 return (struct monst *) 0;
1181 x = cc.x;
1182 y = cc.y;
1185 /* sanity check */
1186 if (!isok(x, y)) {
1187 impossible("makemon trying to create a monster at <%d,%d>?", x, y);
1188 return (struct monst *) 0;
1191 /* Does monster already exist at the position? */
1192 if (MON_AT(x, y)) {
1193 if (!(mmflags & MM_ADJACENTOK)
1194 || !enexto_core(&cc, x, y, ptr, gpflags))
1195 return (struct monst *) 0;
1196 x = cc.x;
1197 y = cc.y;
1200 if (ptr) {
1201 mndx = monsndx(ptr);
1202 /* if you are to make a specific monster and it has
1203 already been genocided, return */
1204 if (svm.mvitals[mndx].mvflags & G_GENOD)
1205 return (struct monst *) 0;
1206 if (wizard && (svm.mvitals[mndx].mvflags & G_EXTINCT)) {
1207 debugpline1("Explicitly creating extinct monster %s.",
1208 mons[mndx].pmnames[NEUTRAL]);
1210 } else {
1211 /* make a random (common) monster that can survive here.
1212 * (the special levels ask for random monsters at specific
1213 * positions, causing mass drowning on the medusa level,
1214 * for instance.)
1216 int tryct = 0; /* maybe there are no good choices */
1218 do {
1219 if (!(ptr = rndmonst())) {
1220 debugpline0("Warning: no monster.");
1221 return (struct monst *) 0; /* no more monsters! */
1223 fakemon.data = ptr; /* set up for goodpos */
1224 } while (++tryct <= 50
1225 /* in Sokoban, don't accept a giant on first try;
1226 after that, boulder carriers are fair game */
1227 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1228 || !goodpos(x, y, &fakemon, gpflags)));
1229 mndx = monsndx(ptr);
1231 (void) propagate(mndx, countbirth, FALSE);
1232 mtmp = newmonst();
1233 *mtmp = cg.zeromonst; /* clear all entries in structure */
1235 if (mmflags & MM_EGD)
1236 newegd(mtmp);
1237 if (mmflags & MM_EPRI)
1238 newepri(mtmp);
1239 if (mmflags & MM_ESHK)
1240 neweshk(mtmp);
1241 if (mmflags & MM_EMIN)
1242 newemin(mtmp);
1243 if (mmflags & MM_EDOG)
1244 newedog(mtmp);
1245 if (mmflags & MM_ASLEEP)
1246 mtmp->msleeping = 1;
1247 mtmp->nmon = fmon;
1248 fmon = mtmp;
1249 mtmp->m_id = next_ident();
1250 set_mon_data(mtmp, ptr); /* mtmp->data = ptr; */
1251 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1252 svq.quest_status.leader_m_id = mtmp->m_id;
1253 mtmp->mnum = mndx;
1255 /* set up level and hit points */
1256 newmonhp(mtmp, mndx);
1258 femaleok = (!is_male(ptr) && !is_neuter(ptr));
1259 maleok = (!is_female(ptr) && !is_neuter(ptr));
1260 if (is_female(ptr) || ((mmflags & MM_FEMALE) != 0 && femaleok))
1261 mtmp->female = 1;
1262 else if (is_male(ptr) || ((mmflags & MM_MALE) != 0 && maleok))
1263 mtmp->female = 0;
1265 /* leader and nemesis gender is usually hardcoded in mons[],
1266 but for ones which can be random, it has already been chosen
1267 (in role_init(), for possible use by the quest pager code) */
1268 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1269 mtmp->female = svq.quest_status.ldrgend;
1270 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1271 mtmp->female = svq.quest_status.nemgend;
1273 /* female used to be set randomly here even for neuters on the
1274 grounds that it was ignored, but after corpses were changed to
1275 retain gender it matters because it affects stacking of corpses */
1276 else
1277 mtmp->female = femaleok ? rn2(2) : 0;
1279 if (In_sokoban(&u.uz) && !mindless(ptr)) { /* know about traps here */
1280 mon_learns_traps(mtmp, PIT);
1281 mon_learns_traps(mtmp, HOLE);
1283 if (Is_stronghold(&u.uz) && !mindless(ptr)) /* know about trap doors */
1284 mon_learns_traps(mtmp, TRAPDOOR);
1285 /* quest leader and nemesis both know about all trap types */
1286 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1287 mon_learns_traps(mtmp, ALL_TRAPS);
1289 place_monster(mtmp, x, y);
1290 mtmp->mcansee = mtmp->mcanmove = TRUE;
1291 mtmp->seen_resistance = M_SEEN_NOTHING;
1292 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1293 if ((mmflags & MM_MINVIS) != 0) /* for ^G */
1294 mon_set_minvis(mtmp); /* call after place_monster() */
1296 switch (ptr->mlet) {
1297 case S_MIMIC:
1298 set_mimic_sym(mtmp);
1299 break;
1300 case S_SPIDER:
1301 case S_SNAKE:
1302 if (gi.in_mklev) {
1303 if (x && y)
1304 (void) mkobj_at(RANDOM_CLASS, x, y, TRUE);
1305 (void) hideunder(mtmp);
1307 break;
1308 case S_LIGHT:
1309 case S_ELEMENTAL:
1310 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1311 mtmp->perminvis = TRUE;
1312 mtmp->minvis = TRUE;
1314 break;
1315 case S_EEL:
1316 if (gi.in_mklev) {
1317 (void) hideunder(mtmp);
1319 break;
1320 case S_LEPRECHAUN:
1321 mtmp->msleeping = 1;
1322 break;
1323 case S_JABBERWOCK:
1324 case S_NYMPH:
1325 if (rn2(5) && !u.uhave.amulet)
1326 mtmp->msleeping = 1;
1327 break;
1328 case S_ORC:
1329 if (Race_if(PM_ELF))
1330 mtmp->mpeaceful = FALSE;
1331 break;
1332 case S_UNICORN:
1333 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1334 mtmp->mpeaceful = TRUE;
1335 break;
1336 case S_BAT:
1337 if (Inhell && is_bat(ptr))
1338 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1339 break;
1341 if ((ct = emits_light(mtmp->data)) > 0)
1342 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1343 monst_to_any(mtmp));
1344 mitem = STRANGE_OBJECT; /* extra inventory item for this monster */
1346 if (mndx == PM_VLAD_THE_IMPALER)
1347 mitem = CANDELABRUM_OF_INVOCATION;
1348 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1349 if (!Protection_from_shape_changers
1350 && (mcham = pm_to_cham(mndx)) != NON_PM) {
1351 /* this is a shapechanger after all */
1352 mtmp->cham = mcham;
1353 /* Vlad stays in his normal shape so he can carry the Candelabrum */
1354 if (mndx != PM_VLAD_THE_IMPALER
1355 /* Note: shapechanger's initial form used to be chosen here
1356 with rndmonst(), yielding a monster which was appropriate
1357 to the level's difficulty but ignoring the changer's usual
1358 type selection, so was inappropriate for vampshifters.
1359 Let newcham() pick the shape. */
1360 && newcham(mtmp, (struct permonst *) 0, NO_NC_FLAGS))
1361 allow_minvent = FALSE;
1362 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1363 mtmp->iswiz = TRUE;
1364 svc.context.no_of_wizards++;
1365 if (svc.context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1366 mitem = SPE_DIG;
1367 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1368 mtmp = christen_monst(mtmp, rndghostname());
1369 } else if (mndx == PM_CROESUS) {
1370 mitem = TWO_HANDED_SWORD;
1371 } else if (ptr->msound == MS_NEMESIS) {
1372 mitem = BELL_OF_OPENING;
1373 } else if (mndx == PM_PESTILENCE) {
1374 mitem = POT_SICKNESS;
1376 if (mitem != STRANGE_OBJECT && allow_minvent)
1377 (void) mongets(mtmp, mitem);
1379 if (gi.in_mklev) {
1380 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1381 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1382 && !u.uhave.amulet && rn2(5))
1383 mtmp->msleeping = TRUE;
1384 } else {
1385 if (byyou) {
1386 newsym(mtmp->mx, mtmp->my);
1387 set_apparxy(mtmp);
1390 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1391 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1392 mtmp->mavenge = 0;
1393 if (u_wield_art(ART_EXCALIBUR) || u_wield_art(ART_DEMONBANE))
1394 mtmp->mpeaceful = mtmp->mtame = FALSE;
1396 if (mndx == PM_RAVEN && uwep && uwep->otyp == BEC_DE_CORBIN)
1397 mtmp->mpeaceful = TRUE;
1398 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0) {
1399 initworm(mtmp, allowtail ? rn2(5) : 0);
1400 if (count_wsegs(mtmp))
1401 place_worm_tail_randomly(mtmp, x, y);
1403 /* it's possible to create an ordinary monster of some special
1404 types; make sure their extended data is initialized to
1405 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1406 (when they're specified, caller intends to handle this itself) */
1407 if ((mndx == PM_ALIGNED_CLERIC || mndx == PM_HIGH_CLERIC)
1408 ? !(mmflags & (MM_EPRI | MM_EMIN))
1409 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1410 struct emin *eminp;
1412 newemin(mtmp);
1413 eminp = EMIN(mtmp);
1415 mtmp->isminion = 1; /* make priest be a roamer */
1416 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1417 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1418 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1419 ? !eminp->renegade
1420 : eminp->renegade;
1422 set_malign(mtmp); /* having finished peaceful changes */
1423 if (anymon && !(mmflags & MM_NOGRP)) {
1424 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1425 m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1426 } else if (ptr->geno & G_LGROUP) {
1427 if (rn2(3))
1428 m_initlgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1429 else
1430 m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags);
1434 if (allow_minvent) {
1435 if (is_armed(ptr))
1436 m_initweap(mtmp); /* equip with weapons / armor */
1437 m_initinv(mtmp); /* add on a few special items incl. more armor */
1438 m_dowear(mtmp, TRUE);
1440 if (!rn2(100) && is_domestic(ptr)
1441 && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) {
1442 /* NULL obj arg means put_saddle_on_mon()
1443 * will create the saddle itself */
1444 put_saddle_on_mon((struct obj *) 0, mtmp);
1447 } else {
1448 /* no initial inventory is allowed */
1449 if (mtmp->minvent)
1450 discard_minvent(mtmp, TRUE);
1451 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1453 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1454 if (ptr->mflags3 & M3_WAITFORU)
1455 mtmp->mstrategy |= STRAT_WAITFORU;
1456 if (ptr->mflags3 & M3_CLOSE)
1457 mtmp->mstrategy |= STRAT_CLOSE;
1458 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1459 mtmp->mstrategy |= STRAT_APPEARMSG;
1462 if (allow_minvent && gm.migrating_objs)
1463 deliver_obj_to_mon(mtmp, 1, DF_NONE); /* in case of waiting items */
1465 if (!gi.in_mklev) {
1466 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1467 if (!(mmflags & MM_NOMSG)) {
1468 char mbuf[BUFSZ], *what = 0;
1469 /* MM_NOEXCLAM is used for #wizgenesis (^G) */
1470 boolean exclaim = !(mmflags & MM_NOEXCLAM);
1472 if ((canseemon(mtmp) && (M_AP_TYPE(mtmp) == M_AP_NOTHING
1473 || M_AP_TYPE(mtmp) == M_AP_MONSTER))
1474 || sensemon(mtmp)) {
1475 what = Amonnam(mtmp);
1476 if (M_AP_TYPE(mtmp) == M_AP_MONSTER)
1477 exclaim = TRUE;
1478 } else if (canseemon(mtmp)) {
1479 /* mimic masquerading as furniture or object and not sensed */
1480 mhidden_description(mtmp, MHID_ARTICLE | MHID_ALTMON, mbuf);
1481 what = upstart(mbuf);
1483 if (what) {
1484 set_msg_xy(mtmp->mx, mtmp->my);
1485 Norep("%s%s appears%s%c", what,
1486 exclaim ? " suddenly" : "",
1487 next2u(x, y) ? " next to you"
1488 : (distu(x, y) <= (BOLT_LIM * BOLT_LIM)) ? " close by"
1489 : "",
1490 exclaim ? '!' : '.');
1493 /* if discernable and a threat, stop fiddling while Rome burns */
1494 if (go.occupation)
1495 (void) dochugw(mtmp, FALSE);
1497 /* TODO: unify with teleport appears msg */
1500 return mtmp;
1503 /* caller rejects makemon()'s result; always returns Null */
1504 struct monst *
1505 unmakemon(
1506 struct monst *mon,
1507 mmflags_nht mmflags)
1509 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1510 int mndx = monsndx(mon->data);
1512 /* if count has reached the limit of 255, we don't know whether
1513 that just happened when creating this monster or the threshold
1514 had already been reached and further increments were suppressed;
1515 assume the latter */
1516 if (countbirth && svm.mvitals[mndx].born > 0
1517 && svm.mvitals[mndx].born < 255)
1518 svm.mvitals[mndx].born -= 1;
1519 if ((mon->data->geno & G_UNIQ) != 0)
1520 svm.mvitals[mndx].mvflags &= ~G_EXTINCT;
1522 mon->mhp = 0; /* let discard_minvent() know that mon isn't being kept */
1523 /* uncreate any artifact that the monster was provided with; unlike
1524 mongone(), this doesn't protect special items like the Amulet
1525 by dropping them so caller should handle them when applicable */
1526 discard_minvent(mon, TRUE);
1528 mongone(mon);
1529 return (struct monst *) 0;
1533 mbirth_limit(int mndx)
1535 /* There is an implicit limit of 4 for "high priest of <deity>",
1536 * but aligned priests can grow into high priests, thus they aren't
1537 * really limited to 4, so leave the default amount in place for them.
1540 /* assert(MAXMONNO < 255); */
1541 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1544 /* used for wand/scroll/spell of create monster */
1545 /* returns TRUE iff you know monsters have been created */
1546 boolean
1547 create_critters(
1548 int cnt,
1549 struct permonst *mptr, /* usually null; used for confused reading */
1550 boolean neverask)
1552 coord c;
1553 coordxy x, y;
1554 struct monst *mon;
1555 boolean known = FALSE;
1556 boolean ask = (wizard && !neverask);
1558 while (cnt--) {
1559 if (ask) {
1560 if (create_particular()) {
1561 known = TRUE;
1562 continue;
1563 } else
1564 ask = FALSE; /* ESC will shut off prompting */
1566 x = u.ux, y = u.uy;
1567 /* if in water, try to encourage an aquatic monster
1568 by finding and then specifying another wet location */
1569 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1570 x = c.x, y = c.y;
1572 if ((mon = makemon(mptr, x, y, NO_MM_FLAGS)) == 0)
1573 continue; /* try again [should probably stop instead] */
1575 if ((canseemon(mon) && (M_AP_TYPE(mon) == M_AP_NOTHING
1576 || M_AP_TYPE(mon) == M_AP_MONSTER))
1577 || sensemon(mon))
1578 known = TRUE;
1580 return known;
1583 staticfn boolean
1584 uncommon(int mndx)
1586 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1587 return TRUE;
1588 if (svm.mvitals[mndx].mvflags & G_GONE)
1589 return TRUE;
1590 if (Inhell)
1591 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1592 else
1593 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1597 * shift the probability of a monster's generation by
1598 * comparing the dungeon alignment and monster alignment.
1599 * return an integer in the range of 0-5.
1601 staticfn int
1602 align_shift(struct permonst *ptr)
1604 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1605 static NEARDATA s_level *lev;
1606 int alshift;
1608 if (oldmoves != svm.moves) {
1609 lev = Is_special(&u.uz);
1610 oldmoves = svm.moves;
1612 switch ((lev) ? lev->flags.align : svd.dungeons[u.uz.dnum].flags.align) {
1613 default: /* just in case */
1614 case AM_NONE:
1615 alshift = 0;
1616 break;
1617 case AM_LAWFUL:
1618 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1619 break;
1620 case AM_NEUTRAL:
1621 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1622 break;
1623 case AM_CHAOTIC:
1624 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1625 break;
1627 return alshift;
1630 /* return larger value if monster prefers the level temperature */
1631 staticfn int
1632 temperature_shift(struct permonst *ptr)
1634 if (svl.level.flags.temperature
1635 && pm_resistance(ptr, (svl.level.flags.temperature > 0)
1636 ? MR_FIRE : MR_COLD))
1637 return 3;
1638 return 0;
1641 /* select a random monster type */
1642 struct permonst *
1643 rndmonst(void)
1645 return rndmonst_adj(0, 0);
1648 /* select a random monster type, with adjusted difficulty */
1649 struct permonst *
1650 rndmonst_adj(int minadj, int maxadj)
1652 struct permonst *ptr;
1653 int mndx;
1654 int weight, totalweight, selected_mndx, zlevel, minmlev, maxmlev;
1655 boolean elemlevel, upper;
1657 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1658 return ptr;
1660 zlevel = level_difficulty();
1661 minmlev = monmin_difficulty(zlevel) + minadj;
1662 maxmlev = monmax_difficulty(zlevel) + maxadj;
1663 upper = Is_rogue_level(&u.uz); /* prefer uppercase only on rogue level */
1664 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz); /* elmntl plane */
1666 /* amount processed so far */
1667 totalweight = 0;
1668 selected_mndx = NON_PM;
1670 for (mndx = LOW_PM; mndx < SPECIAL_PM; ++mndx) {
1671 ptr = &mons[mndx];
1673 if (montooweak(mndx, minmlev) || montoostrong(mndx, maxmlev))
1674 continue;
1675 if (upper && !isupper(monsym(ptr)))
1676 continue;
1677 if (elemlevel && wrong_elem_type(ptr))
1678 continue;
1679 if (uncommon(mndx))
1680 continue;
1681 if (Inhell && (ptr->geno & G_NOHELL))
1682 continue;
1685 * Weighted reservoir sampling: select ptr with a
1686 * (ptr weight)/(total of all weights so far including ptr's)
1687 * probability. For example, if the previous total is 10, and
1688 * this is now looking at acid blobs with a frequency of 2, it
1689 * has a 2/12 chance of abandoning ptr's previous value in favor
1690 * of acid blobs, and 10/12 chance of keeping whatever it was.
1692 * This does not bias results towards either the earlier or the
1693 * later monsters: the smaller pool and better odds from being
1694 * earlier are exactly canceled out by having more monsters to
1695 * potentially steal its spot.
1697 weight = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1698 weight += temperature_shift(ptr);
1699 if (weight < 0 || weight > 127) {
1700 impossible("bad weight in rndmonst for mndx %d", mndx);
1701 weight = 0;
1703 /* was unconditional, but if weight==0, rn2() < 0 will always fail;
1704 also need to avoid rn2(0) if totalweight is still 0 so far */
1705 if (weight > 0) {
1706 totalweight += weight; /* totalweight now guaranteed to be > 0 */
1707 if (rn2(totalweight) < weight)
1708 selected_mndx = mndx;
1712 * Possible modification: if totalweight is "too low" or nothing
1713 * viable was picked, expand minmlev..maxmlev range and try again.
1715 if (selected_mndx == NON_PM || uncommon(selected_mndx)) {
1716 /* maybe no common monsters left, or all are too weak or too strong */
1717 if (selected_mndx != NON_PM)
1718 debugpline1("rndmonst returning Null [uncommon 'mndx'=#%d]",
1719 selected_mndx);
1720 return (struct permonst *) 0;
1722 return &mons[selected_mndx];
1725 /* decide whether it's ok to generate a candidate monster by mkclass() */
1726 staticfn boolean
1727 mk_gen_ok(int mndx, unsigned mvflagsmask, unsigned genomask)
1729 struct permonst *ptr = &mons[mndx];
1731 if (svm.mvitals[mndx].mvflags & mvflagsmask)
1732 return FALSE;
1733 if (ptr->geno & genomask)
1734 return FALSE;
1735 if (is_placeholder(ptr))
1736 return FALSE;
1737 #ifdef MAIL_STRUCTURES
1738 /* special levels might ask for random demon type; reject this one */
1739 if (ptr == &mons[PM_MAIL_DAEMON])
1740 return FALSE;
1741 #endif
1742 return TRUE;
1745 /* Make one of the multiple types of a given monster class.
1746 The second parameter specifies a special casing bit mask
1747 to allow the normal genesis masks to be deactivated.
1748 Returns Null if no monsters in that class can be made. */
1749 struct permonst *
1750 mkclass(char class, int spc)
1752 return mkclass_aligned(class, spc, A_NONE);
1755 /* mkclass() with alignment restrictions; used by ndemon() */
1756 struct permonst *
1757 mkclass_aligned(char class, int spc, /* special mons[].geno handling */
1758 aligntyp atyp)
1760 int first, last, num = 0;
1761 int k, nums[SPECIAL_PM + 1]; /* +1: insurance for final return value */
1762 int maxmlev, gehennom = Inhell != 0;
1763 unsigned mv_mask, gn_mask;
1765 (void) memset((genericptr_t) nums, 0, sizeof nums);
1766 maxmlev = level_difficulty() >> 1;
1767 if (class < 1 || class >= MAXMCLASSES) {
1768 impossible("mkclass called with bad class!");
1769 return (struct permonst *) 0;
1771 /* Assumption #1: monsters of a given class are contiguous in the
1772 * mons[] array. Player monsters and quest denizens
1773 * are an exception; mkclass() won't pick them.
1774 * SPECIAL_PM is long worm tail and separates the
1775 * regular monsters from the exceptions.
1777 for (first = LOW_PM; first < SPECIAL_PM; first++)
1778 if (mons[first].mlet == class)
1779 break;
1780 if (first == SPECIAL_PM) {
1781 impossible("mkclass found no class %d monsters", class);
1782 return (struct permonst *) 0;
1785 mv_mask = G_GONE; /* G_GENOD | G_EXTINCT */
1786 if ((spc & G_IGNORE) != 0) {
1787 mv_mask = 0; /* mv_mask &= ~G_GONE; */
1788 /* G_IGNORE is not a mons[].geno mask so get rid of it now */
1789 spc &= ~G_IGNORE;
1792 /* Assumption #2: monsters of a given class are presented in ascending
1793 * order of strength.
1795 for (last = first; last < SPECIAL_PM && mons[last].mlet == class;
1796 last++) {
1797 if (atyp != A_NONE && sgn(mons[last].maligntyp) != sgn(atyp))
1798 continue;
1799 /* traditionally mkclass() ignored hell-only and never-in-hell;
1800 now we usually honor those but not all the time, mostly so that
1801 the majority of major demons aren't constrained to Gehennom;
1802 arch- and master liches are always so constrained (for creation;
1803 lesser liches might grow up into them elsewhere) */
1804 gn_mask = (G_NOGEN | G_UNIQ);
1805 if (rn2(9) || class == S_LICH)
1806 gn_mask |= (gehennom ? G_NOHELL : G_HELL);
1807 gn_mask &= ~spc;
1809 if (mk_gen_ok(last, mv_mask, gn_mask)) {
1810 /* consider it; don't reject a toostrong() monster if we
1811 don't have anything yet (num==0) or if it is the same
1812 (or lower) difficulty as preceding candidate (non-zero
1813 'num' implies last > first so mons[last-1] is safe);
1814 sometimes accept it even if high difficulty */
1815 if (num && montoostrong(last, maxmlev)
1816 && mons[last].difficulty > mons[last - 1].difficulty
1817 && rn2(2))
1818 break;
1819 if ((k = (mons[last].geno & G_FREQ)) > 0) {
1820 /* skew towards lower value monsters at lower exp. levels
1821 (this used to be done in the next loop, but that didn't
1822 work well when multiple species had the same level and
1823 were followed by one that was past the bias threshold;
1824 cited example was succubus and incubus, where the bias
1825 against picking the next demon resulted in incubus
1826 being picked nearly twice as often as succubus);
1827 we need the '+1' in case the entire set is too high
1828 level (really low svl.level hero) */
1829 nums[last] = k + 1 - (adj_lev(&mons[last]) > (u.ulevel * 2));
1830 num += nums[last];
1834 if (!num)
1835 return (struct permonst *) 0;
1837 /* the hard work has already been done; 'num' should hit 0 before
1838 first reaches last (which is actually one past our last candidate) */
1839 for (num = rnd(num); first < last; first++)
1840 if ((num -= nums[first]) <= 0)
1841 break;
1843 return nums[first] ? &mons[first] : (struct permonst *) 0;
1846 /* like mkclass(), but excludes difficulty considerations; used when
1847 player with polycontrol picks a class instead of a specific type;
1848 genocided types are avoided but extinct ones are acceptable; we don't
1849 check polyok() here--caller accepts some choices !polyok() would reject */
1851 mkclass_poly(int class)
1853 int first, last, num = 0;
1854 unsigned gmask;
1856 for (first = LOW_PM; first < SPECIAL_PM; first++)
1857 if (mons[first].mlet == class)
1858 break;
1859 if (first == SPECIAL_PM)
1860 return NON_PM;
1862 gmask = (G_NOGEN | G_UNIQ);
1863 /* mkclass() does this on a per monster type basis, but doing that here
1864 would make the two loops inconsistent with each other for non L */
1865 if (rn2(9) || class == S_LICH)
1866 gmask |= (Inhell ? G_NOHELL : G_HELL);
1868 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1869 if (mk_gen_ok(last, G_GENOD, gmask))
1870 num += mons[last].geno & G_FREQ;
1871 if (!num)
1872 return NON_PM;
1874 for (num = rnd(num); num > 0; first++)
1875 if (mk_gen_ok(first, G_GENOD, gmask))
1876 num -= mons[first].geno & G_FREQ;
1877 first--; /* correct an off-by-one error */
1879 return first;
1882 /* adjust strength of monsters based on u.uz and u.ulevel */
1884 adj_lev(struct permonst *ptr)
1886 int tmp, tmp2;
1888 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1889 /* does not depend on other strengths, but does get stronger
1890 * every time he is killed
1892 tmp = ptr->mlevel + svm.mvitals[PM_WIZARD_OF_YENDOR].died;
1893 if (tmp > 49)
1894 tmp = 49;
1895 return tmp;
1898 if ((tmp = ptr->mlevel) > 49)
1899 return 50; /* "special" demons/devils */
1900 tmp2 = (level_difficulty() - tmp);
1901 if (tmp2 < 0)
1902 tmp--; /* if mlevel > u.uz decrement tmp */
1903 else
1904 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1906 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1907 if (tmp2 > 0)
1908 tmp += (tmp2 / 4); /* level as well */
1910 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1911 if (tmp2 > 49)
1912 tmp2 = 49; /* hard upper limit */
1913 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1916 /* monster earned experience and will gain some hit points; it might also
1917 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1918 struct permonst *
1919 grow_up(struct monst *mtmp, struct monst *victim)
1921 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1922 unsigned fem;
1923 struct permonst *ptr = mtmp->data;
1925 /* monster died after killing enemy but before calling this function */
1926 /* currently possible if killing a gas spore */
1927 if (DEADMONSTER(mtmp))
1928 return (struct permonst *) 0;
1930 /* note: none of the monsters with special hit point calculations
1931 have both little and big forms (killer bee can't grow into queen
1932 bee by just killing things, so isn't in the little_to_big list) */
1933 oldtype = monsndx(ptr);
1934 newtype = (oldtype == PM_KILLER_BEE && !victim) ? PM_QUEEN_BEE
1935 : little_to_big(oldtype);
1936 #if 0
1937 /* gender-neutral PM_CLERIC now */
1938 if (newtype == PM_PRIEST && mtmp->female)
1939 newtype = PM_PRIESTESS;
1940 #endif
1942 /* growth limits differ depending on method of advancement */
1943 if (victim) { /* killed a monster */
1945 * The HP threshold is the maximum number of hit points for the
1946 * current level; once exceeded, a level will be gained.
1947 * Possible bug: if somehow the hit points are already higher
1948 * than that, monster will gain a level without any increase in HP.
1950 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1951 if (!mtmp->m_lev)
1952 hp_threshold = 4;
1953 else if (is_golem(ptr)) /* strange creatures */
1954 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1955 else if (is_home_elemental(ptr))
1956 hp_threshold *= 3;
1957 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1958 /* If they can grow up, be sure the level is high enough for that */
1959 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1960 lev_limit = (int) mons[newtype].mlevel;
1961 /* number of hit points to gain; unlike for the player, we put
1962 the limit at the bottom of the next level rather than the top */
1963 max_increase = rnd((int) victim->m_lev + 1);
1964 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1965 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1966 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1967 } else {
1968 /* a gain level potion or wraith corpse; always go up a level
1969 unless already at maximum (49 is hard upper limit except
1970 for demon lords, who start at 50 and can't go any higher) */
1971 max_increase = cur_increase = rnd(8);
1972 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1973 lev_limit = 50; /* recalc below */
1976 mtmp->mhpmax += max_increase;
1977 mtmp->mhp += cur_increase;
1978 if (mtmp->mhpmax <= hp_threshold)
1979 return ptr; /* doesn't gain a level */
1981 if (is_mplayer(ptr))
1982 lev_limit = 30; /* same as player */
1983 else if (lev_limit < 5)
1984 lev_limit = 5; /* arbitrary */
1985 else if (lev_limit > 49)
1986 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1988 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1989 ptr = &mons[newtype];
1990 /* new form might force gender change */
1991 fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female;
1993 if (svm.mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1994 if (canspotmon(mtmp))
1995 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1996 an(pmname(ptr, Mgender(mtmp))), mhe(mtmp),
1997 nonliving(ptr) ? "expires" : "dies");
1998 set_mon_data(mtmp, ptr); /* keep svm.mvitals[] accurate */
1999 mondied(mtmp);
2000 return (struct permonst *) 0;
2001 } else if (canspotmon(mtmp)) {
2002 char buf[BUFSZ];
2004 /* 3.6.1:
2005 * Temporary (?) hack to fix growing into opposite gender.
2007 Sprintf(buf, "%s%s",
2008 /* deal with female gnome becoming a gnome lord */
2009 (mtmp->female && !fem) ? "male "
2010 /* or a male gnome becoming a gnome lady
2011 (can't happen with 3.6.0 mons[], but perhaps
2012 slightly less sexist if prepared for it...) */
2013 : (fem && !mtmp->female) ? "female " : "",
2014 pmname(ptr, fem));
2015 pline_mon(mtmp, "%s %s %s.", YMonnam(mtmp),
2016 (fem != mtmp->female) ? "changes into"
2017 : humanoid(ptr) ? "becomes"
2018 : "grows up into",
2019 an(buf));
2021 set_mon_data(mtmp, ptr);
2022 if (mtmp->cham == oldtype && is_shapeshifter(ptr))
2023 mtmp->cham = newtype; /* vampire growing into vampire lord */
2024 newsym(mtmp->mx, mtmp->my); /* color may change */
2025 lev_limit = (int) mtmp->m_lev; /* never undo increment */
2027 mtmp->female = fem; /* gender might be changing */
2028 /* if 'mtmp' is leashed, persistent inventory window needs updating */
2029 if (mtmp->mleashed)
2030 update_inventory(); /* x - leash (attached to a <mon>) */
2033 /* sanity checks */
2034 if ((int) mtmp->m_lev > lev_limit) {
2035 mtmp->m_lev--; /* undo increment */
2036 /* HP might have been allowed to grow when it shouldn't */
2037 if (mtmp->mhpmax == hp_threshold + 1)
2038 mtmp->mhpmax--;
2040 if (mtmp->mhpmax > 50 * 8)
2041 mtmp->mhpmax = 50 * 8; /* absolute limit */
2042 if (mtmp->mhp > mtmp->mhpmax)
2043 mtmp->mhp = mtmp->mhpmax;
2045 return ptr;
2048 struct obj *
2049 mongets(struct monst *mtmp, int otyp)
2051 struct obj *otmp;
2053 if (!otyp)
2054 return (struct obj *) 0;
2055 otmp = mksobj(otyp, TRUE, FALSE);
2056 if (otmp) {
2057 if (mtmp->data->mlet == S_DEMON) {
2058 /* demons never get blessed objects */
2059 if (otmp->blessed)
2060 curse(otmp);
2061 } else if (is_lminion(mtmp)) {
2062 /* lawful minions don't get cursed, bad, or rusting objects */
2063 otmp->cursed = FALSE;
2064 if (otmp->spe < 0)
2065 otmp->spe = 0;
2066 otmp->oerodeproof = 1;
2067 otmp->oeroded = otmp->oeroded2 = 0;
2068 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
2069 otmp->spe = (3 + rn2(4));
2072 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
2073 otmp->spe = 0;
2074 otmp->age = 0L;
2075 otmp->lamplit = FALSE;
2076 otmp->blessed = otmp->cursed = FALSE;
2077 } else if (otmp->otyp == BELL_OF_OPENING) {
2078 otmp->blessed = otmp->cursed = FALSE;
2079 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
2080 otmp->blessed = FALSE;
2081 otmp->cursed = TRUE;
2084 /* leaders don't tolerate inferior quality battle gear */
2085 if (is_prince(mtmp->data)) {
2086 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
2087 otmp->spe = 1;
2088 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
2089 otmp->spe = 0;
2092 if (mpickobj(mtmp, otmp)) {
2093 /* otmp was freed via merging with something else */
2094 otmp = (struct obj *) 0;
2097 return otmp;
2101 golemhp(int type)
2103 switch (type) {
2104 case PM_STRAW_GOLEM:
2105 return 20;
2106 case PM_PAPER_GOLEM:
2107 return 20;
2108 case PM_ROPE_GOLEM:
2109 return 30;
2110 case PM_LEATHER_GOLEM:
2111 return 40;
2112 case PM_GOLD_GOLEM:
2113 return 60;
2114 case PM_WOOD_GOLEM:
2115 return 50;
2116 case PM_FLESH_GOLEM:
2117 return 40;
2118 case PM_CLAY_GOLEM:
2119 return 70;
2120 case PM_STONE_GOLEM:
2121 return 100;
2122 case PM_GLASS_GOLEM:
2123 return 80;
2124 case PM_IRON_GOLEM:
2125 return 120;
2126 default:
2127 return 0;
2132 * Alignment vs. yours determines monster's attitude to you.
2133 * (Some "animal" types are co-aligned, but also hungry.)
2135 boolean
2136 peace_minded(struct permonst *ptr)
2138 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
2140 if (always_peaceful(ptr))
2141 return TRUE;
2142 if (always_hostile(ptr))
2143 return FALSE;
2144 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
2145 return TRUE;
2146 if (ptr->msound == MS_NEMESIS)
2147 return FALSE;
2148 if (ptr == &mons[PM_ERINYS])
2149 return !u.ualign.abuse;
2151 if (race_peaceful(ptr))
2152 return TRUE;
2153 if (race_hostile(ptr))
2154 return FALSE;
2156 /* the monster is hostile if its alignment is different from the
2157 * player's */
2158 if (sgn(mal) != sgn(ual))
2159 return FALSE;
2161 /* Negative monster hostile to player with Amulet. */
2162 if (mal < A_NEUTRAL && u.uhave.amulet)
2163 return FALSE;
2165 /* minions are hostile to players that have strayed at all */
2166 if (is_minion(ptr))
2167 return (boolean) (u.ualign.record >= 0);
2169 /* Last case: a chance of a co-aligned monster being
2170 * hostile. This chance is greater if the player has strayed
2171 * (u.ualign.record negative) or the monster is not strongly aligned.
2173 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
2174 : u.ualign.record))
2175 && !!rn2(2 + abs(mal)));
2178 /* Set malign to have the proper effect on player alignment if monster is
2179 * killed. Negative numbers mean it's bad to kill this monster; positive
2180 * numbers mean it's good. Since there are more hostile monsters than
2181 * peaceful monsters, the penalty for killing a peaceful monster should be
2182 * greater than the bonus for killing a hostile monster to maintain balance.
2183 * Rules:
2184 * it's bad to kill peaceful monsters, potentially worse to kill always-
2185 * peaceful monsters;
2186 * it's never bad to kill a hostile monster, although it may not be good.
2188 void
2189 set_malign(struct monst *mtmp)
2191 schar mal = mtmp->data->maligntyp;
2192 boolean coaligned;
2194 if (mtmp->ispriest || mtmp->isminion) {
2195 /* some monsters have individual alignments; check them */
2196 if (mtmp->ispriest && EPRI(mtmp))
2197 mal = EPRI(mtmp)->shralign;
2198 else if (mtmp->isminion && EMIN(mtmp))
2199 mal = EMIN(mtmp)->min_align;
2200 /* unless alignment is none, set mal to -5,0,5 */
2201 /* (see align.h for valid aligntyp values) */
2202 if (mal != A_NONE)
2203 mal *= 5;
2206 coaligned = (sgn(mal) == sgn(u.ualign.type));
2207 if (mtmp->data->msound == MS_LEADER) {
2208 mtmp->malign = -20;
2209 } else if (mal == A_NONE) {
2210 if (mtmp->mpeaceful)
2211 mtmp->malign = 0;
2212 else
2213 mtmp->malign = 20; /* really hostile */
2214 } else if (always_peaceful(mtmp->data)) {
2215 int absmal = abs(mal);
2216 if (mtmp->mpeaceful)
2217 mtmp->malign = -3 * max(5, absmal);
2218 else
2219 mtmp->malign = 3 * max(5, absmal); /* renegade */
2220 } else if (always_hostile(mtmp->data)) {
2221 int absmal = abs(mal);
2222 if (coaligned)
2223 mtmp->malign = 0;
2224 else
2225 mtmp->malign = max(5, absmal);
2226 } else if (coaligned) {
2227 int absmal = abs(mal);
2228 if (mtmp->mpeaceful)
2229 mtmp->malign = -3 * max(3, absmal);
2230 else /* renegade */
2231 mtmp->malign = max(3, absmal);
2232 } else /* not coaligned and therefore hostile */
2233 mtmp->malign = abs(mal);
2236 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
2237 void
2238 newmcorpsenm(struct monst *mtmp)
2240 if (!mtmp->mextra)
2241 mtmp->mextra = newmextra();
2242 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
2245 /* release monster's mcorpsenm field; basically a no-op */
2246 void
2247 freemcorpsenm(struct monst *mtmp)
2249 if (has_mcorpsenm(mtmp))
2250 MCORPSENM(mtmp) = NON_PM;
2253 static const NEARDATA char syms[] = {
2254 MAXOCLASSES, MAXOCLASSES, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
2255 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
2256 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
2257 S_MIMIC_DEF, S_MIMIC_DEF,
2260 void
2261 set_mimic_sym(struct monst *mtmp)
2263 int typ, roomno, rt;
2264 unsigned appear, ap_type;
2265 int s_sym;
2266 struct obj *otmp;
2267 int mx, my;
2269 if (!mtmp || Protection_from_shape_changers)
2270 return;
2271 mx = mtmp->mx;
2272 my = mtmp->my;
2273 typ = levl[mx][my].typ;
2274 /* only valid for INSIDE of room */
2275 roomno = levl[mx][my].roomno - ROOMOFFSET;
2276 if (roomno >= 0)
2277 rt = svr.rooms[roomno].rtype;
2278 #ifdef SPECIALIZATION
2279 else if (IS_ROOM(typ))
2280 rt = OROOM, roomno = 0;
2281 #endif
2282 else
2283 rt = 0; /* roomno < 0 case for GCC_WARN */
2285 if (OBJ_AT(mx, my)) {
2286 ap_type = M_AP_OBJECT;
2287 appear = svl.level.objects[mx][my]->otyp;
2288 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2289 ap_type = M_AP_FURNITURE;
2291 * If there is a wall to the left that connects to this
2292 * location, then the mimic mimics a horizontal closed door.
2293 * This does not allow doors to be in corners of rooms.
2294 * Since rogue has no closed doors, mimic a wall there
2295 * (yes, mimics can end up on this level by various means).
2297 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2298 || levl[mx - 1][my].typ == TLCORNER
2299 || levl[mx - 1][my].typ == TRWALL
2300 || levl[mx - 1][my].typ == BLCORNER
2301 || levl[mx - 1][my].typ == TDWALL
2302 || levl[mx - 1][my].typ == CROSSWALL
2303 || levl[mx - 1][my].typ == TUWALL))
2304 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2305 else
2306 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2307 } else if (svl.level.flags.is_maze_lev
2308 && !(In_mines(&u.uz) && in_town(u.ux, u.uy))
2309 && !In_sokoban(&u.uz) && rn2(2)) {
2310 ap_type = M_AP_OBJECT;
2311 appear = STATUE;
2312 } else if (roomno < 0 && !t_at(mx, my)) {
2313 ap_type = M_AP_OBJECT;
2314 appear = BOULDER;
2315 } else if (rt == ZOO || rt == VAULT) {
2316 ap_type = M_AP_OBJECT;
2317 appear = GOLD_PIECE;
2318 } else if (rt == DELPHI) {
2319 if (rn2(2)) {
2320 ap_type = M_AP_OBJECT;
2321 appear = STATUE;
2322 } else {
2323 ap_type = M_AP_FURNITURE;
2324 appear = S_fountain;
2326 } else if (rt == TEMPLE) {
2327 ap_type = M_AP_FURNITURE;
2328 appear = S_altar;
2331 * We won't bother with beehives, morgues, barracks, throne rooms
2332 * since they shouldn't contain too many mimics anyway...
2335 } else if (rt >= SHOPBASE) {
2336 if (rn2(10) >= depth(&u.uz)) {
2337 s_sym = S_MIMIC_DEF; /* -> STRANGE_OBJECT */
2338 goto assign_sym;
2340 s_sym = get_shop_item(rt - SHOPBASE);
2341 if (s_sym < 0) {
2342 ap_type = M_AP_OBJECT;
2343 appear = -s_sym;
2344 } else if (rt == FODDERSHOP && s_sym > MAXOCLASSES) {
2345 /* health food store usually generates pseudo-class
2346 VEGETARIAN_CLASS which is MAXOCLASSES+1; we don't bother
2347 trying to select among all possible vegetarian food items */
2348 ap_type = M_AP_OBJECT;
2349 appear = rn2(2) ? LUMP_OF_ROYAL_JELLY : SLIME_MOLD;
2350 } else {
2351 if (s_sym == RANDOM_CLASS || s_sym >= MAXOCLASSES)
2352 s_sym = syms[rn2(SIZE(syms) - 2) + 2];
2353 goto assign_sym;
2355 } else {
2356 s_sym = ROLL_FROM(syms);
2357 assign_sym:
2358 if (s_sym == MAXOCLASSES) {
2359 static const int furnsyms[] = {
2360 S_upstair, S_upstair, S_dnstair, S_dnstair,
2361 S_altar, S_grave, S_throne, S_sink
2364 ap_type = M_AP_FURNITURE;
2365 appear = ROLL_FROM(furnsyms);
2366 } else {
2367 ap_type = M_AP_OBJECT;
2368 if (s_sym == S_MIMIC_DEF) {
2369 appear = STRANGE_OBJECT;
2370 } else if (s_sym == COIN_CLASS) {
2371 appear = GOLD_PIECE;
2372 } else {
2373 otmp = mkobj((char) s_sym, FALSE);
2374 appear = otmp->otyp;
2375 /* make sure container contents are free'ed */
2376 obfree(otmp, (struct obj *) 0);
2380 mtmp->m_ap_type = ap_type;
2381 mtmp->mappearance = appear;
2382 /* when appearing as an object based on a monster type, pick a shape */
2383 if (ap_type == M_AP_OBJECT
2384 && (appear == STATUE || appear == FIGURINE
2385 || appear == CORPSE || appear == EGG || appear == TIN)) {
2386 int mndx = rndmonnum(),
2387 nocorpse_ndx = (svm.mvitals[mndx].mvflags & G_NOCORPSE) != 0;
2389 if (appear == CORPSE && nocorpse_ndx)
2390 mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
2391 else if ((appear == EGG && !can_be_hatched(mndx))
2392 || (appear == TIN && nocorpse_ndx))
2393 mndx = NON_PM; /* revert to generic egg or empty tin */
2395 newmcorpsenm(mtmp);
2396 MCORPSENM(mtmp) = mndx;
2397 } else if (ap_type == M_AP_OBJECT && appear == SLIME_MOLD) {
2398 newmcorpsenm(mtmp);
2399 MCORPSENM(mtmp) = svc.context.current_fruit;
2400 /* if no objects of this fruit type have been created yet,
2401 context.current_fruit is available for re-use when the player
2402 assigns a new fruit name; override that--having a mimic as the
2403 current_fruit is equivalent to creating an instance of that
2404 fruit (no-op if a fruit of this type has actually been made) */
2405 flags.made_fruit = TRUE;
2406 } else if (ap_type == M_AP_FURNITURE && appear == S_altar) {
2407 int algn = rn2(3) - 1; /* -1 (A_Cha) or 0 (A_Neu) or +1 (A_Law) */
2409 newmcorpsenm(mtmp);
2410 MCORPSENM(mtmp) = (Inhell && rn2(3)) ? AM_NONE : Align2amask(algn);
2411 } else if (has_mcorpsenm(mtmp)) {
2412 /* don't retain stale value from a previously mimicked shape */
2413 MCORPSENM(mtmp) = NON_PM;
2416 if (does_block(mx, my, &levl[mx][my]))
2417 block_point(mx, my);
2420 /* release monster from bag of tricks; return number of monsters created */
2422 bagotricks(
2423 struct obj *bag,
2424 boolean tipping, /* caller emptying entirely; affects shop handling */
2425 int *seencount) /* secondary output */
2427 int moncount = 0;
2429 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2430 impossible("bad bag o' tricks");
2431 } else if (bag->spe < 1) {
2432 /* if tipping known empty bag, give normal empty container message */
2433 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2434 /* now known to be empty if sufficiently discovered */
2435 if (bag->dknown && objects[bag->otyp].oc_name_known) {
2436 bag->cknown = 1;
2437 update_inventory(); /* for perm_invent */
2439 } else {
2440 struct monst *mtmp;
2441 int creatcnt = 1, seecount = 0;
2443 consume_obj_charge(bag, !tipping);
2445 if (!rn2(23))
2446 creatcnt += rnd(7);
2447 do {
2448 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2449 if (mtmp) {
2450 ++moncount;
2451 if ((canseemon(mtmp) && (M_AP_TYPE(mtmp) == M_AP_NOTHING
2452 || M_AP_TYPE(mtmp) == M_AP_MONSTER))
2453 || sensemon(mtmp))
2454 ++seecount;
2456 } while (--creatcnt > 0);
2457 if (seecount) {
2458 if (seencount)
2459 *seencount += seecount;
2460 if (bag->dknown) {
2461 makeknown(BAG_OF_TRICKS);
2462 update_inventory(); /* for perm_invent */
2464 } else if (!tipping) {
2465 pline1(!moncount ? nothing_happens : nothing_seems_to_happen);
2468 return moncount;
2471 /* create some or all remaining erinyes around the player */
2472 void
2473 summon_furies(int limit) /* number to create, or 0 to create until extinct */
2475 int i = 0;
2476 while (mk_gen_ok(PM_ERINYS, G_GONE, 0U) && (i < limit || !limit)) {
2477 makemon(&mons[PM_ERINYS], u.ux, u.uy, MM_ADJACENTOK | MM_NOWAIT);
2478 i++;
2482 /*makemon.c*/