NetHack->aNetHack
[aNetHack.git] / src / makemon.c
blobc60adfa1f9b8ce99aaf250584019899d6de2208e
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. */
5 #include "hack.h"
7 #include <ctype.h>
9 /* this assumes that a human quest leader or nemesis is an archetype
10 of the corresponding role; that isn't so for some roles (tourist
11 for instance) but is for the priests and monks we use it for... */
12 #define quest_mon_represents_role(mptr, role_pm) \
13 (mptr->mlet == S_HUMAN && Role_if(role_pm) \
14 && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
16 STATIC_DCL boolean FDECL(uncommon, (int));
17 STATIC_DCL int FDECL(align_shift, (struct permonst *));
18 STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
19 STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
20 STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int));
21 STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
22 STATIC_DCL void FDECL(m_initweap, (struct monst *));
23 STATIC_DCL void FDECL(m_initinv, (struct monst *));
24 STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *,
25 unsigned, coord *));
27 extern const int monstr[];
29 #define m_initsgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 3)
30 #define m_initlgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 10)
31 #define toostrong(monindx, lev) (monstr[monindx] > lev)
32 #define tooweak(monindx, lev) (monstr[monindx] < lev)
34 boolean
35 is_home_elemental(ptr)
36 struct permonst *ptr;
38 if (ptr->mlet == S_ELEMENTAL)
39 switch (monsndx(ptr)) {
40 case PM_AIR_ELEMENTAL:
41 return Is_airlevel(&u.uz);
42 case PM_FIRE_ELEMENTAL:
43 return Is_firelevel(&u.uz);
44 case PM_EARTH_ELEMENTAL:
45 return Is_earthlevel(&u.uz);
46 case PM_WATER_ELEMENTAL:
47 return Is_waterlevel(&u.uz);
49 return FALSE;
53 * Return true if the given monster cannot exist on this elemental level.
55 STATIC_OVL boolean
56 wrong_elem_type(ptr)
57 struct permonst *ptr;
59 if (ptr->mlet == S_ELEMENTAL) {
60 return (boolean) !is_home_elemental(ptr);
61 } else if (Is_earthlevel(&u.uz)) {
62 /* no restrictions? */
63 } else if (Is_waterlevel(&u.uz)) {
64 /* just monsters that can swim */
65 if (!is_swimmer(ptr))
66 return TRUE;
67 } else if (Is_firelevel(&u.uz)) {
68 if (!pm_resistance(ptr, MR_FIRE))
69 return TRUE;
70 } else if (Is_airlevel(&u.uz)) {
71 if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
72 && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
73 return TRUE;
75 return FALSE;
78 /* make a group just like mtmp */
79 STATIC_OVL void
80 m_initgrp(mtmp, x, y, n)
81 register struct monst *mtmp;
82 register int x, y, n;
84 coord mm;
85 register int cnt = rnd(n);
86 struct monst *mon;
87 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
88 /* There is an unresolved problem with several people finding that
89 * the game hangs eating CPU; if interrupted and restored, the level
90 * will be filled with monsters. Of those reports giving system type,
91 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
92 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
93 * problem went away for him and another reporter-to-newsgroup
94 * after adding this debugging code. This has almost got to be a
95 * compiler bug, but until somebody tracks it down and gets it fixed,
96 * might as well go with the "but it went away when I tried to find
97 * it" code.
99 int cnttmp, cntdiv;
101 cnttmp = cnt;
102 debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
103 cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
104 #endif
105 /* Tuning: cut down on swarming at low character levels [mrs] */
106 cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
107 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
108 if (cnt != (cnttmp / cntdiv)) {
109 pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
110 (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
112 #endif
113 if (!cnt)
114 cnt++;
115 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
116 if (cnt < 0)
117 cnt = 1;
118 if (cnt > 10)
119 cnt = 10;
120 #endif
122 mm.x = x;
123 mm.y = y;
124 while (cnt--) {
125 if (peace_minded(mtmp->data))
126 continue;
127 /* Don't create groups of peaceful monsters since they'll get
128 * in our way. If the monster has a percentage chance so some
129 * are peaceful and some are not, the result will just be a
130 * smaller group.
132 if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
133 mon = makemon(mtmp->data, mm.x, mm.y, NO_MM_FLAGS);
134 if (mon) {
135 mon->mpeaceful = FALSE;
136 mon->mavenge = 0;
137 set_malign(mon);
138 /* Undo the second peace_minded() check in makemon(); if the
139 * monster turned out to be peaceful the first time we
140 * didn't create it at all; we don't want a second check.
147 STATIC_OVL
148 void
149 m_initthrow(mtmp, otyp, oquan)
150 struct monst *mtmp;
151 int otyp, oquan;
153 register struct obj *otmp;
155 otmp = mksobj(otyp, TRUE, FALSE);
156 otmp->quan = (long) rn1(oquan, 3);
157 otmp->owt = weight(otmp);
158 if (otyp == ORCISH_ARROW)
159 otmp->opoisoned = TRUE;
160 (void) mpickobj(mtmp, otmp);
163 STATIC_OVL void
164 m_initweap(mtmp)
165 register struct monst *mtmp;
167 register struct permonst *ptr = mtmp->data;
168 register int mm = monsndx(ptr);
169 struct obj *otmp;
170 int bias, spe2, w1, w2;
172 if (Is_rogue_level(&u.uz))
173 return;
175 * First a few special cases:
176 * giants get a boulder to throw sometimes
177 * ettins get clubs
178 * kobolds get darts to throw
179 * centaurs get some sort of bow & arrows or bolts
180 * soldiers get all sorts of things
181 * kops get clubs & cream pies.
183 switch (ptr->mlet) {
184 case S_GIANT:
185 if (rn2(2))
186 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
187 break;
188 case S_HUMAN:
189 if (is_mercenary(ptr)) {
190 w1 = w2 = 0;
191 switch (mm) {
192 case PM_WATCHMAN:
193 case PM_SOLDIER:
194 if (!rn2(3)) {
195 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
196 w2 = rn2(2) ? DAGGER : KNIFE;
197 } else
198 w1 = rn2(2) ? SPEAR : SHORT_SWORD;
199 break;
200 case PM_SERGEANT:
201 w1 = rn2(2) ? FLAIL : MACE;
202 break;
203 case PM_LIEUTENANT:
204 w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
205 break;
206 case PM_CAPTAIN:
207 case PM_WATCH_CAPTAIN:
208 w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
209 break;
210 default:
211 if (!rn2(4))
212 w1 = DAGGER;
213 if (!rn2(7))
214 w2 = SPEAR;
215 break;
217 if (w1)
218 (void) mongets(mtmp, w1);
219 if (!w2 && w1 != DAGGER && !rn2(4))
220 w2 = KNIFE;
221 if (w2)
222 (void) mongets(mtmp, w2);
223 } else if (is_elf(ptr)) {
224 if (rn2(2))
225 (void) mongets(mtmp,
226 rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
227 if (rn2(2))
228 (void) mongets(mtmp, ELVEN_LEATHER_HELM);
229 else if (!rn2(4))
230 (void) mongets(mtmp, ELVEN_BOOTS);
231 if (rn2(2))
232 (void) mongets(mtmp, ELVEN_DAGGER);
233 switch (rn2(3)) {
234 case 0:
235 if (!rn2(4))
236 (void) mongets(mtmp, ELVEN_SHIELD);
237 if (rn2(3))
238 (void) mongets(mtmp, ELVEN_SHORT_SWORD);
239 (void) mongets(mtmp, ELVEN_BOW);
240 m_initthrow(mtmp, ELVEN_ARROW, 12);
241 break;
242 case 1:
243 (void) mongets(mtmp, ELVEN_BROADSWORD);
244 if (rn2(2))
245 (void) mongets(mtmp, ELVEN_SHIELD);
246 break;
247 case 2:
248 if (rn2(2)) {
249 (void) mongets(mtmp, ELVEN_SPEAR);
250 (void) mongets(mtmp, ELVEN_SHIELD);
252 break;
254 if (mm == PM_ELVENKING) {
255 if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
256 (void) mongets(mtmp, PICK_AXE);
257 if (!rn2(50))
258 (void) mongets(mtmp, CRYSTAL_BALL);
260 } else if (ptr->msound == MS_PRIEST
261 || quest_mon_represents_role(ptr, PM_PRIEST)) {
262 otmp = mksobj(MACE, FALSE, FALSE);
263 if (otmp) {
264 otmp->spe = rnd(3);
265 if (!rn2(2))
266 curse(otmp);
267 (void) mpickobj(mtmp, otmp);
269 } else if (mm == PM_NINJA) { /* extra quest villains */
270 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
271 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
272 } else if (ptr->msound == MS_GUARDIAN) {
273 /* quest "guardians" */
274 switch (mm) {
275 case PM_STUDENT:
276 case PM_ATTENDANT:
277 case PM_ABBOT:
278 case PM_ACOLYTE:
279 case PM_GUIDE:
280 case PM_APPRENTICE:
281 if (rn2(2))
282 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
283 if (rn2(5))
284 (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET : 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 : LEATHER_ARMOR);
309 (void) mongets(mtmp, BOW);
310 m_initthrow(mtmp, ARROW, 12);
311 break;
312 case PM_THUG:
313 (void) mongets(mtmp, CLUB);
314 (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
315 if (rn2(2))
316 (void) mongets(mtmp, LEATHER_GLOVES);
317 (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
318 break;
319 case PM_NEANDERTHAL:
320 (void) mongets(mtmp, CLUB);
321 (void) mongets(mtmp, LEATHER_ARMOR);
322 break;
325 break;
327 case S_ANGEL:
328 if (humanoid(ptr)) {
329 /* create minion stuff; can't use mongets */
330 otmp = mksobj(LONG_SWORD, FALSE, FALSE);
332 /* maybe make it special */
333 if (!rn2(20) || is_lord(ptr))
334 otmp = oname(otmp,
335 artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
336 bless(otmp);
337 otmp->oerodeproof = TRUE;
338 spe2 = rn2(4);
339 otmp->spe = max(otmp->spe, spe2);
340 (void) mpickobj(mtmp, otmp);
342 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
343 : LARGE_SHIELD,
344 FALSE, FALSE);
345 otmp->cursed = FALSE;
346 otmp->oerodeproof = TRUE;
347 otmp->spe = 0;
348 (void) mpickobj(mtmp, otmp);
350 break;
352 case S_HUMANOID:
353 if (mm == PM_HOBBIT) {
354 switch (rn2(3)) {
355 case 0:
356 (void) mongets(mtmp, DAGGER);
357 break;
358 case 1:
359 (void) mongets(mtmp, ELVEN_DAGGER);
360 break;
361 case 2:
362 (void) mongets(mtmp, SLING);
363 break;
365 if (!rn2(10))
366 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
367 if (!rn2(10))
368 (void) mongets(mtmp, DWARVISH_CLOAK);
369 } else if (is_dwarf(ptr)) {
370 if (rn2(7))
371 (void) mongets(mtmp, DWARVISH_CLOAK);
372 if (rn2(7))
373 (void) mongets(mtmp, IRON_SHOES);
374 if (!rn2(4)) {
375 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
376 /* note: you can't use a mattock with a shield */
377 if (rn2(2))
378 (void) mongets(mtmp, DWARVISH_MATTOCK);
379 else {
380 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
381 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
383 (void) mongets(mtmp, DWARVISH_IRON_HELM);
384 if (!rn2(3))
385 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
386 } else {
387 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
390 break;
391 case S_KOP:
392 /* create Keystone Kops with cream pies to
393 throw. As suggested by KAA. [MRS] */
394 if (!rn2(4))
395 m_initthrow(mtmp, CREAM_PIE, 2);
396 if (!rn2(3))
397 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
398 break;
399 case S_ORC:
400 if (rn2(2))
401 (void) mongets(mtmp, ORCISH_HELM);
402 switch ((mm != PM_ORC_CAPTAIN) ? mm
403 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
404 case PM_MORDOR_ORC:
405 if (!rn2(3))
406 (void) mongets(mtmp, SCIMITAR);
407 if (!rn2(3))
408 (void) mongets(mtmp, ORCISH_SHIELD);
409 if (!rn2(3))
410 (void) mongets(mtmp, KNIFE);
411 if (!rn2(3))
412 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
413 break;
414 case PM_URUK_HAI:
415 if (!rn2(3))
416 (void) mongets(mtmp, ORCISH_CLOAK);
417 if (!rn2(3))
418 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
419 if (!rn2(3))
420 (void) mongets(mtmp, IRON_SHOES);
421 if (!rn2(3)) {
422 (void) mongets(mtmp, ORCISH_BOW);
423 m_initthrow(mtmp, ORCISH_ARROW, 12);
425 if (!rn2(3))
426 (void) mongets(mtmp, URUK_HAI_SHIELD);
427 break;
428 default:
429 if (mm != PM_ORC_SHAMAN && rn2(2))
430 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
431 ? ORCISH_DAGGER
432 : SCIMITAR);
434 break;
435 case S_OGRE:
436 if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
437 (void) mongets(mtmp, BATTLE_AXE);
438 else
439 (void) mongets(mtmp, CLUB);
440 break;
441 case S_TROLL:
442 if (!rn2(2))
443 switch (rn2(4)) {
444 case 0:
445 (void) mongets(mtmp, RANSEUR);
446 break;
447 case 1:
448 (void) mongets(mtmp, PARTISAN);
449 break;
450 case 2:
451 (void) mongets(mtmp, GLAIVE);
452 break;
453 case 3:
454 (void) mongets(mtmp, SPETUM);
455 break;
457 break;
458 case S_KOBOLD:
459 if (!rn2(4))
460 m_initthrow(mtmp, DART, 12);
461 break;
463 case S_CENTAUR:
464 if (rn2(2)) {
465 if (ptr == &mons[PM_FOREST_CENTAUR]) {
466 (void) mongets(mtmp, BOW);
467 m_initthrow(mtmp, ARROW, 12);
468 } else {
469 (void) mongets(mtmp, CROSSBOW);
470 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
473 break;
474 case S_WRAITH:
475 (void) mongets(mtmp, KNIFE);
476 (void) mongets(mtmp, LONG_SWORD);
477 break;
478 case S_ZOMBIE:
479 if (!rn2(4))
480 (void) mongets(mtmp, LEATHER_ARMOR);
481 if (!rn2(4))
482 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
483 break;
484 case S_LIZARD:
485 if (mm == PM_SALAMANDER)
486 (void) mongets(mtmp,
487 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
488 break;
489 case S_DEMON:
490 switch (mm) {
491 case PM_BALROG:
492 (void) mongets(mtmp, BULLWHIP);
493 (void) mongets(mtmp, BROADSWORD);
494 break;
495 case PM_ORCUS:
496 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
497 break;
498 case PM_HORNED_DEVIL:
499 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
500 break;
501 case PM_DISPATER:
502 (void) mongets(mtmp, WAN_STRIKING);
503 break;
504 case PM_YEENOGHU:
505 (void) mongets(mtmp, FLAIL);
506 break;
508 /* prevent djinn and mail daemons from leaving objects when
509 * they vanish
511 if (!is_demon(ptr))
512 break;
513 /*FALLTHRU*/
514 default:
516 * Now the general case, some chance of getting some type
517 * of weapon for "normal" monsters. Certain special types
518 * of monsters will get a bonus chance or different selections.
520 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
521 switch (rnd(14 - (2 * bias))) {
522 case 1:
523 if (strongmonst(ptr))
524 (void) mongets(mtmp, BATTLE_AXE);
525 else
526 m_initthrow(mtmp, DART, 12);
527 break;
528 case 2:
529 if (strongmonst(ptr))
530 (void) mongets(mtmp, TWO_HANDED_SWORD);
531 else {
532 (void) mongets(mtmp, CROSSBOW);
533 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
535 break;
536 case 3:
537 (void) mongets(mtmp, BOW);
538 m_initthrow(mtmp, ARROW, 12);
539 break;
540 case 4:
541 if (strongmonst(ptr))
542 (void) mongets(mtmp, LONG_SWORD);
543 else
544 m_initthrow(mtmp, DAGGER, 3);
545 break;
546 case 5:
547 if (strongmonst(ptr))
548 (void) mongets(mtmp, LUCERN_HAMMER);
549 else
550 (void) mongets(mtmp, AKLYS);
551 break;
552 default:
553 break;
555 break;
558 if ((int) mtmp->m_lev > rn2(75))
559 (void) mongets(mtmp, rnd_offensive_item(mtmp));
563 * Makes up money for monster's inventory.
564 * This will change with silver & copper coins
566 void
567 mkmonmoney(mtmp, amount)
568 struct monst *mtmp;
569 long amount;
571 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
573 gold->quan = amount;
574 add_to_minv(mtmp, gold);
577 STATIC_OVL void
578 m_initinv(mtmp)
579 register struct monst *mtmp;
581 register int cnt;
582 register struct obj *otmp;
583 register struct permonst *ptr = mtmp->data;
585 if (Is_rogue_level(&u.uz))
586 return;
588 * Soldiers get armour & rations - armour approximates their ac.
589 * Nymphs may get mirror or potion of object detection.
591 switch (ptr->mlet) {
592 case S_HUMAN:
593 if (is_mercenary(ptr)) {
594 register int mac;
596 switch (monsndx(ptr)) {
597 case PM_GUARD:
598 mac = -1;
599 break;
600 case PM_SOLDIER:
601 mac = 3;
602 break;
603 case PM_SERGEANT:
604 mac = 0;
605 break;
606 case PM_LIEUTENANT:
607 mac = -2;
608 break;
609 case PM_CAPTAIN:
610 mac = -3;
611 break;
612 case PM_WATCHMAN:
613 mac = 3;
614 break;
615 case PM_WATCH_CAPTAIN:
616 mac = -2;
617 break;
618 default:
619 impossible("odd mercenary %d?", monsndx(ptr));
620 mac = 0;
621 break;
624 if (mac < -1 && rn2(5))
625 mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
626 : CRYSTAL_PLATE_MAIL);
627 else if (mac < 3 && rn2(5))
628 mac +=
629 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
630 else if (rn2(5))
631 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
632 : STUDDED_LEATHER_ARMOR);
633 else
634 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
636 if (mac < 10 && rn2(3))
637 mac += 1 + mongets(mtmp, HELMET);
638 else if (mac < 10 && rn2(2))
639 mac += 1 + mongets(mtmp, DENTED_POT);
640 if (mac < 10 && rn2(3))
641 mac += 1 + mongets(mtmp, SMALL_SHIELD);
642 else if (mac < 10 && rn2(2))
643 mac += 2 + mongets(mtmp, LARGE_SHIELD);
644 if (mac < 10 && rn2(3))
645 mac += 1 + mongets(mtmp, LOW_BOOTS);
646 else if (mac < 10 && rn2(2))
647 mac += 2 + mongets(mtmp, HIGH_BOOTS);
648 if (mac < 10 && rn2(3))
649 mac += 1 + mongets(mtmp, LEATHER_GLOVES);
650 else if (mac < 10 && rn2(2))
651 mac += 1 + mongets(mtmp, LEATHER_CLOAK);
653 nhUse(mac); /* suppress 'dead increment' from static analyzer */
655 if (ptr != &mons[PM_GUARD] && ptr != &mons[PM_WATCHMAN]
656 && ptr != &mons[PM_WATCH_CAPTAIN]) {
657 if (!rn2(3))
658 (void) mongets(mtmp, K_RATION);
659 if (!rn2(2))
660 (void) mongets(mtmp, C_RATION);
661 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
662 (void) mongets(mtmp, BUGLE);
663 } else if (ptr == &mons[PM_WATCHMAN] && rn2(3))
664 (void) mongets(mtmp, TIN_WHISTLE);
665 } else if (ptr == &mons[PM_SHOPKEEPER]) {
666 (void) mongets(mtmp, SKELETON_KEY);
667 switch (rn2(4)) {
668 /* MAJOR fall through ... */
669 case 0:
670 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
671 case 1:
672 (void) mongets(mtmp, POT_EXTRA_HEALING);
673 case 2:
674 (void) mongets(mtmp, POT_HEALING);
675 case 3:
676 (void) mongets(mtmp, WAN_STRIKING);
678 } else if (ptr->msound == MS_PRIEST
679 || quest_mon_represents_role(ptr, PM_PRIEST)) {
680 (void) mongets(mtmp, rn2(7) ? ROBE
681 : rn2(3) ? CLOAK_OF_PROTECTION
682 : CLOAK_OF_MAGIC_RESISTANCE);
683 (void) mongets(mtmp, SMALL_SHIELD);
684 mkmonmoney(mtmp, (long) rn1(10, 20));
685 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
686 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
688 break;
689 case S_NYMPH:
690 if (!rn2(2))
691 (void) mongets(mtmp, MIRROR);
692 if (!rn2(2))
693 (void) mongets(mtmp, POT_OBJECT_DETECTION);
694 break;
695 case S_GIANT:
696 if (ptr == &mons[PM_MINOTAUR]) {
697 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
698 (void) mongets(mtmp, WAN_DIGGING);
699 } else if (is_giant(ptr)) {
700 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
701 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
702 FALSE, FALSE);
703 otmp->quan = (long) rn1(2, 3);
704 otmp->owt = weight(otmp);
705 (void) mpickobj(mtmp, otmp);
708 break;
709 case S_WRAITH:
710 if (ptr == &mons[PM_NAZGUL]) {
711 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
712 curse(otmp);
713 (void) mpickobj(mtmp, otmp);
715 break;
716 case S_LICH:
717 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
718 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
719 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
720 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
721 rn2(13) ? FALSE : TRUE);
722 if (otmp->spe < 2)
723 otmp->spe = rnd(3);
724 if (!rn2(4))
725 otmp->oerodeproof = 1;
726 (void) mpickobj(mtmp, otmp);
728 break;
729 case S_MUMMY:
730 if (rn2(7))
731 (void) mongets(mtmp, MUMMY_WRAPPING);
732 break;
733 case S_QUANTMECH:
734 if (!rn2(20)) {
735 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
736 otmp->spe = 1; /* flag for special box */
737 otmp->owt = weight(otmp);
738 (void) mpickobj(mtmp, otmp);
740 break;
741 case S_LEPRECHAUN:
742 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
743 break;
744 case S_DEMON:
745 /* moved here from m_initweap() because these don't
746 have AT_WEAP so m_initweap() is not called for them */
747 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
748 (void) mongets(mtmp, SPEAR);
749 } else if (ptr == &mons[PM_ASMODEUS]) {
750 (void) mongets(mtmp, WAN_COLD);
751 (void) mongets(mtmp, WAN_FIRE);
753 break;
754 case S_GNOME:
755 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
756 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
757 otmp->quan = 1;
758 otmp->owt = weight(otmp);
759 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
760 begin_burn(otmp, FALSE);
762 break;
763 default:
764 break;
767 /* ordinary soldiers rarely have access to magic (or gold :-) */
768 if (ptr == &mons[PM_SOLDIER] && rn2(13))
769 return;
771 if ((int) mtmp->m_lev > rn2(50))
772 (void) mongets(mtmp, rnd_defensive_item(mtmp));
773 if ((int) mtmp->m_lev > rn2(100))
774 (void) mongets(mtmp, rnd_misc_item(mtmp));
775 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
776 mkmonmoney(mtmp,
777 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
780 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
781 struct monst *
782 clone_mon(mon, x, y)
783 struct monst *mon;
784 xchar x, y; /* clone's preferred location or 0 (near mon) */
786 coord mm;
787 struct monst *m2;
789 /* may be too weak or have been extinguished for population control */
790 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
791 return (struct monst *) 0;
793 if (x == 0) {
794 mm.x = mon->mx;
795 mm.y = mon->my;
796 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
797 return (struct monst *) 0;
798 } else if (!isok(x, y)) {
799 return (struct monst *) 0; /* paranoia */
800 } else {
801 mm.x = x;
802 mm.y = y;
803 if (MON_AT(mm.x, mm.y)) {
804 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
805 return (struct monst *) 0;
808 m2 = newmonst();
809 *m2 = *mon; /* copy condition of old monster */
810 m2->mextra = (struct mextra *) 0;
811 m2->nmon = fmon;
812 fmon = m2;
813 m2->m_id = context.ident++;
814 if (!m2->m_id)
815 m2->m_id = context.ident++; /* ident overflowed */
816 m2->mx = mm.x;
817 m2->my = mm.y;
819 m2->mcloned = 1;
820 m2->minvent = (struct obj *) 0; /* objects don't clone */
821 m2->mleashed = FALSE;
822 /* Max HP the same, but current HP halved for both. The caller
823 * might want to override this by halving the max HP also.
824 * When current HP is odd, the original keeps the extra point.
825 * We know original has more than 1 HP, so both end up with at least 1.
827 m2->mhpmax = mon->mhpmax;
828 m2->mhp = mon->mhp / 2;
829 mon->mhp -= m2->mhp;
831 /* since shopkeepers and guards will only be cloned if they've been
832 * polymorphed away from their original forms, the clone doesn't have
833 * room for the extra information. we also don't want two shopkeepers
834 * around for the same shop.
836 if (mon->isshk)
837 m2->isshk = FALSE;
838 if (mon->isgd)
839 m2->isgd = FALSE;
840 if (mon->ispriest)
841 m2->ispriest = FALSE;
842 place_monster(m2, m2->mx, m2->my);
843 if (emits_light(m2->data))
844 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
845 monst_to_any(m2));
846 if (has_mname(mon)) {
847 m2 = christen_monst(m2, MNAME(mon));
848 } else if (mon->isshk) {
849 m2 = christen_monst(m2, shkname(mon));
852 /* not all clones caused by player are tame or peaceful */
853 if (!context.mon_moving) {
854 if (mon->mtame)
855 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
856 else if (mon->mpeaceful)
857 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
860 newsym(m2->mx, m2->my); /* display the new monster */
861 if (m2->mtame) {
862 if (mon->isminion) {
863 newemin(m2);
864 if (EMIN(mon))
865 *(EMIN(m2)) = *(EMIN(mon));
866 } else {
867 /* because m2 is a copy of mon it is tame but not init'ed.
868 * however, tamedog will not re-tame a tame dog, so m2
869 * must be made non-tame to get initialized properly.
871 m2->mtame = 0;
872 if (tamedog(m2, (struct obj *) 0)) {
873 *(EDOG(m2)) = *(EDOG(mon));
877 set_malign(m2);
879 return m2;
883 * Propagate a species
885 * Once a certain number of monsters are created, don't create any more
886 * at random (i.e. make them extinct). The previous (3.2) behavior was
887 * to do this when a certain number had _died_, which didn't make
888 * much sense.
890 * Returns FALSE propagation unsuccessful
891 * TRUE propagation successful
893 boolean
894 propagate(mndx, tally, ghostly)
895 int mndx;
896 boolean tally;
897 boolean ghostly;
899 boolean result;
900 uchar lim = mbirth_limit(mndx);
901 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
903 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
905 /* if it's unique, don't ever make it again */
906 if (mons[mndx].geno & G_UNIQ)
907 mvitals[mndx].mvflags |= G_EXTINCT;
909 if (mvitals[mndx].born < 255 && tally
910 && (!ghostly || (ghostly && result)))
911 mvitals[mndx].born++;
912 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
913 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
914 if (wizard) {
915 debugpline1("Automatically extinguished %s.",
916 makeplural(mons[mndx].mname));
918 mvitals[mndx].mvflags |= G_EXTINCT;
919 reset_rndmonst(mndx);
921 return result;
924 /* amount of HP to lose from level drain (or gain from Stormbringer) */
926 monhp_per_lvl(mon)
927 struct monst *mon;
929 struct permonst *ptr = mon->data;
930 int hp = rnd(8); /* default is d8 */
932 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
933 if (is_golem(ptr)) {
934 /* draining usually won't be applicable for these critters */
935 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
936 } else if (ptr->mlevel > 49) {
937 /* arbitrary; such monsters won't be involved in draining anyway */
938 hp = 4 + rnd(4); /* 5..8 */
939 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
940 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
942 hp = 4 + rn2(5); /* 4..8 */
943 } else if (!mon->m_lev) {
944 /* level 0 monsters use 1d4 instead of Nd8 */
945 hp = rnd(4);
947 return hp;
950 /* set up a new monster's initial level and hit points;
951 used by newcham() as well as by makemon() */
952 void
953 newmonhp(mon, mndx)
954 struct monst *mon;
955 int mndx;
957 struct permonst *ptr = &mons[mndx];
959 mon->m_lev = adj_lev(ptr);
960 if (is_golem(ptr)) {
961 mon->mhpmax = mon->mhp = golemhp(mndx);
962 } else if (is_rider(ptr)) {
963 /* we want low HP, but a high mlevel so they can attack well */
964 mon->mhpmax = mon->mhp = d(10, 8);
965 } else if (ptr->mlevel > 49) {
966 /* "special" fixed hp monster
967 * the hit points are encoded in the mlevel in a somewhat strange
968 * way to fit in the 50..127 positive range of a signed character
969 * above the 1..49 that indicate "normal" monster levels */
970 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
971 mon->m_lev = mon->mhp / 4; /* approximation */
972 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
973 /* adult dragons */
974 mon->mhpmax = mon->mhp =
975 (int) (In_endgame(&u.uz)
976 ? (8 * mon->m_lev)
977 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
978 } else if (!mon->m_lev) {
979 mon->mhpmax = mon->mhp = rnd(4);
980 } else {
981 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
982 if (is_home_elemental(ptr))
983 mon->mhpmax = (mon->mhp *= 3);
987 struct mextra *
988 newmextra()
990 struct mextra *mextra;
992 mextra = (struct mextra *) alloc(sizeof(struct mextra));
993 mextra->mname = 0;
994 mextra->egd = 0;
995 mextra->epri = 0;
996 mextra->eshk = 0;
997 mextra->emin = 0;
998 mextra->edog = 0;
999 mextra->mcorpsenm = NON_PM;
1000 return mextra;
1003 boolean
1004 makemon_rnd_goodpos(mon, gpflags, cc)
1005 struct monst *mon;
1006 unsigned gpflags;
1007 coord *cc;
1009 int tryct = 0;
1010 int nx,ny;
1011 boolean good;
1013 do {
1014 nx = rn1(COLNO - 3, 2);
1015 ny = rn2(ROWNO);
1016 good = (!in_mklev && cansee(nx,ny)) ? FALSE
1017 : goodpos(nx, ny, mon, gpflags);
1018 } while ((++tryct < 50) && !good);
1020 if (!good) {
1021 /* else go through all map positions, twice, first round
1022 ignoring positions in sight, and pick first good one.
1023 skip first round if we're in special level loader or blind */
1024 int xofs = nx;
1025 int yofs = ny;
1026 int dx,dy;
1027 int bl = (in_mklev || Blind) ? 1 : 0;
1029 for ( ; bl < 2; bl++) {
1030 for (dx = 0; dx < COLNO; dx++)
1031 for (dy = 0; dy < ROWNO; dy++) {
1032 nx = ((dx + xofs) % (COLNO - 1)) + 1;
1033 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
1034 if (bl == 0 && cansee(nx,ny))
1035 continue;
1036 if (goodpos(nx, ny, mon, gpflags))
1037 goto gotgood;
1039 if (bl == 0 && (!mon || mon->data->mmove)) {
1040 /* all map positions are visible (or not good),
1041 try to pick something logical */
1042 if (dnstair.sx && !rn2(2)) {
1043 nx = dnstair.sx;
1044 ny = dnstair.sy;
1045 } else if (upstair.sx && !rn2(2)) {
1046 nx = upstair.sx;
1047 ny = upstair.sy;
1048 } else if (dnladder.sx && !rn2(2)) {
1049 nx = dnladder.sx;
1050 ny = dnladder.sy;
1051 } else if (upladder.sx && !rn2(2)) {
1052 nx = upladder.sx;
1053 ny = upladder.sy;
1055 if (goodpos(nx, ny, mon, gpflags))
1056 goto gotgood;
1059 } else {
1060 gotgood:
1061 cc->x = nx;
1062 cc->y = ny;
1063 return TRUE;
1065 return FALSE;
1069 * called with [x,y] = coordinates;
1070 * [0,0] means anyplace
1071 * [u.ux,u.uy] means: near player (if !in_mklev)
1073 * In case we make a monster group, only return the one at [x,y].
1075 struct monst *
1076 makemon(ptr, x, y, mmflags)
1077 register struct permonst *ptr;
1078 register int x, y;
1079 int mmflags;
1081 register struct monst *mtmp;
1082 int mndx, mcham, ct, mitem;
1083 boolean anymon = (!ptr);
1084 boolean byyou = (x == u.ux && y == u.uy);
1085 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1086 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1087 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1089 /* if caller wants random location, do it here */
1090 if (x == 0 && y == 0) {
1091 coord cc;
1092 struct monst fakemon;
1094 cc.x = cc.y = 0; /* lint suppression */
1095 fakemon.data = ptr; /* set up for goodpos */
1096 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1097 gpflags, &cc))
1098 return (struct monst *) 0;
1099 x = cc.x;
1100 y = cc.y;
1101 } else if (byyou && !in_mklev) {
1102 coord bypos;
1104 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1105 x = bypos.x;
1106 y = bypos.y;
1107 } else
1108 return (struct monst *) 0;
1111 /* Does monster already exist at the position? */
1112 if (MON_AT(x, y)) {
1113 if ((mmflags & MM_ADJACENTOK) != 0) {
1114 coord bypos;
1115 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1116 x = bypos.x;
1117 y = bypos.y;
1118 } else
1119 return (struct monst *) 0;
1120 } else
1121 return (struct monst *) 0;
1124 if (ptr) {
1125 mndx = monsndx(ptr);
1126 /* if you are to make a specific monster and it has
1127 already been genocided, return */
1128 if (mvitals[mndx].mvflags & G_GENOD)
1129 return (struct monst *) 0;
1130 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1131 debugpline1("Explicitly creating extinct monster %s.",
1132 mons[mndx].mname);
1134 } else {
1135 /* make a random (common) monster that can survive here.
1136 * (the special levels ask for random monsters at specific
1137 * positions, causing mass drowning on the medusa level,
1138 * for instance.)
1140 int tryct = 0; /* maybe there are no good choices */
1141 struct monst fakemon;
1143 do {
1144 if (!(ptr = rndmonst())) {
1145 debugpline0("Warning: no monster.");
1146 return (struct monst *) 0; /* no more monsters! */
1148 fakemon.data = ptr; /* set up for goodpos */
1149 } while (++tryct <= 50
1150 /* in Sokoban, don't accept a giant on first try;
1151 after that, boulder carriers are fair game */
1152 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1153 || !goodpos(x, y, &fakemon, gpflags)));
1154 mndx = monsndx(ptr);
1156 (void) propagate(mndx, countbirth, FALSE);
1157 mtmp = newmonst();
1158 *mtmp = zeromonst; /* clear all entries in structure */
1160 if (mmflags & MM_EGD)
1161 newegd(mtmp);
1162 if (mmflags & MM_EPRI)
1163 newepri(mtmp);
1164 if (mmflags & MM_ESHK)
1165 neweshk(mtmp);
1166 if (mmflags & MM_EMIN)
1167 newemin(mtmp);
1168 if (mmflags & MM_EDOG)
1169 newedog(mtmp);
1171 mtmp->nmon = fmon;
1172 fmon = mtmp;
1173 mtmp->m_id = context.ident++;
1174 if (!mtmp->m_id)
1175 mtmp->m_id = context.ident++; /* ident overflowed */
1176 set_mon_data(mtmp, ptr, 0);
1177 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1178 quest_status.leader_m_id = mtmp->m_id;
1179 mtmp->mnum = mndx;
1181 /* set up level and hit points */
1182 newmonhp(mtmp, mndx);
1184 if (is_female(ptr))
1185 mtmp->female = TRUE;
1186 else if (is_male(ptr))
1187 mtmp->female = FALSE;
1188 /* leader and nemesis gender is usually hardcoded in mons[],
1189 but for ones which can be random, it has already been chosen
1190 (in role_init(), for possible use by the quest pager code) */
1191 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1192 mtmp->female = quest_status.ldrgend;
1193 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1194 mtmp->female = quest_status.nemgend;
1195 else
1196 mtmp->female = rn2(2); /* ignored for neuters */
1198 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1199 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1200 /* quest leader and nemesis both know about all trap types */
1201 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1202 mtmp->mtrapseen = ~0;
1204 place_monster(mtmp, x, y);
1205 mtmp->mcansee = mtmp->mcanmove = TRUE;
1206 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1208 switch (ptr->mlet) {
1209 case S_MIMIC:
1210 set_mimic_sym(mtmp);
1211 break;
1212 case S_SPIDER:
1213 case S_SNAKE:
1214 if (in_mklev)
1215 if (x && y)
1216 (void) mkobj_at(0, x, y, TRUE);
1217 (void) hideunder(mtmp);
1218 break;
1219 case S_LIGHT:
1220 case S_ELEMENTAL:
1221 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1222 mtmp->perminvis = TRUE;
1223 mtmp->minvis = TRUE;
1225 break;
1226 case S_EEL:
1227 (void) hideunder(mtmp);
1228 break;
1229 case S_LEPRECHAUN:
1230 mtmp->msleeping = 1;
1231 break;
1232 case S_JABBERWOCK:
1233 case S_NYMPH:
1234 if (rn2(5) && !u.uhave.amulet)
1235 mtmp->msleeping = 1;
1236 break;
1237 case S_ORC:
1238 if (Race_if(PM_ELF))
1239 mtmp->mpeaceful = FALSE;
1240 break;
1241 case S_UNICORN:
1242 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1243 mtmp->mpeaceful = TRUE;
1244 break;
1245 case S_BAT:
1246 if (Inhell && is_bat(ptr))
1247 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1248 break;
1250 if ((ct = emits_light(mtmp->data)) > 0)
1251 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1252 monst_to_any(mtmp));
1253 mitem = 0; /* extra inventory item for this monster */
1255 if (mndx == PM_VLAD_THE_IMPALER)
1256 mitem = CANDELABRUM_OF_INVOCATION;
1257 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1258 if ((mcham = pm_to_cham(mndx)) != NON_PM) {
1259 /* this is a shapechanger after all */
1260 if (Protection_from_shape_changers
1261 || mndx == PM_VLAD_THE_IMPALER) {
1262 ; /* stuck in its natural form (NON_PM) */
1263 } else {
1264 mtmp->cham = mcham;
1265 /* Note: shapechanger's initial form used to be
1266 chosen here with rndmonst(), yielding a monster
1267 which was appropriate to the level's difficulty
1268 but ignored the changer's usual type selection
1269 so would be inappropriate for vampshifters.
1270 Let newcham() pick the shape. */
1271 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1272 allow_minvent = FALSE;
1274 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1275 mtmp->iswiz = TRUE;
1276 context.no_of_wizards++;
1277 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1278 mitem = SPE_DIG;
1279 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1280 mtmp = christen_monst(mtmp, rndghostname());
1281 } else if (mndx == PM_CROESUS) {
1282 mitem = TWO_HANDED_SWORD;
1283 } else if (ptr->msound == MS_NEMESIS) {
1284 mitem = BELL_OF_OPENING;
1285 } else if (mndx == PM_PESTILENCE) {
1286 mitem = POT_SICKNESS;
1288 if (mitem && allow_minvent)
1289 (void) mongets(mtmp, mitem);
1291 if (in_mklev) {
1292 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1293 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1294 && !u.uhave.amulet && rn2(5))
1295 mtmp->msleeping = TRUE;
1296 } else {
1297 if (byyou) {
1298 newsym(mtmp->mx, mtmp->my);
1299 set_apparxy(mtmp);
1302 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1303 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1304 mtmp->mavenge = 0;
1305 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1306 mtmp->mpeaceful = mtmp->mtame = FALSE;
1308 #ifndef DCC30_BUG
1309 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1310 #else
1311 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1312 same expression. */
1313 if (mndx == PM_LONG_WORM
1314 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1315 #endif
1317 /* we can now create worms with tails - 11/91 */
1318 initworm(mtmp, rn2(5));
1319 if (count_wsegs(mtmp))
1320 place_worm_tail_randomly(mtmp, x, y);
1322 /* it's possible to create an ordinary monster of some special
1323 types; make sure their extended data is initialized to
1324 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1325 (when they're specified, caller intends to handle this itself) */
1326 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1327 ? !(mmflags & (MM_EPRI | MM_EMIN))
1328 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1329 struct emin *eminp;
1330 newemin(mtmp);
1331 eminp = EMIN(mtmp);
1333 mtmp->isminion = 1; /* make priest be a roamer */
1334 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1335 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1336 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1337 ? !eminp->renegade
1338 : eminp->renegade;
1340 set_malign(mtmp); /* having finished peaceful changes */
1341 if (anymon) {
1342 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1343 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1344 } else if (ptr->geno & G_LGROUP) {
1345 if (rn2(3))
1346 m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1347 else
1348 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1352 if (allow_minvent) {
1353 if (is_armed(ptr))
1354 m_initweap(mtmp); /* equip with weapons / armor */
1355 m_initinv(mtmp); /* add on a few special items incl. more armor */
1356 m_dowear(mtmp, TRUE);
1358 if (!rn2(100) && is_domestic(ptr)
1359 && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) {
1360 struct obj *otmp = mksobj(SADDLE, TRUE, FALSE);
1361 put_saddle_on_mon(otmp, mtmp);
1364 } else {
1365 /* no initial inventory is allowed */
1366 if (mtmp->minvent)
1367 discard_minvent(mtmp);
1368 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1370 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1371 if (ptr->mflags3 & M3_WAITFORU)
1372 mtmp->mstrategy |= STRAT_WAITFORU;
1373 if (ptr->mflags3 & M3_CLOSE)
1374 mtmp->mstrategy |= STRAT_CLOSE;
1375 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1376 mtmp->mstrategy |= STRAT_APPEARMSG;
1379 if (!in_mklev)
1380 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1382 return mtmp;
1386 mbirth_limit(mndx)
1387 int mndx;
1389 /* There is an implicit limit of 4 for "high priest of <deity>",
1390 * but aligned priests can grow into high priests, thus they aren't
1391 * really limited to 4, so leave the default amount in place for them.
1394 /* assert(MAXMONNO < 255); */
1395 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1398 /* used for wand/scroll/spell of create monster */
1399 /* returns TRUE iff you know monsters have been created */
1400 boolean
1401 create_critters(cnt, mptr, neverask)
1402 int cnt;
1403 struct permonst *mptr; /* usually null; used for confused reading */
1404 boolean neverask;
1406 coord c;
1407 int x, y;
1408 struct monst *mon;
1409 boolean known = FALSE;
1410 boolean ask = (wizard && !neverask);
1412 while (cnt--) {
1413 if (ask) {
1414 if (create_particular()) {
1415 known = TRUE;
1416 continue;
1417 } else
1418 ask = FALSE; /* ESC will shut off prompting */
1420 x = u.ux, y = u.uy;
1421 /* if in water, try to encourage an aquatic monster
1422 by finding and then specifying another wet location */
1423 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1424 x = c.x, y = c.y;
1426 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1427 if (mon && canspotmon(mon))
1428 known = TRUE;
1430 return known;
1433 STATIC_OVL boolean
1434 uncommon(mndx)
1435 int mndx;
1437 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1438 return TRUE;
1439 if (mvitals[mndx].mvflags & G_GONE)
1440 return TRUE;
1441 if (Inhell)
1442 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1443 else
1444 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1448 * shift the probability of a monster's generation by
1449 * comparing the dungeon alignment and monster alignment.
1450 * return an integer in the range of 0-5.
1452 STATIC_OVL int
1453 align_shift(ptr)
1454 register struct permonst *ptr;
1456 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1457 static NEARDATA s_level *lev;
1458 register int alshift;
1460 if (oldmoves != moves) {
1461 lev = Is_special(&u.uz);
1462 oldmoves = moves;
1464 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1465 default: /* just in case */
1466 case AM_NONE:
1467 alshift = 0;
1468 break;
1469 case AM_LAWFUL:
1470 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1471 break;
1472 case AM_NEUTRAL:
1473 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1474 break;
1475 case AM_CHAOTIC:
1476 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1477 break;
1479 return alshift;
1482 static NEARDATA struct {
1483 int choice_count;
1484 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1485 } rndmonst_state = { -1, { 0 } };
1487 /* select a random monster type */
1488 struct permonst *
1489 rndmonst()
1491 register struct permonst *ptr;
1492 register int mndx, ct;
1494 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1495 return ptr;
1497 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1498 int zlevel, minmlev, maxmlev;
1499 boolean elemlevel;
1500 boolean upper;
1502 rndmonst_state.choice_count = 0;
1503 /* look for first common monster */
1504 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1505 if (!uncommon(mndx))
1506 break;
1507 rndmonst_state.mchoices[mndx] = 0;
1509 if (mndx == SPECIAL_PM) {
1510 /* evidently they've all been exterminated */
1511 debugpline0("rndmonst: no common mons!");
1512 return (struct permonst *) 0;
1513 } /* else `mndx' now ready for use below */
1514 zlevel = level_difficulty();
1515 /* determine the level of the weakest monster to make. */
1516 minmlev = zlevel / 6;
1517 /* determine the level of the strongest monster to make. */
1518 maxmlev = (zlevel + u.ulevel) / 2;
1519 upper = Is_rogue_level(&u.uz);
1520 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1523 * Find out how many monsters exist in the range we have selected.
1525 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1526 ptr = &mons[mndx];
1527 rndmonst_state.mchoices[mndx] = 0;
1528 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1529 continue;
1530 if (upper && !isupper((uchar) def_monsyms[(int) ptr->mlet].sym))
1531 continue;
1532 if (elemlevel && wrong_elem_type(ptr))
1533 continue;
1534 if (uncommon(mndx))
1535 continue;
1536 if (Inhell && (ptr->geno & G_NOHELL))
1537 continue;
1538 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1539 if (ct < 0 || ct > 127)
1540 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1541 rndmonst_state.choice_count += ct;
1542 rndmonst_state.mchoices[mndx] = (char) ct;
1545 * Possible modification: if choice_count is "too low",
1546 * expand minmlev..maxmlev range and try again.
1548 } /* choice_count+mchoices[] recalc */
1550 if (rndmonst_state.choice_count <= 0) {
1551 /* maybe no common mons left, or all are too weak or too strong */
1552 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1553 return (struct permonst *) 0;
1557 * Now, select a monster at random.
1559 ct = rnd(rndmonst_state.choice_count);
1560 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1561 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1562 break;
1564 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1565 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1566 return (struct permonst *) 0;
1568 return &mons[mndx];
1571 /* called when you change level (experience or dungeon depth) or when
1572 monster species can no longer be created (genocide or extinction) */
1573 void
1574 reset_rndmonst(mndx)
1575 int mndx; /* particular species that can no longer be created */
1577 /* cached selection info is out of date */
1578 if (mndx == NON_PM) {
1579 rndmonst_state.choice_count = -1; /* full recalc needed */
1580 } else if (mndx < SPECIAL_PM) {
1581 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1582 rndmonst_state.mchoices[mndx] = 0;
1583 } /* note: safe to ignore extinction of unique monsters */
1586 /* decide whether it's ok to generate a candidate monster by mkclass() */
1587 STATIC_OVL boolean
1588 mk_gen_ok(mndx, mvflagsmask, genomask)
1589 int mndx, mvflagsmask, genomask;
1591 struct permonst *ptr = &mons[mndx];
1593 if (mvitals[mndx].mvflags & mvflagsmask)
1594 return FALSE;
1595 if (ptr->geno & genomask)
1596 return FALSE;
1597 if (is_placeholder(ptr))
1598 return FALSE;
1599 #ifdef MAIL
1600 /* special levels might ask for random demon type; reject this one */
1601 if (ptr == &mons[PM_MAIL_DAEMON])
1602 return FALSE;
1603 #endif
1604 return TRUE;
1607 /* Make one of the multiple types of a given monster class.
1608 * The second parameter specifies a special casing bit mask
1609 * to allow the normal genesis masks to be deactivated.
1610 * Returns Null if no monsters in that class can be made.
1612 struct permonst *
1613 mkclass(class, spc)
1614 char class;
1615 int spc;
1617 register int first, last, num = 0;
1618 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1620 maxmlev = level_difficulty() >> 1;
1621 if (class < 1 || class >= MAXMCLASSES) {
1622 impossible("mkclass called with bad class!");
1623 return (struct permonst *) 0;
1625 /* Assumption #1: monsters of a given class are contiguous in the
1626 * mons[] array.
1628 for (first = LOW_PM; first < SPECIAL_PM; first++)
1629 if (mons[first].mlet == class)
1630 break;
1631 if (first == SPECIAL_PM)
1632 return (struct permonst *) 0;
1634 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1635 if (mk_gen_ok(last, G_GONE, mask)) {
1636 /* consider it */
1637 if (num && toostrong(last, maxmlev)
1638 && monstr[last] != monstr[last - 1] && rn2(2))
1639 break;
1640 num += mons[last].geno & G_FREQ;
1642 if (!num)
1643 return (struct permonst *) 0;
1645 /* Assumption #2: monsters of a given class are presented in ascending
1646 * order of strength.
1648 for (num = rnd(num); num > 0; first++)
1649 if (mk_gen_ok(first, G_GONE, mask)) {
1650 /* skew towards lower value monsters at lower exp. levels */
1651 num -= mons[first].geno & G_FREQ;
1652 if (num && adj_lev(&mons[first]) > (u.ulevel * 2)) {
1653 /* but not when multiple monsters are same level */
1654 if (mons[first].mlevel != mons[first + 1].mlevel)
1655 num--;
1658 first--; /* correct an off-by-one error */
1660 return &mons[first];
1663 /* like mkclass(), but excludes difficulty considerations; used when
1664 player with polycontrol picks a class instead of a specific type;
1665 genocided types are avoided but extinct ones are acceptable; we don't
1666 check polyok() here--caller accepts some choices !polyok() would reject */
1668 mkclass_poly(class)
1669 int class;
1671 register int first, last, num = 0;
1673 for (first = LOW_PM; first < SPECIAL_PM; first++)
1674 if (mons[first].mlet == class)
1675 break;
1676 if (first == SPECIAL_PM)
1677 return NON_PM;
1679 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1680 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1681 num += mons[last].geno & G_FREQ;
1682 if (!num)
1683 return NON_PM;
1685 for (num = rnd(num); num > 0; first++)
1686 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1687 num -= mons[first].geno & G_FREQ;
1688 first--; /* correct an off-by-one error */
1690 return first;
1693 /* adjust strength of monsters based on u.uz and u.ulevel */
1695 adj_lev(ptr)
1696 register struct permonst *ptr;
1698 int tmp, tmp2;
1700 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1701 /* does not depend on other strengths, but does get stronger
1702 * every time he is killed
1704 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1705 if (tmp > 49)
1706 tmp = 49;
1707 return tmp;
1710 if ((tmp = ptr->mlevel) > 49)
1711 return 50; /* "special" demons/devils */
1712 tmp2 = (level_difficulty() - tmp);
1713 if (tmp2 < 0)
1714 tmp--; /* if mlevel > u.uz decrement tmp */
1715 else
1716 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1718 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1719 if (tmp2 > 0)
1720 tmp += (tmp2 / 4); /* level as well */
1722 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1723 if (tmp2 > 49)
1724 tmp2 = 49; /* hard upper limit */
1725 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1728 /* monster earned experience and will gain some hit points; it might also
1729 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1730 struct permonst *
1731 grow_up(mtmp, victim)
1732 struct monst *mtmp, *victim;
1734 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1735 unsigned fem;
1736 struct permonst *ptr = mtmp->data;
1738 /* monster died after killing enemy but before calling this function */
1739 /* currently possible if killing a gas spore */
1740 if (mtmp->mhp <= 0)
1741 return (struct permonst *) 0;
1743 /* note: none of the monsters with special hit point calculations
1744 have both little and big forms */
1745 oldtype = monsndx(ptr);
1746 newtype = little_to_big(oldtype);
1747 if (newtype == PM_PRIEST && mtmp->female)
1748 newtype = PM_PRIESTESS;
1750 /* growth limits differ depending on method of advancement */
1751 if (victim) { /* killed a monster */
1753 * The HP threshold is the maximum number of hit points for the
1754 * current level; once exceeded, a level will be gained.
1755 * Possible bug: if somehow the hit points are already higher
1756 * than that, monster will gain a level without any increase in HP.
1758 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1759 if (!mtmp->m_lev)
1760 hp_threshold = 4;
1761 else if (is_golem(ptr)) /* strange creatures */
1762 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1763 else if (is_home_elemental(ptr))
1764 hp_threshold *= 3;
1765 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1766 /* If they can grow up, be sure the level is high enough for that */
1767 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1768 lev_limit = (int) mons[newtype].mlevel;
1769 /* number of hit points to gain; unlike for the player, we put
1770 the limit at the bottom of the next level rather than the top */
1771 max_increase = rnd((int) victim->m_lev + 1);
1772 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1773 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1774 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1775 } else {
1776 /* a gain level potion or wraith corpse; always go up a level
1777 unless already at maximum (49 is hard upper limit except
1778 for demon lords, who start at 50 and can't go any higher) */
1779 max_increase = cur_increase = rnd(8);
1780 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1781 lev_limit = 50; /* recalc below */
1784 mtmp->mhpmax += max_increase;
1785 mtmp->mhp += cur_increase;
1786 if (mtmp->mhpmax <= hp_threshold)
1787 return ptr; /* doesn't gain a level */
1789 if (is_mplayer(ptr))
1790 lev_limit = 30; /* same as player */
1791 else if (lev_limit < 5)
1792 lev_limit = 5; /* arbitrary */
1793 else if (lev_limit > 49)
1794 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1796 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1797 ptr = &mons[newtype];
1798 /* new form might force gender change */
1799 fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female;
1801 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1802 if (canspotmon(mtmp))
1803 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1804 an(ptr->mname), mhe(mtmp),
1805 nonliving(ptr) ? "expires" : "dies");
1806 set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
1807 mondied(mtmp);
1808 return (struct permonst *) 0;
1809 } else if (canspotmon(mtmp)) {
1810 char buf[BUFSZ];
1812 /* 3.6.1:
1813 * Temporary (?) hack to fix growing into opposite gender.
1815 Sprintf(buf, "%s%s",
1816 /* deal with female gnome becoming a gnome lord */
1817 (mtmp->female && !fem) ? "male "
1818 /* or a male gnome becoming a gnome lady
1819 (can't happen with 3.6.0 mons[], but perhaps
1820 slightly less sexist if prepared for it...) */
1821 : (fem && !mtmp->female) ? "female " : "",
1822 ptr->mname);
1823 pline("%s %s %s.", Monnam(mtmp),
1824 (fem != mtmp->female) ? "changes into"
1825 : humanoid(ptr) ? "becomes"
1826 : "grows up into",
1827 an(buf));
1829 set_mon_data(mtmp, ptr, 1); /* preserve intrinsics */
1830 newsym(mtmp->mx, mtmp->my); /* color may change */
1831 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1833 mtmp->female = fem; /* gender might be changing */
1836 /* sanity checks */
1837 if ((int) mtmp->m_lev > lev_limit) {
1838 mtmp->m_lev--; /* undo increment */
1839 /* HP might have been allowed to grow when it shouldn't */
1840 if (mtmp->mhpmax == hp_threshold + 1)
1841 mtmp->mhpmax--;
1843 if (mtmp->mhpmax > 50 * 8)
1844 mtmp->mhpmax = 50 * 8; /* absolute limit */
1845 if (mtmp->mhp > mtmp->mhpmax)
1846 mtmp->mhp = mtmp->mhpmax;
1848 return ptr;
1852 mongets(mtmp, otyp)
1853 register struct monst *mtmp;
1854 int otyp;
1856 register struct obj *otmp;
1857 int spe;
1859 if (!otyp)
1860 return 0;
1861 otmp = mksobj(otyp, TRUE, FALSE);
1862 if (otmp) {
1863 if (mtmp->data->mlet == S_DEMON) {
1864 /* demons never get blessed objects */
1865 if (otmp->blessed)
1866 curse(otmp);
1867 } else if (is_lminion(mtmp)) {
1868 /* lawful minions don't get cursed, bad, or rusting objects */
1869 otmp->cursed = FALSE;
1870 if (otmp->spe < 0)
1871 otmp->spe = 0;
1872 otmp->oerodeproof = TRUE;
1873 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1874 otmp->spe = (3 + rn2(4));
1877 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1878 otmp->spe = 0;
1879 otmp->age = 0L;
1880 otmp->lamplit = FALSE;
1881 otmp->blessed = otmp->cursed = FALSE;
1882 } else if (otmp->otyp == BELL_OF_OPENING) {
1883 otmp->blessed = otmp->cursed = FALSE;
1884 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1885 otmp->blessed = FALSE;
1886 otmp->cursed = TRUE;
1889 /* leaders don't tolerate inferior quality battle gear */
1890 if (is_prince(mtmp->data)) {
1891 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1892 otmp->spe = 1;
1893 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1894 otmp->spe = 0;
1897 spe = otmp->spe;
1898 (void) mpickobj(mtmp, otmp); /* might free otmp */
1899 return spe;
1901 return 0;
1905 golemhp(type)
1906 int type;
1908 switch (type) {
1909 case PM_STRAW_GOLEM:
1910 return 20;
1911 case PM_PAPER_GOLEM:
1912 return 20;
1913 case PM_ROPE_GOLEM:
1914 return 30;
1915 case PM_LEATHER_GOLEM:
1916 return 40;
1917 case PM_GOLD_GOLEM:
1918 return 40;
1919 case PM_WOOD_GOLEM:
1920 return 50;
1921 case PM_FLESH_GOLEM:
1922 return 40;
1923 case PM_CLAY_GOLEM:
1924 return 50;
1925 case PM_STONE_GOLEM:
1926 return 60;
1927 case PM_GLASS_GOLEM:
1928 return 60;
1929 case PM_IRON_GOLEM:
1930 return 80;
1931 default:
1932 return 0;
1937 * Alignment vs. yours determines monster's attitude to you.
1938 * (Some "animal" types are co-aligned, but also hungry.)
1940 boolean
1941 peace_minded(ptr)
1942 register struct permonst *ptr;
1944 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1946 if (always_peaceful(ptr))
1947 return TRUE;
1948 if (always_hostile(ptr))
1949 return FALSE;
1950 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1951 return TRUE;
1952 if (ptr->msound == MS_NEMESIS)
1953 return FALSE;
1955 if (race_peaceful(ptr))
1956 return TRUE;
1957 if (race_hostile(ptr))
1958 return FALSE;
1960 /* the monster is hostile if its alignment is different from the
1961 * player's */
1962 if (sgn(mal) != sgn(ual))
1963 return FALSE;
1965 /* Negative monster hostile to player with Amulet. */
1966 if (mal < A_NEUTRAL && u.uhave.amulet)
1967 return FALSE;
1969 /* minions are hostile to players that have strayed at all */
1970 if (is_minion(ptr))
1971 return (boolean) (u.ualign.record >= 0);
1973 /* Last case: a chance of a co-aligned monster being
1974 * hostile. This chance is greater if the player has strayed
1975 * (u.ualign.record negative) or the monster is not strongly aligned.
1977 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
1978 : u.ualign.record))
1979 && !!rn2(2 + abs(mal)));
1982 /* Set malign to have the proper effect on player alignment if monster is
1983 * killed. Negative numbers mean it's bad to kill this monster; positive
1984 * numbers mean it's good. Since there are more hostile monsters than
1985 * peaceful monsters, the penalty for killing a peaceful monster should be
1986 * greater than the bonus for killing a hostile monster to maintain balance.
1987 * Rules:
1988 * it's bad to kill peaceful monsters, potentially worse to kill always-
1989 * peaceful monsters;
1990 * it's never bad to kill a hostile monster, although it may not be good.
1992 void
1993 set_malign(mtmp)
1994 struct monst *mtmp;
1996 schar mal = mtmp->data->maligntyp;
1997 boolean coaligned;
1999 if (mtmp->ispriest || mtmp->isminion) {
2000 /* some monsters have individual alignments; check them */
2001 if (mtmp->ispriest && EPRI(mtmp))
2002 mal = EPRI(mtmp)->shralign;
2003 else if (mtmp->isminion && EMIN(mtmp))
2004 mal = EMIN(mtmp)->min_align;
2005 /* unless alignment is none, set mal to -5,0,5 */
2006 /* (see align.h for valid aligntyp values) */
2007 if (mal != A_NONE)
2008 mal *= 5;
2011 coaligned = (sgn(mal) == sgn(u.ualign.type));
2012 if (mtmp->data->msound == MS_LEADER) {
2013 mtmp->malign = -20;
2014 } else if (mal == A_NONE) {
2015 if (mtmp->mpeaceful)
2016 mtmp->malign = 0;
2017 else
2018 mtmp->malign = 20; /* really hostile */
2019 } else if (always_peaceful(mtmp->data)) {
2020 int absmal = abs(mal);
2021 if (mtmp->mpeaceful)
2022 mtmp->malign = -3 * max(5, absmal);
2023 else
2024 mtmp->malign = 3 * max(5, absmal); /* renegade */
2025 } else if (always_hostile(mtmp->data)) {
2026 int absmal = abs(mal);
2027 if (coaligned)
2028 mtmp->malign = 0;
2029 else
2030 mtmp->malign = max(5, absmal);
2031 } else if (coaligned) {
2032 int absmal = abs(mal);
2033 if (mtmp->mpeaceful)
2034 mtmp->malign = -3 * max(3, absmal);
2035 else /* renegade */
2036 mtmp->malign = max(3, absmal);
2037 } else /* not coaligned and therefore hostile */
2038 mtmp->malign = abs(mal);
2041 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
2042 void
2043 newmcorpsenm(mtmp)
2044 struct monst *mtmp;
2046 if (!mtmp->mextra)
2047 mtmp->mextra = newmextra();
2048 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
2051 /* release monster's mcorpsenm field; basically a no-op */
2052 void
2053 freemcorpsenm(mtmp)
2054 struct monst *mtmp;
2056 if (has_mcorpsenm(mtmp))
2057 MCORPSENM(mtmp) = NON_PM;
2060 static NEARDATA char syms[] = {
2061 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
2062 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
2063 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
2064 S_MIMIC_DEF, S_MIMIC_DEF,
2067 void
2068 set_mimic_sym(mtmp)
2069 register struct monst *mtmp;
2071 int typ, roomno, rt;
2072 unsigned appear, ap_type;
2073 int s_sym;
2074 struct obj *otmp;
2075 int mx, my;
2077 if (!mtmp || Protection_from_shape_changers)
2078 return;
2079 mx = mtmp->mx;
2080 my = mtmp->my;
2081 typ = levl[mx][my].typ;
2082 /* only valid for INSIDE of room */
2083 roomno = levl[mx][my].roomno - ROOMOFFSET;
2084 if (roomno >= 0)
2085 rt = rooms[roomno].rtype;
2086 #ifdef SPECIALIZATION
2087 else if (IS_ROOM(typ))
2088 rt = OROOM, roomno = 0;
2089 #endif
2090 else
2091 rt = 0; /* roomno < 0 case for GCC_WARN */
2093 if (OBJ_AT(mx, my)) {
2094 ap_type = M_AP_OBJECT;
2095 appear = level.objects[mx][my]->otyp;
2096 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2097 ap_type = M_AP_FURNITURE;
2099 * If there is a wall to the left that connects to this
2100 * location, then the mimic mimics a horizontal closed door.
2101 * This does not allow doors to be in corners of rooms.
2102 * Since rogue has no closed doors, mimic a wall there
2103 * (yes, mimics can end up on this level by various means).
2105 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2106 || levl[mx - 1][my].typ == TLCORNER
2107 || levl[mx - 1][my].typ == TRWALL
2108 || levl[mx - 1][my].typ == BLCORNER
2109 || levl[mx - 1][my].typ == TDWALL
2110 || levl[mx - 1][my].typ == CROSSWALL
2111 || levl[mx - 1][my].typ == TUWALL))
2112 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2113 else
2114 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2115 if (!mtmp->minvis || See_invisible)
2116 block_point(mx, my); /* vision */
2117 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2118 ap_type = M_AP_OBJECT;
2119 appear = STATUE;
2120 } else if (roomno < 0 && !t_at(mx, my)) {
2121 ap_type = M_AP_OBJECT;
2122 appear = BOULDER;
2123 if (!mtmp->minvis || See_invisible)
2124 block_point(mx, my); /* vision */
2125 } else if (rt == ZOO || rt == VAULT) {
2126 ap_type = M_AP_OBJECT;
2127 appear = GOLD_PIECE;
2128 } else if (rt == DELPHI) {
2129 if (rn2(2)) {
2130 ap_type = M_AP_OBJECT;
2131 appear = STATUE;
2132 } else {
2133 ap_type = M_AP_FURNITURE;
2134 appear = S_fountain;
2136 } else if (rt == TEMPLE) {
2137 ap_type = M_AP_FURNITURE;
2138 appear = S_altar;
2140 * We won't bother with beehives, morgues, barracks, throne rooms
2141 * since they shouldn't contain too many mimics anyway...
2143 } else if (rt >= SHOPBASE) {
2144 s_sym = get_shop_item(rt - SHOPBASE);
2145 if (s_sym < 0) {
2146 ap_type = M_AP_OBJECT;
2147 appear = -s_sym;
2148 } else {
2149 if (s_sym == RANDOM_CLASS)
2150 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2151 goto assign_sym;
2153 } else {
2154 s_sym = syms[rn2((int) sizeof(syms))];
2155 assign_sym:
2156 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2157 ap_type = M_AP_FURNITURE;
2158 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2159 } else {
2160 ap_type = M_AP_OBJECT;
2161 if (s_sym == S_MIMIC_DEF) {
2162 appear = STRANGE_OBJECT;
2163 } else if (s_sym == COIN_CLASS) {
2164 appear = GOLD_PIECE;
2165 } else {
2166 otmp = mkobj((char) s_sym, FALSE);
2167 appear = otmp->otyp;
2168 /* make sure container contents are free'ed */
2169 obfree(otmp, (struct obj *) 0);
2173 mtmp->m_ap_type = ap_type;
2174 mtmp->mappearance = appear;
2175 if (ap_type == M_AP_OBJECT && (appear == STATUE || appear == CORPSE
2176 || appear == FIGURINE || appear == EGG)) {
2177 newmcorpsenm(mtmp);
2178 MCORPSENM(mtmp) = rndmonnum();
2179 if (appear == EGG && !can_be_hatched(MCORPSENM(mtmp)))
2180 MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
2184 /* release monster from bag of tricks; return number of monsters created */
2186 bagotricks(bag, tipping, seencount)
2187 struct obj *bag;
2188 boolean tipping; /* caller emptying entire contents; affects shop handling */
2189 int *seencount; /* secondary output */
2191 int moncount = 0;
2193 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2194 impossible("bad bag o' tricks");
2195 } else if (bag->spe < 1) {
2196 /* if tipping known empty bag, give normal empty container message */
2197 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2198 /* now known to be empty if sufficiently discovered */
2199 if (bag->dknown && objects[bag->otyp].oc_name_known)
2200 bag->cknown = 1;
2201 } else {
2202 struct monst *mtmp;
2203 int creatcnt = 1, seecount = 0;
2205 consume_obj_charge(bag, !tipping);
2207 if (!rn2(23))
2208 creatcnt += rnd(7);
2209 do {
2210 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2211 if (mtmp) {
2212 ++moncount;
2213 if (canspotmon(mtmp))
2214 ++seecount;
2216 } while (--creatcnt > 0);
2217 if (seecount) {
2218 if (seencount)
2219 *seencount += seecount;
2220 if (bag->dknown)
2221 makeknown(BAG_OF_TRICKS);
2222 } else if (!tipping) {
2223 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2226 return moncount;
2229 /*makemon.c*/