Source:NetHack 5.0.0/src/trap.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to trap.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/trap.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	trap.c	$NHDT-Date: 1741926700 2025/03/13 20:31:40 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.621 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2013. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. extern const char *const destroy_strings[][3]; /* from zap.c */
9. 
10. staticfn void mk_trap_statue(coordxy, coordxy);
11. staticfn int dng_bottom(d_level *lev);
12. staticfn void hole_destination(d_level *);
13. staticfn boolean keep_saddle_with_steedcorpse(unsigned, struct obj *,
14.                                             struct obj *);
15. staticfn boolean mu_maybe_destroy_web(struct monst *, boolean, struct trap *);
16. staticfn struct obj *t_missile(int, struct trap *);
17. staticfn boolean floor_trigger(int);
18. staticfn boolean check_in_air(struct monst *, unsigned);
19. staticfn int trapeffect_arrow_trap(struct monst *, struct trap *, unsigned);
20. staticfn int trapeffect_dart_trap(struct monst *, struct trap *, unsigned);
21. staticfn int trapeffect_rocktrap(struct monst *, struct trap *, unsigned);
22. staticfn int trapeffect_sqky_board(struct monst *, struct trap *, unsigned);
23. staticfn int trapeffect_bear_trap(struct monst *, struct trap *, unsigned);
24. staticfn int trapeffect_slp_gas_trap(struct monst *, struct trap *, unsigned);
25. staticfn int trapeffect_rust_trap(struct monst *, struct trap *, unsigned);
26. staticfn int trapeffect_fire_trap(struct monst *, struct trap *, unsigned);
27. staticfn int trapeffect_pit(struct monst *, struct trap *, unsigned);
28. staticfn int trapeffect_hole(struct monst *, struct trap *, unsigned);
29. staticfn int trapeffect_telep_trap(struct monst *, struct trap *, unsigned);
30. staticfn int trapeffect_level_telep(struct monst *, struct trap *, unsigned);
31. staticfn int trapeffect_web(struct monst *, struct trap *, unsigned);
32. staticfn int trapeffect_statue_trap(struct monst *, struct trap *, unsigned);
33. staticfn int trapeffect_magic_trap(struct monst *, struct trap *, unsigned);
34. staticfn int trapeffect_anti_magic(struct monst *, struct trap *, unsigned);
35. staticfn int trapeffect_poly_trap(struct monst *, struct trap *, unsigned);
36. staticfn int trapeffect_landmine(struct monst *, struct trap *, unsigned);
37. staticfn int trapeffect_rolling_boulder_trap(struct monst *, struct trap *,
38.                                            unsigned);
39. staticfn int trapeffect_magic_portal(struct monst *, struct trap *, unsigned);
40. staticfn int trapeffect_vibrating_square(struct monst *, struct trap *,
41.                                            unsigned);
42. staticfn int trapeffect_selector(struct monst *, struct trap *, unsigned);
43. staticfn char *trapnote(struct trap *, boolean);
44. staticfn int choose_trapnote(struct trap *);
45. staticfn int steedintrap(struct trap *, struct obj *);
46. staticfn void launch_drop_spot(struct obj *, coordxy, coordxy);
47. staticfn boolean find_random_launch_coord(struct trap *, coord *);
48. staticfn int mkroll_launch(struct trap *, coordxy, coordxy, short, long);
49. staticfn boolean isclearpath(coord *, int, schar, schar);
50. staticfn boolean m_easy_escape_pit(struct monst *) NONNULLARG1;
51. staticfn void dofiretrap(struct obj *);
52. staticfn void domagictrap(void);
53. staticfn void pot_acid_damage(struct obj *, boolean, boolean);
54. staticfn boolean emergency_disrobe(boolean *);
55. staticfn int untrap_prob(struct trap *);
56. staticfn void move_into_trap(struct trap *);
57. staticfn int try_disarm(struct trap *, boolean);
58. staticfn void reward_untrap(struct trap *, struct monst *);
59. staticfn int disarm_holdingtrap(struct trap *);
60. staticfn int disarm_landmine(struct trap *);
61. staticfn int unsqueak_ok(struct obj *);
62. staticfn int disarm_squeaky_board(struct trap *);
63. staticfn int disarm_shooting_trap(struct trap *, int);
64. staticfn void clear_conjoined_pits(struct trap *);
65. staticfn boolean adj_nonconjoined_pit(struct trap *);
66. staticfn int try_lift(struct monst *, struct trap *, int, boolean);
67. staticfn int help_monster_out(struct monst *, struct trap *);
68. staticfn void disarm_box(struct obj *, boolean, boolean);
69. staticfn void untrap_box(struct obj *, boolean, boolean);
70. #if 0
71. staticfn void join_adjacent_pits(struct trap *);
72. #endif
73. staticfn boolean thitm(int, struct monst *, struct obj *, int,
74.                                                          boolean) NONNULLARG2;
75. staticfn void maybe_finish_sokoban(void);
76. 
77. static const char *const a_your[2] = { "a", "your" };
78. static const char *const A_Your[2] = { "A", "Your" };
79. static const char tower_of_flame[] = "tower of flame";
80. static const char *const A_gush_of_water_hits = "A gush of water hits";
81. static const char *const blindgas[6] = { "humid",   "odorless",
82.                                          "pungent", "chilling",
83.                                          "acrid",   "biting" };
84. 
85. /* called when you're hit by fire (dofiretrap,buzz,zapyourself,explode);
86.    returns TRUE if hit on torso */
87. boolean
88. burnarmor(struct monst *victim)
89. {
90.     struct obj *item;
91.     char buf[BUFSZ];
92.     int mat_idx, oldspe;
93.     boolean hitting_u;
94. 
95.     if (!victim)
96.         return 0;
97.     hitting_u = (victim == &gy.youmonst);
98. 
99.     /* burning damage may dry wet towel */
100.     item = hitting_u ? carrying(TOWEL) : m_carrying(victim, TOWEL);
101.     while (item) {
102.         if (is_wet_towel(item)) { /* True => (item->spe > 0) */
103.             oldspe = item->spe;
104.             dry_a_towel(item, rn2(oldspe + 1), TRUE);
105.             if (item->spe != oldspe)
106.                 break; /* stop once one towel has been affected */
107.         }
108.         item = item->nobj;
109.     }
110. 
111. #define burn_dmg(obj, descr) erode_obj(obj, descr, ERODE_BURN, EF_GREASE)
112.     while (1) {
113.         switch (rn2(5)) {
114.         case 0:
115.             item = hitting_u ? uarmh : which_armor(victim, W_ARMH);
116.             if (item) {
117.                 mat_idx = objects[item->otyp].oc_material;
118.                 Sprintf(buf, "%s %s", materialnm[mat_idx],
119.                         helm_simple_name(item));
120.             }
121.             if (!burn_dmg(item, item ? buf : "helmet"))
122.                 continue;
123.             break;
124.         case 1:
125.             item = hitting_u ? uarmc : which_armor(victim, W_ARMC);
126.             if (item) {
127.                 (void) burn_dmg(item, cloak_simple_name(item));
128.                 return TRUE;
129.             }
130.             item = hitting_u ? uarm : which_armor(victim, W_ARM);
131.             if (item) {
132.                 (void) burn_dmg(item, xname(item));
133.                 return TRUE;
134.             }
135.             item = hitting_u ? uarmu : which_armor(victim, W_ARMU);
136.             if (item)
137.                 (void) burn_dmg(item, "shirt");
138.             return TRUE;
139.         case 2:
140.             item = hitting_u ? uarms : which_armor(victim, W_ARMS);
141.             if (!burn_dmg(item, "wooden shield"))
142.                 continue;
143.             break;
144.         case 3:
145.             item = hitting_u ? uarmg : which_armor(victim, W_ARMG);
146.             if (!burn_dmg(item, "gloves"))
147.                 continue;
148.             break;
149.         case 4:
150.             item = hitting_u ? uarmf : which_armor(victim, W_ARMF);
151.             if (!burn_dmg(item, "boots"))
152.                 continue;
153.             break;
154.         }
155.         break; /* Out of while loop */
156.     }
157. #undef burn_dmg
158. 
159.     return FALSE;
160. }
161. 
162. /* Generic erode-item function.
163.  * "ostr", if non-null, is an alternate string to print instead of the
164.  *   object's name.
165.  * "type" is an ERODE_* value for the erosion type
166.  * "flags" is an or-ed list of EF_* flags
167.  *
168.  * Returns an erosion return value (ER_*)
169.  */
170. int
171. erode_obj(
172.     struct obj *otmp,
173.     const char *ostr,
174.     int type,
175.     int ef_flags)
176. {
177.     static NEARDATA const char
178.         *const action[] = { "smoulder", "rust", "rot", "corrode", "crack" },
179.         *const msg[] = { "burnt", "rusted", "rotten", "corroded", "cracked" },
180.         *const bythe[] = { "heat", "oxidation", "decay", "corrosion",
181.                            "impact" }; /* this could use improvement... */
182.     boolean vulnerable = FALSE, is_primary = TRUE,
183.             check_grease = (ef_flags & EF_GREASE) ? TRUE : FALSE,
184.             print = (ef_flags & EF_VERBOSE) ? TRUE : FALSE,
185.             crackers = FALSE, /* True: different feedback if otmp destroyed */
186.             uvictim, vismon, visobj;
187.     int erosion, cost_type;
188.     struct monst *victim;
189. 
190.     if (!otmp)
191.         return ER_NOTHING;
192. 
193.     victim = carried(otmp) ? &gy.youmonst
194.              : mcarried(otmp) ? otmp->ocarry
195.                : (struct monst *) 0;
196.     uvictim = (victim == &gy.youmonst);
197.     vismon = victim && (victim != &gy.youmonst) && canseemon(victim);
198.     /* Is gb.bhitpos correct here? Ugh. */
199.     visobj = (!victim && cansee(gb.bhitpos.x, gb.bhitpos.y)
200.               && (!is_pool(gb.bhitpos.x, gb.bhitpos.y)
201.                   || (next2u(gb.bhitpos.x,gb.bhitpos.y) && Underwater)));
202. 
203.     switch (type) {
204.     case ERODE_BURN:
205.         if (uvictim && inventory_resistance_check(AD_FIRE))
206.             return ER_NOTHING;
207.         vulnerable = is_flammable(otmp);
208.         check_grease = FALSE;
209.         cost_type = COST_BURN;
210.         break;
211.     case ERODE_RUST:
212.         vulnerable = is_rustprone(otmp);
213.         cost_type = COST_RUST;
214.         break;
215.     case ERODE_ROT:
216.         vulnerable = is_rottable(otmp);
217.         check_grease = FALSE;
218.         is_primary = FALSE;
219.         cost_type = COST_ROT;
220.         break;
221.     case ERODE_CORRODE:
222.         if (uvictim && inventory_resistance_check(AD_ACID))
223.             return ER_NOTHING;
224.         vulnerable = is_corrodeable(otmp);
225.         is_primary = FALSE;
226.         cost_type = COST_CORRODE;
227.         break;
228.     case ERODE_CRACK: /* crystal armor */
229.         vulnerable = is_crackable(otmp);
230.         is_primary = TRUE;
231.         crackers = TRUE;
232.         cost_type = COST_CRACK;
233.         break;
234.     default:
235.         impossible("Invalid erosion type in erode_obj");
236.         return ER_NOTHING;
237.     }
238.     erosion = is_primary ? otmp->oeroded : otmp->oeroded2;
239. 
240.     if (!ostr)
241.         ostr = cxname(otmp);
242.     /* 'visobj' messages insert "the"; probably ought to switch to the() */
243.     if (visobj && !(uvictim || vismon) && !strncmpi(ostr, "the ", 4))
244.         ostr += 4;
245. 
246.     if (check_grease && otmp->greased) {
247.         grease_protect(otmp, ostr, victim);
248.         return ER_GREASED;
249.     } else if (!erosion_matters(otmp)) {
250.         return ER_NOTHING;
251.     } else if (!vulnerable || (otmp->oerodeproof && otmp->rknown)) {
252.         if (flags.verbose && print && (uvictim || vismon))
253.             pline("%s %s %s not affected by %s.",
254.                   uvictim ? "Your" : s_suffix(Monnam(victim)),
255.                   ostr, vtense(ostr, "are"), bythe[type]);
256.         return ER_NOTHING;
257.     } else if (otmp->oerodeproof || (otmp->blessed && !rnl(4))) {
258.         if (flags.verbose && (print || otmp->oerodeproof)
259.             && (uvictim || vismon || visobj))
260.             pline("Somehow, %s %s %s not affected by the %s.",
261.                   uvictim ? "your"
262.                   : !vismon ? "the" /* visobj */
263.                     : s_suffix(mon_nam(victim)),
264.                   ostr, vtense(ostr, "are"), bythe[type]);
265.         /* We assume here that if the object is protected because it
266.          * is blessed, it still shows some minor signs of wear, and
267.          * the hero can distinguish this from an object that is
268.          * actually proof against damage.
269.          */
270.         if (otmp->oerodeproof) {
271.             otmp->rknown = TRUE;
272.             if (victim == &gy.youmonst)
273.                 update_inventory();
274.         }
275. 
276.         return ER_NOTHING;
277.     } else if (erosion < MAX_ERODE) {
278.         const char *adverb = (erosion + 1 == MAX_ERODE) ? " completely"
279.                              : erosion ? " further"
280.                                : "";
281. 
282.         if (uvictim || vismon || visobj)
283.             pline("%s %s %s%s!",
284.                   uvictim ? "Your"
285.                   : !vismon ? "The" /* visobj */
286.                     : s_suffix(Monnam(victim)),
287.                   ostr, vtense(ostr, action[type]), adverb);
288. 
289.         if (ef_flags & EF_PAY)
290.             costly_alteration(otmp, cost_type);
291. 
292.         if (is_primary)
293.             otmp->oeroded++;
294.         else
295.             otmp->oeroded2++;
296. 
297.         if (victim == &gy.youmonst)
298.             update_inventory();
299. 
300.         return ER_DAMAGED;
301.     } else if (ef_flags & EF_DESTROY) {
302.         otmp->in_use = 1; /* in case of hangup during message w/ --More-- */
303.         if (uvictim || vismon || visobj) {
304.             char actbuf[BUFSZ];
305. 
306.             if (!crackers)
307.                 Sprintf(actbuf, "%s away", vtense(ostr, action[type]));
308.             else
309.                 Sprintf(actbuf, "shatters");
310.             pline("%s %s %s!",
311.                   uvictim ? "Your"
312.                   : !vismon ? "The" /* visobj */
313.                     : s_suffix(Monnam(victim)),
314.                   ostr, actbuf);
315.         }
316.         if (ef_flags & EF_PAY)
317.             costly_alteration(otmp, cost_type);
318. 
319.         if (otmp->owornmask) {
320.             /* unwear otmp before deleting it */
321.             if (carried(otmp)) {
322.                 /* otmp remains in hero's invent; if we get here because
323.                    it is being burned up by lava, we don't need to worry
324.                    about unwearing levitation boots and having that
325.                    trigger float_down to then fall in again; if such
326.                    were being worn, they wouldn't be in the lava now */
327.                 remove_worn_item(otmp, TRUE); /* calls Cloak_off(),&c */
328.             } else if (mcarried(otmp)) {
329.                 /* results in otmp->where==OBJ_FREE; delobj() doesn't care */
330.                 extract_from_minvent(otmp->ocarry, otmp, TRUE, FALSE);
331.             } else { /* worn but not in hero invent or monster minvent ? */
332.                 impossible(
333.             "erode_obj(%d): destroying strangely worn item [%d, 0x%08lx: %s]",
334.                            type,
335.                            otmp->where, otmp->owornmask, simpleonames(otmp));
336.                 otmp->owornmask = 0L; /* otherwise a second complaint (about
337.                                        * deleting a worn item) will ensue */
338.             }
339.         }
340.         delobj(otmp);
341.         return ER_DESTROYED;
342.     } else {
343.         if (flags.verbose && print) {
344.             if (uvictim)
345.                 Your("%s %s completely %s.",
346.                      ostr, vtense(ostr, Blind ? "feel" : "look"), msg[type]);
347.             else if (vismon || visobj)
348.                 pline("%s %s %s completely %s.",
349.                       !vismon ? "The" : s_suffix(Monnam(victim)),
350.                       ostr, vtense(ostr, "look"), msg[type]);
351.         }
352.         return ER_NOTHING;
353.     }
354. }
355. 
356. /* Protect an item from erosion with grease. Returns TRUE if the grease
357.  * wears off.
358.  */
359. boolean
360. grease_protect(
361.     struct obj *otmp,
362.     const char *ostr,
363.     struct monst *victim)
364. {
365.     static const char txt[] = "protected by the layer of grease!";
366.     boolean vismon = victim && (victim != &gy.youmonst) && canseemon(victim);
367. 
368.     if (ostr) {
369.         if (victim == &gy.youmonst)
370.             Your("%s %s %s", ostr, vtense(ostr, "are"), txt);
371.         else if (vismon)
372.             pline("%s's %s %s %s", Monnam(victim),
373.                   ostr, vtense(ostr, "are"), txt);
374.     } else if (victim == &gy.youmonst || vismon) {
375.         pline("%s %s", Yobjnam2(otmp, "are"), txt);
376.     }
377.     if (!rn2(2)) {
378.         otmp->greased = 0;
379.         if (carried(otmp)) {
380.             pline_The("grease dissolves.");
381.             update_inventory();
382.         }
383.         return TRUE;
384.     }
385.     return FALSE;
386. }
387. 
388. /* create a "living" statue at x,y */
389. staticfn void
390. mk_trap_statue(coordxy x, coordxy y)
391. {
392.     struct monst *mtmp;
393.     struct obj *otmp, *statue;
394.     struct permonst *mptr;
395.     int trycount = 10;
396. 
397.     do { /* avoid ultimately hostile co-aligned unicorn */
398.         mptr = &mons[rndmonnum_adj(3, 6)];
399.     } while (--trycount > 0 && is_unicorn(mptr)
400.              && sgn(u.ualign.type) == sgn(mptr->maligntyp));
401.     statue = mkcorpstat(STATUE, (struct monst *) 0, mptr, x, y,
402.                         CORPSTAT_NONE);
403.     mtmp = makemon(&mons[statue->corpsenm], 0, 0, MM_NOCOUNTBIRTH | MM_NOMSG);
404.     if (!mtmp)
405.         return; /* should never happen */
406.     while (mtmp->minvent) {
407.         otmp = mtmp->minvent;
408.         otmp->owornmask = 0;
409.         obj_extract_self(otmp);
410.         (void) add_to_container(statue, otmp);
411.     }
412.     statue->owt = weight(statue);
413.     mongone(mtmp);
414. }
415. 
416. /* find "bottom" level of specified dungeon, stopping at quest locate */
417. staticfn int
418. dng_bottom(d_level *lev)
419. {
420.     int bottom = dunlevs_in_dungeon(lev);
421. 
422.     /* when in the upper half of the quest, don't fall past the
423.        middle "quest locate" level if hero hasn't been there yet */
424.     if (In_quest(lev)) {
425.         int qlocate_depth = qlocate_level.dlevel;
426. 
427.         /* deepest reached < qlocate implies current < qlocate */
428.         if (dunlev_reached(lev) < qlocate_depth)
429.             bottom = qlocate_depth; /* early cut-off */
430.     } else if (In_hell(lev)) {
431.         /* if the invocation hasn't been performed yet, the vibrating square
432.            level is effectively the bottom of Gehennom; the sanctum level is
433.            out of reach until after the invocation */
434.         if (!u.uevent.invoked)
435.             bottom -= 1;
436.     }
437.     return bottom;
438. }
439. 
440. /* destination dlevel for holes or trapdoors */
441. staticfn void
442. hole_destination(d_level *dst)
443. {
444.     int bottom = dng_bottom(&u.uz);
445. 
446.     dst->dnum = u.uz.dnum;
447.     dst->dlevel = dunlev(&u.uz);
448.     while (dst->dlevel < bottom) {
449.         dst->dlevel++;
450.         if (rn2(4))
451.             break;
452.     }
453. }
454. 
455. struct trap *
456. maketrap(coordxy x, coordxy y, int typ)
457. {
458.     static union vlaunchinfo zero_vl;
459.     boolean oldplace, was_ice, clear_flags;
460.     struct trap *ttmp;
461.     struct rm *lev = &levl[x][y];
462. 
463.     if (typ == TRAPPED_DOOR || typ == TRAPPED_CHEST)
464.         return (struct trap *) 0;
465. 
466.     if ((ttmp = t_at(x, y)) != 0) {
467.         if (undestroyable_trap(ttmp->ttyp))
468.             return (struct trap *) 0;
469.         oldplace = TRUE;
470.         if (u.utrap && u_at(x, y)
471.             && ((u.utraptype == TT_BEARTRAP && typ != BEAR_TRAP)
472.                 || (u.utraptype == TT_WEB && typ != WEB)
473.                 || (u.utraptype == TT_PIT && !is_pit(typ))
474.                 || (u.utraptype == TT_LAVA && !is_lava(x, y))))
475.             reset_utrap(FALSE);
476.         /* old <tx,ty> remain valid */
477.     } else if (!CAN_OVERWRITE_TERRAIN(lev->typ) /* stairs */
478.                || is_pool_or_lava(x, y)
479.                || (IS_FURNITURE(lev->typ) && (typ != PIT && typ != HOLE))
480.                || (lev->typ == DRAWBRIDGE_UP && typ == MAGIC_PORTAL)
481.                || (IS_AIR(lev->typ) && typ != MAGIC_PORTAL)
482.                || (typ == LEVEL_TELEP && single_level_branch(&u.uz))) {
483.         /* no trap on top of furniture (caller usually screens the
484.            location to inhibit this, but wizard mode wishing doesn't)
485.            and no level teleporter in branch with only one level */
486.         return (struct trap *) 0;
487.     } else {
488.         oldplace = FALSE;
489.         ttmp = newtrap();
490.         (void) memset((genericptr_t) ttmp, 0, sizeof(struct trap));
491.         ttmp->ntrap = 0;
492.         ttmp->tx = x;
493.         ttmp->ty = y;
494.     }
495.     /* [re-]initialize all fields except ntrap (handled below) and <tx,ty> */
496.     ttmp->vl = zero_vl;
497.     ttmp->launch.x = ttmp->launch.y = -1; /* force error if used before set */
498.     ttmp->dst.dnum = ttmp->dst.dlevel = -1;
499.     ttmp->madeby_u = 0;
500.     ttmp->once = 0;
501.     ttmp->tseen = unhideable_trap(typ);
502.     ttmp->ttyp = typ;
503. 
504.     switch (typ) {
505.     case SQKY_BOARD:
506.         ttmp->tnote = choose_trapnote(ttmp);
507.         break;
508.     case STATUE_TRAP: /* create a "living" statue */
509.         mk_trap_statue(x, y);
510.         break;
511.     case ROLLING_BOULDER_TRAP: /* boulder will roll towards trigger */
512.         (void) mkroll_launch(ttmp, x, y, BOULDER, 1L);
513.         break;
514.     case PIT:
515.     case SPIKED_PIT:
516.         ttmp->conjoined = 0;
517.         FALLTHROUGH;
518.         /*FALLTHRU*/
519.     case HOLE:
520.     case TRAPDOOR:
521.         if (is_hole(typ))
522.             hole_destination(&(ttmp->dst));
523.         if (*in_rooms(x, y, SHOPBASE)
524.             && (is_hole(typ) || IS_DOOR(lev->typ) || IS_WALL(lev->typ)))
525.             add_damage(x, y, /* schedule repair */
526.                        ((IS_DOOR(lev->typ) || IS_WALL(lev->typ))
527.                         && !svc.context.mon_moving) ? SHOP_HOLE_COST : 0L);
528. 
529.         clear_flags = TRUE; /* assume lev->flags needs to be reset */
530.         /* DRAWBRIDGE_UP passes the IS_ROOM() test so check it first;
531.            it also needs to retain lev->drawbridgemask */
532.         if (lev->typ == DRAWBRIDGE_UP) {
533.             /* bridge is closed and we're putting a hole or pit at the span
534.                spot; this trap will be deleted if/when the bridge is opened;
535.                terrain becomes room floor even if it was moat, lava, or ice */
536.             clear_flags = FALSE; /* keep lev->drawbridgemask */
537.             was_ice = (lev->drawbridgemask & DB_UNDER) == DB_ICE;
538.             lev->drawbridgemask &= ~DB_UNDER;
539.             lev->drawbridgemask |= DB_FLOOR;
540.             if (was_ice) {
541.                 /* subset of set_levltyp() after changing ice to floor;
542.                    frozen corpses resume rotting, no more ice to melt away */
543.                 obj_ice_effects(x, y, TRUE);
544.                 spot_stop_timers(x, y, MELT_ICE_AWAY);
545.             }
546.         } else if (IS_ROOM(lev->typ)) {
547.             (void) set_levltyp(x, y, ROOM);
548. 
549.         /*
550.          * some cases which can happen when digging
551.          * down while phasing thru solid areas
552.          */
553.         } else if (lev->typ == STONE || lev->typ == SCORR) {
554.             (void) set_levltyp(x, y, CORR);
555.         } else if (IS_WALL(lev->typ) || lev->typ == SDOOR) {
556.             (void) set_levltyp(x, y, svl.level.flags.is_maze_lev ? ROOM
557.                                      : svl.level.flags.is_cavernous_lev ? CORR
558.                                        : DOOR);
559.         }
560.         if (clear_flags)
561.             lev->flags = 0; /* set_levltyp doesn't take care of this [yet?] */
562. 
563.         unearth_objs(x, y);
564.         recalc_block_point(x, y);
565.         break;
566.     case TELEP_TRAP:
567.         if (isok(gl.launchplace.x, gl.launchplace.y)) {
568.             ttmp->teledest.x = gx.xstart + gl.launchplace.x;
569.             ttmp->teledest.y = gy.ystart + gl.launchplace.y;
570.             if (ttmp->teledest.x == x && ttmp->teledest.y == y) {
571.                 impossible("making fixed-dest tele trap pointing to itself");
572.             }
573.         }
574.         break;
575.     }
576. 
577.     if (!oldplace) {
578.         ttmp->ntrap = gf.ftrap;
579.         gf.ftrap = ttmp;
580.     } else {
581.         /* oldplace;
582.            it shouldn't be possible to override a sokoban pit or hole
583.            with some other trap, but we'll check just to be safe */
584.         if (Sokoban)
585.             maybe_finish_sokoban();
586.     }
587.     return ttmp;
588. }
589. 
590. /* limit the destination of a hole or trapdoor to the furthest level you
591.    should be able to fall to */
592. d_level *
593. clamp_hole_destination(d_level *dlev)
594. {
595.     int bottom = dng_bottom(dlev);
596. 
597.     dlev->dlevel = min(dlev->dlevel, bottom);
598.     return dlev;
599. }
600. 
601. void
602. fall_through(
603.     boolean td, /* td == TRUE : trap door or hole */
604.     unsigned ftflags)
605. {
606.     d_level dtmp;
607.     char msgbuf[BUFSZ];
608.     const char *dont_fall = 0;
609.     int newlevel;
610.     struct trap *t = (struct trap *) 0;
611.     boolean controlled_flight = FALSE;
612. 
613.     /* we'll fall even while levitating in Sokoban; otherwise, if we
614.        won't fall and won't be told that we aren't falling, give up now */
615.     if (Blind && Levitation && !Sokoban)
616.         return;
617. 
618.     newlevel = dunlev(&u.uz); /* current level */
619.     newlevel++;
620. 
621.     if (td) {
622.         t = t_at(u.ux, u.uy);
623.         feeltrap(t);
624.         if (!Sokoban && !(ftflags & TOOKPLUNGE)) {
625.             if (t->ttyp == TRAPDOOR)
626.                 pline("A trap door opens up under you!");
627.             else
628.                 pline("There's a gaping hole under you!");
629.         }
630.     } else
631.         pline_The("%s opens up under you!", surface(u.ux, u.uy));
632. 
633.     if (Sokoban && Can_fall_thru(&u.uz)) {
634.         ; /* KMH -- You can't escape the Sokoban level traps */
635.     } else if (Levitation || u.ustuck
636.              || (!Can_fall_thru(&u.uz) && !levl[u.ux][u.uy].candig)
637.              || ((Flying || is_clinger(gy.youmonst.data)
638.                   || (ceiling_hider(gy.youmonst.data) && u.uundetected))
639.                  && !(ftflags & TOOKPLUNGE))) {
640.         dont_fall = "don't fall in.";
641.     } else if (gy.youmonst.data->msize >= MZ_HUGE) {
642.         dont_fall = "don't fit through.";
643.     } else if (!next_to_u()) {
644.         dont_fall = "are jerked back by your pet!";
645.     }
646.     if (dont_fall) {
647.         You1(dont_fall);
648.         /* hero didn't fall through, but any objects here might */
649.         impact_drop((struct obj *) 0, u.ux, u.uy, 0);
650.         if (!td) {
651.             display_nhwindow(WIN_MESSAGE, FALSE);
652.             pline_The("opening under you closes up.");
653.         }
654.         return;
655.     }
656.     if ((Flying || is_clinger(gy.youmonst.data))
657.         && (ftflags & TOOKPLUNGE) && td && t) {
658.         if (Flying)
659.             controlled_flight = TRUE;
660.         You("%s down %s!",
661.             Flying ? "swoop" : "deliberately drop",
662.             (t->ttyp == TRAPDOOR)
663.                 ? "through the trap door"
664.                 : "into the gaping hole");
665.     }
666. 
667.     if (*u.ushops)
668.         shopdig(1);
669.     if (Is_stronghold(&u.uz)) {
670.         find_hell(&dtmp);
671.     } else {
672.         int dist;
673. 
674.         if (t) {
675.             assign_level(&dtmp, &t->dst);
676.             /* don't fall beyond the bottom, in case this came from a bones
677.                file with different dungeon size  */
678.             (void) clamp_hole_destination(&dtmp);
679.         } else {
680.             dtmp.dnum = u.uz.dnum;
681.             dtmp.dlevel = newlevel;
682.         }
683.         dist = depth(&dtmp) - depth(&u.uz);
684.         if (dist > 1)
685.             You("%s down a %s%sshaft!",
686.                 controlled_flight ? "fly" : "fall",
687.                 dist > 3 ? "very " : "",
688.                 dist > 2 ? "deep " : "");
689.     }
690.     if (!td)
691.         Sprintf(msgbuf, "The hole in the %s above you closes up.",
692.                 ceiling(u.ux, u.uy));
693. 
694.     schedule_goto(&dtmp, !Flying ? UTOTYPE_FALLING : UTOTYPE_NONE, (char *) 0,
695.                   !td ? msgbuf : (char *) 0);
696. }
697. 
698. /*
699.  * Animate the given statue.  May have been via shatter attempt, trap,
700.  * or stone to flesh spell.  Return a monster if successfully animated.
701.  * If the monster is animated, the object is deleted.  If fail_reason
702.  * is non-null, then fill in the reason for failure (or success).
703.  *
704.  * The cause of animation is:
705.  *
706.  *      ANIMATE_NORMAL  - hero "finds" the monster
707.  *      ANIMATE_SHATTER - hero tries to destroy the statue
708.  *      ANIMATE_SPELL   - stone to flesh spell hits the statue
709.  *
710.  * Perhaps x, y is not needed if we can use get_obj_location() to find
711.  * the statue's location... ???
712.  *
713.  * Sequencing matters:
714.  *      create monster; if it fails, give up with statue intact;
715.  *      give "statue comes to life" message;
716.  *      if statue belongs to shop, have shk give "you owe" message;
717.  *      transfer statue contents to monster (after stolen_value());
718.  *      delete statue.
719.  *      [This ordering means that if the statue ends up wearing a cloak of
720.  *       invisibility or a mummy wrapping, the visibility checks might be
721.  *       wrong, but to avoid that we'd have to clone the statue contents
722.  *       first in order to give them to the monster before checking their
723.  *       shop status--it's not worth the hassle.]
724.  */
725. struct monst *
726. animate_statue(
727.     struct obj *statue,
728.     coordxy x,
729.     coordxy y,
730.     int cause,
731.     int *fail_reason)
732. {
733.     static const char
734.         historic_statue_is_gone[] = "that the historic statue is now gone";
735.     int mnum = statue->corpsenm;
736.     struct permonst *mptr = &mons[mnum];
737.     struct monst *mon = 0, *shkp;
738.     struct obj *item;
739.     coord cc;
740.     boolean historic = (Role_if(PM_ARCHEOLOGIST)
741.                         && (statue->spe & CORPSTAT_HISTORIC) != 0),
742.             golem_xform = FALSE, use_saved_traits;
743.     const char *comes_to_life;
744.     char statuename[BUFSZ], tmpbuf[BUFSZ];
745. 
746.     if (cant_revive(&mnum, TRUE, statue)) {
747.         /* mnum has changed; we won't be animating this statue as itself */
748.         if (mnum != PM_DOPPELGANGER)
749.             mptr = &mons[mnum];
750.         use_saved_traits = FALSE;
751.     } else if (is_golem(mptr) && cause == ANIMATE_SPELL) {
752.         /* statue of any golem hit by stone-to-flesh becomes flesh golem */
753.         golem_xform = (mptr != &mons[PM_FLESH_GOLEM]);
754.         mnum = PM_FLESH_GOLEM;
755.         mptr = &mons[PM_FLESH_GOLEM];
756.         use_saved_traits = (has_omonst(statue) && !golem_xform);
757.     } else {
758.         use_saved_traits = has_omonst(statue);
759.     }
760. 
761.     if (use_saved_traits) {
762.         /* restore a petrified monster */
763.         cc.x = x, cc.y = y;
764.         mon = montraits(statue, &cc, (cause == ANIMATE_SPELL));
765.         if (mon && mon->mtame && !mon->isminion)
766.             wary_dog(mon, TRUE);
767.     } else {
768.         int sgend = (statue->spe & CORPSTAT_GENDER);
769.         mmflags_nht mmflags = (NO_MINVENT | MM_NOMSG
770.                         | ((sgend == CORPSTAT_MALE) ? MM_MALE : 0)
771.                         | ((sgend == CORPSTAT_FEMALE) ? MM_FEMALE : 0));
772. 
773.         /* statues of unique monsters from bones or wishing end
774.            up here (cant_revive() sets mnum to be doppelganger;
775.            mptr reflects the original form for use by newcham()) */
776.         if ((mnum == PM_DOPPELGANGER && mptr != &mons[PM_DOPPELGANGER])
777.             /* block quest guards from other roles */
778.             || (mptr->msound == MS_GUARDIAN
779.                 && quest_info(MS_GUARDIAN) != mnum)) {
780.             mmflags |= MM_NOCOUNTBIRTH | MM_ADJACENTOK;
781.             mon = makemon(&mons[PM_DOPPELGANGER], x, y, mmflags);
782.             /* if hero has protection from shape changers, cham field will
783.                be NON_PM; otherwise, set form to match the statue */
784.             if (mon && ismnum(mon->cham))
785.                 (void) newcham(mon, mptr, NO_NC_FLAGS);
786.         } else {
787.             if (cause == ANIMATE_SPELL)
788.                 mmflags |= MM_ADJACENTOK;
789.             mon = makemon(mptr, x, y, mmflags);
790.         }
791.     }
792. 
793.     if (!mon) {
794.         if (fail_reason)
795.             *fail_reason = unique_corpstat(&mons[statue->corpsenm])
796.                                ? AS_MON_IS_UNIQUE
797.                                : AS_NO_MON;
798.         return (struct monst *) 0;
799.     }
800. 
801.     /* if statue has been named, give same name to the monster */
802.     if (has_oname(statue) && !unique_corpstat(mon->data))
803.         mon = christen_monst(mon, ONAME(statue));
804.     /* mimic statue becomes seen mimic; other hiders won't be hidden */
805.     if (M_AP_TYPE(mon))
806.         seemimic(mon);
807.     else
808.         mon->mundetected = FALSE;
809.     mon->msleeping = 0;
810.     if (cause == ANIMATE_NORMAL || cause == ANIMATE_SHATTER) {
811.         /* trap always releases hostile monster */
812.         mon->mtame = 0; /* (might be petrified pet tossed onto trap) */
813.         mon->mpeaceful = 0;
814.         set_malign(mon);
815.     }
816. 
817.     comes_to_life = !canspotmon(mon) ? "disappears"
818.                     : golem_xform ? "turns into flesh"
819.                       : (nonliving(mon->data) || is_vampshifter(mon))
820.                         ? "moves"
821.                         : "comes to life";
822.     if (u_at(x, y) || cause == ANIMATE_SPELL) {
823.         /* "the|your|Manlobbi's statue [of a wombat]" */
824.         shkp = shop_keeper(*in_rooms(mon->mx, mon->my, SHOPBASE));
825.         Sprintf(statuename, "%s%s", shk_your(tmpbuf, statue),
826.                 (cause == ANIMATE_SPELL
827.                  /* avoid "of a shopkeeper" if it's Manlobbi himself
828.                     (if carried, it can't be unpaid--hence won't be
829.                     described as "Manlobbi's statue"--because there
830.                     wasn't any living shk when statue was picked up) */
831.                  && (mon != shkp || carried(statue)))
832.                    ? xname(statue)
833.                    : "statue");
834.         pline("%s %s!", upstart(statuename), comes_to_life);
835.     } else if (Hallucination) { /* They don't know it's a statue */
836.         pline_The("%s suddenly seems more animated.", rndmonnam((char *) 0));
837.     } else if (cause == ANIMATE_SHATTER) {
838.         if (cansee(x, y))
839.             Sprintf(statuename, "%s%s", shk_your(tmpbuf, statue),
840.                     xname(statue));
841.         else
842.             Strcpy(statuename, "a statue");
843.         pline("Instead of shattering, %s suddenly %s!", statuename,
844.               comes_to_life);
845.     } else { /* cause == ANIMATE_NORMAL */
846.         set_msg_xy(x, y);
847.         You("find %s posing as a statue.",
848.             canspotmon(mon) ? a_monnam(mon) : something);
849.         if (!canspotmon(mon) && Blind)
850.             map_invisible(x, y);
851.         stop_occupation();
852.     }
853. 
854.     /* if this isn't caused by a monster using a wand of striking,
855.        there might be consequences for the hero */
856.     if (!svc.context.mon_moving) {
857.         /* if statue is owned by a shop, hero will have to pay for it;
858.            stolen_value gives a message (about debt or use of credit)
859.            which refers to "it" so needs to follow a message describing
860.            the object ("the statue comes to life" one above) */
861.         if (cause != ANIMATE_NORMAL && costly_spot(x, y)
862.             && (carried(statue) ? statue->unpaid : !statue->no_charge)
863.             && (shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) != 0
864.             /* avoid charging for Manlobbi's statue of Manlobbi
865.                if stone-to-flesh is used on petrified shopkeep */
866.             && mon != shkp)
867.             (void) stolen_value(statue, x, y, (boolean) shkp->mpeaceful,
868.                                 FALSE);
869. 
870.         if (historic) {
871.             You_feel("guilty %s.", historic_statue_is_gone);
872.             adjalign(-1);
873.         }
874.     } else {
875.         if (historic && cansee(x, y))
876.             You_feel("regret %s.", historic_statue_is_gone);
877.         /* no alignment penalty */
878.     }
879. 
880.     /* transfer any statue contents to monster's inventory */
881.     while ((item = statue->cobj) != 0) {
882.         obj_extract_self(item);
883.         (void) mpickobj(mon, item);
884.     }
885.     m_dowear(mon, TRUE);
886.     /* in case statue is wielded and hero zaps stone-to-flesh at self */
887.     if (statue->owornmask)
888.         remove_worn_item(statue, TRUE);
889.     /* statue no longer exists */
890.     delobj(statue);
891. 
892.     /* avoid hiding under nothing */
893.     if (u_at(x, y) && Upolyd && hides_under(gy.youmonst.data)
894.         && !OBJ_AT(x, y))
895.         u.uundetected = 0;
896. 
897.     if (fail_reason)
898.         *fail_reason = AS_OK;
899.     return mon;
900. }
901. 
902. /*
903.  * You've either stepped onto a statue trap's location or you've triggered a
904.  * statue trap by searching next to it or by trying to break it with a wand
905.  * or pick-axe.
906.  */
907. struct monst *
908. activate_statue_trap(
909.     struct trap *trap,
910.     coordxy x,
911.     coordxy y,
912.     boolean shatter)
913. {
914.     struct monst *mtmp = (struct monst *) 0;
915.     struct obj *otmp = sobj_at(STATUE, x, y);
916.     int fail_reason;
917. 
918.     /*
919.      * Try to animate the first valid statue.  Stop the loop when we
920.      * actually create something or the failure cause is not because
921.      * the mon was unique.
922.      */
923.     deltrap(trap);
924.     while (otmp) {
925.         mtmp = animate_statue(otmp, x, y,
926.                               shatter ? ANIMATE_SHATTER : ANIMATE_NORMAL,
927.                               &fail_reason);
928.         if (mtmp || fail_reason != AS_MON_IS_UNIQUE)
929.             break;
930. 
931.         otmp = nxtobj(otmp, STATUE, TRUE);
932.     }
933. 
934.     feel_newsym(x, y);
935.     return mtmp;
936. }
937. 
938. staticfn boolean
939. keep_saddle_with_steedcorpse(
940.     unsigned steed_mid,
941.     struct obj *objchn,
942.     struct obj *saddle)
943. {
944.     if (!saddle)
945.         return FALSE;
946.     while (objchn) {
947.         if (objchn->otyp == CORPSE && has_omonst(objchn)) {
948.             struct monst *mtmp = OMONST(objchn);
949. 
950.             if (mtmp->m_id == steed_mid) {
951.                 /* move saddle */
952.                 coordxy x, y;
953.                 if (get_obj_location(objchn, &x, &y, 0)) {
954.                     obj_extract_self(saddle);
955.                     place_object(saddle, x, y);
956.                     stackobj(saddle);
957.                 }
958.                 return TRUE;
959.             }
960.         }
961.         if (Has_contents(objchn)
962.             && keep_saddle_with_steedcorpse(steed_mid, objchn->cobj, saddle))
963.             return TRUE;
964.         objchn = objchn->nobj;
965.     }
966.     return FALSE;
967. }
968. 
969. /* monster or you go through and possibly destroy a web.
970.    return TRUE if could go through. */
971. staticfn boolean
972. mu_maybe_destroy_web(
973.     struct monst *mtmp,
974.     boolean domsg,
975.     struct trap *trap)
976. {
977.     boolean isyou = (mtmp == &gy.youmonst);
978.     struct permonst *mptr = mtmp->data;
979. 
980.     if (amorphous(mptr) || is_whirly(mptr) || flaming(mptr)
981.         || unsolid(mptr) || mptr == &mons[PM_GELATINOUS_CUBE]) {
982.         coordxy x = trap->tx;
983.         coordxy y = trap->ty;
984. 
985.         if (flaming(mptr) || acidic(mptr)) {
986.             if (domsg) {
987.                 if (isyou)
988.                     You("%s %s spider web!",
989.                         (flaming(mptr)) ? "burn" : "dissolve",
990.                         a_your[trap->madeby_u]);
991.                 else
992.                     pline_mon(mtmp,
993.                           "%s %s %s spider web!", Monnam(mtmp),
994.                           (flaming(mptr)) ? "burns" : "dissolves",
995.                           a_your[trap->madeby_u]);
996.             }
997.             deltrap(trap);
998.             newsym(x, y);
999.             return TRUE;
1000.         }
1001.         if (domsg) {
1002.             if (isyou) {
1003.                 You("flow through %s spider web.", a_your[trap->madeby_u]);
1004.             } else {
1005.                 pline_mon(mtmp,
1006.                       "%s flows through %s spider web.", Monnam(mtmp),
1007.                       a_your[trap->madeby_u]);
1008.                 seetrap(trap);
1009.             }
1010.         }
1011.         return TRUE;
1012.     }
1013.     return FALSE;
1014. }
1015. 
1016. /* make a single arrow/dart/rock for a trap to shoot or drop */
1017. staticfn struct obj *
1018. t_missile(int otyp, struct trap *trap)
1019. {
1020.     struct obj *otmp = mksobj(otyp, TRUE, FALSE);
1021. 
1022.     otmp->quan = 1L;
1023.     otmp->owt = weight(otmp);
1024.     otmp->opoisoned = 0;
1025.     otmp->ox = trap->tx, otmp->oy = trap->ty;
1026.     return otmp;
1027. }
1028. 
1029. void
1030. set_utrap(unsigned int tim, unsigned int typ)
1031. {
1032.     /* if we get here through reset_utrap(), the caller of that might
1033.        have already set u.utrap to 0 so this check won't be sufficient
1034.        in that situation; caller will need to set context.botl itself */
1035.     if (!u.utrap ^ !tim)
1036.         disp.botl = TRUE;
1037. 
1038.     u.utrap = tim;
1039.     u.utraptype = tim ? typ : TT_NONE;
1040. 
1041.     float_vs_flight(); /* maybe block Lev and/or Fly */
1042. }
1043. 
1044. void
1045. reset_utrap(boolean msg)
1046. {
1047.     boolean was_Lev = (Levitation != 0), was_Fly = (Flying != 0);
1048. 
1049.     set_utrap(0, 0);
1050. 
1051.     if (msg) {
1052.         if (!was_Lev && Levitation)
1053.             float_up();
1054.         if (!was_Fly && Flying)
1055.             You("can fly.");
1056.     }
1057. }
1058. 
1059. /* is trap type ttyp triggered by touching the floor? */
1060. staticfn boolean
1061. floor_trigger(int ttyp)
1062. {
1063.     switch (ttyp) {
1064.     case ARROW_TRAP:
1065.     case DART_TRAP:
1066.     case ROCKTRAP:
1067.     case SQKY_BOARD:
1068.     case BEAR_TRAP:
1069.     case LANDMINE:
1070.     case ROLLING_BOULDER_TRAP:
1071.     case SLP_GAS_TRAP:
1072.     case RUST_TRAP:
1073.     case FIRE_TRAP:
1074.     case PIT:
1075.     case SPIKED_PIT:
1076.     case HOLE:
1077.     case TRAPDOOR:
1078.         return TRUE;
1079.     default:
1080.         return FALSE;
1081.     }
1082. }
1083. 
1084. /* return TRUE if monster mtmp is up in the air, considering trap flags */
1085. staticfn boolean
1086. check_in_air(struct monst *mtmp, unsigned trflags)
1087. {
1088.     boolean is_you = mtmp == &gy.youmonst,
1089.             plunged = (trflags & (TOOKPLUNGE | VIASITTING)) != 0;
1090. 
1091.     return ((trflags & HURTLING) != 0
1092.             || (is_you ? Levitation : is_floater(mtmp->data))
1093.             || ((is_you ? Flying : is_flyer(mtmp->data)) && !plunged));
1094. }
1095. 
1096. /* return TRUE if mtmp is wearing shoes made of iron (iron/kicking) */
1097. boolean
1098. wearing_iron_shoes(struct monst *mtmp)
1099. {
1100.     struct obj *armf = which_armor(mtmp, W_ARMF);
1101.     return armf && objects[armf->otyp].oc_material == IRON;
1102. }
1103. 
1104. /* is trap ttmp harmless to monster mtmp? */
1105. boolean
1106. m_harmless_trap(struct monst *mtmp, struct trap *ttmp)
1107. {
1108.     struct permonst *mdat = mtmp->data;
1109. 
1110.     /* this handles most of the traps, but those are still included
1111.        in the switch case below for completeness */
1112.     if (!Sokoban && floor_trigger(ttmp->ttyp) && check_in_air(mtmp, 0L))
1113.         return TRUE;
1114. 
1115.     switch (ttmp->ttyp) {
1116.     case ARROW_TRAP:
1117.         break;
1118.     case DART_TRAP:
1119.         break;
1120.     case ROCKTRAP:
1121.         break;
1122.     case SQKY_BOARD:
1123.         break;
1124.     case BEAR_TRAP:
1125.         if (mdat->msize <= MZ_SMALL || amorphous(mdat)
1126.             || is_whirly(mdat) || unsolid(mdat))
1127.             return TRUE;
1128.         break;
1129.     case LANDMINE:
1130.         break;
1131.     case ROLLING_BOULDER_TRAP:
1132.         break;
1133.     case SLP_GAS_TRAP:
1134.         if (resists_sleep(mtmp) || defended(mtmp, AD_SLEE))
1135.             return TRUE;
1136.         break;
1137.     case RUST_TRAP:
1138.         if (mdat != &mons[PM_IRON_GOLEM])
1139.             return TRUE;
1140.         break;
1141.     case FIRE_TRAP:
1142.         if (resists_fire(mtmp) || defended(mtmp, AD_FIRE))
1143.             return TRUE;
1144.         break;
1145.     case PIT:
1146.         FALLTHROUGH;
1147.         /*FALLTHRU*/
1148.     case SPIKED_PIT:
1149.         FALLTHROUGH;
1150.         /*FALLTHRU*/
1151.     case HOLE:
1152.         FALLTHROUGH;
1153.         /*FALLTHRU*/
1154.     case TRAPDOOR:
1155.         if (is_clinger(mdat) && !Sokoban)
1156.             return TRUE;
1157.         break;
1158.     case TELEP_TRAP:
1159.         break;
1160.     case LEVEL_TELEP:
1161.         break;
1162.     case MAGIC_PORTAL:
1163.         break;
1164.     case WEB:
1165.         if (amorphous(mdat) || webmaker(mdat)
1166.             || is_whirly(mdat) || unsolid(mdat))
1167.             return TRUE;
1168.         break;
1169.     case STATUE_TRAP:
1170.         return TRUE;
1171.     case MAGIC_TRAP:
1172.         return TRUE; /* usually */
1173.     case ANTI_MAGIC:
1174.         if (resists_magm(mtmp) || defended(mtmp, AD_MAGM))
1175.             return TRUE;
1176.         break;
1177.     case POLY_TRAP:
1178.         break;
1179.     case VIBRATING_SQUARE:
1180.         return TRUE;
1181.     default:
1182.         impossible("m_harmless_trap: unknown trap %i", ttmp->ttyp);
1183.         break;
1184.     }
1185. 
1186.     return FALSE;
1187. }
1188. 
1189. staticfn int
1190. trapeffect_arrow_trap(
1191.     struct monst *mtmp,
1192.     struct trap *trap,
1193.     unsigned trflags UNUSED)
1194. {
1195.     struct obj *otmp;
1196.     int dam;
1197. 
1198.     if (mtmp == &gy.youmonst) {
1199.         if (trap->once && trap->tseen && !rn2(15)) {
1200.             Soundeffect(se_loud_click, 100);
1201.             You_hear("a loud click!");
1202.             deltrap(trap);
1203.             newsym(u.ux, u.uy);
1204.             return Trap_Is_Gone;
1205.         }
1206.         trap->once = 1;
1207.         seetrap(trap);
1208.         pline("An arrow shoots out at you!");
1209.         otmp = t_missile(ARROW, trap);
1210.         dam = dmgval(otmp, &gy.youmonst);
1211.         if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) {
1212.             ; /* nothing */
1213.         } else if (thitu(8, Maybe_Half_Phys(dam), &otmp, "arrow")) {
1214.             if (otmp)
1215.                 obfree(otmp, (struct obj *) 0);
1216.         } else {
1217.             place_object(otmp, u.ux, u.uy);
1218.             if (!Blind)
1219.                 observe_object(otmp);
1220.             stackobj(otmp);
1221.             newsym(u.ux, u.uy);
1222.         }
1223.     } else {
1224.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1225.         boolean see_it = cansee(mtmp->mx, mtmp->my);
1226.         boolean trapkilled = FALSE;
1227. 
1228.         if (trap->once && trap->tseen && !rn2(15)) {
1229.             if (in_sight && see_it)
1230.                 pline_mon(mtmp,
1231.                       "%s triggers a trap but nothing happens.",
1232.                       Monnam(mtmp));
1233.             deltrap(trap);
1234.             newsym(mtmp->mx, mtmp->my);
1235.             return Trap_Is_Gone;
1236.         }
1237.         trap->once = 1;
1238.         otmp = t_missile(ARROW, trap);
1239.         if (in_sight)
1240.             seetrap(trap);
1241.         if (thitm(8, mtmp, otmp, 0, FALSE))
1242.             trapkilled = TRUE;
1243. 
1244.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1245.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1246.     }
1247.     return Trap_Effect_Finished;
1248. }
1249. 
1250. staticfn int
1251. trapeffect_dart_trap(
1252.     struct monst *mtmp,
1253.     struct trap *trap,
1254.     unsigned int trflags UNUSED)
1255. {
1256.     struct obj *otmp;
1257.     int dam;
1258. 
1259.     if (mtmp == &gy.youmonst) {
1260.         int oldumort = u.umortality;
1261. 
1262.         if (trap->once && trap->tseen && !rn2(15)) {
1263.             Soundeffect(se_soft_click, 30);
1264.             You_hear("a soft click.");
1265.             deltrap(trap);
1266.             newsym(u.ux, u.uy);
1267.             return Trap_Is_Gone;
1268.         }
1269.         trap->once = 1;
1270.         seetrap(trap);
1271.         pline("A little dart shoots out at you!");
1272.         otmp = t_missile(DART, trap);
1273.         if (!rn2(6))
1274.             otmp->opoisoned = 1;
1275.         dam = dmgval(otmp, &gy.youmonst);
1276.         if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) {
1277.             ; /* nothing */
1278.         } else if (thitu(7, Maybe_Half_Phys(dam), &otmp, "little dart")) {
1279.             if (otmp) {
1280.                 if (otmp->opoisoned)
1281.                     poisoned("dart", A_CON, "little dart",
1282.                              /* if damage triggered life-saving,
1283.                                 poison is limited to attrib loss */
1284.                              (u.umortality > oldumort) ? 0 : 10, TRUE);
1285.                 obfree(otmp, (struct obj *) 0);
1286.             }
1287.         } else {
1288.             place_object(otmp, u.ux, u.uy);
1289.             if (!Blind)
1290.                 observe_object(otmp);
1291.             stackobj(otmp);
1292.             newsym(u.ux, u.uy);
1293.         }
1294.     } else {
1295.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1296.         boolean see_it = cansee(mtmp->mx, mtmp->my);
1297.         boolean trapkilled = FALSE;
1298. 
1299.         if (trap->once && trap->tseen && !rn2(15)) {
1300.             if (in_sight && see_it)
1301.                 pline_mon(mtmp,
1302.                       "%s triggers a trap but nothing happens.",
1303.                       Monnam(mtmp));
1304.             deltrap(trap);
1305.             newsym(mtmp->mx, mtmp->my);
1306.             return Trap_Is_Gone;
1307.         }
1308.         trap->once = 1;
1309.         otmp = t_missile(DART, trap);
1310.         if (!rn2(6))
1311.             otmp->opoisoned = 1;
1312.         if (in_sight)
1313.             seetrap(trap);
1314.         if (thitm(7, mtmp, otmp, 0, FALSE))
1315.             trapkilled = TRUE;
1316. 
1317.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1318.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1319.     }
1320.     return Trap_Effect_Finished;
1321. }
1322. 
1323. staticfn int
1324. trapeffect_rocktrap(
1325.     struct monst *mtmp,
1326.     struct trap *trap,
1327.     unsigned trflags UNUSED)
1328. {
1329.     struct obj *otmp;
1330.     boolean harmless = FALSE;
1331. 
1332.     if (mtmp == &gy.youmonst) {
1333.         if (trap->once && trap->tseen && !rn2(15)) {
1334.             pline("A trap door in %s opens, but nothing falls out!",
1335.                   the(ceiling(u.ux, u.uy)));
1336.             deltrap(trap);
1337.             newsym(u.ux, u.uy);
1338.         } else {
1339.             int dmg = d(2, 6); /* should be std ROCK dmg? */
1340. 
1341.             trap->once = 1;
1342.             feeltrap(trap);
1343.             otmp = t_missile(ROCK, trap);
1344.             place_object(otmp, u.ux, u.uy);
1345. 
1346.             pline("A trap door in %s opens and %s falls on your %s!",
1347.                   the(ceiling(u.ux, u.uy)), an(xname(otmp)), body_part(HEAD));
1348.             if (uarmh) {
1349.                 /* normally passes_rocks() would protect against a falling
1350.                    rock, but not when wearing a helmet */
1351.                 if (passes_rocks(gy.youmonst.data)) {
1352.                     pline("Unfortunately, you are wearing %s.",
1353.                           an(helm_simple_name(uarmh))); /* helm or hat */
1354.                     dmg = 2;
1355.                 } else if (hard_helmet(uarmh)) {
1356.                     pline("Fortunately, you are wearing a hard helmet.");
1357.                     dmg = 2;
1358.                 } else if (flags.verbose) {
1359.                     pline("%s does not protect you.", Yname2(uarmh));
1360.                 }
1361.             } else if (passes_rocks(gy.youmonst.data)) {
1362.                 pline("It passes harmlessly through you.");
1363.                 harmless = TRUE;
1364.             }
1365.             if (!Blind)
1366.                 observe_object(otmp);
1367.             stackobj(otmp);
1368.             newsym(u.ux, u.uy); /* map the rock */
1369. 
1370.             if (!harmless) {
1371.                 losehp(Maybe_Half_Phys(dmg), "falling rock", KILLED_BY_AN);
1372.                 exercise(A_STR, FALSE);
1373.             }
1374.         }
1375.     } else {
1376.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1377.         boolean see_it = cansee(mtmp->mx, mtmp->my);
1378.         boolean trapkilled = FALSE;
1379. 
1380.         if (trap->once && trap->tseen && !rn2(15)) {
1381.             if (in_sight && see_it)
1382.                 pline_mon(mtmp,
1383.                       "A trap door above %s opens, but nothing falls out!",
1384.                       mon_nam(mtmp));
1385.             deltrap(trap);
1386.             newsym(mtmp->mx, mtmp->my);
1387.             return Trap_Is_Gone;
1388.         }
1389.         trap->once = 1;
1390.         otmp = t_missile(ROCK, trap);
1391.         if (in_sight)
1392.             seetrap(trap);
1393.         if (thitm(0, mtmp, otmp, d(2, 6), FALSE))
1394.             trapkilled = TRUE;
1395. 
1396.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1397.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1398.     }
1399.     return Trap_Effect_Finished;
1400. }
1401. 
1402. staticfn int
1403. trapeffect_sqky_board(
1404.     struct monst *mtmp,
1405.     struct trap *trap,
1406.     unsigned trflags)
1407. {
1408.     enum sound_effect_entries tsnds[] = {
1409.         se_squeak_C, se_squeak_D_flat, se_squeak_D,
1410.         se_squeak_E_flat, se_squeak_E, se_squeak_F,
1411.         se_squeak_F_sharp, se_squeak_G, se_squeak_G_sharp,
1412.         se_squeak_A, se_squeak_B_flat, se_squeak_B,
1413.     };
1414.     boolean forcetrap = ((trflags & FORCETRAP) != 0
1415.                          || (trflags & FAILEDUNTRAP) != 0
1416.                          || (Flying && (trflags & VIASITTING) != 0));
1417. 
1418.     if (mtmp == &gy.youmonst) {
1419.         if ((Levitation || Flying) && !forcetrap) {
1420.             if (!Blind) {
1421.                 seetrap(trap);
1422.                 if (Hallucination)
1423.                     You("notice a crease in the linoleum.");
1424.                 else
1425.                     You("notice a loose board below you.");
1426.             }
1427.         } else {
1428.             seetrap(trap);
1429.             if (IndexOk(trap->tnote, tsnds)) {
1430.                 Soundeffect(tsnds[trap->tnote], 50);
1431.             }
1432.             pline("A board beneath you %s%s%s.",
1433.                   Deaf ? "vibrates" : "squeaks ",
1434.                   Deaf ? "" : trapnote(trap, FALSE),
1435.                   Deaf ? "" : " loudly");
1436.             wake_nearby(FALSE);
1437.         }
1438.     } else {
1439.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1440. 
1441.         if (m_in_air(mtmp))
1442.             return Trap_Effect_Finished;
1443.         /* stepped on a squeaky board */
1444.         if (in_sight) {
1445.             if (!Deaf) {
1446.                 if (IndexOk(trap->tnote, tsnds)) {
1447.                     Soundeffect(tsnds[trap->tnote], 50);
1448.                 }
1449.                 pline_mon(mtmp,
1450.                       "A board beneath %s squeaks %s loudly.",
1451.                       mon_nam(mtmp), trapnote(trap, FALSE));
1452.                 seetrap(trap);
1453.             } else if (!mindless(mtmp->data)) {
1454.                 pline_mon(mtmp,
1455.                       "%s stops momentarily and appears to cringe.",
1456.                       Monnam(mtmp));
1457.             }
1458.         } else {
1459.             /* same near/far threshold as mzapmsg() */
1460.             int range = couldsee(mtmp->mx, mtmp->my) /* 9 or 5 */
1461.                 ? (BOLT_LIM + 1) : (BOLT_LIM - 3);
1462. 
1463.             if (IndexOk(trap->tnote, tsnds)) {
1464.                 Soundeffect(tsnds[trap->tnote],
1465.                              ((mdistu(mtmp) <= range * range)
1466.                                 ? 40 : 20));
1467.             }
1468.             You_hear("%s squeak %s.", trapnote(trap, FALSE),
1469.                      (mdistu(mtmp) <= range * range)
1470.                         ? "nearby" : "in the distance");
1471.         }
1472.         /* wake up nearby monsters */
1473.         wake_nearto(mtmp->mx, mtmp->my, 40);
1474.     }
1475.     return Trap_Effect_Finished;
1476. }
1477. 
1478. staticfn int
1479. trapeffect_bear_trap(
1480.     struct monst *mtmp,
1481.     struct trap *trap,
1482.     unsigned trflags)
1483. {
1484.     boolean is_you = mtmp == &gy.youmonst,
1485.             forcetrap = ((trflags & FORCETRAP) != 0
1486.                          || (trflags & FAILEDUNTRAP) != 0
1487.                          || (is_you && (trflags & VIASITTING) != 0));
1488. 
1489.     if (is_you) {
1490.         int dmg = d(2, 4);
1491. 
1492.         if ((Levitation || Flying) && !forcetrap)
1493.             return Trap_Effect_Finished;
1494.         feeltrap(trap);
1495.         if (amorphous(gy.youmonst.data) || is_whirly(gy.youmonst.data)
1496.             || unsolid(gy.youmonst.data)) {
1497.             pline("%s bear trap closes harmlessly through you.",
1498.                   A_Your[trap->madeby_u]);
1499.             return Trap_Effect_Finished;
1500.         }
1501.         if (!u.usteed && gy.youmonst.data->msize <= MZ_SMALL) {
1502.             pline("%s bear trap closes harmlessly over you.",
1503.                   A_Your[trap->madeby_u]);
1504.             return Trap_Effect_Finished;
1505.         }
1506.         set_utrap((unsigned) rn1(4, 4), TT_BEARTRAP);
1507.         if (u.usteed) {
1508.             pline("%s bear trap closes on %s %s!", A_Your[trap->madeby_u],
1509.                   s_suffix(mon_nam(u.usteed)), mbodypart(u.usteed, FOOT));
1510.             if (thitm(0, u.usteed, (struct obj *) 0, dmg, FALSE))
1511.                 reset_utrap(TRUE); /* steed died, hero not trapped */
1512.         } else {
1513.             pline("%s bear trap closes on your %s!", A_Your[trap->madeby_u],
1514.                   body_part(FOOT));
1515.             if (u.umonnum == PM_OWLBEAR || u.umonnum == PM_BUGBEAR)
1516.                 You("howl in anger!");
1517.             if (wearing_iron_shoes(mtmp))
1518.                 pline("%s protects your leg.", Yname2(uarmf));
1519.             else {
1520.                 set_wounded_legs(rn2(2) ? RIGHT_SIDE : LEFT_SIDE, rn1(10, 10));
1521.                 losehp(Maybe_Half_Phys(dmg), "bear trap", KILLED_BY_AN);
1522.             }
1523.         }
1524.         exercise(A_DEX, FALSE);
1525.     } else {
1526.         struct permonst *mptr = mtmp->data;
1527.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1528.         boolean trapkilled = FALSE;
1529. 
1530.         if (mptr->msize > MZ_SMALL && !amorphous(mptr) && !m_in_air(mtmp)
1531.             && !is_whirly(mptr) && !unsolid(mptr)) {
1532.             mtmp->mtrapped = 1;
1533.             if (in_sight) {
1534.                 pline_mon(mtmp,
1535.                       "%s is caught in %s bear trap!", Monnam(mtmp),
1536.                       a_your[trap->madeby_u]);
1537.                 seetrap(trap);
1538.             } else {
1539.                 if (mptr == &mons[PM_OWLBEAR]
1540.                     || mptr == &mons[PM_BUGBEAR]) {
1541.                     Soundeffect(se_roar, 100);
1542.                     You_hear("the roaring of an angry bear!");
1543.                 }
1544.             }
1545.         } else if (forcetrap) {
1546.             if (in_sight) {
1547.                 pline_mon(mtmp,
1548.                       "%s evades %s bear trap!", Monnam(mtmp),
1549.                       a_your[trap->madeby_u]);
1550.                 seetrap(trap);
1551.             }
1552.         }
1553.         if (mtmp->mtrapped && !wearing_iron_shoes(mtmp))
1554.             trapkilled = thitm(0, mtmp, (struct obj *) 0, d(2, 4), FALSE);
1555. 
1556.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1557.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1558.     }
1559.     return Trap_Effect_Finished;
1560. }
1561. 
1562. staticfn int
1563. trapeffect_slp_gas_trap(
1564.     struct monst *mtmp,
1565.     struct trap *trap,
1566.     unsigned int trflags UNUSED)
1567. {
1568.     if (mtmp == &gy.youmonst) {
1569.         seetrap(trap);
1570.         if (Sleep_resistance || breathless(gy.youmonst.data)) {
1571.             You("are enveloped in a cloud of gas!");
1572.             monstseesu(M_SEEN_SLEEP);
1573.         } else {
1574.             pline("A cloud of gas puts you to sleep!");
1575.             fall_asleep(-rnd(25), TRUE);
1576.             monstunseesu(M_SEEN_SLEEP);
1577.         }
1578.         (void) steedintrap(trap, (struct obj *) 0);
1579.     } else {
1580.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1581. 
1582.         if (!resists_sleep(mtmp) && !breathless(mtmp->data)
1583.             && !helpless(mtmp)) {
1584.             if (sleep_monst(mtmp, rnd(25), -1) && in_sight) {
1585.                 pline_mon(mtmp,
1586.                           "%s suddenly falls asleep!", Monnam(mtmp));
1587.                 seetrap(trap);
1588.             }
1589.         }
1590.     }
1591.     return Trap_Effect_Finished;
1592. }
1593. 
1594. staticfn int
1595. trapeffect_rust_trap(
1596.     struct monst *mtmp,
1597.     struct trap *trap,
1598.     unsigned int trflags UNUSED)
1599. {
1600.     struct obj *otmp, *nextobj;
1601. 
1602.     if (mtmp == &gy.youmonst) {
1603.         seetrap(trap);
1604. 
1605.         /* Unlike monsters, traps cannot aim their rust attacks at
1606.          * you, so instead of looping through and taking either the
1607.          * first rustable one or the body, we take whatever we get,
1608.          * even if it is not rustable.
1609.          */
1610.         switch (rn2(5)) {
1611.         case 0:
1612.             pline("%s you on the %s!", A_gush_of_water_hits, body_part(HEAD));
1613.             (void) water_damage(uarmh, helm_simple_name(uarmh), TRUE);
1614.             break;
1615.         case 1:
1616.             pline("%s your left %s!", A_gush_of_water_hits, body_part(ARM));
1617.             if (water_damage(uarms, "shield", TRUE) != ER_NOTHING)
1618.                 break;
1619.             if (u.twoweap || (uwep && bimanual(uwep)))
1620.                 (void) water_damage(u.twoweap ? uswapwep : uwep, 0, TRUE);
1621.  uglovecheck:
1622.             (void) water_damage(uarmg, gloves_simple_name(uarmg), TRUE);
1623.             break;
1624.         case 2:
1625.             pline("%s your right %s!", A_gush_of_water_hits, body_part(ARM));
1626.             (void) water_damage(uwep, 0, TRUE);
1627.             goto uglovecheck;
1628.         default:
1629.             pline("%s you!", A_gush_of_water_hits);
1630.             /* note: exclude primary and secondary weapons from splashing
1631.                because cases 1 and 2 target them [via water_damage()] */
1632.             for (otmp = gi.invent; otmp; otmp = nextobj) {
1633.                 nextobj = otmp->nobj;
1634.                 if (otmp->lamplit && otmp != uwep
1635.                     && (otmp != uswapwep || !u.twoweap))
1636.                     (void) splash_lit(otmp);
1637.             }
1638.             if (uarmc)
1639.                 (void) water_damage(uarmc, cloak_simple_name(uarmc), TRUE);
1640.             else if (uarm)
1641.                 (void) water_damage(uarm, suit_simple_name(uarm), TRUE);
1642.             else if (uarmu)
1643.                 (void) water_damage(uarmu, "shirt", TRUE);
1644.         }
1645.         update_inventory();
1646. 
1647.         if (u.umonnum == PM_IRON_GOLEM) {
1648.             int dam = u.mhmax;
1649. 
1650.             You("are covered with rust!");
1651.             losehp(Maybe_Half_Phys(dam), "rusting away", KILLED_BY);
1652.         } else if (u.umonnum == PM_GREMLIN && rn2(3)) {
1653.             (void) split_mon(&gy.youmonst, (struct monst *) 0);
1654.         }
1655.     } else {
1656.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1657.         boolean trapkilled = FALSE;
1658.         struct permonst *mptr = mtmp->data;
1659.         struct obj *target;
1660. 
1661.         if (in_sight)
1662.             seetrap(trap);
1663.         switch (rn2(5)) {
1664.         case 0:
1665.             if (in_sight)
1666.                 pline_mon(mtmp,
1667.                       "%s %s on the %s!", A_gush_of_water_hits,
1668.                       mon_nam(mtmp), mbodypart(mtmp, HEAD));
1669.             target = which_armor(mtmp, W_ARMH);
1670.             (void) water_damage(target, helm_simple_name(target), TRUE);
1671.             break;
1672.         case 1:
1673.             if (in_sight)
1674.                 pline_mon(mtmp,
1675.                       "%s %s's left %s!", A_gush_of_water_hits,
1676.                       mon_nam(mtmp), mbodypart(mtmp, ARM));
1677.             target = which_armor(mtmp, W_ARMS);
1678.             if (water_damage(target, "shield", TRUE) != ER_NOTHING)
1679.                 break;
1680.             target = MON_WEP(mtmp);
1681.             if (target && bimanual(target))
1682.                 (void) water_damage(target, 0, TRUE);
1683.  mglovecheck:
1684.             target = which_armor(mtmp, W_ARMG);
1685.             (void) water_damage(target, gloves_simple_name(target), TRUE);
1686.             break;
1687.         case 2:
1688.             if (in_sight)
1689.                 pline_mon(mtmp,
1690.                       "%s %s's right %s!", A_gush_of_water_hits,
1691.                       mon_nam(mtmp), mbodypart(mtmp, ARM));
1692.             (void) water_damage(MON_WEP(mtmp), 0, TRUE);
1693.             goto mglovecheck;
1694.         default:
1695.             if (in_sight)
1696.                 pline("%s %s!", A_gush_of_water_hits, mon_nam(mtmp));
1697.             for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
1698.                 if (otmp->lamplit
1699.                     /* exclude weapon(s) because cases 1 and 2 do them */
1700.                     && (otmp->owornmask & (W_WEP | W_SWAPWEP)) == 0)
1701.                     (void) splash_lit(otmp);
1702.             if ((target = which_armor(mtmp, W_ARMC)) != 0)
1703.                 (void) water_damage(target, cloak_simple_name(target),
1704.                                     TRUE);
1705.             else if ((target = which_armor(mtmp, W_ARM)) != 0)
1706.                 (void) water_damage(target, suit_simple_name(target),
1707.                                     TRUE);
1708.             else if ((target = which_armor(mtmp, W_ARMU)) != 0)
1709.                 (void) water_damage(target, "shirt", TRUE);
1710.         }
1711. 
1712.         if (completelyrusts(mptr)) {
1713.             if (in_sight)
1714.                 pline_mon(mtmp, "%s %s to pieces!", Monnam(mtmp),
1715.                       !mlifesaver(mtmp) ? "falls" : "starts to fall");
1716.             monkilled(mtmp, (const char *) 0, AD_RUST);
1717.             if (DEADMONSTER(mtmp))
1718.                 trapkilled = TRUE;
1719.         } else if (mptr == &mons[PM_GREMLIN] && rn2(3)) {
1720.             (void) split_mon(mtmp, (struct monst *) 0);
1721.         }
1722. 
1723.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1724.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1725.     }
1726.     return Trap_Effect_Finished;
1727. }
1728. 
1729. staticfn int
1730. trapeffect_fire_trap(
1731.     struct monst *mtmp,
1732.     struct trap *trap,
1733.     unsigned int trflags UNUSED)
1734. {
1735.     if (mtmp == &gy.youmonst) {
1736.         seetrap(trap);
1737.         dofiretrap((struct obj *) 0);
1738.     } else {
1739.         coordxy tx = trap->tx, ty = trap->ty;
1740.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1741.         boolean see_it = cansee(tx, ty);
1742.         boolean trapkilled = FALSE;
1743.         struct permonst *mptr = mtmp->data;
1744.         int orig_dmg = d(2, 4);
1745. 
1746.         if (in_sight)
1747.             pline_mon(mtmp,
1748.                  "A %s erupts from the %s under %s!", tower_of_flame,
1749.                   surface(mtmp->mx, mtmp->my), mon_nam(mtmp));
1750.         else if (see_it) { /* evidently `mtmp' is invisible */
1751.             set_msg_xy(mtmp->mx, mtmp->my);
1752.             You_see("a %s erupt from the %s!", tower_of_flame,
1753.                     surface(mtmp->mx, mtmp->my));
1754.         }
1755.         if (resists_fire(mtmp)) {
1756.             if (in_sight) {
1757.                 shieldeff(mtmp->mx, mtmp->my);
1758.                 pline("%s is uninjured.", Monnam(mtmp));
1759.             }
1760.         } else {
1761.             int num = orig_dmg, alt;
1762.             boolean immolate = FALSE;
1763. 
1764.             /* paper burns very fast, assume straw is tightly packed
1765.                and burns a bit slower
1766.                (note: this is inconsistent with mattackm()'s AD_FIRE
1767.                damage where completelyburns() includes straw golem) */
1768.             switch (monsndx(mptr)) {
1769.             case PM_PAPER_GOLEM:
1770.                 immolate = TRUE;
1771.                 alt = mtmp->mhpmax;
1772.                 break;
1773.             case PM_STRAW_GOLEM:
1774.                 alt = mtmp->mhpmax / 2;
1775.                 break;
1776.             case PM_WOOD_GOLEM:
1777.                 alt = mtmp->mhpmax / 4;
1778.                 break;
1779.             case PM_LEATHER_GOLEM:
1780.                 alt = mtmp->mhpmax / 8;
1781.                 break;
1782.             default:
1783.                 alt = 0;
1784.                 break;
1785.             }
1786.             if (alt > num)
1787.                 num = alt;
1788. 
1789.             if (thitm(0, mtmp, (struct obj *) 0, num, immolate))
1790.                 trapkilled = TRUE;
1791.             else {
1792.                 mtmp->mhpmax -= rn2(num + 1);
1793.                 if (mtmp->mhp > mtmp->mhpmax)
1794.                     mtmp->mhp = mtmp->mhpmax;
1795.             }
1796.         }
1797.         if (burnarmor(mtmp) || rn2(3)) {
1798.             int xtradmg = destroy_items(mtmp, AD_FIRE, orig_dmg);
1799.             ignite_items(mtmp->minvent);
1800.             if (!DEADMONSTER(mtmp)) {
1801.                 mtmp->mhp -= xtradmg;
1802.                 if (DEADMONSTER(mtmp)) { /* NOW it's dead */
1803.                     monkilled(mtmp, "", AD_FIRE);
1804.                     trapkilled = TRUE;
1805.                 }
1806.             }
1807.         }
1808.         if (burn_floor_objects(tx, ty, see_it, FALSE)
1809.             && !see_it && distu(tx, ty) <= 3 * 3)
1810.             You("smell smoke.");
1811.         if (is_ice(tx, ty))
1812.             melt_ice(tx, ty, (char *) 0);
1813.         if (DEADMONSTER(mtmp))
1814.             trapkilled = TRUE;
1815.         if (see_it && t_at(tx, ty))
1816.             seetrap(t_at(tx, ty));
1817. 
1818.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
1819.             ? Trap_Caught_Mon : Trap_Effect_Finished;
1820.     }
1821.     return Trap_Effect_Finished;
1822. }
1823. 
1824. staticfn int
1825. trapeffect_pit(
1826.     struct monst *mtmp,
1827.     struct trap *trap,
1828.     unsigned int trflags)
1829. {
1830.     int ttype = trap->ttyp;
1831.     /* relevant_spikes is initially always true for spiked pits, but
1832.        set to false if the spikes are found to not be relevant */
1833.     boolean relevant_spikes = ttype == SPIKED_PIT;
1834. 
1835.     if (mtmp == &gy.youmonst) {
1836.         boolean plunged = (trflags & TOOKPLUNGE) != 0;
1837.         boolean viasitting = (trflags & VIASITTING) != 0;
1838.         boolean conj_pit = conjoined_pits(trap, t_at(u.ux0, u.uy0), TRUE);
1839.         boolean adj_pit = adj_nonconjoined_pit(trap);
1840.         boolean already_known = trap->tseen ? TRUE : FALSE;
1841.         boolean deliberate = FALSE;
1842.         int steed_article = ARTICLE_THE;
1843. 
1844.         /* suppress article in various steed messages when using its
1845.            name (which won't occur when hallucinating) */
1846.         if (u.usteed && has_mgivenname(u.usteed) && !Hallucination)
1847.             steed_article = ARTICLE_NONE;
1848. 
1849.         /* KMH -- You can't escape the Sokoban level traps */
1850.         if (!Sokoban && (Levitation || (Flying && !plunged && !viasitting)))
1851.             return Trap_Effect_Finished;
1852.         feeltrap(trap);
1853.         if (!Sokoban && is_clinger(gy.youmonst.data) && !plunged) {
1854.             if (already_known) {
1855.                 You_see("%s %spit below you.", a_your[trap->madeby_u],
1856.                         ttype == SPIKED_PIT ? "spiked " : "");
1857.             } else {
1858.                 pline("%s pit %sopens up under you!", A_Your[trap->madeby_u],
1859.                       ttype == SPIKED_PIT ? "full of spikes " : "");
1860.                 You("don't fall in!");
1861.             }
1862.             return Trap_Effect_Finished;
1863.         }
1864.         if (!Sokoban) {
1865.             char verbbuf[BUFSZ];
1866. 
1867.             *verbbuf = '\0';
1868.             if (u.usteed) {
1869.                 if ((trflags & RECURSIVETRAP) != 0)
1870.                     Sprintf(verbbuf, "and %s fall",
1871.                             x_monnam(u.usteed, steed_article, (char *) 0,
1872.                                      SUPPRESS_SADDLE, FALSE));
1873.                 else
1874.                     Sprintf(verbbuf, "lead %s",
1875.                             x_monnam(u.usteed, steed_article, "poor",
1876.                                      SUPPRESS_SADDLE, FALSE));
1877.             } else if (iflags.menu_requested && already_known) {
1878.                 You("carefully %s into the pit.",
1879.                     u_locomotion("lower yourself"));
1880.                 deliberate = TRUE;
1881.             } else if (conj_pit) {
1882.                 You("move into an adjacent pit.");
1883.             } else if (adj_pit) {
1884.                 You("stumble over debris%s.",
1885.                     !rn2(5) ? " between the pits" : "");
1886.             } else {
1887.                 Strcpy(verbbuf,
1888.                        !plunged ? "fall" : (Flying ? "dive" : "plunge"));
1889.             }
1890.             if (*verbbuf)
1891.                 You("%s into %s pit!", verbbuf, a_your[trap->madeby_u]);
1892.         }
1893.         /* wumpus reference */
1894.         if (Role_if(PM_RANGER) && !trap->madeby_u && !trap->once
1895.             && In_quest(&u.uz) && Is_qlocate(&u.uz)) {
1896.             pline("Fortunately it has a bottom after all...");
1897.             trap->once = 1;
1898.         } else if (u.umonnum == PM_PIT_VIPER || u.umonnum == PM_PIT_FIEND) {
1899.             pline("How pitiful.  Isn't that the pits?");
1900.         }
1901.         if (relevant_spikes && wearing_iron_shoes(mtmp)) {
1902.             pline("%s protects you from the sharp iron spikes.", Yname2(uarmf));
1903.             relevant_spikes = FALSE;
1904.         } else if (relevant_spikes) {
1905.             const char *predicament = "on a set of sharp iron spikes";
1906. 
1907.             if (u.usteed) {
1908.                 pline("%s %s %s!",
1909.                       upstart(x_monnam(u.usteed, steed_article, "poor",
1910.                                        SUPPRESS_SADDLE, FALSE)),
1911.                       conj_pit ? "steps" : "lands", predicament);
1912.             } else
1913.                 You("%s %s!", conj_pit ? "step" : "land", predicament);
1914.         }
1915.         /* FIXME:
1916.          * if hero gets killed here, setting u.utrap in advance will
1917.          * show "you were trapped in a pit" during disclosure's display
1918.          * of enlightenment, but hero is dying *before* becoming trapped.
1919.          */
1920.         set_utrap((unsigned) rn1(6, 2), TT_PIT);
1921.         if (!steedintrap(trap, (struct obj *) 0)) {
1922.             if (relevant_spikes) {
1923.                 int oldumort = u.umortality;
1924. 
1925.                 losehp(Maybe_Half_Phys(rnd(conj_pit ? 4 : adj_pit ? 6 : 10)),
1926.                        /* note: these don't need locomotion() handling;
1927.                           if fatal while poly'd and Unchanging, the
1928.                           death reason will be overridden with
1929.                           "killed while stuck in creature form" */
1930.                        plunged
1931.                        ? "deliberately plunged into a pit of iron spikes"
1932.                        : (conj_pit || deliberate)
1933.                          ? "stepped into a pit of iron spikes"
1934.                          : adj_pit
1935.                            ? "stumbled into a pit of iron spikes"
1936.                            : "fell into a pit of iron spikes",
1937.                        NO_KILLER_PREFIX);
1938.                 if (!rn2(6))
1939.                     poisoned("spikes", A_STR,
1940.                              (conj_pit || adj_pit || deliberate)
1941.                              ? "stepping on poison spikes"
1942.                              : "fall onto poison spikes",
1943.                              /* if damage triggered life-saving,
1944.                                 poison is limited to attrib loss */
1945.                              (u.umortality > oldumort) ? 0 : 8, FALSE);
1946.             } else {
1947.                 /* plunging flyers take spike damage but not pit damage */
1948.                 if (!conj_pit && !deliberate
1949.                     && !(plunged && (Flying || is_clinger(gy.youmonst.data))))
1950.                     losehp(Maybe_Half_Phys(rnd(adj_pit ? 3 : 6)),
1951.                            plunged ? "deliberately plunged into a pit"
1952.                            : "fell into a pit",
1953.                            NO_KILLER_PREFIX);
1954.             }
1955.             if (Punished && !carried(uball)) {
1956.                 unplacebc();
1957.                 ballfall();
1958.                 placebc();
1959.             }
1960.             if (!conj_pit)
1961.                 selftouch("Falling, you");
1962.             gv.vision_full_recalc = 1; /* vision limits change */
1963.             exercise(A_STR, FALSE);
1964.             exercise(A_DEX, FALSE);
1965.         }
1966.     } else {
1967.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
1968.         boolean trapkilled = FALSE;
1969.         boolean forcetrap = ((trflags & FORCETRAP) != 0);
1970.         boolean inescapable = (forcetrap || (Sokoban && !trap->madeby_u));
1971.         struct permonst *mptr = mtmp->data;
1972.         const char *fallverb;
1973. 
1974.         fallverb = "falls";
1975.         if (!grounded(mptr) || (mtmp->wormno && count_wsegs(mtmp) > 5)) {
1976.             if (forcetrap && !Sokoban) {
1977.                 /* openfallingtrap; not inescapable here */
1978.                 if (in_sight) {
1979.                     seetrap(trap);
1980.                     pline_mon(mtmp,
1981.                              "%s doesn't fall into the pit.", Monnam(mtmp));
1982.                 }
1983.                 return Trap_Effect_Finished;
1984.             }
1985.             if (!inescapable)
1986.                 return Trap_Effect_Finished; /* avoids trap */
1987.             fallverb = "is dragged"; /* sokoban pit */
1988.         }
1989.         if (!passes_walls(mptr))
1990.             mtmp->mtrapped = 1;
1991.         if (in_sight) {
1992.             pline_mon(mtmp,
1993.                      "%s %s into %s pit!", Monnam(mtmp), fallverb,
1994.                      a_your[trap->madeby_u]);
1995.             if (mptr == &mons[PM_PIT_VIPER]
1996.                 || mptr == &mons[PM_PIT_FIEND])
1997.                 pline("How pitiful.  Isn't that the pits?");
1998.             seetrap(trap);
1999.         }
2000.         mselftouch(mtmp, "Falling, ", FALSE);
2001.         if (wearing_iron_shoes(mtmp)) relevant_spikes = FALSE;
2002.         if (DEADMONSTER(mtmp) || thitm(0, mtmp, (struct obj *) 0,
2003.                                        rnd(relevant_spikes ? 10 : 6), FALSE))
2004.             trapkilled = TRUE;
2005. 
2006.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
2007.             ? Trap_Caught_Mon : Trap_Effect_Finished;
2008.     }
2009.     return Trap_Effect_Finished;
2010. }
2011. 
2012. staticfn int
2013. trapeffect_hole(
2014.     struct monst *mtmp,
2015.     struct trap *trap,
2016.     unsigned int trflags)
2017. {
2018.     if (mtmp == &gy.youmonst) {
2019.         if (!Can_fall_thru(&u.uz)) {
2020.             seetrap(trap); /* normally done in fall_through */
2021.             impossible("dotrap: %ss cannot exist on this level.",
2022.                        trapname(trap->ttyp, TRUE));
2023.             return Trap_Effect_Finished; /* don't activate it after all */
2024.         }
2025.         fall_through(TRUE, (trflags & TOOKPLUNGE));
2026.     } else {
2027.         int tt = trap->ttyp;
2028.         struct permonst *mptr = mtmp->data;
2029.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2030.         boolean forcetrap = ((trflags & FORCETRAP) != 0);
2031.         boolean inescapable = (forcetrap || (Sokoban && !trap->madeby_u));
2032. 
2033.         if (!Can_fall_thru(&u.uz)) {
2034.             impossible("mintrap: %ss cannot exist on this level.",
2035.                        trapname(tt, TRUE));
2036.             return Trap_Effect_Finished; /* don't activate it after all */
2037.         }
2038.         if (!grounded(mptr) || (mtmp->wormno && count_wsegs(mtmp) > 5)
2039.             || mptr->msize >= MZ_HUGE) {
2040.             if (forcetrap && !Sokoban) {
2041.                 /* openfallingtrap; not inescapable here */
2042.                 if (in_sight) {
2043.                     seetrap(trap);
2044.                     if (tt == TRAPDOOR)
2045.                         pline_mon(mtmp,
2046.                             "A trap door opens, but %s doesn't fall through.",
2047.                               mon_nam(mtmp));
2048.                     else /* (tt == HOLE) */
2049.                         pline_mon(mtmp,
2050.                                  "%s doesn't fall through the hole.",
2051.                                  Monnam(mtmp));
2052.                 }
2053.                 return Trap_Effect_Finished; /* inescapable = FALSE; */
2054.             }
2055.             if (inescapable) { /* sokoban hole */
2056.                 if (in_sight) {
2057.                     pline_mon(mtmp,
2058.                              "%s seems to be yanked down!", Monnam(mtmp));
2059.                     seetrap(trap);
2060.                 }
2061.             } else
2062.                 return Trap_Effect_Finished;
2063.         }
2064.         return trapeffect_level_telep(mtmp, trap, trflags);
2065.     }
2066.     return Trap_Effect_Finished;
2067. }
2068. 
2069. staticfn int
2070. trapeffect_telep_trap(
2071.     struct monst *mtmp,
2072.     struct trap *trap,
2073.     unsigned int trflags UNUSED)
2074. {
2075.     if (mtmp == &gy.youmonst) {
2076.         seetrap(trap);
2077.         tele_trap(trap);
2078.     } else {
2079.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2080. 
2081.         mtele_trap(mtmp, trap, in_sight);
2082.         return Trap_Moved_Mon;
2083.     }
2084.     return Trap_Effect_Finished;
2085. }
2086. 
2087. staticfn int
2088. trapeffect_level_telep(
2089.     struct monst *mtmp,
2090.     struct trap *trap,
2091.     unsigned int trflags)
2092. {
2093.     if (mtmp == &gy.youmonst) {
2094.         seetrap(trap);
2095.         level_tele_trap(trap, trflags);
2096.     } else {
2097.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2098.         boolean forcetrap = ((trflags & FORCETRAP) != 0);
2099. 
2100.         return mlevel_tele_trap(mtmp, trap, forcetrap, in_sight);
2101.     }
2102.     return Trap_Effect_Finished;
2103. }
2104. 
2105. staticfn int
2106. trapeffect_web(
2107.     struct monst *mtmp,
2108.     struct trap *trap,
2109.     unsigned int trflags)
2110. {
2111.     if (mtmp == &gy.youmonst) {
2112.         boolean webmsgok = (trflags & NOWEBMSG) == 0;
2113.         boolean forcetrap = ((trflags & FORCETRAP) != 0
2114.                              || (trflags & FAILEDUNTRAP) != 0);
2115.         boolean viasitting = (trflags & VIASITTING) != 0;
2116.         int steed_article = ARTICLE_THE;
2117. 
2118.         /* suppress article in various steed messages when using its
2119.            name (which won't occur when hallucinating) */
2120.         if (u.usteed && has_mgivenname(u.usteed) && !Hallucination)
2121.             steed_article = ARTICLE_NONE;
2122. 
2123.         feeltrap(trap);
2124.         if (mu_maybe_destroy_web(&gy.youmonst, webmsgok, trap))
2125.             return Trap_Effect_Finished;
2126.         if (webmaker(gy.youmonst.data)) {
2127.             if (webmsgok)
2128.                 pline(trap->madeby_u ? "You take a walk on your web."
2129.                       : "There is a spider web here.");
2130.             return Trap_Effect_Finished;
2131.         }
2132.         if (webmsgok) {
2133.             char verbbuf[BUFSZ];
2134. 
2135.             if (forcetrap || viasitting) {
2136.                 Strcpy(verbbuf, "are caught by");
2137.             } else if (u.usteed) {
2138.                 Sprintf(verbbuf, "lead %s into",
2139.                         x_monnam(u.usteed, steed_article, "poor",
2140.                                  SUPPRESS_SADDLE, FALSE));
2141.             } else {
2142.                 Sprintf(verbbuf, "%s into", u_locomotion("stumble"));
2143.             }
2144.             You("%s %s spider web!", verbbuf, a_your[trap->madeby_u]);
2145.         }
2146. 
2147.         /* time will be adjusted below */
2148.         set_utrap(1, TT_WEB);
2149. 
2150.         /* Time stuck in the web depends on your/steed's strength. */
2151.         {
2152.             int tim, str = ACURR(A_STR);
2153. 
2154.             /* If mounted, the steed gets trapped.  Use mintrap
2155.              * to do all the work.  If mtrapped is set as a result,
2156.              * unset it and set utrap instead.  In the case of a
2157.              * strongmonst and mintrap said it's trapped, use a
2158.              * short but non-zero trap time.  Otherwise, monsters
2159.              * have no specific strength, so use player strength.
2160.              * This gets skipped for webmsgok, which implies that
2161.              * the steed isn't a factor.
2162.              */
2163.             if (u.usteed && webmsgok) {
2164.                 /* mtmp location might not be up to date */
2165.                 u.usteed->mx = u.ux;
2166.                 u.usteed->my = u.uy;
2167. 
2168.                 /* mintrap currently does not return Trap_Killed_Mon
2169.                    (mon died) for webs */
2170.                 if (mintrap(u.usteed, trflags) != Trap_Effect_Finished) {
2171.                     u.usteed->mtrapped = 0;
2172.                     if (strongmonst(u.usteed->data))
2173.                         str = 17;
2174.                 } else {
2175.                     reset_utrap(FALSE);
2176.                     return Trap_Effect_Finished;
2177.                 }
2178. 
2179.                 webmsgok = FALSE; /* mintrap printed the messages */
2180.             }
2181.             if (str <= 3)
2182.                 tim = rn1(6, 6);
2183.             else if (str < 6)
2184.                 tim = rn1(6, 4);
2185.             else if (str < 9)
2186.                 tim = rn1(4, 4);
2187.             else if (str < 12)
2188.                 tim = rn1(4, 2);
2189.             else if (str < 15)
2190.                 tim = rn1(2, 2);
2191.             else if (str < 18)
2192.                 tim = rnd(2);
2193.             else if (str < 69)
2194.                 tim = 1;
2195.             else {
2196.                 tim = 0;
2197.                 if (webmsgok)
2198.                     You("tear through %s web!", a_your[trap->madeby_u]);
2199.                 deltrap(trap);
2200.                 newsym(u.ux, u.uy); /* get rid of trap symbol */
2201.             }
2202.             set_utrap((unsigned) tim, TT_WEB);
2203.         }
2204.     } else {
2205.         /* Monster in a web. */
2206.         boolean tear_web;
2207.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2208.         boolean forcetrap = ((trflags & FORCETRAP) != 0);
2209.         struct permonst *mptr = mtmp->data;
2210. 
2211.         if (webmaker(mptr))
2212.             return Trap_Effect_Finished;
2213.         if (mu_maybe_destroy_web(mtmp, in_sight, trap))
2214.             return Trap_Effect_Finished;
2215.         tear_web = FALSE;
2216.         switch (monsndx(mptr)) {
2217.         case PM_OWLBEAR: /* Eric Backus */
2218.         case PM_BUGBEAR:
2219.             if (!in_sight) {
2220.                 Soundeffect(se_roar, 60);
2221.                 You_hear("the roaring of a confused bear!");
2222.                 mtmp->mtrapped = 1;
2223.                 break;
2224.             }
2225.             FALLTHROUGH;
2226.             /*FALLTHRU*/
2227.         default:
2228.             if (mptr->mlet == S_GIANT
2229.                 /* exclude baby dragons and relatively short worms */
2230.                 || (mptr->mlet == S_DRAGON && extra_nasty(mptr))
2231.                 || (mtmp->wormno && count_wsegs(mtmp) > 5)) {
2232.                 tear_web = TRUE;
2233.             } else if (in_sight) {
2234.                 pline_mon(mtmp,
2235.                       "%s is caught in %s spider web.", Monnam(mtmp),
2236.                       a_your[trap->madeby_u]);
2237.                 seetrap(trap);
2238.             }
2239.             mtmp->mtrapped = tear_web ? 0 : 1;
2240.             break;
2241.             /* this list is fairly arbitrary; it deliberately
2242.                excludes wumpus & giant/ettin zombies/mummies */
2243.         case PM_TITANOTHERE:
2244.         case PM_BALUCHITHERIUM:
2245.         case PM_PURPLE_WORM:
2246.         case PM_JABBERWOCK:
2247.         case PM_IRON_GOLEM:
2248.         case PM_BALROG:
2249.         case PM_KRAKEN:
2250.         case PM_MASTODON:
2251.         case PM_ORION:
2252.         case PM_NORN:
2253.         case PM_CYCLOPS:
2254.         case PM_LORD_SURTUR:
2255.             tear_web = TRUE;
2256.             break;
2257.         }
2258.         if (tear_web) {
2259.             if (in_sight)
2260.                 pline_mon(mtmp,
2261.                      "%s tears through %s spider web!", Monnam(mtmp),
2262.                       a_your[trap->madeby_u]);
2263.             deltrap(trap);
2264.             newsym(mtmp->mx, mtmp->my);
2265.         } else if (forcetrap && !mtmp->mtrapped) {
2266.             if (in_sight) {
2267.                 pline_mon(mtmp,
2268.                       "%s avoids %s spider web!", Monnam(mtmp),
2269.                       a_your[trap->madeby_u]);
2270.                 seetrap(trap);
2271.             }
2272.         }
2273.         return mtmp->mtrapped ? Trap_Caught_Mon : Trap_Effect_Finished;
2274.     }
2275.     return Trap_Effect_Finished;
2276. }
2277. 
2278. staticfn int
2279. trapeffect_statue_trap(
2280.     struct monst *mtmp,
2281.     struct trap *trap,
2282.     unsigned int trflags UNUSED)
2283. {
2284.     if (mtmp == &gy.youmonst) {
2285.         (void) activate_statue_trap(trap, u.ux, u.uy, FALSE);
2286.     } else {
2287.         /* monsters don't trigger statue traps */
2288.     }
2289.     return Trap_Effect_Finished;
2290. }
2291. 
2292. staticfn int
2293. trapeffect_magic_trap(
2294.     struct monst *mtmp,
2295.     struct trap *trap,
2296.     unsigned int trflags)
2297. {
2298.     if (mtmp == &gy.youmonst) {
2299.         seetrap(trap);
2300.         if (!rn2(30)) {
2301.             deltrap(trap);
2302.             newsym(u.ux, u.uy); /* update position */
2303.             You("are caught in a magical explosion!");
2304.             losehp(rnd(10), "magical explosion", KILLED_BY_AN);
2305.             Your("body absorbs some of the magical energy!");
2306.             u.uen = (u.uenmax += 2);
2307.             if (u.uenmax > u.uenpeak)
2308.                 u.uenpeak = u.uenmax;
2309.             return Trap_Effect_Finished;
2310.         } else {
2311.             domagictrap();
2312.         }
2313.         (void) steedintrap(trap, (struct obj *) 0);
2314.     } else {
2315.         /* A magic trap.  Monsters usually immune. */
2316.         if (!rn2(21))
2317.             return trapeffect_fire_trap(mtmp, trap, trflags);
2318.     }
2319.     return Trap_Effect_Finished;
2320. }
2321. 
2322. staticfn int
2323. trapeffect_anti_magic(
2324.     struct monst *mtmp, /* monster, possibly youmonst */
2325.     struct trap *trap,  /* trap->ttyp == ANTI_MAGIC */
2326.     unsigned int trflags UNUSED)
2327. {
2328.     if (wearing_iron_shoes(mtmp)) {
2329.         struct obj *shoes = which_armor(mtmp, W_ARMF);
2330.         /* iron shoes protect against antimagic traps only if
2331.            positively enchanted; the trap drains the enchantment
2332.            rather than the wearer */
2333.         if (shoes->spe > 0) {
2334.             /* no message if a monster does this, it isn't visible enough */
2335.             if (mtmp == &gy.youmonst) {
2336.                 seetrap(trap);
2337.                 pline("A lethargic aura surrounds %s.", yname(shoes));
2338.                 costly_alteration(shoes, COST_DECHNT);
2339.             }
2340.             shoes->spe -= 1;
2341.             update_inventory();
2342.             return Trap_Effect_Finished;
2343.         }
2344.     }
2345. 
2346.     if (mtmp == &gy.youmonst) {
2347.         int drain, halfd;
2348.         boolean exclaim_it = FALSE;
2349. 
2350.         seetrap(trap);
2351.         if (Antimagic) {
2352.             struct obj *otmp;
2353.             int dmgval2 = rnd(4), hp = Upolyd ? u.mh : u.uhp;
2354. 
2355.             /* Half_XXX_damage has opposite its usual effect (approx)
2356.                but isn't cumulative if hero has more than one */
2357.             if (Half_physical_damage || Half_spell_damage)
2358.                 dmgval2 += rnd(4);
2359.             /* give Magicbane wielder dose of own medicine */
2360.             if (u_wield_art(ART_MAGICBANE))
2361.                 dmgval2 += rnd(4);
2362.             /* having an artifact--other than own quest one--which
2363.                confers magic resistance simply by being carried
2364.                also increases the effect */
2365.             for (otmp = gi.invent; otmp; otmp = otmp->nobj)
2366.                 if (otmp->oartifact && !is_quest_artifact(otmp)
2367.                     && defends_when_carried(AD_MAGM, otmp))
2368.                     break;
2369.             if (otmp)
2370.                 dmgval2 += rnd(4);
2371.             if (Passes_walls)
2372.                 dmgval2 = (dmgval2 + 3) / 4;
2373. 
2374.             You_feel((dmgval2 >= hp) ? "unbearably torpid!"
2375.                      : (dmgval2 >= hp / 4) ? "very lethargic."
2376.                        : "sluggish.");
2377.             /* opposite of magical explosion */
2378.             losehp(dmgval2, "anti-magic implosion", KILLED_BY_AN);
2379.         }
2380. 
2381.         /* if the drain amount is more than hero's maximum energy then up
2382.            to half of the amount comes directly out of maximum, the rest
2383.            comes out of current energy; drain_en() lowers the current
2384.            amount and when doing so it will take even more from maximum
2385.            if the new current value would drop below zero */
2386.         drain = d(2, 6); /* 2d6 => 2..12 */
2387.         halfd = rnd(drain / 2); /* 1..drain/2 (round down) */
2388.         if (u.uenmax > drain) { /* [was u.uenmax > halfd] */
2389.             /* note: since 'halfd' is no more than half, 'drain -= halfd'
2390.                is at least as big, so drain_en() is never asked to remove
2391.                less from current than what we're removing from maximum;
2392.                however, it might do that anyway (via its throttle check) so
2393.                it needs to make sure uen doesn't end up exceeding uenmax */
2394.             u.uenmax -= halfd; /* drain_en() will set context.botl */
2395.             drain -= halfd;
2396.             exclaim_it = TRUE;
2397.         }
2398.         drain_en(drain, exclaim_it);
2399.     } else {
2400.         boolean trapkilled = FALSE;
2401.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2402.         boolean see_it = cansee(mtmp->mx, mtmp->my);
2403.         struct permonst *mptr = mtmp->data;
2404. 
2405.         /* similar to hero's case, more or less */
2406.         if (!resists_magm(mtmp)) { /* lose spell energy */
2407.             if (!mtmp->mcan && (attacktype(mptr, AT_MAGC)
2408.                                 || attacktype(mptr, AT_BREA))) {
2409.                 mtmp->mspec_used += d(2, 6);
2410.                 if (in_sight) {
2411.                     seetrap(trap);
2412.                     pline_mon(mtmp, "%s seems lethargic.",
2413.                               Monnam(mtmp));
2414.                 }
2415.             }
2416.         } else { /* take some damage */
2417.             struct obj *otmp;
2418.             int dmgval2 = rnd(4);
2419. 
2420.             if ((otmp = MON_WEP(mtmp)) != 0
2421.                 && is_art(otmp, ART_MAGICBANE))
2422.                 dmgval2 += rnd(4);
2423.             for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
2424.                 if (otmp->oartifact
2425.                     && defends_when_carried(AD_MAGM, otmp))
2426.                     break;
2427.             if (otmp)
2428.                 dmgval2 += rnd(4);
2429.             if (passes_walls(mptr))
2430.                 dmgval2 = (dmgval2 + 3) / 4;
2431. 
2432.             if (in_sight)
2433.                 seetrap(trap);
2434.             mtmp->mhp -= dmgval2;
2435.             if (DEADMONSTER(mtmp))
2436.                 monkilled(mtmp,
2437.                           in_sight
2438.                           ? "compression from an anti-magic field"
2439.                           : (const char *) 0,
2440.                           -AD_MAGM);
2441.             if (DEADMONSTER(mtmp))
2442.                 trapkilled = TRUE;
2443.             if (see_it)
2444.                 newsym(trap->tx, trap->ty);
2445.         }
2446.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
2447.             ? Trap_Caught_Mon : Trap_Effect_Finished;
2448.     }
2449.     return Trap_Effect_Finished;
2450. }
2451. 
2452. staticfn int
2453. trapeffect_poly_trap(
2454.     struct monst *mtmp,
2455.     struct trap *trap,
2456.     unsigned int trflags)
2457. {
2458.     if (mtmp == &gy.youmonst) {
2459.         boolean viasitting = (trflags & VIASITTING) != 0;
2460.         int steed_article = ARTICLE_THE;
2461.         char verbbuf[BUFSZ];
2462. 
2463.         /* suppress article in various steed messages when using its
2464.            name (which won't occur when hallucinating) */
2465.         if (u.usteed && has_mgivenname(u.usteed) && !Hallucination)
2466.             steed_article = ARTICLE_NONE;
2467. 
2468.         seetrap(trap);
2469.         if (viasitting)
2470.             Strcpy(verbbuf, "trigger"); /* follows "You sit down." */
2471.         else if (u.usteed)
2472.             Sprintf(verbbuf, "lead %s onto",
2473.                     x_monnam(u.usteed, steed_article, (char *) 0,
2474.                              SUPPRESS_SADDLE, FALSE));
2475.         else
2476.             Sprintf(verbbuf, "%s onto", u_locomotion("step"));
2477.         You("%s a polymorph trap!", verbbuf);
2478.         if (wearing_iron_shoes(mtmp)) {
2479.             deltrap(trap);
2480.             pline("%s warps strangely.", Yname2(uarmf));
2481.             poly_obj(
2482.                 uarmf, uarmf->otyp == IRON_SHOES ? KICKING_BOOTS : IRON_SHOES);
2483.             update_inventory();
2484.             if (uarmf)
2485.                 prinv(NULL, uarmf, 0);
2486.         } else if (Antimagic || Unchanging) {
2487.             shieldeff(u.ux, u.uy);
2488.             You_feel("momentarily different.");
2489.             /* Trap did nothing; don't remove it --KAA */
2490.         } else {
2491.             (void) steedintrap(trap, (struct obj *) 0);
2492.             deltrap(trap);      /* delete trap before polymorph */
2493.             newsym(u.ux, u.uy); /* get rid of trap symbol */
2494.             You_feel("a change coming over you.");
2495.             polyself(POLY_NOFLAGS);
2496.         }
2497.     } else {
2498.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2499. 
2500.         if (wearing_iron_shoes(mtmp)) {
2501.             /* remove and readd the shoes to forcibly unwear them */
2502.             struct obj *shoes = which_armor(mtmp, W_ARMF);
2503.             extract_from_minvent(mtmp, shoes, TRUE, TRUE);
2504.             if (mpickobj(mtmp, shoes)) {
2505.                 impossible("re-equipping iron shoes destroyed them?");
2506.                 return Trap_Effect_Finished;
2507.             }
2508.             shoes = poly_obj(
2509.                 shoes, shoes->otyp == IRON_SHOES ? KICKING_BOOTS : IRON_SHOES);
2510.             /* now equip them again */
2511.             if (shoes) {
2512.                 mtmp->misc_worn_check |= W_ARMF;
2513.                 shoes->owornmask = W_ARMF;
2514.                 update_mon_extrinsics(mtmp, shoes, TRUE, TRUE);
2515.             }
2516.         } else if (resists_magm(mtmp)) {
2517.             shieldeff_mon(mtmp);
2518.         } else if (!resist(mtmp, WAND_CLASS, 0, NOTELL)) {
2519.             (void) newcham(mtmp, (struct permonst *) 0, NC_SHOW_MSG);
2520.             if (in_sight)
2521.                 seetrap(trap);
2522.         }
2523.     }
2524.     return Trap_Effect_Finished;
2525. }
2526. 
2527. staticfn int
2528. trapeffect_landmine(
2529.     struct monst *mtmp,
2530.     struct trap *trap,
2531.     unsigned int trflags)
2532. {
2533.     int damage = rnd(16);
2534.     /* iron shoes protect against much of the damage from the
2535.        explosion, but you still take some damage (and wound legs)
2536.        because they can't fully block the blast */
2537.     if (wearing_iron_shoes(mtmp))
2538.         damage = (damage + 3) / 4;
2539. 
2540.     if (mtmp == &gy.youmonst) {
2541.         boolean already_seen = trap->tseen;
2542.         boolean forcetrap = ((trflags & FORCETRAP) != 0
2543.                              || (trflags & FAILEDUNTRAP) != 0);
2544.         boolean forcebungle = (trflags & FORCEBUNGLE) != 0;
2545.         unsigned steed_mid = 0;
2546.         struct obj *saddle = 0;
2547. 
2548.         if ((Levitation || Flying) && !forcetrap) {
2549.             if (!already_seen && rn2(3))
2550.                 return Trap_Effect_Finished;
2551.             feeltrap(trap);
2552.             pline("%s %s in a pile of soil below you.",
2553.                   already_seen ? "There is" : "You discover",
2554.                   trap->madeby_u ? "the trigger of your mine" : "a trigger");
2555.             if (already_seen && rn2(3))
2556.                 return Trap_Effect_Finished;
2557.             Soundeffect(se_kaablamm_of_mine, 80);
2558.             pline("KAABLAMM!!!  %s %s%s off!",
2559.                   forcebungle ? "Your inept attempt sets"
2560.                   : "The air currents set",
2561.                   already_seen ? a_your[trap->madeby_u] : "",
2562.                   already_seen ? " land mine" : "it");
2563.         } else {
2564.             /* prevent landmine from killing steed, throwing you to
2565.              * the ground, and then that same landmine affecting you
2566.              * again because it hasn't been deleted yet
2567.              */
2568.             static boolean recursive_mine = FALSE;
2569. 
2570.             if (recursive_mine)
2571.                 return Trap_Effect_Finished;
2572.             feeltrap(trap);
2573.             pline("KAABLAMM!!!  You triggered %s land mine!",
2574.                   a_your[trap->madeby_u]);
2575.             if (u.usteed)
2576.                 steed_mid = u.usteed->m_id;
2577.             recursive_mine = TRUE;
2578.             (void) steedintrap(trap, (struct obj *) 0);
2579.             recursive_mine = FALSE;
2580.             saddle = sobj_at(SADDLE, u.ux, u.uy);
2581.             set_wounded_legs(LEFT_SIDE, rn1(35, 41));
2582.             set_wounded_legs(RIGHT_SIDE, rn1(35, 41));
2583.             exercise(A_DEX, FALSE);
2584.         }
2585.         /* add a pit before calling losehp so bones won't keep the landmine;
2586.            blow_up_landmine() will remove pit afterwards if inappropriate */
2587.         trap->ttyp = PIT;
2588.         trap->madeby_u = FALSE;
2589.         losehp(Maybe_Half_Phys(damage), "land mine", KILLED_BY_AN);
2590.         blow_up_landmine(trap);
2591.         if (steed_mid && saddle && !u.usteed)
2592.             (void) keep_saddle_with_steedcorpse(steed_mid, fobj, saddle);
2593.         newsym(u.ux, u.uy); /* update trap symbol */
2594.         /* fall recursively into the pit... */
2595.         if ((trap = t_at(u.ux, u.uy)) != 0)
2596.             dotrap(trap, RECURSIVETRAP);
2597.         fill_pit(u.ux, u.uy);
2598.     } else {
2599.         boolean trapkilled = FALSE;
2600.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2601.         coordxy tx = trap->tx, ty = trap->ty;
2602. 
2603.         /* heavier monsters are more likely to set off a land mine; on the
2604.            other hand, any mon lighter than the trigger weight is immune */
2605. #define MINE_TRIGGER_WT (WT_ELF / 2U)
2606.         if (rn2(mtmp->data->cwt + 1) < (int) MINE_TRIGGER_WT)
2607.             return Trap_Effect_Finished;
2608.         if (m_in_air(mtmp)) {
2609.             boolean already_seen = trap->tseen;
2610. 
2611.             if (in_sight && !already_seen) {
2612.                 pline_mon(mtmp,
2613.                      "A trigger appears in a pile of soil below %s.",
2614.                       mon_nam(mtmp));
2615.                 seetrap(trap);
2616.             }
2617.             if (rn2(3))
2618.                 return Trap_Effect_Finished;
2619.             if (in_sight) {
2620.                 newsym(mtmp->mx, mtmp->my);
2621.                 pline_The("air currents set %s off!",
2622.                           already_seen ? "a land mine" : "it");
2623.             }
2624.         } else if (in_sight) {
2625.             newsym(mtmp->mx, mtmp->my);
2626.             pline_mon(mtmp,
2627.                   "%s%s triggers %s land mine!",
2628.                   !Deaf ? "KAABLAMM!!!  " : "", Monnam(mtmp),
2629.                   a_your[trap->madeby_u]);
2630.         }
2631.         if (!in_sight && !Deaf)
2632.             pline("Kaablamm!  %s an explosion in the distance!",
2633.                   "You hear");  /* Deaf-aware */
2634.         blow_up_landmine(trap);
2635.         /* explosion might have destroyed a drawbridge; don't
2636.            dish out more damage if monster is already dead */
2637.         if (DEADMONSTER(mtmp)
2638.             || thitm(0, mtmp, (struct obj *) 0, damage, FALSE)) {
2639.             trapkilled = TRUE;
2640.         } else {
2641.             /* monsters recursively fall into new pit */
2642.             if (mintrap(mtmp, trflags | FORCETRAP) == Trap_Killed_Mon)
2643.                 trapkilled = TRUE;
2644.         }
2645.         /* a boulder may fill the new pit, crushing monster */
2646.         fill_pit(tx, ty); /* thitm may have already destroyed the trap */
2647.         if (DEADMONSTER(mtmp))
2648.             trapkilled = TRUE;
2649.         if (unconscious()) {
2650.             gm.multi = -1;
2651.             gn.nomovemsg = "The explosion awakens you!";
2652.         }
2653.         return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
2654.             ? Trap_Caught_Mon : Trap_Effect_Finished;
2655.     }
2656.     return Trap_Effect_Finished;
2657. }
2658. #undef MINE_TRIGGER_WT
2659. 
2660. staticfn int
2661. trapeffect_rolling_boulder_trap(
2662.     struct monst *mtmp,
2663.     struct trap *trap,
2664.     unsigned int trflags UNUSED)
2665. {
2666.     if (mtmp == &gy.youmonst) {
2667.         int style = ROLL | (trap->tseen ? LAUNCH_KNOWN : 0);
2668. 
2669.         feeltrap(trap);
2670.         pline("%sYou trigger a rolling boulder trap!",
2671.               !Deaf ? "Click!  " : "");
2672.         if (!launch_obj(BOULDER, trap->launch.x, trap->launch.y,
2673.                         trap->launch2.x, trap->launch2.y, style)) {
2674.             /* if this is a known trap, the player may have known there wasn't
2675.                a lined up boulder, so use a shorter message to avoid --More--
2676.                spam */
2677.             if (style & LAUNCH_KNOWN)
2678.                 pline("No boulder was released.");
2679.             else
2680.                 pline("Fortunately for you, no boulder was released.");
2681.         }
2682.     } else {
2683.         if (!m_in_air(mtmp)) {
2684.             boolean in_sight = (mtmp == u.usteed
2685.                                 || (cansee(mtmp->mx, mtmp->my)
2686.                                     && canspotmon(mtmp)));
2687.             int style = ROLL | (in_sight ? 0 : LAUNCH_UNSEEN);
2688.             boolean trapkilled = FALSE;
2689. 
2690.             newsym(mtmp->mx, mtmp->my);
2691.             if (in_sight)
2692.                 pline_mon(mtmp, "%s%s triggers %s.",
2693.                       !Deaf ? "Click!  " : "", Monnam(mtmp),
2694.                       trap->tseen ? "a rolling boulder trap" : something);
2695.             if (launch_obj(BOULDER, trap->launch.x, trap->launch.y,
2696.                            trap->launch2.x, trap->launch2.y, style)) {
2697.                 if (in_sight)
2698.                     trap->tseen = TRUE;
2699.                 if (DEADMONSTER(mtmp))
2700.                     trapkilled = TRUE;
2701.             }
2702.             return trapkilled ? Trap_Killed_Mon : mtmp->mtrapped
2703.                 ? Trap_Caught_Mon : Trap_Effect_Finished;
2704.         }
2705.     }
2706.     return Trap_Effect_Finished;
2707. }
2708. 
2709. staticfn int
2710. trapeffect_magic_portal(
2711.     struct monst *mtmp,
2712.     struct trap *trap,
2713.     unsigned int trflags)
2714. {
2715.     if (mtmp == &gy.youmonst) {
2716.         feeltrap(trap);
2717.         domagicportal(trap);
2718.     } else {
2719.         return trapeffect_level_telep(mtmp, trap, trflags);
2720.     }
2721.     return Trap_Effect_Finished;
2722. }
2723. 
2724. staticfn int
2725. trapeffect_vibrating_square(
2726.     struct monst *mtmp,
2727.     struct trap *trap,
2728.     unsigned int trflags UNUSED)
2729. {
2730.     if (mtmp == &gy.youmonst) {
2731.         feeltrap(trap);
2732.         /* messages handled elsewhere; the trap symbol is merely to mark the
2733.            square for future reference */
2734.     } else {
2735.         boolean in_sight = canseemon(mtmp) || (mtmp == u.usteed);
2736.         boolean see_it = cansee(mtmp->mx, mtmp->my);
2737. 
2738.         if (see_it && !Blind) {
2739.             seetrap(trap); /* before messages */
2740.             if (in_sight) {
2741.                 char buf[BUFSZ], *p, *monnm = mon_nam(mtmp);
2742. 
2743.                 if (nolimbs(mtmp->data) || m_in_air(mtmp)) {
2744.                     /* just "beneath <mon>" */
2745.                     Strcpy(buf, monnm);
2746.                 } else {
2747.                     Strcpy(buf, s_suffix(monnm));
2748.                     p = eos(strcat(buf, " "));
2749.                     Strcpy(p, makeplural(mbodypart(mtmp, FOOT)));
2750.                     /* avoid "beneath 'rear paws'" or 'rear hooves' */
2751.                     (void) strsubst(p, "rear ", "");
2752.                 }
2753.                 You_see("a strange vibration beneath %s.", buf);
2754.             } else {
2755.                 /* notice something (hearing uses a larger threshold
2756.                    for 'nearby') */
2757.                 You_see("the ground vibrate %s.",
2758.                         (mdistu(mtmp) <= 2 * 2)
2759.                            ? "nearby" : "in the distance");
2760.             }
2761.         }
2762.     }
2763.     return Trap_Effect_Finished;
2764. }
2765. 
2766. /*
2767.  * for PR#259 - paranoid_confirm:trap
2768.  *
2769.  * Will a monster suffer any adverse effects from a certain trap?
2770.  * Note: does NOT mean "will a monster trigger a trap in the first place",
2771.  * though if it won't that does imply that they'll not suffer adverse effects.
2772.  * For example, an elf is considered immune to sleeping gas traps even though
2773.  * they'll set the trap off.
2774.  * Return value:
2775.  *  TRAP_NOT_IMMUNE = not immune at the moment;
2776.  *  TRAP_CLEARLY_IMMUNE = obviously immune (if player is polymorphed, assume
2777.  *    they know which traps they are immune to in their current form);
2778.  *  TRAP_HIDDEN_IMMUNE = immune but in non-obvious way such as an unidentified
2779.  *    item or hidden intrinsic providing a resistance; the player should still
2780.  *    be warned of this trap, while monsters implicitly know they're immune.
2781.  */
2782. int
2783. immune_to_trap(struct monst *mon, unsigned ttype)
2784. {
2785.     struct permonst *pm;
2786.     struct obj *obj;
2787.     boolean is_you;
2788. 
2789.     if (!mon) {
2790.         impossible("immune_to_trap: null monster");
2791.         return TRAP_NOT_IMMUNE;
2792.     }
2793.     pm = mon->data;
2794.     is_you = (mon == &gy.youmonst);
2795. 
2796.     switch (ttype) {
2797.     case ARROW_TRAP:
2798.     case DART_TRAP:
2799.     case ROCKTRAP:
2800.         /* can hit anything; even noncorporeal monsters might get a blessed
2801.            projectile */
2802.         return TRAP_NOT_IMMUNE;
2803.     case BEAR_TRAP:
2804.         if (pm->msize <= MZ_SMALL
2805.             || amorphous(pm) || is_whirly(pm) || unsolid(pm))
2806.             return TRAP_CLEARLY_IMMUNE;
2807.         FALLTHROUGH;
2808.         /*FALLTHRU*/
2809.     case SQKY_BOARD:
2810.     case LANDMINE:
2811.     case ROLLING_BOULDER_TRAP:
2812.     case HOLE:
2813.     case TRAPDOOR:
2814.     case PIT:
2815.     case SPIKED_PIT:
2816.         /* ground-based traps, which can be evaded by levitation, flying, or
2817.            hanging to the ceiling */
2818.         if (Sokoban && (is_pit(ttype) || is_hole(ttype)))
2819.             return TRAP_NOT_IMMUNE;
2820.         if (In_sokoban(&u.uz) && ttype == ROLLING_BOULDER_TRAP)
2821.             return TRAP_CLEARLY_IMMUNE; /* not dangerous in Sokoban */
2822.         if (is_floater(pm) || is_flyer(pm)
2823.             || (is_clinger(pm) && has_ceiling(&u.uz)))
2824.             return TRAP_CLEARLY_IMMUNE;
2825.         else if (is_you && (Levitation || Flying))
2826.             return TRAP_CLEARLY_IMMUNE;
2827.         return TRAP_NOT_IMMUNE;
2828.     case SLP_GAS_TRAP:
2829.         if (breathless(pm))
2830.             return TRAP_CLEARLY_IMMUNE;
2831.         else if (!is_you && resists_sleep(mon))
2832.             return TRAP_CLEARLY_IMMUNE;
2833.         else if (is_you && Sleep_resistance)
2834.             return TRAP_HIDDEN_IMMUNE;
2835.         return TRAP_NOT_IMMUNE;
2836.     case LEVEL_TELEP:
2837.     case TELEP_TRAP:
2838.         /* consider unintended teleporting to be an adverse effect; if in
2839.            the endgame or carrying the Amulet, the teleport trap won't work
2840.            anyway, so anything hitting it is immune. */
2841.         if (In_endgame(&u.uz) || mon_has_amulet(mon))
2842.             return TRAP_CLEARLY_IMMUNE;
2843.         return TRAP_NOT_IMMUNE;
2844.     case POLY_TRAP:
2845.         if (resists_magm(mon))
2846.             /* covers Antimagic for player */
2847.             return (is_you ? TRAP_HIDDEN_IMMUNE : TRAP_CLEARLY_IMMUNE);
2848.         return TRAP_NOT_IMMUNE;
2849.     case STATUE_TRAP:
2850.         /* no effect on monsters, only affects players; only trap detection
2851.            can let player know that this is a statue trap there ahead of time;
2852.            in the rare case this happens, do consider it an adverse effect */
2853.         if (!is_you)
2854.             return TRAP_CLEARLY_IMMUNE;
2855.         return TRAP_NOT_IMMUNE;
2856.     case WEB:
2857.         /* most of this code is lifted from mu_maybe_destroy_web */
2858.         if (webmaker(pm) || amorphous(pm) || is_whirly(pm) || flaming(pm)
2859.             || unsolid(pm) || pm == &mons[PM_GELATINOUS_CUBE])
2860.             return TRAP_CLEARLY_IMMUNE;
2861.         return TRAP_NOT_IMMUNE;
2862.     case ANTI_MAGIC:
2863.         /* doesn't hurt any non-magic-resistant monster with no magic */
2864.         if (is_you) {
2865.             if (Antimagic)
2866.                 return TRAP_NOT_IMMUNE;
2867.             else if (u.uenmax == 0)
2868.                 /* player won't lose HP and can't lose more Pw */
2869.                 return TRAP_HIDDEN_IMMUNE;
2870. 
2871.         /* following conditional lifted from mintrap ANTI_MAGIC logic */
2872.         } else if (!resists_magm(mon)
2873.                    && (mon->mcan || (!attacktype(pm, AT_MAGC)
2874.                                      && !attacktype(pm, AT_BREA)))) {
2875.             return TRAP_CLEARLY_IMMUNE;
2876.         }
2877.         return TRAP_NOT_IMMUNE;
2878.     case RUST_TRAP:
2879.         /* harmful if wearing anything rustable or if mon is an iron golem */
2880.         if (pm == &mons[PM_IRON_GOLEM])
2881.             return TRAP_NOT_IMMUNE;
2882. 
2883.         for (obj = is_you ? gi.invent : mon->minvent; obj; obj = obj->nobj) {
2884.             /* rust traps can currently hit only worn armor and weapons */
2885.             if (is_rustprone(obj) && obj->owornmask) {
2886.                 if (is_you && (obj == uquiver
2887.                                || (obj == uswapwep && !u.twoweap)))
2888.                     continue;
2889.                 return TRAP_NOT_IMMUNE;
2890.             }
2891.         }
2892.         return TRAP_CLEARLY_IMMUNE;
2893.     case MAGIC_TRAP:
2894.         /* for player, any number of bad effects;
2895.            for monsters, only replicates fire trap, so fall through */
2896.         if (is_you)
2897.             return TRAP_NOT_IMMUNE;
2898.         FALLTHROUGH;
2899.         /*FALLTHRU*/
2900.     case FIRE_TRAP: /* can always destroy items being carried */
2901.         /* harmful if not resistant or if carrying anything that could burn */
2902.         if (is_you ? !Fire_resistance : !resists_fire(mon))
2903.             return TRAP_NOT_IMMUNE;
2904. 
2905.         for (obj = is_you ? gi.invent : mon->minvent; obj; obj = obj->nobj) {
2906.             if (obj->oclass == SCROLL_CLASS || obj->oclass == POTION_CLASS
2907.                 || obj->oclass == SPBOOK_CLASS
2908.                 || (obj->owornmask && is_flammable(obj))) {
2909.                 if ((obj->otyp == SCR_FIRE || obj->otyp == SPE_FIREBALL)
2910.                     /* mon knows scroll of fire or spellbook of fireball
2911.                        won't be affected; hero knows iff this one has been
2912.                        seen and its type has been discovered */
2913.                     && (!is_you
2914.                         || (obj->dknown && objects[obj->otyp].oc_name_known)))
2915.                     continue;
2916.                 return TRAP_NOT_IMMUNE;
2917.             }
2918.         }
2919.         return (is_you ? TRAP_HIDDEN_IMMUNE : TRAP_CLEARLY_IMMUNE);
2920.     case MAGIC_PORTAL:
2921.         /* never hurts anything, but player is considered non-immune so they
2922.            can be asked about entering it */
2923.         if (!is_you)
2924.             return TRAP_CLEARLY_IMMUNE;
2925.         return TRAP_NOT_IMMUNE;
2926.     case VIBRATING_SQUARE:
2927.         /* no adverse effects */
2928.         return TRAP_CLEARLY_IMMUNE;
2929.     default:
2930.         impossible("immune_to_trap: bad ttype %u", ttype);
2931.         break;
2932.     }
2933.     return TRAP_NOT_IMMUNE;
2934. }
2935. 
2936. staticfn int
2937. trapeffect_selector(
2938.     struct monst *mtmp,
2939.     struct trap *trap,
2940.     unsigned int trflags)
2941. {
2942.     switch (trap->ttyp) {
2943.     case ARROW_TRAP:
2944.         return trapeffect_arrow_trap(mtmp, trap, trflags);
2945.     case DART_TRAP:
2946.         return trapeffect_dart_trap(mtmp, trap, trflags);
2947.     case ROCKTRAP:
2948.         return trapeffect_rocktrap(mtmp, trap, trflags);
2949.     case SQKY_BOARD:
2950.         return trapeffect_sqky_board(mtmp, trap, trflags);
2951.     case BEAR_TRAP:
2952.         return trapeffect_bear_trap(mtmp, trap, trflags);
2953.     case SLP_GAS_TRAP:
2954.         return trapeffect_slp_gas_trap(mtmp, trap, trflags);
2955.     case RUST_TRAP:
2956.         return trapeffect_rust_trap(mtmp, trap, trflags);
2957.     case FIRE_TRAP:
2958.         return trapeffect_fire_trap(mtmp, trap, trflags);
2959.     case PIT:
2960.     case SPIKED_PIT:
2961.         return trapeffect_pit(mtmp, trap, trflags);
2962.     case HOLE:
2963.     case TRAPDOOR:
2964.         return trapeffect_hole(mtmp, trap, trflags);
2965.     case LEVEL_TELEP:
2966.         return trapeffect_level_telep(mtmp, trap, trflags);
2967.     case MAGIC_PORTAL:
2968.         return trapeffect_magic_portal(mtmp, trap, trflags);
2969.     case TELEP_TRAP:
2970.         return trapeffect_telep_trap(mtmp, trap, trflags);
2971.     case WEB:
2972.         return trapeffect_web(mtmp, trap, trflags);
2973.     case STATUE_TRAP:
2974.         return trapeffect_statue_trap(mtmp, trap, trflags);
2975.     case MAGIC_TRAP:
2976.         return trapeffect_magic_trap(mtmp, trap, trflags);
2977.     case ANTI_MAGIC:
2978.         return trapeffect_anti_magic(mtmp, trap, trflags);
2979.     case LANDMINE:
2980.         return trapeffect_landmine(mtmp, trap, trflags);
2981.     case POLY_TRAP:
2982.         return trapeffect_poly_trap(mtmp, trap, trflags);
2983.     case ROLLING_BOULDER_TRAP:
2984.         return trapeffect_rolling_boulder_trap(mtmp, trap, trflags);
2985.     case VIBRATING_SQUARE:
2986.         return trapeffect_vibrating_square(mtmp, trap, trflags);
2987.     default:
2988.         impossible("%s encountered a strange trap of type %d.",
2989.                    (mtmp == &gy.youmonst) ? "You" : "Some monster",
2990.                    trap->ttyp);
2991.     }
2992.     return Trap_Effect_Finished;
2993. }
2994. 
2995. void
2996. dotrap(struct trap *trap, unsigned trflags)
2997. {
2998.     int ttype = trap->ttyp;
2999.     boolean already_seen = trap->tseen,
3000.             forcetrap = ((trflags & FORCETRAP) != 0
3001.                          || (trflags & FAILEDUNTRAP) != 0),
3002.             forcebungle = (trflags & FORCEBUNGLE) != 0,
3003.             plunged = (trflags & TOOKPLUNGE) != 0,
3004.             conj_pit = conjoined_pits(trap, t_at(u.ux0, u.uy0), TRUE),
3005.             adj_pit = adj_nonconjoined_pit(trap);
3006. 
3007.     nomul(0);
3008. 
3009.     if (fixed_tele_trap(trap)) {
3010.         trflags |= FORCETRAP;
3011.         forcetrap = TRUE;
3012.     }
3013. 
3014.     /* KMH -- You can't escape the Sokoban level traps */
3015.     if (Sokoban && (is_pit(ttype) || is_hole(ttype))) {
3016.         /* The "air currents" message is still appropriate -- even when
3017.          * the hero isn't flying or levitating -- because it conveys the
3018.          * reason why the player cannot escape the trap with a dexterity
3019.          * check, clinging to the ceiling, etc.
3020.          */
3021.         pline("Air currents pull you down into %s %s!",
3022.               a_your[trap->madeby_u],
3023.               trapname(ttype, TRUE)); /* do force "pit" while hallucinating */
3024.         /* then proceed to normal trap effect */
3025.     } else if (!forcetrap) {
3026.         if (floor_trigger(ttype) && check_in_air(&gy.youmonst, trflags)) {
3027.             if (already_seen) {
3028.                 You("%s over %s %s.", u_locomotion("step"),
3029.                     (ttype == ARROW_TRAP && !trap->madeby_u)
3030.                     ? "an" : a_your[trap->madeby_u],
3031.                     trapname(ttype, FALSE));
3032.             }
3033.             return;
3034.         }
3035.         if (already_seen && !Fumbling && !undestroyable_trap(ttype)
3036.             && ttype != ANTI_MAGIC && !forcebungle && !plunged
3037.             && !conj_pit && !adj_pit
3038.             && (!rn2(5) || (is_pit(ttype) && is_clinger(gy.youmonst.data)))) {
3039.                 You("escape %s %s.", (ttype == ARROW_TRAP && !trap->madeby_u)
3040.                                      ? "an"
3041.                                      : a_your[trap->madeby_u],
3042.                 trapname(ttype, FALSE));
3043.             return;
3044.         }
3045.     }
3046. 
3047.     if (u.usteed)
3048.         mon_learns_traps(u.usteed, ttype);
3049.     mons_see_trap(trap);
3050. 
3051.     /*
3052.      * Note:
3053.      *  Most references to trap types here don't use trapname() for
3054.      *  hallucination.  This could be considered to be a bug but doing
3055.      *  that would hide the actual trap situation from the player which
3056.      *  would be somewhat harsh for what's usually a minor impairment.
3057.      */
3058. 
3059.     (void) trapeffect_selector(&gy.youmonst, trap, trflags);
3060. }
3061. 
3062. staticfn char *
3063. trapnote(struct trap *trap, boolean noprefix)
3064. {
3065.     static const char *const tnnames[] = {
3066.         "C note",  "D flat", "D note",  "E flat",
3067.         "E note",  "F note", "F sharp", "G note",
3068.         "G sharp", "A note", "B flat",  "B note",
3069.     };
3070.     static char tnbuf[12]; /* result buffer */
3071.     const char *tn;
3072. 
3073.     tnbuf[0] = '\0';
3074.     tn = tnnames[trap->tnote];
3075.     if (!noprefix)
3076.         (void) just_an(tnbuf, tn);
3077.     return strcat(tnbuf, tn);
3078. }
3079. 
3080. /* choose a note not used by any trap on current level,
3081.    ignoring ttmp; if all are in use, pick a random one */
3082. staticfn int
3083. choose_trapnote(struct trap *ttmp)
3084. {
3085.     int tavail[12], tpick[12], tcnt = 0, k;
3086.     struct trap *t;
3087. 
3088.     for (k = 0; k < 12; ++k)
3089.         tavail[k] = tpick[k] = 0;
3090.     for (t = gf.ftrap; t; t = t->ntrap)
3091.         if (t->ttyp == SQKY_BOARD && t != ttmp)
3092.             tavail[t->tnote] = 1;
3093.     /* now populate tpick[] with the available indices */
3094.     for (k = 0; k < 12; ++k)
3095.         if (tavail[k] == 0)
3096.             tpick[tcnt++] = k;
3097.     /* choose an unused note; if all are in use, pick a random one */
3098.     return ((tcnt > 0) ? tpick[rn2(tcnt)] : rn2(12));
3099. }
3100. 
3101. staticfn int
3102. steedintrap(struct trap *trap, struct obj *otmp)
3103. {
3104.     struct monst *steed = u.usteed;
3105.     int tt;
3106.     boolean trapkilled, steedhit;
3107. 
3108.     if (!steed || !trap)
3109.         return Trap_Effect_Finished;
3110.     tt = trap->ttyp;
3111.     steed->mx = u.ux;
3112.     steed->my = u.uy;
3113.     trapkilled = steedhit = FALSE;
3114. 
3115.     switch (tt) {
3116.     case ARROW_TRAP:
3117.         if (!otmp) {
3118.             impossible("steed hit by non-existent arrow?");
3119.             return Trap_Effect_Finished;
3120.         }
3121.         trapkilled = thitm(8, steed, otmp, 0, FALSE);
3122.         steedhit = TRUE;
3123.         break;
3124.     case DART_TRAP:
3125.         if (!otmp) {
3126.             impossible("steed hit by non-existent dart?");
3127.             return Trap_Effect_Finished;
3128.         }
3129.         trapkilled = thitm(7, steed, otmp, 0, FALSE);
3130.         steedhit = TRUE;
3131.         break;
3132.     case SLP_GAS_TRAP:
3133.         if (!resists_sleep(steed) && !breathless(steed->data)
3134.             && !helpless(steed)) {
3135.             if (sleep_monst(steed, rnd(25), -1))
3136.                 /* no in_sight check here; you can feel it even if blind */
3137.                 pline("%s suddenly falls asleep!", Monnam(steed));
3138.         }
3139.         steedhit = TRUE;
3140.         break;
3141.     case LANDMINE:
3142.         trapkilled = thitm(0, steed, (struct obj *) 0, rnd(16), FALSE);
3143.         steedhit = TRUE;
3144.         break;
3145.     case PIT:
3146.     case SPIKED_PIT:
3147.         trapkilled = (DEADMONSTER(steed)
3148.                       || thitm(0, steed, (struct obj *) 0,
3149.                                rnd((tt == PIT) ? 6 : 10), FALSE));
3150.         steedhit = TRUE;
3151.         break;
3152.     case POLY_TRAP:
3153.         if (!resists_magm(steed) && !resist(steed, WAND_CLASS, 0, NOTELL)) {
3154.             /* newcham() will probably end up calling poly_steed() */
3155.             (void) newcham(steed, (struct permonst *) 0, NC_SHOW_MSG);
3156.         }
3157.         steedhit = TRUE;
3158.         break;
3159.     default:
3160.         break;
3161.     }
3162. 
3163.     if (trapkilled) {
3164.         dismount_steed(DISMOUNT_POLY);
3165.         return Trap_Killed_Mon;
3166.     }
3167.     return steedhit ? 1 : 0;
3168. }
3169. 
3170. /* some actions common to both player and monsters for triggered landmine */
3171. void
3172. blow_up_landmine(struct trap *trap)
3173. {
3174.     coordxy x = trap->tx, y = trap->ty, dbx, dby;
3175.     struct rm *lev = &levl[x][y];
3176.     schar old_typ, typ;
3177. 
3178.     old_typ = lev->typ;
3179.     (void) scatter(x, y, 4,
3180.                    MAY_DESTROY | MAY_HIT | MAY_FRACTURE | VIS_EFFECTS,
3181.                    (struct obj *) 0);
3182.     del_engr_at(x, y);
3183.     wake_nearto(x, y, 400);
3184.     if (IS_DOOR(lev->typ))
3185.         lev->doormask = D_BROKEN;
3186.     /* destroy drawbridge if present */
3187.     if (lev->typ == DRAWBRIDGE_DOWN || is_drawbridge_wall(x, y) >= 0) {
3188.         dbx = x, dby = y;
3189.         /* if under the portcullis, the bridge is adjacent */
3190.         if (find_drawbridge(&dbx, &dby))
3191.             destroy_drawbridge(dbx, dby);
3192.     }
3193.     trap = t_at(x, y); /* expected to be null after destruction */
3194.     /* or could be null if scatter blew up oil which melted ice */
3195.     /* convert landmine into pit */
3196.     if (trap) {
3197.         if (Is_waterlevel(&u.uz) || Is_airlevel(&u.uz)) {
3198.             /* no pits here */
3199.             deltrap(trap);
3200.         } else {
3201.             /* fill pit with water, if applicable */
3202.             typ = fillholetyp(x, y, FALSE);
3203.             if (typ != ROOM) {
3204.                 lev->typ = typ;
3205.                 liquid_flow(x, y, typ, trap,
3206.                             cansee(x, y) ? "The hole fills with %s!"
3207.                                          : (char *) 0);
3208.             } else {
3209.                 trap->ttyp = PIT;       /* explosion creates a pit */
3210.                 trap->madeby_u = FALSE; /* resulting pit isn't yours */
3211.                 seetrap(trap);          /* and it isn't concealed */
3212.             }
3213.         }
3214.     }
3215.     fill_pit(x, y);
3216.     maybe_dunk_boulders(x, y);
3217.     recalc_block_point(x, y);
3218.     spot_checks(x, y, old_typ);
3219. }
3220. 
3221. staticfn void
3222. launch_drop_spot(struct obj *obj, coordxy x, coordxy y)
3223. {
3224.     if (!obj) {
3225.         gl.launchplace.obj = (struct obj *) 0;
3226.         gl.launchplace.x = 0;
3227.         gl.launchplace.y = 0;
3228.     } else {
3229.         gl.launchplace.obj = obj;
3230.         gl.launchplace.x = x;
3231.         gl.launchplace.y = y;
3232.     }
3233. }
3234. 
3235. boolean
3236. launch_in_progress(void)
3237. {
3238.     if (gl.launchplace.obj)
3239.         return TRUE;
3240.     return FALSE;
3241. }
3242. 
3243. void
3244. force_launch_placement(void)
3245. {
3246.     if (gl.launchplace.obj) {
3247.         gl.launchplace.obj->otrapped = 0;
3248.         place_object(gl.launchplace.obj, gl.launchplace.x, gl.launchplace.y);
3249.     }
3250. }
3251. 
3252. /*
3253.  * Move obj from (x1,y1) to (x2,y2)
3254.  *
3255.  * Return 0 if no object was launched.
3256.  *        1 if an object was launched and placed somewhere.
3257.  *        2 if an object was launched, but used up.
3258.  */
3259. int
3260. launch_obj(
3261.     short otyp,
3262.     coordxy x1, coordxy y1,
3263.     coordxy x2, coordxy y2,
3264.     int style)
3265. {
3266.     struct monst *mtmp;
3267.     struct obj *otmp, *otmp2;
3268.     int dx, dy;
3269.     coordxy x, y;
3270.     struct obj *singleobj;
3271.     boolean used_up = FALSE, otherside = FALSE;
3272.     int dist, tmp, delaycnt = 0;
3273. 
3274.     otmp = sobj_at(otyp, x1, y1);
3275.     /* Try the other side too, for rolling boulder traps */
3276.     if (!otmp && otyp == BOULDER) {
3277.         otherside = TRUE;
3278.         otmp = sobj_at(otyp, x2, y2);
3279.     }
3280.     if (!otmp)
3281.         return 0;
3282.     if (otherside) { /* swap 'em */
3283.         int tx, ty;
3284. 
3285.         tx = x1;
3286.         ty = y1;
3287.         x1 = x2;
3288.         y1 = y2;
3289.         x2 = tx;
3290.         y2 = ty;
3291.     }
3292. 
3293.     if (otmp->quan == 1L) {
3294.         obj_extract_self(otmp);
3295.         maybe_unhide_at(otmp->ox, otmp->oy);
3296.         singleobj = otmp;
3297.         otmp = (struct obj *) 0;
3298.     } else {
3299.         singleobj = splitobj(otmp, 1L);
3300.         obj_extract_self(singleobj);
3301.     }
3302.     newsym(x1, y1);
3303.     /* in case you're using a pick-axe to chop the boulder that's being
3304.        launched (perhaps a monster triggered it), destroy context so that
3305.        the next dig attempt never thinks that you're resuming
3306.        the previous effort */
3307.     if ((otyp == BOULDER || otyp == STATUE)
3308.         && singleobj->ox == svc.context.digging.pos.x
3309.         && singleobj->oy == svc.context.digging.pos.y)
3310.         (void) memset((genericptr_t) &svc.context.digging, 0,
3311.                       sizeof(struct dig_info));
3312. 
3313.     dist = distmin(x1, y1, x2, y2);
3314.     x = gb.bhitpos.x = x1;
3315.     y = gb.bhitpos.y = y1;
3316.     dx = sgn(x2 - x1);
3317.     dy = sgn(y2 - y1);
3318.     switch (style) {
3319.     case ROLL | LAUNCH_UNSEEN:
3320.         if (otyp == BOULDER) {
3321.             if (cansee(x1, y1)) {
3322.                 You_see("%s start to roll.", an(xname(singleobj)));
3323.             } else if (Hallucination) {
3324.                 Soundeffect(se_someone_bowling, 60);
3325.                 You_hear("someone bowling.");
3326.             } else {
3327.                 Soundeffect(se_rumbling, 60);
3328.                 You_hear("rumbling %s.", (distu(x1, y1) <= 4 * 4) ? "nearby"
3329.                                            : "in the distance");
3330.             }
3331.         }
3332.         style &= ~LAUNCH_UNSEEN;
3333.         goto roll;
3334.     case ROLL | LAUNCH_KNOWN:
3335.         /* use otrapped as a flag to ohitmon */
3336.         singleobj->otrapped = 1;
3337.         style &= ~LAUNCH_KNOWN;
3338.         FALLTHROUGH;
3339.     /*FALLTHRU*/
3340.     case ROLL:
3341.  roll:
3342.         delaycnt = 2;
3343.         FALLTHROUGH;
3344.     /*FALLTHRU*/
3345.     default:
3346.         if (!delaycnt)
3347.             delaycnt = 1;
3348.         if (!cansee(x, y))
3349.             curs_on_u();
3350.         tmp_at(DISP_FLASH, obj_to_glyph(singleobj, rn2_on_display_rng));
3351.         tmp_at(x, y);
3352.     }
3353.     /* Mark a spot to place object in bones files to prevent
3354.      * loss of object. Use the starting spot to ensure that
3355.      * a rolling boulder will still launch, which it wouldn't
3356.      * do if left midstream. Unfortunately we can't use the
3357.      * target resting spot, because there are some things/situations
3358.      * that would prevent it from ever getting there (bars), and we
3359.      * can't tell that yet.
3360.      */
3361.     launch_drop_spot(singleobj, x, y);
3362. 
3363.     /* Set the object in motion */
3364.     while (dist-- > 0 && !used_up) {
3365.         struct trap *t;
3366. 
3367.         tmp_at(x, y);
3368.         tmp = delaycnt;
3369. 
3370.         /* dstage@u.washington.edu -- Delay only if hero sees it */
3371.         if (cansee(x, y))
3372.             while (tmp-- > 0)
3373.                 nh_delay_output();
3374. 
3375.         /*
3376.          * TEMPORARY?  github issue #1490 by BartekCupial reports a
3377.          * segfault when boulder rolls out of bounds.  That should be
3378.          * impossible because trap creation validates the path that
3379.          * the boulder will traverse.
3380.          *
3381.          * The suggested fix increments bhitpos, verifies with isok(),
3382.          * then undoes the increment if not ok.  This is simpler.
3383.          */
3384.         if (!isok(gb.bhitpos.x + dx, gb.bhitpos.y + dy)) {
3385.             x2 = x, y2 = y; /* use current spot for final boulder placement */
3386.             break;
3387.         }
3388.         /*
3389.          * end TEMPORARY?
3390.          */
3391. 
3392.         x = (gb.bhitpos.x += dx);
3393.         y = (gb.bhitpos.y += dy);
3394. 
3395.         if ((mtmp = m_at(x, y)) != 0) {
3396.             if (otyp == BOULDER && throws_rocks(mtmp->data)) {
3397.                 if (rn2(3)) {
3398.                     if (cansee(x, y))
3399.                         pline_mon(mtmp, "%s snatches the boulder.",
3400.                                   Monnam(mtmp));
3401.                     singleobj->otrapped = 0;
3402.                     (void) mpickobj(mtmp, singleobj);
3403.                     used_up = TRUE;
3404.                     launch_drop_spot((struct obj *) 0, 0, 0);
3405.                     break;
3406.                 }
3407.             }
3408.             if (ohitmon(mtmp, singleobj, (style == ROLL) ? -1 : dist,
3409.                         FALSE)) {
3410.                 used_up = TRUE;
3411.                 launch_drop_spot((struct obj *) 0, 0, 0);
3412.                 break;
3413.             }
3414.         } else if (u_at(x, y)) {
3415.             int dam = dmgval(singleobj, &gy.youmonst);
3416. 
3417.             if (gm.multi)
3418.                 nomul(0);
3419.             if (thitu(9 + singleobj->spe, Maybe_Half_Phys(dam),
3420.                       &singleobj, (char *) 0))
3421.                 stop_occupation();
3422.         }
3423.         if (style == ROLL) {
3424.             if (down_gate(x, y) != -1) {
3425.                 if (ship_object(singleobj, x, y, FALSE)) {
3426.                     used_up = TRUE;
3427.                     launch_drop_spot((struct obj *) 0, 0, 0);
3428.                     break;
3429.                 }
3430.             }
3431.             if ((t = t_at(x, y)) != 0
3432.                 && otyp == BOULDER) {
3433.                 int newlev = 0;
3434.                 d_level dest;
3435. 
3436.                 switch (t->ttyp) {
3437.                 case LANDMINE:
3438.                     if (rn2(10) > 2) {
3439.                         if (cansee(x, y))
3440.                             set_msg_xy(x, y);
3441.                         pline("KAABLAMM!!!%s",
3442.                               cansee(x, y)
3443.                                ? "  The rolling boulder triggers a land mine."
3444.                                : "");
3445.                         deltrap(t);
3446.                         del_engr_at(x, y);
3447.                         place_object(singleobj, x, y);
3448.                         singleobj->otrapped = 0;
3449.                         fracture_rock(singleobj);
3450.                         (void) scatter(x, y, 4,
3451.                                        MAY_DESTROY | MAY_HIT | MAY_FRACTURE
3452.                                            | VIS_EFFECTS,
3453.                                        (struct obj *) 0);
3454.                         if (cansee(x, y))
3455.                             newsym(x, y);
3456.                         used_up = TRUE;
3457.                         launch_drop_spot((struct obj *) 0, 0, 0);
3458.                     }
3459.                     break;
3460.                 case LEVEL_TELEP:
3461.                     /* 20% chance of picking current level; 100% chance for
3462.                        that if in single-level branch (Knox) or in endgame */
3463.                     newlev = random_teleport_level();
3464.                     /* if trap doesn't work, skip "disappears" message */
3465.                     if (newlev == depth(&u.uz))
3466.                         break;
3467.                     FALLTHROUGH;
3468.                     /*FALLTHRU*/
3469.                 case TELEP_TRAP:
3470.                     if (cansee(x, y))
3471.                         pline_xy(x, y,
3472.                                  "Suddenly the rolling boulder disappears!");
3473.                     else if (!Deaf)
3474.                         You_hear("a rumbling stop abruptly.");
3475.                     singleobj->otrapped = 0;
3476.                     if (t->ttyp == TELEP_TRAP) {
3477.                         (void) rloco(singleobj);
3478.                     } else {
3479.                         add_to_migration(singleobj);
3480.                         get_level(&dest, newlev);
3481.                         singleobj->ox = dest.dnum;
3482.                         singleobj->oy = dest.dlevel;
3483.                         singleobj->owornmask = (long) MIGR_RANDOM;
3484.                     }
3485.                     seetrap(t);
3486.                     used_up = TRUE;
3487.                     launch_drop_spot((struct obj *) 0, 0, 0);
3488.                     break;
3489.                 case PIT:
3490.                 case SPIKED_PIT:
3491.                 case HOLE:
3492.                 case TRAPDOOR:
3493.                     /* the boulder won't be used up if there is a
3494.                        monster in the trap; stop rolling anyway */
3495.                     x2 = x, y2 = y; /* stops here */
3496.                     if (flooreffects(singleobj, x2, y2, "fall")) {
3497.                         used_up = TRUE;
3498.                         launch_drop_spot((struct obj *) 0, 0, 0);
3499.                     }
3500.                     dist = -1; /* stop rolling immediately */
3501.                     break;
3502.                 default:
3503.                     break;
3504.                 }
3505. 
3506.                 if (used_up || dist == -1)
3507.                     break; /* from 'while' loop */
3508.             }
3509.             if (flooreffects(singleobj, x, y, "fall")) {
3510.                 used_up = TRUE;
3511.                 launch_drop_spot((struct obj *) 0, 0, 0);
3512.                 break;
3513.             }
3514.             if (otyp == BOULDER && (otmp2 = sobj_at(BOULDER, x, y)) != 0) {
3515.                 const char *bmsg = " as one boulder sets another in motion";
3516.                 coordxy fx = x + dx, fy = y + dy;
3517. 
3518.                 if (!isok(fx, fy) || !dist || IS_OBSTRUCTED(levl[fx][fy].typ))
3519.                     bmsg = " as one boulder hits another";
3520. 
3521.                 Soundeffect(se_loud_crash, 80);
3522.                 You_hear("a loud crash%s!", cansee(x, y) ? bmsg : "");
3523.                 obj_extract_self(otmp2);
3524.                 /* pass off the otrapped flag to the next boulder */
3525.                 otmp2->otrapped = singleobj->otrapped;
3526.                 singleobj->otrapped = 0;
3527.                 place_object(singleobj, x, y);
3528.                 singleobj = otmp2;
3529.                 otmp2 = (struct obj *) 0;
3530.                 wake_nearto(x, y, 10 * 10);
3531.             }
3532.         }
3533.         if (otyp == BOULDER && closed_door(x, y)) {
3534.             if (cansee(x, y)) {
3535.                 set_msg_xy(x, y);
3536.                 pline_The("boulder crashes through a door.");
3537.             }
3538.             levl[x][y].doormask = D_BROKEN;
3539.             if (dist)
3540.                 recalc_block_point(x, y);
3541.         }
3542. 
3543.         /* if about to hit something, do so now */
3544.         if (dist > 0 && isok(x + dx, y + dy)) {
3545.             coordxy fx = x + dx, fy = y + dy;
3546.             uchar typ = levl[fx][fy].typ;
3547. 
3548.             if (typ == IRONBARS) {
3549.                 x2 = x, y2 = y; /* object stops here */
3550.                 if (hits_bars(&singleobj, x2, y2, fx, fy, !rn2(20), 0)) {
3551.                     if (!singleobj) {
3552.                         used_up = TRUE;
3553.                         launch_drop_spot((struct obj *) 0, 0, 0);
3554.                     }
3555.                     break;
3556.                 }
3557.             } else if (IS_STWALL(typ) || IS_TREE(typ)) {
3558.                 x2 = x, y2 = y; /* object stops here */
3559.                 if (!Deaf)
3560.                     pline("Thump!");
3561.                 wake_nearto(x2, y2, 16);
3562.                 break;
3563.             }
3564.         }
3565.     } /* while dist > 0 */
3566.     tmp_at(DISP_END, 0);
3567.     launch_drop_spot((struct obj *) 0, 0, 0);
3568.     if (!used_up) {
3569.         singleobj->otrapped = 0;
3570.         place_object(singleobj, x2, y2);
3571.         newsym(x2, y2);
3572.         return 1;
3573.     }
3574.     return 2;
3575. }
3576. 
3577. void
3578. seetrap(struct trap *trap)
3579. {
3580.     if (!trap->tseen) {
3581.         trap->tseen = 1;
3582.         newsym(trap->tx, trap->ty);
3583.     }
3584. }
3585. 
3586. /* like seetrap() but overrides vision */
3587. void
3588. feeltrap(struct trap *trap)
3589. {
3590.     trap->tseen = 1;
3591.     map_trap(trap, 1);
3592.     /* in case it's beneath something, redisplay the something */
3593.     newsym(trap->tx, trap->ty);
3594. }
3595. 
3596. /* try to find a random coordinate where launching a rolling boulder
3597.    could work. return TRUE if found, with coordinate in cc. */
3598. staticfn boolean
3599. find_random_launch_coord(struct trap *ttmp, coord *cc)
3600. {
3601.     int tmp;
3602.     boolean success = FALSE;
3603.     coord bcc = UNDEFINED_VALUES;
3604.     int distance;
3605.     int mindist = 4;
3606.     int trycount = 0;
3607.     coordxy dx, dy;
3608.     coordxy x, y;
3609. 
3610.     if (!ttmp || !cc || Sokoban)
3611.         return FALSE;
3612. 
3613.     x = ttmp->tx;
3614.     y = ttmp->ty;
3615. 
3616.     bcc.x = ttmp->tx + gl.launchplace.x;
3617.     bcc.y = ttmp->ty + gl.launchplace.y;
3618.     if (isok(bcc.x, bcc.y) && linedup(ttmp->tx, ttmp->ty, bcc.x, bcc.y, 1)) {
3619.         cc->x = bcc.x;
3620.         cc->y = bcc.y;
3621.         return TRUE;
3622.     }
3623. 
3624.     if (ttmp->ttyp == ROLLING_BOULDER_TRAP)
3625.         mindist = 2;
3626.     distance = rn1(5, 4); /* 4..8 away */
3627.     tmp = rn2(N_DIRS); /* randomly pick a direction to try first */
3628.     while (distance >= mindist) {
3629.         dx = xdir[tmp];
3630.         dy = ydir[tmp];
3631.         cc->x = x;
3632.         cc->y = y;
3633.         /* Prevent boulder from being placed on water */
3634.         if (ttmp->ttyp == ROLLING_BOULDER_TRAP
3635.             && is_pool_or_lava(x + distance * dx, y + distance * dy))
3636.             success = FALSE;
3637.         else
3638.             success = isclearpath(cc, distance, dx, dy);
3639.         if (ttmp->ttyp == ROLLING_BOULDER_TRAP) {
3640.             boolean success_otherway;
3641. 
3642.             bcc.x = x;
3643.             bcc.y = y;
3644.             success_otherway = isclearpath(&bcc, distance, -(dx), -(dy));
3645.             if (!success_otherway)
3646.                 success = FALSE;
3647.         }
3648.         if (success)
3649.             break;
3650.         if (++tmp > 7)
3651.             tmp = 0;
3652.         if ((++trycount % 8) == 0)
3653.             --distance;
3654.     }
3655.     return success;
3656. }
3657. 
3658. staticfn int
3659. mkroll_launch(
3660.     struct trap *ttmp,
3661.     coordxy x,
3662.     coordxy y,
3663.     short otyp,
3664.     long ocount)
3665. {
3666.     struct obj *otmp;
3667.     coord cc = UNDEFINED_VALUES;
3668.     boolean success = FALSE;
3669. 
3670.     success = find_random_launch_coord(ttmp, &cc);
3671. 
3672.     if (!success) {
3673.         /* create the trap without any ammo, launch pt at trap location */
3674.         cc.x = x;
3675.         cc.y = y;
3676.     } else {
3677.         otmp = mksobj(otyp, TRUE, FALSE);
3678.         otmp->quan = ocount;
3679.         otmp->owt = weight(otmp);
3680.         place_object(otmp, cc.x, cc.y);
3681.         stackobj(otmp);
3682.     }
3683.     ttmp->launch.x = cc.x;
3684.     ttmp->launch.y = cc.y;
3685.     if (ttmp->ttyp == ROLLING_BOULDER_TRAP) {
3686.         ttmp->launch2.x = x - (cc.x - x);
3687.         ttmp->launch2.y = y - (cc.y - y);
3688.     } else
3689.         ttmp->launch_otyp = otyp;
3690.     newsym(ttmp->launch.x, ttmp->launch.y);
3691.     return 1;
3692. }
3693. 
3694. staticfn boolean
3695. isclearpath(
3696.     coord *cc,
3697.     int distance,
3698.     schar dx,
3699.     schar dy)
3700. {
3701.     struct trap *t;
3702.     uchar typ;
3703.     coordxy x, y;
3704. 
3705.     x = cc->x;
3706.     y = cc->y;
3707.     while (distance-- > 0) {
3708.         x += dx;
3709.         y += dy;
3710.         if (!isok(x, y))
3711.             return FALSE;
3712.         typ = levl[x][y].typ;
3713.         if (!ZAP_POS(typ) || closed_door(x, y))
3714.             return FALSE;
3715.         if ((t = t_at(x, y)) != 0
3716.             && (is_pit(t->ttyp) || is_hole(t->ttyp) || is_xport(t->ttyp)))
3717.             return FALSE;
3718.     }
3719.     cc->x = x;
3720.     cc->y = y;
3721.     return TRUE;
3722. }
3723. 
3724. /* can monster escape from a pit easily */
3725. staticfn boolean
3726. m_easy_escape_pit(struct monst *mtmp)
3727. {
3728.     return (mtmp->data == &mons[PM_PIT_FIEND]
3729.             || mtmp->data->msize >= MZ_HUGE);
3730. }
3731. 
3732. int
3733. mintrap(struct monst *mtmp, unsigned mintrapflags)
3734. {
3735.     struct trap *trap = t_at(mtmp->mx, mtmp->my);
3736.     struct permonst *mptr = mtmp->data;
3737.     int trap_result = Trap_Effect_Finished;
3738. 
3739.     if (!trap) {
3740.         mtmp->mtrapped = 0;      /* perhaps teleported? */
3741.     } else if (mtmp->mtrapped) { /* is currently in the trap */
3742.         if (!trap->tseen && cansee(mtmp->mx, mtmp->my) && canseemon(mtmp)
3743.             && (is_pit(trap->ttyp) || trap->ttyp == BEAR_TRAP
3744.                 || trap->ttyp == HOLE
3745.                 || trap->ttyp == WEB)) {
3746.             /* If you come upon an obviously trapped monster, then
3747.                you must be able to see the trap it's in too. */
3748.             seetrap(trap);
3749.         }
3750. 
3751.         if (!rn2(40) || (is_pit(trap->ttyp) && m_easy_escape_pit(mtmp))) {
3752.             if (sobj_at(BOULDER, mtmp->mx, mtmp->my)
3753.                 && is_pit(trap->ttyp)) {
3754.                 if (!rn2(2)) {
3755.                     mtmp->mtrapped = 0;
3756.                     if (canseemon(mtmp))
3757.                         pline_mon(mtmp, "%s pulls free...",
3758.                                  Monnam(mtmp));
3759.                     fill_pit(mtmp->mx, mtmp->my);
3760.                 }
3761.             } else {
3762.                 if (canseemon(mtmp)) {
3763.                     set_msg_xy(mtmp->mx, mtmp->my);
3764.                     if (is_pit(trap->ttyp))
3765.                         pline("%s climbs %sout of the pit.", Monnam(mtmp),
3766.                               m_easy_escape_pit(mtmp) ? "easily " : "");
3767.                     else if (trap->ttyp == BEAR_TRAP || trap->ttyp == WEB)
3768.                         pline("%s pulls free of the %s.", Monnam(mtmp),
3769.                               trapname(trap->ttyp, FALSE));
3770.                 }
3771.                 mtmp->mtrapped = 0;
3772.             }
3773.         } else if (metallivorous(mptr)) {
3774.             if (trap->ttyp == BEAR_TRAP) {
3775.                 if (canseemon(mtmp))
3776.                     pline_mon(mtmp, "%s eats a bear trap!",
3777.                               Monnam(mtmp));
3778.                 deltrap(trap);
3779.                 mtmp->meating = 5;
3780.                 mtmp->mtrapped = 0;
3781.             } else if (trap->ttyp == SPIKED_PIT) {
3782.                 if (canseemon(mtmp))
3783.                     pline_mon(mtmp, "%s munches on some spikes!",
3784.                               Monnam(mtmp));
3785.                 trap->ttyp = PIT;
3786.                 mtmp->meating = 5;
3787.             }
3788.         }
3789.         trap_result = mtmp->mtrapped ? Trap_Caught_Mon : Trap_Effect_Finished;
3790.     } else {
3791.         int tt = trap->ttyp;
3792.         boolean forcetrap = ((mintrapflags & FORCETRAP) != 0);
3793.         boolean forcebungle = (mintrapflags & FORCEBUNGLE) != 0;
3794.         /* monster has seen such a trap before */
3795.         boolean already_seen = (mon_knows_traps(mtmp, tt)
3796.                                 || (tt == HOLE && !mindless(mptr)));
3797. 
3798.         if (fixed_tele_trap(trap)) {
3799.             mintrapflags |= FORCETRAP;
3800.             forcetrap = TRUE;
3801.         }
3802. 
3803.         if (mtmp == u.usteed) {
3804.             ; /* true when called from dotrap, inescapable is not an option */
3805.         } else if (Sokoban && (is_pit(tt) || is_hole(tt))
3806.                    && !trap->madeby_u) {
3807.             ; /* nothing here, the trap effects will handle messaging */
3808.         } else if (!forcetrap) {
3809.             if (floor_trigger(tt) && check_in_air(mtmp, mintrapflags)) {
3810.                 return Trap_Effect_Finished;
3811.             }
3812.             if (already_seen && rn2(4) && !forcebungle)
3813.                 return Trap_Effect_Finished;
3814.         }
3815. 
3816.         mon_learns_traps(mtmp, tt);
3817.         mons_see_trap(trap);
3818. 
3819.         /* Monster is aggravated by being trapped by you.
3820.            Recognizing who made the trap isn't completely
3821.            unreasonable; everybody has their own style. */
3822.         if (trap->madeby_u && rnl(5))
3823.             setmangry(mtmp, FALSE);
3824. 
3825.         trap_result = trapeffect_selector(mtmp, trap, mintrapflags);
3826. 
3827.         /* mtmp can't stay hiding under an object if trapped in non-pit
3828.            (mtmp hiding under object at armed bear trap location, hero
3829.            zaps wand of locking or spell of wizard lock at spot triggering
3830.            the trap and trapping mtmp there) */
3831.         if (!DEADMONSTER(mtmp) && mtmp->mtrapped) {
3832.             boolean alreadyspotted = canspotmon(mtmp);
3833. 
3834.             maybe_unhide_at(mtmp->mx, mtmp->my);
3835.             if (!alreadyspotted && canseemon(mtmp))
3836.                 pline_mon(mtmp, "%s appears.", Amonnam(mtmp));
3837.         }
3838.     }
3839.     return trap_result;
3840. }
3841. 
3842. /* Combine cockatrice checks into single functions to avoid repeating code. */
3843. void
3844. instapetrify(const char *str)
3845. {
3846.     if (Stone_resistance)
3847.         return;
3848.     if (poly_when_stoned(gy.youmonst.data) && polymon(PM_STONE_GOLEM))
3849.         return;
3850.     urgent_pline("You turn to stone...");
3851.     svk.killer.format = KILLED_BY;
3852.     if (str != svk.killer.name)
3853.         Strcpy(svk.killer.name, str ? str : "");
3854.     done(STONING);
3855. }
3856. 
3857. void
3858. minstapetrify(struct monst *mon, boolean byplayer)
3859. {
3860.     if (resists_ston(mon))
3861.         return;
3862.     if (poly_when_stoned(mon->data)) {
3863.         mon_to_stone(mon);
3864.         return;
3865.     }
3866.     if (!vamp_stone(mon))
3867.         return;
3868. 
3869.     /* give a "<mon> is slowing down" message and also remove
3870.        intrinsic speed (comparable to similar effect on the hero) */
3871.     mon_adjust_speed(mon, -3, (struct obj *) 0);
3872. 
3873.     if (cansee(mon->mx, mon->my))
3874.         pline_mon(mon, "%s turns to stone.", Monnam(mon));
3875.     if (byplayer) {
3876.         gs.stoned = TRUE;
3877.         xkilled(mon, XKILL_NOMSG);
3878.     } else
3879.         monstone(mon);
3880. }
3881. 
3882. void
3883. selftouch(const char *arg)
3884. {
3885.     char kbuf[BUFSZ];
3886.     const char *corpse_pmname;
3887. 
3888.     if (uwep && uwep->otyp == CORPSE && touch_petrifies(&mons[uwep->corpsenm])
3889.         && !Stone_resistance) {
3890.         corpse_pmname = obj_pmname(uwep);
3891.         pline("%s touch the %s corpse.", arg, corpse_pmname);
3892.         Sprintf(kbuf, "%s corpse", an(corpse_pmname));
3893.         instapetrify(kbuf);
3894.         /* life-saved; unwield the corpse if we can't handle it */
3895.         if (!uarmg && !Stone_resistance)
3896.             uwepgone();
3897.     }
3898.     /* Or your secondary weapon, if wielded [hypothetical; we don't
3899.        allow two-weapon combat when either weapon is a corpse] */
3900.     if (u.twoweap && uswapwep && uswapwep->otyp == CORPSE
3901.         && touch_petrifies(&mons[uswapwep->corpsenm]) && !Stone_resistance) {
3902.         corpse_pmname = obj_pmname(uswapwep);
3903.         pline("%s touch the %s corpse.", arg, corpse_pmname);
3904.         Sprintf(kbuf, "%s corpse", an(corpse_pmname));
3905.         instapetrify(kbuf);
3906.         /* life-saved; unwield the corpse */
3907.         if (!uarmg && !Stone_resistance)
3908.             uswapwepgone();
3909.     }
3910. }
3911. 
3912. void
3913. mselftouch(
3914.     struct monst *mon,
3915.     const char *arg,
3916.     boolean byplayer)
3917. {
3918.     struct obj *mwep = MON_WEP(mon);
3919. 
3920.     if (mwep && mwep->otyp == CORPSE && touch_petrifies(&mons[mwep->corpsenm])
3921.         && !resists_ston(mon)) {
3922.         if (cansee(mon->mx, mon->my)) {
3923.             pline_mon(mon, "%s%s touches %s.", arg ? arg : "",
3924.                   arg ? mon_nam(mon) : Monnam(mon),
3925.                   corpse_xname(mwep, (const char *) 0, CXN_PFX_THE));
3926.         }
3927.         minstapetrify(mon, byplayer);
3928.         /* if life-saved, might not be able to continue wielding */
3929.         if (!DEADMONSTER(mon)
3930.             && !which_armor(mon, W_ARMG) && !resists_ston(mon))
3931.             mwepgone(mon);
3932.     }
3933. }
3934. 
3935. /* start levitating */
3936. void
3937. float_up(void)
3938. {
3939.     disp.botl = TRUE;
3940.     if (u.utrap) {
3941.         if (u.utraptype == TT_PIT) {
3942.             reset_utrap(FALSE);
3943.             You("float up, out of the %s!", trapname(PIT, FALSE));
3944.             gv.vision_full_recalc = 1; /* vision limits change */
3945.             fill_pit(u.ux, u.uy);
3946.         } else if (u.utraptype == TT_LAVA /* molten lava */
3947.                    || u.utraptype == TT_INFLOOR) { /* solidified lava */
3948.             Your("body pulls upward, but your %s are still stuck.",
3949.                  makeplural(body_part(LEG)));
3950.         } else if (u.utraptype == TT_BURIEDBALL) { /* tethered */
3951.             coord cc;
3952. 
3953.             cc.x = u.ux, cc.y = u.uy;
3954.             /* caveat: this finds the first buried iron ball within
3955.                one step of the specified location, not necessarily the
3956.                buried [former] uball at the original anchor point */
3957.             (void) buried_ball(&cc);
3958.             /* being chained to the floor blocks levitation from floating
3959.                above that floor but not from enhancing carrying capacity */
3960.             You("feel lighter, but your %s is still chained to the %s.",
3961.                 body_part(LEG),
3962.                 IS_ROOM(levl[cc.x][cc.y].typ) ? "floor" : "ground");
3963.         } else if (u.utraptype == WEB) {
3964.             You("float up slightly, but you are still stuck in the %s.",
3965.                 trapname(WEB, FALSE));
3966.         } else { /* bear trap */
3967.             You("float up slightly, but your %s is still stuck.",
3968.                 body_part(LEG));
3969.         }
3970.         /* when still trapped, float_vs_flight() below will block levitation */
3971. #if 0
3972.     } else if (Is_waterlevel(&u.uz)) {
3973.         pline("It feels as though you've lost some weight.");
3974. #endif
3975.     } else if (u.uinwater) {
3976.         spoteffects(TRUE);
3977.     } else if (u.uswallow) {
3978.         /* FIXME: this isn't correct for trapper/lurker above */
3979.         if (is_animal(u.ustuck->data))
3980.             You("float away from the %s.", surface(u.ux, u.uy));
3981.         else
3982.             You("spiral up into %s.", mon_nam(u.ustuck));
3983.     } else if (Hallucination) {
3984.         pline("Up, up, and awaaaay!  You're walking on air!");
3985.     } else if (Is_airlevel(&u.uz)) {
3986.         You("gain control over your movements.");
3987.     } else {
3988.         You("start to float in the air!");
3989.     }
3990.     if (u.usteed && !is_floater(u.usteed->data)
3991.         && !is_flyer(u.usteed->data)) {
3992.         if (Lev_at_will) {
3993.             pline("%s magically floats up!", Monnam(u.usteed));
3994.         } else {
3995.             You("cannot stay on %s.", mon_nam(u.usteed));
3996.             dismount_steed(DISMOUNT_GENERIC);
3997.         }
3998.     }
3999.     if (Flying)
4000.         You("are no longer able to control your flight.");
4001.     float_vs_flight(); /* set BFlying, also BLevitation if still trapped */
4002.     /* levitation gives maximum carrying capacity, so encumbrance
4003.        state might be reduced */
4004.     encumber_msg();
4005.     return;
4006. }
4007. 
4008. /* a boulder fills a pit or a hole at x,y */
4009. void
4010. fill_pit(coordxy x, coordxy y)
4011. {
4012.     struct obj *otmp;
4013.     struct trap *t;
4014. 
4015.     if ((t = t_at(x, y)) != 0 && (is_pit(t->ttyp) || is_hole(t->ttyp))
4016.         && (otmp = sobj_at(BOULDER, x, y)) != 0) {
4017.         obj_extract_self(otmp);
4018.         (void) flooreffects(otmp, x, y, "settle");
4019.     }
4020. }
4021. 
4022. /* stop levitating */
4023. int
4024. float_down(
4025.     long hmask,
4026.     long emask) /* might cancel timeout */
4027. {
4028.     struct trap *trap = (struct trap *) 0;
4029.     d_level current_dungeon_level;
4030.     boolean no_msg = FALSE;
4031. 
4032.     HLevitation &= ~hmask;
4033.     ELevitation &= ~emask;
4034.     if (Levitation)
4035.         return 0; /* maybe another ring/potion/boots */
4036.     if (BLevitation) {
4037.         /* if blocked by terrain, we haven't actually been levitating so
4038.            we don't give any end-of-levitation feedback or side-effects,
4039.            but if blocking is solely due to being trapped in/on floor,
4040.            do give some feedback but skip other float_down() effects */
4041.         boolean trapped = (BLevitation == I_SPECIAL);
4042. 
4043.         float_vs_flight();
4044.         if (trapped && u.utrap) /* u.utrap => paranoia */
4045.             You("are no longer trying to float up from the %s.",
4046.                 (u.utraptype == TT_BEARTRAP) ? "trap's jaws"
4047.                   : (u.utraptype == TT_WEB) ? "web"
4048.                       : (u.utraptype == TT_BURIEDBALL) ? "chain"
4049.                           : (u.utraptype == TT_LAVA) ? "lava"
4050.                               : "ground"); /* TT_INFLOOR */
4051.         encumber_msg(); /* carrying capacity might have changed */
4052.         return 0;
4053.     }
4054.     disp.botl = TRUE;
4055.     nomul(0); /* stop running or resting */
4056.     if (BFlying) {
4057.         /* controlled flight no longer overridden by levitation */
4058.         float_vs_flight(); /* clears BFlying & I_SPECIAL
4059.                             * unless hero is stuck in floor */
4060.         if (Flying) {
4061.             You("have stopped levitating and are now flying.");
4062.             encumber_msg(); /* carrying capacity might have changed */
4063.             return 1;
4064.         }
4065.     }
4066.     if (u.uswallow) {
4067.         You("float down, but you are still %s.",
4068.             digests(u.ustuck->data) ? "swallowed" : "engulfed");
4069.         encumber_msg();
4070.         return 1;
4071.     }
4072. 
4073.     if (Punished && !carried(uball) && !m_at(uball->ox, uball->oy)
4074.         && (is_pool(uball->ox, uball->oy)
4075.             || ((trap = t_at(uball->ox, uball->oy))
4076.                 && (is_pit(trap->ttyp) || is_hole(trap->ttyp))))) {
4077.         u.ux0 = u.ux;
4078.         u.uy0 = u.uy;
4079.         u.ux = uball->ox;
4080.         u.uy = uball->oy;
4081.         movobj(uchain, uball->ox, uball->oy);
4082.         newsym(u.ux0, u.uy0);
4083.         gv.vision_full_recalc = 1; /* in case the hero moved. */
4084.     }
4085.     /* check for falling into pool - added by GAN 10/20/86 */
4086.     if (!Flying) {
4087.         if (!u.uswallow && u.ustuck) {
4088.             if (sticks(gy.youmonst.data))
4089.                 You("aren't able to maintain your hold on %s.",
4090.                     mon_nam(u.ustuck));
4091.             else
4092.                 pline("Startled, %s can no longer hold you!",
4093.                       mon_nam(u.ustuck));
4094.             set_ustuck((struct monst *) 0);
4095.         }
4096.         /* kludge alert:
4097.          * drown() and lava_effects() print various messages almost
4098.          * every time they're called which conflict with the "fall
4099.          * into" message below.  Thus, we want to avoid printing
4100.          * confusing, duplicate or out-of-order messages.
4101.          * Use knowledge of the two routines as a hack -- this
4102.          * should really be handled differently -dlc
4103.          */
4104.         if (is_pool(u.ux, u.uy) && !Wwalking && !Swimming && !u.uinwater)
4105.             no_msg = drown();
4106. 
4107.         if (is_lava(u.ux, u.uy) && !iflags.in_lava_effects) {
4108.             (void) lava_effects();
4109.             no_msg = TRUE;
4110.         }
4111.     }
4112.     if (!trap) {
4113.         trap = t_at(u.ux, u.uy);
4114.         if (Is_airlevel(&u.uz)) {
4115.             You("begin to tumble in place.");
4116.         } else if (Is_waterlevel(&u.uz) && !no_msg) {
4117.             You_feel("heavier.");
4118.         /* u.uinwater msgs already in spoteffects()/drown() */
4119.         } else if (!u.uinwater && !no_msg) {
4120.             if (!(emask & W_SADDLE)) {
4121.                 if (Sokoban && trap) {
4122.                     /* Justification elsewhere for Sokoban traps is based
4123.                      * on air currents.  This is consistent with that.
4124.                      * The unexpected additional force of the air currents
4125.                      * once levitation ceases knocks you off your feet.
4126.                      */
4127.                     if (Hallucination)
4128.                         pline("Bummer!  You've crashed.");
4129.                     else
4130.                         You("fall over.");
4131.                     losehp(rnd(2), "dangerous winds", KILLED_BY);
4132.                     if (u.usteed)
4133.                         dismount_steed(DISMOUNT_FELL);
4134.                     selftouch("As you fall, you");
4135.                 } else if (u.usteed && (is_floater(u.usteed->data)
4136.                                         || is_flyer(u.usteed->data))) {
4137.                     You("settle more firmly in the saddle.");
4138.                 } else if (Hallucination) {
4139.                     pline("Bummer!  You've %s.",
4140.                           is_pool(u.ux, u.uy)
4141.                              ? "splashed down"
4142.                              : "hit the ground");
4143.                 } else {
4144.                     You("float gently to the %s.", surface(u.ux, u.uy));
4145.                 }
4146.             }
4147.         }
4148.     }
4149. 
4150.     /* levitation gives maximum carrying capacity, so having it end
4151.        potentially triggers greater encumbrance; do this after
4152.        'come down' messages, before trap activation or autopickup */
4153.     encumber_msg();
4154. 
4155.     /* can't rely on u.uz0 for detecting trap door-induced level change;
4156.        it gets changed to reflect the new level before we can check it */
4157.     assign_level(&current_dungeon_level, &u.uz);
4158.     if (trap) {
4159.         switch (trap->ttyp) {
4160.         case STATUE_TRAP:
4161.             break;
4162.         case HOLE:
4163.         case TRAPDOOR:
4164.             if (!Can_fall_thru(&u.uz) || u.ustuck)
4165.                 break;
4166.             FALLTHROUGH;
4167.             /*FALLTHRU*/
4168.         default:
4169.             if (!u.utrap) /* not already in the trap */
4170.                 dotrap(trap, NO_TRAP_FLAGS);
4171.         }
4172.     }
4173.     if (!Is_airlevel(&u.uz) && !Is_waterlevel(&u.uz) && !u.uswallow
4174.         /* falling through trap door calls goto_level,
4175.            and goto_level does its own pickup() call */
4176.         && on_level(&u.uz, &current_dungeon_level))
4177.         (void) pickup(1);
4178.     return 1;
4179. }
4180. 
4181. /* shared code for climbing out of a pit */
4182. void
4183. climb_pit(void)
4184. {
4185.     const char *pitname;
4186. 
4187.     if (!u.utrap || u.utraptype != TT_PIT)
4188.         return;
4189. 
4190.     pitname = trapname(PIT, FALSE);
4191.     if (Passes_walls) {
4192.         /* marked as trapped so they can pick things up */
4193.         You("ascend from the %s.", pitname);
4194.         reset_utrap(FALSE);
4195.         fill_pit(u.ux, u.uy);
4196.         gv.vision_full_recalc = 1; /* vision limits change */
4197.     } else if (!rn2(2) && sobj_at(BOULDER, u.ux, u.uy)) {
4198.         Your("%s gets stuck in a crevice.", body_part(LEG));
4199.         display_nhwindow(WIN_MESSAGE, FALSE);
4200.         clear_nhwindow(WIN_MESSAGE);
4201.         You("free your %s.", body_part(LEG));
4202.     } else if ((Flying || is_clinger(gy.youmonst.data)) && !Sokoban) {
4203.         /* eg fell in pit, then poly'd to a flying monster;
4204.            or used '>' to deliberately enter it */
4205.         You("%s from the %s.", u_locomotion("climb"), pitname);
4206.         reset_utrap(FALSE);
4207.         fill_pit(u.ux, u.uy);
4208.         gv.vision_full_recalc = 1; /* vision limits change */
4209.     } else if (!(--u.utrap) || m_easy_escape_pit(&gy.youmonst)) {
4210.         reset_utrap(FALSE);
4211.         You("%s to the edge of the %s.",
4212.             (Sokoban && Levitation)
4213.                 ? "struggle against the air currents and float"
4214.                 : u.usteed ? "ride" : "crawl",
4215.             pitname);
4216.         fill_pit(u.ux, u.uy);
4217.         gv.vision_full_recalc = 1; /* vision limits change */
4218.     } else if (u.dz || flags.verbose) {
4219.         /* these should use 'pitname' rather than "pit" for hallucination
4220.            but that would nullify Norep (this message can be repeated
4221.            many times without further user intervention by using a run
4222.            attempt to keep retrying to escape from the pit) */
4223.         if (u.usteed)
4224.             Norep("%s is still in a pit.", YMonnam(u.usteed));
4225.         else
4226.             Norep((Hallucination && !rn2(5))
4227.                       ? "You've fallen, and you can't get up."
4228.                       : "You are still in a pit.");
4229.     }
4230. }
4231. 
4232. staticfn void
4233. dofiretrap(
4234.     struct obj *box) /* null for floor trap */
4235. {
4236.     boolean see_it = !Blind;
4237.     int orig_dmg, num, alt;
4238.     orig_dmg = num = d(2, 4);
4239. 
4240.     /* Bug: for box case, the equivalent of burn_floor_objects() ought
4241.      * to be done upon its contents.
4242.      */
4243. 
4244.     if ((box && !carried(box)) ? is_pool(box->ox, box->oy) : Underwater) {
4245.         pline("A cascade of steamy bubbles erupts from %s!",
4246.               the(box ? xname(box) : surface(u.ux, u.uy)));
4247.         if (Fire_resistance)
4248.             You("are uninjured.");
4249.         else
4250.             losehp(rnd(3), "boiling water", KILLED_BY);
4251.         return;
4252.     }
4253.     pline("A %s %s from %s!", tower_of_flame, box ? "bursts" : "erupts",
4254.           the(box ? xname(box) : surface(u.ux, u.uy)));
4255.     if (Fire_resistance) {
4256.         shieldeff(u.ux, u.uy);
4257.         monstseesu(M_SEEN_FIRE);
4258.         num = rn2(2);
4259.     } else if (Upolyd) {
4260.         switch (u.umonnum) {
4261.         case PM_PAPER_GOLEM:
4262.             alt = u.mhmax;
4263.             break;
4264.         case PM_STRAW_GOLEM:
4265.             alt = u.mhmax / 2;
4266.             break;
4267.         case PM_WOOD_GOLEM:
4268.             alt = u.mhmax / 4;
4269.             break;
4270.         case PM_LEATHER_GOLEM:
4271.             alt = u.mhmax / 8;
4272.             break;
4273.         default:
4274.             alt = 0;
4275.             break;
4276.         }
4277.         if (alt > num)
4278.             num = alt;
4279.         if (u.mhmax > mons[u.umonnum].mlevel)
4280.             u.mhmax -= rn2(min(u.mhmax, num + 1)), disp.botl = TRUE;
4281.         if (u.mh > u.mhmax)
4282.             u.mh = u.mhmax, disp.botl = TRUE;
4283.         monstunseesu(M_SEEN_FIRE);
4284.     } else {
4285.         int uhpmin = minuhpmax(1), olduhpmax = u.uhpmax;
4286. 
4287.         num = d(2, 4);
4288.         if (u.uhpmax > uhpmin) {
4289.             u.uhpmax -= rn2(min(u.uhpmax, num + 1)), disp.botl = TRUE;
4290.         } /* note: no 'else' here */
4291.         if (u.uhpmax < uhpmin) {
4292.             setuhpmax(min(olduhpmax, uhpmin), FALSE); /* sets disp.botl */
4293.             if (!Drain_resistance)
4294.                 losexp(NULL); /* never fatal when 'drainer' is Null */
4295.         }
4296.         if (u.uhp > u.uhpmax)
4297.             u.uhp = u.uhpmax, disp.botl = TRUE;
4298.         monstunseesu(M_SEEN_FIRE);
4299.     }
4300.     if (!num)
4301.         You("are uninjured.");
4302.     else
4303.         losehp(num, tower_of_flame, KILLED_BY_AN); /* fire damage */
4304.     burn_away_slime();
4305. 
4306.     if (burnarmor(&gy.youmonst) || rn2(3)) {
4307.         (void) destroy_items(&gy.youmonst, AD_FIRE, orig_dmg);
4308.         ignite_items(gi.invent);
4309.     }
4310.     if (!box && burn_floor_objects(u.ux, u.uy, see_it, TRUE) && !see_it)
4311.         You("smell paper burning.");
4312.     if (is_ice(u.ux, u.uy))
4313.         melt_ice(u.ux, u.uy, (char *) 0);
4314. }
4315. 
4316. staticfn void
4317. domagictrap(void)
4318. {
4319.     int fate = rnd(20);
4320. 
4321.     /* What happened to the poor sucker? */
4322. 
4323.     if (fate < 10) {
4324.         /* Most of the time, it creates some monsters. */
4325.         int cnt = rnd(4);
4326. 
4327.         /* blindness effects */
4328.         if (!resists_blnd(&gy.youmonst)) {
4329.             You("are momentarily blinded by a flash of light!");
4330.             make_blinded((long) rn1(5, 10), FALSE);
4331.             if (!Blind)
4332.                 Your1(vision_clears);
4333.         } else if (!Blind) {
4334.             You_see("a flash of light!");
4335.         }
4336. 
4337.         /* deafness effects */
4338.         if (!Deaf) {
4339.             Soundeffect(se_deafening_roar_atmospheric, 100);
4340.             You_hear("a deafening roar!");
4341.             incr_itimeout(&HDeaf, rn1(20, 30));
4342.             disp.botl = TRUE;
4343.         } else {
4344.             /* magic vibrations still hit you */
4345.             You_feel("rankled.");
4346.             incr_itimeout(&HDeaf, rn1(5, 15));
4347.             disp.botl = TRUE;
4348.         }
4349.         while (cnt--)
4350.             (void) makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS);
4351.         /* roar: wake monsters in vicinity, after placing trap-created ones */
4352.         wake_nearto(u.ux, u.uy, 7 * 7);
4353.         /* [flash: should probably also hit nearby gremlins with light] */
4354.     } else {
4355.         switch (fate) {
4356.         case 10:
4357.             /* sometimes nothing happens */
4358.             break;
4359.         case 11: /* toggle intrinsic invisibility */
4360.             Soundeffect(se_low_hum, 100);
4361.             You_hear("a low hum.");
4362.             if (!Invis) {
4363.                 if (!Blind)
4364.                     self_invis_message();
4365.             } else if (!EInvis && !pm_invisible(gy.youmonst.data)) {
4366.                 if (!Blind) {
4367.                     if (!See_invisible)
4368.                         You("can see yourself again!");
4369.                     else
4370.                         You_cant("see through yourself anymore.");
4371.                 }
4372.             } else {
4373.                 /* If we're invisible from another source */
4374.                 You_feel("a little more %s now.",
4375.                          HInvis ? "obvious" : "hidden");
4376.             }
4377.             HInvis = HInvis ? 0 : HInvis | FROMOUTSIDE;
4378.             newsym(u.ux, u.uy);
4379.             break;
4380.         case 12: /* a flash of fire */
4381.             dofiretrap((struct obj *) 0);
4382.             break;
4383. 
4384.         /* odd feelings */
4385.         case 13:
4386.             pline("A shiver runs up and down your %s!", body_part(SPINE));
4387.             break;
4388.         case 14:
4389.             You_hear(Hallucination ? "the moon howling at you."
4390.                                    : "distant howling.");
4391.             break;
4392.         case 15:
4393.             if (on_level(&u.uz, &qstart_level))
4394.                 You_feel(
4395.                     "%slike the prodigal son.",
4396.                     (flags.female || (Upolyd && is_neuter(gy.youmonst.data)))
4397.                         ? "oddly "
4398.                         : "");
4399.             else
4400.                 You("suddenly yearn for %s.",
4401.                     Hallucination
4402.                         ? "Cleveland"
4403.                         : (In_quest(&u.uz) || at_dgn_entrance("The Quest"))
4404.                               ? "your nearby homeland"
4405.                               : "your distant homeland");
4406.             break;
4407.         case 16:
4408.             Your("pack shakes violently!");
4409.             break;
4410.         case 17:
4411.             You(Hallucination ? "smell hamburgers." : "smell charred flesh.");
4412.             break;
4413.         case 18:
4414.             You_feel("tired.");
4415.             break;
4416. 
4417.         /* very occasionally something nice happens. */
4418.         case 19: { /* tame nearby monsters */
4419.             int i, j;
4420.             struct monst *mtmp;
4421. 
4422.             (void) adjattrib(A_CHA, 1, FALSE);
4423.             for (i = -1; i <= 1; i++)
4424.                 for (j = -1; j <= 1; j++) {
4425.                     if (!isok(u.ux + i, u.uy + j))
4426.                         continue;
4427.                     mtmp = m_at(u.ux + i, u.uy + j);
4428.                     if (mtmp)
4429.                         (void) tamedog(mtmp, (struct obj *) 0, TRUE);
4430.                 }
4431.             break;
4432.         }
4433.         case 20: { /* uncurse stuff */
4434.             struct obj pseudo;
4435.             long save_conf = HConfusion;
4436. 
4437.             pseudo = cg.zeroobj; /* force 'uncursed' and zero out oextra */
4438.             /* used to be SCR_REMOVE_CURSE but that could cause seffects()
4439.                to have hero discover scroll of remove curse */
4440.             pseudo.otyp = SPE_REMOVE_CURSE;
4441.             pseudo.oclass = SPBOOK_CLASS;
4442.             HConfusion = 0L;
4443.             (void) seffects(&pseudo);
4444.             HConfusion = save_conf;
4445.             break;
4446.         }
4447.         default:
4448.             break;
4449.         }
4450.     }
4451. }
4452. 
4453. /* Set an item on fire.  Return whether the object was destroyed. */
4454. boolean
4455. fire_damage(
4456.     struct obj *obj,
4457.     boolean force, /* if True, skip luck-based protection check */
4458.     coordxy x, coordxy y) /* where to place contents of burned up container */
4459. {
4460.     int chance;
4461.     struct obj *otmp, *ncobj;
4462.     int in_sight = !Blind && couldsee(x, y); /* Don't care if it's lit */
4463.     int dindx;
4464. 
4465.     /* object might light in a controlled manner */
4466.     if (catch_lit(obj))
4467.         return FALSE;
4468. 
4469.     if (Is_container(obj) || obj->otyp == STATUE) {
4470.         switch (obj->otyp) {
4471.         case STATUE:
4472.         case ICE_BOX:
4473.             return FALSE; /* Immune */
4474.         case CHEST:
4475.             chance = 40;
4476.             break;
4477.         case LARGE_BOX:
4478.             chance = 30;
4479.             break;
4480.         default:
4481.             chance = 20;
4482.             break;
4483.         }
4484.         if ((!force && (Luck + 5) > rn2(chance))
4485.             /* note: containers aren't subject to erosion so are never
4486.                marked fireproof/corrodeproof/&c */
4487.             /*|| (is_flammable(obj) && obj->oerodeproof)*/
4488.             ) {
4489.             return FALSE;
4490.         }
4491.         /* Container is burnt up - dump contents out */
4492.         if (in_sight)
4493.             pline("%s catches fire and burns.", Yname2(obj));
4494.         if (Has_contents(obj)) {
4495.             if (in_sight)
4496.                 pline("Its contents fall out.");
4497.             for (otmp = obj->cobj; otmp; otmp = ncobj) {
4498.                 ncobj = otmp->nobj;
4499.                 obj_extract_self(otmp);
4500.                 if (!flooreffects(otmp, x, y, ""))
4501.                     place_object(otmp, x, y);
4502.             }
4503.         }
4504.         setnotworn(obj);
4505.         delobj(obj);
4506.         return TRUE;
4507.     } else if (!force && (Luck + 5) > rn2(20)) {
4508.         /*  chance per item of sustaining damage:
4509.           *     max luck (Luck==13):    10%
4510.           *     avg luck (Luck==0):     75%
4511.           *     awful luck (Luck<-4):  100%
4512.           */
4513.         return FALSE;
4514.     } else if (obj->oclass == SCROLL_CLASS || obj->oclass == SPBOOK_CLASS) {
4515.         if (obj->otyp == SCR_FIRE || obj->otyp == SPE_FIREBALL)
4516.             return FALSE;
4517.         if (obj->otyp == SPE_BOOK_OF_THE_DEAD) {
4518.             if (in_sight)
4519.                 pline("Smoke rises from %s.", the(xname(obj)));
4520.             return FALSE;
4521.         }
4522.         dindx = (obj->oclass == SCROLL_CLASS) ? 3 : 4;
4523.         if (in_sight)
4524.             pline("%s %s.", Yname2(obj),
4525.                   destroy_strings[dindx][(obj->quan > 1L)]);
4526.         setnotworn(obj);
4527.         delobj(obj);
4528.         return TRUE;
4529.     } else if (obj->oclass == POTION_CLASS) {
4530.         dindx = (obj->otyp != POT_OIL) ? 1 : 2;
4531.         if (in_sight)
4532.             pline("%s %s.", Yname2(obj),
4533.                   destroy_strings[dindx][(obj->quan > 1L)]);
4534.         setnotworn(obj);
4535.         delobj(obj);
4536.         return TRUE;
4537.     } else if (erode_obj(obj, (char *) 0, ERODE_BURN, EF_DESTROY)
4538.                == ER_DESTROYED) {
4539.         return TRUE;
4540.     }
4541.     return FALSE;
4542. }
4543. 
4544. /*
4545.  * Apply fire_damage() to an entire chain.
4546.  *
4547.  * Return number of objects destroyed. --ALI
4548.  */
4549. int
4550. fire_damage_chain(
4551.     struct obj *chain,
4552.     boolean force,
4553.     boolean here,
4554.     coordxy x, coordxy y)
4555. {
4556.     struct obj *obj, *nobj;
4557.     int num = 0;
4558. 
4559.     /* erode_obj() relies on bhitpos if target objects aren't carried by
4560.        the hero or a monster, to check visibility controlling feedback */
4561.     gb.bhitpos.x = x, gb.bhitpos.y = y;
4562. 
4563.     for (obj = chain; obj; obj = nobj) {
4564.         nobj = here ? obj->nexthere : obj->nobj;
4565.         if (fire_damage(obj, force, x, y))
4566.             ++num;
4567.     }
4568. 
4569.     if (num && (Blind && !couldsee(x, y)))
4570.         You("smell smoke.");
4571.     return num;
4572. }
4573. 
4574. /* obj has been thrown or dropped into lava; damage is worse than mere fire */
4575. boolean
4576. lava_damage(struct obj *obj, coordxy x, coordxy y)
4577. {
4578.     int otyp = obj->otyp, ocls = obj->oclass;
4579. 
4580.     /* the Amulet, invocation items, and Rider corpses are never destroyed
4581.        (let Book of the Dead fall through to fire_damage() to get feedback) */
4582.     if (obj_resists(obj, 0, 0) && otyp != SPE_BOOK_OF_THE_DEAD)
4583.         return FALSE;
4584.     /* destroy liquid (venom), wax, veggy, flesh, paper (except for scrolls
4585.        and books--let fire damage deal with them), cloth, leather, wood, bone
4586.        unless it's inherently or explicitly fireproof or contains something;
4587.        note: potions are glass so fall through to fire_damage() and boil */
4588.     if (objects[otyp].oc_material < DRAGON_HIDE
4589.         && ocls != SCROLL_CLASS && ocls != SPBOOK_CLASS
4590.         && objects[otyp].oc_oprop != FIRE_RES
4591.         && otyp != WAN_FIRE && otyp != FIRE_HORN
4592.         /* assumes oerodeproof isn't overloaded for some other purpose on
4593.            non-eroding items */
4594.         && !obj->oerodeproof
4595.         /* fire_damage() knows how to deal with containers and contents */
4596.         && !Has_contents(obj)) {
4597.         if (cansee(x, y)) {
4598.             /* this feedback is pretty clunky and can become very verbose
4599.                when former contents of a burned container get here via
4600.                flooreffects() */
4601.             if (obj == gt.thrownobj || obj == gk.kickedobj)
4602.                 pline("%s %s up!", is_plural(obj) ? "They" : "It",
4603.                       otense(obj, "burn"));
4604.             else
4605.                 You_see("%s hit lava and burn up!", doname(obj));
4606.         }
4607.         if (carried(obj)) { /* shouldn't happen */
4608.             remove_worn_item(obj, TRUE);
4609.             useupall(obj);
4610.         } else
4611.             delobj(obj);
4612.         return TRUE;
4613.     }
4614.     return fire_damage(obj, TRUE, x, y);
4615. }
4616. 
4617. void
4618. acid_damage(struct obj *obj)
4619. {
4620.     /* Scrolls but not spellbooks can be erased by acid. */
4621.     struct monst *victim;
4622.     boolean vismon;
4623. 
4624.     if (!obj)
4625.         return;
4626. 
4627.     victim = carried(obj) ? &gy.youmonst : mcarried(obj) ? obj->ocarry : NULL;
4628.     vismon = victim && (victim != &gy.youmonst) && canseemon(victim);
4629. 
4630.     if (victim == &gy.youmonst && inventory_resistance_check(AD_ACID))
4631.         return;
4632. 
4633.     if (obj->greased) {
4634.         grease_protect(obj, (char *) 0, victim);
4635.     } else if (obj->oclass == SCROLL_CLASS && obj->otyp != SCR_BLANK_PAPER) {
4636.         if (obj->otyp != SCR_BLANK_PAPER
4637. #ifdef MAIL_STRUCTURES
4638.             && obj->otyp != SCR_MAIL
4639. #endif
4640.             ) {
4641.             if (!Blind) {
4642.                 if (victim == &gy.youmonst)
4643.                     Your("%s.", aobjnam(obj, "fade"));
4644.                 else if (vismon)
4645.                     pline("%s %s.", s_suffix(Monnam(victim)),
4646.                           aobjnam(obj, "fade"));
4647.             }
4648.         }
4649.         obj->otyp = SCR_BLANK_PAPER;
4650.         obj->spe = 0;
4651.         obj->dknown = 0;
4652.     } else
4653.         erode_obj(obj, (char *) 0, ERODE_CORRODE, EF_GREASE | EF_VERBOSE);
4654. }
4655. 
4656. staticfn void
4657. pot_acid_damage(
4658.     struct obj *obj,
4659.     boolean in_invent,
4660.     boolean described)
4661. {
4662.     char *bufp;
4663.     boolean one, exploded;
4664. 
4665.     one = (obj->quan == 1L);
4666.     exploded = FALSE;
4667. 
4668.     if (Blind && !in_invent)
4669.         obj->dknown = 0;
4670.     if (ga.acid_ctx.ctx_valid)
4671.         exploded = ((obj->dknown ? ga.acid_ctx.dkn_boom
4672.                                  : ga.acid_ctx.unk_boom) > 0);
4673.     if (described) {
4674.         /* just gave "The grease washes off your potion of acid."
4675.             or "...your <color> potion." (or just "...your potion.");
4676.             don't re-describe potion here; if we used "It explodes!"
4677.             then "it" might be misconstrued as applying to "grease" */
4678.         pline_The("potion%s %s!",
4679.                     plur(obj->quan), otense(obj, "explode"));
4680.     } else {
4681.         /* First message is
4682.             * "a [potion|<color> potion|potion of acid] explodes"
4683.             * depending on obj->dknown (potion has been seen) and
4684.             * objects[POT_ACID].oc_name_known (fully discovered),
4685.             * or "some {plural version} explode" when relevant.
4686.             * Second and subsequent messages for same chain and
4687.             * matching dknown status are
4688.             * "another [potion|<color> &c] explodes" or plural
4689.             * variant.
4690.             */
4691.         bufp = simpleonames(obj);
4692.         pline("%s%s %s!", /* "A potion explodes!" */
4693.                 !exploded ? (one ? "A " : "Some ")
4694.                         : (one ? "Another " : "More "),
4695.             bufp, vtense(bufp, "explode"));
4696.     }
4697.     if (ga.acid_ctx.ctx_valid) {
4698.         if (obj->dknown)
4699.             ga.acid_ctx.dkn_boom++;
4700.         else
4701.             ga.acid_ctx.unk_boom++;
4702.     }
4703.     setnotworn(obj);
4704.     delobj(obj);
4705.     if (in_invent)
4706.         update_inventory();
4707. }
4708. 
4709. /* Get an object wet and damage it appropriately.
4710.    Returns an erosion return value (ER_*). */
4711. int
4712. water_damage(
4713.     struct obj *obj,  /* might be Null; return ER_NOTHING if so */
4714.     const char *ostr, /* if non-Null, use instead of cxname() in messages */
4715.     boolean force)    /* if True, skip luck-based protection check */
4716. {
4717.     boolean in_invent = obj && carried(obj), described = FALSE;
4718. 
4719.     if (!obj)
4720.         return ER_NOTHING;
4721. 
4722.     if (splash_lit(obj))
4723.         return ER_DAMAGED;
4724. 
4725.     if (!ostr)
4726.         ostr = cxname(obj);
4727. 
4728.     if (obj->otyp == CAN_OF_GREASE && obj->spe > 0) {
4729.         return ER_NOTHING;
4730.     } else if (obj->otyp == TOWEL && obj->spe < 7) {
4731.         /* a negative change induces a reverse increment, adding abs(change);
4732.            spe starts 0..6, arg passed to rnd() is 1..7, change is -7..-1,
4733.            final spe is 1..7 and always greater than its starting value */
4734.         wet_a_towel(obj, -rnd(7 - obj->spe), TRUE);
4735.         return ER_NOTHING;
4736.     } else if (obj->greased) {
4737.         if (!rn2(2)) {
4738.             obj->greased = 0;
4739.             if (in_invent) {
4740.                 pline_The("grease on %s washes off.", yname(obj));
4741.                 described = TRUE; /* used to modify potion feedback */
4742.                 update_inventory();
4743.             }
4744.             /* ungreased potions of acid will always be destroyed by water */
4745.             if (obj->otyp == POT_ACID) {
4746.                 pot_acid_damage(obj, in_invent, described);
4747.                 return ER_DESTROYED;
4748.             }
4749.         }
4750.         return ER_GREASED;
4751.     } else if (Is_container(obj)
4752.                && (!Waterproof_container(obj) || (obj->cursed && !rn2(3)))) {
4753.         if (in_invent) {
4754.             pline("Some %s gets into your %s!", hliquid("water"), ostr);
4755.             gm.mentioned_water = !Hallucination;
4756.         }
4757.         water_damage_chain(obj->cobj, FALSE);
4758.         return ER_DAMAGED; /* contents were damaged */
4759.     } else if (Waterproof_container(obj)) {
4760.         if (in_invent && !Blind && !Underwater) {
4761.             pline_The("%s cannot get into your %s.", hliquid("water"), ostr);
4762.             gm.mentioned_water = !Hallucination;
4763.             makeknown(obj->otyp); /* if an oilskin sack, discover it; doesn't
4764.                                    * matter for chest, large box, ice box */
4765.         }
4766.         /* not actually damaged, but because we /didn't/ get the "water
4767.            gets into!" message, the player now has more information and
4768.            thus we need to waste any potion they may have used (also,
4769.            flavourwise the water is now on the floor) */
4770.         return ER_DAMAGED;
4771.     } else if (!force && (Luck + 5) > rn2(20)) {
4772.         /*  chance per item of sustaining damage:
4773.             *   max luck:               10%
4774.             *   avg luck (Luck==0):     75%
4775.             *   awful luck (Luck<-4):  100%
4776.             */
4777.         return ER_NOTHING;
4778.     } else if (obj->oclass == SCROLL_CLASS) {
4779.         if (obj->otyp == SCR_BLANK_PAPER
4780. #ifdef MAIL_STRUCTURES
4781.             || obj->otyp == SCR_MAIL
4782. #endif
4783.            ) return 0;
4784.         if (in_invent)
4785.             Your("%s %s.", ostr, vtense(ostr, "fade"));
4786. 
4787.         obj->otyp = SCR_BLANK_PAPER;
4788.         obj->dknown = 0;
4789.         obj->spe = 0;
4790.         if (in_invent)
4791.             update_inventory();
4792.         return ER_DAMAGED;
4793.     } else if (obj->oclass == SPBOOK_CLASS) {
4794.         int otyp = obj->otyp;
4795. 
4796.         if (otyp == SPE_BOOK_OF_THE_DEAD) {
4797.             coordxy ox = 0, oy = 0;
4798. 
4799.             /* note: The Book of the Dead can't be contained or buried */
4800.             if (get_obj_location(obj, &ox, &oy, CONTAINED_TOO | BURIED_TOO))
4801.                 obj->ox = ox, obj->oy = oy;
4802.             if (isok(ox, oy) && cansee(ox, oy))
4803.                 pline("Steam rises from %s.", the(xname(obj)));
4804.             return 0;
4805.         } else if (otyp == SPE_BLANK_PAPER) {
4806.             return 0;
4807.         }
4808.         if (in_invent)
4809.             Your("%s %s.", ostr, vtense(ostr, "fade"));
4810. 
4811.         obj->otyp = SPE_BLANK_PAPER;
4812.         /* same re-init as over-reading or polymorph; matters if it gets
4813.            polymorphed into non-blank; doesn't matter if eventually written
4814.            on since that replaces it with new book and studied count of 0 */
4815.         if (obj->spestudied)
4816.             obj->spestudied = rn2(obj->spestudied);
4817.         obj->dknown = 0;
4818.         /* blanking a novel is more involved than blanking a spellbook */
4819.         if (otyp == SPE_NOVEL) /* old type */
4820.             blank_novel(obj);
4821.         if (in_invent)
4822.             update_inventory();
4823.         return ER_DAMAGED;
4824.     } else if (obj->oclass == POTION_CLASS) {
4825.         if (obj->otyp == POT_ACID) {
4826.             pot_acid_damage(obj, in_invent, described);
4827.             return ER_DESTROYED;
4828.         } else if (obj->odiluted) {
4829.             if (in_invent)
4830.                 Your("%s %s further.", ostr, vtense(ostr, "dilute"));
4831. 
4832.             obj->otyp = POT_WATER;
4833.             obj->dknown = 0;
4834.             obj->blessed = obj->cursed = 0;
4835.             obj->odiluted = 0;
4836.             if (in_invent)
4837.                 update_inventory();
4838.             return ER_DAMAGED;
4839.         } else if (obj->otyp != POT_WATER) {
4840.             if (in_invent)
4841.                 Your("%s %s.", ostr, vtense(ostr, "dilute"));
4842. 
4843.             obj->odiluted++;
4844.             if (in_invent)
4845.                 update_inventory();
4846.             return ER_DAMAGED;
4847.         }
4848.     } else {
4849.         return erode_obj(obj, ostr, ERODE_RUST, EF_NONE);
4850.     }
4851.     return ER_NOTHING;
4852. }
4853. 
4854. void
4855. water_damage_chain(
4856.     struct obj *obj,
4857.     boolean here)
4858. {
4859.     struct obj *otmp;
4860.     coordxy x, y;
4861.     coord save_bhitpos;
4862. 
4863.     if (!obj)
4864.         return;
4865. 
4866.     /* initialize acid context: so far, neither seen (dknown) potions of
4867.        acid nor unseen have exploded during this water damage sequence */
4868.     ga.acid_ctx.dkn_boom = ga.acid_ctx.unk_boom = 0;
4869.     ga.acid_ctx.ctx_valid = TRUE;
4870.     /* we don't want to permanently overwrite bhitpos below, since we can get
4871.        here from scenarios where it was in use up the call stack (e.g. thrown
4872.        item hurtling the levitating hero into a wall of water) */
4873.     save_bhitpos = gb.bhitpos;
4874.     /* erode_obj() relies on bhitpos if target objects aren't carried by
4875.        the hero or a monster, to check visibility controlling feedback */
4876.     if (get_obj_location(obj, &x, &y, CONTAINED_TOO))
4877.         gb.bhitpos.x = x, gb.bhitpos.y = y;
4878. 
4879.     for (; obj; obj = otmp) {
4880.         otmp = here ? obj->nexthere : obj->nobj;
4881.         water_damage(obj, (char *) 0, FALSE);
4882.     }
4883. 
4884.     /* reset acid context and bhitpos */
4885.     ga.acid_ctx.dkn_boom = ga.acid_ctx.unk_boom = 0;
4886.     ga.acid_ctx.ctx_valid = FALSE;
4887.     gb.bhitpos = save_bhitpos;
4888. }
4889. 
4890. /*
4891.  * This function is potentially expensive - rolling
4892.  * inventory list multiple times.  Luckily it's seldom needed.
4893.  * Returns TRUE if disrobing made player unencumbered enough to
4894.  * crawl out of the current predicament.
4895.  */
4896. staticfn boolean
4897. emergency_disrobe(boolean *lostsome)
4898. {
4899.     int invc = inv_cnt(TRUE);
4900. 
4901.     while (near_capacity() > (Punished ? UNENCUMBERED : SLT_ENCUMBER)) {
4902.         struct obj *obj, *nextobj, *otmp = (struct obj *) 0;
4903.         int i;
4904. 
4905.         /* Pick a random object */
4906.         if (invc > 0) {
4907.             i = rn2(invc);
4908.             for (obj = gi.invent; obj; obj = nextobj) {
4909.                 nextobj = obj->nobj;
4910.                 /*
4911.                  * Undroppables are: body armor, boots, gloves,
4912.                  * amulets, and rings because of the time and effort
4913.                  * in removing them + loadstone and other cursed stuff
4914.                  * for obvious reasons.  Also, any item in the midst
4915.                  * of being taken off or stolen.
4916.                  */
4917.                 if (!((obj->otyp == LOADSTONE && obj->cursed) || obj == uamul
4918.                       || obj == uleft || obj == uright || obj == ublindf
4919.                       || obj == uarm || obj == uarmc || obj == uarmg
4920.                       || obj == uarmf || obj == uarmu
4921.                       || (obj->cursed && (obj == uarmh || obj == uarms))
4922.                       || welded(obj)
4923.                       || obj->o_id == gs.stealoid || obj->in_use))
4924.                     otmp = obj;
4925.                 /* reached the mark and found some stuff to drop? */
4926.                 if (--i < 0 && otmp)
4927.                     break;
4928. 
4929.                 /* else continue */
4930.             }
4931.         }
4932.         if (!otmp)
4933.             return FALSE; /* nothing to drop! */
4934.         if (otmp->owornmask)
4935.             remove_worn_item(otmp, FALSE);
4936.         *lostsome = TRUE;
4937.         dropx(otmp);
4938.         invc--;
4939.     }
4940.     return TRUE;
4941. }
4942. 
4943. /* pick a random goodpos() next to x,y for monster mtmp.
4944.    mtmp could be &gy.youmonst, uses then crawl_destination().
4945.    returns TRUE if any good position found, with the coord in x,y */
4946. boolean
4947. rnd_nextto_goodpos(coordxy *x, coordxy *y, struct monst *mtmp)
4948. {
4949.     int i, j;
4950.     boolean is_u = (mtmp == &gy.youmonst);
4951.     coordxy nx, ny, k, dirs[N_DIRS];
4952. 
4953.     for (i = 0; i < N_DIRS; ++i)
4954.         dirs[i] = i;
4955.     for (i = N_DIRS; i > 0; --i) {
4956.         j = rn2(i);
4957.         k = dirs[j];
4958.         dirs[j] = dirs[i - 1];
4959.         dirs[i - 1] = k;
4960.     }
4961.     for (i = 0; i < N_DIRS; ++i) {
4962.         nx = *x + xdir[dirs[i]];
4963.         ny = *y + ydir[dirs[i]];
4964.         /* crawl_destination and goodpos both include an isok() check */
4965.         if (is_u ? crawl_destination(nx, ny) : goodpos(nx, ny, mtmp, 0)) {
4966.             *x = nx;
4967.             *y = ny;
4968.             return TRUE;
4969.         }
4970.     }
4971.     return FALSE;
4972. }
4973. 
4974. /* print a message about being back on the ground after leaving a pool */
4975. void
4976. back_on_ground(boolean rescued)
4977. {
4978.     const char *preposit = (Levitation || Flying) ? "over" : "on",
4979.                *surf = surface(u.ux, u.uy), *you_are_back;
4980.     char icebuf[QBUFSZ];
4981. 
4982.     if (is_ice(u.ux, u.uy)) {
4983.         /* "on ice" */
4984.         surf = ice_descr(u.ux, u.uy, icebuf);
4985.     } else if (!strcmpi(surf, "floor") || !strcmpi(surf, "ground")) {
4986.         /* "on solid ground" */
4987.         surf = "solid ground";
4988.     } else if (!strcmpi(surf, "bridge") || !strcmpi(surf, "altar")
4989.                || !strcmpi(surf, "headstone")) {
4990.         /* "on a bridge" */
4991.         surf = an(surf);
4992.     } else if (!strcmpi(surf, "stairs") || !strcmpi(surf, "lava")
4993.                || !strcmpi(surf, "bottom")) {
4994.         /* "on the stairs" */
4995.         surf = the(surf);
4996.     } else { /* "cloud", "air", "air bubble", "wall", "fountain", "doorway" */
4997.         /* "in a cloud", "in the air" */
4998.         surf = !strcmp(surf, "air") ? the(surf) : an(surf);
4999.         preposit = "in";
5000.     }
5001.     if (rescued) {
5002.         you_are_back = "You find yourself";
5003.     } else {
5004.         you_are_back = flags.verbose ? "You are back" : "Back";
5005.     }
5006.     pline("%s %s %s.", you_are_back, preposit, surf);
5007.     iflags.last_msg = PLNMSG_BACK_ON_GROUND;
5008. }
5009. 
5010. /* life-saving or divine rescue has attempted to get the hero out of hostile
5011.    terrain and put hero in an unexpected spot or failed due to overfull level
5012.    and just prevented death so "back on solid ground" may be inappropriate */
5013. void
5014. rescued_from_terrain(int how)
5015. {
5016.     static const char find_yourself[] = "find yourself";
5017.     struct rm *lev = &levl[u.ux][u.uy];
5018.     boolean mesggiven = FALSE;
5019. 
5020.     switch (how) {
5021.     case DROWNING:
5022.         if (is_pool(u.ux, u.uy)) {
5023.             You("%s %s of %s.", find_yourself,
5024.                 (Is_waterlevel(&u.uz) || IS_WATERWALL(lev->typ))
5025.                   ? "in the midst" : "on top",
5026.                 hliquid("water"));
5027.             mesggiven = TRUE;
5028.         } else if (IS_AIR(lev->typ)) {
5029.             You("%s in %s.", find_yourself,
5030.                 Is_waterlevel(&u.uz) ? "an air bubble" : "mid air");
5031.             mesggiven = TRUE;
5032.         }
5033.         break;
5034.     case BURNING: /* moved onto lava without fire resistance */
5035.     case DISSOLVED: /* sunk into lava while fire resistant */
5036.         if (is_pool(u.ux, u.uy)) {
5037.             You("%s %s %s.", find_yourself,
5038.                 u.uinwater ? "in" : "on", hliquid("water"));
5039.             mesggiven = TRUE;
5040.         } else if (is_lava(u.ux, u.uy)) {
5041.             You("%s on top of %s.", find_yourself, hliquid("molten lava"));
5042.             mesggiven = TRUE;
5043.         }
5044.         break;
5045.     default:
5046.         break;
5047.     }
5048.     if (!mesggiven)
5049.         back_on_ground(TRUE);
5050. 
5051.     iflags.last_msg = PLNMSG_BACK_ON_GROUND; /* for describe_decor() */
5052.     /* feedback just disclosed this */
5053.     update_lastseentyp(u.ux, u.uy);
5054.     iflags.prev_decor = svl.lastseentyp[u.ux][u.uy];
5055. }
5056. 
5057. /* return TRUE iff player relocated */
5058. boolean
5059. drown(void)
5060. {
5061.     const char *pool_of_water;
5062.     boolean inpool_ok = FALSE;
5063.     int i;
5064.     coordxy x, y;
5065.     boolean is_solid = is_waterwall(u.ux, u.uy);
5066. 
5067.     feel_newsym(u.ux, u.uy); /* in case Blind, map the water here */
5068.     /* happily wading in the same contiguous pool */
5069.     if (u.uinwater && is_pool(u.ux - u.dx, u.uy - u.dy)
5070.         && (Swimming || Amphibious || Breathless)) {
5071.         /* water effects on objects every now and then */
5072.         if (!rn2(5))
5073.             inpool_ok = TRUE;
5074.         else
5075.             return FALSE;
5076.     }
5077. 
5078.     if (!u.uinwater) {
5079.         You("%s into the %s%c", is_solid ? "plunge" : "fall",
5080.             waterbody_name(u.ux, u.uy),
5081.             (Amphibious || Swimming || Breathless) ? '.' : '!');
5082.         if (!Swimming && !is_solid)
5083.             You("sink like %s.", Hallucination ? "the Titanic" : "a rock");
5084.     }
5085. 
5086.     water_damage_chain(gi.invent, FALSE);
5087. 
5088.     if (u.umonnum == PM_GREMLIN && rn2(3)) {
5089.         (void) split_mon(&gy.youmonst, (struct monst *) 0);
5090.     } else if (u.umonnum == PM_IRON_GOLEM) {
5091.         You("rust!");
5092.         i = Maybe_Half_Phys(d(2, 6));
5093.         if (u.mhmax > i)
5094.             u.mhmax -= i;
5095.         losehp(i, "rusting away", KILLED_BY);
5096.     }
5097.     if (inpool_ok)
5098.         return FALSE;
5099. 
5100.     if ((i = number_leashed()) > 0) {
5101.         pline_The("leash%s slip%s loose.", (i > 1) ? "es" : "",
5102.                   (i > 1) ? "" : "s");
5103.         unleash_all();
5104.     }
5105. 
5106.     if (Amphibious || Breathless || Swimming) {
5107.         if (Amphibious || Breathless) {
5108.             if (flags.verbose)
5109.                 pline("But you aren't drowning.");
5110.             if (!Is_waterlevel(&u.uz)) {
5111.                 if (Hallucination)
5112.                     Your("keel hits the bottom.");
5113.                 else
5114.                     You("touch bottom.");
5115.             }
5116.         }
5117.         if (Punished) {
5118.             unplacebc();
5119.             placebc();
5120.         }
5121.         vision_recalc(2); /* unsee old position */
5122.         set_uinwater(1); /* u.uinwater = 1 */
5123.         under_water(1);
5124.         gv.vision_full_recalc = 1;
5125.         return FALSE;
5126.     }
5127.     if ((Teleportation || can_teleport(gy.youmonst.data)) && !Unaware
5128.         && (Teleport_control || rn2(3) < Luck + 2)) {
5129.         You("attempt a teleport spell."); /* utcsri!carroll */
5130.         if (!noteleport_level(&gy.youmonst)) {
5131.             (void) dotele(FALSE);
5132.             if (!is_pool(u.ux, u.uy))
5133.                 return TRUE;
5134.         } else
5135.             pline_The("attempted teleport spell fails.");
5136.     }
5137.     if (u.usteed) {
5138.         dismount_steed(DISMOUNT_GENERIC);
5139.         if (!is_pool(u.ux, u.uy))
5140.             return TRUE;
5141.     }
5142.     /* if sleeping, wake up now so that we don't crawl out of water
5143.        while still asleep; we can't do that the same way that waking
5144.        due to combat is handled; note unmul() clears u.usleep */
5145.     if (u.usleep)
5146.         unmul("Suddenly you wake up!");
5147.     /* being doused will revive from fainting */
5148.     if (is_fainted())
5149.         reset_faint();
5150. 
5151.     x = u.ux, y = u.uy;
5152.     /* have to be able to move in order to crawl */
5153.     if (gm.multi >= 0 && gy.youmonst.data->mmove
5154.         && rnd_nextto_goodpos(&x, &y, &gy.youmonst)) {
5155.         boolean lost = FALSE;
5156.         /* time to do some strip-tease... */
5157.         boolean succ = Is_waterlevel(&u.uz) ? TRUE : emergency_disrobe(&lost);
5158. 
5159.         You("try to crawl out of the %s.", hliquid("water"));
5160.         if (lost)
5161.             You("dump some of your gear to lose weight...");
5162.         if (succ) {
5163.             pline("Pheew!  That was close.");
5164.             teleds(x, y, TELEDS_ALLOW_DRAG);
5165.             return TRUE;
5166.         }
5167.         /* still too much weight */
5168.         pline("But in vain.");
5169.     }
5170.     set_uinwater(1); /* u.uinwater = 1 */
5171.     urgent_pline("You drown.");
5172.     /* first pass is survivable by using up an amulet of life-saving or by
5173.        answering no to "Die?" in explore|wizard mode; second pass can only
5174.        be survivable via the latter */
5175.     for (i = 0; i < 2; i++) {
5176.         /* killer format and name are reconstructed every iteration
5177.            because lifesaving resets them */
5178.         pool_of_water = waterbody_name(u.ux, u.uy);
5179.         svk.killer.format = KILLED_BY_AN;
5180.         /* avoid "drowned in [a] water" */
5181.         if (!strcmp(pool_of_water, "water"))
5182.             pool_of_water = "deep water", svk.killer.format = KILLED_BY;
5183.         /* avoid "drowned in _a_ limitless water" on Plane of Water */
5184.         else if (!strcmp(pool_of_water, "limitless water"))
5185.             svk.killer.format = KILLED_BY;
5186.         Strcpy(svk.killer.name, pool_of_water);
5187.         done(DROWNING);
5188.         /* oops, we're still alive.  better get out of the water. */
5189.         if (safe_teleds(TELEDS_ALLOW_DRAG | TELEDS_TELEPORT))
5190.             break; /* successful life-save */
5191.         /* nowhere safe to land; repeat drowning loop... */
5192.         pline("You're still drowning.");
5193.     }
5194. 
5195.     if (u.uinwater)
5196.         set_uinwater(0); /* u.uinwater = 0 */
5197.     rescued_from_terrain(DROWNING);
5198.     return TRUE;
5199. }
5200. 
5201. void
5202. drain_en(int n, boolean max_already_drained)
5203. {
5204.     const char *mesg;
5205.     char punct = max_already_drained ? '!' : '.';
5206. 
5207.     /*
5208.      * FIXME?
5209.      *  u.uenmax should probably have a higher minimum than 0;
5210.      *  perhaps u.ulevel or (u.ulevel + 1) / 2
5211.      */
5212.     if (u.uenmax < 1) {
5213.         /* energy is completely gone */
5214.         if (u.uen || u.uenmax) { /* paranoia */
5215.             u.uen = u.uenmax = 0;
5216.             disp.botl = TRUE;
5217.         }
5218.         mesg = "momentarily lethargic";
5219.     } else {
5220.         /* throttle further loss a bit when there's not much left to lose */
5221.         if (n > (u.uen + u.uenmax) / 3)
5222.             n = rnd(n);
5223. 
5224.         mesg = "your magical energy drain away";
5225.         if (n > u.uen)
5226.             punct = '!';
5227. 
5228.         u.uen -= n;
5229.         if (u.uen < 0) {
5230.             u.uenmax -= rnd(-u.uen);
5231.             if (u.uenmax < 0)
5232.                 u.uenmax = 0;
5233.             u.uen = 0;
5234.         } else if (u.uen > u.uenmax) {
5235.             /* uen might be greater than uenmax if caller reduced uenmax
5236.                and then we throttled the loss being applied to current */
5237.             u.uen = u.uenmax;
5238.         }
5239.         disp.botl = TRUE;
5240.     }
5241.     /* after manipulating u.uen,uenmax and setting context.botl, so
5242.        that You_feel() -> pline() will update status before the message */
5243.     You_feel("%s%c", mesg, punct);
5244. }
5245. 
5246. /* the #untrap command - disarm a trap */
5247. int
5248. dountrap(void)
5249. {
5250.     if (!could_untrap(TRUE, FALSE))
5251.         return ECMD_OK;
5252. 
5253.     return untrap(FALSE, 0, 0, (struct obj *) 0) ? ECMD_TIME : ECMD_OK;
5254. }
5255. 
5256. /* preliminary checks for dountrap(); also used for autounlock */
5257. int
5258. could_untrap(boolean verbosely, boolean check_floor)
5259. {
5260.     char buf[BUFSZ];
5261. 
5262.     buf[0] = '\0';
5263.     if (near_capacity() >= HVY_ENCUMBER) {
5264.         Strcpy(buf, "You're too strained to do that.");
5265.     } else if ((nohands(gy.youmonst.data) && !webmaker(gy.youmonst.data))
5266.                || !gy.youmonst.data->mmove) {
5267.         Strcpy(buf, "And just how do you expect to do that?");
5268.     } else if (u.ustuck && sticks(gy.youmonst.data)) {
5269.         Sprintf(buf, "You'll have to let go of %s first.", mon_nam(u.ustuck));
5270.     } else if (u.ustuck || (welded(uwep) && bimanual(uwep))) {
5271.         Sprintf(buf, "Your %s seem to be too busy for that.",
5272.                 makeplural(body_part(HAND)));
5273.     } else if (check_floor && !can_reach_floor(FALSE)) {
5274.         /* only checked here for autounlock of chest/box and that will
5275.            be !verbosely so precise details of the message don't matter */
5276.         Sprintf(buf, "You can't reach the %s.", surface(u.ux, u.uy));
5277.     }
5278.     if (buf[0]) {
5279.         if (verbosely)
5280.             pline("%s", buf);
5281.         return 0;
5282.     }
5283.     return 1;
5284. }
5285. 
5286. /* Probability of disabling a trap.  Helge Hafting;
5287.    Returns 0 for success, non-0 for failure. */
5288. staticfn int
5289. untrap_prob(
5290.     struct trap *ttmp) /* must not be Null */
5291. {
5292.     int chance = 3;
5293. 
5294.     /* non-spiders are less adept at dealing with webs */
5295.     if (ttmp->ttyp == WEB) {
5296.         /* this assumes that all fiery artifacts are blades; no need to
5297.            make it more complicated unless/until that changes */
5298.         struct obj *wep = (uwep && is_blade(uwep)) ? uwep
5299.                           : (uswapwep && u.twoweap && is_blade(uswapwep))
5300.                             ? uswapwep : NULL;
5301. 
5302.         /* FIXME? Forcefight of adjacent web works with bare-handed and
5303.            martial arts but #untrap of same resorts to !webmaker() chance */
5304.         if (wep && !m_at(ttmp->tx, ttmp->ty)) {
5305.             /* primary or secondary weapon is a blade (which includes
5306.                daggers but not axes or bladed polearms) */
5307.             if (u_wield_art(ART_STING) || attacks(AD_FIRE, wep))
5308.                 chance = 1;
5309.             /* else chance stays 3 */
5310.         } else if (!webmaker(gy.youmonst.data)) {
5311.             chance = 7; /* 5.0: used to be 30 */
5312.         }
5313.     }
5314.     if (Confusion || Hallucination)
5315.         chance++;
5316.     if (Blind)
5317.         chance++;
5318.     if (Stunned)
5319.         chance += 2;
5320.     if (Fumbling)
5321.         chance *= 2;
5322.     /* Your own traps are better known than others. */
5323.     if (ttmp->madeby_u)
5324.         chance--;
5325.     if (Role_if(PM_RANGER) && ttmp->ttyp == BEAR_TRAP && chance <= 3)
5326.         return 0; /* always succeeds */
5327.     if (Role_if(PM_ROGUE)) {
5328.         if (rn2(2 * MAXULEV) < u.ulevel)
5329.             chance--;
5330.         if (u.uhave.questart && chance > 1)
5331.             chance--;
5332.     } else if (Role_if(PM_RANGER) && chance > 1)
5333.         chance--;
5334.     if (chance < 1)
5335.         chance = 1;
5336.     return rn2(chance);
5337. }
5338. 
5339. /* Replace trap with object(s).  Helge Hafting */
5340. void
5341. cnv_trap_obj(
5342.     int otyp,
5343.     int cnt,
5344.     struct trap *ttmp,
5345.     boolean bury_it)
5346. {
5347.     struct obj *otmp = mksobj(otyp, TRUE, FALSE);
5348.     struct monst *mtmp;
5349. 
5350.     otmp->quan = cnt;
5351.     otmp->owt = weight(otmp);
5352.     /* Only dart traps are capable of being poisonous */
5353.     if (otyp != DART)
5354.         otmp->opoisoned = 0;
5355.     place_object(otmp, ttmp->tx, ttmp->ty);
5356.     if (bury_it) {
5357.         /* magical digging first disarms this trap, then will unearth it */
5358.         (void) bury_an_obj(otmp, (boolean *) 0);
5359.     } else {
5360.         /* Sell your own traps only... */
5361.         if (ttmp->madeby_u)
5362.             sellobj(otmp, ttmp->tx, ttmp->ty);
5363.         stackobj(otmp);
5364.     }
5365.     newsym(ttmp->tx, ttmp->ty);
5366.     if (u.utrap && u_at(ttmp->tx, ttmp->ty))
5367.         reset_utrap(TRUE);
5368.     if (((mtmp = m_at(ttmp->tx, ttmp->ty)) != 0) && mtmp->mtrapped)
5369.         mtmp->mtrapped = 0;
5370.     deltrap(ttmp);
5371. }
5372. 
5373. /* whether moving to a trap location is moving "into" the trap or "onto" it */
5374. boolean
5375. into_vs_onto(int traptype)
5376. {
5377.     switch (traptype) {
5378.     case BEAR_TRAP:
5379.     case PIT:
5380.     case SPIKED_PIT:
5381.     case HOLE:
5382.     case TELEP_TRAP:
5383.     case LEVEL_TELEP:
5384.     case MAGIC_PORTAL:
5385.     case WEB:
5386.         return TRUE;
5387.     }
5388.     return FALSE;
5389. }
5390. 
5391. /* while attempting to disarm an adjacent trap, we've fallen into it */
5392. staticfn void
5393. move_into_trap(struct trap *ttmp)
5394. {
5395.     int bc = 0;
5396.     coordxy x = ttmp->tx, y = ttmp->ty, bx, by, cx, cy;
5397.     boolean unused;
5398. 
5399.     bx = by = cx = cy = 0; /* lint suppression */
5400.     /* we know there's no monster in the way and we're not trapped, but
5401.        need to make sure the move is not diagonally into or out of a
5402.        doorway; the sgn() calls are redundant since ttmp is adjacent */
5403.     if (test_move(u.ux, u.uy, sgn(x - u.ux), sgn(y - u.uy), TEST_MOVE)
5404.         && (!Punished
5405.             || drag_ball(x, y, &bc, &bx, &by, &cx, &cy, &unused, TRUE))) {
5406.         /* move hero and update map */
5407.         u.ux0 = u.ux, u.uy0 = u.uy;
5408.         /* set u.ux,u.uy and u.usteed->mx,my plus handle CLIPPING */
5409.         u_on_newpos(x, y);
5410.         u.umoved = TRUE;
5411.         newsym(u.ux0, u.uy0);
5412.         vision_recalc(1);
5413.         check_leash(u.ux0, u.uy0);
5414.         if (Punished)
5415.             move_bc(0, bc, bx, by, cx, cy);
5416.         /* marking the trap unseen forces dotrap() to treat it like a new
5417.            discovery and prevents pickup() -> look_here() -> check_here()
5418.            from giving a redundant "there is a <trap> here" message when
5419.            there are objects covering this trap */
5420.         ttmp->tseen = 0; /* hack for check_here() */
5421.         /* trigger the trap */
5422.         iflags.failing_untrap++; /* spoteffects() -> dotrap(,FAILEDUNTRAP) */
5423.         spoteffects(TRUE); /* pickup() + dotrap() */
5424.         iflags.failing_untrap--;
5425.         /* this should no longer be necessary; before the failing_untrap
5426.            hack, Flying hero would not trigger an unseen bear trap and
5427.            setting it not-yet-seen above resulted in leaving it hidden */
5428.         if ((ttmp = t_at(u.ux, u.uy)) != 0)
5429.             ttmp->tseen = 1;
5430.         exercise(A_WIS, FALSE);
5431.     } else {
5432.         /* caller has just printed "Whoops..." so if hero is prevented from
5433.            moving, a followup message is needed */
5434.         pline("Fortunately, you don't move %s it.",
5435.               into_vs_onto(ttmp->ttyp) ? "into" : "onto");
5436.     }
5437. }
5438. 
5439. /* 0: doesn't even try; 1: tries and fails; 2: succeeds */
5440. staticfn int
5441. try_disarm(
5442.     struct trap *ttmp,
5443.     boolean force_failure)
5444. {
5445.     struct monst *mtmp = m_at(ttmp->tx, ttmp->ty);
5446.     int ttype = ttmp->ttyp;
5447.     boolean under_u = (!u.dx && !u.dy);
5448.     boolean holdingtrap = (ttype == BEAR_TRAP || ttype == WEB);
5449. 
5450.     /* Test for monster first, monsters are displayed instead of trap. */
5451.     if (mtmp && (!mtmp->mtrapped || !holdingtrap)) {
5452.         pline("%s is in the way.", Monnam(mtmp));
5453.         return 0;
5454.     }
5455.     /* We might be forced to move onto the trap's location. */
5456.     if (sobj_at(BOULDER, ttmp->tx, ttmp->ty) && !Passes_walls && !under_u) {
5457.         There("is a boulder in your way.");
5458.         return 0;
5459.     }
5460.     /* duplicate tight-space checks from test_move */
5461.     if (u.dx && u.dy && bad_rock(gy.youmonst.data, u.ux, ttmp->ty)
5462.         && bad_rock(gy.youmonst.data, ttmp->tx, u.uy)) {
5463.         if ((gi.invent && (inv_weight() + weight_cap() > WT_TOOMUCH_DIAGONAL))
5464.             || bigmonst(gy.youmonst.data)) {
5465.             /* don't allow untrap if they can't get thru to it */
5466.             You("are unable to reach the %s!", trapname(ttype, FALSE));
5467.             return 0;
5468.         }
5469.     }
5470.     /* untrappable traps are located on the ground. */
5471.     if (!can_reach_floor(under_u)) {
5472.         if (u.usteed && P_SKILL(P_RIDING) < P_BASIC)
5473.             rider_cant_reach();
5474.         else
5475.             You("are unable to reach the %s!", trapname(ttype, FALSE));
5476.         return 0;
5477.     }
5478. 
5479.     /* Will our hero succeed? */
5480.     if (force_failure || untrap_prob(ttmp)) {
5481.         if (rnl(5)) {
5482.             pline("Whoops...");
5483.             if (mtmp) { /* must be a trap that holds monsters */
5484.                 if (ttype == BEAR_TRAP) {
5485.                     if (mtmp->mtame)
5486.                         abuse_dog(mtmp);
5487.                     mtmp->mhp -= rnd(4);
5488.                     if (DEADMONSTER(mtmp))
5489.                         killed(mtmp);
5490.                 } else if (ttype == WEB) {
5491.                     struct trap *ttmp2 = t_at(u.ux, u.uy);
5492. 
5493.                     if (!webmaker(gy.youmonst.data)
5494.                         /* don't always try to spread the web */
5495.                         && !rn2(3)
5496.                         /* is there already a trap at hero's spot?
5497.                            if so, don't clobber it with spreading web */
5498.                         && (ttmp2
5499.                             ? (ttmp2->ttyp == WEB)
5500.                             /* make a new web to trap hero in */
5501.                             : (ttmp2 = maketrap(u.ux, u.uy, WEB)) != 0)) {
5502.                         pline_The("web sticks to you.  You're caught too!");
5503.                         dotrap(ttmp2, NOWEBMSG);
5504.                         if (u.usteed && u.utrap) {
5505.                             /* you, not steed, are trapped */
5506.                             dismount_steed(DISMOUNT_FELL);
5507.                         }
5508.                     }
5509.                     if (mtmp->mtrapped)
5510.                         pline("%s remains entangled.", Monnam(mtmp));
5511.                 }
5512.             } else if (under_u) {
5513.                 /* [don't need the iflags.failing_untrap hack here] */
5514.                 dotrap(ttmp, FAILEDUNTRAP);
5515.             } else {
5516.                 move_into_trap(ttmp);
5517.             }
5518.         } else {
5519.             pline("%s %s is difficult to %s.",
5520.                   ttmp->madeby_u ? "Your" : under_u ? "This" : "That",
5521.                   trapname(ttype, FALSE),
5522.                   (ttype == WEB) ? "remove" : "disarm");
5523.         }
5524.         return 1;
5525.     }
5526.     return 2;
5527. }
5528. 
5529. staticfn void
5530. reward_untrap(struct trap *ttmp, struct monst *mtmp)
5531. {
5532.     if (!ttmp->madeby_u) {
5533.         if (rnl(10) < 8 && !mtmp->mpeaceful && !helpless(mtmp)
5534.             && !mtmp->mfrozen && !mindless(mtmp->data)
5535.             && !unique_corpstat(mtmp->data)
5536.             && mtmp->data->mlet != S_HUMAN) {
5537.             mtmp->mpeaceful = 1;
5538.             set_malign(mtmp); /* reset alignment */
5539.             pline("%s is grateful.", Monnam(mtmp));
5540.         }
5541.         /* Helping someone out of a trap is a nice thing to do.
5542.            A lawful may be rewarded, but not too often.  */
5543.         if (!rn2(3) && !rnl(8) && u.ualign.type == A_LAWFUL) {
5544.             adjalign(1);
5545.             You_feel("that you did the right thing.");
5546.         }
5547.     }
5548. }
5549. 
5550. /* Help a monster out of a bear trap or web, or if no monster is
5551.    present, disarm a bear trap or destroy a web.  Helge Hafting */
5552. staticfn int
5553. disarm_holdingtrap(struct trap *ttmp)
5554. {
5555.     struct monst *mtmp;
5556.     const char *which = the_your[ttmp->madeby_u];
5557.     int fails = try_disarm(ttmp, FALSE);
5558. 
5559.     if (fails < 2)
5560.         return fails;
5561. 
5562.     /* ok, disarm it. */
5563. 
5564.     /* untrap the monster, if any.
5565.        There's no need for a cockatrice test, only the trap is touched */
5566.     if ((mtmp = m_at(ttmp->tx, ttmp->ty)) != 0) {
5567.         mtmp->mtrapped = 0;
5568.         You("extract %s from %s %s.", mon_nam(mtmp),
5569.             which, (ttmp->ttyp == BEAR_TRAP) ? "bear trap" : "web");
5570.         reward_untrap(ttmp, mtmp);
5571.     } else if (ttmp->ttyp == BEAR_TRAP) {
5572.         You("disarm %s bear trap.", which);
5573.         cnv_trap_obj(BEARTRAP, 1, ttmp, FALSE);
5574.     } else if (ttmp->ttyp == WEB) {
5575.         struct obj *wep = (uwep && is_blade(uwep)) ? uwep
5576.                           : (uswapwep && u.twoweap && is_blade(uswapwep))
5577.                             ? uswapwep : NULL;
5578. 
5579.         if (wep && wep->oartifact
5580.             && (u_wield_art(ART_STING) || attacks(AD_FIRE, wep)))
5581.             pline("%s %s through %s web!", bare_artifactname(uwep),
5582.                   u_wield_art(ART_STING) ? "cuts" : "burns", which);
5583.         else if (wep)
5584.             You("cut through %s web.", which);
5585.         else
5586.             You("succeed in removing %s web.", which);
5587.         deltrap(ttmp);
5588.     }
5589.     newsym(u.ux + u.dx, u.uy + u.dy);
5590.     return 1;
5591. }
5592. 
5593. staticfn int
5594. disarm_landmine(struct trap *ttmp) /* Helge Hafting */
5595. {
5596.     int fails = try_disarm(ttmp, FALSE);
5597. 
5598.     if (fails < 2)
5599.         return fails;
5600.     You("disarm %s land mine.", the_your[ttmp->madeby_u]);
5601.     cnv_trap_obj(LAND_MINE, 1, ttmp, FALSE);
5602.     return 1;
5603. }
5604. 
5605. /* getobj callback for object to disarm a squeaky board with */
5606. staticfn int
5607. unsqueak_ok(struct obj *obj)
5608. {
5609.     if (!obj)
5610.         return GETOBJ_EXCLUDE;
5611. 
5612.     if (obj->otyp == CAN_OF_GREASE)
5613.         return GETOBJ_SUGGEST;
5614. 
5615.     if (obj->otyp == POT_OIL && obj->dknown
5616.         && objects[POT_OIL].oc_name_known)
5617.         return GETOBJ_SUGGEST;
5618. 
5619.     /* downplay all other potions, including unidentified oil
5620.      * Potential extension: if oil is known, skip this and exclude all other
5621.      * potions. */
5622.     if (obj->oclass == POTION_CLASS)
5623.         return GETOBJ_DOWNPLAY;
5624. 
5625.     return GETOBJ_EXCLUDE;
5626. }
5627. 
5628. /* it may not make much sense to use grease on floor boards, but so what? */
5629. staticfn int
5630. disarm_squeaky_board(struct trap *ttmp)
5631. {
5632.     struct obj *obj;
5633.     boolean bad_tool;
5634.     int fails;
5635. 
5636.     obj = getobj("untrap with", unsqueak_ok, GETOBJ_PROMPT);
5637.     if (!obj)
5638.         return 0;
5639. 
5640.     bad_tool = (obj->cursed
5641.                 || ((obj->otyp != POT_OIL || obj->lamplit)
5642.                     && (obj->otyp != CAN_OF_GREASE || !obj->spe)));
5643.     fails = try_disarm(ttmp, bad_tool);
5644.     if (fails < 2)
5645.         return fails;
5646. 
5647.     /* successfully used oil or grease to fix squeaky board */
5648.     if (obj->otyp == CAN_OF_GREASE) {
5649.         consume_obj_charge(obj, TRUE);
5650.     } else {
5651.         useup(obj); /* oil */
5652.         makeknown(POT_OIL);
5653.     }
5654.     You("repair the squeaky board."); /* no madeby_u */
5655.     deltrap(ttmp);
5656.     newsym(u.ux + u.dx, u.uy + u.dy);
5657.     more_experienced(1, 5);
5658.     newexplevel();
5659.     return 1;
5660. }
5661. 
5662. /* removes traps that shoot arrows, darts, etc. */
5663. staticfn int
5664. disarm_shooting_trap(struct trap *ttmp, int otyp)
5665. {
5666.     int fails = try_disarm(ttmp, FALSE);
5667. 
5668.     if (fails < 2)
5669.         return fails;
5670.     You("disarm %s trap.", the_your[ttmp->madeby_u]);
5671.     cnv_trap_obj(otyp, 50 - rnl(50), ttmp, FALSE);
5672.     return 1;
5673. }
5674. 
5675. /* trying to #untrap a monster from a pit; is the weight too heavy? */
5676. staticfn int
5677. try_lift(
5678.     struct monst *mtmp, /* trapped monster */
5679.     struct trap *ttmp, /* pit, possibly made by hero, or spiked pit */
5680.     int xtra_wt, /* monster (corpse weight) + (stuff ? minvent weight : 0) */
5681.     boolean stuff) /* False: monster w/o minvent; True: w/ minvent */
5682. {
5683.     if (calc_capacity(xtra_wt) >= HVY_ENCUMBER) {
5684.         pline("%s is %s for you to lift.", Monnam(mtmp),
5685.               stuff ? "carrying too much" : "too heavy");
5686.         if (!ttmp->madeby_u && !mtmp->mpeaceful && mtmp->mcanmove
5687.             && !mindless(mtmp->data) && mtmp->data->mlet != S_HUMAN
5688.             && rnl(10) < 3) {
5689.             mtmp->mpeaceful = 1;
5690.             set_malign(mtmp); /* reset alignment */
5691.             pline("%s thinks it was nice of you to try.", Monnam(mtmp));
5692.         }
5693.         return 0;
5694.     }
5695.     return 1;
5696. }
5697. 
5698. /* Help trapped monster (out of a (spiked) pit) */
5699. staticfn int
5700. help_monster_out(
5701.     struct monst *mtmp,
5702.     struct trap *ttmp)
5703. {
5704.     int xtra_wt;
5705.     struct obj *otmp;
5706.     boolean uprob;
5707. 
5708.     /*
5709.      * This works when levitating too -- consistent with the ability
5710.      * to hit monsters while levitating.
5711.      *
5712.      * Should perhaps check that our hero has arms/hands at the
5713.      * moment.  Helping can also be done by engulfing...
5714.      *
5715.      * Test the monster first - monsters are displayed before traps.
5716.      */
5717.     if (!mtmp->mtrapped) {
5718.         pline("%s isn't trapped.", Monnam(mtmp));
5719.         return 0;
5720.     }
5721.     /* Do you have the necessary capacity to lift anything? */
5722.     if (check_capacity((char *) 0))
5723.         return 1;
5724. 
5725.     /* Will our hero succeed? */
5726.     if ((uprob = untrap_prob(ttmp)) != 0 && !helpless(mtmp)) {
5727.         You("try to reach out your %s, but %s backs away skeptically.",
5728.             makeplural(body_part(ARM)), mon_nam(mtmp));
5729.         return 1;
5730.     }
5731. 
5732.     /* is it a cockatrice?... */
5733.     if (touch_petrifies(mtmp->data) && !uarmg && !Stone_resistance) {
5734.         const char *mtmp_pmname = mon_pmname(mtmp);
5735. 
5736.         You("grab the trapped %s using your bare %s.",
5737.             mtmp_pmname, makeplural(body_part(HAND)));
5738. 
5739.         if (poly_when_stoned(gy.youmonst.data) && polymon(PM_STONE_GOLEM)) {
5740.             display_nhwindow(WIN_MESSAGE, FALSE);
5741.         } else {
5742.             char kbuf[BUFSZ];
5743. 
5744.             Sprintf(kbuf, "trying to help %s out of a pit", an(mtmp_pmname));
5745.             instapetrify(kbuf);
5746.             return 1;
5747.         }
5748.     }
5749.     /* need to do cockatrice check first if sleeping or paralyzed */
5750.     if (uprob) {
5751.         You("try to grab %s, but cannot get a firm grasp.", mon_nam(mtmp));
5752.         if (mtmp->msleeping) {
5753.             mtmp->msleeping = 0;
5754.             pline("%s awakens.", Monnam(mtmp));
5755.         }
5756.         return 1;
5757.     }
5758. 
5759.     You("reach out your %s and grab %s.", makeplural(body_part(ARM)),
5760.         mon_nam(mtmp));
5761. 
5762.     if (mtmp->msleeping) {
5763.         mtmp->msleeping = 0;
5764.         pline("%s awakens.", Monnam(mtmp));
5765.     } else if (mtmp->mfrozen && !rn2(mtmp->mfrozen)) {
5766.         /* After such manhandling, perhaps the effect wears off */
5767.         mtmp->mcanmove = 1;
5768.         mtmp->mfrozen = 0;
5769.         pline("%s stirs.", Monnam(mtmp));
5770.     }
5771. 
5772.     /* is the monster too heavy? */
5773.     xtra_wt = mtmp->data->cwt;
5774.     if (!try_lift(mtmp, ttmp, xtra_wt, FALSE))
5775.         return 1;
5776. 
5777.     /* monster without its inventory isn't too heavy; if it carries
5778.        anything, include that minvent weight and check again */
5779.     if (mtmp->minvent) {
5780.         for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
5781.             xtra_wt += otmp->owt;
5782.         if (!try_lift(mtmp, ttmp, xtra_wt, TRUE))
5783.             return 1;
5784.     }
5785. 
5786.     You("pull %s out of the pit.", mon_nam(mtmp));
5787.     mtmp->mtrapped = 0;
5788.     reward_untrap(ttmp, mtmp);
5789.     fill_pit(mtmp->mx, mtmp->my);
5790.     return 1;
5791. }
5792. 
5793. staticfn void
5794. disarm_box(struct obj *box, boolean force, boolean confused)
5795. {
5796.     if (box->otrapped) {
5797.         int ch = ACURR(A_DEX) + u.ulevel;
5798. 
5799.         if (Role_if(PM_ROGUE))
5800.             ch *= 2;
5801.         if (!force && (confused || Fumbling
5802.                        || rnd(75 + level_difficulty() / 2) > ch)) {
5803.             (void) chest_trap(box, FINGER, TRUE);
5804.             /* 'box' might be gone now */
5805.         } else {
5806.             You("disarm it!");
5807.             box->otrapped = 0;
5808.             box->tknown = 1;
5809.             more_experienced(8, 0);
5810.             newexplevel();
5811.         }
5812.         exercise(A_DEX, TRUE);
5813.     } else {
5814.         pline("That %s was not trapped.", xname(box));
5815.         box->tknown = 0;
5816.     }
5817. }
5818. 
5819. /* check a particular container for a trap and optionally disarm it */
5820. staticfn void
5821. untrap_box(
5822.     struct obj *box,
5823.     boolean force,
5824.     boolean confused)
5825. {
5826.     if ((box->otrapped
5827.          && (force || (!confused && rn2(MAXULEV + 1 - u.ulevel) < 10)))
5828.         || box->tknown
5829.         || (!force && confused && !rn2(3))) {
5830.         if (!(box->tknown && box->dknown))
5831.             You("find a trap on %s!", the(xname(box)));
5832.         else
5833.             pline("There's a trap on %s.", the(xname(box)));
5834.         box->tknown = 1;
5835.         observe_object(box);
5836.         if (!confused)
5837.             exercise(A_WIS, TRUE);
5838. 
5839.         if (ynq("Disarm it?") == 'y')
5840.             disarm_box(box, force, confused);
5841.     } else {
5842.         You("find no traps on %s.", the(xname(box)));
5843.     }
5844. }
5845. 
5846. /* hero is able to attempt untrap, so do so */
5847. int
5848. untrap(
5849.     boolean force,
5850.     coordxy rx, coordxy ry,
5851.     struct obj *container)
5852. {
5853.     struct obj *otmp;
5854.     coordxy x, y;
5855.     int ch;
5856.     struct trap *ttmp;
5857.     struct monst *mtmp;
5858.     const char *trapdescr;
5859.     boolean here, useplural, deal_with_floor_trap,
5860.             confused = (Confusion || Hallucination),
5861.             trap_skipped = FALSE, autounlock_door = FALSE;
5862.     int boxcnt = 0;
5863.     char the_trap[BUFSZ], qbuf[QBUFSZ];
5864. 
5865.     /* 'force' is true for #invoke; if carrying MKoT, make it be true
5866.        for #untrap or autounlock */
5867.     if (!force && has_magic_key(&gy.youmonst))
5868.         force = TRUE;
5869. 
5870.     if (!rx && !container) {
5871.         /* usual case */
5872.         if (!getdir((char *) 0))
5873.             return 0;
5874.         x = u.ux + u.dx;
5875.         y = u.uy + u.dy;
5876.     } else {
5877.         /* autounlock's untrap; skip most prompting */
5878.         if (container) {
5879.             untrap_box(container, force, confused);
5880.             return 1;
5881.         }
5882.         /* levl[rx][ry] is a locked or trapped door */
5883.         x = rx, y = ry;
5884.         autounlock_door = TRUE;
5885.     }
5886.     if (!isok(x, y)) {
5887.         pline_The("perils lurking there are beyond your grasp.");
5888.         return 0;
5889.     }
5890. 
5891.     ttmp = t_at(x, y);
5892.     if (ttmp && !ttmp->tseen)
5893.         ttmp = 0;
5894.     trapdescr = ttmp ? trapname(ttmp->ttyp, FALSE) : 0;
5895.     here = u_at(x, y); /* !u.dx && !u.dy */
5896. 
5897.     if (here) /* are there are one or more containers here? */
5898.         for (otmp = svl.level.objects[x][y]; otmp; otmp = otmp->nexthere)
5899.             if (Is_box(otmp)) {
5900.                 if (++boxcnt > 1)
5901.                     break;
5902.             }
5903. 
5904.     deal_with_floor_trap = can_reach_floor(FALSE);
5905.     if (autounlock_door) {
5906.         ; /* skip a bunch */
5907.     } else if (!deal_with_floor_trap) {
5908.         *the_trap = '\0';
5909.         if (ttmp)
5910.             Strcat(the_trap, an(trapdescr));
5911.         if (ttmp && boxcnt)
5912.             Strcat(the_trap, " and ");
5913.         if (boxcnt)
5914.             Strcat(the_trap, (boxcnt == 1) ? "a container" : "containers");
5915.         useplural = ((ttmp && boxcnt > 0) || boxcnt > 1);
5916.         /* note: boxcnt and useplural will always be 0 for !here case */
5917.         if (ttmp || boxcnt)
5918.             There("%s %s %s but you can't reach %s%s.",
5919.                   useplural ? "are" : "is", the_trap, here ? "here" : "there",
5920.                   useplural ? "them" : "it",
5921.                   u.usteed ? " while mounted" : "");
5922.         trap_skipped = (ttmp != 0);
5923.     } else { /* deal_with_floor_trap */
5924. 
5925.         if (ttmp) {
5926.             Strcpy(the_trap, the(trapdescr));
5927.             if (boxcnt) {
5928.                 if (is_pit(ttmp->ttyp)) {
5929.                     You_cant("do much about %s%s.", the_trap,
5930.                              u.utrap ? " that you're stuck in"
5931.                                      : " while standing on the edge of it");
5932.                     trap_skipped = TRUE;
5933.                     deal_with_floor_trap = FALSE;
5934.                 } else {
5935.                     Snprintf(qbuf, sizeof(qbuf),
5936.                              "There %s and %s here.  %s %s?",
5937.                              (boxcnt == 1) ? "is a container"
5938.                                            : "are containers",
5939.                              an(trapdescr),
5940.                              (ttmp->ttyp == WEB) ? "Remove"
5941.                                                  : "Disarm",
5942.                              the_trap);
5943.                     switch (ynq(qbuf)) {
5944.                     case 'q':
5945.                         return 0;
5946.                     case 'n':
5947.                         trap_skipped = TRUE;
5948.                         deal_with_floor_trap = FALSE;
5949.                         break;
5950.                     }
5951.                 }
5952.             }
5953.             if (deal_with_floor_trap) {
5954.                 if (u.utrap) {
5955.                     You("cannot deal with %s while trapped%s!", the_trap,
5956.                         u_at(x, y) ? " in it" : "");
5957.                     return 1;
5958.                 }
5959.                 if ((mtmp = m_at(x, y)) != 0
5960.                     && (M_AP_TYPE(mtmp) == M_AP_FURNITURE
5961.                         || M_AP_TYPE(mtmp) == M_AP_OBJECT)) {
5962.                     stumble_onto_mimic(mtmp);
5963.                     return 1;
5964.                 }
5965.                 switch (ttmp->ttyp) {
5966.                 case BEAR_TRAP:
5967.                 case WEB:
5968.                     return disarm_holdingtrap(ttmp);
5969.                 case LANDMINE:
5970.                     return disarm_landmine(ttmp);
5971.                 case SQKY_BOARD:
5972.                     return disarm_squeaky_board(ttmp);
5973.                 case DART_TRAP:
5974.                     return disarm_shooting_trap(ttmp, DART);
5975.                 case ARROW_TRAP:
5976.                     return disarm_shooting_trap(ttmp, ARROW);
5977.                 case PIT:
5978.                 case SPIKED_PIT:
5979.                     if (here) {
5980.                         You("are already on the edge of the pit.");
5981.                         return 0;
5982.                     }
5983.                     if (!mtmp) {
5984.                         pline("Try filling the pit instead.");
5985.                         return 0;
5986.                     }
5987.                     return help_monster_out(mtmp, ttmp);
5988.                 default:
5989.                     You("cannot disable %s trap.", !here ? "that" : "this");
5990.                     return 0;
5991.                 }
5992.             }
5993.         } /* end if */
5994. 
5995.         if (boxcnt) {
5996.             /* 5.0: this used to allow searching for traps on multiple
5997.                containers on the same move and needed to keep track of
5998.                whether any had been found but not attempted to untrap;
5999.                now at most one per move may be checked and we only
6000.                continue on to door handling if they are all declined */
6001.             for (otmp = svl.level.objects[x][y]; otmp; otmp = otmp->nexthere) {
6002.                 if (!Is_box(otmp))
6003.                     continue;
6004.                 if (otmp->tknown && otmp->dknown)
6005.                     (void) safe_qbuf(qbuf, "Disarm this ", NULL,
6006.                                      otmp, xname, ansimpleoname, "a box");
6007.                 else
6008.                     (void) safe_qbuf(qbuf, "There is ",
6009.                                      " here.  Check it for traps?", otmp,
6010.                                      doname, ansimpleoname, "a box");
6011.                 switch (ynq(qbuf)) {
6012.                     case 'q':
6013.                         return 0;
6014.                     case 'y':
6015.                         if (otmp->tknown && otmp->dknown)
6016.                             disarm_box(otmp, force, confused);
6017.                         else
6018.                             untrap_box(otmp, force, confused);
6019.                         return 1; /* even for 'no' at "Disarm it?" prompt */
6020.                     }
6021.                     /* 'n' => continue to next box */
6022.             }
6023.             There("are no other chests or boxes here.");
6024.         }
6025. 
6026.         if (stumble_on_door_mimic(x, y))
6027.             return 1;
6028.     } /* deal_with_floor_trap */
6029. 
6030.     /*
6031.      * Doors can be manipulated even while levitating/unskilled riding.
6032.      *
6033.      * Ordinarily there won't be a closed or locked door at the same
6034.      * location as a floor trap or a container.  However, there could
6035.      * be a container at a closed/locked door spot if it was dropped
6036.      * there by a monster or poly'd hero with Passes_walls capability,
6037.      * and poly'd hero could move onto that spot and attempt #untrap
6038.      * in direction '.' or '>'.  We'll get here for that situation if
6039.      * player declines to check all containers for traps.
6040.      *
6041.      * The usual situation is #untrap toward an adjacent closed door.
6042.      * No floor trap would be present and any containers would be
6043.      * ignored because they're only checked when direction is '.'/'>'.
6044.      */
6045.     if (!IS_DOOR(levl[x][y].typ)) {
6046.         if (!trap_skipped)
6047.             You("know of no traps there.");
6048.         return 0;
6049.     }
6050. 
6051.     switch (levl[x][y].doormask) {
6052.     case D_NODOOR:
6053.         You("%s no door there.", Blind ? "feel" : "see");
6054.         return 0;
6055.     case D_ISOPEN:
6056.         pline("This door is safely open.");
6057.         return 0;
6058.     case D_BROKEN:
6059.         pline("This door is broken.");
6060.         return 0;
6061.     }
6062. 
6063.     if (((levl[x][y].doormask & D_TRAPPED) != 0
6064.          && (force || (!confused && rn2(MAXULEV - u.ulevel + 11) < 10)))
6065.         || (!force && confused && !rn2(3))) {
6066.         You("find a trap on the door!");
6067.         exercise(A_WIS, TRUE);
6068.         if (ynq("Disarm it?") != 'y')
6069.             return 1;
6070.         if (levl[x][y].doormask & D_TRAPPED) {
6071.             ch = 15 + (Role_if(PM_ROGUE) ? u.ulevel * 3 : u.ulevel);
6072.             exercise(A_DEX, TRUE);
6073.             if (!force && (confused || Fumbling
6074.                            || rnd(75 + level_difficulty() / 2) > ch)) {
6075.                 You("set it off!");
6076.                 b_trapped("door", FINGER);
6077.                 levl[x][y].doormask = D_NODOOR;
6078.                 unblock_point(x, y);
6079.                 newsym(x, y);
6080.                 /* (probably ought to charge for this damage...) */
6081.                 if (*in_rooms(x, y, SHOPBASE))
6082.                     add_damage(x, y, 0L);
6083.             } else {
6084.                 You("disarm it!");
6085.                 levl[x][y].doormask &= ~D_TRAPPED;
6086.                 more_experienced(8, 0);
6087.                 newexplevel();
6088.             }
6089.         } else
6090.             pline("This door was not trapped.");
6091.         return 1;
6092.     } else {
6093.         You("find no traps on the door.");
6094.         return 1;
6095.     }
6096. }
6097. 
6098. /* for magic unlocking; returns true if targeted monster (which might
6099.    be hero) gets untrapped; the trap remains intact */
6100. boolean
6101. openholdingtrap(
6102.     struct monst *mon,
6103.     boolean *noticed) /* set to true iff hero notices the effect;
6104.                        * otherwise left with its previous value intact */
6105. {
6106.     struct trap *t, tdummy;
6107.     char buf[BUFSZ], whichbuf[20];
6108.     const char *trapdescr = 0, *which = 0;
6109.     boolean ishero = (mon == &gy.youmonst);
6110. 
6111.     if (!mon)
6112.         return FALSE;
6113.     if (mon == u.usteed)
6114.         ishero = TRUE;
6115. 
6116.     t = t_at(ishero ? u.ux : mon->mx, ishero ? u.uy : mon->my);
6117. 
6118.     if (ishero && u.utrap) { /* all u.utraptype values are holding traps */
6119.         /* there might not be any trap at hero's spot for tt_buriedball;
6120.            conversely, there might be an unrelated trap at that spot */
6121.         if (!t) {
6122.             t = &tdummy;
6123.             (void) memset(t, 0, sizeof *t), t->ntrap = NULL;
6124.             /* fallback 't' is now nonNull, t->tseen and t->madeby_u are 0 */
6125.         }
6126.         which = the_your[(!t || !t->tseen || !t->madeby_u) ? 0 : 1];
6127. 
6128.         switch (u.utraptype) {
6129.         case TT_LAVA:
6130.             trapdescr = "molten lava";
6131.             break;
6132.         case TT_INFLOOR:
6133.             /* solidified lava, so not "floor" even if within a room */
6134.             trapdescr = "ground";
6135.             break;
6136.         case TT_BURIEDBALL:
6137.             trapdescr = "your anchor";
6138.             which = "";
6139.             break;
6140.         case TT_BEARTRAP:
6141.         case TT_PIT:
6142.         case TT_WEB:
6143.             trapdescr = defsyms[(u.utraptype == TT_WEB) ? S_web
6144.                                 : (u.utraptype == TT_PIT) ? S_pit
6145.                                   : S_bear_trap].explanation;
6146.             break;
6147.         default:
6148.             /* lint suppression in case 't' is unexpectedly Null
6149.                or u.utraptype has new value we don't know about yet */
6150.             trapdescr = "trap";
6151.             break;
6152.         }
6153.     } else {
6154.         /* if no trap here or it's not a holding trap, we're done */
6155.         if (!t || (t->ttyp != BEAR_TRAP && t->ttyp != WEB))
6156.             return FALSE;
6157.         trapdescr = trapname(t->ttyp, FALSE);
6158.     }
6159.     assert(t != NULL);
6160.     if (!which)
6161.         which = t->tseen ? the_your[t->madeby_u]
6162.                          : strchr(vowels, *trapdescr) ? "an" : "a";
6163.     assert(which != 0);
6164.     if (*which)
6165.         which = strcat(strcpy(whichbuf, which), " ");
6166. 
6167.     if (ishero) {
6168.         if (!u.utrap)
6169.             return FALSE;
6170.         *noticed = TRUE;
6171.         if (!u.usteed)
6172.             Strcpy(buf, "You are");
6173.         else if (u.utraptype == TT_BURIEDBALL)
6174.             Sprintf(buf, "You and %s are", y_monnam(u.usteed));
6175.         else
6176.             Sprintf(buf, "%s is", noit_Monnam(u.usteed));
6177.         /* give release message before untrap in case it triggers a message */
6178.         pline("%s released from %s%s.", buf, which, trapdescr);
6179.         /* might float up if Levitation is being unblocked */
6180.         gv.vision_full_recalc = 1; /* vision limits can change (pit escape) */
6181.         reset_utrap(TRUE);
6182.         if (gv.vision_full_recalc)
6183.             vision_recalc(0);
6184.     } else {
6185.         if (!mon->mtrapped)
6186.             return FALSE;
6187.         mon->mtrapped = 0;
6188.         if (canspotmon(mon)) {
6189.             *noticed = TRUE;
6190.             pline("%s is released from %s%s.", Monnam(mon), which,
6191.                   trapdescr);
6192.         } else if (cansee(t->tx, t->ty) && t->tseen) {
6193.             *noticed = TRUE;
6194.             if (t->ttyp == WEB)
6195.                 pline("%s is released from %s%s.", Something, which,
6196.                       trapdescr);
6197.             else /* BEAR_TRAP */
6198.                 pline("%s%s opens.", upstart(strcpy(buf, which)), trapdescr);
6199.         }
6200.         /* might pacify monster if adjacent */
6201.         if (rn2(2) && m_next2u(mon))
6202.             reward_untrap(t, mon);
6203.     }
6204.     return TRUE;
6205. }
6206. 
6207. /* for magic locking; returns true if targeted monster (which might
6208.    be hero) gets hit by a trap (might avoid actually becoming trapped) */
6209. boolean
6210. closeholdingtrap(
6211.     struct monst *mon,
6212.     boolean *noticed) /* set to true iff hero notices the effect;
6213.                        * otherwise left with its previous value intact */
6214. {
6215.     struct trap *t;
6216.     unsigned dotrapflags;
6217.     boolean ishero = (mon == &gy.youmonst), result;
6218. 
6219.     if (!mon)
6220.         return FALSE;
6221.     if (mon == u.usteed)
6222.         ishero = TRUE;
6223.     t = t_at(ishero ? u.ux : mon->mx, ishero ? u.uy : mon->my);
6224.     /* if no trap here or it's not a holding trap, we're done */
6225.     if (!t || (t->ttyp != BEAR_TRAP && t->ttyp != WEB))
6226.         return FALSE;
6227. 
6228.     if (ishero) {
6229.         if (u.utrap)
6230.             return FALSE; /* already trapped */
6231.         *noticed = TRUE;
6232.         dotrapflags = FORCETRAP;
6233.         /* dotrap calls mintrap when mounted hero encounters a web */
6234.         if (u.usteed)
6235.             dotrapflags |= NOWEBMSG;
6236.         dotrap(t, dotrapflags | FORCETRAP);
6237.         result = (u.utrap != 0);
6238.     } else {
6239.         if (mon->mtrapped)
6240.             return FALSE; /* already trapped */
6241.         /* you notice it if you see the trap close/tremble/whatever
6242.            or if you sense the monster who becomes trapped */
6243.         *noticed = cansee(t->tx, t->ty) || canspotmon(mon);
6244.         result = (mintrap(mon, FORCETRAP) != Trap_Effect_Finished);
6245.     }
6246.     return result;
6247. }
6248. 
6249. /* for magic unlocking; returns true if targeted monster (which might
6250.    be hero) gets hit by a trap (target might avoid its effect) */
6251. boolean
6252. openfallingtrap(
6253.     struct monst *mon,
6254.     boolean trapdoor_only,
6255.     boolean *noticed) /* set to true iff hero notices the effect; */
6256. {                     /* otherwise left with its previous value intact */
6257.     struct trap *t;
6258.     boolean ishero = (mon == &gy.youmonst), result;
6259. 
6260.     if (!mon)
6261.         return FALSE;
6262.     if (mon == u.usteed)
6263.         ishero = TRUE;
6264.     t = t_at(ishero ? u.ux : mon->mx, ishero ? u.uy : mon->my);
6265.     /* if no trap here or it's not a falling trap, we're done
6266.        (note: falling rock traps have a trapdoor in the ceiling) */
6267.     if (!t || ((t->ttyp != TRAPDOOR && t->ttyp != ROCKTRAP)
6268.                && (trapdoor_only || (t->ttyp != HOLE && !is_pit(t->ttyp)))))
6269.         return FALSE;
6270. 
6271.     if (ishero) {
6272.         if (u.utrap)
6273.             return FALSE; /* already trapped */
6274.         *noticed = TRUE;
6275.         dotrap(t, FORCETRAP);
6276.         result = (u.utrap != 0);
6277.     } else {
6278.         if (mon->mtrapped)
6279.             return FALSE; /* already trapped */
6280.         /* you notice it if you see the trap close/tremble/whatever
6281.            or if you sense the monster who becomes trapped */
6282.         *noticed = cansee(t->tx, t->ty) || canspotmon(mon);
6283.         /* monster will be angered; mintrap doesn't handle that */
6284.         wakeup(mon, TRUE);
6285.         result = (mintrap(mon, FORCETRAP) != Trap_Effect_Finished);
6286.         /* mon might now be on the migrating monsters list */
6287.     }
6288.     return result;
6289. }
6290. 
6291. /* only called when the player is doing something to the chest directly;
6292.    returns True if chest is destroyed, False if it remains in play */
6293. boolean
6294. chest_trap(
6295.     struct obj *obj,
6296.     int bodypart,
6297.     boolean disarm)
6298. {
6299.     struct obj *otmp = obj, *otmp2;
6300.     char buf[80];
6301.     const char *msg;
6302.     coord cc;
6303. 
6304.     if (get_obj_location(obj, &cc.x, &cc.y, 0)) /* might be carried */
6305.         obj->ox = cc.x, obj->oy = cc.y;
6306. 
6307.     otmp->tknown = 0;   /* for xname(); will be set to 1 below */
6308.     otmp->otrapped = 0; /* trap is one-shot; clear flag first in case
6309.                          * chest kills you and ends up in bones file */
6310.     You(disarm ? "set it off!" : "trigger a trap!");
6311.     display_nhwindow(WIN_MESSAGE, FALSE);
6312.     if (Luck > -13 && rn2(13 + Luck) > 7) { /* saved by luck */
6313.         /* trap went off, but good luck prevents damage */
6314.         switch (rn2(13)) {
6315.         case 12:
6316.         case 11:
6317.             msg = "explosive charge is a dud";
6318.             break;
6319.         case 10:
6320.         case 9:
6321.             msg = "electric charge is grounded";
6322.             break;
6323.         case 8:
6324.         case 7:
6325.             msg = "flame fizzles out";
6326.             break;
6327.         case 6:
6328.         case 5:
6329.         case 4:
6330.             msg = "poisoned needle misses";
6331.             break;
6332.         case 3:
6333.         case 2:
6334.         case 1:
6335.         case 0:
6336.             msg = "gas cloud blows away";
6337.             break;
6338.         default:
6339.             impossible("chest disarm bug");
6340.             msg = (char *) 0;
6341.             break;
6342.         }
6343.         if (msg)
6344.             pline("But luckily the %s!", msg);
6345.     } else {
6346.         switch (rn2(20) ? ((Luck >= 13) ? 0 : rn2(13 - Luck)) : rn2(26)) {
6347.         case 25:
6348.         case 24:
6349.         case 23:
6350.         case 22:
6351.         case 21: {
6352.             struct monst *shkp = 0;
6353.             long loss = 0L;
6354.             boolean costly, insider, chestgone;
6355.             coordxy ox = obj->ox, oy = obj->oy;
6356. 
6357.             /* the obj location need not be that of player */
6358.             costly = (costly_spot(ox, oy)
6359.                       && (shkp = shop_keeper(*in_rooms(ox, oy, SHOPBASE)))
6360.                              != (struct monst *) 0);
6361.             insider = (*u.ushops && inside_shop(u.ux, u.uy)
6362.                        && *in_rooms(ox, oy, SHOPBASE) == *u.ushops);
6363. 
6364.             pline("%s!", Tobjnam(obj, "explode"));
6365.             Sprintf(buf, "exploding %s", xname(obj));
6366. 
6367.             if (costly)
6368.                 loss += stolen_value(obj, ox, oy, (boolean) shkp->mpeaceful,
6369.                                      TRUE);
6370.             delete_contents(obj);
6371.             /*
6372.              * Note:  the explosion is taking place at the chest's
6373.              * location, not necessarily at the hero's.  (Simplest
6374.              * case: kicking it from one step away and getting the
6375.              * chest_trap() outcome.)
6376.              */
6377.             /* unpunish() in advance if either ball or chain (or both)
6378.                is going to be destroyed */
6379.             if (Punished && ((uchain->ox == ox && uchain->oy == oy)
6380.                              || (uball->where == OBJ_FLOOR
6381.                                  && uball->ox == ox && uball->oy == oy)))
6382.                 unpunish();
6383.             /* destroy everything at the spot (the Amulet, the
6384.                invocation tools, and Rider corpses will remain intact);
6385.                usually the chest will be destroyed along with the stuff at
6386.                this spot, but not if it is being carried */
6387.             chestgone = FALSE;
6388.             for (otmp = svl.level.objects[ox][oy]; otmp; otmp = otmp2) {
6389.                 otmp2 = otmp->nexthere;
6390.                 if (costly)
6391.                     loss += stolen_value(otmp, otmp->ox, otmp->oy,
6392.                                          (boolean) shkp->mpeaceful, TRUE);
6393.                 if (otmp == obj)
6394.                     chestgone = TRUE;
6395.                 delobj(otmp);
6396.             }
6397.             wake_nearby(FALSE);
6398.             losehp(Maybe_Half_Phys(d(6, 6)), buf, KILLED_BY_AN);
6399.             exercise(A_STR, FALSE);
6400.             if (costly && loss) {
6401.                 if (insider) {
6402.                     You("owe %ld %s for objects destroyed.", loss,
6403.                         currency(loss));
6404.                 } else {
6405.                     You("caused %ld %s worth of damage!", loss,
6406.                         currency(loss));
6407.                     make_angry_shk(shkp, ox, oy);
6408.                 }
6409.             }
6410.             if (chestgone)
6411.                 return TRUE;
6412.             break; /* set tknown and return False */
6413.         } /* case 21 */
6414.         case 20:
6415.         case 19:
6416.         case 18:
6417.         case 17:
6418.             pline("A cloud of noxious gas billows from %s.", the(xname(obj)));
6419.             if (rn2(3))
6420.                 poisoned("gas cloud", A_STR, "cloud of poison gas", 15,
6421.                          FALSE);
6422.             else
6423.                 create_gas_cloud(obj->ox, obj->oy, 1, 8);
6424.             exercise(A_CON, FALSE);
6425.             break;
6426.         case 16:
6427.         case 15:
6428.         case 14:
6429.         case 13:
6430.             You_feel("a needle prick your %s.", body_part(bodypart));
6431.             poisoned("needle", A_CON, "poisoned needle", 10, FALSE);
6432.             exercise(A_CON, FALSE);
6433.             break;
6434.         case 12:
6435.         case 11:
6436.         case 10:
6437.         case 9:
6438.             dofiretrap(obj);
6439.             break;
6440.         case 8:
6441.         case 7:
6442.         case 6: {
6443.             int dmg = d(4, 4), orig_dmg = dmg;
6444. 
6445.             You("are jolted by a surge of electricity!");
6446.             if (Shock_resistance) {
6447.                 shieldeff(u.ux, u.uy);
6448.                 You("don't seem to be affected.");
6449.                 monstseesu(M_SEEN_ELEC);
6450.                 dmg = 0;
6451.             } else {
6452.                 monstunseesu(M_SEEN_ELEC);
6453.             }
6454.             (void) destroy_items(&gy.youmonst, AD_ELEC, orig_dmg);
6455.             if (dmg)
6456.                 losehp(dmg, "electric shock", KILLED_BY_AN);
6457.             break;
6458.         } /* case 6 */
6459.         case 5:
6460.         case 4:
6461.         case 3:
6462.             if (!Free_action) {
6463.                 pline("Suddenly you are frozen in place!");
6464.                 nomul(-d(5, 6));
6465.                 gm.multi_reason = "frozen by a trap";
6466.                 exercise(A_DEX, FALSE);
6467.                 gn.nomovemsg = You_can_move_again;
6468.             } else
6469.                 You("momentarily stiffen.");
6470.             break;
6471.         case 2:
6472.         case 1:
6473.         case 0:
6474.             pline("A cloud of %s gas billows from %s.",
6475.                   Blind ? ROLL_FROM(blindgas) : rndcolor(),
6476.                   the(xname(obj)));
6477.             if (!Stunned) {
6478.                 if (Hallucination)
6479.                     pline("What a groovy feeling!");
6480.                 else
6481.                     You("%s%s...", stagger(gy.youmonst.data, "stagger"),
6482.                         Halluc_resistance ? ""
6483.                                           : Blind ? " and get dizzy"
6484.                                                   : " and your vision blurs");
6485.             }
6486.             make_stunned((HStun & TIMEOUT) + (long) rn1(7, 16), FALSE);
6487.             (void) make_hallucinated(
6488.                 (HHallucination & TIMEOUT) + (long) rn1(5, 16), FALSE, 0L);
6489.             break;
6490.         default:
6491.             impossible("bad chest trap");
6492.             break;
6493.         }
6494.         bot(); /* to get immediate botl re-display */
6495.     }
6496. 
6497.     obj->tknown = 1; /* hero knows chest is no longer trapped */
6498.     return FALSE;
6499. }
6500. 
6501. struct trap *
6502. t_at(coordxy x, coordxy y)
6503. {
6504.     struct trap *trap = gf.ftrap;
6505. 
6506.     while (trap) {
6507.         if (trap->tx == x && trap->ty == y)
6508.             return trap;
6509.         trap = trap->ntrap;
6510.     }
6511.     return (struct trap *) 0;
6512. }
6513. 
6514. /* return number of traps of type ttyp on this level */
6515. int
6516. count_traps(int ttyp)
6517. {
6518.     int ret = 0;
6519.     struct trap *trap = gf.ftrap;
6520. 
6521.     while (trap) {
6522.         if ((int) trap->ttyp == ttyp)
6523.             ret++;
6524.         trap = trap->ntrap;
6525.     }
6526. 
6527.     return ret;
6528. }
6529. 
6530. void
6531. deltrap(struct trap *trap)
6532. {
6533.     struct trap *ttmp;
6534. 
6535.     clear_conjoined_pits(trap);
6536.     if (trap == gf.ftrap) {
6537.         gf.ftrap = gf.ftrap->ntrap;
6538.     } else {
6539.         for (ttmp = gf.ftrap; ttmp; ttmp = ttmp->ntrap)
6540.             if (ttmp->ntrap == trap)
6541.                 break;
6542.         if (!ttmp)
6543.             panic("deltrap: no preceding trap!");
6544.         ttmp->ntrap = trap->ntrap;
6545.     }
6546.     if (Sokoban && (trap->ttyp == PIT || trap->ttyp == HOLE))
6547.         maybe_finish_sokoban();
6548.     dealloc_trap(trap);
6549. }
6550. 
6551. boolean
6552. conjoined_pits(
6553.     struct trap *trap2,
6554.     struct trap *trap1,
6555.     boolean u_entering_trap2)
6556. {
6557.     coordxy dx, dy;
6558.     int diridx, adjidx;
6559. 
6560.     if (!trap1 || !trap2)
6561.         return FALSE;
6562.     if (!isok(trap2->tx, trap2->ty) || !isok(trap1->tx, trap1->ty)
6563.         || !is_pit(trap2->ttyp)
6564.         || !is_pit(trap1->ttyp)
6565.         || (u_entering_trap2 && !(u.utrap && u.utraptype == TT_PIT)))
6566.         return FALSE;
6567.     dx = sgn(trap2->tx - trap1->tx);
6568.     dy = sgn(trap2->ty - trap1->ty);
6569.     diridx = xytodir(dx, dy);
6570.     if (diridx != DIR_ERR) {
6571.         adjidx = DIR_180(diridx);
6572.         if ((trap1->conjoined & (1 << diridx))
6573.             && (trap2->conjoined & (1 << adjidx)))
6574.             return TRUE;
6575.     }
6576.     return FALSE;
6577. }
6578. 
6579. staticfn void
6580. clear_conjoined_pits(struct trap *trap)
6581. {
6582.     int diridx, adjidx;
6583.     coordxy x, y;
6584.     struct trap *t;
6585. 
6586.     if (trap && is_pit(trap->ttyp)) {
6587.         for (diridx = 0; diridx < N_DIRS; ++diridx) {
6588.             if (trap->conjoined & (1 << diridx)) {
6589.                 x = trap->tx + xdir[diridx];
6590.                 y = trap->ty + ydir[diridx];
6591.                 if (isok(x, y)
6592.                     && (t = t_at(x, y)) != 0
6593.                     && is_pit(t->ttyp)) {
6594.                     adjidx = DIR_180(diridx);
6595.                     t->conjoined &= ~(1 << adjidx);
6596.                 }
6597.                 trap->conjoined &= ~(1 << diridx);
6598.             }
6599.         }
6600.     }
6601. }
6602. 
6603. staticfn boolean
6604. adj_nonconjoined_pit(struct trap *adjtrap)
6605. {
6606.     struct trap *trap_with_u = t_at(u.ux0, u.uy0);
6607. 
6608.     if (trap_with_u && adjtrap && u.utrap && u.utraptype == TT_PIT
6609.         && is_pit(trap_with_u->ttyp) && is_pit(adjtrap->ttyp)) {
6610.         if (xytodir(u.dx, u.dy) != DIR_ERR)
6611.             return TRUE;
6612.     }
6613.     return FALSE;
6614. }
6615. 
6616. #if 0
6617. /*
6618.  * Mark all neighboring pits as conjoined pits.
6619.  * (currently not called from anywhere)
6620.  */
6621. staticfn void
6622. join_adjacent_pits(struct trap *trap)
6623. {
6624.     struct trap *t;
6625.     int diridx;
6626.     coordxy x, y;
6627. 
6628.     if (!trap)
6629.         return;
6630.     for (diridx = 0; diridx < N_DIRS; ++diridx) {
6631.         x = trap->tx + xdir[diridx];
6632.         y = trap->ty + ydir[diridx];
6633.         if (isok(x, y)) {
6634.             if ((t = t_at(x, y)) != 0 && is_pit(t->ttyp)) {
6635.                 trap->conjoined |= (1 << diridx);
6636.                 join_adjacent_pits(t);
6637.             } else
6638.                 trap->conjoined &= ~(1 << diridx);
6639.         }
6640.     }
6641. }
6642. #endif /*0*/
6643. 
6644. /*
6645.  * Returns TRUE if you escaped a pit and are standing on the precipice.
6646.  */
6647. boolean
6648. uteetering_at_seen_pit(struct trap *trap)
6649. {
6650.     return (trap && is_pit(trap->ttyp) && trap->tseen
6651.             && u_at(trap->tx, trap->ty)
6652.             && !(u.utrap && u.utraptype == TT_PIT));
6653. }
6654. 
6655. /*
6656.  * Returns TRUE if you didn't fall through a hole or didn't
6657.  * release a trap door
6658.  */
6659. boolean
6660. uescaped_shaft(struct trap *trap)
6661. {
6662.     return (trap && is_hole(trap->ttyp) && trap->tseen
6663.             && u_at(trap->tx, trap->ty));
6664. }
6665. 
6666. /* Destroy a trap that emanates from the floor. */
6667. boolean
6668. delfloortrap(struct trap *ttmp)
6669. {
6670.     /* some of these are arbitrary -dlc */
6671.     if (ttmp && ((ttmp->ttyp == SQKY_BOARD) || (ttmp->ttyp == BEAR_TRAP)
6672.                  || (ttmp->ttyp == LANDMINE) || (ttmp->ttyp == FIRE_TRAP)
6673.                  || is_pit(ttmp->ttyp)
6674.                  || is_hole(ttmp->ttyp)
6675.                  || (ttmp->ttyp == TELEP_TRAP) || (ttmp->ttyp == LEVEL_TELEP)
6676.                  || (ttmp->ttyp == WEB) || (ttmp->ttyp == MAGIC_TRAP)
6677.                  || (ttmp->ttyp == ANTI_MAGIC))) {
6678.         struct monst *mtmp;
6679. 
6680.         if (u_at(ttmp->tx, ttmp->ty)) {
6681.             if (u.utraptype != TT_BURIEDBALL)
6682.                 reset_utrap(TRUE);
6683.         } else if ((mtmp = m_at(ttmp->tx, ttmp->ty)) != 0) {
6684.             mtmp->mtrapped = 0;
6685.         }
6686.         deltrap(ttmp);
6687.         return TRUE;
6688.     }
6689.     return FALSE;
6690. }
6691. 
6692. /* used for doors (also tins).  can be used for anything else that opens. */
6693. void
6694. b_trapped(const char *item, int bodypart)
6695. {
6696.     int lvl = level_difficulty(),
6697.         dmg = rnd(5 + (lvl < 5 ? lvl : 2 + lvl / 2));
6698. 
6699.     Soundeffect(se_kaboom, 80);
6700.     pline("KABOOM!!  %s was booby-trapped!", The(item));
6701.     wake_nearby(FALSE);
6702.     losehp(Maybe_Half_Phys(dmg), "explosion", KILLED_BY_AN);
6703.     exercise(A_STR, FALSE);
6704.     if (bodypart != NO_PART)
6705.         exercise(A_CON, FALSE);
6706.     make_stunned((HStun & TIMEOUT) + (long) dmg, TRUE);
6707. }
6708. 
6709. /* Monster is hit by trap. */
6710. staticfn boolean
6711. thitm(
6712.     int tlev,          /* missile's attack level */
6713.     struct monst *mon, /* target */
6714.     struct obj *obj,   /* missile; might be Null */
6715.     int d_override,    /* non-zero: force hit for this amount of damage */
6716.     boolean nocorpse)  /* True: a trap is completely burning up the target */
6717. {
6718.     int strike;
6719.     boolean trapkilled = FALSE;
6720. 
6721.     if (d_override)
6722.         strike = 1;
6723.     else if (obj)
6724.         strike = (find_mac(mon) + tlev + obj->spe <= rnd(20));
6725.     else
6726.         strike = (find_mac(mon) + tlev <= rnd(20));
6727. 
6728.     /* Actually more accurate than thitu, which doesn't take
6729.      * obj->spe into account.
6730.      */
6731.     if (!strike) {
6732.         if (obj && cansee(mon->mx, mon->my))
6733.             pline_mon(mon, "%s is almost hit by %s!",
6734.                       Monnam(mon), doname(obj));
6735.     } else {
6736.         int dam = 1;
6737.         boolean harmless = (obj && stone_missile(obj)
6738.                             && passes_rocks(mon->data));
6739. 
6740.         if (obj && cansee(mon->mx, mon->my))
6741.             pline_mon(mon, "%s is hit by %s%s",
6742.                       Monnam(mon), doname(obj),
6743.                       harmless ? " but is not harmed." : "!");
6744.         if (d_override) {
6745.             dam = d_override;
6746.         } else if (obj) {
6747.             dam = dmgval(obj, mon);
6748.             if (dam < 1)
6749.                 dam = 1;
6750.         }
6751.         if (!harmless) {
6752.             mon->mhp -= dam;
6753.             if (mon->mhp <= 0) {
6754.                 int xx = mon->mx, yy = mon->my;
6755. 
6756.                 monkilled(mon, "", nocorpse ? -AD_RBRE : AD_PHYS);
6757.                 if (DEADMONSTER(mon)) {
6758.                     newsym(xx, yy);
6759.                     trapkilled = TRUE;
6760.                 }
6761.             }
6762.         } else {
6763.             strike = 0; /* harmless; don't use up the missile */
6764.         }
6765.     }
6766.     if (obj && (!strike || d_override)) {
6767.         place_object(obj, mon->mx, mon->my);
6768.         stackobj(obj);
6769.     } else if (obj)
6770.         dealloc_obj(obj);
6771. 
6772.     return trapkilled;
6773. }
6774. 
6775. boolean
6776. unconscious(void)
6777. {
6778.     if (gm.multi >= 0)
6779.         return FALSE;
6780. 
6781.     return (u.usleep
6782.             || (gn.nomovemsg
6783.                 && (!strncmp(gn.nomovemsg, "You awake", 9)
6784.                     || !strncmp(gn.nomovemsg, "You regain con", 14)
6785.                     || !strncmp(gn.nomovemsg, "You are consci", 14))));
6786. }
6787. 
6788. static const char lava_killer[] = "molten lava";
6789. 
6790. /* hero enters pool of molten lava; returns True if hero is killed and
6791.    then life-saved (with teleport to safe spot), False for other survival;
6792.    no return at all if hero dies and isn't life-saved */
6793. boolean
6794. lava_effects(void)
6795. {
6796.     struct obj *obj, *obj2, *nextobj;
6797.     boolean usurvive, boil_away;
6798.     unsigned protect_oid = 0;
6799.     int burncount = 0, burnmesgcount = 0;
6800.     const int dmg = d(6, 6); /* only applicable for water walking */
6801. 
6802.     if (iflags.in_lava_effects) {
6803.         debugpline0("Skipping recursive lava_effects().");
6804.         return FALSE;
6805.     }
6806.     feel_newsym(u.ux, u.uy); /* in case Blind, map the lava here */
6807.     burn_away_slime();
6808.     if (likes_lava(gy.youmonst.data))
6809.         return FALSE;
6810. 
6811.     usurvive = Fire_resistance || (Wwalking && dmg < u.uhp);
6812.     /*
6813.      * A timely interrupt might manage to salvage your life
6814.      * but not your gear.  For scrolls and potions this
6815.      * will destroy whole stacks, where fire resistant hero
6816.      * survivor only loses partial stacks via destroy_items().
6817.      *
6818.      * Flag items to be destroyed before any messages so
6819.      * that player causing hangup at --More-- won't get an
6820.      * emergency save file created before item destruction.
6821.      */
6822.     if (!usurvive) {
6823.         for (obj = gi.invent; obj; obj = nextobj) {
6824.             nextobj = obj->nobj;
6825.             if (obj->in_use) { /* remove_worn_item() sets in_use */
6826.                 /* one item can be protected from burning up [accommodates
6827.                    steal(AMULET_OF_FLYING) -> remove_worn_item() -> fall
6828.                    into lava (which happens before item is transferred
6829.                    from invent to thief->minvent)]; item will still be in
6830.                    inventory when we return to caller or save bones (or
6831.                    perform hangup save if that occurs) */
6832.                 if (!protect_oid) {
6833.                     protect_oid = obj->o_id;
6834.                     obj->in_use = 0;
6835.                 } else {
6836.                     impossible(
6837.                      "lava_effects: '%s' (#%u) is already in use; so is #%u.",
6838.                                simpleonames(obj), obj->o_id, protect_oid);
6839.                 }
6840.                 continue;
6841.             }
6842.             /* set obj->in_use for items which will be destroyed below */
6843.             if ((is_organic(obj) || obj->oclass == POTION_CLASS)
6844.                 && !obj->oerodeproof
6845.                 && objects[obj->otyp].oc_oprop != FIRE_RES
6846.                 && obj->otyp != SCR_FIRE && obj->otyp != SPE_FIREBALL
6847.                 && !obj_resists(obj, 0, 0)) /* for invocation items */
6848.                 obj->in_use = 1;
6849.         }
6850.     }
6851. 
6852.     /* Check whether we should burn away boots *first* so we know whether to
6853.      * make the player sink into the lava. Assumption: water walking only
6854.      * comes from boots.
6855.      * (5.0: that assumption is no longer true, but having boots be the first
6856.      * thing to come into contact with lava makes sense.)
6857.      */
6858.     if (uarmf && (uarmf->in_use
6859.                   || (is_organic(uarmf) && !uarmf->oerodeproof))) {
6860.         obj = uarmf;
6861.         pline("%s into flame!", Yobjnam2(obj, "burst"));
6862.         ++burnmesgcount;
6863.         iflags.in_lava_effects++; /* (see above) */
6864.         (void) Boots_off();
6865.         if (obj->o_id != protect_oid)
6866.             useup(obj);
6867.         iflags.in_lava_effects--;
6868.         ++burncount;
6869.     }
6870. 
6871.     if (!Fire_resistance) {
6872.         if (Wwalking) {
6873.             pline_The("%s here burns you!", hliquid("lava"));
6874.             if (usurvive) {
6875.                 losehp(dmg, lava_killer, KILLED_BY); /* lava damage */
6876.                 goto burn_stuff;
6877.             }
6878.         } else
6879.             You("fall into the %s!", waterbody_name(u.ux, u.uy));
6880. 
6881.         usurvive = Lifesaved || discover;
6882.         if (wizard)
6883.             usurvive = TRUE;
6884. 
6885.         /* prevent remove_worn_item() -> Boots_off(WATER_WALKING_BOOTS) ->
6886.            spoteffects() -> lava_effects() recursion which would
6887.            successfully delete (via useupall) the no-longer-worn boots;
6888.            once recursive call returned, we would try to delete them again
6889.            here in the outer call (and access stale memory, probably panic) */
6890.         iflags.in_lava_effects++;
6891. 
6892.         for (obj = gi.invent; obj; obj = obj2) {
6893.             obj2 = obj->nobj;
6894.             if (obj->o_id == protect_oid) {
6895.                 /* skip protected item; caller expects to retain access */
6896.                 obj->in_use = 1; /* was cleared when setting protect_oid */
6897.             } else if (obj->otyp == SPE_BOOK_OF_THE_DEAD) {
6898.                 if (usurvive && !Blind)
6899.                     pline("%s glows a strange %s, but remains intact.",
6900.                           The(xname(obj)), hcolor("dark red"));
6901.             } else if (obj->in_use) {
6902.                 if (obj->owornmask) {
6903.                     if (usurvive) {
6904.                         pline("%s into flame!", Yobjnam2(obj, "burst"));
6905.                         ++burnmesgcount;
6906.                     }
6907.                     remove_worn_item(obj, TRUE);
6908.                 }
6909.                 useupall(obj);
6910.                 ++burncount;
6911.             }
6912.         }
6913.         if (usurvive && burncount > burnmesgcount)
6914.             pline("%s item%s in your inventory %s been destroyed.",
6915.                   (burnmesgcount > 0)
6916.                     ? ((burncount - burnmesgcount == 1) ? "Another" : "Other")
6917.                     : ((burncount == 1) ? "An" : "Some"),
6918.                   plur(burncount - burnmesgcount),
6919.                   (burncount - burnmesgcount == 1) ? "has" : "have");
6920. 
6921.         /* s/he died... */
6922.         boil_away = (u.umonnum == PM_WATER_ELEMENTAL
6923.                      || u.umonnum == PM_STEAM_VORTEX
6924.                      || u.umonnum == PM_FOG_CLOUD);
6925.         /* burn to death; if hero is life-saved on the first pass, try
6926.            to teleport to safety; if that fails, burn all over again */
6927.         for (burncount = 0; burncount < 2; ++burncount) {
6928.             u.uhp = -1;
6929.             /* killer format and name are reconstructed every iteration
6930.                because lifesaving resets them */
6931.             svk.killer.format = KILLED_BY;
6932.             Strcpy(svk.killer.name, lava_killer);
6933.             urgent_pline("You %s...", boil_away ? "boil away"
6934.                                                 : "burn to a crisp");
6935.             done(BURNING);
6936.             if (safe_teleds(TELEDS_ALLOW_DRAG | TELEDS_TELEPORT))
6937.                 break; /* successful life-save */
6938.             /* nowhere safe to land; repeat burning loop */
6939.             pline("You're still burning.");
6940.         }
6941. 
6942.         iflags.in_lava_effects--;
6943. 
6944.         if (burncount == 2) {
6945.             /* life-saved twice (second time must have been due to declining
6946.                to die in wizard|explore mode) and failed to be teleported
6947.                to safety both times; moveloop() will just drop the hero into
6948.                the lava again on next move so take countermeasures to give
6949.                the player--or the debug fuzzer--a chance to try something
6950.                else instead of just immediately burning up all over again */
6951.             if (!Fire_resistance)
6952.                 set_itimeout(&HFire_resistance, 5L);
6953.             if (!Wwalking)
6954.                 set_itimeout(&HWwalking, 5L);
6955.             goto burn_stuff;
6956.         }
6957.         rescued_from_terrain(BURNING);
6958. 
6959.         /* normally done via safe_teleds() -> teleds() -> spoteffects() but
6960.            spoteffects() was no-op when called with nonzero in_lava_effects */
6961.         spoteffects(FALSE); /* suppress auto-pickup for this landing... */
6962. 
6963.         return TRUE;
6964.     } else if (!Wwalking && (!u.utrap || u.utraptype != TT_LAVA)) {
6965.         boil_away = !Fire_resistance;
6966.         /* if not fire resistant, sink_into_lava() will quickly be fatal;
6967.            hero needs to escape immediately */
6968.         set_utrap((unsigned) (rn1(4, 4) + ((boil_away ? 2
6969.                                                       : rn1(4, 12)) << 8)),
6970.                   TT_LAVA);
6971.         You("sink into the %s%s!", waterbody_name(u.ux, u.uy),
6972.             !boil_away ? ", but it only burns slightly"
6973.                        : " and are about to be immolated");
6974.         if (Fire_resistance)
6975.             monstseesu(M_SEEN_FIRE);
6976.         else
6977.             monstunseesu(M_SEEN_FIRE);
6978.         if (u.uhp > 1)
6979.             losehp(!boil_away ? 1 : (u.uhp / 2), lava_killer,
6980.                    KILLED_BY); /* lava damage */
6981.     }
6982. 
6983.  burn_stuff:
6984.      (void) destroy_items(&gy.youmonst, AD_FIRE, dmg);
6985.     ignite_items(gi.invent);
6986.     return FALSE;
6987. }
6988. 
6989. /* called each turn when trapped in lava */
6990. void
6991. sink_into_lava(void)
6992. {
6993.     static const char sink_deeper[] = "You sink deeper into the lava.";
6994. 
6995.     if (!u.utrap || u.utraptype != TT_LAVA) {
6996.         ; /* do nothing; this usually won't happen but could after
6997.            * polymorphing from a flier into a ceiling hider and then hiding;
6998.            * allmain() only checks whether the hero is at a lava location,
6999.            * not whether he or she is currently sinking */
7000.     } else if (!is_lava(u.ux, u.uy)) {
7001.         reset_utrap(FALSE); /* this shouldn't happen either */
7002.     } else if (!u.uinvulnerable) {
7003.         /* ordinarily we'd have to be fire resistant to survive long
7004.            enough to become stuck in lava, but it can happen without
7005.            resistance if water walking boots allow survival and then
7006.            get burned up; u.utrap time will be quite short in that case */
7007.         if (!Fire_resistance)
7008.             u.uhp = (u.uhp + 2) / 3;
7009. 
7010.         u.utrap -= (1 << 8);
7011.         if (u.utrap < (1 << 8)) {
7012.             svk.killer.format = KILLED_BY;
7013.             Strcpy(svk.killer.name, "molten lava");
7014.             urgent_pline("You sink below the surface and die.");
7015.             burn_away_slime(); /* add insult to injury? */
7016.             done(DISSOLVED);
7017.             /* can only get here via life-saving; try to get away from lava */
7018.             reset_utrap(TRUE);
7019.             /* levitation or flight have become unblocked, otherwise Tport */
7020.             if (!Levitation && !Flying)
7021.                 (void) safe_teleds(TELEDS_ALLOW_DRAG | TELEDS_TELEPORT);
7022.         } else if (!u.umoved) {
7023.             /* can't fully turn into slime while in lava, but might not
7024.                have it be burned away until you've come awfully close */
7025.             if (Slimed && rnd(10 - 1) >= (int) (Slimed & TIMEOUT)) {
7026.                 pline(sink_deeper);
7027.                 burn_away_slime();
7028.             } else {
7029.                 Norep(sink_deeper);
7030.             }
7031.             u.utrap += rnd(4);
7032.         }
7033.     }
7034. }
7035. 
7036. /* called when something has been done (breaking a boulder, for instance)
7037.    which entails a luck penalty if performed on a sokoban level */
7038. void
7039. sokoban_guilt(void)
7040. {
7041.     if (Sokoban) {
7042.         u.uconduct.sokocheat++;
7043.         change_luck(-1);
7044.         /*
7045.          * TODO:
7046.          *  Issue some feedback so that player can learn that whatever
7047.          *  he/she just did is a naughty thing to do in sokoban and
7048.          *  should probably be avoided in future....
7049.          *
7050.          *  Caveat:  doing this might introduce message sequencing
7051.          *  issues, depending upon feedback during the various actions
7052.          *  which trigger Sokoban luck penalties.
7053.          */
7054.     }
7055. }
7056. 
7057. /* called when a trap has been deleted or had its ttyp replaced */
7058. staticfn void
7059. maybe_finish_sokoban(void)
7060. {
7061.     struct trap *t;
7062. 
7063.     if (Sokoban && !gi.in_mklev) {
7064.         /* scan all remaining traps, ignoring any created by the hero;
7065.            if this level has no more pits or holes, the current sokoban
7066.            puzzle has been solved */
7067.         for (t = gf.ftrap; t; t = t->ntrap) {
7068.             if (t->madeby_u)
7069.                 continue;
7070.             if (t->ttyp == PIT || t->ttyp == HOLE)
7071.                 break;
7072.         }
7073.         if (!t) {
7074.             /* for livelog to report the sokoban depth in the way that
7075.                players tend to think about it: 1 for entry level, 4 for top */
7076.             int sokonum = svd.dungeons[u.uz.dnum].entry_lev - u.uz.dlevel + 1;
7077. 
7078.             /* we've passed the last trap without finding a pit or hole;
7079.                clear the sokoban_rules flag so that luck penalties for
7080.                things like breaking boulders or jumping will no longer
7081.                be given, and restrictions on diagonal moves are lifted */
7082.             Sokoban = 0; /* clear svl.level.flags.sokoban_rules */
7083.             /*
7084.              * TODO: give some feedback about solving the sokoban puzzle
7085.              * (perhaps say "congratulations" in Japanese?).
7086.              */
7087. 
7088.             /* log the completion event regardless of whether or not
7089.                any normal in-game feedback has just been given */
7090.             livelog_printf(LL_MINORAC | LL_DUMP,
7091.                            "completed %d%s Sokoban level",
7092.                            sokonum, ordin(sokonum));
7093.         }
7094.     }
7095. }
7096. 
7097. /* Return the string name of the trap type passed in, unless the player is
7098.    hallucinating, in which case return a random or hallucinatory trap name. */
7099. const char *
7100. trapname(
7101.     int ttyp,
7102.     boolean override) /* if True, ignore Hallucination */
7103. {
7104.     static const char *const halu_trapnames[] = {
7105.         /* riffs on actual nethack traps */
7106.         "bottomless pit", "polymorphism trap", "devil teleporter",
7107.         "falling boulder trap", "anti-anti-magic field", "weeping gas trap",
7108.         "queasy board", "electrified web", "owlbear trap", "sand mine",
7109.         "vacillating triangle",
7110.         /* some traps found in nethack variants */
7111.         "death trap", "disintegration trap", "ice trap", "monochrome trap",
7112.         /* plausible real-life traps */
7113.         "axeblade trap", "pool of boiling oil", "pool of quicksand",
7114.         "field of caltrops", "buzzsaw trap", "spiked floor", "revolving wall",
7115.         "uneven floor", "finger trap", "jack-in-a-box", "yellow snow",
7116.         "booby trap", "rat trap", "poisoned nail", "snare", "whirlpool",
7117.         "trip wire", "roach motel (tm)",
7118.         /* sci-fi */
7119.         "negative space", "tensor field", "singularity", "imperial fleet",
7120.         "black hole", "thermal detonator", "event horizon",
7121.         "entoptic phenomenon",
7122.         /* miscellaneous suggestions */
7123.         "sweet-smelling gas vent", "phone booth", "exploding runes",
7124.         "never-ending elevator", "slime pit", "warp zone", "illusory floor",
7125.         "pile of poo", "honey trap", "tourist trap",
7126.         "banana peel", "garden rake", "whoopie cushion", "box and stick trap",
7127.         "fly trap", "legal trap", "pit of snakes", "pollywog trap",
7128.         "slippery slope", "thirst trap", "suntrap",
7129.     };
7130.     static char roletrap[33]; /* [17 + 5 + 1] should suffice */
7131. 
7132.     if (Hallucination && !override) {
7133.         int total_names = TRAPNUM + SIZE(halu_trapnames),
7134.             nameidx = rn2_on_display_rng(total_names + 1);
7135. 
7136.         if (nameidx == total_names) {
7137.             boolean fem = Upolyd ? u.mfemale : flags.female;
7138. 
7139.             /* inspired by "tourist trap" */
7140.             copynchars(roletrap,
7141.                        rn2(3) ? ((fem && gu.urole.name.f) ? gu.urole.name.f
7142.                                                           : gu.urole.name.m)
7143.                               : rank_of(u.ulevel, Role_switch, fem),
7144.                        (int) (sizeof roletrap - sizeof " trap"));
7145.             Strcat(roletrap, " trap");
7146.             return lcase(roletrap);
7147.         } else if (nameidx >= TRAPNUM) {
7148.             nameidx -= TRAPNUM;
7149.             return halu_trapnames[nameidx];
7150.         } /* else use an actual trap type */
7151.         if (nameidx != NO_TRAP)
7152.             ttyp = nameidx;
7153.     }
7154.     return defsyms[trap_to_defsym(ttyp)].explanation;
7155. }
7156. 
7157. /* Ignite ignitable items (limited to light sources) in the given object
7158.    chain, due to some external source of fire.  The object chain should
7159.    be somewhere exposed, like someone's open inventory or the floor. */
7160. void
7161. ignite_items(struct obj *objchn)
7162. {
7163.     struct obj *obj, *nextobj;
7164.     boolean bynexthere = (objchn && objchn->where == OBJ_FLOOR);
7165. 
7166.     for (obj = objchn; obj; obj = bynexthere ? obj->nexthere : nextobj) {
7167.         nextobj = obj->nobj;
7168.         /* ignitable items like lamps and candles will catch fire */
7169.         if (!obj->lamplit && !obj->in_use)
7170.             catch_lit(obj);
7171.     }
7172. }
7173. 
7174. void
7175. trap_ice_effects(coordxy x, coordxy y, boolean ice_is_melting)
7176. {
7177.     struct trap *ttmp = t_at(x, y);
7178. 
7179.     if (ttmp && ice_is_melting) {
7180.         struct monst *mtmp;
7181. 
7182.         if (((mtmp = m_at(x, y)) != 0) && mtmp->mtrapped)
7183.             mtmp->mtrapped = 0;
7184.         if (ttmp->ttyp == LANDMINE || ttmp->ttyp == BEAR_TRAP) {
7185.             /* landmine or bear trap set on top of the ice falls
7186.                into the water */
7187.             int otyp = (ttmp->ttyp == LANDMINE) ? LAND_MINE : BEARTRAP;
7188.             cnv_trap_obj(otyp, 1, ttmp, TRUE);
7189.         } else {
7190.             if (!undestroyable_trap(ttmp->ttyp))
7191.                 deltrap(ttmp);
7192.         }
7193.     }
7194. }
7195. 
7196. /* sanity check traps */
7197. void
7198. trap_sanity_check(void)
7199. {
7200.     struct trap *ttmp = gf.ftrap;
7201. 
7202.     while (ttmp) {
7203.         if (!isok(ttmp->tx, ttmp->ty))
7204.             impossible("trap sanity: location (%i,%i)", ttmp->tx, ttmp->ty);
7205.         if (ttmp->ttyp <= NO_TRAP || ttmp->ttyp >= TRAPNUM)
7206.             impossible("trap sanity: type (%i)", ttmp->ttyp);
7207.         ttmp = ttmp->ntrap;
7208.     }
7209. }
7210. 
7211. /*trap.c*/