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