hack.objnam.c revision 1.7.10.1 1 /* $NetBSD: hack.objnam.c,v 1.7.10.1 2009/06/29 23:31:28 snj Exp $ */
2
3 /*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
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.
18 *
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
22 * written permission.
23 *
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.
35 */
36
37 /*
38 * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
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.
51 *
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.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.objnam.c,v 1.7.10.1 2009/06/29 23:31:28 snj Exp $");
67 #endif /* not lint */
68
69 #include <stdlib.h>
70 #include "hack.h"
71 #include "extern.h"
72 #define Snprintf (void) snprintf
73 #define Strcat (void) strcat
74 #define Strcpy (void) strcpy
75 #define PREFIX 15
76
77 char *
78 strprepend(s, pref)
79 char *s, *pref;
80 {
81 int i = strlen(pref);
82 if (i > PREFIX) {
83 pline("WARNING: prefix too short.");
84 return (s);
85 }
86 s -= i;
87 (void) strncpy(s, pref, i); /* do not copy trailing 0 */
88 return (s);
89 }
90
91 char *
92 sitoa(a)
93 int a;
94 {
95 static char buf[13];
96 Snprintf(buf, sizeof(buf), (a < 0) ? "%d" : "+%d", a);
97 return (buf);
98 }
99
100 char *
101 typename(otyp)
102 int otyp;
103 {
104 static char buf[BUFSZ];
105 size_t bufpos;
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) {
112 case POTION_SYM:
113 Strcpy(buf, "potion");
114 break;
115 case SCROLL_SYM:
116 Strcpy(buf, "scroll");
117 break;
118 case WAND_SYM:
119 Strcpy(buf, "wand");
120 break;
121 case RING_SYM:
122 Strcpy(buf, "ring");
123 break;
124 default:
125 if (nn) {
126 Strcpy(buf, an);
127 if (otyp >= TURQUOISE && otyp <= JADE)
128 Strcat(buf, " stone");
129 if (un) {
130 bufpos = strlen(buf);
131 Snprintf(buf+bufpos, sizeof(buf)-bufpos,
132 " called %s", un);
133 }
134 if (dn) {
135 bufpos = strlen(buf);
136 Snprintf(buf+bufpos, sizeof(buf)-bufpos,
137 " (%s)", dn);
138 }
139 } else {
140 strlcpy(buf, dn ? dn : an, sizeof(buf));
141 if (ocl->oc_olet == GEM_SYM) {
142 strlcat(buf, " gem", sizeof(buf));
143 }
144 if (un) {
145 bufpos = strlen(buf);
146 Snprintf(buf+bufpos, sizeof(buf)-bufpos,
147 " called %s", un);
148 }
149 }
150 return (buf);
151 }
152 /* here for ring/scroll/potion/wand */
153 if (nn) {
154 bufpos = strlen(buf);
155 Snprintf(buf+bufpos, sizeof(buf)-bufpos, " of %s", an);
156 }
157 if (un) {
158 bufpos = strlen(buf);
159 Snprintf(buf+bufpos, sizeof(buf)-bufpos, " called %s", un);
160 }
161 if (dn) {
162 bufpos = strlen(buf);
163 Snprintf(buf+bufpos, sizeof(buf)-bufpos, " (%s)", dn);
164 }
165 return (buf);
166 }
167
168 char *
169 xname(obj)
170 struct obj *obj;
171 {
172 static char bufr[BUFSZ];
173 /* caution: doname() and aobjnam() below "know" these sizes */
174 char *buf = &(bufr[PREFIX]); /* leave room for "17 -3 " */
175 size_t bufmax = sizeof(bufr) - PREFIX;
176 int nn = objects[obj->otyp].oc_name_known;
177 const char *an = objects[obj->otyp].oc_name;
178 const char *dn = objects[obj->otyp].oc_descr;
179 char *un = objects[obj->otyp].oc_uname;
180 int pl = (obj->quan != 1);
181
182 if (!obj->dknown && !Blind)
183 obj->dknown = 1;/* %% doesnt belong here */
184 switch (obj->olet) {
185 case AMULET_SYM:
186 Strcpy(buf, (obj->spe < 0 && obj->known)
187 ? "cheap plastic imitation of the " : "");
188 Strcat(buf, "Amulet of Yendor");
189 break;
190 case TOOL_SYM:
191 if (!nn) {
192 strlcpy(buf, dn, bufmax);
193 break;
194 }
195 strlcpy(buf, an, bufmax);
196 break;
197 case FOOD_SYM:
198 if (obj->otyp == DEAD_HOMUNCULUS && pl) {
199 pl = 0;
200 Strcpy(buf, "dead homunculi");
201 break;
202 }
203 /* fungis ? */
204 /* fall into next case */
205 case WEAPON_SYM:
206 if (obj->otyp == WORM_TOOTH && pl) {
207 pl = 0;
208 Strcpy(buf, "worm teeth");
209 break;
210 }
211 if (obj->otyp == CRYSKNIFE && pl) {
212 pl = 0;
213 Strcpy(buf, "crysknives");
214 break;
215 }
216 /* fall into next case */
217 case ARMOR_SYM:
218 case CHAIN_SYM:
219 case ROCK_SYM:
220 strlcpy(buf, an, bufmax);
221 break;
222 case BALL_SYM:
223 Snprintf(buf, bufmax, "%sheavy iron ball",
224 (obj->owt > objects[obj->otyp].oc_weight) ? "very " : "");
225 break;
226 case POTION_SYM:
227 if (nn || un || !obj->dknown) {
228 Strcpy(buf, "potion");
229 if (pl) {
230 pl = 0;
231 Strcat(buf, "s");
232 }
233 if (!obj->dknown)
234 break;
235 if (un) {
236 Strcat(buf, " called ");
237 strlcat(buf, un, bufmax);
238 } else {
239 Strcat(buf, " of ");
240 strlcat(buf, an, bufmax);
241 }
242 } else {
243 strlcpy(buf, dn, bufmax);
244 strlcat(buf, " potion", bufmax);
245 }
246 break;
247 case SCROLL_SYM:
248 Strcpy(buf, "scroll");
249 if (pl) {
250 pl = 0;
251 Strcat(buf, "s");
252 }
253 if (!obj->dknown)
254 break;
255 if (nn) {
256 Strcat(buf, " of ");
257 strlcat(buf, an, bufmax);
258 } else if (un) {
259 Strcat(buf, " called ");
260 strlcat(buf, un, bufmax);
261 } else {
262 Strcat(buf, " labeled ");
263 strlcat(buf, dn, bufmax);
264 }
265 break;
266 case WAND_SYM:
267 if (!obj->dknown)
268 Snprintf(buf, bufmax, "wand");
269 else if (nn)
270 Snprintf(buf, bufmax, "wand of %s", an);
271 else if (un)
272 Snprintf(buf, bufmax, "wand called %s", un);
273 else
274 Snprintf(buf, bufmax, "%s wand", dn);
275 break;
276 case RING_SYM:
277 if (!obj->dknown)
278 Snprintf(buf, bufmax, "ring");
279 else if (nn)
280 Snprintf(buf, bufmax, "ring of %s", an);
281 else if (un)
282 Snprintf(buf, bufmax, "ring called %s", un);
283 else
284 Snprintf(buf, bufmax, "%s ring", dn);
285 break;
286 case GEM_SYM:
287 if (!obj->dknown) {
288 Strcpy(buf, "gem");
289 break;
290 }
291 if (!nn) {
292 Snprintf(buf, bufmax, "%s gem", dn);
293 break;
294 }
295 strlcpy(buf, an, bufmax);
296 if (obj->otyp >= TURQUOISE && obj->otyp <= JADE)
297 strlcat(buf, " stone", bufmax);
298 break;
299 default:
300 Snprintf(buf, bufmax, "glorkum %c (0%o) %u %d",
301 obj->olet, obj->olet, obj->otyp, obj->spe);
302 }
303 if (pl) {
304 char *p;
305
306 for (p = buf; *p; p++) {
307 if (!strncmp(" of ", p, 4)) {
308 /* pieces of, cloves of, lumps of */
309 int c1, c2 = 's';
310
311 do {
312 c1 = c2;
313 c2 = *p;
314 *p++ = c1;
315 } while (c1);
316 goto nopl;
317 }
318 }
319 p = eos(buf) - 1;
320 if (*p == 's' || *p == 'z' || *p == 'x' ||
321 (*p == 'h' && p[-1] == 's')) {
322 /* boxes */
323 strlcat(buf, "es", bufmax);
324 } else if (*p == 'y' && !strchr(vowels, p[-1])) {
325 /* rubies, zruties */
326 *p = '\0';
327 strlcat(buf, "ies", bufmax);
328 } else {
329 strlcat(buf, "s", bufmax);
330 }
331 }
332 nopl:
333 if (obj->onamelth) {
334 strlcat(buf, " named ", bufmax);
335 strlcat(buf, ONAME(obj), bufmax);
336 }
337 return (buf);
338 }
339
340 char *
341 doname(obj)
342 struct obj *obj;
343 {
344 char prefix[PREFIX];
345 char *bp = xname(obj);
346 size_t bppos, bpmax;
347
348 /* XXX do this better somehow w/o knowing internals of xname() */
349 bpmax = BUFSZ - PREFIX;
350
351 if (obj->quan != 1)
352 Snprintf(prefix, sizeof(prefix), "%u ", obj->quan);
353 else
354 Strcpy(prefix, "a ");
355 switch (obj->olet) {
356 case AMULET_SYM:
357 if (strncmp(bp, "cheap ", 6))
358 Strcpy(prefix, "the ");
359 break;
360 case ARMOR_SYM:
361 if (obj->owornmask & W_ARMOR)
362 strlcat(bp, " (being worn)", bpmax);
363 /* fall into next case */
364 case WEAPON_SYM:
365 if (obj->known) {
366 strlcat(prefix, sitoa(obj->spe), sizeof(prefix));
367 strlcat(prefix, " ", sizeof(prefix));
368 }
369 break;
370 case WAND_SYM:
371 if (obj->known) {
372 bppos = strlen(bp);
373 Snprintf(bp+bppos, bpmax-bppos, " (%d)", obj->spe);
374 }
375 break;
376 case RING_SYM:
377 if (obj->owornmask & W_RINGR)
378 strlcat(bp, " (on right hand)", bpmax);
379 if (obj->owornmask & W_RINGL)
380 strlcat(bp, " (on left hand)", bpmax);
381 if (obj->known && (objects[obj->otyp].bits & SPEC)) {
382 strlcat(prefix, sitoa(obj->spe), sizeof(prefix));
383 strlcat(prefix, " ", sizeof(prefix));
384 }
385 break;
386 }
387 if (obj->owornmask & W_WEP)
388 strlcat(bp, " (weapon in hand)", bpmax);
389 if (obj->unpaid)
390 strlcat(bp, " (unpaid)", bpmax);
391 if (!strcmp(prefix, "a ") && strchr(vowels, *bp))
392 Strcpy(prefix, "an ");
393 bp = strprepend(bp, prefix);
394 return (bp);
395 }
396
397 /* used only in hack.fight.c (thitu) */
398 void
399 setan(const char *str, char *buf, size_t bufmax)
400 {
401 if (strchr(vowels, *str))
402 Snprintf(buf, bufmax, "an %s", str);
403 else
404 Snprintf(buf, bufmax, "a %s", str);
405 }
406
407 char *
408 aobjnam(otmp, verb)
409 struct obj *otmp;
410 const char *verb;
411 {
412 char *bp = xname(otmp);
413 char prefix[PREFIX];
414 size_t bpmax;
415
416 /* XXX do this better somehow w/o knowing internals of xname() */
417 bpmax = BUFSZ - PREFIX;
418
419 if (otmp->quan != 1) {
420 Snprintf(prefix, sizeof(prefix), "%u ", otmp->quan);
421 bp = strprepend(bp, prefix);
422 }
423 if (verb) {
424 /* verb is given in plural (i.e., without trailing s) */
425 strlcat(bp, " ", bpmax);
426 if (otmp->quan != 1)
427 strlcat(bp, verb, bpmax);
428 else if (!strcmp(verb, "are"))
429 strlcat(bp, "is", bpmax);
430 else {
431 strlcat(bp, verb, bpmax);
432 strlcat(bp, "s", bpmax);
433 }
434 }
435 return (bp);
436 }
437
438 char *
439 Doname(obj)
440 struct obj *obj;
441 {
442 char *s = doname(obj);
443
444 if ('a' <= *s && *s <= 'z')
445 *s -= ('a' - 'A');
446 return (s);
447 }
448
449 const char *const wrp[] = {"wand", "ring", "potion", "scroll", "gem"};
450 const char wrpsym[] = {WAND_SYM, RING_SYM, POTION_SYM, SCROLL_SYM, GEM_SYM};
451
452 struct obj *
453 readobjnam(bp)
454 char *bp;
455 {
456 char *p;
457 unsigned ii;
458 int i;
459 int cnt, spe, spesgn, typ, heavy;
460 char let;
461 char *un, *dn, *an;
462 /* int the = 0; char *oname = 0; */
463 cnt = spe = spesgn = typ = heavy = 0;
464 let = 0;
465 an = dn = un = 0;
466 for (p = bp; *p; p++)
467 if ('A' <= *p && *p <= 'Z')
468 *p += 'a' - 'A';
469 if (!strncmp(bp, "the ", 4)) {
470 /* the = 1; */
471 bp += 4;
472 } else if (!strncmp(bp, "an ", 3)) {
473 cnt = 1;
474 bp += 3;
475 } else if (!strncmp(bp, "a ", 2)) {
476 cnt = 1;
477 bp += 2;
478 }
479 if (!cnt && digit(*bp)) {
480 cnt = atoi(bp);
481 while (digit(*bp))
482 bp++;
483 while (*bp == ' ')
484 bp++;
485 }
486 if (!cnt)
487 cnt = 1; /* %% what with "gems" etc. ? */
488
489 if (*bp == '+' || *bp == '-') {
490 spesgn = (*bp++ == '+') ? 1 : -1;
491 spe = atoi(bp);
492 while (digit(*bp))
493 bp++;
494 while (*bp == ' ')
495 bp++;
496 } else {
497 p = strrchr(bp, '(');
498 if (p) {
499 if (p > bp && p[-1] == ' ')
500 p[-1] = 0;
501 else
502 *p = 0;
503 p++;
504 spe = atoi(p);
505 while (digit(*p))
506 p++;
507 if (strcmp(p, ")"))
508 spe = 0;
509 else
510 spesgn = 1;
511 }
512 }
513 /*
514 * now we have the actual name, as delivered by xname, say green
515 * potions called whisky scrolls labeled "QWERTY" egg dead zruties
516 * fortune cookies very heavy iron ball named hoei wand of wishing
517 * elven cloak
518 */
519 for (p = bp; *p; p++)
520 if (!strncmp(p, " named ", 7)) {
521 *p = 0;
522 /* oname = p+7; */
523 }
524 for (p = bp; *p; p++)
525 if (!strncmp(p, " called ", 8)) {
526 *p = 0;
527 un = p + 8;
528 }
529 for (p = bp; *p; p++)
530 if (!strncmp(p, " labeled ", 9)) {
531 *p = 0;
532 dn = p + 9;
533 }
534 /* first change to singular if necessary */
535 if (cnt != 1) {
536 /* find "cloves of garlic", "worthless pieces of blue glass" */
537 for (p = bp; *p; p++)
538 if (!strncmp(p, "s of ", 5)) {
539 while ((*p = p[1]) != '\0')
540 p++;
541 goto sing;
542 }
543 /* remove -s or -es (boxes) or -ies (rubies, zruties) */
544 p = eos(bp);
545 if (p[-1] == 's') {
546 if (p[-2] == 'e') {
547 if (p[-3] == 'i') {
548 if (!strcmp(p - 7, "cookies"))
549 goto mins;
550 Strcpy(p - 3, "y");
551 goto sing;
552 }
553 /* note: cloves / knives from clove / knife */
554 if (!strcmp(p - 6, "knives")) {
555 Strcpy(p - 3, "fe");
556 goto sing;
557 }
558 /* note: nurses, axes but boxes */
559 if (!strcmp(p - 5, "boxes")) {
560 p[-2] = 0;
561 goto sing;
562 }
563 }
564 mins:
565 p[-1] = 0;
566 } else {
567 if (!strcmp(p - 9, "homunculi")) {
568 Strcpy(p - 1, "us"); /* !! makes string
569 * longer */
570 goto sing;
571 }
572 if (!strcmp(p - 5, "teeth")) {
573 Strcpy(p - 5, "tooth");
574 goto sing;
575 }
576 /* here we cannot find the plural suffix */
577 }
578 }
579 sing:
580 if (!strcmp(bp, "amulet of yendor")) {
581 typ = AMULET_OF_YENDOR;
582 goto typfnd;
583 }
584 p = eos(bp);
585 if (!strcmp(p - 5, " mail")) { /* Note: ring mail is not a ring ! */
586 let = ARMOR_SYM;
587 an = bp;
588 goto srch;
589 }
590 for (ii = 0; ii < sizeof(wrpsym); ii++) {
591 int j = strlen(wrp[ii]);
592 if (!strncmp(bp, wrp[ii], j)) {
593 let = wrpsym[ii];
594 bp += j;
595 if (!strncmp(bp, " of ", 4))
596 an = bp + 4;
597 /* else if(*bp) ?? */
598 goto srch;
599 }
600 if (!strcmp(p - j, wrp[ii])) {
601 let = wrpsym[ii];
602 p -= j;
603 *p = 0;
604 if (p[-1] == ' ')
605 p[-1] = 0;
606 dn = bp;
607 goto srch;
608 }
609 }
610 if (!strcmp(p - 6, " stone")) {
611 p[-6] = 0;
612 let = GEM_SYM;
613 an = bp;
614 goto srch;
615 }
616 if (!strcmp(bp, "very heavy iron ball")) {
617 heavy = 1;
618 typ = HEAVY_IRON_BALL;
619 goto typfnd;
620 }
621 an = bp;
622 srch:
623 if (!an && !dn && !un)
624 goto any;
625 i = 1;
626 if (let)
627 i = bases[letindex(let)];
628 while (i <= NROFOBJECTS && (!let || objects[i].oc_olet == let)) {
629 const char *zn = objects[i].oc_name;
630
631 if (!zn)
632 goto nxti;
633 if (an && strcmp(an, zn))
634 goto nxti;
635 if (dn && (!(zn = objects[i].oc_descr) || strcmp(dn, zn)))
636 goto nxti;
637 if (un && (!(zn = objects[i].oc_uname) || strcmp(un, zn)))
638 goto nxti;
639 typ = i;
640 goto typfnd;
641 nxti:
642 i++;
643 }
644 any:
645 if (!let)
646 let = wrpsym[rn2(sizeof(wrpsym))];
647 typ = probtype(let);
648 typfnd:
649 {
650 struct obj *otmp;
651 let = objects[typ].oc_olet;
652 otmp = mksobj(typ);
653 if (heavy)
654 otmp->owt += 15;
655 if (cnt > 0 && strchr("%?!*)", let) &&
656 (cnt < 4 || (let == WEAPON_SYM && typ <= ROCK && cnt < 20)))
657 otmp->quan = cnt;
658
659 if (spe > 3 && spe > otmp->spe)
660 spe = 0;
661 else if (let == WAND_SYM)
662 spe = otmp->spe;
663 if (spe == 3 && u.uluck < 0)
664 spesgn = -1;
665 if (let != WAND_SYM && spesgn == -1)
666 spe = -spe;
667 if (let == BALL_SYM)
668 spe = 0;
669 else if (let == AMULET_SYM)
670 spe = -1;
671 else if (typ == WAN_WISHING && rn2(10))
672 spe = (rn2(10) ? -1 : 0);
673 otmp->spe = spe;
674
675 if (spesgn == -1)
676 otmp->cursed = 1;
677
678 return (otmp);
679 }
680 }
681