Source:NetHack 5.0.0/src/display.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to display.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/display.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	display.c	$NHDT-Date: 1777000050 2026/04/23 19:07:30 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.263 $ */
2. /* Copyright (c) Dean Luick, with acknowledgements to Kevin Darcy */
3. /* and Dave Cohrs, 1990.                                          */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /*
7.  *                      THE NEW DISPLAY CODE
8.  *
9.  * The old display code has been broken up into three parts: vision, display,
10.  * and drawing.  Vision decides what locations can and cannot be physically
11.  * seen by the hero.  Display decides _what_ is displayed at a given location.
12.  * Drawing decides _how_ to draw a monster, fountain, sword, etc.
13.  *
14.  * The display system uses information from the vision system to decide
15.  * what to draw at a given location.  The routines for the vision system
16.  * can be found in vision.c and vision.h.  The routines for display can
17.  * be found in this file (display.c) and display.h.  The drawing routines
18.  * are part of the window port.  See doc/window.txt for the drawing
19.  * interface.
20.  *
21.  * The display system deals with an abstraction called a glyph.  Anything
22.  * that could possibly be displayed has a unique glyph identifier.
23.  *
24.  * What is seen on the screen is a combination of what the hero remembers
25.  * and what the hero currently sees.  Objects and dungeon features (walls
26.  * doors, etc) are remembered when out of sight.  Monsters and temporary
27.  * effects are not remembered.  Each location on the level has an
28.  * associated glyph.  This is the hero's _memory_ of what he or she has
29.  * seen there before.
30.  *
31.  * Display rules:
32.  *
33.  *      If the location is in sight, display in order:
34.  *              visible (or sensed) monsters
35.  *              visible objects
36.  *              known traps
37.  *              background
38.  *
39.  *      If the location is out of sight, display in order:
40.  *              sensed monsters (via telepathy or persistent detection)
41.  *              warning (partly-sensed monster shown as an abstraction)
42.  *              memory
43.  *
44.  *      "Remembered, unseen monster" is handled like an object rather
45.  *      than a monster, and stays displayed whether or not it is in sight.
46.  *      It is removed when a visible or sensed or warned-of monster gets
47.  *      shown at its location or when searching or fighting reveals that
48.  *      no monster is there.
49.  *
50.  *
51.  * Here is a list of the major routines in this file to be used externally:
52.  *
53.  * newsym
54.  *
55.  * Possibly update the screen location (x,y).  This is the workhorse routine.
56.  * It is always correct --- where correct means following the in-sight/out-
57.  * of-sight rules.  **Most of the code should use this routine.**  This
58.  * routine updates the map and displays monsters.
59.  *
60.  *
61.  * map_background
62.  * map_object
63.  * map_trap or map_engraving
64.  * map_invisible
65.  * unmap_object
66.  *
67.  * If you absolutely must override the in-sight/out-of-sight rules, there
68.  * are two possibilities.  First, you can mess with vision to force the
69.  * location in sight then use newsym(), or you can  use the map_* routines.
70.  * The first has not been tried [no need] and the second is used in the
71.  * detect routines --- detect object, magic mapping, etc.  The map_*
72.  * routines *change* what the hero remembers.  All changes made by these
73.  * routines will be sticky --- they will survive screen redraws.  Do *not*
74.  * use these for things that only temporarily change the screen.  These
75.  * routines are also used directly by newsym().  unmap_object is used to
76.  * clear a remembered object when/if detection reveals it isn't there.
77.  *
78.  *
79.  * show_glyph
80.  *
81.  * This is direct (no processing in between) buffered access to the screen.
82.  * Temporary screen effects are run through this and its companion,
83.  * flush_screen().  There is yet a lower level routine, print_glyph(),
84.  * but this is unbuffered and graphic dependent (i.e. it must be surrounded
85.  * by graphic set-up and tear-down routines).  Do not use print_glyph().
86.  *
87.  *
88.  * see_monsters
89.  * see_objects
90.  * see_traps
91.  *
92.  * These are only used when something affects all of the monsters or
93.  * objects or traps.  For objects and traps, the only thing is hallucination.
94.  * For monsters, there are hallucination and changing from/to blindness, etc.
95.  *
96.  *
97.  * tmp_at
98.  *
99.  * This is a useful interface for displaying temporary items on the screen.
100.  * Its interface is different than previously, so look at it carefully.
101.  *
102.  *
103.  *
104.  * Parts of the rm structure that are used:
105.  *
106.  *      typ     - What is really there.
107.  *      glyph   - What the hero remembers.  This will never be a monster.
108.  *                Monsters "float" above this.
109.  *      lit     - True if the position is lit.  An optimization for
110.  *                lit/unlit rooms.
111.  *      waslit  - True if the position was *remembered* as lit.
112.  *      seenv   - A vector of bits representing the directions from which the
113.  *                hero has seen this position.  The vector's primary use is
114.  *                determining how walls are seen.  E.g. a wall sometimes looks
115.  *                like stone on one side, but is seen as wall from the other.
116.  *                Other uses are for unmapping detected objects and felt
117.  *                locations, where we need to know if the hero has ever
118.  *                seen the location.
119.  *      flags   - Additional information for the typ field.  Different for
120.  *                each typ.
121.  *      horizontal - Indicates whether the wall or door is horizontal or
122.  *                vertical.
123.  */
124. #include "hack.h"
125. 
126. staticfn void show_mon_or_warn(coordxy, coordxy, int);
127. staticfn void display_monster(coordxy, coordxy,
128.                               struct monst *, int, boolean) NONNULLPTRS;
129. staticfn int swallow_to_glyph(int, int);
130. staticfn void display_warning(struct monst *) NONNULLARG1;
131. staticfn boolean mon_overrides_region(struct monst *, coordxy, coordxy);
132. staticfn int check_pos(coordxy, coordxy, int);
133. staticfn void get_bkglyph_and_framecolor(coordxy x, coordxy y, int *,
134.                                          uint32 *);
135. staticfn int tether_glyph(coordxy, coordxy);
136. staticfn void mimic_light_blocking(struct monst *) NONNULLARG1;
137. 
138. /*#define WA_VERBOSE*/ /* give (x,y) locations for all "bad" spots */
139. #ifdef WA_VERBOSE
140. staticfn boolean more_than_one(coordxy, coordxy, coordxy, coordxy, coordxy);
141. #endif
142. 
143. staticfn int set_twall(coordxy, coordxy, coordxy, coordxy,
144.                        coordxy, coordxy, coordxy, coordxy);
145. staticfn int set_wall(coordxy, coordxy, int);
146. staticfn int set_corn(coordxy, coordxy, coordxy, coordxy,
147.                       coordxy, coordxy, coordxy, coordxy);
148. staticfn int set_crosswall(coordxy, coordxy);
149. staticfn void set_seenv(struct rm *, coordxy, coordxy, coordxy, coordxy);
150. staticfn void t_warn(struct rm *);
151. staticfn int wall_angle(struct rm *);
152. 
153. #define _glyph_at(x, y) gg.gbuf[y][x].glyphinfo.glyph
154. 
155. /*
156.  *      See display.h for descriptions of tp_sensemon() through
157.  *      is_safemon().  Some of these were generating an awful lot of
158.  *      code "behind the curtain", particularly canspotmon() (which is
159.  *      still a macro but one that now expands to a pair of function
160.  *      calls rather than to a ton of special case checks).  Return
161.  *      values are all int 0 or 1, not boolean.
162.  *
163.  *      They're still implemented as macros within this file.
164.  */
165. int
166. tp_sensemon(struct monst *mon)
167. {
168.     return _tp_sensemon(mon);
169. }
170. #define tp_sensemon(mon) _tp_sensemon(mon)
171. 
172. int
173. sensemon(struct monst *mon)
174. {
175.     return _sensemon(mon);
176. }
177. #define sensemon(mon) _sensemon(mon)
178. 
179. int
180. mon_warning(struct monst *mon)
181. {
182.     return _mon_warning(mon);
183. }
184. #define mon_warning(mon) _mon_warning(mon)
185. 
186. int
187. mon_visible(struct monst *mon)
188. {
189.     return _mon_visible(mon);
190. }
191. #define mon_visible(mon) _mon_visible(mon)
192. 
193. int
194. see_with_infrared(struct monst *mon)
195. {
196.     return _see_with_infrared(mon);
197. }
198. #define see_with_infrared(mon) _see_with_infrared(mon)
199. 
200. int
201. canseemon(struct monst *mon)
202. {
203.     return _canseemon(mon);
204. }
205. #define canseemon(mon) _canseemon(mon)
206. 
207. int
208. knowninvisible(struct monst *mon)
209. {
210.     return _knowninvisible(mon);
211. }
212. /* #define knowninvisible() isn't useful here */
213. 
214. int
215. is_safemon(struct monst *mon)
216. {
217.     return _is_safemon(mon);
218. }
219. /* #define is_safemon() isn't useful here */
220. 
221. /*
222.  *      End of former macro-only vision related (mostly) routines
223.  *      converted to functions.
224.  */
225. 
226. /*
227.  * magic_map_background()
228.  *
229.  * This function is similar to map_background (see below) except we pay
230.  * attention to and correct unexplored, lit ROOM and CORR spots.
231.  */
232. void
233. magic_map_background(coordxy x, coordxy y, int show)
234. {
235.     int glyph = back_to_glyph(x, y); /* assumes hero can see x,y */
236.     struct rm *lev = &levl[x][y];
237. 
238.     /*
239.      * Correct for out of sight lit corridors and rooms that the hero
240.      * doesn't remember as lit.
241.      */
242.     if (!cansee(x, y) && !lev->waslit) {
243.         /* Floor spaces are dark if unlit.  Corridors are dark if unlit. */
244.         if (lev->typ == ROOM && glyph == cmap_to_glyph(S_room))
245.             glyph = (flags.dark_room && iflags.use_color)
246.                         ? cmap_to_glyph(DARKROOMSYM)
247.                         : GLYPH_NOTHING;
248.         else if (lev->typ == CORR && glyph == cmap_to_glyph(S_litcorr))
249.             glyph = cmap_to_glyph(S_corr);
250.     }
251.     if (svl.level.flags.hero_memory
252.         && (glyph_is_unexplored(lev->glyph) || glyph_is_cmap(lev->glyph)))
253.         lev->glyph = glyph;
254.     if (show)
255.         show_glyph(x, y, glyph);
256. 
257.     update_lastseentyp(x, y);
258. }
259. 
260. /*
261.  * The routines map_background(), map_object(), and map_trap() could just
262.  * as easily be:
263.  *
264.  *      map_glyph(x,y,glyph,show)
265.  *
266.  * Which is called with the xx_to_glyph() in the call.  Then I can get
267.  * rid of 3 routines that don't do very much anyway.  And then stop
268.  * having to create fake objects and traps.  However, I am reluctant to
269.  * make this change.
270.  */
271. 
272. /*
273.  * map_background()
274.  *
275.  * Make the real background part of our map.  This routine assumes that
276.  * the hero can physically see the location.  Update the screen if directed.
277.  */
278. void
279. map_background(coordxy x, coordxy y, int show)
280. {
281.     int glyph = back_to_glyph(x, y);
282. 
283.     if (svl.level.flags.hero_memory)
284.         levl[x][y].glyph = glyph;
285.     if (show)
286.         show_glyph(x, y, glyph);
287. }
288. 
289. /*
290.  * map_trap()
291.  *
292.  * Map the trap and print it out if directed.  This routine assumes that the
293.  * hero can physically see the location.
294.  */
295. void
296. map_trap(struct trap *trap, int show)
297. {
298.     coordxy x = trap->tx, y = trap->ty;
299.     int glyph = trap_to_glyph(trap);
300. 
301.     if (svl.level.flags.hero_memory)
302.         levl[x][y].glyph = glyph;
303.     if (show)
304.         show_glyph(x, y, glyph);
305. }
306. 
307. /*
308.  * map_engraving()
309.  *
310.  * Map the engraving and print it out if directed.
311.  */
312. void
313. map_engraving(struct engr *ep, int show)
314. {
315.     coordxy x = ep->engr_x, y = ep->engr_y;
316.     int glyph = engraving_to_glyph(ep);
317. 
318.     if (svl.level.flags.hero_memory)
319.         levl[x][y].glyph = glyph;
320.     if (show)
321.         show_glyph(x, y, glyph);
322. }
323. 
324. /*
325.  * map_object()
326.  *
327.  * Map the given object.  This routine assumes that the hero can physically
328.  * see the location of the object.  Update the screen if directed.
329.  * [Note: feel_location() -> map_location() -> map_object() contradicts
330.  * the claim here that the hero can see obj's <ox,oy>.]
331.  */
332. void
333. map_object(struct obj *obj, int show)
334. {
335.     coordxy x = obj->ox, y = obj->oy;
336.     int glyph = obj_to_glyph(obj, newsym_rn2);
337. 
338.     /* if this object is already displayed as a generic object, it might
339.        become a specific one now */
340.     if (glyph_is_generic_object(glyph) && cansee(x, y) && !Hallucination) {
341.         /* these 'r' and 'neardist' calculations match distant_name(objnam.c)
342.            and see_nearby_objects(below); we assume that this is a lone
343.            object or a pile-top, not something below the top of a pile */
344.         int r = (u.xray_range > 2) ? u.xray_range : 2,
345.             /* neardist produces a small square with rounded corners */
346.             neardist = (r * r) * 2 - r; /* same as r*r + r*(r-1) */
347. 
348.         if (distu(x, y) <= neardist) {
349.             observe_object(obj);
350.             glyph = obj_to_glyph(obj, newsym_rn2);
351.         }
352.     }
353. 
354.     if (svl.level.flags.hero_memory) {
355.         /* MRKR: While hallucinating, statues are seen as random monsters */
356.         /*       but remembered as random objects.                        */
357. 
358.         if (Hallucination && obj->otyp == STATUE) {
359.             levl[x][y].glyph = random_obj_to_glyph(newsym_rn2);
360.         } else {
361.             levl[x][y].glyph = glyph;
362.         }
363.     }
364.     if (show)
365.         show_glyph(x, y, glyph);
366. }
367. 
368. /*
369.  * map_invisible()
370.  *
371.  * Make the hero remember that a square contains an invisible monster.
372.  * This is a special case in that the square will continue to be displayed
373.  * this way even when the hero is close enough to see it.  To get rid of
374.  * this and display the square's actual contents, use unmap_object() followed
375.  * by newsym() if necessary.
376.  */
377. void
378. map_invisible(coordxy x, coordxy y)
379. {
380.     if (x != u.ux || y != u.uy) { /* don't display I at hero's location */
381.         if (svl.level.flags.hero_memory)
382.             levl[x][y].glyph = GLYPH_INVISIBLE;
383.         show_glyph(x, y, GLYPH_INVISIBLE);
384.     }
385. }
386. 
387. boolean
388. unmap_invisible(coordxy x, coordxy y)
389. {
390.     if (isok(x,y) && glyph_is_invisible(levl[x][y].glyph)) {
391.         unmap_object(x, y);
392.         newsym(x, y);
393.         return TRUE;
394.     }
395.     return FALSE;
396. }
397. 
398. 
399. /*
400.  * unmap_object()
401.  *
402.  * Remove something from the map when the hero realizes it's not there
403.  * anymore.  Replace it with background or known trap, but not with
404.  * any other remembered object.  If this is used for detection, a full
405.  * screen update is imminent anyway; if this is used to get rid of an
406.  * invisible monster notation, we might have to call newsym().
407.  */
408. void
409. unmap_object(coordxy x, coordxy y)
410. {
411.     struct trap *trap;
412.     struct engr *ep;
413. 
414.     if (!svl.level.flags.hero_memory)
415.         return;
416. 
417.     if ((trap = t_at(x, y)) != 0 && trap->tseen && !covers_traps(x, y)) {
418.         map_trap(trap, 0);
419.     } else if (levl[x][y].seenv) {
420.         struct rm *lev = &levl[x][y];
421. 
422.         if (spot_shows_engravings(x, y)
423.             && (ep = engr_at(x, y)) != 0 && !covers_traps(x, y)) {
424.             if (cansee(x, y))
425.                 ep->erevealed = 1;
426.             map_engraving(ep, 0);
427.         } else {
428.             map_background(x, y, 0);
429.         }
430. 
431.         /* turn remembered dark room squares dark */
432.         if (!lev->waslit && lev->glyph == cmap_to_glyph(S_room)
433.             && lev->typ == ROOM)
434.             lev->glyph = cmap_to_glyph(S_stone);
435.     } else {
436.         levl[x][y].glyph = cmap_to_glyph(S_stone); /* default val */
437.     }
438. }
439. 
440. /*
441.  * map_location()
442.  *
443.  * Make whatever at this location show up.  This is only for non-living
444.  * things.  This will not handle feeling invisible objects correctly.
445.  *
446.  * Internal to display.c, this is a #define for speed.
447.  */
448. #define _map_location(x, y, show) \
449.     do {                                                                \
450.         struct obj *obj;                                                \
451.         struct trap *trap;                                              \
452.         struct engr *ml_ep;                                             \
453.         NhRegion *_ml_reg;                                              \
454.                                                                         \
455.         if ((obj = vobj_at(x, y)) && !covers_objects(x, y)) {           \
456.             map_object(obj, show);                                      \
457.         } else if ((trap = t_at(x, y))                                  \
458.                     && trap->tseen && !covers_traps(x, y)) {            \
459.             map_trap(trap, show);                                       \
460.         } else if (spot_shows_engravings(x, y)                          \
461.                  && (ml_ep = engr_at(x, y)) != 0                        \
462.                  && ml_ep->erevealed                                    \
463.                  && !covers_traps(x, y)) {                              \
464.             map_engraving(ml_ep, show);                                 \
465.         } else {                                                        \
466.             map_background(x, y, show);                                 \
467.         }                                                               \
468.                                                                         \
469.         update_lastseentyp(x, y);                                       \
470.         if (show && !Blind && (_ml_reg = visible_region_at(x, y)) != 0) \
471.             show_region(_ml_reg, x, y);                                 \
472.     } while (0)
473. 
474. void
475. map_location(coordxy x, coordxy y, int show)
476. {
477.     _map_location(x, y, show);
478. }
479. 
480. /* display something on monster layer; may need to fixup object layer */
481. staticfn void
482. show_mon_or_warn(coordxy x, coordxy y, int monglyph)
483. {
484.     struct obj *o;
485. 
486.     /* "remembered, unseen monster" is tracked by object layer so if we're
487.        putting something on monster layer at same spot, stop remembering
488.        that; if an object is in view there, start remembering it instead */
489.     if (glyph_is_invisible(levl[x][y].glyph)) {
490.         unmap_object(x, y);
491.         if (cansee(x, y) && (o = vobj_at(x, y)) != 0)
492.             map_object(o, FALSE);
493.     }
494. 
495.     show_glyph(x, y, monglyph);
496. }
497. 
498. #define DETECTED 2
499. #define PHYSICALLY_SEEN 1
500. #define is_worm_tail(mon) ((mon) && ((x != (mon)->mx) || (y != (mon)->my)))
501. 
502. /*
503.  * display_monster()
504.  *
505.  * Note that this is *not* a map_XXXX() function!  Monsters sort of float
506.  * above everything.
507.  *
508.  * Yuck.  Display body parts by recognizing that the display position is
509.  * not the same as the monster position.  Currently the only body part is
510.  * a worm tail.
511.  *
512.  */
513. staticfn void
514. display_monster(
515.     coordxy x, coordxy y,   /* display position */
516.     struct monst *mon,      /* monster to display */
517.     int sightflags,         /* 1 if the monster is physically seen;
518.                              * 2 if detected using Detect_monsters */
519.     boolean worm_tail)      /* mon is actually a worm tail */
520. {
521.     boolean mon_mimic = (M_AP_TYPE(mon) != M_AP_NOTHING);
522.     int sensed = (mon_mimic && (Protection_from_shape_changers
523.                                 || sensemon(mon))),
524.         mgendercode = mon->female ? FEMALE : MALE;
525. 
526.     /*
527.      * We must do the mimic check first.  If the mimic is mimicking something,
528.      * and the location is in sight, we have to change the hero's memory
529.      * so that when the position is out of sight, the hero remembers what
530.      * the mimic was mimicking.
531.      */
532.     if (mon_mimic && (sightflags == PHYSICALLY_SEEN)) {
533.         switch (M_AP_TYPE(mon)) {
534.         default:
535.             impossible("display_monster:  bad m_ap_type value [ = %d ]",
536.                        (int) mon->m_ap_type);
537.             FALLTHROUGH;
538.             /*FALLTHRU*/
539.         case M_AP_NOTHING:
540.             show_glyph(x, y, mon_to_glyph(mon, newsym_rn2));
541.             break;
542. 
543.         case M_AP_FURNITURE: {
544.             /*
545.              * This is a poor man's version of map_background().  I can't
546.              * use map_background() because we are overriding what is in
547.              * the 'typ' field.  Maybe have map_background()'s parameters
548.              * be (x,y,glyph) instead of just (x,y).
549.              *
550.              * mappearance is currently set to an S_ index value in
551.              * makemon.c.
552.              */
553.             int sym = mon->mappearance, glyph = cmap_to_glyph(sym);
554. 
555.             levl[x][y].glyph = glyph;
556.             if (!sensed) {
557.                 show_glyph(x, y, glyph);
558.                 /* override real topology with mimic's fake one */
559.                 svl.lastseentyp[x][y] = cmap_to_type(sym);
560.             }
561.             break;
562.         }
563. 
564.         case M_AP_OBJECT: {
565.             /* Make a fake object to send to map_object(). */
566.             struct obj obj;
567. 
568.             obj = cg.zeroobj;
569.             obj.ox = x;
570.             obj.oy = y;
571.             obj.otyp = mon->mappearance;
572.             /* might be mimicking a corpse or statue */
573.             obj.corpsenm = has_mcorpsenm(mon) ? MCORPSENM(mon) : PM_TENGU;
574.             map_object(&obj, !sensed);
575.             break;
576.         }
577. 
578.         case M_AP_MONSTER: {
579.             int mndx = what_mon((int) mon->mappearance, rn2_on_display_rng);
580. 
581.             show_glyph(x, y, monnum_to_glyph(mndx, mgendercode));
582.             break;
583.         } /* case M_AP_MONSTER */
584.         } /* switch M_AP_TYPE() */
585.     }
586. 
587.     /* If mimic is unsuccessfully mimicking something, display the monster. */
588.     if (!mon_mimic || sensed) {
589.         int num;
590. 
591.         /* [ALI] Only use detected glyphs when monster wouldn't be
592.          * visible by any other means.
593.          *
594.          * There are no glyphs for "detected pets" so we have to
595.          * decide whether to display such things as detected or as tame.
596.          * If both are being highlighted in the same way, it doesn't
597.          * matter, but if not, showing them as pets is preferrable.
598.          */
599.         if (mon->mtame && !Hallucination) {
600.             if (worm_tail)
601.                 num = petnum_to_glyph(PM_LONG_WORM_TAIL, mgendercode);
602.             else
603.                 num = pet_to_glyph(mon, rn2_on_display_rng);
604.         } else if (sightflags == DETECTED) {
605.             if (worm_tail)
606.                 num = detected_monnum_to_glyph(what_mon(PM_LONG_WORM_TAIL,
607.                                                         rn2_on_display_rng),
608.                                                mgendercode);
609.             else
610.                 num = detected_mon_to_glyph(mon, rn2_on_display_rng);
611.         } else {
612.             if (worm_tail)
613.                 num = monnum_to_glyph(what_mon(PM_LONG_WORM_TAIL,
614.                                                rn2_on_display_rng),
615.                                       mgendercode);
616.             else
617.                 num = mon_to_glyph(mon, rn2_on_display_rng);
618.         }
619.         show_mon_or_warn(x, y, num);
620.         mon->meverseen = 1;
621.     }
622. }
623. 
624. /*
625.  * display_warning()
626.  *
627.  * This is also *not* a map_XXXX() function!  Monster warnings float
628.  * above everything just like monsters do, but only if the monster
629.  * is not showing.
630.  *
631.  * Do not call for worm tails.
632.  */
633. staticfn void
634. display_warning(struct monst *mon)
635. {
636.     coordxy x = mon->mx, y = mon->my;
637.     int glyph;
638. 
639.     if (mon_warning(mon)) {
640.         int wl = Hallucination ? rn2_on_display_rng(WARNCOUNT - 1) + 1
641.                                : warning_of(mon);
642. 
643.         glyph = warning_to_glyph(wl);
644.     } else if (MATCH_WARN_OF_MON(mon)) {
645.         glyph = mon_to_glyph(mon, rn2_on_display_rng);
646.     } else {
647.         impossible("display_warning did not match warning type?");
648.         return;
649.     }
650.     show_mon_or_warn(x, y, glyph);
651. }
652. 
653. int
654. warning_of(struct monst *mon)
655. {
656.     int wl = 0, tmp = 0;
657. 
658.     if (mon_warning(mon)) {
659.         tmp = (int) (mon->m_lev / 4);    /* match display.h */
660.         wl = (tmp > WARNCOUNT - 1) ? WARNCOUNT - 1 : tmp;
661.     }
662.     return wl;
663. }
664. 
665. /* used by newsym() to decide whether to show a monster or a visible gas
666.    cloud region when both are at the same spot; caller deals with region */
667. staticfn boolean
668. mon_overrides_region(
669.     struct monst *mon, /* might be Null */
670.     coordxy mx, coordxy my) /* won't match mon->mx,my if long worm's tail */
671. {
672.     int r;
673. 
674.     /* this is redundant because newsym() doesn't call us when swallowed */
675.     if (u.uswallow && (!mon || mon != u.ustuck))
676.         return FALSE;
677. 
678.     if (mon) {
679.         /* when not a worm tail, show mon if sensed rather than seen */
680.         if (mx == mon->mx && my == mon->my
681.             && (sensemon(mon) || mon_warning(mon)))
682.             return TRUE;
683. 
684.         /* even if worm tail;
685.            check whether the spot is adjacent and 'mon' would be visible
686.            there if the gas cloud wasn't interfering with normal vision;
687.            _mon_visible() handles mon->mundetected; don't need to check
688.            infravision when monster is adjacent */
689.         r = (u.xray_range > 1) ? u.xray_range : 1;
690.         if (!Blind && _mon_visible(mon)
691.             && M_AP_TYPE(mon) != M_AP_FURNITURE
692.             && M_AP_TYPE(mon) != M_AP_OBJECT
693.             && distu(mx, my) <= r * (r + 1))
694.             return TRUE;
695.     }
696. 
697.     /* if not overriding region for current mon, propagate "remembered,
698.        unseen monster" */
699.     return glyph_is_invisible(levl[mx][my].glyph) ? TRUE : FALSE;
700. }
701. 
702. #ifdef HANGUPHANDLING
703. #define _suppress_map_output() \
704.     (gi.in_mklev || program_state.saving || program_state.restoring     \
705.      || program_state.done_hup)
706. #else
707. #define _suppress_map_output() \
708.     (gi.in_mklev || program_state.saving || program_state.restoring)
709. #endif
710. 
711. /* map or status window might not be ready for output during level creation
712.    or game restoration (something like u.usteed which affects display of
713.    the hero and also a status condition might not be set up yet) */
714. boolean
715. suppress_map_output(void)
716. {
717.     return _suppress_map_output();
718. }
719. 
720. /*
721.  * feel_newsym()
722.  *
723.  * When hero knows what happened to location, even when blind.
724.  */
725. void
726. feel_newsym(coordxy x, coordxy y)
727. {
728.     if (Blind)
729.         feel_location(x, y);
730.     else
731.         newsym(x, y);
732. }
733. 
734. 
735. /*
736.  * feel_location()
737.  *
738.  * Feel the given location.  This assumes that the hero is blind and that
739.  * the given position is either the hero's or one of the eight squares
740.  * adjacent to the hero (except for a boulder push).
741.  * If an invisible monster has gone away, that will be discovered.  If an
742.  * invisible monster has appeared, this will _not_ be discovered since
743.  * searching only finds one monster per turn so we must check that separately.
744.  */
745. void
746. feel_location(coordxy x, coordxy y)
747. {
748.     struct rm *lev;
749.     struct obj *boulder;
750.     struct monst *mon;
751.     struct engr *ep;
752. 
753.     /* replicate safeguards used by newsym(); might not be required here */
754.     if (_suppress_map_output())
755.         return;
756.     if (!isok(x, y))
757.         return;
758.     lev = &levl[x][y];
759.     /* If hero's memory of an invisible monster is accurate, we want to keep
760.      * him from detecting the same monster over and over again on each turn.
761.      * We must return (so we don't erase the monster).  (We must also, in the
762.      * search function, be sure to skip over previously detected 'I's.)
763.      */
764.     if (glyph_is_invisible(lev->glyph) && m_at(x, y))
765.         return;
766. 
767.     /* The hero can't feel non pool locations while under water
768.        except for lava and ice. */
769.     if (Underwater && !Is_waterlevel(&u.uz)
770.         && !is_pool_or_lava(x, y) && !is_ice(x, y))
771.         return;
772. 
773.     /* Set the seen vector as if the hero had seen it.
774.        It doesn't matter if the hero is levitating or not. */
775.     set_seenv(lev, u.ux, u.uy, x, y);
776. 
777.     if (!can_reach_floor(FALSE)) {
778.         /*
779.          * Levitation Rules.  It is assumed that the hero can feel the state
780.          * of the walls around herself and can tell if she is in a corridor,
781.          * room, or doorway.  Boulders are felt because they are large enough.
782.          * Anything else is unknown because the hero can't reach the ground.
783.          * This makes things difficult.
784.          *
785.          * Check (and display) in order:
786.          *
787.          *      + Stone, walls, and closed doors.
788.          *      + Boulders.  [see a boulder before a doorway]
789.          *      + doors.
790.          *      + Room/water positions
791.          *      + Everything else (hallways!)
792.          */
793.         if (IS_OBSTRUCTED(lev->typ)
794.             || (IS_DOOR(lev->typ)
795.                 && (lev->doormask & (D_LOCKED | D_CLOSED)))) {
796.             map_background(x, y, 1);
797.         } else if ((boulder = sobj_at(BOULDER, x, y)) != 0) {
798.             map_object(boulder, 1);
799.         } else if (IS_DOOR(lev->typ)) {
800.             map_background(x, y, 1);
801.         } else if (IS_ROOM(lev->typ) || IS_POOL(lev->typ)) {
802.             boolean do_room_glyph;
803. 
804.             /*
805.              * An open room or water location.  Normally we wouldn't touch
806.              * this, but we have to get rid of remembered boulder symbols.
807.              * This will only occur in rare occasions when the hero goes
808.              * blind and doesn't find a boulder where expected (something
809.              * came along and picked it up).  We know that there is not a
810.              * boulder at this location.  Show fountains, pools, etc.
811.              * underneath if already seen.  Otherwise, show the appropriate
812.              * floor symbol.
813.              *
814.              * Similarly, if the hero digs a hole in a wall or feels a
815.              * location that used to contain an unseen monster.  In these
816.              * cases, there's no reason to assume anything was underneath,
817.              * so just show the appropriate floor symbol.  If something was
818.              * embedded in the wall, the glyph will probably already
819.              * reflect that.  Don't change the symbol in this case.
820.              *
821.              * This isn't quite correct.  If the boulder was on top of some
822.              * other objects they should be seen once the boulder is removed.
823.              * However, we have no way of knowing that what is there now
824.              * was there then.  So we let the hero have a lapse of memory.
825.              * We could also just display what is currently on the top of the
826.              * object stack (if anything).
827.              */
828.             do_room_glyph = FALSE;
829.             if (lev->glyph == objnum_to_glyph(BOULDER)
830.                 || glyph_is_invisible(lev->glyph)) {
831.                 if (lev->typ != ROOM && lev->seenv)
832.                     map_background(x, y, 1);
833.                 else
834.                     do_room_glyph = TRUE;
835.             } else if (lev->glyph >= cmap_to_glyph(S_stone)
836.                        && lev->glyph < cmap_to_glyph(S_darkroom)) {
837.                 do_room_glyph = TRUE;
838.             }
839.             if (do_room_glyph) {
840.                 lev->glyph = (flags.dark_room && iflags.use_color
841.                               && !Is_rogue_level(&u.uz))
842.                                  ? cmap_to_glyph(S_darkroom)
843.                                  : (lev->waslit ? cmap_to_glyph(S_room)
844.                                                 : cmap_to_glyph(S_stone));
845.                 show_glyph(x, y, lev->glyph);
846.             }
847.         } else {
848.             /* We feel it (I think hallways are the only things left). */
849.             map_background(x, y, 1);
850.             /* Corridors are never felt as lit (unless remembered that way) */
851.             /* (lit_corridor only).                                         */
852.             if (lev->typ == CORR && lev->glyph == cmap_to_glyph(S_litcorr)
853.                 && !lev->waslit)
854.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(S_corr));
855.             else if (lev->typ == ROOM && flags.dark_room && iflags.use_color
856.                      && lev->glyph == cmap_to_glyph(S_room))
857.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(S_darkroom));
858.         }
859.     } else {
860.         if ((ep = engr_at(x, y)) != 0 && engr_can_be_felt(ep))
861.             ep->erevealed = 1;
862. 
863.         _map_location(x, y, 1);
864. 
865.         if (Punished) {
866.             /*
867.              * A ball or chain is only felt if it is first on the object
868.              * location list.  Otherwise, we need to clear the felt bit ---
869.              * something has been dropped on the ball/chain.  If the bit is
870.              * not cleared, then when the ball/chain is moved it will drop
871.              * the wrong glyph.
872.              *
873.              * Note: during unpunish() we can be called by delobj() when
874.              * destroying uchain while uball hasn't been cleared yet (so
875.              * Punished will still yield True but uchain might not be part
876.              * of the floor list anymore).
877.              */
878.             if (uchain && uchain->where == OBJ_FLOOR
879.                 && uchain->ox == x && uchain->oy == y
880.                 && svl.level.objects[x][y] == uchain)
881.                 u.bc_felt |= BC_CHAIN;
882.             else
883.                 u.bc_felt &= ~BC_CHAIN; /* do not feel the chain */
884. 
885.             if (uball && uball->where == OBJ_FLOOR
886.                 && uball->ox == x && uball->oy == y
887.                 && svl.level.objects[x][y] == uball)
888.                 u.bc_felt |= BC_BALL;
889.             else
890.                 u.bc_felt &= ~BC_BALL; /* do not feel the ball */
891.         }
892. 
893.         /* Floor spaces are dark if unlit.  Corridors are dark if unlit. */
894.         if (lev->typ == ROOM && lev->glyph == cmap_to_glyph(S_room)
895.             && (!lev->waslit || (flags.dark_room && iflags.use_color)))
896.             show_glyph(x, y, lev->glyph = cmap_to_glyph(
897.                                  flags.dark_room ? S_darkroom : S_stone));
898.         else if (lev->typ == CORR && lev->glyph == cmap_to_glyph(S_litcorr)
899.                  && !lev->waslit)
900.             show_glyph(x, y, lev->glyph = cmap_to_glyph(S_corr));
901.     }
902.     /* draw monster on top if we can sense it */
903.     if (!u_at(x, y) && (mon = m_at(x, y)) != 0 && sensemon(mon))
904.         display_monster(x, y, mon,
905.                         (tp_sensemon(mon) || MATCH_WARN_OF_MON(mon))
906.                             ? PHYSICALLY_SEEN
907.                             : DETECTED,
908.                         is_worm_tail(mon));
909. }
910. 
911. /*
912.  * newsym()
913.  *
914.  * Possibly put a new glyph at the given location.
915.  */
916. void
917. newsym(coordxy x, coordxy y)
918. {
919.     struct rm *lev;
920.     struct engr *ep;
921.     struct monst *mon;
922.     int see_it;
923.     boolean worm_tail;
924. 
925.     /* don't try to produce map output when level is in a state of flux */
926.     if (_suppress_map_output())
927.         return;
928.     /* should never happen; same error handling as u_on_newpos() */
929.     if (!isok(x, y)) {
930.         void (*errfunc)(const char *, ...) PRINTF_F_PTR(1, 2);
931. 
932.         errfunc = (x < 0 || y < 0 || x > COLNO - 1 || y > ROWNO - 1) ? panic
933.                   : impossible; /* misuse of column 0 is less severe */
934.         (*errfunc)("newsym: attempting screen update for <%d,%d>", x, y);
935.         return;
936.     }
937. 
938.     /* only permit updating the hero when swallowed */
939.     if (u.uswallow) {
940.         if (u_at(x, y))
941.             display_self();
942.         return;
943.     }
944.     if (Underwater && !Is_waterlevel(&u.uz)) {
945.         /* when underwater, don't do anything unless <x,y> is an
946.            adjacent water or lava or ice position */
947.         if (!(is_pool_or_lava(x, y) || is_ice(x, y)) || !next2u(x, y))
948.             return;
949.     }
950.     lev = &levl[x][y];
951. 
952.     /* Can physically see the location. */
953.     if (cansee(x, y)) {
954.         NhRegion *reg = visible_region_at(x, y);
955.         /*
956.          * Don't use templit here:  E.g.
957.          *
958.          *      lev->waslit = !!(lev->lit || templit(x,y));
959.          *
960.          * Otherwise we have the "light pool" problem, where non-permanently
961.          * lit areas just out of sight stay remembered as lit.  They should
962.          * re-darken.
963.          *
964.          * Perhaps ALL areas should revert to their "unlit" look when
965.          * out of sight.
966.          */
967.         lev->waslit = (lev->lit != 0); /* remember lit condition */
968. 
969.         mon = m_at(x, y);
970.         worm_tail = is_worm_tail(mon);
971. 
972.         if ((ep = engr_at(x, y)) != 0)
973.             ep->erevealed = 1; /* even when covered by objects or a monster */
974.         /*
975.          * Normal region shown only on accessible positions, but
976.          * poison clouds and steam clouds also shown above lava,
977.          * pools and moats.
978.          * However, sensed monsters (via detection or telepathy or
979.          * warning) take precedence over all regions.
980.          * Adjacent monsters also take precedence if they would be
981.          * seen when there's no gas region.
982.          *
983.          * FIXME:
984.          *  The adjacency checking [in mon_overrides_region()] works
985.          *  when the hero is outside the region and the monster is
986.          *  inside, and when they're both inside, but not when the
987.          *  hero is inside and monster outside (because 'reg' will be
988.          *  Null for mon's <x,y>).  Checking whether hero is inside
989.          *  a region for every newsym() seems excessive.  The hero is
990.          *  usually blind when in a gas cloud so the problem is less
991.          *  noticeable then it might otherwise be.
992.          */
993.         if (reg && (ACCESSIBLE(lev->typ)
994.                     || (reg->visible && is_pool_or_lava(x, y)))) {
995.             if (!mon_overrides_region(mon, x, y)) {
996.                 show_region(reg, x, y);
997.                 return;
998.             }
999.         }
1000. 
1001.         if (u_at(x, y)) {
1002.             int see_self = canspotself();
1003. 
1004.             /* update map information for <u.ux,u.uy> (remembered topology
1005.                and object/known trap/terrain glyph) but only display it if
1006.                hero can't see him/herself, then show self if appropriate */
1007.             _map_location(x, y, !see_self);
1008.             if (see_self)
1009.                 display_self();
1010.         } else {
1011.             boolean show = FALSE;
1012. 
1013.             see_it = mon && (mon_visible(mon)
1014.                              || (!worm_tail && (tp_sensemon(mon)
1015.                                                 || MATCH_WARN_OF_MON(mon))));
1016.             if (mon && (see_it || (!worm_tail && Detect_monsters))) {
1017.                 if (mon->mtrapped) {
1018.                     struct trap *trap = t_at(x, y);
1019.                     int tt = trap ? trap->ttyp : NO_TRAP;
1020. 
1021.                     /* if monster is in a physical trap, you see trap too */
1022.                     if (tt == BEAR_TRAP || is_pit(tt) || tt == WEB)
1023.                         trap->tseen = 1;
1024.                 }
1025.                 _map_location(x, y, show); /* map under the monster */
1026.                 /* also gets rid of any invisibility glyph */
1027.                 display_monster(x, y, mon,
1028.                                 see_it ? PHYSICALLY_SEEN : DETECTED,
1029.                                 worm_tail);
1030.             } else if (mon && mon_warning(mon) && !worm_tail) {
1031.                 display_warning(mon);
1032.             } else if (glyph_is_invisible(lev->glyph)) {
1033.                 map_invisible(x, y);
1034.             } else {
1035.                 _map_location(x, y, 1); /* map the location */
1036.             }
1037.         }
1038. 
1039.     /* Can't see the location. */
1040.     } else {
1041.         if (u_at(x, y)) {
1042.             feel_location(u.ux, u.uy); /* forces an update */
1043. 
1044.             if (canspotself())
1045.                 display_self();
1046.         } else if ((mon = m_at(x, y)) != 0
1047.                    && ((see_it = (tp_sensemon(mon) || MATCH_WARN_OF_MON(mon)
1048.                                   || (see_with_infrared(mon)
1049.                                       && mon_visible(mon)))) != 0
1050.                        || (Detect_monsters && !is_worm_tail(mon)))) {
1051.             /* Seen or sensed monsters are printed every time.
1052.                This also gets rid of any invisibility glyph. */
1053.             display_monster(x, y, mon, see_it ? 0 : DETECTED,
1054.                             is_worm_tail(mon) ? TRUE : FALSE);
1055.         } else if (mon && mon_warning(mon) && !is_worm_tail(mon)) {
1056.             display_warning(mon);
1057. 
1058.         /*
1059.          * If the location is remembered as being both dark (waslit is false)
1060.          * and lit (glyph is a lit room or lit corridor) then it was either:
1061.          *
1062.          *      (1) A dark location that the hero could see through night
1063.          *          vision.
1064.          *      (2) Darkened while out of the hero's sight.  This can happen
1065.          *          when cursed scroll of light is read.
1066.          *
1067.          * In either case, we have to manually correct the hero's memory to
1068.          * match waslit.  Deciding when to change waslit is non-trivial.
1069.          *
1070.          *  Note:  If flags.lit_corridor is set, then corridors act like room
1071.          *         squares.  That is, they light up if in night vision range.
1072.          *         If flags.lit_corridor is not set, then corridors will
1073.          *         remain dark unless lit by a light spell and may darken
1074.          *         again, as discussed above.
1075.          *
1076.          * These checks and changes must be here and not in back_to_glyph().
1077.          * They are dependent on the position being out of sight.
1078.          */
1079.         } else if (Is_rogue_level(&u.uz)) {
1080.             if (lev->glyph == cmap_to_glyph(S_litcorr) && lev->typ == CORR)
1081.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(S_corr));
1082.             else if (lev->glyph == cmap_to_glyph(S_room) && lev->typ == ROOM
1083.                      && !lev->waslit)
1084.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(S_stone));
1085.             else
1086.                 goto show_mem;
1087.         } else if (!lev->waslit || (flags.dark_room && iflags.use_color)) {
1088.             if (lev->glyph == cmap_to_glyph(S_litcorr) && lev->typ == CORR)
1089.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(S_corr));
1090.             else if (lev->glyph == cmap_to_glyph(S_room) && lev->typ == ROOM)
1091.                 show_glyph(x, y, lev->glyph = cmap_to_glyph(DARKROOMSYM));
1092.             else
1093.                 goto show_mem;
1094.         } else {
1095.  show_mem:
1096.             show_glyph(x, y, lev->glyph);
1097.         }
1098.     }
1099. }
1100. 
1101. #undef is_worm_tail
1102. 
1103. /*
1104.  * shieldeff()
1105.  *
1106.  * Put magic shield pyrotechnics at the given location.  This *could* be
1107.  * pulled into a platform dependent routine for fancier graphics if desired.
1108.  */
1109. void
1110. shieldeff(coordxy x, coordxy y)
1111. {
1112.     int i;
1113. 
1114.     if (!flags.sparkle)
1115.         return;
1116.     if (cansee(x, y)) { /* Don't see anything if can't see the location */
1117.         for (i = 0; i < SHIELD_COUNT; i++) {
1118.             show_glyph(x, y, cmap_to_glyph(shield_static[i]));
1119.             flush_screen(1); /* make sure the glyph shows up */
1120.             nh_delay_output();
1121.         }
1122.         newsym(x, y); /* restore the old information */
1123.     }
1124. }
1125. 
1126. staticfn int
1127. tether_glyph(coordxy x, coordxy y)
1128. {
1129.     int tdx, tdy;
1130.     tdx = u.ux - x;
1131.     tdy = u.uy - y;
1132.     return zapdir_to_glyph(sgn(tdx),sgn(tdy), 2);
1133. }
1134. 
1135. /*
1136.  * tmp_at()
1137.  *
1138.  * Temporarily place glyphs on the screen.  Do not call nh_delay_output().  It
1139.  * is up to the caller to decide if it wants to wait [presently, everyone
1140.  * but explode() wants to delay].
1141.  *
1142.  * Call:
1143.  *      (DISP_BEAM,    glyph)   open, initialize glyph
1144.  *      (DISP_FLASH,   glyph)   open, initialize glyph
1145.  *      (DISP_ALWAYS,  glyph)   open, initialize glyph
1146.  *      (DISP_CHANGE,  glyph)   change glyph
1147.  *      (DISP_END,     0)       close & clean up (2nd argument doesn't matter)
1148.  *      (DISP_FREEMEM, 0)       only used to prevent memory leak during exit)
1149.  *      (x, y)                  display the glyph at the location
1150.  *
1151.  * DISP_BEAM   - Display the given glyph at each location, but do not erase
1152.  *               any until the close call.
1153.  * DISP_ALL    - Same as DISP_BEAM except glyph is shown at the specified
1154.  *               spot even when that spot can't be seen.
1155.  * DISP_TETHER - Display a tether glyph at each location, and the tethered
1156.  *               object at the farthest location, but do not erase any
1157.  *               until the return trip or close.
1158.  * DISP_FLASH  - Display the given glyph at each location, but erase the
1159.  *               previous location's glyph.
1160.  * DISP_ALWAYS - Like DISP_FLASH, but vision is not taken into account.
1161.  */
1162. 
1163. #define TMP_AT_MAX_GLYPHS (COLNO * 2)
1164. 
1165. static struct tmp_glyph {
1166.     coord saved[TMP_AT_MAX_GLYPHS]; /* previously updated positions */
1167.     int sidx;                       /* index of next unused slot in saved[] */
1168.     int style; /* either DISP_BEAM or DISP_FLASH or DISP_ALWAYS */
1169.     int glyph; /* glyph to use when printing */
1170.     struct tmp_glyph *prev;
1171. } tgfirst;
1172. 
1173. void
1174. tmp_at(coordxy x, coordxy y)
1175. {
1176.     static struct tmp_glyph *tglyph = (struct tmp_glyph *) 0;
1177.     struct tmp_glyph *tmp;
1178. 
1179.     switch (x) {
1180.     case DISP_BEAM:
1181.     case DISP_ALL:
1182.     case DISP_TETHER:
1183.     case DISP_FLASH:
1184.     case DISP_ALWAYS:
1185.         if (!tglyph)
1186.             tmp = &tgfirst;
1187.         else /* nested effect; we need dynamic memory */
1188.             tmp = (struct tmp_glyph *) alloc(sizeof *tmp);
1189.         tmp->prev = tglyph;
1190.         tglyph = tmp;
1191.         tglyph->sidx = 0;
1192.         tglyph->style = x;
1193.         tglyph->glyph = y;
1194.         flush_screen(0); /* flush buffered glyphs */
1195.         return;
1196. 
1197.     case DISP_FREEMEM: /* in case game ends with tmp_at() in progress */
1198.         while (tglyph) {
1199.             tmp = tglyph->prev;
1200.             if (tglyph != &tgfirst)
1201.                 free((genericptr_t) tglyph);
1202.             tglyph = tmp;
1203.         }
1204.         return;
1205. 
1206.     default:
1207.         break;
1208.     }
1209. 
1210.     if (!tglyph) {
1211.         panic("tmp_at: tglyph not initialized");
1212.     } else {
1213.         switch (x) {
1214.         case DISP_CHANGE:
1215.             tglyph->glyph = y;
1216.             break;
1217. 
1218.         case DISP_END:
1219.             if (tglyph->style == DISP_BEAM || tglyph->style == DISP_ALL) {
1220.                 int i;
1221. 
1222.                 /* Erase (reset) from source to end */
1223.                 for (i = 0; i < tglyph->sidx; i++)
1224.                     newsym(tglyph->saved[i].x, tglyph->saved[i].y);
1225.             } else if (tglyph->style == DISP_TETHER) {
1226.                 int i;
1227. 
1228.                 if (y == BACKTRACK && tglyph->sidx > 1) {
1229.                     /* backtrack */
1230.                     for (i = tglyph->sidx - 1; i > 0; i--) {
1231.                         newsym(tglyph->saved[i].x, tglyph->saved[i].y);
1232.                         show_glyph(tglyph->saved[i - 1].x,
1233.                                    tglyph->saved[i - 1].y, tglyph->glyph);
1234.                         flush_screen(0); /* make sure it shows up */
1235.                         nh_delay_output();
1236.                     }
1237.                     tglyph->sidx = 1;
1238.                 }
1239.                 for (i = 0; i < tglyph->sidx; i++)
1240.                     newsym(tglyph->saved[i].x, tglyph->saved[i].y);
1241.             } else {              /* DISP_FLASH or DISP_ALWAYS */
1242.                 if (tglyph->sidx) /* been called at least once */
1243.                     newsym(tglyph->saved[0].x, tglyph->saved[0].y);
1244.             }
1245.             /* tglyph->sidx = 0; -- about to be freed, so not necessary */
1246.             tmp = tglyph->prev;
1247.             if (tglyph != &tgfirst)
1248.                 free((genericptr_t) tglyph);
1249.             tglyph = tmp;
1250.             break;
1251. 
1252.         default: /* do it */
1253.             if (!isok(x, y))
1254.                 break;
1255.             if (tglyph->style == DISP_BEAM || tglyph->style == DISP_ALL) {
1256.                 if (tglyph->style != DISP_ALL && !cansee(x, y))
1257.                     break;
1258.                 if (tglyph->sidx >= TMP_AT_MAX_GLYPHS)
1259.                     break; /* too many locations */
1260.                 /* save pos for later erasing */
1261.                 tglyph->saved[tglyph->sidx].x = x;
1262.                 tglyph->saved[tglyph->sidx].y = y;
1263.                 tglyph->sidx += 1;
1264.             } else if (tglyph->style == DISP_TETHER) {
1265.                 if (tglyph->sidx >= TMP_AT_MAX_GLYPHS)
1266.                     break; /* too many locations */
1267.                 if (tglyph->sidx) {
1268.                     int px, py;
1269. 
1270.                     px = tglyph->saved[tglyph->sidx - 1].x;
1271.                     py = tglyph->saved[tglyph->sidx - 1].y;
1272.                     show_glyph(px, py, tether_glyph(px, py));
1273.                 }
1274.                 /* save pos for later use or erasure */
1275.                 tglyph->saved[tglyph->sidx].x = x;
1276.                 tglyph->saved[tglyph->sidx].y = y;
1277.                 tglyph->sidx += 1;
1278.             } else { /* DISP_FLASH/ALWAYS */
1279.                 if (tglyph
1280.                         ->sidx) { /* not first call, so reset previous pos */
1281.                     newsym(tglyph->saved[0].x, tglyph->saved[0].y);
1282.                     tglyph->sidx = 0; /* display is presently up to date */
1283.                 }
1284.                 if (!cansee(x, y) && tglyph->style != DISP_ALWAYS)
1285.                     break;
1286.                 tglyph->saved[0].x = x;
1287.                 tglyph->saved[0].y = y;
1288.                 tglyph->sidx = 1;
1289.             }
1290. 
1291.             show_glyph(x, y, tglyph->glyph); /* show it */
1292.             flush_screen(0);                 /* make sure it shows up */
1293.             break;
1294.         } /* end switch */
1295.     }
1296. }
1297. 
1298. /*
1299.  * flash_glyph_at(x, y, glyph, repeatcount)
1300.  *
1301.  * Briefly flash between the passed glyph and the glyph that's
1302.  * meant to be at the location.
1303.  */
1304. void
1305. flash_glyph_at(coordxy x, coordxy y, int tg, int rpt)
1306. {
1307.     int i, glyph[2];
1308. 
1309.     rpt *= 2; /* two loop iterations per 'count' */
1310.     glyph[0] = tg;
1311.     glyph[1] = (svl.level.flags.hero_memory) ? levl[x][y].glyph
1312.                                              : back_to_glyph(x, y);
1313.     /* even iteration count (guaranteed) ends with glyph[1] showing;
1314.        caller might want to override that, but no newsym() calls here
1315.        in case caller has tinkered with location visibility */
1316.     for (i = 0; i < rpt; i++) {
1317.         show_glyph(x, y, glyph[i % 2]);
1318.         flush_screen(1);
1319.         nh_delay_output();
1320.     }
1321. }
1322. 
1323. /*
1324.  * swallowed()
1325.  *
1326.  * The hero is swallowed.  Show a special graphics sequence for this.  This
1327.  * bypasses all of the display routines and messes with buffered screen
1328.  * directly.  This method works because both vision and display check for
1329.  * being swallowed.
1330.  */
1331. void
1332. swallowed(int first)
1333. {
1334.     static coordxy lastx, lasty; /* last swallowed position */
1335.     int swallower, left_ok, rght_ok;
1336. 
1337.     if (first) {
1338.         cls();
1339.         bot();
1340.     } else {
1341.         coordxy x, y;
1342. 
1343.         /* Clear old location */
1344.         for (y = lasty - 1; y <= lasty + 1; y++)
1345.             for (x = lastx - 1; x <= lastx + 1; x++)
1346.                 if (isok(x, y))
1347.                     show_glyph(x, y, GLYPH_UNEXPLORED);
1348.     }
1349. 
1350.     swallower = monsndx(u.ustuck->data);
1351.     /* assume isok(u.ux,u.uy) */
1352.     left_ok = isok(u.ux - 1, u.uy);
1353.     rght_ok = isok(u.ux + 1, u.uy);
1354.     /*
1355.      *  Display the hero surrounded by the monster's stomach.
1356.      */
1357.     if (isok(u.ux, u.uy - 1)) {
1358.         if (left_ok)
1359.             show_glyph(u.ux - 1, u.uy - 1,
1360.                        swallow_to_glyph(swallower, S_sw_tl));
1361.         show_glyph(u.ux, u.uy - 1, swallow_to_glyph(swallower, S_sw_tc));
1362.         if (rght_ok)
1363.             show_glyph(u.ux + 1, u.uy - 1,
1364.                        swallow_to_glyph(swallower, S_sw_tr));
1365.     }
1366. 
1367.     if (left_ok)
1368.         show_glyph(u.ux - 1, u.uy, swallow_to_glyph(swallower, S_sw_ml));
1369.     display_self();
1370.     if (rght_ok)
1371.         show_glyph(u.ux + 1, u.uy, swallow_to_glyph(swallower, S_sw_mr));
1372. 
1373.     if (isok(u.ux, u.uy + 1)) {
1374.         if (left_ok)
1375.             show_glyph(u.ux - 1, u.uy + 1,
1376.                        swallow_to_glyph(swallower, S_sw_bl));
1377.         show_glyph(u.ux, u.uy + 1, swallow_to_glyph(swallower, S_sw_bc));
1378.         if (rght_ok)
1379.             show_glyph(u.ux + 1, u.uy + 1,
1380.                        swallow_to_glyph(swallower, S_sw_br));
1381.     }
1382. 
1383.     /* Update the swallowed position. */
1384.     lastx = u.ux;
1385.     lasty = u.uy;
1386. }
1387. 
1388. /*
1389.  * under_water()
1390.  *
1391.  * Similar to swallowed() in operation.  Shows hero when underwater
1392.  * except when in water level.  Special routines exist for that.
1393.  */
1394. void
1395. under_water(int mode)
1396. {
1397.     static coordxy lastx, lasty;
1398.     static boolean dela;
1399.     coordxy x, y;
1400. 
1401.     /* swallowing has a higher precedence than under water */
1402.     if (Is_waterlevel(&u.uz) || u.uswallow)
1403.         return;
1404. 
1405.     /* full update */
1406.     if (mode == 1 || dela) {
1407.         cls();
1408.         dela = FALSE;
1409. 
1410.     /* delayed full update */
1411.     } else if (mode == 2) {
1412.         dela = TRUE;
1413.         return;
1414. 
1415.     /* limited update */
1416.     } else {
1417.         for (y = lasty - 1; y <= lasty + 1; y++)
1418.             for (x = lastx - 1; x <= lastx + 1; x++)
1419.                 if (isok(x, y))
1420.                     show_glyph(x, y, GLYPH_UNEXPLORED);
1421.     }
1422. 
1423.     /*
1424.      * TODO?  Should this honor Xray radius rather than force radius 1?
1425.      */
1426. 
1427.     for (x = u.ux - 1; x <= u.ux + 1; x++)
1428.         for (y = u.uy - 1; y <= u.uy + 1; y++)
1429.             if (isok(x, y) && (is_pool_or_lava(x, y) || is_ice(x, y))) {
1430.                 if (Blind && !u_at(x, y))
1431.                     show_glyph(x, y, GLYPH_UNEXPLORED);
1432.                 else
1433.                     newsym(x, y);
1434.             }
1435.     lastx = u.ux;
1436.     lasty = u.uy;
1437. }
1438. 
1439. /*
1440.  *      under_ground()
1441.  *
1442.  *      Very restricted display.  You can only see yourself.
1443.  */
1444. void
1445. under_ground(int mode)
1446. {
1447.     static boolean dela;
1448. 
1449.     /* swallowing has a higher precedence than under ground */
1450.     if (u.uswallow)
1451.         return;
1452. 
1453.     /* full update */
1454.     if (mode == 1 || dela) {
1455.         cls();
1456.         dela = FALSE;
1457. 
1458.     /* delayed full update */
1459.     } else if (mode == 2) {
1460.         dela = TRUE;
1461.         return;
1462. 
1463.     /* limited update */
1464.     } else {
1465.         newsym(u.ux, u.uy);
1466.     }
1467. }
1468. 
1469. /* ======================================================================== */
1470. 
1471. /*
1472.  * Loop through all of the monsters and update them.  Called when:
1473.  *      + going blind & telepathic
1474.  *      + regaining sight & telepathic
1475.  *      + getting and losing infravision
1476.  *      + hallucinating
1477.  *      + doing a full screen redraw
1478.  *      + see invisible times out or a ring of see invisible is taken off
1479.  *        or intrinsic see invisible is stolen by a gremlin
1480.  *      + when a potion of see invisible is quaffed or a ring of see
1481.  *        invisible is put on
1482.  *      + gaining telepathy when blind [givit() in eat.c, pleased() in pray.c]
1483.  *      + losing telepathy while blind [xkilled() in mon.c, attrcurse() in
1484.  *        sit.c]
1485.  */
1486. void
1487. see_monsters(void)
1488. {
1489.     struct monst *mon;
1490.     int new_warn_obj_cnt = 0;
1491. 
1492.     if (gd.defer_see_monsters)
1493.         return;
1494. 
1495.     /* steed and unseen engulfer/holder/holdee are recognized via touch
1496.        even if they aren't going to be rendered; other monsters
1497.        may get flagged as having been seen by display_monster() if it's
1498.        called by newsym() */
1499.     if (u.usteed)
1500.         u.usteed->meverseen = 1;
1501.     if (u.ustuck)
1502.         u.ustuck->meverseen = 1;
1503. 
1504.     /* loop through level.monsters (aka fmon) */
1505.     for (mon = fmon; mon; mon = mon->nmon) {
1506.         if (DEADMONSTER(mon))
1507.             continue;
1508.         if ((mon->mstate & MON_STILL_ARRIVING) != 0)
1509.             continue;
1510.         newsym(mon->mx, mon->my);
1511.         if (mon->wormno)
1512.             see_wsegs(mon);
1513.         if (Warn_of_mon
1514.             && (svc.context.warntype.obj & mon->data->mflags2) != 0L)
1515.             new_warn_obj_cnt++;
1516.     }
1517. 
1518.     /*
1519.      * Make Sting glow blue or stop glowing if required.
1520.      */
1521.     if (new_warn_obj_cnt != gw.warn_obj_cnt) {
1522.         Sting_effects(new_warn_obj_cnt);
1523.         gw.warn_obj_cnt = new_warn_obj_cnt;
1524.     }
1525. 
1526.     /* when mounted, hero's location gets caught by monster loop */
1527.     if (!u.usteed)
1528.         newsym(u.ux, u.uy);
1529. }
1530. 
1531. staticfn void
1532. mimic_light_blocking(struct monst *mtmp)
1533. {
1534.     if (mtmp->minvis && is_lightblocker_mappear(mtmp)) {
1535.         if (See_invisible)
1536.             block_point(mtmp->mx, mtmp->my);
1537.         else
1538.             unblock_point(mtmp->mx, mtmp->my);
1539.     }
1540. }
1541. 
1542. /*
1543.  * Block/unblock light depending on what a mimic is mimicking and if it's
1544.  * invisible or not.  Should be called only when the state of See_invisible
1545.  * changes.
1546.  */
1547. void
1548. set_mimic_blocking(void)
1549. {
1550.     iter_mons(mimic_light_blocking);
1551. }
1552. 
1553. /*
1554.  * Loop through all of the object *locations* and update them.  Called when
1555.  *      + hallucinating.
1556.  */
1557. void
1558. see_objects(void)
1559. {
1560.     struct obj *obj;
1561. 
1562.     for (obj = fobj; obj; obj = obj->nobj)
1563.         if (vobj_at(obj->ox, obj->oy) == obj)
1564.             newsym(obj->ox, obj->oy);
1565. 
1566.     /* Qt's "paper doll" subset of persistent inventory shows map tiles
1567.        for objects which aren't on the floor so not handled by above loop;
1568.        inventory which includes glyphs should also be affected, so do this
1569.        for all interfaces in case any feature that for persistent inventory */
1570.     update_inventory();
1571. }
1572. 
1573. /* mark the top object of nearby stacks as having been seen, and if
1574.    that object was being displayed as generic, redisplay it as specific */
1575. void
1576. see_nearby_objects(void)
1577. {
1578.     struct obj *obj;
1579.     int glyph;
1580.     coordxy ix, iy, x = u.ux, y = u.uy;
1581.     /* these 'r' and 'neardist' calculations match distant_name(objnam.c) */
1582.     int r = (u.xray_range > 2) ? u.xray_range : 2,
1583.         /* neardist produces a small square with rounded corners */
1584.         neardist = (r * r) * 2 - r; /* same as r*r + r*(r-1) */
1585. 
1586.     /* [note: this could be optimized to avoid the distu() calculations] */
1587.     for (iy = y - r; iy <= y + r; ++iy)
1588.         for (ix = x - r; ix <= x + r; ++ix) {
1589.             if (!isok(ix, iy))
1590.                 continue;
1591.             /* skip if no object or the object has already been marked as
1592.                having been seen up close */
1593.             if ((obj = vobj_at(ix, iy)) == 0 || obj->dknown)
1594.                 continue;
1595.             /* skip if the spot can't be seen or is too far (diagonal) */
1596.             if (!cansee(ix, iy) || distu(ix, iy) > neardist)
1597.                 continue;
1598. 
1599.             observe_object(obj);
1600.             /* operate on remembered glyph rather than current one */
1601.             glyph = levl[ix][iy].glyph;
1602.             if (glyph_is_generic_object(glyph))
1603.                 newsym_force(ix, iy);
1604.         }
1605. }
1606. 
1607. /*
1608.  * Update hallucinated traps.
1609.  */
1610. void
1611. see_traps(void)
1612. {
1613.     struct trap *trap;
1614.     int glyph;
1615. 
1616.     for (trap = gf.ftrap; trap; trap = trap->ntrap) {
1617.         glyph = _glyph_at(trap->tx, trap->ty);
1618.         if (glyph_is_trap(glyph))
1619.             newsym(trap->tx, trap->ty);
1620.     }
1621. }
1622. 
1623. /*  glyph, ttychar, framecolor,
1624.     { glyphflags, { NO_COLOR, sym.symidx },
1625.     customcolor, color256idx, tileidx, u } */
1626. static glyph_info no_ginfo = {
1627.     NO_GLYPH, ' ', NO_COLOR,
1628.     { MG_BADXY, { NO_COLOR, 0 },
1629.     0,
1630.     0U, 0U
1631. #ifdef ENHANCED_SYMBOLS
1632.       , 0
1633. #endif
1634.     }
1635. };
1636. 
1637. #ifndef UNBUFFERED_GLYPHINFO
1638. /* Note that the 'glyph' argument is not used in the expansion
1639.  * of this !UNBUFFERED_GLYPHINFO (default) variation, but is
1640.  * a requirement for the UNBUFFERED_GLYPHINFO variation */
1641. #define Glyphinfo_at(x, y, glyph) \
1642.     (((x) < 0 || (y) < 0 || (x) >= COLNO || (y) >= ROWNO) ? &no_ginfo   \
1643.      : &gg.gbuf[(y)][(x)].glyphinfo)
1644. #else
1645. static glyph_info ginfo;
1646. staticfn glyph_info *glyphinfo_at(coordxy, coordxy, int);
1647. #define Glyphinfo_at(x, y, glyph) glyphinfo_at((x), (y), (glyph))
1648. #endif
1649. 
1650. #ifdef TILES_IN_GLYPHMAP
1651. extern const glyph_info nul_glyphinfo; /* tile.c */
1652. #else
1653. /* glyph, ttychar, framecolor, { glyphflags, { sym.symidx }, nhcolor,
1654.                      tileidx, 0} */
1655. const glyph_info nul_glyphinfo = {
1656.     NO_GLYPH, ' ', NO_COLOR,
1657.         {  /* glyph_map */
1658.             MG_UNEXPL,
1659.             { NO_COLOR, SYM_UNEXPLORED + SYM_OFF_X },
1660.               0,
1661.               0U, 0U
1662. #ifdef ENHANCED_SYMBOLS
1663.                , 0
1664. #endif
1665.         }
1666. };
1667. #endif
1668. 
1669. #ifdef TILES_IN_GLYPHMAP
1670. extern glyph_map glyphmap[MAX_GLYPH]; /* from tile.c */
1671. #else
1672. glyph_map glyphmap[MAX_GLYPH] = {
1673.     { 0U, { NO_COLOR, 0 },
1674.       0,
1675.       0U, 0U
1676. #ifdef ENHANCED_SYMBOLS
1677.        , 0
1678. #endif
1679.     }
1680. };
1681. #endif
1682. 
1683. /*
1684.  * Put the cursor on the hero.  Flush all accumulated glyphs before doing it.
1685.  */
1686. void
1687. curs_on_u(void)
1688. {
1689.     flush_screen(1); /* Flush waiting glyphs & put cursor on hero */
1690. }
1691. 
1692. /* the #redraw command */
1693. int
1694. doredraw(void)
1695. {
1696.     docrt();
1697.     return ECMD_OK;
1698. }
1699. 
1700. /* the main refresh-the-screen routine */
1701. void
1702. docrt(void)
1703. {
1704.     docrt_flags(docrtRecalc);
1705. }
1706. 
1707. /* docrt() with finer control */
1708. void
1709. docrt_flags(int refresh_flags)
1710. {
1711.     coordxy x, y;
1712.     struct rm *lev;
1713.     boolean maponly = (refresh_flags & docrtMapOnly) != 0,
1714.             redrawonly = (refresh_flags & docrtRefresh) != 0,
1715.             nocls = (refresh_flags & docrtNocls) != 0;
1716. 
1717.     if (!u.ux || program_state.in_docrt)
1718.         return; /* display isn't ready yet */
1719. 
1720.     program_state.in_docrt = TRUE;
1721. 
1722.     if (redrawonly) {
1723.         redraw_map(FALSE);
1724.         goto post_map;
1725.     }
1726.     if (u.uswallow) {
1727.         swallowed(1);
1728.         goto post_map;
1729.     }
1730.     if (Underwater && !Is_waterlevel(&u.uz)) {
1731.         under_water(1);
1732.         goto post_map;
1733.     }
1734.     if (u.uburied) { /* [not implemented] */
1735.         under_ground(1);
1736.         goto post_map;
1737.     }
1738. 
1739.     /* shut down vision */
1740.     vision_recalc(2);
1741. 
1742.     /*
1743.      * This routine assumes that cls() does the following:
1744.      *      + fills the physical screen with the symbol for rock
1745.      *      + clears the glyph buffer
1746.      */
1747.     if (!nocls)
1748.         cls();
1749. 
1750.     /* display memory */
1751.     for (x = 1; x < COLNO; x++) {
1752.         lev = &levl[x][0];
1753.         for (y = 0; y < ROWNO; y++, lev++)
1754.             show_glyph(x, y, lev->glyph);
1755.     }
1756. 
1757.     /* see what is to be seen */
1758.     vision_recalc(0);
1759. 
1760.     /* overlay with monsters */
1761.     see_monsters();
1762. 
1763.  post_map:
1764. 
1765.     if (!maponly) {
1766.         /* perm_invent */
1767.         update_inventory();
1768.         /* status */
1769.         disp.botlx = TRUE; /* force a redraw of the bottom lines */
1770.         /* note: caller needs to call bot() to actually redraw status */
1771.     }
1772.     program_state.in_docrt = FALSE;
1773. }
1774. 
1775. /* for panning beyond a clipped region; resend the current map data to
1776.    the interface rather than use docrt()'s regeneration of that data */
1777. void
1778. redraw_map(boolean cursor_on_u)
1779. {
1780.     coordxy x, y;
1781.     int glyph;
1782.     glyph_info bkglyphinfo = nul_glyphinfo;
1783. 
1784.     /*
1785.      * Not sure whether this is actually necessary; save and restore did
1786.      * used to get much too involved with each dungeon level as it was
1787.      * read and written.
1788.      *
1789.      * !u.ux: display isn't ready yet; (restoring || !on_level()): was part
1790.      * of cliparound() but interface shouldn't access this much internals
1791.      */
1792.     if (!u.ux || suppress_map_output() || !on_level(&u.uz0, &u.uz))
1793.         return;
1794. 
1795.     /*
1796.      * This yields sensible clipping when #terrain+getpos is in
1797.      * progress and the screen displays something other than what
1798.      * the map would currently be showing.
1799.      */
1800.     for (y = 0; y < ROWNO; ++y)
1801.         for (x = 1; x < COLNO; ++x) {
1802.             glyph = _glyph_at(x, y); /* not levl[x][y].glyph */
1803.             get_bkglyph_and_framecolor(x, y, &bkglyphinfo.glyph,
1804.                                        &bkglyphinfo.framecolor);
1805.             print_glyph(WIN_MAP, x, y,
1806.                         Glyphinfo_at(x, y, glyph), &bkglyphinfo);
1807.         }
1808.     flush_screen(cursor_on_u);
1809. #ifndef UNBUFFERED_GLYPHINFO
1810.     nhUse(glyph);
1811. #endif
1812. }
1813. 
1814. /*
1815.  * =======================================================
1816.  */
1817. void
1818. reglyph_darkroom(void)
1819. {
1820.     coordxy x, y;
1821. 
1822.     for (x = 1; x < COLNO; x++)
1823.         for (y = 0; y < ROWNO; y++) {
1824.             struct rm *lev = &levl[x][y];
1825. 
1826.             if (!flags.dark_room) {
1827.                 if (lev->glyph == cmap_to_glyph(S_corr)
1828.                     && lev->waslit)
1829.                     lev->glyph = cmap_to_glyph(S_litcorr);
1830.             } else {
1831.                 if (lev->glyph == cmap_to_glyph(S_litcorr)
1832.                     && !cansee(x, y))
1833.                     lev->glyph = cmap_to_glyph(S_corr);
1834.             }
1835. 
1836.             if (!flags.dark_room || !iflags.use_color
1837.                 || Is_rogue_level(&u.uz)) {
1838.                 if (lev->glyph == cmap_to_glyph(S_darkroom))
1839.                     lev->glyph = lev->waslit ? cmap_to_glyph(S_room)
1840.                                              : GLYPH_NOTHING;
1841.             } else {
1842.                 if (lev->glyph == cmap_to_glyph(S_room) && lev->seenv
1843.                     && lev->waslit && !cansee(x, y))
1844.                     lev->glyph = cmap_to_glyph(S_darkroom);
1845.                 else if (lev->glyph == GLYPH_NOTHING
1846.                          && lev->typ == ROOM && lev->seenv && !cansee(x, y))
1847.                     lev->glyph = cmap_to_glyph(S_darkroom);
1848.             }
1849.         }
1850.     if (flags.dark_room && iflags.use_color)
1851.         gs.showsyms[S_darkroom] = gs.showsyms[S_room];
1852.     else
1853.         gs.showsyms[S_darkroom] = gs.showsyms[SYM_NOTHING + SYM_OFF_X];
1854. }
1855. 
1856. /* ======================================================================== */
1857. /* Glyph Buffering (3rd screen) =========================================== */
1858. 
1859. /* FIXME: This is a dirty hack, because newsym() doesn't distinguish
1860.  * between object piles and single objects, it doesn't mark the location
1861.  * for update. */
1862. void
1863. newsym_force(coordxy x, coordxy y)
1864. {
1865.     newsym(x, y);
1866.     gg.gbuf[y][x].gnew = 1;
1867.     if (gg.gbuf_start[y] > x)
1868.         gg.gbuf_start[y] = x;
1869.     if (gg.gbuf_stop[y] < x)
1870.         gg.gbuf_stop[y] = x;
1871. }
1872. 
1873. /*
1874.  * Store the glyph in the 3rd screen for later flushing.
1875.  */
1876. void
1877. show_glyph(coordxy x, coordxy y, int glyph)
1878. {
1879. #ifndef UNBUFFERED_GLYPHINFO
1880.     glyph_info glyphinfo;
1881. #endif
1882.     boolean show_glyph_change = FALSE;
1883.     int oldglyph;
1884. 
1885.     /* don't process map glyphs when saving, restoring, or in_mklev */
1886.     if (_suppress_map_output())
1887.         return;
1888. 
1889.     //if (glyph == 3972 || glyph == 3988)
1890.     //    __debugbreak();
1891.     /*
1892.      * Check for bad positions and glyphs.
1893.      */
1894.     if (!isok(x, y)) {
1895.         const char *text = "";
1896.         int offset = -1;
1897. 
1898.         /* column 0 is invalid, but it's often used as a flag, so ignore it */
1899.         if (x == 0)
1900.             return;
1901. 
1902.         /*
1903.          *  This assumes an ordering of the offsets.  See display.h for
1904.          *  the definition.
1905.          */
1906.         if (glyph < 0 || glyph >= MAX_GLYPH) {
1907.             /* invalid location and also invalid glyph */
1908.             text = "invalid";
1909.         } else if ((offset = (glyph - GLYPH_NOTHING_OFF)) >= 0) {
1910.             text = "nothing";
1911.         } else if ((offset = (glyph - GLYPH_UNEXPLORED_OFF)) >= 0) {
1912.             text = "unexplored";
1913.         } else if ((offset = (glyph - GLYPH_STATUE_FEM_PILETOP_OFF)) >= 0) {
1914.             text = "statue of a female monster at top of a pile";
1915.         } else if ((offset = (glyph - GLYPH_STATUE_MALE_PILETOP_OFF)) >= 0) {
1916.             text = "statue of a male monster at top of a pile";
1917.         } else if ((offset = (glyph - GLYPH_BODY_PILETOP_OFF)) >= 0) {
1918.             text = "body at top of a pile";
1919.         } else if ((offset = (glyph - GLYPH_OBJ_PILETOP_OFF)) >= 0) {
1920.             text = (glyph_is_piletop_generic_obj(glyph)
1921.                     ? "generic object at top of a pile"
1922.                     : "object at top of a pile");
1923.         } else if ((offset = (glyph - GLYPH_STATUE_FEM_OFF)) >= 0) {
1924.             text = "statue of female monster";
1925.         } else if ((offset = (glyph - GLYPH_STATUE_MALE_OFF)) >= 0) {
1926.             text = "statue of male monster";
1927.         } else if ((offset = (glyph - GLYPH_WARNING_OFF)) >= 0) {
1928.             /* warn flash */
1929.             text = "warning explosion";
1930.         } else if ((offset = (glyph - GLYPH_EXPLODE_FROSTY_OFF)) >= 0) {
1931.             text = "frosty explosion";
1932.         } else if ((offset = (glyph - GLYPH_EXPLODE_FIERY_OFF)) >= 0) {
1933.             text = "fiery explosion";
1934.         } else if ((offset = (glyph - GLYPH_EXPLODE_MAGICAL_OFF)) >= 0) {
1935.             text = "magical explosion";
1936.         } else if ((offset = (glyph - GLYPH_EXPLODE_WET_OFF)) >= 0) {
1937.             text = "wet explosion";
1938.         } else if ((offset = (glyph - GLYPH_EXPLODE_MUDDY_OFF)) >= 0) {
1939.             text = "muddy explosion";
1940.         } else if ((offset = (glyph - GLYPH_EXPLODE_NOXIOUS_OFF)) >= 0) {
1941.             text = "noxious explosion";
1942.         } else if ((offset = (glyph - GLYPH_EXPLODE_DARK_OFF)) >= 0) {
1943.             text = "dark explosion";
1944.         } else if ((offset = (glyph - GLYPH_SWALLOW_OFF)) >= 0) {
1945.             text = "swallow";
1946.         } else if ((offset = (glyph - GLYPH_CMAP_C_OFF)) >= 0) {
1947.             text = "cmap C";
1948.         } else if ((offset = (glyph - GLYPH_ZAP_OFF)) >= 0) {
1949.             text = "zap";
1950.         } else if ((offset = (glyph - GLYPH_CMAP_B_OFF)) >= 0) {
1951.             text = "cmap B";
1952.         } else if ((offset = (glyph - GLYPH_ALTAR_OFF)) >= 0) {
1953.             text = "altar";
1954.         } else if ((offset = (glyph - GLYPH_CMAP_A_OFF)) >= 0) {
1955.             text = "cmap A";
1956.         } else if ((offset = (glyph - GLYPH_CMAP_SOKO_OFF)) >= 0) {
1957.             text = "sokoban dungeon walls";
1958.         } else if ((offset = (glyph - GLYPH_CMAP_KNOX_OFF)) >= 0) {
1959.             text = "knox dungeon walls";
1960.         } else if ((offset = (glyph - GLYPH_CMAP_GEH_OFF)) >= 0) {
1961.             text = "gehennom dungeon walls";
1962.         } else if ((offset = (glyph - GLYPH_CMAP_MINES_OFF)) >= 0) {
1963.             text = "gnomish mines dungeon walls";
1964.         } else if ((offset = (glyph - GLYPH_CMAP_MAIN_OFF)) >= 0) {
1965.             text = "main dungeon walls";
1966.         } else if ((offset = (glyph - GLYPH_CMAP_STONE_OFF)) >= 0) {
1967.             text = "stone";
1968.         } else if ((offset = (glyph - GLYPH_OBJ_OFF)) >= 0) {
1969.             text = (glyph_is_normal_generic_obj(glyph)
1970.                     ? "generic object"
1971.                     : "object");
1972.         } else if ((offset = (glyph - GLYPH_RIDDEN_FEM_OFF)) >= 0) {
1973.             text = "ridden female monster";
1974.         } else if ((offset = (glyph - GLYPH_RIDDEN_MALE_OFF)) >= 0) {
1975.             text = "ridden male monster";
1976.         } else if ((offset = (glyph - GLYPH_BODY_OFF)) >= 0) {
1977.             text = "body";
1978.         } else if ((offset = (glyph - GLYPH_DETECT_FEM_OFF)) >= 0) {
1979.             text = "detected female monster";
1980.         } else if ((offset = (glyph - GLYPH_DETECT_MALE_OFF)) >= 0) {
1981.             text = "detected male monster";
1982.         } else if ((offset = (glyph - GLYPH_INVIS_OFF)) >= 0) {
1983.             text = "invisible monster";
1984.         } else if ((offset = (glyph - GLYPH_PET_FEM_OFF)) >= 0) {
1985.             text = "female pet";
1986.         } else if ((offset = (glyph - GLYPH_PET_MALE_OFF)) >= 0) {
1987.             text = "male pet";
1988.         } else if ((offset = (glyph - GLYPH_MON_FEM_OFF)) >= 0) {
1989.             text = "female monster";
1990.         } else if ((offset = (glyph - GLYPH_MON_MALE_OFF)) >= 0) {
1991.             text = "male monster";
1992.         }
1993.         impossible("show_glyph:  bad pos <%d,%d> with glyph %d [%s %d].",
1994.                    x, y, glyph, text, offset);
1995.         return;
1996.     } else if (glyph < 0 || glyph >= MAX_GLYPH) {
1997.         /* valid location but invalid glyph */
1998.         impossible("show_glyph:  bad glyph %d [max %d] at <%d,%d>.",
1999.                    glyph, MAX_GLYPH, x, y);
2000.         return;
2001.     }
2002. #ifndef UNBUFFERED_GLYPHINFO
2003.     /* without UNBUFFERED_GLYPHINFO defined the glyphinfo values are buffered
2004.        alongside the glyphs themselves for better performance, increased
2005.        buffer size */
2006.     map_glyphinfo(x, y, glyph, 0, &glyphinfo);
2007. #endif
2008. 
2009.     oldglyph = gg.gbuf[y][x].glyphinfo.glyph;
2010. 
2011.     if (a11y.glyph_updates && !a11y.mon_notices_blocked
2012.         && !program_state.in_docrt && !program_state.gameover
2013.         && !program_state.in_getlev && !program_state.stopprint
2014.         && !_suppress_map_output()
2015.         && (oldglyph != glyph || gg.gbuf[y][x].gnew)) {
2016.         int c = glyph_to_cmap(glyph);
2017. 
2018.         if ((glyph_is_nothing(oldglyph) || glyph_is_unexplored(oldglyph)
2019.              || is_cmap_furniture(c))
2020.             && !is_cmap_wall(c) && !is_cmap_room(c)) {
2021.             if ((a11y.mon_notices && glyph_is_monster(glyph))
2022.                 || (glyph_is_monster(oldglyph))
2023.                 || u_at(x, y)) {
2024.                 ; /* nothing */
2025.             } else {
2026.                 show_glyph_change = TRUE;
2027.             }
2028.         }
2029.     }
2030. 
2031.     if (gg.gbuf[y][x].glyphinfo.glyph != glyph
2032. #ifndef UNBUFFERED_GLYPHINFO
2033.         /* flags might change (single object vs pile, monster tamed or pet
2034.            gone feral), color might change (altar's alignment converted by
2035.            invisible hero), but ttychar normally won't change unless the
2036.            glyph does too (changing boulder symbol would be an exception,
2037.            but that triggers full redraw so doesn't matter here); still,
2038.            be thorough and check everything */
2039.         || gg.gbuf[y][x].glyphinfo.ttychar != glyphinfo.ttychar
2040.         || gg.gbuf[y][x].glyphinfo.gm.customcolor != glyphinfo.gm.customcolor
2041.         || gg.gbuf[y][x].glyphinfo.gm.glyphflags != glyphinfo.gm.glyphflags
2042.         || gg.gbuf[y][x].glyphinfo.gm.sym.color != glyphinfo.gm.sym.color
2043.         || gg.gbuf[y][x].glyphinfo.gm.tileidx != glyphinfo.gm.tileidx
2044. #endif
2045.         || iflags.use_background_glyph) {
2046.         gg.gbuf[y][x].glyphinfo.glyph = glyph;
2047.         gg.gbuf[y][x].gnew = 1;
2048. #ifndef UNBUFFERED_GLYPHINFO
2049.         gg.gbuf[y][x].glyphinfo.glyph = glyphinfo.glyph;
2050.         gg.gbuf[y][x].glyphinfo.ttychar = glyphinfo.ttychar;
2051.         gg.gbuf[y][x].glyphinfo.gm = glyphinfo.gm;
2052. #endif
2053.         if (gg.gbuf_start[y] > x)
2054.             gg.gbuf_start[y] = x;
2055.         if (gg.gbuf_stop[y] < x)
2056.             gg.gbuf_stop[y] = x;
2057.     }
2058. 
2059.     if (show_glyph_change) {
2060.         char buf[BUFSZ];
2061.         coord cc;
2062.         int sym = 0;
2063.         const char *firstmatch = 0;
2064.         boolean tmp_accessiblemsg = a11y.accessiblemsg;
2065. 
2066.         a11y.accessiblemsg = TRUE;
2067.         cc.x = x, cc.y = y;
2068.         do_screen_description(cc, TRUE, sym, buf, &firstmatch, NULL);
2069.         pline_xy(x, y, "%s.", firstmatch);
2070.         a11y.accessiblemsg = tmp_accessiblemsg;
2071.     }
2072. }
2073. 
2074. /*
2075.  * Reset the changed glyph borders so that none of the 3rd screen has
2076.  * changed.
2077.  */
2078. #define reset_glyph_bbox()                \
2079.     {                                     \
2080.         int i;                            \
2081.                                           \
2082.         for (i = 0; i < ROWNO; i++) {     \
2083.             gg.gbuf_start[i] = COLNO - 1; \
2084.             gg.gbuf_stop[i] = 0;          \
2085.         }                                 \
2086.     }
2087. 
2088. static gbuf_entry nul_gbuf = {
2089.     0,                                 /* gnew */
2090.     { GLYPH_UNEXPLORED, (unsigned) ' ', NO_COLOR,
2091.        /* glyphinfo.glyph, glyphinfo.ttychar */
2092.         /* glyphinfo.gm */
2093.         { MG_UNEXPL, { NO_COLOR, 0 },
2094.           0,
2095.           0U, 0U
2096. #ifdef ENHANCED_SYMBOLS
2097.             , 0
2098. #endif
2099.         }
2100.     }
2101. };
2102. 
2103. /*
2104.  * Turn the 3rd screen into UNEXPLORED that needs to be refreshed.
2105.  */
2106. void
2107. clear_glyph_buffer(void)
2108. {
2109.     coordxy x, y;
2110.     gbuf_entry *gptr = &gg.gbuf[0][0];
2111.     glyph_info *giptr =
2112. #ifndef UNBUFFERED_GLYPHINFO
2113.                         &gptr->glyphinfo;
2114. #else
2115.                         &ginfo;
2116. 
2117.     map_glyphinfo(0, 0, GLYPH_UNEXPLORED, 0, giptr);
2118. #endif
2119. #ifndef UNBUFFERED_GLYPHINFO
2120.     nul_gbuf.gnew = (giptr->ttychar != nul_gbuf.glyphinfo.ttychar
2121.                      || giptr->gm.sym.color != nul_gbuf.glyphinfo.gm.sym.color
2122.                      || giptr->gm.glyphflags
2123.                         != nul_gbuf.glyphinfo.gm.glyphflags
2124.                      || giptr->gm.customcolor
2125.                         != nul_gbuf.glyphinfo.gm.customcolor
2126.                      || giptr->gm.tileidx != nul_gbuf.glyphinfo.gm.tileidx)
2127. #else
2128.     nul_gbuf.gnew = (giptr->ttychar != ' '
2129.                      || giptr->gm.sym.color != NO_COLOR
2130.                      || giptr->gm.customcolor != 0
2131.                      || (giptr->gm.glyphflags & ~MG_UNEXPL) != 0)
2132. #endif
2133.                          ? 1 : 0;
2134.     for (y = 0; y < ROWNO; y++) {
2135.         gptr = &gg.gbuf[y][0];
2136.         for (x = COLNO; x; x--) {
2137.             *gptr++ = nul_gbuf;
2138.         }
2139.         gg.gbuf_start[y] = 1;
2140.         gg.gbuf_stop[y] = COLNO - 1;
2141.     }
2142. }
2143. 
2144. /* used by tty after menu or text popup has temporarily overwritten the map
2145.    and it has been erased so shows spaces, not necessarily S_unexplored */
2146. void
2147. row_refresh(coordxy start, coordxy stop, coordxy y)
2148. {
2149.     coordxy x;
2150.     int glyph;
2151.     boolean force;
2152.     gbuf_entry *gptr = &gg.gbuf[0][0];
2153.     glyph_info bkglyphinfo = nul_glyphinfo;
2154.     glyph_info *giptr =
2155. #ifndef UNBUFFERED_GLYPHINFO
2156.                         &gptr->glyphinfo;
2157. #else
2158.                         &ginfo;
2159. 
2160.     map_glyphinfo(0, 0, GLYPH_UNEXPLORED, 0U, giptr);
2161. #endif
2162. #ifndef UNBUFFERED_GLYPHINFO
2163.     force = (giptr->ttychar != nul_gbuf.glyphinfo.ttychar
2164.                  || giptr->gm.sym.color != nul_gbuf.glyphinfo.gm.sym.color
2165.                  || giptr->gm.glyphflags != nul_gbuf.glyphinfo.gm.glyphflags
2166.                  || giptr->gm.customcolor != nul_gbuf.glyphinfo.gm.customcolor
2167.                  || giptr->gm.tileidx != nul_gbuf.glyphinfo.gm.tileidx)
2168. #else
2169.     force = (giptr->ttychar != ' '
2170.                  || giptr->gm.sym.color != NO_COLOR
2171.                  || giptr->gm.gm.customcolor != 0
2172.                  || (giptr->gm.glyphflags & ~MG_UNEXPL) != 0)
2173. #endif
2174.                  ? 1 : 0;
2175.     for (x = start; x <= stop; x++) {
2176.         gptr = &gg.gbuf[y][x];
2177.         glyph = gptr->glyphinfo.glyph;
2178.         get_bkglyph_and_framecolor(x, y, &bkglyphinfo.glyph,
2179.                                    &bkglyphinfo.framecolor);
2180.         if (force || glyph != GLYPH_UNEXPLORED
2181.             || bkglyphinfo.framecolor != NO_COLOR) {
2182.             print_glyph(WIN_MAP, x, y,
2183.                         Glyphinfo_at(x, y, glyph), &bkglyphinfo);
2184.         }
2185.     }
2186. }
2187. 
2188. void
2189. cls(void)
2190. {
2191.     static boolean in_cls = 0;
2192. 
2193.     if (in_cls)
2194.         return;
2195.     in_cls = TRUE;
2196.     display_nhwindow(WIN_MESSAGE, FALSE); /* flush messages */
2197.     disp.botlx = TRUE;                 /* force update of botl window */
2198.     clear_nhwindow(WIN_MAP);              /* clear physical screen */
2199. 
2200.     clear_glyph_buffer(); /* force gbuf[][].glyph to unexplored */
2201.     in_cls = FALSE;
2202. }
2203. 
2204. /*
2205.  * Synch the third screen with the display.
2206.  */
2207. void
2208. flush_screen(int cursor_on_u)
2209. {
2210.     /* Prevent infinite loops on errors:
2211.      *      flush_screen->print_glyph->impossible->pline->flush_screen
2212.      */
2213.     static int flushing = 0;
2214.     static int delay_flushing = 0;
2215.     coordxy x, y;
2216.     glyph_info bkglyphinfo = nul_glyphinfo;
2217.     int bkglyph;
2218. 
2219.     /* 5.0: don't update map, status, or perm_invent during save/restore */
2220.     if (_suppress_map_output())
2221.         return;
2222. 
2223.     if (cursor_on_u == -1)
2224.         delay_flushing = !delay_flushing;
2225.     if (delay_flushing)
2226.         return;
2227.     if (flushing)
2228.         return; /* if already flushing then return */
2229.     flushing = 1;
2230. #ifdef HANGUPHANDLING
2231.     if (program_state.done_hup)
2232.         return;
2233. #endif
2234. 
2235.     /* get this done now, before we place the cursor on the hero */
2236.     if (disp.botl || disp.botlx)
2237.         bot();
2238.     else if (disp.time_botl)
2239.         timebot();
2240. 
2241.     for (y = 0; y < ROWNO; y++) {
2242.         gbuf_entry *gptr = &gg.gbuf[y][x = gg.gbuf_start[y]];
2243. 
2244.         for (; x <= gg.gbuf_stop[y]; gptr++, x++) {
2245.             get_bkglyph_and_framecolor(x, y, &bkglyph,
2246.                                        &bkglyphinfo.framecolor);
2247.             if (gptr->gnew
2248.                 || (gw.wsettings.map_frame_color != NO_COLOR
2249.                     && bkglyphinfo.framecolor != NO_COLOR)) {
2250.                 /* map_glyphinfo() won't touch framecolor */
2251.                 map_glyphinfo(x, y, bkglyph, 0, &bkglyphinfo);
2252.                 print_glyph(WIN_MAP, x, y,
2253.                             Glyphinfo_at(x, y, gptr->glyphinfo.glyph),
2254.                             &bkglyphinfo);
2255.                 gptr->gnew = 0;
2256.             }
2257.         }
2258.     }
2259.     reset_glyph_bbox();
2260. 
2261.     /* after map update, before display_nhwindow(WIN_MAP) */
2262.     if (cursor_on_u)
2263.         curs(WIN_MAP, u.ux, u.uy); /* move cursor to the hero */
2264. 
2265.     display_nhwindow(WIN_MAP, FALSE);
2266.     flushing = 0;
2267. }
2268. 
2269. /* ======================================================================== */
2270. 
2271. /*
2272.  * back_to_glyph()
2273.  *
2274.  * Use the information in the rm structure at the given position to create
2275.  * a glyph of a background.
2276.  *
2277.  * I had to add a field in the rm structure (horizontal) so that we knew
2278.  * if open doors and secret doors were horizontal or vertical.  Previously,
2279.  * the screen symbol had the horizontal/vertical information set at
2280.  * level generation time.
2281.  *
2282.  * I used the 'ladder' field (really doormask) for deciding if stairwells
2283.  * were up or down.  I didn't want to check the upstairs and dnstairs
2284.  * variables.
2285.  */
2286. int
2287. back_to_glyph(coordxy x, coordxy y)
2288. {
2289.     int idx, bypass_glyph = NO_GLYPH;
2290.     struct rm *ptr = &(levl[x][y]);
2291.     struct stairway *sway;
2292. 
2293.     switch (ptr->typ) {
2294.     case SCORR:
2295.     case STONE:
2296.         idx = svl.level.flags.arboreal ? S_tree : S_stone;
2297.         break;
2298.     case ROOM:
2299.         idx = S_room;
2300.         break;
2301.     case CORR:
2302.         idx = (ptr->waslit || flags.lit_corridor) ? S_litcorr : S_corr;
2303.         break;
2304.     case SDOOR:
2305.         if (ptr->arboreal_sdoor) {
2306.             idx = S_tree;
2307.             break;
2308.         }
2309.         FALLTHROUGH;
2310.         /*FALLTHRU*/
2311.     case HWALL:
2312.     case VWALL:
2313.     case TLCORNER:
2314.     case TRCORNER:
2315.     case BLCORNER:
2316.     case BRCORNER:
2317.     case CROSSWALL:
2318.     case TUWALL:
2319.     case TDWALL:
2320.     case TLWALL:
2321.     case TRWALL:
2322.         idx = ptr->seenv ? wall_angle(ptr) : S_stone;
2323.         break;
2324.     case DOOR:
2325.         if (ptr->doormask) {
2326.             if (ptr->doormask & D_BROKEN)
2327.                 idx = S_ndoor;
2328.             else if (ptr->doormask & D_ISOPEN)
2329.                 idx = (ptr->horizontal) ? S_hodoor : S_vodoor;
2330.             else /* else is closed */
2331.                 idx = (ptr->horizontal) ? S_hcdoor : S_vcdoor;
2332.         } else
2333.             idx = S_ndoor;
2334.         break;
2335.     case IRONBARS:
2336.         idx = S_bars;
2337.         break;
2338.     case TREE:
2339.         idx = S_tree;
2340.         break;
2341.     case POOL:
2342.     case MOAT:
2343.         idx = S_pool;
2344.         break;
2345.     case STAIRS:
2346.         sway = stairway_at(x, y);
2347.         if (known_branch_stairs(sway))
2348.             idx = (ptr->ladder & LA_DOWN) ? S_brdnstair : S_brupstair;
2349.         else
2350.             idx = (ptr->ladder & LA_DOWN) ? S_dnstair : S_upstair;
2351.         break;
2352.     case LADDER:
2353.         sway = stairway_at(x, y);
2354.         if (known_branch_stairs(sway))
2355.             idx = (ptr->ladder & LA_DOWN) ? S_brdnladder : S_brupladder;
2356.         else
2357.             idx = (ptr->ladder & LA_DOWN) ? S_dnladder : S_upladder;
2358.         break;
2359.     case FOUNTAIN:
2360.         idx = S_fountain;
2361.         break;
2362.     case SINK:
2363.         idx = S_sink;
2364.         break;
2365.     case ALTAR:
2366.         idx = S_altar;  /* not really used */
2367.         bypass_glyph = altar_to_glyph(ptr->altarmask);
2368.         break;
2369.     case GRAVE:
2370.         idx = S_grave;
2371.         break;
2372.     case THRONE:
2373.         idx = S_throne;
2374.         break;
2375.     case LAVAPOOL:
2376.         idx = S_lava;
2377.         break;
2378.     case LAVAWALL:
2379.         idx = S_lavawall;
2380.         break;
2381.     case ICE:
2382.         idx = S_ice;
2383.         break;
2384.     case AIR:
2385.         idx = S_air;
2386.         break;
2387.     case CLOUD:
2388.         idx = S_cloud;
2389.         break;
2390.     case WATER:
2391.         idx = S_water;
2392.         break;
2393.     case DBWALL:
2394.         idx = (ptr->horizontal) ? S_hcdbridge : S_vcdbridge;
2395.         break;
2396.     case DRAWBRIDGE_UP:
2397.         switch (ptr->drawbridgemask & DB_UNDER) {
2398.         case DB_MOAT:
2399.             idx = S_pool;
2400.             break;
2401.         case DB_LAVA:
2402.             idx = S_lava;
2403.             break;
2404.         case DB_ICE:
2405.             idx = S_ice;
2406.             break;
2407.         case DB_FLOOR:
2408.             idx = S_room;
2409.             break;
2410.         default:
2411.             impossible("Strange db-under: %d",
2412.                        ptr->drawbridgemask & DB_UNDER);
2413.             idx = S_room; /* something is better than nothing */
2414.             break;
2415.         }
2416.         break;
2417.     case DRAWBRIDGE_DOWN:
2418.         idx = (ptr->horizontal) ? S_hodbridge : S_vodbridge;
2419.         break;
2420.     default:
2421.         impossible("back_to_glyph:  unknown level type [ = %d ]", ptr->typ);
2422.         idx = S_room;
2423.         break;
2424.     }
2425. 
2426.     return (bypass_glyph != NO_GLYPH) ? bypass_glyph : cmap_to_glyph(idx);
2427. }
2428. 
2429. /*
2430.  * swallow_to_glyph()
2431.  *
2432.  * Convert a monster number and a swallow location into the correct glyph.
2433.  * If you don't want a patchwork monster while hallucinating, decide on
2434.  * a random monster in swallowed() and don't use what_mon() here.
2435.  */
2436. staticfn int
2437. swallow_to_glyph(int mnum, int loc)
2438. {
2439.     int m_3 = what_mon(mnum, rn2_on_display_rng) << 3;
2440. 
2441.     if (loc < S_sw_tl || S_sw_br < loc) {
2442.         impossible("swallow_to_glyph: bad swallow location");
2443.         loc = S_sw_br;
2444.     }
2445.     return (m_3 | (loc - S_sw_tl)) + GLYPH_SWALLOW_OFF;
2446. }
2447. 
2448. /*
2449.  * zapdir_to_glyph()
2450.  *
2451.  * Change the given zap direction and beam type into a glyph.  Each beam
2452.  * type has four glyphs, one for each of the symbols below.  The order of
2453.  * the zap symbols [0-3] as defined in defsym.h are:
2454.  *
2455.  *      |  S_vbeam      ( 0, 1) or ( 0,-1)
2456.  *      -  S_hbeam      ( 1, 0) or (-1, 0)
2457.  *      \  S_lslant     ( 1, 1) or (-1,-1)
2458.  *      /  S_rslant     (-1, 1) or ( 1,-1)
2459.  */
2460. int
2461. zapdir_to_glyph(int dx, int dy, int beam_type)
2462. {
2463.     if (beam_type >= NUM_ZAP) {
2464.         impossible("zapdir_to_glyph:  illegal beam type");
2465.         beam_type = 0;
2466.     }
2467.     dx = (dx == dy) ? 2 : (dx && dy) ? 3 : dx ? 1 : 0;
2468. 
2469.     return ((int) ((beam_type << 2) | dx)) + GLYPH_ZAP_OFF;
2470. }
2471. 
2472. /*
2473.  * Utility routine for dowhatis() used to find out the glyph displayed at
2474.  * the location.  This isn't necessarily the same as the glyph in the levl
2475.  * structure, so we must check the "third screen".
2476.  */
2477. int
2478. glyph_at(coordxy x, coordxy y)
2479. {
2480.     if (x < 0 || y < 0 || x >= COLNO || y >= ROWNO)
2481.         return cmap_to_glyph(S_room); /* XXX */
2482.     return gg.gbuf[y][x].glyphinfo.glyph; /* _glyph_at(x,y) */
2483. }
2484. 
2485. #ifdef UNBUFFERED_GLYPHINFO
2486. glyph_info *
2487. glyphinfo_at(coordxy x, coordxy y, int glyph)
2488. {
2489.     map_glyphinfo(x, y, glyph, 0, &ginfo); /* ginfo declared at file scope */
2490.     return &ginfo;
2491. }
2492. #endif
2493. 
2494. /*
2495.  * Get the glyph for the background so that it can potentially
2496.  * be merged into graphical window ports to improve the appearance
2497.  * of stuff on dark room squares and the plane of air etc.
2498.  * [This should be using background as recorded for #overview rather
2499.  * than current data from the map.]
2500.  *
2501.  * Also get the frame color to use for highlighting the map location
2502.  * for some purpose.
2503.  *
2504.  */
2505. 
2506. staticfn void
2507. get_bkglyph_and_framecolor(
2508.     coordxy x, coordxy y,
2509.     int *bkglyph,
2510.     uint32 *framecolor)
2511. {
2512.     int idx, tmp_bkglyph = GLYPH_UNEXPLORED;
2513.     struct rm *lev = &levl[x][y];
2514. 
2515.     if (iflags.use_background_glyph && lev->seenv != 0
2516.         && (gg.gbuf[y][x].glyphinfo.glyph != GLYPH_UNEXPLORED)) {
2517.         switch (lev->typ) {
2518.         case SCORR:
2519.         case STONE:
2520.             idx = svl.level.flags.arboreal ? S_tree : S_stone;
2521.             break;
2522.         case ROOM:
2523.            idx = S_room;
2524.            break;
2525.         case CORR:
2526.            idx = (lev->waslit || flags.lit_corridor) ? S_litcorr : S_corr;
2527.            break;
2528.         case ICE:
2529.            idx = S_ice;
2530.            break;
2531.         case AIR:
2532.            idx = S_air;
2533.            break;
2534.         case CLOUD:
2535.            idx = S_cloud;
2536.            break;
2537.         case POOL:
2538.         case MOAT:
2539.            idx = S_pool;
2540.            break;
2541.         case WATER:
2542.            idx = S_water;
2543.            break;
2544.         case LAVAPOOL:
2545.            idx = S_lava;
2546.            break;
2547.         case LAVAWALL:
2548.             idx = S_lavawall;
2549.             break;
2550.         default:
2551.            idx = S_room;
2552.            break;
2553.         }
2554. 
2555.         if (!cansee(x, y) && (!lev->waslit || flags.dark_room)) {
2556.             /* Floor spaces and corridors are dark if unlit. */
2557.             if (lev->typ == CORR && idx == S_litcorr)
2558.                 idx = S_corr;
2559.             else if (idx == S_room)
2560.                 idx = (flags.dark_room && iflags.use_color)
2561.                          ? DARKROOMSYM : S_stone;
2562.         }
2563.         if (idx != S_room)
2564.             tmp_bkglyph = cmap_to_glyph(idx);
2565.     }
2566.     *bkglyph = tmp_bkglyph;
2567. #if 0
2568.     /* this guard should be unnecessary */
2569.     if (!framecolor) {
2570.         impossible("null framecolor passed to get_bkglyph_and_framecolor");
2571.         return;
2572.     }
2573. #endif
2574.     if (iflags.bgcolors
2575.         && gw.wsettings.map_frame_color != NO_COLOR && mapxy_valid(x, y))
2576.         *framecolor = gw.wsettings.map_frame_color;
2577.     else
2578.         *framecolor = NO_COLOR;
2579. }
2580. 
2581. #if defined(TILES_IN_GLYPHMAP) && defined(MSDOS)
2582. #define HAS_ROGUE_IBM_GRAPHICS \
2583.     (gc.currentgraphics == ROGUESET && SYMHANDLING(H_IBM) && !iflags.grmode)
2584. #else
2585. #define HAS_ROGUE_IBM_GRAPHICS \
2586.     (gc.currentgraphics == ROGUESET && SYMHANDLING(H_IBM))
2587. #endif
2588. 
2589. /* masks for per-level variances kept in gg.glyphmap_perlevel_flags */
2590. #define GMAP_SET            0x0001
2591. #define GMAP_ROGUELEVEL     0x0002
2592. 
2593. void
2594. map_glyphinfo(
2595.     coordxy x, coordxy y,
2596.     int glyph,
2597.     unsigned mgflags,
2598.     glyph_info *glyphinfo)
2599. {
2600.     int offset;
2601.     boolean is_you = (u_at(x, y)
2602.                       /* verify hero or steed (not something underneath
2603.                          when hero is invisible without see invisible,
2604.                          or a temporary display effect like an explosion);
2605.                          this is only approximate, because hero might be
2606.                          mimicking an object (after eating mimic corpse or
2607.                          if polyd into mimic) or furniture (only if polyd) */
2608.                       && glyph_is_monster(glyph));
2609.     const glyph_map *gmap = &glyphmap[glyph];
2610. 
2611.     glyphinfo->gm = *gmap; /* glyphflags, sym.symidx, sym.color, tileidx */
2612.     /*
2613.      * Only hero tinkering permitted on-the-fly (who).
2614.      * Unique glyphs in glyphmap[] determine everything else (what).
2615.      *
2616.      * (Note: if hero is invisible without see invisible, he/she usually
2617.      * can't see himself/herself.  This applies to accessibility hacks
2618.      * as well as to regular display.)
2619.      */
2620.     if (is_you) {
2621.         if (!iflags.use_color || Upolyd || glyph != hero_glyph) {
2622.             ; /* color tweak not needed (!use_color) or not wanted (poly'd
2623.                  or riding--which uses steed's color, not hero's) */
2624.         } else if (HAS_ROGUE_IBM_GRAPHICS
2625.                    && gs.symset[gc.currentgraphics].nocolor == 0) {
2626.             /* actually player should be yellow-on-gray if in corridor */
2627.             glyphinfo->gm.sym.color = CLR_YELLOW;
2628.         } else if (flags.showrace) {
2629.             /* for showrace, non-human hero is displayed by the symbol of
2630.                corresponding type of monster rather than by '@' (handled
2631.                by newsym()); we change the color to same as human hero */
2632.             glyphinfo->gm.sym.color = HI_DOMESTIC;
2633.         }
2634.         /* accessibility
2635.           This unchanging display character for hero was requested by
2636.           a blind player to enhance screen reader use.
2637.           Turn on override symbol if caller hasn't specified NOOVERRIDE. */
2638.         if (sysopt.accessibility == 1 && !(mgflags & MG_FLAG_NOOVERRIDE)) {
2639.             offset = SYM_HERO_OVERRIDE + SYM_OFF_X;
2640.             if ((gg.glyphmap_perlevel_flags & GMAP_ROGUELEVEL)
2641.                     ? go.ov_rogue_syms[offset]
2642.                     : go.ov_primary_syms[offset])
2643.                 glyphinfo->gm.sym.symidx = offset;
2644.         }
2645.         glyphinfo->gm.glyphflags |= MG_HERO;
2646.     }
2647.     if (sysopt.accessibility == 1
2648.         && (mgflags & MG_FLAG_NOOVERRIDE) && glyph_is_pet(glyph)) {
2649.         /* one more accessibility kludge;
2650.            turn off override symbol if caller has specified NOOVERRIDE */
2651.         glyphinfo->gm.sym.symidx = mons[glyph_to_mon(glyph)].mlet + SYM_OFF_M;
2652.     }
2653.     glyphinfo->ttychar = gs.showsyms[glyphinfo->gm.sym.symidx];
2654.     glyphinfo->glyph = glyph;
2655. }
2656. 
2657. /*
2658.  *  This must be the same order as used for buzz() in zap.c.
2659.  *  The zap_color_ and altar_color_ enums are in decl.h.
2660.  */
2661. const int zapcolors[NUM_ZAP] = {
2662.     zap_color_missile,    zap_color_fire,  zap_color_frost,
2663.     zap_color_sleep,      zap_color_death, zap_color_lightning,
2664.     zap_color_poison_gas, zap_color_acid,
2665. };
2666. const int altarcolors[] = {
2667.     altar_color_unaligned, altar_color_chaotic, altar_color_neutral,
2668.     altar_color_lawful, altar_color_other
2669. };
2670. const int explodecolors[EXPL_MAX] = {
2671.     explode_color_dark,   explode_color_noxious, explode_color_muddy,
2672.     explode_color_wet,    explode_color_magical, explode_color_fiery,
2673.     explode_color_frosty,
2674. };
2675. 
2676. /* main_walls, mines_walls, gehennom_walls, knox_walls, sokoban_walls */
2677. int wallcolors[sokoban_walls + 1] = {
2678.     /* default init value is to match defsym[S_vwall + n].color (CLR_GRAY) */
2679.     CLR_GRAY, CLR_GRAY, CLR_GRAY, CLR_GRAY, CLR_GRAY,
2680.     /* CLR_GRAY, CLR_BROWN, CLR_RED, CLR_GRAY, CLR_BRIGHT_BLUE, */
2681. };
2682. 
2683. #define zap_color(n) color = iflags.use_color ? zapcolors[n] : NO_COLOR
2684. #define cmap_color(n) color = iflags.use_color ? defsyms[n].color : NO_COLOR
2685. #define obj_color(n) color = iflags.use_color ? objects[n].oc_color : NO_COLOR
2686. #define mon_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR
2687. #define invis_color(n) color = NO_COLOR
2688. #define pet_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR
2689. #define warn_color(n) \
2690.     color = iflags.use_color ? def_warnsyms[n].color : NO_COLOR
2691. #define explode_color(n) \
2692.     color = iflags.use_color ? explodecolors[n] : NO_COLOR
2693. #define wall_color(n) color = iflags.use_color ? wallcolors[n] : NO_COLOR
2694. #define altar_color(n) color = iflags.use_color ? altarcolors[n] : NO_COLOR
2695. 
2696. staticfn int cmap_to_roguecolor(int);
2697. 
2698. staticfn int
2699. cmap_to_roguecolor(int cmap)
2700. {
2701.     int color = NO_COLOR;
2702. 
2703.     if (gs.symset[gc.currentgraphics].nocolor)
2704.         return NO_COLOR;
2705. 
2706.     if (cmap >= S_vwall && cmap <= S_hcdoor)
2707.         color = CLR_BROWN;
2708.     else if (cmap >= S_arrow_trap && cmap <= S_polymorph_trap)
2709.         color = CLR_MAGENTA;
2710.     else if (cmap == S_corr || cmap == S_litcorr)
2711.         color = CLR_GRAY;
2712.     else if (cmap >= S_room && cmap <= S_water
2713.                 && cmap != S_darkroom)
2714.         color = CLR_GREEN;
2715.     else
2716.         color = NO_COLOR;
2717. 
2718.     return color;
2719. }
2720. 
2721. /*
2722.    reset_glyphmap(trigger)
2723. 
2724.    The glyphmap likely needs to be re-calculated for the following triggers:
2725. 
2726.    gm_levelchange    called when the player has gone to a new level.
2727. 
2728.    gm_symchange      called if someone has interactively altered the symbols
2729.                      for the game, most likely at the options menu.
2730. 
2731.    gm_optionchange   The game settings have been toggled and some of them
2732.                      are configured to trigger a reset_glyphmap()
2733. 
2734.    gm_accessibility_change  One of the accessibility flags has changed.
2735. 
2736. */
2737. 
2738. void
2739. reset_glyphmap(enum glyphmap_change_triggers trigger)
2740. {
2741.     int glyph;
2742.     int offset;
2743.     int color = NO_COLOR;
2744. 
2745.     /* condense multiple tests in macro version down to single */
2746.     boolean has_rogue_ibm_graphics = HAS_ROGUE_IBM_GRAPHICS,
2747.             has_rogue_color = (has_rogue_ibm_graphics
2748.                                && gs.symset[gc.currentgraphics].nocolor == 0);
2749.     if (trigger == gm_levelchange)
2750.         gg.glyphmap_perlevel_flags = 0;
2751. 
2752.     if (!gg.glyphmap_perlevel_flags) {
2753.         /*
2754.          *    GMAP_SET                0x00000001
2755.          *    GMAP_ROGUELEVEL         0x00000002
2756.          */
2757.         gg.glyphmap_perlevel_flags |= GMAP_SET;
2758. 
2759.         if (Is_rogue_level(&u.uz)) {
2760.             gg.glyphmap_perlevel_flags |= GMAP_ROGUELEVEL;
2761.         }
2762.     }
2763. 
2764.     for (glyph = 0; glyph < MAX_GLYPH; ++glyph) {
2765.         glyph_map *gmap = &glyphmap[glyph];
2766. 
2767.         gmap->glyphflags = 0U;
2768.         /*
2769.          *  Map the glyph to a character and color.
2770.          *
2771.          *  Warning:  For speed, this makes an assumption on the order of
2772.          *            offsets.  The order is set in display.h.
2773.          */
2774.         if ((offset = (glyph - GLYPH_NOTHING_OFF)) >= 0) {
2775.             gmap->sym.symidx = SYM_NOTHING + SYM_OFF_X;
2776.             color = NO_COLOR;
2777.             gmap->glyphflags |= MG_NOTHING;
2778.         } else if ((offset = (glyph - GLYPH_UNEXPLORED_OFF)) >= 0) {
2779.             gmap->sym.symidx = SYM_UNEXPLORED + SYM_OFF_X;
2780.             color = NO_COLOR;
2781.             gmap->glyphflags |= MG_UNEXPL;
2782.         } else if ((offset = (glyph - GLYPH_STATUE_FEM_PILETOP_OFF)) >= 0) {
2783.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2784.             if (has_rogue_color)
2785.                 color = CLR_RED;
2786.             else
2787.                 obj_color(STATUE);
2788.             gmap->glyphflags |= (MG_STATUE | MG_FEMALE | MG_OBJPILE);
2789.         } else if ((offset = (glyph - GLYPH_STATUE_MALE_PILETOP_OFF)) >= 0) {
2790.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2791.             if (has_rogue_color)
2792.                 color = CLR_RED;
2793.             else
2794.                 obj_color(STATUE);
2795.             gmap->glyphflags |= (MG_STATUE | MG_MALE | MG_OBJPILE);
2796.         } else if ((offset = (glyph - GLYPH_BODY_PILETOP_OFF)) >= 0) {
2797.             gmap->sym.symidx = objects[CORPSE].oc_class + SYM_OFF_O;
2798.             if (has_rogue_color)
2799.                 color = CLR_RED;
2800.             else
2801.                 mon_color(offset);
2802.             gmap->glyphflags |= (MG_CORPSE | MG_OBJPILE);
2803.         } else if ((offset = (glyph - GLYPH_OBJ_PILETOP_OFF)) >= 0) {
2804.             gmap->sym.symidx = objects[offset].oc_class + SYM_OFF_O;
2805.             if (offset == BOULDER)
2806.                 gmap->sym.symidx = SYM_BOULDER + SYM_OFF_X;
2807.             if (has_rogue_color) {
2808.                 switch (objects[offset].oc_class) {
2809.                 case COIN_CLASS:
2810.                     color = CLR_YELLOW;
2811.                     break;
2812.                 case FOOD_CLASS:
2813.                     color = CLR_RED;
2814.                     break;
2815.                 default:
2816.                     color = CLR_BRIGHT_BLUE;
2817.                     break;
2818.                 }
2819.             } else
2820.                 obj_color(offset);
2821.             gmap->glyphflags |= MG_OBJPILE;
2822.         } else if ((offset = (glyph - GLYPH_STATUE_FEM_OFF)) >= 0) {
2823.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2824.             if (has_rogue_color)
2825.                 color = CLR_RED;
2826.             else
2827.                 obj_color(STATUE);
2828.             gmap->glyphflags |= (MG_STATUE | MG_FEMALE);
2829.         } else if ((offset = (glyph - GLYPH_STATUE_MALE_OFF)) >= 0) {
2830.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2831.             if (has_rogue_color)
2832.                 color = CLR_RED;
2833.             else
2834.                 obj_color(STATUE);
2835.             gmap->glyphflags |= (MG_STATUE | MG_MALE);
2836.         } else if ((offset = (glyph - GLYPH_WARNING_OFF))
2837.                    >= 0) { /* warn flash */
2838.             gmap->sym.symidx = offset + SYM_OFF_W;
2839.             if (has_rogue_color)
2840.                 color = NO_COLOR;
2841.             else
2842.                 warn_color(offset);
2843.         } else if ((offset = (glyph - GLYPH_EXPLODE_FROSTY_OFF)) >= 0) {
2844.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2845.             explode_color(expl_frosty);
2846.         } else if ((offset = (glyph - GLYPH_EXPLODE_FIERY_OFF)) >= 0) {
2847.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2848.             explode_color(expl_fiery);
2849.         } else if ((offset = (glyph - GLYPH_EXPLODE_MAGICAL_OFF)) >= 0) {
2850.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2851.             explode_color(expl_magical);
2852.         } else if ((offset = (glyph - GLYPH_EXPLODE_WET_OFF)) >= 0) {
2853.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2854.             explode_color(expl_wet);
2855.         } else if ((offset = (glyph - GLYPH_EXPLODE_MUDDY_OFF)) >= 0) {
2856.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2857.             explode_color(expl_muddy);
2858.         } else if ((offset = (glyph - GLYPH_EXPLODE_NOXIOUS_OFF)) >= 0) {
2859.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2860.             explode_color(expl_noxious);
2861.         } else if ((offset = (glyph - GLYPH_EXPLODE_DARK_OFF)) >= 0) {
2862.             gmap->sym.symidx = S_expl_tl + offset + SYM_OFF_P;
2863.             explode_color(expl_dark);
2864.         } else if ((offset = (glyph - GLYPH_SWALLOW_OFF)) >= 0) {
2865.             /* see swallow_to_glyph() in display.c */
2866.             gmap->sym.symidx = (S_sw_tl + (offset & 0x7)) + SYM_OFF_P;
2867.             if (has_rogue_color)
2868.                 color = NO_COLOR;
2869.             else
2870.                 mon_color(offset >> 3);
2871.         } else if ((offset = (glyph - GLYPH_CMAP_C_OFF)) >= 0) {
2872.             gmap->sym.symidx = S_digbeam + offset + SYM_OFF_P;
2873.             if (has_rogue_color)
2874.                 color = cmap_to_roguecolor(S_digbeam + offset);
2875.             else
2876.                 cmap_color(S_digbeam + offset);
2877.         } else if ((offset = (glyph - GLYPH_ZAP_OFF)) >= 0) {
2878.             /* see zapdir_to_glyph() in display.c */
2879.             gmap->sym.symidx = (S_vbeam + (offset & 0x3)) + SYM_OFF_P;
2880.             if (has_rogue_color)
2881.                 color = NO_COLOR;
2882.             else
2883.                 zap_color((offset >> 2));
2884.         } else if ((offset = (glyph - GLYPH_CMAP_B_OFF)) >= 0) {
2885.             int cmap = S_grave + offset;
2886.             int sym = gs.showsyms[cmap + SYM_OFF_P];
2887. 
2888.             gmap->sym.symidx = cmap + SYM_OFF_P;
2889.             cmap_color(cmap);
2890.             if (!iflags.use_color) {
2891.                 unsigned spec_cmap = 0;
2892. 
2893.                 /* try to provide a visible difference between water and lava
2894.                    if they use the same symbol and color is disabled;
2895.                    similar for floor and ice, for fountain vs sink, and for
2896.                    corridor engravings (CMAP_A below) */
2897.                 switch (cmap) {
2898.                 case S_lava:
2899.                 case S_lavawall:
2900.                     if (sym == gs.showsyms[S_pool + SYM_OFF_P]
2901.                         || sym == gs.showsyms[S_water + SYM_OFF_P])
2902.                         spec_cmap = MG_BW_LAVA;
2903.                     break;
2904.                 case S_ice:
2905.                     if (sym == gs.showsyms[S_room + SYM_OFF_P]
2906.                         || sym == gs.showsyms[S_darkroom + SYM_OFF_P])
2907.                         spec_cmap = MG_BW_ICE;
2908.                     break;
2909.                 case S_sink:
2910.                     if (sym == gs.showsyms[S_fountain + SYM_OFF_P])
2911.                         spec_cmap = MG_BW_SINK;
2912.                     break;
2913.                 }
2914.                 gmap->glyphflags |= spec_cmap;
2915.             } else if (has_rogue_color) {
2916.                 color = cmap_to_roguecolor(cmap);
2917.             }
2918.         } else if ((offset = (glyph - GLYPH_ALTAR_OFF)) >= 0) {
2919.             /* unaligned, chaotic, neutral, lawful, other altar */
2920.             gmap->sym.symidx = S_altar + SYM_OFF_P;
2921.             if (has_rogue_color)
2922.                 color = cmap_to_roguecolor(S_altar);
2923.             else
2924.                 altar_color(offset);
2925.         } else if ((offset = (glyph - GLYPH_CMAP_A_OFF)) >= 0) {
2926.             int sym, cmap = S_ndoor + offset;
2927. 
2928.             gmap->sym.symidx = cmap + SYM_OFF_P;
2929.             cmap_color(cmap);
2930.             sym = gs.showsyms[gmap->sym.symidx];
2931.             /*
2932.              *   Some specialty color mappings not hardcoded in data init
2933.              */
2934.             if (has_rogue_color) {
2935.                 color = cmap_to_roguecolor(cmap);
2936.             /* provide a visible difference if normal and lit corridor
2937.                use the same symbol */
2938.             } else if (cmap == S_litcorr
2939.                        && sym == gs.showsyms[S_corr + SYM_OFF_P]) {
2940.                 color = CLR_WHITE;
2941.             /* likewise for corridor and engraving-in-corridor */
2942.             } else if (cmap == S_engrcorr
2943.                        && (sym == gs.showsyms[S_corr + SYM_OFF_P]
2944.                            || sym == gs.showsyms[S_litcorr + SYM_OFF_P])) {
2945.                 gmap->glyphflags |= MG_BW_ENGR;
2946.             }
2947.         } else if ((offset = (glyph - GLYPH_CMAP_SOKO_OFF)) >= 0) {
2948.             gmap->sym.symidx = S_vwall + offset + SYM_OFF_P;
2949.             wall_color(sokoban_walls);
2950.         } else if ((offset = (glyph - GLYPH_CMAP_KNOX_OFF)) >= 0) {
2951.             gmap->sym.symidx = S_vwall + offset + SYM_OFF_P;
2952.             wall_color(knox_walls);
2953.         } else if ((offset = (glyph - GLYPH_CMAP_GEH_OFF)) >= 0) {
2954.             gmap->sym.symidx = S_vwall + offset + SYM_OFF_P;
2955.             wall_color(gehennom_walls);
2956.         } else if ((offset = (glyph - GLYPH_CMAP_MINES_OFF)) >= 0) {
2957.             gmap->sym.symidx = S_vwall + offset + SYM_OFF_P;
2958.             wall_color(mines_walls);
2959.         } else if ((offset = (glyph - GLYPH_CMAP_MAIN_OFF)) >= 0) {
2960.             gmap->sym.symidx = S_vwall + offset + SYM_OFF_P;
2961.             if (has_rogue_color)
2962.                 color = cmap_to_roguecolor(S_vwall + offset);
2963.             else
2964.                 wall_color(main_walls);
2965.         } else if ((offset = (glyph - GLYPH_CMAP_STONE_OFF)) >= 0) {
2966.             gmap->sym.symidx = SYM_OFF_P;
2967.             cmap_color(S_stone);
2968.         } else if ((offset = (glyph - GLYPH_OBJ_OFF)) >= 0) {
2969.             gmap->sym.symidx = objects[offset].oc_class + SYM_OFF_O;
2970.             if (offset == BOULDER)
2971.                 gmap->sym.symidx = SYM_BOULDER + SYM_OFF_X;
2972.             if (has_rogue_color) {
2973.                 switch (objects[offset].oc_class) {
2974.                 case COIN_CLASS:
2975.                     color = CLR_YELLOW;
2976.                     break;
2977.                 case FOOD_CLASS:
2978.                     color = CLR_RED;
2979.                     break;
2980.                 default:
2981.                     color = CLR_BRIGHT_BLUE;
2982.                     break;
2983.                 }
2984.             } else
2985.                 obj_color(offset);
2986.         } else if ((offset = (glyph - GLYPH_RIDDEN_FEM_OFF)) >= 0) {
2987.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2988.             if (has_rogue_color)
2989.                 /* This currently implies that the hero is here -- monsters */
2990.                 /* don't ride (yet...).  Should we set it to yellow like in */
2991.                 /* the monster case below?  There is no equivalent in rogue.
2992.                  */
2993.                 color = NO_COLOR;
2994.             else
2995.                 mon_color(offset);
2996.             gmap->glyphflags |= (MG_RIDDEN | MG_FEMALE);
2997.         } else if ((offset = (glyph - GLYPH_RIDDEN_MALE_OFF)) >= 0) {
2998.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
2999.             if (has_rogue_color)
3000.                 color = NO_COLOR;
3001.             else
3002.                 mon_color(offset);
3003.             gmap->glyphflags |= (MG_RIDDEN | MG_MALE);
3004.         } else if ((offset = (glyph - GLYPH_BODY_OFF)) >= 0) {
3005.             gmap->sym.symidx = objects[CORPSE].oc_class + SYM_OFF_O;
3006.             if (has_rogue_color)
3007.                 color = CLR_RED;
3008.             else
3009.                 mon_color(offset);
3010.             gmap->glyphflags |= MG_CORPSE;
3011.         } else if ((offset = (glyph - GLYPH_DETECT_FEM_OFF)) >= 0) {
3012.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3013.             if (has_rogue_color)
3014.                 color = NO_COLOR;
3015.             else
3016.                 mon_color(offset);
3017.             /* Disabled for now; anyone want to get reverse video to work? */
3018.             /* is_reverse = TRUE; */
3019.             gmap->glyphflags |= (MG_DETECT | MG_FEMALE);
3020.         } else if ((offset = (glyph - GLYPH_DETECT_MALE_OFF)) >= 0) {
3021.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3022.             if (has_rogue_color)
3023.                 color = NO_COLOR;
3024.             else
3025.                 mon_color(offset);
3026.             /* Disabled for now; anyone want to get reverse video to work? */
3027.             /* is_reverse = TRUE; */
3028.             gmap->glyphflags |= (MG_DETECT | MG_MALE);
3029.         } else if ((offset = (glyph - GLYPH_INVIS_OFF)) >= 0) {
3030.             gmap->sym.symidx = SYM_INVISIBLE + SYM_OFF_X;
3031.             if (has_rogue_color)
3032.                 color = NO_COLOR;
3033.             else
3034.                 invis_color(offset);
3035.             gmap->glyphflags |= MG_INVIS;
3036.         } else if ((offset = (glyph - GLYPH_PET_FEM_OFF)) >= 0) {
3037.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3038.             if (has_rogue_color)
3039.                 color = NO_COLOR;
3040.             else
3041.                 pet_color(offset);
3042.             gmap->glyphflags |= (MG_PET | MG_FEMALE);
3043.         } else if ((offset = (glyph - GLYPH_PET_MALE_OFF)) >= 0) {
3044.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3045.             if (has_rogue_color)
3046.                 color = NO_COLOR;
3047.             else
3048.                 pet_color(offset);
3049.             gmap->glyphflags |= (MG_PET | MG_MALE);
3050.         } else if ((offset = (glyph - GLYPH_MON_FEM_OFF)) >= 0) {
3051.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3052.             if (has_rogue_color) {
3053.                 color = NO_COLOR;
3054.             } else {
3055.                 mon_color(offset);
3056.             }
3057.             gmap->glyphflags |= MG_FEMALE;
3058.         } else if ((offset = (glyph - GLYPH_MON_MALE_OFF)) >= 0) {
3059.             gmap->sym.symidx = mons[offset].mlet + SYM_OFF_M;
3060.             if (has_rogue_color) {
3061.                 color = CLR_YELLOW;
3062.             } else {
3063.                 mon_color(offset);
3064.             }
3065.             gmap->glyphflags |= MG_MALE;
3066.         }
3067.         /* This was requested by a blind player to enhance screen reader use
3068.          */
3069.         if (sysopt.accessibility == 1 && (gmap->glyphflags & MG_PET) != 0) {
3070.             int pet_override = ((gg.glyphmap_perlevel_flags & GMAP_ROGUELEVEL)
3071.                          ? go.ov_rogue_syms[SYM_PET_OVERRIDE + SYM_OFF_X]
3072.                          : go.ov_primary_syms[SYM_PET_OVERRIDE + SYM_OFF_X]);
3073. 
3074.             if (gs.showsyms[pet_override] != ' ')
3075.                 gmap->sym.symidx = SYM_PET_OVERRIDE + SYM_OFF_X;
3076.         }
3077.         /* Turn off color if no color defined, or rogue level w/o PC graphics.
3078.          */
3079.         if ((!has_color(color)
3080.              || ((gg.glyphmap_perlevel_flags & GMAP_ROGUELEVEL)
3081.                  && !has_rogue_color)) || !iflags.use_color)
3082.             color = NO_COLOR;
3083.         gmap->sym.color = color;
3084.     }
3085.     gg.glyph_reset_timestamp = svm.moves;
3086. }
3087. 
3088. /* ------------------------------------------------------------------------ */
3089. /* Wall Angle ------------------------------------------------------------- */
3090. 
3091. #ifdef WA_VERBOSE
3092. 
3093. staticfn const char *type_to_name(int);
3094. staticfn void error4(coordxy, coordxy, int, int, int, int);
3095. 
3096. static int bad_count[MAX_TYPE]; /* count of positions flagged as bad */
3097. static const char *const type_names[MAX_TYPE] = {
3098.     "STONE", "VWALL", "HWALL", "TLCORNER", "TRCORNER", "BLCORNER", "BRCORNER",
3099.     "CROSSWALL", "TUWALL", "TDWALL", "TLWALL", "TRWALL", "DBWALL", "TREE",
3100.     "SDOOR", "SCORR", "POOL", "MOAT", "WATER", "DRAWBRIDGE_UP", "LAVAPOOL",
3101.     "LAVAWALL",
3102.     "IRON_BARS", "DOOR", "CORR", "ROOM", "STAIRS", "LADDER", "FOUNTAIN",
3103.     "THRONE", "SINK", "GRAVE", "ALTAR", "ICE", "DRAWBRIDGE_DOWN", "AIR",
3104.     "CLOUD"
3105. };
3106. 
3107. staticfn const char *
3108. type_to_name(int type)
3109. {
3110.     return (type < 0 || type >= MAX_TYPE) ? "unknown" : type_names[type];
3111. }
3112. 
3113. staticfn void
3114. error4(coordxy x, coordxy y, int a, int b, int c, int dd)
3115. {
3116.     pline("set_wall_state: %s @ (%d,%d) %s%s%s%s",
3117.           type_to_name(levl[x][y].typ), x, y,
3118.           a ? "1" : "", b ? "2" : "", c ? "3" : "", dd ? "4" : "");
3119.     bad_count[levl[x][y].typ]++;
3120. }
3121. #endif /* WA_VERBOSE */
3122. 
3123. /*
3124.  * Return 'which' if position is implies an unfinished exterior.  Return
3125.  * zero otherwise.  Unfinished implies outer area is rock or a corridor.
3126.  *
3127.  * Things that are ambiguous: lava
3128.  */
3129. staticfn int
3130. check_pos(coordxy x, coordxy y, int which)
3131. {
3132.     int type;
3133. 
3134.     if (!isok(x, y))
3135.         return which;
3136.     type = levl[x][y].typ;
3137.     /* Everything below POOL, excluding TREE */
3138.     if (IS_STWALL(type) || type == CORR || type == SCORR || IS_SDOOR(type))
3139.         return which;
3140.     return 0;
3141. }
3142. 
3143. /* Return TRUE if more than one is non-zero. */
3144. /*ARGSUSED*/
3145. #ifdef WA_VERBOSE
3146. staticfn boolean
3147. more_than_one(coordxy x, coordxy y, coordxy a, coordxy b, coordxy c)
3148. {
3149.     if ((a && (b | c)) || (b && (a | c)) || (c && (a | b))) {
3150.         error4(x, y, a, b, c, 0);
3151.         return TRUE;
3152.     }
3153.     return FALSE;
3154. }
3155. #else
3156. #define more_than_one(x, y, a, b, c) \
3157.     (((a) && ((b) | (c))) || ((b) && ((a) | (c))) || ((c) && ((a) | (b))))
3158. #endif
3159. 
3160. /* Return the wall mode for a T wall. */
3161. staticfn int
3162. set_twall(
3163. #ifdef WA_VERBOSE
3164.           coordxy x0, coordxy y0, /* used #if WA_VERBOSE */
3165. #else
3166.           coordxy x0 UNUSED, coordxy y0 UNUSED,
3167. #endif
3168.           coordxy x1, coordxy y1,
3169.           coordxy x2, coordxy y2,
3170.           coordxy x3, coordxy y3)
3171. {
3172.     int wmode, is_1, is_2, is_3;
3173. 
3174.     is_1 = check_pos(x1, y1, WM_T_LONG);
3175.     is_2 = check_pos(x2, y2, WM_T_BL);
3176.     is_3 = check_pos(x3, y3, WM_T_BR);
3177.     if (more_than_one(x0, y0, is_1, is_2, is_3)) {
3178.         wmode = 0;
3179.     } else {
3180.         wmode = is_1 + is_2 + is_3;
3181.     }
3182.     return wmode;
3183. }
3184. 
3185. /* Return wall mode for a horizontal or vertical wall. */
3186. staticfn int
3187. set_wall(coordxy x, coordxy y, int horiz)
3188. {
3189.     int wmode, is_1, is_2;
3190. 
3191.     if (horiz) {
3192.         is_1 = check_pos(x, y - 1, WM_W_TOP);
3193.         is_2 = check_pos(x, y + 1, WM_W_BOTTOM);
3194.     } else {
3195.         is_1 = check_pos(x - 1, y, WM_W_LEFT);
3196.         is_2 = check_pos(x + 1, y, WM_W_RIGHT);
3197.     }
3198.     if (more_than_one(x, y, is_1, is_2, 0)) {
3199.         wmode = 0;
3200.     } else {
3201.         wmode = is_1 + is_2;
3202.     }
3203.     return wmode;
3204. }
3205. 
3206. /* Return a wall mode for a corner wall. (x4,y4) is the "inner" position. */
3207. staticfn int
3208. set_corn(
3209.     coordxy x1, coordxy y1,
3210.     coordxy x2, coordxy y2,
3211.     coordxy x3, coordxy y3,
3212.     coordxy x4, coordxy y4)
3213. {
3214.     coordxy wmode, is_1, is_2, is_3, is_4;
3215. 
3216.     is_1 = check_pos(x1, y1, 1);
3217.     is_2 = check_pos(x2, y2, 1);
3218.     is_3 = check_pos(x3, y3, 1);
3219.     is_4 = check_pos(x4, y4, 1); /* inner location */
3220. 
3221.     /*
3222.      * All 4 should not be true.  So if the inner location is rock,
3223.      * use it.  If all of the outer 3 are true, use outer.  We currently
3224.      * can't cover the case where only part of the outer is rock, so
3225.      * we just say that all the walls are finished (if not overridden
3226.      * by the inner section).
3227.      */
3228.     if (is_4) {
3229.         wmode = WM_C_INNER;
3230.     } else if (is_1 && is_2 && is_3)
3231.         wmode = WM_C_OUTER;
3232.     else
3233.         wmode = 0; /* finished walls on all sides */
3234. 
3235.     return wmode;
3236. }
3237. 
3238. /* Return mode for a crosswall. */
3239. staticfn int
3240. set_crosswall(coordxy x, coordxy y)
3241. {
3242.     coordxy wmode, is_1, is_2, is_3, is_4;
3243. 
3244.     is_1 = check_pos(x - 1, y - 1, 1);
3245.     is_2 = check_pos(x + 1, y - 1, 1);
3246.     is_3 = check_pos(x + 1, y + 1, 1);
3247.     is_4 = check_pos(x - 1, y + 1, 1);
3248. 
3249.     wmode = is_1 + is_2 + is_3 + is_4;
3250.     if (wmode > 1) {
3251.         if (is_1 && is_3 && (is_2 + is_4 == 0)) {
3252.             wmode = WM_X_TLBR;
3253.         } else if (is_2 && is_4 && (is_1 + is_3 == 0)) {
3254.             wmode = WM_X_BLTR;
3255.         } else {
3256. #ifdef WA_VERBOSE
3257.             error4(x, y, is_1, is_2, is_3, is_4);
3258. #endif
3259.             wmode = 0;
3260.         }
3261.     } else if (is_1)
3262.         wmode = WM_X_TL;
3263.     else if (is_2)
3264.         wmode = WM_X_TR;
3265.     else if (is_3)
3266.         wmode = WM_X_BR;
3267.     else if (is_4)
3268.         wmode = WM_X_BL;
3269. 
3270.     return wmode;
3271. }
3272. 
3273. /* called for every <x,y> by set_wall_state() and for specific <x,y> during
3274.    vault wall repair */
3275. void
3276. xy_set_wall_state(coordxy x, coordxy y)
3277. {
3278.     coordxy wmode;
3279.     struct rm *lev = &levl[x][y];
3280. 
3281.     switch (lev->typ) {
3282.     case SDOOR:
3283.         wmode = set_wall(x, y, (coordxy) lev->horizontal);
3284.         break;
3285.     case VWALL:
3286.         wmode = set_wall(x, y, 0);
3287.         break;
3288.     case HWALL:
3289.         wmode = set_wall(x, y, 1);
3290.         break;
3291.     case TDWALL:
3292.         wmode = set_twall(x, y, x, y - 1, x - 1, y + 1, x + 1, y + 1);
3293.         break;
3294.     case TUWALL:
3295.         wmode = set_twall(x, y, x, y + 1, x + 1, y - 1, x - 1, y - 1);
3296.         break;
3297.     case TLWALL:
3298.         wmode = set_twall(x, y, x + 1, y, x - 1, y - 1, x - 1, y + 1);
3299.         break;
3300.     case TRWALL:
3301.         wmode = set_twall(x, y, x - 1, y, x + 1, y + 1, x + 1, y - 1);
3302.         break;
3303.     case TLCORNER:
3304.         wmode = set_corn(x - 1, y - 1, x, y - 1, x - 1, y, x + 1, y + 1);
3305.         break;
3306.     case TRCORNER:
3307.         wmode = set_corn(x, y - 1, x + 1, y - 1, x + 1, y, x - 1, y + 1);
3308.         break;
3309.     case BLCORNER:
3310.         wmode = set_corn(x, y + 1, x - 1, y + 1, x - 1, y, x + 1, y - 1);
3311.         break;
3312.     case BRCORNER:
3313.         wmode = set_corn(x + 1, y, x + 1, y + 1, x, y + 1, x - 1, y - 1);
3314.         break;
3315.     case CROSSWALL:
3316.         wmode = set_crosswall(x, y);
3317.         break;
3318. 
3319.     default:
3320.         wmode = -1; /* don't set wall info */
3321.         break;
3322.     }
3323. 
3324.     if (wmode >= 0)
3325.         lev->wall_info = (lev->wall_info & ~WM_MASK) | wmode;
3326. }
3327. 
3328. /* Called from mklev.  Scan the level and set the wall modes. */
3329. void
3330. set_wall_state(void)
3331. {
3332.     coordxy x, y;
3333. 
3334. #ifdef WA_VERBOSE
3335.     for (x = 0; x < MAX_TYPE; x++)
3336.         bad_count[x] = 0;
3337. #endif
3338. 
3339.     for (x = 0; x < COLNO; x++)
3340.         for (y = 0; y < ROWNO; y++)
3341.             xy_set_wall_state(x, y);
3342. 
3343. #ifdef WA_VERBOSE
3344.     /* check if any bad positions found */
3345.     for (x = y = 0; x < MAX_TYPE; x++)
3346.         if (bad_count[x]) {
3347.             if (y == 0) {
3348.                 y = 1; /* only prcoordxy once */
3349.                 pline("set_wall_type: wall mode problems with: ");
3350.             }
3351.             pline("%s %d;", type_names[x], bad_count[x]);
3352.         }
3353. #endif /* WA_VERBOSE */
3354. }
3355. 
3356. /* ------------------------------------------------------------------------ */
3357. /* This matrix is used here and in vision.c. */
3358. const seenV seenv_matrix[3][3] = {
3359.     { SV2, SV1,   SV0 },
3360.     { SV3, SVALL, SV7 },
3361.     { SV4, SV5,   SV6 }
3362. };
3363. 
3364. /* negative: -1, zero: 0, positive +1; same as sgn() function (hacklib.c) */
3365. #define sign(z) ((z) < 0 ? -1 : ((z) != 0))
3366. 
3367. /* Set the seen vector of lev as if seen from (x0,y0) to (x,y). */
3368. staticfn void
3369. set_seenv(
3370.     struct rm *lev,
3371.     coordxy x0, coordxy y0, /* from */
3372.     coordxy x, coordxy y)   /*  to  */
3373. {
3374.     coordxy dx = x - x0, dy = y0 - y;
3375. 
3376.     lev->seenv |= seenv_matrix[sign(dy) + 1][sign(dx) + 1];
3377. }
3378. 
3379. #undef sign
3380. 
3381. /* Called by blackout(vault.c) when vault guard removes temporary corridor,
3382.    turning spot <x0,y0> back into stone; <x1,y1> is an adjacent spot. */
3383. void
3384. unset_seenv(
3385.     struct rm *lev,         /* &levl[x1][y1] */
3386.     coordxy x0, coordxy y0, /* from */
3387.     coordxy x1, coordxy y1) /*  to; abs(x1-x0)==1 && abs(y0-y1)==1 */
3388. 
3389. {
3390.     coordxy dx = x1 - x0, dy = y0 - y1;
3391. 
3392.     lev->seenv &= ~seenv_matrix[dy + 1][dx + 1];
3393. }
3394. 
3395. /* ------------------------------------------------------------------------ */
3396. 
3397. /* T wall types, one for each row in wall_matrix[][]. */
3398. #define T_d 0
3399. #define T_l 1
3400. #define T_u 2
3401. #define T_r 3
3402. 
3403. /*
3404.  * These are the column names of wall_matrix[][].  They are the "results"
3405.  * of a tdwall pattern match.  All T walls are rotated so they become
3406.  * a tdwall.  Then we do a single pattern match, but return the
3407.  * correct result for the original wall by using different rows for
3408.  * each of the wall types.
3409.  */
3410. #define T_stone 0
3411. #define T_tlcorn 1
3412. #define T_trcorn 2
3413. #define T_hwall 3
3414. #define T_tdwall 4
3415. 
3416. static const int wall_matrix[4][5] = {
3417.     { S_stone, S_tlcorn, S_trcorn, S_hwall, S_tdwall }, /* tdwall */
3418.     { S_stone, S_trcorn, S_brcorn, S_vwall, S_tlwall }, /* tlwall */
3419.     { S_stone, S_brcorn, S_blcorn, S_hwall, S_tuwall }, /* tuwall */
3420.     { S_stone, S_blcorn, S_tlcorn, S_vwall, S_trwall }, /* trwall */
3421. };
3422. 
3423. /* Cross wall types, one for each "solid" quarter.  Rows of cross_matrix[][].
3424.  */
3425. #define C_bl 0
3426. #define C_tl 1
3427. #define C_tr 2
3428. #define C_br 3
3429. 
3430. /*
3431.  * These are the column names for cross_matrix[][].  They express results
3432.  * in C_br (bottom right) terms.  All crosswalls with a single solid
3433.  * quarter are rotated so the solid section is at the bottom right.
3434.  * We pattern match on that, but return the correct result depending
3435.  * on which row we're looking at.
3436.  */
3437. #define C_trcorn 0
3438. #define C_brcorn 1
3439. #define C_blcorn 2
3440. #define C_tlwall 3
3441. #define C_tuwall 4
3442. #define C_crwall 5
3443. 
3444. static const int cross_matrix[4][6] = {
3445.     { S_brcorn, S_blcorn, S_tlcorn, S_tuwall, S_trwall, S_crwall },
3446.     { S_blcorn, S_tlcorn, S_trcorn, S_trwall, S_tdwall, S_crwall },
3447.     { S_tlcorn, S_trcorn, S_brcorn, S_tdwall, S_tlwall, S_crwall },
3448.     { S_trcorn, S_brcorn, S_blcorn, S_tlwall, S_tuwall, S_crwall },
3449. };
3450. 
3451. /* Print out a T wall warning and all interesting info. */
3452. staticfn void
3453. t_warn(struct rm *lev)
3454. {
3455.     static const char warn_str[] = "wall_angle: %s: case %d: seenv = 0x%x";
3456.     const char *wname;
3457. 
3458.     /* 5.0: non-T_wall cases added after shop repair (via breaching a wall,
3459.        using locking magic to put a door there, then unlocking the door;
3460.        D_CLOSED carried over to the wall) triggered warning for "unknown" */
3461.     switch (lev->typ) {
3462.     case TUWALL:
3463.         wname = "tuwall";
3464.         break;
3465.     case TLWALL:
3466.         wname = "tlwall";
3467.         break;
3468.     case TRWALL:
3469.         wname = "trwall";
3470.         break;
3471.     case TDWALL:
3472.         wname = "tdwall";
3473.         break;
3474.     case VWALL:
3475.         wname = "vwall";
3476.         break;
3477.     case HWALL:
3478.         wname = "hwall";
3479.         break;
3480.     case TLCORNER:
3481.         wname = "tlcorner";
3482.         break;
3483.     case TRCORNER:
3484.         wname = "trcorner";
3485.         break;
3486.     case BLCORNER:
3487.         wname = "blcorner";
3488.         break;
3489.     case BRCORNER:
3490.         wname = "brcorner";
3491.         break;
3492.     default:
3493.         wname = "unknown";
3494.         break;
3495.     }
3496.     impossible(warn_str, wname, lev->wall_info & WM_MASK,
3497.                (unsigned int) lev->seenv);
3498. }
3499. 
3500. /*
3501.  * Return the correct graphics character index using wall type, wall mode,
3502.  * and the seen vector.  It is expected that seenv is non zero.
3503.  *
3504.  * All T-wall vectors are rotated to be TDWALL.  All single crosswall
3505.  * blocks are rotated to bottom right.  All double crosswall are rotated
3506.  * to W_X_BLTR.  All results are converted back.
3507.  *
3508.  * The only way to understand this is to take out pen and paper and
3509.  * draw diagrams.  See rm.h for more details on the wall modes and
3510.  * seen vector (SV).
3511.  */
3512. staticfn int
3513. wall_angle(struct rm *lev)
3514. {
3515.     unsigned int seenv = lev->seenv & 0xff;
3516.     const int *row;
3517.     int col, idx;
3518. 
3519. #define only(sv, bits) (((sv) & (bits)) && !((sv) & ~(bits)))
3520.     switch (lev->typ) {
3521.     case TUWALL:
3522.         row = wall_matrix[T_u];
3523.         seenv = (seenv >> 4 | seenv << 4) & 0xff; /* rotate to tdwall */
3524.         goto do_twall;
3525.     case TLWALL:
3526.         row = wall_matrix[T_l];
3527.         seenv = (seenv >> 2 | seenv << 6) & 0xff; /* rotate to tdwall */
3528.         goto do_twall;
3529.     case TRWALL:
3530.         row = wall_matrix[T_r];
3531.         seenv = (seenv >> 6 | seenv << 2) & 0xff; /* rotate to tdwall */
3532.         goto do_twall;
3533.     case TDWALL:
3534.         row = wall_matrix[T_d];
3535.  do_twall:
3536.         switch (lev->wall_info & WM_MASK) {
3537.         case 0:
3538.             if (seenv == SV4) {
3539.                 col = T_tlcorn;
3540.             } else if (seenv == SV6) {
3541.                 col = T_trcorn;
3542.             } else if (seenv & (SV3 | SV5 | SV7)
3543.                        || ((seenv & SV4) && (seenv & SV6))) {
3544.                 col = T_tdwall;
3545.             } else if (seenv & (SV0 | SV1 | SV2)) {
3546.                 col = (seenv & (SV4 | SV6) ? T_tdwall : T_hwall);
3547.             } else {
3548.                 t_warn(lev);
3549.                 col = T_stone;
3550.             }
3551.             break;
3552.         case WM_T_LONG:
3553.             if (seenv & (SV3 | SV4) && !(seenv & (SV5 | SV6 | SV7))) {
3554.                 col = T_tlcorn;
3555.             } else if (seenv & (SV6 | SV7) && !(seenv & (SV3 | SV4 | SV5))) {
3556.                 col = T_trcorn;
3557.             } else if ((seenv & SV5)
3558.                        || ((seenv & (SV3 | SV4)) && (seenv & (SV6 | SV7)))) {
3559.                 col = T_tdwall;
3560.             } else {
3561.                 /* only SV0|SV1|SV2 */
3562.                 if (!only(seenv, SV0 | SV1 | SV2))
3563.                     t_warn(lev);
3564.                 col = T_stone;
3565.             }
3566.             break;
3567.         case WM_T_BL:
3568.             if (only(seenv, SV4 | SV5))
3569.                 col = T_tlcorn;
3570.             else if ((seenv & (SV0 | SV1 | SV2 | SV7))
3571.                      && !(seenv & (SV3 | SV4 | SV5)))
3572.                 col = T_hwall;
3573.             else if (only(seenv, SV6))
3574.                 col = T_stone;
3575.             else
3576.                 col = T_tdwall;
3577.             break;
3578.         case WM_T_BR:
3579.             if (only(seenv, SV5 | SV6))
3580.                 col = T_trcorn;
3581.             else if ((seenv & (SV0 | SV1 | SV2 | SV3))
3582.                      && !(seenv & (SV5 | SV6 | SV7)))
3583.                 col = T_hwall;
3584.             else if (only(seenv, SV4))
3585.                 col = T_stone;
3586.             else
3587.                 col = T_tdwall;
3588. 
3589.             break;
3590.         default:
3591.             impossible("wall_angle: unknown T wall mode %d",
3592.                        lev->wall_info & WM_MASK);
3593.             col = T_stone;
3594.             break;
3595.         }
3596.         idx = row[col];
3597.         break;
3598. 
3599.     case SDOOR:
3600.         if (lev->arboreal_sdoor) {
3601.             idx = S_tree;
3602.             break;
3603.         }
3604.         if (lev->horizontal)
3605.             goto horiz;
3606.         FALLTHROUGH;
3607.         /*FALLTHRU*/
3608.     case VWALL:
3609.         switch (lev->wall_info & WM_MASK) {
3610.         case 0:
3611.             idx = seenv ? S_vwall : S_stone;
3612.             break;
3613.         case 1:
3614.             idx = seenv & (SV1 | SV2 | SV3 | SV4 | SV5) ? S_vwall : S_stone;
3615.             break;
3616.         case 2:
3617.             idx = seenv & (SV0 | SV1 | SV5 | SV6 | SV7) ? S_vwall : S_stone;
3618.             break;
3619.         default:
3620.             impossible("wall_angle: unknown vwall mode %d",
3621.                        lev->wall_info & WM_MASK);
3622.             idx = S_stone;
3623.             break;
3624.         }
3625.         break;
3626. 
3627.     case HWALL:
3628.  horiz:
3629.         switch (lev->wall_info & WM_MASK) {
3630.         case 0:
3631.             idx = seenv ? S_hwall : S_stone;
3632.             break;
3633.         case 1:
3634.             idx = seenv & (SV3 | SV4 | SV5 | SV6 | SV7) ? S_hwall : S_stone;
3635.             break;
3636.         case 2:
3637.             idx = seenv & (SV0 | SV1 | SV2 | SV3 | SV7) ? S_hwall : S_stone;
3638.             break;
3639.         default:
3640.             impossible("wall_angle: unknown hwall mode %d",
3641.                        lev->wall_info & WM_MASK);
3642.             idx = S_stone;
3643.             break;
3644.         }
3645.         break;
3646. 
3647. #define set_corner(idx, lev, which, outer, inner, name)    \
3648.     switch ((lev)->wall_info & WM_MASK) {                  \
3649.     case 0:                                                \
3650.         idx = which;                                       \
3651.         break;                                             \
3652.     case WM_C_OUTER:                                       \
3653.         idx = seenv & (outer) ? which : S_stone;           \
3654.         break;                                             \
3655.     case WM_C_INNER:                                       \
3656.         idx = seenv & ~(inner) ? which : S_stone;          \
3657.         break;                                             \
3658.     default:                                               \
3659.         impossible("wall_angle: unknown %s mode %d", name, \
3660.                    (lev)->wall_info &WM_MASK);             \
3661.         idx = S_stone;                                     \
3662.         break;                                             \
3663.     }
3664. 
3665.     case TLCORNER:
3666.         set_corner(idx, lev, S_tlcorn, (SV3 | SV4 | SV5), SV4, "tlcorn");
3667.         break;
3668.     case TRCORNER:
3669.         set_corner(idx, lev, S_trcorn, (SV5 | SV6 | SV7), SV6, "trcorn");
3670.         break;
3671.     case BLCORNER:
3672.         set_corner(idx, lev, S_blcorn, (SV1 | SV2 | SV3), SV2, "blcorn");
3673.         break;
3674.     case BRCORNER:
3675.         set_corner(idx, lev, S_brcorn, (SV7 | SV0 | SV1), SV0, "brcorn");
3676.         break;
3677. 
3678.     case CROSSWALL:
3679.         switch (lev->wall_info & WM_MASK) {
3680.         case 0:
3681.             if (seenv == SV0)
3682.                 idx = S_brcorn;
3683.             else if (seenv == SV2)
3684.                 idx = S_blcorn;
3685.             else if (seenv == SV4)
3686.                 idx = S_tlcorn;
3687.             else if (seenv == SV6)
3688.                 idx = S_trcorn;
3689.             else if (!(seenv & ~(SV0 | SV1 | SV2))
3690.                      && (seenv & SV1 || seenv == (SV0 | SV2)))
3691.                 idx = S_tuwall;
3692.             else if (!(seenv & ~(SV2 | SV3 | SV4))
3693.                      && (seenv & SV3 || seenv == (SV2 | SV4)))
3694.                 idx = S_trwall;
3695.             else if (!(seenv & ~(SV4 | SV5 | SV6))
3696.                      && (seenv & SV5 || seenv == (SV4 | SV6)))
3697.                 idx = S_tdwall;
3698.             else if (!(seenv & ~(SV0 | SV6 | SV7))
3699.                      && (seenv & SV7 || seenv == (SV0 | SV6)))
3700.                 idx = S_tlwall;
3701.             else
3702.                 idx = S_crwall;
3703.             break;
3704. 
3705.         case WM_X_TL:
3706.             row = cross_matrix[C_tl];
3707.             seenv = (seenv >> 4 | seenv << 4) & 0xff;
3708.             goto do_crwall;
3709.         case WM_X_TR:
3710.             row = cross_matrix[C_tr];
3711.             seenv = (seenv >> 6 | seenv << 2) & 0xff;
3712.             goto do_crwall;
3713.         case WM_X_BL:
3714.             row = cross_matrix[C_bl];
3715.             seenv = (seenv >> 2 | seenv << 6) & 0xff;
3716.             goto do_crwall;
3717.         case WM_X_BR:
3718.             row = cross_matrix[C_br];
3719.  do_crwall:
3720.             if (seenv == SV4) {
3721.                 idx = S_stone;
3722.             } else {
3723.                 seenv = seenv & ~SV4; /* strip SV4 */
3724.                 if (seenv == SV0) {
3725.                     col = C_brcorn;
3726.                 } else if (seenv & (SV2 | SV3)) {
3727.                     if (seenv & (SV5 | SV6 | SV7))
3728.                         col = C_crwall;
3729.                     else if (seenv & (SV0 | SV1))
3730.                         col = C_tuwall;
3731.                     else
3732.                         col = C_blcorn;
3733.                 } else if (seenv & (SV5 | SV6)) {
3734.                     if (seenv & (SV1 | SV2 | SV3))
3735.                         col = C_crwall;
3736.                     else if (seenv & (SV0 | SV7))
3737.                         col = C_tlwall;
3738.                     else
3739.                         col = C_trcorn;
3740.                 } else if (seenv & SV1) {
3741.                     col = seenv & SV7 ? C_crwall : C_tuwall;
3742.                 } else if (seenv & SV7) {
3743.                     col = seenv & SV1 ? C_crwall : C_tlwall;
3744.                 } else {
3745.                     impossible("wall_angle: bottom of crwall check");
3746.                     col = C_crwall;
3747.                 }
3748. 
3749.                 idx = row[col];
3750.             }
3751.             break;
3752. 
3753.         case WM_X_TLBR:
3754.             if (only(seenv, SV1 | SV2 | SV3))
3755.                 idx = S_blcorn;
3756.             else if (only(seenv, SV5 | SV6 | SV7))
3757.                 idx = S_trcorn;
3758.             else if (only(seenv, SV0 | SV4))
3759.                 idx = S_stone;
3760.             else
3761.                 idx = S_crwall;
3762.             break;
3763. 
3764.         case WM_X_BLTR:
3765.             if (only(seenv, SV0 | SV1 | SV7))
3766.                 idx = S_brcorn;
3767.             else if (only(seenv, SV3 | SV4 | SV5))
3768.                 idx = S_tlcorn;
3769.             else if (only(seenv, SV2 | SV6))
3770.                 idx = S_stone;
3771.             else
3772.                 idx = S_crwall;
3773.             break;
3774. 
3775.         default:
3776.             impossible("wall_angle: unknown crosswall mode");
3777.             idx = S_stone;
3778.             break;
3779.         }
3780.         break;
3781. 
3782.     default:
3783.         impossible("wall_angle: unexpected wall type %d", lev->typ);
3784.         idx = S_stone;
3785.     }
3786.     return idx;
3787. }
3788. 
3789. /*
3790.  * c++ 20 has problems with some of the display.h macros because
3791.  * comparisons and bit-fiddling and math between different enums
3792.  * is deprecated.
3793.  * Create function versions of some of the macros used in some
3794.  * NetHack c++ source files (Qt) for use there.
3795.  */
3796. int
3797. fn_cmap_to_glyph(int cmap)
3798. {
3799.     return cmap_to_glyph(cmap);
3800. }
3801. 
3802. /* for 'onefile' processing where end of this file isn't necessarily the
3803.    end of the source code seen by the compiler (there are lots of other
3804.    macros defined above...) */
3805. #undef _glyph_at
3806. #undef DETECTED
3807. #undef PHYSICALLY_SEEN
3808. #undef is_worm_tail
3809. #undef _suppress_map_output
3810. #undef TMP_AT_MAX_GLYPHS
3811. #undef Glyphinfo_at
3812. #undef reset_glyph_bbox
3813. #undef HAS_ROGUE_IBM_GRAPHICS
3814. #undef GMAP_SET
3815. #undef GMAP_ROGUELEVEL
3816. #ifndef WA_VERBOSE
3817. #undef more_than_one
3818. #endif
3819. #undef only
3820. #undef set_corner
3821. 
3822. /*display.c*/