Source:NetHack 5.0.0/src/dbridge.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to dbridge.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/dbridge.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	dbridge.c	$NHDT-Date: 1772771734 2026/03/05 20:35:34 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.70 $ */
2. /*      Copyright (c) 1989 by Jean-Christophe Collet              */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. /*
6.  * This file contains the drawbridge manipulation (create, open, close,
7.  * destroy).
8.  *
9.  * Added comprehensive monster-handling, and the "entity" structure to
10.  * deal with players as well. - 11/89
11.  *
12.  * Any traps and/or engravings at either the portcullis or span location
13.  * are destroyed whenever the bridge is lowered, raised, or destroyed.
14.  * (Engraving handling could be extended to flag whether an engraving on
15.  * the DB_UNDER surface is hidden by the lowered bridge, or one on the
16.  * bridge itself is hidden because the bridge has been raised, but that
17.  * seems like an awful lot of effort for very little gain.)
18.  */
19. 
20. #include "hack.h"
21. 
22. staticfn void get_wall_for_db(coordxy *, coordxy *);
23. staticfn struct entity *e_at(coordxy, coordxy);
24. staticfn void m_to_e(struct monst *, coordxy, coordxy, struct entity *);
25. staticfn void u_to_e(struct entity *);
26. staticfn void set_entity(coordxy, coordxy, struct entity *);
27. staticfn const char *e_nam(struct entity *);
28. staticfn const char *E_phrase(struct entity *, const char *);
29. staticfn boolean e_survives_at(struct entity *, coordxy, coordxy);
30. staticfn void e_died(struct entity *, int, int);
31. staticfn boolean automiss(struct entity *);
32. staticfn boolean e_missed(struct entity *, boolean);
33. staticfn boolean e_jumps(struct entity *);
34. staticfn void do_entity(struct entity *);
35. staticfn void nokiller(void);
36. 
37. boolean
38. is_waterwall(coordxy x, coordxy y)
39. {
40.     if (isok(x, y) && IS_WATERWALL(levl[x][y].typ))
41.         return TRUE;
42.     return FALSE;
43. }
44. 
45. boolean
46. is_pool(coordxy x, coordxy y)
47. {
48.     schar ltyp;
49. 
50.     if (!isok(x, y))
51.         return FALSE;
52.     ltyp = levl[x][y].typ;
53.     /* The ltyp == MOAT is not redundant with is_moat, because the
54.      * Juiblex level does not have moats, although it has MOATs. There
55.      * is probably a better way to express this. */
56.     if (ltyp == POOL || ltyp == MOAT || ltyp == WATER || is_moat(x, y))
57.         return TRUE;
58.     return FALSE;
59. }
60. 
61. boolean
62. is_lava(coordxy x, coordxy y)
63. {
64.     schar ltyp;
65. 
66.     if (!isok(x, y))
67.         return FALSE;
68.     ltyp = levl[x][y].typ;
69.     if (ltyp == LAVAPOOL || ltyp == LAVAWALL
70.         || (ltyp == DRAWBRIDGE_UP
71.             && (levl[x][y].drawbridgemask & DB_UNDER) == DB_LAVA))
72.         return TRUE;
73.     return FALSE;
74. }
75. 
76. boolean
77. is_pool_or_lava(coordxy x, coordxy y)
78. {
79.     if (is_pool(x, y) || is_lava(x, y))
80.         return TRUE;
81.     else
82.         return FALSE;
83. }
84. 
85. boolean
86. is_ice(coordxy x, coordxy y)
87. {
88.     schar ltyp;
89. 
90.     if (!isok(x, y))
91.         return FALSE;
92.     ltyp = levl[x][y].typ;
93.     if (ltyp == ICE || (ltyp == DRAWBRIDGE_UP
94.                         && (levl[x][y].drawbridgemask & DB_UNDER) == DB_ICE))
95.         return TRUE;
96.     return FALSE;
97. }
98. 
99. boolean
100. is_moat(coordxy x, coordxy y)
101. {
102.     schar ltyp;
103. 
104.     if (!isok(x, y))
105.         return FALSE;
106.     ltyp = levl[x][y].typ;
107.     if (!Is_juiblex_level(&u.uz)
108.         && (ltyp == MOAT
109.             || (ltyp == DRAWBRIDGE_UP
110.                 && (levl[x][y].drawbridgemask & DB_UNDER) == DB_MOAT)))
111.         return TRUE;
112.     return FALSE;
113. }
114. 
115. schar
116. db_under_typ(int mask)
117. {
118.     switch (mask & DB_UNDER) {
119.     case DB_ICE:
120.         return ICE;
121.     case DB_LAVA:
122.         return LAVAPOOL;
123.     case DB_MOAT:
124.         return MOAT;
125.     default:
126.         return STONE;
127.     }
128. }
129. 
130. /*
131.  * We want to know whether a wall (or a door) is the portcullis (passageway)
132.  * of an eventual drawbridge.
133.  *
134.  * Return value:  the direction of the drawbridge, or -1 if not valid
135.  */
136. int
137. is_drawbridge_wall(coordxy x, coordxy y)
138. {
139.     struct rm *lev;
140. 
141.     if (!isok(x, y))
142.         return -1;
143. 
144.     lev = &levl[x][y];
145.     if (lev->typ != DOOR && lev->typ != DBWALL)
146.         return -1;
147. 
148.     if (isok(x + 1, y) && IS_DRAWBRIDGE(levl[x + 1][y].typ)
149.         && (levl[x + 1][y].drawbridgemask & DB_DIR) == DB_WEST)
150.         return DB_WEST;
151.     if (isok(x - 1, y) && IS_DRAWBRIDGE(levl[x - 1][y].typ)
152.         && (levl[x - 1][y].drawbridgemask & DB_DIR) == DB_EAST)
153.         return DB_EAST;
154.     if (isok(x, y - 1) && IS_DRAWBRIDGE(levl[x][y - 1].typ)
155.         && (levl[x][y - 1].drawbridgemask & DB_DIR) == DB_SOUTH)
156.         return DB_SOUTH;
157.     if (isok(x, y + 1) && IS_DRAWBRIDGE(levl[x][y + 1].typ)
158.         && (levl[x][y + 1].drawbridgemask & DB_DIR) == DB_NORTH)
159.         return DB_NORTH;
160. 
161.     return -1;
162. }
163. 
164. /*
165.  * Use is_db_wall where you want to verify that a
166.  * drawbridge "wall" is UP in the location x, y
167.  * (instead of UP or DOWN, as with is_drawbridge_wall).
168.  */
169. boolean
170. is_db_wall(coordxy x, coordxy y)
171. {
172.     return (boolean) (levl[x][y].typ == DBWALL);
173. }
174. 
175. /*
176.  * Return true with x,y pointing to the drawbridge if x,y initially indicate
177.  * a drawbridge or drawbridge wall.
178.  */
179. boolean
180. find_drawbridge(coordxy *x, coordxy *y)
181. {
182.     int dir;
183. 
184.     if (IS_DRAWBRIDGE(levl[*x][*y].typ))
185.         return TRUE;
186.     dir = is_drawbridge_wall(*x, *y);
187.     if (dir >= 0) {
188.         switch (dir) {
189.         case DB_NORTH:
190.             (*y)++;
191.             break;
192.         case DB_SOUTH:
193.             (*y)--;
194.             break;
195.         case DB_EAST:
196.             (*x)--;
197.             break;
198.         case DB_WEST:
199.             (*x)++;
200.             break;
201.         }
202.         return TRUE;
203.     }
204.     return FALSE;
205. }
206. 
207. /*
208.  * Find the drawbridge wall associated with a drawbridge.
209.  */
210. staticfn void
211. get_wall_for_db(coordxy *x, coordxy *y)
212. {
213.     switch (levl[*x][*y].drawbridgemask & DB_DIR) {
214.     case DB_NORTH:
215.         (*y)--;
216.         break;
217.     case DB_SOUTH:
218.         (*y)++;
219.         break;
220.     case DB_EAST:
221.         (*x)++;
222.         break;
223.     case DB_WEST:
224.         (*x)--;
225.         break;
226.     }
227. }
228. 
229. /*
230.  * Creation of a drawbridge at pos x,y.
231.  *     dir is the direction.
232.  *     flag must be put to TRUE if we want the drawbridge to be opened.
233.  */
234. boolean
235. create_drawbridge(coordxy x, coordxy y, int dir, boolean flag)
236. {
237.     coordxy x2, y2;
238.     boolean horiz;
239.     boolean lava = levl[x][y].typ == LAVAPOOL; /* assume initialized map */
240. 
241.     x2 = x;
242.     y2 = y;
243.     switch (dir) {
244.     case DB_NORTH:
245.         horiz = TRUE;
246.         y2--;
247.         break;
248.     case DB_SOUTH:
249.         horiz = TRUE;
250.         y2++;
251.         break;
252.     case DB_EAST:
253.         horiz = FALSE;
254.         x2++;
255.         break;
256.     default:
257.         impossible("bad direction in create_drawbridge");
258.         FALLTHROUGH;
259.         /*FALLTHRU*/
260.     case DB_WEST:
261.         horiz = FALSE;
262.         x2--;
263.         break;
264.     }
265.     if (!IS_WALL(levl[x2][y2].typ))
266.         return FALSE;
267.     if (flag) { /* We want the bridge open */
268.         levl[x][y].typ = DRAWBRIDGE_DOWN;
269.         levl[x2][y2].typ = DOOR;
270.         levl[x2][y2].doormask = D_NODOOR;
271.     } else {
272.         levl[x][y].typ = DRAWBRIDGE_UP;
273.         levl[x2][y2].typ = DBWALL;
274.         /* Drawbridges are non-diggable. */
275.         levl[x2][y2].wall_info = W_NONDIGGABLE;
276.     }
277.     levl[x][y].horizontal = !horiz;
278.     levl[x2][y2].horizontal = horiz;
279.     levl[x][y].drawbridgemask = dir;
280.     if (lava)
281.         levl[x][y].drawbridgemask |= DB_LAVA;
282.     return  TRUE;
283. }
284. 
285. staticfn struct entity *
286. e_at(coordxy x, coordxy y)
287. {
288.     int entitycnt;
289. 
290.     for (entitycnt = 0; entitycnt < ENTITIES; entitycnt++)
291.         if (go.occupants[entitycnt].edata
292.             && go.occupants[entitycnt].ex == x
293.             && go.occupants[entitycnt].ey == y)
294.             break;
295.     debugpline1("entitycnt = %d", entitycnt);
296. #ifdef D_DEBUG
297.     wait_synch();
298. #endif
299.     return (entitycnt == ENTITIES) ? (struct entity *) 0
300.                                    : &(go.occupants[entitycnt]);
301. }
302. 
303. staticfn void
304. m_to_e(struct monst *mtmp, coordxy x, coordxy y, struct entity *etmp)
305. {
306.     etmp->emon = mtmp;
307.     if (mtmp) {
308.         etmp->ex = x;
309.         etmp->ey = y;
310.         if (mtmp->wormno && (x != mtmp->mx || y != mtmp->my))
311.             etmp->edata = &mons[PM_LONG_WORM_TAIL];
312.         else
313.             etmp->edata = mtmp->data;
314.     } else {
315.         etmp->edata = (struct permonst *) 0;
316.         etmp->ex = etmp->ey = 0;
317.     }
318. }
319. 
320. staticfn void
321. u_to_e(struct entity *etmp)
322. {
323.     etmp->emon = &gy.youmonst;
324.     etmp->ex = u.ux;
325.     etmp->ey = u.uy;
326.     etmp->edata = gy.youmonst.data;
327. }
328. 
329. staticfn void
330. set_entity(
331.     coordxy x, coordxy y, /* location of span or portcullis */
332.     struct entity *etmp)  /* pointer to occupants[0] or occupants[1] */
333. {
334.     if (u_at(x, y))
335.         u_to_e(etmp);
336.     else /* m_at() might yield Null; that's ok */
337.         m_to_e(m_at(x, y), x, y, etmp);
338. }
339. 
340. #define is_u(etmp) (etmp->emon == &gy.youmonst)
341. #define e_canseemon(etmp) (is_u(etmp) || canseemon(etmp->emon))
342. 
343. /*
344.  * e_strg is a utility routine which is not actually in use anywhere, since
345.  * the specialized routines below suffice for all current purposes.
346.  */
347. 
348. /* #define e_strg(etmp, func) (is_u(etmp) ? (char *) 0 : func(etmp->emon)) */
349. 
350. staticfn const char *
351. e_nam(struct entity *etmp)
352. {
353.     return is_u(etmp) ? "you" : mon_nam(etmp->emon);
354. }
355. 
356. /*
357.  * Generates capitalized entity name, makes 2nd -> 3rd person conversion on
358.  * verb, where necessary.
359.  */
360. staticfn const char *
361. E_phrase(struct entity *etmp, const char *verb)
362. {
363.     static char wholebuf[80];
364. 
365.     Strcpy(wholebuf, is_u(etmp) ? "You" : Monnam(etmp->emon));
366.     if (!verb || !*verb)
367.         return wholebuf;
368.     Strcat(wholebuf, " ");
369.     if (is_u(etmp))
370.         Strcat(wholebuf, verb);
371.     else
372.         Strcat(wholebuf, vtense((char *) 0, verb));
373.     return wholebuf;
374. }
375. 
376. /*
377.  * Simple-minded "can it be here?" routine
378.  */
379. staticfn boolean
380. e_survives_at(struct entity *etmp, coordxy x, coordxy y)
381. {
382.     if (noncorporeal(etmp->edata))
383.         return TRUE;
384.     if (is_pool(x, y))
385.         return (boolean) ((is_u(etmp) && (Wwalking || Amphibious || Breathless
386.                                           || Swimming || Flying || Levitation))
387.                           || is_swimmer(etmp->edata)
388.                           || is_flyer(etmp->edata)
389.                           || is_floater(etmp->edata));
390.     /* must force call to lava_effects in e_died if is_u */
391.     if (is_lava(x, y))
392.         return (boolean) ((is_u(etmp) && (Levitation || Flying))
393.                           || likes_lava(etmp->edata)
394.                           || is_flyer(etmp->edata));
395.     if (is_db_wall(x, y))
396.         return (boolean) (is_u(etmp) ? Passes_walls
397.                           : passes_walls(etmp->edata));
398.     return TRUE;
399. }
400. 
401. staticfn void
402. e_died(
403.     struct entity *etmp,
404.     int xkill_flags, int how)
405. {
406.     if (is_u(etmp)) {
407.         if (how == DROWNING) {
408.             svk.killer.name[0] = 0; /* drown() sets its own killer */
409.             (void) drown();
410.         } else if (how == BURNING) {
411.             svk.killer.name[0] = 0; /* lava_effects() sets own killer */
412.             (void) lava_effects();
413.         } else {
414.             coord xy;
415. 
416.             /* use more specific killer if specified */
417.             if (!svk.killer.name[0]) {
418.                 svk.killer.format = KILLED_BY_AN;
419.                 Strcpy(svk.killer.name, "falling drawbridge");
420.             }
421.             done(how);
422.             /* So, you didn't die */
423.             if (!e_survives_at(etmp, etmp->ex, etmp->ey)) {
424.                 if (enexto(&xy, etmp->ex, etmp->ey, etmp->edata)) {
425.                     pline("A %s force teleports you away...",
426.                           Hallucination ? "normal" : "strange");
427.                     teleds(xy.x, xy.y, TELEDS_NO_FLAGS);
428.                 }
429.                 /* otherwise on top of the drawbridge is the
430.                  * only viable spot in the dungeon, so stay there
431.                  */
432.             }
433.         }
434.         /* we might have crawled out of the moat to survive */
435.         etmp->ex = u.ux, etmp->ey = u.uy;
436.     } else {
437.         int entitycnt;
438. 
439.         svk.killer.name[0] = 0;
440. /* fake "digested to death" damage-type suppresses corpse */
441. #define mk_message(dest) (((dest & XKILL_NOMSG) != 0) ? (char *) 0 : "")
442. #define mk_corpse(dest) (((dest & XKILL_NOCORPSE) != 0) ? AD_DGST : AD_PHYS)
443.         /* if monsters are moving, one of them caused the destruction */
444.         if (svc.context.mon_moving)
445.             monkilled(etmp->emon,
446.                       mk_message(xkill_flags), mk_corpse(xkill_flags));
447.         else /* you caused it */
448.             xkilled(etmp->emon, xkill_flags);
449. 
450.         /* if etmp gets life-saved, kill it again; otherwise we might end up
451.            trying to place another monster (probably a xorn) on same spot */
452.         if (!DEADMONSTER(etmp->emon)) {
453.             int seeit = canspotmon(etmp->emon);
454. 
455.             xkill_flags |= XKILL_NOMSG | XKILL_NOCONDUCT;
456.             if (svc.context.mon_moving)
457.                 monkilled(etmp->emon, "", mk_corpse(xkill_flags));
458.             else /* you caused it */
459.                 xkilled(etmp->emon, xkill_flags);
460. 
461.             if (DEADMONSTER(etmp->emon)) {
462.                 if (seeit)
463.                     pline("Unfortunately for %s, %s is still crushed.",
464.                           mon_nam(etmp->emon), mhe(etmp->emon));
465.             } else {
466.                 ; /* FIXME: still not dead?  What should we do now? */
467.             }
468.         }
469.         etmp->edata = (struct permonst *) 0;
470. 
471.         /* dead long worm handling */
472.         for (entitycnt = 0; entitycnt < ENTITIES; entitycnt++) {
473.             if (etmp != &(go.occupants[entitycnt])
474.                 && etmp->emon == go.occupants[entitycnt].emon)
475.                 go.occupants[entitycnt].edata = (struct permonst *) 0;
476.         }
477. #undef mk_message
478. #undef mk_corpse
479.     }
480. }
481. 
482. /*
483.  * These are never directly affected by a bridge or portcullis.
484.  */
485. staticfn boolean
486. automiss(struct entity *etmp)
487. {
488.     return (boolean) ((is_u(etmp) ? Passes_walls : passes_walls(etmp->edata))
489.                       || noncorporeal(etmp->edata));
490. }
491. 
492. /*
493.  * Does falling drawbridge or portcullis miss etmp?
494.  */
495. staticfn boolean
496. e_missed(struct entity *etmp, boolean chunks)
497. {
498.     int misses;
499. 
500.     if (chunks) {
501.         debugpline0("Do chunks miss?");
502.     }
503.     if (automiss(etmp))
504.         return TRUE;
505. 
506.     if (is_flyer(etmp->edata)
507.         && (is_u(etmp) ? !Unaware
508.                        : !helpless(etmp->emon)))
509.         /* flying requires mobility */
510.         misses = 5; /* out of 8 */
511.     else if (is_floater(etmp->edata)
512.              || (is_u(etmp) && Levitation)) /* doesn't require mobility */
513.         misses = 3;
514.     else if (chunks && is_pool(etmp->ex, etmp->ey))
515.         misses = 2; /* sitting ducks */
516.     else
517.         misses = 0;
518. 
519.     if (is_db_wall(etmp->ex, etmp->ey))
520.         misses -= 3; /* less airspace */
521. 
522.     debugpline1("Miss chance = %d (out of 8)", misses);
523. 
524.     return (misses >= rnd(8)) ? TRUE : FALSE;
525. }
526. 
527. /*
528.  * Can etmp jump from death?
529.  */
530. staticfn boolean
531. e_jumps(struct entity *etmp)
532. {
533.     int tmp = 4; /* out of 10 */
534. 
535.     if (is_u(etmp) ? (Unaware || Fumbling)
536.                    : (helpless(etmp->emon)
537.                       || !etmp->edata->mmove || etmp->emon->wormno))
538.         return FALSE;
539. 
540.     if (is_u(etmp) ? Confusion : etmp->emon->mconf)
541.         tmp -= 2;
542. 
543.     if (is_u(etmp) ? Stunned : etmp->emon->mstun)
544.         tmp -= 3;
545. 
546.     if (is_db_wall(etmp->ex, etmp->ey))
547.         tmp -= 2; /* less room to maneuver */
548. 
549.     debugpline2("%s to jump (%d chances in 10)", E_phrase(etmp, "try"), tmp);
550.     return (tmp >= rnd(10)) ? TRUE : FALSE;
551. }
552. 
553. staticfn void
554. do_entity(struct entity *etmp)
555. {
556.     coordxy newx, newy, oldx, oldy;
557.     int at_portcullis;
558.     boolean must_jump = FALSE, relocates = FALSE, e_inview;
559.     struct rm *crm;
560. 
561.     if (!etmp->edata)
562.         return;
563. 
564.     e_inview = e_canseemon(etmp);
565.     oldx = etmp->ex;
566.     oldy = etmp->ey;
567.     at_portcullis = is_db_wall(oldx, oldy);
568.     crm = &levl[oldx][oldy];
569. 
570.     if (automiss(etmp) && e_survives_at(etmp, oldx, oldy)) {
571.         if (e_inview && (at_portcullis || IS_DRAWBRIDGE(crm->typ)))
572.             pline_The("%s passes through %s!",
573.                       at_portcullis ? "portcullis" : "drawbridge",
574.                       e_nam(etmp));
575.         if (is_u(etmp))
576.             spoteffects(FALSE);
577.         return;
578.     }
579.     if (e_missed(etmp, FALSE)) {
580.         if (at_portcullis) {
581.             pline_The("portcullis misses %s!", e_nam(etmp));
582.         } else {
583.             debugpline1("The drawbridge misses %s!", e_nam(etmp));
584.         }
585.         if (e_survives_at(etmp, oldx, oldy)) {
586.             return;
587.         } else {
588.             debugpline0("Mon can't survive here");
589.             if (at_portcullis)
590.                 must_jump = TRUE;
591.             else
592.                 relocates = TRUE; /* just ride drawbridge in */
593.         }
594.     } else {
595.         if (crm->typ == DRAWBRIDGE_DOWN) {
596.             if (is_u(etmp)) {
597.                 svk.killer.format = NO_KILLER_PREFIX;
598.                 Strcpy(svk.killer.name,
599.                        "crushed to death underneath a drawbridge");
600.             }
601.             pline("%s crushed underneath the drawbridge.",
602.                   E_phrase(etmp, "are"));             /* no jump */
603.             e_died(etmp,
604.                    XKILL_NOCORPSE | (e_inview ? XKILL_GIVEMSG : XKILL_NOMSG),
605.                    CRUSHING); /* no corpse */
606.             return;       /* Note: Beyond this point, we know we're  */
607.         }                 /* not at an opened drawbridge, since all  */
608.         must_jump = TRUE; /* *missable* creatures survive on the     */
609.     }                     /* square, and all the unmissed ones die.  */
610.     if (must_jump) {
611.         if (at_portcullis) {
612.             if (e_jumps(etmp)) {
613.                 relocates = TRUE;
614.                 debugpline0("Jump succeeds!");
615.             } else {
616.                 if (e_inview) {
617.                     pline("%s crushed by the falling portcullis!",
618.                           E_phrase(etmp, "are"));
619.                 } else if (!Deaf) {
620.                     Soundeffect(se_crushing_sound, 100);
621.                     You_hear("a crushing sound.");
622.                 }
623.                 e_died(etmp,
624.                        XKILL_NOCORPSE | (e_inview ? XKILL_GIVEMSG
625.                                                   : XKILL_NOMSG),
626.                        CRUSHING);
627.                 /* no corpse */
628.                 return;
629.             }
630.         } else { /* tries to jump off bridge to original square */
631.             relocates = !e_jumps(etmp);
632.             debugpline1("Jump %s!", (relocates) ? "fails" : "succeeds");
633.         }
634.     }
635. 
636.     /*
637.      * Here's where we try to do relocation.  Assumes that etmp is not
638.      * arriving at the portcullis square while the drawbridge is
639.      * falling, since this square would be inaccessible (i.e. etmp
640.      * started on drawbridge square) or unnecessary (i.e. etmp started
641.      * here) in such a situation.
642.      */
643.     debugpline0("Doing relocation.");
644.     newx = oldx;
645.     newy = oldy;
646.     (void) find_drawbridge(&newx, &newy);
647.     if ((newx == oldx) && (newy == oldy))
648.         get_wall_for_db(&newx, &newy);
649.     debugpline0("Checking new square for occupancy.");
650.     if (relocates && (e_at(newx, newy))) {
651.         /*
652.          * Standoff problem: one or both entities must die, and/or
653.          * both switch places.  Avoid infinite recursion by checking
654.          * first whether the other entity is staying put.  Clean up if
655.          * we happen to move/die in recursion.
656.          */
657.         struct entity *other;
658. 
659.         other = e_at(newx, newy);
660.         debugpline1("New square is occupied by %s", e_nam(other));
661.         if (e_survives_at(other, newx, newy) && automiss(other)) {
662.             relocates = FALSE; /* "other" won't budge */
663.             debugpline1("%s suicide.", E_phrase(etmp, "commit"));
664.         } else {
665.             debugpline1("Handling %s", e_nam(other));
666.             while ((e_at(newx, newy) != 0) && (e_at(newx, newy) != etmp))
667.                 do_entity(other);
668.             debugpline1("Checking existence of %s", e_nam(etmp));
669. #ifdef D_DEBUG
670.             wait_synch();
671. #endif
672.             if (e_at(oldx, oldy) != etmp) {
673.                 debugpline1("%s moved or died in recursion somewhere",
674.                             E_phrase(etmp, "have"));
675. #ifdef D_DEBUG
676.                 wait_synch();
677. #endif
678.                 return;
679.             }
680.         }
681.     }
682.     if (relocates && !e_at(newx, newy)) { /* if e_at() entity = worm tail */
683.         debugpline1("Moving %s", e_nam(etmp));
684.         if (!is_u(etmp)) {
685.             remove_monster(etmp->ex, etmp->ey);
686.             place_monster(etmp->emon, newx, newy);
687.             update_monster_region(etmp->emon);
688.         } else {
689.             u.ux = newx;
690.             u.uy = newy;
691.         }
692.         etmp->ex = newx;
693.         etmp->ey = newy;
694.         e_inview = e_canseemon(etmp);
695.     }
696.     debugpline1("Final disposition of %s", e_nam(etmp));
697. #ifdef D_DEBUG
698.     wait_synch();
699. #endif
700.     if (is_db_wall(etmp->ex, etmp->ey)) {
701.         debugpline1("%s in portcullis chamber", E_phrase(etmp, "are"));
702. #ifdef D_DEBUG
703.         wait_synch();
704. #endif
705.         if (e_inview) {
706.             if (is_u(etmp)) {
707.                 You("tumble towards the closed portcullis!");
708.                 if (automiss(etmp))
709.                     You("pass through it!");
710.                 else
711.                     pline_The("drawbridge closes in...");
712.             } else
713.                 pline("%s behind the drawbridge.",
714.                       E_phrase(etmp, "disappear"));
715.         }
716.         if (!e_survives_at(etmp, etmp->ex, etmp->ey)) {
717.             svk.killer.format = KILLED_BY_AN;
718.             Strcpy(svk.killer.name, "closing drawbridge");
719.             e_died(etmp, XKILL_NOMSG, CRUSHING);
720.             return;
721.         }
722.         debugpline1("%s in here", E_phrase(etmp, "survive"));
723.     } else {
724.         debugpline1("%s on drawbridge square", E_phrase(etmp, "are"));
725.         if (is_pool(etmp->ex, etmp->ey) && !e_inview)
726.             if (!Deaf) {
727.                 Soundeffect(se_splash, 100);
728.                 You_hear("a splash.");
729.             }
730.         if (e_survives_at(etmp, etmp->ex, etmp->ey)) {
731.             if (e_inview && !is_flyer(etmp->edata)
732.                 && !is_floater(etmp->edata))
733.                 pline("%s from the bridge.", E_phrase(etmp, "fall"));
734.             return;
735.         }
736.         debugpline1("%s cannot survive on the drawbridge square",
737.                     E_phrase(etmp, NULL));
738.         if (is_pool(etmp->ex, etmp->ey) || is_lava(etmp->ex, etmp->ey))
739.             if (e_inview && !is_u(etmp)) {
740.                 /* drown() will supply msgs if nec. */
741.                 boolean lava = is_lava(etmp->ex, etmp->ey);
742. 
743.                 if (Hallucination)
744.                     pline("%s the %s and disappears.",
745.                           E_phrase(etmp, "drink"), lava ? "lava" : "moat");
746.                 else
747.                     pline("%s into the %s.", E_phrase(etmp, "fall"),
748.                           lava ? hliquid("lava") : "moat");
749.             }
750.         svk.killer.format = NO_KILLER_PREFIX;
751.         Strcpy(svk.killer.name, "fell from a drawbridge");
752.         e_died(etmp, /* CRUSHING is arbitrary */
753.                XKILL_NOCORPSE | (e_inview ? XKILL_GIVEMSG : XKILL_NOMSG),
754.                is_pool(etmp->ex, etmp->ey) ? DROWNING
755.                  : is_lava(etmp->ex, etmp->ey) ? BURNING
756.                    : CRUSHING); /*no corpse*/
757.         return;
758.     }
759. }
760. 
761. /* clear stale reason for death and both 'entities' before returning */
762. staticfn void
763. nokiller(void)
764. {
765.     svk.killer.name[0] = '\0';
766.     svk.killer.format = 0;
767.     m_to_e((struct monst *) 0, 0, 0, &go.occupants[0]);
768.     m_to_e((struct monst *) 0, 0, 0, &go.occupants[1]);
769. }
770. 
771. /*
772.  * Close the drawbridge located at x,y
773.  */
774. void
775. close_drawbridge(coordxy x, coordxy y)
776. {
777.     struct rm *lev1, *lev2;
778.     struct trap *t;
779.     coordxy x2, y2;
780. 
781.     lev1 = &levl[x][y];
782.     if (lev1->typ != DRAWBRIDGE_DOWN)
783.         return;
784.     x2 = x;
785.     y2 = y;
786.     get_wall_for_db(&x2, &y2);
787.     if (cansee(x, y) || cansee(x2, y2)) {
788.         You_see("a drawbridge %s up!",
789.                 (((u.ux == x || u.uy == y) && !Underwater)
790.                  || distu(x2, y2) < distu(x, y))
791.                     ? "coming"
792.                     : "going");
793.     } else { /* "5 gears turn" for castle drawbridge tune */
794.         Soundeffect(se_chains_rattling_gears_turning, 75);
795.         You_hear("chains rattling and gears turning.");
796.     }
797.     lev1->typ = DRAWBRIDGE_UP;
798.     lev2 = &levl[x2][y2];
799.     lev2->typ = DBWALL;
800.     switch (lev1->drawbridgemask & DB_DIR) {
801.     case DB_NORTH:
802.     case DB_SOUTH:
803.         lev2->horizontal = TRUE;
804.         break;
805.     case DB_WEST:
806.     case DB_EAST:
807.         lev2->horizontal = FALSE;
808.         break;
809.     }
810.     lev2->wall_info = W_NONDIGGABLE;
811.     set_entity(x, y, &(go.occupants[0]));
812.     set_entity(x2, y2, &(go.occupants[1]));
813.     do_entity(&(go.occupants[0]));          /* Do set_entity after first */
814.     set_entity(x2, y2, &(go.occupants[1])); /* do_entity for worm tail */
815.     do_entity(&(go.occupants[1]));
816.     if (OBJ_AT(x, y) && !Deaf) {
817.         Soundeffect(se_smashing_and_crushing, 75);
818.         You_hear("smashing and crushing.");
819.     }
820.     (void) revive_nasty(x, y, (char *) 0);
821.     (void) revive_nasty(x2, y2, (char *) 0);
822.     delallobj(x, y);
823.     delallobj(x2, y2);
824.     if ((t = t_at(x, y)) != 0)
825.         deltrap(t);
826.     if ((t = t_at(x2, y2)) != 0)
827.         deltrap(t);
828.     del_engr_at(x, y);
829.     del_engr_at(x2, y2);
830.     newsym(x, y);
831.     newsym(x2, y2);
832.     block_point(x2, y2); /* vision */
833.     nokiller();
834. }
835. 
836. /*
837.  * Open the drawbridge located at x,y
838.  */
839. void
840. open_drawbridge(coordxy x, coordxy y)
841. {
842.     struct rm *lev1, *lev2;
843.     struct trap *t;
844.     coordxy x2, y2;
845. 
846.     lev1 = &levl[x][y];
847.     if (lev1->typ != DRAWBRIDGE_UP)
848.         return;
849.     x2 = x;
850.     y2 = y;
851.     get_wall_for_db(&x2, &y2);
852.     if (cansee(x, y) || cansee(x2, y2)) {
853.         You_see("a drawbridge %s down!",
854.                 (distu(x2, y2) < distu(x, y)) ? "going" : "coming");
855.     } else { /* "5 gears turn" for castle drawbridge tune */
856.         Soundeffect(se_gears_turning_chains_rattling, 100);
857.         You_hear("gears turning and chains rattling.");
858.     }
859.     lev1->typ = DRAWBRIDGE_DOWN;
860.     lev2 = &levl[x2][y2];
861.     lev2->typ = DOOR;
862.     lev2->doormask = D_NODOOR;
863.     set_entity(x, y, &(go.occupants[0]));
864.     set_entity(x2, y2, &(go.occupants[1]));
865.     do_entity(&(go.occupants[0]));          /* do set_entity after first */
866.     set_entity(x2, y2, &(go.occupants[1])); /* do_entity for worm tails */
867.     do_entity(&(go.occupants[1]));
868.     (void) revive_nasty(x, y, (char *) 0);
869.     delallobj(x, y);
870.     if ((t = t_at(x, y)) != 0)
871.         deltrap(t);
872.     if ((t = t_at(x2, y2)) != 0)
873.         deltrap(t);
874.     del_engr_at(x, y);
875.     del_engr_at(x2, y2);
876.     newsym(x, y);
877.     newsym(x2, y2);
878.     unblock_point(x2, y2); /* vision */
879.     if (Is_stronghold(&u.uz))
880.         u.uevent.uopened_dbridge = TRUE;
881.     nokiller();
882. }
883. 
884. /*
885.  * Let's destroy the drawbridge located at x,y
886.  */
887. void
888. destroy_drawbridge(coordxy x, coordxy y)
889. {
890.     struct rm *lev1, *lev2;
891.     struct trap *t;
892.     struct obj *otmp;
893.     coordxy x2, y2;
894.     int i;
895.     boolean e_inview;
896.     struct entity *etmp1 = &(go.occupants[0]), *etmp2 = &(go.occupants[1]);
897. 
898.     lev1 = &levl[x][y];
899.     if (!IS_DRAWBRIDGE(lev1->typ))
900.         return;
901.     x2 = x;
902.     y2 = y;
903.     get_wall_for_db(&x2, &y2);
904.     lev2 = &levl[x2][y2];
905.     if ((lev1->drawbridgemask & DB_UNDER) == DB_MOAT
906.         || (lev1->drawbridgemask & DB_UNDER) == DB_LAVA) {
907.         struct obj *otmp2;
908.         boolean lava = (lev1->drawbridgemask & DB_UNDER) == DB_LAVA;
909. 
910.         Soundeffect(se_loud_splash, 100);  /* Deaf-aware */
911.         if (lev1->typ == DRAWBRIDGE_UP) {
912.             if (cansee(x2, y2) || u_at(x2, y2))
913.                 pline_The("portcullis of the drawbridge falls into the %s!",
914.                           lava ? hliquid("lava") : "moat");
915.             else
916.                 You_hear("a loud *SPLASH*!");  /* Deaf-aware */
917.         } else {
918.             if (cansee(x, y) || u_at(x, y))
919.                 pline_The("drawbridge collapses into the %s!",
920.                           lava ? hliquid("lava") : "moat");
921.             else
922.                 You_hear("a loud *SPLASH*!");  /* Deaf-aware */
923.         }
924.         lev1->typ = lava ? LAVAPOOL : MOAT;
925.         lev1->drawbridgemask = 0;
926.         if ((otmp2 = sobj_at(BOULDER, x, y)) != 0) {
927.             obj_extract_self(otmp2);
928.             (void) flooreffects(otmp2, x, y, "fall");
929.         }
930.     } else {
931.         /* no moat beneath */
932.         Soundeffect(se_loud_crash, 100);  /* Deaf-aware */
933.         if (cansee(x, y) || u_at(x, y))
934.             pline_The("drawbridge disintegrates!");
935.         else
936.             You_hear("a loud *CRASH*!");  /* Deaf-aware */
937.         lev1->typ = ((lev1->drawbridgemask & DB_ICE) ? ICE : ROOM);
938.         lev1->icedpool = ((lev1->drawbridgemask & DB_ICE) ? ICED_MOAT : 0);
939.     }
940.     wake_nearto(x, y, 500);
941.     lev2->typ = DOOR;
942.     lev2->doormask = D_NODOOR;
943.     if ((t = t_at(x, y)) != 0)
944.         deltrap(t);
945.     if ((t = t_at(x2, y2)) != 0)
946.         deltrap(t);
947.     del_engr_at(x, y);
948.     del_engr_at(x2, y2);
949.     for (i = rn2(6); i > 0; --i) { /* scatter some debris */
950.         /* doesn't matter if we happen to pick <x,y2> or <x2,y>;
951.            since drawbridges are never placed diagonally, those
952.            pairings will always match one of <x,y> or <x2,y2> */
953.         otmp = mksobj_at(IRON_CHAIN, rn2(2) ? x : x2, rn2(2) ? y : y2, TRUE,
954.                          FALSE);
955.         /* a force of 5 here would yield a radius of 2 for
956.            iron chain; anything less produces a radius of 1 */
957.         (void) scatter(otmp->ox, otmp->oy, 1, MAY_HIT, otmp);
958.     }
959.     newsym(x, y);
960.     newsym(x2, y2);
961.     if (!does_block(x2, y2, lev2))
962.         unblock_point(x2, y2); /* vision */
963.     vision_recalc(0);
964.     if (Is_stronghold(&u.uz))
965.         u.uevent.uopened_dbridge = TRUE;
966. 
967.     set_entity(x2, y2, etmp2); /* currently only automissers can be here */
968.     if (etmp2->edata) {
969.         e_inview = e_canseemon(etmp2);
970.         if (!automiss(etmp2)) {
971.             if (e_inview)
972.                 pline("%s blown apart by flying debris.",
973.                       E_phrase(etmp2, "are"));
974.             svk.killer.format = KILLED_BY_AN;
975.             Strcpy(svk.killer.name, "exploding drawbridge");
976.             e_died(etmp2,
977.                    XKILL_NOCORPSE | (e_inview ? XKILL_GIVEMSG : XKILL_NOMSG),
978.                    CRUSHING); /*no corpse*/
979.         } /* nothing which is vulnerable can survive this */
980.     }
981.     set_entity(x, y, etmp1);
982.     if (etmp1->edata) {
983.         e_inview = e_canseemon(etmp1);
984.         if (e_missed(etmp1, TRUE)) {
985.             debugpline1("%s spared!", E_phrase(etmp1, "are"));
986.             /* if there is water or lava here, fall in now */
987.             if (is_u(etmp1))
988.                 spoteffects(FALSE);
989.             else
990.                 (void) minliquid(etmp1->emon);
991.         } else {
992.             if (e_inview) {
993.                 if (!is_u(etmp1) && Hallucination)
994.                     pline("%s into some heavy metal!",
995.                           E_phrase(etmp1, "get"));
996.                 else
997.                     pline("%s hit by a huge chunk of metal!",
998.                           E_phrase(etmp1, "are"));
999.             } else {
1000.                 if (!Deaf && !is_u(etmp1) && !is_pool(x, y)) {
1001.                     Soundeffect(se_crushing_sound, 75);
1002.                     You_hear("a crushing sound.");
1003.                 } else {
1004.                     debugpline1("%s from shrapnel", E_phrase(etmp1, "die"));
1005.                 }
1006.             }
1007.             svk.killer.format = KILLED_BY_AN;
1008.             Strcpy(svk.killer.name, "collapsing drawbridge");
1009.             e_died(etmp1,
1010.                    XKILL_NOCORPSE | (e_inview ? XKILL_GIVEMSG : XKILL_NOMSG),
1011.                    CRUSHING); /*no corpse*/
1012.             if (levl[etmp1->ex][etmp1->ey].typ == MOAT)
1013.                 do_entity(etmp1);
1014.         }
1015.     }
1016.     nokiller();
1017.     if (Is_stronghold(&u.uz))
1018.         u.uevent.uheard_tune = 3; /* bridge is gone so tune is now useless */
1019. }
1020. 
1021. /*dbridge.c*/