option parsing buffer overflow vulnerability
[aNetHack.git] / src / mon.c
blobb4a8f97f65c57d90392a3d75dde5744bd68750e7
1 /* NetHack 3.6 mon.c $NHDT-Date: 1461282107 2016/04/21 23:41:47 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.215 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* If you're using precompiled headers, you don't want this either */
6 #ifdef MICROPORT_BUG
7 #define MKROOM_H
8 #endif
10 #include "hack.h"
11 #include "mfndpos.h"
12 #include <ctype.h>
14 STATIC_VAR boolean vamp_rise_msg;
16 STATIC_DCL void FDECL(sanity_check_single_mon, (struct monst *, BOOLEAN_P,
17 const char *));
18 STATIC_DCL boolean FDECL(restrap, (struct monst *));
19 STATIC_DCL long FDECL(mm_aggression, (struct monst *, struct monst *));
20 STATIC_DCL long FDECL(mm_displacement, (struct monst *, struct monst *));
21 STATIC_DCL int NDECL(pick_animal);
22 STATIC_DCL void FDECL(kill_eggs, (struct obj *));
23 STATIC_DCL int FDECL(pickvampshape, (struct monst *));
24 STATIC_DCL boolean FDECL(isspecmon, (struct monst *));
25 STATIC_DCL boolean FDECL(validspecmon, (struct monst *, int));
26 STATIC_DCL boolean FDECL(validvamp, (struct monst *, int *, int));
27 STATIC_DCL struct permonst *FDECL(accept_newcham_form, (int));
28 STATIC_DCL struct obj *FDECL(make_corpse, (struct monst *, unsigned));
29 STATIC_DCL void FDECL(m_detach, (struct monst *, struct permonst *));
30 STATIC_DCL void FDECL(lifesaved_monster, (struct monst *));
32 #define LEVEL_SPECIFIC_NOCORPSE(mdat) \
33 (Is_rogue_level(&u.uz) \
34 || (level.flags.graveyard && is_undead(mdat) && rn2(3)))
36 #if 0
37 /* part of the original warning code which was replaced in 3.3.1 */
38 const char *warnings[] = {
39 "white", "pink", "red", "ruby", "purple", "black"
41 #endif /* 0 */
44 void
45 sanity_check_single_mon(mtmp, chk_geno, msg)
46 struct monst *mtmp;
47 boolean chk_geno;
48 const char *msg;
50 if (DEADMONSTER(mtmp))
51 return;
52 if (mtmp->data < &mons[LOW_PM] || mtmp->data >= &mons[NUMMONS]) {
53 impossible("illegal mon data %s; mnum=%d (%s)",
54 fmt_ptr((genericptr_t) mtmp->data), mtmp->mnum, msg);
55 } else {
56 int mndx = monsndx(mtmp->data);
58 if (mtmp->mnum != mndx) {
59 impossible("monster mnum=%d, monsndx=%d (%s)",
60 mtmp->mnum, mndx, msg);
61 mtmp->mnum = mndx;
63 if (chk_geno && (mvitals[mndx].mvflags & G_GENOD) != 0)
64 impossible("genocided %s in play (%s)", mons[mndx].mname, msg);
66 if (mtmp->isshk && !has_eshk(mtmp))
67 impossible("shk without eshk (%s)", msg);
68 if (mtmp->ispriest && !has_epri(mtmp))
69 impossible("priest without epri (%s)", msg);
70 if (mtmp->isgd && !has_egd(mtmp))
71 impossible("guard without egd (%s)", msg);
72 if (mtmp->isminion && !has_emin(mtmp))
73 impossible("minion without emin (%s)", msg);
74 /* guardian angel on astral level is tame but has emin rather than edog */
75 if (mtmp->mtame && !has_edog(mtmp) && !mtmp->isminion)
76 impossible("pet without edog (%s)", msg);
79 void
80 mon_sanity_check()
82 int x, y;
83 struct monst *mtmp, *m;
85 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
86 sanity_check_single_mon(mtmp, TRUE, "fmon");
87 if (DEADMONSTER(mtmp))
88 continue;
89 x = mtmp->mx, y = mtmp->my;
90 if (!isok(x, y) && !(mtmp->isgd && x == 0 && y == 0))
91 impossible("mon (%s) claims to be at <%d,%d>?",
92 fmt_ptr((genericptr_t) mtmp), x, y);
93 else if (level.monsters[x][y] != mtmp)
94 impossible("mon (%s) at <%d,%d> is not there!",
95 fmt_ptr((genericptr_t) mtmp), x, y);
98 for (x = 0; x < COLNO; x++)
99 for (y = 0; y < ROWNO; y++)
100 if ((mtmp = level.monsters[x][y]) != 0) {
101 for (m = fmon; m; m = m->nmon)
102 if (m == mtmp)
103 break;
104 if (!m)
105 impossible("map mon (%s) at <%d,%d> not in fmon list!",
106 fmt_ptr((genericptr_t) mtmp), x, y);
107 else if ((mtmp->mx != x || mtmp->my != y)
108 && mtmp->data != &mons[PM_LONG_WORM])
109 impossible("map mon (%s) at <%d,%d> is found at <%d,%d>?",
110 fmt_ptr((genericptr_t) mtmp),
111 mtmp->mx, mtmp->my, x, y);
114 for (mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon) {
115 sanity_check_single_mon(mtmp, FALSE, "migr");
120 /* convert the monster index of an undead to its living counterpart */
122 undead_to_corpse(mndx)
123 int mndx;
125 switch (mndx) {
126 case PM_KOBOLD_ZOMBIE:
127 case PM_KOBOLD_MUMMY:
128 mndx = PM_KOBOLD;
129 break;
130 case PM_DWARF_ZOMBIE:
131 case PM_DWARF_MUMMY:
132 mndx = PM_DWARF;
133 break;
134 case PM_GNOME_ZOMBIE:
135 case PM_GNOME_MUMMY:
136 mndx = PM_GNOME;
137 break;
138 case PM_ORC_ZOMBIE:
139 case PM_ORC_MUMMY:
140 mndx = PM_ORC;
141 break;
142 case PM_ELF_ZOMBIE:
143 case PM_ELF_MUMMY:
144 mndx = PM_ELF;
145 break;
146 case PM_VAMPIRE:
147 case PM_VAMPIRE_LORD:
148 #if 0 /* DEFERRED */
149 case PM_VAMPIRE_MAGE:
150 #endif
151 case PM_HUMAN_ZOMBIE:
152 case PM_HUMAN_MUMMY:
153 mndx = PM_HUMAN;
154 break;
155 case PM_GIANT_ZOMBIE:
156 case PM_GIANT_MUMMY:
157 mndx = PM_GIANT;
158 break;
159 case PM_ETTIN_ZOMBIE:
160 case PM_ETTIN_MUMMY:
161 mndx = PM_ETTIN;
162 break;
163 default:
164 break;
166 return mndx;
169 /* Convert the monster index of some monsters (such as quest guardians)
170 * to their generic species type.
172 * Return associated character class monster, rather than species
173 * if mode is 1.
176 genus(mndx, mode)
177 int mndx, mode;
179 switch (mndx) {
180 /* Quest guardians */
181 case PM_STUDENT:
182 mndx = mode ? PM_ARCHEOLOGIST : PM_HUMAN;
183 break;
184 case PM_CHIEFTAIN:
185 mndx = mode ? PM_BARBARIAN : PM_HUMAN;
186 break;
187 case PM_NEANDERTHAL:
188 mndx = mode ? PM_CAVEMAN : PM_HUMAN;
189 break;
190 case PM_ATTENDANT:
191 mndx = mode ? PM_HEALER : PM_HUMAN;
192 break;
193 case PM_PAGE:
194 mndx = mode ? PM_KNIGHT : PM_HUMAN;
195 break;
196 case PM_ABBOT:
197 mndx = mode ? PM_MONK : PM_HUMAN;
198 break;
199 case PM_ACOLYTE:
200 mndx = mode ? PM_PRIEST : PM_HUMAN;
201 break;
202 case PM_HUNTER:
203 mndx = mode ? PM_RANGER : PM_HUMAN;
204 break;
205 case PM_THUG:
206 mndx = mode ? PM_ROGUE : PM_HUMAN;
207 break;
208 case PM_ROSHI:
209 mndx = mode ? PM_SAMURAI : PM_HUMAN;
210 break;
211 case PM_GUIDE:
212 mndx = mode ? PM_TOURIST : PM_HUMAN;
213 break;
214 case PM_APPRENTICE:
215 mndx = mode ? PM_WIZARD : PM_HUMAN;
216 break;
217 case PM_WARRIOR:
218 mndx = mode ? PM_VALKYRIE : PM_HUMAN;
219 break;
220 default:
221 if (mndx >= LOW_PM && mndx < NUMMONS) {
222 struct permonst *ptr = &mons[mndx];
224 if (is_human(ptr))
225 mndx = PM_HUMAN;
226 else if (is_elf(ptr))
227 mndx = PM_ELF;
228 else if (is_dwarf(ptr))
229 mndx = PM_DWARF;
230 else if (is_gnome(ptr))
231 mndx = PM_GNOME;
232 else if (is_orc(ptr))
233 mndx = PM_ORC;
235 break;
237 return mndx;
240 /* return monster index if chameleon, or NON_PM if not */
242 pm_to_cham(mndx)
243 int mndx;
245 int mcham = NON_PM;
248 * As of 3.6.0 we just check M2_SHAPESHIFTER instead of having a
249 * big switch statement with hardcoded shapeshifter types here.
251 if (mndx >= LOW_PM && is_shapeshifter(&mons[mndx]))
252 mcham = mndx;
253 return mcham;
256 /* for deciding whether corpse will carry along full monster data */
257 #define KEEPTRAITS(mon) \
258 ((mon)->isshk || (mon)->mtame || unique_corpstat((mon)->data) \
259 || is_reviver((mon)->data) \
260 /* normally quest leader will be unique, */ \
261 /* but he or she might have been polymorphed */ \
262 || (mon)->m_id == quest_status.leader_m_id \
263 /* special cancellation handling for these */ \
264 || (dmgtype((mon)->data, AD_SEDU) || dmgtype((mon)->data, AD_SSEX)))
266 /* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
267 * leave corpses. Monsters which leave "special" corpses should have
268 * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
269 * etc....
271 STATIC_OVL struct obj *
272 make_corpse(mtmp, corpseflags)
273 register struct monst *mtmp;
274 unsigned corpseflags;
276 register struct permonst *mdat = mtmp->data;
277 int num;
278 struct obj *obj = (struct obj *) 0;
279 struct obj *otmp = (struct obj *) 0;
280 int x = mtmp->mx, y = mtmp->my;
281 int mndx = monsndx(mdat);
282 unsigned corpstatflags = corpseflags;
283 boolean burythem = ((corpstatflags & CORPSTAT_BURIED) != 0);
285 switch (mndx) {
286 case PM_GRAY_DRAGON:
287 case PM_SILVER_DRAGON:
288 #if 0 /* DEFERRED */
289 case PM_SHIMMERING_DRAGON:
290 #endif
291 case PM_RED_DRAGON:
292 case PM_ORANGE_DRAGON:
293 case PM_WHITE_DRAGON:
294 case PM_BLACK_DRAGON:
295 case PM_BLUE_DRAGON:
296 case PM_GREEN_DRAGON:
297 case PM_YELLOW_DRAGON:
298 /* Make dragon scales. This assumes that the order of the
299 dragons is the same as the order of the scales. */
300 if (!rn2(mtmp->mrevived ? 20 : 3)) {
301 num = GRAY_DRAGON_SCALES + monsndx(mdat) - PM_GRAY_DRAGON;
302 obj = mksobj_at(num, x, y, FALSE, FALSE);
303 obj->spe = 0;
304 obj->cursed = obj->blessed = FALSE;
306 goto default_1;
307 case PM_WHITE_UNICORN:
308 case PM_GRAY_UNICORN:
309 case PM_BLACK_UNICORN:
310 if (mtmp->mrevived && rn2(2)) {
311 if (canseemon(mtmp))
312 pline("%s recently regrown horn crumbles to dust.",
313 s_suffix(Monnam(mtmp)));
314 } else {
315 obj = mksobj_at(UNICORN_HORN, x, y, TRUE, FALSE);
316 if (obj && mtmp->mrevived)
317 obj->degraded_horn = 1;
319 goto default_1;
320 case PM_LONG_WORM:
321 (void) mksobj_at(WORM_TOOTH, x, y, TRUE, FALSE);
322 goto default_1;
323 case PM_VAMPIRE:
324 case PM_VAMPIRE_LORD:
325 /* include mtmp in the mkcorpstat() call */
326 num = undead_to_corpse(mndx);
327 corpstatflags |= CORPSTAT_INIT;
328 obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
329 obj->age -= 100; /* this is an *OLD* corpse */
330 break;
331 case PM_KOBOLD_MUMMY:
332 case PM_DWARF_MUMMY:
333 case PM_GNOME_MUMMY:
334 case PM_ORC_MUMMY:
335 case PM_ELF_MUMMY:
336 case PM_HUMAN_MUMMY:
337 case PM_GIANT_MUMMY:
338 case PM_ETTIN_MUMMY:
339 case PM_KOBOLD_ZOMBIE:
340 case PM_DWARF_ZOMBIE:
341 case PM_GNOME_ZOMBIE:
342 case PM_ORC_ZOMBIE:
343 case PM_ELF_ZOMBIE:
344 case PM_HUMAN_ZOMBIE:
345 case PM_GIANT_ZOMBIE:
346 case PM_ETTIN_ZOMBIE:
347 num = undead_to_corpse(mndx);
348 corpstatflags |= CORPSTAT_INIT;
349 obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
350 obj->age -= 100; /* this is an *OLD* corpse */
351 break;
352 case PM_IRON_GOLEM:
353 num = d(2, 6);
354 while (num--)
355 obj = mksobj_at(IRON_CHAIN, x, y, TRUE, FALSE);
356 free_mname(mtmp); /* don't christen obj */
357 break;
358 case PM_GLASS_GOLEM:
359 num = d(2, 4); /* very low chance of creating all glass gems */
360 while (num--)
361 obj = mksobj_at((LAST_GEM + rnd(9)), x, y, TRUE, FALSE);
362 free_mname(mtmp);
363 break;
364 case PM_CLAY_GOLEM:
365 obj = mksobj_at(ROCK, x, y, FALSE, FALSE);
366 obj->quan = (long) (rn2(20) + 50);
367 obj->owt = weight(obj);
368 free_mname(mtmp);
369 break;
370 case PM_STONE_GOLEM:
371 corpstatflags &= ~CORPSTAT_INIT;
372 obj =
373 mkcorpstat(STATUE, (struct monst *) 0, mdat, x, y, corpstatflags);
374 break;
375 case PM_WOOD_GOLEM:
376 num = d(2, 4);
377 while (num--) {
378 obj = mksobj_at(QUARTERSTAFF, x, y, TRUE, FALSE);
380 free_mname(mtmp);
381 break;
382 case PM_LEATHER_GOLEM:
383 num = d(2, 4);
384 while (num--)
385 obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE, FALSE);
386 free_mname(mtmp);
387 break;
388 case PM_GOLD_GOLEM:
389 /* Good luck gives more coins */
390 obj = mkgold((long) (200 - rnl(101)), x, y);
391 free_mname(mtmp);
392 break;
393 case PM_PAPER_GOLEM:
394 num = rnd(4);
395 while (num--)
396 obj = mksobj_at(SCR_BLANK_PAPER, x, y, TRUE, FALSE);
397 free_mname(mtmp);
398 break;
399 /* expired puddings will congeal into a large blob;
400 like dragons, relies on the order remaining consistent */
401 case PM_GRAY_OOZE:
402 case PM_BROWN_PUDDING:
403 case PM_GREEN_SLIME:
404 case PM_BLACK_PUDDING:
405 /* we have to do this here because most other places
406 expect there to be an object coming back; not this one */
407 obj = mksobj_at(GLOB_OF_BLACK_PUDDING - (PM_BLACK_PUDDING - mndx),
408 x, y, TRUE, FALSE);
410 while (obj && (otmp = obj_nexto(obj)) != (struct obj *) 0) {
411 pudding_merge_message(obj, otmp);
412 obj = obj_meld(&obj, &otmp);
414 free_mname(mtmp);
415 return obj;
416 default_1:
417 default:
418 if (mvitals[mndx].mvflags & G_NOCORPSE) {
419 return (struct obj *) 0;
420 } else {
421 corpstatflags |= CORPSTAT_INIT;
422 /* preserve the unique traits of some creatures */
423 obj = mkcorpstat(CORPSE, KEEPTRAITS(mtmp) ? mtmp : 0,
424 mdat, x, y, corpstatflags);
425 if (burythem) {
426 boolean dealloc;
428 (void) bury_an_obj(obj, &dealloc);
429 newsym(x, y);
430 return dealloc ? (struct obj *) 0 : obj;
433 break;
435 /* All special cases should precede the G_NOCORPSE check */
437 if (!obj) return NULL;
439 /* if polymorph or undead turning has killed this monster,
440 prevent the same attack beam from hitting its corpse */
441 if (context.bypasses)
442 bypass_obj(obj);
444 if (has_mname(mtmp))
445 obj = oname(obj, MNAME(mtmp));
447 /* Avoid "It was hidden under a green mold corpse!"
448 * during Blind combat. An unseen monster referred to as "it"
449 * could be killed and leave a corpse. If a hider then hid
450 * underneath it, you could be told the corpse type of a
451 * monster that you never knew was there without this.
452 * The code in hitmu() substitutes the word "something"
453 * if the corpses obj->dknown is 0.
455 if (Blind && !sensemon(mtmp))
456 obj->dknown = 0;
458 stackobj(obj);
459 newsym(x, y);
460 return obj;
463 /* check mtmp and water/lava for compatibility, 0 (survived), 1 (died) */
465 minliquid(mtmp)
466 register struct monst *mtmp;
468 boolean inpool, inlava, infountain;
470 /* [what about ceiling clingers?] */
471 inpool = (is_pool(mtmp->mx, mtmp->my)
472 && !(is_flyer(mtmp->data) || is_floater(mtmp->data)));
473 inlava = (is_lava(mtmp->mx, mtmp->my)
474 && !(is_flyer(mtmp->data) || is_floater(mtmp->data)));
475 infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
477 /* Flying and levitation keeps our steed out of the liquid */
478 /* (but not water-walking or swimming) */
479 if (mtmp == u.usteed && (Flying || Levitation))
480 return 0;
482 /* Gremlin multiplying won't go on forever since the hit points
483 * keep going down, and when it gets to 1 hit point the clone
484 * function will fail.
486 if (mtmp->data == &mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
487 if (split_mon(mtmp, (struct monst *) 0))
488 dryup(mtmp->mx, mtmp->my, FALSE);
489 if (inpool)
490 water_damage_chain(mtmp->minvent, FALSE);
491 return 0;
492 } else if (mtmp->data == &mons[PM_IRON_GOLEM] && inpool && !rn2(5)) {
493 int dam = d(2, 6);
495 if (cansee(mtmp->mx, mtmp->my))
496 pline("%s rusts.", Monnam(mtmp));
497 mtmp->mhp -= dam;
498 if (mtmp->mhpmax > dam)
499 mtmp->mhpmax -= dam;
500 if (mtmp->mhp < 1) {
501 mondead(mtmp);
502 if (mtmp->mhp < 1)
503 return 1;
505 water_damage_chain(mtmp->minvent, FALSE);
506 return 0;
509 if (inlava) {
511 * Lava effects much as water effects. Lava likers are able to
512 * protect their stuff. Fire resistant monsters can only protect
513 * themselves --ALI
515 if (!is_clinger(mtmp->data) && !likes_lava(mtmp->data)) {
516 if (!resists_fire(mtmp)) {
517 if (cansee(mtmp->mx, mtmp->my))
518 pline("%s %s.", Monnam(mtmp),
519 mtmp->data == &mons[PM_WATER_ELEMENTAL]
520 ? "boils away"
521 : "burns to a crisp");
522 mondead(mtmp);
523 } else {
524 if (--mtmp->mhp < 1) {
525 if (cansee(mtmp->mx, mtmp->my))
526 pline("%s surrenders to the fire.", Monnam(mtmp));
527 mondead(mtmp);
528 } else if (cansee(mtmp->mx, mtmp->my))
529 pline("%s burns slightly.", Monnam(mtmp));
531 if (mtmp->mhp > 0) {
532 (void) fire_damage_chain(mtmp->minvent, FALSE, FALSE,
533 mtmp->mx, mtmp->my);
534 (void) rloc(mtmp, FALSE);
535 return 0;
537 return 1;
539 } else if (inpool) {
540 /* Most monsters drown in pools. flooreffects() will take care of
541 * water damage to dead monsters' inventory, but survivors need to
542 * be handled here. Swimmers are able to protect their stuff...
544 if (!is_clinger(mtmp->data) && !is_swimmer(mtmp->data)
545 && !amphibious(mtmp->data)) {
546 if (cansee(mtmp->mx, mtmp->my)) {
547 pline("%s drowns.", Monnam(mtmp));
549 if (u.ustuck && u.uswallow && u.ustuck == mtmp) {
550 /* This can happen after a purple worm plucks you off a
551 flying steed while you are over water. */
552 pline("%s sinks as water rushes in and flushes you out.",
553 Monnam(mtmp));
555 mondead(mtmp);
556 if (mtmp->mhp > 0) {
557 water_damage_chain(mtmp->minvent, FALSE);
558 (void) rloc(mtmp, FALSE);
559 return 0;
561 return 1;
563 } else {
564 /* but eels have a difficult time outside */
565 if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)) {
566 /* as mhp gets lower, the rate of further loss slows down */
567 if (mtmp->mhp > 1 && rn2(mtmp->mhp) > rn2(8))
568 mtmp->mhp--;
569 monflee(mtmp, 2, FALSE, FALSE);
572 return 0;
576 mcalcmove(mon)
577 struct monst *mon;
579 int mmove = mon->data->mmove;
581 /* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
582 * MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
583 * both adjustments have negligible effect on higher speeds.
585 if (mon->mspeed == MSLOW)
586 mmove = (2 * mmove + 1) / 3;
587 else if (mon->mspeed == MFAST)
588 mmove = (4 * mmove + 2) / 3;
590 if (mon == u.usteed) {
591 if (u.ugallop && context.mv) {
592 /* average movement is 1.50 times normal */
593 mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
595 } else if (mmove) {
596 /* vary movement points allocated to slightly reduce predictability;
597 random increment (avg +2) exceeds random decrement (avg +1) by
598 a small amount; normal speed monsters will occasionally get an
599 extra move and slow ones won't be quite as slow */
600 mmove += rn2(5) - rn2(3); /* + 0..4 - 0..2, average net +1 */
601 if (mmove < 1)
602 mmove = 1;
605 return mmove;
608 /* actions that happen once per ``turn'', regardless of each
609 individual monster's metabolism; some of these might need to
610 be reclassified to occur more in proportion with movement rate */
611 void
612 mcalcdistress()
614 struct monst *mtmp;
616 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
617 if (DEADMONSTER(mtmp))
618 continue;
620 /* must check non-moving monsters once/turn in case
621 * they managed to end up in liquid */
622 if (mtmp->data->mmove == 0) {
623 if (vision_full_recalc)
624 vision_recalc(0);
625 if (minliquid(mtmp))
626 continue;
629 /* regenerate hit points */
630 mon_regen(mtmp, FALSE);
632 /* possibly polymorph shapechangers and lycanthropes */
633 if (mtmp->cham >= LOW_PM)
634 decide_to_shapeshift(mtmp, (canspotmon(mtmp)
635 || (u.uswallow && mtmp == u.ustuck))
636 ? SHIFT_MSG : 0);
637 were_change(mtmp);
639 /* gradually time out temporary problems */
640 if (mtmp->mblinded && !--mtmp->mblinded)
641 mtmp->mcansee = 1;
642 if (mtmp->mfrozen && !--mtmp->mfrozen)
643 mtmp->mcanmove = 1;
644 if (mtmp->mfleetim && !--mtmp->mfleetim)
645 mtmp->mflee = 0;
647 /* FIXME: mtmp->mlstmv ought to be updated here */
652 movemon()
654 register struct monst *mtmp, *nmtmp;
655 register boolean somebody_can_move = FALSE;
658 * Some of you may remember the former assertion here that
659 * because of deaths and other actions, a simple one-pass
660 * algorithm wasn't possible for movemon. Deaths are no longer
661 * removed to the separate list fdmon; they are simply left in
662 * the chain with hit points <= 0, to be cleaned up at the end
663 * of the pass.
665 * The only other actions which cause monsters to be removed from
666 * the chain are level migrations and losedogs(). I believe losedogs()
667 * is a cleanup routine not associated with monster movements, and
668 * monsters can only affect level migrations on themselves, not others
669 * (hence the fetching of nmon before moving the monster). Currently,
670 * monsters can jump into traps, read cursed scrolls of teleportation,
671 * and drink cursed potions of raise level to change levels. These are
672 * all reflexive at this point. Should one monster be able to level
673 * teleport another, this scheme would have problems.
676 for (mtmp = fmon; mtmp; mtmp = nmtmp) {
677 /* end monster movement early if hero is flagged to leave the level */
678 if (u.utotype
679 #ifdef SAFERHANGUP
680 /* or if the program has lost contact with the user */
681 || program_state.done_hup
682 #endif
684 somebody_can_move = FALSE;
685 break;
687 nmtmp = mtmp->nmon;
688 /* one dead monster needs to perform a move after death:
689 vault guard whose temporary corridor is still on the map */
690 if (mtmp->isgd && !mtmp->mx && mtmp->mhp <= 0)
691 (void) gd_move(mtmp);
692 if (DEADMONSTER(mtmp))
693 continue;
695 /* Find a monster that we have not treated yet. */
696 if (mtmp->movement < NORMAL_SPEED)
697 continue;
699 mtmp->movement -= NORMAL_SPEED;
700 if (mtmp->movement >= NORMAL_SPEED)
701 somebody_can_move = TRUE;
703 if (vision_full_recalc)
704 vision_recalc(0); /* vision! */
706 /* reset obj bypasses before next monster moves */
707 if (context.bypasses)
708 clear_bypasses();
709 clear_splitobjs();
710 if (minliquid(mtmp))
711 continue;
713 if (is_hider(mtmp->data)) {
714 /* unwatched mimics and piercers may hide again [MRS] */
715 if (restrap(mtmp))
716 continue;
717 if (mtmp->m_ap_type == M_AP_FURNITURE
718 || mtmp->m_ap_type == M_AP_OBJECT)
719 continue;
720 if (mtmp->mundetected)
721 continue;
722 } else if (mtmp->data->mlet == S_EEL && !mtmp->mundetected
723 && (mtmp->mflee || distu(mtmp->mx, mtmp->my) > 2)
724 && !canseemon(mtmp) && !rn2(4)) {
725 /* some eels end up stuck in isolated pools, where they
726 can't--or at least won't--move, so they never reach
727 their post-move chance to re-hide */
728 if (hideunder(mtmp))
729 continue;
732 /* continue if the monster died fighting */
733 if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
734 /* Note:
735 * Conflict does not take effect in the first round.
736 * Therefore, A monster when stepping into the area will
737 * get to swing at you.
739 * The call to fightm() must be _last_. The monster might
740 * have died if it returns 1.
742 if (couldsee(mtmp->mx, mtmp->my)
743 && (distu(mtmp->mx, mtmp->my) <= BOLT_LIM * BOLT_LIM)
744 && fightm(mtmp))
745 continue; /* mon might have died */
747 if (dochugw(mtmp)) /* otherwise just move the monster */
748 continue;
751 if (any_light_source())
752 vision_full_recalc = 1; /* in case a mon moved with a light source */
753 /* reset obj bypasses after last monster has moved */
754 if (context.bypasses)
755 clear_bypasses();
756 clear_splitobjs();
757 /* remove dead monsters; dead vault guard will be left at <0,0>
758 if temporary corridor out of vault hasn't been removed yet */
759 dmonsfree();
761 /* a monster may have levteleported player -dlc */
762 if (u.utotype) {
763 deferred_goto();
764 /* changed levels, so these monsters are dormant */
765 somebody_can_move = FALSE;
768 return somebody_can_move;
771 #define mstoning(obj) \
772 (ofood(obj) && (touch_petrifies(&mons[(obj)->corpsenm]) \
773 || (obj)->corpsenm == PM_MEDUSA))
776 * Maybe eat a metallic object (not just gold).
777 * Return value: 0 => nothing happened, 1 => monster ate something,
778 * 2 => monster died (it must have grown into a genocided form, but
779 * that can't happen at present because nothing which eats objects
780 * has young and old forms).
783 meatmetal(mtmp)
784 register struct monst *mtmp;
786 register struct obj *otmp;
787 struct permonst *ptr;
788 int poly, grow, heal, mstone;
790 /* If a pet, eating is handled separately, in dog.c */
791 if (mtmp->mtame)
792 return 0;
794 /* Eats topmost metal object if it is there */
795 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp;
796 otmp = otmp->nexthere) {
797 /* Don't eat indigestible/choking/inappropriate objects */
798 if ((mtmp->data == &mons[PM_RUST_MONSTER] && !is_rustprone(otmp))
799 || (otmp->otyp == AMULET_OF_STRANGULATION)
800 || (otmp->otyp == RIN_SLOW_DIGESTION))
801 continue;
802 if (is_metallic(otmp) && !obj_resists(otmp, 5, 95)
803 && touch_artifact(otmp, mtmp)) {
804 if (mtmp->data == &mons[PM_RUST_MONSTER] && otmp->oerodeproof) {
805 if (canseemon(mtmp) && flags.verbose) {
806 pline("%s eats %s!", Monnam(mtmp),
807 distant_name(otmp, doname));
809 /* The object's rustproofing is gone now */
810 otmp->oerodeproof = 0;
811 mtmp->mstun = 1;
812 if (canseemon(mtmp) && flags.verbose) {
813 pline("%s spits %s out in disgust!", Monnam(mtmp),
814 distant_name(otmp, doname));
816 } else {
817 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
818 pline("%s eats %s!", Monnam(mtmp),
819 distant_name(otmp, doname));
820 else if (flags.verbose)
821 You_hear("a crunching sound.");
822 mtmp->meating = otmp->owt / 2 + 1;
823 /* Heal up to the object's weight in hp */
824 if (mtmp->mhp < mtmp->mhpmax) {
825 mtmp->mhp += objects[otmp->otyp].oc_weight;
826 if (mtmp->mhp > mtmp->mhpmax)
827 mtmp->mhp = mtmp->mhpmax;
829 if (otmp == uball) {
830 unpunish();
831 delobj(otmp);
832 } else if (otmp == uchain) {
833 unpunish(); /* frees uchain */
834 } else {
835 poly = polyfodder(otmp);
836 grow = mlevelgain(otmp);
837 heal = mhealup(otmp);
838 mstone = mstoning(otmp);
839 delobj(otmp);
840 ptr = mtmp->data;
841 if (poly) {
842 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
843 ptr = mtmp->data;
844 } else if (grow) {
845 ptr = grow_up(mtmp, (struct monst *) 0);
846 } else if (mstone) {
847 if (poly_when_stoned(ptr)) {
848 mon_to_stone(mtmp);
849 ptr = mtmp->data;
850 } else if (!resists_ston(mtmp)) {
851 if (canseemon(mtmp))
852 pline("%s turns to stone!", Monnam(mtmp));
853 monstone(mtmp);
854 ptr = (struct permonst *) 0;
856 } else if (heal) {
857 mtmp->mhp = mtmp->mhpmax;
859 if (!ptr)
860 return 2; /* it died */
862 /* Left behind a pile? */
863 if (rnd(25) < 3)
864 (void) mksobj_at(ROCK, mtmp->mx, mtmp->my, TRUE, FALSE);
865 newsym(mtmp->mx, mtmp->my);
866 return 1;
870 return 0;
873 /* monster eats a pile of objects */
875 meatobj(mtmp) /* for gelatinous cubes */
876 struct monst *mtmp;
878 register struct obj *otmp, *otmp2;
879 struct permonst *ptr, *original_ptr = mtmp->data;
880 int poly, grow, heal, count = 0, ecount = 0;
881 char buf[BUFSZ];
883 buf[0] = '\0';
884 /* If a pet, eating is handled separately, in dog.c */
885 if (mtmp->mtame)
886 return 0;
888 /* eat organic objects, including cloth and wood, if present;
889 engulf others, except huge rocks and metal attached to player
890 [despite comment at top, doesn't assume that eater is a g.cube] */
891 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
892 otmp2 = otmp->nexthere;
894 /* touch sensitive items */
895 if (otmp->otyp == CORPSE && is_rider(&mons[otmp->corpsenm])) {
896 /* Rider corpse isn't just inedible; can't engulf it either */
897 (void) revive_corpse(otmp);
899 /* untouchable (or inaccessible) items */
900 } else if ((otmp->otyp == CORPSE
901 && touch_petrifies(&mons[otmp->corpsenm])
902 && !resists_ston(mtmp))
903 /* don't engulf boulders and statues or ball&chain */
904 || otmp->oclass == ROCK_CLASS
905 || otmp == uball || otmp == uchain) {
906 /* do nothing--neither eaten nor engulfed */
907 continue;
909 /* inedible items -- engulf these */
910 } else if (!is_organic(otmp) || obj_resists(otmp, 5, 95)
911 || !touch_artifact(otmp, mtmp)
912 /* redundant due to non-organic composition but
913 included for emphasis */
914 || (otmp->otyp == AMULET_OF_STRANGULATION
915 || otmp->otyp == RIN_SLOW_DIGESTION)
916 /* cockatrice corpses handled above; this
917 touch_petrifies() check catches eggs */
918 || ((otmp->otyp == CORPSE || otmp->otyp == EGG)
919 && ((touch_petrifies(&mons[otmp->corpsenm])
920 && !resists_ston(mtmp))
921 || (otmp->corpsenm == PM_GREEN_SLIME
922 && !slimeproof(mtmp->data))))) {
923 /* engulf */
924 ++ecount;
925 if (ecount == 1)
926 Sprintf(buf, "%s engulfs %s.", Monnam(mtmp),
927 distant_name(otmp, doname));
928 else if (ecount == 2)
929 Sprintf(buf, "%s engulfs several objects.", Monnam(mtmp));
930 obj_extract_self(otmp);
931 (void) mpickobj(mtmp, otmp); /* slurp */
933 /* lastly, edible items; yum! */
934 } else {
935 /* devour */
936 ++count;
937 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
938 pline("%s eats %s!", Monnam(mtmp),
939 distant_name(otmp, doname));
940 else if (flags.verbose)
941 You_hear("a slurping sound.");
942 /* Heal up to the object's weight in hp */
943 if (mtmp->mhp < mtmp->mhpmax) {
944 mtmp->mhp += objects[otmp->otyp].oc_weight;
945 if (mtmp->mhp > mtmp->mhpmax)
946 mtmp->mhp = mtmp->mhpmax;
948 if (Has_contents(otmp)) {
949 register struct obj *otmp3;
951 /* contents of eaten containers become engulfed; this
952 is arbitrary, but otherwise g.cubes are too powerful */
953 while ((otmp3 = otmp->cobj) != 0) {
954 obj_extract_self(otmp3);
955 if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) {
956 otmp3->age = monstermoves - otmp3->age;
957 start_corpse_timeout(otmp3);
959 (void) mpickobj(mtmp, otmp3);
962 poly = polyfodder(otmp);
963 grow = mlevelgain(otmp);
964 heal = mhealup(otmp);
965 delobj(otmp); /* munch */
966 ptr = mtmp->data;
967 if (poly) {
968 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
969 ptr = mtmp->data;
970 } else if (grow) {
971 ptr = grow_up(mtmp, (struct monst *) 0);
972 } else if (heal) {
973 mtmp->mhp = mtmp->mhpmax;
975 /* in case it polymorphed or died */
976 if (ptr != original_ptr)
977 return !ptr ? 2 : 1;
980 /* Engulf & devour is instant, so don't set meating */
981 if (mtmp->minvis)
982 newsym(mtmp->mx, mtmp->my);
985 if (ecount > 0) {
986 if (cansee(mtmp->mx, mtmp->my) && flags.verbose && buf[0])
987 pline1(buf);
988 else if (flags.verbose)
989 You_hear("%s slurping sound%s.",
990 (ecount == 1) ? "a" : "several", plur(ecount));
992 return (count > 0 || ecount > 0) ? 1 : 0;
995 void
996 mpickgold(mtmp)
997 register struct monst *mtmp;
999 register struct obj *gold;
1000 int mat_idx;
1002 if ((gold = g_at(mtmp->mx, mtmp->my)) != 0) {
1003 mat_idx = objects[gold->otyp].oc_material;
1004 obj_extract_self(gold);
1005 add_to_minv(mtmp, gold);
1006 if (cansee(mtmp->mx, mtmp->my)) {
1007 if (flags.verbose && !mtmp->isgd)
1008 pline("%s picks up some %s.", Monnam(mtmp),
1009 mat_idx == GOLD ? "gold" : "money");
1010 newsym(mtmp->mx, mtmp->my);
1015 boolean
1016 mpickstuff(mtmp, str)
1017 register struct monst *mtmp;
1018 register const char *str;
1020 register struct obj *otmp, *otmp2, *otmp3;
1021 int carryamt = 0;
1023 /* prevent shopkeepers from leaving the door of their shop */
1024 if (mtmp->isshk && inhishop(mtmp))
1025 return FALSE;
1027 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
1028 otmp2 = otmp->nexthere;
1029 /* Nymphs take everything. Most monsters don't pick up corpses. */
1030 if (!str ? searches_for_item(mtmp, otmp)
1031 : !!(index(str, otmp->oclass))) {
1032 if (otmp->otyp == CORPSE && mtmp->data->mlet != S_NYMPH
1033 /* let a handful of corpse types thru to can_carry() */
1034 && !touch_petrifies(&mons[otmp->corpsenm])
1035 && otmp->corpsenm != PM_LIZARD
1036 && !acidic(&mons[otmp->corpsenm]))
1037 continue;
1038 if (!touch_artifact(otmp, mtmp))
1039 continue;
1040 carryamt = can_carry(mtmp, otmp);
1041 if (carryamt == 0)
1042 continue;
1043 if (is_pool(mtmp->mx, mtmp->my))
1044 continue;
1045 /* handle cases where the critter can only get some */
1046 otmp3 = otmp;
1047 if (carryamt != otmp->quan) {
1048 otmp3 = splitobj(otmp, carryamt);
1050 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
1051 pline("%s picks up %s.", Monnam(mtmp),
1052 (distu(mtmp->mx, mtmp->my) <= 5)
1053 ? doname(otmp3)
1054 : distant_name(otmp3, doname));
1055 obj_extract_self(otmp3); /* remove from floor */
1056 (void) mpickobj(mtmp, otmp3); /* may merge and free otmp3 */
1057 m_dowear(mtmp, FALSE);
1058 newsym(mtmp->mx, mtmp->my);
1059 return TRUE; /* pick only one object */
1062 return FALSE;
1066 curr_mon_load(mtmp)
1067 struct monst *mtmp;
1069 int curload = 0;
1070 struct obj *obj;
1072 for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1073 if (obj->otyp != BOULDER || !throws_rocks(mtmp->data))
1074 curload += obj->owt;
1077 return curload;
1081 max_mon_load(mtmp)
1082 struct monst *mtmp;
1084 long maxload;
1086 /* Base monster carrying capacity is equal to human maximum
1087 * carrying capacity, or half human maximum if not strong.
1088 * (for a polymorphed player, the value used would be the
1089 * non-polymorphed carrying capacity instead of max/half max).
1090 * This is then modified by the ratio between the monster weights
1091 * and human weights. Corpseless monsters are given a capacity
1092 * proportional to their size instead of weight.
1094 if (!mtmp->data->cwt)
1095 maxload = (MAX_CARR_CAP * (long) mtmp->data->msize) / MZ_HUMAN;
1096 else if (!strongmonst(mtmp->data)
1097 || (strongmonst(mtmp->data) && (mtmp->data->cwt > WT_HUMAN)))
1098 maxload = (MAX_CARR_CAP * (long) mtmp->data->cwt) / WT_HUMAN;
1099 else
1100 maxload = MAX_CARR_CAP; /*strong monsters w/cwt <= WT_HUMAN*/
1102 if (!strongmonst(mtmp->data))
1103 maxload /= 2;
1105 if (maxload < 1)
1106 maxload = 1;
1108 return (int) maxload;
1111 /* for restricting monsters' object-pickup.
1113 * to support the new pet behavior, this now returns the max # of objects
1114 * that a given monster could pick up from a pile. frequently this will be
1115 * otmp->quan, but special cases for 'only one' now exist so.
1117 * this will probably cause very amusing behavior with pets and gold coins.
1119 * TODO: allow picking up 2-N objects from a pile of N based on weight.
1120 * Change from 'int' to 'long' to accomate big stacks of gold.
1121 * Right now we fake it by reporting a partial quantity, but the
1122 * likesgold handling m_move results in picking up the whole stack.
1125 can_carry(mtmp, otmp)
1126 struct monst *mtmp;
1127 struct obj *otmp;
1129 int iquan, otyp = otmp->otyp, newload = otmp->owt;
1130 struct permonst *mdat = mtmp->data;
1131 short nattk = 0;
1133 if (notake(mdat))
1134 return 0; /* can't carry anything */
1136 if (otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm])
1137 && !(mtmp->misc_worn_check & W_ARMG) && !resists_ston(mtmp))
1138 return 0;
1139 if (otyp == CORPSE && is_rider(&mons[otmp->corpsenm]))
1140 return 0;
1141 if (objects[otyp].oc_material == SILVER && mon_hates_silver(mtmp)
1142 && (otyp != BELL_OF_OPENING || !is_covetous(mdat)))
1143 return 0;
1145 /* hostile monsters who like gold will pick up the whole stack;
1146 tame mosnters with hands will pick up the partial stack */
1147 iquan = (otmp->quan > (long) LARGEST_INT)
1148 ? 20000 + rn2(LARGEST_INT - 20000 + 1)
1149 : (int) otmp->quan;
1151 /* monsters without hands can't pick up multiple objects at once
1152 * unless they have an engulfing attack
1154 * ...dragons, of course, can always carry gold pieces and gems somehow
1156 if (iquan > 1) {
1157 boolean glomper = FALSE;
1159 if (mtmp->data->mlet == S_DRAGON
1160 && (otmp->oclass == COIN_CLASS
1161 || otmp->oclass == GEM_CLASS))
1162 glomper = TRUE;
1163 else
1164 for (nattk = 0; nattk < NATTK; nattk++)
1165 if (mtmp->data->mattk[nattk].aatyp == AT_ENGL) {
1166 glomper = TRUE;
1167 break;
1169 if ((mtmp->data->mflags1 & M1_NOHANDS) && !glomper)
1170 return 1;
1173 /* steeds don't pick up stuff (to avoid shop abuse) */
1174 if (mtmp == u.usteed)
1175 return 0;
1176 if (mtmp->isshk)
1177 return iquan; /* no limit */
1178 if (mtmp->mpeaceful && !mtmp->mtame)
1179 return 0;
1180 /* otherwise players might find themselves obligated to violate
1181 * their alignment if the monster takes something they need
1184 /* special--boulder throwers carry unlimited amounts of boulders */
1185 if (throws_rocks(mdat) && otyp == BOULDER)
1186 return iquan;
1188 /* nymphs deal in stolen merchandise, but not boulders or statues */
1189 if (mdat->mlet == S_NYMPH)
1190 return (otmp->oclass == ROCK_CLASS) ? 0 : iquan;
1192 if (curr_mon_load(mtmp) + newload > max_mon_load(mtmp))
1193 return 0;
1195 return iquan;
1198 /* return number of acceptable neighbour positions */
1200 mfndpos(mon, poss, info, flag)
1201 struct monst *mon;
1202 coord *poss; /* coord poss[9] */
1203 long *info; /* long info[9] */
1204 long flag;
1206 struct permonst *mdat = mon->data;
1207 register struct trap *ttmp;
1208 xchar x, y, nx, ny;
1209 int cnt = 0;
1210 uchar ntyp;
1211 uchar nowtyp;
1212 boolean wantpool, poolok, lavaok, nodiag;
1213 boolean rockok = FALSE, treeok = FALSE, thrudoor;
1214 int maxx, maxy;
1215 boolean poisongas_ok, in_poisongas;
1216 NhRegion *gas_reg;
1217 int gas_glyph = cmap_to_glyph(S_poisoncloud);
1219 x = mon->mx;
1220 y = mon->my;
1221 nowtyp = levl[x][y].typ;
1223 nodiag = NODIAG(mdat - mons);
1224 wantpool = mdat->mlet == S_EEL;
1225 poolok = (is_flyer(mdat) || is_clinger(mdat)
1226 || (is_swimmer(mdat) && !wantpool));
1227 lavaok = (is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat));
1228 thrudoor = ((flag & (ALLOW_WALL | BUSTDOOR)) != 0L);
1229 poisongas_ok = ((nonliving(mdat) || is_vampshifter(mon)
1230 || breathless(mdat)) || resists_poison(mon));
1231 in_poisongas = ((gas_reg = visible_region_at(x,y)) != 0
1232 && gas_reg->glyph == gas_glyph);
1234 if (flag & ALLOW_DIG) {
1235 struct obj *mw_tmp;
1237 /* need to be specific about what can currently be dug */
1238 if (!needspick(mdat)) {
1239 rockok = treeok = TRUE;
1240 } else if ((mw_tmp = MON_WEP(mon)) && mw_tmp->cursed
1241 && mon->weapon_check == NO_WEAPON_WANTED) {
1242 rockok = is_pick(mw_tmp);
1243 treeok = is_axe(mw_tmp);
1244 } else {
1245 rockok = (m_carrying(mon, PICK_AXE)
1246 || (m_carrying(mon, DWARVISH_MATTOCK)
1247 && !which_armor(mon, W_ARMS)));
1248 treeok = (m_carrying(mon, AXE) || (m_carrying(mon, BATTLE_AXE)
1249 && !which_armor(mon, W_ARMS)));
1251 if (rockok || treeok)
1252 thrudoor = TRUE;
1255 nexttry: /* eels prefer the water, but if there is no water nearby,
1256 they will crawl over land */
1257 if (mon->mconf) {
1258 flag |= ALLOW_ALL;
1259 flag &= ~NOTONL;
1261 if (!mon->mcansee)
1262 flag |= ALLOW_SSM;
1263 maxx = min(x + 1, COLNO - 1);
1264 maxy = min(y + 1, ROWNO - 1);
1265 for (nx = max(1, x - 1); nx <= maxx; nx++)
1266 for (ny = max(0, y - 1); ny <= maxy; ny++) {
1267 if (nx == x && ny == y)
1268 continue;
1269 ntyp = levl[nx][ny].typ;
1270 if (IS_ROCK(ntyp)
1271 && !((flag & ALLOW_WALL) && may_passwall(nx, ny))
1272 && !((IS_TREE(ntyp) ? treeok : rockok) && may_dig(nx, ny)))
1273 continue;
1274 /* KMH -- Added iron bars */
1275 if (ntyp == IRONBARS && !(flag & ALLOW_BARS))
1276 continue;
1277 if (IS_DOOR(ntyp) && !(amorphous(mdat) || can_fog(mon))
1278 && (((levl[nx][ny].doormask & D_CLOSED) && !(flag & OPENDOOR))
1279 || ((levl[nx][ny].doormask & D_LOCKED)
1280 && !(flag & UNLOCKDOOR))) && !thrudoor)
1281 continue;
1282 /* avoid poison gas? */
1283 if (!poisongas_ok && !in_poisongas
1284 && (gas_reg = visible_region_at(nx,ny)) != 0
1285 && gas_reg->glyph == gas_glyph)
1286 continue;
1287 /* first diagonal checks (tight squeezes handled below) */
1288 if (nx != x && ny != y
1289 && (nodiag
1290 || (IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN))
1291 || (IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN))
1292 || ((IS_DOOR(nowtyp) || IS_DOOR(ntyp))
1293 && Is_rogue_level(&u.uz))
1294 /* mustn't pass between adjacent long worm segments,
1295 but can attack that way */
1296 || (m_at(x, ny) && m_at(nx, y) && worm_cross(x, y, nx, ny)
1297 && !m_at(nx, ny) && (nx != u.ux || ny != u.uy))))
1298 continue;
1299 if ((is_pool(nx, ny) == wantpool || poolok)
1300 && (lavaok || !is_lava(nx, ny))) {
1301 int dispx, dispy;
1302 boolean monseeu = (mon->mcansee
1303 && (!Invis || perceives(mdat)));
1304 boolean checkobj = OBJ_AT(nx, ny);
1306 /* Displacement also displaces the Elbereth/scare monster,
1307 * as long as you are visible.
1309 if (Displaced && monseeu && mon->mux == nx && mon->muy == ny) {
1310 dispx = u.ux;
1311 dispy = u.uy;
1312 } else {
1313 dispx = nx;
1314 dispy = ny;
1317 info[cnt] = 0;
1318 if (onscary(dispx, dispy, mon)) {
1319 if (!(flag & ALLOW_SSM))
1320 continue;
1321 info[cnt] |= ALLOW_SSM;
1323 if ((nx == u.ux && ny == u.uy)
1324 || (nx == mon->mux && ny == mon->muy)) {
1325 if (nx == u.ux && ny == u.uy) {
1326 /* If it's right next to you, it found you,
1327 * displaced or no. We must set mux and muy
1328 * right now, so when we return we can tell
1329 * that the ALLOW_U means to attack _you_ and
1330 * not the image.
1332 mon->mux = u.ux;
1333 mon->muy = u.uy;
1335 if (!(flag & ALLOW_U))
1336 continue;
1337 info[cnt] |= ALLOW_U;
1338 } else {
1339 if (MON_AT(nx, ny)) {
1340 struct monst *mtmp2 = m_at(nx, ny);
1341 long mmflag = flag | mm_aggression(mon, mtmp2);
1343 if (mmflag & ALLOW_M) {
1344 info[cnt] |= ALLOW_M;
1345 if (mtmp2->mtame) {
1346 if (!(mmflag & ALLOW_TM))
1347 continue;
1348 info[cnt] |= ALLOW_TM;
1350 } else {
1351 mmflag = flag | mm_displacement(mon, mtmp2);
1352 if (!(mmflag & ALLOW_MDISP))
1353 continue;
1354 info[cnt] |= ALLOW_MDISP;
1357 /* Note: ALLOW_SANCT only prevents movement, not
1358 attack, into a temple. */
1359 if (level.flags.has_temple && *in_rooms(nx, ny, TEMPLE)
1360 && !*in_rooms(x, y, TEMPLE)
1361 && in_your_sanctuary((struct monst *) 0, nx, ny)) {
1362 if (!(flag & ALLOW_SANCT))
1363 continue;
1364 info[cnt] |= ALLOW_SANCT;
1367 if (checkobj && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
1368 if (flag & NOGARLIC)
1369 continue;
1370 info[cnt] |= NOGARLIC;
1372 if (checkobj && sobj_at(BOULDER, nx, ny)) {
1373 if (!(flag & ALLOW_ROCK))
1374 continue;
1375 info[cnt] |= ALLOW_ROCK;
1377 if (monseeu && onlineu(nx, ny)) {
1378 if (flag & NOTONL)
1379 continue;
1380 info[cnt] |= NOTONL;
1382 /* check for diagonal tight squeeze */
1383 if (nx != x && ny != y && bad_rock(mdat, x, ny)
1384 && bad_rock(mdat, nx, y) && cant_squeeze_thru(mon))
1385 continue;
1386 /* The monster avoids a particular type of trap if it's
1387 * familiar with the trap type. Pets get ALLOW_TRAPS
1388 * and checking is done in dogmove.c. In either case,
1389 * "harmless" traps are neither avoided nor marked in info[].
1391 if ((ttmp = t_at(nx, ny)) != 0) {
1392 if (ttmp->ttyp >= TRAPNUM || ttmp->ttyp == 0) {
1393 impossible(
1394 "A monster looked at a very strange trap of type %d.",
1395 ttmp->ttyp);
1396 continue;
1398 if ((ttmp->ttyp != RUST_TRAP
1399 || mdat == &mons[PM_IRON_GOLEM])
1400 && ttmp->ttyp != STATUE_TRAP
1401 && ((ttmp->ttyp != PIT && ttmp->ttyp != SPIKED_PIT
1402 && ttmp->ttyp != TRAPDOOR && ttmp->ttyp != HOLE)
1403 || (!is_flyer(mdat) && !is_floater(mdat)
1404 && !is_clinger(mdat)) || Sokoban)
1405 && (ttmp->ttyp != SLP_GAS_TRAP || !resists_sleep(mon))
1406 && (ttmp->ttyp != BEAR_TRAP
1407 || (mdat->msize > MZ_SMALL && !amorphous(mdat)
1408 && !is_flyer(mdat) && !is_floater(mdat)
1409 && !is_whirly(mdat) && !unsolid(mdat)))
1410 && (ttmp->ttyp != FIRE_TRAP || !resists_fire(mon))
1411 && (ttmp->ttyp != SQKY_BOARD || !is_flyer(mdat))
1412 && (ttmp->ttyp != WEB
1413 || (!amorphous(mdat) && !webmaker(mdat)
1414 && !is_whirly(mdat) && !unsolid(mdat)))
1415 && (ttmp->ttyp != ANTI_MAGIC || !resists_magm(mon))) {
1416 if (!(flag & ALLOW_TRAPS)) {
1417 if (mon->mtrapseen & (1L << (ttmp->ttyp - 1)))
1418 continue;
1420 info[cnt] |= ALLOW_TRAPS;
1423 poss[cnt].x = nx;
1424 poss[cnt].y = ny;
1425 cnt++;
1428 if (!cnt && wantpool && !is_pool(x, y)) {
1429 wantpool = FALSE;
1430 goto nexttry;
1432 return cnt;
1435 /* Monster against monster special attacks; for the specified monster
1436 combinations, this allows one monster to attack another adjacent one
1437 in the absence of Conflict. There is no provision for targetting
1438 other monsters; just hand to hand fighting when they happen to be
1439 next to each other. */
1440 STATIC_OVL long
1441 mm_aggression(magr, mdef)
1442 struct monst *magr, /* monster that is currently deciding where to move */
1443 *mdef; /* another monster which is next to it */
1445 /* supposedly purple worms are attracted to shrieking because they
1446 like to eat shriekers, so attack the latter when feasible */
1447 if (magr->data == &mons[PM_PURPLE_WORM]
1448 && mdef->data == &mons[PM_SHRIEKER])
1449 return ALLOW_M | ALLOW_TM;
1450 /* Various other combinations such as dog vs cat, cat vs rat, and
1451 elf vs orc have been suggested. For the time being we don't
1452 support those. */
1453 return 0L;
1456 /* Monster displacing another monster out of the way */
1457 STATIC_OVL long
1458 mm_displacement(magr, mdef)
1459 struct monst *magr, /* monster that is currently deciding where to move */
1460 *mdef; /* another monster which is next to it */
1462 struct permonst *pa = magr->data, *pd = mdef->data;
1464 /* if attacker can't barge through, there's nothing to do;
1465 or if defender can barge through too, don't let attacker
1466 do so, otherwise they might just end up swapping places
1467 again when defender gets its chance to move */
1468 if ((pa->mflags3 & M3_DISPLACES) != 0 && (pd->mflags3 & M3_DISPLACES) == 0
1469 /* no displacing grid bugs diagonally */
1470 && !(magr->mx != mdef->mx && magr->my != mdef->my
1471 && NODIAG(monsndx(pd)))
1472 /* no displacing trapped monsters or multi-location longworms */
1473 && !mdef->mtrapped && (!mdef->wormno || !count_wsegs(mdef))
1474 /* riders can move anything; others, same size or smaller only */
1475 && (is_rider(pa) || pa->msize >= pd->msize))
1476 return ALLOW_MDISP;
1477 return 0L;
1480 /* Is the square close enough for the monster to move or attack into? */
1481 boolean
1482 monnear(mon, x, y)
1483 struct monst *mon;
1484 int x, y;
1486 int distance = dist2(mon->mx, mon->my, x, y);
1488 if (distance == 2 && NODIAG(mon->data - mons))
1489 return 0;
1490 return (boolean) (distance < 3);
1493 /* really free dead monsters */
1494 void
1495 dmonsfree()
1497 struct monst **mtmp, *freetmp;
1498 int count = 0;
1500 for (mtmp = &fmon; *mtmp;) {
1501 freetmp = *mtmp;
1502 if (freetmp->mhp <= 0 && !freetmp->isgd) {
1503 *mtmp = freetmp->nmon;
1504 freetmp->nmon = NULL;
1505 dealloc_monst(freetmp);
1506 count++;
1507 } else
1508 mtmp = &(freetmp->nmon);
1511 if (count != iflags.purge_monsters)
1512 impossible("dmonsfree: %d removed doesn't match %d pending",
1513 count, iflags.purge_monsters);
1514 iflags.purge_monsters = 0;
1517 /* called when monster is moved to larger structure */
1518 void
1519 replmon(mtmp, mtmp2)
1520 struct monst *mtmp, *mtmp2;
1522 struct obj *otmp;
1524 /* transfer the monster's inventory */
1525 for (otmp = mtmp2->minvent; otmp; otmp = otmp->nobj) {
1526 if (otmp->where != OBJ_MINVENT || otmp->ocarry != mtmp)
1527 impossible("replmon: minvent inconsistency");
1528 otmp->ocarry = mtmp2;
1530 mtmp->minvent = 0;
1532 /* remove the old monster from the map and from `fmon' list */
1533 relmon(mtmp, (struct monst **) 0);
1535 /* finish adding its replacement */
1536 if (mtmp != u.usteed) /* don't place steed onto the map */
1537 place_monster(mtmp2, mtmp2->mx, mtmp2->my);
1538 if (mtmp2->wormno) /* update level.monsters[wseg->wx][wseg->wy] */
1539 place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
1540 if (emits_light(mtmp2->data)) {
1541 /* since this is so rare, we don't have any `mon_move_light_source' */
1542 new_light_source(mtmp2->mx, mtmp2->my, emits_light(mtmp2->data),
1543 LS_MONSTER, monst_to_any(mtmp2));
1544 /* here we rely on fact that `mtmp' hasn't actually been deleted */
1545 del_light_source(LS_MONSTER, monst_to_any(mtmp));
1547 mtmp2->nmon = fmon;
1548 fmon = mtmp2;
1549 if (u.ustuck == mtmp)
1550 u.ustuck = mtmp2;
1551 if (u.usteed == mtmp)
1552 u.usteed = mtmp2;
1553 if (mtmp2->isshk)
1554 replshk(mtmp, mtmp2);
1556 /* discard the old monster */
1557 dealloc_monst(mtmp);
1560 /* release mon from the display and the map's monster list,
1561 maybe transfer it to one of the other monster lists */
1562 void
1563 relmon(mon, monst_list)
1564 struct monst *mon;
1565 struct monst **monst_list; /* &migrating_mons or &mydogs or null */
1567 struct monst *mtmp;
1568 boolean unhide = (monst_list != 0);
1569 int mx = mon->mx, my = mon->my;
1571 if (!fmon)
1572 panic("relmon: no fmon available.");
1574 if (unhide) {
1575 /* can't remain hidden across level changes (exception: wizard
1576 clone can continue imitating some other monster form); also,
1577 might be imitating a boulder so need line-of-sight unblocking */
1578 mon->mundetected = 0;
1579 if (mon->m_ap_type && mon->m_ap_type != M_AP_MONSTER)
1580 seemimic(mon);
1583 if (mon->wormno)
1584 remove_worm(mon);
1585 else
1586 remove_monster(mx, my);
1588 if (mon == fmon) {
1589 fmon = fmon->nmon;
1590 } else {
1591 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
1592 if (mtmp->nmon == mon)
1593 break;
1595 if (mtmp)
1596 mtmp->nmon = mon->nmon;
1597 else
1598 panic("relmon: mon not in list.");
1601 if (unhide) {
1602 newsym(mx, my);
1603 /* insert into mydogs or migrating_mons */
1604 mon->nmon = *monst_list;
1605 *monst_list = mon;
1606 } else {
1607 /* orphan has no next monster */
1608 mon->nmon = 0;
1612 void
1613 copy_mextra(mtmp2, mtmp1)
1614 struct monst *mtmp2, *mtmp1;
1616 if (!mtmp2 || !mtmp1 || !mtmp1->mextra)
1617 return;
1619 if (!mtmp2->mextra)
1620 mtmp2->mextra = newmextra();
1621 if (MNAME(mtmp1)) {
1622 new_mname(mtmp2, (int) strlen(MNAME(mtmp1)) + 1);
1623 Strcpy(MNAME(mtmp2), MNAME(mtmp1));
1625 if (EGD(mtmp1)) {
1626 if (!EGD(mtmp2))
1627 newegd(mtmp2);
1628 (void) memcpy((genericptr_t) EGD(mtmp2), (genericptr_t) EGD(mtmp1),
1629 sizeof (struct egd));
1631 if (EPRI(mtmp1)) {
1632 if (!EPRI(mtmp2))
1633 newepri(mtmp2);
1634 (void) memcpy((genericptr_t) EPRI(mtmp2), (genericptr_t) EPRI(mtmp1),
1635 sizeof (struct epri));
1637 if (ESHK(mtmp1)) {
1638 if (!ESHK(mtmp2))
1639 neweshk(mtmp2);
1640 (void) memcpy((genericptr_t) ESHK(mtmp2), (genericptr_t) ESHK(mtmp1),
1641 sizeof (struct eshk));
1643 if (EMIN(mtmp1)) {
1644 if (!EMIN(mtmp2))
1645 newemin(mtmp2);
1646 (void) memcpy((genericptr_t) EMIN(mtmp2), (genericptr_t) EMIN(mtmp1),
1647 sizeof (struct emin));
1649 if (EDOG(mtmp1)) {
1650 if (!EDOG(mtmp2))
1651 newedog(mtmp2);
1652 (void) memcpy((genericptr_t) EDOG(mtmp2), (genericptr_t) EDOG(mtmp1),
1653 sizeof (struct edog));
1655 if (has_mcorpsenm(mtmp1))
1656 MCORPSENM(mtmp2) = MCORPSENM(mtmp1);
1659 void
1660 dealloc_mextra(m)
1661 struct monst *m;
1663 struct mextra *x = m->mextra;
1665 if (x) {
1666 if (x->mname)
1667 free((genericptr_t) x->mname);
1668 if (x->egd)
1669 free((genericptr_t) x->egd);
1670 if (x->epri)
1671 free((genericptr_t) x->epri);
1672 if (x->eshk)
1673 free((genericptr_t) x->eshk);
1674 if (x->emin)
1675 free((genericptr_t) x->emin);
1676 if (x->edog)
1677 free((genericptr_t) x->edog);
1678 /* [no action needed for x->mcorpsenm] */
1680 free((genericptr_t) x);
1681 m->mextra = (struct mextra *) 0;
1685 void
1686 dealloc_monst(mon)
1687 struct monst *mon;
1689 if (mon->nmon)
1690 panic("dealloc_monst with nmon");
1691 if (mon->mextra)
1692 dealloc_mextra(mon);
1693 free((genericptr_t) mon);
1696 /* remove effects of mtmp from other data structures */
1697 STATIC_OVL void
1698 m_detach(mtmp, mptr)
1699 struct monst *mtmp;
1700 struct permonst *mptr; /* reflects mtmp->data _prior_ to mtmp's death */
1702 if (mtmp == context.polearm.hitmon)
1703 context.polearm.hitmon = 0;
1704 if (mtmp->mleashed)
1705 m_unleash(mtmp, FALSE);
1706 /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
1707 mtmp->mtrapped = 0;
1708 mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */
1709 relobj(mtmp, 0, FALSE);
1710 remove_monster(mtmp->mx, mtmp->my);
1711 if (emits_light(mptr))
1712 del_light_source(LS_MONSTER, monst_to_any(mtmp));
1713 if (mtmp->m_ap_type)
1714 seemimic(mtmp);
1715 newsym(mtmp->mx, mtmp->my);
1716 unstuck(mtmp);
1717 fill_pit(mtmp->mx, mtmp->my);
1719 if (mtmp->isshk)
1720 shkgone(mtmp);
1721 if (mtmp->wormno)
1722 wormgone(mtmp);
1723 iflags.purge_monsters++;
1726 /* find the worn amulet of life saving which will save a monster */
1727 struct obj *
1728 mlifesaver(mon)
1729 struct monst *mon;
1731 if (!nonliving(mon->data) || is_vampshifter(mon)) {
1732 struct obj *otmp = which_armor(mon, W_AMUL);
1734 if (otmp && otmp->otyp == AMULET_OF_LIFE_SAVING)
1735 return otmp;
1737 return (struct obj *) 0;
1740 STATIC_OVL void
1741 lifesaved_monster(mtmp)
1742 struct monst *mtmp;
1744 boolean surviver;
1745 struct obj *lifesave = mlifesaver(mtmp);
1747 if (lifesave) {
1748 /* not canseemon; amulets are on the head, so you don't want
1749 * to show this for a long worm with only a tail visible.
1750 * Nor do you check invisibility, because glowing and
1751 * disintegrating amulets are always visible. */
1752 if (cansee(mtmp->mx, mtmp->my)) {
1753 pline("But wait...");
1754 pline("%s medallion begins to glow!", s_suffix(Monnam(mtmp)));
1755 makeknown(AMULET_OF_LIFE_SAVING);
1756 /* amulet is visible, but monster might not be */
1757 if (canseemon(mtmp)) {
1758 if (attacktype(mtmp->data, AT_EXPL)
1759 || attacktype(mtmp->data, AT_BOOM))
1760 pline("%s reconstitutes!", Monnam(mtmp));
1761 else
1762 pline("%s looks much better!", Monnam(mtmp));
1764 pline_The("medallion crumbles to dust!");
1766 m_useup(mtmp, lifesave);
1768 surviver = !(mvitals[monsndx(mtmp->data)].mvflags & G_GENOD);
1769 mtmp->mcanmove = 1;
1770 mtmp->mfrozen = 0;
1771 if (mtmp->mtame && !mtmp->isminion) {
1772 wary_dog(mtmp, !surviver);
1774 if (mtmp->mhpmax <= 0)
1775 mtmp->mhpmax = 10;
1776 mtmp->mhp = mtmp->mhpmax;
1777 if (surviver)
1778 return;
1780 /* genocided monster can't be life-saved */
1781 if (cansee(mtmp->mx, mtmp->my))
1782 pline("Unfortunately, %s is still genocided...", mon_nam(mtmp));
1784 mtmp->mhp = 0;
1787 void
1788 mondead(mtmp)
1789 register struct monst *mtmp;
1791 struct permonst *mptr;
1792 int tmp;
1794 lifesaved_monster(mtmp);
1795 if (mtmp->mhp > 0)
1796 return;
1798 if (is_vampshifter(mtmp)) {
1799 int mndx = mtmp->cham;
1800 int x = mtmp->mx, y = mtmp->my;
1802 /* this only happens if shapeshifted */
1803 if (mndx >= LOW_PM && mndx != monsndx(mtmp->data)
1804 && !(mvitals[mndx].mvflags & G_GENOD)) {
1805 char buf[BUFSZ];
1806 boolean in_door = (amorphous(mtmp->data)
1807 && closed_door(mtmp->mx, mtmp->my)),
1808 /* alternate message phrasing for some monster types */
1809 spec_mon = (nonliving(mtmp->data)
1810 || noncorporeal(mtmp->data)
1811 || amorphous(mtmp->data));
1813 /* construct a format string before transformation */
1814 Sprintf(buf, "The %s%s suddenly %s and rises as %%s!",
1815 spec_mon ? "" : "seemingly dead ",
1816 x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1817 SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
1818 | SUPPRESS_INVISIBLE | SUPPRESS_IT,
1819 FALSE),
1820 spec_mon ? "reconstitutes" : "transforms");
1821 mtmp->mcanmove = 1;
1822 mtmp->mfrozen = 0;
1823 if (mtmp->mhpmax <= 0)
1824 mtmp->mhpmax = 10;
1825 mtmp->mhp = mtmp->mhpmax;
1826 /* this can happen if previously a fog cloud */
1827 if (u.uswallow && (mtmp == u.ustuck))
1828 expels(mtmp, mtmp->data, FALSE);
1829 if (in_door) {
1830 coord new_xy;
1832 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
1833 rloc_to(mtmp, new_xy.x, new_xy.y);
1836 newcham(mtmp, &mons[mndx], FALSE, FALSE);
1837 if (mtmp->data == &mons[mndx])
1838 mtmp->cham = NON_PM;
1839 else
1840 mtmp->cham = mndx;
1841 if (canspotmon(mtmp)) {
1842 pline(buf, a_monnam(mtmp));
1843 vamp_rise_msg = TRUE;
1845 newsym(x, y);
1846 return;
1850 /* dead vault guard is actually kept at coordinate <0,0> until
1851 his temporary corridor to/from the vault has been removed;
1852 need to do this after life-saving and before m_detach() */
1853 if (mtmp->isgd && !grddead(mtmp))
1854 return;
1856 /* Player is thrown from his steed when it dies */
1857 if (mtmp == u.usteed)
1858 dismount_steed(DISMOUNT_GENERIC);
1860 mptr = mtmp->data; /* save this for m_detach() */
1861 /* restore chameleon, lycanthropes to true form at death */
1862 if (mtmp->cham >= LOW_PM) {
1863 set_mon_data(mtmp, &mons[mtmp->cham], -1);
1864 mtmp->cham = NON_PM;
1865 } else if (mtmp->data == &mons[PM_WEREJACKAL])
1866 set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1867 else if (mtmp->data == &mons[PM_WEREWOLF])
1868 set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1869 else if (mtmp->data == &mons[PM_WERERAT])
1870 set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1872 /* if MAXMONNO monsters of a given type have died, and it
1873 * can be done, extinguish that monster.
1875 * mvitals[].died does double duty as total number of dead monsters
1876 * and as experience factor for the player killing more monsters.
1877 * this means that a dragon dying by other means reduces the
1878 * experience the player gets for killing a dragon directly; this
1879 * is probably not too bad, since the player likely finagled the
1880 * first dead dragon via ring of conflict or pets, and extinguishing
1881 * based on only player kills probably opens more avenues of abuse
1882 * for rings of conflict and such.
1884 tmp = monsndx(mtmp->data);
1885 if (mvitals[tmp].died < 255)
1886 mvitals[tmp].died++;
1888 /* if it's a (possibly polymorphed) quest leader, mark him as dead */
1889 if (mtmp->m_id == quest_status.leader_m_id)
1890 quest_status.leader_is_dead = TRUE;
1891 #ifdef MAIL
1892 /* if the mail daemon dies, no more mail delivery. -3. */
1893 if (tmp == PM_MAIL_DAEMON)
1894 mvitals[tmp].mvflags |= G_GENOD;
1895 #endif
1897 if (mtmp->data->mlet == S_KOP) {
1898 /* Dead Kops may come back. */
1899 switch (rnd(5)) {
1900 case 1: /* returns near the stairs */
1901 (void) makemon(mtmp->data, xdnstair, ydnstair, NO_MM_FLAGS);
1902 break;
1903 case 2: /* randomly */
1904 (void) makemon(mtmp->data, 0, 0, NO_MM_FLAGS);
1905 break;
1906 default:
1907 break;
1910 if (mtmp->iswiz)
1911 wizdead();
1912 if (mtmp->data->msound == MS_NEMESIS)
1913 nemdead();
1914 if (mtmp->data == &mons[PM_MEDUSA])
1915 u.uachieve.killed_medusa = 1;
1916 if (glyph_is_invisible(levl[mtmp->mx][mtmp->my].glyph))
1917 unmap_object(mtmp->mx, mtmp->my);
1918 m_detach(mtmp, mptr);
1921 /* TRUE if corpse might be dropped, magr may die if mon was swallowed */
1922 boolean
1923 corpse_chance(mon, magr, was_swallowed)
1924 struct monst *mon;
1925 struct monst *magr; /* killer, if swallowed */
1926 boolean was_swallowed; /* digestion */
1928 struct permonst *mdat = mon->data;
1929 int i, tmp;
1931 if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1932 if (cansee(mon->mx, mon->my) && !was_swallowed)
1933 pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
1934 return FALSE;
1937 /* Gas spores always explode upon death */
1938 for (i = 0; i < NATTK; i++) {
1939 if (mdat->mattk[i].aatyp == AT_BOOM) {
1940 if (mdat->mattk[i].damn)
1941 tmp = d((int) mdat->mattk[i].damn, (int) mdat->mattk[i].damd);
1942 else if (mdat->mattk[i].damd)
1943 tmp = d((int) mdat->mlevel + 1, (int) mdat->mattk[i].damd);
1944 else
1945 tmp = 0;
1946 if (was_swallowed && magr) {
1947 if (magr == &youmonst) {
1948 There("is an explosion in your %s!", body_part(STOMACH));
1949 Sprintf(killer.name, "%s explosion",
1950 s_suffix(mdat->mname));
1951 losehp(Maybe_Half_Phys(tmp), killer.name, KILLED_BY_AN);
1952 } else {
1953 You_hear("an explosion.");
1954 magr->mhp -= tmp;
1955 if (magr->mhp < 1)
1956 mondied(magr);
1957 if (magr->mhp < 1) { /* maybe lifesaved */
1958 if (canspotmon(magr))
1959 pline("%s rips open!", Monnam(magr));
1960 } else if (canseemon(magr))
1961 pline("%s seems to have indigestion.", Monnam(magr));
1964 return FALSE;
1967 Sprintf(killer.name, "%s explosion", s_suffix(mdat->mname));
1968 killer.format = KILLED_BY_AN;
1969 explode(mon->mx, mon->my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS);
1970 return FALSE;
1974 /* must duplicate this below check in xkilled() since it results in
1975 * creating no objects as well as no corpse
1977 if (LEVEL_SPECIFIC_NOCORPSE(mdat))
1978 return FALSE;
1980 if (((bigmonst(mdat) || mdat == &mons[PM_LIZARD]) && !mon->mcloned)
1981 || is_golem(mdat) || is_mplayer(mdat) || is_rider(mdat))
1982 return TRUE;
1983 tmp = 2 + ((mdat->geno & G_FREQ) < 2) + verysmall(mdat);
1984 return (boolean) !rn2(tmp);
1987 /* drop (perhaps) a cadaver and remove monster */
1988 void
1989 mondied(mdef)
1990 register struct monst *mdef;
1992 mondead(mdef);
1993 if (mdef->mhp > 0)
1994 return; /* lifesaved */
1996 if (corpse_chance(mdef, (struct monst *) 0, FALSE)
1997 && (accessible(mdef->mx, mdef->my) || is_pool(mdef->mx, mdef->my)))
1998 (void) make_corpse(mdef, CORPSTAT_NONE);
2001 /* monster disappears, not dies */
2002 void
2003 mongone(mdef)
2004 struct monst *mdef;
2006 mdef->mhp = 0; /* can skip some inventory bookkeeping */
2008 /* dead vault guard is actually kept at coordinate <0,0> until
2009 his temporary corridor to/from the vault has been removed */
2010 if (mdef->isgd && !grddead(mdef))
2011 return;
2012 /* hero is thrown from his steed when it disappears */
2013 if (mdef == u.usteed)
2014 dismount_steed(DISMOUNT_GENERIC);
2015 /* drop special items like the Amulet so that a dismissed Kop or nurse
2016 can't remove them from the game */
2017 mdrop_special_objs(mdef);
2018 /* release rest of monster's inventory--it is removed from game */
2019 discard_minvent(mdef);
2020 m_detach(mdef, mdef->data);
2023 /* drop a statue or rock and remove monster */
2024 void
2025 monstone(mdef)
2026 struct monst *mdef;
2028 struct obj *otmp, *obj, *oldminvent;
2029 xchar x = mdef->mx, y = mdef->my;
2030 boolean wasinside = FALSE;
2032 /* we have to make the statue before calling mondead, to be able to
2033 * put inventory in it, and we have to check for lifesaving before
2034 * making the statue....
2036 lifesaved_monster(mdef);
2037 if (mdef->mhp > 0)
2038 return;
2040 mdef->mtrapped = 0; /* (see m_detach) */
2042 if ((int) mdef->data->msize > MZ_TINY
2043 || !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
2044 oldminvent = 0;
2045 /* some objects may end up outside the statue */
2046 while ((obj = mdef->minvent) != 0) {
2047 obj_extract_self(obj);
2048 if (obj->owornmask)
2049 update_mon_intrinsics(mdef, obj, FALSE, TRUE);
2050 obj_no_longer_held(obj);
2051 if (obj->owornmask & W_WEP)
2052 setmnotwielded(mdef, obj);
2053 obj->owornmask = 0L;
2054 if (obj->otyp == BOULDER
2055 #if 0 /* monsters don't carry statues */
2056 || (obj->otyp == STATUE
2057 && mons[obj->corpsenm].msize >= mdef->data->msize)
2058 #endif
2059 /* invocation tools resist even with 0% resistance */
2060 || obj_resists(obj, 0, 0)) {
2061 if (flooreffects(obj, x, y, "fall"))
2062 continue;
2063 place_object(obj, x, y);
2064 } else {
2065 if (obj->lamplit)
2066 end_burn(obj, TRUE);
2067 obj->nobj = oldminvent;
2068 oldminvent = obj;
2071 /* defer statue creation until after inventory removal
2072 so that saved monster traits won't retain any stale
2073 item-conferred attributes */
2074 otmp = mkcorpstat(STATUE, mdef, mdef->data, x, y, CORPSTAT_NONE);
2075 if (has_mname(mdef))
2076 otmp = oname(otmp, MNAME(mdef));
2077 while ((obj = oldminvent) != 0) {
2078 oldminvent = obj->nobj;
2079 (void) add_to_container(otmp, obj);
2081 /* Archeologists should not break unique statues */
2082 if (mdef->data->geno & G_UNIQ)
2083 otmp->spe = 1;
2084 otmp->owt = weight(otmp);
2085 } else
2086 otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
2088 stackobj(otmp);
2089 /* mondead() already does this, but we must do it before the newsym */
2090 if (glyph_is_invisible(levl[x][y].glyph))
2091 unmap_object(x, y);
2092 if (cansee(x, y))
2093 newsym(x, y);
2094 /* We don't currently trap the hero in the statue in this case but we
2095 * could */
2096 if (u.uswallow && u.ustuck == mdef)
2097 wasinside = TRUE;
2098 mondead(mdef);
2099 if (wasinside) {
2100 if (is_animal(mdef->data))
2101 You("%s through an opening in the new %s.",
2102 locomotion(youmonst.data, "jump"), xname(otmp));
2106 /* another monster has killed the monster mdef */
2107 void
2108 monkilled(mdef, fltxt, how)
2109 struct monst *mdef;
2110 const char *fltxt;
2111 int how;
2113 boolean be_sad = FALSE; /* true if unseen pet is killed */
2115 if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
2116 && fltxt)
2117 pline("%s is %s%s%s!", Monnam(mdef),
2118 nonliving(mdef->data) ? "destroyed" : "killed",
2119 *fltxt ? " by the " : "", fltxt);
2120 else
2121 be_sad = (mdef->mtame != 0);
2123 /* no corpses if digested or disintegrated */
2124 if (how == AD_DGST || how == -AD_RBRE)
2125 mondead(mdef);
2126 else
2127 mondied(mdef);
2129 if (be_sad && mdef->mhp <= 0)
2130 You("have a sad feeling for a moment, then it passes.");
2133 void
2134 unstuck(mtmp)
2135 struct monst *mtmp;
2137 if (u.ustuck == mtmp) {
2138 if (u.uswallow) {
2139 u.ux = mtmp->mx;
2140 u.uy = mtmp->my;
2141 u.uswallow = 0;
2142 u.uswldtim = 0;
2143 if (Punished && uchain->where != OBJ_FLOOR)
2144 placebc();
2145 vision_full_recalc = 1;
2146 docrt();
2148 u.ustuck = 0;
2152 void
2153 killed(mtmp)
2154 struct monst *mtmp;
2156 xkilled(mtmp, 1);
2159 /* the player has killed the monster mtmp */
2160 void
2161 xkilled(mtmp, dest)
2162 struct monst *mtmp;
2163 int dest; /* dest==1, normal; dest==0, don't print message; dest==2, don't
2164 drop corpse either; dest==3, message but no corpse */
2166 int tmp, mndx, x = mtmp->mx, y = mtmp->my;
2167 struct permonst *mdat;
2168 struct obj *otmp;
2169 struct trap *t;
2170 boolean wasinside = u.uswallow && (u.ustuck == mtmp);
2171 boolean burycorpse = FALSE;
2173 /* KMH, conduct */
2174 u.uconduct.killer++;
2176 if (dest & 1) {
2177 const char *verb = nonliving(mtmp->data) ? "destroy" : "kill";
2179 if (!wasinside && !canspotmon(mtmp))
2180 You("%s it!", verb);
2181 else {
2182 You("%s %s!", verb,
2183 !mtmp->mtame
2184 ? mon_nam(mtmp)
2185 : x_monnam(mtmp,
2186 (has_mname(mtmp)) ? ARTICLE_NONE : ARTICLE_THE,
2187 "poor",
2188 (has_mname(mtmp)) ? SUPPRESS_SADDLE : 0,
2189 FALSE));
2193 if (mtmp->mtrapped && (t = t_at(x, y)) != 0
2194 && (t->ttyp == PIT || t->ttyp == SPIKED_PIT)) {
2195 if (sobj_at(BOULDER, x, y))
2196 dest |= 2; /*
2197 * Prevent corpses/treasure being created "on top"
2198 * of the boulder that is about to fall in. This is
2199 * out of order, but cannot be helped unless this
2200 * whole routine is rearranged.
2202 if (m_carrying(mtmp, BOULDER))
2203 burycorpse = TRUE;
2206 /* your pet knows who just killed it...watch out */
2207 if (mtmp->mtame && !mtmp->isminion)
2208 EDOG(mtmp)->killed_by_u = 1;
2210 if (wasinside && thrownobj && thrownobj != uball) {
2211 /* thrown object has killed hero's engulfer; add it to mon's
2212 inventory now so that it will be placed with mon's other
2213 stuff prior to lookhere/autopickup when hero is expelled
2214 below (as a side-effect, this missile has immunity from
2215 being consumed [for this shot/throw only]) */
2216 mpickobj(mtmp, thrownobj);
2217 /* let throwing code know that missile has been disposed of */
2218 thrownobj = 0;
2221 vamp_rise_msg = FALSE; /* might get set in mondead() */
2222 /* dispose of monster and make cadaver */
2223 if (stoned)
2224 monstone(mtmp);
2225 else
2226 mondead(mtmp);
2228 if (mtmp->mhp > 0) { /* monster lifesaved */
2229 /* Cannot put the non-visible lifesaving message in
2230 * lifesaved_monster() since the message appears only when you
2231 * kill it (as opposed to visible lifesaving which always
2232 * appears).
2234 stoned = FALSE;
2235 if (!cansee(x, y) && !vamp_rise_msg)
2236 pline("Maybe not...");
2237 return;
2240 mdat = mtmp->data; /* note: mondead can change mtmp->data */
2241 mndx = monsndx(mdat);
2243 if (stoned) {
2244 stoned = FALSE;
2245 goto cleanup;
2248 if ((dest & 2) || LEVEL_SPECIFIC_NOCORPSE(mdat))
2249 goto cleanup;
2251 #ifdef MAIL
2252 if (mdat == &mons[PM_MAIL_DAEMON]) {
2253 stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
2255 #endif
2256 if (accessible(x, y) || is_pool(x, y)) {
2257 struct obj *cadaver;
2258 int otyp;
2260 /* illogical but traditional "treasure drop" */
2261 if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
2262 /* no extra item from swallower or steed */
2263 && (x != u.ux || y != u.uy)
2264 /* no extra item from kops--too easy to abuse */
2265 && mdat->mlet != S_KOP
2266 /* no items from cloned monsters */
2267 && !mtmp->mcloned) {
2268 otmp = mkobj(RANDOM_CLASS, TRUE);
2269 /* don't create large objects from small monsters */
2270 otyp = otmp->otyp;
2271 if (mdat->msize < MZ_HUMAN && otyp != FIGURINE
2272 /* oc_big is also oc_bimanual and oc_bulky */
2273 && (otmp->owt > 30 || objects[otyp].oc_big)) {
2274 delobj(otmp);
2275 } else if (!flooreffects(otmp, x, y, (dest & 1) ? "fall" : "")) {
2276 place_object(otmp, x, y);
2277 stackobj(otmp);
2280 /* corpse--none if hero was inside the monster */
2281 if (!wasinside && corpse_chance(mtmp, (struct monst *) 0, FALSE)) {
2282 cadaver = make_corpse(mtmp, burycorpse ? CORPSTAT_BURIED
2283 : CORPSTAT_NONE);
2284 if (burycorpse && cadaver && cansee(x, y) && !mtmp->minvis
2285 && cadaver->where == OBJ_BURIED && (dest & 1)) {
2286 pline("%s corpse ends up buried.", s_suffix(Monnam(mtmp)));
2290 if (wasinside)
2291 spoteffects(TRUE); /* poor man's expels() */
2292 /* monster is gone, corpse or other object might now be visible */
2293 newsym(x, y);
2295 cleanup:
2296 /* punish bad behaviour */
2297 if (is_human(mdat) && (!always_hostile(mdat) && mtmp->malign <= 0)
2298 && (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD)
2299 && u.ualign.type != A_CHAOTIC) {
2300 HTelepat &= ~INTRINSIC;
2301 change_luck(-2);
2302 You("murderer!");
2303 if (Blind && !Blind_telepat)
2304 see_monsters(); /* Can't sense monsters any more. */
2306 if ((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)
2307 change_luck(-1);
2308 if (is_unicorn(mdat) && sgn(u.ualign.type) == sgn(mdat->maligntyp)) {
2309 change_luck(-5);
2310 You_feel("guilty...");
2313 /* give experience points */
2314 tmp = experience(mtmp, (int) mvitals[mndx].died);
2315 more_experienced(tmp, 0);
2316 newexplevel(); /* will decide if you go up */
2318 /* adjust alignment points */
2319 if (mtmp->m_id == quest_status.leader_m_id) { /* REAL BAD! */
2320 adjalign(-(u.ualign.record + (int) ALIGNLIM / 2));
2321 pline("That was %sa bad idea...",
2322 u.uevent.qcompleted ? "probably " : "");
2323 } else if (mdat->msound == MS_NEMESIS) { /* Real good! */
2324 adjalign((int) (ALIGNLIM / 4));
2325 } else if (mdat->msound == MS_GUARDIAN) { /* Bad */
2326 adjalign(-(int) (ALIGNLIM / 8));
2327 if (!Hallucination)
2328 pline("That was probably a bad idea...");
2329 else
2330 pline("Whoopsie-daisy!");
2331 } else if (mtmp->ispriest) {
2332 adjalign((p_coaligned(mtmp)) ? -2 : 2);
2333 /* cancel divine protection for killing your priest */
2334 if (p_coaligned(mtmp))
2335 u.ublessed = 0;
2336 if (mdat->maligntyp == A_NONE)
2337 adjalign((int) (ALIGNLIM / 4)); /* BIG bonus */
2338 } else if (mtmp->mtame) {
2339 adjalign(-15); /* bad!! */
2340 /* your god is mighty displeased... */
2341 if (!Hallucination)
2342 You_hear("the rumble of distant thunder...");
2343 else
2344 You_hear("the studio audience applaud!");
2345 } else if (mtmp->mpeaceful)
2346 adjalign(-5);
2348 /* malign was already adjusted for u.ualign.type and randomization */
2349 adjalign(mtmp->malign);
2352 /* changes the monster into a stone monster of the same type
2353 this should only be called when poly_when_stoned() is true */
2354 void
2355 mon_to_stone(mtmp)
2356 struct monst *mtmp;
2358 if (mtmp->data->mlet == S_GOLEM) {
2359 /* it's a golem, and not a stone golem */
2360 if (canseemon(mtmp))
2361 pline("%s solidifies...", Monnam(mtmp));
2362 if (newcham(mtmp, &mons[PM_STONE_GOLEM], FALSE, FALSE)) {
2363 if (canseemon(mtmp))
2364 pline("Now it's %s.", an(mtmp->data->mname));
2365 } else {
2366 if (canseemon(mtmp))
2367 pline("... and returns to normal.");
2369 } else
2370 impossible("Can't polystone %s!", a_monnam(mtmp));
2373 boolean
2374 vamp_stone(mtmp)
2375 struct monst *mtmp;
2377 if (is_vampshifter(mtmp)) {
2378 int mndx = mtmp->cham;
2379 int x = mtmp->mx, y = mtmp->my;
2381 /* this only happens if shapeshifted */
2382 if (mndx >= LOW_PM && mndx != monsndx(mtmp->data)
2383 && !(mvitals[mndx].mvflags & G_GENOD)) {
2384 char buf[BUFSZ];
2385 boolean in_door = (amorphous(mtmp->data)
2386 && closed_door(mtmp->mx, mtmp->my));
2388 /* construct a format string before transformation */
2389 Sprintf(buf, "The lapidifying %s %s %s",
2390 x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
2391 SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
2392 | SUPPRESS_INVISIBLE | SUPPRESS_IT,
2393 FALSE),
2394 amorphous(mtmp->data) ? "coalesces on the" :
2395 is_flyer(mtmp->data) ? "drops to the" : "writhes on the",
2396 surface(x,y));
2397 mtmp->mcanmove = 1;
2398 mtmp->mfrozen = 0;
2399 if (mtmp->mhpmax <= 0)
2400 mtmp->mhpmax = 10;
2401 mtmp->mhp = mtmp->mhpmax;
2402 /* this can happen if previously a fog cloud */
2403 if (u.uswallow && (mtmp == u.ustuck))
2404 expels(mtmp, mtmp->data, FALSE);
2405 if (in_door) {
2406 coord new_xy;
2408 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
2409 rloc_to(mtmp, new_xy.x, new_xy.y);
2412 if (canspotmon(mtmp)) {
2413 pline("%s!", buf);
2414 display_nhwindow(WIN_MESSAGE, FALSE);
2416 newcham(mtmp, &mons[mndx], FALSE, FALSE);
2417 if (mtmp->data == &mons[mndx])
2418 mtmp->cham = NON_PM;
2419 else
2420 mtmp->cham = mndx;
2421 if (canspotmon(mtmp)) {
2422 pline("%s rises from the %s with renewed agility!",
2423 Amonnam(mtmp), surface(mtmp->mx, mtmp->my));
2425 newsym(mtmp->mx, mtmp->my);
2426 return FALSE; /* didn't petrify */
2429 return TRUE;
2432 /* make monster mtmp next to you (if possible);
2433 might place monst on far side of a wall or boulder */
2434 void
2435 mnexto(mtmp)
2436 struct monst *mtmp;
2438 coord mm;
2439 boolean couldspot = canspotmon(mtmp);
2441 if (mtmp == u.usteed) {
2442 /* Keep your steed in sync with you instead */
2443 mtmp->mx = u.ux;
2444 mtmp->my = u.uy;
2445 return;
2448 if (!enexto(&mm, u.ux, u.uy, mtmp->data))
2449 return;
2450 rloc_to(mtmp, mm.x, mm.y);
2451 if (!in_mklev && (mtmp->mstrategy & STRAT_APPEARMSG)) {
2452 mtmp->mstrategy &= ~STRAT_APPEARMSG; /* one chance only */
2453 if (!couldspot && canspotmon(mtmp))
2454 pline("%s suddenly %s!", Amonnam(mtmp),
2455 !Blind ? "appears" : "arrives");
2457 return;
2460 /* like mnexto() but requires destination to be directly accessible */
2461 void
2462 maybe_mnexto(mtmp)
2463 struct monst *mtmp;
2465 coord mm;
2466 struct permonst *ptr = mtmp->data;
2467 boolean diagok = !NODIAG(ptr - mons);
2468 int tryct = 20;
2470 do {
2471 if (!enexto(&mm, u.ux, u.uy, ptr))
2472 return;
2473 if (couldsee(mm.x, mm.y)
2474 /* don't move grid bugs diagonally */
2475 && (diagok || mm.x == mtmp->mx || mm.y == mtmp->my)) {
2476 rloc_to(mtmp, mm.x, mm.y);
2477 return;
2479 } while (--tryct > 0);
2482 /* mnearto()
2483 * Put monster near (or at) location if possible.
2484 * Returns:
2485 * 1 - if a monster was moved from x, y to put mtmp at x, y.
2486 * 0 - in most cases.
2488 boolean
2489 mnearto(mtmp, x, y, move_other)
2490 register struct monst *mtmp;
2491 xchar x, y;
2492 boolean move_other; /* make sure mtmp gets to x, y! so move m_at(x, y) */
2494 struct monst *othermon = (struct monst *) 0;
2495 xchar newx, newy;
2496 coord mm;
2498 if (mtmp->mx == x && mtmp->my == y)
2499 return FALSE;
2501 if (move_other && (othermon = m_at(x, y)) != 0) {
2502 if (othermon->wormno)
2503 remove_worm(othermon);
2504 else
2505 remove_monster(x, y);
2508 newx = x;
2509 newy = y;
2510 if (!goodpos(newx, newy, mtmp, 0)) {
2511 /* Actually we have real problems if enexto ever fails.
2512 * Migrating_mons that need to be placed will cause
2513 * no end of trouble.
2515 if (!enexto(&mm, newx, newy, mtmp->data))
2516 return FALSE;
2517 newx = mm.x;
2518 newy = mm.y;
2520 rloc_to(mtmp, newx, newy);
2522 if (move_other && othermon) {
2523 xchar oldx = othermon->mx, oldy = othermon->my;
2525 othermon->mx = othermon->my = 0;
2526 (void) mnearto(othermon, x, y, FALSE);
2527 if (othermon->mx == 0 && othermon->my == 0) {
2528 /* reloc failed, dump monster into "limbo"
2529 (aka migrate to current level) */
2530 othermon->mx = oldx;
2531 othermon->my = oldy;
2532 mdrop_special_objs(othermon);
2533 migrate_to_level(othermon, ledger_no(&u.uz), MIGR_APPROX_XY, NULL);
2537 return FALSE;
2540 /* monster responds to player action; not the same as a passive attack;
2541 assumes reason for response has been tested, and response _must_ be made */
2542 void
2543 m_respond(mtmp)
2544 struct monst *mtmp;
2546 if (mtmp->data->msound == MS_SHRIEK) {
2547 if (!Deaf) {
2548 pline("%s shrieks.", Monnam(mtmp));
2549 stop_occupation();
2551 if (!rn2(10)) {
2552 if (!rn2(13))
2553 (void) makemon(&mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
2554 else
2555 (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
2557 aggravate();
2559 if (mtmp->data == &mons[PM_MEDUSA]) {
2560 register int i;
2562 for (i = 0; i < NATTK; i++)
2563 if (mtmp->data->mattk[i].aatyp == AT_GAZE) {
2564 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
2565 break;
2570 void
2571 setmangry(mtmp)
2572 struct monst *mtmp;
2574 mtmp->mstrategy &= ~STRAT_WAITMASK;
2575 if (!mtmp->mpeaceful)
2576 return;
2577 if (mtmp->mtame)
2578 return;
2579 mtmp->mpeaceful = 0;
2580 if (mtmp->ispriest) {
2581 if (p_coaligned(mtmp))
2582 adjalign(-5); /* very bad */
2583 else
2584 adjalign(2);
2585 } else
2586 adjalign(-1); /* attacking peaceful monsters is bad */
2587 if (couldsee(mtmp->mx, mtmp->my)) {
2588 if (humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
2589 pline("%s gets angry!", Monnam(mtmp));
2590 else if (flags.verbose && !Deaf)
2591 growl(mtmp);
2594 /* attacking your own quest leader will anger his or her guardians */
2595 if (!context.mon_moving /* should always be the case here */
2596 && mtmp->data == &mons[quest_info(MS_LEADER)]) {
2597 struct monst *mon;
2598 struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
2599 int got_mad = 0;
2601 /* guardians will sense this attack even if they can't see it */
2602 for (mon = fmon; mon; mon = mon->nmon) {
2603 if (DEADMONSTER(mon))
2604 continue;
2605 if (mon->data == q_guardian && mon->mpeaceful) {
2606 mon->mpeaceful = 0;
2607 if (canseemon(mon))
2608 ++got_mad;
2611 if (got_mad && !Hallucination)
2612 pline_The("%s appear%s to be angry too...",
2613 got_mad == 1 ? q_guardian->mname
2614 : makeplural(q_guardian->mname),
2615 got_mad == 1 ? "s" : "");
2619 /* wake up a monster, usually making it angry in the process */
2620 void
2621 wakeup(mtmp)
2622 register struct monst *mtmp;
2624 mtmp->msleeping = 0;
2625 finish_meating(mtmp);
2626 setmangry(mtmp);
2627 if (mtmp->m_ap_type) {
2628 seemimic(mtmp);
2629 } else if (context.forcefight && !context.mon_moving
2630 && mtmp->mundetected) {
2631 mtmp->mundetected = 0;
2632 newsym(mtmp->mx, mtmp->my);
2636 /* Wake up nearby monsters without angering them. */
2637 void
2638 wake_nearby()
2640 register struct monst *mtmp;
2642 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2643 if (DEADMONSTER(mtmp))
2644 continue;
2645 if (distu(mtmp->mx, mtmp->my) < u.ulevel * 20) {
2646 mtmp->msleeping = 0;
2647 if (!unique_corpstat(mtmp->data))
2648 mtmp->mstrategy &= ~STRAT_WAITMASK;
2649 if (mtmp->mtame && !mtmp->isminion)
2650 EDOG(mtmp)->whistletime = moves;
2655 /* Wake up monsters near some particular location. */
2656 void
2657 wake_nearto(x, y, distance)
2658 register int x, y, distance;
2660 register struct monst *mtmp;
2662 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2663 if (DEADMONSTER(mtmp))
2664 continue;
2665 if (distance == 0 || dist2(mtmp->mx, mtmp->my, x, y) < distance) {
2666 mtmp->msleeping = 0;
2667 if (!unique_corpstat(mtmp->data))
2668 mtmp->mstrategy &= ~STRAT_WAITMASK;
2673 /* NOTE: we must check for mimicry before calling this routine */
2674 void
2675 seemimic(mtmp)
2676 register struct monst *mtmp;
2678 boolean is_blocker_appear = (is_lightblocker_mappear(mtmp));
2680 if (has_mcorpsenm(mtmp))
2681 freemcorpsenm(mtmp);
2683 mtmp->m_ap_type = M_AP_NOTHING;
2684 mtmp->mappearance = 0;
2687 * Discovered mimics don't block light.
2689 if (is_blocker_appear
2690 && !does_block(mtmp->mx, mtmp->my, &levl[mtmp->mx][mtmp->my]))
2691 unblock_point(mtmp->mx, mtmp->my);
2693 newsym(mtmp->mx, mtmp->my);
2696 /* force all chameleons to become normal */
2697 void
2698 rescham()
2700 register struct monst *mtmp;
2701 int mcham;
2703 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2704 if (DEADMONSTER(mtmp))
2705 continue;
2706 mcham = (int) mtmp->cham;
2707 if (mcham >= LOW_PM) {
2708 (void) newcham(mtmp, &mons[mcham], FALSE, FALSE);
2709 mtmp->cham = NON_PM;
2711 if (is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
2712 new_were(mtmp);
2713 if (mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
2714 seemimic(mtmp);
2715 /* we pretend that the mimic doesn't
2716 know that it has been unmasked */
2717 mtmp->msleeping = 1;
2722 /* Let the chameleons change again -dgk */
2723 void
2724 restartcham()
2726 register struct monst *mtmp;
2728 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2729 if (DEADMONSTER(mtmp))
2730 continue;
2731 mtmp->cham = pm_to_cham(monsndx(mtmp->data));
2732 if (mtmp->data->mlet == S_MIMIC && mtmp->msleeping
2733 && cansee(mtmp->mx, mtmp->my)) {
2734 set_mimic_sym(mtmp);
2735 newsym(mtmp->mx, mtmp->my);
2740 /* called when restoring a monster from a saved level; protection
2741 against shape-changing might be different now than it was at the
2742 time the level was saved. */
2743 void
2744 restore_cham(mon)
2745 struct monst *mon;
2747 int mcham;
2749 if (Protection_from_shape_changers) {
2750 mcham = (int) mon->cham;
2751 if (mcham >= LOW_PM) {
2752 mon->cham = NON_PM;
2753 (void) newcham(mon, &mons[mcham], FALSE, FALSE);
2754 } else if (is_were(mon->data) && !is_human(mon->data)) {
2755 new_were(mon);
2757 } else if (mon->cham == NON_PM) {
2758 mon->cham = pm_to_cham(monsndx(mon->data));
2762 /* unwatched hiders may hide again; if so, returns True */
2763 STATIC_OVL boolean
2764 restrap(mtmp)
2765 register struct monst *mtmp;
2767 struct trap *t;
2769 if (mtmp->mcan || mtmp->m_ap_type || cansee(mtmp->mx, mtmp->my)
2770 || rn2(3) || mtmp == u.ustuck
2771 /* can't hide while trapped except in pits */
2772 || (mtmp->mtrapped && (t = t_at(mtmp->mx, mtmp->my)) != 0
2773 && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))
2774 || (sensemon(mtmp) && distu(mtmp->mx, mtmp->my) <= 2))
2775 return FALSE;
2777 if (mtmp->data->mlet == S_MIMIC) {
2778 set_mimic_sym(mtmp);
2779 return TRUE;
2780 } else if (levl[mtmp->mx][mtmp->my].typ == ROOM) {
2781 mtmp->mundetected = 1;
2782 return TRUE;
2785 return FALSE;
2788 /* monster/hero tries to hide under something at the current location */
2789 boolean
2790 hideunder(mtmp)
2791 struct monst *mtmp;
2793 struct trap *t;
2794 boolean undetected = FALSE, is_u = (mtmp == &youmonst);
2795 xchar x = is_u ? u.ux : mtmp->mx, y = is_u ? u.uy : mtmp->my;
2797 if (mtmp == u.ustuck) {
2798 ; /* can't hide if holding you or held by you */
2799 } else if (is_u ? (u.utrap && u.utraptype != TT_PIT)
2800 : (mtmp->mtrapped && (t = t_at(x, y)) != 0
2801 && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))) {
2802 ; /* can't hide while stuck in a non-pit trap */
2803 } else if (mtmp->data->mlet == S_EEL) {
2804 undetected = (is_pool(x, y) && !Is_waterlevel(&u.uz));
2805 } else if (hides_under(mtmp->data) && OBJ_AT(x, y)) {
2806 struct obj *otmp = level.objects[x][y];
2808 /* most monsters won't hide under cockatrice corpse */
2809 if (otmp->nexthere || otmp->otyp != CORPSE
2810 || (mtmp == &youmonst ? Stone_resistance : resists_ston(mtmp))
2811 || !touch_petrifies(&mons[otmp->corpsenm]))
2812 undetected = TRUE;
2815 if (is_u)
2816 u.uundetected = undetected;
2817 else
2818 mtmp->mundetected = undetected;
2819 return undetected;
2822 /* called when returning to a previously visited level */
2823 void
2824 hide_monst(mon)
2825 struct monst *mon;
2827 boolean hider_under = hides_under(mon->data) || mon->data->mlet == S_EEL;
2829 if ((is_hider(mon->data) || hider_under)
2830 && !(mon->mundetected || mon->m_ap_type)) {
2831 xchar x = mon->mx, y = mon->my;
2832 char save_viz = viz_array[y][x];
2834 /* override vision, forcing hero to be unable to see monster's spot */
2835 viz_array[y][x] &= ~(IN_SIGHT | COULD_SEE);
2836 if (is_hider(mon->data))
2837 (void) restrap(mon);
2838 /* try again if mimic missed its 1/3 chance to hide */
2839 if (mon->data->mlet == S_MIMIC && !mon->m_ap_type)
2840 (void) restrap(mon);
2841 if (hider_under)
2842 (void) hideunder(mon);
2843 viz_array[y][x] = save_viz;
2847 static short *animal_list = 0; /* list of PM values for animal monsters */
2848 static int animal_list_count;
2850 void
2851 mon_animal_list(construct)
2852 boolean construct;
2854 if (construct) {
2855 short animal_temp[SPECIAL_PM];
2856 int i, n;
2858 /* if (animal_list) impossible("animal_list already exists"); */
2860 for (n = 0, i = LOW_PM; i < SPECIAL_PM; i++)
2861 if (is_animal(&mons[i]))
2862 animal_temp[n++] = i;
2863 /* if (n == 0) animal_temp[n++] = NON_PM; */
2865 animal_list = (short *) alloc(n * sizeof *animal_list);
2866 (void) memcpy((genericptr_t) animal_list, (genericptr_t) animal_temp,
2867 n * sizeof *animal_list);
2868 animal_list_count = n;
2869 } else { /* release */
2870 if (animal_list)
2871 free((genericptr_t) animal_list), animal_list = 0;
2872 animal_list_count = 0;
2876 STATIC_OVL int
2877 pick_animal()
2879 int res;
2881 if (!animal_list)
2882 mon_animal_list(TRUE);
2884 res = animal_list[rn2(animal_list_count)];
2885 /* rogue level should use monsters represented by uppercase letters
2886 only, but since chameleons aren't generated there (not uppercase!)
2887 we don't perform a lot of retries */
2888 if (Is_rogue_level(&u.uz) && !isupper((uchar) mons[res].mlet))
2889 res = animal_list[rn2(animal_list_count)];
2890 return res;
2893 void
2894 decide_to_shapeshift(mon, shiftflags)
2895 struct monst *mon;
2896 int shiftflags;
2898 struct permonst *ptr;
2899 unsigned was_female = mon->female;
2900 boolean msg = FALSE;
2902 if ((shiftflags & SHIFT_MSG)
2903 || ((shiftflags & SHIFT_SEENMSG) && sensemon(mon)))
2904 msg = TRUE;
2906 if (!is_vampshifter(mon)) {
2907 /* regular shapeshifter */
2908 if (!rn2(6))
2909 (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
2910 } else {
2911 /* The vampire has to be in good health (mhp) to maintain
2912 * its shifted form.
2914 * If we're shifted and getting low on hp, maybe shift back.
2915 * If we're not already shifted and in good health, maybe shift.
2917 if (mon->data->mlet != S_VAMPIRE) {
2918 if ((mon->mhp <= (mon->mhpmax + 5) / 6) && rn2(4)
2919 && mon->cham >= LOW_PM)
2920 (void) newcham(mon, &mons[mon->cham], FALSE, msg);
2921 } else {
2922 if (mon->mhp >= 9 * mon->mhpmax / 10 && !rn2(6)
2923 && (!canseemon(mon)
2924 || distu(mon->mx, mon->my) > BOLT_LIM * BOLT_LIM))
2925 (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
2927 /* override the 10% chance for sex change */
2928 ptr = mon->data;
2929 if (!is_male(ptr) && !is_female(ptr) && !is_neuter(ptr))
2930 mon->female = was_female;
2934 STATIC_OVL int
2935 pickvampshape(mon)
2936 struct monst *mon;
2938 int mndx = mon->cham, wolfchance = 10;
2939 /* avoid picking monsters with lowercase display symbols ('d' for wolf
2940 and 'v' for fog cloud) on rogue level*/
2941 boolean uppercase_only = Is_rogue_level(&u.uz);
2943 switch (mndx) {
2944 case PM_VLAD_THE_IMPALER:
2945 /* ensure Vlad can keep carrying the Candelabrum */
2946 if (mon_has_special(mon))
2947 break; /* leave mndx as is */
2948 wolfchance = 3;
2949 /*FALLTHRU*/
2950 case PM_VAMPIRE_LORD: /* vampire lord or Vlad can become wolf */
2951 if (!rn2(wolfchance) && !uppercase_only) {
2952 mndx = PM_WOLF;
2953 break;
2955 /*FALLTHRU*/
2956 case PM_VAMPIRE: /* any vampire can become fog or bat */
2957 mndx = (!rn2(4) && !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
2958 break;
2960 return mndx;
2963 /* nonshapechangers who warrant special polymorph handling */
2964 STATIC_OVL boolean
2965 isspecmon(mon)
2966 struct monst *mon;
2968 return (mon->isshk || mon->ispriest || mon->isgd
2969 || mon->m_id == quest_status.leader_m_id);
2972 /* restrict certain special monsters (shopkeepers, aligned priests,
2973 vault guards) to forms that allow them to behave sensibly (catching
2974 gold, speaking?) so that they don't need too much extra code */
2975 STATIC_OVL boolean
2976 validspecmon(mon, mndx)
2977 struct monst *mon;
2978 int mndx;
2980 if (mndx == NON_PM)
2981 return TRUE; /* caller wants random */
2983 if (!accept_newcham_form(mndx))
2984 return FALSE; /* geno'd or !polyok */
2986 if (isspecmon(mon)) {
2987 struct permonst *ptr = &mons[mndx];
2989 /* reject notake because object manipulation is expected
2990 and nohead because speech capability is expected */
2991 if (notake(ptr) || !has_head(ptr))
2992 return FALSE;
2993 /* [should we check ptr->msound here too?] */
2995 return TRUE; /* potential new form is ok */
2998 /* prevent wizard mode user from specifying invalid vampshifter shape */
2999 STATIC_OVL boolean
3000 validvamp(mon, mndx_p, monclass)
3001 struct monst *mon;
3002 int *mndx_p, monclass;
3004 /* simplify caller's usage */
3005 if (!is_vampshifter(mon))
3006 return validspecmon(mon, *mndx_p);
3008 if (*mndx_p == PM_VAMPIRE || *mndx_p == PM_VAMPIRE_LORD
3009 || *mndx_p == PM_VLAD_THE_IMPALER) {
3010 /* player picked some type of vampire; use mon's self */
3011 *mndx_p = mon->cham;
3012 return TRUE;
3014 if (mon->cham == PM_VLAD_THE_IMPALER && mon_has_special(mon)) {
3015 /* Vlad with Candelabrum; override choice, then accept it */
3016 *mndx_p = PM_VLAD_THE_IMPALER;
3017 return TRUE;
3019 /* basic vampires can't become wolves; any can become fog or bat
3020 (we don't enforce upper-case only for rogue level here) */
3021 if (*mndx_p == PM_WOLF)
3022 return (boolean) (mon->cham != PM_VAMPIRE);
3023 if (*mndx_p == PM_FOG_CLOUD || *mndx_p == PM_VAMPIRE_BAT)
3024 return TRUE;
3026 /* if we get here, specific type was no good; try by class */
3027 switch (monclass) {
3028 case S_VAMPIRE:
3029 *mndx_p = mon->cham;
3030 break;
3031 case S_BAT:
3032 *mndx_p = PM_VAMPIRE_BAT;
3033 break;
3034 case S_VORTEX:
3035 *mndx_p = PM_FOG_CLOUD;
3036 break;
3037 case S_DOG:
3038 if (mon->cham != PM_VAMPIRE) {
3039 *mndx_p = PM_WOLF;
3040 break;
3042 /*FALLTHRU*/
3043 default:
3044 *mndx_p = NON_PM;
3045 break;
3047 return (boolean) (*mndx_p != NON_PM);
3051 select_newcham_form(mon)
3052 struct monst *mon;
3054 int mndx = NON_PM, tryct;
3056 switch (mon->cham) {
3057 case PM_SANDESTIN:
3058 if (rn2(7))
3059 mndx = pick_nasty();
3060 break;
3061 case PM_DOPPELGANGER:
3062 if (!rn2(7)) {
3063 mndx = pick_nasty();
3064 } else if (rn2(3)) { /* role monsters */
3065 mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
3066 } else if (!rn2(3)) { /* quest guardians */
3067 mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
3068 /* avoid own role's guardian */
3069 if (mndx == urole.guardnum)
3070 mndx = NON_PM;
3071 } else { /* general humanoids */
3072 tryct = 5;
3073 do {
3074 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3075 if (humanoid(&mons[mndx]) && polyok(&mons[mndx]))
3076 break;
3077 } while (--tryct > 0);
3078 if (!tryct)
3079 mndx = NON_PM;
3081 break;
3082 case PM_CHAMELEON:
3083 if (!rn2(3))
3084 mndx = pick_animal();
3085 break;
3086 case PM_VLAD_THE_IMPALER:
3087 case PM_VAMPIRE_LORD:
3088 case PM_VAMPIRE:
3089 mndx = pickvampshape(mon);
3090 break;
3091 case NON_PM: /* ordinary */
3093 struct obj *m_armr = which_armor(mon, W_ARM);
3095 if (m_armr && Is_dragon_scales(m_armr))
3096 mndx = (int) (Dragon_scales_to_pm(m_armr) - mons);
3097 else if (m_armr && Is_dragon_mail(m_armr))
3098 mndx = (int) (Dragon_mail_to_pm(m_armr) - mons);
3100 break;
3103 /* for debugging: allow control of polymorphed monster */
3104 if (wizard && iflags.mon_polycontrol) {
3105 char pprompt[BUFSZ], buf[BUFSZ];
3106 int monclass;
3108 Sprintf(pprompt, "Change %s @ %s into what kind of monster?",
3109 noit_mon_nam(mon),
3110 coord_desc((int) mon->mx, (int) mon->my, buf,
3111 (iflags.getpos_coords != GPCOORDS_NONE)
3112 ? iflags.getpos_coords : GPCOORDS_MAP));
3113 tryct = 5;
3114 do {
3115 monclass = 0;
3116 getlin(pprompt, buf);
3117 mungspaces(buf);
3118 /* for ESC, take form selected above (might be NON_PM) */
3119 if (*buf == '\033')
3120 break;
3121 /* for "*", use NON_PM to pick an arbitrary shape below */
3122 if (!strcmp(buf, "*") || !strcmp(buf, "random")) {
3123 mndx = NON_PM;
3124 break;
3126 mndx = name_to_mon(buf);
3127 if (mndx == NON_PM) {
3128 /* didn't get a type, so check whether it's a class
3129 (single letter or text match with def_monsyms[]) */
3130 monclass = name_to_monclass(buf, &mndx);
3131 if (monclass && mndx == NON_PM)
3132 mndx = mkclass_poly(monclass);
3134 if (mndx >= LOW_PM) {
3135 /* got a specific type of monster; use it if we can */
3136 if (validvamp(mon, &mndx, monclass))
3137 break;
3138 /* can't; revert to random in case we exhaust tryct */
3139 mndx = NON_PM;
3142 pline("It can't become that.");
3143 } while (--tryct > 0);
3144 if (!tryct)
3145 pline1(thats_enough_tries);
3146 if (is_vampshifter(mon) && !validvamp(mon, &mndx, monclass))
3147 mndx = pickvampshape(mon); /* don't resort to arbitrary */
3150 /* if no form was specified above, pick one at random now */
3151 if (mndx == NON_PM) {
3152 tryct = 50;
3153 do {
3154 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3155 } while (--tryct > 0 && !validspecmon(mon, mndx)
3156 /* try harder to select uppercase monster on rogue level */
3157 && (tryct > 40 && Is_rogue_level(&u.uz)
3158 && !isupper((uchar) mons[mndx].mlet)));
3160 return mndx;
3163 /* this used to be inline within newcham() but monpolycontrol needs it too */
3164 STATIC_OVL struct permonst *
3165 accept_newcham_form(mndx)
3166 int mndx;
3168 struct permonst *mdat;
3170 if (mndx == NON_PM)
3171 return 0;
3172 mdat = &mons[mndx];
3173 if ((mvitals[mndx].mvflags & G_GENOD) != 0)
3174 return 0;
3175 if (is_placeholder(mdat))
3176 return 0;
3177 /* select_newcham_form() might deliberately pick a player
3178 character type (random selection never does) which
3179 polyok() rejects, so we need a special case here */
3180 if (is_mplayer(mdat))
3181 return mdat;
3182 /* polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops */
3183 return polyok(mdat) ? mdat : 0;
3186 void
3187 mgender_from_permonst(mtmp, mdat)
3188 struct monst *mtmp;
3189 struct permonst *mdat;
3191 if (is_male(mdat)) {
3192 if (mtmp->female)
3193 mtmp->female = FALSE;
3194 } else if (is_female(mdat)) {
3195 if (!mtmp->female)
3196 mtmp->female = TRUE;
3197 } else if (!is_neuter(mdat)) {
3198 if (!rn2(10))
3199 mtmp->female = !mtmp->female;
3203 /* make a chameleon take on another shape, or a polymorph target
3204 (possibly self-inflicted) become a different monster;
3205 returns 1 if it actually changes form */
3207 newcham(mtmp, mdat, polyspot, msg)
3208 struct monst *mtmp;
3209 struct permonst *mdat;
3210 boolean polyspot; /* change is the result of wand or spell of polymorph */
3211 boolean msg; /* "The oldmon turns into a newmon!" */
3213 int hpn, hpd;
3214 int mndx, tryct;
3215 struct permonst *olddata = mtmp->data;
3216 char *p, oldname[BUFSZ], l_oldname[BUFSZ], newname[BUFSZ];
3218 /* Riders are immune to polymorph and green slime
3219 (but apparent Rider might actually be a doppelganger) */
3220 if (mtmp->cham == NON_PM) { /* not a shapechanger */
3221 if (is_rider(olddata))
3222 return 0;
3223 /* make Nazgul and erinyes immune too, to reduce chance of
3224 anomalous extinction feedback during final disclsoure */
3225 if (mbirth_limit(monsndx(olddata)) < MAXMONNO)
3226 return 0;
3229 if (msg) {
3230 /* like Monnam() but never mention saddle */
3231 Strcpy(oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3232 SUPPRESS_SADDLE, FALSE));
3233 oldname[0] = highc(oldname[0]);
3235 /* we need this one whether msg is true or not */
3236 Strcpy(l_oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3237 has_mname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE));
3239 /* mdat = 0 -> caller wants a random monster shape */
3240 if (mdat == 0) {
3241 /* select_newcham_form() loops when resorting to random but
3242 it doesn't always pick that so we still retry here too */
3243 tryct = 20;
3244 do {
3245 mndx = select_newcham_form(mtmp);
3246 mdat = accept_newcham_form(mndx);
3247 /* for the first several tries we require upper-case on
3248 the rogue level (after that, we take whatever we get) */
3249 if (tryct > 15 && Is_rogue_level(&u.uz)
3250 && mdat && !isupper((uchar) mdat->mlet))
3251 mdat = 0;
3252 if (mdat)
3253 break;
3254 } while (--tryct > 0);
3255 if (!tryct)
3256 return 0;
3257 } else if (mvitals[monsndx(mdat)].mvflags & G_GENOD)
3258 return 0; /* passed in mdat is genocided */
3260 if (mdat == olddata)
3261 return 0; /* still the same monster */
3263 mgender_from_permonst(mtmp, mdat);
3264 /* Endgame mplayers start out as "Foo the Bar", but some of the
3265 * titles are inappropriate when polymorphed, particularly into
3266 * the opposite sex. Player characters don't use ranks when
3267 * polymorphed, so dropping rank for mplayers seems reasonable.
3269 if (In_endgame(&u.uz) && is_mplayer(olddata)
3270 && has_mname(mtmp) && (p = strstr(MNAME(mtmp), " the ")) != 0)
3271 *p = '\0';
3273 if (mtmp->wormno) { /* throw tail away */
3274 wormgone(mtmp);
3275 place_monster(mtmp, mtmp->mx, mtmp->my);
3277 if (mtmp->m_ap_type && mdat->mlet != S_MIMIC)
3278 seemimic(mtmp); /* revert to normal monster */
3280 /* (this code used to try to adjust the monster's health based on
3281 a normal one of its type but there are too many special cases
3282 which need to handled in order to do that correctly, so just
3283 give the new form the same proportion of HP as its old one had) */
3284 hpn = mtmp->mhp;
3285 hpd = mtmp->mhpmax;
3286 /* set level and hit points */
3287 newmonhp(mtmp, monsndx(mdat));
3288 /* new hp: same fraction of max as before */
3289 #ifndef LINT
3290 mtmp->mhp = (int) (((long) hpn * (long) mtmp->mhp) / (long) hpd);
3291 #endif
3292 /* sanity check (potential overflow) */
3293 if (mtmp->mhp < 0 || mtmp->mhp > mtmp->mhpmax)
3294 mtmp->mhp = mtmp->mhpmax;
3295 /* unlikely but not impossible; a 1HD creature with 1HP that changes
3296 into a 0HD creature will require this statement */
3297 if (!mtmp->mhp)
3298 mtmp->mhp = 1;
3300 /* take on the new form... */
3301 set_mon_data(mtmp, mdat, 0);
3303 if (emits_light(olddata) != emits_light(mtmp->data)) {
3304 /* used to give light, now doesn't, or vice versa,
3305 or light's range has changed */
3306 if (emits_light(olddata))
3307 del_light_source(LS_MONSTER, monst_to_any(mtmp));
3308 if (emits_light(mtmp->data))
3309 new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
3310 LS_MONSTER, monst_to_any(mtmp));
3312 if (!mtmp->perminvis || pm_invisible(olddata))
3313 mtmp->perminvis = pm_invisible(mdat);
3314 mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
3315 if (mtmp->mundetected)
3316 (void) hideunder(mtmp);
3317 if (u.ustuck == mtmp) {
3318 if (u.uswallow) {
3319 if (!attacktype(mdat, AT_ENGL)) {
3320 /* Does mdat care? */
3321 if (!noncorporeal(mdat) && !amorphous(mdat)
3322 && !is_whirly(mdat) && (mdat != &mons[PM_YELLOW_LIGHT])) {
3323 char msgtrail[BUFSZ];
3325 if (is_vampshifter(mtmp)) {
3326 Sprintf(msgtrail, " which was a shapeshifted %s",
3327 m_monnam(mtmp));
3328 } else if (is_animal(mdat)) {
3329 Strcpy(msgtrail, "'s stomach");
3330 } else {
3331 msgtrail[0] = '\0';
3334 /* Do this even if msg is FALSE */
3335 You("%s %s%s!",
3336 (amorphous(olddata) || is_whirly(olddata))
3337 ? "emerge from" : "break out of",
3338 l_oldname, msgtrail);
3339 msg = FALSE; /* message has been given */
3340 mtmp->mhp = 1; /* almost dead */
3342 expels(mtmp, olddata, FALSE);
3343 } else {
3344 /* update swallow glyphs for new monster */
3345 swallowed(0);
3347 } else if (!sticks(mdat) && !sticks(youmonst.data))
3348 unstuck(mtmp);
3351 #ifndef DCC30_BUG
3352 if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
3353 #else
3354 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
3355 * same expression.
3357 if (mdat == &mons[PM_LONG_WORM]
3358 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
3359 #endif
3360 /* we can now create worms with tails - 11/91 */
3361 initworm(mtmp, rn2(5));
3362 if (count_wsegs(mtmp))
3363 place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
3366 newsym(mtmp->mx, mtmp->my);
3368 if (msg) {
3369 char *save_mname = 0;
3371 if (has_mname(mtmp)) {
3372 save_mname = MNAME(mtmp);
3373 MNAME(mtmp) = (char *) 0;
3375 Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3376 ? "slime"
3377 : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3378 SUPPRESS_SADDLE, FALSE));
3379 /* oldname was capitalized above; newname will be lower case */
3380 if (!strcmpi(newname, "it")) { /* can't see or sense it now */
3381 if (!!strcmpi(oldname, "it")) /* could see or sense it before */
3382 pline("%s disappears!", oldname);
3383 (void) usmellmon(mdat);
3384 } else { /* can see or sense it now */
3385 if (!strcmpi(oldname, "it")) /* couldn't see or sense it before */
3386 pline("%s appears!", upstart(newname));
3387 else
3388 pline("%s turns into %s!", oldname, newname);
3390 if (save_mname)
3391 MNAME(mtmp) = save_mname;
3394 /* when polymorph trap/wand/potion produces a vampire, turn in into
3395 a full-fledged vampshifter unless shape-changing is blocked */
3396 if (mtmp->cham == NON_PM && mdat->mlet == S_VAMPIRE
3397 && !Protection_from_shape_changers)
3398 mtmp->cham = pm_to_cham(monsndx(mdat));
3400 possibly_unwield(mtmp, polyspot); /* might lose use of weapon */
3401 mon_break_armor(mtmp, polyspot);
3402 if (!(mtmp->misc_worn_check & W_ARMG))
3403 mselftouch(mtmp, "No longer petrify-resistant, ",
3404 !context.mon_moving);
3405 m_dowear(mtmp, FALSE);
3407 /* This ought to re-test can_carry() on each item in the inventory
3408 * rather than just checking ex-giants & boulders, but that'd be
3409 * pretty expensive to perform. If implemented, then perhaps
3410 * minvent should be sorted in order to drop heaviest items first.
3412 /* former giants can't continue carrying boulders */
3413 if (mtmp->minvent && !throws_rocks(mdat)) {
3414 register struct obj *otmp, *otmp2;
3416 for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
3417 otmp2 = otmp->nobj;
3418 if (otmp->otyp == BOULDER) {
3419 /* this keeps otmp from being polymorphed in the
3420 same zap that the monster that held it is polymorphed */
3421 if (polyspot)
3422 bypass_obj(otmp);
3423 obj_extract_self(otmp);
3424 /* probably ought to give some "drop" message here */
3425 if (flooreffects(otmp, mtmp->mx, mtmp->my, ""))
3426 continue;
3427 place_object(otmp, mtmp->mx, mtmp->my);
3432 return 1;
3435 /* sometimes an egg will be special */
3436 #define BREEDER_EGG (!rn2(77))
3439 * Determine if the given monster number can be hatched from an egg.
3440 * Return the monster number to use as the egg's corpsenm. Return
3441 * NON_PM if the given monster can't be hatched.
3444 can_be_hatched(mnum)
3445 int mnum;
3447 /* ranger quest nemesis has the oviparous bit set, making it
3448 be possible to wish for eggs of that unique monster; turn
3449 such into ordinary eggs rather than forbidding them outright */
3450 if (mnum == PM_SCORPIUS)
3451 mnum = PM_SCORPION;
3453 mnum = little_to_big(mnum);
3455 * Queen bees lay killer bee eggs (usually), but killer bees don't
3456 * grow into queen bees. Ditto for [winged-]gargoyles.
3458 if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE
3459 || (lays_eggs(&mons[mnum])
3460 && (BREEDER_EGG
3461 || (mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
3462 return mnum;
3463 return NON_PM;
3466 /* type of egg laid by #sit; usually matches parent */
3468 egg_type_from_parent(mnum, force_ordinary)
3469 int mnum; /* parent monster; caller must handle lays_eggs() check */
3470 boolean force_ordinary;
3472 if (force_ordinary || !BREEDER_EGG) {
3473 if (mnum == PM_QUEEN_BEE)
3474 mnum = PM_KILLER_BEE;
3475 else if (mnum == PM_WINGED_GARGOYLE)
3476 mnum = PM_GARGOYLE;
3478 return mnum;
3481 /* decide whether an egg of the indicated monster type is viable;
3482 also used to determine whether an egg or tin can be created... */
3483 boolean
3484 dead_species(m_idx, egg)
3485 int m_idx;
3486 boolean egg;
3488 int alt_idx;
3490 /* generic eggs are unhatchable and have corpsenm of NON_PM */
3491 if (m_idx < LOW_PM)
3492 return TRUE;
3494 * For monsters with both baby and adult forms, genociding either
3495 * form kills all eggs of that monster. Monsters with more than
3496 * two forms (small->large->giant mimics) are more or less ignored;
3497 * fortunately, none of them have eggs. Species extinction due to
3498 * overpopulation does not kill eggs.
3500 alt_idx = egg ? big_to_little(m_idx) : m_idx;
3501 return (boolean) ((mvitals[m_idx].mvflags & G_GENOD) != 0
3502 || (mvitals[alt_idx].mvflags & G_GENOD) != 0);
3505 /* kill off any eggs of genocided monsters */
3506 STATIC_OVL void
3507 kill_eggs(obj_list)
3508 struct obj *obj_list;
3510 struct obj *otmp;
3512 for (otmp = obj_list; otmp; otmp = otmp->nobj)
3513 if (otmp->otyp == EGG) {
3514 if (dead_species(otmp->corpsenm, TRUE)) {
3516 * It seems we could also just catch this when
3517 * it attempted to hatch, so we wouldn't have to
3518 * search all of the objlists.. or stop all
3519 * hatch timers based on a corpsenm.
3521 kill_egg(otmp);
3523 #if 0 /* not used */
3524 } else if (otmp->otyp == TIN) {
3525 if (dead_species(otmp->corpsenm, FALSE))
3526 otmp->corpsenm = NON_PM; /* empty tin */
3527 } else if (otmp->otyp == CORPSE) {
3528 if (dead_species(otmp->corpsenm, FALSE))
3529 ; /* not yet implemented... */
3530 #endif
3531 } else if (Has_contents(otmp)) {
3532 kill_eggs(otmp->cobj);
3536 /* kill all members of genocided species */
3537 void
3538 kill_genocided_monsters()
3540 struct monst *mtmp, *mtmp2;
3541 boolean kill_cham;
3542 int mndx;
3545 * Called during genocide, and again upon level change. The latter
3546 * catches up with any migrating monsters as they finally arrive at
3547 * their intended destinations, so possessions get deposited there.
3549 * Chameleon handling:
3550 * 1) if chameleons have been genocided, destroy them
3551 * regardless of current form;
3552 * 2) otherwise, force every chameleon which is imitating
3553 * any genocided species to take on a new form.
3555 for (mtmp = fmon; mtmp; mtmp = mtmp2) {
3556 mtmp2 = mtmp->nmon;
3557 if (DEADMONSTER(mtmp))
3558 continue;
3559 mndx = monsndx(mtmp->data);
3560 kill_cham = (mtmp->cham >= LOW_PM
3561 && (mvitals[mtmp->cham].mvflags & G_GENOD));
3562 if ((mvitals[mndx].mvflags & G_GENOD) || kill_cham) {
3563 if (mtmp->cham >= LOW_PM && !kill_cham)
3564 (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
3565 else
3566 mondead(mtmp);
3568 if (mtmp->minvent)
3569 kill_eggs(mtmp->minvent);
3572 kill_eggs(invent);
3573 kill_eggs(fobj);
3574 kill_eggs(migrating_objs);
3575 kill_eggs(level.buriedobjlist);
3578 void
3579 golemeffects(mon, damtype, dam)
3580 register struct monst *mon;
3581 int damtype, dam;
3583 int heal = 0, slow = 0;
3585 if (mon->data == &mons[PM_FLESH_GOLEM]) {
3586 if (damtype == AD_ELEC)
3587 heal = (dam + 5) / 6;
3588 else if (damtype == AD_FIRE || damtype == AD_COLD)
3589 slow = 1;
3590 } else if (mon->data == &mons[PM_IRON_GOLEM]) {
3591 if (damtype == AD_ELEC)
3592 slow = 1;
3593 else if (damtype == AD_FIRE)
3594 heal = dam;
3595 } else {
3596 return;
3598 if (slow) {
3599 if (mon->mspeed != MSLOW)
3600 mon_adjust_speed(mon, -1, (struct obj *) 0);
3602 if (heal) {
3603 if (mon->mhp < mon->mhpmax) {
3604 mon->mhp += heal;
3605 if (mon->mhp > mon->mhpmax)
3606 mon->mhp = mon->mhpmax;
3607 if (cansee(mon->mx, mon->my))
3608 pline("%s seems healthier.", Monnam(mon));
3613 boolean
3614 angry_guards(silent)
3615 boolean silent;
3617 struct monst *mtmp;
3618 int ct = 0, nct = 0, sct = 0, slct = 0;
3620 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3621 if (DEADMONSTER(mtmp))
3622 continue;
3623 if (is_watch(mtmp->data) && mtmp->mpeaceful) {
3624 ct++;
3625 if (cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
3626 if (distu(mtmp->mx, mtmp->my) == 2)
3627 nct++;
3628 else
3629 sct++;
3631 if (mtmp->msleeping || mtmp->mfrozen) {
3632 slct++;
3633 mtmp->msleeping = mtmp->mfrozen = 0;
3635 mtmp->mpeaceful = 0;
3638 if (ct) {
3639 if (!silent) { /* do we want pline msgs? */
3640 if (slct)
3641 pline_The("guard%s wake%s up!", slct > 1 ? "s" : "",
3642 slct == 1 ? "s" : "");
3643 if (nct || sct) {
3644 if (nct)
3645 pline_The("guard%s get%s angry!", nct == 1 ? "" : "s",
3646 nct == 1 ? "s" : "");
3647 else if (!Blind)
3648 You_see("%sangry guard%s approaching!",
3649 sct == 1 ? "an " : "", sct > 1 ? "s" : "");
3650 } else
3651 You_hear("the shrill sound of a guard's whistle.");
3653 return TRUE;
3655 return FALSE;
3658 void
3659 pacify_guards()
3661 struct monst *mtmp;
3663 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3664 if (DEADMONSTER(mtmp))
3665 continue;
3666 if (is_watch(mtmp->data))
3667 mtmp->mpeaceful = 1;
3671 void
3672 mimic_hit_msg(mtmp, otyp)
3673 struct monst *mtmp;
3674 short otyp;
3676 short ap = mtmp->mappearance;
3678 switch (mtmp->m_ap_type) {
3679 case M_AP_NOTHING:
3680 case M_AP_FURNITURE:
3681 case M_AP_MONSTER:
3682 break;
3683 case M_AP_OBJECT:
3684 if (otyp == SPE_HEALING || otyp == SPE_EXTRA_HEALING) {
3685 pline("%s seems a more vivid %s than before.",
3686 The(simple_typename(ap)),
3687 c_obj_colors[objects[ap].oc_color]);
3689 break;
3693 boolean
3694 usmellmon(mdat)
3695 struct permonst *mdat;
3697 int mndx;
3698 boolean nonspecific = FALSE;
3699 boolean msg_given = FALSE;
3701 if (mdat) {
3702 if (!olfaction(youmonst.data))
3703 return FALSE;
3704 mndx = monsndx(mdat);
3705 switch (mndx) {
3706 case PM_ROTHE:
3707 case PM_MINOTAUR:
3708 You("notice a bovine smell.");
3709 msg_given = TRUE;
3710 break;
3711 case PM_CAVEMAN:
3712 case PM_CAVEWOMAN:
3713 case PM_BARBARIAN:
3714 case PM_NEANDERTHAL:
3715 You("smell body odor.");
3716 msg_given = TRUE;
3717 break;
3719 case PM_PESTILENCE:
3720 case PM_FAMINE:
3721 case PM_DEATH:
3722 break;
3724 case PM_HORNED_DEVIL:
3725 case PM_BALROG:
3726 case PM_ASMODEUS:
3727 case PM_DISPATER:
3728 case PM_YEENOGHU:
3729 case PM_ORCUS:
3730 break;
3731 case PM_HUMAN_WEREJACKAL:
3732 case PM_HUMAN_WERERAT:
3733 case PM_HUMAN_WEREWOLF:
3734 case PM_WEREJACKAL:
3735 case PM_WERERAT:
3736 case PM_WEREWOLF:
3737 case PM_OWLBEAR:
3738 You("detect an odor reminiscent of an animal's den.");
3739 msg_given = TRUE;
3740 break;
3742 case PM_PURPLE_WORM:
3743 break;
3745 case PM_STEAM_VORTEX:
3746 You("smell steam.");
3747 msg_given = TRUE;
3748 break;
3749 case PM_GREEN_SLIME:
3750 pline("%s stinks.", Something);
3751 msg_given = TRUE;
3752 break;
3753 case PM_VIOLET_FUNGUS:
3754 case PM_SHRIEKER:
3755 You("smell mushrooms.");
3756 msg_given = TRUE;
3757 break;
3758 /* These are here to avoid triggering the
3759 nonspecific treatment through the default case below*/
3760 case PM_WHITE_UNICORN:
3761 case PM_GRAY_UNICORN:
3762 case PM_BLACK_UNICORN:
3763 case PM_JELLYFISH:
3764 break;
3765 default:
3766 nonspecific = TRUE;
3767 break;
3770 if (nonspecific)
3771 switch (mdat->mlet) {
3772 case S_DOG:
3773 You("notice a dog smell.");
3774 msg_given = TRUE;
3775 break;
3776 case S_DRAGON:
3777 You("smell a dragon!");
3778 msg_given = TRUE;
3779 break;
3780 case S_FUNGUS:
3781 pline("%s smells moldy.", Something);
3782 msg_given = TRUE;
3783 break;
3784 case S_UNICORN:
3785 You("detect a%s odor reminiscent of a stable.",
3786 (mndx == PM_PONY) ? "n" : " strong");
3787 msg_given = TRUE;
3788 break;
3789 case S_ZOMBIE:
3790 You("smell rotting flesh.");
3791 msg_given = TRUE;
3792 break;
3793 case S_EEL:
3794 You("smell fish.");
3795 msg_given = TRUE;
3796 break;
3797 case S_ORC:
3798 if (maybe_polyd(is_orc(youmonst.data), Race_if(PM_ORC)))
3799 You("notice an attractive smell.");
3800 else
3801 pline("A foul stench makes you feel a little nauseated.");
3802 msg_given = TRUE;
3803 break;
3804 default:
3805 break;
3808 return msg_given ? TRUE : FALSE;
3811 /*mon.c*/