1 /* $NetBSD: hack.objnam.c,v 1.9 2009/06/07 20:13:18 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.
64 #include <sys/cdefs.h>
66 __RCSID("$NetBSD: hack.objnam.c,v 1.9 2009/06/07 20:13:18 dholland Exp $");
72 #define Snprintf (void) snprintf
73 #define Strcat (void) strcat
74 #define Strcpy (void) strcpy
77 static char *strprepend(char *, char *);
78 static char *sitoa(int);
81 strprepend(char *s
, char *pref
)
85 pline("WARNING: prefix too short.");
89 (void) strncpy(s
, pref
, i
); /* do not copy trailing 0 */
97 Snprintf(buf
, sizeof(buf
), (a
< 0) ? "%d" : "+%d", a
);
104 static char buf
[BUFSZ
];
106 struct objclass
*ocl
= &objects
[otyp
];
107 const char *an
= ocl
->oc_name
;
108 const char *dn
= ocl
->oc_descr
;
109 char *un
= ocl
->oc_uname
;
110 int nn
= ocl
->oc_name_known
;
111 switch (ocl
->oc_olet
) {
113 Strcpy(buf
, "potion");
116 Strcpy(buf
, "scroll");
127 if (otyp
>= TURQUOISE
&& otyp
<= JADE
)
128 Strcat(buf
, " stone");
130 bufpos
= strlen(buf
);
131 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
135 bufpos
= strlen(buf
);
136 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
140 strlcpy(buf
, dn
? dn
: an
, sizeof(buf
));
141 if (ocl
->oc_olet
== GEM_SYM
) {
142 strlcat(buf
, " gem", sizeof(buf
));
145 bufpos
= strlen(buf
);
146 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
,
152 /* here for ring/scroll/potion/wand */
154 bufpos
= strlen(buf
);
155 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " of %s", an
);
158 bufpos
= strlen(buf
);
159 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " called %s", un
);
162 bufpos
= strlen(buf
);
163 Snprintf(buf
+bufpos
, sizeof(buf
)-bufpos
, " (%s)", dn
);
169 xname(struct obj
*obj
)
171 static char bufr
[BUFSZ
];
172 /* caution: doname() and aobjnam() below "know" these sizes */
173 char *buf
= &(bufr
[PREFIX
]); /* leave room for "17 -3 " */
174 size_t bufmax
= sizeof(bufr
) - PREFIX
;
175 int nn
= objects
[obj
->otyp
].oc_name_known
;
176 const char *an
= objects
[obj
->otyp
].oc_name
;
177 const char *dn
= objects
[obj
->otyp
].oc_descr
;
178 char *un
= objects
[obj
->otyp
].oc_uname
;
179 int pl
= (obj
->quan
!= 1);
181 if (!obj
->dknown
&& !Blind
)
182 obj
->dknown
= 1;/* %% doesnt belong here */
185 Strcpy(buf
, (obj
->spe
< 0 && obj
->known
)
186 ? "cheap plastic imitation of the " : "");
187 Strcat(buf
, "Amulet of Yendor");
191 strlcpy(buf
, dn
, bufmax
);
194 strlcpy(buf
, an
, bufmax
);
197 if (obj
->otyp
== DEAD_HOMUNCULUS
&& pl
) {
199 Strcpy(buf
, "dead homunculi");
203 /* fall into next case */
205 if (obj
->otyp
== WORM_TOOTH
&& pl
) {
207 Strcpy(buf
, "worm teeth");
210 if (obj
->otyp
== CRYSKNIFE
&& pl
) {
212 Strcpy(buf
, "crysknives");
215 /* fall into next case */
219 strlcpy(buf
, an
, bufmax
);
222 Snprintf(buf
, bufmax
, "%sheavy iron ball",
223 (obj
->owt
> objects
[obj
->otyp
].oc_weight
) ? "very " : "");
226 if (nn
|| un
|| !obj
->dknown
) {
227 Strcpy(buf
, "potion");
235 Strcat(buf
, " called ");
236 strlcat(buf
, un
, bufmax
);
239 strlcat(buf
, an
, bufmax
);
242 strlcpy(buf
, dn
, bufmax
);
243 strlcat(buf
, " potion", bufmax
);
247 Strcpy(buf
, "scroll");
256 strlcat(buf
, an
, bufmax
);
258 Strcat(buf
, " called ");
259 strlcat(buf
, un
, bufmax
);
261 Strcat(buf
, " labeled ");
262 strlcat(buf
, dn
, bufmax
);
267 Snprintf(buf
, bufmax
, "wand");
269 Snprintf(buf
, bufmax
, "wand of %s", an
);
271 Snprintf(buf
, bufmax
, "wand called %s", un
);
273 Snprintf(buf
, bufmax
, "%s wand", dn
);
277 Snprintf(buf
, bufmax
, "ring");
279 Snprintf(buf
, bufmax
, "ring of %s", an
);
281 Snprintf(buf
, bufmax
, "ring called %s", un
);
283 Snprintf(buf
, bufmax
, "%s ring", dn
);
291 Snprintf(buf
, bufmax
, "%s gem", dn
);
294 strlcpy(buf
, an
, bufmax
);
295 if (obj
->otyp
>= TURQUOISE
&& obj
->otyp
<= JADE
)
296 strlcat(buf
, " stone", bufmax
);
299 Snprintf(buf
, bufmax
, "glorkum %c (0%o) %u %d",
300 obj
->olet
, obj
->olet
, obj
->otyp
, obj
->spe
);
305 for (p
= buf
; *p
; p
++) {
306 if (!strncmp(" of ", p
, 4)) {
307 /* pieces of, cloves of, lumps of */
319 if (*p
== 's' || *p
== 'z' || *p
== 'x' ||
320 (*p
== 'h' && p
[-1] == 's')) {
322 strlcat(buf
, "es", bufmax
);
323 } else if (*p
== 'y' && !strchr(vowels
, p
[-1])) {
324 /* rubies, zruties */
326 strlcat(buf
, "ies", bufmax
);
328 strlcat(buf
, "s", bufmax
);
333 strlcat(buf
, " named ", bufmax
);
334 strlcat(buf
, ONAME(obj
), bufmax
);
340 doname(struct obj
*obj
)
343 char *bp
= xname(obj
);
346 /* XXX do this better somehow w/o knowing internals of xname() */
347 bpmax
= BUFSZ
- PREFIX
;
350 Snprintf(prefix
, sizeof(prefix
), "%u ", obj
->quan
);
352 Strcpy(prefix
, "a ");
355 if (strncmp(bp
, "cheap ", 6))
356 Strcpy(prefix
, "the ");
359 if (obj
->owornmask
& W_ARMOR
)
360 strlcat(bp
, " (being worn)", bpmax
);
361 /* fall into next case */
364 strlcat(prefix
, sitoa(obj
->spe
), sizeof(prefix
));
365 strlcat(prefix
, " ", sizeof(prefix
));
371 Snprintf(bp
+bppos
, bpmax
-bppos
, " (%d)", obj
->spe
);
375 if (obj
->owornmask
& W_RINGR
)
376 strlcat(bp
, " (on right hand)", bpmax
);
377 if (obj
->owornmask
& W_RINGL
)
378 strlcat(bp
, " (on left hand)", bpmax
);
379 if (obj
->known
&& (objects
[obj
->otyp
].bits
& SPEC
)) {
380 strlcat(prefix
, sitoa(obj
->spe
), sizeof(prefix
));
381 strlcat(prefix
, " ", sizeof(prefix
));
385 if (obj
->owornmask
& W_WEP
)
386 strlcat(bp
, " (weapon in hand)", bpmax
);
388 strlcat(bp
, " (unpaid)", bpmax
);
389 if (!strcmp(prefix
, "a ") && strchr(vowels
, *bp
))
390 Strcpy(prefix
, "an ");
391 bp
= strprepend(bp
, prefix
);
395 /* used only in hack.fight.c (thitu) */
397 setan(const char *str
, char *buf
, size_t bufmax
)
399 if (strchr(vowels
, *str
))
400 Snprintf(buf
, bufmax
, "an %s", str
);
402 Snprintf(buf
, bufmax
, "a %s", str
);
406 aobjnam(struct obj
*otmp
, const char *verb
)
408 char *bp
= xname(otmp
);
412 /* XXX do this better somehow w/o knowing internals of xname() */
413 bpmax
= BUFSZ
- PREFIX
;
415 if (otmp
->quan
!= 1) {
416 Snprintf(prefix
, sizeof(prefix
), "%u ", otmp
->quan
);
417 bp
= strprepend(bp
, prefix
);
420 /* verb is given in plural (i.e., without trailing s) */
421 strlcat(bp
, " ", bpmax
);
423 strlcat(bp
, verb
, bpmax
);
424 else if (!strcmp(verb
, "are"))
425 strlcat(bp
, "is", bpmax
);
427 strlcat(bp
, verb
, bpmax
);
428 strlcat(bp
, "s", bpmax
);
435 Doname(struct obj
*obj
)
437 char *s
= doname(obj
);
439 if ('a' <= *s
&& *s
<= 'z')
444 static const char *const wrp
[] = {"wand", "ring", "potion", "scroll", "gem"};
445 static const char wrpsym
[] = {WAND_SYM
, RING_SYM
, POTION_SYM
, SCROLL_SYM
, GEM_SYM
};
453 int cnt
, spe
, spesgn
, typ
, heavy
;
456 /* int the = 0; char *oname = 0; */
457 cnt
= spe
= spesgn
= typ
= heavy
= 0;
460 for (p
= bp
; *p
; p
++)
461 if ('A' <= *p
&& *p
<= 'Z')
463 if (!strncmp(bp
, "the ", 4)) {
466 } else if (!strncmp(bp
, "an ", 3)) {
469 } else if (!strncmp(bp
, "a ", 2)) {
473 if (!cnt
&& digit(*bp
)) {
481 cnt
= 1; /* %% what with "gems" etc. ? */
483 if (*bp
== '+' || *bp
== '-') {
484 spesgn
= (*bp
++ == '+') ? 1 : -1;
491 p
= strrchr(bp
, '(');
493 if (p
> bp
&& p
[-1] == ' ')
508 * now we have the actual name, as delivered by xname, say green
509 * potions called whisky scrolls labeled "QWERTY" egg dead zruties
510 * fortune cookies very heavy iron ball named hoei wand of wishing
513 for (p
= bp
; *p
; p
++)
514 if (!strncmp(p
, " named ", 7)) {
518 for (p
= bp
; *p
; p
++)
519 if (!strncmp(p
, " called ", 8)) {
523 for (p
= bp
; *p
; p
++)
524 if (!strncmp(p
, " labeled ", 9)) {
528 /* first change to singular if necessary */
530 /* find "cloves of garlic", "worthless pieces of blue glass" */
531 for (p
= bp
; *p
; p
++)
532 if (!strncmp(p
, "s of ", 5)) {
533 while ((*p
= p
[1]) != '\0')
537 /* remove -s or -es (boxes) or -ies (rubies, zruties) */
542 if (!strcmp(p
- 7, "cookies"))
547 /* note: cloves / knives from clove / knife */
548 if (!strcmp(p
- 6, "knives")) {
552 /* note: nurses, axes but boxes */
553 if (!strcmp(p
- 5, "boxes")) {
561 if (!strcmp(p
- 9, "homunculi")) {
562 Strcpy(p
- 1, "us"); /* !! makes string
566 if (!strcmp(p
- 5, "teeth")) {
567 Strcpy(p
- 5, "tooth");
570 /* here we cannot find the plural suffix */
574 if (!strcmp(bp
, "amulet of yendor")) {
575 typ
= AMULET_OF_YENDOR
;
579 if (!strcmp(p
- 5, " mail")) { /* Note: ring mail is not a ring ! */
584 for (ii
= 0; ii
< sizeof(wrpsym
); ii
++) {
585 int j
= strlen(wrp
[ii
]);
586 if (!strncmp(bp
, wrp
[ii
], j
)) {
589 if (!strncmp(bp
, " of ", 4))
591 /* else if(*bp) ?? */
594 if (!strcmp(p
- j
, wrp
[ii
])) {
604 if (!strcmp(p
- 6, " stone")) {
610 if (!strcmp(bp
, "very heavy iron ball")) {
612 typ
= HEAVY_IRON_BALL
;
617 if (!an
&& !dn
&& !un
)
621 i
= bases
[letindex(let
)];
622 while (i
<= NROFOBJECTS
&& (!let
|| objects
[i
].oc_olet
== let
)) {
623 const char *zn
= objects
[i
].oc_name
;
627 if (an
&& strcmp(an
, zn
))
629 if (dn
&& (!(zn
= objects
[i
].oc_descr
) || strcmp(dn
, zn
)))
631 if (un
&& (!(zn
= objects
[i
].oc_uname
) || strcmp(un
, zn
)))
640 let
= wrpsym
[rn2(sizeof(wrpsym
))];
645 let
= objects
[typ
].oc_olet
;
649 if (cnt
> 0 && strchr("%?!*)", let
) &&
650 (cnt
< 4 || (let
== WEAPON_SYM
&& typ
<= ROCK
&& cnt
< 20)))
653 if (spe
> 3 && spe
> otmp
->spe
)
655 else if (let
== WAND_SYM
)
657 if (spe
== 3 && u
.uluck
< 0)
659 if (let
!= WAND_SYM
&& spesgn
== -1)
663 else if (let
== AMULET_SYM
)
665 else if (typ
== WAN_WISHING
&& rn2(10))
666 spe
= (rn2(10) ? -1 : 0);