Source:NetHack 3.6.2/src/objnam.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to objnam.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/objnam.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	objnam.c	$NHDT-Date: 1551138256 2019/02/25 23:44:16 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.235 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. /* "an uncursed greased partly eaten guardian naga hatchling [corpse]" */
9. #define PREFIX 80 /* (56) */
10. #define SCHAR_LIM 127
11. #define NUMOBUF 12
12. 
13. STATIC_DCL char *FDECL(strprepend, (char *, const char *));
14. STATIC_DCL short FDECL(rnd_otyp_by_wpnskill, (SCHAR_P));
15. STATIC_DCL short FDECL(rnd_otyp_by_namedesc, (const char *, CHAR_P, int));
16. STATIC_DCL boolean FDECL(wishymatch, (const char *, const char *, BOOLEAN_P));
17. STATIC_DCL char *NDECL(nextobuf);
18. STATIC_DCL void FDECL(releaseobuf, (char *));
19. STATIC_DCL char *FDECL(minimal_xname, (struct obj *));
20. STATIC_DCL void FDECL(add_erosion_words, (struct obj *, char *));
21. STATIC_DCL char *FDECL(doname_base, (struct obj *obj, unsigned));
22. STATIC_DCL char *FDECL(just_an, (char *str, const char *));
23. STATIC_DCL boolean FDECL(singplur_lookup, (char *, char *, BOOLEAN_P,
24.                                            const char *const *));
25. STATIC_DCL char *FDECL(singplur_compound, (char *));
26. STATIC_DCL char *FDECL(xname_flags, (struct obj *, unsigned));
27. STATIC_DCL boolean FDECL(badman, (const char *, BOOLEAN_P));
28. 
29. struct Jitem {
30.     int item;
31.     const char *name;
32. };
33. 
34. #define BSTRCMPI(base, ptr, str) ((ptr) < base || strcmpi((ptr), str))
35. #define BSTRNCMPI(base, ptr, str, num) \
36.     ((ptr) < base || strncmpi((ptr), str, num))
37. #define Strcasecpy(dst, src) (void) strcasecpy(dst, src)
38. 
39. /* true for gems/rocks that should have " stone" appended to their names */
40. #define GemStone(typ)                                                  \
41.     (typ == FLINT                                                      \
42.      || (objects[typ].oc_material == GEMSTONE                          \
43.          && (typ != DILITHIUM_CRYSTAL && typ != RUBY && typ != DIAMOND \
44.              && typ != SAPPHIRE && typ != BLACK_OPAL && typ != EMERALD \
45.              && typ != OPAL)))
46. 
47. STATIC_OVL struct Jitem Japanese_items[] = { { SHORT_SWORD, "wakizashi" },
48.                                              { BROADSWORD, "ninja-to" },
49.                                              { FLAIL, "nunchaku" },
50.                                              { GLAIVE, "naginata" },
51.                                              { LOCK_PICK, "osaku" },
52.                                              { WOODEN_HARP, "koto" },
53.                                              { KNIFE, "shito" },
54.                                              { PLATE_MAIL, "tanko" },
55.                                              { HELMET, "kabuto" },
56.                                              { LEATHER_GLOVES, "yugake" },
57.                                              { FOOD_RATION, "gunyoki" },
58.                                              { POT_BOOZE, "sake" },
59.                                              { 0, "" } };
60. 
61. STATIC_DCL const char *FDECL(Japanese_item_name, (int i));
62. 
63. STATIC_OVL char *
64. strprepend(s, pref)
65. register char *s;
66. register const char *pref;
67. {
68.     register int i = (int) strlen(pref);
69. 
70.     if (i > PREFIX) {
71.         impossible("PREFIX too short (for %d).", i);
72.         return s;
73.     }
74.     s -= i;
75.     (void) strncpy(s, pref, i); /* do not copy trailing 0 */
76.     return s;
77. }
78. 
79. /* manage a pool of BUFSZ buffers, so callers don't have to */
80. static char NEARDATA obufs[NUMOBUF][BUFSZ];
81. static int obufidx = 0;
82. 
83. STATIC_OVL char *
84. nextobuf()
85. {
86.     obufidx = (obufidx + 1) % NUMOBUF;
87.     return obufs[obufidx];
88. }
89. 
90. /* put the most recently allocated buffer back if possible */
91. STATIC_OVL void
92. releaseobuf(bufp)
93. char *bufp;
94. {
95.     /* caller may not know whether bufp is the most recently allocated
96.        buffer; if it isn't, do nothing; note that because of the somewhat
97.        obscure PREFIX handling for object name formatting by xname(),
98.        the pointer our caller has and is passing to us might be into the
99.        middle of an obuf rather than the address returned by nextobuf() */
100.     if (bufp >= obufs[obufidx]
101.         && bufp < obufs[obufidx] + sizeof obufs[obufidx]) /* obufs[][BUFSZ] */
102.         obufidx = (obufidx - 1 + NUMOBUF) % NUMOBUF;
103. }
104. 
105. char *
106. obj_typename(otyp)
107. register int otyp;
108. {
109.     char *buf = nextobuf();
110.     struct objclass *ocl = &objects[otyp];
111.     const char *actualn = OBJ_NAME(*ocl);
112.     const char *dn = OBJ_DESCR(*ocl);
113.     const char *un = ocl->oc_uname;
114.     int nn = ocl->oc_name_known;
115. 
116.     if (Role_if(PM_SAMURAI) && Japanese_item_name(otyp))
117.         actualn = Japanese_item_name(otyp);
118.     switch (ocl->oc_class) {
119.     case COIN_CLASS:
120.         Strcpy(buf, "coin");
121.         break;
122.     case POTION_CLASS:
123.         Strcpy(buf, "potion");
124.         break;
125.     case SCROLL_CLASS:
126.         Strcpy(buf, "scroll");
127.         break;
128.     case WAND_CLASS:
129.         Strcpy(buf, "wand");
130.         break;
131.     case SPBOOK_CLASS:
132.         if (otyp != SPE_NOVEL) {
133.             Strcpy(buf, "spellbook");
134.         } else {
135.             Strcpy(buf, !nn ? "book" : "novel");
136.             nn = 0;
137.         }
138.         break;
139.     case RING_CLASS:
140.         Strcpy(buf, "ring");
141.         break;
142.     case AMULET_CLASS:
143.         if (nn)
144.             Strcpy(buf, actualn);
145.         else
146.             Strcpy(buf, "amulet");
147.         if (un)
148.             Sprintf(eos(buf), " called %s", un);
149.         if (dn)
150.             Sprintf(eos(buf), " (%s)", dn);
151.         return buf;
152.     default:
153.         if (nn) {
154.             Strcpy(buf, actualn);
155.             if (GemStone(otyp))
156.                 Strcat(buf, " stone");
157.             if (un)
158.                 Sprintf(eos(buf), " called %s", un);
159.             if (dn)
160.                 Sprintf(eos(buf), " (%s)", dn);
161.         } else {
162.             Strcpy(buf, dn ? dn : actualn);
163.             if (ocl->oc_class == GEM_CLASS)
164.                 Strcat(buf,
165.                        (ocl->oc_material == MINERAL) ? " stone" : " gem");
166.             if (un)
167.                 Sprintf(eos(buf), " called %s", un);
168.         }
169.         return buf;
170.     }
171.     /* here for ring/scroll/potion/wand */
172.     if (nn) {
173.         if (ocl->oc_unique)
174.             Strcpy(buf, actualn); /* avoid spellbook of Book of the Dead */
175.         else
176.             Sprintf(eos(buf), " of %s", actualn);
177.     }
178.     if (un)
179.         Sprintf(eos(buf), " called %s", un);
180.     if (dn)
181.         Sprintf(eos(buf), " (%s)", dn);
182.     return buf;
183. }
184. 
185. /* less verbose result than obj_typename(); either the actual name
186.    or the description (but not both); user-assigned name is ignored */
187. char *
188. simple_typename(otyp)
189. int otyp;
190. {
191.     char *bufp, *pp, *save_uname = objects[otyp].oc_uname;
192. 
193.     objects[otyp].oc_uname = 0; /* suppress any name given by user */
194.     bufp = obj_typename(otyp);
195.     objects[otyp].oc_uname = save_uname;
196.     if ((pp = strstri(bufp, " (")) != 0)
197.         *pp = '\0'; /* strip the appended description */
198.     return bufp;
199. }
200. 
201. boolean
202. obj_is_pname(obj)
203. struct obj *obj;
204. {
205.     if (!obj->oartifact || !has_oname(obj))
206.         return FALSE;
207.     if (!program_state.gameover && !iflags.override_ID) {
208.         if (not_fully_identified(obj))
209.             return FALSE;
210.     }
211.     return TRUE;
212. }
213. 
214. /* used by distant_name() to pass extra information to xname_flags();
215.    it would be much cleaner if this were a parameter, but that would
216.    require all of the xname() and doname() calls to be modified */
217. static int distantname = 0;
218. 
219. /* Give the name of an object seen at a distance.  Unlike xname/doname,
220.  * we don't want to set dknown if it's not set already.
221.  */
222. char *
223. distant_name(obj, func)
224. struct obj *obj;
225. char *FDECL((*func), (OBJ_P));
226. {
227.     char *str;
228. 
229.     /* 3.6.1: this used to save Blind, set it, make the call, then restore
230.      * the saved value; but the Eyes of the Overworld override blindness
231.      * and let characters wearing them get dknown set for distant items.
232.      *
233.      * TODO? if the hero is wearing those Eyes, figure out whether the
234.      * object is within X-ray radius and only treat it as distant when
235.      * beyond that radius.  Logic is iffy but result might be interesting.
236.      */
237.     ++distantname;
238.     str = (*func)(obj);
239.     --distantname;
240.     return str;
241. }
242. 
243. /* convert player specified fruit name into corresponding fruit juice name
244.    ("slice of pizza" -> "pizza juice" rather than "slice of pizza juice") */
245. char *
246. fruitname(juice)
247. boolean juice; /* whether or not to append " juice" to the name */
248. {
249.     char *buf = nextobuf();
250.     const char *fruit_nam = strstri(pl_fruit, " of ");
251. 
252.     if (fruit_nam)
253.         fruit_nam += 4; /* skip past " of " */
254.     else
255.         fruit_nam = pl_fruit; /* use it as is */
256. 
257.     Sprintf(buf, "%s%s", makesingular(fruit_nam), juice ? " juice" : "");
258.     return buf;
259. }
260. 
261. /* look up a named fruit by index (1..127) */
262. struct fruit *
263. fruit_from_indx(indx)
264. int indx;
265. {
266.     struct fruit *f;
267. 
268.     for (f = ffruit; f; f = f->nextf)
269.         if (f->fid == indx)
270.             break;
271.     return f;
272. }
273. 
274. /* look up a named fruit by name */
275. struct fruit *
276. fruit_from_name(fname, exact, highest_fid)
277. const char *fname;
278. boolean exact; /* False => prefix or exact match, True = exact match only */
279. int *highest_fid; /* optional output; only valid if 'fname' isn't found */
280. {
281.     struct fruit *f, *tentativef;
282.     char *altfname;
283.     unsigned k;
284.     /*
285.      * note: named fruits are case-senstive...
286.      */
287. 
288.     if (highest_fid)
289.         *highest_fid = 0;
290.     /* first try for an exact match */
291.     for (f = ffruit; f; f = f->nextf)
292.         if (!strcmp(f->fname, fname))
293.             return f;
294.         else if (highest_fid && f->fid > *highest_fid)
295.             *highest_fid = f->fid;
296. 
297.     /* didn't match as-is; if caller is willing to accept a prefix
298.        match, try to find one; we want to find the longest prefix that
299.        matches, not the first */
300.     if (!exact) {
301.         tentativef = 0;
302.         for (f = ffruit; f; f = f->nextf) {
303.             k = strlen(f->fname);
304.             if (!strncmp(f->fname, fname, k)
305.                 && (!fname[k] || fname[k] == ' ')
306.                 && (!tentativef || k > strlen(tentativef->fname)))
307.                 tentativef = f;
308.         }
309.         f = tentativef;
310.     }
311.     /* if we still don't have a match, try singularizing the target;
312.        for exact match, that's trivial, but for prefix, it's hard */
313.     if (!f) {
314.         altfname = makesingular(fname);
315.         for (f = ffruit; f; f = f->nextf) {
316.             if (!strcmp(f->fname, altfname))
317.                 break;
318.         }
319.         releaseobuf(altfname);
320.     }
321.     if (!f && !exact) {
322.         char fnamebuf[BUFSZ], *p;
323.         unsigned fname_k = strlen(fname); /* length of assumed plural fname */
324. 
325.         tentativef = 0;
326.         for (f = ffruit; f; f = f->nextf) {
327.             k = strlen(f->fname);
328.             /* reload fnamebuf[] each iteration in case it gets modified;
329.                there's no need to recalculate fname_k */
330.             Strcpy(fnamebuf, fname);
331.             /* bug? if singular of fname is longer than plural,
332.                failing the 'fname_k > k' test could skip a viable
333.                candidate; unfortunately, we can't singularize until
334.                after stripping off trailing stuff and we can't get
335.                accurate fname_k until fname has been singularized;
336.                compromise and use 'fname_k >= k' instead of '>',
337.                accepting 1 char length discrepancy without risking
338.                false match (I hope...) */
339.             if (fname_k >= k && (p = index(&fnamebuf[k], ' ')) != 0) {
340.                 *p = '\0'; /* truncate at 1st space past length of f->fname */
341.                 altfname = makesingular(fnamebuf);
342.                 k = strlen(altfname); /* actually revised 'fname_k' */
343.                 if (!strcmp(f->fname, altfname)
344.                     && (!tentativef || k > strlen(tentativef->fname)))
345.                     tentativef = f;
346.                 releaseobuf(altfname); /* avoid churning through all obufs */
347.             }
348.         }
349.         f = tentativef;
350.     }
351.     return f;
352. }
353. 
354. /* sort the named-fruit linked list by fruit index number */
355. void
356. reorder_fruit(forward)
357. boolean forward;
358. {
359.     struct fruit *f, *allfr[1 + 127];
360.     int i, j, k = SIZE(allfr);
361. 
362.     for (i = 0; i < k; ++i)
363.         allfr[i] = (struct fruit *) 0;
364.     for (f = ffruit; f; f = f->nextf) {
365.         /* without sanity checking, this would reduce to 'allfr[f->fid]=f' */
366.         j = f->fid;
367.         if (j < 1 || j >= k) {
368.             impossible("reorder_fruit: fruit index (%d) out of range", j);
369.             return; /* don't sort after all; should never happen... */
370.         } else if (allfr[j]) {
371.             impossible("reorder_fruit: duplicate fruit index (%d)", j);
372.             return;
373.         }
374.         allfr[j] = f;
375.     }
376.     ffruit = 0; /* reset linked list; we're rebuilding it from scratch */
377.     /* slot [0] will always be empty; must start 'i' at 1 to avoid
378.        [k - i] being out of bounds during first iteration */
379.     for (i = 1; i < k; ++i) {
380.         /* for forward ordering, go through indices from high to low;
381.            for backward ordering, go from low to high */
382.         j = forward ? (k - i) : i;
383.         if (allfr[j]) {
384.             allfr[j]->nextf = ffruit;
385.             ffruit = allfr[j];
386.         }
387.     }
388. }
389. 
390. char *
391. xname(obj)
392. struct obj *obj;
393. {
394.     return xname_flags(obj, CXN_NORMAL);
395. }
396. 
397. char *
398. xname_flags(obj, cxn_flags)
399. register struct obj *obj;
400. unsigned cxn_flags; /* bitmask of CXN_xxx values */
401. {
402.     register char *buf;
403.     register int typ = obj->otyp;
404.     register struct objclass *ocl = &objects[typ];
405.     int nn = ocl->oc_name_known, omndx = obj->corpsenm;
406.     const char *actualn = OBJ_NAME(*ocl);
407.     const char *dn = OBJ_DESCR(*ocl);
408.     const char *un = ocl->oc_uname;
409.     boolean pluralize = (obj->quan != 1L) && !(cxn_flags & CXN_SINGULAR);
410.     boolean known, dknown, bknown;
411. 
412.     buf = nextobuf() + PREFIX; /* leave room for "17 -3 " */
413.     if (Role_if(PM_SAMURAI) && Japanese_item_name(typ))
414.         actualn = Japanese_item_name(typ);
415.     /* 3.6.2: this used to be part of 'dn's initialization, but it
416.        needs to come after possibly overriding 'actualn' */
417.     if (!dn)
418.         dn = actualn;
419. 
420.     buf[0] = '\0';
421.     /*
422.      * clean up known when it's tied to oc_name_known, eg after AD_DRIN
423.      * This is only required for unique objects since the article
424.      * printed for the object is tied to the combination of the two
425.      * and printing the wrong article gives away information.
426.      */
427.     if (!nn && ocl->oc_uses_known && ocl->oc_unique)
428.         obj->known = 0;
429.     if (!Blind && !distantname)
430.         obj->dknown = TRUE;
431.     if (Role_if(PM_PRIEST))
432.         obj->bknown = TRUE;
433. 
434.     if (iflags.override_ID) {
435.         known = dknown = bknown = TRUE;
436.         nn = 1;
437.     } else {
438.         known = obj->known;
439.         dknown = obj->dknown;
440.         bknown = obj->bknown;
441.     }
442. 
443.     if (obj_is_pname(obj))
444.         goto nameit;
445.     switch (obj->oclass) {
446.     case AMULET_CLASS:
447.         if (!dknown)
448.             Strcpy(buf, "amulet");
449.         else if (typ == AMULET_OF_YENDOR || typ == FAKE_AMULET_OF_YENDOR)
450.             /* each must be identified individually */
451.             Strcpy(buf, known ? actualn : dn);
452.         else if (nn)
453.             Strcpy(buf, actualn);
454.         else if (un)
455.             Sprintf(buf, "amulet called %s", un);
456.         else
457.             Sprintf(buf, "%s amulet", dn);
458.         break;
459.     case WEAPON_CLASS:
460.         if (is_poisonable(obj) && obj->opoisoned)
461.             Strcpy(buf, "poisoned ");
462.         /*FALLTHRU*/
463.     case VENOM_CLASS:
464.     case TOOL_CLASS:
465.         if (typ == LENSES)
466.             Strcpy(buf, "pair of ");
467.         else if (is_wet_towel(obj))
468.             Strcpy(buf, (obj->spe < 3) ? "moist " : "wet ");
469. 
470.         if (!dknown)
471.             Strcat(buf, dn);
472.         else if (nn)
473.             Strcat(buf, actualn);
474.         else if (un) {
475.             Strcat(buf, dn);
476.             Strcat(buf, " called ");
477.             Strcat(buf, un);
478.         } else
479.             Strcat(buf, dn);
480. 
481.         if (typ == FIGURINE && omndx != NON_PM) {
482.             char anbuf[10]; /* [4] would be enough: 'a','n',' ','\0' */
483. 
484.             Sprintf(eos(buf), " of %s%s",
485.                     just_an(anbuf, mons[omndx].mname),
486.                     mons[omndx].mname);
487.         } else if (is_wet_towel(obj)) {
488.             if (wizard)
489.                 Sprintf(eos(buf), " (%d)", obj->spe);
490.         }
491.         break;
492.     case ARMOR_CLASS:
493.         /* depends on order of the dragon scales objects */
494.         if (typ >= GRAY_DRAGON_SCALES && typ <= YELLOW_DRAGON_SCALES) {
495.             Sprintf(buf, "set of %s", actualn);
496.             break;
497.         }
498.         if (is_boots(obj) || is_gloves(obj))
499.             Strcpy(buf, "pair of ");
500. 
501.         if (obj->otyp >= ELVEN_SHIELD && obj->otyp <= ORCISH_SHIELD
502.             && !dknown) {
503.             Strcpy(buf, "shield");
504.             break;
505.         }
506.         if (obj->otyp == SHIELD_OF_REFLECTION && !dknown) {
507.             Strcpy(buf, "smooth shield");
508.             break;
509.         }
510. 
511.         if (nn) {
512.             Strcat(buf, actualn);
513.         } else if (un) {
514.             if (is_boots(obj))
515.                 Strcat(buf, "boots");
516.             else if (is_gloves(obj))
517.                 Strcat(buf, "gloves");
518.             else if (is_cloak(obj))
519.                 Strcpy(buf, "cloak");
520.             else if (is_helmet(obj))
521.                 Strcpy(buf, "helmet");
522.             else if (is_shield(obj))
523.                 Strcpy(buf, "shield");
524.             else
525.                 Strcpy(buf, "armor");
526.             Strcat(buf, " called ");
527.             Strcat(buf, un);
528.         } else
529.             Strcat(buf, dn);
530.         break;
531.     case FOOD_CLASS:
532.         if (typ == SLIME_MOLD) {
533.             struct fruit *f = fruit_from_indx(obj->spe);
534. 
535.             if (!f) {
536.                 impossible("Bad fruit #%d?", obj->spe);
537.                 Strcpy(buf, "fruit");
538.             } else {
539.                 Strcpy(buf, f->fname);
540.                 if (pluralize) {
541.                     /* ick; already pluralized fruit names
542.                        are allowed--we want to try to avoid
543.                        adding a redundant plural suffix */
544.                     Strcpy(buf, makeplural(makesingular(buf)));
545.                     pluralize = FALSE;
546.                 }
547.             }
548.             break;
549.         }
550.         if (obj->globby) {
551.             Sprintf(buf, "%s%s",
552.                     (obj->owt <= 100)
553.                        ? "small "
554.                        : (obj->owt > 500)
555.                           ? "very large "
556.                           : (obj->owt > 300)
557.                              ? "large "
558.                              : "",
559.                     actualn);
560.             break;
561.         }
562. 
563.         Strcpy(buf, actualn);
564.         if (typ == TIN && known)
565.             tin_details(obj, omndx, buf);
566.         break;
567.     case COIN_CLASS:
568.     case CHAIN_CLASS:
569.         Strcpy(buf, actualn);
570.         break;
571.     case ROCK_CLASS:
572.         if (typ == STATUE && omndx != NON_PM) {
573.             char anbuf[10];
574. 
575.             Sprintf(buf, "%s%s of %s%s",
576.                     (Role_if(PM_ARCHEOLOGIST) && (obj->spe & STATUE_HISTORIC))
577.                        ? "historic "
578.                        : "",
579.                     actualn,
580.                     type_is_pname(&mons[omndx])
581.                        ? ""
582.                        : the_unique_pm(&mons[omndx])
583.                           ? "the "
584.                           : just_an(anbuf, mons[omndx].mname),
585.                     mons[omndx].mname);
586.         } else
587.             Strcpy(buf, actualn);
588.         break;
589.     case BALL_CLASS:
590.         Sprintf(buf, "%sheavy iron ball",
591.                 (obj->owt > ocl->oc_weight) ? "very " : "");
592.         break;
593.     case POTION_CLASS:
594.         if (dknown && obj->odiluted)
595.             Strcpy(buf, "diluted ");
596.         if (nn || un || !dknown) {
597.             Strcat(buf, "potion");
598.             if (!dknown)
599.                 break;
600.             if (nn) {
601.                 Strcat(buf, " of ");
602.                 if (typ == POT_WATER && bknown
603.                     && (obj->blessed || obj->cursed)) {
604.                     Strcat(buf, obj->blessed ? "holy " : "unholy ");
605.                 }
606.                 Strcat(buf, actualn);
607.             } else {
608.                 Strcat(buf, " called ");
609.                 Strcat(buf, un);
610.             }
611.         } else {
612.             Strcat(buf, dn);
613.             Strcat(buf, " potion");
614.         }
615.         break;
616.     case SCROLL_CLASS:
617.         Strcpy(buf, "scroll");
618.         if (!dknown)
619.             break;
620.         if (nn) {
621.             Strcat(buf, " of ");
622.             Strcat(buf, actualn);
623.         } else if (un) {
624.             Strcat(buf, " called ");
625.             Strcat(buf, un);
626.         } else if (ocl->oc_magic) {
627.             Strcat(buf, " labeled ");
628.             Strcat(buf, dn);
629.         } else {
630.             Strcpy(buf, dn);
631.             Strcat(buf, " scroll");
632.         }
633.         break;
634.     case WAND_CLASS:
635.         if (!dknown)
636.             Strcpy(buf, "wand");
637.         else if (nn)
638.             Sprintf(buf, "wand of %s", actualn);
639.         else if (un)
640.             Sprintf(buf, "wand called %s", un);
641.         else
642.             Sprintf(buf, "%s wand", dn);
643.         break;
644.     case SPBOOK_CLASS:
645.         if (typ == SPE_NOVEL) { /* 3.6 tribute */
646.             if (!dknown)
647.                 Strcpy(buf, "book");
648.             else if (nn)
649.                 Strcpy(buf, actualn);
650.             else if (un)
651.                 Sprintf(buf, "novel called %s", un);
652.             else
653.                 Sprintf(buf, "%s book", dn);
654.             break;
655.             /* end of tribute */
656.         } else if (!dknown) {
657.             Strcpy(buf, "spellbook");
658.         } else if (nn) {
659.             if (typ != SPE_BOOK_OF_THE_DEAD)
660.                 Strcpy(buf, "spellbook of ");
661.             Strcat(buf, actualn);
662.         } else if (un) {
663.             Sprintf(buf, "spellbook called %s", un);
664.         } else
665.             Sprintf(buf, "%s spellbook", dn);
666.         break;
667.     case RING_CLASS:
668.         if (!dknown)
669.             Strcpy(buf, "ring");
670.         else if (nn)
671.             Sprintf(buf, "ring of %s", actualn);
672.         else if (un)
673.             Sprintf(buf, "ring called %s", un);
674.         else
675.             Sprintf(buf, "%s ring", dn);
676.         break;
677.     case GEM_CLASS: {
678.         const char *rock = (ocl->oc_material == MINERAL) ? "stone" : "gem";
679. 
680.         if (!dknown) {
681.             Strcpy(buf, rock);
682.         } else if (!nn) {
683.             if (un)
684.                 Sprintf(buf, "%s called %s", rock, un);
685.             else
686.                 Sprintf(buf, "%s %s", dn, rock);
687.         } else {
688.             Strcpy(buf, actualn);
689.             if (GemStone(typ))
690.                 Strcat(buf, " stone");
691.         }
692.         break;
693.     }
694.     default:
695.         Sprintf(buf, "glorkum %d %d %d", obj->oclass, typ, obj->spe);
696.     }
697.     if (pluralize)
698.         Strcpy(buf, makeplural(buf));
699. 
700.     if (obj->otyp == T_SHIRT && program_state.gameover) {
701.         char tmpbuf[BUFSZ];
702. 
703.         Sprintf(eos(buf), " with text \"%s\"", tshirt_text(obj, tmpbuf));
704.     }
705. 
706.     if (has_oname(obj) && dknown) {
707.         Strcat(buf, " named ");
708.  nameit:
709.         Strcat(buf, ONAME(obj));
710.     }
711. 
712.     if (!strncmpi(buf, "the ", 4))
713.         buf += 4;
714.     return buf;
715. }
716. 
717. /* similar to simple_typename but minimal_xname operates on a particular
718.    object rather than its general type; it formats the most basic info:
719.      potion                     -- if description not known
720.      brown potion               -- if oc_name_known not set
721.      potion of object detection -- if discovered
722.  */
723. STATIC_OVL char *
724. minimal_xname(obj)
725. struct obj *obj;
726. {
727.     char *bufp;
728.     struct obj bareobj;
729.     struct objclass saveobcls;
730.     int otyp = obj->otyp;
731. 
732.     /* suppress user-supplied name */
733.     saveobcls.oc_uname = objects[otyp].oc_uname;
734.     objects[otyp].oc_uname = 0;
735.     /* suppress actual name if object's description is unknown */
736.     saveobcls.oc_name_known = objects[otyp].oc_name_known;
737.     if (!obj->dknown)
738.         objects[otyp].oc_name_known = 0;
739. 
740.     /* caveat: this makes a lot of assumptions about which fields
741.        are required in order for xname() to yield a sensible result */
742.     bareobj = zeroobj;
743.     bareobj.otyp = otyp;
744.     bareobj.oclass = obj->oclass;
745.     bareobj.dknown = obj->dknown;
746.     /* suppress known except for amulets (needed for fakes and real A-of-Y) */
747.     bareobj.known = (obj->oclass == AMULET_CLASS)
748.                         ? obj->known
749.                         /* default is "on" for types which don't use it */
750.                         : !objects[otyp].oc_uses_known;
751.     bareobj.quan = 1L;         /* don't want plural */
752.     bareobj.corpsenm = NON_PM; /* suppress statue and figurine details */
753.     /* but suppressing fruit details leads to "bad fruit #0"
754.        [perhaps we should force "slime mold" rather than use xname?] */
755.     if (obj->otyp == SLIME_MOLD)
756.         bareobj.spe = obj->spe;
757. 
758.     bufp = distant_name(&bareobj, xname); /* xname(&bareobj) */
759.     if (!strncmp(bufp, "uncursed ", 9))
760.         bufp += 9; /* Role_if(PM_PRIEST) */
761. 
762.     objects[otyp].oc_uname = saveobcls.oc_uname;
763.     objects[otyp].oc_name_known = saveobcls.oc_name_known;
764.     return bufp;
765. }
766. 
767. /* xname() output augmented for multishot missile feedback */
768. char *
769. mshot_xname(obj)
770. struct obj *obj;
771. {
772.     char tmpbuf[BUFSZ];
773.     char *onm = xname(obj);
774. 
775.     if (m_shot.n > 1 && m_shot.o == obj->otyp) {
776.         /* "the Nth arrow"; value will eventually be passed to an() or
777.            The(), both of which correctly handle this "the " prefix */
778.         Sprintf(tmpbuf, "the %d%s ", m_shot.i, ordin(m_shot.i));
779.         onm = strprepend(onm, tmpbuf);
780.     }
781.     return onm;
782. }
783. 
784. /* used for naming "the unique_item" instead of "a unique_item" */
785. boolean
786. the_unique_obj(obj)
787. struct obj *obj;
788. {
789.     boolean known = (obj->known || iflags.override_ID);
790. 
791.     if (!obj->dknown && !iflags.override_ID)
792.         return FALSE;
793.     else if (obj->otyp == FAKE_AMULET_OF_YENDOR && !known)
794.         return TRUE; /* lie */
795.     else
796.         return (boolean) (objects[obj->otyp].oc_unique
797.                           && (known || obj->otyp == AMULET_OF_YENDOR));
798. }
799. 
800. /* should monster type be prefixed with "the"? (mostly used for corpses) */
801. boolean
802. the_unique_pm(ptr)
803. struct permonst *ptr;
804. {
805.     boolean uniq;
806. 
807.     /* even though monsters with personal names are unique, we want to
808.        describe them as "Name" rather than "the Name" */
809.     if (type_is_pname(ptr))
810.         return FALSE;
811. 
812.     uniq = (ptr->geno & G_UNIQ) ? TRUE : FALSE;
813.     /* high priest is unique if it includes "of <deity>", otherwise not
814.        (caller needs to handle the 1st possibility; we assume the 2nd);
815.        worm tail should be irrelevant but is included for completeness */
816.     if (ptr == &mons[PM_HIGH_PRIEST] || ptr == &mons[PM_LONG_WORM_TAIL])
817.         uniq = FALSE;
818.     /* Wizard no longer needs this; he's flagged as unique these days */
819.     if (ptr == &mons[PM_WIZARD_OF_YENDOR])
820.         uniq = TRUE;
821.     return uniq;
822. }
823. 
824. STATIC_OVL void
825. add_erosion_words(obj, prefix)
826. struct obj *obj;
827. char *prefix;
828. {
829.     boolean iscrys = (obj->otyp == CRYSKNIFE);
830.     boolean rknown;
831. 
832.     rknown = (iflags.override_ID == 0) ? obj->rknown : TRUE;
833. 
834.     if (!is_damageable(obj) && !iscrys)
835.         return;
836. 
837.     /* The only cases where any of these bits do double duty are for
838.      * rotted food and diluted potions, which are all not is_damageable().
839.      */
840.     if (obj->oeroded && !iscrys) {
841.         switch (obj->oeroded) {
842.         case 2:
843.             Strcat(prefix, "very ");
844.             break;
845.         case 3:
846.             Strcat(prefix, "thoroughly ");
847.             break;
848.         }
849.         Strcat(prefix, is_rustprone(obj) ? "rusty " : "burnt ");
850.     }
851.     if (obj->oeroded2 && !iscrys) {
852.         switch (obj->oeroded2) {
853.         case 2:
854.             Strcat(prefix, "very ");
855.             break;
856.         case 3:
857.             Strcat(prefix, "thoroughly ");
858.             break;
859.         }
860.         Strcat(prefix, is_corrodeable(obj) ? "corroded " : "rotted ");
861.     }
862.     if (rknown && obj->oerodeproof)
863.         Strcat(prefix, iscrys
864.                           ? "fixed "
865.                           : is_rustprone(obj)
866.                              ? "rustproof "
867.                              : is_corrodeable(obj)
868.                                 ? "corrodeproof " /* "stainless"? */
869.                                 : is_flammable(obj)
870.                                    ? "fireproof "
871.                                    : "");
872. }
873. 
874. /* used to prevent rust on items where rust makes no difference */
875. boolean
876. erosion_matters(obj)
877. struct obj *obj;
878. {
879.     switch (obj->oclass) {
880.     case TOOL_CLASS:
881.         /* it's possible for a rusty weptool to be polymorphed into some
882.            non-weptool iron tool, in which case the rust implicitly goes
883.            away, but it's also possible for it to be polymorphed into a
884.            non-iron tool, in which case rust also implicitly goes away,
885.            so there's no particular reason to try to handle the first
886.            instance differently [this comment belongs in poly_obj()...] */
887.         return is_weptool(obj) ? TRUE : FALSE;
888.     case WEAPON_CLASS:
889.     case ARMOR_CLASS:
890.     case BALL_CLASS:
891.     case CHAIN_CLASS:
892.         return TRUE;
893.     default:
894.         break;
895.     }
896.     return FALSE;
897. }
898. 
899. #define DONAME_WITH_PRICE 1
900. #define DONAME_VAGUE_QUAN 2
901. 
902. STATIC_OVL char *
903. doname_base(obj, doname_flags)
904. struct obj *obj;
905. unsigned doname_flags;
906. {
907.     boolean ispoisoned = FALSE,
908.             with_price = (doname_flags & DONAME_WITH_PRICE) != 0,
909.             vague_quan = (doname_flags & DONAME_VAGUE_QUAN) != 0;
910.     boolean known, dknown, cknown, bknown, lknown;
911.     int omndx = obj->corpsenm;
912.     char prefix[PREFIX];
913.     char tmpbuf[PREFIX + 1]; /* for when we have to add something at
914.                                 the start of prefix instead of the
915.                                 end (Strcat is used on the end) */
916.     register char *bp = xname(obj);
917. 
918.     if (iflags.override_ID) {
919.         known = dknown = cknown = bknown = lknown = TRUE;
920.     } else {
921.         known = obj->known;
922.         dknown = obj->dknown;
923.         cknown = obj->cknown;
924.         bknown = obj->bknown;
925.         lknown = obj->lknown;
926.     }
927. 
928.     /* When using xname, we want "poisoned arrow", and when using
929.      * doname, we want "poisoned +0 arrow".  This kludge is about the only
930.      * way to do it, at least until someone overhauls xname() and doname(),
931.      * combining both into one function taking a parameter.
932.      */
933.     /* must check opoisoned--someone can have a weirdly-named fruit */
934.     if (!strncmp(bp, "poisoned ", 9) && obj->opoisoned) {
935.         bp += 9;
936.         ispoisoned = TRUE;
937.     }
938. 
939.     if (obj->quan != 1L) {
940.         if (dknown || !vague_quan)
941.             Sprintf(prefix, "%ld ", obj->quan);
942.         else
943.             Strcpy(prefix, "some ");
944.     } else if (obj->otyp == CORPSE) {
945.         /* skip article prefix for corpses [else corpse_xname()
946.            would have to be taught how to strip it off again] */
947.         *prefix = '\0';
948.     } else if (obj_is_pname(obj) || the_unique_obj(obj)) {
949.         if (!strncmpi(bp, "the ", 4))
950.             bp += 4;
951.         Strcpy(prefix, "the ");
952.     } else {
953.         Strcpy(prefix, "a ");
954.     }
955. 
956.     /* "empty" goes at the beginning, but item count goes at the end */
957.     if (cknown
958.         /* bag of tricks: include "empty" prefix if it's known to
959.            be empty but its precise number of charges isn't known
960.            (when that is known, suffix of "(n:0)" will be appended,
961.            making the prefix be redundant; note that 'known' flag
962.            isn't set when emptiness gets discovered because then
963.            charging magic would yield known number of new charges) */
964.         && ((obj->otyp == BAG_OF_TRICKS)
965.              ? (obj->spe == 0 && !obj->known)
966.              /* not bag of tricks: empty if container which has no contents */
967.              : ((Is_container(obj) || obj->otyp == STATUE)
968.                 && !Has_contents(obj))))
969.         Strcat(prefix, "empty ");
970. 
971.     if (bknown && obj->oclass != COIN_CLASS
972.         && (obj->otyp != POT_WATER || !objects[POT_WATER].oc_name_known
973.             || (!obj->cursed && !obj->blessed))) {
974.         /* allow 'blessed clear potion' if we don't know it's holy water;
975.          * always allow "uncursed potion of water"
976.          */
977.         if (obj->cursed)
978.             Strcat(prefix, "cursed ");
979.         else if (obj->blessed)
980.             Strcat(prefix, "blessed ");
981.         else if (!iflags.implicit_uncursed
982.             /* For most items with charges or +/-, if you know how many
983.              * charges are left or what the +/- is, then you must have
984.              * totally identified the item, so "uncursed" is unnecessary,
985.              * because an identified object not described as "blessed" or
986.              * "cursed" must be uncursed.
987.              *
988.              * If the charges or +/- is not known, "uncursed" must be
989.              * printed to avoid ambiguity between an item whose curse
990.              * status is unknown, and an item known to be uncursed.
991.              */
992.                  || ((!known || !objects[obj->otyp].oc_charged
993.                       || obj->oclass == ARMOR_CLASS
994.                       || obj->oclass == RING_CLASS)
995. #ifdef MAIL
996.                      && obj->otyp != SCR_MAIL
997. #endif
998.                      && obj->otyp != FAKE_AMULET_OF_YENDOR
999.                      && obj->otyp != AMULET_OF_YENDOR
1000.                      && !Role_if(PM_PRIEST)))
1001.             Strcat(prefix, "uncursed ");
1002.     }
1003. 
1004.     if (lknown && Is_box(obj)) {
1005.         if (obj->obroken)
1006.             /* 3.6.0 used "unlockable" here but that could be misunderstood
1007.                to mean "capable of being unlocked" rather than the intended
1008.                "not capable of being locked" */
1009.             Strcat(prefix, "broken ");
1010.         else if (obj->olocked)
1011.             Strcat(prefix, "locked ");
1012.         else
1013.             Strcat(prefix, "unlocked ");
1014.     }
1015. 
1016.     if (obj->greased)
1017.         Strcat(prefix, "greased ");
1018. 
1019.     if (cknown && Has_contents(obj)) {
1020.         /* we count the number of separate stacks, which corresponds
1021.            to the number of inventory slots needed to be able to take
1022.            everything out if no merges occur */
1023.         long itemcount = count_contents(obj, FALSE, FALSE, TRUE);
1024. 
1025.         Sprintf(eos(bp), " containing %ld item%s", itemcount,
1026.                 plur(itemcount));
1027.     }
1028. 
1029.     switch (is_weptool(obj) ? WEAPON_CLASS : obj->oclass) {
1030.     case AMULET_CLASS:
1031.         if (obj->owornmask & W_AMUL)
1032.             Strcat(bp, " (being worn)");
1033.         break;
1034.     case ARMOR_CLASS:
1035.         if (obj->owornmask & W_ARMOR)
1036.             Strcat(bp, (obj == uskin) ? " (embedded in your skin)"
1037.                        /* in case of perm_invent update while Wear/Takeoff
1038.                           is in progress; check doffing() before donning()
1039.                           because donning() returns True for both cases */
1040.                        : doffing(obj) ? " (being doffed)"
1041.                          : donning(obj) ? " (being donned)"
1042.                            : " (being worn)");
1043.         /*FALLTHRU*/
1044.     case WEAPON_CLASS:
1045.         if (ispoisoned)
1046.             Strcat(prefix, "poisoned ");
1047.         add_erosion_words(obj, prefix);
1048.         if (known) {
1049.             Strcat(prefix, sitoa(obj->spe));
1050.             Strcat(prefix, " ");
1051.         }
1052.         break;
1053.     case TOOL_CLASS:
1054.         if (obj->owornmask & (W_TOOL | W_SADDLE)) { /* blindfold */
1055.             Strcat(bp, " (being worn)");
1056.             break;
1057.         }
1058.         if (obj->otyp == LEASH && obj->leashmon != 0) {
1059.             struct monst *mlsh = find_mid(obj->leashmon, FM_FMON);
1060. 
1061.             if (!mlsh) {
1062.                 impossible("leashed monster not on this level");
1063.                 obj->leashmon = 0;
1064.             } else {
1065.                 Sprintf(eos(bp), " (attached to %s)",
1066.                         noit_mon_nam(mlsh));
1067.             }
1068.             break;
1069.         }
1070.         if (obj->otyp == CANDELABRUM_OF_INVOCATION) {
1071.             if (!obj->spe)
1072.                 Strcpy(tmpbuf, "no");
1073.             else
1074.                 Sprintf(tmpbuf, "%d", obj->spe);
1075.             Sprintf(eos(bp), " (%s candle%s%s)", tmpbuf, plur(obj->spe),
1076.                     !obj->lamplit ? " attached" : ", lit");
1077.             break;
1078.         } else if (obj->otyp == OIL_LAMP || obj->otyp == MAGIC_LAMP
1079.                    || obj->otyp == BRASS_LANTERN || Is_candle(obj)) {
1080.             if (Is_candle(obj)
1081.                 && obj->age < 20L * (long) objects[obj->otyp].oc_cost)
1082.                 Strcat(prefix, "partly used ");
1083.             if (obj->lamplit)
1084.                 Strcat(bp, " (lit)");
1085.             break;
1086.         }
1087.         if (objects[obj->otyp].oc_charged)
1088.             goto charges;
1089.         break;
1090.     case WAND_CLASS:
1091.  charges:
1092.         if (known)
1093.             Sprintf(eos(bp), " (%d:%d)", (int) obj->recharged, obj->spe);
1094.         break;
1095.     case POTION_CLASS:
1096.         if (obj->otyp == POT_OIL && obj->lamplit)
1097.             Strcat(bp, " (lit)");
1098.         break;
1099.     case RING_CLASS:
1100.  ring:
1101.         if (obj->owornmask & W_RINGR)
1102.             Strcat(bp, " (on right ");
1103.         if (obj->owornmask & W_RINGL)
1104.             Strcat(bp, " (on left ");
1105.         if (obj->owornmask & W_RING) {
1106.             Strcat(bp, body_part(HAND));
1107.             Strcat(bp, ")");
1108.         }
1109.         if (known && objects[obj->otyp].oc_charged) {
1110.             Strcat(prefix, sitoa(obj->spe));
1111.             Strcat(prefix, " ");
1112.         }
1113.         break;
1114.     case FOOD_CLASS:
1115.         if (obj->oeaten)
1116.             Strcat(prefix, "partly eaten ");
1117.         if (obj->otyp == CORPSE) {
1118.             /* (quan == 1) => want corpse_xname() to supply article,
1119.                (quan != 1) => already have count or "some" as prefix;
1120.                "corpse" is already in the buffer returned by xname() */
1121.             unsigned cxarg = (((obj->quan != 1L) ? 0 : CXN_ARTICLE)
1122.                               | CXN_NOCORPSE);
1123.             char *cxstr = corpse_xname(obj, prefix, cxarg);
1124. 
1125.             Sprintf(prefix, "%s ", cxstr);
1126.             /* avoid having doname(corpse) consume an extra obuf */
1127.             releaseobuf(cxstr);
1128.         } else if (obj->otyp == EGG) {
1129. #if 0 /* corpses don't tell if they're stale either */
1130.             if (known && stale_egg(obj))
1131.                 Strcat(prefix, "stale ");
1132. #endif
1133.             if (omndx >= LOW_PM
1134.                 && (known || (mvitals[omndx].mvflags & MV_KNOWS_EGG))) {
1135.                 Strcat(prefix, mons[omndx].mname);
1136.                 Strcat(prefix, " ");
1137.                 if (obj->spe)
1138.                     Strcat(bp, " (laid by you)");
1139.             }
1140.         }
1141.         if (obj->otyp == MEAT_RING)
1142.             goto ring;
1143.         break;
1144.     case BALL_CLASS:
1145.     case CHAIN_CLASS:
1146.         add_erosion_words(obj, prefix);
1147.         if (obj->owornmask & W_BALL)
1148.             Strcat(bp, " (chained to you)");
1149.         break;
1150.     }
1151. 
1152.     if ((obj->owornmask & W_WEP) && !mrg_to_wielded) {
1153.         if (obj->quan != 1L) {
1154.             Strcat(bp, " (wielded)");
1155.         } else {
1156.             const char *hand_s = body_part(HAND);
1157. 
1158.             if (bimanual(obj))
1159.                 hand_s = makeplural(hand_s);
1160.             /* note: Sting's glow message, if added, will insert text
1161.                in front of "(weapon in hand)"'s closing paren */
1162.             Sprintf(eos(bp), " (%sweapon in %s)",
1163.                     (obj->otyp == AKLYS) ? "tethered " : "", hand_s);
1164. 
1165.             if (warn_obj_cnt && obj == uwep && (EWarn_of_mon & W_WEP) != 0L) {
1166.                 if (!Blind) /* we know bp[] ends with ')'; overwrite that */
1167.                     Sprintf(eos(bp) - 1, ", %s %s)",
1168.                             glow_verb(warn_obj_cnt, TRUE),
1169.                             glow_color(obj->oartifact));
1170.             }
1171.         }
1172.     }
1173.     if (obj->owornmask & W_SWAPWEP) {
1174.         if (u.twoweap)
1175.             Sprintf(eos(bp), " (wielded in other %s)", body_part(HAND));
1176.         else
1177.             Strcat(bp, " (alternate weapon; not wielded)");
1178.     }
1179.     if (obj->owornmask & W_QUIVER) {
1180.         switch (obj->oclass) {
1181.         case WEAPON_CLASS:
1182.             if (is_ammo(obj)) {
1183.                 if (objects[obj->otyp].oc_skill == -P_BOW) {
1184.                     /* Ammo for a bow */
1185.                     Strcat(bp, " (in quiver)");
1186.                     break;
1187.                 } else {
1188.                     /* Ammo not for a bow */
1189.                     Strcat(bp, " (in quiver pouch)");
1190.                     break;
1191.                 }
1192.             } else {
1193.                 /* Weapons not considered ammo */
1194.                 Strcat(bp, " (at the ready)");
1195.                 break;
1196.             }
1197.         /* Small things and ammo not for a bow */
1198.         case RING_CLASS:
1199.         case AMULET_CLASS:
1200.         case WAND_CLASS:
1201.         case COIN_CLASS:
1202.         case GEM_CLASS:
1203.             Strcat(bp, " (in quiver pouch)");
1204.             break;
1205.         default: /* odd things */
1206.             Strcat(bp, " (at the ready)");
1207.         }
1208.     }
1209.     /* treat 'restoring' like suppress_price because shopkeeper and
1210.        bill might not be available yet while restore is in progress */
1211.     if (iflags.suppress_price || restoring) {
1212.         ; /* don't attempt to obtain any stop pricing, even if 'with_price' */
1213.     } else if (is_unpaid(obj)) { /* in inventory or in container in invent */
1214.         long quotedprice = unpaid_cost(obj, TRUE);
1215. 
1216.         Sprintf(eos(bp), " (%s, %ld %s)",
1217.                 obj->unpaid ? "unpaid" : "contents",
1218.                 quotedprice, currency(quotedprice));
1219.     } else if (with_price) { /* on floor or in container on floor */
1220.         int nochrg = 0;
1221.         long price = get_cost_of_shop_item(obj, &nochrg);
1222. 
1223.         if (price > 0L)
1224.             Sprintf(eos(bp), " (%s, %ld %s)",
1225.                     nochrg ? "contents" : "for sale",
1226.                     price, currency(price));
1227.         else if (nochrg > 0)
1228.             Strcat(bp, " (no charge)");
1229.     }
1230.     if (!strncmp(prefix, "a ", 2)) {
1231.         /* save current prefix, without "a "; might be empty */
1232.         Strcpy(tmpbuf, prefix + 2);
1233.         /* set prefix[] to "", "a ", or "an " */
1234.         (void) just_an(prefix, *tmpbuf ? tmpbuf : bp);
1235.         /* append remainder of original prefix */
1236.         Strcat(prefix, tmpbuf);
1237.     }
1238. 
1239.     /* show weight for items (debug tourist info)
1240.      * aum is stolen from Crawl's "Arbitrary Unit of Measure" */
1241.     if (wizard && iflags.wizweight) {
1242.         Sprintf(eos(bp), " (%d aum)", obj->owt);
1243.     }
1244.     bp = strprepend(bp, prefix);
1245.     return bp;
1246. }
1247. 
1248. char *
1249. doname(obj)
1250. struct obj *obj;
1251. {
1252.     return doname_base(obj, (unsigned) 0);
1253. }
1254. 
1255. /* Name of object including price. */
1256. char *
1257. doname_with_price(obj)
1258. struct obj *obj;
1259. {
1260.     return doname_base(obj, DONAME_WITH_PRICE);
1261. }
1262. 
1263. /* "some" instead of precise quantity if obj->dknown not set */
1264. char *
1265. doname_vague_quan(obj)
1266. struct obj *obj;
1267. {
1268.     /* Used by farlook.
1269.      * If it hasn't been seen up close and quantity is more than one,
1270.      * use "some" instead of the quantity: "some gold pieces" rather
1271.      * than "25 gold pieces".  This is suboptimal, to put it mildly,
1272.      * because lookhere and pickup report the precise amount.
1273.      * Picking the item up while blind also shows the precise amount
1274.      * for inventory display, then dropping it while still blind leaves
1275.      * obj->dknown unset so the count reverts to "some" for farlook.
1276.      *
1277.      * TODO: add obj->qknown flag for 'quantity known' on stackable
1278.      * items; it could overlay obj->cknown since no containers stack.
1279.      */
1280.     return doname_base(obj, DONAME_VAGUE_QUAN);
1281. }
1282. 
1283. /* used from invent.c */
1284. boolean
1285. not_fully_identified(otmp)
1286. struct obj *otmp;
1287. {
1288.     /* gold doesn't have any interesting attributes [yet?] */
1289.     if (otmp->oclass == COIN_CLASS)
1290.         return FALSE; /* always fully ID'd */
1291.     /* check fundamental ID hallmarks first */
1292.     if (!otmp->known || !otmp->dknown
1293. #ifdef MAIL
1294.         || (!otmp->bknown && otmp->otyp != SCR_MAIL)
1295. #else
1296.         || !otmp->bknown
1297. #endif
1298.         || !objects[otmp->otyp].oc_name_known)
1299.         return TRUE;
1300.     if ((!otmp->cknown && (Is_container(otmp) || otmp->otyp == STATUE))
1301.         || (!otmp->lknown && Is_box(otmp)))
1302.         return TRUE;
1303.     if (otmp->oartifact && undiscovered_artifact(otmp->oartifact))
1304.         return TRUE;
1305.     /* otmp->rknown is the only item of interest if we reach here */
1306.     /*
1307.      *  Note:  if a revision ever allows scrolls to become fireproof or
1308.      *  rings to become shockproof, this checking will need to be revised.
1309.      *  `rknown' ID only matters if xname() will provide the info about it.
1310.      */
1311.     if (otmp->rknown
1312.         || (otmp->oclass != ARMOR_CLASS && otmp->oclass != WEAPON_CLASS
1313.             && !is_weptool(otmp)            /* (redundant) */
1314.             && otmp->oclass != BALL_CLASS)) /* (useless) */
1315.         return FALSE;
1316.     else /* lack of `rknown' only matters for vulnerable objects */
1317.         return (boolean) (is_rustprone(otmp) || is_corrodeable(otmp)
1318.                           || is_flammable(otmp));
1319. }
1320. 
1321. /* format a corpse name (xname() omits monster type; doname() calls us);
1322.    eatcorpse() also uses us for death reason when eating tainted glob */
1323. char *
1324. corpse_xname(otmp, adjective, cxn_flags)
1325. struct obj *otmp;
1326. const char *adjective;
1327. unsigned cxn_flags; /* bitmask of CXN_xxx values */
1328. {
1329.     char *nambuf = nextobuf();
1330.     int omndx = otmp->corpsenm;
1331.     boolean ignore_quan = (cxn_flags & CXN_SINGULAR) != 0,
1332.             /* suppress "the" from "the unique monster corpse" */
1333.         no_prefix = (cxn_flags & CXN_NO_PFX) != 0,
1334.             /* include "the" for "the woodchuck corpse */
1335.         the_prefix = (cxn_flags & CXN_PFX_THE) != 0,
1336.             /* include "an" for "an ogre corpse */
1337.         any_prefix = (cxn_flags & CXN_ARTICLE) != 0,
1338.             /* leave off suffix (do_name() appends "corpse" itself) */
1339.         omit_corpse = (cxn_flags & CXN_NOCORPSE) != 0,
1340.         possessive = FALSE,
1341.         glob = (otmp->otyp != CORPSE && otmp->globby);
1342.     const char *mname;
1343. 
1344.     if (glob) {
1345.         mname = OBJ_NAME(objects[otmp->otyp]); /* "glob of <monster>" */
1346.     } else if (omndx == NON_PM) { /* paranoia */
1347.         mname = "thing";
1348.         /* [Possible enhancement:  check whether corpse has monster traits
1349.             attached in order to use priestname() for priests and minions.] */
1350.     } else if (omndx == PM_ALIGNED_PRIEST) {
1351.         /* avoid "aligned priest"; it just exposes internal details */
1352.         mname = "priest";
1353.     } else {
1354.         mname = mons[omndx].mname;
1355.         if (the_unique_pm(&mons[omndx]) || type_is_pname(&mons[omndx])) {
1356.             mname = s_suffix(mname);
1357.             possessive = TRUE;
1358.             /* don't precede personal name like "Medusa" with an article */
1359.             if (type_is_pname(&mons[omndx]))
1360.                 no_prefix = TRUE;
1361.             /* always precede non-personal unique monster name like
1362.                "Oracle" with "the" unless explicitly overridden */
1363.             else if (the_unique_pm(&mons[omndx]) && !no_prefix)
1364.                 the_prefix = TRUE;
1365.         }
1366.     }
1367.     if (no_prefix)
1368.         the_prefix = any_prefix = FALSE;
1369.     else if (the_prefix)
1370.         any_prefix = FALSE; /* mutually exclusive */
1371. 
1372.     *nambuf = '\0';
1373.     /* can't use the() the way we use an() below because any capitalized
1374.        Name causes it to assume a personal name and return Name as-is;
1375.        that's usually the behavior wanted, but here we need to force "the"
1376.        to precede capitalized unique monsters (pnames are handled above) */
1377.     if (the_prefix)
1378.         Strcat(nambuf, "the ");
1379. 
1380.     if (!adjective || !*adjective) {
1381.         /* normal case:  newt corpse */
1382.         Strcat(nambuf, mname);
1383.     } else {
1384.         /* adjective positioning depends upon format of monster name */
1385.         if (possessive) /* Medusa's cursed partly eaten corpse */
1386.             Sprintf(eos(nambuf), "%s %s", mname, adjective);
1387.         else /* cursed partly eaten troll corpse */
1388.             Sprintf(eos(nambuf), "%s %s", adjective, mname);
1389.         /* in case adjective has a trailing space, squeeze it out */
1390.         mungspaces(nambuf);
1391.         /* doname() might include a count in the adjective argument;
1392.            if so, don't prepend an article */
1393.         if (digit(*adjective))
1394.             any_prefix = FALSE;
1395.     }
1396. 
1397.     if (glob) {
1398.         ; /* omit_corpse doesn't apply; quantity is always 1 */
1399.     } else if (!omit_corpse) {
1400.         Strcat(nambuf, " corpse");
1401.         /* makeplural(nambuf) => append "s" to "corpse" */
1402.         if (otmp->quan > 1L && !ignore_quan) {
1403.             Strcat(nambuf, "s");
1404.             any_prefix = FALSE; /* avoid "a newt corpses" */
1405.         }
1406.     }
1407. 
1408.     /* it's safe to overwrite our nambuf after an() has copied
1409.        its old value into another buffer */
1410.     if (any_prefix)
1411.         Strcpy(nambuf, an(nambuf));
1412. 
1413.     return nambuf;
1414. }
1415. 
1416. /* xname doesn't include monster type for "corpse"; cxname does */
1417. char *
1418. cxname(obj)
1419. struct obj *obj;
1420. {
1421.     if (obj->otyp == CORPSE)
1422.         return corpse_xname(obj, (const char *) 0, CXN_NORMAL);
1423.     return xname(obj);
1424. }
1425. 
1426. /* like cxname, but ignores quantity */
1427. char *
1428. cxname_singular(obj)
1429. struct obj *obj;
1430. {
1431.     if (obj->otyp == CORPSE)
1432.         return corpse_xname(obj, (const char *) 0, CXN_SINGULAR);
1433.     return xname_flags(obj, CXN_SINGULAR);
1434. }
1435. 
1436. /* treat an object as fully ID'd when it might be used as reason for death */
1437. char *
1438. killer_xname(obj)
1439. struct obj *obj;
1440. {
1441.     struct obj save_obj;
1442.     unsigned save_ocknown;
1443.     char *buf, *save_ocuname, *save_oname = (char *) 0;
1444. 
1445.     /* bypass object twiddling for artifacts */
1446.     if (obj->oartifact)
1447.         return bare_artifactname(obj);
1448. 
1449.     /* remember original settings for core of the object;
1450.        oextra structs other than oname don't matter here--since they
1451.        aren't modified they don't need to be saved and restored */
1452.     save_obj = *obj;
1453.     if (has_oname(obj))
1454.         save_oname = ONAME(obj);
1455. 
1456.     /* killer name should be more specific than general xname; however, exact
1457.        info like blessed/cursed and rustproof makes things be too verbose */
1458.     obj->known = obj->dknown = 1;
1459.     obj->bknown = obj->rknown = obj->greased = 0;
1460.     /* if character is a priest[ess], bknown will get toggled back on */
1461.     if (obj->otyp != POT_WATER)
1462.         obj->blessed = obj->cursed = 0;
1463.     else
1464.         obj->bknown = 1; /* describe holy/unholy water as such */
1465.     /* "killed by poisoned <obj>" would be misleading when poison is
1466.        not the cause of death and "poisoned by poisoned <obj>" would
1467.        be redundant when it is, so suppress "poisoned" prefix */
1468.     obj->opoisoned = 0;
1469.     /* strip user-supplied name; artifacts keep theirs */
1470.     if (!obj->oartifact && save_oname)
1471.         ONAME(obj) = (char *) 0;
1472.     /* temporarily identify the type of object */
1473.     save_ocknown = objects[obj->otyp].oc_name_known;
1474.     objects[obj->otyp].oc_name_known = 1;
1475.     save_ocuname = objects[obj->otyp].oc_uname;
1476.     objects[obj->otyp].oc_uname = 0; /* avoid "foo called bar" */
1477. 
1478.     /* format the object */
1479.     if (obj->otyp == CORPSE) {
1480.         buf = nextobuf();
1481.         Strcpy(buf, corpse_xname(obj, (const char *) 0, CXN_NORMAL));
1482.     } else if (obj->otyp == SLIME_MOLD) {
1483.         /* concession to "most unique deaths competition" in the annual
1484.            devnull tournament, suppress player supplied fruit names because
1485.            those can be used to fake other objects and dungeon features */
1486.         buf = nextobuf();
1487.         Sprintf(buf, "deadly slime mold%s", plur(obj->quan));
1488.     } else {
1489.         buf = xname(obj);
1490.     }
1491.     /* apply an article if appropriate; caller should always use KILLED_BY */
1492.     if (obj->quan == 1L && !strstri(buf, "'s ") && !strstri(buf, "s' "))
1493.         buf = (obj_is_pname(obj) || the_unique_obj(obj)) ? the(buf) : an(buf);
1494. 
1495.     objects[obj->otyp].oc_name_known = save_ocknown;
1496.     objects[obj->otyp].oc_uname = save_ocuname;
1497.     *obj = save_obj; /* restore object's core settings */
1498.     if (!obj->oartifact && save_oname)
1499.         ONAME(obj) = save_oname;
1500. 
1501.     return buf;
1502. }
1503. 
1504. /* xname,doname,&c with long results reformatted to omit some stuff */
1505. char *
1506. short_oname(obj, func, altfunc, lenlimit)
1507. struct obj *obj;
1508. char *FDECL((*func), (OBJ_P)),    /* main formatting routine */
1509.      *FDECL((*altfunc), (OBJ_P)); /* alternate for shortest result */
1510. unsigned lenlimit;
1511. {
1512.     struct obj save_obj;
1513.     char unamebuf[12], onamebuf[12], *save_oname, *save_uname, *outbuf;
1514. 
1515.     outbuf = (*func)(obj);
1516.     if ((unsigned) strlen(outbuf) <= lenlimit)
1517.         return outbuf;
1518. 
1519.     /* shorten called string to fairly small amount */
1520.     save_uname = objects[obj->otyp].oc_uname;
1521.     if (save_uname && strlen(save_uname) >= sizeof unamebuf) {
1522.         (void) strncpy(unamebuf, save_uname, sizeof unamebuf - 4);
1523.         Strcpy(unamebuf + sizeof unamebuf - 4, "...");
1524.         objects[obj->otyp].oc_uname = unamebuf;
1525.         releaseobuf(outbuf);
1526.         outbuf = (*func)(obj);
1527.         objects[obj->otyp].oc_uname = save_uname; /* restore called string */
1528.         if ((unsigned) strlen(outbuf) <= lenlimit)
1529.             return outbuf;
1530.     }
1531. 
1532.     /* shorten named string to fairly small amount */
1533.     save_oname = has_oname(obj) ? ONAME(obj) : 0;
1534.     if (save_oname && strlen(save_oname) >= sizeof onamebuf) {
1535.         (void) strncpy(onamebuf, save_oname, sizeof onamebuf - 4);
1536.         Strcpy(onamebuf + sizeof onamebuf - 4, "...");
1537.         ONAME(obj) = onamebuf;
1538.         releaseobuf(outbuf);
1539.         outbuf = (*func)(obj);
1540.         ONAME(obj) = save_oname; /* restore named string */
1541.         if ((unsigned) strlen(outbuf) <= lenlimit)
1542.             return outbuf;
1543.     }
1544. 
1545.     /* shorten both called and named strings;
1546.        unamebuf and onamebuf have both already been populated */
1547.     if (save_uname && strlen(save_uname) >= sizeof unamebuf && save_oname
1548.         && strlen(save_oname) >= sizeof onamebuf) {
1549.         objects[obj->otyp].oc_uname = unamebuf;
1550.         ONAME(obj) = onamebuf;
1551.         releaseobuf(outbuf);
1552.         outbuf = (*func)(obj);
1553.         if ((unsigned) strlen(outbuf) <= lenlimit) {
1554.             objects[obj->otyp].oc_uname = save_uname;
1555.             ONAME(obj) = save_oname;
1556.             return outbuf;
1557.         }
1558.     }
1559. 
1560.     /* still long; strip several name-lengthening attributes;
1561.        called and named strings are still in truncated form */
1562.     save_obj = *obj;
1563.     obj->bknown = obj->rknown = obj->greased = 0;
1564.     obj->oeroded = obj->oeroded2 = 0;
1565.     releaseobuf(outbuf);
1566.     outbuf = (*func)(obj);
1567.     if (altfunc && (unsigned) strlen(outbuf) > lenlimit) {
1568.         /* still long; use the alternate function (usually one of
1569.            the jackets around minimal_xname()) */
1570.         releaseobuf(outbuf);
1571.         outbuf = (*altfunc)(obj);
1572.     }
1573.     /* restore the object */
1574.     *obj = save_obj;
1575.     if (save_oname)
1576.         ONAME(obj) = save_oname;
1577.     if (save_uname)
1578.         objects[obj->otyp].oc_uname = save_uname;
1579. 
1580.     /* use whatever we've got, whether it's too long or not */
1581.     return outbuf;
1582. }
1583. 
1584. /*
1585.  * Used if only one of a collection of objects is named (e.g. in eat.c).
1586.  */
1587. const char *
1588. singular(otmp, func)
1589. register struct obj *otmp;
1590. char *FDECL((*func), (OBJ_P));
1591. {
1592.     long savequan;
1593.     char *nam;
1594. 
1595.     /* using xname for corpses does not give the monster type */
1596.     if (otmp->otyp == CORPSE && func == xname)
1597.         func = cxname;
1598. 
1599.     savequan = otmp->quan;
1600.     otmp->quan = 1L;
1601.     nam = (*func)(otmp);
1602.     otmp->quan = savequan;
1603.     return nam;
1604. }
1605. 
1606. /* pick "", "a ", or "an " as article for 'str'; used by an() and doname() */
1607. STATIC_OVL char *
1608. just_an(outbuf, str)
1609. char *outbuf;
1610. const char *str;
1611. {
1612.     char c0;
1613. 
1614.     *outbuf = '\0';
1615.     c0 = lowc(*str);
1616.     if (!str[1]) {
1617.         /* single letter; might be used for named fruit */
1618.         Strcpy(outbuf, index("aefhilmnosx", c0) ? "an " : "a ");
1619.     } else if (!strncmpi(str, "the ", 4) || !strcmpi(str, "molten lava")
1620.                || !strcmpi(str, "iron bars") || !strcmpi(str, "ice")) {
1621.         ; /* no article */
1622.     } else {
1623.         if ((index(vowels, c0) && strncmpi(str, "one-", 4)
1624.              && strncmpi(str, "eucalyptus", 10) && strncmpi(str, "unicorn", 7)
1625.              && strncmpi(str, "uranium", 7) && strncmpi(str, "useful", 6))
1626.             || (index("x", c0) && !index(vowels, lowc(str[1]))))
1627.             Strcpy(outbuf, "an ");
1628.         else
1629.             Strcpy(outbuf, "a ");
1630.     }
1631.     return outbuf;
1632. }
1633. 
1634. char *
1635. an(str)
1636. const char *str;
1637. {
1638.     char *buf = nextobuf();
1639. 
1640.     if (!str || !*str) {
1641.         impossible("Alphabet soup: 'an(%s)'.", str ? "\"\"" : "<null>");
1642.         return strcpy(buf, "an []");
1643.     }
1644.     (void) just_an(buf, str);
1645.     return strcat(buf, str);
1646. }
1647. 
1648. char *
1649. An(str)
1650. const char *str;
1651. {
1652.     char *tmp = an(str);
1653. 
1654.     *tmp = highc(*tmp);
1655.     return tmp;
1656. }
1657. 
1658. /*
1659.  * Prepend "the" if necessary; assumes str is a subject derived from xname.
1660.  * Use type_is_pname() for monster names, not the().  the() is idempotent.
1661.  */
1662. char *
1663. the(str)
1664. const char *str;
1665. {
1666.     char *buf = nextobuf();
1667.     boolean insert_the = FALSE;
1668. 
1669.     if (!str || !*str) {
1670.         impossible("Alphabet soup: 'the(%s)'.", str ? "\"\"" : "<null>");
1671.         return strcpy(buf, "the []");
1672.     }
1673.     if (!strncmpi(str, "the ", 4)) {
1674.         buf[0] = lowc(*str);
1675.         Strcpy(&buf[1], str + 1);
1676.         return buf;
1677.     } else if (*str < 'A' || *str > 'Z'
1678.                /* treat named fruit as not a proper name, even if player
1679.                   has assigned a capitalized proper name as his/her fruit */
1680.                || fruit_from_name(str, TRUE, (int *) 0)) {
1681.         /* not a proper name, needs an article */
1682.         insert_the = TRUE;
1683.     } else {
1684.         /* Probably a proper name, might not need an article */
1685.         register char *tmp, *named, *called;
1686.         int l;
1687. 
1688.         /* some objects have capitalized adjectives in their names */
1689.         if (((tmp = rindex(str, ' ')) != 0 || (tmp = rindex(str, '-')) != 0)
1690.             && (tmp[1] < 'A' || tmp[1] > 'Z')) {
1691.             insert_the = TRUE;
1692.         } else if (tmp && index(str, ' ') < tmp) { /* has spaces */
1693.             /* it needs an article if the name contains "of" */
1694.             tmp = strstri(str, " of ");
1695.             named = strstri(str, " named ");
1696.             called = strstri(str, " called ");
1697.             if (called && (!named || called < named))
1698.                 named = called;
1699. 
1700.             if (tmp && (!named || tmp < named)) /* found an "of" */
1701.                 insert_the = TRUE;
1702.             /* stupid special case: lacks "of" but needs "the" */
1703.             else if (!named && (l = strlen(str)) >= 31
1704.                      && !strcmp(&str[l - 31],
1705.                                 "Platinum Yendorian Express Card"))
1706.                 insert_the = TRUE;
1707.         }
1708.     }
1709.     if (insert_the)
1710.         Strcpy(buf, "the ");
1711.     else
1712.         buf[0] = '\0';
1713.     Strcat(buf, str);
1714. 
1715.     return buf;
1716. }
1717. 
1718. char *
1719. The(str)
1720. const char *str;
1721. {
1722.     char *tmp = the(str);
1723. 
1724.     *tmp = highc(*tmp);
1725.     return tmp;
1726. }
1727. 
1728. /* returns "count cxname(otmp)" or just cxname(otmp) if count == 1 */
1729. char *
1730. aobjnam(otmp, verb)
1731. struct obj *otmp;
1732. const char *verb;
1733. {
1734.     char prefix[PREFIX];
1735.     char *bp = cxname(otmp);
1736. 
1737.     if (otmp->quan != 1L) {
1738.         Sprintf(prefix, "%ld ", otmp->quan);
1739.         bp = strprepend(bp, prefix);
1740.     }
1741.     if (verb) {
1742.         Strcat(bp, " ");
1743.         Strcat(bp, otense(otmp, verb));
1744.     }
1745.     return bp;
1746. }
1747. 
1748. /* combine yname and aobjnam eg "your count cxname(otmp)" */
1749. char *
1750. yobjnam(obj, verb)
1751. struct obj *obj;
1752. const char *verb;
1753. {
1754.     char *s = aobjnam(obj, verb);
1755. 
1756.     /* leave off "your" for most of your artifacts, but prepend
1757.      * "your" for unique objects and "foo of bar" quest artifacts */
1758.     if (!carried(obj) || !obj_is_pname(obj)
1759.         || obj->oartifact >= ART_ORB_OF_DETECTION) {
1760.         char *outbuf = shk_your(nextobuf(), obj);
1761.         int space_left = BUFSZ - 1 - strlen(outbuf);
1762. 
1763.         s = strncat(outbuf, s, space_left);
1764.     }
1765.     return s;
1766. }
1767. 
1768. /* combine Yname2 and aobjnam eg "Your count cxname(otmp)" */
1769. char *
1770. Yobjnam2(obj, verb)
1771. struct obj *obj;
1772. const char *verb;
1773. {
1774.     register char *s = yobjnam(obj, verb);
1775. 
1776.     *s = highc(*s);
1777.     return s;
1778. }
1779. 
1780. /* like aobjnam, but prepend "The", not count, and use xname */
1781. char *
1782. Tobjnam(otmp, verb)
1783. struct obj *otmp;
1784. const char *verb;
1785. {
1786.     char *bp = The(xname(otmp));
1787. 
1788.     if (verb) {
1789.         Strcat(bp, " ");
1790.         Strcat(bp, otense(otmp, verb));
1791.     }
1792.     return bp;
1793. }
1794. 
1795. /* capitalized variant of doname() */
1796. char *
1797. Doname2(obj)
1798. struct obj *obj;
1799. {
1800.     char *s = doname(obj);
1801. 
1802.     *s = highc(*s);
1803.     return s;
1804. }
1805. 
1806. /* returns "[your ]xname(obj)" or "Foobar's xname(obj)" or "the xname(obj)" */
1807. char *
1808. yname(obj)
1809. struct obj *obj;
1810. {
1811.     char *s = cxname(obj);
1812. 
1813.     /* leave off "your" for most of your artifacts, but prepend
1814.      * "your" for unique objects and "foo of bar" quest artifacts */
1815.     if (!carried(obj) || !obj_is_pname(obj)
1816.         || obj->oartifact >= ART_ORB_OF_DETECTION) {
1817.         char *outbuf = shk_your(nextobuf(), obj);
1818.         int space_left = BUFSZ - 1 - strlen(outbuf);
1819. 
1820.         s = strncat(outbuf, s, space_left);
1821.     }
1822. 
1823.     return s;
1824. }
1825. 
1826. /* capitalized variant of yname() */
1827. char *
1828. Yname2(obj)
1829. struct obj *obj;
1830. {
1831.     char *s = yname(obj);
1832. 
1833.     *s = highc(*s);
1834.     return s;
1835. }
1836. 
1837. /* returns "your minimal_xname(obj)"
1838.  * or "Foobar's minimal_xname(obj)"
1839.  * or "the minimal_xname(obj)"
1840.  */
1841. char *
1842. ysimple_name(obj)
1843. struct obj *obj;
1844. {
1845.     char *outbuf = nextobuf();
1846.     char *s = shk_your(outbuf, obj); /* assert( s == outbuf ); */
1847.     int space_left = BUFSZ - 1 - strlen(s);
1848. 
1849.     return strncat(s, minimal_xname(obj), space_left);
1850. }
1851. 
1852. /* capitalized variant of ysimple_name() */
1853. char *
1854. Ysimple_name2(obj)
1855. struct obj *obj;
1856. {
1857.     char *s = ysimple_name(obj);
1858. 
1859.     *s = highc(*s);
1860.     return s;
1861. }
1862. 
1863. /* "scroll" or "scrolls" */
1864. char *
1865. simpleonames(obj)
1866. struct obj *obj;
1867. {
1868.     char *simpleoname = minimal_xname(obj);
1869. 
1870.     if (obj->quan != 1L)
1871.         simpleoname = makeplural(simpleoname);
1872.     return simpleoname;
1873. }
1874. 
1875. /* "a scroll" or "scrolls"; "a silver bell" or "the Bell of Opening" */
1876. char *
1877. ansimpleoname(obj)
1878. struct obj *obj;
1879. {
1880.     char *simpleoname = simpleonames(obj);
1881.     int otyp = obj->otyp;
1882. 
1883.     /* prefix with "the" if a unique item, or a fake one imitating same,
1884.        has been formatted with its actual name (we let typename() handle
1885.        any `known' and `dknown' checking necessary) */
1886.     if (otyp == FAKE_AMULET_OF_YENDOR)
1887.         otyp = AMULET_OF_YENDOR;
1888.     if (objects[otyp].oc_unique
1889.         && !strcmp(simpleoname, OBJ_NAME(objects[otyp])))
1890.         return the(simpleoname);
1891. 
1892.     /* simpleoname is singular if quan==1, plural otherwise */
1893.     if (obj->quan == 1L)
1894.         simpleoname = an(simpleoname);
1895.     return simpleoname;
1896. }
1897. 
1898. /* "the scroll" or "the scrolls" */
1899. char *
1900. thesimpleoname(obj)
1901. struct obj *obj;
1902. {
1903.     char *simpleoname = simpleonames(obj);
1904. 
1905.     return the(simpleoname);
1906. }
1907. 
1908. /* artifact's name without any object type or known/dknown/&c feedback */
1909. char *
1910. bare_artifactname(obj)
1911. struct obj *obj;
1912. {
1913.     char *outbuf;
1914. 
1915.     if (obj->oartifact) {
1916.         outbuf = nextobuf();
1917.         Strcpy(outbuf, artiname(obj->oartifact));
1918.         if (!strncmp(outbuf, "The ", 4))
1919.             outbuf[0] = lowc(outbuf[0]);
1920.     } else {
1921.         outbuf = xname(obj);
1922.     }
1923.     return outbuf;
1924. }
1925. 
1926. static const char *wrp[] = {
1927.     "wand",   "ring",      "potion",     "scroll", "gem",
1928.     "amulet", "spellbook", "spell book",
1929.     /* for non-specific wishes */
1930.     "weapon", "armor",     "tool",       "food",   "comestible",
1931. };
1932. static const char wrpsym[] = { WAND_CLASS,   RING_CLASS,   POTION_CLASS,
1933.                                SCROLL_CLASS, GEM_CLASS,    AMULET_CLASS,
1934.                                SPBOOK_CLASS, SPBOOK_CLASS, WEAPON_CLASS,
1935.                                ARMOR_CLASS,  TOOL_CLASS,   FOOD_CLASS,
1936.                                FOOD_CLASS };
1937. 
1938. /* return form of the verb (input plural) if xname(otmp) were the subject */
1939. char *
1940. otense(otmp, verb)
1941. struct obj *otmp;
1942. const char *verb;
1943. {
1944.     char *buf;
1945. 
1946.     /*
1947.      * verb is given in plural (without trailing s).  Return as input
1948.      * if the result of xname(otmp) would be plural.  Don't bother
1949.      * recomputing xname(otmp) at this time.
1950.      */
1951.     if (!is_plural(otmp))
1952.         return vtense((char *) 0, verb);
1953. 
1954.     buf = nextobuf();
1955.     Strcpy(buf, verb);
1956.     return buf;
1957. }
1958. 
1959. /* various singular words that vtense would otherwise categorize as plural;
1960.    also used by makesingular() to catch some special cases */
1961. static const char *const special_subjs[] = {
1962.     "erinys",  "manes", /* this one is ambiguous */
1963.     "Cyclops", "Hippocrates",     "Pelias",    "aklys",
1964.     "amnesia", "detect monsters", "paralysis", "shape changers",
1965.     "nemesis", 0
1966.     /* note: "detect monsters" and "shape changers" are normally
1967.        caught via "<something>(s) of <whatever>", but they can be
1968.        wished for using the shorter form, so we include them here
1969.        to accommodate usage by makesingular during wishing */
1970. };
1971. 
1972. /* return form of the verb (input plural) for present tense 3rd person subj */
1973. char *
1974. vtense(subj, verb)
1975. register const char *subj;
1976. register const char *verb;
1977. {
1978.     char *buf = nextobuf(), *bspot;
1979.     int len, ltmp;
1980.     const char *sp, *spot;
1981.     const char *const *spec;
1982. 
1983.     /*
1984.      * verb is given in plural (without trailing s).  Return as input
1985.      * if subj appears to be plural.  Add special cases as necessary.
1986.      * Many hard cases can already be handled by using otense() instead.
1987.      * If this gets much bigger, consider decomposing makeplural.
1988.      * Note: monster names are not expected here (except before corpse).
1989.      *
1990.      * Special case: allow null sobj to get the singular 3rd person
1991.      * present tense form so we don't duplicate this code elsewhere.
1992.      */
1993.     if (subj) {
1994.         if (!strncmpi(subj, "a ", 2) || !strncmpi(subj, "an ", 3))
1995.             goto sing;
1996.         spot = (const char *) 0;
1997.         for (sp = subj; (sp = index(sp, ' ')) != 0; ++sp) {
1998.             if (!strncmpi(sp, " of ", 4) || !strncmpi(sp, " from ", 6)
1999.                 || !strncmpi(sp, " called ", 8) || !strncmpi(sp, " named ", 7)
2000.                 || !strncmpi(sp, " labeled ", 9)) {
2001.                 if (sp != subj)
2002.                     spot = sp - 1;
2003.                 break;
2004.             }
2005.         }
2006.         len = (int) strlen(subj);
2007.         if (!spot)
2008.             spot = subj + len - 1;
2009. 
2010.         /*
2011.          * plural: anything that ends in 's', but not '*us' or '*ss'.
2012.          * Guess at a few other special cases that makeplural creates.
2013.          */
2014.         if ((lowc(*spot) == 's' && spot != subj
2015.              && !index("us", lowc(*(spot - 1))))
2016.             || !BSTRNCMPI(subj, spot - 3, "eeth", 4)
2017.             || !BSTRNCMPI(subj, spot - 3, "feet", 4)
2018.             || !BSTRNCMPI(subj, spot - 1, "ia", 2)
2019.             || !BSTRNCMPI(subj, spot - 1, "ae", 2)) {
2020.             /* check for special cases to avoid false matches */
2021.             len = (int) (spot - subj) + 1;
2022.             for (spec = special_subjs; *spec; spec++) {
2023.                 ltmp = strlen(*spec);
2024.                 if (len == ltmp && !strncmpi(*spec, subj, len))
2025.                     goto sing;
2026.                 /* also check for <prefix><space><special_subj>
2027.                    to catch things like "the invisible erinys" */
2028.                 if (len > ltmp && *(spot - ltmp) == ' '
2029.                     && !strncmpi(*spec, spot - ltmp + 1, ltmp))
2030.                     goto sing;
2031.             }
2032. 
2033.             return strcpy(buf, verb);
2034.         }
2035.         /*
2036.          * 3rd person plural doesn't end in telltale 's';
2037.          * 2nd person singular behaves as if plural.
2038.          */
2039.         if (!strcmpi(subj, "they") || !strcmpi(subj, "you"))
2040.             return strcpy(buf, verb);
2041.     }
2042. 
2043.  sing:
2044.     Strcpy(buf, verb);
2045.     len = (int) strlen(buf);
2046.     bspot = buf + len - 1;
2047. 
2048.     if (!strcmpi(buf, "are")) {
2049.         Strcasecpy(buf, "is");
2050.     } else if (!strcmpi(buf, "have")) {
2051.         Strcasecpy(bspot - 1, "s");
2052.     } else if (index("zxs", lowc(*bspot))
2053.                || (len >= 2 && lowc(*bspot) == 'h'
2054.                    && index("cs", lowc(*(bspot - 1))))
2055.                || (len == 2 && lowc(*bspot) == 'o')) {
2056.         /* Ends in z, x, s, ch, sh; add an "es" */
2057.         Strcasecpy(bspot + 1, "es");
2058.     } else if (lowc(*bspot) == 'y' && !index(vowels, lowc(*(bspot - 1)))) {
2059.         /* like "y" case in makeplural */
2060.         Strcasecpy(bspot, "ies");
2061.     } else {
2062.         Strcasecpy(bspot + 1, "s");
2063.     }
2064. 
2065.     return buf;
2066. }
2067. 
2068. struct sing_plur {
2069.     const char *sing, *plur;
2070. };
2071. 
2072. /* word pairs that don't fit into formula-based transformations;
2073.    also some suffices which have very few--often one--matches or
2074.    which aren't systematically reversible (knives, staves) */
2075. static struct sing_plur one_off[] = {
2076.     { "child",
2077.       "children" },      /* (for wise guys who give their food funny names) */
2078.     { "cubus", "cubi" }, /* in-/suc-cubus */
2079.     { "culus", "culi" }, /* homunculus */
2080.     { "djinni", "djinn" },
2081.     { "erinys", "erinyes" },
2082.     { "foot", "feet" },
2083.     { "fungus", "fungi" },
2084.     { "goose", "geese" },
2085.     { "knife", "knives" },
2086.     { "labrum", "labra" }, /* candelabrum */
2087.     { "louse", "lice" },
2088.     { "mouse", "mice" },
2089.     { "mumak", "mumakil" },
2090.     { "nemesis", "nemeses" },
2091.     { "ovum", "ova" },
2092.     { "ox", "oxen" },
2093.     { "passerby", "passersby" },
2094.     { "rtex", "rtices" }, /* vortex */
2095.     { "serum", "sera" },
2096.     { "staff", "staves" },
2097.     { "tooth", "teeth" },
2098.     { 0, 0 }
2099. };
2100. 
2101. static const char *const as_is[] = {
2102.     /* makesingular() leaves these plural due to how they're used */
2103.     "boots",   "shoes",     "gloves",    "lenses",   "scales",
2104.     "eyes",    "gauntlets", "iron bars",
2105.     /* both singular and plural are spelled the same */
2106.     "bison",   "deer",      "elk",       "fish",      "fowl",
2107.     "tuna",    "yaki",      "-hai",      "krill",     "manes",
2108.     "moose",   "ninja",     "sheep",     "ronin",     "roshi",
2109.     "shito",   "tengu",     "ki-rin",    "Nazgul",    "gunyoki",
2110.     "piranha", "samurai",   "shuriken", 0,
2111.     /* Note:  "fish" and "piranha" are collective plurals, suitable
2112.        for "wiped out all <foo>".  For "3 <foo>", they should be
2113.        "fishes" and "piranhas" instead.  We settle for collective
2114.        variant instead of attempting to support both. */
2115. };
2116. 
2117. /* singularize/pluralize decisions common to both makesingular & makeplural */
2118. STATIC_OVL boolean
2119. singplur_lookup(basestr, endstring, to_plural, alt_as_is)
2120. char *basestr, *endstring;    /* base string, pointer to eos(string) */
2121. boolean to_plural;            /* true => makeplural, false => makesingular */
2122. const char *const *alt_as_is; /* another set like as_is[] */
2123. {
2124.     const struct sing_plur *sp;
2125.     const char *same, *other, *const *as;
2126.     int al;
2127.     int baselen = strlen(basestr);
2128. 
2129.     for (as = as_is; *as; ++as) {
2130.         al = (int) strlen(*as);
2131.         if (!BSTRCMPI(basestr, endstring - al, *as))
2132.             return TRUE;
2133.     }
2134.     if (alt_as_is) {
2135.         for (as = alt_as_is; *as; ++as) {
2136.             al = (int) strlen(*as);
2137.             if (!BSTRCMPI(basestr, endstring - al, *as))
2138.                 return TRUE;
2139.         }
2140.     }
2141. 
2142.     /* avoid false hit on one_off[].plur == "lice" or .sing == "goose";
2143.        if more of these turn up, one_off[] entries will need to flagged
2144.        as to which are whole words and which are matchable as suffices
2145.        then matching in the loop below will end up becoming more complex */
2146.     if (!strcmpi(basestr, "slice")
2147.         || !strcmpi(basestr, "mongoose")) {
2148.         if (to_plural)
2149.             Strcasecpy(endstring, "s");
2150.         return TRUE;
2151.     }
2152.     /* skip "ox" -> "oxen" entry when pluralizing "<something>ox"
2153.        unless it is muskox */
2154.     if (to_plural && baselen > 2 && !strcmpi(endstring - 2, "ox")
2155.         && !(baselen > 5 && !strcmpi(endstring - 6, "muskox"))) {
2156.         /* "fox" -> "foxes" */
2157.         Strcasecpy(endstring, "es");
2158.         return TRUE;
2159.     }
2160.     if (to_plural) {
2161.         if (baselen > 2 && !strcmpi(endstring - 3, "man")
2162.             && badman(basestr, to_plural)) {
2163.             Strcasecpy(endstring, "s");
2164.             return TRUE;
2165.         }
2166.     } else {
2167.         if (baselen > 2 && !strcmpi(endstring - 3, "men")
2168.             && badman(basestr, to_plural))
2169.             return TRUE;
2170.     }
2171.     for (sp = one_off; sp->sing; sp++) {
2172.         /* check whether endstring already matches */
2173.         same = to_plural ? sp->plur : sp->sing;
2174.         al = (int) strlen(same);
2175.         if (!BSTRCMPI(basestr, endstring - al, same))
2176.             return TRUE; /* use as-is */
2177.         /* check whether it matches the inverse; if so, transform it */
2178.         other = to_plural ? sp->sing : sp->plur;
2179.         al = (int) strlen(other);
2180.         if (!BSTRCMPI(basestr, endstring - al, other)) {
2181.             Strcasecpy(endstring - al, same);
2182.             return TRUE; /* one_off[] transformation */
2183.         }
2184.     }
2185.     return FALSE;
2186. }
2187. 
2188. /* searches for common compounds, ex. lump of royal jelly */
2189. STATIC_OVL char *
2190. singplur_compound(str)
2191. char *str;
2192. {
2193.     /* if new entries are added, be sure to keep compound_start[] in sync */
2194.     static const char *const compounds[] =
2195.         {
2196.           " of ",     " labeled ", " called ",
2197.           " named ",  " above", /* lurkers above */
2198.           " versus ", " from ",    " in ",
2199.           " on ",     " a la ",    " with", /* " with "? */
2200.           " de ",     " d'",       " du ",
2201.           "-in-",     "-at-",      0
2202.         }, /* list of first characters for all compounds[] entries */
2203.         compound_start[] = " -";
2204. 
2205.     const char *const *cmpd;
2206.     char *p;
2207. 
2208.     for (p = str; *p; ++p) {
2209.         /* substring starting at p can only match if *p is found
2210.            within compound_start[] */
2211.         if (!index(compound_start, *p))
2212.             continue;
2213. 
2214.         /* check current substring against all words in the compound[] list */
2215.         for (cmpd = compounds; *cmpd; ++cmpd)
2216.             if (!strncmpi(p, *cmpd, (int) strlen(*cmpd)))
2217.                 return p;
2218.     }
2219.     /* wasn't recognized as a compound phrase */
2220.     return 0;
2221. }
2222. 
2223. /* Plural routine; once upon a time it may have been chiefly used for
2224.  * user-defined fruits, but it is now used extensively throughout the
2225.  * program.
2226.  *
2227.  * For fruit, we have to try to account for everything reasonable the
2228.  * player has; something unreasonable can still break the code.
2229.  * However, it's still a lot more accurate than "just add an 's' at the
2230.  * end", which Rogue uses...
2231.  *
2232.  * Also used for plural monster names ("Wiped out all homunculi." or the
2233.  * vanquished monsters list) and body parts.  A lot of unique monsters have
2234.  * names which get mangled by makeplural and/or makesingular.  They're not
2235.  * genocidable, and vanquished-mon handling does its own special casing
2236.  * (for uniques who've been revived and re-killed), so we don't bother
2237.  * trying to get those right here.
2238.  *
2239.  * Also misused by muse.c to convert 1st person present verbs to 2nd person.
2240.  * 3.6.0: made case-insensitive.
2241.  */
2242. char *
2243. makeplural(oldstr)
2244. const char *oldstr;
2245. {
2246.     register char *spot;
2247.     char lo_c, *str = nextobuf();
2248.     const char *excess = (char *) 0;
2249.     int len;
2250. 
2251.     if (oldstr)
2252.         while (*oldstr == ' ')
2253.             oldstr++;
2254.     if (!oldstr || !*oldstr) {
2255.         impossible("plural of null?");
2256.         Strcpy(str, "s");
2257.         return str;
2258.     }
2259.     Strcpy(str, oldstr);
2260. 
2261.     /*
2262.      * Skip changing "pair of" to "pairs of".  According to Webster, usual
2263.      * English usage is use pairs for humans, e.g. 3 pairs of dancers,
2264.      * and pair for objects and non-humans, e.g. 3 pair of boots.  We don't
2265.      * refer to pairs of humans in this game so just skip to the bottom.
2266.      */
2267.     if (!strncmpi(str, "pair of ", 8))
2268.         goto bottom;
2269. 
2270.     /* look for "foo of bar" so that we can focus on "foo" */
2271.     if ((spot = singplur_compound(str)) != 0) {
2272.         excess = oldstr + (int) (spot - str);
2273.         *spot = '\0';
2274.     } else
2275.         spot = eos(str);
2276. 
2277.     spot--;
2278.     while (spot > str && *spot == ' ')
2279.         spot--; /* Strip blanks from end */
2280.     *(spot + 1) = '\0';
2281.     /* Now spot is the last character of the string */
2282. 
2283.     len = strlen(str);
2284. 
2285.     /* Single letters */
2286.     if (len == 1 || !letter(*spot)) {
2287.         Strcpy(spot + 1, "'s");
2288.         goto bottom;
2289.     }
2290. 
2291.     /* dispense with some words which don't need pluralization */
2292.     {
2293.         static const char *const already_plural[] = {
2294.             "ae",  /* algae, larvae, &c */
2295.             "matzot", 0,
2296.         };
2297. 
2298.         /* spot+1: synch up with makesingular's usage */
2299.         if (singplur_lookup(str, spot + 1, TRUE, already_plural))
2300.             goto bottom;
2301. 
2302.         /* more of same, but not suitable for blanket loop checking */
2303.         if ((len == 2 && !strcmpi(str, "ya"))
2304.             || (len >= 3 && !strcmpi(spot - 2, " ya")))
2305.             goto bottom;
2306.     }
2307. 
2308.     /* man/men ("Wiped out all cavemen.") */
2309.     if (len >= 3 && !strcmpi(spot - 2, "man")
2310.         /* exclude shamans and humans etc */
2311.         && !badman(str, TRUE)) {
2312.         Strcasecpy(spot - 1, "en");
2313.         goto bottom;
2314.     }
2315.     if (lowc(*spot) == 'f') { /* (staff handled via one_off[]) */
2316.         lo_c = lowc(*(spot - 1));
2317.         if (len >= 3 && !strcmpi(spot - 2, "erf")) {
2318.             /* avoid "nerf" -> "nerves", "serf" -> "serves" */
2319.             ; /* fall through to default (append 's') */
2320.         } else if (index("lr", lo_c) || index(vowels, lo_c)) {
2321.             /* [aeioulr]f to [aeioulr]ves */
2322.             Strcasecpy(spot, "ves");
2323.             goto bottom;
2324.         }
2325.     }
2326.     /* ium/ia (mycelia, baluchitheria) */
2327.     if (len >= 3 && !strcmpi(spot - 2, "ium")) {
2328.         Strcasecpy(spot - 2, "ia");
2329.         goto bottom;
2330.     }
2331.     /* algae, larvae, hyphae (another fungus part) */
2332.     if ((len >= 4 && !strcmpi(spot - 3, "alga"))
2333.         || (len >= 5
2334.             && (!strcmpi(spot - 4, "hypha") || !strcmpi(spot - 4, "larva")))
2335.         || (len >= 6 && !strcmpi(spot - 5, "amoeba"))
2336.         || (len >= 8 && (!strcmpi(spot - 7, "vertebra")))) {
2337.         /* a to ae */
2338.         Strcasecpy(spot + 1, "e");
2339.         goto bottom;
2340.     }
2341.     /* fungus/fungi, homunculus/homunculi, but buses, lotuses, wumpuses */
2342.     if (len > 3 && !strcmpi(spot - 1, "us")
2343.         && !((len >= 5 && !strcmpi(spot - 4, "lotus"))
2344.              || (len >= 6 && !strcmpi(spot - 5, "wumpus")))) {
2345.         Strcasecpy(spot - 1, "i");
2346.         goto bottom;
2347.     }
2348.     /* sis/ses (nemesis) */
2349.     if (len >= 3 && !strcmpi(spot - 2, "sis")) {
2350.         Strcasecpy(spot - 1, "es");
2351.         goto bottom;
2352.     }
2353.     /* matzoh/matzot, possible food name */
2354.     if (len >= 6
2355.         && (!strcmpi(spot - 5, "matzoh") || !strcmpi(spot - 5, "matzah"))) {
2356.         Strcasecpy(spot - 1, "ot"); /* oh/ah -> ot */
2357.         goto bottom;
2358.     }
2359.     if (len >= 5
2360.         && (!strcmpi(spot - 4, "matzo") || !strcmpi(spot - 4, "matza"))) {
2361.         Strcasecpy(spot, "ot"); /* o/a -> ot */
2362.         goto bottom;
2363.     }
2364. 
2365.     /* note: -eau/-eaux (gateau, bordeau...) */
2366.     /* note: ox/oxen, VAX/VAXen, goose/geese */
2367. 
2368.     lo_c = lowc(*spot);
2369. 
2370.     /* Ends in z, x, s, ch, sh; add an "es" */
2371.     if (index("zxs", lo_c)
2372.         || (len >= 2 && lo_c == 'h' && index("cs", lowc(*(spot - 1))))
2373.         /* Kludge to get "tomatoes" and "potatoes" right */
2374.         || (len >= 4 && !strcmpi(spot - 2, "ato"))
2375.         || (len >= 5 && !strcmpi(spot - 4, "dingo"))) {
2376.         Strcasecpy(spot + 1, "es"); /* append es */
2377.         goto bottom;
2378.     }
2379.     /* Ends in y preceded by consonant (note: also "qu") change to "ies" */
2380.     if (lo_c == 'y' && !index(vowels, lowc(*(spot - 1)))) {
2381.         Strcasecpy(spot, "ies"); /* y -> ies */
2382.         goto bottom;
2383.     }
2384.     /* Default: append an 's' */
2385.     Strcasecpy(spot + 1, "s");
2386. 
2387.  bottom:
2388.     if (excess)
2389.         Strcat(str, excess);
2390.     return str;
2391. }
2392. 
2393. /*
2394.  * Singularize a string the user typed in; this helps reduce the complexity
2395.  * of readobjnam, and is also used in pager.c to singularize the string
2396.  * for which help is sought.
2397.  *
2398.  * "Manes" is ambiguous: monster type (keep s), or horse body part (drop s)?
2399.  * Its inclusion in as_is[]/special_subj[] makes it get treated as the former.
2400.  *
2401.  * A lot of unique monsters have names ending in s; plural, or singular
2402.  * from plural, doesn't make much sense for them so we don't bother trying.
2403.  * 3.6.0: made case-insensitive.
2404.  */
2405. char *
2406. makesingular(oldstr)
2407. const char *oldstr;
2408. {
2409.     register char *p, *bp;
2410.     const char *excess = 0;
2411.     char *str = nextobuf();
2412. 
2413.     if (oldstr)
2414.         while (*oldstr == ' ')
2415.             oldstr++;
2416.     if (!oldstr || !*oldstr) {
2417.         impossible("singular of null?");
2418.         str[0] = '\0';
2419.         return str;
2420.     }
2421. 
2422.     bp = strcpy(str, oldstr);
2423. 
2424.     /* check for "foo of bar" so that we can focus on "foo" */
2425.     if ((p = singplur_compound(bp)) != 0) {
2426.         excess = oldstr + (int) (p - bp);
2427.         *p = '\0';
2428.     } else
2429.         p = eos(bp);
2430. 
2431.     /* dispense with some words which don't need singularization */
2432.     if (singplur_lookup(bp, p, FALSE, special_subjs))
2433.         goto bottom;
2434. 
2435.     /* remove -s or -es (boxes) or -ies (rubies) */
2436.     if (p >= bp + 1 && lowc(p[-1]) == 's') {
2437.         if (p >= bp + 2 && lowc(p[-2]) == 'e') {
2438.             if (p >= bp + 3 && lowc(p[-3]) == 'i') { /* "ies" */
2439.                 if (!BSTRCMPI(bp, p - 7, "cookies")
2440.                     || (!BSTRCMPI(bp, p - 4, "pies")
2441.                         /* avoid false match for "harpies" */
2442.                         && (p - 4 == bp || p[-5] == ' '))
2443.                     /* alternate djinni/djinn spelling; not really needed */
2444.                     || (!BSTRCMPI(bp, p - 6, "genies")
2445.                         /* avoid false match for "progenies" */
2446.                         && (p - 6 == bp || p[-7] == ' '))
2447.                     || !BSTRCMPI(bp, p - 5, "mbies") /* zombie */
2448.                     || !BSTRCMPI(bp, p - 5, "yries")) /* valkyrie */
2449.                     goto mins;
2450.                 Strcasecpy(p - 3, "y"); /* ies -> y */
2451.                 goto bottom;
2452.             }
2453.             /* wolves, but f to ves isn't fully reversible */
2454.             if (p - 4 >= bp && (index("lr", lowc(*(p - 4)))
2455.                                 || index(vowels, lowc(*(p - 4))))
2456.                 && !BSTRCMPI(bp, p - 3, "ves")) {
2457.                 if (!BSTRCMPI(bp, p - 6, "cloves")
2458.                     || !BSTRCMPI(bp, p - 6, "nerves"))
2459.                     goto mins;
2460.                 Strcasecpy(p - 3, "f"); /* ves -> f */
2461.                 goto bottom;
2462.             }
2463.             /* note: nurses, axes but boxes, wumpuses */
2464.             if (!BSTRCMPI(bp, p - 4, "eses")
2465.                 || !BSTRCMPI(bp, p - 4, "oxes") /* boxes, foxes */
2466.                 || !BSTRCMPI(bp, p - 4, "nxes") /* lynxes */
2467.                 || !BSTRCMPI(bp, p - 4, "ches")
2468.                 || !BSTRCMPI(bp, p - 4, "uses") /* lotuses */
2469.                 || !BSTRCMPI(bp, p - 4, "sses") /* priestesses */
2470.                 || !BSTRCMPI(bp, p - 5, "atoes") /* tomatoes */
2471.                 || !BSTRCMPI(bp, p - 7, "dingoes")
2472.                 || !BSTRCMPI(bp, p - 7, "Aleaxes")) {
2473.                 *(p - 2) = '\0'; /* drop es */
2474.                 goto bottom;
2475.             } /* else fall through to mins */
2476. 
2477.             /* ends in 's' but not 'es' */
2478.         } else if (!BSTRCMPI(bp, p - 2, "us")) { /* lotus, fungus... */
2479.             if (BSTRCMPI(bp, p - 6, "tengus") /* but not these... */
2480.                 && BSTRCMPI(bp, p - 7, "hezrous"))
2481.                 goto bottom;
2482.         } else if (!BSTRCMPI(bp, p - 2, "ss")
2483.                    || !BSTRCMPI(bp, p - 5, " lens")
2484.                    || (p - 4 == bp && !strcmpi(p - 4, "lens"))) {
2485.             goto bottom;
2486.         }
2487.  mins:
2488.         *(p - 1) = '\0'; /* drop s */
2489. 
2490.     } else { /* input doesn't end in 's' */
2491. 
2492.         if (!BSTRCMPI(bp, p - 3, "men")
2493.             && !badman(bp, FALSE)) {
2494.             Strcasecpy(p - 2, "an");
2495.             goto bottom;
2496.         }
2497.         /* matzot -> matzo, algae -> alga */
2498.         if (!BSTRCMPI(bp, p - 6, "matzot") || !BSTRCMPI(bp, p - 2, "ae")) {
2499.             *(p - 1) = '\0'; /* drop t/e */
2500.             goto bottom;
2501.         }
2502.         /* balactheria -> balactherium */
2503.         if (p - 4 >= bp && !strcmpi(p - 2, "ia")
2504.             && index("lr", lowc(*(p - 3))) && lowc(*(p - 4)) == 'e') {
2505.             Strcasecpy(p - 1, "um"); /* a -> um */
2506.         }
2507. 
2508.         /* here we cannot find the plural suffix */
2509.     }
2510. 
2511.  bottom:
2512.     /* if we stripped off a suffix (" of bar" from "foo of bar"),
2513.        put it back now [strcat() isn't actually 100% safe here...] */
2514.     if (excess)
2515.         Strcat(bp, excess);
2516. 
2517.     return bp;
2518. }
2519. 
2520. boolean
2521. badman(basestr, to_plural)
2522. const char *basestr;
2523. boolean to_plural;            /* true => makeplural, false => makesingular */
2524. {
2525.     /* these are all the prefixes for *man that don't have a *men plural */
2526.     static const char *no_men[] = {
2527.         "albu", "antihu", "anti", "ata", "auto", "bildungsro", "cai", "cay",
2528.         "ceru", "corner", "decu", "des", "dura", "fir", "hanu", "het",
2529.         "infrahu", "inhu", "nonhu", "otto", "out", "prehu", "protohu",
2530.         "subhu", "superhu", "talis", "unhu", "sha",
2531.         "hu", "un", "le", "re", "so", "to", "at", "a",
2532.     };
2533.     /* these are all the prefixes for *men that don't have a *man singular */
2534.     static const char *no_man[] = {
2535.         "abdo", "acu", "agno", "ceru", "cogno", "cycla", "fleh", "grava",
2536.         "hegu", "preno", "sonar", "speci", "dai", "exa", "fla", "sta", "teg",
2537.         "tegu", "vela", "da", "hy", "lu", "no", "nu", "ra", "ru", "se", "vi",
2538.         "ya", "o", "a",
2539.     };
2540.     int i, al;
2541.     const char *endstr, *spot;
2542. 
2543.     if (!basestr || strlen(basestr) < 4)
2544.         return FALSE;
2545. 
2546.     endstr = eos((char *) basestr);
2547. 
2548.     if (to_plural) {
2549.         for (i = 0; i < SIZE(no_men); i++) {
2550.             al = (int) strlen(no_men[i]);
2551.             spot = endstr - (al + 3);
2552.             if (!BSTRNCMPI(basestr, spot, no_men[i], al)
2553.                 && (spot == basestr || *(spot - 1) == ' '))
2554.                 return TRUE;
2555.         }
2556.     } else {
2557.         for (i = 0; i < SIZE(no_man); i++) {
2558.             al = (int) strlen(no_man[i]);
2559.             spot = endstr - (al + 3);
2560.             if (!BSTRNCMPI(basestr, spot, no_man[i], al)
2561.                 && (spot == basestr || *(spot - 1) == ' '))
2562.                 return TRUE;
2563.         }
2564.     }
2565.     return FALSE;
2566. }
2567. 
2568. /* compare user string against object name string using fuzzy matching */
2569. STATIC_OVL boolean
2570. wishymatch(u_str, o_str, retry_inverted)
2571. const char *u_str;      /* from user, so might be variant spelling */
2572. const char *o_str;      /* from objects[], so is in canonical form */
2573. boolean retry_inverted; /* optional extra "of" handling */
2574. {
2575.     static NEARDATA const char detect_SP[] = "detect ",
2576.                                SP_detection[] = " detection";
2577.     char *p, buf[BUFSZ];
2578. 
2579.     /* ignore spaces & hyphens and upper/lower case when comparing */
2580.     if (fuzzymatch(u_str, o_str, " -", TRUE))
2581.         return TRUE;
2582. 
2583.     if (retry_inverted) {
2584.         const char *u_of, *o_of;
2585. 
2586.         /* when just one of the strings is in the form "foo of bar",
2587.            convert it into "bar foo" and perform another comparison */
2588.         u_of = strstri(u_str, " of ");
2589.         o_of = strstri(o_str, " of ");
2590.         if (u_of && !o_of) {
2591.             Strcpy(buf, u_of + 4);
2592.             p = eos(strcat(buf, " "));
2593.             while (u_str < u_of)
2594.                 *p++ = *u_str++;
2595.             *p = '\0';
2596.             return fuzzymatch(buf, o_str, " -", TRUE);
2597.         } else if (o_of && !u_of) {
2598.             Strcpy(buf, o_of + 4);
2599.             p = eos(strcat(buf, " "));
2600.             while (o_str < o_of)
2601.                 *p++ = *o_str++;
2602.             *p = '\0';
2603.             return fuzzymatch(u_str, buf, " -", TRUE);
2604.         }
2605.     }
2606. 
2607.     /* [note: if something like "elven speed boots" ever gets added, these
2608.        special cases should be changed to call wishymatch() recursively in
2609.        order to get the "of" inversion handling] */
2610.     if (!strncmp(o_str, "dwarvish ", 9)) {
2611.         if (!strncmpi(u_str, "dwarven ", 8))
2612.             return fuzzymatch(u_str + 8, o_str + 9, " -", TRUE);
2613.     } else if (!strncmp(o_str, "elven ", 6)) {
2614.         if (!strncmpi(u_str, "elvish ", 7))
2615.             return fuzzymatch(u_str + 7, o_str + 6, " -", TRUE);
2616.         else if (!strncmpi(u_str, "elfin ", 6))
2617.             return fuzzymatch(u_str + 6, o_str + 6, " -", TRUE);
2618.     } else if (!strncmp(o_str, detect_SP, sizeof detect_SP - 1)) {
2619.         /* check for "detect <foo>" vs "<foo> detection" */
2620.         if ((p = strstri(u_str, SP_detection)) != 0
2621.             && !*(p + sizeof SP_detection - 1)) {
2622.             /* convert "<foo> detection" into "detect <foo>" */
2623.             *p = '\0';
2624.             Strcat(strcpy(buf, detect_SP), u_str);
2625.             /* "detect monster" -> "detect monsters" */
2626.             if (!strcmpi(u_str, "monster"))
2627.                 Strcat(buf, "s");
2628.             *p = ' ';
2629.             return fuzzymatch(buf, o_str, " -", TRUE);
2630.         }
2631.     } else if (strstri(o_str, SP_detection)) {
2632.         /* and the inverse, "<foo> detection" vs "detect <foo>" */
2633.         if (!strncmpi(u_str, detect_SP, sizeof detect_SP - 1)) {
2634.             /* convert "detect <foo>s" into "<foo> detection" */
2635.             p = makesingular(u_str + sizeof detect_SP - 1);
2636.             Strcat(strcpy(buf, p), SP_detection);
2637.             /* caller may be looping through objects[], so avoid
2638.                churning through all the obufs */
2639.             releaseobuf(p);
2640.             return fuzzymatch(buf, o_str, " -", TRUE);
2641.         }
2642.     } else if (strstri(o_str, "ability")) {
2643.         /* when presented with "foo of bar", makesingular() used to
2644.            singularize both foo & bar, but now only does so for foo */
2645.         /* catch "{potion(s),ring} of {gain,restore,sustain} abilities" */
2646.         if ((p = strstri(u_str, "abilities")) != 0
2647.             && !*(p + sizeof "abilities" - 1)) {
2648.             (void) strncpy(buf, u_str, (unsigned) (p - u_str));
2649.             Strcpy(buf + (p - u_str), "ability");
2650.             return fuzzymatch(buf, o_str, " -", TRUE);
2651.         }
2652.     } else if (!strcmp(o_str, "aluminum")) {
2653.         /* this special case doesn't really fit anywhere else... */
2654.         /* (note that " wand" will have been stripped off by now) */
2655.         if (!strcmpi(u_str, "aluminium"))
2656.             return fuzzymatch(u_str + 9, o_str + 8, " -", TRUE);
2657.     }
2658. 
2659.     return FALSE;
2660. }
2661. 
2662. struct o_range {
2663.     const char *name, oclass;
2664.     int f_o_range, l_o_range;
2665. };
2666. 
2667. /* wishable subranges of objects */
2668. STATIC_OVL NEARDATA const struct o_range o_ranges[] = {
2669.     { "bag", TOOL_CLASS, SACK, BAG_OF_TRICKS },
2670.     { "lamp", TOOL_CLASS, OIL_LAMP, MAGIC_LAMP },
2671.     { "candle", TOOL_CLASS, TALLOW_CANDLE, WAX_CANDLE },
2672.     { "horn", TOOL_CLASS, TOOLED_HORN, HORN_OF_PLENTY },
2673.     { "shield", ARMOR_CLASS, SMALL_SHIELD, SHIELD_OF_REFLECTION },
2674.     { "hat", ARMOR_CLASS, FEDORA, DUNCE_CAP },
2675.     { "helm", ARMOR_CLASS, ELVEN_LEATHER_HELM, HELM_OF_TELEPATHY },
2676.     { "gloves", ARMOR_CLASS, LEATHER_GLOVES, GAUNTLETS_OF_DEXTERITY },
2677.     { "gauntlets", ARMOR_CLASS, LEATHER_GLOVES, GAUNTLETS_OF_DEXTERITY },
2678.     { "boots", ARMOR_CLASS, LOW_BOOTS, LEVITATION_BOOTS },
2679.     { "shoes", ARMOR_CLASS, LOW_BOOTS, IRON_SHOES },
2680.     { "cloak", ARMOR_CLASS, MUMMY_WRAPPING, CLOAK_OF_DISPLACEMENT },
2681.     { "shirt", ARMOR_CLASS, HAWAIIAN_SHIRT, T_SHIRT },
2682.     { "dragon scales", ARMOR_CLASS, GRAY_DRAGON_SCALES,
2683.       YELLOW_DRAGON_SCALES },
2684.     { "dragon scale mail", ARMOR_CLASS, GRAY_DRAGON_SCALE_MAIL,
2685.       YELLOW_DRAGON_SCALE_MAIL },
2686.     { "sword", WEAPON_CLASS, SHORT_SWORD, KATANA },
2687.     { "venom", VENOM_CLASS, BLINDING_VENOM, ACID_VENOM },
2688.     { "gray stone", GEM_CLASS, LUCKSTONE, FLINT },
2689.     { "grey stone", GEM_CLASS, LUCKSTONE, FLINT },
2690. };
2691. 
2692. /* alternate spellings; if the difference is only the presence or
2693.    absence of spaces and/or hyphens (such as "pickaxe" vs "pick axe"
2694.    vs "pick-axe") then there is no need for inclusion in this list;
2695.    likewise for ``"of" inversions ("boots of speed" vs "speed boots") */
2696. static const struct alt_spellings {
2697.     const char *sp;
2698.     int ob;
2699. } spellings[] = {
2700.     { "pickax", PICK_AXE },
2701.     { "whip", BULLWHIP },
2702.     { "saber", SILVER_SABER },
2703.     { "silver sabre", SILVER_SABER },
2704.     { "smooth shield", SHIELD_OF_REFLECTION },
2705.     { "grey dragon scale mail", GRAY_DRAGON_SCALE_MAIL },
2706.     { "grey dragon scales", GRAY_DRAGON_SCALES },
2707.     { "iron ball", HEAVY_IRON_BALL },
2708.     { "lantern", BRASS_LANTERN },
2709.     { "mattock", DWARVISH_MATTOCK },
2710.     { "amulet of poison resistance", AMULET_VERSUS_POISON },
2711.     { "potion of sleep", POT_SLEEPING },
2712.     { "stone", ROCK },
2713.     { "camera", EXPENSIVE_CAMERA },
2714.     { "tee shirt", T_SHIRT },
2715.     { "can", TIN },
2716.     { "can opener", TIN_OPENER },
2717.     { "kelp", KELP_FROND },
2718.     { "eucalyptus", EUCALYPTUS_LEAF },
2719.     { "royal jelly", LUMP_OF_ROYAL_JELLY },
2720.     { "lembas", LEMBAS_WAFER },
2721.     { "cookie", FORTUNE_COOKIE },
2722.     { "pie", CREAM_PIE },
2723.     { "marker", MAGIC_MARKER },
2724.     { "hook", GRAPPLING_HOOK },
2725.     { "grappling iron", GRAPPLING_HOOK },
2726.     { "grapnel", GRAPPLING_HOOK },
2727.     { "grapple", GRAPPLING_HOOK },
2728.     { "protection from shape shifters", RIN_PROTECTION_FROM_SHAPE_CHAN },
2729.     /* if we ever add other sizes, move this to o_ranges[] with "bag" */
2730.     { "box", LARGE_BOX },
2731.     /* normally we wouldn't have to worry about unnecessary <space>, but
2732.        " stone" will get stripped off, preventing a wishymatch; that actually
2733.        lets "flint stone" be a match, so we also accept bogus "flintstone" */
2734.     { "luck stone", LUCKSTONE },
2735.     { "load stone", LOADSTONE },
2736.     { "touch stone", TOUCHSTONE },
2737.     { "flintstone", FLINT },
2738.     { (const char *) 0, 0 },
2739. };
2740. 
2741. STATIC_OVL short
2742. rnd_otyp_by_wpnskill(skill)
2743. schar skill;
2744. {
2745.     int i, n = 0;
2746.     short otyp = STRANGE_OBJECT;
2747. 
2748.     for (i = bases[WEAPON_CLASS];
2749.          i < NUM_OBJECTS && objects[i].oc_class == WEAPON_CLASS; i++)
2750.         if (objects[i].oc_skill == skill) {
2751.             n++;
2752.             otyp = i;
2753.         }
2754.     if (n > 0) {
2755.         n = rn2(n);
2756.         for (i = bases[WEAPON_CLASS];
2757.              i < NUM_OBJECTS && objects[i].oc_class == WEAPON_CLASS; i++)
2758.             if (objects[i].oc_skill == skill)
2759.                 if (--n < 0)
2760.                     return i;
2761.     }
2762.     return otyp;
2763. }
2764. 
2765. STATIC_OVL short
2766. rnd_otyp_by_namedesc(name, oclass, xtra_prob)
2767. const char *name;
2768. char oclass;
2769. int xtra_prob; /* to force 0% random generation items to also be considered */
2770. {
2771.     int i, n = 0;
2772.     short validobjs[NUM_OBJECTS];
2773.     register const char *zn;
2774.     int prob, maxprob = 0;
2775. 
2776.     if (!name || !*name)
2777.         return STRANGE_OBJECT;
2778. 
2779.     memset((genericptr_t) validobjs, 0, sizeof validobjs);
2780. 
2781.     /* FIXME:
2782.      * When this spans classes (the !oclass case), the item
2783.      * probabilities are not very useful because they don't take
2784.      * the class generation probability into account.  [If 10%
2785.      * of spellbooks were blank and 1% of scrolls were blank,
2786.      * "blank" would have 10/11 chance to yield a blook even though
2787.      * scrolls are supposed to be much more common than books.]
2788.      */
2789.     for (i = oclass ? bases[(int) oclass] : STRANGE_OBJECT + 1;
2790.          i < NUM_OBJECTS && (!oclass || objects[i].oc_class == oclass);
2791.          ++i) {
2792.         /* don't match extra descriptions (w/o real name) */
2793.         if ((zn = OBJ_NAME(objects[i])) == 0)
2794.             continue;
2795.         if (wishymatch(name, zn, TRUE)
2796.             || ((zn = OBJ_DESCR(objects[i])) != 0
2797.                 && wishymatch(name, zn, FALSE))
2798.             || ((zn = objects[i].oc_uname) != 0
2799.                 && wishymatch(name, zn, FALSE))) {
2800.             validobjs[n++] = (short) i;
2801.             maxprob += (objects[i].oc_prob + xtra_prob);
2802.         }
2803.     }
2804. 
2805.     if (n > 0 && maxprob) {
2806.         prob = rn2(maxprob);
2807.         for (i = 0; i < n - 1; i++)
2808.             if ((prob -= (objects[validobjs[i]].oc_prob + xtra_prob)) < 0)
2809.                 break;
2810.         return validobjs[i];
2811.     }
2812.     return STRANGE_OBJECT;
2813. }
2814. 
2815. int
2816. shiny_obj(oclass)
2817. char oclass;
2818. {
2819.     return (int) rnd_otyp_by_namedesc("shiny", oclass, 0);
2820. }
2821. 
2822. /*
2823.  * Return something wished for.  Specifying a null pointer for
2824.  * the user request string results in a random object.  Otherwise,
2825.  * if asking explicitly for "nothing" (or "nil") return no_wish;
2826.  * if not an object return &zeroobj; if an error (no matching object),
2827.  * return null.
2828.  */
2829. struct obj *
2830. readobjnam(bp, no_wish)
2831. register char *bp;
2832. struct obj *no_wish;
2833. {
2834.     register char *p;
2835.     register int i;
2836.     register struct obj *otmp;
2837.     int cnt, spe, spesgn, typ, very, rechrg;
2838.     int blessed, uncursed, iscursed, ispoisoned, isgreased;
2839.     int eroded, eroded2, erodeproof, locked, unlocked, broken;
2840.     int halfeaten, mntmp, contents;
2841.     int islit, unlabeled, ishistoric, isdiluted, trapped;
2842.     int tmp, tinv, tvariety;
2843.     int wetness, gsize = 0;
2844.     struct fruit *f;
2845.     int ftype = context.current_fruit;
2846.     char fruitbuf[BUFSZ], globbuf[BUFSZ];
2847.     /* Fruits may not mess up the ability to wish for real objects (since
2848.      * you can leave a fruit in a bones file and it will be added to
2849.      * another person's game), so they must be checked for last, after
2850.      * stripping all the possible prefixes and seeing if there's a real
2851.      * name in there.  So we have to save the full original name.  However,
2852.      * it's still possible to do things like "uncursed burnt Alaska",
2853.      * or worse yet, "2 burned 5 course meals", so we need to loop to
2854.      * strip off the prefixes again, this time stripping only the ones
2855.      * possible on food.
2856.      * We could get even more detailed so as to allow food names with
2857.      * prefixes that _are_ possible on food, so you could wish for
2858.      * "2 3 alarm chilis".  Currently this isn't allowed; options.c
2859.      * automatically sticks 'candied' in front of such names.
2860.      */
2861.     char oclass;
2862.     char *un, *dn, *actualn, *origbp = bp;
2863.     const char *name = 0;
2864. 
2865.     cnt = spe = spesgn = typ = 0;
2866.     very = rechrg = blessed = uncursed = iscursed = ispoisoned =
2867.         isgreased = eroded = eroded2 = erodeproof = halfeaten =
2868.         islit = unlabeled = ishistoric = isdiluted = trapped =
2869.         locked = unlocked = broken = 0;
2870.     tvariety = RANDOM_TIN;
2871.     mntmp = NON_PM;
2872. #define UNDEFINED 0
2873. #define EMPTY 1
2874. #define SPINACH 2
2875.     contents = UNDEFINED;
2876.     oclass = 0;
2877.     actualn = dn = un = 0;
2878.     wetness = 0;
2879. 
2880.     if (!bp)
2881.         goto any;
2882.     /* first, remove extra whitespace they may have typed */
2883.     (void) mungspaces(bp);
2884.     /* allow wishing for "nothing" to preserve wishless conduct...
2885.        [now requires "wand of nothing" if that's what was really wanted] */
2886.     if (!strcmpi(bp, "nothing") || !strcmpi(bp, "nil")
2887.         || !strcmpi(bp, "none"))
2888.         return no_wish;
2889.     /* save the [nearly] unmodified choice string */
2890.     Strcpy(fruitbuf, bp);
2891. 
2892.     for (;;) {
2893.         register int l;
2894. 
2895.         if (!bp || !*bp)
2896.             goto any;
2897.         if (!strncmpi(bp, "an ", l = 3) || !strncmpi(bp, "a ", l = 2)) {
2898.             cnt = 1;
2899.         } else if (!strncmpi(bp, "the ", l = 4)) {
2900.             ; /* just increment `bp' by `l' below */
2901.         } else if (!cnt && digit(*bp) && strcmp(bp, "0")) {
2902.             cnt = atoi(bp);
2903.             while (digit(*bp))
2904.                 bp++;
2905.             while (*bp == ' ')
2906.                 bp++;
2907.             l = 0;
2908.         } else if (*bp == '+' || *bp == '-') {
2909.             spesgn = (*bp++ == '+') ? 1 : -1;
2910.             spe = atoi(bp);
2911.             while (digit(*bp))
2912.                 bp++;
2913.             while (*bp == ' ')
2914.                 bp++;
2915.             l = 0;
2916.         } else if (!strncmpi(bp, "blessed ", l = 8)
2917.                    || !strncmpi(bp, "holy ", l = 5)) {
2918.             blessed = 1;
2919.         } else if (!strncmpi(bp, "moist ", l = 6)
2920.                    || !strncmpi(bp, "wet ", l = 4)) {
2921.             if (!strncmpi(bp, "wet ", 4))
2922.                 wetness = rn2(3) + 3;
2923.             else
2924.                 wetness = rnd(2);
2925.         } else if (!strncmpi(bp, "cursed ", l = 7)
2926.                    || !strncmpi(bp, "unholy ", l = 7)) {
2927.             iscursed = 1;
2928.         } else if (!strncmpi(bp, "uncursed ", l = 9)) {
2929.             uncursed = 1;
2930.         } else if (!strncmpi(bp, "rustproof ", l = 10)
2931.                    || !strncmpi(bp, "erodeproof ", l = 11)
2932.                    || !strncmpi(bp, "corrodeproof ", l = 13)
2933.                    || !strncmpi(bp, "fixed ", l = 6)
2934.                    || !strncmpi(bp, "fireproof ", l = 10)
2935.                    || !strncmpi(bp, "rotproof ", l = 9)) {
2936.             erodeproof = 1;
2937.         } else if (!strncmpi(bp, "lit ", l = 4)
2938.                    || !strncmpi(bp, "burning ", l = 8)) {
2939.             islit = 1;
2940.         } else if (!strncmpi(bp, "unlit ", l = 6)
2941.                    || !strncmpi(bp, "extinguished ", l = 13)) {
2942.             islit = 0;
2943.             /* "unlabeled" and "blank" are synonymous */
2944.         } else if (!strncmpi(bp, "unlabeled ", l = 10)
2945.                    || !strncmpi(bp, "unlabelled ", l = 11)
2946.                    || !strncmpi(bp, "blank ", l = 6)) {
2947.             unlabeled = 1;
2948.         } else if (!strncmpi(bp, "poisoned ", l = 9)) {
2949.             ispoisoned = 1;
2950.             /* "trapped" recognized but not honored outside wizard mode */
2951.         } else if (!strncmpi(bp, "trapped ", l = 8)) {
2952.             trapped = 0; /* undo any previous "untrapped" */
2953.             if (wizard)
2954.                 trapped = 1;
2955.         } else if (!strncmpi(bp, "untrapped ", l = 10)) {
2956.             trapped = 2; /* not trapped */
2957.         /* locked, unlocked, broken: box/chest lock states */
2958.         } else if (!strncmpi(bp, "locked ", l = 7)) {
2959.             locked = 1, unlocked = broken = 0;
2960.         } else if (!strncmpi(bp, "unlocked ", l = 9)) {
2961.             unlocked = 1, locked = broken = 0;
2962.         } else if (!strncmpi(bp, "broken ", l = 7)) {
2963.             broken = 1, locked = unlocked = 0;
2964.         } else if (!strncmpi(bp, "greased ", l = 8)) {
2965.             isgreased = 1;
2966.         } else if (!strncmpi(bp, "very ", l = 5)) {
2967.             /* very rusted very heavy iron ball */
2968.             very = 1;
2969.         } else if (!strncmpi(bp, "thoroughly ", l = 11)) {
2970.             very = 2;
2971.         } else if (!strncmpi(bp, "rusty ", l = 6)
2972.                    || !strncmpi(bp, "rusted ", l = 7)
2973.                    || !strncmpi(bp, "burnt ", l = 6)
2974.                    || !strncmpi(bp, "burned ", l = 7)) {
2975.             eroded = 1 + very;
2976.             very = 0;
2977.         } else if (!strncmpi(bp, "corroded ", l = 9)
2978.                    || !strncmpi(bp, "rotted ", l = 7)) {
2979.             eroded2 = 1 + very;
2980.             very = 0;
2981.         } else if (!strncmpi(bp, "partly eaten ", l = 13)
2982.                    || !strncmpi(bp, "partially eaten ", l = 16)) {
2983.             halfeaten = 1;
2984.         } else if (!strncmpi(bp, "historic ", l = 9)) {
2985.             ishistoric = 1;
2986.         } else if (!strncmpi(bp, "diluted ", l = 8)) {
2987.             isdiluted = 1;
2988.         } else if (!strncmpi(bp, "empty ", l = 6)) {
2989.             contents = EMPTY;
2990.         } else if (!strncmpi(bp, "small ", l = 6)) { /* glob sizes */
2991.             /* "small" might be part of monster name (mimic, if wishing
2992.                for its corpse) rather than prefix for glob size; when
2993.                used for globs, it might be either "small glob of <foo>" or
2994.                "small <foo> glob" and user might add 's' even though plural
2995.                doesn't accomplish anything because globs don't stack */
2996.             if (strncmpi(bp + l, "glob", 4) && !strstri(bp + l, " glob"))
2997.                 break;
2998.             gsize = 1;
2999.         } else if (!strncmpi(bp, "medium ", l = 7)) {
3000.             /* xname() doesn't display "medium" but without this
3001.                there'd be no way to ask for the intermediate size
3002.                ("glob" without size prefix yields smallest one) */
3003.             gsize = 2;
3004.         } else if (!strncmpi(bp, "large ", l = 6)) {
3005.             /* "large" might be part of monster name (dog, cat, koboold,
3006.                mimic) or object name (box, round shield) rather than
3007.                prefix for glob size */
3008.             if (strncmpi(bp + l, "glob", 4) && !strstri(bp + l, " glob"))
3009.                 break;
3010.             /* "very large " had "very " peeled off on previous iteration */
3011.             gsize = (very != 1) ? 3 : 4;
3012.         } else
3013.             break;
3014.         bp += l;
3015.     }
3016.     if (!cnt)
3017.         cnt = 1; /* will be changed to 2 if makesingular() changes string */
3018.     if (strlen(bp) > 1 && (p = rindex(bp, '(')) != 0) {
3019.         boolean keeptrailingchars = TRUE;
3020. 
3021.         p[(p > bp && p[-1] == ' ') ? -1 : 0] = '\0'; /*terminate bp */
3022.         ++p; /* advance past '(' */
3023.         if (!strncmpi(p, "lit)", 4)) {
3024.             islit = 1;
3025.             p += 4 - 1; /* point at ')' */
3026.         } else {
3027.             spe = atoi(p);
3028.             while (digit(*p))
3029.                 p++;
3030.             if (*p == ':') {
3031.                 p++;
3032.                 rechrg = spe;
3033.                 spe = atoi(p);
3034.                 while (digit(*p))
3035.                     p++;
3036.             }
3037.             if (*p != ')') {
3038.                 spe = rechrg = 0;
3039.                 /* mis-matched parentheses; rest of string will be ignored
3040.                  * [probably we should restore everything back to '('
3041.                  * instead since it might be part of "named ..."]
3042.                  */
3043.                 keeptrailingchars = FALSE;
3044.             } else {
3045.                 spesgn = 1;
3046.             }
3047.         }
3048.         if (keeptrailingchars) {
3049.             char *pp = eos(bp);
3050. 
3051.             /* 'pp' points at 'pb's terminating '\0',
3052.                'p' points at ')' and will be incremented past it */
3053.             do {
3054.                 *pp++ = *++p;
3055.             } while (*p);
3056.         }
3057.     }
3058.     /*
3059.      * otmp->spe is type schar, so we don't want spe to be any bigger or
3060.      * smaller.  Also, spe should always be positive --some cheaters may
3061.      * try to confuse atoi().
3062.      */
3063.     if (spe < 0) {
3064.         spesgn = -1; /* cheaters get what they deserve */
3065.         spe = abs(spe);
3066.     }
3067.     if (spe > SCHAR_LIM)
3068.         spe = SCHAR_LIM;
3069.     if (rechrg < 0 || rechrg > 7)
3070.         rechrg = 7; /* recharge_limit */
3071. 
3072.     /* now we have the actual name, as delivered by xname, say
3073.      *  green potions called whisky
3074.      *  scrolls labeled "QWERTY"
3075.      *  egg
3076.      *  fortune cookies
3077.      *  very heavy iron ball named hoei
3078.      *  wand of wishing
3079.      *  elven cloak
3080.      */
3081.     if ((p = strstri(bp, " named ")) != 0) {
3082.         *p = 0;
3083.         name = p + 7;
3084.     }
3085.     if ((p = strstri(bp, " called ")) != 0) {
3086.         *p = 0;
3087.         un = p + 8;
3088.         /* "helmet called telepathy" is not "helmet" (a specific type)
3089.          * "shield called reflection" is not "shield" (a general type)
3090.          */
3091.         for (i = 0; i < SIZE(o_ranges); i++)
3092.             if (!strcmpi(bp, o_ranges[i].name)) {
3093.                 oclass = o_ranges[i].oclass;
3094.                 goto srch;
3095.             }
3096.     }
3097.     if ((p = strstri(bp, " labeled ")) != 0) {
3098.         *p = 0;
3099.         dn = p + 9;
3100.     } else if ((p = strstri(bp, " labelled ")) != 0) {
3101.         *p = 0;
3102.         dn = p + 10;
3103.     }
3104.     if ((p = strstri(bp, " of spinach")) != 0) {
3105.         *p = 0;
3106.         contents = SPINACH;
3107.     }
3108. 
3109.     /*
3110.      * Skip over "pair of ", "pairs of", "set of" and "sets of".
3111.      *
3112.      * Accept "3 pair of boots" as well as "3 pairs of boots".  It is
3113.      * valid English either way.  See makeplural() for more on pair/pairs.
3114.      *
3115.      * We should only double count if the object in question is not
3116.      * referred to as a "pair of".  E.g. We should double if the player
3117.      * types "pair of spears", but not if the player types "pair of
3118.      * lenses".  Luckily (?) all objects that are referred to as pairs
3119.      * -- boots, gloves, and lenses -- are also not mergable, so cnt is
3120.      * ignored anyway.
3121.      */
3122.     if (!strncmpi(bp, "pair of ", 8)) {
3123.         bp += 8;
3124.         cnt *= 2;
3125.     } else if (!strncmpi(bp, "pairs of ", 9)) {
3126.         bp += 9;
3127.         if (cnt > 1)
3128.             cnt *= 2;
3129.     } else if (!strncmpi(bp, "set of ", 7)) {
3130.         bp += 7;
3131.     } else if (!strncmpi(bp, "sets of ", 8)) {
3132.         bp += 8;
3133.     }
3134. 
3135.     /* intercept pudding globs here; they're a valid wish target,
3136.      * but we need them to not get treated like a corpse.
3137.      *
3138.      * also don't let player wish for multiple globs.
3139.      */
3140.     i = (int) strlen(bp);
3141.     p = (char *) 0;
3142.     /* check for "glob", "<foo> glob", and "glob of <foo>" */
3143.     if (!strcmpi(bp, "glob") || !BSTRCMPI(bp, bp + i - 5, " glob")
3144.         || !strcmpi(bp, "globs") || !BSTRCMPI(bp, bp + i - 6, " globs")
3145.         || (p = strstri(bp, "glob of ")) != 0
3146.         || (p = strstri(bp, "globs of ")) != 0) {
3147.         mntmp = name_to_mon(!p ? bp : (strstri(p, " of ") + 4));
3148.         /* if we didn't recognize monster type, pick a valid one at random */
3149.         if (mntmp == NON_PM)
3150.             mntmp = rn1(PM_BLACK_PUDDING - PM_GRAY_OOZE, PM_GRAY_OOZE);
3151.         /* construct canonical spelling in case name_to_mon() recognized a
3152.            variant (grey ooze) or player used inverted syntax (<foo> glob);
3153.            if player has given a valid monster type but not valid glob type,
3154.            object name lookup won't find it and wish attempt will fail */
3155.         Sprintf(globbuf, "glob of %s", mons[mntmp].mname);
3156.         bp = globbuf;
3157.         mntmp = NON_PM; /* not useful for "glob of <foo>" object lookup */
3158.         cnt = 0; /* globs don't stack */
3159.         oclass = FOOD_CLASS;
3160.         actualn = bp, dn = 0;
3161.         goto srch;
3162.     } else {
3163.         /*
3164.          * Find corpse type using "of" (figurine of an orc, tin of orc meat)
3165.          * Don't check if it's a wand or spellbook.
3166.          * (avoid "wand/finger of death" confusion).
3167.          */
3168.         if (!strstri(bp, "wand ") && !strstri(bp, "spellbook ")
3169.             && !strstri(bp, "finger ")) {
3170.             if ((p = strstri(bp, "tin of ")) != 0) {
3171.                 if (!strcmpi(p + 7, "spinach")) {
3172.                     contents = SPINACH;
3173.                     mntmp = NON_PM;
3174.                 } else {
3175.                     tmp = tin_variety_txt(p + 7, &tinv);
3176.                     tvariety = tinv;
3177.                     mntmp = name_to_mon(p + 7 + tmp);
3178.                 }
3179.                 typ = TIN;
3180.                 goto typfnd;
3181.             } else if ((p = strstri(bp, " of ")) != 0
3182.                        && (mntmp = name_to_mon(p + 4)) >= LOW_PM)
3183.                 *p = 0;
3184.         }
3185.     }
3186.     /* Find corpse type w/o "of" (red dragon scale mail, yeti corpse) */
3187.     if (strncmpi(bp, "samurai sword", 13)  /* not the "samurai" monster! */
3188.         && strncmpi(bp, "wizard lock", 11) /* not the "wizard" monster! */
3189.         && strncmpi(bp, "ninja-to", 8)     /* not the "ninja" rank */
3190.         && strncmpi(bp, "master key", 10)  /* not the "master" rank */
3191.         && strncmpi(bp, "magenta", 7)) {   /* not the "mage" rank */
3192.         if (mntmp < LOW_PM && strlen(bp) > 2
3193.             && (mntmp = name_to_mon(bp)) >= LOW_PM) {
3194.             int mntmptoo, mntmplen; /* double check for rank title */
3195.             char *obp = bp;
3196. 
3197.             mntmptoo = title_to_mon(bp, (int *) 0, &mntmplen);
3198.             bp += (mntmp != mntmptoo) ? (int) strlen(mons[mntmp].mname)
3199.                                       : mntmplen;
3200.             if (*bp == ' ') {
3201.                 bp++;
3202.             } else if (!strncmpi(bp, "s ", 2)) {
3203.                 bp += 2;
3204.             } else if (!strncmpi(bp, "es ", 3)) {
3205.                 bp += 3;
3206.             } else if (!*bp && !actualn && !dn && !un && !oclass) {
3207.                 /* no referent; they don't really mean a monster type */
3208.                 bp = obp;
3209.                 mntmp = NON_PM;
3210.             }
3211.         }
3212.     }
3213. 
3214.     /* first change to singular if necessary */
3215.     if (*bp) {
3216.         char *sng = makesingular(bp);
3217. 
3218.         if (strcmp(bp, sng)) {
3219.             if (cnt == 1)
3220.                 cnt = 2;
3221.             Strcpy(bp, sng);
3222.         }
3223.     }
3224. 
3225.     /* Alternate spellings (pick-ax, silver sabre, &c) */
3226.     {
3227.         const struct alt_spellings *as = spellings;
3228. 
3229.         while (as->sp) {
3230.             if (fuzzymatch(bp, as->sp, " -", TRUE)) {
3231.                 typ = as->ob;
3232.                 goto typfnd;
3233.             }
3234.             as++;
3235.         }
3236.         /* can't use spellings list for this one due to shuffling */
3237.         if (!strncmpi(bp, "grey spell", 10))
3238.             *(bp + 2) = 'a';
3239. 
3240.         if ((p = strstri(bp, "armour")) != 0) {
3241.             /* skip past "armo", then copy remainder beyond "u" */
3242.             p += 4;
3243.             while ((*p = *(p + 1)) != '\0')
3244.                 ++p; /* self terminating */
3245.         }
3246.     }
3247. 
3248.     /* dragon scales - assumes order of dragons */
3249.     if (!strcmpi(bp, "scales") && mntmp >= PM_GRAY_DRAGON
3250.         && mntmp <= PM_YELLOW_DRAGON) {
3251.         typ = GRAY_DRAGON_SCALES + mntmp - PM_GRAY_DRAGON;
3252.         mntmp = NON_PM; /* no monster */
3253.         goto typfnd;
3254.     }
3255. 
3256.     p = eos(bp);
3257.     if (!BSTRCMPI(bp, p - 10, "holy water")) {
3258.         typ = POT_WATER;
3259.         if ((p - bp) >= 12 && *(p - 12) == 'u')
3260.             iscursed = 1; /* unholy water */
3261.         else
3262.             blessed = 1;
3263.         goto typfnd;
3264.     }
3265.     if (unlabeled && !BSTRCMPI(bp, p - 6, "scroll")) {
3266.         typ = SCR_BLANK_PAPER;
3267.         goto typfnd;
3268.     }
3269.     if (unlabeled && !BSTRCMPI(bp, p - 9, "spellbook")) {
3270.         typ = SPE_BLANK_PAPER;
3271.         goto typfnd;
3272.     }
3273.     /* specific food rather than color of gem/potion/spellbook[/scales] */
3274.     if (!BSTRCMPI(bp, p - 6, "orange") && mntmp == NON_PM) {
3275.         typ = ORANGE;
3276.         goto typfnd;
3277.     }
3278.     /*
3279.      * NOTE: Gold pieces are handled as objects nowadays, and therefore
3280.      * this section should probably be reconsidered as well as the entire
3281.      * gold/money concept.  Maybe we want to add other monetary units as
3282.      * well in the future. (TH)
3283.      */
3284.     if (!BSTRCMPI(bp, p - 10, "gold piece")
3285.         || !BSTRCMPI(bp, p - 7, "zorkmid")
3286.         || !strcmpi(bp, "gold") || !strcmpi(bp, "money")
3287.         || !strcmpi(bp, "coin") || *bp == GOLD_SYM) {
3288.         if (cnt > 5000 && !wizard)
3289.             cnt = 5000;
3290.         else if (cnt < 1)
3291.             cnt = 1;
3292.         otmp = mksobj(GOLD_PIECE, FALSE, FALSE);
3293.         otmp->quan = (long) cnt;
3294.         otmp->owt = weight(otmp);
3295.         context.botl = 1;
3296.         return otmp;
3297.     }
3298. 
3299.     /* check for single character object class code ("/" for wand, &c) */
3300.     if (strlen(bp) == 1 && (i = def_char_to_objclass(*bp)) < MAXOCLASSES
3301.         && i > ILLOBJ_CLASS && (i != VENOM_CLASS || wizard)) {
3302.         oclass = i;
3303.         goto any;
3304.     }
3305. 
3306.     /* Search for class names: XXXXX potion, scroll of XXXXX.  Avoid */
3307.     /* false hits on, e.g., rings for "ring mail". */
3308.     if (strncmpi(bp, "enchant ", 8)
3309.         && strncmpi(bp, "destroy ", 8)
3310.         && strncmpi(bp, "detect food", 11)
3311.         && strncmpi(bp, "food detection", 14)
3312.         && strncmpi(bp, "ring mail", 9)
3313.         && strncmpi(bp, "studded leather armor", 21)
3314.         && strncmpi(bp, "leather armor", 13)
3315.         && strncmpi(bp, "tooled horn", 11)
3316.         && strncmpi(bp, "food ration", 11)
3317.         && strncmpi(bp, "meat ring", 9))
3318.         for (i = 0; i < (int) (sizeof wrpsym); i++) {
3319.             register int j = strlen(wrp[i]);
3320. 
3321.             if (!strncmpi(bp, wrp[i], j)) {
3322.                 oclass = wrpsym[i];
3323.                 if (oclass != AMULET_CLASS) {
3324.                     bp += j;
3325.                     if (!strncmpi(bp, " of ", 4))
3326.                         actualn = bp + 4;
3327.                     /* else if(*bp) ?? */
3328.                 } else
3329.                     actualn = bp;
3330.                 goto srch;
3331.             }
3332.             if (!BSTRCMPI(bp, p - j, wrp[i])) {
3333.                 oclass = wrpsym[i];
3334.                 p -= j;
3335.                 *p = 0;
3336.                 if (p > bp && p[-1] == ' ')
3337.                     p[-1] = 0;
3338.                 actualn = dn = bp;
3339.                 goto srch;
3340.             }
3341.         }
3342. 
3343.     /* Wishing in wizard mode can create traps and furniture.
3344.      * Part I:  distinguish between trap and object for the two
3345.      * types of traps which have corresponding objects:  bear trap
3346.      * and land mine.  "beartrap" (object) and "bear trap" (trap)
3347.      * have a difference in spelling which we used to exploit by
3348.      * adding a special case in wishymatch(), but "land mine" is
3349.      * spelled the same either way so needs different handing.
3350.      * Since we need something else for land mine, we've dropped
3351.      * the bear trap hack so that both are handled exactly the
3352.      * same.  To get an armed trap instead of a disarmed object,
3353.      * the player can prefix either the object name or the trap
3354.      * name with "trapped " (which ordinarily applies to chests
3355.      * and tins), or append something--anything at all except for
3356.      * " object", but " trap" is suggested--to either the trap
3357.      * name or the object name.
3358.      */
3359.     if (wizard && (!strncmpi(bp, "bear", 4) || !strncmpi(bp, "land", 4))) {
3360.         boolean beartrap = (lowc(*bp) == 'b');
3361.         char *zp = bp + 4; /* skip "bear"/"land" */
3362. 
3363.         if (*zp == ' ')
3364.             ++zp; /* embedded space is optional */
3365.         if (!strncmpi(zp, beartrap ? "trap" : "mine", 4)) {
3366.             zp += 4;
3367.             if (trapped == 2 || !strcmpi(zp, " object")) {
3368.                 /* "untrapped <foo>" or "<foo> object" */
3369.                 typ = beartrap ? BEARTRAP : LAND_MINE;
3370.                 goto typfnd;
3371.             } else if (trapped == 1 || *zp != '\0') {
3372.                 /* "trapped <foo>" or "<foo> trap" (actually "<foo>*") */
3373.                 int idx = trap_to_defsym(beartrap ? BEAR_TRAP : LANDMINE);
3374. 
3375.                 /* use canonical trap spelling, skip object matching */
3376.                 Strcpy(bp, defsyms[idx].explanation);
3377.                 goto wiztrap;
3378.             }
3379.             /* [no prefix or suffix; we're going to end up matching
3380.                the object name and getting a disarmed trap object] */
3381.         }
3382.     }
3383. 
3384.  retry:
3385.     /* "grey stone" check must be before general "stone" */
3386.     for (i = 0; i < SIZE(o_ranges); i++)
3387.         if (!strcmpi(bp, o_ranges[i].name)) {
3388.             typ = rnd_class(o_ranges[i].f_o_range, o_ranges[i].l_o_range);
3389.             goto typfnd;
3390.         }
3391. 
3392.     if (!BSTRCMPI(bp, p - 6, " stone") || !BSTRCMPI(bp, p - 4, " gem")) {
3393.         p[!strcmpi(p - 4, " gem") ? -4 : -6] = '\0';
3394.         oclass = GEM_CLASS;
3395.         dn = actualn = bp;
3396.         goto srch;
3397.     } else if (!strcmpi(bp, "looking glass")) {
3398.         ; /* avoid false hit on "* glass" */
3399.     } else if (!BSTRCMPI(bp, p - 6, " glass") || !strcmpi(bp, "glass")) {
3400.         register char *g = bp;
3401. 
3402.         /* treat "broken glass" as a non-existent item; since "broken" is
3403.            also a chest/box prefix it might have been stripped off above */
3404.         if (broken || strstri(g, "broken"))
3405.             return (struct obj *) 0;
3406.         if (!strncmpi(g, "worthless ", 10))
3407.             g += 10;
3408.         if (!strncmpi(g, "piece of ", 9))
3409.             g += 9;
3410.         if (!strncmpi(g, "colored ", 8))
3411.             g += 8;
3412.         else if (!strncmpi(g, "coloured ", 9))
3413.             g += 9;
3414.         if (!strcmpi(g, "glass")) { /* choose random color */
3415.             /* 9 different kinds */
3416.             typ = LAST_GEM + rnd(9);
3417.             if (objects[typ].oc_class == GEM_CLASS)
3418.                 goto typfnd;
3419.             else
3420.                 typ = 0; /* somebody changed objects[]? punt */
3421.         } else { /* try to construct canonical form */
3422.             char tbuf[BUFSZ];
3423. 
3424.             Strcpy(tbuf, "worthless piece of ");
3425.             Strcat(tbuf, g); /* assume it starts with the color */
3426.             Strcpy(bp, tbuf);
3427.         }
3428.     }
3429. 
3430.     actualn = bp;
3431.     if (!dn)
3432.         dn = actualn; /* ex. "skull cap" */
3433.  srch:
3434.     /* check real names of gems first */
3435.     if (!oclass && actualn) {
3436.         for (i = bases[GEM_CLASS]; i <= LAST_GEM; i++) {
3437.             register const char *zn;
3438. 
3439.             if ((zn = OBJ_NAME(objects[i])) != 0 && !strcmpi(actualn, zn)) {
3440.                 typ = i;
3441.                 goto typfnd;
3442.             }
3443.         }
3444.         /* "tin of foo" would be caught above, but plain "tin" has
3445.            a random chance of yielding "tin wand" unless we do this */
3446.         if (!strcmpi(actualn, "tin")) {
3447.             typ = TIN;
3448.             goto typfnd;
3449.         }
3450.     }
3451. 
3452.     if (((typ = rnd_otyp_by_namedesc(actualn, oclass, 1)) != STRANGE_OBJECT)
3453.         || ((typ = rnd_otyp_by_namedesc(dn, oclass, 1)) != STRANGE_OBJECT)
3454.         || ((typ = rnd_otyp_by_namedesc(un, oclass, 1)) != STRANGE_OBJECT)
3455.         || ((typ = rnd_otyp_by_namedesc(origbp, oclass, 1)) != STRANGE_OBJECT))
3456.         goto typfnd;
3457.     typ = 0;
3458. 
3459.     if (actualn) {
3460.         struct Jitem *j = Japanese_items;
3461. 
3462.         while (j->item) {
3463.             if (actualn && !strcmpi(actualn, j->name)) {
3464.                 typ = j->item;
3465.                 goto typfnd;
3466.             }
3467.             j++;
3468.         }
3469.     }
3470.     /* if we've stripped off "armor" and failed to match anything
3471.        in objects[], append "mail" and try again to catch misnamed
3472.        requests like "plate armor" and "yellow dragon scale armor" */
3473.     if (oclass == ARMOR_CLASS && !strstri(bp, "mail")) {
3474.         /* modifying bp's string is ok; we're about to resort
3475.            to random armor if this also fails to match anything */
3476.         Strcat(bp, " mail");
3477.         goto retry;
3478.     }
3479.     if (!strcmpi(bp, "spinach")) {
3480.         contents = SPINACH;
3481.         typ = TIN;
3482.         goto typfnd;
3483.     }
3484.     /* Note: not strcmpi.  2 fruits, one capital, one not, are possible.
3485.        Also not strncmp.  We used to ignore trailing text with it, but
3486.        that resulted in "grapefruit" matching "grape" if the latter came
3487.        earlier than the former in the fruit list. */
3488.     {
3489.         char *fp;
3490.         int l, cntf;
3491.         int blessedf, iscursedf, uncursedf, halfeatenf;
3492. 
3493.         blessedf = iscursedf = uncursedf = halfeatenf = 0;
3494.         cntf = 0;
3495. 
3496.         fp = fruitbuf;
3497.         for (;;) {
3498.             if (!fp || !*fp)
3499.                 break;
3500.             if (!strncmpi(fp, "an ", l = 3) || !strncmpi(fp, "a ", l = 2)) {
3501.                 cntf = 1;
3502.             } else if (!cntf && digit(*fp)) {
3503.                 cntf = atoi(fp);
3504.                 while (digit(*fp))
3505.                     fp++;
3506.                 while (*fp == ' ')
3507.                     fp++;
3508.                 l = 0;
3509.             } else if (!strncmpi(fp, "blessed ", l = 8)) {
3510.                 blessedf = 1;
3511.             } else if (!strncmpi(fp, "cursed ", l = 7)) {
3512.                 iscursedf = 1;
3513.             } else if (!strncmpi(fp, "uncursed ", l = 9)) {
3514.                 uncursedf = 1;
3515.             } else if (!strncmpi(fp, "partly eaten ", l = 13)
3516.                        || !strncmpi(fp, "partially eaten ", l = 16)) {
3517.                 halfeatenf = 1;
3518.             } else
3519.                 break;
3520.             fp += l;
3521.         }
3522. 
3523.         for (f = ffruit; f; f = f->nextf) {
3524.             /* match type: 0=none, 1=exact, 2=singular, 3=plural */
3525.             int ftyp = 0;
3526. 
3527.             if (!strcmp(fp, f->fname))
3528.                 ftyp = 1;
3529.             else if (!strcmp(fp, makesingular(f->fname)))
3530.                 ftyp = 2;
3531.             else if (!strcmp(fp, makeplural(f->fname)))
3532.                 ftyp = 3;
3533.             if (ftyp) {
3534.                 typ = SLIME_MOLD;
3535.                 blessed = blessedf;
3536.                 iscursed = iscursedf;
3537.                 uncursed = uncursedf;
3538.                 halfeaten = halfeatenf;
3539.                 /* adjust count if user explicitly asked for
3540.                    singular amount (can't happen unless fruit
3541.                    has been given an already pluralized name)
3542.                    or for plural amount */
3543.                 if (ftyp == 2 && !cntf)
3544.                     cntf = 1;
3545.                 else if (ftyp == 3 && !cntf)
3546.                     cntf = 2;
3547.                 cnt = cntf;
3548.                 ftype = f->fid;
3549.                 goto typfnd;
3550.             }
3551.         }
3552.     }
3553. 
3554.     if (!oclass && actualn) {
3555.         short objtyp;
3556. 
3557.         /* Perhaps it's an artifact specified by name, not type */
3558.         name = artifact_name(actualn, &objtyp);
3559.         if (name) {
3560.             typ = objtyp;
3561.             goto typfnd;
3562.         }
3563.     }
3564. /* Let wizards wish for traps and furniture.
3565.  * Must come after objects check so wizards can still wish for
3566.  * trap objects like beartraps.
3567.  * Disallow such topology tweaks for WIZKIT startup wishes.
3568.  */
3569.  wiztrap:
3570.     if (wizard && !program_state.wizkit_wishing) {
3571.         struct rm *lev;
3572.         int trap, x = u.ux, y = u.uy;
3573. 
3574.         for (trap = NO_TRAP + 1; trap < TRAPNUM; trap++) {
3575.             struct trap *t;
3576.             const char *tname;
3577. 
3578.             tname = defsyms[trap_to_defsym(trap)].explanation;
3579.             if (strncmpi(tname, bp, strlen(tname)))
3580.                 continue;
3581.             /* found it; avoid stupid mistakes */
3582.             if (is_hole(trap) && !Can_fall_thru(&u.uz))
3583.                 trap = ROCKTRAP;
3584.             if ((t = maketrap(x, y, trap)) != 0) {
3585.                 trap = t->ttyp;
3586.                 tname = defsyms[trap_to_defsym(trap)].explanation;
3587.                 pline("%s%s.", An(tname),
3588.                       (trap != MAGIC_PORTAL) ? "" : " to nowhere");
3589.             } else
3590.                 pline("Creation of %s failed.", an(tname));
3591.             return (struct obj *) &zeroobj;
3592.         }
3593. 
3594.         /* furniture and terrain */
3595.         lev = &levl[x][y];
3596.         p = eos(bp);
3597.         if (!BSTRCMPI(bp, p - 8, "fountain")) {
3598.             lev->typ = FOUNTAIN;
3599.             level.flags.nfountains++;
3600.             if (!strncmpi(bp, "magic ", 6))
3601.                 lev->blessedftn = 1;
3602.             pline("A %sfountain.", lev->blessedftn ? "magic " : "");
3603.             newsym(x, y);
3604.             return (struct obj *) &zeroobj;
3605.         }
3606.         if (!BSTRCMPI(bp, p - 6, "throne")) {
3607.             lev->typ = THRONE;
3608.             pline("A throne.");
3609.             newsym(x, y);
3610.             return (struct obj *) &zeroobj;
3611.         }
3612.         if (!BSTRCMPI(bp, p - 4, "sink")) {
3613.             lev->typ = SINK;
3614.             level.flags.nsinks++;
3615.             pline("A sink.");
3616.             newsym(x, y);
3617.             return (struct obj *) &zeroobj;
3618.         }
3619.         /* ("water" matches "potion of water" rather than terrain) */
3620.         if (!BSTRCMPI(bp, p - 4, "pool") || !BSTRCMPI(bp, p - 4, "moat")) {
3621.             lev->typ = !BSTRCMPI(bp, p - 4, "pool") ? POOL : MOAT;
3622.             del_engr_at(x, y);
3623.             pline("A %s.", (lev->typ == POOL) ? "pool" : "moat");
3624.             /* Must manually make kelp! */
3625.             water_damage_chain(level.objects[x][y], TRUE);
3626.             newsym(x, y);
3627.             return (struct obj *) &zeroobj;
3628.         }
3629.         if (!BSTRCMPI(bp, p - 4, "lava")) { /* also matches "molten lava" */
3630.             lev->typ = LAVAPOOL;
3631.             del_engr_at(x, y);
3632.             pline("A pool of molten lava.");
3633.             if (!(Levitation || Flying))
3634.                 (void) lava_effects();
3635.             newsym(x, y);
3636.             return (struct obj *) &zeroobj;
3637.         }
3638. 
3639.         if (!BSTRCMPI(bp, p - 5, "altar")) {
3640.             aligntyp al;
3641. 
3642.             lev->typ = ALTAR;
3643.             if (!strncmpi(bp, "chaotic ", 8))
3644.                 al = A_CHAOTIC;
3645.             else if (!strncmpi(bp, "neutral ", 8))
3646.                 al = A_NEUTRAL;
3647.             else if (!strncmpi(bp, "lawful ", 7))
3648.                 al = A_LAWFUL;
3649.             else if (!strncmpi(bp, "unaligned ", 10))
3650.                 al = A_NONE;
3651.             else /* -1 - A_CHAOTIC, 0 - A_NEUTRAL, 1 - A_LAWFUL */
3652.                 al = (!rn2(6)) ? A_NONE : rn2((int) A_LAWFUL + 2) - 1;
3653.             lev->altarmask = Align2amask(al);
3654.             pline("%s altar.", An(align_str(al)));
3655.             newsym(x, y);
3656.             return (struct obj *) &zeroobj;
3657.         }
3658. 
3659.         if (!BSTRCMPI(bp, p - 5, "grave")
3660.             || !BSTRCMPI(bp, p - 9, "headstone")) {
3661.             make_grave(x, y, (char *) 0);
3662.             pline("%s.", IS_GRAVE(lev->typ) ? "A grave"
3663.                                             : "Can't place a grave here");
3664.             newsym(x, y);
3665.             return (struct obj *) &zeroobj;
3666.         }
3667. 
3668.         if (!BSTRCMPI(bp, p - 4, "tree")) {
3669.             lev->typ = TREE;
3670.             pline("A tree.");
3671.             newsym(x, y);
3672.             block_point(x, y);
3673.             return (struct obj *) &zeroobj;
3674.         }
3675. 
3676.         if (!BSTRCMPI(bp, p - 4, "bars")) {
3677.             lev->typ = IRONBARS;
3678.             pline("Iron bars.");
3679.             newsym(x, y);
3680.             return (struct obj *) &zeroobj;
3681.         }
3682.     }
3683. 
3684.     if (!oclass && !typ) {
3685.         if (!strncmpi(bp, "polearm", 7)) {
3686.             typ = rnd_otyp_by_wpnskill(P_POLEARMS);
3687.             goto typfnd;
3688.         } else if (!strncmpi(bp, "hammer", 6)) {
3689.             typ = rnd_otyp_by_wpnskill(P_HAMMER);
3690.             goto typfnd;
3691.         }
3692.     }
3693. 
3694.     if (!oclass)
3695.         return ((struct obj *) 0);
3696.  any:
3697.     if (!oclass)
3698.         oclass = wrpsym[rn2((int) sizeof wrpsym)];
3699.  typfnd:
3700.     if (typ)
3701.         oclass = objects[typ].oc_class;
3702. 
3703.     /* handle some objects that are only allowed in wizard mode */
3704.     if (typ && !wizard) {
3705.         switch (typ) {
3706.         case AMULET_OF_YENDOR:
3707.             typ = FAKE_AMULET_OF_YENDOR;
3708.             break;
3709.         case CANDELABRUM_OF_INVOCATION:
3710.             typ = rnd_class(TALLOW_CANDLE, WAX_CANDLE);
3711.             break;
3712.         case BELL_OF_OPENING:
3713.             typ = BELL;
3714.             break;
3715.         case SPE_BOOK_OF_THE_DEAD:
3716.             typ = SPE_BLANK_PAPER;
3717.             break;
3718.         case MAGIC_LAMP:
3719.             typ = OIL_LAMP;
3720.             break;
3721.         default:
3722.             /* catch any other non-wishable objects (venom) */
3723.             if (objects[typ].oc_nowish)
3724.                 return (struct obj *) 0;
3725.             break;
3726.         }
3727.     }
3728. 
3729.     /*
3730.      * Create the object, then fine-tune it.
3731.      */
3732.     otmp = typ ? mksobj(typ, TRUE, FALSE) : mkobj(oclass, FALSE);
3733.     typ = otmp->otyp, oclass = otmp->oclass; /* what we actually got */
3734. 
3735.     if (islit && (typ == OIL_LAMP || typ == MAGIC_LAMP || typ == BRASS_LANTERN
3736.                   || Is_candle(otmp) || typ == POT_OIL)) {
3737.         place_object(otmp, u.ux, u.uy); /* make it viable light source */
3738.         begin_burn(otmp, FALSE);
3739.         obj_extract_self(otmp); /* now release it for caller's use */
3740.     }
3741. 
3742.     /* if player specified a reasonable count, maybe honor it */
3743.     if (cnt > 0 && objects[typ].oc_merge
3744.         && (wizard || cnt < rnd(6) || (cnt <= 7 && Is_candle(otmp))
3745.             || (cnt <= 20 && ((oclass == WEAPON_CLASS && is_ammo(otmp))
3746.                               || typ == ROCK || is_missile(otmp)))))
3747.         otmp->quan = (long) cnt;
3748. 
3749.     if (oclass == VENOM_CLASS)
3750.         otmp->spe = 1;
3751. 
3752.     if (spesgn == 0) {
3753.         spe = otmp->spe;
3754.     } else if (wizard) {
3755.         ; /* no alteration to spe */
3756.     } else if (oclass == ARMOR_CLASS || oclass == WEAPON_CLASS
3757.                || is_weptool(otmp)
3758.                || (oclass == RING_CLASS && objects[typ].oc_charged)) {
3759.         if (spe > rnd(5) && spe > otmp->spe)
3760.             spe = 0;
3761.         if (spe > 2 && Luck < 0)
3762.             spesgn = -1;
3763.     } else {
3764.         if (oclass == WAND_CLASS) {
3765.             if (spe > 1 && spesgn == -1)
3766.                 spe = 1;
3767.         } else {
3768.             if (spe > 0 && spesgn == -1)
3769.                 spe = 0;
3770.         }
3771.         if (spe > otmp->spe)
3772.             spe = otmp->spe;
3773.     }
3774. 
3775.     if (spesgn == -1)
3776.         spe = -spe;
3777. 
3778.     /* set otmp->spe.  This may, or may not, use spe... */
3779.     switch (typ) {
3780.     case TIN:
3781.         if (contents == EMPTY) {
3782.             otmp->corpsenm = NON_PM;
3783.             otmp->spe = 0;
3784.         } else if (contents == SPINACH) {
3785.             otmp->corpsenm = NON_PM;
3786.             otmp->spe = 1;
3787.         }
3788.         break;
3789.     case TOWEL:
3790.         if (wetness)
3791.             otmp->spe = wetness;
3792.         break;
3793.     case SLIME_MOLD:
3794.         otmp->spe = ftype;
3795.     /* Fall through */
3796.     case SKELETON_KEY:
3797.     case CHEST:
3798.     case LARGE_BOX:
3799.     case HEAVY_IRON_BALL:
3800.     case IRON_CHAIN:
3801.     case STATUE:
3802.         /* otmp->cobj already done in mksobj() */
3803.         break;
3804. #ifdef MAIL
3805.     case SCR_MAIL:
3806.         /* 0: delivered in-game via external event (or randomly for fake mail);
3807.            1: from bones or wishing; 2: written with marker */
3808.         otmp->spe = 1;
3809.         break;
3810. #endif
3811.     case WAN_WISHING:
3812.         if (!wizard) {
3813.             otmp->spe = (rn2(10) ? -1 : 0);
3814.             break;
3815.         }
3816.         /*FALLTHRU*/
3817.     default:
3818.         otmp->spe = spe;
3819.     }
3820. 
3821.     /* set otmp->corpsenm or dragon scale [mail] */
3822.     if (mntmp >= LOW_PM) {
3823.         if (mntmp == PM_LONG_WORM_TAIL)
3824.             mntmp = PM_LONG_WORM;
3825. 
3826.         switch (typ) {
3827.         case TIN:
3828.             otmp->spe = 0; /* No spinach */
3829.             if (dead_species(mntmp, FALSE)) {
3830.                 otmp->corpsenm = NON_PM; /* it's empty */
3831.             } else if ((!(mons[mntmp].geno & G_UNIQ) || wizard)
3832.                        && !(mvitals[mntmp].mvflags & G_NOCORPSE)
3833.                        && mons[mntmp].cnutrit != 0) {
3834.                 otmp->corpsenm = mntmp;
3835.             }
3836.             break;
3837.         case CORPSE:
3838.             if ((!(mons[mntmp].geno & G_UNIQ) || wizard)
3839.                 && !(mvitals[mntmp].mvflags & G_NOCORPSE)) {
3840.                 if (mons[mntmp].msound == MS_GUARDIAN)
3841.                     mntmp = genus(mntmp, 1);
3842.                 set_corpsenm(otmp, mntmp);
3843.             }
3844.             break;
3845.         case EGG:
3846.             mntmp = can_be_hatched(mntmp);
3847.             /* this also sets hatch timer if appropriate */
3848.             set_corpsenm(otmp, mntmp);
3849.             break;
3850.         case FIGURINE:
3851.             if (!(mons[mntmp].geno & G_UNIQ) && !is_human(&mons[mntmp])
3852. #ifdef MAIL
3853.                 && mntmp != PM_MAIL_DAEMON
3854. #endif
3855.                 )
3856.                 otmp->corpsenm = mntmp;
3857.             break;
3858.         case STATUE:
3859.             otmp->corpsenm = mntmp;
3860.             if (Has_contents(otmp) && verysmall(&mons[mntmp]))
3861.                 delete_contents(otmp); /* no spellbook */
3862.             otmp->spe = ishistoric ? STATUE_HISTORIC : 0;
3863.             break;
3864.         case SCALE_MAIL:
3865.             /* Dragon mail - depends on the order of objects & dragons. */
3866.             if (mntmp >= PM_GRAY_DRAGON && mntmp <= PM_YELLOW_DRAGON)
3867.                 otmp->otyp = GRAY_DRAGON_SCALE_MAIL + mntmp - PM_GRAY_DRAGON;
3868.             break;
3869.         }
3870.     }
3871. 
3872.     /* set blessed/cursed -- setting the fields directly is safe
3873.      * since weight() is called below and addinv() will take care
3874.      * of luck */
3875.     if (iscursed) {
3876.         curse(otmp);
3877.     } else if (uncursed) {
3878.         otmp->blessed = 0;
3879.         otmp->cursed = (Luck < 0 && !wizard);
3880.     } else if (blessed) {
3881.         otmp->blessed = (Luck >= 0 || wizard);
3882.         otmp->cursed = (Luck < 0 && !wizard);
3883.     } else if (spesgn < 0) {
3884.         curse(otmp);
3885.     }
3886. 
3887.     /* set eroded and erodeproof */
3888.     if (erosion_matters(otmp)) {
3889.         if (eroded && (is_flammable(otmp) || is_rustprone(otmp)))
3890.             otmp->oeroded = eroded;
3891.         if (eroded2 && (is_corrodeable(otmp) || is_rottable(otmp)))
3892.             otmp->oeroded2 = eroded2;
3893.         /*
3894.          * 3.6.1: earlier versions included `&& !eroded && !eroded2' here,
3895.          * but damageproof combined with damaged is feasible (eroded
3896.          * armor modified by confused reading of cursed destroy armor)
3897.          * so don't prevent player from wishing for such a combination.
3898.          */
3899.         if (erodeproof && (is_damageable(otmp) || otmp->otyp == CRYSKNIFE))
3900.             otmp->oerodeproof = (Luck >= 0 || wizard);
3901.     }
3902. 
3903.     /* set otmp->recharged */
3904.     if (oclass == WAND_CLASS) {
3905.         /* prevent wishing abuse */
3906.         if (otmp->otyp == WAN_WISHING && !wizard)
3907.             rechrg = 1;
3908.         otmp->recharged = (unsigned) rechrg;
3909.     }
3910. 
3911.     /* set poisoned */
3912.     if (ispoisoned) {
3913.         if (is_poisonable(otmp))
3914.             otmp->opoisoned = (Luck >= 0);
3915.         else if (oclass == FOOD_CLASS)
3916.             /* try to taint by making it as old as possible */
3917.             otmp->age = 1L;
3918.     }
3919.     /* and [un]trapped */
3920.     if (trapped) {
3921.         if (Is_box(otmp) || typ == TIN)
3922.             otmp->otrapped = (trapped == 1);
3923.     }
3924.     /* empty for containers rather than for tins */
3925.     if (contents == EMPTY) {
3926.         if (otmp->otyp == BAG_OF_TRICKS || otmp->otyp == HORN_OF_PLENTY) {
3927.             if (otmp->spe > 0)
3928.                 otmp->spe = 0;
3929.         } else if (Has_contents(otmp)) {
3930.             /* this assumes that artifacts can't be randomly generated
3931.                inside containers */
3932.             delete_contents(otmp);
3933.             otmp->owt = weight(otmp);
3934.         }
3935.     }
3936.     /* set locked/unlocked/broken */
3937.     if (Is_box(otmp)) {
3938.         if (locked) {
3939.             otmp->olocked = 1, otmp->obroken = 0;
3940.         } else if (unlocked) {
3941.             otmp->olocked = 0, otmp->obroken = 0;
3942.         } else if (broken) {
3943.             otmp->olocked = 0, otmp->obroken = 1;
3944.         }
3945.     }
3946. 
3947.     if (isgreased)
3948.         otmp->greased = 1;
3949. 
3950.     if (isdiluted && otmp->oclass == POTION_CLASS && otmp->otyp != POT_WATER)
3951.         otmp->odiluted = 1;
3952. 
3953.     /* set tin variety */
3954.     if (otmp->otyp == TIN && tvariety >= 0 && (rn2(4) || wizard))
3955.         set_tin_variety(otmp, tvariety);
3956. 
3957.     if (name) {
3958.         const char *aname;
3959.         short objtyp;
3960. 
3961.         /* an artifact name might need capitalization fixing */
3962.         aname = artifact_name(name, &objtyp);
3963.         if (aname && objtyp == otmp->otyp)
3964.             name = aname;
3965. 
3966.         /* 3.6 tribute - fix up novel */
3967.         if (otmp->otyp == SPE_NOVEL) {
3968.             const char *novelname;
3969. 
3970.             novelname = lookup_novel(name, &otmp->novelidx);
3971.             if (novelname)
3972.                 name = novelname;
3973.         }
3974. 
3975.         otmp = oname(otmp, name);
3976.         /* name==aname => wished for artifact (otmp->oartifact => got it) */
3977.         if (otmp->oartifact || name == aname) {
3978.             otmp->quan = 1L;
3979.             u.uconduct.wisharti++; /* KMH, conduct */
3980.         }
3981.     }
3982. 
3983.     /* more wishing abuse: don't allow wishing for certain artifacts */
3984.     /* and make them pay; charge them for the wish anyway! */
3985.     if ((is_quest_artifact(otmp)
3986.          || (otmp->oartifact && rn2(nartifact_exist()) > 1)) && !wizard) {
3987.         artifact_exists(otmp, safe_oname(otmp), FALSE);
3988.         obfree(otmp, (struct obj *) 0);
3989.         otmp = (struct obj *) &zeroobj;
3990.         pline("For a moment, you feel %s in your %s, but it disappears!",
3991.               something, makeplural(body_part(HAND)));
3992.         return otmp;
3993.     }
3994. 
3995.     if (halfeaten && otmp->oclass == FOOD_CLASS) {
3996.         if (otmp->otyp == CORPSE)
3997.             otmp->oeaten = mons[otmp->corpsenm].cnutrit;
3998.         else
3999.             otmp->oeaten = objects[otmp->otyp].oc_nutrition;
4000.         /* (do this adjustment before setting up object's weight) */
4001.         consume_oeaten(otmp, 1);
4002.     }
4003.     otmp->owt = weight(otmp);
4004.     if (very && otmp->otyp == HEAVY_IRON_BALL)
4005.         otmp->owt += IRON_BALL_W_INCR;
4006.     else if (gsize > 1 && otmp->globby)
4007.         /* 0: unspecified => small; 1: small => keep default owt of 20;
4008.            2: medium => 120; 3: large => 320; 4: very large => 520 */
4009.         otmp->owt += 100 + (gsize - 2) * 200;
4010. 
4011.     return otmp;
4012. }
4013. 
4014. int
4015. rnd_class(first, last)
4016. int first, last;
4017. {
4018.     int i, x, sum = 0;
4019. 
4020.     if (first == last)
4021.         return first;
4022.     for (i = first; i <= last; i++)
4023.         sum += objects[i].oc_prob;
4024.     if (!sum) /* all zero */
4025.         return first + rn2(last - first + 1);
4026.     x = rnd(sum);
4027.     for (i = first; i <= last; i++)
4028.         if (objects[i].oc_prob && (x -= objects[i].oc_prob) <= 0)
4029.             return i;
4030.     return 0;
4031. }
4032. 
4033. STATIC_OVL const char *
4034. Japanese_item_name(i)
4035. int i;
4036. {
4037.     struct Jitem *j = Japanese_items;
4038. 
4039.     while (j->item) {
4040.         if (i == j->item)
4041.             return j->name;
4042.         j++;
4043.     }
4044.     return (const char *) 0;
4045. }
4046. 
4047. const char *
4048. suit_simple_name(suit)
4049. struct obj *suit;
4050. {
4051.     const char *suitnm, *esuitp;
4052. 
4053.     if (suit) {
4054.         if (Is_dragon_mail(suit))
4055.             return "dragon mail"; /* <color> dragon scale mail */
4056.         else if (Is_dragon_scales(suit))
4057.             return "dragon scales";
4058.         suitnm = OBJ_NAME(objects[suit->otyp]);
4059.         esuitp = eos((char *) suitnm);
4060.         if (strlen(suitnm) > 5 && !strcmp(esuitp - 5, " mail"))
4061.             return "mail"; /* most suits fall into this category */
4062.         else if (strlen(suitnm) > 7 && !strcmp(esuitp - 7, " jacket"))
4063.             return "jacket"; /* leather jacket */
4064.     }
4065.     /* "suit" is lame but "armor" is ambiguous and "body armor" is absurd */
4066.     return "suit";
4067. }
4068. 
4069. const char *
4070. cloak_simple_name(cloak)
4071. struct obj *cloak;
4072. {
4073.     if (cloak) {
4074.         switch (cloak->otyp) {
4075.         case ROBE:
4076.             return "robe";
4077.         case MUMMY_WRAPPING:
4078.             return "wrapping";
4079.         case ALCHEMY_SMOCK:
4080.             return (objects[cloak->otyp].oc_name_known && cloak->dknown)
4081.                        ? "smock"
4082.                        : "apron";
4083.         default:
4084.             break;
4085.         }
4086.     }
4087.     return "cloak";
4088. }
4089. 
4090. /* helm vs hat for messages */
4091. const char *
4092. helm_simple_name(helmet)
4093. struct obj *helmet;
4094. {
4095.     /*
4096.      *  There is some wiggle room here; the result has been chosen
4097.      *  for consistency with the "protected by hard helmet" messages
4098.      *  given for various bonks on the head:  headgear that provides
4099.      *  such protection is a "helm", that which doesn't is a "hat".
4100.      *
4101.      *      elven leather helm / leather hat    -> hat
4102.      *      dwarvish iron helm / hard hat       -> helm
4103.      *  The rest are completely straightforward:
4104.      *      fedora, cornuthaum, dunce cap       -> hat
4105.      *      all other types of helmets          -> helm
4106.      */
4107.     return (helmet && !is_metallic(helmet)) ? "hat" : "helm";
4108. }
4109. 
4110. const char *
4111. mimic_obj_name(mtmp)
4112. struct monst *mtmp;
4113. {
4114.     if (M_AP_TYPE(mtmp) == M_AP_OBJECT) {
4115.         if (mtmp->mappearance == GOLD_PIECE)
4116.             return "gold";
4117.         if (mtmp->mappearance != STRANGE_OBJECT)
4118.             return simple_typename(mtmp->mappearance);
4119.     }
4120.     return "whatcha-may-callit";
4121. }
4122. 
4123. /*
4124.  * Construct a query prompt string, based around an object name, which is
4125.  * guaranteed to fit within [QBUFSZ].  Takes an optional prefix, three
4126.  * choices for filling in the middle (two object formatting functions and a
4127.  * last resort literal which should be very short), and an optional suffix.
4128.  */
4129. char *
4130. safe_qbuf(qbuf, qprefix, qsuffix, obj, func, altfunc, lastR)
4131. char *qbuf; /* output buffer */
4132. const char *qprefix, *qsuffix;
4133. struct obj *obj;
4134. char *FDECL((*func), (OBJ_P)), *FDECL((*altfunc), (OBJ_P));
4135. const char *lastR;
4136. {
4137.     char *bufp, *endp;
4138.     /* convert size_t (or int for ancient systems) to ordinary unsigned */
4139.     unsigned len, lenlimit,
4140.         len_qpfx = (unsigned) (qprefix ? strlen(qprefix) : 0),
4141.         len_qsfx = (unsigned) (qsuffix ? strlen(qsuffix) : 0),
4142.         len_lastR = (unsigned) strlen(lastR);
4143. 
4144.     lenlimit = QBUFSZ - 1;
4145.     endp = qbuf + lenlimit;
4146.     /* sanity check, aimed mainly at paniclog (it's conceivable for
4147.        the result of short_oname() to be shorter than the length of
4148.        the last resort string, but we ignore that possibility here) */
4149.     if (len_qpfx > lenlimit)
4150.         impossible("safe_qbuf: prefix too long (%u characters).", len_qpfx);
4151.     else if (len_qpfx + len_qsfx > lenlimit)
4152.         impossible("safe_qbuf: suffix too long (%u + %u characters).",
4153.                    len_qpfx, len_qsfx);
4154.     else if (len_qpfx + len_lastR + len_qsfx > lenlimit)
4155.         impossible("safe_qbuf: filler too long (%u + %u + %u characters).",
4156.                    len_qpfx, len_lastR, len_qsfx);
4157. 
4158.     /* the output buffer might be the same as the prefix if caller
4159.        has already partially filled it */
4160.     if (qbuf == qprefix) {
4161.         /* prefix is already in the buffer */
4162.         *endp = '\0';
4163.     } else if (qprefix) {
4164.         /* put prefix into the buffer */
4165.         (void) strncpy(qbuf, qprefix, lenlimit);
4166.         *endp = '\0';
4167.     } else {
4168.         /* no prefix; output buffer starts out empty */
4169.         qbuf[0] = '\0';
4170.     }
4171.     len = (unsigned) strlen(qbuf);
4172. 
4173.     if (len + len_lastR + len_qsfx > lenlimit) {
4174.         /* too long; skip formatting, last resort output is truncated */
4175.         if (len < lenlimit) {
4176.             (void) strncpy(&qbuf[len], lastR, lenlimit - len);
4177.             *endp = '\0';
4178.             len = (unsigned) strlen(qbuf);
4179.             if (qsuffix && len < lenlimit) {
4180.                 (void) strncpy(&qbuf[len], qsuffix, lenlimit - len);
4181.                 *endp = '\0';
4182.                 /* len = (unsigned) strlen(qbuf); */
4183.             }
4184.         }
4185.     } else {
4186.         /* suffix and last resort are guaranteed to fit */
4187.         len += len_qsfx; /* include the pending suffix */
4188.         /* format the object */
4189.         bufp = short_oname(obj, func, altfunc, lenlimit - len);
4190.         if (len + strlen(bufp) <= lenlimit)
4191.             Strcat(qbuf, bufp); /* formatted name fits */
4192.         else
4193.             Strcat(qbuf, lastR); /* use last resort */
4194.         releaseobuf(bufp);
4195. 
4196.         if (qsuffix)
4197.             Strcat(qbuf, qsuffix);
4198.     }
4199.     /* assert( strlen(qbuf) < QBUFSZ ); */
4200.     return qbuf;
4201. }
4202. 
4203. /*objnam.c*/