Iron bars, water and lava are not interesting for travel
[aNetHack.git] / src / mon.c
blobe31ecb27bb28b7cd7f1a3dc9d40e45726ab41ba2
1 /* NetHack 3.6 mon.c $NHDT-Date: 1466289475 2016/06/18 22:37:55 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.227 $ */
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 struct attack *dummy = &mtmp->data->mattk[0];
519 const char *how = on_fire(mtmp->data, dummy);
521 pline("%s %s.", Monnam(mtmp),
522 !strcmp(how, "boiling") ? "boils away"
523 : !strcmp(how, "melting") ? "melts away"
524 : "burns to a crisp");
526 mondead(mtmp);
527 } else {
528 mtmp->mhp -= 1;
529 if (mtmp->mhp < 1) {
530 if (cansee(mtmp->mx, mtmp->my))
531 pline("%s surrenders to the fire.", Monnam(mtmp));
532 mondead(mtmp);
533 } else if (cansee(mtmp->mx, mtmp->my))
534 pline("%s burns slightly.", Monnam(mtmp));
536 if (mtmp->mhp > 0) {
537 (void) fire_damage_chain(mtmp->minvent, FALSE, FALSE,
538 mtmp->mx, mtmp->my);
539 (void) rloc(mtmp, FALSE);
540 return 0;
542 return 1;
544 } else if (inpool) {
545 /* Most monsters drown in pools. flooreffects() will take care of
546 * water damage to dead monsters' inventory, but survivors need to
547 * be handled here. Swimmers are able to protect their stuff...
549 if (!is_clinger(mtmp->data) && !is_swimmer(mtmp->data)
550 && !amphibious(mtmp->data)) {
551 if (cansee(mtmp->mx, mtmp->my)) {
552 pline("%s drowns.", Monnam(mtmp));
554 if (u.ustuck && u.uswallow && u.ustuck == mtmp) {
555 /* This can happen after a purple worm plucks you off a
556 flying steed while you are over water. */
557 pline("%s sinks as %s rushes in and flushes you out.",
558 Monnam(mtmp), hliquid("water"));
560 mondead(mtmp);
561 if (mtmp->mhp > 0) {
562 water_damage_chain(mtmp->minvent, FALSE);
563 (void) rloc(mtmp, FALSE);
564 return 0;
566 return 1;
568 } else {
569 /* but eels have a difficult time outside */
570 if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)) {
571 /* as mhp gets lower, the rate of further loss slows down */
572 if (mtmp->mhp > 1 && rn2(mtmp->mhp) > rn2(8))
573 mtmp->mhp--;
574 monflee(mtmp, 2, FALSE, FALSE);
577 return 0;
581 mcalcmove(mon)
582 struct monst *mon;
584 int mmove = mon->data->mmove;
586 /* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
587 * MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
588 * both adjustments have negligible effect on higher speeds.
590 if (mon->mspeed == MSLOW)
591 mmove = (2 * mmove + 1) / 3;
592 else if (mon->mspeed == MFAST)
593 mmove = (4 * mmove + 2) / 3;
595 if (mon == u.usteed) {
596 if (u.ugallop && context.mv) {
597 /* average movement is 1.50 times normal */
598 mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
600 } else if (mmove) {
601 /* vary movement points allocated to slightly reduce predictability;
602 random increment (avg +2) exceeds random decrement (avg +1) by
603 a small amount; normal speed monsters will occasionally get an
604 extra move and slow ones won't be quite as slow */
605 mmove += rn2(5) - rn2(3); /* + 0..4 - 0..2, average net +1 */
606 if (mmove < 1)
607 mmove = 1;
610 return mmove;
613 /* actions that happen once per ``turn'', regardless of each
614 individual monster's metabolism; some of these might need to
615 be reclassified to occur more in proportion with movement rate */
616 void
617 mcalcdistress()
619 struct monst *mtmp;
621 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
622 if (DEADMONSTER(mtmp))
623 continue;
625 /* must check non-moving monsters once/turn in case
626 * they managed to end up in liquid */
627 if (mtmp->data->mmove == 0) {
628 if (vision_full_recalc)
629 vision_recalc(0);
630 if (minliquid(mtmp))
631 continue;
634 /* regenerate hit points */
635 mon_regen(mtmp, FALSE);
637 /* possibly polymorph shapechangers and lycanthropes */
638 if (mtmp->cham >= LOW_PM)
639 decide_to_shapeshift(mtmp, (canspotmon(mtmp)
640 || (u.uswallow && mtmp == u.ustuck))
641 ? SHIFT_MSG : 0);
642 were_change(mtmp);
644 /* gradually time out temporary problems */
645 if (mtmp->mblinded && !--mtmp->mblinded)
646 mtmp->mcansee = 1;
647 if (mtmp->mfrozen && !--mtmp->mfrozen)
648 mtmp->mcanmove = 1;
649 if (mtmp->mfleetim && !--mtmp->mfleetim)
650 mtmp->mflee = 0;
652 /* FIXME: mtmp->mlstmv ought to be updated here */
657 movemon()
659 register struct monst *mtmp, *nmtmp;
660 register boolean somebody_can_move = FALSE;
663 * Some of you may remember the former assertion here that
664 * because of deaths and other actions, a simple one-pass
665 * algorithm wasn't possible for movemon. Deaths are no longer
666 * removed to the separate list fdmon; they are simply left in
667 * the chain with hit points <= 0, to be cleaned up at the end
668 * of the pass.
670 * The only other actions which cause monsters to be removed from
671 * the chain are level migrations and losedogs(). I believe losedogs()
672 * is a cleanup routine not associated with monster movements, and
673 * monsters can only affect level migrations on themselves, not others
674 * (hence the fetching of nmon before moving the monster). Currently,
675 * monsters can jump into traps, read cursed scrolls of teleportation,
676 * and drink cursed potions of raise level to change levels. These are
677 * all reflexive at this point. Should one monster be able to level
678 * teleport another, this scheme would have problems.
681 for (mtmp = fmon; mtmp; mtmp = nmtmp) {
682 /* end monster movement early if hero is flagged to leave the level */
683 if (u.utotype
684 #ifdef SAFERHANGUP
685 /* or if the program has lost contact with the user */
686 || program_state.done_hup
687 #endif
689 somebody_can_move = FALSE;
690 break;
692 nmtmp = mtmp->nmon;
693 /* one dead monster needs to perform a move after death:
694 vault guard whose temporary corridor is still on the map */
695 if (mtmp->isgd && !mtmp->mx && mtmp->mhp <= 0)
696 (void) gd_move(mtmp);
697 if (DEADMONSTER(mtmp))
698 continue;
700 /* Find a monster that we have not treated yet. */
701 if (mtmp->movement < NORMAL_SPEED)
702 continue;
704 mtmp->movement -= NORMAL_SPEED;
705 if (mtmp->movement >= NORMAL_SPEED)
706 somebody_can_move = TRUE;
708 if (vision_full_recalc)
709 vision_recalc(0); /* vision! */
711 /* reset obj bypasses before next monster moves */
712 if (context.bypasses)
713 clear_bypasses();
714 clear_splitobjs();
715 if (minliquid(mtmp))
716 continue;
718 if (is_hider(mtmp->data)) {
719 /* unwatched mimics and piercers may hide again [MRS] */
720 if (restrap(mtmp))
721 continue;
722 if (mtmp->m_ap_type == M_AP_FURNITURE
723 || mtmp->m_ap_type == M_AP_OBJECT)
724 continue;
725 if (mtmp->mundetected)
726 continue;
727 } else if (mtmp->data->mlet == S_EEL && !mtmp->mundetected
728 && (mtmp->mflee || distu(mtmp->mx, mtmp->my) > 2)
729 && !canseemon(mtmp) && !rn2(4)) {
730 /* some eels end up stuck in isolated pools, where they
731 can't--or at least won't--move, so they never reach
732 their post-move chance to re-hide */
733 if (hideunder(mtmp))
734 continue;
737 /* continue if the monster died fighting */
738 if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
739 /* Note:
740 * Conflict does not take effect in the first round.
741 * Therefore, A monster when stepping into the area will
742 * get to swing at you.
744 * The call to fightm() must be _last_. The monster might
745 * have died if it returns 1.
747 if (couldsee(mtmp->mx, mtmp->my)
748 && (distu(mtmp->mx, mtmp->my) <= BOLT_LIM * BOLT_LIM)
749 && fightm(mtmp))
750 continue; /* mon might have died */
752 if (dochugw(mtmp)) /* otherwise just move the monster */
753 continue;
756 if (any_light_source())
757 vision_full_recalc = 1; /* in case a mon moved with a light source */
758 /* reset obj bypasses after last monster has moved */
759 if (context.bypasses)
760 clear_bypasses();
761 clear_splitobjs();
762 /* remove dead monsters; dead vault guard will be left at <0,0>
763 if temporary corridor out of vault hasn't been removed yet */
764 dmonsfree();
766 /* a monster may have levteleported player -dlc */
767 if (u.utotype) {
768 deferred_goto();
769 /* changed levels, so these monsters are dormant */
770 somebody_can_move = FALSE;
773 return somebody_can_move;
776 #define mstoning(obj) \
777 (ofood(obj) && (touch_petrifies(&mons[(obj)->corpsenm]) \
778 || (obj)->corpsenm == PM_MEDUSA))
781 * Maybe eat a metallic object (not just gold).
782 * Return value: 0 => nothing happened, 1 => monster ate something,
783 * 2 => monster died (it must have grown into a genocided form, but
784 * that can't happen at present because nothing which eats objects
785 * has young and old forms).
788 meatmetal(mtmp)
789 register struct monst *mtmp;
791 register struct obj *otmp;
792 struct permonst *ptr;
793 int poly, grow, heal, mstone;
795 /* If a pet, eating is handled separately, in dog.c */
796 if (mtmp->mtame)
797 return 0;
799 /* Eats topmost metal object if it is there */
800 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp;
801 otmp = otmp->nexthere) {
802 /* Don't eat indigestible/choking/inappropriate objects */
803 if ((mtmp->data == &mons[PM_RUST_MONSTER] && !is_rustprone(otmp))
804 || (otmp->otyp == AMULET_OF_STRANGULATION)
805 || (otmp->otyp == RIN_SLOW_DIGESTION))
806 continue;
807 if (is_metallic(otmp) && !obj_resists(otmp, 5, 95)
808 && touch_artifact(otmp, mtmp)) {
809 if (mtmp->data == &mons[PM_RUST_MONSTER] && otmp->oerodeproof) {
810 if (canseemon(mtmp) && flags.verbose) {
811 pline("%s eats %s!", Monnam(mtmp),
812 distant_name(otmp, doname));
814 /* The object's rustproofing is gone now */
815 otmp->oerodeproof = 0;
816 mtmp->mstun = 1;
817 if (canseemon(mtmp) && flags.verbose) {
818 pline("%s spits %s out in disgust!", Monnam(mtmp),
819 distant_name(otmp, doname));
821 } else {
822 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
823 pline("%s eats %s!", Monnam(mtmp),
824 distant_name(otmp, doname));
825 else if (flags.verbose)
826 You_hear("a crunching sound.");
827 mtmp->meating = otmp->owt / 2 + 1;
828 /* Heal up to the object's weight in hp */
829 if (mtmp->mhp < mtmp->mhpmax) {
830 mtmp->mhp += objects[otmp->otyp].oc_weight;
831 if (mtmp->mhp > mtmp->mhpmax)
832 mtmp->mhp = mtmp->mhpmax;
834 if (otmp == uball) {
835 unpunish();
836 delobj(otmp);
837 } else if (otmp == uchain) {
838 unpunish(); /* frees uchain */
839 } else {
840 poly = polyfodder(otmp);
841 grow = mlevelgain(otmp);
842 heal = mhealup(otmp);
843 mstone = mstoning(otmp);
844 delobj(otmp);
845 ptr = mtmp->data;
846 if (poly) {
847 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
848 ptr = mtmp->data;
849 } else if (grow) {
850 ptr = grow_up(mtmp, (struct monst *) 0);
851 } else if (mstone) {
852 if (poly_when_stoned(ptr)) {
853 mon_to_stone(mtmp);
854 ptr = mtmp->data;
855 } else if (!resists_ston(mtmp)) {
856 if (canseemon(mtmp))
857 pline("%s turns to stone!", Monnam(mtmp));
858 monstone(mtmp);
859 ptr = (struct permonst *) 0;
861 } else if (heal) {
862 mtmp->mhp = mtmp->mhpmax;
864 if (!ptr)
865 return 2; /* it died */
867 /* Left behind a pile? */
868 if (rnd(25) < 3)
869 (void) mksobj_at(ROCK, mtmp->mx, mtmp->my, TRUE, FALSE);
870 newsym(mtmp->mx, mtmp->my);
871 return 1;
875 return 0;
878 /* monster eats a pile of objects */
880 meatobj(mtmp) /* for gelatinous cubes */
881 struct monst *mtmp;
883 register struct obj *otmp, *otmp2;
884 struct permonst *ptr, *original_ptr = mtmp->data;
885 int poly, grow, heal, count = 0, ecount = 0;
886 char buf[BUFSZ];
888 buf[0] = '\0';
889 /* If a pet, eating is handled separately, in dog.c */
890 if (mtmp->mtame)
891 return 0;
893 /* eat organic objects, including cloth and wood, if present;
894 engulf others, except huge rocks and metal attached to player
895 [despite comment at top, doesn't assume that eater is a g.cube] */
896 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
897 otmp2 = otmp->nexthere;
899 /* touch sensitive items */
900 if (otmp->otyp == CORPSE && is_rider(&mons[otmp->corpsenm])) {
901 /* Rider corpse isn't just inedible; can't engulf it either */
902 (void) revive_corpse(otmp);
904 /* untouchable (or inaccessible) items */
905 } else if ((otmp->otyp == CORPSE
906 && touch_petrifies(&mons[otmp->corpsenm])
907 && !resists_ston(mtmp))
908 /* don't engulf boulders and statues or ball&chain */
909 || otmp->oclass == ROCK_CLASS
910 || otmp == uball || otmp == uchain
911 /* normally mtmp won't have stepped onto scare monster
912 scroll, but if it does, don't eat or engulf that
913 (note: scrolls inside eaten containers will still
914 become engulfed) */
915 || otmp->otyp == SCR_SCARE_MONSTER) {
916 /* do nothing--neither eaten nor engulfed */
917 continue;
919 /* inedible items -- engulf these */
920 } else if (!is_organic(otmp) || obj_resists(otmp, 5, 95)
921 || !touch_artifact(otmp, mtmp)
922 /* redundant due to non-organic composition but
923 included for emphasis */
924 || (otmp->otyp == AMULET_OF_STRANGULATION
925 || otmp->otyp == RIN_SLOW_DIGESTION)
926 /* cockatrice corpses handled above; this
927 touch_petrifies() check catches eggs */
928 || ((otmp->otyp == CORPSE || otmp->otyp == EGG)
929 && ((touch_petrifies(&mons[otmp->corpsenm])
930 && !resists_ston(mtmp))
931 || (otmp->corpsenm == PM_GREEN_SLIME
932 && !slimeproof(mtmp->data))))) {
933 /* engulf */
934 ++ecount;
935 if (ecount == 1)
936 Sprintf(buf, "%s engulfs %s.", Monnam(mtmp),
937 distant_name(otmp, doname));
938 else if (ecount == 2)
939 Sprintf(buf, "%s engulfs several objects.", Monnam(mtmp));
940 obj_extract_self(otmp);
941 (void) mpickobj(mtmp, otmp); /* slurp */
943 /* lastly, edible items; yum! */
944 } else {
945 /* devour */
946 ++count;
947 if (cansee(mtmp->mx, mtmp->my)) {
948 if (flags.verbose)
949 pline("%s eats %s!", Monnam(mtmp),
950 distant_name(otmp, doname));
951 /* give this one even if !verbose */
952 if (otmp->oclass == SCROLL_CLASS
953 && !strcmpi(OBJ_DESCR(objects[otmp->otyp]), "YUM YUM"))
954 pline("Yum%c", otmp->blessed ? '!' : '.');
955 } else {
956 if (flags.verbose)
957 You_hear("a slurping sound.");
959 /* Heal up to the object's weight in hp */
960 if (mtmp->mhp < mtmp->mhpmax) {
961 mtmp->mhp += objects[otmp->otyp].oc_weight;
962 if (mtmp->mhp > mtmp->mhpmax)
963 mtmp->mhp = mtmp->mhpmax;
965 if (Has_contents(otmp)) {
966 register struct obj *otmp3;
968 /* contents of eaten containers become engulfed; this
969 is arbitrary, but otherwise g.cubes are too powerful */
970 while ((otmp3 = otmp->cobj) != 0) {
971 obj_extract_self(otmp3);
972 if (otmp->otyp == ICE_BOX && otmp3->otyp == CORPSE) {
973 otmp3->age = monstermoves - otmp3->age;
974 start_corpse_timeout(otmp3);
976 (void) mpickobj(mtmp, otmp3);
979 poly = polyfodder(otmp);
980 grow = mlevelgain(otmp);
981 heal = mhealup(otmp);
982 delobj(otmp); /* munch */
983 ptr = mtmp->data;
984 if (poly) {
985 if (newcham(mtmp, (struct permonst *) 0, FALSE, FALSE))
986 ptr = mtmp->data;
987 } else if (grow) {
988 ptr = grow_up(mtmp, (struct monst *) 0);
989 } else if (heal) {
990 mtmp->mhp = mtmp->mhpmax;
992 /* in case it polymorphed or died */
993 if (ptr != original_ptr)
994 return !ptr ? 2 : 1;
997 /* Engulf & devour is instant, so don't set meating */
998 if (mtmp->minvis)
999 newsym(mtmp->mx, mtmp->my);
1002 if (ecount > 0) {
1003 if (cansee(mtmp->mx, mtmp->my) && flags.verbose && buf[0])
1004 pline1(buf);
1005 else if (flags.verbose)
1006 You_hear("%s slurping sound%s.",
1007 (ecount == 1) ? "a" : "several", plur(ecount));
1009 return (count > 0 || ecount > 0) ? 1 : 0;
1012 void
1013 mpickgold(mtmp)
1014 register struct monst *mtmp;
1016 register struct obj *gold;
1017 int mat_idx;
1019 if ((gold = g_at(mtmp->mx, mtmp->my)) != 0) {
1020 mat_idx = objects[gold->otyp].oc_material;
1021 obj_extract_self(gold);
1022 add_to_minv(mtmp, gold);
1023 if (cansee(mtmp->mx, mtmp->my)) {
1024 if (flags.verbose && !mtmp->isgd)
1025 pline("%s picks up some %s.", Monnam(mtmp),
1026 mat_idx == GOLD ? "gold" : "money");
1027 newsym(mtmp->mx, mtmp->my);
1032 boolean
1033 mpickstuff(mtmp, str)
1034 register struct monst *mtmp;
1035 register const char *str;
1037 register struct obj *otmp, *otmp2, *otmp3;
1038 int carryamt = 0;
1040 /* prevent shopkeepers from leaving the door of their shop */
1041 if (mtmp->isshk && inhishop(mtmp))
1042 return FALSE;
1044 for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
1045 otmp2 = otmp->nexthere;
1046 /* Nymphs take everything. Most monsters don't pick up corpses. */
1047 if (!str ? searches_for_item(mtmp, otmp)
1048 : !!(index(str, otmp->oclass))) {
1049 if (otmp->otyp == CORPSE && mtmp->data->mlet != S_NYMPH
1050 /* let a handful of corpse types thru to can_carry() */
1051 && !touch_petrifies(&mons[otmp->corpsenm])
1052 && otmp->corpsenm != PM_LIZARD
1053 && !acidic(&mons[otmp->corpsenm]))
1054 continue;
1055 if (!touch_artifact(otmp, mtmp))
1056 continue;
1057 carryamt = can_carry(mtmp, otmp);
1058 if (carryamt == 0)
1059 continue;
1060 if (is_pool(mtmp->mx, mtmp->my))
1061 continue;
1062 /* handle cases where the critter can only get some */
1063 otmp3 = otmp;
1064 if (carryamt != otmp->quan) {
1065 otmp3 = splitobj(otmp, carryamt);
1067 if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
1068 pline("%s picks up %s.", Monnam(mtmp),
1069 (distu(mtmp->mx, mtmp->my) <= 5)
1070 ? doname(otmp3)
1071 : distant_name(otmp3, doname));
1072 obj_extract_self(otmp3); /* remove from floor */
1073 (void) mpickobj(mtmp, otmp3); /* may merge and free otmp3 */
1074 m_dowear(mtmp, FALSE);
1075 newsym(mtmp->mx, mtmp->my);
1076 return TRUE; /* pick only one object */
1079 return FALSE;
1083 curr_mon_load(mtmp)
1084 struct monst *mtmp;
1086 int curload = 0;
1087 struct obj *obj;
1089 for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1090 if (obj->otyp != BOULDER || !throws_rocks(mtmp->data))
1091 curload += obj->owt;
1094 return curload;
1098 max_mon_load(mtmp)
1099 struct monst *mtmp;
1101 long maxload;
1103 /* Base monster carrying capacity is equal to human maximum
1104 * carrying capacity, or half human maximum if not strong.
1105 * (for a polymorphed player, the value used would be the
1106 * non-polymorphed carrying capacity instead of max/half max).
1107 * This is then modified by the ratio between the monster weights
1108 * and human weights. Corpseless monsters are given a capacity
1109 * proportional to their size instead of weight.
1111 if (!mtmp->data->cwt)
1112 maxload = (MAX_CARR_CAP * (long) mtmp->data->msize) / MZ_HUMAN;
1113 else if (!strongmonst(mtmp->data)
1114 || (strongmonst(mtmp->data) && (mtmp->data->cwt > WT_HUMAN)))
1115 maxload = (MAX_CARR_CAP * (long) mtmp->data->cwt) / WT_HUMAN;
1116 else
1117 maxload = MAX_CARR_CAP; /*strong monsters w/cwt <= WT_HUMAN*/
1119 if (!strongmonst(mtmp->data))
1120 maxload /= 2;
1122 if (maxload < 1)
1123 maxload = 1;
1125 return (int) maxload;
1128 /* for restricting monsters' object-pickup.
1130 * to support the new pet behavior, this now returns the max # of objects
1131 * that a given monster could pick up from a pile. frequently this will be
1132 * otmp->quan, but special cases for 'only one' now exist so.
1134 * this will probably cause very amusing behavior with pets and gold coins.
1136 * TODO: allow picking up 2-N objects from a pile of N based on weight.
1137 * Change from 'int' to 'long' to accomate big stacks of gold.
1138 * Right now we fake it by reporting a partial quantity, but the
1139 * likesgold handling m_move results in picking up the whole stack.
1142 can_carry(mtmp, otmp)
1143 struct monst *mtmp;
1144 struct obj *otmp;
1146 int iquan, otyp = otmp->otyp, newload = otmp->owt;
1147 struct permonst *mdat = mtmp->data;
1148 short nattk = 0;
1150 if (notake(mdat))
1151 return 0; /* can't carry anything */
1153 if (otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm])
1154 && !(mtmp->misc_worn_check & W_ARMG) && !resists_ston(mtmp))
1155 return 0;
1156 if (otyp == CORPSE && is_rider(&mons[otmp->corpsenm]))
1157 return 0;
1158 if (objects[otyp].oc_material == SILVER && mon_hates_silver(mtmp)
1159 && (otyp != BELL_OF_OPENING || !is_covetous(mdat)))
1160 return 0;
1162 /* hostile monsters who like gold will pick up the whole stack;
1163 tame mosnters with hands will pick up the partial stack */
1164 iquan = (otmp->quan > (long) LARGEST_INT)
1165 ? 20000 + rn2(LARGEST_INT - 20000 + 1)
1166 : (int) otmp->quan;
1168 /* monsters without hands can't pick up multiple objects at once
1169 * unless they have an engulfing attack
1171 * ...dragons, of course, can always carry gold pieces and gems somehow
1173 if (iquan > 1) {
1174 boolean glomper = FALSE;
1176 if (mtmp->data->mlet == S_DRAGON
1177 && (otmp->oclass == COIN_CLASS
1178 || otmp->oclass == GEM_CLASS))
1179 glomper = TRUE;
1180 else
1181 for (nattk = 0; nattk < NATTK; nattk++)
1182 if (mtmp->data->mattk[nattk].aatyp == AT_ENGL) {
1183 glomper = TRUE;
1184 break;
1186 if ((mtmp->data->mflags1 & M1_NOHANDS) && !glomper)
1187 return 1;
1190 /* steeds don't pick up stuff (to avoid shop abuse) */
1191 if (mtmp == u.usteed)
1192 return 0;
1193 if (mtmp->isshk)
1194 return iquan; /* no limit */
1195 if (mtmp->mpeaceful && !mtmp->mtame)
1196 return 0;
1197 /* otherwise players might find themselves obligated to violate
1198 * their alignment if the monster takes something they need
1201 /* special--boulder throwers carry unlimited amounts of boulders */
1202 if (throws_rocks(mdat) && otyp == BOULDER)
1203 return iquan;
1205 /* nymphs deal in stolen merchandise, but not boulders or statues */
1206 if (mdat->mlet == S_NYMPH)
1207 return (otmp->oclass == ROCK_CLASS) ? 0 : iquan;
1209 if (curr_mon_load(mtmp) + newload > max_mon_load(mtmp))
1210 return 0;
1212 return iquan;
1215 /* return number of acceptable neighbour positions */
1217 mfndpos(mon, poss, info, flag)
1218 struct monst *mon;
1219 coord *poss; /* coord poss[9] */
1220 long *info; /* long info[9] */
1221 long flag;
1223 struct permonst *mdat = mon->data;
1224 register struct trap *ttmp;
1225 xchar x, y, nx, ny;
1226 int cnt = 0;
1227 uchar ntyp;
1228 uchar nowtyp;
1229 boolean wantpool, poolok, lavaok, nodiag;
1230 boolean rockok = FALSE, treeok = FALSE, thrudoor;
1231 int maxx, maxy;
1232 boolean poisongas_ok, in_poisongas;
1233 NhRegion *gas_reg;
1234 int gas_glyph = cmap_to_glyph(S_poisoncloud);
1236 x = mon->mx;
1237 y = mon->my;
1238 nowtyp = levl[x][y].typ;
1240 nodiag = NODIAG(mdat - mons);
1241 wantpool = mdat->mlet == S_EEL;
1242 poolok = (is_flyer(mdat) || is_clinger(mdat)
1243 || (is_swimmer(mdat) && !wantpool));
1244 lavaok = (is_flyer(mdat) || is_clinger(mdat) || likes_lava(mdat));
1245 thrudoor = ((flag & (ALLOW_WALL | BUSTDOOR)) != 0L);
1246 poisongas_ok = ((nonliving(mdat) || is_vampshifter(mon)
1247 || breathless(mdat)) || resists_poison(mon));
1248 in_poisongas = ((gas_reg = visible_region_at(x,y)) != 0
1249 && gas_reg->glyph == gas_glyph);
1251 if (flag & ALLOW_DIG) {
1252 struct obj *mw_tmp;
1254 /* need to be specific about what can currently be dug */
1255 if (!needspick(mdat)) {
1256 rockok = treeok = TRUE;
1257 } else if ((mw_tmp = MON_WEP(mon)) && mw_tmp->cursed
1258 && mon->weapon_check == NO_WEAPON_WANTED) {
1259 rockok = is_pick(mw_tmp);
1260 treeok = is_axe(mw_tmp);
1261 } else {
1262 rockok = (m_carrying(mon, PICK_AXE)
1263 || (m_carrying(mon, DWARVISH_MATTOCK)
1264 && !which_armor(mon, W_ARMS)));
1265 treeok = (m_carrying(mon, AXE) || (m_carrying(mon, BATTLE_AXE)
1266 && !which_armor(mon, W_ARMS)));
1268 if (rockok || treeok)
1269 thrudoor = TRUE;
1272 nexttry: /* eels prefer the water, but if there is no water nearby,
1273 they will crawl over land */
1274 if (mon->mconf) {
1275 flag |= ALLOW_ALL;
1276 flag &= ~NOTONL;
1278 if (!mon->mcansee)
1279 flag |= ALLOW_SSM;
1280 maxx = min(x + 1, COLNO - 1);
1281 maxy = min(y + 1, ROWNO - 1);
1282 for (nx = max(1, x - 1); nx <= maxx; nx++)
1283 for (ny = max(0, y - 1); ny <= maxy; ny++) {
1284 if (nx == x && ny == y)
1285 continue;
1286 ntyp = levl[nx][ny].typ;
1287 if (IS_ROCK(ntyp)
1288 && !((flag & ALLOW_WALL) && may_passwall(nx, ny))
1289 && !((IS_TREE(ntyp) ? treeok : rockok) && may_dig(nx, ny)))
1290 continue;
1291 /* KMH -- Added iron bars */
1292 if (ntyp == IRONBARS && !(flag & ALLOW_BARS))
1293 continue;
1294 if (IS_DOOR(ntyp) && !(amorphous(mdat) || can_fog(mon))
1295 && (((levl[nx][ny].doormask & D_CLOSED) && !(flag & OPENDOOR))
1296 || ((levl[nx][ny].doormask & D_LOCKED)
1297 && !(flag & UNLOCKDOOR))) && !thrudoor)
1298 continue;
1299 /* avoid poison gas? */
1300 if (!poisongas_ok && !in_poisongas
1301 && (gas_reg = visible_region_at(nx,ny)) != 0
1302 && gas_reg->glyph == gas_glyph)
1303 continue;
1304 /* first diagonal checks (tight squeezes handled below) */
1305 if (nx != x && ny != y
1306 && (nodiag
1307 || (IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN))
1308 || (IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN))
1309 || ((IS_DOOR(nowtyp) || IS_DOOR(ntyp))
1310 && Is_rogue_level(&u.uz))
1311 /* mustn't pass between adjacent long worm segments,
1312 but can attack that way */
1313 || (m_at(x, ny) && m_at(nx, y) && worm_cross(x, y, nx, ny)
1314 && !m_at(nx, ny) && (nx != u.ux || ny != u.uy))))
1315 continue;
1316 if ((is_pool(nx, ny) == wantpool || poolok)
1317 && (lavaok || !is_lava(nx, ny))) {
1318 int dispx, dispy;
1319 boolean monseeu = (mon->mcansee
1320 && (!Invis || perceives(mdat)));
1321 boolean checkobj = OBJ_AT(nx, ny);
1323 /* Displacement also displaces the Elbereth/scare monster,
1324 * as long as you are visible.
1326 if (Displaced && monseeu && mon->mux == nx && mon->muy == ny) {
1327 dispx = u.ux;
1328 dispy = u.uy;
1329 } else {
1330 dispx = nx;
1331 dispy = ny;
1334 info[cnt] = 0;
1335 if (onscary(dispx, dispy, mon)) {
1336 if (!(flag & ALLOW_SSM))
1337 continue;
1338 info[cnt] |= ALLOW_SSM;
1340 if ((nx == u.ux && ny == u.uy)
1341 || (nx == mon->mux && ny == mon->muy)) {
1342 if (nx == u.ux && ny == u.uy) {
1343 /* If it's right next to you, it found you,
1344 * displaced or no. We must set mux and muy
1345 * right now, so when we return we can tell
1346 * that the ALLOW_U means to attack _you_ and
1347 * not the image.
1349 mon->mux = u.ux;
1350 mon->muy = u.uy;
1352 if (!(flag & ALLOW_U))
1353 continue;
1354 info[cnt] |= ALLOW_U;
1355 } else {
1356 if (MON_AT(nx, ny)) {
1357 struct monst *mtmp2 = m_at(nx, ny);
1358 long mmflag = flag | mm_aggression(mon, mtmp2);
1360 if (mmflag & ALLOW_M) {
1361 info[cnt] |= ALLOW_M;
1362 if (mtmp2->mtame) {
1363 if (!(mmflag & ALLOW_TM))
1364 continue;
1365 info[cnt] |= ALLOW_TM;
1367 } else {
1368 mmflag = flag | mm_displacement(mon, mtmp2);
1369 if (!(mmflag & ALLOW_MDISP))
1370 continue;
1371 info[cnt] |= ALLOW_MDISP;
1374 /* Note: ALLOW_SANCT only prevents movement, not
1375 attack, into a temple. */
1376 if (level.flags.has_temple && *in_rooms(nx, ny, TEMPLE)
1377 && !*in_rooms(x, y, TEMPLE)
1378 && in_your_sanctuary((struct monst *) 0, nx, ny)) {
1379 if (!(flag & ALLOW_SANCT))
1380 continue;
1381 info[cnt] |= ALLOW_SANCT;
1384 if (checkobj && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
1385 if (flag & NOGARLIC)
1386 continue;
1387 info[cnt] |= NOGARLIC;
1389 if (checkobj && sobj_at(BOULDER, nx, ny)) {
1390 if (!(flag & ALLOW_ROCK))
1391 continue;
1392 info[cnt] |= ALLOW_ROCK;
1394 if (monseeu && onlineu(nx, ny)) {
1395 if (flag & NOTONL)
1396 continue;
1397 info[cnt] |= NOTONL;
1399 /* check for diagonal tight squeeze */
1400 if (nx != x && ny != y && bad_rock(mdat, x, ny)
1401 && bad_rock(mdat, nx, y) && cant_squeeze_thru(mon))
1402 continue;
1403 /* The monster avoids a particular type of trap if it's
1404 * familiar with the trap type. Pets get ALLOW_TRAPS
1405 * and checking is done in dogmove.c. In either case,
1406 * "harmless" traps are neither avoided nor marked in info[].
1408 if ((ttmp = t_at(nx, ny)) != 0) {
1409 if (ttmp->ttyp >= TRAPNUM || ttmp->ttyp == 0) {
1410 impossible(
1411 "A monster looked at a very strange trap of type %d.",
1412 ttmp->ttyp);
1413 continue;
1415 if ((ttmp->ttyp != RUST_TRAP
1416 || mdat == &mons[PM_IRON_GOLEM])
1417 && ttmp->ttyp != STATUE_TRAP
1418 && ((ttmp->ttyp != PIT && ttmp->ttyp != SPIKED_PIT
1419 && ttmp->ttyp != TRAPDOOR && ttmp->ttyp != HOLE)
1420 || (!is_flyer(mdat) && !is_floater(mdat)
1421 && !is_clinger(mdat)) || Sokoban)
1422 && (ttmp->ttyp != SLP_GAS_TRAP || !resists_sleep(mon))
1423 && (ttmp->ttyp != BEAR_TRAP
1424 || (mdat->msize > MZ_SMALL && !amorphous(mdat)
1425 && !is_flyer(mdat) && !is_floater(mdat)
1426 && !is_whirly(mdat) && !unsolid(mdat)))
1427 && (ttmp->ttyp != FIRE_TRAP || !resists_fire(mon))
1428 && (ttmp->ttyp != SQKY_BOARD || !is_flyer(mdat))
1429 && (ttmp->ttyp != WEB
1430 || (!amorphous(mdat) && !webmaker(mdat)
1431 && !is_whirly(mdat) && !unsolid(mdat)))
1432 && (ttmp->ttyp != ANTI_MAGIC || !resists_magm(mon))) {
1433 if (!(flag & ALLOW_TRAPS)) {
1434 if (mon->mtrapseen & (1L << (ttmp->ttyp - 1)))
1435 continue;
1437 info[cnt] |= ALLOW_TRAPS;
1440 poss[cnt].x = nx;
1441 poss[cnt].y = ny;
1442 cnt++;
1445 if (!cnt && wantpool && !is_pool(x, y)) {
1446 wantpool = FALSE;
1447 goto nexttry;
1449 return cnt;
1452 /* Monster against monster special attacks; for the specified monster
1453 combinations, this allows one monster to attack another adjacent one
1454 in the absence of Conflict. There is no provision for targetting
1455 other monsters; just hand to hand fighting when they happen to be
1456 next to each other. */
1457 STATIC_OVL long
1458 mm_aggression(magr, mdef)
1459 struct monst *magr, /* monster that is currently deciding where to move */
1460 *mdef; /* another monster which is next to it */
1462 /* supposedly purple worms are attracted to shrieking because they
1463 like to eat shriekers, so attack the latter when feasible */
1464 if (magr->data == &mons[PM_PURPLE_WORM]
1465 && mdef->data == &mons[PM_SHRIEKER])
1466 return ALLOW_M | ALLOW_TM;
1467 /* Various other combinations such as dog vs cat, cat vs rat, and
1468 elf vs orc have been suggested. For the time being we don't
1469 support those. */
1470 return 0L;
1473 /* Monster displacing another monster out of the way */
1474 STATIC_OVL long
1475 mm_displacement(magr, mdef)
1476 struct monst *magr, /* monster that is currently deciding where to move */
1477 *mdef; /* another monster which is next to it */
1479 struct permonst *pa = magr->data, *pd = mdef->data;
1481 /* if attacker can't barge through, there's nothing to do;
1482 or if defender can barge through too, don't let attacker
1483 do so, otherwise they might just end up swapping places
1484 again when defender gets its chance to move */
1485 if ((pa->mflags3 & M3_DISPLACES) != 0 && (pd->mflags3 & M3_DISPLACES) == 0
1486 /* no displacing grid bugs diagonally */
1487 && !(magr->mx != mdef->mx && magr->my != mdef->my
1488 && NODIAG(monsndx(pd)))
1489 /* no displacing trapped monsters or multi-location longworms */
1490 && !mdef->mtrapped && (!mdef->wormno || !count_wsegs(mdef))
1491 /* riders can move anything; others, same size or smaller only */
1492 && (is_rider(pa) || pa->msize >= pd->msize))
1493 return ALLOW_MDISP;
1494 return 0L;
1497 /* Is the square close enough for the monster to move or attack into? */
1498 boolean
1499 monnear(mon, x, y)
1500 struct monst *mon;
1501 int x, y;
1503 int distance = dist2(mon->mx, mon->my, x, y);
1505 if (distance == 2 && NODIAG(mon->data - mons))
1506 return 0;
1507 return (boolean) (distance < 3);
1510 /* really free dead monsters */
1511 void
1512 dmonsfree()
1514 struct monst **mtmp, *freetmp;
1515 int count = 0;
1517 for (mtmp = &fmon; *mtmp;) {
1518 freetmp = *mtmp;
1519 if (freetmp->mhp <= 0 && !freetmp->isgd) {
1520 *mtmp = freetmp->nmon;
1521 freetmp->nmon = NULL;
1522 dealloc_monst(freetmp);
1523 count++;
1524 } else
1525 mtmp = &(freetmp->nmon);
1528 if (count != iflags.purge_monsters)
1529 impossible("dmonsfree: %d removed doesn't match %d pending",
1530 count, iflags.purge_monsters);
1531 iflags.purge_monsters = 0;
1534 /* called when monster is moved to larger structure */
1535 void
1536 replmon(mtmp, mtmp2)
1537 struct monst *mtmp, *mtmp2;
1539 struct obj *otmp;
1541 /* transfer the monster's inventory */
1542 for (otmp = mtmp2->minvent; otmp; otmp = otmp->nobj) {
1543 if (otmp->where != OBJ_MINVENT || otmp->ocarry != mtmp)
1544 impossible("replmon: minvent inconsistency");
1545 otmp->ocarry = mtmp2;
1547 mtmp->minvent = 0;
1549 /* remove the old monster from the map and from `fmon' list */
1550 relmon(mtmp, (struct monst **) 0);
1552 /* finish adding its replacement */
1553 if (mtmp != u.usteed) /* don't place steed onto the map */
1554 place_monster(mtmp2, mtmp2->mx, mtmp2->my);
1555 if (mtmp2->wormno) /* update level.monsters[wseg->wx][wseg->wy] */
1556 place_wsegs(mtmp2); /* locations to mtmp2 not mtmp. */
1557 if (emits_light(mtmp2->data)) {
1558 /* since this is so rare, we don't have any `mon_move_light_source' */
1559 new_light_source(mtmp2->mx, mtmp2->my, emits_light(mtmp2->data),
1560 LS_MONSTER, monst_to_any(mtmp2));
1561 /* here we rely on fact that `mtmp' hasn't actually been deleted */
1562 del_light_source(LS_MONSTER, monst_to_any(mtmp));
1564 mtmp2->nmon = fmon;
1565 fmon = mtmp2;
1566 if (u.ustuck == mtmp)
1567 u.ustuck = mtmp2;
1568 if (u.usteed == mtmp)
1569 u.usteed = mtmp2;
1570 if (mtmp2->isshk)
1571 replshk(mtmp, mtmp2);
1573 /* discard the old monster */
1574 dealloc_monst(mtmp);
1577 /* release mon from the display and the map's monster list,
1578 maybe transfer it to one of the other monster lists */
1579 void
1580 relmon(mon, monst_list)
1581 struct monst *mon;
1582 struct monst **monst_list; /* &migrating_mons or &mydogs or null */
1584 struct monst *mtmp;
1585 boolean unhide = (monst_list != 0);
1586 int mx = mon->mx, my = mon->my;
1588 if (!fmon)
1589 panic("relmon: no fmon available.");
1591 if (unhide) {
1592 /* can't remain hidden across level changes (exception: wizard
1593 clone can continue imitating some other monster form); also,
1594 might be imitating a boulder so need line-of-sight unblocking */
1595 mon->mundetected = 0;
1596 if (mon->m_ap_type && mon->m_ap_type != M_AP_MONSTER)
1597 seemimic(mon);
1600 if (mon->wormno)
1601 remove_worm(mon);
1602 else
1603 remove_monster(mx, my);
1605 if (mon == fmon) {
1606 fmon = fmon->nmon;
1607 } else {
1608 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
1609 if (mtmp->nmon == mon)
1610 break;
1612 if (mtmp)
1613 mtmp->nmon = mon->nmon;
1614 else
1615 panic("relmon: mon not in list.");
1618 if (unhide) {
1619 newsym(mx, my);
1620 /* insert into mydogs or migrating_mons */
1621 mon->nmon = *monst_list;
1622 *monst_list = mon;
1623 } else {
1624 /* orphan has no next monster */
1625 mon->nmon = 0;
1629 void
1630 copy_mextra(mtmp2, mtmp1)
1631 struct monst *mtmp2, *mtmp1;
1633 if (!mtmp2 || !mtmp1 || !mtmp1->mextra)
1634 return;
1636 if (!mtmp2->mextra)
1637 mtmp2->mextra = newmextra();
1638 if (MNAME(mtmp1)) {
1639 new_mname(mtmp2, (int) strlen(MNAME(mtmp1)) + 1);
1640 Strcpy(MNAME(mtmp2), MNAME(mtmp1));
1642 if (EGD(mtmp1)) {
1643 if (!EGD(mtmp2))
1644 newegd(mtmp2);
1645 *EGD(mtmp2) = *EGD(mtmp1);
1647 if (EPRI(mtmp1)) {
1648 if (!EPRI(mtmp2))
1649 newepri(mtmp2);
1650 *EPRI(mtmp2) = *EPRI(mtmp1);
1652 if (ESHK(mtmp1)) {
1653 if (!ESHK(mtmp2))
1654 neweshk(mtmp2);
1655 *ESHK(mtmp2) = *ESHK(mtmp1);
1657 if (EMIN(mtmp1)) {
1658 if (!EMIN(mtmp2))
1659 newemin(mtmp2);
1660 *EMIN(mtmp2) = *EMIN(mtmp1);
1662 if (EDOG(mtmp1)) {
1663 if (!EDOG(mtmp2))
1664 newedog(mtmp2);
1665 *EDOG(mtmp2) = *EDOG(mtmp1);
1667 if (has_mcorpsenm(mtmp1))
1668 MCORPSENM(mtmp2) = MCORPSENM(mtmp1);
1671 void
1672 dealloc_mextra(m)
1673 struct monst *m;
1675 struct mextra *x = m->mextra;
1677 if (x) {
1678 if (x->mname)
1679 free((genericptr_t) x->mname);
1680 if (x->egd)
1681 free((genericptr_t) x->egd);
1682 if (x->epri)
1683 free((genericptr_t) x->epri);
1684 if (x->eshk)
1685 free((genericptr_t) x->eshk);
1686 if (x->emin)
1687 free((genericptr_t) x->emin);
1688 if (x->edog)
1689 free((genericptr_t) x->edog);
1690 /* [no action needed for x->mcorpsenm] */
1692 free((genericptr_t) x);
1693 m->mextra = (struct mextra *) 0;
1697 void
1698 dealloc_monst(mon)
1699 struct monst *mon;
1701 if (mon->nmon)
1702 panic("dealloc_monst with nmon");
1703 if (mon->mextra)
1704 dealloc_mextra(mon);
1705 free((genericptr_t) mon);
1708 /* remove effects of mtmp from other data structures */
1709 STATIC_OVL void
1710 m_detach(mtmp, mptr)
1711 struct monst *mtmp;
1712 struct permonst *mptr; /* reflects mtmp->data _prior_ to mtmp's death */
1714 if (mtmp == context.polearm.hitmon)
1715 context.polearm.hitmon = 0;
1716 if (mtmp->mleashed)
1717 m_unleash(mtmp, FALSE);
1718 /* to prevent an infinite relobj-flooreffects-hmon-killed loop */
1719 mtmp->mtrapped = 0;
1720 mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */
1721 relobj(mtmp, 0, FALSE);
1722 remove_monster(mtmp->mx, mtmp->my);
1723 if (emits_light(mptr))
1724 del_light_source(LS_MONSTER, monst_to_any(mtmp));
1725 if (mtmp->m_ap_type)
1726 seemimic(mtmp);
1727 newsym(mtmp->mx, mtmp->my);
1728 unstuck(mtmp);
1729 fill_pit(mtmp->mx, mtmp->my);
1731 if (mtmp->isshk)
1732 shkgone(mtmp);
1733 if (mtmp->wormno)
1734 wormgone(mtmp);
1735 iflags.purge_monsters++;
1738 /* find the worn amulet of life saving which will save a monster */
1739 struct obj *
1740 mlifesaver(mon)
1741 struct monst *mon;
1743 if (!nonliving(mon->data) || is_vampshifter(mon)) {
1744 struct obj *otmp = which_armor(mon, W_AMUL);
1746 if (otmp && otmp->otyp == AMULET_OF_LIFE_SAVING)
1747 return otmp;
1749 return (struct obj *) 0;
1752 STATIC_OVL void
1753 lifesaved_monster(mtmp)
1754 struct monst *mtmp;
1756 boolean surviver;
1757 struct obj *lifesave = mlifesaver(mtmp);
1759 if (lifesave) {
1760 /* not canseemon; amulets are on the head, so you don't want
1761 * to show this for a long worm with only a tail visible.
1762 * Nor do you check invisibility, because glowing and
1763 * disintegrating amulets are always visible. */
1764 if (cansee(mtmp->mx, mtmp->my)) {
1765 pline("But wait...");
1766 pline("%s medallion begins to glow!", s_suffix(Monnam(mtmp)));
1767 makeknown(AMULET_OF_LIFE_SAVING);
1768 /* amulet is visible, but monster might not be */
1769 if (canseemon(mtmp)) {
1770 if (attacktype(mtmp->data, AT_EXPL)
1771 || attacktype(mtmp->data, AT_BOOM))
1772 pline("%s reconstitutes!", Monnam(mtmp));
1773 else
1774 pline("%s looks much better!", Monnam(mtmp));
1776 pline_The("medallion crumbles to dust!");
1778 m_useup(mtmp, lifesave);
1780 surviver = !(mvitals[monsndx(mtmp->data)].mvflags & G_GENOD);
1781 mtmp->mcanmove = 1;
1782 mtmp->mfrozen = 0;
1783 if (mtmp->mtame && !mtmp->isminion) {
1784 wary_dog(mtmp, !surviver);
1786 if (mtmp->mhpmax <= 0)
1787 mtmp->mhpmax = 10;
1788 mtmp->mhp = mtmp->mhpmax;
1789 if (surviver)
1790 return;
1792 /* genocided monster can't be life-saved */
1793 if (cansee(mtmp->mx, mtmp->my))
1794 pline("Unfortunately, %s is still genocided...", mon_nam(mtmp));
1795 mtmp->mhp = 0;
1799 void
1800 mondead(mtmp)
1801 register struct monst *mtmp;
1803 struct permonst *mptr;
1804 int tmp;
1806 mtmp->mhp = 0; /* in case caller hasn't done this */
1807 lifesaved_monster(mtmp);
1808 if (mtmp->mhp > 0)
1809 return;
1811 if (is_vampshifter(mtmp)) {
1812 int mndx = mtmp->cham;
1813 int x = mtmp->mx, y = mtmp->my;
1815 /* this only happens if shapeshifted */
1816 if (mndx >= LOW_PM && mndx != monsndx(mtmp->data)
1817 && !(mvitals[mndx].mvflags & G_GENOD)) {
1818 char buf[BUFSZ];
1819 boolean in_door = (amorphous(mtmp->data)
1820 && closed_door(mtmp->mx, mtmp->my)),
1821 /* alternate message phrasing for some monster types */
1822 spec_mon = (nonliving(mtmp->data)
1823 || noncorporeal(mtmp->data)
1824 || amorphous(mtmp->data));
1826 /* construct a format string before transformation */
1827 Sprintf(buf, "The %s%s suddenly %s and rises as %%s!",
1828 spec_mon ? "" : "seemingly dead ",
1829 x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1830 SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
1831 | SUPPRESS_INVISIBLE | SUPPRESS_IT,
1832 FALSE),
1833 spec_mon ? "reconstitutes" : "transforms");
1834 mtmp->mcanmove = 1;
1835 mtmp->mfrozen = 0;
1836 if (mtmp->mhpmax <= 0)
1837 mtmp->mhpmax = 10;
1838 mtmp->mhp = mtmp->mhpmax;
1839 /* this can happen if previously a fog cloud */
1840 if (u.uswallow && (mtmp == u.ustuck))
1841 expels(mtmp, mtmp->data, FALSE);
1842 if (in_door) {
1843 coord new_xy;
1845 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
1846 rloc_to(mtmp, new_xy.x, new_xy.y);
1849 newcham(mtmp, &mons[mndx], FALSE, FALSE);
1850 if (mtmp->data == &mons[mndx])
1851 mtmp->cham = NON_PM;
1852 else
1853 mtmp->cham = mndx;
1854 if (canspotmon(mtmp)) {
1855 pline(buf, a_monnam(mtmp));
1856 vamp_rise_msg = TRUE;
1858 newsym(x, y);
1859 return;
1863 /* dead vault guard is actually kept at coordinate <0,0> until
1864 his temporary corridor to/from the vault has been removed;
1865 need to do this after life-saving and before m_detach() */
1866 if (mtmp->isgd && !grddead(mtmp))
1867 return;
1869 /* Player is thrown from his steed when it dies */
1870 if (mtmp == u.usteed)
1871 dismount_steed(DISMOUNT_GENERIC);
1873 mptr = mtmp->data; /* save this for m_detach() */
1874 /* restore chameleon, lycanthropes to true form at death */
1875 if (mtmp->cham >= LOW_PM) {
1876 set_mon_data(mtmp, &mons[mtmp->cham], -1);
1877 mtmp->cham = NON_PM;
1878 } else if (mtmp->data == &mons[PM_WEREJACKAL])
1879 set_mon_data(mtmp, &mons[PM_HUMAN_WEREJACKAL], -1);
1880 else if (mtmp->data == &mons[PM_WEREWOLF])
1881 set_mon_data(mtmp, &mons[PM_HUMAN_WEREWOLF], -1);
1882 else if (mtmp->data == &mons[PM_WERERAT])
1883 set_mon_data(mtmp, &mons[PM_HUMAN_WERERAT], -1);
1885 /* if MAXMONNO monsters of a given type have died, and it
1886 * can be done, extinguish that monster.
1888 * mvitals[].died does double duty as total number of dead monsters
1889 * and as experience factor for the player killing more monsters.
1890 * this means that a dragon dying by other means reduces the
1891 * experience the player gets for killing a dragon directly; this
1892 * is probably not too bad, since the player likely finagled the
1893 * first dead dragon via ring of conflict or pets, and extinguishing
1894 * based on only player kills probably opens more avenues of abuse
1895 * for rings of conflict and such.
1897 tmp = monsndx(mtmp->data);
1898 if (mvitals[tmp].died < 255)
1899 mvitals[tmp].died++;
1901 /* if it's a (possibly polymorphed) quest leader, mark him as dead */
1902 if (mtmp->m_id == quest_status.leader_m_id)
1903 quest_status.leader_is_dead = TRUE;
1904 #ifdef MAIL
1905 /* if the mail daemon dies, no more mail delivery. -3. */
1906 if (tmp == PM_MAIL_DAEMON)
1907 mvitals[tmp].mvflags |= G_GENOD;
1908 #endif
1910 if (mtmp->data->mlet == S_KOP) {
1911 /* Dead Kops may come back. */
1912 switch (rnd(5)) {
1913 case 1: /* returns near the stairs */
1914 (void) makemon(mtmp->data, xdnstair, ydnstair, NO_MM_FLAGS);
1915 break;
1916 case 2: /* randomly */
1917 (void) makemon(mtmp->data, 0, 0, NO_MM_FLAGS);
1918 break;
1919 default:
1920 break;
1923 if (mtmp->iswiz)
1924 wizdead();
1925 if (mtmp->data->msound == MS_NEMESIS)
1926 nemdead();
1927 if (mtmp->data == &mons[PM_MEDUSA])
1928 u.uachieve.killed_medusa = 1;
1929 if (glyph_is_invisible(levl[mtmp->mx][mtmp->my].glyph))
1930 unmap_object(mtmp->mx, mtmp->my);
1931 m_detach(mtmp, mptr);
1934 /* TRUE if corpse might be dropped, magr may die if mon was swallowed */
1935 boolean
1936 corpse_chance(mon, magr, was_swallowed)
1937 struct monst *mon;
1938 struct monst *magr; /* killer, if swallowed */
1939 boolean was_swallowed; /* digestion */
1941 struct permonst *mdat = mon->data;
1942 int i, tmp;
1944 if (mdat == &mons[PM_VLAD_THE_IMPALER] || mdat->mlet == S_LICH) {
1945 if (cansee(mon->mx, mon->my) && !was_swallowed)
1946 pline("%s body crumbles into dust.", s_suffix(Monnam(mon)));
1947 return FALSE;
1950 /* Gas spores always explode upon death */
1951 for (i = 0; i < NATTK; i++) {
1952 if (mdat->mattk[i].aatyp == AT_BOOM) {
1953 if (mdat->mattk[i].damn)
1954 tmp = d((int) mdat->mattk[i].damn, (int) mdat->mattk[i].damd);
1955 else if (mdat->mattk[i].damd)
1956 tmp = d((int) mdat->mlevel + 1, (int) mdat->mattk[i].damd);
1957 else
1958 tmp = 0;
1959 if (was_swallowed && magr) {
1960 if (magr == &youmonst) {
1961 There("is an explosion in your %s!", body_part(STOMACH));
1962 Sprintf(killer.name, "%s explosion",
1963 s_suffix(mdat->mname));
1964 losehp(Maybe_Half_Phys(tmp), killer.name, KILLED_BY_AN);
1965 } else {
1966 You_hear("an explosion.");
1967 magr->mhp -= tmp;
1968 if (magr->mhp < 1)
1969 mondied(magr);
1970 if (magr->mhp < 1) { /* maybe lifesaved */
1971 if (canspotmon(magr))
1972 pline("%s rips open!", Monnam(magr));
1973 } else if (canseemon(magr))
1974 pline("%s seems to have indigestion.", Monnam(magr));
1977 return FALSE;
1980 Sprintf(killer.name, "%s explosion", s_suffix(mdat->mname));
1981 killer.format = KILLED_BY_AN;
1982 explode(mon->mx, mon->my, -1, tmp, MON_EXPLODE, EXPL_NOXIOUS);
1983 return FALSE;
1987 /* must duplicate this below check in xkilled() since it results in
1988 * creating no objects as well as no corpse
1990 if (LEVEL_SPECIFIC_NOCORPSE(mdat))
1991 return FALSE;
1993 if (((bigmonst(mdat) || mdat == &mons[PM_LIZARD]) && !mon->mcloned)
1994 || is_golem(mdat) || is_mplayer(mdat) || is_rider(mdat) || mon->isshk)
1995 return TRUE;
1996 tmp = 2 + ((mdat->geno & G_FREQ) < 2) + verysmall(mdat);
1997 return (boolean) !rn2(tmp);
2000 /* drop (perhaps) a cadaver and remove monster */
2001 void
2002 mondied(mdef)
2003 register struct monst *mdef;
2005 mondead(mdef);
2006 if (mdef->mhp > 0)
2007 return; /* lifesaved */
2009 if (corpse_chance(mdef, (struct monst *) 0, FALSE)
2010 && (accessible(mdef->mx, mdef->my) || is_pool(mdef->mx, mdef->my)))
2011 (void) make_corpse(mdef, CORPSTAT_NONE);
2014 /* monster disappears, not dies */
2015 void
2016 mongone(mdef)
2017 struct monst *mdef;
2019 mdef->mhp = 0; /* can skip some inventory bookkeeping */
2021 /* dead vault guard is actually kept at coordinate <0,0> until
2022 his temporary corridor to/from the vault has been removed */
2023 if (mdef->isgd && !grddead(mdef))
2024 return;
2025 /* hero is thrown from his steed when it disappears */
2026 if (mdef == u.usteed)
2027 dismount_steed(DISMOUNT_GENERIC);
2028 /* drop special items like the Amulet so that a dismissed Kop or nurse
2029 can't remove them from the game */
2030 mdrop_special_objs(mdef);
2031 /* release rest of monster's inventory--it is removed from game */
2032 discard_minvent(mdef);
2033 m_detach(mdef, mdef->data);
2036 /* drop a statue or rock and remove monster */
2037 void
2038 monstone(mdef)
2039 struct monst *mdef;
2041 struct obj *otmp, *obj, *oldminvent;
2042 xchar x = mdef->mx, y = mdef->my;
2043 boolean wasinside = FALSE;
2045 if (!vamp_stone(mdef)) /* vampshifter reverts to vampire */
2046 return;
2048 /* we have to make the statue before calling mondead, to be able to
2049 * put inventory in it, and we have to check for lifesaving before
2050 * making the statue....
2052 mdef->mhp = 0; /* in case caller hasn't done this */
2053 lifesaved_monster(mdef);
2054 if (mdef->mhp > 0)
2055 return;
2057 mdef->mtrapped = 0; /* (see m_detach) */
2059 if ((int) mdef->data->msize > MZ_TINY
2060 || !rn2(2 + ((int) (mdef->data->geno & G_FREQ) > 2))) {
2061 oldminvent = 0;
2062 /* some objects may end up outside the statue */
2063 while ((obj = mdef->minvent) != 0) {
2064 obj_extract_self(obj);
2065 if (obj->owornmask)
2066 update_mon_intrinsics(mdef, obj, FALSE, TRUE);
2067 obj_no_longer_held(obj);
2068 if (obj->owornmask & W_WEP)
2069 setmnotwielded(mdef, obj);
2070 obj->owornmask = 0L;
2071 if (obj->otyp == BOULDER
2072 #if 0 /* monsters don't carry statues */
2073 || (obj->otyp == STATUE
2074 && mons[obj->corpsenm].msize >= mdef->data->msize)
2075 #endif
2076 /* invocation tools resist even with 0% resistance */
2077 || obj_resists(obj, 0, 0)) {
2078 if (flooreffects(obj, x, y, "fall"))
2079 continue;
2080 place_object(obj, x, y);
2081 } else {
2082 if (obj->lamplit)
2083 end_burn(obj, TRUE);
2084 obj->nobj = oldminvent;
2085 oldminvent = obj;
2088 /* defer statue creation until after inventory removal
2089 so that saved monster traits won't retain any stale
2090 item-conferred attributes */
2091 otmp = mkcorpstat(STATUE, mdef, mdef->data, x, y, CORPSTAT_NONE);
2092 if (has_mname(mdef))
2093 otmp = oname(otmp, MNAME(mdef));
2094 while ((obj = oldminvent) != 0) {
2095 oldminvent = obj->nobj;
2096 (void) add_to_container(otmp, obj);
2098 /* Archeologists should not break unique statues */
2099 if (mdef->data->geno & G_UNIQ)
2100 otmp->spe = 1;
2101 otmp->owt = weight(otmp);
2102 } else
2103 otmp = mksobj_at(ROCK, x, y, TRUE, FALSE);
2105 stackobj(otmp);
2106 /* mondead() already does this, but we must do it before the newsym */
2107 if (glyph_is_invisible(levl[x][y].glyph))
2108 unmap_object(x, y);
2109 if (cansee(x, y))
2110 newsym(x, y);
2111 /* We don't currently trap the hero in the statue in this case but we
2112 * could */
2113 if (u.uswallow && u.ustuck == mdef)
2114 wasinside = TRUE;
2115 mondead(mdef);
2116 if (wasinside) {
2117 if (is_animal(mdef->data))
2118 You("%s through an opening in the new %s.",
2119 locomotion(youmonst.data, "jump"), xname(otmp));
2123 /* another monster has killed the monster mdef */
2124 void
2125 monkilled(mdef, fltxt, how)
2126 struct monst *mdef;
2127 const char *fltxt;
2128 int how;
2130 boolean be_sad = FALSE; /* true if unseen pet is killed */
2132 if ((mdef->wormno ? worm_known(mdef) : cansee(mdef->mx, mdef->my))
2133 && fltxt)
2134 pline("%s is %s%s%s!", Monnam(mdef),
2135 nonliving(mdef->data) ? "destroyed" : "killed",
2136 *fltxt ? " by the " : "", fltxt);
2137 else
2138 be_sad = (mdef->mtame != 0);
2140 /* no corpses if digested or disintegrated */
2141 if (how == AD_DGST || how == -AD_RBRE)
2142 mondead(mdef);
2143 else
2144 mondied(mdef);
2146 if (be_sad && mdef->mhp <= 0)
2147 You("have a sad feeling for a moment, then it passes.");
2150 void
2151 unstuck(mtmp)
2152 struct monst *mtmp;
2154 if (u.ustuck == mtmp) {
2155 if (u.uswallow) {
2156 u.ux = mtmp->mx;
2157 u.uy = mtmp->my;
2158 u.uswallow = 0;
2159 u.uswldtim = 0;
2160 if (Punished && uchain->where != OBJ_FLOOR)
2161 placebc();
2162 vision_full_recalc = 1;
2163 docrt();
2164 /* prevent swallower (mtmp might have just poly'd into something
2165 without an engulf attack) from immediately re-engulfing */
2166 if (attacktype(mtmp->data, AT_ENGL) && !mtmp->mspec_used)
2167 mtmp->mspec_used = rnd(2);
2169 u.ustuck = 0;
2173 void
2174 killed(mtmp)
2175 struct monst *mtmp;
2177 xkilled(mtmp, XKILL_GIVEMSG);
2180 /* the player has killed the monster mtmp */
2181 void
2182 xkilled(mtmp, xkill_flags)
2183 struct monst *mtmp;
2184 int xkill_flags; /* 1: suppress message, 2: suppress corpse, 4: pacifist */
2186 int tmp, mndx, x = mtmp->mx, y = mtmp->my;
2187 struct permonst *mdat;
2188 struct obj *otmp;
2189 struct trap *t;
2190 boolean wasinside = u.uswallow && (u.ustuck == mtmp),
2191 burycorpse = FALSE,
2192 nomsg = (xkill_flags & XKILL_NOMSG) != 0,
2193 nocorpse = (xkill_flags & XKILL_NOCORPSE) != 0,
2194 noconduct = (xkill_flags & XKILL_NOCONDUCT) != 0;
2196 mtmp->mhp = 0; /* caller will usually have already done this */
2197 if (!noconduct) /* KMH, conduct */
2198 u.uconduct.killer++;
2200 if (!nomsg) {
2201 boolean namedpet = has_mname(mtmp) && !Hallucination;
2203 You("%s %s!",
2204 nonliving(mtmp->data) ? "destroy" : "kill",
2205 !(wasinside || canspotmon(mtmp)) ? "it"
2206 : !mtmp->mtame ? mon_nam(mtmp)
2207 : x_monnam(mtmp, namedpet ? ARTICLE_NONE : ARTICLE_THE,
2208 "poor", namedpet ? SUPPRESS_SADDLE : 0, FALSE));
2211 if (mtmp->mtrapped && (t = t_at(x, y)) != 0
2212 && (t->ttyp == PIT || t->ttyp == SPIKED_PIT)) {
2213 if (sobj_at(BOULDER, x, y))
2214 nocorpse = TRUE; /* Prevent corpses/treasure being created
2215 "on top" of boulder that is about to fall in.
2216 This is out of order, but cannot be helped
2217 unless this whole routine is rearranged. */
2218 if (m_carrying(mtmp, BOULDER))
2219 burycorpse = TRUE;
2222 /* your pet knows who just killed it...watch out */
2223 if (mtmp->mtame && !mtmp->isminion)
2224 EDOG(mtmp)->killed_by_u = 1;
2226 if (wasinside && thrownobj && thrownobj != uball) {
2227 /* thrown object has killed hero's engulfer; add it to mon's
2228 inventory now so that it will be placed with mon's other
2229 stuff prior to lookhere/autopickup when hero is expelled
2230 below (as a side-effect, this missile has immunity from
2231 being consumed [for this shot/throw only]) */
2232 mpickobj(mtmp, thrownobj);
2233 /* let throwing code know that missile has been disposed of */
2234 thrownobj = 0;
2237 vamp_rise_msg = FALSE; /* might get set in mondead() */
2238 /* dispose of monster and make cadaver */
2239 if (stoned)
2240 monstone(mtmp);
2241 else
2242 mondead(mtmp);
2244 if (mtmp->mhp > 0) { /* monster lifesaved */
2245 /* Cannot put the non-visible lifesaving message in
2246 * lifesaved_monster() since the message appears only when you
2247 * kill it (as opposed to visible lifesaving which always appears).
2249 stoned = FALSE;
2250 if (!cansee(x, y) && !vamp_rise_msg)
2251 pline("Maybe not...");
2252 return;
2255 mdat = mtmp->data; /* note: mondead can change mtmp->data */
2256 mndx = monsndx(mdat);
2258 if (stoned) {
2259 stoned = FALSE;
2260 goto cleanup;
2263 if (nocorpse || LEVEL_SPECIFIC_NOCORPSE(mdat))
2264 goto cleanup;
2266 #ifdef MAIL
2267 if (mdat == &mons[PM_MAIL_DAEMON]) {
2268 stackobj(mksobj_at(SCR_MAIL, x, y, FALSE, FALSE));
2270 #endif
2271 if (accessible(x, y) || is_pool(x, y)) {
2272 struct obj *cadaver;
2273 int otyp;
2275 /* illogical but traditional "treasure drop" */
2276 if (!rn2(6) && !(mvitals[mndx].mvflags & G_NOCORPSE)
2277 /* no extra item from swallower or steed */
2278 && (x != u.ux || y != u.uy)
2279 /* no extra item from kops--too easy to abuse */
2280 && mdat->mlet != S_KOP
2281 /* no items from cloned monsters */
2282 && !mtmp->mcloned) {
2283 otmp = mkobj(RANDOM_CLASS, TRUE);
2284 /* don't create large objects from small monsters */
2285 otyp = otmp->otyp;
2286 if (mdat->msize < MZ_HUMAN && otyp != FIGURINE
2287 /* oc_big is also oc_bimanual and oc_bulky */
2288 && (otmp->owt > 30 || objects[otyp].oc_big)) {
2289 delobj(otmp);
2290 } else if (!flooreffects(otmp, x, y, nomsg ? "" : "fall")) {
2291 place_object(otmp, x, y);
2292 stackobj(otmp);
2295 /* corpse--none if hero was inside the monster */
2296 if (!wasinside && corpse_chance(mtmp, (struct monst *) 0, FALSE)) {
2297 cadaver = make_corpse(mtmp, burycorpse ? CORPSTAT_BURIED
2298 : CORPSTAT_NONE);
2299 if (burycorpse && cadaver && cansee(x, y) && !mtmp->minvis
2300 && cadaver->where == OBJ_BURIED && !nomsg) {
2301 pline("%s corpse ends up buried.", s_suffix(Monnam(mtmp)));
2305 if (wasinside)
2306 spoteffects(TRUE); /* poor man's expels() */
2307 /* monster is gone, corpse or other object might now be visible */
2308 newsym(x, y);
2310 cleanup:
2311 /* punish bad behaviour */
2312 if (is_human(mdat)
2313 && (!always_hostile(mdat) && mtmp->malign <= 0)
2314 && (mndx < PM_ARCHEOLOGIST || mndx > PM_WIZARD)
2315 && u.ualign.type != A_CHAOTIC) {
2316 HTelepat &= ~INTRINSIC;
2317 change_luck(-2);
2318 You("murderer!");
2319 if (Blind && !Blind_telepat)
2320 see_monsters(); /* Can't sense monsters any more. */
2322 if ((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)
2323 change_luck(-1);
2324 if (is_unicorn(mdat) && sgn(u.ualign.type) == sgn(mdat->maligntyp)) {
2325 change_luck(-5);
2326 You_feel("guilty...");
2329 /* give experience points */
2330 tmp = experience(mtmp, (int) mvitals[mndx].died);
2331 more_experienced(tmp, 0);
2332 newexplevel(); /* will decide if you go up */
2334 /* adjust alignment points */
2335 if (mtmp->m_id == quest_status.leader_m_id) { /* REAL BAD! */
2336 adjalign(-(u.ualign.record + (int) ALIGNLIM / 2));
2337 pline("That was %sa bad idea...",
2338 u.uevent.qcompleted ? "probably " : "");
2339 } else if (mdat->msound == MS_NEMESIS) { /* Real good! */
2340 adjalign((int) (ALIGNLIM / 4));
2341 } else if (mdat->msound == MS_GUARDIAN) { /* Bad */
2342 adjalign(-(int) (ALIGNLIM / 8));
2343 if (!Hallucination)
2344 pline("That was probably a bad idea...");
2345 else
2346 pline("Whoopsie-daisy!");
2347 } else if (mtmp->ispriest) {
2348 adjalign((p_coaligned(mtmp)) ? -2 : 2);
2349 /* cancel divine protection for killing your priest */
2350 if (p_coaligned(mtmp))
2351 u.ublessed = 0;
2352 if (mdat->maligntyp == A_NONE)
2353 adjalign((int) (ALIGNLIM / 4)); /* BIG bonus */
2354 } else if (mtmp->mtame) {
2355 adjalign(-15); /* bad!! */
2356 /* your god is mighty displeased... */
2357 if (!Hallucination)
2358 You_hear("the rumble of distant thunder...");
2359 else
2360 You_hear("the studio audience applaud!");
2361 } else if (mtmp->mpeaceful)
2362 adjalign(-5);
2364 /* malign was already adjusted for u.ualign.type and randomization */
2365 adjalign(mtmp->malign);
2368 /* changes the monster into a stone monster of the same type
2369 this should only be called when poly_when_stoned() is true */
2370 void
2371 mon_to_stone(mtmp)
2372 struct monst *mtmp;
2374 if (mtmp->data->mlet == S_GOLEM) {
2375 /* it's a golem, and not a stone golem */
2376 if (canseemon(mtmp))
2377 pline("%s solidifies...", Monnam(mtmp));
2378 if (newcham(mtmp, &mons[PM_STONE_GOLEM], FALSE, FALSE)) {
2379 if (canseemon(mtmp))
2380 pline("Now it's %s.", an(mtmp->data->mname));
2381 } else {
2382 if (canseemon(mtmp))
2383 pline("... and returns to normal.");
2385 } else
2386 impossible("Can't polystone %s!", a_monnam(mtmp));
2389 boolean
2390 vamp_stone(mtmp)
2391 struct monst *mtmp;
2393 if (is_vampshifter(mtmp)) {
2394 int mndx = mtmp->cham;
2395 int x = mtmp->mx, y = mtmp->my;
2397 /* this only happens if shapeshifted */
2398 if (mndx >= LOW_PM && mndx != monsndx(mtmp->data)
2399 && !(mvitals[mndx].mvflags & G_GENOD)) {
2400 char buf[BUFSZ];
2401 boolean in_door = (amorphous(mtmp->data)
2402 && closed_door(mtmp->mx, mtmp->my));
2404 /* construct a format string before transformation */
2405 Sprintf(buf, "The lapidifying %s %s %s",
2406 x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
2407 (SUPPRESS_SADDLE | SUPPRESS_HALLUCINATION
2408 | SUPPRESS_INVISIBLE | SUPPRESS_IT), FALSE),
2409 amorphous(mtmp->data) ? "coalesces on the"
2410 : is_flyer(mtmp->data) ? "drops to the"
2411 : "writhes on the",
2412 surface(x,y));
2413 mtmp->mcanmove = 1;
2414 mtmp->mfrozen = 0;
2415 if (mtmp->mhpmax <= 0)
2416 mtmp->mhpmax = 10;
2417 mtmp->mhp = mtmp->mhpmax;
2418 /* this can happen if previously a fog cloud */
2419 if (u.uswallow && (mtmp == u.ustuck))
2420 expels(mtmp, mtmp->data, FALSE);
2421 if (in_door) {
2422 coord new_xy;
2424 if (enexto(&new_xy, mtmp->mx, mtmp->my, &mons[mndx])) {
2425 rloc_to(mtmp, new_xy.x, new_xy.y);
2428 if (canspotmon(mtmp)) {
2429 pline("%s!", buf);
2430 display_nhwindow(WIN_MESSAGE, FALSE);
2432 newcham(mtmp, &mons[mndx], FALSE, FALSE);
2433 if (mtmp->data == &mons[mndx])
2434 mtmp->cham = NON_PM;
2435 else
2436 mtmp->cham = mndx;
2437 if (canspotmon(mtmp)) {
2438 pline("%s rises from the %s with renewed agility!",
2439 Amonnam(mtmp), surface(mtmp->mx, mtmp->my));
2441 newsym(mtmp->mx, mtmp->my);
2442 return FALSE; /* didn't petrify */
2445 return TRUE;
2448 /* make monster mtmp next to you (if possible);
2449 might place monst on far side of a wall or boulder */
2450 void
2451 mnexto(mtmp)
2452 struct monst *mtmp;
2454 coord mm;
2455 boolean couldspot = canspotmon(mtmp);
2457 if (mtmp == u.usteed) {
2458 /* Keep your steed in sync with you instead */
2459 mtmp->mx = u.ux;
2460 mtmp->my = u.uy;
2461 return;
2464 if (!enexto(&mm, u.ux, u.uy, mtmp->data))
2465 return;
2466 rloc_to(mtmp, mm.x, mm.y);
2467 if (!in_mklev && (mtmp->mstrategy & STRAT_APPEARMSG)) {
2468 mtmp->mstrategy &= ~STRAT_APPEARMSG; /* one chance only */
2469 if (!couldspot && canspotmon(mtmp))
2470 pline("%s suddenly %s!", Amonnam(mtmp),
2471 !Blind ? "appears" : "arrives");
2473 return;
2476 /* like mnexto() but requires destination to be directly accessible */
2477 void
2478 maybe_mnexto(mtmp)
2479 struct monst *mtmp;
2481 coord mm;
2482 struct permonst *ptr = mtmp->data;
2483 boolean diagok = !NODIAG(ptr - mons);
2484 int tryct = 20;
2486 do {
2487 if (!enexto(&mm, u.ux, u.uy, ptr))
2488 return;
2489 if (couldsee(mm.x, mm.y)
2490 /* don't move grid bugs diagonally */
2491 && (diagok || mm.x == mtmp->mx || mm.y == mtmp->my)) {
2492 rloc_to(mtmp, mm.x, mm.y);
2493 return;
2495 } while (--tryct > 0);
2498 /* mnearto()
2499 * Put monster near (or at) location if possible.
2500 * Returns:
2501 * 1 - if a monster was moved from x, y to put mtmp at x, y.
2502 * 0 - in most cases.
2504 boolean
2505 mnearto(mtmp, x, y, move_other)
2506 register struct monst *mtmp;
2507 xchar x, y;
2508 boolean move_other; /* make sure mtmp gets to x, y! so move m_at(x, y) */
2510 struct monst *othermon = (struct monst *) 0;
2511 xchar newx, newy;
2512 coord mm;
2514 if (mtmp->mx == x && mtmp->my == y)
2515 return FALSE;
2517 if (move_other && (othermon = m_at(x, y)) != 0) {
2518 if (othermon->wormno)
2519 remove_worm(othermon);
2520 else
2521 remove_monster(x, y);
2524 newx = x;
2525 newy = y;
2526 if (!goodpos(newx, newy, mtmp, 0)) {
2527 /* Actually we have real problems if enexto ever fails.
2528 * Migrating_mons that need to be placed will cause
2529 * no end of trouble.
2531 if (!enexto(&mm, newx, newy, mtmp->data))
2532 return FALSE;
2533 newx = mm.x;
2534 newy = mm.y;
2536 rloc_to(mtmp, newx, newy);
2538 if (move_other && othermon) {
2539 xchar oldx = othermon->mx, oldy = othermon->my;
2541 othermon->mx = othermon->my = 0;
2542 (void) mnearto(othermon, x, y, FALSE);
2543 if (othermon->mx == 0 && othermon->my == 0) {
2544 /* reloc failed, dump monster into "limbo"
2545 (aka migrate to current level) */
2546 othermon->mx = oldx;
2547 othermon->my = oldy;
2548 mdrop_special_objs(othermon);
2549 migrate_to_level(othermon, ledger_no(&u.uz), MIGR_APPROX_XY, NULL);
2553 return FALSE;
2556 /* monster responds to player action; not the same as a passive attack;
2557 assumes reason for response has been tested, and response _must_ be made */
2558 void
2559 m_respond(mtmp)
2560 struct monst *mtmp;
2562 if (mtmp->data->msound == MS_SHRIEK) {
2563 if (!Deaf) {
2564 pline("%s shrieks.", Monnam(mtmp));
2565 stop_occupation();
2567 if (!rn2(10)) {
2568 if (!rn2(13))
2569 (void) makemon(&mons[PM_PURPLE_WORM], 0, 0, NO_MM_FLAGS);
2570 else
2571 (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
2573 aggravate();
2575 if (mtmp->data == &mons[PM_MEDUSA]) {
2576 register int i;
2578 for (i = 0; i < NATTK; i++)
2579 if (mtmp->data->mattk[i].aatyp == AT_GAZE) {
2580 (void) gazemu(mtmp, &mtmp->data->mattk[i]);
2581 break;
2586 void
2587 setmangry(mtmp)
2588 struct monst *mtmp;
2590 mtmp->mstrategy &= ~STRAT_WAITMASK;
2591 if (!mtmp->mpeaceful)
2592 return;
2593 if (mtmp->mtame)
2594 return;
2595 mtmp->mpeaceful = 0;
2596 if (mtmp->ispriest) {
2597 if (p_coaligned(mtmp))
2598 adjalign(-5); /* very bad */
2599 else
2600 adjalign(2);
2601 } else
2602 adjalign(-1); /* attacking peaceful monsters is bad */
2603 if (couldsee(mtmp->mx, mtmp->my)) {
2604 if (humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
2605 pline("%s gets angry!", Monnam(mtmp));
2606 else if (flags.verbose && !Deaf)
2607 growl(mtmp);
2610 /* attacking your own quest leader will anger his or her guardians */
2611 if (!context.mon_moving /* should always be the case here */
2612 && mtmp->data == &mons[quest_info(MS_LEADER)]) {
2613 struct monst *mon;
2614 struct permonst *q_guardian = &mons[quest_info(MS_GUARDIAN)];
2615 int got_mad = 0;
2617 /* guardians will sense this attack even if they can't see it */
2618 for (mon = fmon; mon; mon = mon->nmon) {
2619 if (DEADMONSTER(mon))
2620 continue;
2621 if (mon->data == q_guardian && mon->mpeaceful) {
2622 mon->mpeaceful = 0;
2623 if (canseemon(mon))
2624 ++got_mad;
2627 if (got_mad && !Hallucination)
2628 pline_The("%s appear%s to be angry too...",
2629 got_mad == 1 ? q_guardian->mname
2630 : makeplural(q_guardian->mname),
2631 got_mad == 1 ? "s" : "");
2634 /* make other peaceful monsters react */
2635 if (!context.mon_moving) {
2636 struct monst *mon;
2638 for (mon = fmon; mon; mon = mon->nmon) {
2639 if (DEADMONSTER(mon))
2640 continue;
2641 if (!mindless(mon->data) && mon->mpeaceful
2642 && couldsee(mon->mx, mon->my) && !mon->msleeping
2643 && mon->mcansee && m_canseeu(mon)) {
2644 boolean exclaimed = FALSE;
2646 if (humanoid(mon->data) || mon->isshk || mon->ispriest) {
2647 if (is_watch(mon->data)) {
2648 verbalize("Halt! You're under arrest!");
2649 (void) angry_guards(!!Deaf);
2650 } else {
2651 const char *exclam[] = {
2652 "Gasp!", "Uh-oh.", "Oh my!", "What?", "Why?"
2654 if (!rn2(5)) {
2655 verbalize("%s", exclam[mon->m_id % SIZE(exclam)]);
2656 exclaimed = TRUE;
2658 if (!mon->isshk && !mon->ispriest
2659 && (mon->data->mlevel < rn2(10))) {
2660 monflee(mon, rn2(50)+25, TRUE, !exclaimed);
2661 exclaimed = TRUE;
2663 if (!mon->isshk && !mon->ispriest) {
2664 mon->mpeaceful = 0;
2665 adjalign(-1);
2666 if (!exclaimed)
2667 pline("%s gets angry!", Monnam(mon));
2670 } else if ((mtmp->data == mon->data) && !rn2(3)) {
2671 if (!rn2(4)) {
2672 growl(mon);
2673 exclaimed = TRUE;
2675 if (rn2(6))
2676 monflee(mon, rn2(25)+15, TRUE, !exclaimed);
2684 /* wake up a monster, usually making it angry in the process */
2685 void
2686 wakeup(mtmp)
2687 register struct monst *mtmp;
2689 mtmp->msleeping = 0;
2690 finish_meating(mtmp);
2691 setmangry(mtmp);
2692 if (mtmp->m_ap_type) {
2693 seemimic(mtmp);
2694 } else if (context.forcefight && !context.mon_moving
2695 && mtmp->mundetected) {
2696 mtmp->mundetected = 0;
2697 newsym(mtmp->mx, mtmp->my);
2701 /* Wake up nearby monsters without angering them. */
2702 void
2703 wake_nearby()
2705 register struct monst *mtmp;
2707 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2708 if (DEADMONSTER(mtmp))
2709 continue;
2710 if (distu(mtmp->mx, mtmp->my) < u.ulevel * 20) {
2711 mtmp->msleeping = 0;
2712 if (!unique_corpstat(mtmp->data))
2713 mtmp->mstrategy &= ~STRAT_WAITMASK;
2714 if (mtmp->mtame && !mtmp->isminion)
2715 EDOG(mtmp)->whistletime = moves;
2720 /* Wake up monsters near some particular location. */
2721 void
2722 wake_nearto(x, y, distance)
2723 register int x, y, distance;
2725 register struct monst *mtmp;
2727 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2728 if (DEADMONSTER(mtmp))
2729 continue;
2730 if (distance == 0 || dist2(mtmp->mx, mtmp->my, x, y) < distance) {
2731 mtmp->msleeping = 0;
2732 if (!unique_corpstat(mtmp->data))
2733 mtmp->mstrategy &= ~STRAT_WAITMASK;
2738 /* NOTE: we must check for mimicry before calling this routine */
2739 void
2740 seemimic(mtmp)
2741 register struct monst *mtmp;
2743 boolean is_blocker_appear = (is_lightblocker_mappear(mtmp));
2745 if (has_mcorpsenm(mtmp))
2746 freemcorpsenm(mtmp);
2748 mtmp->m_ap_type = M_AP_NOTHING;
2749 mtmp->mappearance = 0;
2752 * Discovered mimics don't block light.
2754 if (is_blocker_appear
2755 && !does_block(mtmp->mx, mtmp->my, &levl[mtmp->mx][mtmp->my]))
2756 unblock_point(mtmp->mx, mtmp->my);
2758 newsym(mtmp->mx, mtmp->my);
2761 /* force all chameleons to become normal */
2762 void
2763 rescham()
2765 register struct monst *mtmp;
2766 int mcham;
2768 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2769 if (DEADMONSTER(mtmp))
2770 continue;
2771 mcham = (int) mtmp->cham;
2772 if (mcham >= LOW_PM) {
2773 (void) newcham(mtmp, &mons[mcham], FALSE, FALSE);
2774 mtmp->cham = NON_PM;
2776 if (is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
2777 new_were(mtmp);
2778 if (mtmp->m_ap_type && cansee(mtmp->mx, mtmp->my)) {
2779 seemimic(mtmp);
2780 /* we pretend that the mimic doesn't
2781 know that it has been unmasked */
2782 mtmp->msleeping = 1;
2787 /* Let the chameleons change again -dgk */
2788 void
2789 restartcham()
2791 register struct monst *mtmp;
2793 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
2794 if (DEADMONSTER(mtmp))
2795 continue;
2796 mtmp->cham = pm_to_cham(monsndx(mtmp->data));
2797 if (mtmp->data->mlet == S_MIMIC && mtmp->msleeping
2798 && cansee(mtmp->mx, mtmp->my)) {
2799 set_mimic_sym(mtmp);
2800 newsym(mtmp->mx, mtmp->my);
2805 /* called when restoring a monster from a saved level; protection
2806 against shape-changing might be different now than it was at the
2807 time the level was saved. */
2808 void
2809 restore_cham(mon)
2810 struct monst *mon;
2812 int mcham;
2814 if (Protection_from_shape_changers) {
2815 mcham = (int) mon->cham;
2816 if (mcham >= LOW_PM) {
2817 mon->cham = NON_PM;
2818 (void) newcham(mon, &mons[mcham], FALSE, FALSE);
2819 } else if (is_were(mon->data) && !is_human(mon->data)) {
2820 new_were(mon);
2822 } else if (mon->cham == NON_PM) {
2823 mon->cham = pm_to_cham(monsndx(mon->data));
2827 /* unwatched hiders may hide again; if so, returns True */
2828 STATIC_OVL boolean
2829 restrap(mtmp)
2830 register struct monst *mtmp;
2832 struct trap *t;
2834 if (mtmp->mcan || mtmp->m_ap_type || cansee(mtmp->mx, mtmp->my)
2835 || rn2(3) || mtmp == u.ustuck
2836 /* can't hide while trapped except in pits */
2837 || (mtmp->mtrapped && (t = t_at(mtmp->mx, mtmp->my)) != 0
2838 && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))
2839 || (sensemon(mtmp) && distu(mtmp->mx, mtmp->my) <= 2))
2840 return FALSE;
2842 if (mtmp->data->mlet == S_MIMIC) {
2843 set_mimic_sym(mtmp);
2844 return TRUE;
2845 } else if (levl[mtmp->mx][mtmp->my].typ == ROOM) {
2846 mtmp->mundetected = 1;
2847 return TRUE;
2850 return FALSE;
2853 /* monster/hero tries to hide under something at the current location */
2854 boolean
2855 hideunder(mtmp)
2856 struct monst *mtmp;
2858 struct trap *t;
2859 boolean undetected = FALSE, is_u = (mtmp == &youmonst);
2860 xchar x = is_u ? u.ux : mtmp->mx, y = is_u ? u.uy : mtmp->my;
2862 if (mtmp == u.ustuck) {
2863 ; /* can't hide if holding you or held by you */
2864 } else if (is_u ? (u.utrap && u.utraptype != TT_PIT)
2865 : (mtmp->mtrapped && (t = t_at(x, y)) != 0
2866 && !(t->ttyp == PIT || t->ttyp == SPIKED_PIT))) {
2867 ; /* can't hide while stuck in a non-pit trap */
2868 } else if (mtmp->data->mlet == S_EEL) {
2869 undetected = (is_pool(x, y) && !Is_waterlevel(&u.uz));
2870 } else if (hides_under(mtmp->data) && OBJ_AT(x, y)) {
2871 struct obj *otmp = level.objects[x][y];
2873 /* most monsters won't hide under cockatrice corpse */
2874 if (otmp->nexthere || otmp->otyp != CORPSE
2875 || (mtmp == &youmonst ? Stone_resistance : resists_ston(mtmp))
2876 || !touch_petrifies(&mons[otmp->corpsenm]))
2877 undetected = TRUE;
2880 if (is_u)
2881 u.uundetected = undetected;
2882 else
2883 mtmp->mundetected = undetected;
2884 return undetected;
2887 /* called when returning to a previously visited level */
2888 void
2889 hide_monst(mon)
2890 struct monst *mon;
2892 boolean hider_under = hides_under(mon->data) || mon->data->mlet == S_EEL;
2894 if ((is_hider(mon->data) || hider_under)
2895 && !(mon->mundetected || mon->m_ap_type)) {
2896 xchar x = mon->mx, y = mon->my;
2897 char save_viz = viz_array[y][x];
2899 /* override vision, forcing hero to be unable to see monster's spot */
2900 viz_array[y][x] &= ~(IN_SIGHT | COULD_SEE);
2901 if (is_hider(mon->data))
2902 (void) restrap(mon);
2903 /* try again if mimic missed its 1/3 chance to hide */
2904 if (mon->data->mlet == S_MIMIC && !mon->m_ap_type)
2905 (void) restrap(mon);
2906 if (hider_under)
2907 (void) hideunder(mon);
2908 viz_array[y][x] = save_viz;
2912 static short *animal_list = 0; /* list of PM values for animal monsters */
2913 static int animal_list_count;
2915 void
2916 mon_animal_list(construct)
2917 boolean construct;
2919 if (construct) {
2920 short animal_temp[SPECIAL_PM];
2921 int i, n;
2923 /* if (animal_list) impossible("animal_list already exists"); */
2925 for (n = 0, i = LOW_PM; i < SPECIAL_PM; i++)
2926 if (is_animal(&mons[i]))
2927 animal_temp[n++] = i;
2928 /* if (n == 0) animal_temp[n++] = NON_PM; */
2930 animal_list = (short *) alloc(n * sizeof *animal_list);
2931 (void) memcpy((genericptr_t) animal_list, (genericptr_t) animal_temp,
2932 n * sizeof *animal_list);
2933 animal_list_count = n;
2934 } else { /* release */
2935 if (animal_list)
2936 free((genericptr_t) animal_list), animal_list = 0;
2937 animal_list_count = 0;
2941 STATIC_OVL int
2942 pick_animal()
2944 int res;
2946 if (!animal_list)
2947 mon_animal_list(TRUE);
2949 res = animal_list[rn2(animal_list_count)];
2950 /* rogue level should use monsters represented by uppercase letters
2951 only, but since chameleons aren't generated there (not uppercase!)
2952 we don't perform a lot of retries */
2953 if (Is_rogue_level(&u.uz) && !isupper((uchar) mons[res].mlet))
2954 res = animal_list[rn2(animal_list_count)];
2955 return res;
2958 void
2959 decide_to_shapeshift(mon, shiftflags)
2960 struct monst *mon;
2961 int shiftflags;
2963 struct permonst *ptr;
2964 unsigned was_female = mon->female;
2965 boolean msg = FALSE;
2967 if ((shiftflags & SHIFT_MSG)
2968 || ((shiftflags & SHIFT_SEENMSG) && sensemon(mon)))
2969 msg = TRUE;
2971 if (!is_vampshifter(mon)) {
2972 /* regular shapeshifter */
2973 if (!rn2(6))
2974 (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
2975 } else {
2976 /* The vampire has to be in good health (mhp) to maintain
2977 * its shifted form.
2979 * If we're shifted and getting low on hp, maybe shift back.
2980 * If we're not already shifted and in good health, maybe shift.
2982 if (mon->data->mlet != S_VAMPIRE) {
2983 if ((mon->mhp <= (mon->mhpmax + 5) / 6) && rn2(4)
2984 && mon->cham >= LOW_PM)
2985 (void) newcham(mon, &mons[mon->cham], FALSE, msg);
2986 } else {
2987 if (mon->mhp >= 9 * mon->mhpmax / 10 && !rn2(6)
2988 && (!canseemon(mon)
2989 || distu(mon->mx, mon->my) > BOLT_LIM * BOLT_LIM))
2990 (void) newcham(mon, (struct permonst *) 0, FALSE, msg);
2992 /* override the 10% chance for sex change */
2993 ptr = mon->data;
2994 if (!is_male(ptr) && !is_female(ptr) && !is_neuter(ptr))
2995 mon->female = was_female;
2999 STATIC_OVL int
3000 pickvampshape(mon)
3001 struct monst *mon;
3003 int mndx = mon->cham, wolfchance = 10;
3004 /* avoid picking monsters with lowercase display symbols ('d' for wolf
3005 and 'v' for fog cloud) on rogue level*/
3006 boolean uppercase_only = Is_rogue_level(&u.uz);
3008 switch (mndx) {
3009 case PM_VLAD_THE_IMPALER:
3010 /* ensure Vlad can keep carrying the Candelabrum */
3011 if (mon_has_special(mon))
3012 break; /* leave mndx as is */
3013 wolfchance = 3;
3014 /*FALLTHRU*/
3015 case PM_VAMPIRE_LORD: /* vampire lord or Vlad can become wolf */
3016 if (!rn2(wolfchance) && !uppercase_only) {
3017 mndx = PM_WOLF;
3018 break;
3020 /*FALLTHRU*/
3021 case PM_VAMPIRE: /* any vampire can become fog or bat */
3022 mndx = (!rn2(4) && !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
3023 break;
3025 return mndx;
3028 /* nonshapechangers who warrant special polymorph handling */
3029 STATIC_OVL boolean
3030 isspecmon(mon)
3031 struct monst *mon;
3033 return (mon->isshk || mon->ispriest || mon->isgd
3034 || mon->m_id == quest_status.leader_m_id);
3037 /* restrict certain special monsters (shopkeepers, aligned priests,
3038 vault guards) to forms that allow them to behave sensibly (catching
3039 gold, speaking?) so that they don't need too much extra code */
3040 STATIC_OVL boolean
3041 validspecmon(mon, mndx)
3042 struct monst *mon;
3043 int mndx;
3045 if (mndx == NON_PM)
3046 return TRUE; /* caller wants random */
3048 if (!accept_newcham_form(mndx))
3049 return FALSE; /* geno'd or !polyok */
3051 if (isspecmon(mon)) {
3052 struct permonst *ptr = &mons[mndx];
3054 /* reject notake because object manipulation is expected
3055 and nohead because speech capability is expected */
3056 if (notake(ptr) || !has_head(ptr))
3057 return FALSE;
3058 /* [should we check ptr->msound here too?] */
3060 return TRUE; /* potential new form is ok */
3063 /* prevent wizard mode user from specifying invalid vampshifter shape */
3064 STATIC_OVL boolean
3065 validvamp(mon, mndx_p, monclass)
3066 struct monst *mon;
3067 int *mndx_p, monclass;
3069 /* simplify caller's usage */
3070 if (!is_vampshifter(mon))
3071 return validspecmon(mon, *mndx_p);
3073 if (*mndx_p == PM_VAMPIRE || *mndx_p == PM_VAMPIRE_LORD
3074 || *mndx_p == PM_VLAD_THE_IMPALER) {
3075 /* player picked some type of vampire; use mon's self */
3076 *mndx_p = mon->cham;
3077 return TRUE;
3079 if (mon->cham == PM_VLAD_THE_IMPALER && mon_has_special(mon)) {
3080 /* Vlad with Candelabrum; override choice, then accept it */
3081 *mndx_p = PM_VLAD_THE_IMPALER;
3082 return TRUE;
3084 /* basic vampires can't become wolves; any can become fog or bat
3085 (we don't enforce upper-case only for rogue level here) */
3086 if (*mndx_p == PM_WOLF)
3087 return (boolean) (mon->cham != PM_VAMPIRE);
3088 if (*mndx_p == PM_FOG_CLOUD || *mndx_p == PM_VAMPIRE_BAT)
3089 return TRUE;
3091 /* if we get here, specific type was no good; try by class */
3092 switch (monclass) {
3093 case S_VAMPIRE:
3094 *mndx_p = mon->cham;
3095 break;
3096 case S_BAT:
3097 *mndx_p = PM_VAMPIRE_BAT;
3098 break;
3099 case S_VORTEX:
3100 *mndx_p = PM_FOG_CLOUD;
3101 break;
3102 case S_DOG:
3103 if (mon->cham != PM_VAMPIRE) {
3104 *mndx_p = PM_WOLF;
3105 break;
3107 /*FALLTHRU*/
3108 default:
3109 *mndx_p = NON_PM;
3110 break;
3112 return (boolean) (*mndx_p != NON_PM);
3116 select_newcham_form(mon)
3117 struct monst *mon;
3119 int mndx = NON_PM, tryct;
3121 switch (mon->cham) {
3122 case PM_SANDESTIN:
3123 if (rn2(7))
3124 mndx = pick_nasty();
3125 break;
3126 case PM_DOPPELGANGER:
3127 if (!rn2(7)) {
3128 mndx = pick_nasty();
3129 } else if (rn2(3)) { /* role monsters */
3130 mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
3131 } else if (!rn2(3)) { /* quest guardians */
3132 mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
3133 /* avoid own role's guardian */
3134 if (mndx == urole.guardnum)
3135 mndx = NON_PM;
3136 } else { /* general humanoids */
3137 tryct = 5;
3138 do {
3139 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3140 if (humanoid(&mons[mndx]) && polyok(&mons[mndx]))
3141 break;
3142 } while (--tryct > 0);
3143 if (!tryct)
3144 mndx = NON_PM;
3146 break;
3147 case PM_CHAMELEON:
3148 if (!rn2(3))
3149 mndx = pick_animal();
3150 break;
3151 case PM_VLAD_THE_IMPALER:
3152 case PM_VAMPIRE_LORD:
3153 case PM_VAMPIRE:
3154 mndx = pickvampshape(mon);
3155 break;
3156 case NON_PM: /* ordinary */
3158 struct obj *m_armr = which_armor(mon, W_ARM);
3160 if (m_armr && Is_dragon_scales(m_armr))
3161 mndx = (int) (Dragon_scales_to_pm(m_armr) - mons);
3162 else if (m_armr && Is_dragon_mail(m_armr))
3163 mndx = (int) (Dragon_mail_to_pm(m_armr) - mons);
3165 break;
3168 /* for debugging: allow control of polymorphed monster */
3169 if (wizard && iflags.mon_polycontrol) {
3170 char pprompt[BUFSZ], buf[BUFSZ];
3171 int monclass;
3173 Sprintf(pprompt, "Change %s @ %s into what kind of monster?",
3174 noit_mon_nam(mon),
3175 coord_desc((int) mon->mx, (int) mon->my, buf,
3176 (iflags.getpos_coords != GPCOORDS_NONE)
3177 ? iflags.getpos_coords : GPCOORDS_MAP));
3178 tryct = 5;
3179 do {
3180 monclass = 0;
3181 getlin(pprompt, buf);
3182 mungspaces(buf);
3183 /* for ESC, take form selected above (might be NON_PM) */
3184 if (*buf == '\033')
3185 break;
3186 /* for "*", use NON_PM to pick an arbitrary shape below */
3187 if (!strcmp(buf, "*") || !strcmp(buf, "random")) {
3188 mndx = NON_PM;
3189 break;
3191 mndx = name_to_mon(buf);
3192 if (mndx == NON_PM) {
3193 /* didn't get a type, so check whether it's a class
3194 (single letter or text match with def_monsyms[]) */
3195 monclass = name_to_monclass(buf, &mndx);
3196 if (monclass && mndx == NON_PM)
3197 mndx = mkclass_poly(monclass);
3199 if (mndx >= LOW_PM) {
3200 /* got a specific type of monster; use it if we can */
3201 if (validvamp(mon, &mndx, monclass))
3202 break;
3203 /* can't; revert to random in case we exhaust tryct */
3204 mndx = NON_PM;
3207 pline("It can't become that.");
3208 } while (--tryct > 0);
3209 if (!tryct)
3210 pline1(thats_enough_tries);
3211 if (is_vampshifter(mon) && !validvamp(mon, &mndx, monclass))
3212 mndx = pickvampshape(mon); /* don't resort to arbitrary */
3215 /* if no form was specified above, pick one at random now */
3216 if (mndx == NON_PM) {
3217 tryct = 50;
3218 do {
3219 mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
3220 } while (--tryct > 0 && !validspecmon(mon, mndx)
3221 /* try harder to select uppercase monster on rogue level */
3222 && (tryct > 40 && Is_rogue_level(&u.uz)
3223 && !isupper((uchar) mons[mndx].mlet)));
3225 return mndx;
3228 /* this used to be inline within newcham() but monpolycontrol needs it too */
3229 STATIC_OVL struct permonst *
3230 accept_newcham_form(mndx)
3231 int mndx;
3233 struct permonst *mdat;
3235 if (mndx == NON_PM)
3236 return 0;
3237 mdat = &mons[mndx];
3238 if ((mvitals[mndx].mvflags & G_GENOD) != 0)
3239 return 0;
3240 if (is_placeholder(mdat))
3241 return 0;
3242 /* select_newcham_form() might deliberately pick a player
3243 character type (random selection never does) which
3244 polyok() rejects, so we need a special case here */
3245 if (is_mplayer(mdat))
3246 return mdat;
3247 /* polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops */
3248 return polyok(mdat) ? mdat : 0;
3251 void
3252 mgender_from_permonst(mtmp, mdat)
3253 struct monst *mtmp;
3254 struct permonst *mdat;
3256 if (is_male(mdat)) {
3257 if (mtmp->female)
3258 mtmp->female = FALSE;
3259 } else if (is_female(mdat)) {
3260 if (!mtmp->female)
3261 mtmp->female = TRUE;
3262 } else if (!is_neuter(mdat)) {
3263 if (!rn2(10))
3264 mtmp->female = !mtmp->female;
3268 /* make a chameleon take on another shape, or a polymorph target
3269 (possibly self-inflicted) become a different monster;
3270 returns 1 if it actually changes form */
3272 newcham(mtmp, mdat, polyspot, msg)
3273 struct monst *mtmp;
3274 struct permonst *mdat;
3275 boolean polyspot; /* change is the result of wand or spell of polymorph */
3276 boolean msg; /* "The oldmon turns into a newmon!" */
3278 int hpn, hpd;
3279 int mndx, tryct;
3280 struct permonst *olddata = mtmp->data;
3281 char *p, oldname[BUFSZ], l_oldname[BUFSZ], newname[BUFSZ];
3283 /* Riders are immune to polymorph and green slime
3284 (but apparent Rider might actually be a doppelganger) */
3285 if (mtmp->cham == NON_PM) { /* not a shapechanger */
3286 if (is_rider(olddata))
3287 return 0;
3288 /* make Nazgul and erinyes immune too, to reduce chance of
3289 anomalous extinction feedback during final disclsoure */
3290 if (mbirth_limit(monsndx(olddata)) < MAXMONNO)
3291 return 0;
3294 if (msg) {
3295 /* like Monnam() but never mention saddle */
3296 Strcpy(oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3297 SUPPRESS_SADDLE, FALSE));
3298 oldname[0] = highc(oldname[0]);
3300 /* we need this one whether msg is true or not */
3301 Strcpy(l_oldname, x_monnam(mtmp, ARTICLE_THE, (char *) 0,
3302 has_mname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE));
3304 /* mdat = 0 -> caller wants a random monster shape */
3305 if (mdat == 0) {
3306 /* select_newcham_form() loops when resorting to random but
3307 it doesn't always pick that so we still retry here too */
3308 tryct = 20;
3309 do {
3310 mndx = select_newcham_form(mtmp);
3311 mdat = accept_newcham_form(mndx);
3312 /* for the first several tries we require upper-case on
3313 the rogue level (after that, we take whatever we get) */
3314 if (tryct > 15 && Is_rogue_level(&u.uz)
3315 && mdat && !isupper((uchar) mdat->mlet))
3316 mdat = 0;
3317 if (mdat)
3318 break;
3319 } while (--tryct > 0);
3320 if (!tryct)
3321 return 0;
3322 } else if (mvitals[monsndx(mdat)].mvflags & G_GENOD)
3323 return 0; /* passed in mdat is genocided */
3325 if (mdat == olddata)
3326 return 0; /* still the same monster */
3328 mgender_from_permonst(mtmp, mdat);
3329 /* Endgame mplayers start out as "Foo the Bar", but some of the
3330 * titles are inappropriate when polymorphed, particularly into
3331 * the opposite sex. Player characters don't use ranks when
3332 * polymorphed, so dropping rank for mplayers seems reasonable.
3334 if (In_endgame(&u.uz) && is_mplayer(olddata)
3335 && has_mname(mtmp) && (p = strstr(MNAME(mtmp), " the ")) != 0)
3336 *p = '\0';
3338 if (mtmp->wormno) { /* throw tail away */
3339 wormgone(mtmp);
3340 place_monster(mtmp, mtmp->mx, mtmp->my);
3342 if (mtmp->m_ap_type && mdat->mlet != S_MIMIC)
3343 seemimic(mtmp); /* revert to normal monster */
3345 /* (this code used to try to adjust the monster's health based on
3346 a normal one of its type but there are too many special cases
3347 which need to handled in order to do that correctly, so just
3348 give the new form the same proportion of HP as its old one had) */
3349 hpn = mtmp->mhp;
3350 hpd = mtmp->mhpmax;
3351 /* set level and hit points */
3352 newmonhp(mtmp, monsndx(mdat));
3353 /* new hp: same fraction of max as before */
3354 #ifndef LINT
3355 mtmp->mhp = (int) (((long) hpn * (long) mtmp->mhp) / (long) hpd);
3356 #endif
3357 /* sanity check (potential overflow) */
3358 if (mtmp->mhp < 0 || mtmp->mhp > mtmp->mhpmax)
3359 mtmp->mhp = mtmp->mhpmax;
3360 /* unlikely but not impossible; a 1HD creature with 1HP that changes
3361 into a 0HD creature will require this statement */
3362 if (!mtmp->mhp)
3363 mtmp->mhp = 1;
3365 /* take on the new form... */
3366 set_mon_data(mtmp, mdat, 0);
3368 if (emits_light(olddata) != emits_light(mtmp->data)) {
3369 /* used to give light, now doesn't, or vice versa,
3370 or light's range has changed */
3371 if (emits_light(olddata))
3372 del_light_source(LS_MONSTER, monst_to_any(mtmp));
3373 if (emits_light(mtmp->data))
3374 new_light_source(mtmp->mx, mtmp->my, emits_light(mtmp->data),
3375 LS_MONSTER, monst_to_any(mtmp));
3377 if (!mtmp->perminvis || pm_invisible(olddata))
3378 mtmp->perminvis = pm_invisible(mdat);
3379 mtmp->minvis = mtmp->invis_blkd ? 0 : mtmp->perminvis;
3380 if (mtmp->mundetected)
3381 (void) hideunder(mtmp);
3382 if (u.ustuck == mtmp) {
3383 if (u.uswallow) {
3384 if (!attacktype(mdat, AT_ENGL)) {
3385 /* Does mdat care? */
3386 if (!noncorporeal(mdat) && !amorphous(mdat)
3387 && !is_whirly(mdat) && (mdat != &mons[PM_YELLOW_LIGHT])) {
3388 char msgtrail[BUFSZ];
3390 if (is_vampshifter(mtmp)) {
3391 Sprintf(msgtrail, " which was a shapeshifted %s",
3392 m_monnam(mtmp));
3393 } else if (is_animal(mdat)) {
3394 Strcpy(msgtrail, "'s stomach");
3395 } else {
3396 msgtrail[0] = '\0';
3399 /* Do this even if msg is FALSE */
3400 You("%s %s%s!",
3401 (amorphous(olddata) || is_whirly(olddata))
3402 ? "emerge from" : "break out of",
3403 l_oldname, msgtrail);
3404 msg = FALSE; /* message has been given */
3405 mtmp->mhp = 1; /* almost dead */
3407 expels(mtmp, olddata, FALSE);
3408 } else {
3409 /* update swallow glyphs for new monster */
3410 swallowed(0);
3412 } else if (!sticks(mdat) && !sticks(youmonst.data))
3413 unstuck(mtmp);
3416 #ifndef DCC30_BUG
3417 if (mdat == &mons[PM_LONG_WORM] && (mtmp->wormno = get_wormno()) != 0) {
3418 #else
3419 /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the
3420 * same expression.
3422 if (mdat == &mons[PM_LONG_WORM]
3423 && (mtmp->wormno = get_wormno(), mtmp->wormno != 0)) {
3424 #endif
3425 /* we can now create worms with tails - 11/91 */
3426 initworm(mtmp, rn2(5));
3427 if (count_wsegs(mtmp))
3428 place_worm_tail_randomly(mtmp, mtmp->mx, mtmp->my);
3431 newsym(mtmp->mx, mtmp->my);
3433 if (msg) {
3434 char *save_mname = 0;
3436 if (has_mname(mtmp)) {
3437 save_mname = MNAME(mtmp);
3438 MNAME(mtmp) = (char *) 0;
3440 Strcpy(newname, (mdat == &mons[PM_GREEN_SLIME])
3441 ? "slime"
3442 : x_monnam(mtmp, ARTICLE_A, (char *) 0,
3443 SUPPRESS_SADDLE, FALSE));
3444 /* oldname was capitalized above; newname will be lower case */
3445 if (!strcmpi(newname, "it")) { /* can't see or sense it now */
3446 if (!!strcmpi(oldname, "it")) /* could see or sense it before */
3447 pline("%s disappears!", oldname);
3448 (void) usmellmon(mdat);
3449 } else { /* can see or sense it now */
3450 if (!strcmpi(oldname, "it")) /* couldn't see or sense it before */
3451 pline("%s appears!", upstart(newname));
3452 else
3453 pline("%s turns into %s!", oldname, newname);
3455 if (save_mname)
3456 MNAME(mtmp) = save_mname;
3459 /* when polymorph trap/wand/potion produces a vampire, turn in into
3460 a full-fledged vampshifter unless shape-changing is blocked */
3461 if (mtmp->cham == NON_PM && mdat->mlet == S_VAMPIRE
3462 && !Protection_from_shape_changers)
3463 mtmp->cham = pm_to_cham(monsndx(mdat));
3465 possibly_unwield(mtmp, polyspot); /* might lose use of weapon */
3466 mon_break_armor(mtmp, polyspot);
3467 if (!(mtmp->misc_worn_check & W_ARMG))
3468 mselftouch(mtmp, "No longer petrify-resistant, ",
3469 !context.mon_moving);
3470 m_dowear(mtmp, FALSE);
3472 /* This ought to re-test can_carry() on each item in the inventory
3473 * rather than just checking ex-giants & boulders, but that'd be
3474 * pretty expensive to perform. If implemented, then perhaps
3475 * minvent should be sorted in order to drop heaviest items first.
3477 /* former giants can't continue carrying boulders */
3478 if (mtmp->minvent && !throws_rocks(mdat)) {
3479 register struct obj *otmp, *otmp2;
3481 for (otmp = mtmp->minvent; otmp; otmp = otmp2) {
3482 otmp2 = otmp->nobj;
3483 if (otmp->otyp == BOULDER) {
3484 /* this keeps otmp from being polymorphed in the
3485 same zap that the monster that held it is polymorphed */
3486 if (polyspot)
3487 bypass_obj(otmp);
3488 obj_extract_self(otmp);
3489 /* probably ought to give some "drop" message here */
3490 if (flooreffects(otmp, mtmp->mx, mtmp->my, ""))
3491 continue;
3492 place_object(otmp, mtmp->mx, mtmp->my);
3497 return 1;
3500 /* sometimes an egg will be special */
3501 #define BREEDER_EGG (!rn2(77))
3504 * Determine if the given monster number can be hatched from an egg.
3505 * Return the monster number to use as the egg's corpsenm. Return
3506 * NON_PM if the given monster can't be hatched.
3509 can_be_hatched(mnum)
3510 int mnum;
3512 /* ranger quest nemesis has the oviparous bit set, making it
3513 be possible to wish for eggs of that unique monster; turn
3514 such into ordinary eggs rather than forbidding them outright */
3515 if (mnum == PM_SCORPIUS)
3516 mnum = PM_SCORPION;
3518 mnum = little_to_big(mnum);
3520 * Queen bees lay killer bee eggs (usually), but killer bees don't
3521 * grow into queen bees. Ditto for [winged-]gargoyles.
3523 if (mnum == PM_KILLER_BEE || mnum == PM_GARGOYLE
3524 || (lays_eggs(&mons[mnum])
3525 && (BREEDER_EGG
3526 || (mnum != PM_QUEEN_BEE && mnum != PM_WINGED_GARGOYLE))))
3527 return mnum;
3528 return NON_PM;
3531 /* type of egg laid by #sit; usually matches parent */
3533 egg_type_from_parent(mnum, force_ordinary)
3534 int mnum; /* parent monster; caller must handle lays_eggs() check */
3535 boolean force_ordinary;
3537 if (force_ordinary || !BREEDER_EGG) {
3538 if (mnum == PM_QUEEN_BEE)
3539 mnum = PM_KILLER_BEE;
3540 else if (mnum == PM_WINGED_GARGOYLE)
3541 mnum = PM_GARGOYLE;
3543 return mnum;
3546 /* decide whether an egg of the indicated monster type is viable;
3547 also used to determine whether an egg or tin can be created... */
3548 boolean
3549 dead_species(m_idx, egg)
3550 int m_idx;
3551 boolean egg;
3553 int alt_idx;
3555 /* generic eggs are unhatchable and have corpsenm of NON_PM */
3556 if (m_idx < LOW_PM)
3557 return TRUE;
3559 * For monsters with both baby and adult forms, genociding either
3560 * form kills all eggs of that monster. Monsters with more than
3561 * two forms (small->large->giant mimics) are more or less ignored;
3562 * fortunately, none of them have eggs. Species extinction due to
3563 * overpopulation does not kill eggs.
3565 alt_idx = egg ? big_to_little(m_idx) : m_idx;
3566 return (boolean) ((mvitals[m_idx].mvflags & G_GENOD) != 0
3567 || (mvitals[alt_idx].mvflags & G_GENOD) != 0);
3570 /* kill off any eggs of genocided monsters */
3571 STATIC_OVL void
3572 kill_eggs(obj_list)
3573 struct obj *obj_list;
3575 struct obj *otmp;
3577 for (otmp = obj_list; otmp; otmp = otmp->nobj)
3578 if (otmp->otyp == EGG) {
3579 if (dead_species(otmp->corpsenm, TRUE)) {
3581 * It seems we could also just catch this when
3582 * it attempted to hatch, so we wouldn't have to
3583 * search all of the objlists.. or stop all
3584 * hatch timers based on a corpsenm.
3586 kill_egg(otmp);
3588 #if 0 /* not used */
3589 } else if (otmp->otyp == TIN) {
3590 if (dead_species(otmp->corpsenm, FALSE))
3591 otmp->corpsenm = NON_PM; /* empty tin */
3592 } else if (otmp->otyp == CORPSE) {
3593 if (dead_species(otmp->corpsenm, FALSE))
3594 ; /* not yet implemented... */
3595 #endif
3596 } else if (Has_contents(otmp)) {
3597 kill_eggs(otmp->cobj);
3601 /* kill all members of genocided species */
3602 void
3603 kill_genocided_monsters()
3605 struct monst *mtmp, *mtmp2;
3606 boolean kill_cham;
3607 int mndx;
3610 * Called during genocide, and again upon level change. The latter
3611 * catches up with any migrating monsters as they finally arrive at
3612 * their intended destinations, so possessions get deposited there.
3614 * Chameleon handling:
3615 * 1) if chameleons have been genocided, destroy them
3616 * regardless of current form;
3617 * 2) otherwise, force every chameleon which is imitating
3618 * any genocided species to take on a new form.
3620 for (mtmp = fmon; mtmp; mtmp = mtmp2) {
3621 mtmp2 = mtmp->nmon;
3622 if (DEADMONSTER(mtmp))
3623 continue;
3624 mndx = monsndx(mtmp->data);
3625 kill_cham = (mtmp->cham >= LOW_PM
3626 && (mvitals[mtmp->cham].mvflags & G_GENOD));
3627 if ((mvitals[mndx].mvflags & G_GENOD) || kill_cham) {
3628 if (mtmp->cham >= LOW_PM && !kill_cham)
3629 (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
3630 else
3631 mondead(mtmp);
3633 if (mtmp->minvent)
3634 kill_eggs(mtmp->minvent);
3637 kill_eggs(invent);
3638 kill_eggs(fobj);
3639 kill_eggs(migrating_objs);
3640 kill_eggs(level.buriedobjlist);
3643 void
3644 golemeffects(mon, damtype, dam)
3645 register struct monst *mon;
3646 int damtype, dam;
3648 int heal = 0, slow = 0;
3650 if (mon->data == &mons[PM_FLESH_GOLEM]) {
3651 if (damtype == AD_ELEC)
3652 heal = (dam + 5) / 6;
3653 else if (damtype == AD_FIRE || damtype == AD_COLD)
3654 slow = 1;
3655 } else if (mon->data == &mons[PM_IRON_GOLEM]) {
3656 if (damtype == AD_ELEC)
3657 slow = 1;
3658 else if (damtype == AD_FIRE)
3659 heal = dam;
3660 } else {
3661 return;
3663 if (slow) {
3664 if (mon->mspeed != MSLOW)
3665 mon_adjust_speed(mon, -1, (struct obj *) 0);
3667 if (heal) {
3668 if (mon->mhp < mon->mhpmax) {
3669 mon->mhp += heal;
3670 if (mon->mhp > mon->mhpmax)
3671 mon->mhp = mon->mhpmax;
3672 if (cansee(mon->mx, mon->my))
3673 pline("%s seems healthier.", Monnam(mon));
3678 boolean
3679 angry_guards(silent)
3680 boolean silent;
3682 struct monst *mtmp;
3683 int ct = 0, nct = 0, sct = 0, slct = 0;
3685 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3686 if (DEADMONSTER(mtmp))
3687 continue;
3688 if (is_watch(mtmp->data) && mtmp->mpeaceful) {
3689 ct++;
3690 if (cansee(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
3691 if (distu(mtmp->mx, mtmp->my) == 2)
3692 nct++;
3693 else
3694 sct++;
3696 if (mtmp->msleeping || mtmp->mfrozen) {
3697 slct++;
3698 mtmp->msleeping = mtmp->mfrozen = 0;
3700 mtmp->mpeaceful = 0;
3703 if (ct) {
3704 if (!silent) { /* do we want pline msgs? */
3705 if (slct)
3706 pline_The("guard%s wake%s up!", slct > 1 ? "s" : "",
3707 slct == 1 ? "s" : "");
3708 if (nct || sct) {
3709 if (nct)
3710 pline_The("guard%s get%s angry!", nct == 1 ? "" : "s",
3711 nct == 1 ? "s" : "");
3712 else if (!Blind)
3713 You_see("%sangry guard%s approaching!",
3714 sct == 1 ? "an " : "", sct > 1 ? "s" : "");
3715 } else
3716 You_hear("the shrill sound of a guard's whistle.");
3718 return TRUE;
3720 return FALSE;
3723 void
3724 pacify_guards()
3726 struct monst *mtmp;
3728 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
3729 if (DEADMONSTER(mtmp))
3730 continue;
3731 if (is_watch(mtmp->data))
3732 mtmp->mpeaceful = 1;
3736 void
3737 mimic_hit_msg(mtmp, otyp)
3738 struct monst *mtmp;
3739 short otyp;
3741 short ap = mtmp->mappearance;
3743 switch (mtmp->m_ap_type) {
3744 case M_AP_NOTHING:
3745 case M_AP_FURNITURE:
3746 case M_AP_MONSTER:
3747 break;
3748 case M_AP_OBJECT:
3749 if (otyp == SPE_HEALING || otyp == SPE_EXTRA_HEALING) {
3750 pline("%s seems a more vivid %s than before.",
3751 The(simple_typename(ap)),
3752 c_obj_colors[objects[ap].oc_color]);
3754 break;
3758 boolean
3759 usmellmon(mdat)
3760 struct permonst *mdat;
3762 int mndx;
3763 boolean nonspecific = FALSE;
3764 boolean msg_given = FALSE;
3766 if (mdat) {
3767 if (!olfaction(youmonst.data))
3768 return FALSE;
3769 mndx = monsndx(mdat);
3770 switch (mndx) {
3771 case PM_ROTHE:
3772 case PM_MINOTAUR:
3773 You("notice a bovine smell.");
3774 msg_given = TRUE;
3775 break;
3776 case PM_CAVEMAN:
3777 case PM_CAVEWOMAN:
3778 case PM_BARBARIAN:
3779 case PM_NEANDERTHAL:
3780 You("smell body odor.");
3781 msg_given = TRUE;
3782 break;
3784 case PM_PESTILENCE:
3785 case PM_FAMINE:
3786 case PM_DEATH:
3787 break;
3789 case PM_HORNED_DEVIL:
3790 case PM_BALROG:
3791 case PM_ASMODEUS:
3792 case PM_DISPATER:
3793 case PM_YEENOGHU:
3794 case PM_ORCUS:
3795 break;
3796 case PM_HUMAN_WEREJACKAL:
3797 case PM_HUMAN_WERERAT:
3798 case PM_HUMAN_WEREWOLF:
3799 case PM_WEREJACKAL:
3800 case PM_WERERAT:
3801 case PM_WEREWOLF:
3802 case PM_OWLBEAR:
3803 You("detect an odor reminiscent of an animal's den.");
3804 msg_given = TRUE;
3805 break;
3807 case PM_PURPLE_WORM:
3808 break;
3810 case PM_STEAM_VORTEX:
3811 You("smell steam.");
3812 msg_given = TRUE;
3813 break;
3814 case PM_GREEN_SLIME:
3815 pline("%s stinks.", Something);
3816 msg_given = TRUE;
3817 break;
3818 case PM_VIOLET_FUNGUS:
3819 case PM_SHRIEKER:
3820 You("smell mushrooms.");
3821 msg_given = TRUE;
3822 break;
3823 /* These are here to avoid triggering the
3824 nonspecific treatment through the default case below*/
3825 case PM_WHITE_UNICORN:
3826 case PM_GRAY_UNICORN:
3827 case PM_BLACK_UNICORN:
3828 case PM_JELLYFISH:
3829 break;
3830 default:
3831 nonspecific = TRUE;
3832 break;
3835 if (nonspecific)
3836 switch (mdat->mlet) {
3837 case S_DOG:
3838 You("notice a dog smell.");
3839 msg_given = TRUE;
3840 break;
3841 case S_DRAGON:
3842 You("smell a dragon!");
3843 msg_given = TRUE;
3844 break;
3845 case S_FUNGUS:
3846 pline("%s smells moldy.", Something);
3847 msg_given = TRUE;
3848 break;
3849 case S_UNICORN:
3850 You("detect a%s odor reminiscent of a stable.",
3851 (mndx == PM_PONY) ? "n" : " strong");
3852 msg_given = TRUE;
3853 break;
3854 case S_ZOMBIE:
3855 You("smell rotting flesh.");
3856 msg_given = TRUE;
3857 break;
3858 case S_EEL:
3859 You("smell fish.");
3860 msg_given = TRUE;
3861 break;
3862 case S_ORC:
3863 if (maybe_polyd(is_orc(youmonst.data), Race_if(PM_ORC)))
3864 You("notice an attractive smell.");
3865 else
3866 pline("A foul stench makes you feel a little nauseated.");
3867 msg_given = TRUE;
3868 break;
3869 default:
3870 break;
3873 return msg_given ? TRUE : FALSE;
3876 /*mon.c*/