Source:NetHack 5.0.0/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 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/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 5.0	do_name.c	$NHDT-Date: 1737013431 2025/01/15 23:43:51 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.326 $ */
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. staticfn char *nextmbuf(void);
9. staticfn char *name_from_player(char *, const char *, const char *);
10. staticfn void do_mgivenname(void);
11. staticfn boolean alreadynamed(struct monst *, char *, char *) NONNULLPTRS;
12. staticfn void do_oname(struct obj *) NONNULLARG1;
13. staticfn char *docall_xname(struct obj *) NONNULLARG1;
14. staticfn void namefloorobj(void);
15. 
16. #define NUMMBUF 5
17. 
18. /* manage a pool of BUFSZ buffers, so callers don't have to */
19. staticfn char *
20. nextmbuf(void)
21. {
22.     static char NEARDATA bufs[NUMMBUF][BUFSZ];
23.     static int bufidx = 0;
24. 
25.     bufidx = (bufidx + 1) % NUMMBUF;
26.     return bufs[bufidx];
27. }
28. 
29. /* allocate space for a monster's name; removes old name if there is one */
30. void
31. new_mgivenname(
32.     struct monst *mon,
33.     int lth) /* desired length (caller handles adding 1 for terminator) */
34. {
35.     if (lth) {
36.         /* allocate mextra if necessary; otherwise get rid of old name */
37.         if (!mon->mextra)
38.             mon->mextra = newmextra();
39.         else
40.             free_mgivenname(mon); /* has mextra, might also have name */
41.         MGIVENNAME(mon) = (char *) alloc((unsigned) lth);
42.     } else {
43.         /* zero length: the new name is empty; get rid of the old name */
44.         if (has_mgivenname(mon))
45.             free_mgivenname(mon);
46.     }
47. }
48. 
49. /* release a monster's name; retains mextra even if all fields are now null */
50. void
51. free_mgivenname(struct monst *mon)
52. {
53.     if (has_mgivenname(mon)) {
54.         free((genericptr_t) MGIVENNAME(mon));
55.         MGIVENNAME(mon) = (char *) 0;
56.     }
57. }
58. 
59. /* allocate space for an object's name; removes old name if there is one */
60. void
61. new_oname(
62.     struct obj *obj,
63.     int lth) /* desired length (caller handles adding 1 for terminator) */
64. {
65.     if (lth) {
66.         /* allocate oextra if necessary; otherwise get rid of old name */
67.         if (!obj->oextra)
68.             obj->oextra = newoextra();
69.         else
70.             free_oname(obj); /* already has oextra, might also have name */
71.         ONAME(obj) = (char *) alloc((unsigned) lth);
72.     } else {
73.         /* zero length: the new name is empty; get rid of the old name */
74.         if (has_oname(obj))
75.             free_oname(obj);
76.     }
77. }
78. 
79. /* release an object's name; retains oextra even if all fields are now null */
80. void
81. free_oname(struct obj *obj)
82. {
83.     if (has_oname(obj)) {
84.         free((genericptr_t) ONAME(obj));
85.         ONAME(obj) = (char *) 0;
86.     }
87. }
88. 
89. /*  safe_oname() always returns a valid pointer to
90.  *  a string, either the pointer to an object's name
91.  *  if it has one, or a pointer to an empty string
92.  *  if it doesn't.
93.  */
94. const char *
95. safe_oname(struct obj *obj)
96. {
97.     if (has_oname(obj))
98.         return ONAME(obj);
99.     return "";
100. }
101. 
102. /* get a name for a monster or an object from player;
103.    truncate if longer than PL_PSIZ, then return it */
104. staticfn char *
105. name_from_player(
106.     char *outbuf,       /* output buffer, assumed to be at least BUFSZ long;
107.                          * anything longer than PL_PSIZ will be truncated */
108.     const char *prompt,
109.     const char *defres) /* only used if EDIT_GETLIN is enabled; only useful
110.                          * if windowport xxx's xxx_getlin() supports that */
111. {
112.     outbuf[0] = '\0';
113. #ifdef EDIT_GETLIN
114.     if (defres && *defres)
115.         Strcpy(outbuf, defres); /* default response from getlin() */
116. #else
117.     nhUse(defres);
118. #endif
119.     getlin(prompt, outbuf);
120.     if (!*outbuf || *outbuf == '\033')
121.         return NULL;
122. 
123.     /* strip leading and trailing spaces, condense internal sequences */
124.     (void) mungspaces(outbuf);
125.     if (strlen(outbuf) >= PL_PSIZ)
126.         outbuf[PL_PSIZ - 1] = '\0';
127.     return outbuf;
128. }
129. 
130. /* historical note: this returns a monster pointer because it used to
131.    allocate a new bigger block of memory to hold the monster and its name */
132. struct monst *
133. christen_monst(struct monst *mtmp, const char *name)
134. {
135.     int lth;
136.     char buf[PL_PSIZ];
137. 
138.     /* dogname & catname are PL_PSIZ arrays; object names have same limit */
139.     lth = (name && *name) ? ((int) strlen(name) + 1) : 0;
140.     if (lth > PL_PSIZ) {
141.         lth = PL_PSIZ;
142.         name = strncpy(buf, name, PL_PSIZ - 1);
143.         buf[PL_PSIZ - 1] = '\0';
144.     }
145.     new_mgivenname(mtmp, lth); /* removes old name if one is present */
146.     if (lth)
147.         Strcpy(MGIVENNAME(mtmp), name);
148.     /* if 'mtmp' is leashed, persistent inventory window needs updating */
149.     if (mtmp->mleashed)
150.         update_inventory(); /* x - leash (attached to Fido) */
151.     return mtmp;
152. }
153. 
154. /* check whether user-supplied name matches or nearly matches an unnameable
155.    monster's name, or is an attempt to delete the monster's name; if so, give
156.    alternate reject message for do_mgivenname() */
157. staticfn boolean
158. alreadynamed(struct monst *mtmp, char *monnambuf, char *usrbuf)
159. {
160.     char pronounbuf[10], *p;
161. 
162.     if (!*usrbuf) { /* attempt to erase existing name */
163.         boolean name_not_title = (has_mgivenname(mtmp)
164.                                   || type_is_pname(mtmp->data)
165.                                   || mtmp->isshk);
166.         pline("%s would rather keep %s existing %s.", upstart(monnambuf),
167.               is_rider(mtmp->data) ? "its" : mhis(mtmp),
168.               name_not_title ? "name" : "title");
169.         return TRUE;
170.     } else if (fuzzymatch(usrbuf, monnambuf, " -_", TRUE)
171.                /* catch trying to name "the Oracle" as "Oracle" */
172.                || (!strncmpi(monnambuf, "the ", 4)
173.                    && fuzzymatch(usrbuf, monnambuf + 4, " -_", TRUE))
174.                /* catch trying to name "invisible Orcus" as "Orcus" */
175.                || ((p = strstri(monnambuf, "invisible ")) != 0
176.                    && fuzzymatch(usrbuf, p + 10, " -_", TRUE))
177.                /* catch trying to name "the priest of Crom" as "Crom" */
178.                || ((p = strstri(monnambuf, " of ")) != 0
179.                    && fuzzymatch(usrbuf, p + 4, " -_", TRUE))) {
180.         if (is_rider(mtmp->data)) {
181.             /* avoid gendered pronoun for riders */
182.             pline("%s is already called that.", upstart(monnambuf));
183.         } else {
184.             pline("%s is already called %s.",
185.                   upstart(strcpy(pronounbuf, mhe(mtmp))), monnambuf);
186.         }
187.         return TRUE;
188.     } else if (mtmp->data == &mons[PM_JUIBLEX]
189.                && strstri(monnambuf, "Juiblex")
190.                && !strcmpi(usrbuf, "Jubilex")) {
191.         pline("%s doesn't like being called %s.", upstart(monnambuf), usrbuf);
192.         return TRUE;
193.     }
194.     return FALSE;
195. }
196. 
197. /* allow player to assign a name to some chosen monster */
198. staticfn void
199. do_mgivenname(void)
200. {
201.     char buf[BUFSZ], monnambuf[BUFSZ], qbuf[QBUFSZ];
202.     coord cc;
203.     int cx, cy;
204.     struct monst *mtmp = 0;
205.     boolean do_swallow = FALSE;
206. 
207.     if (Hallucination) {
208.         You("would never recognize it anyway.");
209.         return;
210.     }
211.     cc.x = u.ux;
212.     cc.y = u.uy;
213.     if (getpos(&cc, FALSE, "the monster you want to name") < 0
214.         || !isok(cc.x, cc.y))
215.         return;
216.     cx = cc.x, cy = cc.y;
217. 
218.     if (u_at(cx, cy)) {
219.         if (u.usteed && canspotmon(u.usteed)) {
220.             mtmp = u.usteed;
221.         } else {
222.             pline("This %s creature is called %s and cannot be renamed.",
223.                   beautiful(), svp.plname);
224.             return;
225.         }
226.     } else
227.         mtmp = m_at(cx, cy);
228. 
229.     /* Allow you to name the monster that has swallowed you */
230.     if (!mtmp && u.uswallow) {
231.         int glyph = glyph_at(cx, cy);
232. 
233.         if (glyph_is_swallow(glyph)) {
234.             mtmp = u.ustuck;
235.             do_swallow = TRUE;
236.         }
237.     }
238. 
239.     if (!do_swallow && (!mtmp
240.         || (!sensemon(mtmp)
241.             && (!(cansee(cx, cy) || see_with_infrared(mtmp))
242.                 || mtmp->mundetected || M_AP_TYPE(mtmp) == M_AP_FURNITURE
243.                 || M_AP_TYPE(mtmp) == M_AP_OBJECT
244.                 || (mtmp->minvis && !See_invisible))))) {
245. 
246.         pline("I see no monster there.");
247.         return;
248.     }
249.     /* special case similar to the one in lookat() */
250.     Sprintf(qbuf, "What do you want to call %s?",
251.             distant_monnam(mtmp, ARTICLE_THE, monnambuf));
252.     /* use getlin() to get a name string from the player */
253.     if (!name_from_player(buf, qbuf,
254.                           has_mgivenname(mtmp) ? MGIVENNAME(mtmp) : NULL))
255.         return;
256. 
257.     /* Unique monsters have their own specific names or titles.
258.      * Shopkeepers, temple priests and other minions use alternate
259.      * name formatting routines which ignore any user-supplied name.
260.      *
261.      * Don't say a new name is being rejected if it happens to match
262.      * the existing name, or if the player is trying to remove the
263.      * monster's existing name without assigning a new one.
264.      */
265.     if ((mtmp->data->geno & G_UNIQ) && !mtmp->ispriest) {
266.         if (!alreadynamed(mtmp, monnambuf, buf))
267.             pline("%s doesn't like being called names!", upstart(monnambuf));
268.     } else if (mtmp->isshk
269.                && !(Deaf || helpless(mtmp)
270.                     || mtmp->data->msound <= MS_ANIMAL)) {
271.         if (!alreadynamed(mtmp, monnambuf, buf)) {
272.             SetVoice(mtmp, 0, 80, 0);
273.             verbalize("I'm %s, not %s.", shkname(mtmp), buf);
274.         }
275.     } else if (mtmp->ispriest || mtmp->isminion || mtmp->isshk
276.                || mtmp->data == &mons[PM_GHOST] || has_ebones(mtmp)) {
277.         if (!alreadynamed(mtmp, monnambuf, buf))
278.             pline("%s will not accept the name %s.", upstart(monnambuf), buf);
279.     } else {
280.         (void) christen_monst(mtmp, buf);
281.     }
282. }
283. 
284. /*
285.  * This routine used to change the address of 'obj' so be unsafe if not
286.  * used with extreme care.  Applying a name to an object no longer
287.  * allocates a replacement object, so that old risk is gone.
288.  */
289. staticfn void
290. do_oname(struct obj *obj)
291. {
292.     char *bufp, buf[BUFSZ], bufcpy[BUFSZ], qbuf[QBUFSZ];
293.     const char *aname;
294.     short objtyp = STRANGE_OBJECT;
295. 
296.     /* Do this now because there's no point in even asking for a name */
297.     if (obj->otyp == SPE_NOVEL) {
298.         pline("%s already has a published name.", Ysimple_name2(obj));
299.         return;
300.     }
301. 
302.     Sprintf(qbuf, "What do you want to name %s ",
303.             is_plural(obj) ? "these" : "this");
304.     (void) safe_qbuf(qbuf, qbuf, "?", obj, xname, simpleonames, "item");
305.     /* use getlin() to get a name string from the player */
306.     if (!name_from_player(buf, qbuf, safe_oname(obj)))
307.         return;
308. 
309.     /*
310.      * We don't violate illiteracy conduct here, although it is
311.      * arguable that we should for anything other than "X".  Doing so
312.      * would make attaching player's notes to hero's inventory have an
313.      * in-game effect, which may or may not be the correct thing to do.
314.      *
315.      * We do violate illiteracy in oname() if player creates Sting or
316.      * Orcrist, clearly being literate (no pun intended...).
317.      */
318. 
319.     if (obj->oartifact) {
320.         /* this used to give "The artifact seems to resist the attempt."
321.            but resisting is definite, no "seems to" about it */
322.         pline("%s resists the attempt.",
323.               /* any artifact should always pass the has_oname() test
324.                  but be careful just in case */
325.               has_oname(obj) ? ONAME(obj) : "The artifact");
326.         return;
327.     }
328. 
329.     /* relax restrictions over proper capitalization for artifacts */
330.     if ((aname = artifact_name(buf, &objtyp, TRUE)) != 0
331.         && (restrict_name(obj, aname) || exist_artifact(obj->otyp, aname))) {
332.         /* substitute canonical spelling before slippage */
333.         Strcpy(buf, aname);
334.         /* this used to change one letter, substituting a value
335.            of 'a' through 'y' (due to an off by one error, 'z'
336.            would never be selected) and then force that to
337.            upper case if such was the case of the input;
338.            now, the hand slip scuffs one or two letters as if
339.            the text had been trodden upon, sometimes picking
340.            punctuation instead of an arbitrary letter;
341.            unfortunately, we have to cover the possibility of
342.            it targeting spaces so failing to make any change
343.            (we know that it must eventually target a nonspace
344.            because buf[] matches a valid artifact name) */
345.         Strcpy(bufcpy, buf);
346.         /* for "the Foo of Bar", only scuff "Foo of Bar" part */
347.         bufp = !strncmpi(buf, "the ", 4) ? (buf + 4) : buf;
348.         do {
349.             wipeout_text(bufp, rnd_on_display_rng(2), (unsigned) 0);
350.         } while (!strcmp(buf, bufcpy));
351.         pline("While engraving, your %s slips.", body_part(HAND));
352.         display_nhwindow(WIN_MESSAGE, FALSE);
353.         You("engrave: \"%s\".", buf);
354.         /* violate illiteracy conduct since hero attempted to write
355.            a valid artifact name */
356.         u.uconduct.literate++;
357.     } else if (obj->otyp == objtyp) {
358.         /* artifact_name() always returns non-Null when it sets objtyp */
359.         assert(aname != 0);
360. 
361.         /* artifact_name() found a match and restrict_name() didn't reject
362.            it; since 'obj' is the right type, naming will change it into an
363.            artifact so use canonical capitalization (Sting or Orcrist) */
364.         Strcpy(buf, aname);
365.     }
366. 
367.     obj = oname(obj, buf, ONAME_VIA_NAMING | ONAME_KNOW_ARTI);
368.     nhUse(obj);
369. }
370. 
371. struct obj *
372. oname(
373.     struct obj *obj,  /* item to assign name to */
374.     const char *name, /* name to assign */
375.     unsigned oflgs)   /* flags, mostly for artifact creation */
376. {
377.     int lth;
378.     char buf[PL_PSIZ];
379.     boolean via_naming = (oflgs & ONAME_VIA_NAMING) != 0,
380.             skip_inv_update = (oflgs & ONAME_SKIP_INVUPD) != 0;
381. 
382.     lth = *name ? (int) (strlen(name) + 1) : 0;
383.     if (lth > PL_PSIZ) {
384.         lth = PL_PSIZ;
385.         name = strncpy(buf, name, PL_PSIZ - 1);
386.         buf[PL_PSIZ - 1] = '\0';
387.     }
388.     /* If named artifact exists in the game, do not create another.
389.        Also trying to create an artifact shouldn't de-artifact
390.        it (e.g. Excalibur from prayer). In this case the object
391.        will retain its current name. */
392.     if (obj->oartifact || (lth && exist_artifact(obj->otyp, name)))
393.         return obj;
394. 
395.     new_oname(obj, lth); /* removes old name if one is present */
396.     if (lth)
397.         Strcpy(ONAME(obj), name);
398. 
399.     if (lth)
400.         artifact_exists(obj, name, TRUE, oflgs);
401.     if (obj->oartifact) {
402.         /* can't dual-wield with artifact as secondary weapon */
403.         if (obj == uswapwep)
404.             untwoweapon();
405.         /* activate warning if you've just named your weapon "Sting" */
406.         if (obj == uwep)
407.             set_artifact_intrinsic(obj, TRUE, W_WEP);
408.         /* if obj is owned by a shop, increase your bill */
409.         if (obj->unpaid)
410.             alter_cost(obj, 0L);
411.         if (via_naming) {
412.             /* violate illiteracy conduct since successfully wrote arti-name */
413.             if (!u.uconduct.literate++)
414.                 livelog_printf(LL_CONDUCT | LL_ARTIFACT,
415.                                "became literate by naming %s",
416.                                bare_artifactname(obj));
417.             else
418.                 livelog_printf(LL_ARTIFACT,
419.                                "chose %s to be named \"%s\"",
420.                                ansimpleoname(obj), bare_artifactname(obj));
421.         }
422.     }
423.     if (carried(obj) && !skip_inv_update)
424.         update_inventory();
425.     return obj;
426. }
427. 
428. boolean
429. objtyp_is_callable(int i)
430. {
431.     if (objects[i].oc_uname)
432.         return TRUE;
433. 
434.     switch(objects[i].oc_class) {
435.     case AMULET_CLASS:
436.         /* 5.0: calling these used to be allowed but that enabled the
437.            player to tell whether two unID'd amulets of yendor were both
438.            fake or one was real by calling them distinct names and then
439.            checking discoveries to see whether first name was replaced
440.            by second or both names stuck; with more than two available
441.            to work with, if they weren't all fake it was possible to
442.            determine which one was the real one */
443.         if (i == AMULET_OF_YENDOR || i == FAKE_AMULET_OF_YENDOR)
444.             break; /* return FALSE */
445.         FALLTHROUGH;
446.         /*FALLTHRU*/
447.     case SCROLL_CLASS:
448.     case POTION_CLASS:
449.     case WAND_CLASS:
450.     case RING_CLASS:
451.     case GEM_CLASS:
452.     case SPBOOK_CLASS:
453.     case ARMOR_CLASS:
454.     case TOOL_CLASS:
455.     case VENOM_CLASS:
456.         if (OBJ_DESCR(objects[i]))
457.             return TRUE;
458.         break;
459.     default:
460.         break;
461.     }
462.     return FALSE;
463. }
464. 
465. /* getobj callback for object to name (specific item) - anything but gold */
466. int
467. name_ok(struct obj *obj)
468. {
469.     if (!obj || obj->oclass == COIN_CLASS)
470.         return GETOBJ_EXCLUDE;
471. 
472.     if (!obj->dknown || obj->oartifact || obj->otyp == SPE_NOVEL)
473.         return GETOBJ_DOWNPLAY;
474. 
475.     return GETOBJ_SUGGEST;
476. }
477. 
478. /* getobj callback for object to call (name its type) */
479. int
480. call_ok(struct obj *obj)
481. {
482.     if (!obj || !objtyp_is_callable(obj->otyp))
483.         return GETOBJ_EXCLUDE;
484. 
485.     /* not a likely candidate if not seen yet since naming will fail,
486.        or if it has been discovered and doesn't already have a name;
487.        when something has been named and then becomes discovered, it
488.        remains a likely candidate until player renames it to <space>
489.        to remove that no longer needed name */
490.     if (!obj->dknown || (objects[obj->otyp].oc_name_known
491.                          && !objects[obj->otyp].oc_uname))
492.         return GETOBJ_DOWNPLAY;
493. 
494.     return GETOBJ_SUGGEST;
495. }
496. 
497. /* #call / #name command - player can name monster or object or type of obj */
498. int
499. docallcmd(void)
500. {
501.     struct obj *obj;
502.     winid win;
503.     anything any;
504.     menu_item *pick_list = 0;
505.     struct _cmd_queue cq, *cmdq;
506.     char ch = 0;
507.     /* if player wants a,b,c instead of i,o when looting, do that here too */
508.     boolean abc = flags.lootabc;
509.     int clr = NO_COLOR;
510. 
511.     if ((cmdq = cmdq_pop()) != 0) {
512.         cq = *cmdq;
513.         free((genericptr_t) cmdq);
514.         if (cq.typ == CMDQ_KEY)
515.             ch = cq.key;
516.         else
517.             cmdq_clear(CQ_CANNED);
518.         goto docallcmd;
519.     }
520.     win = create_nhwindow(NHW_MENU);
521.     start_menu(win, MENU_BEHAVE_STANDARD);
522.     any = cg.zeroany;
523.     any.a_char = 'm'; /* group accelerator 'C' */
524.     add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, 'C',
525.              ATR_NONE, clr, "a monster", MENU_ITEMFLAGS_NONE);
526.     if (gi.invent) {
527.         /* we use y and n as accelerators so that we can accept user's
528.            response keyed to old "name an individual object?" prompt */
529.         any.a_char = 'i'; /* group accelerator 'y' */
530.         add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, 'y',
531.                  ATR_NONE, clr, "a particular object in inventory",
532.                  MENU_ITEMFLAGS_NONE);
533.         any.a_char = 'o'; /* group accelerator 'n' */
534.         add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, 'n',
535.                  ATR_NONE, clr, "the type of an object in inventory",
536.                  MENU_ITEMFLAGS_NONE);
537.     }
538.     any.a_char = 'f'; /* group accelerator ',' (or ':' instead?) */
539.     add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, ',',
540.              ATR_NONE, clr, "the type of an object upon the floor",
541.              MENU_ITEMFLAGS_NONE);
542.     any.a_char = 'd'; /* group accelerator '\' */
543.     add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, '\\',
544.              ATR_NONE, clr, "the type of an object on discoveries list",
545.              MENU_ITEMFLAGS_NONE);
546.     any.a_char = 'a'; /* group accelerator 'l' */
547.     add_menu(win, &nul_glyphinfo, &any, abc ? 0 : any.a_char, 'l',
548.              ATR_NONE, clr, "record an annotation for the current level",
549.              MENU_ITEMFLAGS_NONE);
550.     end_menu(win, "What do you want to name?");
551.     if (select_menu(win, PICK_ONE, &pick_list) > 0) {
552.         ch = pick_list[0].item.a_char;
553.         free((genericptr_t) pick_list);
554.     } else
555.         ch = 'q';
556.     destroy_nhwindow(win);
557. 
558.  docallcmd:
559.     switch (ch) {
560.     default:
561.     case 'q':
562.         break;
563.     case 'm': /* name a visible monster */
564.         do_mgivenname();
565.         break;
566.     case 'i': /* name an individual object in inventory */
567.         obj = getobj("name", name_ok, GETOBJ_PROMPT);
568.         if (obj)
569.             do_oname(obj);
570.         break;
571.     case 'o': /* name a type of object in inventory */
572.         obj = getobj("call", call_ok, GETOBJ_NOFLAGS);
573.         if (obj) {
574.             /* behave as if examining it in inventory;
575.                this might set dknown if it was picked up
576.                while blind and the hero can now see */
577.             (void) xname(obj);
578. 
579.             if (!obj->dknown) {
580.                 You("would never recognize another one.");
581. #if 0
582.             } else if (call_ok(obj) == GETOBJ_EXCLUDE) {
583.                 You("know those as well as you ever will.");
584. #endif
585.             } else {
586.                 docall(obj);
587.             }
588.         }
589.         break;
590.     case 'f': /* name a type of object visible on the floor */
591.         namefloorobj();
592.         break;
593.     case 'd': /* name a type of object on the discoveries list */
594.         rename_disco();
595.         break;
596.     case 'a': /* annotate level */
597.         donamelevel();
598.         break;
599.     }
600.     return ECMD_OK;
601. }
602. 
603. /* for use by safe_qbuf() */
604. staticfn char *
605. docall_xname(struct obj *obj)
606. {
607.     struct obj otemp;
608. 
609.     otemp = *obj;
610.     otemp.oextra = (struct oextra *) 0;
611.     otemp.quan = 1L;
612.     /* in case water is already known, convert "[un]holy water" to "water" */
613.     otemp.blessed = otemp.cursed = 0;
614.     /* remove attributes that are doname() caliber but get formatted
615.        by xname(); most of these fixups aren't really needed because the
616.        relevant type of object isn't callable so won't reach this far */
617.     if (otemp.oclass == WEAPON_CLASS)
618.         otemp.opoisoned = 0; /* not poisoned */
619.     else if (otemp.oclass == POTION_CLASS)
620.         otemp.odiluted = 0; /* not diluted */
621.     else if (otemp.otyp == TOWEL || otemp.otyp == STATUE)
622.         otemp.spe = 0; /* not wet or historic */
623.     else if (otemp.otyp == TIN)
624.         otemp.known = 0; /* suppress tin type (homemade, &c) and mon type */
625.     else if (otemp.otyp == FIGURINE)
626.         otemp.corpsenm = NON_PM; /* suppress mon type */
627.     else if (otemp.otyp == HEAVY_IRON_BALL)
628.         otemp.owt = objects[HEAVY_IRON_BALL].oc_weight; /* not "very heavy" */
629.     else if (otemp.oclass == FOOD_CLASS && otemp.globby)
630.         otemp.owt = 120; /* 6*20, neither a small glob nor a large one */
631. 
632.     return an(xname(&otemp));
633. }
634. 
635. void
636. docall(struct obj *obj)
637. {
638.     char buf[BUFSZ], qbuf[QBUFSZ];
639.     char **uname_p;
640.     boolean had_name = FALSE;
641. 
642.     if (!obj->dknown)
643.         return; /* probably blind; Blind || Hallucination for 'fromsink' */
644.     flush_screen(1); /* buffered updates might matter to player's response */
645. 
646.     if (obj->oclass == POTION_CLASS && obj->fromsink)
647.         /* fromsink: kludge, meaning it's sink water */
648.         Sprintf(qbuf, "Call a stream of %s fluid:",
649.                 OBJ_DESCR(objects[obj->otyp]));
650.     else
651.         (void) safe_qbuf(qbuf, "Call ", ":", obj,
652.                          docall_xname, simpleonames, "thing");
653.     /* pointer to old name */
654.     uname_p = &(objects[obj->otyp].oc_uname);
655.     /* use getlin() to get a name string from the player */
656.     if (!name_from_player(buf, qbuf, *uname_p))
657.         return;
658. 
659.     /* clear old name */
660.     if (*uname_p) {
661.         had_name = TRUE;
662.         free((genericptr_t) *uname_p), *uname_p = NULL; /* clear oc_uname */
663.     }
664. 
665.     /* strip leading and trailing spaces; uncalls item if all spaces */
666.     (void) mungspaces(buf);
667.     if (!*buf) {
668.         if (had_name) /* possibly remove from disco[]; old *uname_p is gone */
669.             undiscover_object(obj->otyp);
670.     } else {
671.         *uname_p = dupstr(buf);
672.         discover_object(obj->otyp, FALSE, TRUE, TRUE); /* possibly add to disco[] */
673.     }
674.     if (obj->where == OBJ_INVENT || carrying(obj->otyp))
675.         update_inventory();
676. }
677. 
678. staticfn void
679. namefloorobj(void)
680. {
681.     coord cc;
682.     int glyph;
683.     char buf[BUFSZ];
684.     struct obj *obj = 0;
685.     boolean fakeobj = FALSE, use_plural;
686. 
687.     cc.x = u.ux, cc.y = u.uy;
688.     /* "dot for under/over you" only makes sense when the cursor hasn't
689.        been moved off the hero's '@' yet, but there's no way to adjust
690.        the help text once getpos() has started */
691.     Sprintf(buf, "object on map (or '.' for one %s you)",
692.             (u.uundetected && hides_under(gy.youmonst.data))
693.               ? "over" : "under");
694.     if (getpos(&cc, FALSE, buf) < 0 || cc.x <= 0)
695.         return;
696.     if (u_at(cc.x, cc.y)) {
697.         obj = vobj_at(u.ux, u.uy);
698.     } else {
699.         glyph = glyph_at(cc.x, cc.y);
700.         if (glyph_is_object(glyph))
701.             fakeobj = object_from_map(glyph, cc.x, cc.y, &obj);
702.         /* else 'obj' stays null */
703.     }
704.     if (!obj) {
705.         /* "under you" is safe here since there's no object to hide under */
706.         There("doesn't seem to be any object %s.",
707.               u_at(cc.x, cc.y) ? "under you" : "there");
708.         return;
709.     }
710.     /* note well: 'obj' might be an instance of STRANGE_OBJECT if target
711.        is a mimic; passing that to xname (directly or via simpleonames)
712.        would yield "glorkum" so we need to handle it explicitly; it will
713.        always fail the Hallucination test and pass the !callable test,
714.        resulting in the "can't be assigned a type name" message */
715.     Strcpy(buf, (obj->otyp != STRANGE_OBJECT)
716.                  ? simpleonames(obj)
717.                  : obj_descr[STRANGE_OBJECT].oc_name);
718.     use_plural = (obj->quan > 1L);
719.     if (Hallucination) {
720.         const char *unames[6];
721.         char tmpbuf[BUFSZ];
722. 
723.         /* straight role name */
724.         unames[0] = ((Upolyd ? u.mfemale : flags.female) && gu.urole.name.f)
725.                      ? gu.urole.name.f
726.                      : gu.urole.name.m;
727.         /* random rank title for hero's role
728. 
729.            note: the 30 is hardcoded in xlev_to_rank, so should be
730.            hardcoded here too */
731.         unames[1] = rank_of(rn2_on_display_rng(30) + 1,
732.                             Role_switch, flags.female);
733.         /* random fake monster */
734.         unames[2] = bogusmon(tmpbuf, (char *) 0);
735.         /* increased chance for fake monster */
736.         unames[3] = unames[2];
737.         /* traditional */
738.         unames[4] = roguename();
739.         /* silly */
740.         unames[5] = "Wibbly Wobbly";
741.         pline("%s %s to call you \"%s.\"",
742.               The(buf), use_plural ? "decide" : "decides",
743.               unames[rn2_on_display_rng(SIZE(unames))]);
744.     } else if (call_ok(obj) == GETOBJ_EXCLUDE) {
745.         pline("%s %s can't be assigned a type name.",
746.               use_plural ? "Those" : "That", buf);
747.     } else if (!obj->dknown) {
748.         You("don't know %s %s well enough to name %s.",
749.             use_plural ? "those" : "that", buf, use_plural ? "them" : "it");
750.     } else {
751.         docall(obj);
752.     }
753.     if (fakeobj) {
754.         obj->where = OBJ_FREE; /* object_from_map() sets it to OBJ_FLOOR */
755.         dealloc_obj(obj); /* has no contents */
756.     }
757. }
758. 
759. static const char *const ghostnames[] = {
760.     /* these names should have length < PL_NSIZ */
761.     /* Capitalize the names for aesthetics -dgk */
762.     "Adri",    "Andries",       "Andreas",     "Bert",    "David",  "Dirk",
763.     "Emile",   "Frans",         "Fred",        "Greg",    "Hether", "Jay",
764.     "John",    "Jon",           "Karnov",      "Kay",     "Kenny",  "Kevin",
765.     "Maud",    "Michiel",       "Mike",        "Peter",   "Robert", "Ron",
766.     "Tom",     "Wilmar",        "Nick Danger", "Phoenix", "Jiro",   "Mizue",
767.     "Stephan", "Lance Braccus", "Shadowhawk",  "Murphy"
768. };
769. 
770. /* ghost names formerly set by x_monnam(), now by makemon() instead */
771. const char *
772. rndghostname(void)
773. {
774.     return rn2(7) ? ROLL_FROM(ghostnames)
775.                   : (const char *) svp.plname;
776. }
777. 
778. /*
779.  * Monster naming functions:
780.  * x_monnam is the generic monster-naming function.
781.  *                seen        unseen       detected               named
782.  * mon_nam:     the newt        it      the invisible orc       Fido
783.  * noit_mon_nam:your newt (as if detected) your invisible orc   Fido
784.  * some_mon_nam:the newt    someone     the invisible orc       Fido
785.  *          or              something
786.  * l_monnam:    newt            it      invisible orc           dog called Fido
787.  * Monnam:      The newt        It      The invisible orc       Fido
788.  * noit_Monnam: Your newt (as if detected) Your invisible orc   Fido
789.  * Some_Monnam: The newt    Someone     The invisible orc       Fido
790.  *          or              Something
791.  * Adjmonnam:   The poor newt   It      The poor invisible orc  The poor Fido
792.  * Amonnam:     A newt          It      An invisible orc        Fido
793.  * a_monnam:    a newt          it      an invisible orc        Fido
794.  * m_monnam:    newt            xan     orc                     Fido
795.  * y_monnam:    your newt     your xan  your invisible orc      Fido
796.  * YMonnam:     Your newt     Your xan  Your invisible orc      Fido
797.  * noname_monnam(mon,article):
798.  *              article newt    art xan art invisible orc       art dog
799.  */
800. 
801. /*
802.  * article
803.  *
804.  * ARTICLE_NONE, ARTICLE_THE, ARTICLE_A: obvious
805.  * ARTICLE_YOUR: "your" on pets, "the" on everything else
806.  *
807.  * If the monster would be referred to as "it" or if the monster has a name
808.  * _and_ there is no adjective, "invisible", "saddled", etc., override this
809.  * and always use no article.
810.  *
811.  * suppress
812.  *
813.  * SUPPRESS_IT, SUPPRESS_INVISIBLE, SUPPRESS_HALLUCINATION, SUPPRESS_SADDLE.
814.  * SUPPRESS_MAPPEARANCE: if monster is mimicking another monster (cloned
815.  *              Wizard or quickmimic pet), describe the real monster rather
816.  *              than its current form;
817.  * EXACT_NAME: combination of all the above
818.  * SUPPRESS_NAME: omit monster's assigned name (unless uniq w/ pname).
819.  * AUGMENT_IT: not suppression but shares suppression bitmask; if result
820.  *              would have been "it", return "someone" if humanoid or
821.  *              "something" otherwise.
822.  *
823.  * Bug: if the monster is a priest or shopkeeper, not every one of these
824.  * options works, since those are special cases.
825.  */
826. char *
827. x_monnam(
828.     struct monst *mtmp,
829.     int article,
830.     const char *adjective,
831.     int suppress,
832.     boolean called)
833. {
834.     char *buf = nextmbuf();
835.     struct permonst *mdat = mtmp->data;
836.     const char *pm_name;
837.     boolean do_hallu, do_invis, do_it, do_saddle, do_mappear,
838.             do_exact, do_name, augment_it;
839.     boolean name_at_start, has_adjectives, insertbuf2,
840.             mappear_as_mon = (M_AP_TYPE(mtmp) == M_AP_MONSTER);
841.     char *bp, buf2[BUFSZ];
842. 
843.     if (mtmp == &gy.youmonst)
844.         return strcpy(buf, "you"); /* ignore article, "invisible", &c */
845. 
846.     if (program_state.gameover)
847.         suppress |= SUPPRESS_HALLUCINATION;
848.     if (article == ARTICLE_YOUR && !mtmp->mtame)
849.         article = ARTICLE_THE;
850. 
851.     if (u.uswallow && mtmp == u.ustuck) {
852.         /*
853.          * This monster has become important, for the moment anyway.
854.          * As the hero's consumer, it is worthy of ARTICLE_THE.
855.          * Also, suppress invisible as that particular characteristic
856.          * is unimportant now and you can see its interior anyway.
857.          */
858.         article = ARTICLE_THE;
859.         suppress |= SUPPRESS_INVISIBLE;
860.     }
861.     do_hallu = Hallucination && !(suppress & SUPPRESS_HALLUCINATION);
862.     do_invis = mtmp->minvis && !(suppress & SUPPRESS_INVISIBLE);
863.     do_it = !canspotmon(mtmp) && article != ARTICLE_YOUR
864.             && !program_state.gameover && mtmp != u.usteed
865.             && !engulfing_u(mtmp) && !(suppress & SUPPRESS_IT);
866.     do_saddle = !(suppress & SUPPRESS_SADDLE);
867.     do_mappear = mappear_as_mon && !(suppress & SUPPRESS_MAPPEARANCE);
868.     do_exact = (suppress & EXACT_NAME) == EXACT_NAME;
869.     do_name = !(suppress & SUPPRESS_NAME) || type_is_pname(mdat);
870.     augment_it = (suppress & AUGMENT_IT) != 0;
871. 
872.     buf[0] = '\0';
873. 
874.     /* unseen monsters, etc.; usually "it" but sometimes more specific;
875.        when hallucinating, the more specific values might be inverted */
876.     if (do_it) {
877.         /* !is_animal excludes all Y; !mindless excludes Z, M, \' */
878.         boolean s_one = humanoid(mdat) && !is_animal(mdat) && !mindless(mdat);
879. 
880.         Strcpy(buf, !augment_it ? "it"
881.                     : (!do_hallu ? s_one : !rn2(2)) ? "someone"
882.                       : "something");
883.         return buf;
884.     }
885. 
886.     /* priests and minions: don't even use this function */
887.     if ((mtmp->ispriest || mtmp->isminion) && !do_mappear) {
888.         char *name;
889.         long save_prop = EHalluc_resistance;
890.         unsigned save_invis = mtmp->minvis;
891. 
892.         /* when true name is wanted, explicitly block Hallucination */
893.         if (!do_hallu)
894.             EHalluc_resistance = 1L;
895.         if (!do_invis)
896.             mtmp->minvis = 0;
897.         /* EXACT_NAME will force "of <deity>" on the Astral Plane */
898.         name = priestname(mtmp, article, do_exact, buf2);
899.         EHalluc_resistance = save_prop;
900.         mtmp->minvis = save_invis;
901.         if (article == ARTICLE_NONE && !strncmp(name, "the ", 4))
902.             name += 4;
903.         return strcpy(buf, name);
904.     }
905. 
906.     /* 'pm_name' is the base part of most names */
907.     if (do_mappear) {
908.         /*assert(ismnum(mtmp->mappearance));*/
909.         pm_name = pmname(&mons[mtmp->mappearance], Mgender(mtmp));
910.     } else {
911.         pm_name = mon_pmname(mtmp);
912.     }
913. 
914.     /* Shopkeepers: use shopkeeper name.  For normal shopkeepers, just
915.      * "Asidonhopo"; for unusual ones, "Asidonhopo the invisible
916.      * shopkeeper" or "Asidonhopo the blue dragon".  If hallucinating,
917.      * none of this applies.
918.      */
919.     if (mtmp->isshk && !do_hallu && !do_mappear) {
920.         if (adjective && article == ARTICLE_THE) {
921.             /* pathological case: "the angry Asidonhopo the blue dragon"
922.                sounds silly */
923.             Strcpy(buf, "the ");
924.             Strcat(strcat(buf, adjective), " ");
925.             Strcat(buf, shkname(mtmp));
926.         } else {
927.             Strcat(buf, shkname(mtmp));
928.             if (mdat != &mons[PM_SHOPKEEPER] || do_invis){
929.                 Strcat(buf, " the ");
930.                 if (do_invis)
931.                     Strcat(buf, "invisible ");
932.                 Strcat(buf, pm_name);
933.             }
934.         }
935.         return buf;
936.     }
937. 
938.     /* Put the adjectives in the buffer */
939.     if (adjective)
940.         Strcat(strcat(buf, adjective), " ");
941.     if (do_invis)
942.         Strcat(buf, "invisible ");
943.     if (do_saddle && (mtmp->misc_worn_check & W_SADDLE) && !Blind
944.         && !Hallucination)
945.         Strcat(buf, "saddled ");
946.     has_adjectives = (buf[0] != '\0');
947. 
948.     /* Put the actual monster name or type into the buffer now.
949.        Remember whether the buffer starts with a personal name. */
950.     if (do_hallu) {
951.         char rnamecode;
952.         char *rname = rndmonnam(&rnamecode);
953. 
954.         Strcat(buf, rname);
955.         name_at_start = bogon_is_pname(rnamecode);
956.     } else if (do_name && has_mgivenname(mtmp)) {
957.         char *name = MGIVENNAME(mtmp);
958. 
959. #if 0
960.       /* hardfought */
961.       if (has_ebones(mtmp)) {
962. #endif
963.         if (mdat == &mons[PM_GHOST]) {
964.             Sprintf(eos(buf), "%s ghost", s_suffix(name));
965.             name_at_start = TRUE;
966.         } else if (called) {
967.             Sprintf(eos(buf), "%s called %s", pm_name, name);
968.             name_at_start = (boolean) type_is_pname(mdat);
969.         } else if (is_mplayer(mdat) && (bp = strstri(name, " the ")) != 0) {
970.             /* <name> the <adjective> <invisible> <saddled> <rank> */
971.             char pbuf[BUFSZ];
972. 
973.             Strcpy(pbuf, name);
974.             pbuf[bp - name + 5] = '\0'; /* adjectives right after " the " */
975.             if (has_adjectives)
976.                 Strcat(pbuf, buf);
977.             Strcat(pbuf, bp + 5); /* append the rest of the name */
978.             Strcpy(buf, pbuf);
979.             article = ARTICLE_NONE;
980.             name_at_start = TRUE;
981.         } else {
982.             Strcat(buf, name);
983.             name_at_start = TRUE;
984.         }
985. #if 0 /* hardfought */
986.       }
987. #endif
988.     } else if (is_mplayer(mdat) && !In_endgame(&u.uz)) {
989.         char pbuf[BUFSZ];
990. 
991.         Strcpy(pbuf, rank_of((int) mtmp->m_lev, monsndx(mdat),
992.                              (boolean) mtmp->female));
993.         Strcat(buf, lcase(pbuf));
994.         name_at_start = FALSE;
995.     } else {
996.         Strcat(buf, pm_name);
997.         name_at_start = (boolean) type_is_pname(mdat);
998.     }
999. 
1000.     if (name_at_start && (article == ARTICLE_YOUR || !has_adjectives)) {
1001.         if (mdat == &mons[PM_WIZARD_OF_YENDOR])
1002.             article = ARTICLE_THE;
1003.         else
1004.             article = ARTICLE_NONE;
1005.     } else if ((mdat->geno & G_UNIQ) != 0 && article == ARTICLE_A) {
1006.         article = ARTICLE_THE;
1007.     }
1008. 
1009.     insertbuf2 = TRUE;
1010.     buf2[0] = '\0'; /* lint suppression */
1011.     switch (article) {
1012.     case ARTICLE_YOUR:
1013.         Strcpy(buf2, "your ");
1014.         break;
1015.     case ARTICLE_THE:
1016.         Strcpy(buf2, "the ");
1017.         break;
1018.     case ARTICLE_A:
1019.         /* avoid an() here */
1020.         (void) just_an(buf2, buf); /* copy "a " or "an " into buf2[] */
1021.         break;
1022.     case ARTICLE_NONE:
1023.     default:
1024.         insertbuf2 = FALSE;
1025.         break;
1026.     }
1027.     if (insertbuf2) {
1028.         Strcat(buf2, buf); /* buf2[] isn't viable to return,  */
1029.         Strcpy(buf, buf2); /* so transfer the result to buf[] */
1030.     }
1031.     return buf;
1032. }
1033. 
1034. char *
1035. l_monnam(struct monst *mtmp)
1036. {
1037.     return x_monnam(mtmp, ARTICLE_NONE, (char *) 0,
1038.                     (has_mgivenname(mtmp)) ? SUPPRESS_SADDLE : 0, TRUE);
1039. }
1040. 
1041. char *
1042. mon_nam(struct monst *mtmp)
1043. {
1044.     return x_monnam(mtmp, ARTICLE_THE, (char *) 0,
1045.                     (has_mgivenname(mtmp)) ? SUPPRESS_SADDLE : 0, FALSE);
1046. }
1047. 
1048. /* print the name as if mon_nam() (y_monnam() if tame) was called, but
1049.    assume that the player can always see the monster--used for probing and
1050.    for monsters aggravating the player with a cursed potion of invisibility;
1051.    also used for pet moving "reluctantly" onto cursed object when that pet
1052.    can be seen either before or after it moves */
1053. char *
1054. noit_mon_nam(struct monst *mtmp)
1055. {
1056.     return x_monnam(mtmp, ARTICLE_YOUR, (char *) 0,
1057.                     (has_mgivenname(mtmp) ? (SUPPRESS_SADDLE | SUPPRESS_IT)
1058.                                           : SUPPRESS_IT),
1059.                     FALSE);
1060. }
1061. 
1062. /* in between noit_mon_nam() and mon_nam(); if the latter would pick "it",
1063.    use "someone" (for humanoids) or "something" (for others) instead */
1064. char *
1065. some_mon_nam(struct monst *mtmp)
1066. {
1067.     return x_monnam(mtmp, ARTICLE_THE, (char *) 0,
1068.                     (has_mgivenname(mtmp) ? (SUPPRESS_SADDLE | AUGMENT_IT)
1069.                                           : AUGMENT_IT),
1070.                     FALSE);
1071. }
1072. 
1073. char *
1074. Monnam(struct monst *mtmp)
1075. {
1076.     char *bp = mon_nam(mtmp);
1077. 
1078.     *bp = highc(*bp);
1079.     return bp;
1080. }
1081. 
1082. char *
1083. noit_Monnam(struct monst *mtmp)
1084. {
1085.     char *bp = noit_mon_nam(mtmp);
1086. 
1087.     *bp = highc(*bp);
1088.     return bp;
1089. }
1090. 
1091. char *
1092. Some_Monnam(struct monst *mtmp)
1093. {
1094.     char *bp = some_mon_nam(mtmp);
1095. 
1096.     *bp = highc(*bp);
1097.     return bp;
1098. }
1099. 
1100. /* return "a dog" rather than "Fido", honoring hallucination and visibility */
1101. char *
1102. noname_monnam(struct monst *mtmp, int article)
1103. {
1104.     return x_monnam(mtmp, article, (char *) 0, SUPPRESS_NAME, FALSE);
1105. }
1106. 
1107. /* monster's own name -- overrides hallucination and [in]visibility
1108.    so shouldn't be used in ordinary messages (mainly for disclosure) */
1109. char *
1110. m_monnam(struct monst *mtmp)
1111. {
1112.     return x_monnam(mtmp, ARTICLE_NONE, (char *) 0, EXACT_NAME, FALSE);
1113. }
1114. 
1115. /* pet name: "your little dog" */
1116. char *
1117. y_monnam(struct monst *mtmp)
1118. {
1119.     int prefix, suppression_flag;
1120. 
1121.     prefix = mtmp->mtame ? ARTICLE_YOUR : ARTICLE_THE;
1122.     suppression_flag = (has_mgivenname(mtmp)
1123.                         /* "saddled" is redundant when mounted */
1124.                         || mtmp == u.usteed)
1125.                            ? SUPPRESS_SADDLE
1126.                            : 0;
1127. 
1128.     return x_monnam(mtmp, prefix, (char *) 0, suppression_flag, FALSE);
1129. }
1130. 
1131. /* y_monnam() for start of sentence */
1132. char *
1133. YMonnam(struct monst *mtmp)
1134. {
1135.     char *bp = y_monnam(mtmp);
1136. 
1137.     *bp = highc(*bp);
1138.     return bp;
1139. }
1140. 
1141. char *
1142. Adjmonnam(struct monst *mtmp, const char *adj)
1143. {
1144.     char *bp = x_monnam(mtmp, ARTICLE_THE, adj,
1145.                         has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE);
1146. 
1147.     *bp = highc(*bp);
1148.     return bp;
1149. }
1150. 
1151. char *
1152. a_monnam(struct monst *mtmp)
1153. {
1154.     return x_monnam(mtmp, ARTICLE_A, (char *) 0,
1155.                     has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, FALSE);
1156. }
1157. 
1158. char *
1159. Amonnam(struct monst *mtmp)
1160. {
1161.     char *bp = a_monnam(mtmp);
1162. 
1163.     *bp = highc(*bp);
1164.     return bp;
1165. }
1166. 
1167. /* used for monster ID by the '/', ';', and 'C' commands to block remote
1168.    identification of the endgame altars via their attending priests */
1169. char *
1170. distant_monnam(
1171.     struct monst *mon,
1172.     int article, /* only ARTICLE_NONE and ARTICLE_THE are handled here */
1173.     char *outbuf)
1174. {
1175.     /* high priest(ess)'s identity is concealed on the Astral Plane,
1176.        unless you're adjacent (overridden for hallucination which does
1177.        its own obfuscation) */
1178.     if (mon->data == &mons[PM_HIGH_CLERIC] && !Hallucination
1179.         && Is_astralevel(&u.uz) && !m_next2u(mon)) {
1180.         Strcpy(outbuf, article == ARTICLE_THE ? "the " : "");
1181.         Strcat(outbuf, mon->female ? "high priestess" : "high priest");
1182.     } else {
1183.         Strcpy(outbuf, x_monnam(mon, article, (char *) 0, 0, TRUE));
1184.     }
1185.     return outbuf;
1186. }
1187. 
1188. /* returns mon_nam(mon) relative to other_mon; normal name unless they're
1189.    the same, in which case the reference is to {him|her|it} self */
1190. char *
1191. mon_nam_too(struct monst *mon, struct monst *other_mon)
1192. {
1193.     char *outbuf;
1194. 
1195.     if (mon != other_mon) {
1196.         outbuf = mon_nam(mon);
1197.     } else {
1198.         outbuf = nextmbuf();
1199.         switch (pronoun_gender(mon, PRONOUN_HALLU)) {
1200.         case 0:
1201.             Strcpy(outbuf, "himself");
1202.             break;
1203.         case 1:
1204.             Strcpy(outbuf, "herself");
1205.             break;
1206.         default:
1207.         case 2:
1208.             Strcpy(outbuf, "itself");
1209.             break;
1210.         case 3: /* could happen when hallucinating */
1211.             Strcpy(outbuf, "themselves");
1212.             break;
1213.         }
1214.     }
1215.     return outbuf;
1216. }
1217. 
1218. /* construct "<monnamtext> <verb> <othertext> {him|her|it}self" which might
1219.    be distorted by Hallu; if that's plural, adjust monnamtext and verb */
1220. char *
1221. monverbself(
1222.     struct monst *mon,
1223.     char *monnamtext, /* modifiable 'mbuf' with adequate room at end */
1224.     const char *verb,
1225.     const char *othertext)
1226. {
1227.     char *verbs, selfbuf[40]; /* sizeof "themselves" suffices */
1228. 
1229.     /* "himself"/"herself"/"itself", maybe "themselves" if hallucinating */
1230.     Strcpy(selfbuf, mon_nam_too(mon, mon));
1231.     /* verb starts plural; this will yield singular except for "themselves" */
1232.     verbs = vtense(selfbuf, verb);
1233.     if (!strcmp(verb, verbs)) { /* a match indicates that it stayed plural */
1234.         monnamtext = makeplural(monnamtext);
1235.         /* for "it", makeplural() produces "them" but we want "they" */
1236.         if (!strcmpi(monnamtext, genders[3].he)) {
1237.             boolean capitaliz = (monnamtext[0] == highc(monnamtext[0]));
1238. 
1239.             Strcpy(monnamtext, genders[3].him);
1240.             if (capitaliz)
1241.                 monnamtext[0] = highc(monnamtext[0]);
1242.         }
1243.     }
1244.     Strcat(strcat(monnamtext, " "), verbs);
1245.     if (othertext && *othertext)
1246.         Strcat(strcat(monnamtext, " "), othertext);
1247.     Strcat(strcat(monnamtext, " "), selfbuf);
1248.     return monnamtext;
1249. }
1250. 
1251. /* for debugging messages, where data might be suspect and we aren't
1252.    taking what the hero does or doesn't know into consideration */
1253. char *
1254. minimal_monnam(struct monst *mon, boolean ckloc)
1255. {
1256.     struct permonst *ptr;
1257.     char *outbuf = nextmbuf();
1258. 
1259.     if (!mon) {
1260.         Strcpy(outbuf, "[Null monster]");
1261.     } else if ((ptr = mon->data) == 0) {
1262.         Strcpy(outbuf, "[Null mon->data]");
1263.     } else if (ptr < &mons[0]) {
1264.         Sprintf(outbuf, "[Invalid mon->data %s < %s]",
1265.                 fmt_ptr((genericptr_t) mon->data),
1266.                 fmt_ptr((genericptr_t) &mons[0]));
1267.     } else if (ptr >= &mons[NUMMONS]) {
1268.         Sprintf(outbuf, "[Invalid mon->data %s >= %s]",
1269.                 fmt_ptr((genericptr_t) mon->data),
1270.                 fmt_ptr((genericptr_t) &mons[NUMMONS]));
1271.     } else if (ckloc && ptr == &mons[PM_LONG_WORM] && mon->mx
1272.                && svl.level.monsters[mon->mx][mon->my] != mon) {
1273.         Sprintf(outbuf, "%s <%d,%d>",
1274.                 pmname(&mons[PM_LONG_WORM_TAIL], Mgender(mon)),
1275.                 mon->mx, mon->my);
1276.     } else {
1277.         Sprintf(outbuf, "%s%s <%d,%d>",
1278.                 mon->mtame ? "tame " : mon->mpeaceful ? "peaceful " : "",
1279.                 mon_pmname(mon), mon->mx, mon->my);
1280.         if (mon->cham != NON_PM)
1281.             Sprintf(eos(outbuf), "{%s}",
1282.                     pmname(&mons[mon->cham], Mgender(mon)));
1283.     }
1284.     return outbuf;
1285. }
1286. 
1287. #ifndef PMNAME_MACROS
1288. int
1289. Mgender(struct monst *mtmp)
1290. {
1291.     int mgender = MALE;
1292. 
1293.     if (mtmp == &gy.youmonst) {
1294.         if (Upolyd ? u.mfemale : flags.female)
1295.             mgender = FEMALE;
1296.     } else if (mtmp->female) {
1297.         mgender = FEMALE;
1298.     }
1299.     return mgender;
1300. }
1301. 
1302. const char *
1303. pmname(struct permonst *pm, int mgender)
1304. {
1305.     if (mgender < MALE || mgender >= NUM_MGENDERS || !pm->pmnames[mgender])
1306.         mgender = NEUTRAL;
1307.     return pm->pmnames[mgender];
1308. }
1309. #endif /* PMNAME_MACROS */
1310. 
1311. /* mons[]->pmname for a monster */
1312. const char *
1313. mon_pmname(struct monst *mon)
1314. {
1315.     /* for neuter, mon->data->pmnames[MALE] will be Null and use [NEUTRAL] */
1316.     return pmname(mon->data, Mgender(mon));
1317. }
1318. 
1319. /* mons[]->pmname for a corpse or statue or figurine */
1320. const char *
1321. obj_pmname(struct obj *obj)
1322. {
1323. #if 0   /* ignore saved montraits even when they're available; they determine
1324.          * what a corpse would revive as if resurrected (human corpse from
1325.          * slain vampire revives as vampire rather than as human, for example)
1326.          * and don't necessarily reflect the state of the corpse itself */
1327.     if (has_omonst(obj)) {
1328.         struct monst *m = OMONST(obj);
1329. 
1330.         /* obj->oextra->omonst->data is Null but ...->mnum is set */
1331.         if (ismnum(m->mnum))
1332.             return pmname(&mons[m->mnum], Mgender(m));
1333.     }
1334. #endif
1335.     if ((obj->otyp == CORPSE || obj->otyp == STATUE || obj->otyp == FIGURINE)
1336.         && ismnum(obj->corpsenm)) {
1337.         int cgend = (obj->spe & CORPSTAT_GENDER),
1338.             mgend = ((cgend == CORPSTAT_MALE) ? MALE
1339.                      : (cgend == CORPSTAT_FEMALE) ? FEMALE
1340.                        : NEUTRAL),
1341.             mndx = obj->corpsenm;
1342. 
1343.         /* mons[].pmnames[] for monster cleric uses "priest" or "priestess"
1344.            or "aligned cleric"; we want to avoid "aligned cleric [corpse]"
1345.            unless it has been explicitly flagged as neuter rather than
1346.            defaulting to random (which fails male or female check above);
1347.            role monster cleric uses "priest" or "priestess" or "cleric"
1348.            without "aligned" prefix so we switch to that; [can't force
1349.            random gender to be chosen here because splitting a stack of
1350.            corpses could cause the split-off portion to change gender, so
1351.            settle for avoiding "aligned"] */
1352.         if (mndx == PM_ALIGNED_CLERIC && cgend == CORPSTAT_RANDOM)
1353.             mndx = PM_CLERIC;
1354. 
1355.         return pmname(&mons[mndx], mgend);
1356.     }
1357.     impossible("obj_pmname otyp:%i,corpsenm:%i", obj->otyp, obj->corpsenm);
1358.     return "two-legged glorkum-seeker";
1359. }
1360. 
1361. /* used by bogusmon(next) and also by init_CapMons(rumors.c);
1362.    bogon_is_pname(below) checks a hard-coded subset of these rather than
1363.    use this list.
1364.    Also used in rumors.c */
1365. const char bogon_codes[] = "-_+|="; /* see dat/bonusmon.txt */
1366. 
1367. /* fake monsters used to be in a hard-coded array, now in a data file */
1368. char *
1369. bogusmon(char *buf, char *code)
1370. {
1371.     char *mnam = buf;
1372. 
1373.     if (code)
1374.         *code = '\0';
1375.     /* might fail (return empty buf[]) if the file isn't available */
1376.     get_rnd_text(BOGUSMONFILE, buf, rn2_on_display_rng, MD_PAD_BOGONS);
1377.     if (!*mnam) {
1378.         Strcpy(buf, "bogon");
1379.     } else if (strchr(bogon_codes, *mnam)) { /* strip prefix if present */
1380.         if (code)
1381.             *code = *mnam;
1382.         ++mnam;
1383.     }
1384.     return mnam;
1385. }
1386. 
1387. /* return a random monster name, for hallucination */
1388. char *
1389. rndmonnam(char *code)
1390. {
1391.     static char buf[BUFSZ];
1392.     char *mnam;
1393.     int name;
1394. #define BOGUSMONSIZE 100 /* arbitrary */
1395. 
1396.     if (code)
1397.         *code = '\0';
1398. 
1399.     do {
1400.         name = rn2_on_display_rng(SPECIAL_PM + BOGUSMONSIZE - LOW_PM) + LOW_PM;
1401.     } while (name < SPECIAL_PM
1402.              && (type_is_pname(&mons[name]) || (mons[name].geno & G_NOGEN)));
1403. 
1404.     if (name >= SPECIAL_PM) {
1405.         mnam = bogusmon(buf, code);
1406.     } else {
1407.         mnam = strcpy(buf, pmname(&mons[name], rn2_on_display_rng(2)));
1408.     }
1409.     return mnam;
1410. #undef BOGUSMONSIZE
1411. }
1412. 
1413. /* check bogusmon prefix to decide whether it's a personal name */
1414. boolean
1415. bogon_is_pname(char code)
1416. {
1417.     if (!code)
1418.         return FALSE;
1419.     return strchr("-+=", code) ? TRUE : FALSE;
1420. }
1421. 
1422. /* name of a Rogue player */
1423. const char *
1424. roguename(void)
1425. {
1426.     char *i, *opts;
1427. 
1428.     if ((opts = nh_getenv("ROGUEOPTS")) != 0) {
1429.         for (i = opts; *i; i++)
1430.             if (!strncmp("name=", i, 5)) {
1431.                 char *j;
1432.                 if ((j = strchr(i + 5, ',')) != 0)
1433.                     *j = (char) 0;
1434.                 return i + 5;
1435.             }
1436.     }
1437.     return rn2(3) ? (rn2(2) ? "Michael Toy" : "Kenneth Arnold")
1438.                   : "Glenn Wichman";
1439. }
1440. 
1441. static NEARDATA const char *const hcolors[] = {
1442.     "ultraviolet", "infrared", "bluish-orange", "reddish-green", "dark white",
1443.     "light black", "sky blue-pink", "pinkish-cyan", "indigo-chartreuse",
1444.     "salty", "sweet", "sour", "bitter", "umami", /* basic tastes */
1445.     "striped", "spiral", "swirly", "plaid", "checkered", "argyle", "paisley",
1446.     "blotchy", "guernsey-spotted", "polka-dotted", "square", "round",
1447.     "triangular", "cabernet", "sangria", "fuchsia", "wisteria", "lemon-lime",
1448.     "strawberry-banana", "peppermint", "romantic", "incandescent",
1449.     "octarine", /* Discworld: the Colour of Magic */
1450.     "excitingly dull", "mauve", "electric",
1451.     "neon", "fluorescent", "phosphorescent", "translucent", "opaque",
1452.     "psychedelic", "iridescent", "rainbow-colored", "polychromatic",
1453.     "colorless", "colorless green",
1454.     "dancing", "singing", "loving", "loudy", "noisy", "clattery", "silent",
1455.     "apocyan", "infra-pink", "opalescent", "violant", "tuneless",
1456.     "viridian", "aureolin", "cinnabar", "purpurin", "gamboge", "madder",
1457.     "bistre", "ecru", "fulvous", "tekhelet", "selective yellow",
1458. };
1459. 
1460. const char *
1461. hcolor(const char *colorpref)
1462. {
1463.     return (Hallucination || !colorpref)
1464.         ? hcolors[rn2_on_display_rng(SIZE(hcolors))]
1465.         : colorpref;
1466. }
1467. 
1468. /* return a random real color unless hallucinating */
1469. const char *
1470. rndcolor(void)
1471. {
1472.     int k = rn2(CLR_MAX);
1473. 
1474.     return Hallucination ? hcolor((char *) 0)
1475.                          : (k == NO_COLOR) ? "colorless"
1476.                                            : c_obj_colors[k];
1477. }
1478. 
1479. static NEARDATA const char *const hliquids[] = {
1480.     "yoghurt", "oobleck", "clotted blood", "diluted water", "purified water",
1481.     "instant coffee", "tea", "herbal infusion", "liquid rainbow",
1482.     "creamy foam", "mulled wine", "bouillon", "nectar", "grog", "flubber",
1483.     "ketchup", "slow light", "oil", "vinaigrette", "liquid crystal", "honey",
1484.     "caramel sauce", "ink", "aqueous humour", "milk substitute",
1485.     "fruit juice", "glowing lava", "gastric acid", "mineral water",
1486.     "cough syrup", "quicksilver", "sweet vitriol", "grey goo", "pink slime",
1487.     "cosmic latte", "bone oil", "custard", "lard", "vinegar", "creosote",
1488.     /* "new coke (tm)", --better not */
1489. };
1490. 
1491. /* if hallucinating, return a random liquid instead of 'liquidpref' */
1492. const char *
1493. hliquid(
1494.     const char *liquidpref) /* use as-is when not hallucintg (unless empty) */
1495. {
1496.     boolean hallucinate = Hallucination && !program_state.gameover;
1497. 
1498.     if (hallucinate || !liquidpref || !*liquidpref) {
1499.         int indx, count = SIZE(hliquids);
1500. 
1501.         /* if we have a non-hallucinatory default value, include it
1502.            among the choices */
1503.         if (liquidpref && *liquidpref)
1504.             ++count;
1505.         indx = rn2_on_display_rng(count);
1506.         if (IndexOk(indx, hliquids))
1507.             return hliquids[indx];
1508.     }
1509.     return liquidpref;
1510. }
1511. 
1512. /* Aliases for road-runner nemesis
1513.  */
1514. static const char *const coynames[] = {
1515.     "Carnivorous Vulgaris", "Road-Runnerus Digestus", "Eatibus Anythingus",
1516.     "Famishus-Famishus", "Eatibus Almost Anythingus", "Eatius Birdius",
1517.     "Famishius Fantasticus", "Eternalii Famishiis", "Famishus Vulgarus",
1518.     "Famishius Vulgaris Ingeniusi", "Eatius-Slobbius", "Hardheadipus Oedipus",
1519.     "Carnivorous Slobbius", "Hard-Headipus Ravenus", "Evereadii Eatibus",
1520.     "Apetitius Giganticus", "Hungrii Flea-Bagius", "Overconfidentii Vulgaris",
1521.     "Caninus Nervous Rex", "Grotesques Appetitus", "Nemesis Ridiculii",
1522.     "Canis latrans"
1523. };
1524. 
1525. char *
1526. coyotename(struct monst *mtmp, char *buf)
1527. {
1528.     if (mtmp && buf) {
1529.         Sprintf(buf, "%s - %s",
1530.                 x_monnam(mtmp, ARTICLE_NONE, (char *) 0, 0, TRUE),
1531.                 mtmp->mcan ? coynames[SIZE(coynames) - 1]
1532.                            : coynames[mtmp->m_id % (SIZE(coynames) - 1)]);
1533.     }
1534.     return buf;
1535. }
1536. 
1537. char *
1538. rndorcname(char *s)
1539. {
1540.     static const char *const v[] = { "a", "ai", "og", "u" };
1541.     static const char *const snd[] = { "gor", "gris", "un", "bane", "ruk",
1542.                                  "oth","ul", "z", "thos","akh","hai" };
1543.     int i, iend = rn1(2, 3), vstart = rn2(2);
1544. 
1545.     if (s) {
1546.         *s = '\0';
1547.         for (i = 0; i < iend; ++i) {
1548.             vstart = 1 - vstart;                /* 0 -> 1, 1 -> 0 */
1549.             Sprintf(eos(s), "%s%s", (i > 0 && !rn2(30)) ? "-" : "",
1550.                     vstart ? ROLL_FROM(v) : ROLL_FROM(snd));
1551.         }
1552.     }
1553.     return s;
1554. }
1555. 
1556. struct monst *
1557. christen_orc(struct monst *mtmp, const char *gang, const char *other)
1558. {
1559.     int sz = 0;
1560.     char buf[BUFSZ], buf2[BUFSZ], *orcname;
1561. 
1562.     orcname = rndorcname(buf2);
1563.     /* rndorcname() won't return NULL */
1564.     sz = (int) strlen(orcname);
1565.     if (gang)
1566.         sz += (int) (strlen(gang) + sizeof " of " - sizeof "");
1567.     else if (other)
1568.         sz += (int) strlen(other);
1569. 
1570.     if (sz < BUFSZ) {
1571.         char gbuf[BUFSZ];
1572.         boolean nameit = FALSE;
1573. 
1574.         if (gang) {
1575.             Sprintf(buf, "%s of %s", upstart(orcname),
1576.                     upstart(strcpy(gbuf, gang)));
1577.             nameit = TRUE;
1578.         } else if (other) {
1579.             Sprintf(buf, "%s%s", upstart(orcname), other);
1580.             nameit = TRUE;
1581.         }
1582.         if (nameit)
1583.             mtmp = christen_monst(mtmp, buf);
1584.     }
1585.     return mtmp;
1586. }
1587. 
1588. /* Discworld novel titles, in the order that they were published; a subset
1589.    of them have index macros used for variant spellings; if the titles are
1590.    reordered for some reason, make sure that those get renumbered to match */
1591. static const char *const sir_Terry_novels[] = {
1592.     "The Colour of Magic", "The Light Fantastic", "Equal Rites", "Mort",
1593.     "Sourcery", "Wyrd Sisters", "Pyramids", "Guards! Guards!", "Eric",
1594.     "Moving Pictures", "Reaper Man", "Witches Abroad", "Small Gods",
1595.     "Lords and Ladies", "Men at Arms", "Soul Music", "Interesting Times",
1596.     "Maskerade", "Feet of Clay", "Hogfather", "Jingo", "The Last Continent",
1597.     "Carpe Jugulum", "The Fifth Elephant", "The Truth", "Thief of Time",
1598.     "The Last Hero", "The Amazing Maurice and His Educated Rodents",
1599.     "Night Watch", "The Wee Free Men", "Monstrous Regiment",
1600.     "A Hat Full of Sky", "Going Postal", "Thud!", "Wintersmith",
1601.     "Making Money", "Unseen Academicals", "I Shall Wear Midnight", "Snuff",
1602.     "Raising Steam", "The Shepherd's Crown"
1603. };
1604. #define NVL_COLOUR_OF_MAGIC 0
1605. #define NVL_SOURCERY 4
1606. #define NVL_MASKERADE 17
1607. #define NVL_AMAZING_MAURICE 27
1608. #define NVL_THUD 33
1609. 
1610. const char *
1611. noveltitle(int *novidx)
1612. {
1613.     int j, k = SIZE(sir_Terry_novels);
1614. 
1615.     j = rn2(k);
1616.     if (novidx) {
1617.         if (*novidx == -1)
1618.             *novidx = j;
1619.         else if (*novidx >= 0 && *novidx < k)
1620.             j = *novidx;
1621.     }
1622.     return sir_Terry_novels[j];
1623. }
1624. 
1625. /* figure out canonical novel title from player-specified one */
1626. const char *
1627. lookup_novel(const char *lookname, int *idx)
1628. {
1629.     int k;
1630. 
1631.     /*
1632.      * Accept variant spellings:
1633.      * _The_Colour_of_Magic_ uses British spelling, and American
1634.      * editions keep that, but we also recognize American spelling;
1635.      * _Sourcery_ is a joke rather than British spelling of "sorcery".
1636.      */
1637.     if (!strcmpi(The(lookname), "The Color of Magic"))
1638.         lookname = sir_Terry_novels[NVL_COLOUR_OF_MAGIC];
1639.     else if (!strcmpi(lookname, "Sorcery"))
1640.         lookname = sir_Terry_novels[NVL_SOURCERY];
1641.     else if (!strcmpi(lookname, "Masquerade"))
1642.         lookname = sir_Terry_novels[NVL_MASKERADE];
1643.     else if (!strcmpi(The(lookname), "The Amazing Maurice"))
1644.         lookname = sir_Terry_novels[NVL_AMAZING_MAURICE];
1645.     else if (!strcmpi(lookname, "Thud"))
1646.         lookname = sir_Terry_novels[NVL_THUD];
1647. 
1648.     for (k = 0; k < SIZE(sir_Terry_novels); ++k) {
1649.         if (!strcmpi(lookname, sir_Terry_novels[k])
1650.             || !strcmpi(The(lookname), sir_Terry_novels[k])) {
1651.             if (idx)
1652.                 *idx = k;
1653.             return sir_Terry_novels[k];
1654.         }
1655.     }
1656.     /* name not found; if novelidx is already set, override the name */
1657.     if (idx && IndexOk(*idx, sir_Terry_novels))
1658.         return sir_Terry_novels[*idx];
1659. 
1660.     return (const char *) 0;
1661. }
1662. 
1663. /*do_name.c*/