Source:NetHack 3.1.0/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.1.0.

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