Source:NetHack 5.0.0/src/getpos.c

From NetHackWiki
Jump to navigation Jump to search

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

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

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

1. /* NetHack 5.0	getpos.c	$NHDT-Date: 1763708572 2025/11/20 23:02:52 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.6 $ */
2. /*-Copyright (c) Pasi Kallinen, 2023. */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. 
7. extern const char what_is_a_location[]; /* from pager.c */
8. 
9. staticfn void getpos_toggle_hilite_state(void);
10. staticfn void getpos_getvalids_selection(struct selectionvar *,
11.                                        boolean (*)(coordxy, coordxy));
12. staticfn void getpos_help_keyxhelp(winid, const char *, const char *, int);
13. staticfn void getpos_help(boolean, const char *);
14. staticfn int QSORTCALLBACK cmp_coord_distu(const void *, const void *);
15. staticfn int gloc_filter_classify_glyph(int);
16. staticfn int gloc_filter_floodfill_matcharea(coordxy, coordxy);
17. staticfn void gloc_filter_floodfill(coordxy, coordxy);
18. staticfn void gloc_filter_init(void);
19. staticfn void gloc_filter_done(void);
20. staticfn void gather_locs(coord **, int *, int);
21. staticfn boolean known_vibrating_square_at(coordxy, coordxy);
22. staticfn void truncate_to_map(coordxy *, coordxy *, schar, schar);
23. staticfn void getpos_refresh(void);
24. /* Callback function for getpos() to highlight desired map locations.
25.  * Parameter TRUE: initialize and highlight, FALSE: done (remove highlights).
26.  */
27. staticfn void (*getpos_hilitefunc)(boolean) = (void (*)(boolean)) 0;
28. staticfn boolean (*getpos_getvalid)(coordxy, coordxy)
29.                                           = (boolean (*)(coordxy, coordxy)) 0;
30. enum getposHiliteState {
31.     HiliteNormalMap = 0,
32.     HiliteGoodposSymbol = 1,
33.     HiliteBackground = 2,
34. };
35. 
36. static enum getposHiliteState
37.     getpos_hilite_state = HiliteNormalMap,
38.     defaultHiliteState = HiliteNormalMap;
39. 
40. void
41. getpos_sethilite(
42.     void (*gp_hilitef)(boolean),
43.     boolean (*gp_getvalidf)(coordxy, coordxy))
44. {
45.     boolean (*old_getvalid)(coordxy, coordxy) = getpos_getvalid;
46.     uint32 old_map_frame_color = gw.wsettings.map_frame_color;
47.     struct selectionvar *sel = selection_new();
48. 
49.     defaultHiliteState = iflags.bgcolors ? HiliteBackground : HiliteNormalMap;
50.     if (gp_getvalidf != old_getvalid)
51.         getpos_hilite_state = defaultHiliteState;
52. 
53.     getpos_getvalids_selection(sel, getpos_getvalid);
54.     getpos_hilitefunc = gp_hilitef;
55.     getpos_getvalid = gp_getvalidf;
56.     getpos_getvalids_selection(sel, getpos_getvalid);
57.     gw.wsettings.map_frame_color = (getpos_hilite_state == HiliteBackground)
58.                                    ? HI_ZAP : NO_COLOR;
59. 
60.     if (getpos_getvalid != old_getvalid
61.         || gw.wsettings.map_frame_color != old_map_frame_color)
62.         selection_force_newsyms(sel);
63.     selection_free(sel, TRUE);
64. }
65. 
66. /* cycle 'getpos_hilite_state' to its next state;
67.    when 'bgcolors' is Off, it will alternate between not showing valid
68.    positions and showing them via temporary S_goodpos symbol;
69.    when 'bgcolors' is On, there are three states and showing them via
70.    setting background color becomes the default */
71. staticfn void
72. getpos_toggle_hilite_state(void)
73. {
74.     /* getpos_hilitefunc isn't Null */
75.     if (getpos_hilite_state == HiliteGoodposSymbol) {
76.         /* currently on, finish */
77.         (*getpos_hilitefunc)(FALSE); /* tmp_at(DISP_END) */
78.     }
79. 
80.     getpos_hilite_state = (getpos_hilite_state + 1)
81.                           % (iflags.bgcolors ? 3 : 2);
82.     /* resetting the callback functions to their current values will draw
83.        valid-spots with background color if that is the new state and turn
84.        off that color if it was the previous state */
85.     getpos_sethilite(getpos_hilitefunc, getpos_getvalid);
86. 
87.     if (getpos_hilite_state == HiliteGoodposSymbol) {
88.         /* now on, begin */
89.         (*getpos_hilitefunc)(TRUE);
90.     }
91. }
92. 
93. boolean
94. mapxy_valid(coordxy x, coordxy y)
95. {
96.     if (getpos_getvalid)
97.         return (*getpos_getvalid)(x, y);
98.     return FALSE;
99. }
100. 
101. staticfn void
102. getpos_getvalids_selection(
103.     struct selectionvar *sel,
104.     boolean (*validf)(coordxy, coordxy))
105. {
106.     coordxy x, y;
107. 
108.     if (!sel || !validf)
109.         return;
110. 
111.     for (x = 1; x < sel->wid; x++)
112.         for (y = 0; y < sel->hei; y++)
113.             if ((*validf)(x, y))
114.                 selection_setpoint(x, y, sel, 1);
115. }
116. 
117. static const char *const gloc_descr[NUM_GLOCS][4] = {
118.     { "any monsters", "monster", "next/previous monster", "monsters" },
119.     { "any items", "item", "next/previous object", "objects" },
120.     { "any doors", "door", "next/previous door or doorway",
121.       "doors or doorways" },
122.     { "any unexplored areas", "unexplored area", "unexplored location",
123.       "locations next to unexplored locations" },
124.     { "anything interesting", "interesting thing", "anything interesting",
125.       "anything interesting" },
126.     { "any valid locations", "valid location", "valid location",
127.       "valid locations" }
128. };
129. 
130. static const char *const gloc_filtertxt[NUM_GFILTER] = {
131.     "",
132.     " in view",
133.     " in this area"
134. };
135. 
136. staticfn void
137. getpos_help_keyxhelp(
138.     winid tmpwin,
139.     const char *k1, const char *k2,
140.     int gloc)
141. {
142.     char sbuf[BUFSZ], fbuf[QBUFSZ];
143.     const char *move_cursor_to = "move the cursor to ",
144.                *filtertxt = gloc_filtertxt[iflags.getloc_filter];
145. 
146.     if (gloc == GLOC_EXPLORE) {
147.         /* default of "move to unexplored location" is inaccurate
148.            because the position will be one spot short of that */
149.         move_cursor_to = "move the cursor next to an ";
150.         if (iflags.getloc_usemenu)
151.             /* default is too wide for basic 80-column tty so shorten it
152.                to avoid wrapping */
153.             filtertxt = strsubst(strcpy(fbuf, filtertxt),
154.                                  "this area", "area");
155.     }
156.     Sprintf(sbuf, "Use '%s'/'%s' to %s%s%s.",
157.             k1, k2,
158.             iflags.getloc_usemenu ? "get a menu of " : move_cursor_to,
159.             gloc_descr[gloc][2 + iflags.getloc_usemenu], filtertxt);
160.     putstr(tmpwin, 0, sbuf);
161. }
162. 
163. DISABLE_WARNING_FORMAT_NONLITERAL
164. 
165. /* the response for '?' help request in getpos() */
166. staticfn void
167. getpos_help(boolean force, const char *goal)
168. {
169.     static const char *const fastmovemode[2] = { "8 units at a time",
170.                                                  "skipping same glyphs" };
171.     char sbuf[BUFSZ];
172.     boolean doing_what_is;
173.     winid tmpwin = create_nhwindow(NHW_MENU);
174. 
175.     Sprintf(sbuf,
176.             "Use '%s', '%s', '%s', '%s' to move the cursor to %s.", /* hjkl */
177.             visctrl(cmd_from_func(do_move_west)),
178.             visctrl(cmd_from_func(do_move_south)),
179.             visctrl(cmd_from_func(do_move_north)),
180.             visctrl(cmd_from_func(do_move_east)), goal);
181.     putstr(tmpwin, 0, sbuf);
182.     Sprintf(sbuf,
183.             "Use '%s', '%s', '%s', '%s' to fast-move the cursor, %s.",
184.             visctrl(cmd_from_func(do_run_west)),
185.             visctrl(cmd_from_func(do_run_south)),
186.             visctrl(cmd_from_func(do_run_north)),
187.             visctrl(cmd_from_func(do_run_east)),
188.             fastmovemode[iflags.getloc_moveskip]);
189.     putstr(tmpwin, 0, sbuf);
190.     Sprintf(sbuf, "(or prefix normal move with '%s' or '%s' to fast-move)",
191.             visctrl(cmd_from_func(do_run)),
192.             visctrl(cmd_from_func(do_rush)));
193.     putstr(tmpwin, 0, sbuf);
194.     putstr(tmpwin, 0, "Or enter a background symbol (ex. '<').");
195.     Sprintf(sbuf, "Use '%s' to move the cursor on yourself.",
196.             visctrl(gc.Cmd.spkeys[NHKF_GETPOS_SELF]));
197.     putstr(tmpwin, 0, sbuf);
198.     if (!iflags.terrainmode || (iflags.terrainmode & TER_MON) != 0) {
199.         getpos_help_keyxhelp(tmpwin,
200.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_MON_NEXT]),
201.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_MON_PREV]),
202.                              GLOC_MONS);
203.     }
204.     if (goal && !strcmp(goal, "a monster"))
205.         goto skip_non_mons;
206.     if (!iflags.terrainmode || (iflags.terrainmode & TER_OBJ) != 0) {
207.         getpos_help_keyxhelp(tmpwin,
208.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_OBJ_NEXT]),
209.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_OBJ_PREV]),
210.                              GLOC_OBJS);
211.     }
212.     if (!iflags.terrainmode || (iflags.terrainmode & TER_MAP) != 0) {
213.         /* these are primarily useful when choosing a travel
214.            destination for the '_' command */
215.         getpos_help_keyxhelp(tmpwin,
216.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_DOOR_NEXT]),
217.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_DOOR_PREV]),
218.                              GLOC_DOOR);
219.         getpos_help_keyxhelp(tmpwin,
220.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_UNEX_NEXT]),
221.                              visctrl(gc.Cmd.spkeys[NHKF_GETPOS_UNEX_PREV]),
222.                              GLOC_EXPLORE);
223.         getpos_help_keyxhelp(tmpwin,
224.                           visctrl(gc.Cmd.spkeys[NHKF_GETPOS_INTERESTING_NEXT]),
225.                           visctrl(gc.Cmd.spkeys[NHKF_GETPOS_INTERESTING_PREV]),
226.                              GLOC_INTERESTING);
227.     }
228.     Sprintf(sbuf, "Use '%s' to change fast-move mode to %s.",
229.             visctrl(gc.Cmd.spkeys[NHKF_GETPOS_MOVESKIP]),
230.             fastmovemode[!iflags.getloc_moveskip]);
231.     putstr(tmpwin, 0, sbuf);
232.     if (!iflags.terrainmode || (iflags.terrainmode & TER_DETECT) == 0) {
233.         Sprintf(sbuf, "Use '%s' to toggle menu listing for possible targets.",
234.                 visctrl(gc.Cmd.spkeys[NHKF_GETPOS_MENU]));
235.         putstr(tmpwin, 0, sbuf);
236.         Sprintf(sbuf,
237.                 "Use '%s' to change the mode of limiting possible targets.",
238.                 visctrl(gc.Cmd.spkeys[NHKF_GETPOS_LIMITVIEW]));
239.         putstr(tmpwin, 0, sbuf);
240.     }
241.     if (!iflags.terrainmode) {
242.         char kbuf[BUFSZ];
243. 
244.         if (getpos_getvalid) {
245.             Sprintf(sbuf, "Use '%s' or '%s' to move to valid locations.",
246.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_VALID_NEXT]),
247.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_VALID_PREV]));
248.             putstr(tmpwin, 0, sbuf);
249.         }
250.         if (getpos_hilitefunc) {
251.             Sprintf(sbuf, "Use '%s' to toggle marking of valid locations.",
252.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_SHOWVALID]));
253.             putstr(tmpwin, 0, sbuf);
254.         }
255.         Sprintf(sbuf, "Use '%s' to toggle automatic description.",
256.                 visctrl(gc.Cmd.spkeys[NHKF_GETPOS_AUTODESC]));
257.         putstr(tmpwin, 0, sbuf);
258.         if (iflags.cmdassist) { /* assisting the '/' command, I suppose... */
259.             Sprintf(sbuf,
260.                     (iflags.getpos_coords == GPCOORDS_NONE)
261.         ? "(Set 'whatis_coord' option to include coordinates with '%s' text.)"
262.         : "(Reset 'whatis_coord' option to omit coordinates from '%s' text.)",
263.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_AUTODESC]));
264.         }
265.  skip_non_mons:
266.         /* disgusting hack; the alternate selection characters work for any
267.            getpos call, but only matter for dowhatis (and doquickwhatis,
268.            also for dotherecmdmenu's simulated mouse) */
269.         doing_what_is = (goal == what_is_a_location);
270.         if (doing_what_is) {
271.             Sprintf(kbuf, "'%s' or '%s' or '%s' or '%s'",
272.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK]),
273.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_Q]),
274.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_O]),
275.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_V]));
276.         } else {
277.             Sprintf(kbuf, "'%s'", visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK]));
278.         }
279.         Snprintf(sbuf, sizeof(sbuf),
280.                  "Type a %s when you are at the right place.", kbuf);
281.         putstr(tmpwin, 0, sbuf);
282.         if (doing_what_is) {
283.             Sprintf(sbuf,
284.       "  '%s' describe current spot, show 'more info', move to another spot.",
285.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_V]));
286.             putstr(tmpwin, 0, sbuf);
287.             Sprintf(sbuf,
288.                     "  '%s' describe current spot,%s move to another spot;",
289.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK]),
290.                     flags.help && !force ? " prompt if 'more info'," : "");
291.             putstr(tmpwin, 0, sbuf);
292.             Sprintf(sbuf,
293.                     "  '%s' describe current spot, move to another spot;",
294.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_Q]));
295.             putstr(tmpwin, 0, sbuf);
296.             Sprintf(sbuf,
297.                     "  '%s' describe current spot, stop looking at things;",
298.                     visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK_O]));
299.             putstr(tmpwin, 0, sbuf);
300.         }
301.     }
302.     if (!force)
303.         putstr(tmpwin, 0, "Type Space or Escape when you're done.");
304.     putstr(tmpwin, 0, "");
305.     display_nhwindow(tmpwin, TRUE);
306.     destroy_nhwindow(tmpwin);
307. }
308. 
309. RESTORE_WARNING_FORMAT_NONLITERAL
310. 
311. staticfn int QSORTCALLBACK
312. cmp_coord_distu(const void *a, const void *b)
313. {
314.     const coord *c1 = a;
315.     const coord *c2 = b;
316.     int dx, dy, dist_1, dist_2;
317. 
318.     dx = u.ux - c1->x;
319.     dy = u.uy - c1->y;
320.     dist_1 = max(abs(dx), abs(dy));
321.     dx = u.ux - c2->x;
322.     dy = u.uy - c2->y;
323.     dist_2 = max(abs(dx), abs(dy));
324. 
325.     if (dist_1 == dist_2)
326.         return (c1->y != c2->y) ? (c1->y - c2->y) : (c1->x - c2->x);
327. 
328.     return dist_1 - dist_2;
329. }
330. 
331. #define IS_UNEXPLORED_LOC(x,y) \
332.     (isok((x), (y))                                             \
333.      && glyph_is_unexplored(levl[(x)][(y)].glyph)               \
334.      && !levl[(x)][(y)].seenv)
335. 
336. #define GLOC_SAME_AREA(x,y) \
337.     (isok((x), (y))                                             \
338.      && (selection_getpoint((x),(y), gg.gloc_filter_map)))
339. 
340. staticfn int
341. gloc_filter_classify_glyph(int glyph)
342. {
343.     int c;
344. 
345.     if (!glyph_is_cmap(glyph))
346.         return 0;
347. 
348.     c = glyph_to_cmap(glyph);
349. 
350.     if (is_cmap_room(c) || is_cmap_furniture(c))
351.         return 1;
352.     else if (is_cmap_wall(c) || c == S_tree)
353.         return 2;
354.     else if (is_cmap_corr(c))
355.         return 3;
356.     else if (is_cmap_water(c))
357.         return 4;
358.     else if (is_cmap_lava(c))
359.         return 5;
360.     return 0;
361. }
362. 
363. staticfn int
364. gloc_filter_floodfill_matcharea(coordxy x, coordxy y)
365. {
366.     int glyph = back_to_glyph(x, y);
367. 
368.     if (!levl[x][y].seenv)
369.         return FALSE;
370. 
371.     if (glyph == gg.gloc_filter_floodfill_match_glyph)
372.         return TRUE;
373. 
374.     if (gloc_filter_classify_glyph(glyph)
375.         == gloc_filter_classify_glyph(gg.gloc_filter_floodfill_match_glyph))
376.         return TRUE;
377. 
378.     return FALSE;
379. }
380. 
381. staticfn void
382. gloc_filter_floodfill(coordxy x, coordxy y)
383. {
384.     gg.gloc_filter_floodfill_match_glyph = back_to_glyph(x, y);
385. 
386.     set_selection_floodfillchk(gloc_filter_floodfill_matcharea);
387.     selection_floodfill(gg.gloc_filter_map, x, y, FALSE);
388. }
389. 
390. staticfn void
391. gloc_filter_init(void)
392. {
393.     if (iflags.getloc_filter == GFILTER_AREA) {
394.         if (!gg.gloc_filter_map) {
395.             gg.gloc_filter_map = selection_new();
396.         }
397.         /* special case: if we're in a doorway, try to figure out which
398.            direction we're moving, and use that side of the doorway */
399.         if (IS_DOOR(levl[u.ux][u.uy].typ)) {
400.             if ((u.dx || u.dy) && isok(u.ux + u.dx, u.uy + u.dy)) {
401.                 gloc_filter_floodfill(u.ux + u.dx, u.uy + u.dy);
402.             } else {
403.                 /* TODO: maybe add both sides of the doorway? */
404.             }
405.         } else {
406.             gloc_filter_floodfill(u.ux, u.uy);
407.         }
408.     }
409. }
410. 
411. staticfn void
412. gloc_filter_done(void)
413. {
414.     if (gg.gloc_filter_map) {
415.         selection_free(gg.gloc_filter_map, TRUE);
416.         gg.gloc_filter_map = (struct selectionvar *) 0;
417. 
418.     }
419. }
420. 
421. staticfn boolean
422. known_vibrating_square_at(coordxy x, coordxy y)
423. {
424.     /* note: this only acknowledges the genuine vibrating square, not
425.        fake ones produced by wizard mode wishing for traps which could
426.        possibly be transfered to normal play via bones file */
427.     if (invocation_pos(x, y)) {
428.         struct trap *ttmp = t_at(x, y);
429. 
430.         return ttmp && ttmp->ttyp == VIBRATING_SQUARE && ttmp->tseen;
431.     }
432.     return FALSE;
433. }
434. 
435. DISABLE_WARNING_UNREACHABLE_CODE
436. 
437. boolean
438. gather_locs_interesting(
439.     coordxy x, coordxy y,
440.     int gloc)
441. {
442.     int glyph, sym;
443. 
444.     if (iflags.getloc_filter == GFILTER_VIEW && !cansee(x, y))
445.         return FALSE;
446.     if (iflags.getloc_filter == GFILTER_AREA && !GLOC_SAME_AREA(x, y)
447.         && !GLOC_SAME_AREA(x - 1, y) && !GLOC_SAME_AREA(x, y - 1)
448.         && !GLOC_SAME_AREA(x + 1, y) && !GLOC_SAME_AREA(x, y + 1))
449.         return FALSE;
450. 
451.     glyph = glyph_at(x, y);
452.     sym = glyph_is_cmap(glyph) ? glyph_to_cmap(glyph) : -1;
453.     switch (gloc) {
454.     default:
455.     case GLOC_MONS:
456.         /* unlike '/M', this skips monsters revealed by
457.            warning glyphs and remembered unseen ones */
458.         return (glyph_is_monster(glyph)
459.                 && glyph != monnum_to_glyph(PM_LONG_WORM_TAIL, MALE)
460.                 && glyph != monnum_to_glyph(PM_LONG_WORM_TAIL, FEMALE));
461.     case GLOC_OBJS:
462.         return (glyph_is_object(glyph)
463.                 && glyph != objnum_to_glyph(BOULDER)
464.                 && glyph != objnum_to_glyph(ROCK));
465.     case GLOC_DOOR:
466.         return (glyph_is_cmap(glyph)
467.                 && (is_cmap_door(sym)
468.                     || is_cmap_drawbridge(sym)
469.                     || sym == S_ndoor));
470.     case GLOC_EXPLORE:
471.         return (glyph_is_cmap(glyph)
472.                 && !glyph_is_nothing(glyph_to_cmap(glyph))
473.                 && (is_cmap_door(sym)
474.                     || is_cmap_drawbridge(sym)
475.                     || sym == S_ndoor
476.                     || is_cmap_room(sym)
477.                     || is_cmap_corr(sym))
478.                 && (IS_UNEXPLORED_LOC(x + 1, y)
479.                     || IS_UNEXPLORED_LOC(x - 1, y)
480.                     || IS_UNEXPLORED_LOC(x, y + 1)
481.                     || IS_UNEXPLORED_LOC(x, y - 1)));
482.     case GLOC_VALID:
483.         if (getpos_getvalid)
484.             return (*getpos_getvalid)(x, y);
485.         FALLTHROUGH;
486.         /*FALLTHRU*/
487.     case GLOC_INTERESTING:
488.         return (gather_locs_interesting(x, y, GLOC_DOOR)
489.                 || !((glyph_is_cmap(glyph)
490.                       && (is_cmap_wall(sym)
491.                           || sym == S_tree
492.                           || sym == S_bars
493.                           || sym == S_ice
494.                           || sym == S_air
495.                           || sym == S_cloud
496.                           || is_cmap_lava(sym)
497.                           || is_cmap_water(sym)
498.                           || sym == S_ndoor
499.                           || is_cmap_room(sym)
500.                           || is_cmap_corr(sym)))
501.                      || glyph_is_nothing(glyph)
502.                      || glyph_is_unexplored(glyph))
503.                 || known_vibrating_square_at(x, y));
504.     }
505.     /*NOTREACHED*/
506.     return FALSE;
507. }
508. 
509. RESTORE_WARNINGS
510. 
511. /* gather locations for monsters or objects shown on the map */
512. staticfn void
513. gather_locs(coord **arr_p, int *cnt_p, int gloc)
514. {
515.     int pass, idx;
516.     coordxy x, y;
517. 
518.     /*
519.      * We always include the hero's location even if there is no monster
520.      * (invisible hero without see invisible) or object (usual case)
521.      * displayed there.  That way, the count will always be at least 1,
522.      * and player has a visual indicator (cursor returns to hero's spot)
523.      * highlighting when successive 'm's or 'o's have cycled all the way
524.      * through all monsters or objects.
525.      *
526.      * Hero's spot will always sort to array[0] because it will always
527.      * be the shortest distance (namely, 0 units) away from <u.ux,u.uy>.
528.      */
529. 
530.     gloc_filter_init();
531. 
532.     *cnt_p = idx = 0;
533.     for (pass = 0; pass < 2; pass++) {
534.         for (x = 1; x < COLNO; x++)
535.             for (y = 0; y < ROWNO; y++) {
536.                 if (u_at(x, y) || gather_locs_interesting(x, y, gloc)) {
537.                     if (!pass) {
538.                         ++*cnt_p;
539.                     } else {
540.                         (*arr_p)[idx].x = x;
541.                         (*arr_p)[idx].y = y;
542.                         ++idx;
543.                     }
544.                 }
545.             }
546. 
547.         if (!pass) /* end of first pass */
548.             *arr_p = (coord *) alloc(*cnt_p * sizeof (coord));
549.         else /* end of second pass */
550.             qsort(*arr_p, *cnt_p, sizeof (coord), cmp_coord_distu);
551.     } /* pass */
552. 
553.     gloc_filter_done();
554. }
555. 
556. char *
557. dxdy_to_dist_descr(coordxy dx, coordxy dy, boolean fulldir)
558. {
559.     static char buf[30];
560.     int dst;
561. 
562.     if (!dx && !dy) {
563.         Sprintf(buf, "here");
564.     } else if ((dst = xytodir(dx, dy)) != -1) {
565.         /* explicit direction; 'one step' is implicit */
566.         Sprintf(buf, "%s", directionname(dst));
567.     } else {
568.         static const char *const dirnames[4][2] = {
569.             { "n", "north" },
570.             { "s", "south" },
571.             { "w", "west" },
572.             { "e", "east" } };
573.         buf[0] = '\0';
574.         /* 9999: protect buf[] against overflow caused by invalid values */
575.         if (dy) {
576.             if (abs(dy) > 9999)
577.                 dy = sgn(dy) * 9999;
578.             Sprintf(eos(buf), "%d%s%s", abs(dy), dirnames[(dy > 0)][fulldir],
579.                     dx ? "," : "");
580.         }
581.         if (dx) {
582.             if (abs(dx) > 9999)
583.                 dx = sgn(dx) * 9999;
584.             Sprintf(eos(buf), "%d%s", abs(dx),
585.                     dirnames[2 + (dx > 0)][fulldir]);
586.         }
587.     }
588.     return buf;
589. }
590. 
591. DISABLE_WARNING_FORMAT_NONLITERAL
592. 
593. /* coordinate formatting for 'whatis_coord' option */
594. char *
595. coord_desc(coordxy x, coordxy y, char *outbuf, char cmode)
596. {
597.     static char screen_fmt[16]; /* [12] suffices: "[%02d,%02d]" */
598.     int dx, dy;
599. 
600.     outbuf[0] = '\0';
601.     switch (cmode) {
602.     default:
603.         break;
604.     case GPCOORDS_COMFULL:
605.     case GPCOORDS_COMPASS:
606.         /* "east", "3s", "2n,4w" */
607.         dx = x - u.ux;
608.         dy = y - u.uy;
609.         Sprintf(outbuf, "(%s)",
610.                 dxdy_to_dist_descr(dx, dy, cmode == GPCOORDS_COMFULL));
611.         break;
612.     case GPCOORDS_MAP: /* x,y */
613.         /* upper left corner of map is <1,0>;
614.            with default COLNO,ROWNO lower right corner is <79,20> */
615.         Sprintf(outbuf, "<%d,%d>", x, y);
616.         break;
617.     case GPCOORDS_SCREEN: /* y+2,x */
618.         /* for normal map sizes, force a fixed-width formatting so that
619.            /m, /M, /o, and /O output lines up cleanly; map sizes bigger
620.            than Nx999 or 999xM will still work, but not line up like normal
621.            when displayed in a column setting.
622. 
623.            The (100) is placed in brackets below to mark the [: "03"] as
624.            explicit compile-time dead code for clang */
625.         if (!*screen_fmt)
626.             Sprintf(screen_fmt, "[%%%sd,%%%sd]",
627.                     (ROWNO - 1 + 2 < (100)) ? "02" :  "03",
628.                     (COLNO - 1 < (100)) ? "02" : "03");
629.         /* map line 0 is screen row 2;
630.            map column 0 isn't used, map column 1 is screen column 1 */
631.         Sprintf(outbuf, screen_fmt, y + 2, x);
632.         break;
633.     }
634.     return outbuf;
635. }
636. 
637. RESTORE_WARNING_FORMAT_NONLITERAL
638. 
639. void
640. auto_describe(coordxy cx, coordxy cy)
641. {
642.     coord cc;
643.     int sym = 0;
644.     char tmpbuf[BUFSZ];
645.     const char *firstmatch = "unknown";
646. 
647.     cc.x = cx;
648.     cc.y = cy;
649.     if (do_screen_description(cc, TRUE, sym, tmpbuf, &firstmatch,
650.                               (struct permonst **) 0)) {
651.         (void) coord_desc(cx, cy, tmpbuf, iflags.getpos_coords);
652.         custompline((SUPPRESS_HISTORY | OVERRIDE_MSGTYPE | NO_CURS_ON_U),
653.                     "%s%s%s%s%s", firstmatch, *tmpbuf ? " " : "", tmpbuf,
654.                     (iflags.autodescribe
655.                      && getpos_getvalid && !(*getpos_getvalid)(cx, cy))
656.                       ? " (invalid target)" : "",
657.                     (iflags.getloc_travelmode && !is_valid_travelpt(cx, cy))
658.                       ? " (no travel path)" : "");
659.         curs(WIN_MAP, cx, cy);
660.         flush_screen(0);
661.     }
662. }
663. 
664. boolean
665. getpos_menu(coord *ccp, int gloc)
666. {
667.     coord *garr = DUMMY;
668.     int gcount = 0;
669.     winid tmpwin;
670.     anything any;
671.     int i, pick_cnt;
672.     menu_item *picks = (menu_item *) 0;
673.     char tmpbuf[BUFSZ];
674.     int clr = NO_COLOR;
675. 
676.     gather_locs(&garr, &gcount, gloc);
677. 
678.     if (gcount < 2) { /* gcount always includes the hero */
679.         free((genericptr_t) garr);
680.         You("cannot %s %s.",
681.             (iflags.getloc_filter == GFILTER_VIEW) ? "see" : "detect",
682.             gloc_descr[gloc][0]);
683.         return FALSE;
684.     }
685. 
686.     tmpwin = create_nhwindow(NHW_MENU);
687.     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
688.     any = cg.zeroany;
689. 
690.     /* gather_locs returns array[0] == you. skip it. */
691.     for (i = 1; i < gcount; i++) {
692.         char fullbuf[BUFSZ];
693.         coord tmpcc;
694.         const char *firstmatch = "unknown";
695.         int sym = 0;
696. 
697.         any.a_int = i + 1;
698.         tmpcc.x = garr[i].x;
699.         tmpcc.y = garr[i].y;
700.         if (do_screen_description(tmpcc, TRUE, sym, tmpbuf,
701.                               &firstmatch, (struct permonst **)0)) {
702.             (void) coord_desc(garr[i].x, garr[i].y, tmpbuf,
703.                               iflags.getpos_coords);
704.             Snprintf(fullbuf, sizeof fullbuf, "%s%s%s", firstmatch,
705.                     (*tmpbuf ? " " : ""), tmpbuf);
706.             add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0,
707.                      ATR_NONE, clr, fullbuf, MENU_ITEMFLAGS_NONE);
708.         }
709.     }
710. 
711.     Sprintf(tmpbuf, "Pick %s%s%s",
712.             an(gloc_descr[gloc][1]),
713.             gloc_filtertxt[iflags.getloc_filter],
714.             iflags.getloc_travelmode ? " for travel destination" : "");
715.     end_menu(tmpwin, tmpbuf);
716.     pick_cnt = select_menu(tmpwin, PICK_ONE, &picks);
717.     destroy_nhwindow(tmpwin);
718.     if (pick_cnt > 0) {
719.         ccp->x = garr[picks->item.a_int - 1].x;
720.         ccp->y = garr[picks->item.a_int - 1].y;
721.         free((genericptr_t) picks);
722.     }
723.     free((genericptr_t) garr);
724.     return (pick_cnt > 0);
725. }
726. 
727. /* add dx,dy to cx,cy, truncating at map edges */
728. staticfn void
729. truncate_to_map(coordxy *cx, coordxy *cy, schar dx, schar dy)
730. {
731.     /* diagonal moves complicate this... */
732.     if (*cx + dx < 1) {
733.         dy -= sgn(dy) * (1 - (*cx + dx));
734.         dx = 1 - *cx; /* so that (cx+dx == 1) */
735.     } else if (*cx + dx > COLNO - 1) {
736.         dy += sgn(dy) * ((COLNO - 1) - (*cx + dx));
737.         dx = (COLNO - 1) - *cx;
738.     }
739.     if (*cy + dy < 0) {
740.         dx -= sgn(dx) * (0 - (*cy + dy));
741.         dy = 0 - *cy; /* so that (cy+dy == 0) */
742.     } else if (*cy + dy > ROWNO - 1) {
743.         dx += sgn(dx) * ((ROWNO - 1) - (*cy + dy));
744.         dy = (ROWNO - 1) - *cy;
745.     }
746.     *cx += dx;
747.     *cy += dy;
748. }
749. 
750. /* called when ^R typed; if '$' is being shown for valid spots, remove that;
751.    if alternate background color is being shown for that, redraw it */
752. staticfn void
753. getpos_refresh(void)
754. {
755.     if (getpos_hilitefunc && getpos_hilite_state == HiliteGoodposSymbol) {
756.         (*getpos_hilitefunc)(FALSE); /* tmp_at(DISP_END) */
757.         getpos_hilite_state = defaultHiliteState;
758.     }
759. 
760.     docrt_flags(docrtRefresh);
761. 
762.     if (getpos_hilitefunc && getpos_hilite_state == HiliteBackground) {
763.         /* resetting to current values will draw valid-spots highlighting */
764.         getpos_sethilite(getpos_hilitefunc, getpos_getvalid);
765.     }
766. }
767. 
768. /* have the player use movement keystrokes to position the cursor at a
769.    particular map location, then use one of [.,:;] to pick the spot */
770. int
771. getpos(coord *ccp, boolean force, const char *goal)
772. {
773.     static struct {
774.         int nhkf, ret;
775.     } const pick_chars_def[] = {
776.         { NHKF_GETPOS_PICK, LOOK_TRADITIONAL },
777.         { NHKF_GETPOS_PICK_Q, LOOK_QUICK },
778.         { NHKF_GETPOS_PICK_O, LOOK_ONCE },
779.         { NHKF_GETPOS_PICK_V, LOOK_VERBOSE }
780.     };
781.     static const int mMoOdDxX_def[] = {
782.         NHKF_GETPOS_MON_NEXT,
783.         NHKF_GETPOS_MON_PREV,
784.         NHKF_GETPOS_OBJ_NEXT,
785.         NHKF_GETPOS_OBJ_PREV,
786.         NHKF_GETPOS_DOOR_NEXT,
787.         NHKF_GETPOS_DOOR_PREV,
788.         NHKF_GETPOS_UNEX_NEXT,
789.         NHKF_GETPOS_UNEX_PREV,
790.         NHKF_GETPOS_INTERESTING_NEXT,
791.         NHKF_GETPOS_INTERESTING_PREV,
792.         NHKF_GETPOS_VALID_NEXT,
793.         NHKF_GETPOS_VALID_PREV
794.     };
795.     struct _cmd_queue cq, *cmdq;
796.     const char *cp;
797.     char pick_chars[6];
798.     char mMoOdDxX[13];
799.     int result = 0;
800.     int i, c;
801.     int sidx;
802.     coordxy cx, cy;
803.     coordxy tx = u.ux, ty = u.uy;
804.     boolean msg_given = TRUE; /* clear message window by default */
805.     boolean show_goal_msg = FALSE;
806.     coord *garr[NUM_GLOCS] = DUMMY;
807.     int gcount[NUM_GLOCS] = DUMMY;
808.     int gidx[NUM_GLOCS] = DUMMY;
809.     schar udx = u.dx, udy = u.dy, udz = u.dz;
810.     int dx, dy;
811.     boolean rushrun = FALSE;
812. 
813.     /* temporary? if we have a queued direction, return the adjacent spot
814.        in that direction */
815.     if (!gi.in_doagain) {
816.         if ((cmdq = cmdq_pop()) != 0) {
817.             cq = *cmdq;
818.             free((genericptr_t) cmdq);
819.             if (cq.typ == CMDQ_DIR && !cq.dirz) {
820.                 ccp->x = u.ux + cq.dirx;
821.                 ccp->y = u.uy + cq.diry;
822.             } else {
823.                 cmdq_clear(CQ_CANNED);
824.                 result = -1;
825.             }
826.             return result;
827.         }
828.     }
829. 
830.     for (i = 0; i < SIZE(pick_chars_def); i++)
831.         pick_chars[i] = gc.Cmd.spkeys[pick_chars_def[i].nhkf];
832.     pick_chars[SIZE(pick_chars_def)] = '\0';
833. 
834.     for (i = 0; i < SIZE(mMoOdDxX_def); i++)
835.         mMoOdDxX[i] = gc.Cmd.spkeys[mMoOdDxX_def[i]];
836.     mMoOdDxX[SIZE(mMoOdDxX_def)] = '\0';
837. 
838.     if (handle_tip(TIP_GETPOS))
839.         show_goal_msg = TRUE; /* tip has overwritten prompt in mesg window */
840. 
841.     if (!goal)
842.         goal = "desired location";
843.     if (flags.verbose) {
844.         pline("(For instructions type a '%s')",
845.               visctrl(gc.Cmd.spkeys[NHKF_GETPOS_HELP]));
846.         msg_given = TRUE;
847.     }
848.     cx = gg.getposx = ccp->x;
849.     cy = gg.getposy = ccp->y;
850. #ifdef CLIPPING
851.     cliparound(cx, cy);
852. #endif
853.     curs(WIN_MAP, cx, cy);
854.     flush_screen(0);
855. #ifdef MACOS9
856.     lock_mouse_cursor(TRUE);
857. #endif
858.     lock_mouse_buttons(TRUE);
859.     for (;;) {
860.         if (show_goal_msg) {
861.             pline("Move cursor to %s:", goal);
862.             curs(WIN_MAP, cx, cy);
863.             flush_screen(0);
864.             show_goal_msg = FALSE;
865.         } else if (iflags.autodescribe && !msg_given) {
866.             auto_describe(cx, cy);
867.         }
868. 
869.         rushrun = FALSE;
870. 
871.         if ((cmdq = cmdq_pop()) != 0) {
872.             if (cmdq->typ == CMDQ_KEY) {
873.                 c = cmdq->key;
874.             } else {
875.                 cmdq_clear(CQ_CANNED);
876.                 result = -1;
877.                 goto exitgetpos;
878.             }
879.             free(cmdq);
880.         } else {
881.             c = readchar_poskey(&tx, &ty, &sidx);
882.             /* remember_getpos is normally False because reusing the
883.                cursor positioning during ^A is almost never the right
884.                thing to do, but caller could set it if that was needed */
885.             if (iflags.remember_getpos && !gi.in_doagain)
886.                 cmdq_add_key(CQ_REPEAT, c);
887.         }
888. 
889.         if (iflags.autodescribe)
890.             msg_given = FALSE;
891. 
892.         if (c == gc.Cmd.spkeys[NHKF_ESC]) {
893.             cx = cy = -10;
894.             msg_given = TRUE; /* force clear */
895.             result = -1;
896.             break;
897.         }
898.         if (c == cmd_from_func(do_run) || c == cmd_from_func(do_rush)) {
899.             c = readchar_poskey(&tx, &ty, &sidx);
900.             rushrun = TRUE;
901.         }
902.         if (c == 0) {
903.             if (!isok(tx, ty))
904.                 continue;
905.             /* a mouse click event, just assign and return */
906.             cx = tx;
907.             cy = ty;
908.             break;
909.         }
910.         if ((cp = strchr(pick_chars, c)) != 0) {
911.             /* '.' => 0, ',' => 1, ';' => 2, ':' => 3 */
912.             result = pick_chars_def[(int) (cp - pick_chars)].ret;
913.             break;
914.         } else if (movecmd(c, MV_WALK)) {
915.             if (rushrun)
916.                 goto do_rushrun;
917.             dx = u.dx;
918.             dy = u.dy;
919.             truncate_to_map(&cx, &cy, dx, dy);
920.             goto nxtc;
921.         } else if (movecmd(c, MV_RUSH) || movecmd(c, MV_RUN)) {
922.  do_rushrun:
923.             if (iflags.getloc_moveskip) {
924.                 /* skip same glyphs */
925.                 int glyph = glyph_at(cx, cy);
926. 
927.                 dx = u.dx;
928.                 dy = u.dy;
929.                 while (isok(cx + dx, cy + dy)
930.                        && glyph == glyph_at(cx + dx, cy + dy)
931.                        && isok(cx + dx + u.dx, cy + dy + u.dy)
932.                        && glyph == glyph_at(cx + dx + u.dx, cy + dy + u.dy)) {
933.                     dx += u.dx;
934.                     dy += u.dy;
935. 
936.                 }
937.             } else {
938.                 dx = 8 * u.dx;
939.                 dy = 8 * u.dy;
940.             }
941.             truncate_to_map(&cx, &cy, dx, dy);
942.             goto nxtc;
943.         }
944. 
945.         if (c == gc.Cmd.spkeys[NHKF_GETPOS_HELP] || redraw_cmd(c)) {
946.             /* '?' will redraw twice, first when removing popup text window
947.                after showing the help text, then to reset highlighting */
948.             if (c == gc.Cmd.spkeys[NHKF_GETPOS_HELP])
949.                 getpos_help(force, goal);
950.             /* ^R: docrt(), hilite_state = default */
951.             getpos_refresh();
952.             curs(WIN_MAP, cx, cy);
953.             /* update message window to reflect that we're still targeting */
954.             show_goal_msg = TRUE;
955.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_SHOWVALID]) {
956.             if (getpos_hilitefunc) {
957.                 getpos_toggle_hilite_state();
958.                 curs(WIN_MAP, cx, cy);
959.             }
960.             show_goal_msg = TRUE; /* we're still targeting */
961.             goto nxtc;
962.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_AUTODESC]) {
963.             iflags.autodescribe = !iflags.autodescribe;
964.             pline("Automatic description %sis %s.",
965.                   flags.verbose ? "of features under cursor " : "",
966.                   iflags.autodescribe ? "on" : "off");
967.             if (!iflags.autodescribe)
968.                 show_goal_msg = TRUE;
969.             msg_given = TRUE;
970.             goto nxtc;
971.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_LIMITVIEW]) {
972.             static const char *const view_filters[NUM_GFILTER] = {
973.                 "Not limiting targets",
974.                 "Limiting targets to those in sight",
975.                 "Limiting targets to those in same area"
976.             };
977. 
978.             iflags.getloc_filter = (iflags.getloc_filter + 1) % NUM_GFILTER;
979.             for (i = 0; i < NUM_GLOCS; i++) {
980.                 if (garr[i]) {
981.                     free((genericptr_t) garr[i]);
982.                     garr[i] = NULL;
983.                 }
984.                 gidx[i] = gcount[i] = 0;
985.             }
986.             pline("%s.", view_filters[iflags.getloc_filter]);
987.             msg_given = TRUE;
988.             goto nxtc;
989.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_MENU]) {
990.             iflags.getloc_usemenu = !iflags.getloc_usemenu;
991.             pline("%s a menu to show possible targets%s.",
992.                   iflags.getloc_usemenu ? "Using" : "Not using",
993.                   iflags.getloc_usemenu
994.                       ? " for 'm|M', 'o|O', 'd|D', and 'x|X'" : "");
995.             msg_given = TRUE;
996.             goto nxtc;
997.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_SELF]) {
998.             /* reset 'm&M', 'o&O', &c; otherwise, there's no way for player
999.                to achieve that except by manually cycling through all spots */
1000.             for (i = 0; i < NUM_GLOCS; i++)
1001.                 gidx[i] = 0;
1002.             cx = u.ux;
1003.             cy = u.uy;
1004.             goto nxtc;
1005.         } else if (c == gc.Cmd.spkeys[NHKF_GETPOS_MOVESKIP]) {
1006.             iflags.getloc_moveskip = !iflags.getloc_moveskip;
1007.             pline("%skipping over similar terrain when fastmoving the cursor.",
1008.                   iflags.getloc_moveskip ? "S" : "Not s");
1009.             msg_given = TRUE;
1010.             goto nxtc;
1011.         } else if ((cp = strchr(mMoOdDxX, c)) != 0) { /* 'm|M', 'o|O', &c */
1012.             /* nearest or farthest monster or object or door or unexplored */
1013.             int gtmp = (int) (cp - mMoOdDxX), /* 0..7 */
1014.                 gloc = gtmp >> 1;             /* 0..3 */
1015. 
1016.             if (iflags.getloc_usemenu) {
1017.                 coord tmpcrd;
1018. 
1019.                 if (getpos_menu(&tmpcrd, gloc)) {
1020.                     cx = tmpcrd.x;
1021.                     cy = tmpcrd.y;
1022.                 }
1023.                 goto nxtc;
1024.             }
1025. 
1026.             if (!garr[gloc]) {
1027.                 gather_locs(&garr[gloc], &gcount[gloc], gloc);
1028.                 gidx[gloc] = 0; /* garr[][0] is hero's spot */
1029.             }
1030.             if (!(gtmp & 1)) {  /* c=='m' || c=='o' || c=='d' || c=='x') */
1031.                 gidx[gloc] = (gidx[gloc] + 1) % gcount[gloc];
1032.             } else {            /* c=='M' || c=='O' || c=='D' || c=='X') */
1033.                 if (--gidx[gloc] < 0)
1034.                     gidx[gloc] = gcount[gloc] - 1;
1035.             }
1036.             cx = garr[gloc][gidx[gloc]].x;
1037.             cy = garr[gloc][gidx[gloc]].y;
1038.             goto nxtc;
1039.         } else {
1040.             if (!strchr(quitchars, c)) {
1041.                 char matching[MAXPCHARS];
1042.                 int pass, k = 0;
1043.                 coordxy lo_x, lo_y, hi_x, hi_y;
1044. 
1045.                 (void) memset((genericptr_t) matching, 0, sizeof matching);
1046.                 for (sidx = 0; sidx < MAXPCHARS; sidx++) {
1047.                     /* don't even try to match some terrain: walls, room... */
1048.                     if (is_cmap_wall(sidx) || is_cmap_room(sidx)
1049.                         || is_cmap_corr(sidx) || is_cmap_door(sidx)
1050.                         || sidx == S_ndoor)
1051.                         continue;
1052.                     if (c == defsyms[sidx].sym
1053.                         || c == (int) gs.showsyms[sidx]
1054.                         /* have '^' match webs and vibrating square or any
1055.                            other trap that uses something other than '^' */
1056.                         || (c == '^' && is_cmap_trap(sidx))
1057.                         /* have room engraving character (default '`')
1058.                            match corridor engravings (default '#') too */
1059.                         || (c == gs.showsyms[S_engroom]
1060.                             && is_cmap_engraving(sidx)))
1061.                         matching[sidx] = (char) ++k;
1062.                 }
1063.                 if (k) {
1064.                     for (pass = 0; pass <= 1; pass++) {
1065.                         /* pass 0: just past current pos to lower right;
1066.                            pass 1: upper left corner to current pos */
1067.                         lo_y = (pass == 0) ? cy : 0;
1068.                         hi_y = (pass == 0) ? ROWNO - 1 : cy;
1069.                         for (ty = lo_y; ty <= hi_y; ty++) {
1070.                             lo_x = (pass == 0 && ty == lo_y) ? cx + 1 : 1;
1071.                             hi_x = (pass == 1 && ty == hi_y) ? cx : COLNO - 1;
1072.                             for (tx = lo_x; tx <= hi_x; tx++) {
1073.                                 /* first, look at what is currently visible
1074.                                    (might be monster) */
1075.                                 k = glyph_at(tx, ty);
1076.                                 if (glyph_is_cmap(k)
1077.                                     && matching[glyph_to_cmap(k)])
1078.                                     goto foundc;
1079.                                 /* next, try glyph that's remembered here
1080.                                    (might be trap or object) */
1081.                                 if (svl.level.flags.hero_memory
1082.                                     /* !terrainmode: don't move to remembered
1083.                                        trap or object if not currently shown */
1084.                                     && !iflags.terrainmode) {
1085.                                     k = levl[tx][ty].glyph;
1086.                                     if (glyph_is_cmap(k)
1087.                                         && matching[glyph_to_cmap(k)])
1088.                                         goto foundc;
1089.                                 }
1090.                                 /* FIXME: check player-specified vib.sq trap
1091.                                    symbol rather than or in addition to '~' */
1092.                                 if (c == '~'
1093.                                     && known_vibrating_square_at(tx, ty))
1094.                                     goto foundc;
1095.                                 /* last, try actual terrain here (shouldn't
1096.                                    we be using svl.lastseentyp[][] instead?) */
1097.                                 if (levl[tx][ty].seenv) {
1098.                                     k = back_to_glyph(tx, ty);
1099.                                     if (glyph_is_cmap(k)
1100.                                         && matching[glyph_to_cmap(k)])
1101.                                         goto foundc;
1102.                                 }
1103.                                 continue;
1104.  foundc:
1105.                                 cx = tx, cy = ty;
1106.                                 if (msg_given) {
1107.                                     clear_nhwindow(WIN_MESSAGE);
1108.                                     msg_given = FALSE;
1109.                                 }
1110.                                 goto nxtc;
1111.                             } /* column */
1112.                         }     /* row */
1113.                     }         /* pass */
1114.                     pline("Can't find dungeon feature '%c'.", c);
1115.                     msg_given = TRUE;
1116.                     goto nxtc;
1117.                 } else {
1118.                     char note[QBUFSZ];
1119. 
1120.                     if (!force)
1121.                         Strcpy(note, "aborted");
1122.                     else /* hjkl */
1123.                         Sprintf(note, "use '%s', '%s', '%s', '%s' or '%s'",
1124.                                 visctrl(cmd_from_func(do_move_west)),
1125.                                 visctrl(cmd_from_func(do_move_south)),
1126.                                 visctrl(cmd_from_func(do_move_north)),
1127.                                 visctrl(cmd_from_func(do_move_east)),
1128.                                 visctrl(gc.Cmd.spkeys[NHKF_GETPOS_PICK]));
1129.                     pline("Unknown direction: '%s' (%s).", visctrl((char) c),
1130.                           note);
1131.                     msg_given = TRUE;
1132.                 } /* k => matching */
1133.             }     /* !quitchars */
1134.             if (force)
1135.                 goto nxtc;
1136.             pline("Done.");
1137.             msg_given = FALSE; /* suppress clear */
1138.             cx = -1;
1139.             cy = 0;
1140.             result = 0; /* not -1 */
1141.             break;
1142.         }
1143.  nxtc:
1144.         gg.getposx = cx, gg.getposy = cy;
1145. #ifdef CLIPPING
1146.         cliparound(cx, cy);
1147. #endif
1148.         curs(WIN_MAP, cx, cy);
1149.         flush_screen(0);
1150.     }
1151.  exitgetpos:
1152. #ifdef MACOS9
1153.     lock_mouse_cursor(FALSE);
1154. #endif
1155.     lock_mouse_buttons(FALSE);
1156.     if (msg_given)
1157.         clear_nhwindow(WIN_MESSAGE);
1158.     ccp->x = cx;
1159.     ccp->y = cy;
1160.     gg.getposx = gg.getposy = 0;
1161.     for (i = 0; i < NUM_GLOCS; i++)
1162.         if (garr[i])
1163.             free((genericptr_t) garr[i]);
1164.     getpos_sethilite(NULL, NULL);
1165.     u.dx = udx, u.dy = udy, u.dz = udz;
1166.     return result;
1167. }
1168. 
1169. /*getpos.c*/