OSDN Git Service

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