Use define for iron ball weight increment
[aNetHack.git] / src / makemon.c
blob422a446c2c51028aae1ef5877bcf68ed3fb56d35
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 STATIC_VAR NEARDATA struct monst zeromonst;
11 /* this assumes that a human quest leader or nemesis is an archetype
12 of the corresponding role; that isn't so for some roles (tourist
13 for instance) but is for the priests and monks we use it for... */
14 #define quest_mon_represents_role(mptr, role_pm) \
15 (mptr->mlet == S_HUMAN && Role_if(role_pm) \
16 && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS))
18 STATIC_DCL boolean FDECL(uncommon, (int));
19 STATIC_DCL int FDECL(align_shift, (struct permonst *));
20 STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int));
21 STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
22 STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int));
23 STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int));
24 STATIC_DCL void FDECL(m_initweap, (struct monst *));
25 STATIC_DCL void FDECL(m_initinv, (struct monst *));
26 STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *, unsigned, coord *));
28 extern const int monstr[];
30 #define m_initsgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 3)
31 #define m_initlgrp(mtmp, x, y) m_initgrp(mtmp, x, y, 10)
32 #define toostrong(monindx, lev) (monstr[monindx] > lev)
33 #define tooweak(monindx, lev) (monstr[monindx] < lev)
35 boolean
36 is_home_elemental(ptr)
37 struct permonst *ptr;
39 if (ptr->mlet == S_ELEMENTAL)
40 switch (monsndx(ptr)) {
41 case PM_AIR_ELEMENTAL:
42 return Is_airlevel(&u.uz);
43 case PM_FIRE_ELEMENTAL:
44 return Is_firelevel(&u.uz);
45 case PM_EARTH_ELEMENTAL:
46 return Is_earthlevel(&u.uz);
47 case PM_WATER_ELEMENTAL:
48 return Is_waterlevel(&u.uz);
50 return FALSE;
54 * Return true if the given monster cannot exist on this elemental level.
56 STATIC_OVL boolean
57 wrong_elem_type(ptr)
58 struct permonst *ptr;
60 if (ptr->mlet == S_ELEMENTAL) {
61 return (boolean) !is_home_elemental(ptr);
62 } else if (Is_earthlevel(&u.uz)) {
63 /* no restrictions? */
64 } else if (Is_waterlevel(&u.uz)) {
65 /* just monsters that can swim */
66 if (!is_swimmer(ptr))
67 return TRUE;
68 } else if (Is_firelevel(&u.uz)) {
69 if (!pm_resistance(ptr, MR_FIRE))
70 return TRUE;
71 } else if (Is_airlevel(&u.uz)) {
72 if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
73 && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
74 return TRUE;
76 return FALSE;
79 /* make a group just like mtmp */
80 STATIC_OVL void
81 m_initgrp(mtmp, x, y, n)
82 register struct monst *mtmp;
83 register int x, y, n;
85 coord mm;
86 register int cnt = rnd(n);
87 struct monst *mon;
88 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
89 /* There is an unresolved problem with several people finding that
90 * the game hangs eating CPU; if interrupted and restored, the level
91 * will be filled with monsters. Of those reports giving system type,
92 * there were two DG/UX and two HP-UX, all using gcc as the compiler.
93 * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the
94 * problem went away for him and another reporter-to-newsgroup
95 * after adding this debugging code. This has almost got to be a
96 * compiler bug, but until somebody tracks it down and gets it fixed,
97 * might as well go with the "but it went away when I tried to find
98 * it" code.
100 int cnttmp, cntdiv;
102 cnttmp = cnt;
103 debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt);
104 cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1);
105 #endif
106 /* Tuning: cut down on swarming at low character levels [mrs] */
107 cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
108 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
109 if (cnt != (cnttmp / cntdiv)) {
110 pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt,
111 (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv);
113 #endif
114 if (!cnt)
115 cnt++;
116 #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX))
117 if (cnt < 0)
118 cnt = 1;
119 if (cnt > 10)
120 cnt = 10;
121 #endif
123 mm.x = x;
124 mm.y = y;
125 while (cnt--) {
126 if (peace_minded(mtmp->data))
127 continue;
128 /* Don't create groups of peaceful monsters since they'll get
129 * in our way. If the monster has a percentage chance so some
130 * are peaceful and some are not, the result will just be a
131 * smaller group.
133 if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
134 mon = makemon(mtmp->data, mm.x, mm.y, NO_MM_FLAGS);
135 if (mon) {
136 mon->mpeaceful = FALSE;
137 mon->mavenge = 0;
138 set_malign(mon);
139 /* Undo the second peace_minded() check in makemon(); if the
140 * monster turned out to be peaceful the first time we
141 * didn't create it at all; we don't want a second check.
148 STATIC_OVL
149 void
150 m_initthrow(mtmp, otyp, oquan)
151 struct monst *mtmp;
152 int otyp, oquan;
154 register struct obj *otmp;
156 otmp = mksobj(otyp, TRUE, FALSE);
157 otmp->quan = (long) rn1(oquan, 3);
158 otmp->owt = weight(otmp);
159 if (otyp == ORCISH_ARROW)
160 otmp->opoisoned = TRUE;
161 (void) mpickobj(mtmp, otmp);
164 STATIC_OVL void
165 m_initweap(mtmp)
166 register struct monst *mtmp;
168 register struct permonst *ptr = mtmp->data;
169 register int mm = monsndx(ptr);
170 struct obj *otmp;
171 int bias, spe2, w1, w2;
173 if (Is_rogue_level(&u.uz))
174 return;
176 * First a few special cases:
177 * giants get a boulder to throw sometimes
178 * ettins get clubs
179 * kobolds get darts to throw
180 * centaurs get some sort of bow & arrows or bolts
181 * soldiers get all sorts of things
182 * kops get clubs & cream pies.
184 switch (ptr->mlet) {
185 case S_GIANT:
186 if (rn2(2))
187 (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB);
188 break;
189 case S_HUMAN:
190 if (is_mercenary(ptr)) {
191 w1 = w2 = 0;
192 switch (mm) {
193 case PM_WATCHMAN:
194 case PM_SOLDIER:
195 if (!rn2(3)) {
196 w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
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_ELVENKING) {
256 if (rn2(3) || (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_PRIEST)) {
263 otmp = mksobj(MACE, FALSE, FALSE);
264 if (otmp) {
265 otmp->spe = rnd(3);
266 if (!rn2(2))
267 curse(otmp);
268 (void) mpickobj(mtmp, otmp);
270 } else if (mm == PM_NINJA) { /* extra quest villains */
271 (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART);
272 (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
274 break;
276 case S_ANGEL:
277 if (humanoid(ptr)) {
278 /* create minion stuff; can't use mongets */
279 otmp = mksobj(LONG_SWORD, FALSE, FALSE);
281 /* maybe make it special */
282 if (!rn2(20) || is_lord(ptr))
283 otmp = oname(otmp,
284 artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD));
285 bless(otmp);
286 otmp->oerodeproof = TRUE;
287 spe2 = rn2(4);
288 otmp->spe = max(otmp->spe, spe2);
289 (void) mpickobj(mtmp, otmp);
291 otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION
292 : LARGE_SHIELD,
293 FALSE, FALSE);
294 otmp->cursed = FALSE;
295 otmp->oerodeproof = TRUE;
296 otmp->spe = 0;
297 (void) mpickobj(mtmp, otmp);
299 break;
301 case S_HUMANOID:
302 if (mm == PM_HOBBIT) {
303 switch (rn2(3)) {
304 case 0:
305 (void) mongets(mtmp, DAGGER);
306 break;
307 case 1:
308 (void) mongets(mtmp, ELVEN_DAGGER);
309 break;
310 case 2:
311 (void) mongets(mtmp, SLING);
312 break;
314 if (!rn2(10))
315 (void) mongets(mtmp, ELVEN_MITHRIL_COAT);
316 if (!rn2(10))
317 (void) mongets(mtmp, DWARVISH_CLOAK);
318 } else if (is_dwarf(ptr)) {
319 if (rn2(7))
320 (void) mongets(mtmp, DWARVISH_CLOAK);
321 if (rn2(7))
322 (void) mongets(mtmp, IRON_SHOES);
323 if (!rn2(4)) {
324 (void) mongets(mtmp, DWARVISH_SHORT_SWORD);
325 /* note: you can't use a mattock with a shield */
326 if (rn2(2))
327 (void) mongets(mtmp, DWARVISH_MATTOCK);
328 else {
329 (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
330 (void) mongets(mtmp, DWARVISH_ROUNDSHIELD);
332 (void) mongets(mtmp, DWARVISH_IRON_HELM);
333 if (!rn2(3))
334 (void) mongets(mtmp, DWARVISH_MITHRIL_COAT);
335 } else {
336 (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
339 break;
340 case S_KOP:
341 /* create Keystone Kops with cream pies to
342 throw. As suggested by KAA. [MRS] */
343 if (!rn2(4))
344 m_initthrow(mtmp, CREAM_PIE, 2);
345 if (!rn2(3))
346 (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE);
347 break;
348 case S_ORC:
349 if (rn2(2))
350 (void) mongets(mtmp, ORCISH_HELM);
351 switch ((mm != PM_ORC_CAPTAIN) ? mm
352 : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
353 case PM_MORDOR_ORC:
354 if (!rn2(3))
355 (void) mongets(mtmp, SCIMITAR);
356 if (!rn2(3))
357 (void) mongets(mtmp, ORCISH_SHIELD);
358 if (!rn2(3))
359 (void) mongets(mtmp, KNIFE);
360 if (!rn2(3))
361 (void) mongets(mtmp, ORCISH_CHAIN_MAIL);
362 break;
363 case PM_URUK_HAI:
364 if (!rn2(3))
365 (void) mongets(mtmp, ORCISH_CLOAK);
366 if (!rn2(3))
367 (void) mongets(mtmp, ORCISH_SHORT_SWORD);
368 if (!rn2(3))
369 (void) mongets(mtmp, IRON_SHOES);
370 if (!rn2(3)) {
371 (void) mongets(mtmp, ORCISH_BOW);
372 m_initthrow(mtmp, ORCISH_ARROW, 12);
374 if (!rn2(3))
375 (void) mongets(mtmp, URUK_HAI_SHIELD);
376 break;
377 default:
378 if (mm != PM_ORC_SHAMAN && rn2(2))
379 (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
380 ? ORCISH_DAGGER
381 : SCIMITAR);
383 break;
384 case S_OGRE:
385 if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
386 (void) mongets(mtmp, BATTLE_AXE);
387 else
388 (void) mongets(mtmp, CLUB);
389 break;
390 case S_TROLL:
391 if (!rn2(2))
392 switch (rn2(4)) {
393 case 0:
394 (void) mongets(mtmp, RANSEUR);
395 break;
396 case 1:
397 (void) mongets(mtmp, PARTISAN);
398 break;
399 case 2:
400 (void) mongets(mtmp, GLAIVE);
401 break;
402 case 3:
403 (void) mongets(mtmp, SPETUM);
404 break;
406 break;
407 case S_KOBOLD:
408 if (!rn2(4))
409 m_initthrow(mtmp, DART, 12);
410 break;
412 case S_CENTAUR:
413 if (rn2(2)) {
414 if (ptr == &mons[PM_FOREST_CENTAUR]) {
415 (void) mongets(mtmp, BOW);
416 m_initthrow(mtmp, ARROW, 12);
417 } else {
418 (void) mongets(mtmp, CROSSBOW);
419 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
422 break;
423 case S_WRAITH:
424 (void) mongets(mtmp, KNIFE);
425 (void) mongets(mtmp, LONG_SWORD);
426 break;
427 case S_ZOMBIE:
428 if (!rn2(4))
429 (void) mongets(mtmp, LEATHER_ARMOR);
430 if (!rn2(4))
431 (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD));
432 break;
433 case S_LIZARD:
434 if (mm == PM_SALAMANDER)
435 (void) mongets(mtmp,
436 (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO));
437 break;
438 case S_DEMON:
439 switch (mm) {
440 case PM_BALROG:
441 (void) mongets(mtmp, BULLWHIP);
442 (void) mongets(mtmp, BROADSWORD);
443 break;
444 case PM_ORCUS:
445 (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
446 break;
447 case PM_HORNED_DEVIL:
448 (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
449 break;
450 case PM_DISPATER:
451 (void) mongets(mtmp, WAN_STRIKING);
452 break;
453 case PM_YEENOGHU:
454 (void) mongets(mtmp, FLAIL);
455 break;
457 /* prevent djinn and mail daemons from leaving objects when
458 * they vanish
460 if (!is_demon(ptr))
461 break;
462 /*FALLTHRU*/
463 default:
465 * Now the general case, some chance of getting some type
466 * of weapon for "normal" monsters. Certain special types
467 * of monsters will get a bonus chance or different selections.
469 bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr);
470 switch (rnd(14 - (2 * bias))) {
471 case 1:
472 if (strongmonst(ptr))
473 (void) mongets(mtmp, BATTLE_AXE);
474 else
475 m_initthrow(mtmp, DART, 12);
476 break;
477 case 2:
478 if (strongmonst(ptr))
479 (void) mongets(mtmp, TWO_HANDED_SWORD);
480 else {
481 (void) mongets(mtmp, CROSSBOW);
482 m_initthrow(mtmp, CROSSBOW_BOLT, 12);
484 break;
485 case 3:
486 (void) mongets(mtmp, BOW);
487 m_initthrow(mtmp, ARROW, 12);
488 break;
489 case 4:
490 if (strongmonst(ptr))
491 (void) mongets(mtmp, LONG_SWORD);
492 else
493 m_initthrow(mtmp, DAGGER, 3);
494 break;
495 case 5:
496 if (strongmonst(ptr))
497 (void) mongets(mtmp, LUCERN_HAMMER);
498 else
499 (void) mongets(mtmp, AKLYS);
500 break;
501 default:
502 break;
504 break;
507 if ((int) mtmp->m_lev > rn2(75))
508 (void) mongets(mtmp, rnd_offensive_item(mtmp));
512 * Makes up money for monster's inventory.
513 * This will change with silver & copper coins
515 void
516 mkmonmoney(mtmp, amount)
517 struct monst *mtmp;
518 long amount;
520 struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE);
522 gold->quan = amount;
523 add_to_minv(mtmp, gold);
526 STATIC_OVL void
527 m_initinv(mtmp)
528 register struct monst *mtmp;
530 register int cnt;
531 register struct obj *otmp;
532 register struct permonst *ptr = mtmp->data;
534 if (Is_rogue_level(&u.uz))
535 return;
537 * Soldiers get armour & rations - armour approximates their ac.
538 * Nymphs may get mirror or potion of object detection.
540 switch (ptr->mlet) {
541 case S_HUMAN:
542 if (is_mercenary(ptr)) {
543 register int mac;
545 switch (monsndx(ptr)) {
546 case PM_GUARD:
547 mac = -1;
548 break;
549 case PM_SOLDIER:
550 mac = 3;
551 break;
552 case PM_SERGEANT:
553 mac = 0;
554 break;
555 case PM_LIEUTENANT:
556 mac = -2;
557 break;
558 case PM_CAPTAIN:
559 mac = -3;
560 break;
561 case PM_WATCHMAN:
562 mac = 3;
563 break;
564 case PM_WATCH_CAPTAIN:
565 mac = -2;
566 break;
567 default:
568 impossible("odd mercenary %d?", monsndx(ptr));
569 mac = 0;
570 break;
573 if (mac < -1 && rn2(5))
574 mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL
575 : CRYSTAL_PLATE_MAIL);
576 else if (mac < 3 && rn2(5))
577 mac +=
578 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL);
579 else if (rn2(5))
580 mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL
581 : STUDDED_LEATHER_ARMOR);
582 else
583 mac += 2 + mongets(mtmp, LEATHER_ARMOR);
585 if (mac < 10 && rn2(3))
586 mac += 1 + mongets(mtmp, HELMET);
587 else if (mac < 10 && rn2(2))
588 mac += 1 + mongets(mtmp, DENTED_POT);
589 if (mac < 10 && rn2(3))
590 mac += 1 + mongets(mtmp, SMALL_SHIELD);
591 else if (mac < 10 && rn2(2))
592 mac += 2 + mongets(mtmp, LARGE_SHIELD);
593 if (mac < 10 && rn2(3))
594 mac += 1 + mongets(mtmp, LOW_BOOTS);
595 else if (mac < 10 && rn2(2))
596 mac += 2 + mongets(mtmp, HIGH_BOOTS);
597 if (mac < 10 && rn2(3))
598 mac += 1 + mongets(mtmp, LEATHER_GLOVES);
599 else if (mac < 10 && rn2(2))
600 mac += 1 + mongets(mtmp, LEATHER_CLOAK);
602 nhUse(mac); /* suppress 'dead increment' from static analyzer */
604 if (ptr != &mons[PM_GUARD] && ptr != &mons[PM_WATCHMAN]
605 && ptr != &mons[PM_WATCH_CAPTAIN]) {
606 if (!rn2(3))
607 (void) mongets(mtmp, K_RATION);
608 if (!rn2(2))
609 (void) mongets(mtmp, C_RATION);
610 if (ptr != &mons[PM_SOLDIER] && !rn2(3))
611 (void) mongets(mtmp, BUGLE);
612 } else if (ptr == &mons[PM_WATCHMAN] && rn2(3))
613 (void) mongets(mtmp, TIN_WHISTLE);
614 } else if (ptr == &mons[PM_SHOPKEEPER]) {
615 (void) mongets(mtmp, SKELETON_KEY);
616 switch (rn2(4)) {
617 /* MAJOR fall through ... */
618 case 0:
619 (void) mongets(mtmp, WAN_MAGIC_MISSILE);
620 case 1:
621 (void) mongets(mtmp, POT_EXTRA_HEALING);
622 case 2:
623 (void) mongets(mtmp, POT_HEALING);
624 case 3:
625 (void) mongets(mtmp, WAN_STRIKING);
627 } else if (ptr->msound == MS_PRIEST
628 || quest_mon_represents_role(ptr, PM_PRIEST)) {
629 (void) mongets(mtmp, rn2(7) ? ROBE
630 : rn2(3) ? CLOAK_OF_PROTECTION
631 : CLOAK_OF_MAGIC_RESISTANCE);
632 (void) mongets(mtmp, SMALL_SHIELD);
633 mkmonmoney(mtmp, (long) rn1(10, 20));
634 } else if (quest_mon_represents_role(ptr, PM_MONK)) {
635 (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
637 break;
638 case S_NYMPH:
639 if (!rn2(2))
640 (void) mongets(mtmp, MIRROR);
641 if (!rn2(2))
642 (void) mongets(mtmp, POT_OBJECT_DETECTION);
643 break;
644 case S_GIANT:
645 if (ptr == &mons[PM_MINOTAUR]) {
646 if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz)))
647 (void) mongets(mtmp, WAN_DIGGING);
648 } else if (is_giant(ptr)) {
649 for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) {
650 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1),
651 FALSE, FALSE);
652 otmp->quan = (long) rn1(2, 3);
653 otmp->owt = weight(otmp);
654 (void) mpickobj(mtmp, otmp);
657 break;
658 case S_WRAITH:
659 if (ptr == &mons[PM_NAZGUL]) {
660 otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
661 curse(otmp);
662 (void) mpickobj(mtmp, otmp);
664 break;
665 case S_LICH:
666 if (ptr == &mons[PM_MASTER_LICH] && !rn2(13))
667 (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING));
668 else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) {
669 otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE,
670 rn2(13) ? FALSE : TRUE);
671 if (otmp->spe < 2)
672 otmp->spe = rnd(3);
673 if (!rn2(4))
674 otmp->oerodeproof = 1;
675 (void) mpickobj(mtmp, otmp);
677 break;
678 case S_MUMMY:
679 if (rn2(7))
680 (void) mongets(mtmp, MUMMY_WRAPPING);
681 break;
682 case S_QUANTMECH:
683 if (!rn2(20)) {
684 otmp = mksobj(LARGE_BOX, FALSE, FALSE);
685 otmp->spe = 1; /* flag for special box */
686 otmp->owt = weight(otmp);
687 (void) mpickobj(mtmp, otmp);
689 break;
690 case S_LEPRECHAUN:
691 mkmonmoney(mtmp, (long) d(level_difficulty(), 30));
692 break;
693 case S_DEMON:
694 /* moved here from m_initweap() because these don't
695 have AT_WEAP so m_initweap() is not called for them */
696 if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) {
697 (void) mongets(mtmp, SPEAR);
698 } else if (ptr == &mons[PM_ASMODEUS]) {
699 (void) mongets(mtmp, WAN_COLD);
700 (void) mongets(mtmp, WAN_FIRE);
702 break;
703 case S_GNOME:
704 if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) {
705 otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE);
706 otmp->quan = 1;
707 otmp->owt = weight(otmp);
708 if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit)
709 begin_burn(otmp, FALSE);
711 break;
712 default:
713 break;
716 /* ordinary soldiers rarely have access to magic (or gold :-) */
717 if (ptr == &mons[PM_SOLDIER] && rn2(13))
718 return;
720 if ((int) mtmp->m_lev > rn2(50))
721 (void) mongets(mtmp, rnd_defensive_item(mtmp));
722 if ((int) mtmp->m_lev > rn2(100))
723 (void) mongets(mtmp, rnd_misc_item(mtmp));
724 if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5))
725 mkmonmoney(mtmp,
726 (long) d(level_difficulty(), mtmp->minvent ? 5 : 10));
729 /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */
730 struct monst *
731 clone_mon(mon, x, y)
732 struct monst *mon;
733 xchar x, y; /* clone's preferred location or 0 (near mon) */
735 coord mm;
736 struct monst *m2;
738 /* may be too weak or have been extinguished for population control */
739 if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT))
740 return (struct monst *) 0;
742 if (x == 0) {
743 mm.x = mon->mx;
744 mm.y = mon->my;
745 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
746 return (struct monst *) 0;
747 } else if (!isok(x, y)) {
748 return (struct monst *) 0; /* paranoia */
749 } else {
750 mm.x = x;
751 mm.y = y;
752 if (MON_AT(mm.x, mm.y)) {
753 if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y))
754 return (struct monst *) 0;
757 m2 = newmonst();
758 *m2 = *mon; /* copy condition of old monster */
759 m2->mextra = (struct mextra *) 0;
760 m2->nmon = fmon;
761 fmon = m2;
762 m2->m_id = context.ident++;
763 if (!m2->m_id)
764 m2->m_id = context.ident++; /* ident overflowed */
765 m2->mx = mm.x;
766 m2->my = mm.y;
768 m2->mcloned = 1;
769 m2->minvent = (struct obj *) 0; /* objects don't clone */
770 m2->mleashed = FALSE;
771 /* Max HP the same, but current HP halved for both. The caller
772 * might want to override this by halving the max HP also.
773 * When current HP is odd, the original keeps the extra point.
775 m2->mhpmax = mon->mhpmax;
776 m2->mhp = mon->mhp / 2;
777 mon->mhp -= m2->mhp;
779 /* since shopkeepers and guards will only be cloned if they've been
780 * polymorphed away from their original forms, the clone doesn't have
781 * room for the extra information. we also don't want two shopkeepers
782 * around for the same shop.
784 if (mon->isshk)
785 m2->isshk = FALSE;
786 if (mon->isgd)
787 m2->isgd = FALSE;
788 if (mon->ispriest)
789 m2->ispriest = FALSE;
790 place_monster(m2, m2->mx, m2->my);
791 if (emits_light(m2->data))
792 new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER,
793 monst_to_any(m2));
794 if (has_mname(mon)) {
795 m2 = christen_monst(m2, MNAME(mon));
796 } else if (mon->isshk) {
797 m2 = christen_monst(m2, shkname(mon));
800 /* not all clones caused by player are tame or peaceful */
801 if (!context.mon_moving) {
802 if (mon->mtame)
803 m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0;
804 else if (mon->mpeaceful)
805 m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0;
808 newsym(m2->mx, m2->my); /* display the new monster */
809 if (m2->mtame) {
810 if (mon->isminion) {
811 newemin(m2);
812 if (EMIN(mon))
813 *(EMIN(m2)) = *(EMIN(mon));
814 } else {
815 /* because m2 is a copy of mon it is tame but not init'ed.
816 * however, tamedog will not re-tame a tame dog, so m2
817 * must be made non-tame to get initialized properly.
819 m2->mtame = 0;
820 if (tamedog(m2, (struct obj *) 0)) {
821 *(EDOG(m2)) = *(EDOG(mon));
825 set_malign(m2);
827 return m2;
831 * Propagate a species
833 * Once a certain number of monsters are created, don't create any more
834 * at random (i.e. make them extinct). The previous (3.2) behavior was
835 * to do this when a certain number had _died_, which didn't make
836 * much sense.
838 * Returns FALSE propagation unsuccessful
839 * TRUE propagation successful
841 boolean
842 propagate(mndx, tally, ghostly)
843 int mndx;
844 boolean tally;
845 boolean ghostly;
847 boolean result;
848 uchar lim = mbirth_limit(mndx);
849 boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */
851 result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE;
853 /* if it's unique, don't ever make it again */
854 if (mons[mndx].geno & G_UNIQ)
855 mvitals[mndx].mvflags |= G_EXTINCT;
857 if (mvitals[mndx].born < 255 && tally
858 && (!ghostly || (ghostly && result)))
859 mvitals[mndx].born++;
860 if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN)
861 && !(mvitals[mndx].mvflags & G_EXTINCT)) {
862 if (wizard) {
863 debugpline1("Automatically extinguished %s.",
864 makeplural(mons[mndx].mname));
866 mvitals[mndx].mvflags |= G_EXTINCT;
867 reset_rndmonst(mndx);
869 return result;
872 /* amount of HP to lose from level drain (or gain from Stormbringer) */
874 monhp_per_lvl(mon)
875 struct monst *mon;
877 struct permonst *ptr = mon->data;
878 int hp = rnd(8); /* default is d8 */
880 /* like newmonhp, but home elementals are ignored, riders use normal d8 */
881 if (is_golem(ptr)) {
882 /* draining usually won't be applicable for these critters */
883 hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel;
884 } else if (ptr->mlevel > 49) {
885 /* arbitrary; such monsters won't be involved in draining anyway */
886 hp = 4 + rnd(4); /* 5..8 */
887 } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) {
888 /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4)
890 hp = 4 + rn2(5); /* 4..8 */
891 } else if (!mon->m_lev) {
892 /* level 0 monsters use 1d4 instead of Nd8 */
893 hp = rnd(4);
895 return hp;
898 /* set up a new monster's initial level and hit points;
899 used by newcham() as well as by makemon() */
900 void
901 newmonhp(mon, mndx)
902 struct monst *mon;
903 int mndx;
905 struct permonst *ptr = &mons[mndx];
907 mon->m_lev = adj_lev(ptr);
908 if (is_golem(ptr)) {
909 mon->mhpmax = mon->mhp = golemhp(mndx);
910 } else if (is_rider(ptr)) {
911 /* we want low HP, but a high mlevel so they can attack well */
912 mon->mhpmax = mon->mhp = d(10, 8);
913 } else if (ptr->mlevel > 49) {
914 /* "special" fixed hp monster
915 * the hit points are encoded in the mlevel in a somewhat strange
916 * way to fit in the 50..127 positive range of a signed character
917 * above the 1..49 that indicate "normal" monster levels */
918 mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6);
919 mon->m_lev = mon->mhp / 4; /* approximation */
920 } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) {
921 /* adult dragons */
922 mon->mhpmax = mon->mhp =
923 (int) (In_endgame(&u.uz)
924 ? (8 * mon->m_lev)
925 : (4 * mon->m_lev + d((int) mon->m_lev, 4)));
926 } else if (!mon->m_lev) {
927 mon->mhpmax = mon->mhp = rnd(4);
928 } else {
929 mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8);
930 if (is_home_elemental(ptr))
931 mon->mhpmax = (mon->mhp *= 3);
935 struct mextra *
936 newmextra()
938 struct mextra *mextra;
940 mextra = (struct mextra *) alloc(sizeof(struct mextra));
941 mextra->mname = 0;
942 mextra->egd = 0;
943 mextra->epri = 0;
944 mextra->eshk = 0;
945 mextra->emin = 0;
946 mextra->edog = 0;
947 mextra->mcorpsenm = NON_PM;
948 return mextra;
951 boolean
952 makemon_rnd_goodpos(mon, gpflags, cc)
953 struct monst *mon;
954 unsigned gpflags;
955 coord *cc;
957 int tryct = 0;
958 int nx,ny;
959 boolean good;
961 do {
962 nx = rn1(COLNO - 3, 2);
963 ny = rn2(ROWNO);
964 good = (!in_mklev && cansee(nx,ny)) ? FALSE
965 : goodpos(nx, ny, mon, gpflags);
966 } while ((++tryct < 50) && !good);
968 if (!good) {
969 /* else go through all map positions, twice, first round
970 ignoring positions in sight, and pick first good one.
971 skip first round if we're in special level loader or blind */
972 int xofs = nx;
973 int yofs = ny;
974 int dx,dy;
975 int bl = (in_mklev || Blind) ? 1 : 0;
977 for ( ; bl < 2; bl++) {
978 for (dx = 0; dx < COLNO; dx++)
979 for (dy = 0; dy < ROWNO; dy++) {
980 nx = ((dx + xofs) % (COLNO - 1)) + 1;
981 ny = ((dy + yofs) % (ROWNO - 1)) + 1;
982 if (bl == 0 && cansee(nx,ny))
983 continue;
984 if (goodpos(nx, ny, mon, gpflags))
985 goto gotgood;
987 if (bl == 0 && (!mon || mon->data->mmove)) {
988 /* all map positions are visible (or not good),
989 try to pick something logical */
990 if (dnstair.sx && !rn2(2)) {
991 nx = dnstair.sx;
992 ny = dnstair.sy;
993 } else if (upstair.sx && !rn2(2)) {
994 nx = upstair.sx;
995 ny = upstair.sy;
996 } else if (dnladder.sx && !rn2(2)) {
997 nx = dnladder.sx;
998 ny = dnladder.sy;
999 } else if (upladder.sx && !rn2(2)) {
1000 nx = upladder.sx;
1001 ny = upladder.sy;
1003 if (goodpos(nx, ny, mon, gpflags))
1004 goto gotgood;
1007 } else {
1008 gotgood:
1009 cc->x = nx;
1010 cc->y = ny;
1011 return TRUE;
1013 return FALSE;
1017 * called with [x,y] = coordinates;
1018 * [0,0] means anyplace
1019 * [u.ux,u.uy] means: near player (if !in_mklev)
1021 * In case we make a monster group, only return the one at [x,y].
1023 struct monst *
1024 makemon(ptr, x, y, mmflags)
1025 register struct permonst *ptr;
1026 register int x, y;
1027 int mmflags;
1029 register struct monst *mtmp;
1030 int mndx, mcham, ct, mitem;
1031 boolean anymon = (!ptr);
1032 boolean byyou = (x == u.ux && y == u.uy);
1033 boolean allow_minvent = ((mmflags & NO_MINVENT) == 0);
1034 boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0);
1035 unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0;
1037 /* if caller wants random location, do it here */
1038 if (x == 0 && y == 0) {
1039 coord cc;
1040 struct monst fakemon;
1042 cc.x = cc.y = 0; /* lint suppression */
1043 fakemon.data = ptr; /* set up for goodpos */
1044 if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *)0,
1045 gpflags, &cc))
1046 return (struct monst *) 0;
1047 x = cc.x;
1048 y = cc.y;
1049 } else if (byyou && !in_mklev) {
1050 coord bypos;
1052 if (enexto_core(&bypos, u.ux, u.uy, ptr, gpflags)) {
1053 x = bypos.x;
1054 y = bypos.y;
1055 } else
1056 return (struct monst *) 0;
1059 /* Does monster already exist at the position? */
1060 if (MON_AT(x, y)) {
1061 if ((mmflags & MM_ADJACENTOK) != 0) {
1062 coord bypos;
1063 if (enexto_core(&bypos, x, y, ptr, gpflags)) {
1064 x = bypos.x;
1065 y = bypos.y;
1066 } else
1067 return (struct monst *) 0;
1068 } else
1069 return (struct monst *) 0;
1072 if (ptr) {
1073 mndx = monsndx(ptr);
1074 /* if you are to make a specific monster and it has
1075 already been genocided, return */
1076 if (mvitals[mndx].mvflags & G_GENOD)
1077 return (struct monst *) 0;
1078 if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) {
1079 debugpline1("Explicitly creating extinct monster %s.",
1080 mons[mndx].mname);
1082 } else {
1083 /* make a random (common) monster that can survive here.
1084 * (the special levels ask for random monsters at specific
1085 * positions, causing mass drowning on the medusa level,
1086 * for instance.)
1088 int tryct = 0; /* maybe there are no good choices */
1089 struct monst fakemon;
1091 do {
1092 if (!(ptr = rndmonst())) {
1093 debugpline0("Warning: no monster.");
1094 return (struct monst *) 0; /* no more monsters! */
1096 fakemon.data = ptr; /* set up for goodpos */
1097 } while (++tryct <= 50
1098 /* in Sokoban, don't accept a giant on first try;
1099 after that, boulder carriers are fair game */
1100 && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz))
1101 || !goodpos(x, y, &fakemon, gpflags)));
1102 mndx = monsndx(ptr);
1104 (void) propagate(mndx, countbirth, FALSE);
1105 mtmp = newmonst();
1106 *mtmp = zeromonst; /* clear all entries in structure */
1108 if (mmflags & MM_EGD)
1109 newegd(mtmp);
1110 if (mmflags & MM_EPRI)
1111 newepri(mtmp);
1112 if (mmflags & MM_ESHK)
1113 neweshk(mtmp);
1114 if (mmflags & MM_EMIN)
1115 newemin(mtmp);
1116 if (mmflags & MM_EDOG)
1117 newedog(mtmp);
1119 mtmp->nmon = fmon;
1120 fmon = mtmp;
1121 mtmp->m_id = context.ident++;
1122 if (!mtmp->m_id)
1123 mtmp->m_id = context.ident++; /* ident overflowed */
1124 set_mon_data(mtmp, ptr, 0);
1125 if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1126 quest_status.leader_m_id = mtmp->m_id;
1127 mtmp->mnum = mndx;
1129 /* set up level and hit points */
1130 newmonhp(mtmp, mndx);
1132 if (is_female(ptr))
1133 mtmp->female = TRUE;
1134 else if (is_male(ptr))
1135 mtmp->female = FALSE;
1136 /* leader and nemesis gender is usually hardcoded in mons[],
1137 but for ones which can be random, it has already been chosen
1138 (in role_init(), for possible use by the quest pager code) */
1139 else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx)
1140 mtmp->female = quest_status.ldrgend;
1141 else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx)
1142 mtmp->female = quest_status.nemgend;
1143 else
1144 mtmp->female = rn2(2); /* ignored for neuters */
1146 if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */
1147 mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1));
1148 /* quest leader and nemesis both know about all trap types */
1149 if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS)
1150 mtmp->mtrapseen = ~0;
1152 place_monster(mtmp, x, y);
1153 mtmp->mcansee = mtmp->mcanmove = TRUE;
1154 mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr);
1156 switch (ptr->mlet) {
1157 case S_MIMIC:
1158 set_mimic_sym(mtmp);
1159 break;
1160 case S_SPIDER:
1161 case S_SNAKE:
1162 if (in_mklev)
1163 if (x && y)
1164 (void) mkobj_at(0, x, y, TRUE);
1165 (void) hideunder(mtmp);
1166 break;
1167 case S_LIGHT:
1168 case S_ELEMENTAL:
1169 if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) {
1170 mtmp->perminvis = TRUE;
1171 mtmp->minvis = TRUE;
1173 break;
1174 case S_EEL:
1175 (void) hideunder(mtmp);
1176 break;
1177 case S_LEPRECHAUN:
1178 mtmp->msleeping = 1;
1179 break;
1180 case S_JABBERWOCK:
1181 case S_NYMPH:
1182 if (rn2(5) && !u.uhave.amulet)
1183 mtmp->msleeping = 1;
1184 break;
1185 case S_ORC:
1186 if (Race_if(PM_ELF))
1187 mtmp->mpeaceful = FALSE;
1188 break;
1189 case S_UNICORN:
1190 if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp))
1191 mtmp->mpeaceful = TRUE;
1192 break;
1193 case S_BAT:
1194 if (Inhell && is_bat(ptr))
1195 mon_adjust_speed(mtmp, 2, (struct obj *) 0);
1196 break;
1198 if ((ct = emits_light(mtmp->data)) > 0)
1199 new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER,
1200 monst_to_any(mtmp));
1201 mitem = 0; /* extra inventory item for this monster */
1203 if (mndx == PM_VLAD_THE_IMPALER)
1204 mitem = CANDELABRUM_OF_INVOCATION;
1205 mtmp->cham = NON_PM; /* default is "not a shapechanger" */
1206 if ((mcham = pm_to_cham(mndx)) != NON_PM) {
1207 /* this is a shapechanger after all */
1208 if (Protection_from_shape_changers
1209 || mndx == PM_VLAD_THE_IMPALER) {
1210 ; /* stuck in its natural form (NON_PM) */
1211 } else {
1212 mtmp->cham = mcham;
1213 /* Note: shapechanger's initial form used to be
1214 chosen here with rndmonst(), yielding a monster
1215 which was appropriate to the level's difficulty
1216 but ignored the changer's usual type selection
1217 so would be inappropriate for vampshifters.
1218 Let newcham() pick the shape. */
1219 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
1220 allow_minvent = FALSE;
1222 } else if (mndx == PM_WIZARD_OF_YENDOR) {
1223 mtmp->iswiz = TRUE;
1224 context.no_of_wizards++;
1225 if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz))
1226 mitem = SPE_DIG;
1227 } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) {
1228 mtmp = christen_monst(mtmp, rndghostname());
1229 } else if (mndx == PM_CROESUS) {
1230 mitem = TWO_HANDED_SWORD;
1231 } else if (ptr->msound == MS_NEMESIS) {
1232 mitem = BELL_OF_OPENING;
1233 } else if (mndx == PM_PESTILENCE) {
1234 mitem = POT_SICKNESS;
1236 if (mitem && allow_minvent)
1237 (void) mongets(mtmp, mitem);
1239 if (in_mklev) {
1240 if ((is_ndemon(ptr) || mndx == PM_WUMPUS
1241 || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL)
1242 && !u.uhave.amulet && rn2(5))
1243 mtmp->msleeping = TRUE;
1244 } else {
1245 if (byyou) {
1246 newsym(mtmp->mx, mtmp->my);
1247 set_apparxy(mtmp);
1250 if (is_dprince(ptr) && ptr->msound == MS_BRIBE) {
1251 mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1;
1252 mtmp->mavenge = 0;
1253 if (uwep && uwep->oartifact == ART_EXCALIBUR)
1254 mtmp->mpeaceful = mtmp->mtame = FALSE;
1256 #ifndef DCC30_BUG
1257 if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0)
1258 #else
1259 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
1260 same expression. */
1261 if (mndx == PM_LONG_WORM
1262 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0))
1263 #endif
1265 /* we can now create worms with tails - 11/91 */
1266 initworm(mtmp, rn2(5));
1267 if (count_wsegs(mtmp))
1268 place_worm_tail_randomly(mtmp, x, y);
1270 /* it's possible to create an ordinary monster of some special
1271 types; make sure their extended data is initialized to
1272 something sensible if caller hasn't specified MM_EPRI|MM_EMIN
1273 (when they're specified, caller intends to handle this itself) */
1274 if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST)
1275 ? !(mmflags & (MM_EPRI | MM_EMIN))
1276 : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) {
1277 struct emin *eminp;
1278 newemin(mtmp);
1279 eminp = EMIN(mtmp);
1281 mtmp->isminion = 1; /* make priest be a roamer */
1282 eminp->min_align = rn2(3) - 1; /* no A_NONE */
1283 eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3));
1284 mtmp->mpeaceful = (eminp->min_align == u.ualign.type)
1285 ? !eminp->renegade
1286 : eminp->renegade;
1288 set_malign(mtmp); /* having finished peaceful changes */
1289 if (anymon) {
1290 if ((ptr->geno & G_SGROUP) && rn2(2)) {
1291 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1292 } else if (ptr->geno & G_LGROUP) {
1293 if (rn2(3))
1294 m_initlgrp(mtmp, mtmp->mx, mtmp->my);
1295 else
1296 m_initsgrp(mtmp, mtmp->mx, mtmp->my);
1300 if (allow_minvent) {
1301 if (is_armed(ptr))
1302 m_initweap(mtmp); /* equip with weapons / armor */
1303 m_initinv(mtmp); /* add on a few special items incl. more armor */
1304 m_dowear(mtmp, TRUE);
1305 } else {
1306 /* no initial inventory is allowed */
1307 if (mtmp->minvent)
1308 discard_minvent(mtmp);
1309 mtmp->minvent = (struct obj *) 0; /* caller expects this */
1311 if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) {
1312 if (ptr->mflags3 & M3_WAITFORU)
1313 mtmp->mstrategy |= STRAT_WAITFORU;
1314 if (ptr->mflags3 & M3_CLOSE)
1315 mtmp->mstrategy |= STRAT_CLOSE;
1316 if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS))
1317 mtmp->mstrategy |= STRAT_APPEARMSG;
1320 if (!in_mklev)
1321 newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */
1323 return mtmp;
1327 mbirth_limit(mndx)
1328 int mndx;
1330 /* assert(MAXMONNO < 255); */
1331 return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO);
1334 /* used for wand/scroll/spell of create monster */
1335 /* returns TRUE iff you know monsters have been created */
1336 boolean
1337 create_critters(cnt, mptr, neverask)
1338 int cnt;
1339 struct permonst *mptr; /* usually null; used for confused reading */
1340 boolean neverask;
1342 coord c;
1343 int x, y;
1344 struct monst *mon;
1345 boolean known = FALSE;
1346 boolean ask = (wizard && !neverask);
1348 while (cnt--) {
1349 if (ask) {
1350 if (create_particular()) {
1351 known = TRUE;
1352 continue;
1353 } else
1354 ask = FALSE; /* ESC will shut off prompting */
1356 x = u.ux, y = u.uy;
1357 /* if in water, try to encourage an aquatic monster
1358 by finding and then specifying another wet location */
1359 if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL]))
1360 x = c.x, y = c.y;
1362 mon = makemon(mptr, x, y, NO_MM_FLAGS);
1363 if (mon && canspotmon(mon))
1364 known = TRUE;
1366 return known;
1369 STATIC_OVL boolean
1370 uncommon(mndx)
1371 int mndx;
1373 if (mons[mndx].geno & (G_NOGEN | G_UNIQ))
1374 return TRUE;
1375 if (mvitals[mndx].mvflags & G_GONE)
1376 return TRUE;
1377 if (Inhell)
1378 return (boolean) (mons[mndx].maligntyp > A_NEUTRAL);
1379 else
1380 return (boolean) ((mons[mndx].geno & G_HELL) != 0);
1384 * shift the probability of a monster's generation by
1385 * comparing the dungeon alignment and monster alignment.
1386 * return an integer in the range of 0-5.
1388 STATIC_OVL int
1389 align_shift(ptr)
1390 register struct permonst *ptr;
1392 static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */
1393 static NEARDATA s_level *lev;
1394 register int alshift;
1396 if (oldmoves != moves) {
1397 lev = Is_special(&u.uz);
1398 oldmoves = moves;
1400 switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
1401 default: /* just in case */
1402 case AM_NONE:
1403 alshift = 0;
1404 break;
1405 case AM_LAWFUL:
1406 alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT);
1407 break;
1408 case AM_NEUTRAL:
1409 alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT;
1410 break;
1411 case AM_CHAOTIC:
1412 alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT);
1413 break;
1415 return alshift;
1418 static NEARDATA struct {
1419 int choice_count;
1420 char mchoices[SPECIAL_PM]; /* value range is 0..127 */
1421 } rndmonst_state = { -1, { 0 } };
1423 /* select a random monster type */
1424 struct permonst *
1425 rndmonst()
1427 register struct permonst *ptr;
1428 register int mndx, ct;
1430 if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0)
1431 return ptr;
1433 if (rndmonst_state.choice_count < 0) { /* need to recalculate */
1434 int zlevel, minmlev, maxmlev;
1435 boolean elemlevel;
1436 boolean upper;
1438 rndmonst_state.choice_count = 0;
1439 /* look for first common monster */
1440 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) {
1441 if (!uncommon(mndx))
1442 break;
1443 rndmonst_state.mchoices[mndx] = 0;
1445 if (mndx == SPECIAL_PM) {
1446 /* evidently they've all been exterminated */
1447 debugpline0("rndmonst: no common mons!");
1448 return (struct permonst *) 0;
1449 } /* else `mndx' now ready for use below */
1450 zlevel = level_difficulty();
1451 /* determine the level of the weakest monster to make. */
1452 minmlev = zlevel / 6;
1453 /* determine the level of the strongest monster to make. */
1454 maxmlev = (zlevel + u.ulevel) / 2;
1455 upper = Is_rogue_level(&u.uz);
1456 elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
1459 * Find out how many monsters exist in the range we have selected.
1461 for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */
1462 ptr = &mons[mndx];
1463 rndmonst_state.mchoices[mndx] = 0;
1464 if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev))
1465 continue;
1466 if (upper && !isupper(def_monsyms[(int) (ptr->mlet)].sym))
1467 continue;
1468 if (elemlevel && wrong_elem_type(ptr))
1469 continue;
1470 if (uncommon(mndx))
1471 continue;
1472 if (Inhell && (ptr->geno & G_NOHELL))
1473 continue;
1474 ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr);
1475 if (ct < 0 || ct > 127)
1476 panic("rndmonst: bad count [#%d: %d]", mndx, ct);
1477 rndmonst_state.choice_count += ct;
1478 rndmonst_state.mchoices[mndx] = (char) ct;
1481 * Possible modification: if choice_count is "too low",
1482 * expand minmlev..maxmlev range and try again.
1484 } /* choice_count+mchoices[] recalc */
1486 if (rndmonst_state.choice_count <= 0) {
1487 /* maybe no common mons left, or all are too weak or too strong */
1488 debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count);
1489 return (struct permonst *) 0;
1493 * Now, select a monster at random.
1495 ct = rnd(rndmonst_state.choice_count);
1496 for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++)
1497 if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0)
1498 break;
1500 if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */
1501 impossible("rndmonst: bad `mndx' [#%d]", mndx);
1502 return (struct permonst *) 0;
1504 return &mons[mndx];
1507 /* called when you change level (experience or dungeon depth) or when
1508 monster species can no longer be created (genocide or extinction) */
1509 void
1510 reset_rndmonst(mndx)
1511 int mndx; /* particular species that can no longer be created */
1513 /* cached selection info is out of date */
1514 if (mndx == NON_PM) {
1515 rndmonst_state.choice_count = -1; /* full recalc needed */
1516 } else if (mndx < SPECIAL_PM) {
1517 rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx];
1518 rndmonst_state.mchoices[mndx] = 0;
1519 } /* note: safe to ignore extinction of unique monsters */
1522 /* decide whether it's ok to generate a candidate monster by mkclass() */
1523 STATIC_OVL boolean
1524 mk_gen_ok(mndx, mvflagsmask, genomask)
1525 int mndx, mvflagsmask, genomask;
1527 struct permonst *ptr = &mons[mndx];
1529 if (mvitals[mndx].mvflags & mvflagsmask)
1530 return FALSE;
1531 if (ptr->geno & genomask)
1532 return FALSE;
1533 if (is_placeholder(ptr))
1534 return FALSE;
1535 #ifdef MAIL
1536 /* special levels might ask for random demon type; reject this one */
1537 if (ptr == &mons[PM_MAIL_DAEMON])
1538 return FALSE;
1539 #endif
1540 return TRUE;
1543 /* Make one of the multiple types of a given monster class.
1544 * The second parameter specifies a special casing bit mask
1545 * to allow the normal genesis masks to be deactivated.
1546 * Returns Null if no monsters in that class can be made.
1548 struct permonst *
1549 mkclass(class, spc)
1550 char class;
1551 int spc;
1553 register int first, last, num = 0;
1554 int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc;
1556 maxmlev = level_difficulty() >> 1;
1557 if (class < 1 || class >= MAXMCLASSES) {
1558 impossible("mkclass called with bad class!");
1559 return (struct permonst *) 0;
1561 /* Assumption #1: monsters of a given class are contiguous in the
1562 * mons[] array.
1564 for (first = LOW_PM; first < SPECIAL_PM; first++)
1565 if (mons[first].mlet == class)
1566 break;
1567 if (first == SPECIAL_PM)
1568 return (struct permonst *) 0;
1570 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1571 if (mk_gen_ok(last, G_GONE, mask)) {
1572 /* consider it */
1573 if (num && toostrong(last, maxmlev)
1574 && monstr[last] != monstr[last - 1] && rn2(2))
1575 break;
1576 num += mons[last].geno & G_FREQ;
1578 if (!num)
1579 return (struct permonst *) 0;
1581 /* Assumption #2: monsters of a given class are presented in ascending
1582 * order of strength.
1584 for (num = rnd(num); num > 0; first++)
1585 if (mk_gen_ok(first, G_GONE, mask)) {
1586 /* skew towards lower value monsters at lower exp. levels */
1587 num -= mons[first].geno & G_FREQ;
1588 if (num && adj_lev(&mons[first]) > (u.ulevel * 2)) {
1589 /* but not when multiple monsters are same level */
1590 if (mons[first].mlevel != mons[first + 1].mlevel)
1591 num--;
1594 first--; /* correct an off-by-one error */
1596 return &mons[first];
1599 /* like mkclass(), but excludes difficulty considerations; used when
1600 player with polycontrol picks a class instead of a specific type;
1601 genocided types are avoided but extinct ones are acceptable; we don't
1602 check polyok() here--caller accepts some choices !polyok() would reject */
1604 mkclass_poly(class)
1605 int class;
1607 register int first, last, num = 0;
1609 for (first = LOW_PM; first < SPECIAL_PM; first++)
1610 if (mons[first].mlet == class)
1611 break;
1612 if (first == SPECIAL_PM)
1613 return NON_PM;
1615 for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++)
1616 if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ)))
1617 num += mons[last].geno & G_FREQ;
1618 if (!num)
1619 return NON_PM;
1621 for (num = rnd(num); num > 0; first++)
1622 if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ)))
1623 num -= mons[first].geno & G_FREQ;
1624 first--; /* correct an off-by-one error */
1626 return first;
1629 /* adjust strength of monsters based on u.uz and u.ulevel */
1631 adj_lev(ptr)
1632 register struct permonst *ptr;
1634 int tmp, tmp2;
1636 if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
1637 /* does not depend on other strengths, but does get stronger
1638 * every time he is killed
1640 tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died;
1641 if (tmp > 49)
1642 tmp = 49;
1643 return tmp;
1646 if ((tmp = ptr->mlevel) > 49)
1647 return 50; /* "special" demons/devils */
1648 tmp2 = (level_difficulty() - tmp);
1649 if (tmp2 < 0)
1650 tmp--; /* if mlevel > u.uz decrement tmp */
1651 else
1652 tmp += (tmp2 / 5); /* else increment 1 per five diff */
1654 tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */
1655 if (tmp2 > 0)
1656 tmp += (tmp2 / 4); /* level as well */
1658 tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */
1659 if (tmp2 > 49)
1660 tmp2 = 49; /* hard upper limit */
1661 return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
1664 /* monster earned experience and will gain some hit points; it might also
1665 grow into a bigger monster (baby to adult, soldier to officer, etc) */
1666 struct permonst *
1667 grow_up(mtmp, victim)
1668 struct monst *mtmp, *victim;
1670 int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold;
1671 struct permonst *ptr = mtmp->data;
1673 /* monster died after killing enemy but before calling this function */
1674 /* currently possible if killing a gas spore */
1675 if (mtmp->mhp <= 0)
1676 return (struct permonst *) 0;
1678 /* note: none of the monsters with special hit point calculations
1679 have both little and big forms */
1680 oldtype = monsndx(ptr);
1681 newtype = little_to_big(oldtype);
1682 if (newtype == PM_PRIEST && mtmp->female)
1683 newtype = PM_PRIESTESS;
1685 /* growth limits differ depending on method of advancement */
1686 if (victim) { /* killed a monster */
1688 * The HP threshold is the maximum number of hit points for the
1689 * current level; once exceeded, a level will be gained.
1690 * Possible bug: if somehow the hit points are already higher
1691 * than that, monster will gain a level without any increase in HP.
1693 hp_threshold = mtmp->m_lev * 8; /* normal limit */
1694 if (!mtmp->m_lev)
1695 hp_threshold = 4;
1696 else if (is_golem(ptr)) /* strange creatures */
1697 hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1;
1698 else if (is_home_elemental(ptr))
1699 hp_threshold *= 3;
1700 lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */
1701 /* If they can grow up, be sure the level is high enough for that */
1702 if (oldtype != newtype && mons[newtype].mlevel > lev_limit)
1703 lev_limit = (int) mons[newtype].mlevel;
1704 /* number of hit points to gain; unlike for the player, we put
1705 the limit at the bottom of the next level rather than the top */
1706 max_increase = rnd((int) victim->m_lev + 1);
1707 if (mtmp->mhpmax + max_increase > hp_threshold + 1)
1708 max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0);
1709 cur_increase = (max_increase > 1) ? rn2(max_increase) : 0;
1710 } else {
1711 /* a gain level potion or wraith corpse; always go up a level
1712 unless already at maximum (49 is hard upper limit except
1713 for demon lords, who start at 50 and can't go any higher) */
1714 max_increase = cur_increase = rnd(8);
1715 hp_threshold = 0; /* smaller than `mhpmax + max_increase' */
1716 lev_limit = 50; /* recalc below */
1719 mtmp->mhpmax += max_increase;
1720 mtmp->mhp += cur_increase;
1721 if (mtmp->mhpmax <= hp_threshold)
1722 return ptr; /* doesn't gain a level */
1724 if (is_mplayer(ptr))
1725 lev_limit = 30; /* same as player */
1726 else if (lev_limit < 5)
1727 lev_limit = 5; /* arbitrary */
1728 else if (lev_limit > 49)
1729 lev_limit = (ptr->mlevel > 49 ? 50 : 49);
1731 if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) {
1732 ptr = &mons[newtype];
1733 if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */
1734 if (canspotmon(mtmp))
1735 pline("As %s grows up into %s, %s %s!", mon_nam(mtmp),
1736 an(ptr->mname), mhe(mtmp),
1737 nonliving(ptr) ? "expires" : "dies");
1738 set_mon_data(mtmp, ptr, -1); /* keep mvitals[] accurate */
1739 mondied(mtmp);
1740 return (struct permonst *) 0;
1741 } else if (canspotmon(mtmp)) {
1742 pline("%s %s %s.", Monnam(mtmp),
1743 humanoid(ptr) ? "becomes" : "grows up into",
1744 an(ptr->mname));
1746 set_mon_data(mtmp, ptr, 1); /* preserve intrinsics */
1747 newsym(mtmp->mx, mtmp->my); /* color may change */
1748 lev_limit = (int) mtmp->m_lev; /* never undo increment */
1750 /* sanity checks */
1751 if ((int) mtmp->m_lev > lev_limit) {
1752 mtmp->m_lev--; /* undo increment */
1753 /* HP might have been allowed to grow when it shouldn't */
1754 if (mtmp->mhpmax == hp_threshold + 1)
1755 mtmp->mhpmax--;
1757 if (mtmp->mhpmax > 50 * 8)
1758 mtmp->mhpmax = 50 * 8; /* absolute limit */
1759 if (mtmp->mhp > mtmp->mhpmax)
1760 mtmp->mhp = mtmp->mhpmax;
1762 return ptr;
1766 mongets(mtmp, otyp)
1767 register struct monst *mtmp;
1768 int otyp;
1770 register struct obj *otmp;
1771 int spe;
1773 if (!otyp)
1774 return 0;
1775 otmp = mksobj(otyp, TRUE, FALSE);
1776 if (otmp) {
1777 if (mtmp->data->mlet == S_DEMON) {
1778 /* demons never get blessed objects */
1779 if (otmp->blessed)
1780 curse(otmp);
1781 } else if (is_lminion(mtmp)) {
1782 /* lawful minions don't get cursed, bad, or rusting objects */
1783 otmp->cursed = FALSE;
1784 if (otmp->spe < 0)
1785 otmp->spe = 0;
1786 otmp->oerodeproof = TRUE;
1787 } else if (is_mplayer(mtmp->data) && is_sword(otmp)) {
1788 otmp->spe = (3 + rn2(4));
1791 if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
1792 otmp->spe = 0;
1793 otmp->age = 0L;
1794 otmp->lamplit = FALSE;
1795 otmp->blessed = otmp->cursed = FALSE;
1796 } else if (otmp->otyp == BELL_OF_OPENING) {
1797 otmp->blessed = otmp->cursed = FALSE;
1798 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
1799 otmp->blessed = FALSE;
1800 otmp->cursed = TRUE;
1803 /* leaders don't tolerate inferior quality battle gear */
1804 if (is_prince(mtmp->data)) {
1805 if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1)
1806 otmp->spe = 1;
1807 else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0)
1808 otmp->spe = 0;
1811 spe = otmp->spe;
1812 (void) mpickobj(mtmp, otmp); /* might free otmp */
1813 return spe;
1815 return 0;
1819 golemhp(type)
1820 int type;
1822 switch (type) {
1823 case PM_STRAW_GOLEM:
1824 return 20;
1825 case PM_PAPER_GOLEM:
1826 return 20;
1827 case PM_ROPE_GOLEM:
1828 return 30;
1829 case PM_LEATHER_GOLEM:
1830 return 40;
1831 case PM_GOLD_GOLEM:
1832 return 40;
1833 case PM_WOOD_GOLEM:
1834 return 50;
1835 case PM_FLESH_GOLEM:
1836 return 40;
1837 case PM_CLAY_GOLEM:
1838 return 50;
1839 case PM_STONE_GOLEM:
1840 return 60;
1841 case PM_GLASS_GOLEM:
1842 return 60;
1843 case PM_IRON_GOLEM:
1844 return 80;
1845 default:
1846 return 0;
1851 * Alignment vs. yours determines monster's attitude to you.
1852 * (Some "animal" types are co-aligned, but also hungry.)
1854 boolean
1855 peace_minded(ptr)
1856 register struct permonst *ptr;
1858 aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
1860 if (always_peaceful(ptr))
1861 return TRUE;
1862 if (always_hostile(ptr))
1863 return FALSE;
1864 if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
1865 return TRUE;
1866 if (ptr->msound == MS_NEMESIS)
1867 return FALSE;
1869 if (race_peaceful(ptr))
1870 return TRUE;
1871 if (race_hostile(ptr))
1872 return FALSE;
1874 /* the monster is hostile if its alignment is different from the
1875 * player's */
1876 if (sgn(mal) != sgn(ual))
1877 return FALSE;
1879 /* Negative monster hostile to player with Amulet. */
1880 if (mal < A_NEUTRAL && u.uhave.amulet)
1881 return FALSE;
1883 /* minions are hostile to players that have strayed at all */
1884 if (is_minion(ptr))
1885 return (boolean) (u.ualign.record >= 0);
1887 /* Last case: a chance of a co-aligned monster being
1888 * hostile. This chance is greater if the player has strayed
1889 * (u.ualign.record negative) or the monster is not strongly aligned.
1891 return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15
1892 : u.ualign.record))
1893 && !!rn2(2 + abs(mal)));
1896 /* Set malign to have the proper effect on player alignment if monster is
1897 * killed. Negative numbers mean it's bad to kill this monster; positive
1898 * numbers mean it's good. Since there are more hostile monsters than
1899 * peaceful monsters, the penalty for killing a peaceful monster should be
1900 * greater than the bonus for killing a hostile monster to maintain balance.
1901 * Rules:
1902 * it's bad to kill peaceful monsters, potentially worse to kill always-
1903 * peaceful monsters;
1904 * it's never bad to kill a hostile monster, although it may not be good.
1906 void
1907 set_malign(mtmp)
1908 struct monst *mtmp;
1910 schar mal = mtmp->data->maligntyp;
1911 boolean coaligned;
1913 if (mtmp->ispriest || mtmp->isminion) {
1914 /* some monsters have individual alignments; check them */
1915 if (mtmp->ispriest && EPRI(mtmp))
1916 mal = EPRI(mtmp)->shralign;
1917 else if (mtmp->isminion && EMIN(mtmp))
1918 mal = EMIN(mtmp)->min_align;
1919 /* unless alignment is none, set mal to -5,0,5 */
1920 /* (see align.h for valid aligntyp values) */
1921 if (mal != A_NONE)
1922 mal *= 5;
1925 coaligned = (sgn(mal) == sgn(u.ualign.type));
1926 if (mtmp->data->msound == MS_LEADER) {
1927 mtmp->malign = -20;
1928 } else if (mal == A_NONE) {
1929 if (mtmp->mpeaceful)
1930 mtmp->malign = 0;
1931 else
1932 mtmp->malign = 20; /* really hostile */
1933 } else if (always_peaceful(mtmp->data)) {
1934 int absmal = abs(mal);
1935 if (mtmp->mpeaceful)
1936 mtmp->malign = -3 * max(5, absmal);
1937 else
1938 mtmp->malign = 3 * max(5, absmal); /* renegade */
1939 } else if (always_hostile(mtmp->data)) {
1940 int absmal = abs(mal);
1941 if (coaligned)
1942 mtmp->malign = 0;
1943 else
1944 mtmp->malign = max(5, absmal);
1945 } else if (coaligned) {
1946 int absmal = abs(mal);
1947 if (mtmp->mpeaceful)
1948 mtmp->malign = -3 * max(3, absmal);
1949 else /* renegade */
1950 mtmp->malign = max(3, absmal);
1951 } else /* not coaligned and therefore hostile */
1952 mtmp->malign = abs(mal);
1955 /* allocate a new mcorpsenm field for a monster; only need mextra itself */
1956 void
1957 newmcorpsenm(mtmp)
1958 struct monst *mtmp;
1960 if (!mtmp->mextra)
1961 mtmp->mextra = newmextra();
1962 MCORPSENM(mtmp) = NON_PM; /* not initialized yet */
1965 /* release monster's mcorpsenm field; basically a no-op */
1966 void
1967 freemcorpsenm(mtmp)
1968 struct monst *mtmp;
1970 if (has_mcorpsenm(mtmp))
1971 MCORPSENM(mtmp) = NON_PM;
1974 static NEARDATA char syms[] = {
1975 MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
1976 FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
1977 AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
1978 S_MIMIC_DEF, S_MIMIC_DEF,
1981 void
1982 set_mimic_sym(mtmp)
1983 register struct monst *mtmp;
1985 int typ, roomno, rt;
1986 unsigned appear, ap_type;
1987 int s_sym;
1988 struct obj *otmp;
1989 int mx, my;
1991 if (!mtmp || Protection_from_shape_changers)
1992 return;
1993 mx = mtmp->mx;
1994 my = mtmp->my;
1995 typ = levl[mx][my].typ;
1996 /* only valid for INSIDE of room */
1997 roomno = levl[mx][my].roomno - ROOMOFFSET;
1998 if (roomno >= 0)
1999 rt = rooms[roomno].rtype;
2000 #ifdef SPECIALIZATION
2001 else if (IS_ROOM(typ))
2002 rt = OROOM, roomno = 0;
2003 #endif
2004 else
2005 rt = 0; /* roomno < 0 case for GCC_WARN */
2007 if (OBJ_AT(mx, my)) {
2008 ap_type = M_AP_OBJECT;
2009 appear = level.objects[mx][my]->otyp;
2010 } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) {
2011 ap_type = M_AP_FURNITURE;
2013 * If there is a wall to the left that connects to this
2014 * location, then the mimic mimics a horizontal closed door.
2015 * This does not allow doors to be in corners of rooms.
2016 * Since rogue has no closed doors, mimic a wall there
2017 * (yes, mimics can end up on this level by various means).
2019 if (mx != 0 && (levl[mx - 1][my].typ == HWALL
2020 || levl[mx - 1][my].typ == TLCORNER
2021 || levl[mx - 1][my].typ == TRWALL
2022 || levl[mx - 1][my].typ == BLCORNER
2023 || levl[mx - 1][my].typ == TDWALL
2024 || levl[mx - 1][my].typ == CROSSWALL
2025 || levl[mx - 1][my].typ == TUWALL))
2026 appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor;
2027 else
2028 appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor;
2029 if (!mtmp->minvis || See_invisible)
2030 block_point(mx, my); /* vision */
2031 } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) {
2032 ap_type = M_AP_OBJECT;
2033 appear = STATUE;
2034 } else if (roomno < 0 && !t_at(mx, my)) {
2035 ap_type = M_AP_OBJECT;
2036 appear = BOULDER;
2037 if (!mtmp->minvis || See_invisible)
2038 block_point(mx, my); /* vision */
2039 } else if (rt == ZOO || rt == VAULT) {
2040 ap_type = M_AP_OBJECT;
2041 appear = GOLD_PIECE;
2042 } else if (rt == DELPHI) {
2043 if (rn2(2)) {
2044 ap_type = M_AP_OBJECT;
2045 appear = STATUE;
2046 } else {
2047 ap_type = M_AP_FURNITURE;
2048 appear = S_fountain;
2050 } else if (rt == TEMPLE) {
2051 ap_type = M_AP_FURNITURE;
2052 appear = S_altar;
2054 * We won't bother with beehives, morgues, barracks, throne rooms
2055 * since they shouldn't contain too many mimics anyway...
2057 } else if (rt >= SHOPBASE) {
2058 s_sym = get_shop_item(rt - SHOPBASE);
2059 if (s_sym < 0) {
2060 ap_type = M_AP_OBJECT;
2061 appear = -s_sym;
2062 } else {
2063 if (s_sym == RANDOM_CLASS)
2064 s_sym = syms[rn2((int) sizeof(syms) - 2) + 2];
2065 goto assign_sym;
2067 } else {
2068 s_sym = syms[rn2((int) sizeof(syms))];
2069 assign_sym:
2070 if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) {
2071 ap_type = M_AP_FURNITURE;
2072 appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair;
2073 } else {
2074 ap_type = M_AP_OBJECT;
2075 if (s_sym == S_MIMIC_DEF) {
2076 appear = STRANGE_OBJECT;
2077 } else if (s_sym == COIN_CLASS) {
2078 appear = GOLD_PIECE;
2079 } else {
2080 otmp = mkobj((char) s_sym, FALSE);
2081 appear = otmp->otyp;
2082 /* make sure container contents are free'ed */
2083 obfree(otmp, (struct obj *) 0);
2087 mtmp->m_ap_type = ap_type;
2088 mtmp->mappearance = appear;
2089 if (ap_type == M_AP_OBJECT && (appear == STATUE || appear == CORPSE
2090 || appear == FIGURINE || appear == EGG)) {
2091 newmcorpsenm(mtmp);
2092 MCORPSENM(mtmp) = rndmonnum();
2093 if (appear == EGG && !can_be_hatched(MCORPSENM(mtmp)))
2094 MCORPSENM(mtmp) = NON_PM; /* revert to generic egg */
2098 /* release monster from bag of tricks; return number of monsters created */
2100 bagotricks(bag, tipping, seencount)
2101 struct obj *bag;
2102 boolean tipping; /* caller emptying entire contents; affects shop handling */
2103 int *seencount; /* secondary output */
2105 int moncount = 0;
2107 if (!bag || bag->otyp != BAG_OF_TRICKS) {
2108 impossible("bad bag o' tricks");
2109 } else if (bag->spe < 1) {
2110 /* if tipping known empty bag, give normal empty container message */
2111 pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens);
2112 /* now known to be empty if sufficiently discovered */
2113 if (bag->dknown && objects[bag->otyp].oc_name_known)
2114 bag->cknown = 1;
2115 } else {
2116 struct monst *mtmp;
2117 int creatcnt = 1, seecount = 0;
2119 consume_obj_charge(bag, !tipping);
2121 if (!rn2(23))
2122 creatcnt += rnd(7);
2123 do {
2124 mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
2125 if (mtmp) {
2126 ++moncount;
2127 if (canspotmon(mtmp))
2128 ++seecount;
2130 } while (--creatcnt > 0);
2131 if (seecount) {
2132 if (seencount)
2133 *seencount += seecount;
2134 if (bag->dknown)
2135 makeknown(BAG_OF_TRICKS);
2136 } else if (!tipping) {
2137 pline1(!moncount ? nothing_happens : "Nothing seems to happen.");
2140 return moncount;
2143 /*makemon.c*/