1 /* $NetBSD: hack.objnam.c,v 1.11 2011/08/07 06:03:45 dholland Exp $ */
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 #define Snprintf (void) snprintf
68 #define Strcat (void) strcat
69 #define Strcpy (void) strcpy
72 static char *strprepend(char *, char *);
73 static char *sitoa(int);
76 strprepend(char *s
, char *pref
)
80 pline("WARNING: prefix too short.");
84 (void) strncpy(s
, pref
, i
); /* do not copy trailing 0 */
92 Snprintf(buf
, sizeof(buf
), (a
< 0) ? "%d" : "+%d", a
);
99 static char buf
[BUFSZ
];
101 struct objclass
*ocl
= &objects
[otyp
];
102 const char *an
= ocl
->oc_name
;
103 const char *dn
= ocl
->oc_descr
;
104 char *un
= ocl
->oc_uname
;
105 int nn
= ocl
->oc_name_known
;
106 switch (ocl
->oc_olet
) {
108 Strcpy(buf
, "potion");
111 Strcpy(buf
, "scroll");
122 if (otyp
>= TURQUOISE
&& otyp
<= JADE
)
123 Strcat(buf
, " stone");
125 bufpos
= strlen(buf
);
126 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
130 bufpos
= strlen(buf
);
131 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
135 strlcpy(buf
, dn
? dn
: an
, sizeof(buf
));
136 if (ocl
->oc_olet
== GEM_SYM
) {
137 strlcat(buf
, " gem", sizeof(buf
));
140 bufpos
= strlen(buf
);
141 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
147 /* here for ring/scroll/potion/wand */
149 bufpos
= strlen(buf
);
150 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " of %s", an
);
153 bufpos
= strlen(buf
);
154 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " called %s", un
);
157 bufpos
= strlen(buf
);
158 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " (%s)", dn
);
164 xname(struct obj
*obj
)
166 static char bufr
[BUFSZ
];
167 /* caution: doname() and aobjnam() below "know" these sizes */
168 char *buf
= &(bufr
[PREFIX
]); /* leave room for "17 -3 " */
169 size_t bufmax
= sizeof(bufr
) - PREFIX
;
170 int nn
= objects
[obj
->otyp
].oc_name_known
;
171 const char *an
= objects
[obj
->otyp
].oc_name
;
172 const char *dn
= objects
[obj
->otyp
].oc_descr
;
173 char *un
= objects
[obj
->otyp
].oc_uname
;
174 int pl
= (obj
->quan
!= 1);
176 if (!obj
->dknown
&& !Blind
)
177 obj
->dknown
= 1;/* %% doesnt belong here */
180 Strcpy(buf
, (obj
->spe
< 0 && obj
->known
)
181 ? "cheap plastic imitation of the " : "");
182 Strcat(buf
, "Amulet of Yendor");
186 strlcpy(buf
, dn
, bufmax
);
189 strlcpy(buf
, an
, bufmax
);
192 if (obj
->otyp
== DEAD_HOMUNCULUS
&& pl
) {
194 Strcpy(buf
, "dead homunculi");
200 if (obj
->otyp
== WORM_TOOTH
&& pl
) {
202 Strcpy(buf
, "worm teeth");
205 if (obj
->otyp
== CRYSKNIFE
&& pl
) {
207 Strcpy(buf
, "crysknives");
214 strlcpy(buf
, an
, bufmax
);
217 Snprintf(buf
, bufmax
, "%sheavy iron ball",
218 (obj
->owt
> objects
[obj
->otyp
].oc_weight
) ? "very " : "");
221 if (nn
|| un
|| !obj
->dknown
) {
222 Strcpy(buf
, "potion");
230 Strcat(buf
, " called ");
231 strlcat(buf
, un
, bufmax
);
234 strlcat(buf
, an
, bufmax
);
237 strlcpy(buf
, dn
, bufmax
);
238 strlcat(buf
, " potion", bufmax
);
242 Strcpy(buf
, "scroll");
251 strlcat(buf
, an
, bufmax
);
253 Strcat(buf
, " called ");
254 strlcat(buf
, un
, bufmax
);
256 Strcat(buf
, " labeled ");
257 strlcat(buf
, dn
, bufmax
);
262 Snprintf(buf
, bufmax
, "wand");
264 Snprintf(buf
, bufmax
, "wand of %s", an
);
266 Snprintf(buf
, bufmax
, "wand called %s", un
);
268 Snprintf(buf
, bufmax
, "%s wand", dn
);
272 Snprintf(buf
, bufmax
, "ring");
274 Snprintf(buf
, bufmax
, "ring of %s", an
);
276 Snprintf(buf
, bufmax
, "ring called %s", un
);
278 Snprintf(buf
, bufmax
, "%s ring", dn
);
286 Snprintf(buf
, bufmax
, "%s gem", dn
);
289 strlcpy(buf
, an
, bufmax
);
290 if (obj
->otyp
>= TURQUOISE
&& obj
->otyp
<= JADE
)
291 strlcat(buf
, " stone", bufmax
);
294 Snprintf(buf
, bufmax
, "glorkum %c (0%o) %u %d",
295 obj
->olet
, obj
->olet
, obj
->otyp
, obj
->spe
);
300 for (p
= buf
; *p
; p
++) {
301 if (!strncmp(" of ", p
, 4)) {
302 /* pieces of, cloves of, lumps of */
314 if (*p
== 's' || *p
== 'z' || *p
== 'x' ||
315 (*p
== 'h' && p
[-1] == 's')) {
317 strlcat(buf
, "es", bufmax
);
318 } else if (*p
== 'y' && !strchr(vowels
, p
[-1])) {
319 /* rubies, zruties */
321 strlcat(buf
, "ies", bufmax
);
323 strlcat(buf
, "s", bufmax
);
328 strlcat(buf
, " named ", bufmax
);
329 strlcat(buf
, ONAME(obj
), bufmax
);
335 doname(struct obj
*obj
)
338 char *bp
= xname(obj
);
341 /* XXX do this better somehow w/o knowing internals of xname() */
342 bpmax
= BUFSZ
- PREFIX
;
345 Snprintf(prefix
, sizeof(prefix
), "%u ", obj
->quan
);
347 Strcpy(prefix
, "a ");
350 if (strncmp(bp
, "cheap ", 6))
351 Strcpy(prefix
, "the ");
354 if (obj
->owornmask
& W_ARMOR
)
355 strlcat(bp
, " (being worn)", bpmax
);
359 strlcat(prefix
, sitoa(obj
->spe
), sizeof(prefix
));
360 strlcat(prefix
, " ", sizeof(prefix
));
366 Snprintf(bp
+bppos
, bpmax
-bppos
, " (%d)", obj
->spe
);
370 if (obj
->owornmask
& W_RINGR
)
371 strlcat(bp
, " (on right hand)", bpmax
);
372 if (obj
->owornmask
& W_RINGL
)
373 strlcat(bp
, " (on left hand)", bpmax
);
374 if (obj
->known
&& (objects
[obj
->otyp
].bits
& SPEC
)) {
375 strlcat(prefix
, sitoa(obj
->spe
), sizeof(prefix
));
376 strlcat(prefix
, " ", sizeof(prefix
));
380 if (obj
->owornmask
& W_WEP
)
381 strlcat(bp
, " (weapon in hand)", bpmax
);
383 strlcat(bp
, " (unpaid)", bpmax
);
384 if (!strcmp(prefix
, "a ") && strchr(vowels
, *bp
))
385 Strcpy(prefix
, "an ");
386 bp
= strprepend(bp
, prefix
);
390 /* used only in hack.fight.c (thitu) */
392 setan(const char *str
, char *buf
, size_t bufmax
)
394 if (strchr(vowels
, *str
))
395 Snprintf(buf
, bufmax
, "an %s", str
);
397 Snprintf(buf
, bufmax
, "a %s", str
);
401 aobjnam(struct obj
*otmp
, const char *verb
)
403 char *bp
= xname(otmp
);
407 /* XXX do this better somehow w/o knowing internals of xname() */
408 bpmax
= BUFSZ
- PREFIX
;
410 if (otmp
->quan
!= 1) {
411 Snprintf(prefix
, sizeof(prefix
), "%u ", otmp
->quan
);
412 bp
= strprepend(bp
, prefix
);
415 /* verb is given in plural (i.e., without trailing s) */
416 strlcat(bp
, " ", bpmax
);
418 strlcat(bp
, verb
, bpmax
);
419 else if (!strcmp(verb
, "are"))
420 strlcat(bp
, "is", bpmax
);
422 strlcat(bp
, verb
, bpmax
);
423 strlcat(bp
, "s", bpmax
);
430 Doname(struct obj
*obj
)
432 char *s
= doname(obj
);
434 if ('a' <= *s
&& *s
<= 'z')
439 static const char *const wrp
[] = {"wand", "ring", "potion", "scroll", "gem"};
440 static const char wrpsym
[] = {WAND_SYM
, RING_SYM
, POTION_SYM
, SCROLL_SYM
, GEM_SYM
};
448 int cnt
, spe
, spesgn
, typ
, heavy
;
451 /* int the = 0; char *oname = 0; */
452 cnt
= spe
= spesgn
= typ
= heavy
= 0;
455 for (p
= bp
; *p
; p
++)
456 if ('A' <= *p
&& *p
<= 'Z')
458 if (!strncmp(bp
, "the ", 4)) {
461 } else if (!strncmp(bp
, "an ", 3)) {
464 } else if (!strncmp(bp
, "a ", 2)) {
468 if (!cnt
&& digit(*bp
)) {
476 cnt
= 1; /* %% what with "gems" etc. ? */
478 if (*bp
== '+' || *bp
== '-') {
479 spesgn
= (*bp
++ == '+') ? 1 : -1;
486 p
= strrchr(bp
, '(');
488 if (p
> bp
&& p
[-1] == ' ')
503 * now we have the actual name, as delivered by xname, say green
504 * potions called whisky scrolls labeled "QWERTY" egg dead zruties
505 * fortune cookies very heavy iron ball named hoei wand of wishing
508 for (p
= bp
; *p
; p
++)
509 if (!strncmp(p
, " named ", 7)) {
513 for (p
= bp
; *p
; p
++)
514 if (!strncmp(p
, " called ", 8)) {
518 for (p
= bp
; *p
; p
++)
519 if (!strncmp(p
, " labeled ", 9)) {
523 /* first change to singular if necessary */
525 /* find "cloves of garlic", "worthless pieces of blue glass" */
526 for (p
= bp
; *p
; p
++)
527 if (!strncmp(p
, "s of ", 5)) {
528 while ((*p
= p
[1]) != '\0')
532 /* remove -s or -es (boxes) or -ies (rubies, zruties) */
537 if (!strcmp(p
- 7, "cookies"))
542 /* note: cloves / knives from clove / knife */
543 if (!strcmp(p
- 6, "knives")) {
547 /* note: nurses, axes but boxes */
548 if (!strcmp(p
- 5, "boxes")) {
556 if (!strcmp(p
- 9, "homunculi")) {
557 Strcpy(p
- 1, "us"); /* !! makes string
561 if (!strcmp(p
- 5, "teeth")) {
562 Strcpy(p
- 5, "tooth");
565 /* here we cannot find the plural suffix */
569 if (!strcmp(bp
, "amulet of yendor")) {
570 typ
= AMULET_OF_YENDOR
;
574 if (!strcmp(p
- 5, " mail")) { /* Note: ring mail is not a ring ! */
579 for (ii
= 0; ii
< sizeof(wrpsym
); ii
++) {
580 int j
= strlen(wrp
[ii
]);
581 if (!strncmp(bp
, wrp
[ii
], j
)) {
584 if (!strncmp(bp
, " of ", 4))
586 /* else if(*bp) ?? */
589 if (!strcmp(p
- j
, wrp
[ii
])) {
599 if (!strcmp(p
- 6, " stone")) {
605 if (!strcmp(bp
, "very heavy iron ball")) {
607 typ
= HEAVY_IRON_BALL
;
612 if (!an
&& !dn
&& !un
)
616 i
= bases
[letindex(let
)];
617 while (i
<= NROFOBJECTS
&& (!let
|| objects
[i
].oc_olet
== let
)) {
618 const char *zn
= objects
[i
].oc_name
;
622 if (an
&& strcmp(an
, zn
))
624 if (dn
&& (!(zn
= objects
[i
].oc_descr
) || strcmp(dn
, zn
)))
626 if (un
&& (!(zn
= objects
[i
].oc_uname
) || strcmp(un
, zn
)))
635 let
= wrpsym
[rn2(sizeof(wrpsym
))];
640 let
= objects
[typ
].oc_olet
;
644 if (cnt
> 0 && strchr("%?!*)", let
) &&
645 (cnt
< 4 || (let
== WEAPON_SYM
&& typ
<= ROCK
&& cnt
< 20)))
648 if (spe
> 3 && spe
> otmp
->spe
)
650 else if (let
== WAND_SYM
)
652 if (spe
== 3 && u
.uluck
< 0)
654 if (let
!= WAND_SYM
&& spesgn
== -1)
658 else if (let
== AMULET_SYM
)
660 else if (typ
== WAN_WISHING
&& rn2(10))
661 spe
= (rn2(10) ? -1 : 0);