Source:NetHack 3.6.7/src/do name.c

From NetHackWiki
Jump to navigation Jump to search

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

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

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

1. /* NetHack 3.6	do_name.c	$NHDT-Date: 1674864731 2023/01/28 00:12:11 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.154 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Pasi Kallinen, 2018. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. STATIC_DCL char *NDECL(nextmbuf);
9. STATIC_DCL void FDECL(getpos_help, (BOOLEAN_P, const char *));
10. STATIC_DCL int FDECL(CFDECLSPEC cmp_coord_distu, (const void *, const void *));
11. STATIC_DCL boolean FDECL(gather_locs_interesting, (int, int, int));
12. STATIC_DCL char *FDECL(name_from_player, (char *, const char *, const char *));
13. STATIC_DCL void FDECL(gather_locs, (coord **, int *, int));
14. STATIC_DCL int FDECL(gloc_filter_floodfill_matcharea, (int, int));
15. STATIC_DCL void FDECL(auto_describe, (int, int));
16. STATIC_DCL void NDECL(do_mname);
17. STATIC_DCL boolean FDECL(alreadynamed, (struct monst *, char *, char *));
18. STATIC_DCL void FDECL(do_oname, (struct obj *));
19. STATIC_PTR char *FDECL(docall_xname, (struct obj *));
20. STATIC_DCL void NDECL(namefloorobj);
21. STATIC_DCL char *FDECL(bogusmon, (char *,char *));
22. 
23. extern const char what_is_an_unknown_object[]; /* from pager.c */
24. 
25. #define NUMMBUF 5
26. 
27. /* manage a pool of BUFSZ buffers, so callers don't have to */
28. STATIC_OVL char *
29. nextmbuf()
30. {
31.     static char NEARDATA bufs[NUMMBUF][BUFSZ];
32.     static int bufidx = 0;
33. 
34.     bufidx = (bufidx + 1) % NUMMBUF;
35.     return bufs[bufidx];
36. }
37. 
38. /* function for getpos() to highlight desired map locations.
39.  * parameter value 0 = initialize, 1 = highlight, 2 = done
40.  */
41. static void FDECL((*getpos_hilitefunc), (int)) = (void FDECL((*), (int))) 0;
42. static boolean FDECL((*getpos_getvalid), (int, int)) =
43.                                            (boolean FDECL((*), (int, int))) 0;
44. 
45. void
46. getpos_sethilite(gp_hilitef, gp_getvalidf)
47. void FDECL((*gp_hilitef), (int));
48. boolean FDECL((*gp_getvalidf), (int, int));
49. {
50.     getpos_hilitefunc = gp_hilitef;
51.     getpos_getvalid = gp_getvalidf;
52. }
53. 
54. static const char *const gloc_descr[NUM_GLOCS][4] = {
55.     { "any monsters", "monster", "next/previous monster", "monsters" },
56.     { "any items", "item", "next/previous object", "objects" },
57.     { "any doors", "door", "next/previous door or doorway", "doors or doorways" },
58.     { "any unexplored areas", "unexplored area", "unexplored location",
59.       "unexplored locations" },
60.     { "anything interesting", "interesting thing", "anything interesting",
61.       "anything interesting" },
62.     { "any valid locations", "valid location", "valid location",
63.       "valid locations" }
64. };
65. 
66. static const char *const gloc_filtertxt[NUM_GFILTER] = {
67.     "",
68.     " in view",
69.     " in this area"
70. };
71. 
72. void
73. getpos_help_keyxhelp(tmpwin, k1, k2, gloc)
74. winid tmpwin;
75. const char *k1;
76. const char *k2;
77. int gloc;
78. {
79.     char sbuf[BUFSZ];
80. 
81.     Sprintf(sbuf, "Use '%s'/'%s' to %s%s%s.",
82.             k1, k2,
83.             iflags.getloc_usemenu ? "get a menu of "
84.                                   : "move the cursor to ",
85.             gloc_descr[gloc][2 + iflags.getloc_usemenu],
86.             gloc_filtertxt[iflags.getloc_filter]);
87.     putstr(tmpwin, 0, sbuf);
88. }
89. 
90. /* the response for '?' help request in getpos() */
91. STATIC_OVL void
92. getpos_help(force, goal)
93. boolean force;
94. const char *goal;
95. {
96.     static const char *const fastmovemode[2] = { "8 units at a time",
97.                                                  "skipping same glyphs" };
98.     char sbuf[BUFSZ];
99.     boolean doing_what_is;
100.     winid tmpwin = create_nhwindow(NHW_MENU);
101. 
102.     Sprintf(sbuf,
103.             "Use '%c', '%c', '%c', '%c' to move the cursor to %s.", /* hjkl */
104.             Cmd.move_W, Cmd.move_S, Cmd.move_N, Cmd.move_E, goal);
105.     putstr(tmpwin, 0, sbuf);
106.     Sprintf(sbuf,
107.             "Use 'H', 'J', 'K', 'L' to fast-move the cursor, %s.",
108.             fastmovemode[iflags.getloc_moveskip]);
109.     putstr(tmpwin, 0, sbuf);
110.     putstr(tmpwin, 0, "Or enter a background symbol (ex. '<').");
111.     Sprintf(sbuf, "Use '%s' to move the cursor on yourself.",
112.            visctrl(Cmd.spkeys[NHKF_GETPOS_SELF]));
113.     putstr(tmpwin, 0, sbuf);
114.     if (!iflags.terrainmode || (iflags.terrainmode & TER_MON) != 0) {
115.         getpos_help_keyxhelp(tmpwin,
116.                              visctrl(Cmd.spkeys[NHKF_GETPOS_MON_NEXT]),
117.                              visctrl(Cmd.spkeys[NHKF_GETPOS_MON_PREV]),
118.                              GLOC_MONS);
119.     }
120.     if (!iflags.terrainmode || (iflags.terrainmode & TER_OBJ) != 0) {
121.         getpos_help_keyxhelp(tmpwin,
122.                              visctrl(Cmd.spkeys[NHKF_GETPOS_OBJ_NEXT]),
123.                              visctrl(Cmd.spkeys[NHKF_GETPOS_OBJ_PREV]),
124.                              GLOC_OBJS);
125.     }
126.     if (!iflags.terrainmode || (iflags.terrainmode & TER_MAP) != 0) {
127.         /* these are primarily useful when choosing a travel
128.            destination for the '_' command */
129.         getpos_help_keyxhelp(tmpwin,
130.                              visctrl(Cmd.spkeys[NHKF_GETPOS_DOOR_NEXT]),
131.                              visctrl(Cmd.spkeys[NHKF_GETPOS_DOOR_PREV]),
132.                              GLOC_DOOR);
133.         getpos_help_keyxhelp(tmpwin,
134.                              visctrl(Cmd.spkeys[NHKF_GETPOS_UNEX_NEXT]),
135.                              visctrl(Cmd.spkeys[NHKF_GETPOS_UNEX_PREV]),
136.                              GLOC_EXPLORE);
137.         getpos_help_keyxhelp(tmpwin,
138.                              visctrl(Cmd.spkeys[NHKF_GETPOS_INTERESTING_NEXT]),
139.                              visctrl(Cmd.spkeys[NHKF_GETPOS_INTERESTING_PREV]),
140.                              GLOC_INTERESTING);
141.     }
142.     Sprintf(sbuf, "Use '%s' to change fast-move mode to %s.",
143.             visctrl(Cmd.spkeys[NHKF_GETPOS_MOVESKIP]),
144.             fastmovemode[!iflags.getloc_moveskip]);
145.     putstr(tmpwin, 0, sbuf);
146.     if (!iflags.terrainmode || (iflags.terrainmode & TER_DETECT) == 0) {
147.         Sprintf(sbuf, "Use '%s' to toggle menu listing for possible targets.",
148.                 visctrl(Cmd.spkeys[NHKF_GETPOS_MENU]));
149.         putstr(tmpwin, 0, sbuf);
150.         Sprintf(sbuf,
151.                 "Use '%s' to change the mode of limiting possible targets.",
152.                 visctrl(Cmd.spkeys[NHKF_GETPOS_LIMITVIEW]));
153.         putstr(tmpwin, 0, sbuf);
154.     }
155.     if (!iflags.terrainmode) {
156.         char kbuf[BUFSZ];
157. 
158.         if (getpos_getvalid) {
159.             Sprintf(sbuf, "Use '%s' or '%s' to move to valid locations.",
160.                     visctrl(Cmd.spkeys[NHKF_GETPOS_VALID_NEXT]),
161.                     visctrl(Cmd.spkeys[NHKF_GETPOS_VALID_PREV]));
162.             putstr(tmpwin, 0, sbuf);
163.         }
164.         if (getpos_hilitefunc) {
165.             Sprintf(sbuf, "Use '%s' to display valid locations.",
166.                     visctrl(Cmd.spkeys[NHKF_GETPOS_SHOWVALID]));
167.             putstr(tmpwin, 0, sbuf);
168.         }
169.         Sprintf(sbuf, "Use '%s' to toggle automatic description.",
170.                 visctrl(Cmd.spkeys[NHKF_GETPOS_AUTODESC]));
171.         putstr(tmpwin, 0, sbuf);
172.         if (iflags.cmdassist) { /* assisting the '/' command, I suppose... */
173.             Sprintf(sbuf,
174.                     (iflags.getpos_coords == GPCOORDS_NONE)
175.          ? "(Set 'whatis_coord' option to include coordinates with '%s' text.)"
176.          : "(Reset 'whatis_coord' option to omit coordinates from '%s' text.)",
177.                     visctrl(Cmd.spkeys[NHKF_GETPOS_AUTODESC]));
178.         }
179.         /* disgusting hack; the alternate selection characters work for any
180.            getpos call, but only matter for dowhatis (and doquickwhatis) */
181.         doing_what_is = (goal == what_is_an_unknown_object);
182.         if (doing_what_is) {
183.             Sprintf(kbuf, "'%s' or '%s' or '%s' or '%s'",
184.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK]),
185.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_Q]),
186.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_O]),
187.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_V]));
188.         } else {
189.             Sprintf(kbuf, "'%s'", visctrl(Cmd.spkeys[NHKF_GETPOS_PICK]));
190.         }
191.         Sprintf(sbuf, "Type a %s when you are at the right place.", kbuf);
192.         putstr(tmpwin, 0, sbuf);
193.         if (doing_what_is) {
194.             Sprintf(sbuf,
195.        "  '%s' describe current spot, show 'more info', move to another spot.",
196.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_V]));
197.             putstr(tmpwin, 0, sbuf);
198.             Sprintf(sbuf,
199.                     "  '%s' describe current spot,%s move to another spot;",
200.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK]),
201.                     flags.help ? " prompt if 'more info'," : "");
202.             putstr(tmpwin, 0, sbuf);
203.             Sprintf(sbuf,
204.                     "  '%s' describe current spot, move to another spot;",
205.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_Q]));
206.             putstr(tmpwin, 0, sbuf);
207.             Sprintf(sbuf,
208.                     "  '%s' describe current spot, stop looking at things;",
209.                     visctrl(Cmd.spkeys[NHKF_GETPOS_PICK_O]));
210.             putstr(tmpwin, 0, sbuf);
211.         }
212.     }
213.     if (!force)
214.         putstr(tmpwin, 0, "Type Space or Escape when you're done.");
215.     putstr(tmpwin, 0, "");
216.     display_nhwindow(tmpwin, TRUE);
217.     destroy_nhwindow(tmpwin);
218. }
219. 
220. STATIC_OVL int
221. cmp_coord_distu(a, b)
222. const void *a;
223. const void *b;
224. {
225.     const coord *c1 = a;
226.     const coord *c2 = b;
227.     int dx, dy, dist_1, dist_2;
228. 
229.     dx = u.ux - c1->x;
230.     dy = u.uy - c1->y;
231.     dist_1 = max(abs(dx), abs(dy));
232.     dx = u.ux - c2->x;
233.     dy = u.uy - c2->y;
234.     dist_2 = max(abs(dx), abs(dy));
235. 
236.     if (dist_1 == dist_2)
237.         return (c1->y != c2->y) ? (c1->y - c2->y) : (c1->x - c2->x);
238. 
239.     return dist_1 - dist_2;
240. }
241. 
242. #define IS_UNEXPLORED_LOC(x,y) \
243.     (isok((x), (y))                                     \
244.      && glyph_is_cmap(levl[(x)][(y)].glyph)             \
245.      && glyph_to_cmap(levl[(x)][(y)].glyph) == S_stone  \
246.      && !levl[(x)][(y)].seenv)
247. 
248. static struct opvar *gloc_filter_map = (struct opvar *) 0;
249. 
250. #define GLOC_SAME_AREA(x,y)                                     \
251.     (isok((x), (y))                                             \
252.      && (selection_getpoint((x),(y), gloc_filter_map)))
253. 
254. static int gloc_filter_floodfill_match_glyph;
255. 
256. int
257. gloc_filter_classify_glyph(glyph)
258. int glyph;
259. {
260.     int c;
261. 
262.     if (!glyph_is_cmap(glyph))
263.         return 0;
264. 
265.     c = glyph_to_cmap(glyph);
266. 
267.     if (is_cmap_room(c) || is_cmap_furniture(c))
268.         return 1;
269.     else if (is_cmap_wall(c) || c == S_tree)
270.         return 2;
271.     else if (is_cmap_corr(c))
272.         return 3;
273.     else if (is_cmap_water(c))
274.         return 4;
275.     else if (is_cmap_lava(c))
276.         return 5;
277.     return 0;
278. }
279. 
280. STATIC_OVL int
281. gloc_filter_floodfill_matcharea(x, y)
282. int x, y;
283. {
284.     int glyph = back_to_glyph(x, y);
285. 
286.     if (!levl[x][y].seenv)
287.         return FALSE;
288. 
289.     if (glyph == gloc_filter_floodfill_match_glyph)
290.         return TRUE;
291. 
292.     if (gloc_filter_classify_glyph(glyph)
293.         == gloc_filter_classify_glyph(gloc_filter_floodfill_match_glyph))
294.         return TRUE;
295. 
296.     return FALSE;
297. }
298. 
299. void
300. gloc_filter_floodfill(x, y)
301. int x, y;
302. {
303.     gloc_filter_floodfill_match_glyph = back_to_glyph(x, y);
304. 
305.     set_selection_floodfillchk(gloc_filter_floodfill_matcharea);
306.     selection_floodfill(gloc_filter_map, x, y, FALSE);
307. }
308. 
309. void
310. gloc_filter_init()
311. {
312.     if (iflags.getloc_filter == GFILTER_AREA) {
313.         if (!gloc_filter_map) {
314.             gloc_filter_map = selection_opvar((char *) 0);
315.         }
316.         /* special case: if we're in a doorway, try to figure out which
317.            direction we're moving, and use that side of the doorway */
318.         if (IS_DOOR(levl[u.ux][u.uy].typ)) {
319.             if (u.dx || u.dy) {
320.                 gloc_filter_floodfill(u.ux + u.dx, u.uy + u.dy);
321.             } else {
322.                 /* TODO: maybe add both sides of the doorway? */
323.             }
324.         } else {
325.             gloc_filter_floodfill(u.ux, u.uy);
326.         }
327.     }
328. }
329. 
330. void
331. gloc_filter_done()
332. {
333.     if (gloc_filter_map) {
334.         opvar_free_x(gloc_filter_map);
335.         gloc_filter_map = (struct opvar *) 0;
336.     }
337. }
338. 
339. STATIC_OVL boolean
340. gather_locs_interesting(x, y, gloc)
341. int x, y, gloc;
342. {
343.     /* TODO: if glyph is a pile glyph, convert to ordinary one
344.      *       in order to keep tail/boulder/rock check simple.
345.      */
346.     int glyph = glyph_at(x, y);
347. 
348.     if (iflags.getloc_filter == GFILTER_VIEW && !cansee(x, y))
349.         return FALSE;
350.     if (iflags.getloc_filter == GFILTER_AREA && !GLOC_SAME_AREA(x, y)
351.         && !GLOC_SAME_AREA(x - 1, y) && !GLOC_SAME_AREA(x, y - 1)
352.         && !GLOC_SAME_AREA(x + 1, y) && !GLOC_SAME_AREA(x, y + 1))
353.         return FALSE;
354. 
355.     switch (gloc) {
356.     default:
357.     case GLOC_MONS:
358.         /* unlike '/M', this skips monsters revealed by
359.            warning glyphs and remembered unseen ones */
360.         return (glyph_is_monster(glyph)
361.                 && glyph != monnum_to_glyph(PM_LONG_WORM_TAIL));
362.     case GLOC_OBJS:
363.         return (glyph_is_object(glyph)
364.                 && glyph != objnum_to_glyph(BOULDER)
365.                 && glyph != objnum_to_glyph(ROCK));
366.     case GLOC_DOOR:
367.         return (glyph_is_cmap(glyph)
368.                 && (is_cmap_door(glyph_to_cmap(glyph))
369.                     || is_cmap_drawbridge(glyph_to_cmap(glyph))
370.                     || glyph_to_cmap(glyph) == S_ndoor));
371.     case GLOC_EXPLORE:
372.         return (glyph_is_cmap(glyph)
373.                 && (is_cmap_door(glyph_to_cmap(glyph))
374.                     || is_cmap_drawbridge(glyph_to_cmap(glyph))
375.                     || glyph_to_cmap(glyph) == S_ndoor
376.                     || glyph_to_cmap(glyph) == S_room
377.                     || glyph_to_cmap(glyph) == S_darkroom
378.                     || glyph_to_cmap(glyph) == S_corr
379.                     || glyph_to_cmap(glyph) == S_litcorr)
380.                 && (IS_UNEXPLORED_LOC(x + 1, y)
381.                     || IS_UNEXPLORED_LOC(x - 1, y)
382.                     || IS_UNEXPLORED_LOC(x, y + 1)
383.                     || IS_UNEXPLORED_LOC(x, y - 1)));
384.     case GLOC_VALID:
385.         if (getpos_getvalid)
386.             return (*getpos_getvalid)(x,y);
387.         /*FALLTHRU*/
388.     case GLOC_INTERESTING:
389.         return gather_locs_interesting(x,y, GLOC_DOOR)
390.             || !(glyph_is_cmap(glyph)
391.                  && (is_cmap_wall(glyph_to_cmap(glyph))
392.                      || glyph_to_cmap(glyph) == S_tree
393.                      || glyph_to_cmap(glyph) == S_bars
394.                      || glyph_to_cmap(glyph) == S_ice
395.                      || glyph_to_cmap(glyph) == S_air
396.                      || glyph_to_cmap(glyph) == S_cloud
397.                      || glyph_to_cmap(glyph) == S_lava
398.                      || glyph_to_cmap(glyph) == S_water
399.                      || glyph_to_cmap(glyph) == S_pool
400.                      || glyph_to_cmap(glyph) == S_ndoor
401.                      || glyph_to_cmap(glyph) == S_room
402.                      || glyph_to_cmap(glyph) == S_darkroom
403.                      || glyph_to_cmap(glyph) == S_corr
404.                      || glyph_to_cmap(glyph) == S_litcorr));
405.     }
406.     /*NOTREACHED*/
407.     return FALSE;
408. }
409. 
410. /* gather locations for monsters or objects shown on the map */
411. STATIC_OVL void
412. gather_locs(arr_p, cnt_p, gloc)
413. coord **arr_p;
414. int *cnt_p;
415. int gloc;
416. {
417.     int x, y, pass, idx;
418. 
419.     /*
420.      * We always include the hero's location even if there is no monster
421.      * (invisible hero without see invisible) or object (usual case)
422.      * displayed there.  That way, the count will always be at least 1,
423.      * and player has a visual indicator (cursor returns to hero's spot)
424.      * highlighting when successive 'm's or 'o's have cycled all the way
425.      * through all monsters or objects.
426.      *
427.      * Hero's spot will always sort to array[0] because it will always
428.      * be the shortest distance (namely, 0 units) away from <u.ux,u.uy>.
429.      */
430. 
431.     gloc_filter_init();
432. 
433.     *cnt_p = idx = 0;
434.     for (pass = 0; pass < 2; pass++) {
435.         for (x = 1; x < COLNO; x++)
436.             for (y = 0; y < ROWNO; y++) {
437.                 if ((x == u.ux && y == u.uy)
438.                     || gather_locs_interesting(x, y, gloc)) {
439.                     if (!pass) {
440.                         ++*cnt_p;
441.                     } else {
442.                         (*arr_p)[idx].x = x;
443.                         (*arr_p)[idx].y = y;
444.                         ++idx;
445.                     }
446.                 }
447.             }
448. 
449.         if (!pass) /* end of first pass */
450.             *arr_p = (coord *) alloc(*cnt_p * sizeof (coord));
451.         else /* end of second pass */
452.             qsort(*arr_p, *cnt_p, sizeof (coord), cmp_coord_distu);
453.     } /* pass */
454. 
455.     gloc_filter_done();
456. }
457. 
458. char *
459. dxdy_to_dist_descr(dx, dy, fulldir)
460. int dx, dy;
461. boolean fulldir;
462. {
463.     static char buf[30];
464.     int dst;
465. 
466.     if (!dx && !dy) {
467.         Sprintf(buf, "here");
468.     } else if ((dst = xytod(dx, dy)) != -1) {
469.         /* explicit direction; 'one step' is implicit */
470.         Sprintf(buf, "%s", directionname(dst));
471.     } else {
472.         static const char *dirnames[4][2] = {
473.             { "n", "north" },
474.             { "s", "south" },
475.             { "w", "west" },
476.             { "e", "east" } };
477.         buf[0] = '\0';
478.         /* 9999: protect buf[] against overflow caused by invalid values */
479.         if (dy) {
480.             if (abs(dy) > 9999)
481.                 dy = sgn(dy) * 9999;
482.             Sprintf(eos(buf), "%d%s%s", abs(dy), dirnames[(dy > 0)][fulldir],
483.                     dx ? "," : "");
484.         }
485.         if (dx) {
486.             if (abs(dx) > 9999)
487.                 dx = sgn(dx) * 9999;
488.             Sprintf(eos(buf), "%d%s", abs(dx),
489.                     dirnames[2 + (dx > 0)][fulldir]);
490.         }
491.     }
492.     return buf;
493. }
494. 
495. /* coordinate formatting for 'whatis_coord' option */
496. char *
497. coord_desc(x, y, outbuf, cmode)
498. int x, y;
499. char *outbuf, cmode;
500. {
501.     static char screen_fmt[16]; /* [12] suffices: "[%02d,%02d]" */
502.     int dx, dy;
503. 
504.     outbuf[0] = '\0';
505.     switch (cmode) {
506.     default:
507.         break;
508.     case GPCOORDS_COMFULL:
509.     case GPCOORDS_COMPASS:
510.         /* "east", "3s", "2n,4w" */
511.         dx = x - u.ux;
512.         dy = y - u.uy;
513.         Sprintf(outbuf, "(%s)",
514.                 dxdy_to_dist_descr(dx, dy, cmode == GPCOORDS_COMFULL));
515.         break;
516.     case GPCOORDS_MAP: /* x,y */
517.         /* upper left corner of map is <1,0>;
518.            with default COLNO,ROWNO lower right corner is <79,20> */
519.         Sprintf(outbuf, "<%d,%d>", x, y);
520.         break;
521.     case GPCOORDS_SCREEN: /* y+2,x */
522.         /* for normal map sizes, force a fixed-width formatting so that
523.            /m, /M, /o, and /O output lines up cleanly; map sizes bigger
524.            than Nx999 or 999xM will still work, but not line up like normal
525.            when displayed in a column setting */
526.         if (!*screen_fmt)
527.             Sprintf(screen_fmt, "[%%%sd,%%%sd]",
528.                     (ROWNO - 1 + 2 < 100) ? "02" :  "03",
529.                     (COLNO - 1 < 100) ? "02" : "03");
530.         /* map line 0 is screen row 2;
531.            map column 0 isn't used, map column 1 is screen column 1 */
532.         Sprintf(outbuf, screen_fmt, y + 2, x);
533.         break;
534.     }
535.     return outbuf;
536. }
537. 
538. STATIC_OVL void
539. auto_describe(cx, cy)
540. int cx, cy;
541. {
542.     coord cc;
543.     int sym = 0;
544.     char tmpbuf[BUFSZ];
545.     const char *firstmatch = "unknown";
546. 
547.     cc.x = cx;
548.     cc.y = cy;
549.     if (do_screen_description(cc, TRUE, sym, tmpbuf, &firstmatch,
550.                               (struct permonst **) 0)) {
551.         (void) coord_desc(cx, cy, tmpbuf, iflags.getpos_coords);
552.         custompline(SUPPRESS_HISTORY,
553.                     "%s%s%s%s%s", firstmatch, *tmpbuf ? " " : "", tmpbuf,
554.                     (iflags.autodescribe
555.                      && getpos_getvalid && !(*getpos_getvalid)(cx, cy))
556.                       ? " (illegal)" : "",
557.                     (iflags.getloc_travelmode && !is_valid_travelpt(cx, cy))
558.                       ? " (no travel path)" : "");
559.         curs(WIN_MAP, cx, cy);
560.         flush_screen(0);
561.     }
562. }
563. 
564. boolean
565. getpos_menu(ccp, gloc)
566. coord *ccp;
567. int gloc;
568. {
569.     coord *garr = DUMMY;
570.     int gcount = 0;
571.     winid tmpwin;
572.     anything any;
573.     int i, pick_cnt;
574.     menu_item *picks = (menu_item *) 0;
575.     char tmpbuf[BUFSZ];
576. 
577.     gather_locs(&garr, &gcount, gloc);
578. 
579.     if (gcount < 2) { /* gcount always includes the hero */
580.         free((genericptr_t) garr);
581.         You("cannot %s %s.",
582.             iflags.getloc_filter == GFILTER_VIEW ? "see" : "detect",
583.             gloc_descr[gloc][0]);
584.         return FALSE;
585.     }
586. 
587.     tmpwin = create_nhwindow(NHW_MENU);
588.     start_menu(tmpwin);
589.     any = zeroany;
590. 
591.     /* gather_locs returns array[0] == you. skip it. */
592.     for (i = 1; i < gcount; i++) {
593.         char fullbuf[BUFSZ];
594.         coord tmpcc;
595.         const char *firstmatch = "unknown";
596.         int sym = 0;
597. 
598.         any.a_int = i + 1;
599.         tmpcc.x = garr[i].x;
600.         tmpcc.y = garr[i].y;
601.         if (do_screen_description(tmpcc, TRUE, sym, tmpbuf,
602.                               &firstmatch, (struct permonst **)0)) {
603.             (void) coord_desc(garr[i].x, garr[i].y, tmpbuf,
604.                               iflags.getpos_coords);
605.             Sprintf(fullbuf, "%s%s%s", firstmatch,
606.                     (*tmpbuf ? " " : ""), tmpbuf);
607.             add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, fullbuf,
608.                      MENU_UNSELECTED);
609.         }
610.     }
611. 
612.     Sprintf(tmpbuf, "Pick %s%s%s",
613.             an(gloc_descr[gloc][1]),
614.             gloc_filtertxt[iflags.getloc_filter],
615.             iflags.getloc_travelmode ? " for travel destination" : "");
616.     end_menu(tmpwin, tmpbuf);
617.     pick_cnt = select_menu(tmpwin, PICK_ONE, &picks);
618.     destroy_nhwindow(tmpwin);
619.     if (pick_cnt > 0) {
620.         ccp->x = garr[picks->item.a_int - 1].x;
621.         ccp->y = garr[picks->item.a_int - 1].y;
622.         free((genericptr_t) picks);
623.     }
624.     free((genericptr_t) garr);
625.     return (pick_cnt > 0);
626. }
627. 
628. int
629. getpos(ccp, force, goal)
630. coord *ccp;
631. boolean force;
632. const char *goal;
633. {
634.     const char *cp;
635.     static struct {
636.         int nhkf, ret;
637.     } const pick_chars_def[] = {
638.         { NHKF_GETPOS_PICK, LOOK_TRADITIONAL },
639.         { NHKF_GETPOS_PICK_Q, LOOK_QUICK },
640.         { NHKF_GETPOS_PICK_O, LOOK_ONCE },
641.         { NHKF_GETPOS_PICK_V, LOOK_VERBOSE }
642.     };
643.     static const int mMoOdDxX_def[] = {
644.         NHKF_GETPOS_MON_NEXT,
645.         NHKF_GETPOS_MON_PREV,
646.         NHKF_GETPOS_OBJ_NEXT,
647.         NHKF_GETPOS_OBJ_PREV,
648.         NHKF_GETPOS_DOOR_NEXT,
649.         NHKF_GETPOS_DOOR_PREV,
650.         NHKF_GETPOS_UNEX_NEXT,
651.         NHKF_GETPOS_UNEX_PREV,
652.         NHKF_GETPOS_INTERESTING_NEXT,
653.         NHKF_GETPOS_INTERESTING_PREV,
654.         NHKF_GETPOS_VALID_NEXT,
655.         NHKF_GETPOS_VALID_PREV
656.     };
657.     char pick_chars[6];
658.     char mMoOdDxX[13];
659.     int result = 0;
660.     int cx, cy, i, c;
661.     int sidx, tx, ty;
662.     boolean msg_given = TRUE; /* clear message window by default */
663.     boolean show_goal_msg = FALSE;
664.     boolean hilite_state = FALSE;
665.     coord *garr[NUM_GLOCS] = DUMMY;
666.     int gcount[NUM_GLOCS] = DUMMY;
667.     int gidx[NUM_GLOCS] = DUMMY;
668. 
669.     for (i = 0; i < SIZE(pick_chars_def); i++)
670.         pick_chars[i] = Cmd.spkeys[pick_chars_def[i].nhkf];
671.     pick_chars[SIZE(pick_chars_def)] = '\0';
672. 
673.     for (i = 0; i < SIZE(mMoOdDxX_def); i++)
674.         mMoOdDxX[i] = Cmd.spkeys[mMoOdDxX_def[i]];
675.     mMoOdDxX[SIZE(mMoOdDxX_def)] = '\0';
676. 
677.     if (!goal)
678.         goal = "desired location";
679.     if (flags.verbose) {
680.         pline("(For instructions type a '%s')",
681.               visctrl(Cmd.spkeys[NHKF_GETPOS_HELP]));
682.         msg_given = TRUE;
683.     }
684.     cx = ccp->x;
685.     cy = ccp->y;
686. #ifdef CLIPPING
687.     cliparound(cx, cy);
688. #endif
689.     curs(WIN_MAP, cx, cy);
690.     flush_screen(0);
691. #ifdef MAC
692.     lock_mouse_cursor(TRUE);
693. #endif
694.     for (;;) {
695.         if (show_goal_msg) {
696.             pline("Move cursor to %s:", goal);
697.             curs(WIN_MAP, cx, cy);
698.             flush_screen(0);
699.             show_goal_msg = FALSE;
700.         } else if (iflags.autodescribe && !msg_given && !hilite_state) {
701.             auto_describe(cx, cy);
702.         }
703. 
704.         c = nh_poskey(&tx, &ty, &sidx);
705. 
706.         if (hilite_state) {
707.             (*getpos_hilitefunc)(2);
708.             hilite_state = FALSE;
709.             curs(WIN_MAP, cx, cy);
710.             flush_screen(0);
711.         }
712. 
713.         if (iflags.autodescribe)
714.             msg_given = FALSE;
715. 
716.         if (c == Cmd.spkeys[NHKF_ESC]) {
717.             cx = cy = -10;
718.             msg_given = TRUE; /* force clear */
719.             result = -1;
720.             break;
721.         }
722.         if (c == 0) {
723.             if (!isok(tx, ty))
724.                 continue;
725.             /* a mouse click event, just assign and return */
726.             cx = tx;
727.             cy = ty;
728.             break;
729.         }
730.         if ((cp = index(pick_chars, c)) != 0) {
731.             /* '.' => 0, ',' => 1, ';' => 2, ':' => 3 */
732.             result = pick_chars_def[(int) (cp - pick_chars)].ret;
733.             break;
734.         }
735.         for (i = 0; i < 8; i++) {
736.             int dx, dy;
737. 
738.             if (Cmd.dirchars[i] == c) {
739.                 /* a normal movement letter or digit */
740.                 dx = xdir[i];
741.                 dy = ydir[i];
742.             } else if (Cmd.alphadirchars[i] == lowc((char) c)
743.                        || (Cmd.num_pad && Cmd.dirchars[i] == (c & 0177))) {
744.                 /* a shifted movement letter or Meta-digit */
745.                 if (iflags.getloc_moveskip) {
746.                     /* skip same glyphs */
747.                     int glyph = glyph_at(cx, cy);
748. 
749.                     dx = xdir[i];
750.                     dy = ydir[i];
751.                     while (isok(cx + dx, cy + dy)
752.                            && glyph == glyph_at(cx + dx, cy + dy)
753.                            && isok(cx + dx + xdir[i], cy + dy + ydir[i])
754.                            && glyph == glyph_at(cx + dx + xdir[i],
755.                                                 cy + dy + ydir[i])) {
756.                         dx += xdir[i];
757.                         dy += ydir[i];
758.                     }
759.                 } else {
760.                     dx = 8 * xdir[i];
761.                     dy = 8 * ydir[i];
762.                 }
763.             } else
764.                 continue;
765. 
766.             /* truncate at map edge; diagonal moves complicate this... */
767.             if (cx + dx < 1) {
768.                 dy -= sgn(dy) * (1 - (cx + dx));
769.                 dx = 1 - cx; /* so that (cx+dx == 1) */
770.             } else if (cx + dx > COLNO - 1) {
771.                 dy += sgn(dy) * ((COLNO - 1) - (cx + dx));
772.                 dx = (COLNO - 1) - cx;
773.             }
774.             if (cy + dy < 0) {
775.                 dx -= sgn(dx) * (0 - (cy + dy));
776.                 dy = 0 - cy; /* so that (cy+dy == 0) */
777.             } else if (cy + dy > ROWNO - 1) {
778.                 dx += sgn(dx) * ((ROWNO - 1) - (cy + dy));
779.                 dy = (ROWNO - 1) - cy;
780.             }
781.             cx += dx;
782.             cy += dy;
783.             goto nxtc;
784.         }
785. 
786.         if (c == Cmd.spkeys[NHKF_GETPOS_HELP] || redraw_cmd(c)) {
787.             if (c == Cmd.spkeys[NHKF_GETPOS_HELP])
788.                 getpos_help(force, goal);
789.             else /* ^R */
790.                 docrt(); /* redraw */
791.             /* update message window to reflect that we're still targetting */
792.             show_goal_msg = TRUE;
793.             msg_given = TRUE;
794.         } else if (c == Cmd.spkeys[NHKF_GETPOS_SHOWVALID]
795.                    && getpos_hilitefunc) {
796.             if (!hilite_state) {
797.                 (*getpos_hilitefunc)(0);
798.                 (*getpos_hilitefunc)(1);
799.                 hilite_state = TRUE;
800.             }
801.             goto nxtc;
802.         } else if (c == Cmd.spkeys[NHKF_GETPOS_AUTODESC]) {
803.             iflags.autodescribe = !iflags.autodescribe;
804.             pline("Automatic description %sis %s.",
805.                   flags.verbose ? "of features under cursor " : "",
806.                   iflags.autodescribe ? "on" : "off");
807.             if (!iflags.autodescribe)
808.                 show_goal_msg = TRUE;
809.             msg_given = TRUE;
810.             goto nxtc;
811.         } else if (c == Cmd.spkeys[NHKF_GETPOS_LIMITVIEW]) {
812.             static const char *const view_filters[NUM_GFILTER] = {
813.                 "Not limiting targets",
814.                 "Limiting targets to those in sight",
815.                 "Limiting targets to those in same area"
816.             };
817. 
818.             iflags.getloc_filter = (iflags.getloc_filter + 1) % NUM_GFILTER;
819.             for (i = 0; i < NUM_GLOCS; i++) {
820.                 if (garr[i]) {
821.                     free((genericptr_t) garr[i]);
822.                     garr[i] = NULL;
823.                 }
824.                 gidx[i] = gcount[i] = 0;
825.             }
826.             pline("%s.", view_filters[iflags.getloc_filter]);
827.             msg_given = TRUE;
828.             goto nxtc;
829.         } else if (c == Cmd.spkeys[NHKF_GETPOS_MENU]) {
830.             iflags.getloc_usemenu = !iflags.getloc_usemenu;
831.             pline("%s a menu to show possible targets%s.",
832.                   iflags.getloc_usemenu ? "Using" : "Not using",
833.                   iflags.getloc_usemenu
834.                       ? " for 'm|M', 'o|O', 'd|D', and 'x|X'" : "");
835.             msg_given = TRUE;
836.             goto nxtc;
837.         } else if (c == Cmd.spkeys[NHKF_GETPOS_SELF]) {
838.             /* reset 'm&M', 'o&O', &c; otherwise, there's no way for player
839.                to achieve that except by manually cycling through all spots */
840.             for (i = 0; i < NUM_GLOCS; i++)
841.                 gidx[i] = 0;
842.             cx = u.ux;
843.             cy = u.uy;
844.             goto nxtc;
845.         } else if (c == Cmd.spkeys[NHKF_GETPOS_MOVESKIP]) {
846.             iflags.getloc_moveskip = !iflags.getloc_moveskip;
847.             pline("%skipping over similar terrain when fastmoving the cursor.",
848.                   iflags.getloc_moveskip ? "S" : "Not s");
849.         } else if ((cp = index(mMoOdDxX, c)) != 0) { /* 'm|M', 'o|O', &c */
850.             /* nearest or farthest monster or object or door or unexplored */
851.             int gtmp = (int) (cp - mMoOdDxX), /* 0..7 */
852.                 gloc = gtmp >> 1;             /* 0..3 */
853. 
854.             if (iflags.getloc_usemenu) {
855.                 coord tmpcrd;
856. 
857.                 if (getpos_menu(&tmpcrd, gloc)) {
858.                     cx = tmpcrd.x;
859.                     cy = tmpcrd.y;
860.                 }
861.                 goto nxtc;
862.             }
863. 
864.             if (!garr[gloc]) {
865.                 gather_locs(&garr[gloc], &gcount[gloc], gloc);
866.                 gidx[gloc] = 0; /* garr[][0] is hero's spot */
867.             }
868.             if (!(gtmp & 1)) {  /* c=='m' || c=='o' || c=='d' || c=='x') */
869.                 gidx[gloc] = (gidx[gloc] + 1) % gcount[gloc];
870.             } else {            /* c=='M' || c=='O' || c=='D' || c=='X') */
871.                 if (--gidx[gloc] < 0)
872.                     gidx[gloc] = gcount[gloc] - 1;
873.             }
874.             cx = garr[gloc][gidx[gloc]].x;
875.             cy = garr[gloc][gidx[gloc]].y;
876.             goto nxtc;
877.         } else {
878.             if (!index(quitchars, c)) {
879.                 char matching[MAXPCHARS];
880.                 int pass, lo_x, lo_y, hi_x, hi_y, k = 0;
881. 
882.                 (void) memset((genericptr_t) matching, 0, sizeof matching);
883.                 for (sidx = 1; sidx < MAXPCHARS; sidx++) { /* [0] left as 0 */
884.                     if (IS_DOOR(sidx) || IS_WALL(sidx)
885.                         || sidx == SDOOR || sidx == SCORR
886.                         || glyph_to_cmap(k) == S_room
887.                         || glyph_to_cmap(k) == S_darkroom
888.                         || glyph_to_cmap(k) == S_corr
889.                         || glyph_to_cmap(k) == S_litcorr)
890.                         continue;
891.                     if (c == defsyms[sidx].sym || c == (int) showsyms[sidx])
892.                         matching[sidx] = (char) ++k;
893.                 }
894.                 if (k) {
895.                     for (pass = 0; pass <= 1; pass++) {
896.                         /* pass 0: just past current pos to lower right;
897.                            pass 1: upper left corner to current pos */
898.                         lo_y = (pass == 0) ? cy : 0;
899.                         hi_y = (pass == 0) ? ROWNO - 1 : cy;
900.                         for (ty = lo_y; ty <= hi_y; ty++) {
901.                             lo_x = (pass == 0 && ty == lo_y) ? cx + 1 : 1;
902.                             hi_x = (pass == 1 && ty == hi_y) ? cx : COLNO - 1;
903.                             for (tx = lo_x; tx <= hi_x; tx++) {
904.                                 /* first, look at what is currently visible
905.                                    (might be monster) */
906.                                 k = glyph_at(tx, ty);
907.                                 if (glyph_is_cmap(k)
908.                                     && matching[glyph_to_cmap(k)])
909.                                     goto foundc;
910.                                 /* next, try glyph that's remembered here
911.                                    (might be trap or object) */
912.                                 if (level.flags.hero_memory
913.                                     /* !terrainmode: don't move to remembered
914.                                        trap or object if not currently shown */
915.                                     && !iflags.terrainmode) {
916.                                     k = levl[tx][ty].glyph;
917.                                     if (glyph_is_cmap(k)
918.                                         && matching[glyph_to_cmap(k)])
919.                                         goto foundc;
920.                                 }
921.                                 /* last, try actual terrain here (shouldn't
922.                                    we be using lastseentyp[][] instead?) */
923.                                 if (levl[tx][ty].seenv) {
924.                                     k = back_to_glyph(tx, ty);
925.                                     if (glyph_is_cmap(k)
926.                                         && matching[glyph_to_cmap(k)])
927.                                         goto foundc;
928.                                 }
929.                                 continue;
930.                             foundc:
931.                                 cx = tx, cy = ty;
932.                                 if (msg_given) {
933.                                     clear_nhwindow(WIN_MESSAGE);
934.                                     msg_given = FALSE;
935.                                 }
936.                                 goto nxtc;
937.                             } /* column */
938.                         }     /* row */
939.                     }         /* pass */
940.                     pline("Can't find dungeon feature '%c'.", c);
941.                     msg_given = TRUE;
942.                     goto nxtc;
943.                 } else {
944.                     char note[QBUFSZ];
945. 
946.                     if (!force)
947.                         Strcpy(note, "aborted");
948.                     else /* hjkl */
949.                         Sprintf(note, "use '%c', '%c', '%c', '%c' or '%s'",
950.                                 Cmd.move_W, Cmd.move_S, Cmd.move_N, Cmd.move_E,
951.                                 visctrl(Cmd.spkeys[NHKF_GETPOS_PICK]));
952.                     pline("Unknown direction: '%s' (%s).", visctrl((char) c),
953.                           note);
954.                     msg_given = TRUE;
955.                 } /* k => matching */
956.             }     /* !quitchars */
957.             if (force)
958.                 goto nxtc;
959.             pline("Done.");
960.             msg_given = FALSE; /* suppress clear */
961.             cx = -1;
962.             cy = 0;
963.             result = 0; /* not -1 */
964.             break;
965.         }
966.     nxtc:
967.         ;
968. #ifdef CLIPPING
969.         cliparound(cx, cy);
970. #endif
971.         curs(WIN_MAP, cx, cy);
972.         flush_screen(0);
973.     }
974. #ifdef MAC
975.     lock_mouse_cursor(FALSE);
976. #endif
977.     if (msg_given)
978.         clear_nhwindow(WIN_MESSAGE);
979.     ccp->x = cx;
980.     ccp->y = cy;
981.     for (i = 0; i < NUM_GLOCS; i++)
982.         if (garr[i])
983.             free((genericptr_t) garr[i]);
984.     getpos_hilitefunc = (void FDECL((*), (int))) 0;
985.     getpos_getvalid = (boolean FDECL((*), (int, int))) 0;
986.     return result;
987. }
988. 
989. /* allocate space for a monster's name; removes old name if there is one */
990. void
991. new_mname(mon, lth)
992. struct monst *mon;
993. int lth; /* desired length (caller handles adding 1 for terminator) */
994. {
995.     if (lth) {
996.         /* allocate mextra if necessary; otherwise get rid of old name */
997.         if (!mon->mextra)
998.             mon->mextra = newmextra();
999.         else
1000.             free_mname(mon); /* already has mextra, might also have name */
1001.         MNAME(mon) = (char *) alloc((unsigned) lth);
1002.     } else {
1003.         /* zero length: the new name is empty; get rid of the old name */
1004.         if (has_mname(mon))
1005.             free_mname(mon);
1006.     }
1007. }
1008. 
1009. /* release a monster's name; retains mextra even if all fields are now null */
1010. void
1011. free_mname(mon)
1012. struct monst *mon;
1013. {
1014.     if (has_mname(mon)) {
1015.         free((genericptr_t) MNAME(mon));
1016.         MNAME(mon) = (char *) 0;
1017.     }
1018. }
1019. 
1020. /* allocate space for an object's name; removes old name if there is one */
1021. void
1022. new_oname(obj, lth)
1023. struct obj *obj;
1024. int lth; /* desired length (caller handles adding 1 for terminator) */
1025. {
1026.     if (lth) {
1027.         /* allocate oextra if necessary; otherwise get rid of old name */
1028.         if (!obj->oextra)
1029.             obj->oextra = newoextra();
1030.         else
1031.             free_oname(obj); /* already has oextra, might also have name */
1032.         ONAME(obj) = (char *) alloc((unsigned) lth);
1033.     } else {
1034.         /* zero length: the new name is empty; get rid of the old name */
1035.         if (has_oname(obj))
1036.             free_oname(obj);
1037.     }
1038. }
1039. 
1040. /* release an object's name; retains oextra even if all fields are now null */
1041. void
1042. free_oname(obj)
1043. struct obj *obj;
1044. {
1045.     if (has_oname(obj)) {
1046.         free((genericptr_t) ONAME(obj));
1047.         ONAME(obj) = (char *) 0;
1048.     }
1049. }
1050. 
1051. /*  safe_oname() always returns a valid pointer to
1052.  *  a string, either the pointer to an object's name
1053.  *  if it has one, or a pointer to an empty string
1054.  *  if it doesn't.
1055.  */
1056. const char *
1057. safe_oname(obj)
1058. struct obj *obj;
1059. {
1060.     if (has_oname(obj))
1061.         return ONAME(obj);
1062.     return "";
1063. }
1064. 
1065. /* get a name for a monster or an object from player;
1066.    truncate if longer than PL_PSIZ, then return it */
1067. static char *
1068. name_from_player(outbuf, prompt, defres)
1069. char *outbuf;       /* output buffer, assumed to be at least BUFSZ long;
1070.                      * anything longer than PL_PSIZ will be truncated */
1071. const char *prompt;
1072. const char *defres; /* only used if EDIT_GETLIN is enabled; only useful
1073.                      * if windowport xxx's xxx_getlin() supports that */
1074. {
1075.     outbuf[0] = '\0';
1076. #ifdef EDIT_GETLIN
1077.     if (defres && *defres)
1078.         Strcpy(outbuf, defres); /* default response from getlin() */
1079. #else
1080.     nhUse(defres);
1081. #endif
1082.     getlin(prompt, outbuf);
1083.     if (!*outbuf || *outbuf == '\033')
1084.         return NULL;
1085. 
1086.     /* strip leading and trailing spaces, condense internal sequences */
1087.     (void) mungspaces(outbuf);
1088.     if (strlen(outbuf) >= PL_PSIZ)
1089.         outbuf[PL_PSIZ - 1] = '\0';
1090.     return outbuf;
1091. }
1092. 
1093. /* historical note: this returns a monster pointer because it used to
1094.    allocate a new bigger block of memory to hold the monster and its name */
1095. struct monst *
1096. christen_monst(mtmp, name)
1097. struct monst *mtmp;
1098. const char *name;
1099. {
1100.     int lth;
1101.     char buf[PL_PSIZ];
1102. 
1103.     /* dogname & catname are PL_PSIZ arrays; object names have same limit */
1104.     lth = (name && *name) ? ((int) strlen(name) + 1) : 0;
1105.     if (lth > PL_PSIZ) {
1106.         lth = PL_PSIZ;
1107.         name = strncpy(buf, name, PL_PSIZ - 1);
1108.         buf[PL_PSIZ - 1] = '\0';
1109.     }
1110.     new_mname(mtmp, lth); /* removes old name if one is present */
1111.     if (lth)
1112.         Strcpy(MNAME(mtmp), name);
1113.     return mtmp;
1114. }
1115. 
1116. /* check whether user-supplied name matches or nearly matches an unnameable
1117.    monster's name; if so, give an alternate reject message for do_mname() */
1118. STATIC_OVL boolean
1119. alreadynamed(mtmp, monnambuf, usrbuf)
1120. struct monst *mtmp;
1121. char *monnambuf, *usrbuf;
1122. {
1123.     char pronounbuf[10], *p;
1124. 
1125.     if (fuzzymatch(usrbuf, monnambuf, " -_", TRUE)
1126.         /* catch trying to name "the Oracle" as "Oracle" */
1127.         || (!strncmpi(monnambuf, "the ", 4)
1128.             && fuzzymatch(usrbuf, monnambuf + 4, " -_", TRUE))
1129.         /* catch trying to name "invisible Orcus" as "Orcus" */
1130.         || ((p = strstri(monnambuf, "invisible ")) != 0
1131.             && fuzzymatch(usrbuf, p + 10, " -_", TRUE))
1132.         /* catch trying to name "the {priest,Angel} of Crom" as "Crom" */
1133.         || ((p = strstri(monnambuf, " of ")) != 0
1134.             && fuzzymatch(usrbuf, p + 4, " -_", TRUE))) {
1135.         pline("%s is already called %s.",
1136.               upstart(strcpy(pronounbuf, mhe(mtmp))), monnambuf);
1137.         return TRUE;
1138.     } else if (mtmp->data == &mons[PM_JUIBLEX]
1139.                && strstri(monnambuf, "Juiblex")
1140.                && !strcmpi(usrbuf, "Jubilex")) {
1141.         pline("%s doesn't like being called %s.", upstart(monnambuf), usrbuf);
1142.         return TRUE;
1143.     }
1144.     return FALSE;
1145. }
1146. 
1147. /* allow player to assign a name to some chosen monster */
1148. STATIC_OVL void
1149. do_mname()
1150. {
1151.     char buf[BUFSZ], monnambuf[BUFSZ], qbuf[QBUFSZ];
1152.     coord cc;
1153.     int cx, cy;
1154.     struct monst *mtmp = 0;
1155. 
1156.     if (Hallucination) {
1157.         You("would never recognize it anyway.");
1158.         return;
1159.     }
1160.     cc.x = u.ux;
1161.     cc.y = u.uy;
1162.     if (getpos(&cc, FALSE, "the monster you want to name") < 0
1163.         || !isok(cc.x, cc.y))
1164.         return;
1165.     cx = cc.x, cy = cc.y;
1166. 
1167.     if (cx == u.ux && cy == u.uy) {
1168.         if (u.usteed && canspotmon(u.usteed)) {
1169.             mtmp = u.usteed;
1170.         } else {
1171.             pline("This %s creature is called %s and cannot be renamed.",
1172.                   beautiful(), plname);
1173.             return;
1174.         }
1175.     } else
1176.         mtmp = m_at(cx, cy);
1177. 
1178.     if (!mtmp
1179.         || (!sensemon(mtmp)
1180.             && (!(cansee(cx, cy) || see_with_infrared(mtmp))
1181.                 || mtmp->mundetected || M_AP_TYPE(mtmp) == M_AP_FURNITURE
1182.                 || M_AP_TYPE(mtmp) == M_AP_OBJECT
1183.                 || (mtmp->minvis && !See_invisible)))) {
1184.         pline("I see no monster there.");
1185.         return;
1186.     }
1187.     /* special case similar to the one in lookat() */
1188.     Sprintf(qbuf, "What do you want to call %s?",
1189.             distant_monnam(mtmp, ARTICLE_THE, monnambuf));
1190.     /* use getlin() to get a name string from the player */
1191.     if (!name_from_player(buf, qbuf, has_mname(mtmp) ? MNAME(mtmp) : NULL))
1192.         return;
1193. 
1194.     /* Unique monsters have their own specific names or titles.
1195.      * Shopkeepers, temple priests and other minions use alternate
1196.      * name formatting routines which ignore any user-supplied name.
1197.      *
1198.      * Don't say the name is being rejected if it happens to match
1199.      * the existing name.
1200.      *
1201.      * TODO: should have an alternate message when the attempt is to
1202.      * remove existing name without assigning a new one.
1203.      */
1204.     if ((mtmp->data->geno & G_UNIQ) && !mtmp->ispriest) {
1205.         if (!alreadynamed(mtmp, monnambuf, buf))
1206.             pline("%s doesn't like being called names!", upstart(monnambuf));
1207.     } else if (mtmp->isshk
1208.                && !(Deaf || mtmp->msleeping || !mtmp->mcanmove
1209.                     || mtmp->data->msound <= MS_ANIMAL)) {
1210.         if (!alreadynamed(mtmp, monnambuf, buf))
1211.             verbalize("I'm %s, not %s.", shkname(mtmp), buf);
1212.     } else if (mtmp->ispriest || mtmp->isminion || mtmp->isshk) {
1213.         if (!alreadynamed(mtmp, monnambuf, buf))
1214.             pline("%s will not accept the name %s.", upstart(monnambuf), buf);
1215.     } else
1216.         (void) christen_monst(mtmp, buf);
1217. }
1218. 
1219. STATIC_VAR int via_naming = 0;
1220. 
1221. /*
1222.  * This routine used to change the address of 'obj' so be unsafe if not
1223.  * used with extreme care.  Applying a name to an object no longer
1224.  * allocates a replacement object, so that old risk is gone.
1225.  */
1226. STATIC_OVL
1227. void
1228. do_oname(obj)
1229. register struct obj *obj;
1230. {
1231.     char *bufp, buf[BUFSZ], bufcpy[BUFSZ], qbuf[QBUFSZ];
1232.     const char *aname;
1233.     short objtyp;
1234. 
1235.     /* Do this now because there's no point in even asking for a name */
1236.     if (obj->otyp == SPE_NOVEL) {
1237.         pline("%s already has a published name.", Ysimple_name2(obj));
1238.         return;
1239.     }
1240. 
1241.     Sprintf(qbuf, "What do you want to name %s ",
1242.             is_plural(obj) ? "these" : "this");
1243.     (void) safe_qbuf(qbuf, qbuf, "?", obj, xname, simpleonames, "item");
1244.     /* use getlin() to get a name string from the player */
1245.     if (!name_from_player(buf, qbuf, safe_oname(obj)))
1246.         return;
1247. 
1248.     /*
1249.      * We don't violate illiteracy conduct here, although it is
1250.      * arguable that we should for anything other than "X".  Doing so
1251.      * would make attaching player's notes to hero's inventory have an
1252.      * in-game effect, which may or may not be the correct thing to do.
1253.      *
1254.      * We do violate illiteracy in oname() if player creates Sting or
1255.      * Orcrist, clearly being literate (no pun intended...).
1256.      */
1257. 
1258.     /* relax restrictions over proper capitalization for artifacts */
1259.     if ((aname = artifact_name(buf, &objtyp)) != 0 && objtyp == obj->otyp)
1260.         Strcpy(buf, aname);
1261. 
1262.     if (obj->oartifact) {
1263.         pline_The("artifact seems to resist the attempt.");
1264.         return;
1265.     } else if (restrict_name(obj, buf) || exist_artifact(obj->otyp, buf)) {
1266.         /* this used to change one letter, substituting a value
1267.            of 'a' through 'y' (due to an off by one error, 'z'
1268.            would never be selected) and then force that to
1269.            upper case if such was the case of the input;
1270.            now, the hand slip scuffs one or two letters as if
1271.            the text had been trodden upon, sometimes picking
1272.            punctuation instead of an arbitrary letter;
1273.            unfortunately, we have to cover the possibility of
1274.            it targetting spaces so failing to make any change
1275.            (we know that it must eventually target a nonspace
1276.            because buf[] matches a valid artifact name) */
1277.         Strcpy(bufcpy, buf);
1278.         /* for "the Foo of Bar", only scuff "Foo of Bar" part */
1279.         bufp = !strncmpi(bufcpy, "the ", 4) ? (buf + 4) : buf;
1280.         do {
1281.             wipeout_text(bufp, rn2_on_display_rng(2), (unsigned) 0);
1282.         } while (!strcmp(buf, bufcpy));
1283.         pline("While engraving, your %s slips.", body_part(HAND));
1284.         display_nhwindow(WIN_MESSAGE, FALSE);
1285.         You("engrave: \"%s\".", buf);
1286.         /* violate illiteracy conduct since hero attempted to write
1287.            a valid artifact name */
1288.         u.uconduct.literate++;
1289.     }
1290.     ++via_naming; /* This ought to be an argument rather than a static... */
1291.     obj = oname(obj, buf);
1292.     --via_naming; /* ...but oname() is used in a lot of places, so defer. */
1293. }
1294. 
1295. struct obj *
1296. oname(obj, name)
1297. struct obj *obj;
1298. const char *name;
1299. {
1300.     int lth;
1301.     char buf[PL_PSIZ];
1302. 
1303.     lth = *name ? (int) (strlen(name) + 1) : 0;
1304.     if (lth > PL_PSIZ) {
1305.         lth = PL_PSIZ;
1306.         name = strncpy(buf, name, PL_PSIZ - 1);
1307.         buf[PL_PSIZ - 1] = '\0';
1308.     }
1309.     /* If named artifact exists in the game, do not create another.
1310.      * Also trying to create an artifact shouldn't de-artifact
1311.      * it (e.g. Excalibur from prayer). In this case the object
1312.      * will retain its current name. */
1313.     if (obj->oartifact || (lth && exist_artifact(obj->otyp, name)))
1314.         return obj;
1315. 
1316.     new_oname(obj, lth); /* removes old name if one is present */
1317.     if (lth)
1318.         Strcpy(ONAME(obj), name);
1319. 
1320.     if (lth)
1321.         artifact_exists(obj, name, TRUE);
1322.     if (obj->oartifact) {
1323.         /* can't dual-wield with artifact as secondary weapon */
1324.         if (obj == uswapwep)
1325.             untwoweapon();
1326.         /* activate warning if you've just named your weapon "Sting" */
1327.         if (obj == uwep)
1328.             set_artifact_intrinsic(obj, TRUE, W_WEP);
1329.         /* if obj is owned by a shop, increase your bill */
1330.         if (obj->unpaid)
1331.             alter_cost(obj, 0L);
1332.         if (via_naming) {
1333.             /* violate illiteracy conduct since successfully wrote arti-name */
1334.             u.uconduct.literate++;
1335.         }
1336.     }
1337.     if (carried(obj))
1338.         update_inventory();
1339.     return obj;
1340. }
1341. 
1342. static NEARDATA const char callable[] = {
1343.     SCROLL_CLASS, POTION_CLASS, WAND_CLASS,  RING_CLASS, AMULET_CLASS,
1344.     GEM_CLASS,    SPBOOK_CLASS, ARMOR_CLASS, TOOL_CLASS, 0
1345. };
1346. 
1347. boolean
1348. objtyp_is_callable(i)
1349. int i;
1350. {
1351.     return (boolean) (objects[i].oc_uname
1352.                       || (OBJ_DESCR(objects[i])
1353.                           && index(callable, objects[i].oc_class)));
1354. }
1355. 
1356. /* C and #name commands - player can name monster or object or type of obj */
1357. int
1358. docallcmd()
1359. {
1360.     struct obj *obj;
1361.     winid win;
1362.     anything any;
1363.     menu_item *pick_list = 0;
1364.     char ch, allowall[2];
1365.     /* if player wants a,b,c instead of i,o when looting, do that here too */
1366.     boolean abc = flags.lootabc;
1367. 
1368.     win = create_nhwindow(NHW_MENU);
1369.     start_menu(win);
1370.     any = zeroany;
1371.     any.a_char = 'm'; /* group accelerator 'C' */
1372.     add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, 'C', ATR_NONE,
1373.              "a monster", MENU_UNSELECTED);
1374.     if (invent) {
1375.         /* we use y and n as accelerators so that we can accept user's
1376.            response keyed to old "name an individual object?" prompt */
1377.         any.a_char = 'i'; /* group accelerator 'y' */
1378.         add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, 'y', ATR_NONE,
1379.                  "a particular object in inventory", MENU_UNSELECTED);
1380.         any.a_char = 'o'; /* group accelerator 'n' */
1381.         add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, 'n', ATR_NONE,
1382.                  "the type of an object in inventory", MENU_UNSELECTED);
1383.     }
1384.     any.a_char = 'f'; /* group accelerator ',' (or ':' instead?) */
1385.     add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, ',', ATR_NONE,
1386.              "the type of an object upon the floor", MENU_UNSELECTED);
1387.     any.a_char = 'd'; /* group accelerator '\' */
1388.     add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, '\\', ATR_NONE,
1389.              "the type of an object on discoveries list", MENU_UNSELECTED);
1390.     any.a_char = 'a'; /* group accelerator 'l' */
1391.     add_menu(win, NO_GLYPH, &any, abc ? 0 : any.a_char, 'l', ATR_NONE,
1392.              "record an annotation for the current level", MENU_UNSELECTED);
1393.     end_menu(win, "What do you want to name?");
1394.     if (select_menu(win, PICK_ONE, &pick_list) > 0) {
1395.         ch = pick_list[0].item.a_char;
1396.         free((genericptr_t) pick_list);
1397.     } else
1398.         ch = 'q';
1399.     destroy_nhwindow(win);
1400. 
1401.     switch (ch) {
1402.     default:
1403.     case 'q':
1404.         break;
1405.     case 'm': /* name a visible monster */
1406.         do_mname();
1407.         break;
1408.     case 'i': /* name an individual object in inventory */
1409.         allowall[0] = ALL_CLASSES;
1410.         allowall[1] = '\0';
1411.         obj = getobj(allowall, "name");
1412.         if (obj)
1413.             do_oname(obj);
1414.         break;
1415.     case 'o': /* name a type of object in inventory */
1416.         obj = getobj(callable, "call");
1417.         if (obj) {
1418.             /* behave as if examining it in inventory;
1419.                this might set dknown if it was picked up
1420.                while blind and the hero can now see */
1421.             (void) xname(obj);
1422. 
1423.             if (!obj->dknown) {
1424.                 You("would never recognize another one.");
1425. #if 0
1426.             } else if (!objtyp_is_callable(obj->otyp)) {
1427.                 You("know those as well as you ever will.");
1428. #endif
1429.             } else {
1430.                 docall(obj);
1431.             }
1432.         }
1433.         break;
1434.     case 'f': /* name a type of object visible on the floor */
1435.         namefloorobj();
1436.         break;
1437.     case 'd': /* name a type of object on the discoveries list */
1438.         rename_disco();
1439.         break;
1440.     case 'a': /* annotate level */
1441.         donamelevel();
1442.         break;
1443.     }
1444.     return 0;
1445. }
1446. 
1447. /* for use by safe_qbuf() */
1448. STATIC_PTR char *
1449. docall_xname(obj)
1450. struct obj *obj;
1451. {
1452.     struct obj otemp;
1453. 
1454.     otemp = *obj;
1455.     otemp.oextra = (struct oextra *) 0;
1456.     otemp.quan = 1L;
1457.     /* in case water is already known, convert "[un]holy water" to "water" */
1458.     otemp.blessed = otemp.cursed = 0;
1459.     /* remove attributes that are doname() caliber but get formatted
1460.        by xname(); most of these fixups aren't really needed because the
1461.        relevant type of object isn't callable so won't reach this far */
1462.     if (otemp.oclass == WEAPON_CLASS)
1463.         otemp.opoisoned = 0; /* not poisoned */
1464.     else if (otemp.oclass == POTION_CLASS)
1465.         otemp.odiluted = 0; /* not diluted */
1466.     else if (otemp.otyp == TOWEL || otemp.otyp == STATUE)
1467.         otemp.spe = 0; /* not wet or historic */
1468.     else if (otemp.otyp == TIN)
1469.         otemp.known = 0; /* suppress tin type (homemade, &c) and mon type */
1470.     else if (otemp.otyp == FIGURINE)
1471.         otemp.corpsenm = NON_PM; /* suppress mon type */
1472.     else if (otemp.otyp == HEAVY_IRON_BALL)
1473.         otemp.owt = objects[HEAVY_IRON_BALL].oc_weight; /* not "very heavy" */
1474.     else if (otemp.oclass == FOOD_CLASS && otemp.globby)
1475.         otemp.owt = 120; /* 6*20, neither a small glob nor a large one */
1476. 
1477.     return an(xname(&otemp));
1478. }
1479. 
1480. void
1481. docall(obj)
1482. struct obj *obj;
1483. {
1484.     char buf[BUFSZ], qbuf[QBUFSZ];
1485.     char **str1;
1486. 
1487.     if (!obj->dknown)
1488.         return; /* probably blind */
1489.     flush_screen(1); /* buffered updates might matter to player's response */
1490. 
1491.     if (obj->oclass == POTION_CLASS && obj->fromsink)
1492.         /* kludge, meaning it's sink water */
1493.         Sprintf(qbuf, "Call a stream of %s fluid:",
1494.                 OBJ_DESCR(objects[obj->otyp]));
1495.     else
1496.         (void) safe_qbuf(qbuf, "Call ", ":", obj,
1497.                          docall_xname, simpleonames, "thing");
1498.     /* pointer to old name */
1499.     str1 = &(objects[obj->otyp].oc_uname);
1500.     /* use getlin() to get a name string from the player */
1501.     if (!name_from_player(buf, qbuf, *str1))
1502.         return;
1503. 
1504.     /* clear old name */
1505.     if (*str1)
1506.         free((genericptr_t) *str1);
1507. 
1508.     /* strip leading and trailing spaces; uncalls item if all spaces */
1509.     (void) mungspaces(buf);
1510.     if (!*buf) {
1511.         if (*str1) { /* had name, so possibly remove from disco[] */
1512.             /* strip name first, for the update_inventory() call
1513.                from undiscover_object() */
1514.             *str1 = (char *) 0;
1515.             undiscover_object(obj->otyp);
1516.         }
1517.     } else {
1518.         *str1 = dupstr(buf);
1519.         discover_object(obj->otyp, FALSE, TRUE); /* possibly add to disco[] */
1520.     }
1521. }
1522. 
1523. STATIC_OVL void
1524. namefloorobj()
1525. {
1526.     coord cc;
1527.     int glyph;
1528.     char buf[BUFSZ];
1529.     struct obj *obj = 0;
1530.     boolean fakeobj = FALSE, use_plural;
1531. 
1532.     cc.x = u.ux, cc.y = u.uy;
1533.     /* "dot for under/over you" only makes sense when the cursor hasn't
1534.        been moved off the hero's '@' yet, but there's no way to adjust
1535.        the help text once getpos() has started */
1536.     Sprintf(buf, "object on map (or '.' for one %s you)",
1537.             (u.uundetected && hides_under(youmonst.data)) ? "over" : "under");
1538.     if (getpos(&cc, FALSE, buf) < 0 || cc.x <= 0)
1539.         return;
1540.     if (cc.x == u.ux && cc.y == u.uy) {
1541.         obj = vobj_at(u.ux, u.uy);
1542.     } else {
1543.         glyph = glyph_at(cc.x, cc.y);
1544.         if (glyph_is_object(glyph))
1545.             fakeobj = object_from_map(glyph, cc.x, cc.y, &obj);
1546.         /* else 'obj' stays null */
1547.     }
1548.     if (!obj) {
1549.         /* "under you" is safe here since there's no object to hide under */
1550.         pline("There doesn't seem to be any object %s.",
1551.               (cc.x == u.ux && cc.y == u.uy) ? "under you" : "there");
1552.         return;
1553.     }
1554.     /* note well: 'obj' might be an instance of STRANGE_OBJECT if target
1555.        is a mimic; passing that to xname (directly or via simpleonames)
1556.        would yield "glorkum" so we need to handle it explicitly; it will
1557.        always fail the Hallucination test and pass the !callable test,
1558.        resulting in the "can't be assigned a type name" message */
1559.     Strcpy(buf, (obj->otyp != STRANGE_OBJECT)
1560.                  ? simpleonames(obj)
1561.                  : obj_descr[STRANGE_OBJECT].oc_name);
1562.     use_plural = (obj->quan > 1L);
1563.     if (Hallucination) {
1564.         const char *unames[6];
1565.         char tmpbuf[BUFSZ];
1566. 
1567.         /* straight role name */
1568.         unames[0] = ((Upolyd ? u.mfemale : flags.female) && urole.name.f)
1569.                      ? urole.name.f
1570.                      : urole.name.m;
1571.         /* random rank title for hero's role
1572. 
1573.            note: the 30 is hardcoded in xlev_to_rank, so should be
1574.            hardcoded here too */
1575.         unames[1] = rank_of(rn2_on_display_rng(30) + 1,
1576.                             Role_switch, flags.female);
1577.         /* random fake monster */
1578.         unames[2] = bogusmon(tmpbuf, (char *) 0);
1579.         /* increased chance for fake monster */
1580.         unames[3] = unames[2];
1581.         /* traditional */
1582.         unames[4] = roguename();
1583.         /* silly */
1584.         unames[5] = "Wibbly Wobbly";
1585.         pline("%s %s to call you \"%s.\"",
1586.               The(buf), use_plural ? "decide" : "decides",
1587.               unames[rn2_on_display_rng(SIZE(unames))]);
1588.     } else if (!objtyp_is_callable(obj->otyp)) {
1589.         pline("%s %s can't be assigned a type name.",
1590.               use_plural ? "Those" : "That", buf);
1591.     } else if (!obj->dknown) {
1592.         You("don't know %s %s well enough to name %s.",
1593.             use_plural ? "those" : "that", buf, use_plural ? "them" : "it");
1594.     } else {
1595.         docall(obj);
1596.     }
1597.     if (fakeobj) {
1598.         obj->where = OBJ_FREE; /* object_from_map() sets it to OBJ_FLOOR */
1599.         dealloc_obj(obj);
1600.     }
1601. }
1602. 
1603. static const char *const ghostnames[] = {
1604.     /* these names should have length < PL_NSIZ */
1605.     /* Capitalize the names for aesthetics -dgk */
1606.     "Adri",    "Andries",       "Andreas",     "Bert",    "David",  "Dirk",
1607.     "Emile",   "Frans",         "Fred",        "Greg",    "Hether", "Jay",
1608.     "John",    "Jon",           "Karnov",      "Kay",     "Kenny",  "Kevin",
1609.     "Maud",    "Michiel",       "Mike",        "Peter",   "Robert", "Ron",
1610.     "Tom",     "Wilmar",        "Nick Danger", "Phoenix", "Jiro",   "Mizue",
1611.     "Stephan", "Lance Braccus", "Shadowhawk"
1612. };
1613. 
1614. /* ghost names formerly set by x_monnam(), now by makemon() instead */
1615. const char *
1616. rndghostname()
1617. {
1618.     return rn2(7) ? ghostnames[rn2(SIZE(ghostnames))] : (const char *) plname;
1619. }
1620. 
1621. /*
1622.  * Monster naming functions:
1623.  * x_monnam is the generic monster-naming function.
1624.  *                seen        unseen       detected               named
1625.  * mon_nam:     the newt        it      the invisible orc       Fido
1626.  * noit_mon_nam:the newt (as if detected) the invisible orc     Fido
1627.  * l_monnam:    newt            it      invisible orc           dog called Fido
1628.  * Monnam:      The newt        It      The invisible orc       Fido
1629.  * noit_Monnam: The newt (as if detected) The invisible orc     Fido
1630.  * Adjmonnam:   The poor newt   It      The poor invisible orc  The poor Fido
1631.  * Amonnam:     A newt          It      An invisible orc        Fido
1632.  * a_monnam:    a newt          it      an invisible orc        Fido
1633.  * m_monnam:    newt            xan     orc                     Fido
1634.  * y_monnam:    your newt     your xan  your invisible orc      Fido
1635.  * noname_monnam(mon,article):
1636.  *              article newt    art xan art invisible orc       art dog
1637.  */
1638. 
1639. /* Bug: if the monster is a priest or shopkeeper, not every one of these
1640.  * options works, since those are special cases.
1641.  */
1642. char *
1643. x_monnam(mtmp, article, adjective, suppress, called)
1644. register struct monst *mtmp;
1645. int article;
1646. /* ARTICLE_NONE, ARTICLE_THE, ARTICLE_A: obvious
1647.  * ARTICLE_YOUR: "your" on pets, "the" on everything else
1648.  *
1649.  * If the monster would be referred to as "it" or if the monster has a name
1650.  * _and_ there is no adjective, "invisible", "saddled", etc., override this
1651.  * and always use no article.
1652.  */
1653. const char *adjective;
1654. int suppress;
1655. /* SUPPRESS_IT, SUPPRESS_INVISIBLE, SUPPRESS_HALLUCINATION, SUPPRESS_SADDLE.
1656.  * EXACT_NAME: combination of all the above
1657.  * SUPPRESS_NAME: omit monster's assigned name (unless uniq w/ pname).
1658.  */
1659. boolean called;
1660. {
1661.     char *buf = nextmbuf();
1662.     struct permonst *mdat = mtmp->data;
1663.     const char *pm_name = mdat->mname;
1664.     boolean do_hallu, do_invis, do_it, do_saddle, do_name;
1665.     boolean name_at_start, has_adjectives;
1666.     char *bp;
1667. 
1668.     if (program_state.gameover)
1669.         suppress |= SUPPRESS_HALLUCINATION;
1670.     if (article == ARTICLE_YOUR && !mtmp->mtame)
1671.         article = ARTICLE_THE;
1672. 
1673.     do_hallu = Hallucination && !(suppress & SUPPRESS_HALLUCINATION);
1674.     do_invis = mtmp->minvis && !(suppress & SUPPRESS_INVISIBLE);
1675.     do_it = !canspotmon(mtmp) && article != ARTICLE_YOUR
1676.             && !program_state.gameover && mtmp != u.usteed
1677.             && !(u.uswallow && mtmp == u.ustuck) && !(suppress & SUPPRESS_IT);
1678.     do_saddle = !(suppress & SUPPRESS_SADDLE);
1679.     do_name = !(suppress & SUPPRESS_NAME) || type_is_pname(mdat);
1680. 
1681.     buf[0] = '\0';
1682. 
1683.     /* unseen monsters, etc.  Use "it" */
1684.     if (do_it) {
1685.         Strcpy(buf, "it");
1686.         return buf;
1687.     }
1688. 
1689.     /* priests and minions: don't even use this function */
1690.     if (mtmp->ispriest || mtmp->isminion) {
1691.         char priestnambuf[BUFSZ];
1692.         char *name;
1693.         long save_prop = EHalluc_resistance;
1694.         unsigned save_invis = mtmp->minvis;
1695. 
1696.         /* when true name is wanted, explicitly block Hallucination */
1697.         if (!do_hallu)
1698.             EHalluc_resistance = 1L;
1699.         if (!do_invis)
1700.             mtmp->minvis = 0;
1701.         name = priestname(mtmp, priestnambuf);
1702.         EHalluc_resistance = save_prop;
1703.         mtmp->minvis = save_invis;
1704.         if (article == ARTICLE_NONE && !strncmp(name, "the ", 4))
1705.             name += 4;
1706.         return strcpy(buf, name);
1707.     }
1708.     /* an "aligned priest" not flagged as a priest or minion should be
1709.        "priest" or "priestess" (normally handled by priestname()) */
1710.     if (mdat == &mons[PM_ALIGNED_PRIEST])
1711.         pm_name = mtmp->female ? "priestess" : "priest";
1712.     else if (mdat == &mons[PM_HIGH_PRIEST] && mtmp->female)
1713.         pm_name = "high priestess";
1714. 
1715.     /* Shopkeepers: use shopkeeper name.  For normal shopkeepers, just
1716.      * "Asidonhopo"; for unusual ones, "Asidonhopo the invisible
1717.      * shopkeeper" or "Asidonhopo the blue dragon".  If hallucinating,
1718.      * none of this applies.
1719.      */
1720.     if (mtmp->isshk && !do_hallu) {
1721.         if (adjective && article == ARTICLE_THE) {
1722.             /* pathological case: "the angry Asidonhopo the blue dragon"
1723.                sounds silly */
1724.             Strcpy(buf, "the ");
1725.             Strcat(strcat(buf, adjective), " ");
1726.             Strcat(buf, shkname(mtmp));
1727.             return buf;
1728.         }
1729.         Strcat(buf, shkname(mtmp));
1730.         if (mdat == &mons[PM_SHOPKEEPER] && !do_invis)
1731.             return buf;
1732.         Strcat(buf, " the ");
1733.         if (do_invis)
1734.             Strcat(buf, "invisible ");
1735.         Strcat(buf, pm_name);
1736.         return buf;
1737.     }
1738. 
1739.     /* Put the adjectives in the buffer */
1740.     if (adjective)
1741.         Strcat(strcat(buf, adjective), " ");
1742.     if (do_invis)
1743.         Strcat(buf, "invisible ");
1744.     if (do_saddle && (mtmp->misc_worn_check & W_SADDLE) && !Blind
1745.         && !Hallucination)
1746.         Strcat(buf, "saddled ");
1747.     has_adjectives = (buf[0] != '\0');
1748. 
1749.     /* Put the actual monster name or type into the buffer now.
1750.        Remember whether the buffer starts with a personal name. */
1751.     if (do_hallu) {
1752.         char rnamecode;
1753.         char *rname = rndmonnam(&rnamecode);
1754. 
1755.         Strcat(buf, rname);
1756.         name_at_start = bogon_is_pname(rnamecode);
1757.     } else if (do_name && has_mname(mtmp)) {
1758.         char *name = MNAME(mtmp);
1759. 
1760.         if (mdat == &mons[PM_GHOST]) {
1761.             Sprintf(eos(buf), "%s ghost", s_suffix(name));
1762.             name_at_start = TRUE;
1763.         } else if (called) {
1764.             Sprintf(eos(buf), "%s called %s", pm_name, name);
1765.             name_at_start = (boolean) type_is_pname(mdat);
1766.         } else if (is_mplayer(mdat) && (bp = strstri(name, " the ")) != 0) {
1767.             /* <name> the <adjective> <invisible> <saddled> <rank> */
1768.             char pbuf[BUFSZ];
1769. 
1770.             Strcpy(pbuf, name);
1771.             pbuf[bp - name + 5] = '\0'; /* adjectives right after " the " */
1772.             if (has_adjectives)
1773.                 Strcat(pbuf, buf);
1774.             Strcat(pbuf, bp + 5); /* append the rest of the name */
1775.             Strcpy(buf, pbuf);
1776.             article = ARTICLE_NONE;
1777.             name_at_start = TRUE;
1778.         } else {
1779.             Strcat(buf, name);
1780.             name_at_start = TRUE;
1781.         }
1782.     } else if (is_mplayer(mdat) && !In_endgame(&u.uz)) {
1783.         char pbuf[BUFSZ];
1784. 
1785.         Strcpy(pbuf, rank_of((int) mtmp->m_lev, monsndx(mdat),
1786.                              (boolean) mtmp->female));
1787.         Strcat(buf, lcase(pbuf));
1788.         name_at_start = FALSE;
1789.     } else {
1790.         Strcat(buf, pm_name);
1791.         name_at_start = (boolean) type_is_pname(mdat);
1792.     }
1793. 
1794.     if (name_at_start && (article == ARTICLE_YOUR || !has_adjectives)) {
1795.         if (mdat == &mons[PM_WIZARD_OF_YENDOR])
1796.             article = ARTICLE_THE;
1797.         else
1798.             article = ARTICLE_NONE;
1799.     } else if ((mdat->geno & G_UNIQ) && article == ARTICLE_A) {
1800.         article = ARTICLE_THE;
1801.     }
1802. 
1803.     {
1804.         char buf2[BUFSZ];
1805. 
1806.         switch (article) {
1807.         case ARTICLE_YOUR:
1808.             Strcpy(buf2, "your ");
1809.             Strcat(buf2, buf);
1810.             Strcpy(buf, buf2);
1811.             return buf;
1812.         case ARTICLE_THE:
1813.             Strcpy(buf2, "the ");
1814.             Strcat(buf2, buf);
1815.             Strcpy(buf, buf2);
1816.             return buf;
1817.         case ARTICLE_A:
1818.             return an(buf);
1819.         case ARTICLE_NONE:
1820.         default:
1821.             return buf;
1822.         }
1823.     }
1824. }
1825. 
1826. char *
1827. l_monnam(mtmp)
1828. struct monst *mtmp;
1829. {
1830.     return x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1831.                     (has_mname(mtmp)) ? SUPPRESS_SADDLE : 0, TRUE);
1832. }
1833. 
1834. char *
1835. mon_nam(mtmp)
1836. struct monst *mtmp;
1837. {
1838.     return x_monnam(mtmp, ARTICLE_THE, (char *) 0,
1839.                     (has_mname(mtmp)) ? SUPPRESS_SADDLE : 0, FALSE);
1840. }
1841. 
1842. /* print the name as if mon_nam() was called, but assume that the player
1843.  * can always see the monster--used for probing and for monsters aggravating
1844.  * the player with a cursed potion of invisibility
1845.  */
1846. char *
1847. noit_mon_nam(mtmp)
1848. struct monst *mtmp;
1849. {
1850.     return x_monnam(mtmp, ARTICLE_THE, (char *) 0,
1851.                     (has_mname(mtmp)) ? (SUPPRESS_SADDLE | SUPPRESS_IT)
1852.                                       : SUPPRESS_IT,
1853.                     FALSE);
1854. }
1855. 
1856. char *
1857. Monnam(mtmp)
1858. struct monst *mtmp;
1859. {
1860.     register char *bp = mon_nam(mtmp);
1861. 
1862.     *bp = highc(*bp);
1863.     return  bp;
1864. }
1865. 
1866. char *
1867. noit_Monnam(mtmp)
1868. struct monst *mtmp;
1869. {
1870.     register char *bp = noit_mon_nam(mtmp);
1871. 
1872.     *bp = highc(*bp);
1873.     return  bp;
1874. }
1875. 
1876. /* return "a dog" rather than "Fido", honoring hallucination and visibility */
1877. char *
1878. noname_monnam(mtmp, article)
1879. struct monst *mtmp;
1880. int article;
1881. {
1882.     return x_monnam(mtmp, article, (char *) 0, SUPPRESS_NAME, FALSE);
1883. }
1884. 
1885. /* monster's own name -- overrides hallucination and [in]visibility
1886.    so shouldn't be used in ordinary messages (mainly for disclosure) */
1887. char *
1888. m_monnam(mtmp)
1889. struct monst *mtmp;
1890. {
1891.     return x_monnam(mtmp, ARTICLE_NONE, (char *) 0, EXACT_NAME, FALSE);
1892. }
1893. 
1894. /* pet name: "your little dog" */
1895. char *
1896. y_monnam(mtmp)
1897. struct monst *mtmp;
1898. {
1899.     int prefix, suppression_flag;
1900. 
1901.     prefix = mtmp->mtame ? ARTICLE_YOUR : ARTICLE_THE;
1902.     suppression_flag = (has_mname(mtmp)
1903.                         /* "saddled" is redundant when mounted */
1904.                         || mtmp == u.usteed)
1905.                            ? SUPPRESS_SADDLE
1906.                            : 0;
1907. 
1908.     return x_monnam(mtmp, prefix, (char *) 0, suppression_flag, FALSE);
1909. }
1910. 
1911. char *
1912. Adjmonnam(mtmp, adj)
1913. struct monst *mtmp;
1914. const char *adj;
1915. {
1916.     char *bp = x_monnam(mtmp, ARTICLE_THE, adj,
1917.                         has_mname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE);
1918. 
1919.     *bp = highc(*bp);
1920.     return  bp;
1921. }
1922. 
1923. char *
1924. a_monnam(mtmp)
1925. struct monst *mtmp;
1926. {
1927.     return x_monnam(mtmp, ARTICLE_A, (char *) 0,
1928.                     has_mname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE);
1929. }
1930. 
1931. char *
1932. Amonnam(mtmp)
1933. struct monst *mtmp;
1934. {
1935.     char *bp = a_monnam(mtmp);
1936. 
1937.     *bp = highc(*bp);
1938.     return  bp;
1939. }
1940. 
1941. /* used for monster ID by the '/', ';', and 'C' commands to block remote
1942.    identification of the endgame altars via their attending priests */
1943. char *
1944. distant_monnam(mon, article, outbuf)
1945. struct monst *mon;
1946. int article; /* only ARTICLE_NONE and ARTICLE_THE are handled here */
1947. char *outbuf;
1948. {
1949.     /* high priest(ess)'s identity is concealed on the Astral Plane,
1950.        unless you're adjacent (overridden for hallucination which does
1951.        its own obfuscation) */
1952.     if (mon->data == &mons[PM_HIGH_PRIEST] && !Hallucination
1953.         && Is_astralevel(&u.uz) && distu(mon->mx, mon->my) > 2) {
1954.         Strcpy(outbuf, article == ARTICLE_THE ? "the " : "");
1955.         Strcat(outbuf, mon->female ? "high priestess" : "high priest");
1956.     } else {
1957.         Strcpy(outbuf, x_monnam(mon, article, (char *) 0, 0, TRUE));
1958.     }
1959.     return outbuf;
1960. }
1961. 
1962. /* returns mon_nam(mon) relative to other_mon; normal name unless they're
1963.    the same, in which case the reference is to {him|her|it} self */
1964. char *
1965. mon_nam_too(mon, other_mon)
1966. struct monst *mon, *other_mon;
1967. {
1968.     char *outbuf;
1969. 
1970.     if (mon != other_mon) {
1971.         outbuf = mon_nam(mon);
1972.     } else {
1973.         outbuf = nextmbuf();
1974.         switch (pronoun_gender(mon, FALSE)) {
1975.         case 0:
1976.             Strcpy(outbuf, "himself");
1977.             break;
1978.         case 1:
1979.             Strcpy(outbuf, "herself");
1980.             break;
1981.         default:
1982.             Strcpy(outbuf, "itself");
1983.             break;
1984.         }
1985.     }
1986.     return outbuf;
1987. }
1988. 
1989. /* for debugging messages, where data might be suspect and we aren't
1990.    taking what the hero does or doesn't know into consideration */
1991. char *
1992. minimal_monnam(mon, ckloc)
1993. struct monst *mon;
1994. boolean ckloc;
1995. {
1996.     struct permonst *ptr;
1997.     char *outbuf = nextmbuf();
1998. 
1999.     if (!mon) {
2000.         Strcpy(outbuf, "[Null monster]");
2001.     } else if ((ptr = mon->data) == 0) {
2002.         Strcpy(outbuf, "[Null mon->data]");
2003.     } else if (ptr < &mons[0]) {
2004.         Sprintf(outbuf, "[Invalid mon->data %s < %s]",
2005.                 fmt_ptr((genericptr_t) mon->data),
2006.                 fmt_ptr((genericptr_t) &mons[0]));
2007.     } else if (ptr >= &mons[NUMMONS]) {
2008.         Sprintf(outbuf, "[Invalid mon->data %s >= %s]",
2009.                 fmt_ptr((genericptr_t) mon->data),
2010.                 fmt_ptr((genericptr_t) &mons[NUMMONS]));
2011.     } else if (ckloc && ptr == &mons[PM_LONG_WORM]
2012.                && level.monsters[mon->mx][mon->my] != mon) {
2013.         Sprintf(outbuf, "%s <%d,%d>",
2014.                 mons[PM_LONG_WORM_TAIL].mname, mon->mx, mon->my);
2015.     } else {
2016.         Sprintf(outbuf, "%s%s <%d,%d>",
2017.                 mon->mtame ? "tame " : mon->mpeaceful ? "peaceful " : "",
2018.                 mon->data->mname, mon->mx, mon->my);
2019.         if (mon->cham != NON_PM)
2020.             Sprintf(eos(outbuf), "{%s}", mons[mon->cham].mname);
2021.     }
2022.     return outbuf;
2023. }
2024. 
2025. /* fake monsters used to be in a hard-coded array, now in a data file */
2026. STATIC_OVL char *
2027. bogusmon(buf, code)
2028. char *buf, *code;
2029. {
2030.     static const char bogon_codes[] = "-_+|="; /* see dat/bonusmon.txt */
2031.     char *mname = buf;
2032. 
2033.     if (code)
2034.         *code = '\0';
2035.     /* might fail (return empty buf[]) if the file isn't available */
2036.     get_rnd_text(BOGUSMONFILE, buf, rn2_on_display_rng);
2037.     if (!*mname) {
2038.         Strcpy(buf, "bogon");
2039.     } else if (index(bogon_codes, *mname)) { /* strip prefix if present */
2040.         if (code)
2041.             *code = *mname;
2042.         ++mname;
2043.     }
2044.     return mname;
2045. }
2046. 
2047. /* return a random monster name, for hallucination */
2048. char *
2049. rndmonnam(code)
2050. char *code;
2051. {
2052.     static char buf[BUFSZ];
2053.     char *mname;
2054.     int name;
2055. #define BOGUSMONSIZE 100 /* arbitrary */
2056. 
2057.     if (code)
2058.         *code = '\0';
2059. 
2060.     do {
2061.         name = rn2_on_display_rng(SPECIAL_PM + BOGUSMONSIZE - LOW_PM) + LOW_PM;
2062.     } while (name < SPECIAL_PM
2063.              && (type_is_pname(&mons[name]) || (mons[name].geno & G_NOGEN)));
2064. 
2065.     if (name >= SPECIAL_PM) {
2066.         mname = bogusmon(buf, code);
2067.     } else {
2068.         mname = strcpy(buf, mons[name].mname);
2069.     }
2070.     return mname;
2071. #undef BOGUSMONSIZE
2072. }
2073. 
2074. /* check bogusmon prefix to decide whether it's a personal name */
2075. boolean
2076. bogon_is_pname(code)
2077. char code;
2078. {
2079.     if (!code)
2080.         return FALSE;
2081.     return index("-+=", code) ? TRUE : FALSE;
2082. }
2083. 
2084. /* name of a Rogue player */
2085. const char *
2086. roguename()
2087. {
2088.     char *i, *opts;
2089. 
2090.     if ((opts = nh_getenv("ROGUEOPTS")) != 0) {
2091.         for (i = opts; *i; i++)
2092.             if (!strncmp("name=", i, 5)) {
2093.                 char *j;
2094.                 if ((j = index(i + 5, ',')) != 0)
2095.                     *j = (char) 0;
2096.                 return i + 5;
2097.             }
2098.     }
2099.     return rn2(3) ? (rn2(2) ? "Michael Toy" : "Kenneth Arnold")
2100.                   : "Glenn Wichman";
2101. }
2102. 
2103. static NEARDATA const char *const hcolors[] = {
2104.     "ultraviolet", "infrared", "bluish-orange", "reddish-green", "dark white",
2105.     "light black", "sky blue-pink", "salty", "sweet", "sour", "bitter",
2106.     "striped", "spiral", "swirly", "plaid", "checkered", "argyle", "paisley",
2107.     "blotchy", "guernsey-spotted", "polka-dotted", "square", "round",
2108.     "triangular", "cabernet", "sangria", "fuchsia", "wisteria", "lemon-lime",
2109.     "strawberry-banana", "peppermint", "romantic", "incandescent",
2110.     "octarine", /* Discworld: the Colour of Magic */
2111. };
2112. 
2113. const char *
2114. hcolor(colorpref)
2115. const char *colorpref;
2116. {
2117.     return (Hallucination || !colorpref)
2118.         ? hcolors[rn2_on_display_rng(SIZE(hcolors))]
2119.         : colorpref;
2120. }
2121. 
2122. /* return a random real color unless hallucinating */
2123. const char *
2124. rndcolor()
2125. {
2126.     int k = rn2(CLR_MAX);
2127. 
2128.     return Hallucination ? hcolor((char *) 0)
2129.                          : (k == NO_COLOR) ? "colorless"
2130.                                            : c_obj_colors[k];
2131. }
2132. 
2133. static NEARDATA const char *const hliquids[] = {
2134.     "yoghurt", "oobleck", "clotted blood", "diluted water", "purified water",
2135.     "instant coffee", "tea", "herbal infusion", "liquid rainbow",
2136.     "creamy foam", "mulled wine", "bouillon", "nectar", "grog", "flubber",
2137.     "ketchup", "slow light", "oil", "vinaigrette", "liquid crystal", "honey",
2138.     "caramel sauce", "ink", "aqueous humour", "milk substitute",
2139.     "fruit juice", "glowing lava", "gastric acid", "mineral water",
2140.     "cough syrup", "quicksilver", "sweet vitriol", "grey goo", "pink slime",
2141. };
2142. 
2143. const char *
2144. hliquid(liquidpref)
2145. const char *liquidpref;
2146. {
2147.     return (Hallucination || !liquidpref) ? hliquids[rn2(SIZE(hliquids))]
2148.                                           : liquidpref;
2149. }
2150. 
2151. /* Aliases for road-runner nemesis
2152.  */
2153. static const char *const coynames[] = {
2154.     "Carnivorous Vulgaris", "Road-Runnerus Digestus", "Eatibus Anythingus",
2155.     "Famishus-Famishus", "Eatibus Almost Anythingus", "Eatius Birdius",
2156.     "Famishius Fantasticus", "Eternalii Famishiis", "Famishus Vulgarus",
2157.     "Famishius Vulgaris Ingeniusi", "Eatius-Slobbius", "Hardheadipus Oedipus",
2158.     "Carnivorous Slobbius", "Hard-Headipus Ravenus", "Evereadii Eatibus",
2159.     "Apetitius Giganticus", "Hungrii Flea-Bagius", "Overconfidentii Vulgaris",
2160.     "Caninus Nervous Rex", "Grotesques Appetitus", "Nemesis Ridiculii",
2161.     "Canis latrans"
2162. };
2163. 
2164. char *
2165. coyotename(mtmp, buf)
2166. struct monst *mtmp;
2167. char *buf;
2168. {
2169.     if (mtmp && buf) {
2170.         Sprintf(buf, "%s - %s",
2171.                 x_monnam(mtmp, ARTICLE_NONE, (char *) 0, 0, TRUE),
2172.                 mtmp->mcan ? coynames[SIZE(coynames) - 1]
2173.                            : coynames[mtmp->m_id % (SIZE(coynames) - 1)]);
2174.     }
2175.     return buf;
2176. }
2177. 
2178. char *
2179. rndorcname(s)
2180. char *s;
2181. {
2182.     static const char *v[] = { "a", "ai", "og", "u" };
2183.     static const char *snd[] = { "gor", "gris", "un", "bane", "ruk",
2184.                                  "oth","ul", "z", "thos","akh","hai" };
2185.     int i, iend = rn1(2, 3), vstart = rn2(2);
2186. 
2187.     if (s) {
2188.         *s = '\0';
2189.         for (i = 0; i < iend; ++i) {
2190.             vstart = 1 - vstart;                /* 0 -> 1, 1 -> 0 */
2191.             Sprintf(eos(s), "%s%s", (i > 0 && !rn2(30)) ? "-" : "",
2192.                     vstart ? v[rn2(SIZE(v))] : snd[rn2(SIZE(snd))]);
2193.         }
2194.     }
2195.     return s;
2196. }
2197. 
2198. struct monst *
2199. christen_orc(mtmp, gang, other)
2200. struct monst *mtmp;
2201. const char *gang, *other;
2202. {
2203.     int sz = 0;
2204.     char buf[BUFSZ], buf2[BUFSZ], *orcname;
2205. 
2206.     orcname = rndorcname(buf2);
2207.     sz = (int) strlen(orcname);
2208.     if (gang)
2209.         sz += (int) (strlen(gang) + sizeof " of " - sizeof "");
2210.     else if (other)
2211.         sz += (int) strlen(other);
2212. 
2213.     if (sz < BUFSZ) {
2214.         char gbuf[BUFSZ];
2215.         boolean nameit = FALSE;
2216. 
2217.         if (gang && orcname) {
2218.             Sprintf(buf, "%s of %s", upstart(orcname),
2219.                     upstart(strcpy(gbuf, gang)));
2220.             nameit = TRUE;
2221.         } else if (other && orcname) {
2222.             Sprintf(buf, "%s%s", upstart(orcname), other);
2223.             nameit = TRUE;
2224.         }
2225.         if (nameit)
2226.             mtmp = christen_monst(mtmp, buf);
2227.     }
2228.     return mtmp;
2229. }
2230. 
2231. /* make sure "The Colour of Magic" remains the first entry in here */
2232. static const char *const sir_Terry_novels[] = {
2233.     "The Colour of Magic", "The Light Fantastic", "Equal Rites", "Mort",
2234.     "Sourcery", "Wyrd Sisters", "Pyramids", "Guards! Guards!", "Eric",
2235.     "Moving Pictures", "Reaper Man", "Witches Abroad", "Small Gods",
2236.     "Lords and Ladies", "Men at Arms", "Soul Music", "Interesting Times",
2237.     "Maskerade", "Feet of Clay", "Hogfather", "Jingo", "The Last Continent",
2238.     "Carpe Jugulum", "The Fifth Elephant", "The Truth", "Thief of Time",
2239.     "The Last Hero", "The Amazing Maurice and His Educated Rodents",
2240.     "Night Watch", "The Wee Free Men", "Monstrous Regiment",
2241.     "A Hat Full of Sky", "Going Postal", "Thud!", "Wintersmith",
2242.     "Making Money", "Unseen Academicals", "I Shall Wear Midnight", "Snuff",
2243.     "Raising Steam", "The Shepherd's Crown"
2244. };
2245. 
2246. const char *
2247. noveltitle(novidx)
2248. int *novidx;
2249. {
2250.     int j, k = SIZE(sir_Terry_novels);
2251. 
2252.     j = rn2(k);
2253.     if (novidx) {
2254.         if (*novidx == -1)
2255.             *novidx = j;
2256.         else if (*novidx >= 0 && *novidx < k)
2257.             j = *novidx;
2258.     }
2259.     return sir_Terry_novels[j];
2260. }
2261. 
2262. const char *
2263. lookup_novel(lookname, idx)
2264. const char *lookname;
2265. int *idx;
2266. {
2267.     int k;
2268. 
2269.     /* Take American or U.K. spelling of this one */
2270.     if (!strcmpi(The(lookname), "The Color of Magic"))
2271.         lookname = sir_Terry_novels[0];
2272. 
2273.     for (k = 0; k < SIZE(sir_Terry_novels); ++k) {
2274.         if (!strcmpi(lookname, sir_Terry_novels[k])
2275.             || !strcmpi(The(lookname), sir_Terry_novels[k])) {
2276.             if (idx)
2277.                 *idx = k;
2278.             return sir_Terry_novels[k];
2279.         }
2280.     }
2281.     /* name not found; if novelidx is already set, override the name */
2282.     if (idx && *idx >= 0 && *idx < SIZE(sir_Terry_novels))
2283.         return sir_Terry_novels[*idx];
2284. 
2285.     return (const char *) 0;
2286. }
2287. 
2288. /*do_name.c*/