Source:NetHack 3.6.3/src/detect.c

From NetHackWiki
Jump to navigation Jump to search

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

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

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

1. /* NetHack 3.6	detect.c	$NHDT-Date: 1575245054 2019/12/02 00:04:14 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.100 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2018. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /*
7.  * Detection routines, including crystal ball, magic mapping, and search
8.  * command.
9.  */
10. 
11. #include "hack.h"
12. #include "artifact.h"
13. 
14. extern boolean known; /* from read.c */
15. 
16. STATIC_DCL boolean NDECL(unconstrain_map);
17. STATIC_DCL void NDECL(reconstrain_map);
18. STATIC_DCL void FDECL(browse_map, (int, const char *));
19. STATIC_DCL void FDECL(map_monst, (struct monst *, BOOLEAN_P));
20. STATIC_DCL void FDECL(do_dknown_of, (struct obj *));
21. STATIC_DCL boolean FDECL(check_map_spot, (int, int, CHAR_P, unsigned));
22. STATIC_DCL boolean FDECL(clear_stale_map, (CHAR_P, unsigned));
23. STATIC_DCL void FDECL(sense_trap, (struct trap *, XCHAR_P, XCHAR_P, int));
24. STATIC_DCL int FDECL(detect_obj_traps, (struct obj *, BOOLEAN_P, int));
25. STATIC_DCL void FDECL(show_map_spot, (int, int));
26. STATIC_PTR void FDECL(findone, (int, int, genericptr_t));
27. STATIC_PTR void FDECL(openone, (int, int, genericptr_t));
28. STATIC_DCL int FDECL(mfind0, (struct monst *, BOOLEAN_P));
29. STATIC_DCL int FDECL(reveal_terrain_getglyph, (int, int, int,
30.                                                unsigned, int, int));
31. 
32. /* bring hero out from underwater or underground or being engulfed;
33.    return True iff any change occurred */
34. STATIC_OVL boolean
35. unconstrain_map()
36. {
37.     boolean res = u.uinwater || u.uburied || u.uswallow;
38. 
39.     /* bring Underwater, buried, or swallowed hero to normal map */
40.     iflags.save_uinwater = u.uinwater, u.uinwater = 0;
41.     iflags.save_uburied  = u.uburied,  u.uburied  = 0;
42.     iflags.save_uswallow = u.uswallow, u.uswallow = 0;
43. 
44.     return res;
45. }
46. 
47. /* put hero back underwater or underground or engulfed */
48. STATIC_OVL void
49. reconstrain_map()
50. {
51.     u.uinwater = iflags.save_uinwater, iflags.save_uinwater = 0;
52.     u.uburied  = iflags.save_uburied,  iflags.save_uburied  = 0;
53.     u.uswallow = iflags.save_uswallow, iflags.save_uswallow = 0;
54. }
55. 
56. /* use getpos()'s 'autodescribe' to view whatever is currently shown on map */
57. STATIC_DCL void
58. browse_map(ter_typ, ter_explain)
59. int ter_typ;
60. const char *ter_explain;
61. {
62.     coord dummy_pos; /* don't care whether player actually picks a spot */
63.     boolean save_autodescribe;
64. 
65.     dummy_pos.x = u.ux, dummy_pos.y = u.uy; /* starting spot for getpos() */
66.     save_autodescribe = iflags.autodescribe;
67.     iflags.autodescribe = TRUE;
68.     iflags.terrainmode = ter_typ;
69.     getpos(&dummy_pos, FALSE, ter_explain);
70.     iflags.terrainmode = 0;
71.     iflags.autodescribe = save_autodescribe;
72. }
73. 
74. /* extracted from monster_detection() so can be shared by do_vicinity_map() */
75. STATIC_DCL void
76. map_monst(mtmp, showtail)
77. struct monst *mtmp;
78. boolean showtail;
79. {
80.     if (def_monsyms[(int) mtmp->data->mlet].sym == ' ')
81.         show_glyph(mtmp->mx, mtmp->my,
82.                    detected_mon_to_glyph(mtmp, newsym_rn2));
83.     else
84.         show_glyph(mtmp->mx, mtmp->my, mtmp->mtame
85.                    ? pet_to_glyph(mtmp, newsym_rn2)
86.                    : mon_to_glyph(mtmp, newsym_rn2));
87. 
88.     if (showtail && mtmp->data == &mons[PM_LONG_WORM])
89.         detect_wsegs(mtmp, 0);
90. }
91. 
92. /* this is checking whether a trap symbol represents a trapped chest,
93.    not whether a trapped chest is actually present */
94. boolean
95. trapped_chest_at(ttyp, x, y)
96. int ttyp;
97. int x, y;
98. {
99.     struct monst *mtmp;
100.     struct obj *otmp;
101. 
102.     if (!glyph_is_trap(glyph_at(x, y)))
103.         return FALSE;
104.     if (ttyp != BEAR_TRAP || (Hallucination && rn2(20)))
105.         return FALSE;
106. 
107.     /*
108.      * TODO?  We should check containers recursively like the trap
109.      * detecting routine does.  Chests and large boxes do not nest in
110.      * themselves or each other, but could be contained inside statues.
111.      *
112.      * For farlook, we should also check for buried containers, but
113.      * for '^' command to examine adjacent trap glyph, we shouldn't.
114.      */
115. 
116.     /* on map, presence of any trappable container will do */
117.     if (sobj_at(CHEST, x, y) || sobj_at(LARGE_BOX, x, y))
118.         return TRUE;
119.     /* in inventory, we need to find one which is actually trapped */
120.     if (x == u.ux && y == u.uy) {
121.         for (otmp = invent; otmp; otmp = otmp->nobj)
122.             if (Is_box(otmp) && otmp->otrapped)
123.                 return TRUE;
124.         if (u.usteed) { /* steed isn't on map so won't be found by m_at() */
125.             for (otmp = u.usteed->minvent; otmp; otmp = otmp->nobj)
126.                 if (Is_box(otmp) && otmp->otrapped)
127.                     return TRUE;
128.         }
129.     }
130.     if ((mtmp = m_at(x, y)) != 0)
131.         for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
132.             if (Is_box(otmp) && otmp->otrapped)
133.                 return TRUE;
134.     return FALSE;
135. }
136. 
137. /* this is checking whether a trap symbol represents a trapped door,
138.    not whether the door here is actually trapped */
139. boolean
140. trapped_door_at(ttyp, x, y)
141. int ttyp;
142. int x, y;
143. {
144.     struct rm *lev;
145. 
146.     if (!glyph_is_trap(glyph_at(x, y)))
147.         return FALSE;
148.     if (ttyp != BEAR_TRAP || (Hallucination && rn2(20)))
149.         return FALSE;
150.     lev = &levl[x][y];
151.     if (!IS_DOOR(lev->typ))
152.         return FALSE;
153.     if ((lev->doormask & (D_NODOOR | D_BROKEN | D_ISOPEN)) != 0
154.          && trapped_chest_at(ttyp, x, y))
155.         return FALSE;
156.     return TRUE;
157. }
158. 
159. /* recursively search obj for an object in class oclass, return 1st found */
160. struct obj *
161. o_in(obj, oclass)
162. struct obj *obj;
163. char oclass;
164. {
165.     register struct obj *otmp;
166.     struct obj *temp;
167. 
168.     if (obj->oclass == oclass)
169.         return obj;
170.     /*
171.      * Note:  we exclude SchroedingersBox because the corpse it contains
172.      * isn't necessarily a corpse yet.  Resolving the status would lead
173.      * to complications if it turns out to be a live cat.  We know that
174.      * that Box can't contain anything else because putting something in
175.      * would resolve the cat/corpse situation and convert to ordinary box.
176.      */
177.     if (Has_contents(obj) && !SchroedingersBox(obj)) {
178.         for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
179.             if (otmp->oclass == oclass)
180.                 return otmp;
181.             else if (Has_contents(otmp) && (temp = o_in(otmp, oclass)) != 0)
182.                 return temp;
183.     }
184.     return (struct obj *) 0;
185. }
186. 
187. /* Recursively search obj for an object made of specified material.
188.  * Return first found.
189.  */
190. struct obj *
191. o_material(obj, material)
192. struct obj *obj;
193. unsigned material;
194. {
195.     register struct obj *otmp;
196.     struct obj *temp;
197. 
198.     if (objects[obj->otyp].oc_material == material)
199.         return obj;
200. 
201.     if (Has_contents(obj)) {
202.         for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
203.             if (objects[otmp->otyp].oc_material == material)
204.                 return otmp;
205.             else if (Has_contents(otmp)
206.                      && (temp = o_material(otmp, material)) != 0)
207.                 return temp;
208.     }
209.     return (struct obj *) 0;
210. }
211. 
212. STATIC_OVL void
213. do_dknown_of(obj)
214. struct obj *obj;
215. {
216.     struct obj *otmp;
217. 
218.     obj->dknown = 1;
219.     if (Has_contents(obj)) {
220.         for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
221.             do_dknown_of(otmp);
222.     }
223. }
224. 
225. /* Check whether the location has an outdated object displayed on it. */
226. STATIC_OVL boolean
227. check_map_spot(x, y, oclass, material)
228. int x, y;
229. char oclass;
230. unsigned material;
231. {
232.     int glyph;
233.     register struct obj *otmp;
234.     register struct monst *mtmp;
235. 
236.     glyph = glyph_at(x, y);
237.     if (glyph_is_object(glyph)) {
238.         /* there's some object shown here */
239.         if (oclass == ALL_CLASSES) {
240.             return (boolean) !(level.objects[x][y] /* stale if nothing here */
241.                                || ((mtmp = m_at(x, y)) != 0 && mtmp->minvent));
242.         } else {
243.             if (material
244.                 && objects[glyph_to_obj(glyph)].oc_material == material) {
245.                 /* object shown here is of interest because material matches */
246.                 for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
247.                     if (o_material(otmp, GOLD))
248.                         return FALSE;
249.                 /* didn't find it; perhaps a monster is carrying it */
250.                 if ((mtmp = m_at(x, y)) != 0) {
251.                     for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
252.                         if (o_material(otmp, GOLD))
253.                             return FALSE;
254.                 }
255.                 /* detection indicates removal of this object from the map */
256.                 return TRUE;
257.             }
258.             if (oclass && objects[glyph_to_obj(glyph)].oc_class == oclass) {
259.                 /* obj shown here is of interest because its class matches */
260.                 for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
261.                     if (o_in(otmp, oclass))
262.                         return FALSE;
263.                 /* didn't find it; perhaps a monster is carrying it */
264.                 if ((mtmp = m_at(x, y)) != 0) {
265.                     for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
266.                         if (o_in(otmp, oclass))
267.                             return FALSE;
268.                 }
269.                 /* detection indicates removal of this object from the map */
270.                 return TRUE;
271.             }
272.         }
273.     }
274.     return FALSE;
275. }
276. 
277. /*
278.  * When doing detection, remove stale data from the map display (corpses
279.  * rotted away, objects carried away by monsters, etc) so that it won't
280.  * reappear after the detection has completed.  Return true if noticeable
281.  * change occurs.
282.  */
283. STATIC_OVL boolean
284. clear_stale_map(oclass, material)
285. char oclass;
286. unsigned material;
287. {
288.     register int zx, zy;
289.     boolean change_made = FALSE;
290. 
291.     for (zx = 1; zx < COLNO; zx++)
292.         for (zy = 0; zy < ROWNO; zy++)
293.             if (check_map_spot(zx, zy, oclass, material)) {
294.                 unmap_object(zx, zy);
295.                 change_made = TRUE;
296.             }
297. 
298.     return change_made;
299. }
300. 
301. /* look for gold, on the floor or in monsters' possession */
302. int
303. gold_detect(sobj)
304. register struct obj *sobj;
305. {
306.     register struct obj *obj;
307.     register struct monst *mtmp;
308.     struct obj gold, *temp = 0;
309.     boolean stale, ugold = FALSE, steedgold = FALSE;
310.     int ter_typ = TER_DETECT | TER_OBJ;
311. 
312.     known = stale = clear_stale_map(COIN_CLASS,
313.                                     (unsigned) (sobj->blessed ? GOLD : 0));
314. 
315.     /* look for gold carried by monsters (might be in a container) */
316.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
317.         if (DEADMONSTER(mtmp))
318.             continue; /* probably not needed in this case but... */
319.         if (findgold(mtmp->minvent) || monsndx(mtmp->data) == PM_GOLD_GOLEM) {
320.             if (mtmp == u.usteed) {
321.                 steedgold = TRUE;
322.             } else {
323.                 known = TRUE;
324.                 goto outgoldmap; /* skip further searching */
325.             }
326.         } else {
327.             for (obj = mtmp->minvent; obj; obj = obj->nobj)
328.                 if ((sobj->blessed && o_material(obj, GOLD))
329.                     || o_in(obj, COIN_CLASS)) {
330.                     if (mtmp == u.usteed) {
331.                         steedgold = TRUE;
332.                     } else {
333.                         known = TRUE;
334.                         goto outgoldmap; /* skip further searching */
335.                     }
336.                 }
337.         }
338.     }
339. 
340.     /* look for gold objects */
341.     for (obj = fobj; obj; obj = obj->nobj) {
342.         if (sobj->blessed && o_material(obj, GOLD)) {
343.             known = TRUE;
344.             if (obj->ox != u.ux || obj->oy != u.uy)
345.                 goto outgoldmap;
346.         } else if (o_in(obj, COIN_CLASS)) {
347.             known = TRUE;
348.             if (obj->ox != u.ux || obj->oy != u.uy)
349.                 goto outgoldmap;
350.         }
351.     }
352. 
353.     if (!known) {
354.         /* no gold found on floor or monster's inventory.
355.            adjust message if you have gold in your inventory */
356.         if (sobj) {
357.             char buf[BUFSZ];
358. 
359.             if (youmonst.data == &mons[PM_GOLD_GOLEM])
360.                 Sprintf(buf, "You feel like a million %s!", currency(2L));
361.             else if (money_cnt(invent) || hidden_gold())
362.                 Strcpy(buf,
363.                    "You feel worried about your future financial situation.");
364.             else if (steedgold)
365.                 Sprintf(buf, "You feel interested in %s financial situation.",
366.                         s_suffix(x_monnam(u.usteed,
367.                                           u.usteed->mtame ? ARTICLE_YOUR
368.                                                           : ARTICLE_THE,
369.                                           (char *) 0,
370.                                           SUPPRESS_SADDLE, FALSE)));
371.             else
372.                 Strcpy(buf, "You feel materially poor.");
373. 
374.             strange_feeling(sobj, buf);
375.         }
376.         return 1;
377.     }
378.     /* only under me - no separate display required */
379.     if (stale)
380.         docrt();
381.     You("notice some gold between your %s.", makeplural(body_part(FOOT)));
382.     return 0;
383. 
384.  outgoldmap:
385.     cls();
386. 
387.     (void) unconstrain_map();
388.     /* Discover gold locations. */
389.     for (obj = fobj; obj; obj = obj->nobj) {
390.         if (sobj->blessed && (temp = o_material(obj, GOLD)) != 0) {
391.             if (temp != obj) {
392.                 temp->ox = obj->ox;
393.                 temp->oy = obj->oy;
394.             }
395.             map_object(temp, 1);
396.         } else if ((temp = o_in(obj, COIN_CLASS)) != 0) {
397.             if (temp != obj) {
398.                 temp->ox = obj->ox;
399.                 temp->oy = obj->oy;
400.             }
401.             map_object(temp, 1);
402.         }
403.         if (temp && temp->ox == u.ux && temp->oy == u.uy)
404.             ugold = TRUE;
405.     }
406.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
407.         if (DEADMONSTER(mtmp))
408.             continue; /* probably overkill here */
409.         temp = 0;
410.         if (findgold(mtmp->minvent) || monsndx(mtmp->data) == PM_GOLD_GOLEM) {
411.             gold = zeroobj; /* ensure oextra is cleared too */
412.             gold.otyp = GOLD_PIECE;
413.             gold.quan = (long) rnd(10); /* usually more than 1 */
414.             gold.ox = mtmp->mx;
415.             gold.oy = mtmp->my;
416.             map_object(&gold, 1);
417.             temp = &gold;
418.         } else {
419.             for (obj = mtmp->minvent; obj; obj = obj->nobj)
420.                 if (sobj->blessed && (temp = o_material(obj, GOLD)) != 0) {
421.                     temp->ox = mtmp->mx;
422.                     temp->oy = mtmp->my;
423.                     map_object(temp, 1);
424.                     break;
425.                 } else if ((temp = o_in(obj, COIN_CLASS)) != 0) {
426.                     temp->ox = mtmp->mx;
427.                     temp->oy = mtmp->my;
428.                     map_object(temp, 1);
429.                     break;
430.                 }
431.         }
432.         if (temp && temp->ox == u.ux && temp->oy == u.uy)
433.             ugold = TRUE;
434.     }
435.     if (!ugold) {
436.         newsym(u.ux, u.uy);
437.         ter_typ |= TER_MON; /* so autodescribe will recognize hero */
438.     }
439.     You_feel("very greedy, and sense gold!");
440.     exercise(A_WIS, TRUE);
441. 
442.     browse_map(ter_typ, "gold");
443. 
444.     reconstrain_map();
445.     docrt();
446.     if (Underwater)
447.         under_water(2);
448.     if (u.uburied)
449.         under_ground(2);
450.     return 0;
451. }
452. 
453. /* returns 1 if nothing was detected, 0 if something was detected */
454. int
455. food_detect(sobj)
456. register struct obj *sobj;
457. {
458.     register struct obj *obj;
459.     register struct monst *mtmp;
460.     register int ct = 0, ctu = 0;
461.     boolean confused = (Confusion || (sobj && sobj->cursed)), stale;
462.     char oclass = confused ? POTION_CLASS : FOOD_CLASS;
463.     const char *what = confused ? something : "food";
464. 
465.     stale = clear_stale_map(oclass, 0);
466.     if (u.usteed) /* some situations leave steed with stale coordinates */
467.         u.usteed->mx = u.ux, u.usteed->my = u.uy;
468. 
469.     for (obj = fobj; obj; obj = obj->nobj)
470.         if (o_in(obj, oclass)) {
471.             if (obj->ox == u.ux && obj->oy == u.uy)
472.                 ctu++;
473.             else
474.                 ct++;
475.         }
476.     for (mtmp = fmon; mtmp && (!ct || !ctu); mtmp = mtmp->nmon) {
477.         /* no DEADMONSTER(mtmp) check needed -- dmons never have inventory */
478.         for (obj = mtmp->minvent; obj; obj = obj->nobj)
479.             if (o_in(obj, oclass)) {
480.                 if (mtmp->mx == u.ux && mtmp->my == u.uy)
481.                     ctu++; /* steed or an engulfer with inventory */
482.                 else
483.                     ct++;
484.                 break;
485.             }
486.     }
487. 
488.     if (!ct && !ctu) {
489.         known = stale && !confused;
490.         if (stale) {
491.             docrt();
492.             You("sense a lack of %s nearby.", what);
493.             if (sobj && sobj->blessed) {
494.                 if (!u.uedibility)
495.                     Your("%s starts to tingle.", body_part(NOSE));
496.                 u.uedibility = 1;
497.             }
498.         } else if (sobj) {
499.             char buf[BUFSZ];
500. 
501.             Sprintf(buf, "Your %s twitches%s.", body_part(NOSE),
502.                     (sobj->blessed && !u.uedibility)
503.                         ? " then starts to tingle"
504.                         : "");
505.             if (sobj->blessed && !u.uedibility) {
506.                 boolean savebeginner = flags.beginner;
507. 
508.                 flags.beginner = FALSE; /* prevent non-delivery of message */
509.                 strange_feeling(sobj, buf);
510.                 flags.beginner = savebeginner;
511.                 u.uedibility = 1;
512.             } else
513.                 strange_feeling(sobj, buf);
514.         }
515.         return !stale;
516.     } else if (!ct) {
517.         known = TRUE;
518.         You("%s %s nearby.", sobj ? "smell" : "sense", what);
519.         if (sobj && sobj->blessed) {
520.             if (!u.uedibility)
521.                 pline("Your %s starts to tingle.", body_part(NOSE));
522.             u.uedibility = 1;
523.         }
524.     } else {
525.         struct obj *temp;
526.         int ter_typ = TER_DETECT | TER_OBJ;
527. 
528.         known = TRUE;
529.         cls();
530.         (void) unconstrain_map();
531.         for (obj = fobj; obj; obj = obj->nobj)
532.             if ((temp = o_in(obj, oclass)) != 0) {
533.                 if (temp != obj) {
534.                     temp->ox = obj->ox;
535.                     temp->oy = obj->oy;
536.                 }
537.                 map_object(temp, 1);
538.             }
539.         for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
540.             /* no DEADMONSTER() check needed -- dmons never have inventory */
541.             for (obj = mtmp->minvent; obj; obj = obj->nobj)
542.                 if ((temp = o_in(obj, oclass)) != 0) {
543.                     temp->ox = mtmp->mx;
544.                     temp->oy = mtmp->my;
545.                     map_object(temp, 1);
546.                     break; /* skip rest of this monster's inventory */
547.                 }
548.         if (!ctu) {
549.             newsym(u.ux, u.uy);
550.             ter_typ |= TER_MON; /* for autodescribe of self */
551.         }
552.         if (sobj) {
553.             if (sobj->blessed) {
554.                 Your("%s %s to tingle and you smell %s.", body_part(NOSE),
555.                      u.uedibility ? "continues" : "starts", what);
556.                 u.uedibility = 1;
557.             } else
558.                 Your("%s tingles and you smell %s.", body_part(NOSE), what);
559.         } else
560.             You("sense %s.", what);
561.         exercise(A_WIS, TRUE);
562. 
563.         browse_map(ter_typ, "food");
564. 
565.         reconstrain_map();
566.         docrt();
567.         if (Underwater)
568.             under_water(2);
569.         if (u.uburied)
570.             under_ground(2);
571.     }
572.     return 0;
573. }
574. 
575. /*
576.  * Used for scrolls, potions, spells, and crystal balls.  Returns:
577.  *
578.  *      1 - nothing was detected
579.  *      0 - something was detected
580.  */
581. int
582. object_detect(detector, class)
583. struct obj *detector; /* object doing the detecting */
584. int class;            /* an object class, 0 for all */
585. {
586.     register int x, y;
587.     char stuff[BUFSZ];
588.     int is_cursed = (detector && detector->cursed);
589.     int do_dknown = (detector && (detector->oclass == POTION_CLASS
590.                                   || detector->oclass == SPBOOK_CLASS)
591.                      && detector->blessed);
592.     int ct = 0, ctu = 0;
593.     register struct obj *obj, *otmp = (struct obj *) 0;
594.     register struct monst *mtmp;
595.     int sym, boulder = 0, ter_typ = TER_DETECT | TER_OBJ;
596. 
597.     if (class < 0 || class >= MAXOCLASSES) {
598.         impossible("object_detect:  illegal class %d", class);
599.         class = 0;
600.     }
601. 
602.     /* Special boulder symbol check - does the class symbol happen
603.      * to match showsyms[SYM_BOULDER + SYM_OFF_X] which is user-defined.
604.      * If so, that means we aren't sure what they really wanted to
605.      * detect. Rather than trump anything, show both possibilities.
606.      * We can exclude checking the buried obj chain for boulders below.
607.      */
608.     sym = class ? def_oc_syms[class].sym : 0;
609.     if (sym && showsyms[SYM_BOULDER + SYM_OFF_X] && sym == showsyms[SYM_BOULDER + SYM_OFF_X])
610.         boulder = ROCK_CLASS;
611. 
612.     if (Hallucination || (Confusion && class == SCROLL_CLASS))
613.         Strcpy(stuff, something);
614.     else
615.         Strcpy(stuff, class ? def_oc_syms[class].name : "objects");
616.     if (boulder && class != ROCK_CLASS)
617.         Strcat(stuff, " and/or large stones");
618. 
619.     if (do_dknown)
620.         for (obj = invent; obj; obj = obj->nobj)
621.             do_dknown_of(obj);
622. 
623.     for (obj = fobj; obj; obj = obj->nobj) {
624.         if ((!class && !boulder) || o_in(obj, class) || o_in(obj, boulder)) {
625.             if (obj->ox == u.ux && obj->oy == u.uy)
626.                 ctu++;
627.             else
628.                 ct++;
629.         }
630.         if (do_dknown)
631.             do_dknown_of(obj);
632.     }
633. 
634.     for (obj = level.buriedobjlist; obj; obj = obj->nobj) {
635.         if (!class || o_in(obj, class)) {
636.             if (obj->ox == u.ux && obj->oy == u.uy)
637.                 ctu++;
638.             else
639.                 ct++;
640.         }
641.         if (do_dknown)
642.             do_dknown_of(obj);
643.     }
644. 
645.     if (u.usteed)
646.         u.usteed->mx = u.ux, u.usteed->my = u.uy;
647. 
648.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
649.         if (DEADMONSTER(mtmp))
650.             continue;
651.         for (obj = mtmp->minvent; obj; obj = obj->nobj) {
652.             if ((!class && !boulder) || o_in(obj, class)
653.                 || o_in(obj, boulder))
654.                 ct++;
655.             if (do_dknown)
656.                 do_dknown_of(obj);
657.         }
658.         if ((is_cursed && M_AP_TYPE(mtmp) == M_AP_OBJECT
659.              && (!class || class == objects[mtmp->mappearance].oc_class))
660.             || (findgold(mtmp->minvent) && (!class || class == COIN_CLASS))) {
661.             ct++;
662.             break;
663.         }
664.     }
665. 
666.     if (!clear_stale_map(!class ? ALL_CLASSES : class, 0) && !ct) {
667.         if (!ctu) {
668.             if (detector)
669.                 strange_feeling(detector, "You feel a lack of something.");
670.             return 1;
671.         }
672. 
673.         You("sense %s nearby.", stuff);
674.         return 0;
675.     }
676. 
677.     cls();
678. 
679.     (void) unconstrain_map();
680.     /*
681.      *  Map all buried objects first.
682.      */
683.     for (obj = level.buriedobjlist; obj; obj = obj->nobj)
684.         if (!class || (otmp = o_in(obj, class)) != 0) {
685.             if (class) {
686.                 if (otmp != obj) {
687.                     otmp->ox = obj->ox;
688.                     otmp->oy = obj->oy;
689.                 }
690.                 map_object(otmp, 1);
691.             } else
692.                 map_object(obj, 1);
693.         }
694.     /*
695.      * If we are mapping all objects, map only the top object of a pile or
696.      * the first object in a monster's inventory.  Otherwise, go looking
697.      * for a matching object class and display the first one encountered
698.      * at each location.
699.      *
700.      * Objects on the floor override buried objects.
701.      */
702.     for (x = 1; x < COLNO; x++)
703.         for (y = 0; y < ROWNO; y++)
704.             for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
705.                 if ((!class && !boulder) || (otmp = o_in(obj, class)) != 0
706.                     || (otmp = o_in(obj, boulder)) != 0) {
707.                     if (class || boulder) {
708.                         if (otmp != obj) {
709.                             otmp->ox = obj->ox;
710.                             otmp->oy = obj->oy;
711.                         }
712.                         map_object(otmp, 1);
713.                     } else
714.                         map_object(obj, 1);
715.                     break;
716.                 }
717. 
718.     /* Objects in the monster's inventory override floor objects. */
719.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
720.         if (DEADMONSTER(mtmp))
721.             continue;
722.         for (obj = mtmp->minvent; obj; obj = obj->nobj)
723.             if ((!class && !boulder) || (otmp = o_in(obj, class)) != 0
724.                 || (otmp = o_in(obj, boulder)) != 0) {
725.                 if (!class && !boulder)
726.                     otmp = obj;
727.                 otmp->ox = mtmp->mx; /* at monster location */
728.                 otmp->oy = mtmp->my;
729.                 map_object(otmp, 1);
730.                 break;
731.             }
732.         /* Allow a mimic to override the detected objects it is carrying. */
733.         if (is_cursed && M_AP_TYPE(mtmp) == M_AP_OBJECT
734.             && (!class || class == objects[mtmp->mappearance].oc_class)) {
735.             struct obj temp;
736. 
737.             temp = zeroobj;
738.             temp.otyp = mtmp->mappearance; /* needed for obj_to_glyph() */
739.             temp.quan = 1L;
740.             temp.ox = mtmp->mx;
741.             temp.oy = mtmp->my;
742.             temp.corpsenm = PM_TENGU; /* if mimicing a corpse */
743.             map_object(&temp, 1);
744.         } else if (findgold(mtmp->minvent)
745.                    && (!class || class == COIN_CLASS)) {
746.             struct obj gold;
747. 
748.             gold = zeroobj; /* ensure oextra is cleared too */
749.             gold.otyp = GOLD_PIECE;
750.             gold.quan = (long) rnd(10); /* usually more than 1 */
751.             gold.ox = mtmp->mx;
752.             gold.oy = mtmp->my;
753.             map_object(&gold, 1);
754.         }
755.     }
756.     if (!glyph_is_object(glyph_at(u.ux, u.uy))) {
757.         newsym(u.ux, u.uy);
758.         ter_typ |= TER_MON;
759.     }
760.     You("detect the %s of %s.", ct ? "presence" : "absence", stuff);
761. 
762.     if (!ct)
763.         display_nhwindow(WIN_MAP, TRUE);
764.     else
765.         browse_map(ter_typ, "object");
766. 
767.     reconstrain_map();
768.     docrt(); /* this will correctly reset vision */
769.     if (Underwater)
770.         under_water(2);
771.     if (u.uburied)
772.         under_ground(2);
773.     return 0;
774. }
775. 
776. /*
777.  * Used by: crystal balls, potions, fountains
778.  *
779.  * Returns 1 if nothing was detected.
780.  * Returns 0 if something was detected.
781.  */
782. int
783. monster_detect(otmp, mclass)
784. register struct obj *otmp; /* detecting object (if any) */
785. int mclass;                /* monster class, 0 for all */
786. {
787.     register struct monst *mtmp;
788.     int mcnt = 0;
789. 
790.     /* Note: This used to just check fmon for a non-zero value
791.      * but in versions since 3.3.0 fmon can test TRUE due to the
792.      * presence of dmons, so we have to find at least one
793.      * with positive hit-points to know for sure.
794.      */
795.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
796.         if (!DEADMONSTER(mtmp)) {
797.             mcnt++;
798.             break;
799.         }
800. 
801.     if (!mcnt) {
802.         if (otmp)
803.             strange_feeling(otmp, Hallucination
804.                                       ? "You get the heebie jeebies."
805.                                       : "You feel threatened.");
806.         return 1;
807.     } else {
808.         boolean unconstrained, woken = FALSE;
809.         unsigned swallowed = u.uswallow; /* before unconstrain_map() */
810. 
811.         cls();
812.         unconstrained = unconstrain_map();
813.         for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
814.             if (DEADMONSTER(mtmp))
815.                 continue;
816.             if (!mclass || mtmp->data->mlet == mclass
817.                 || (mtmp->data == &mons[PM_LONG_WORM]
818.                     && mclass == S_WORM_TAIL))
819.                 map_monst(mtmp, TRUE);
820. 
821.             if (otmp && otmp->cursed
822.                 && (mtmp->msleeping || !mtmp->mcanmove)) {
823.                 mtmp->msleeping = mtmp->mfrozen = 0;
824.                 mtmp->mcanmove = 1;
825.                 woken = TRUE;
826.             }
827.         }
828.         if (!swallowed)
829.             display_self();
830.         You("sense the presence of monsters.");
831.         if (woken)
832.             pline("Monsters sense the presence of you.");
833. 
834.         if ((otmp && otmp->blessed) && !unconstrained) {
835.             /* persistent detection--just show updated map */
836.             display_nhwindow(WIN_MAP, TRUE);
837.         } else {
838.             /* one-shot detection--allow player to move cursor around and
839.                get autodescribe feedback */
840.             EDetect_monsters |= I_SPECIAL;
841.             browse_map(TER_DETECT | TER_MON, "monster of interest");
842.             EDetect_monsters &= ~I_SPECIAL;
843.         }
844. 
845.         reconstrain_map();
846.         docrt(); /* redraw the screen to remove unseen monsters from map */
847.         if (Underwater)
848.             under_water(2);
849.         if (u.uburied)
850.             under_ground(2);
851.     }
852.     return 0;
853. }
854. 
855. STATIC_OVL void
856. sense_trap(trap, x, y, src_cursed)
857. struct trap *trap;
858. xchar x, y;
859. int src_cursed;
860. {
861.     if (Hallucination || src_cursed) {
862.         struct obj obj; /* fake object */
863. 
864.         obj = zeroobj;
865.         if (trap) {
866.             obj.ox = trap->tx;
867.             obj.oy = trap->ty;
868.         } else {
869.             obj.ox = x;
870.             obj.oy = y;
871.         }
872.         obj.otyp = !Hallucination ? GOLD_PIECE : random_object(rn2);
873.         obj.quan = (long) ((obj.otyp == GOLD_PIECE) ? rnd(10)
874.                            : objects[obj.otyp].oc_merge ? rnd(2) : 1);
875.         obj.corpsenm = random_monster(rn2); /* if otyp == CORPSE */
876.         map_object(&obj, 1);
877.     } else if (trap) {
878.         map_trap(trap, 1);
879.         trap->tseen = 1;
880.     } else { /* trapped door or trapped chest */
881.         struct trap temp_trap; /* fake trap */
882. 
883.         (void) memset((genericptr_t) &temp_trap, 0, sizeof temp_trap);
884.         temp_trap.tx = x;
885.         temp_trap.ty = y;
886.         temp_trap.ttyp = BEAR_TRAP; /* some kind of trap */
887.         map_trap(&temp_trap, 1);
888.     }
889. }
890. 
891. #define OTRAP_NONE 0  /* nothing found */
892. #define OTRAP_HERE 1  /* found at hero's location */
893. #define OTRAP_THERE 2 /* found at any other location */
894. 
895. /* check a list of objects for chest traps; return 1 if found at <ux,uy>,
896.    2 if found at some other spot, 3 if both, 0 otherwise; optionally
897.    update the map to show where such traps were found */
898. STATIC_OVL int
899. detect_obj_traps(objlist, show_them, how)
900. struct obj *objlist;
901. boolean show_them;
902. int how; /* 1 for misleading map feedback */
903. {
904.     struct obj *otmp;
905.     xchar x, y;
906.     int result = OTRAP_NONE;
907. 
908.     /*
909.      * TODO?  Display locations of unarmed land mine and beartrap objects.
910.      * If so, should they be displayed as objects or as traps?
911.      */
912. 
913.     for (otmp = objlist; otmp; otmp = otmp->nobj) {
914.         if (Is_box(otmp) && otmp->otrapped
915.             && get_obj_location(otmp, &x, &y, BURIED_TOO | CONTAINED_TOO)) {
916.             result |= (x == u.ux && y == u.uy) ? OTRAP_HERE : OTRAP_THERE;
917.             if (show_them)
918.                 sense_trap((struct trap *) 0, x, y, how);
919.         }
920.         if (Has_contents(otmp))
921.             result |= detect_obj_traps(otmp->cobj, show_them, how);
922.     }
923.     return result;
924. }
925. 
926. /* the detections are pulled out so they can
927.  * also be used in the crystal ball routine
928.  * returns 1 if nothing was detected
929.  * returns 0 if something was detected
930.  */
931. int
932. trap_detect(sobj)
933. struct obj *sobj; /* null if crystal ball, *scroll if gold detection scroll */
934. {
935.     register struct trap *ttmp;
936.     struct monst *mon;
937.     int door, glyph, tr, ter_typ = TER_DETECT | TER_TRP;
938.     int cursed_src = sobj && sobj->cursed;
939.     boolean found = FALSE;
940.     coord cc;
941. 
942.     if (u.usteed)
943.         u.usteed->mx = u.ux, u.usteed->my = u.uy;
944. 
945.     /* floor/ceiling traps */
946.     for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
947.         if (ttmp->tx != u.ux || ttmp->ty != u.uy)
948.             goto outtrapmap;
949.         else
950.             found = TRUE;
951.     }
952.     /* chest traps (might be buried or carried) */
953.     if ((tr = detect_obj_traps(fobj, FALSE, 0)) != OTRAP_NONE) {
954.         if (tr & OTRAP_THERE)
955.             goto outtrapmap;
956.         else
957.             found = TRUE;
958.     }
959.     if ((tr = detect_obj_traps(level.buriedobjlist, FALSE, 0)) != OTRAP_NONE) {
960.         if (tr & OTRAP_THERE)
961.             goto outtrapmap;
962.         else
963.             found = TRUE;
964.     }
965.     for (mon = fmon; mon; mon = mon->nmon) {
966.         if (DEADMONSTER(mon))
967.             continue;
968.         if ((tr = detect_obj_traps(mon->minvent, FALSE, 0)) != OTRAP_NONE) {
969.             if (tr & OTRAP_THERE)
970.                 goto outtrapmap;
971.             else
972.                 found = TRUE;
973.         }
974.     }
975.     if (detect_obj_traps(invent, FALSE, 0) != OTRAP_NONE)
976.         found = TRUE;
977.     /* door traps */
978.     for (door = 0; door < doorindex; door++) {
979.         cc = doors[door];
980.         if (levl[cc.x][cc.y].doormask & D_TRAPPED) {
981.             if (cc.x != u.ux || cc.y != u.uy)
982.                 goto outtrapmap;
983.             else
984.                 found = TRUE;
985.         }
986.     }
987.     if (!found) {
988.         char buf[BUFSZ];
989. 
990.         Sprintf(buf, "Your %s stop itching.", makeplural(body_part(TOE)));
991.         strange_feeling(sobj, buf);
992.         return 1;
993.     }
994.     /* traps exist, but only under me - no separate display required */
995.     Your("%s itch.", makeplural(body_part(TOE)));
996.     return 0;
997. 
998.  outtrapmap:
999.     cls();
1000. 
1001.     (void) unconstrain_map();
1002.     /* show chest traps first, so that subsequent floor trap display
1003.        will override if both types are present at the same location */
1004.     (void) detect_obj_traps(fobj, TRUE, cursed_src);
1005.     (void) detect_obj_traps(level.buriedobjlist, TRUE, cursed_src);
1006.     for (mon = fmon; mon; mon = mon->nmon) {
1007.         if (DEADMONSTER(mon))
1008.             continue;
1009.         (void) detect_obj_traps(mon->minvent, TRUE, cursed_src);
1010.     }
1011.     (void) detect_obj_traps(invent, TRUE, cursed_src);
1012. 
1013.     for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
1014.         sense_trap(ttmp, 0, 0, cursed_src);
1015. 
1016.     for (door = 0; door < doorindex; door++) {
1017.         cc = doors[door];
1018.         if (levl[cc.x][cc.y].doormask & D_TRAPPED)
1019.             sense_trap((struct trap *) 0, cc.x, cc.y, cursed_src);
1020.     }
1021. 
1022.     /* redisplay hero unless sense_trap() revealed something at <ux,uy> */
1023.     glyph = glyph_at(u.ux, u.uy);
1024.     if (!(glyph_is_trap(glyph) || glyph_is_object(glyph))) {
1025.         newsym(u.ux, u.uy);
1026.         ter_typ |= TER_MON; /* for autodescribe at <u.ux,u.uy> */
1027.     }
1028.     You_feel("%s.", cursed_src ? "very greedy" : "entrapped");
1029. 
1030.     browse_map(ter_typ, "trap of interest");
1031. 
1032.     reconstrain_map();
1033.     docrt(); /* redraw the screen to remove unseen traps from the map */
1034.     if (Underwater)
1035.         under_water(2);
1036.     if (u.uburied)
1037.         under_ground(2);
1038.     return 0;
1039. }
1040. 
1041. const char *
1042. level_distance(where)
1043. d_level *where;
1044. {
1045.     register schar ll = depth(&u.uz) - depth(where);
1046.     register boolean indun = (u.uz.dnum == where->dnum);
1047. 
1048.     if (ll < 0) {
1049.         if (ll < (-8 - rn2(3)))
1050.             if (!indun)
1051.                 return "far away";
1052.             else
1053.                 return "far below";
1054.         else if (ll < -1)
1055.             if (!indun)
1056.                 return "away below you";
1057.             else
1058.                 return "below you";
1059.         else if (!indun)
1060.             return "in the distance";
1061.         else
1062.             return "just below";
1063.     } else if (ll > 0) {
1064.         if (ll > (8 + rn2(3)))
1065.             if (!indun)
1066.                 return "far away";
1067.             else
1068.                 return "far above";
1069.         else if (ll > 1)
1070.             if (!indun)
1071.                 return "away above you";
1072.             else
1073.                 return "above you";
1074.         else if (!indun)
1075.             return "in the distance";
1076.         else
1077.             return "just above";
1078.     } else if (!indun)
1079.         return "in the distance";
1080.     else
1081.         return "near you";
1082. }
1083. 
1084. static const struct {
1085.     const char *what;
1086.     d_level *where;
1087. } level_detects[] = {
1088.     { "Delphi", &oracle_level },
1089.     { "Medusa's lair", &medusa_level },
1090.     { "a castle", &stronghold_level },
1091.     { "the Wizard of Yendor's tower", &wiz1_level },
1092. };
1093. 
1094. void
1095. use_crystal_ball(optr)
1096. struct obj **optr;
1097. {
1098.     char ch;
1099.     int oops;
1100.     struct obj *obj = *optr;
1101. 
1102.     if (Blind) {
1103.         pline("Too bad you can't see %s.", the(xname(obj)));
1104.         return;
1105.     }
1106.     oops = (rnd(20) > ACURR(A_INT) || obj->cursed);
1107.     if (oops && (obj->spe > 0)) {
1108.         switch (rnd(obj->oartifact ? 4 : 5)) {
1109.         case 1:
1110.             pline("%s too much to comprehend!", Tobjnam(obj, "are"));
1111.             break;
1112.         case 2:
1113.             pline("%s you!", Tobjnam(obj, "confuse"));
1114.             make_confused((HConfusion & TIMEOUT) + (long) rnd(100), FALSE);
1115.             break;
1116.         case 3:
1117.             if (!resists_blnd(&youmonst)) {
1118.                 pline("%s your vision!", Tobjnam(obj, "damage"));
1119.                 make_blinded((Blinded & TIMEOUT) + (long) rnd(100), FALSE);
1120.                 if (!Blind)
1121.                     Your1(vision_clears);
1122.             } else {
1123.                 pline("%s your vision.", Tobjnam(obj, "assault"));
1124.                 You("are unaffected!");
1125.             }
1126.             break;
1127.         case 4:
1128.             pline("%s your mind!", Tobjnam(obj, "zap"));
1129.             (void) make_hallucinated(
1130.                 (HHallucination & TIMEOUT) + (long) rnd(100), FALSE, 0L);
1131.             break;
1132.         case 5:
1133.             pline("%s!", Tobjnam(obj, "explode"));
1134.             useup(obj);
1135.             *optr = obj = 0; /* it's gone */
1136.             /* physical damage cause by the shards and force */
1137.             losehp(Maybe_Half_Phys(rnd(30)), "exploding crystal ball",
1138.                    KILLED_BY_AN);
1139.             break;
1140.         }
1141.         if (obj)
1142.             consume_obj_charge(obj, TRUE);
1143.         return;
1144.     }
1145. 
1146.     if (Hallucination) {
1147.         if (!obj->spe) {
1148.             pline("All you see is funky %s haze.", hcolor((char *) 0));
1149.         } else {
1150.             switch (rnd(6)) {
1151.             case 1:
1152.                 You("grok some groovy globs of incandescent lava.");
1153.                 break;
1154.             case 2:
1155.                 pline("Whoa!  Psychedelic colors, %s!",
1156.                       poly_gender() == 1 ? "babe" : "dude");
1157.                 break;
1158.             case 3:
1159.                 pline_The("crystal pulses with sinister %s light!",
1160.                           hcolor((char *) 0));
1161.                 break;
1162.             case 4:
1163.                 You_see("goldfish swimming above fluorescent rocks.");
1164.                 break;
1165.             case 5:
1166.                 You_see(
1167.                     "tiny snowflakes spinning around a miniature farmhouse.");
1168.                 break;
1169.             default:
1170.                 pline("Oh wow... like a kaleidoscope!");
1171.                 break;
1172.             }
1173.             consume_obj_charge(obj, TRUE);
1174.         }
1175.         return;
1176.     }
1177. 
1178.     /* read a single character */
1179.     if (flags.verbose)
1180.         You("may look for an object or monster symbol.");
1181.     ch = yn_function("What do you look for?", (char *) 0, '\0');
1182.     /* Don't filter out ' ' here; it has a use */
1183.     if ((ch != def_monsyms[S_GHOST].sym) && index(quitchars, ch)) {
1184.         if (flags.verbose)
1185.             pline1(Never_mind);
1186.         return;
1187.     }
1188.     You("peer into %s...", the(xname(obj)));
1189.     nomul(-rnd(10));
1190.     multi_reason = "gazing into a crystal ball";
1191.     nomovemsg = "";
1192.     if (obj->spe <= 0) {
1193.         pline_The("vision is unclear.");
1194.     } else {
1195.         int class, i;
1196.         int ret = 0;
1197. 
1198.         makeknown(CRYSTAL_BALL);
1199.         consume_obj_charge(obj, TRUE);
1200. 
1201.         /* special case: accept ']' as synonym for mimic
1202.          * we have to do this before the def_char_to_objclass check
1203.          */
1204.         if (ch == DEF_MIMIC_DEF)
1205.             ch = DEF_MIMIC;
1206. 
1207.         if ((class = def_char_to_objclass(ch)) != MAXOCLASSES)
1208.             ret = object_detect((struct obj *) 0, class);
1209.         else if ((class = def_char_to_monclass(ch)) != MAXMCLASSES)
1210.             ret = monster_detect((struct obj *) 0, class);
1211.         else if (showsyms[SYM_BOULDER + SYM_OFF_X]
1212.                  && (ch == showsyms[SYM_BOULDER + SYM_OFF_X]))
1213.             ret = object_detect((struct obj *) 0, ROCK_CLASS);
1214.         else
1215.             switch (ch) {
1216.             case '^':
1217.                 ret = trap_detect((struct obj *) 0);
1218.                 break;
1219.             default:
1220.                 i = rn2(SIZE(level_detects));
1221.                 You_see("%s, %s.", level_detects[i].what,
1222.                         level_distance(level_detects[i].where));
1223.                 ret = 0;
1224.                 break;
1225.             }
1226. 
1227.         if (ret) {
1228.             if (!rn2(100)) /* make them nervous */
1229.                 You_see("the Wizard of Yendor gazing out at you.");
1230.             else
1231.                 pline_The("vision is unclear.");
1232.         }
1233.     }
1234.     return;
1235. }
1236. 
1237. STATIC_OVL void
1238. show_map_spot(x, y)
1239. register int x, y;
1240. {
1241.     struct rm *lev;
1242.     struct trap *t;
1243.     int oldglyph;
1244. 
1245.     if (Confusion && rn2(7))
1246.         return;
1247.     lev = &levl[x][y];
1248. 
1249.     lev->seenv = SVALL;
1250. 
1251.     /* Secret corridors are found, but not secret doors. */
1252.     if (lev->typ == SCORR) {
1253.         lev->typ = CORR;
1254.         unblock_point(x, y);
1255.     }
1256. 
1257.     /*
1258.      * Force the real background, then if it's not furniture and there's
1259.      * a known trap there, display the trap, else if there was an object
1260.      * shown there, redisplay the object.  So during mapping, furniture
1261.      * takes precedence over traps, which take precedence over objects,
1262.      * opposite to how normal vision behaves.
1263.      */
1264.     oldglyph = glyph_at(x, y);
1265.     if (level.flags.hero_memory) {
1266.         magic_map_background(x, y, 0);
1267.         newsym(x, y); /* show it, if not blocked */
1268.     } else {
1269.         magic_map_background(x, y, 1); /* display it */
1270.     }
1271.     if (!IS_FURNITURE(lev->typ)) {
1272.         if ((t = t_at(x, y)) != 0 && t->tseen) {
1273.             map_trap(t, 1);
1274.         } else if (glyph_is_trap(oldglyph) || glyph_is_object(oldglyph)) {
1275.             show_glyph(x, y, oldglyph);
1276.             if (level.flags.hero_memory)
1277.                 lev->glyph = oldglyph;
1278.         }
1279.     }
1280. }
1281. 
1282. void
1283. do_mapping()
1284. {
1285.     register int zx, zy;
1286.     boolean unconstrained;
1287. 
1288.     unconstrained = unconstrain_map();
1289.     for (zx = 1; zx < COLNO; zx++)
1290.         for (zy = 0; zy < ROWNO; zy++)
1291.             show_map_spot(zx, zy);
1292. 
1293.     if (!level.flags.hero_memory || unconstrained) {
1294.         flush_screen(1);                 /* flush temp screen */
1295.         /* browse_map() instead of display_nhwindow(WIN_MAP, TRUE) */
1296.         browse_map(TER_DETECT | TER_MAP | TER_TRP | TER_OBJ,
1297.                    "anything of interest");
1298.         docrt();
1299.     }
1300.     reconstrain_map();
1301.     exercise(A_WIS, TRUE);
1302. }
1303. 
1304. /* clairvoyance */
1305. void
1306. do_vicinity_map(sobj)
1307. struct obj *sobj; /* scroll--actually fake spellbook--object */
1308. {
1309.     register int zx, zy;
1310.     struct monst *mtmp;
1311.     struct obj *otmp;
1312.     long save_EDetect_mons;
1313.     char save_viz_uyux;
1314.     boolean unconstrained, refresh = FALSE,
1315.             mdetected = FALSE, odetected = FALSE,
1316.             /* fake spellbook 'sobj' implies hero has cast the spell;
1317.                when book is blessed, casting is skilled or expert level;
1318.                if already clairvoyant, non-skilled spell acts like skilled */
1319.             extended = (sobj && (sobj->blessed || Clairvoyant));
1320.     int newglyph, oldglyph,
1321.         lo_y = ((u.uy - 5 < 0) ? 0 : u.uy - 5),
1322.         hi_y = ((u.uy + 6 >= ROWNO) ? ROWNO - 1 : u.uy + 6),
1323.         lo_x = ((u.ux - 9 < 1) ? 1 : u.ux - 9), /* avoid column 0 */
1324.         hi_x = ((u.ux + 10 >= COLNO) ? COLNO - 1 : u.ux + 10),
1325.         ter_typ = TER_DETECT | TER_MAP | TER_TRP | TER_OBJ;
1326. 
1327.     /*
1328.      * 3.6.0 attempted to emphasize terrain over transient map
1329.      * properties (monsters and objects) but that led to problems.
1330.      * Notably, known trap would be displayed instead of a monster
1331.      * on or in it and then the display remained that way after the
1332.      * clairvoyant snapshot finished.  That could have been fixed by
1333.      * issuing --More-- and then regular vision update, but we want
1334.      * to avoid that when having a clairvoyant episode every N turns
1335.      * (from donating to a temple priest or by carrying the Amulet).
1336.      * Unlike when casting the spell, it is much too intrustive when
1337.      * in the midst of walking around or combatting monsters.
1338.      *
1339.      * As of 3.6.2, show terrain, then object, then monster like regular
1340.      * map updating, except in this case the map locations get marked
1341.      * as seen from every direction rather than just from direction of
1342.      * hero.  Skilled spell marks revealed objects as 'seen up close'
1343.      * (but for piles, only the top item) and shows monsters as if
1344.      * detected.  Non-skilled and timed clairvoyance reveals non-visible
1345.      * monsters as 'remembered, unseen'.
1346.      */
1347. 
1348.     /* if hero is engulfed, show engulfer at <u.ux,u.uy> */
1349.     save_viz_uyux = viz_array[u.uy][u.ux];
1350.     if (u.uswallow)
1351.         viz_array[u.uy][u.ux] |= IN_SIGHT; /* <x,y> are reversed to [y][x] */
1352.     save_EDetect_mons = EDetect_monsters;
1353.     /* for skilled spell, getpos() scanning of the map will display all
1354.        monsters within range; otherwise, "unseen creature" will be shown */
1355.     EDetect_monsters |= I_SPECIAL;
1356.     unconstrained = unconstrain_map();
1357.     for (zx = lo_x; zx <= hi_x; zx++)
1358.         for (zy = lo_y; zy <= hi_y; zy++) {
1359.             oldglyph = glyph_at(zx, zy);
1360.             /* this will remove 'remembered, unseen mon' (and objects) */
1361.             show_map_spot(zx, zy);
1362.             /* if there are any objects here, see the top one */
1363.             if (OBJ_AT(zx, zy)) {
1364.                 /* not vobj_at(); this is not vision-based access;
1365.                    unlike object detection, we don't notice buried items */
1366.                 otmp = level.objects[zx][zy];
1367.                 if (extended)
1368.                     otmp->dknown = 1;
1369.                 map_object(otmp, TRUE);
1370.                 newglyph = glyph_at(zx, zy);
1371.                 /* if otmp is underwater, we'll need to redisplay the water */
1372.                 if (newglyph != oldglyph && covers_objects(zx, zy))
1373.                     odetected = TRUE;
1374.             }
1375.             /* if there is a monster here, see or detect it,
1376.                possibly as "remembered, unseen monster" */
1377.             if ((mtmp = m_at(zx, zy)) != 0
1378.                 && mtmp->mx == zx && mtmp->my == zy) { /* skip worm tails */
1379.                 /* if we're going to offer browse_map()/getpos() scanning of
1380.                    the map and we're not doing extended/blessed clairvoyance
1381.                    (hence must be swallowed or underwater), show "unseen
1382.                    creature" unless map already displayed a monster here */
1383.                 if ((unconstrained || !level.flags.hero_memory)
1384.                     && !extended && (zx != u.ux || zy != u.uy)
1385.                     && !glyph_is_monster(oldglyph))
1386.                     map_invisible(zx, zy);
1387.                 else
1388.                     map_monst(mtmp, FALSE);
1389.                 newglyph = glyph_at(zx, zy);
1390.                 if (extended && newglyph != oldglyph
1391.                     && !glyph_is_invisible(newglyph))
1392.                     mdetected = TRUE;
1393.             }
1394.         }
1395. 
1396.     if (!level.flags.hero_memory || unconstrained || mdetected || odetected) {
1397.         flush_screen(1);                 /* flush temp screen */
1398.         /* the getpos() prompt from browse_map() is only shown when
1399.            flags.verbose is set, but make this unconditional so that
1400.            not-verbose users become aware of the prompting situation */
1401.         You("sense your surroundings.");
1402.         if (extended || glyph_is_monster(glyph_at(u.ux, u.uy)))
1403.             ter_typ |= TER_MON;
1404.         browse_map(ter_typ, "anything of interest");
1405.         refresh = TRUE;
1406.     }
1407.     reconstrain_map();
1408.     EDetect_monsters = save_EDetect_mons;
1409.     viz_array[u.uy][u.ux] = save_viz_uyux;
1410. 
1411.     /* replace monsters with remembered,unseen monster, then run
1412.        see_monsters() to update visible ones and warned-of ones */
1413.     for (zx = lo_x; zx <= hi_x; zx++)
1414.         for (zy = lo_y; zy <= hi_y; zy++) {
1415.             if (zx == u.ux && zy == u.uy)
1416.                 continue;
1417.             newglyph = glyph_at(zx, zy);
1418.             if (glyph_is_monster(newglyph)
1419.                 && glyph_to_mon(newglyph) != PM_LONG_WORM_TAIL) {
1420.                 /* map_invisible() was unconditional here but that made
1421.                    remembered objects be forgotten for the case where a
1422.                    monster is immediately redrawn by see_monsters() */
1423.                 if ((mtmp = m_at(zx, zy)) == 0 || !canspotmon(mtmp))
1424.                     map_invisible(zx, zy);
1425.             }
1426.         }
1427.     see_monsters();
1428. 
1429.     if (refresh)
1430.         docrt();
1431. }
1432. 
1433. /* convert a secret door into a normal door */
1434. void
1435. cvt_sdoor_to_door(lev)
1436. struct rm *lev;
1437. {
1438.     int newmask = lev->doormask & ~WM_MASK;
1439. 
1440.     if (Is_rogue_level(&u.uz))
1441.         /* rogue didn't have doors, only doorways */
1442.         newmask = D_NODOOR;
1443.     else
1444.         /* newly exposed door is closed */
1445.         if (!(newmask & D_LOCKED))
1446.         newmask |= D_CLOSED;
1447. 
1448.     lev->typ = DOOR;
1449.     lev->doormask = newmask;
1450. }
1451. 
1452. /* find something at one location; it should find all somethings there
1453.    since it is used for magical detection rather than physical searching */
1454. STATIC_PTR void
1455. findone(zx, zy, num)
1456. int zx, zy;
1457. genericptr_t num;
1458. {
1459.     register struct trap *ttmp;
1460.     register struct monst *mtmp;
1461. 
1462.     /*
1463.      * This used to use if/else-if/else-if/else/end-if but that only
1464.      * found the first hidden thing at the location.  Two hidden things
1465.      * at the same spot is uncommon, but it's possible for an undetected
1466.      * monster to be hiding at the location of an unseen trap.
1467.      */
1468. 
1469.     if (levl[zx][zy].typ == SDOOR) {
1470.         cvt_sdoor_to_door(&levl[zx][zy]); /* .typ = DOOR */
1471.         magic_map_background(zx, zy, 0);
1472.         newsym(zx, zy);
1473.         (*(int *) num)++;
1474.     } else if (levl[zx][zy].typ == SCORR) {
1475.         levl[zx][zy].typ = CORR;
1476.         unblock_point(zx, zy);
1477.         magic_map_background(zx, zy, 0);
1478.         newsym(zx, zy);
1479.         (*(int *) num)++;
1480.     }
1481. 
1482.     if ((ttmp = t_at(zx, zy)) != 0 && !ttmp->tseen
1483.         /* [shouldn't successful 'find' reveal and activate statue traps?] */
1484.         && ttmp->ttyp != STATUE_TRAP) {
1485.         ttmp->tseen = 1;
1486.         newsym(zx, zy);
1487.         (*(int *) num)++;
1488.     }
1489. 
1490.     if ((mtmp = m_at(zx, zy)) != 0
1491.         /* brings hidden monster out of hiding even if already sensed */
1492.         && (!canspotmon(mtmp) || mtmp->mundetected || M_AP_TYPE(mtmp))) {
1493.         if (M_AP_TYPE(mtmp)) {
1494.             seemimic(mtmp);
1495.             (*(int *) num)++;
1496.         } else if (mtmp->mundetected && (is_hider(mtmp->data)
1497.                                          || hides_under(mtmp->data)
1498.                                          || mtmp->data->mlet == S_EEL)) {
1499.             mtmp->mundetected = 0;
1500.             newsym(zx, zy);
1501.             (*(int *) num)++;
1502.         }
1503.         if (!canspotmon(mtmp) && !glyph_is_invisible(levl[zx][zy].glyph))
1504.             map_invisible(zx, zy);
1505.     } else if (unmap_invisible(zx, zy)) {
1506.         (*(int *) num)++;
1507.     }
1508. }
1509. 
1510. STATIC_PTR void
1511. openone(zx, zy, num)
1512. int zx, zy;
1513. genericptr_t num;
1514. {
1515.     register struct trap *ttmp;
1516.     register struct obj *otmp;
1517.     int *num_p = (int *) num;
1518. 
1519.     if (OBJ_AT(zx, zy)) {
1520.         for (otmp = level.objects[zx][zy]; otmp; otmp = otmp->nexthere) {
1521.             if (Is_box(otmp) && otmp->olocked) {
1522.                 otmp->olocked = 0;
1523.                 (*num_p)++;
1524.             }
1525.         }
1526.         /* let it fall to the next cases. could be on trap. */
1527.     }
1528.     if (levl[zx][zy].typ == SDOOR
1529.         || (levl[zx][zy].typ == DOOR
1530.             && (levl[zx][zy].doormask & (D_CLOSED | D_LOCKED)))) {
1531.         if (levl[zx][zy].typ == SDOOR)
1532.             cvt_sdoor_to_door(&levl[zx][zy]); /* .typ = DOOR */
1533.         if (levl[zx][zy].doormask & D_TRAPPED) {
1534.             if (distu(zx, zy) < 3)
1535.                 b_trapped("door", 0);
1536.             else
1537.                 Norep("You %s an explosion!",
1538.                       cansee(zx, zy) ? "see" : (!Deaf ? "hear"
1539.                                                       : "feel the shock of"));
1540.             wake_nearto(zx, zy, 11 * 11);
1541.             levl[zx][zy].doormask = D_NODOOR;
1542.         } else
1543.             levl[zx][zy].doormask = D_ISOPEN;
1544.         unblock_point(zx, zy);
1545.         newsym(zx, zy);
1546.         (*num_p)++;
1547.     } else if (levl[zx][zy].typ == SCORR) {
1548.         levl[zx][zy].typ = CORR;
1549.         unblock_point(zx, zy);
1550.         newsym(zx, zy);
1551.         (*num_p)++;
1552.     } else if ((ttmp = t_at(zx, zy)) != 0) {
1553.         struct monst *mon;
1554.         boolean dummy; /* unneeded "you notice it arg" */
1555. 
1556.         if (!ttmp->tseen && ttmp->ttyp != STATUE_TRAP) {
1557.             ttmp->tseen = 1;
1558.             newsym(zx, zy);
1559.             (*num_p)++;
1560.         }
1561.         mon = (zx == u.ux && zy == u.uy) ? &youmonst : m_at(zx, zy);
1562.         if (openholdingtrap(mon, &dummy)
1563.             || openfallingtrap(mon, TRUE, &dummy))
1564.             (*num_p)++;
1565.     } else if (find_drawbridge(&zx, &zy)) {
1566.         /* make sure it isn't an open drawbridge */
1567.         open_drawbridge(zx, zy);
1568.         (*num_p)++;
1569.     }
1570. }
1571. 
1572. /* returns number of things found */
1573. int
1574. findit()
1575. {
1576.     int num = 0;
1577. 
1578.     if (u.uswallow)
1579.         return 0;
1580.     do_clear_area(u.ux, u.uy, BOLT_LIM, findone, (genericptr_t) &num);
1581.     return num;
1582. }
1583. 
1584. /* returns number of things found and opened */
1585. int
1586. openit()
1587. {
1588.     int num = 0;
1589. 
1590.     if (u.uswallow) {
1591.         if (is_animal(u.ustuck->data)) {
1592.             if (Blind)
1593.                 pline("Its mouth opens!");
1594.             else
1595.                 pline("%s opens its mouth!", Monnam(u.ustuck));
1596.         }
1597.         expels(u.ustuck, u.ustuck->data, TRUE);
1598.         return -1;
1599.     }
1600. 
1601.     do_clear_area(u.ux, u.uy, BOLT_LIM, openone, (genericptr_t) &num);
1602.     return num;
1603. }
1604. 
1605. /* callback hack for overriding vision in do_clear_area() */
1606. boolean
1607. detecting(func)
1608. void FDECL((*func), (int, int, genericptr_t));
1609. {
1610.     return (func == findone || func == openone);
1611. }
1612. 
1613. void
1614. find_trap(trap)
1615. struct trap *trap;
1616. {
1617.     int tt = what_trap(trap->ttyp, rn2);
1618.     boolean cleared = FALSE;
1619. 
1620.     trap->tseen = 1;
1621.     exercise(A_WIS, TRUE);
1622.     feel_newsym(trap->tx, trap->ty);
1623. 
1624.     /* The "Hallucination ||" is to preserve 3.6.1 behaviour, but this
1625.        behaviour might need a rework in the hallucination case
1626.        (e.g. to not prompt if any trap glyph appears on the square). */
1627.     if (Hallucination ||
1628.         levl[trap->tx][trap->ty].glyph !=
1629.         trap_to_glyph(trap, rn2_on_display_rng)) {
1630.         /* There's too much clutter to see your find otherwise */
1631.         cls();
1632.         map_trap(trap, 1);
1633.         display_self();
1634.         cleared = TRUE;
1635.     }
1636. 
1637.     You("find %s.", an(defsyms[trap_to_defsym(tt)].explanation));
1638. 
1639.     if (cleared) {
1640.         display_nhwindow(WIN_MAP, TRUE); /* wait */
1641.         docrt();
1642.     }
1643. }
1644. 
1645. STATIC_OVL int
1646. mfind0(mtmp, via_warning)
1647. struct monst *mtmp;
1648. boolean via_warning;
1649. {
1650.     int x = mtmp->mx, y = mtmp->my;
1651.     boolean found_something = FALSE;
1652. 
1653.     if (via_warning && !warning_of(mtmp))
1654.         return -1;
1655. 
1656.     if (M_AP_TYPE(mtmp)) {
1657.         seemimic(mtmp);
1658.         found_something = TRUE;
1659.     } else {
1660.         /* this used to only be executed if a !canspotmon() test passed
1661.            but that failed to bring sensed monsters out of hiding */
1662.         found_something = !canspotmon(mtmp);
1663.         if (mtmp->mundetected && (is_hider(mtmp->data)
1664.                                   || hides_under(mtmp->data)
1665.                                   || mtmp->data->mlet == S_EEL)) {
1666.             if (via_warning) {
1667.                 Your("warning senses cause you to take a second %s.",
1668.                      Blind ? "to check nearby" : "look close by");
1669.                 display_nhwindow(WIN_MESSAGE, FALSE); /* flush messages */
1670.             }
1671.             mtmp->mundetected = 0;
1672.             found_something = TRUE;
1673.         }
1674.         newsym(x, y);
1675.     }
1676. 
1677.     if (found_something) {
1678.         if (!canspotmon(mtmp) && glyph_is_invisible(levl[x][y].glyph))
1679.             return -1; /* Found invisible monster in square which already has
1680.                         * 'I' in it.  Logically, this should still take time
1681.                         * and lead to `return 1', but if we did that the hero
1682.                         * would keep finding the same monster every turn. */
1683.         exercise(A_WIS, TRUE);
1684.         if (!canspotmon(mtmp)) {
1685.             map_invisible(x, y);
1686.             You_feel("an unseen monster!");
1687.         } else if (!sensemon(mtmp)) {
1688.             You("find %s.", mtmp->mtame ? y_monnam(mtmp) : a_monnam(mtmp));
1689.         }
1690.         return 1;
1691.     }
1692.     return 0;
1693. }
1694. 
1695. int
1696. dosearch0(aflag)
1697. register int aflag; /* intrinsic autosearch vs explicit searching */
1698. {
1699. #ifdef GCC_BUG
1700.     /* Some old versions of gcc seriously muck up nested loops.  If you get
1701.      * strange crashes while searching in a version compiled with gcc, try
1702.      * putting #define GCC_BUG in *conf.h (or adding -DGCC_BUG to CFLAGS in
1703.      * the makefile).
1704.      */
1705.     volatile xchar x, y;
1706. #else
1707.     register xchar x, y;
1708. #endif
1709.     register struct trap *trap;
1710.     register struct monst *mtmp;
1711. 
1712.     if (u.uswallow) {
1713.         if (!aflag)
1714.             pline("What are you looking for?  The exit?");
1715.     } else {
1716.         int fund = (uwep && uwep->oartifact
1717.                     && spec_ability(uwep, SPFX_SEARCH)) ? uwep->spe : 0;
1718. 
1719.         if (ublindf && ublindf->otyp == LENSES && !Blind)
1720.             fund += 2; /* JDS: lenses help searching */
1721.         if (fund > 5)
1722.             fund = 5;
1723.         for (x = u.ux - 1; x < u.ux + 2; x++)
1724.             for (y = u.uy - 1; y < u.uy + 2; y++) {
1725.                 if (!isok(x, y))
1726.                     continue;
1727.                 if (x == u.ux && y == u.uy)
1728.                     continue;
1729. 
1730.                 if (Blind && !aflag)
1731.                     feel_location(x, y);
1732.                 if (levl[x][y].typ == SDOOR) {
1733.                     if (rnl(7 - fund))
1734.                         continue;
1735.                     cvt_sdoor_to_door(&levl[x][y]); /* .typ = DOOR */
1736.                     exercise(A_WIS, TRUE);
1737.                     nomul(0);
1738.                     feel_location(x, y); /* make sure it shows up */
1739.                     You("find a hidden door.");
1740.                 } else if (levl[x][y].typ == SCORR) {
1741.                     if (rnl(7 - fund))
1742.                         continue;
1743.                     levl[x][y].typ = CORR;
1744.                     unblock_point(x, y); /* vision */
1745.                     exercise(A_WIS, TRUE);
1746.                     nomul(0);
1747.                     feel_newsym(x, y); /* make sure it shows up */
1748.                     You("find a hidden passage.");
1749.                 } else {
1750.                     /* Be careful not to find anything in an SCORR or SDOOR */
1751.                     if ((mtmp = m_at(x, y)) != 0 && !aflag) {
1752.                         int mfres = mfind0(mtmp, 0);
1753. 
1754.                         if (mfres == -1)
1755.                             continue;
1756.                         else if (mfres > 0)
1757.                             return mfres;
1758.                     }
1759. 
1760.                     /* see if an invisible monster has moved--if Blind,
1761.                      * feel_location() already did it
1762.                      */
1763.                     if (!aflag && !mtmp && !Blind)
1764.                         (void) unmap_invisible(x, y);
1765. 
1766.                     if ((trap = t_at(x, y)) && !trap->tseen && !rnl(8)) {
1767.                         nomul(0);
1768.                         if (trap->ttyp == STATUE_TRAP) {
1769.                             if (activate_statue_trap(trap, x, y, FALSE))
1770.                                 exercise(A_WIS, TRUE);
1771.                             return 1;
1772.                         } else {
1773.                             find_trap(trap);
1774.                         }
1775.                     }
1776.                 }
1777.             }
1778.     }
1779.     return 1;
1780. }
1781. 
1782. /* the 's' command -- explicit searching */
1783. int
1784. dosearch()
1785. {
1786.     return dosearch0(0);
1787. }
1788. 
1789. void
1790. warnreveal()
1791. {
1792.     int x, y;
1793.     struct monst *mtmp;
1794. 
1795.     for (x = u.ux - 1; x <= u.ux + 1; x++)
1796.         for (y = u.uy - 1; y <= u.uy + 1; y++) {
1797.             if (!isok(x, y) || (x == u.ux && y == u.uy))
1798.                 continue;
1799.             if ((mtmp = m_at(x, y)) != 0
1800.                 && warning_of(mtmp) && mtmp->mundetected)
1801.                 (void) mfind0(mtmp, 1); /* via_warning */
1802.         }
1803. }
1804. 
1805. /* Pre-map the sokoban levels */
1806. void
1807. sokoban_detect()
1808. {
1809.     register int x, y;
1810.     register struct trap *ttmp;
1811.     register struct obj *obj;
1812. 
1813.     /* Map the background and boulders */
1814.     for (x = 1; x < COLNO; x++)
1815.         for (y = 0; y < ROWNO; y++) {
1816.             levl[x][y].seenv = SVALL;
1817.             levl[x][y].waslit = TRUE;
1818.             map_background(x, y, 1);
1819.             if ((obj = sobj_at(BOULDER, x, y)) != 0)
1820.                 map_object(obj, 1);
1821.         }
1822. 
1823.     /* Map the traps */
1824.     for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
1825.         ttmp->tseen = 1;
1826.         map_trap(ttmp, 1);
1827.         /* set sokoban_rules when there is at least one pit or hole */
1828.         if (ttmp->ttyp == PIT || ttmp->ttyp == HOLE)
1829.             Sokoban = 1;
1830.     }
1831. }
1832. 
1833. STATIC_DCL int
1834. reveal_terrain_getglyph(x, y, full, swallowed, default_glyph, which_subset)
1835. int x, y, full;
1836. unsigned swallowed;
1837. int default_glyph, which_subset;
1838. {
1839.     int glyph, levl_glyph;
1840.     uchar seenv;
1841.     boolean keep_traps = (which_subset & TER_TRP) !=0,
1842.             keep_objs = (which_subset & TER_OBJ) != 0,
1843.             keep_mons = (which_subset & TER_MON) != 0;
1844.     struct monst *mtmp;
1845.     struct trap *t;
1846. 
1847.     /* for 'full', show the actual terrain for the entire level,
1848.        otherwise what the hero remembers for seen locations with
1849.        monsters, objects, and/or traps removed as caller dictates */
1850.     seenv = (full || level.flags.hero_memory)
1851.               ? levl[x][y].seenv : cansee(x, y) ? SVALL : 0;
1852.     if (full) {
1853.         levl[x][y].seenv = SVALL;
1854.         glyph = back_to_glyph(x, y);
1855.         levl[x][y].seenv = seenv;
1856.     } else {
1857.         levl_glyph = level.flags.hero_memory
1858.               ? levl[x][y].glyph
1859.               : seenv ? back_to_glyph(x, y): default_glyph;
1860.         /* glyph_at() returns the displayed glyph, which might
1861.            be a monster.  levl[][].glyph contains the remembered
1862.            glyph, which will never be a monster (unless it is
1863.            the invisible monster glyph, which is handled like
1864.            an object, replacing any object or trap at its spot) */
1865.         glyph = !swallowed ? glyph_at(x, y) : levl_glyph;
1866.         if (keep_mons && x == u.ux && y == u.uy && swallowed)
1867.             glyph = mon_to_glyph(u.ustuck, rn2_on_display_rng);
1868.         else if (((glyph_is_monster(glyph)
1869.                    || glyph_is_warning(glyph)) && !keep_mons)
1870.                  || glyph_is_swallow(glyph))
1871.             glyph = levl_glyph;
1872.         if (((glyph_is_object(glyph) && !keep_objs)
1873.              || glyph_is_invisible(glyph))
1874.             && keep_traps && !covers_traps(x, y)) {
1875.             if ((t = t_at(x, y)) != 0 && t->tseen)
1876.                 glyph = trap_to_glyph(t, rn2_on_display_rng);
1877.         }
1878.         if ((glyph_is_object(glyph) && !keep_objs)
1879.             || (glyph_is_trap(glyph) && !keep_traps)
1880.             || glyph_is_invisible(glyph)) {
1881.             if (!seenv) {
1882.                 glyph = default_glyph;
1883.             } else if (lastseentyp[x][y] == levl[x][y].typ) {
1884.                 glyph = back_to_glyph(x, y);
1885.             } else {
1886.                 /* look for a mimic here posing as furniture;
1887.                    if we don't find one, we'll have to fake it */
1888.                 if ((mtmp = m_at(x, y)) != 0
1889.                     && M_AP_TYPE(mtmp) == M_AP_FURNITURE) {
1890.                     glyph = cmap_to_glyph(mtmp->mappearance);
1891.                 } else {
1892.                     /* we have a topology type but we want a screen
1893.                        symbol in order to derive a glyph; some screen
1894.                        symbols need the flags field of levl[][] in
1895.                        addition to the type (to disambiguate STAIRS to
1896.                        S_upstair or S_dnstair, for example; current
1897.                        flags might not be intended for remembered type,
1898.                        but we've got no other choice) */
1899.                     schar save_typ = levl[x][y].typ;
1900. 
1901.                     levl[x][y].typ = lastseentyp[x][y];
1902.                     glyph = back_to_glyph(x, y);
1903.                     levl[x][y].typ = save_typ;
1904.                 }
1905.             }
1906.         }
1907.     }
1908.     if (glyph == cmap_to_glyph(S_darkroom))
1909.         glyph = cmap_to_glyph(S_room); /* FIXME: dirty hack */
1910.     return glyph;
1911. }
1912. 
1913. #ifdef DUMPLOG
1914. void
1915. dump_map()
1916. {
1917.     int x, y, glyph, skippedrows, lastnonblank;
1918.     int subset = TER_MAP | TER_TRP | TER_OBJ | TER_MON;
1919.     int default_glyph = cmap_to_glyph(level.flags.arboreal ? S_tree : S_stone);
1920.     char buf[BUFSZ];
1921.     boolean blankrow, toprow;
1922. 
1923.     /*
1924.      * Squeeze out excess vertial space when dumping the map.
1925.      * If there are any blank map rows at the top, suppress them
1926.      * (our caller has already printed a separator).  If there is
1927.      * more than one blank map row at the bottom, keep just one.
1928.      * Any blank rows within the middle of the map are kept.
1929.      * Note: putstr() with winid==0 is for dumplog.
1930.      */
1931.     skippedrows = 0;
1932.     toprow = TRUE;
1933.     for (y = 0; y < ROWNO; y++) {
1934.         blankrow = TRUE; /* assume blank until we discover otherwise */
1935.         lastnonblank = -1; /* buf[] index rather than map's x */
1936.         for (x = 1; x < COLNO; x++) {
1937.             int ch, color;
1938.             unsigned special;
1939. 
1940.             glyph = reveal_terrain_getglyph(x, y, FALSE, u.uswallow,
1941.                                             default_glyph, subset);
1942.             (void) mapglyph(glyph, &ch, &color, &special, x, y, 0);
1943.             buf[x - 1] = ch;
1944.             if (ch != ' ') {
1945.                 blankrow = FALSE;
1946.                 lastnonblank = x - 1;
1947.             }
1948.         }
1949.         if (!blankrow) {
1950.             buf[lastnonblank + 1] = '\0';
1951.             if (toprow) {
1952.                 skippedrows = 0;
1953.                 toprow = FALSE;
1954.             }
1955.             for (x = 0; x < skippedrows; x++)
1956.                 putstr(0, 0, "");
1957.             putstr(0, 0, buf); /* map row #y */
1958.             skippedrows = 0;
1959.         } else {
1960.             ++skippedrows;
1961.         }
1962.     }
1963.     if (skippedrows)
1964.         putstr(0, 0, "");
1965. }
1966. #endif /* DUMPLOG */
1967. 
1968. /* idea from crawl; show known portion of map without any monsters,
1969.    objects, or traps occluding the view of the underlying terrain */
1970. void
1971. reveal_terrain(full, which_subset)
1972. int full; /* wizard|explore modes allow player to request full map */
1973. int which_subset; /* when not full, whether to suppress objs and/or traps */
1974. {
1975.     if ((Hallucination || Stunned || Confusion) && !full) {
1976.         You("are too disoriented for this.");
1977.     } else {
1978.         int x, y, glyph, default_glyph;
1979.         char buf[BUFSZ];
1980.         /* there is a TER_MAP bit too; we always show map regardless of it */
1981.         boolean keep_traps = (which_subset & TER_TRP) !=0,
1982.                 keep_objs = (which_subset & TER_OBJ) != 0,
1983.                 keep_mons = (which_subset & TER_MON) != 0; /* not used */
1984.         unsigned swallowed = u.uswallow; /* before unconstrain_map() */
1985. 
1986.         if (unconstrain_map())
1987.             docrt();
1988.         default_glyph = cmap_to_glyph(level.flags.arboreal ? S_tree : S_stone);
1989. 
1990.         for (x = 1; x < COLNO; x++)
1991.             for (y = 0; y < ROWNO; y++) {
1992.                 glyph = reveal_terrain_getglyph(x,y, full, swallowed,
1993.                                                 default_glyph, which_subset);
1994.                 show_glyph(x, y, glyph);
1995.             }
1996. 
1997.         /* hero's location is not highlighted, but getpos() starts with
1998.            cursor there, and after moving it anywhere '@' moves it back */
1999.         flush_screen(1);
2000.         if (full) {
2001.             Strcpy(buf, "underlying terrain");
2002.         } else {
2003.             Strcpy(buf, "known terrain");
2004.             if (keep_traps)
2005.                 Sprintf(eos(buf), "%s traps",
2006.                         (keep_objs || keep_mons) ? "," : " and");
2007.             if (keep_objs)
2008.                 Sprintf(eos(buf), "%s%s objects",
2009.                         (keep_traps || keep_mons) ? "," : "",
2010.                         keep_mons ? "" : " and");
2011.             if (keep_mons)
2012.                 Sprintf(eos(buf), "%s and monsters",
2013.                         (keep_traps || keep_objs) ? "," : "");
2014.         }
2015.         pline("Showing %s only...", buf);
2016. 
2017.         /* allow player to move cursor around and get autodescribe feedback
2018.            based on what is visible now rather than what is on 'real' map */
2019.         which_subset |= TER_MAP; /* guarantee non-zero */
2020.         browse_map(which_subset, "anything of interest");
2021. 
2022.         reconstrain_map();
2023.         docrt(); /* redraw the screen, restoring regular map */
2024.         if (Underwater)
2025.             under_water(2);
2026.         if (u.uburied)
2027.             under_ground(2);
2028.     }
2029.     return;
2030. }
2031. 
2032. /*detect.c*/