Source:NetHack 3.3.0/detect.c

From NetHackWiki
(Redirected from NetHack 3.3.0/detect.c)
Jump to navigation Jump to search

Below is the full text to detect.c from the source code of NetHack 3.3.0. To link to a particular line, write [[NetHack 3.3.0/detect.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

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.    /*	SCCS Id: @(#)detect.c	3.3	1999/12/06	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    /*
6.     * Detection routines, including crystal ball, magic mapping, and search
7.     * command.
8.     */
9.    
10.   #include "hack.h"
11.   #include "artifact.h"
12.   
13.   extern boolean known;	/* from read.c */
14.   
15.   STATIC_DCL void FDECL(do_dknown_of, (struct obj *));
16.   STATIC_DCL boolean FDECL(check_map_spot, (int,int,CHAR_P));
17.   STATIC_DCL boolean FDECL(clear_stale_map, (CHAR_P));
18.   STATIC_DCL void FDECL(sense_trap, (struct trap *,XCHAR_P,XCHAR_P,int));
19.   STATIC_DCL void FDECL(show_map_spot, (int,int));
20.   STATIC_PTR void FDECL(findone,(int,int,genericptr_t));
21.   STATIC_PTR void FDECL(openone,(int,int,genericptr_t));
22.   
23.   /* Recursively search obj for an object in class oclass and return 1st found */
24.   struct obj *
25.   o_in(obj, oclass)
26.   struct obj* obj;
27.   char oclass;
28.   {
29.       register struct obj* otmp;
30.       struct obj *temp;
31.   
32.       if (obj->oclass == oclass) return obj;
33.   
34.       if (Has_contents(obj)) {
35.   	for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
36.   	    if (otmp->oclass == oclass) return otmp;
37.   	    else if (Has_contents(otmp) && (temp = o_in(otmp, oclass)))
38.   		return temp;
39.       }
40.       return (struct obj *) 0;
41.   }
42.   
43.   STATIC_OVL void
44.   do_dknown_of(obj)
45.   struct obj *obj;
46.   {
47.       struct obj *otmp;
48.   
49.       obj->dknown = 1;
50.       if (Has_contents(obj)) {
51.   	for(otmp = obj->cobj; otmp; otmp = otmp->nobj)
52.   	    do_dknown_of(otmp);
53.       }
54.   }
55.   
56.   /* Check whether the location has an outdated object displayed on it. */
57.   STATIC_OVL boolean
58.   check_map_spot(x, y, oclass)
59.   int x, y;
60.   register char oclass;
61.   {
62.   	register int glyph;
63.   	register struct obj *otmp;
64.   	register struct monst *mtmp;
65.   
66.   	glyph = glyph_at(x,y);
67.   	if (glyph_is_object(glyph)) {
68.   	    /* there's some object shown here */
69.   	    if (oclass == ALL_CLASSES) {
70.   		return((boolean)( !(level.objects[x][y] ||     /* stale if nothing here */
71.   			    ((mtmp = m_at(x,y)) != 0 &&
72.   				(mtmp->mgold || mtmp->minvent)))));
73.   	    } else if (objects[glyph_to_obj(glyph)].oc_class == oclass) {
74.   		/* the object shown here is of interest */
75.   		for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
76.   		    if (o_in(otmp, oclass)) return FALSE;
77.   		/* didn't find it; perhaps a monster is carrying it */
78.   		if ((mtmp = m_at(x,y)) != 0) {
79.   		    if (oclass == GOLD_CLASS && mtmp->mgold)
80.   			return FALSE;
81.   		    else for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
82.   			    if (o_in(otmp, oclass)) return FALSE;
83.   		}
84.   		/* detection indicates removal of this object from the map */
85.   		return TRUE;
86.   	    }
87.   	}
88.   	return FALSE;
89.   }
90.   
91.   /*
92.      When doing detection, remove stale data from the map display (corpses
93.      rotted away, objects carried away by monsters, etc) so that it won't
94.      reappear after the detection has completed.  Return true if noticeable
95.      change occurs.
96.    */
97.   STATIC_OVL boolean
98.   clear_stale_map(oclass)
99.   register char oclass;
100.  {
101.  	register int zx, zy;
102.  	register boolean change_made = FALSE;
103.  
104.  	for (zx = 1; zx < COLNO; zx++)
105.  	    for (zy = 0; zy < ROWNO; zy++)
106.  		if (check_map_spot(zx, zy, oclass)) {
107.  		    unmap_object(zx, zy);
108.  		    change_made = TRUE;
109.  		}
110.  
111.  	return change_made;
112.  }
113.  
114.  /* look for gold, on the floor or in monsters' possession */
115.  int
116.  gold_detect(sobj)
117.  register struct obj *sobj;
118.  {
119.      register struct obj *obj;
120.      register struct monst *mtmp;
121.      int uw = u.uinwater;
122.      struct obj *temp;
123.      boolean stale;
124.  
125.      known = stale = clear_stale_map(GOLD_CLASS);
126.  
127.      /* look for gold carried by monsters (might be in a container) */
128.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
129.  	if (mtmp->mgold) {
130.  	    known = TRUE;
131.  	    goto outgoldmap;	/* skip further searching */
132.  	} else for (obj = mtmp->minvent; obj; obj = obj->nobj)
133.  	    if (o_in(obj, GOLD_CLASS)) {
134.  		known = TRUE;
135.  		goto outgoldmap;	/* skip further searching */
136.  	    }
137.  
138.      /* look for gold objects */
139.      for (obj = fobj; obj; obj = obj->nobj)
140.  	if (o_in(obj, GOLD_CLASS)) {
141.  	    known = TRUE;
142.  	    if (obj->ox != u.ux || obj->oy != u.uy) goto outgoldmap;
143.  	}
144.  
145.      if (!known) {
146.  	/* no gold found */
147.  	if (sobj) strange_feeling(sobj, "You feel materially poor.");
148.  	return(1);
149.      }
150.      /* only under me - no separate display required */
151.      if (stale) docrt();
152.      You("notice some gold between your %s.", makeplural(body_part(FOOT)));
153.      return(0);
154.  
155.  outgoldmap:
156.      cls();
157.  
158.      u.uinwater = 0;
159.      /* Discover gold locations. */
160.      for (obj = fobj; obj; obj = obj->nobj)
161.  	if ((temp = o_in(obj, GOLD_CLASS))) {
162.  	    if (temp != obj) {
163.  		temp->ox = obj->ox;
164.  		temp->oy = obj->oy;
165.  	    }
166.  	    map_object(temp,1);
167.  	}
168.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
169.  	if (mtmp->mgold) {
170.  	    struct obj gold;
171.  
172.  	    gold.otyp = GOLD_PIECE;
173.  	    gold.ox = mtmp->mx;
174.  	    gold.oy = mtmp->my;
175.  	    map_object(&gold,1);
176.  	} else for (obj = mtmp->minvent; obj; obj = obj->nobj)
177.  	    if ((temp = o_in(obj, GOLD_CLASS))) {
178.  		temp->ox = mtmp->mx;
179.  		temp->oy = mtmp->my;
180.  		map_object(temp,1);
181.  		break;
182.  	    }
183.  
184.      newsym(u.ux,u.uy);
185.      You_feel("very greedy, and sense gold!");
186.      exercise(A_WIS, TRUE);
187.      display_nhwindow(WIN_MAP, TRUE);
188.      docrt();
189.      u.uinwater = uw;
190.      if (Underwater) under_water(2);
191.      if (u.uburied) under_ground(2);
192.      return(0);
193.  }
194.  
195.  /* returns 1 if nothing was detected		*/
196.  /* returns 0 if something was detected		*/
197.  int
198.  food_detect(sobj)
199.  register struct obj	*sobj;
200.  {
201.      register struct obj *obj;
202.      register struct monst *mtmp;
203.      register int ct = 0, ctu = 0;
204.      boolean confused = (Confusion || (sobj && sobj->cursed)), stale;
205.      char oclass = confused ? POTION_CLASS : FOOD_CLASS;
206.      const char *what = confused ? something : "food";
207.      int uw = u.uinwater;
208.  
209.      stale = clear_stale_map(oclass);
210.  
211.      for (obj = fobj; obj; obj = obj->nobj)
212.  	if (o_in(obj, oclass)) {
213.  	    if (obj->ox == u.ux && obj->oy == u.uy) ctu++;
214.  	    else ct++;
215.  	}
216.      for (mtmp = fmon; mtmp && !ct; mtmp = mtmp->nmon)
217.  	for (obj = mtmp->minvent; obj; obj = obj->nobj)
218.  	    if (o_in(obj, oclass)) {
219.  		ct++;
220.  		break;
221.  	    }
222.  
223.      if (!ct && !ctu) {
224.  	known = stale && !confused;
225.  	if (stale) {
226.  	    docrt();
227.  	    You("sense a lack of %s nearby.", what);
228.  	} else if (sobj)
229.  	    strange_feeling(sobj, "Your nose twitches.");
230.  	return !stale;
231.      } else if (!ct) {
232.  	known = TRUE;
233.  	You("%s %s nearby.", sobj ? "smell" : "sense", what);
234.      } else {
235.  	struct obj *temp;
236.  	known = TRUE;
237.  	cls();
238.  	u.uinwater = 0;
239.  	for (obj = fobj; obj; obj = obj->nobj)
240.  	    if ((temp = o_in(obj, oclass)) != 0) {
241.  		if (temp != obj) {
242.  		    temp->ox = obj->ox;
243.  		    temp->oy = obj->oy;
244.  		}
245.  		map_object(temp,1);
246.  	    }
247.  	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
248.  	    for (obj = mtmp->minvent; obj; obj = obj->nobj)
249.  		if ((temp = o_in(obj, oclass)) != 0) {
250.  		    temp->ox = mtmp->mx;
251.  		    temp->oy = mtmp->my;
252.  		    map_object(temp,1);
253.  		    break;	/* skip rest of this monster's inventory */
254.  		}
255.  	newsym(u.ux,u.uy);
256.  	if (sobj) Your("nose tingles and you smell %s.", what);
257.  	else You("sense %s.", what);
258.  	display_nhwindow(WIN_MAP, TRUE);
259.  	exercise(A_WIS, TRUE);
260.  	docrt();
261.  	u.uinwater = uw;
262.  	if (Underwater) under_water(2);
263.  	if (u.uburied) under_ground(2);
264.      }
265.      return(0);
266.  }
267.  
268.  /*
269.   * Used for scrolls, potions, and crystal balls.  Returns:
270.   *
271.   *	1 - nothing was detected
272.   *	0 - something was detected
273.   */
274.  int
275.  object_detect(detector, class)
276.  struct obj	*detector;	/* object doing the detecting */
277.  int		class;		/* an object class, 0 for all */
278.  {
279.      register int x, y;
280.      int is_cursed = (detector && detector->cursed);
281.      int do_dknown =
282.  	(detector && detector->oclass == POTION_CLASS && detector->blessed);
283.      int ct = 0, ctu = 0;
284.      register struct obj *obj, *otmp = (struct obj *)0;
285.      register struct monst *mtmp;
286.      int uw = u.uinwater;
287.      const char *stuff;
288.  
289.      if (class < 0 || class >= MAXOCLASSES) {
290.  	impossible("object_detect:  illegal class %d", class);
291.  	class = 0;
292.      }
293.  
294.      if (Hallucination || (Confusion && class == SCROLL_CLASS))
295.  	stuff = something;
296.      else
297.  	stuff = class ? oclass_names[class] : "objects";
298.  
299.      if (do_dknown) for(obj = invent; obj; obj = obj->nobj) do_dknown_of(obj);
300.  
301.      for (obj = fobj; obj; obj = obj->nobj) {
302.  	if (!class || o_in(obj, class)) {
303.  	    if (obj->ox == u.ux && obj->oy == u.uy) ctu++;
304.  	    else ct++;
305.  	}
306.  	if (do_dknown) do_dknown_of(obj);
307.      }
308.  
309.      for (obj = level.buriedobjlist; obj; obj = obj->nobj) {
310.  	if (!class || o_in(obj, class)) {
311.  	    if (obj->ox == u.ux && obj->oy == u.uy) ctu++;
312.  	    else ct++;
313.  	}
314.  	if (do_dknown) do_dknown_of(obj);
315.      }
316.  
317.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
318.  	for (obj = mtmp->minvent; obj; obj = obj->nobj) {
319.  	    if (!class || o_in(obj, class)) ct++;
320.  	    if (do_dknown) do_dknown_of(obj);
321.  	}
322.  
323.  	if ((is_cursed && mtmp->m_ap_type == M_AP_OBJECT &&
324.  	    (!class || class == objects[mtmp->mappearance].oc_class)) ||
325.  	    (mtmp->mgold && (!class || class == GOLD_CLASS))) {
326.  	    ct++;
327.  	    break;
328.  	}
329.      }
330.  
331.      if (!clear_stale_map(!class ? ALL_CLASSES : class) && !ct) {
332.  	if (!ctu) {
333.  	    if (detector)
334.  		strange_feeling(detector, "You feel a lack of something.");
335.  	    return 1;
336.  	}
337.  
338.  	You("sense %s nearby.", stuff);
339.  	return 0;
340.      }
341.  
342.      cls();
343.  
344.      u.uinwater = 0;
345.  /*
346.   *	Map all buried objects first.
347.   */
348.      for (obj = level.buriedobjlist; obj; obj = obj->nobj)
349.  	if (!class || (otmp = o_in(obj, class))) {
350.  	    if (class) {
351.  		if (otmp != obj) {
352.  		    otmp->ox = obj->ox;
353.  		    otmp->oy = obj->oy;
354.  		}
355.  		map_object(otmp, 1);
356.  	    } else
357.  		map_object(obj, 1);
358.  	}
359.      /*
360.       * If we are mapping all objects, map only the top object of a pile or
361.       * the first object in a monster's inventory.  Otherwise, go looking
362.       * for a matching object class and display the first one encountered
363.       * at each location.
364.       *
365.       * Objects on the floor override buried objects.
366.       */
367.      for (x = 1; x < COLNO; x++)
368.  	for (y = 0; y < ROWNO; y++)
369.  	    for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
370.  		if (!class || (otmp = o_in(obj, class))) {
371.  		    if (class) {
372.  			if (otmp != obj) {
373.  			    otmp->ox = obj->ox;
374.  			    otmp->oy = obj->oy;
375.  			}
376.  			map_object(otmp, 1);
377.  		    } else
378.  			map_object(obj, 1);
379.  		    break;
380.  		}
381.  
382.      /* Objects in the monster's inventory override floor objects. */
383.      for (mtmp = fmon ; mtmp ; mtmp = mtmp->nmon) {
384.  	for (obj = mtmp->minvent; obj; obj = obj->nobj)
385.  	    if (!class || (otmp = o_in(obj, class))) {
386.  		if (!class) otmp = obj;
387.  		otmp->ox = mtmp->mx;		/* at monster location */
388.  		otmp->oy = mtmp->my;
389.  		map_object(otmp, 1);
390.  		break;
391.  	    }
392.  
393.  
394.  	/* Allow a mimic to override the detected objects it is carrying. */
395.  	if (is_cursed && mtmp->m_ap_type == M_AP_OBJECT &&
396.  		(!class || class == objects[mtmp->mappearance].oc_class)) {
397.  	    struct obj temp;
398.  
399.  	    temp.otyp = mtmp->mappearance;	/* needed for obj_to_glyph() */
400.  	    temp.ox = mtmp->mx;
401.  	    temp.oy = mtmp->my;
402.  	    temp.corpsenm = PM_TENGU;		/* if mimicing a corpse */
403.  	    map_object(&temp, 1);
404.  	} else if (mtmp->mgold && (!class || class == GOLD_CLASS)) {
405.  	    struct obj gold;
406.  
407.  	    gold.otyp = GOLD_PIECE;
408.  	    gold.ox = mtmp->mx;
409.  	    gold.oy = mtmp->my;
410.  	    map_object(&gold, 1);
411.  	}
412.      }
413.  
414.      newsym(u.ux,u.uy);
415.      You("detect the %s of %s.", ct ? "presence" : "absence", stuff);
416.      display_nhwindow(WIN_MAP, TRUE);
417.      /*
418.       * What are we going to do when the hero does an object detect while blind
419.       * and the detected object covers a known pool?
420.       */
421.      docrt();	/* this will correctly reset vision */
422.  
423.      u.uinwater = uw;
424.      if (Underwater) under_water(2);
425.      if (u.uburied) under_ground(2);
426.      return 0;
427.  }
428.  
429.  /*
430.   * Used by: crystal balls, potions, fountains
431.   *
432.   * Returns 1 if nothing was detected.
433.   * Returns 0 if something was detected.
434.   */
435.  int
436.  monster_detect(otmp, mclass)
437.  register struct obj *otmp;	/* detecting object (if any) */
438.  int mclass;			/* monster class, 0 for all */
439.  {
440.      register struct monst *mtmp;
441.  
442.      if (!fmon) {
443.  	if (otmp)
444.  	    strange_feeling(otmp, Hallucination ?
445.  			    "You get the heebie jeebies." :
446.  			    "You feel threatened.");
447.  	return 1;
448.      } else {
449.  	boolean woken = FALSE;
450.  
451.  	cls();
452.  	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
453.  	    if (!mclass || mtmp->data->mlet == mclass)
454.  	    if (mtmp->mx > 0)
455.  		show_glyph(mtmp->mx,mtmp->my,mon_to_glyph(mtmp));
456.  	    if (otmp && otmp->cursed &&
457.  		(mtmp->msleeping || !mtmp->mcanmove)) {
458.  		mtmp->msleeping = mtmp->mfrozen = 0;
459.  		mtmp->mcanmove = 1;
460.  		woken = TRUE;
461.  	    }
462.  	}
463.  	display_self();
464.  	You("sense the presence of monsters.");
465.  	if (woken)
466.  	    pline("Monsters sense the presence of you.");
467.  	display_nhwindow(WIN_MAP, TRUE);
468.  	docrt();
469.  	if (Underwater) under_water(2);
470.  	if (u.uburied) under_ground(2);
471.      }
472.      return 0;
473.  }
474.  
475.  STATIC_OVL void
476.  sense_trap(trap, x, y, src_cursed)
477.  struct trap *trap;
478.  xchar x, y;
479.  int src_cursed;
480.  {
481.      if (Hallucination || src_cursed) {
482.  	struct obj obj;			/* fake object */
483.  	if (trap) {
484.  	    obj.ox = trap->tx;
485.  	    obj.oy = trap->ty;
486.  	} else {
487.  	    obj.ox = x;
488.  	    obj.oy = y;
489.  	}
490.  	obj.otyp = (src_cursed) ? GOLD_PIECE : random_object();
491.  	obj.corpsenm = random_monster();	/* if otyp == CORPSE */
492.  	map_object(&obj,1);
493.      } else if (trap) {
494.  	map_trap(trap,1);
495.  	trap->tseen = 1;
496.      } else {
497.  	struct trap temp_trap;		/* fake trap */
498.  	temp_trap.tx = x;
499.  	temp_trap.ty = y;
500.  	temp_trap.ttyp = BEAR_TRAP;	/* some kind of trap */
501.  	map_trap(&temp_trap,1);
502.      }
503.  
504.  }
505.  
506.  /* the detections are pulled out so they can	*/
507.  /* also be used in the crystal ball routine	*/
508.  /* returns 1 if nothing was detected		*/
509.  /* returns 0 if something was detected		*/
510.  int
511.  trap_detect(sobj)
512.  register struct obj *sobj;
513.  /* sobj is null if crystal ball, *scroll if gold detection scroll */
514.  {
515.      register struct trap *ttmp;
516.      register struct obj *obj;
517.      register int door;
518.      int uw = u.uinwater;
519.      boolean found = FALSE;
520.      coord cc;
521.  
522.      for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
523.  	if (ttmp->tx != u.ux || ttmp->ty != u.uy)
524.  	    goto outtrapmap;
525.  	else found = TRUE;
526.      }
527.      for (obj = fobj; obj; obj = obj->nobj) {
528.  	if ((obj->otyp==LARGE_BOX || obj->otyp==CHEST) && obj->otrapped)
529.  	    if (obj->ox != u.ux || obj->oy != u.uy)
530.  		goto outtrapmap;
531.  	    else found = TRUE;
532.      }
533.      for (door = 0; door <= doorindex; door++) {
534.  	cc = doors[door];
535.  	if (levl[cc.x][cc.y].doormask & D_TRAPPED)
536.  	    if (cc.x != u.ux || cc.x != u.uy)
537.  		goto outtrapmap;
538.  	    else found = TRUE;
539.      }
540.      if (!found) {
541.  	char buf[42];
542.  	Sprintf(buf, "Your %s stop itching.", makeplural(body_part(TOE)));
543.  	strange_feeling(sobj,buf);
544.  	return(1);
545.      }
546.      /* traps exist, but only under me - no separate display required */
547.      Your("%s itch.", makeplural(body_part(TOE)));
548.      return(0);
549.  outtrapmap:
550.      cls();
551.  
552.      u.uinwater = 0;
553.      for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
554.  	sense_trap(ttmp, 0, 0, sobj && sobj->cursed);
555.  
556.      for (obj = fobj; obj; obj = obj->nobj)
557.  	if ((obj->otyp==LARGE_BOX || obj->otyp==CHEST) && obj->otrapped)
558.  	sense_trap((struct trap *)0, obj->ox, obj->oy, sobj && sobj->cursed);
559.  
560.      for (door = 0; door <= doorindex; door++) {
561.  	cc = doors[door];
562.  	if (levl[cc.x][cc.y].doormask & D_TRAPPED)
563.  	sense_trap((struct trap *)0, cc.x, cc.y, sobj && sobj->cursed);
564.      }
565.  
566.      newsym(u.ux,u.uy);
567.      You_feel("%s.", sobj && sobj->cursed ? "very greedy" : "entrapped");
568.      display_nhwindow(WIN_MAP, TRUE);
569.      docrt();
570.      u.uinwater = uw;
571.      if (Underwater) under_water(2);
572.      if (u.uburied) under_ground(2);
573.      return(0);
574.  }
575.  
576.  const char *
577.  level_distance(where)
578.  d_level *where;
579.  {
580.      register schar ll = depth(&u.uz) - depth(where);
581.      register boolean indun = (u.uz.dnum == where->dnum);
582.  
583.      if (ll < 0) {
584.  	if (ll < (-8 - rn2(3)))
585.  	    if (!indun)	return "far away";
586.  	    else	return "far below";
587.  	else if (ll < -1)
588.  	    if (!indun)	return "away below you";
589.  	    else	return "below you";
590.  	else
591.  	    if (!indun)	return "in the distance";
592.  	    else	return "just below";
593.      } else if (ll > 0) {
594.  	if (ll > (8 + rn2(3)))
595.  	    if (!indun)	return "far away";
596.  	    else	return "far above";
597.  	else if (ll > 1)
598.  	    if (!indun)	return "away above you";
599.  	    else	return "above you";
600.  	else
601.  	    if (!indun)	return "in the distance";
602.  	    else	return "just above";
603.      } else
604.  	    if (!indun)	return "in the distance";
605.  	    else	return "near you";
606.  }
607.  
608.  static struct {
609.      const char *what;
610.      d_level *where;
611.  } level_detects[] = {
612.    { "Delphi", &oracle_level },
613.    { "Medusa's lair", &medusa_level },
614.    { "a castle", &stronghold_level },
615.    { "the Wizard of Yendor's tower", &wiz1_level },
616.  };
617.  
618.  void
619.  use_crystal_ball(obj)
620.  struct obj *obj;
621.  {
622.      char ch;
623.      int oops;
624.      const char *bname = xname(obj);
625.  
626.      if (Blind) {
627.  	pline("Too bad you can't see %s", the(bname));
628.  	return;
629.      }
630.      oops = (rnd(20) > ACURR(A_INT) || obj->cursed);
631.      if (oops && (obj->spe > 0)) {
632.  	switch (rnd(obj->oartifact ? 4 : 5)) {
633.  	case 1 : pline("%s is too much to comprehend!", The(bname));
634.  	    break;
635.  	case 2 : pline("%s confuses you!", The(bname));
636.  	    make_confused(HConfusion + rnd(100),FALSE);
637.  	    break;
638.  	case 3 : if (!resists_blnd(&youmonst)) {
639.  		pline("%s damages your vision!", The(bname));
640.  		make_blinded(Blinded + rnd(100),FALSE);
641.  	    } else {
642.  		pline("%s assaults your vision.", The(bname));
643.  		You("are unaffected!");
644.  	    }
645.  	    break;
646.  	case 4 : pline("%s zaps your mind!", The(bname));
647.  	    make_hallucinated(HHallucination + rnd(100),FALSE,0L);
648.  	    break;
649.  	case 5 : pline("%s explodes!", The(bname));
650.  	    useup(obj);
651.  	    losehp(rnd(30), "exploding crystal ball", KILLED_BY_AN);
652.  	    break;
653.  	}
654.  	check_unpaid(obj);
655.  	obj->spe--;
656.  	return;
657.      }
658.  
659.      if (Hallucination) {
660.  	if (!obj->spe) {
661.  	    pline("All you see is funky %s haze.", hcolor((char *)0));
662.  	} else {
663.  	    switch(rnd(6)) {
664.  	    case 1 : You("grok some groovy globs of incandescent lava.");
665.  		break;
666.  	    case 2 : pline("Whoa!  Psychedelic colors, %s!",
667.  			   poly_gender() == 1 ? "babe" : "dude");
668.  		break;
669.  	    case 3 : pline_The("crystal pulses with sinister %s light!",
670.  				hcolor((char *)0));
671.  		break;
672.  	    case 4 : You("see goldfish swimming above fluorescent rocks.");
673.  		break;
674.  	    case 5 : You("see tiny snowflakes spinning around a miniature farmhouse.");
675.  		break;
676.  	    default: pline("Oh wow... like a kaleidoscope!");
677.  		break;
678.  	    }
679.  	    check_unpaid(obj);
680.  	    obj->spe--;
681.  	}
682.  	return;
683.      }
684.  
685.      /* read a single character */
686.      if (flags.verbose) You("may look for an object or monster symbol.");
687.      ch = yn_function("What do you look for?", (char *)0, '\0');
688.      if (index(quitchars,ch)) {
689.  	if (flags.verbose) pline("Never mind.");
690.  	return;
691.      }
692.      You("peer into %s...", the(bname));
693.      nomul(-rnd(10));
694.      nomovemsg = "";
695.      if (obj->spe <= 0)
696.  	pline_The("vision is unclear.");
697.      else {
698.  	int class;
699.  	int ret = 0;
700.  
701.  	makeknown(CRYSTAL_BALL);
702.  	check_unpaid(obj);
703.  	obj->spe--;
704.  
705.  	if ((class = def_char_to_objclass(ch)) != MAXOCLASSES)
706.  		ret = object_detect((struct obj *)0, class);
707.  	else if ((class = def_char_to_monclass(ch)) != MAXMCLASSES)
708.  		ret = monster_detect((struct obj *)0, class);
709.  	else switch(ch) {
710.  		case '^':
711.  		    ret = trap_detect((struct obj *)0);
712.  		    break;
713.  		default:
714.  		    {
715.  		    int i = rn2(SIZE(level_detects));
716.  		    You("see %s, %s.",
717.  			level_detects[i].what,
718.  			level_distance(level_detects[i].where));
719.  		    }
720.  		    ret = 0;
721.  		    break;
722.  	}
723.  
724.  	if (ret) {
725.  	    if (!rn2(100))  /* make them nervous */
726.  		You("see the Wizard of Yendor gazing out at you.");
727.  	    else pline_The("vision is unclear.");
728.  	}
729.      }
730.      return;
731.  }
732.  
733.  STATIC_OVL void
734.  show_map_spot(x, y)
735.  register int x, y;
736.  {
737.      register struct rm *lev;
738.  
739.      if (Confusion && rn2(7)) return;
740.      lev = &levl[x][y];
741.  
742.      lev->seenv = SVALL;
743.  
744.      /* Secret corridors are found, but not secret doors. */
745.      if (lev->typ == SCORR) {
746.  	lev->typ = CORR;
747.  	unblock_point(x,y);
748.      }
749.  
750.      /* if we don't remember an object or trap there, map it */
751.      if (lev->typ == ROOM ?
752.  	    (glyph_is_cmap(lev->glyph) && !glyph_is_trap(lev->glyph) &&
753.  		glyph_to_cmap(lev->glyph) != ROOM) :
754.  	    (!glyph_is_object(lev->glyph) && !glyph_is_trap(lev->glyph))) {
755.  	if (level.flags.hero_memory) {
756.  	    map_background(x,y,0);
757.  	    newsym(x,y);		/* show it, if not blocked */
758.  	} else {
759.  	    map_background(x,y,1);	/* display it */
760.  	}
761.      }
762.  }
763.  
764.  void
765.  do_mapping()
766.  {
767.      register int zx, zy;
768.      int uw = u.uinwater;
769.  
770.      u.uinwater = 0;
771.      for (zx = 1; zx < COLNO; zx++)
772.  	for (zy = 0; zy < ROWNO; zy++)
773.  	    show_map_spot(zx, zy);
774.      exercise(A_WIS, TRUE);
775.      u.uinwater = uw;
776.      if (!level.flags.hero_memory || Underwater) {
777.  	flush_screen(1);			/* flush temp screen */
778.  	display_nhwindow(WIN_MAP, TRUE);	/* wait */
779.  	docrt();
780.      }
781.  }
782.  
783.  void
784.  do_vicinity_map()
785.  {
786.      register int zx, zy;
787.      int lo_y = (u.uy-5 < 0 ? 0 : u.uy-5),
788.  	hi_y = (u.uy+6 > ROWNO ? ROWNO : u.uy+6),
789.  	lo_x = (u.ux-9 < 1 ? 1 : u.ux-9),	/* avoid column 0 */
790.  	hi_x = (u.ux+10 > COLNO ? COLNO : u.ux+10);
791.  
792.      for (zx = lo_x; zx < hi_x; zx++)
793.  	for (zy = lo_y; zy < hi_y; zy++)
794.  	    show_map_spot(zx, zy);
795.  
796.      if (!level.flags.hero_memory || Underwater) {
797.  	flush_screen(1);			/* flush temp screen */
798.  	display_nhwindow(WIN_MAP, TRUE);	/* wait */
799.  	docrt();
800.      }
801.  }
802.  
803.  /* convert a secret door into a normal door */
804.  void
805.  cvt_sdoor_to_door(lev)
806.  struct rm *lev;
807.  {
808.  	int newmask = lev->doormask & ~WM_MASK;
809.  
810.  #ifdef REINCARNATION
811.  	if (Is_rogue_level(&u.uz))
812.  	    /* rogue didn't have doors, only doorways */
813.  	    newmask = D_NODOOR;
814.  	else
815.  #endif
816.  	    /* newly exposed door is closed */
817.  	    if (!(newmask & D_LOCKED)) newmask |= D_CLOSED;
818.  
819.  	lev->typ = DOOR;
820.  	lev->doormask = newmask;
821.  }
822.  
823.  
824.  STATIC_PTR void
825.  findone(zx,zy,num)
826.  int zx,zy;
827.  genericptr_t num;
828.  {
829.  	register struct trap *ttmp;
830.  	register struct monst *mtmp;
831.  
832.  	if(levl[zx][zy].typ == SDOOR) {
833.  		cvt_sdoor_to_door(&levl[zx][zy]);	/* .typ = DOOR */
834.  		newsym(zx, zy);
835.  		(*(int*)num)++;
836.  	} else if(levl[zx][zy].typ == SCORR) {
837.  		levl[zx][zy].typ = CORR;
838.  		newsym(zx, zy);
839.  		(*(int*)num)++;
840.  	} else if ((ttmp = t_at(zx, zy)) != 0) {
841.  		if(!ttmp->tseen && ttmp->ttyp != STATUE_TRAP) {
842.  			ttmp->tseen = 1;
843.  			newsym(zx,zy);
844.  			(*(int*)num)++;
845.  		}
846.  	} else if ((mtmp = m_at(zx, zy)) != 0) {
847.  		if(mtmp->m_ap_type) {
848.  			seemimic(mtmp);
849.  			(*(int*)num)++;
850.  		}
851.  		if (mtmp->mundetected &&
852.  		    (is_hider(mtmp->data) || mtmp->data->mlet == S_EEL)) {
853.  			mtmp->mundetected = 0;
854.  			newsym(zx, zy);
855.  			(*(int*)num)++;
856.  		}
857.  	}
858.  }
859.  
860.  STATIC_PTR void
861.  openone(zx,zy,num)
862.  int zx,zy;
863.  genericptr_t num;
864.  {
865.  	register struct trap *ttmp;
866.  	register struct obj *otmp;
867.  
868.  	if(OBJ_AT(zx, zy)) {
869.  		for(otmp = level.objects[zx][zy];
870.  				otmp; otmp = otmp->nexthere) {
871.  		    if(Is_box(otmp) && otmp->olocked) {
872.  			otmp->olocked = 0;
873.  			(*(int*)num)++;
874.  		    }
875.  		}
876.  		/* let it fall to the next cases. could be on trap. */
877.  	}
878.  	if(levl[zx][zy].typ == SDOOR || (levl[zx][zy].typ == DOOR &&
879.  		      (levl[zx][zy].doormask & (D_CLOSED|D_LOCKED)))) {
880.  		if(levl[zx][zy].typ == SDOOR)
881.  		    cvt_sdoor_to_door(&levl[zx][zy]);	/* .typ = DOOR */
882.  		if(levl[zx][zy].doormask & D_TRAPPED) {
883.  		    if(distu(zx, zy) < 3) b_trapped("door", 0);
884.  		    else Norep("You %s an explosion!",
885.  				cansee(zx, zy) ? "see" :
886.  				   (flags.soundok ? "hear" :
887.  						"feel the shock of"));
888.  		    wake_nearto(zx, zy, 11*11);
889.  		    levl[zx][zy].doormask = D_NODOOR;
890.  		} else
891.  		    levl[zx][zy].doormask = D_ISOPEN;
892.  		newsym(zx, zy);
893.  		(*(int*)num)++;
894.  	} else if(levl[zx][zy].typ == SCORR) {
895.  		levl[zx][zy].typ = CORR;
896.  		newsym(zx, zy);
897.  		(*(int*)num)++;
898.  	} else if ((ttmp = t_at(zx, zy)) != 0) {
899.  		if (!ttmp->tseen && ttmp->ttyp != STATUE_TRAP) {
900.  		    ttmp->tseen = 1;
901.  		    newsym(zx,zy);
902.  		    (*(int*)num)++;
903.  		}
904.  	} else if (find_drawbridge(&zx, &zy)) {
905.  		/* make sure it isn't an open drawbridge */
906.  		open_drawbridge(zx, zy);
907.  		(*(int*)num)++;
908.  	}
909.  }
910.  
911.  int
912.  findit()	/* returns number of things found */
913.  {
914.  	int num = 0;
915.  
916.  	if(u.uswallow) return(0);
917.  	do_clear_area(u.ux, u.uy, BOLT_LIM, findone, (genericptr_t) &num);
918.  	return(num);
919.  }
920.  
921.  int
922.  openit()	/* returns number of things found and opened */
923.  {
924.  	int num = 0;
925.  
926.  	if(u.uswallow) {
927.  		if (is_animal(u.ustuck->data)) {
928.  			if (Blind) pline("Its mouth opens!");
929.  			else pline("%s opens its mouth!", Monnam(u.ustuck));
930.  		}
931.  		expels(u.ustuck, u.ustuck->data, TRUE);
932.  		return(-1);
933.  	}
934.  
935.  	do_clear_area(u.ux, u.uy, BOLT_LIM, openone, (genericptr_t) &num);
936.  	return(num);
937.  }
938.  
939.  void
940.  find_trap(trap)
941.  struct trap *trap;
942.  {
943.      int tt = what_trap(trap->ttyp);
944.  
945.      You("find %s.", an(defsyms[trap_to_defsym(tt)].explanation));
946.      trap->tseen = 1;
947.      exercise(A_WIS, TRUE);
948.      if (Blind)
949.  	feel_location(trap->tx, trap->ty);
950.      else
951.  	newsym(trap->tx, trap->ty);
952.  }
953.  
954.  int
955.  dosearch0(aflag)
956.  register int aflag;
957.  {
958.  #ifdef GCC_BUG
959.  /* some versions of gcc seriously muck up nested loops. if you get strange
960.     crashes while searching in a version compiled with gcc, try putting
961.     #define GCC_BUG in *conf.h (or adding -DGCC_BUG to CFLAGS in the
962.     makefile).
963.   */
964.  	volatile xchar x, y;
965.  #else
966.  	register xchar x, y;
967.  #endif
968.  	register struct trap *trap;
969.  	register struct monst *mtmp;
970.  
971.  	if(u.uswallow) {
972.  		if (!aflag)
973.  			pline("What are you looking for?  The exit?");
974.  	} else {
975.  	    int fund = (uwep && uwep->oartifact &&
976.  		    spec_ability(uwep, SPFX_SEARCH)) ?
977.  			((uwep->spe > 5) ? 5 : uwep->spe) : 0;
978.  	    for(x = u.ux-1; x < u.ux+2; x++)
979.  	      for(y = u.uy-1; y < u.uy+2; y++) {
980.  		if(!isok(x,y)) continue;
981.  		if(x != u.ux || y != u.uy) {
982.  		    if (Blind && !aflag) feel_location(x,y);
983.  		    if(levl[x][y].typ == SDOOR) {
984.  			if(rnl(7-fund)) continue;
985.  			cvt_sdoor_to_door(&levl[x][y]);	/* .typ = DOOR */
986.  			exercise(A_WIS, TRUE);
987.  			nomul(0);
988.  			if (Blind && !aflag)
989.  			    feel_location(x,y);	/* make sure it shows up */
990.  			else
991.  			    newsym(x,y);
992.  		    } else if(levl[x][y].typ == SCORR) {
993.  			if(rnl(7-fund)) continue;
994.  			levl[x][y].typ = CORR;
995.  			unblock_point(x,y);	/* vision */
996.  			exercise(A_WIS, TRUE);
997.  			nomul(0);
998.  			newsym(x,y);
999.  		    } else {
1000. 		/* Be careful not to find anything in an SCORR or SDOOR */
1001. 			if((mtmp = m_at(x, y)) && !aflag) {
1002. 			    if(mtmp->m_ap_type) {
1003. 				seemimic(mtmp);
1004. 		find:		exercise(A_WIS, TRUE);
1005. 				if (!canspotmon(mtmp)) {
1006. 				    if (glyph_is_invisible(levl[x][y].glyph)) {
1007. 					/* found invisible monster in a square
1008. 					 * which already has an 'I' in it.
1009. 					 * Logically, this should still take
1010. 					 * time and lead to a return(1), but if
1011. 					 * we did that the player would keep
1012. 					 * finding the same monster every turn.
1013. 					 */
1014. 					continue;
1015. 				    } else {
1016. 					You_feel("an unseen monster!");
1017. 					map_invisible(x, y);
1018. 				    }
1019. 				} else
1020. 				    You("find %s.", a_monnam(mtmp));
1021. 				return(1);
1022. 			    }
1023. 			    if(mtmp->mundetected &&
1024. 			(is_hider(mtmp->data) || mtmp->data->mlet == S_EEL)) {
1025. 				mtmp->mundetected = 0;
1026. 				newsym(x,y);
1027. 				goto find;
1028. 			    }
1029. 			    if (!canspotmon(mtmp))
1030. 				goto find;
1031. 			}
1032. 
1033. 			/* see if an invisible monster has moved--if Blind,
1034. 			 * feel_location() already did it
1035. 			 */
1036. 			if (!aflag && !mtmp && !Blind &&
1037. 				    glyph_is_invisible(levl[x][y].glyph)) {
1038. 			    unmap_object(x,y);
1039. 			    newsym(x,y);
1040. 			}
1041. 
1042. 			if ((trap = t_at(x,y)) && !trap->tseen && !rnl(8)) {
1043. 			    nomul(0);
1044. 
1045. 			    if (trap->ttyp == STATUE_TRAP) {
1046. 				if (activate_statue_trap(trap, x, y, FALSE))
1047. 				    exercise(A_WIS, TRUE);
1048. 				return(1);
1049. 			    } else {
1050. 				find_trap(trap);
1051. 			    }
1052. 			}
1053. 		    }
1054. 		}
1055. 	    }
1056. 	}
1057. 	return(1);
1058. }
1059. 
1060. int
1061. dosearch()
1062. {
1063. 	return(dosearch0(0));
1064. }
1065. 
1066. /* Pre-map the sokoban levels */
1067. void
1068. sokoban_detect()
1069. {
1070. 	register int x, y;
1071. 	register struct trap *ttmp;
1072. 	register struct obj *obj;
1073. 
1074. 
1075. 	/* Map the background and boulders */
1076. 	for (x = 1; x < COLNO; x++)
1077. 	    for (y = 0; y < ROWNO; y++) {
1078. 	    	levl[x][y].seenv = SVALL;
1079. 	    	levl[x][y].waslit = TRUE;
1080. 	    	map_background(x, y, 1);
1081. 	    	for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
1082. 	    	    if (obj->otyp == BOULDER)
1083. 	    	    	map_object(obj, 1);
1084. 	    }
1085. 
1086. 	/* Map the traps */
1087. 	for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
1088. 	    ttmp->tseen = 1;
1089. 	    map_trap(ttmp, 1);
1090. 	}
1091. 
1092. 	return;
1093. }
1094. 
1095. 
1096. /*detect.c*/