hack.objnam.c revision 1.6.26.1 1 /* $NetBSD: hack.objnam.c,v 1.6.26.1 2009/06/29 23:43:48 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.6.26.1 2009/06/29 23:43:48 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 int i;
458 int cnt, spe, spesgn, typ, heavy;
459 char let;
460 char *un, *dn, *an;
461 /* int the = 0; char *oname = 0; */
462 cnt = spe = spesgn = typ = heavy = 0;
463 let = 0;
464 an = dn = un = 0;
465 for (p = bp; *p; p++)
466 if ('A' <= *p && *p <= 'Z')
467 *p += 'a' - 'A';
468 if (!strncmp(bp, "the ", 4)) {
469 /* the = 1; */
470 bp += 4;
471 } else if (!strncmp(bp, "an ", 3)) {
472 cnt = 1;
473 bp += 3;
474 } else if (!strncmp(bp, "a ", 2)) {
475 cnt = 1;
476 bp += 2;
477 }
478 if (!cnt && digit(*bp)) {
479 cnt = atoi(bp);
480 while (digit(*bp))
481 bp++;
482 while (*bp == ' ')
483 bp++;
484 }
485 if (!cnt)
486 cnt = 1; /* %% what with "gems" etc. ? */
487
488 if (*bp == '+' || *bp == '-') {
489 spesgn = (*bp++ == '+') ? 1 : -1;
490 spe = atoi(bp);
491 while (digit(*bp))
492 bp++;
493 while (*bp == ' ')
494 bp++;
495 } else {
496 p = strrchr(bp, '(');
497 if (p) {
498 if (p > bp && p[-1] == ' ')
499 p[-1] = 0;
500 else
501 *p = 0;
502 p++;
503 spe = atoi(p);
504 while (digit(*p))
505 p++;
506 if (strcmp(p, ")"))
507 spe = 0;
508 else
509 spesgn = 1;
510 }
511 }
512 /*
513 * now we have the actual name, as delivered by xname, say green
514 * potions called whisky scrolls labeled "QWERTY" egg dead zruties
515 * fortune cookies very heavy iron ball named hoei wand of wishing
516 * elven cloak
517 */
518 for (p = bp; *p; p++)
519 if (!strncmp(p, " named ", 7)) {
520 *p = 0;
521 /* oname = p+7; */
522 }
523 for (p = bp; *p; p++)
524 if (!strncmp(p, " called ", 8)) {
525 *p = 0;
526 un = p + 8;
527 }
528 for (p = bp; *p; p++)
529 if (!strncmp(p, " labeled ", 9)) {
530 *p = 0;
531 dn = p + 9;
532 }
533 /* first change to singular if necessary */
534 if (cnt != 1) {
535 /* find "cloves of garlic", "worthless pieces of blue glass" */
536 for (p = bp; *p; p++)
537 if (!strncmp(p, "s of ", 5)) {
538 while ((*p = p[1]) != '\0')
539 p++;
540 goto sing;
541 }
542 /* remove -s or -es (boxes) or -ies (rubies, zruties) */
543 p = eos(bp);
544 if (p[-1] == 's') {
545 if (p[-2] == 'e') {
546 if (p[-3] == 'i') {
547 if (!strcmp(p - 7, "cookies"))
548 goto mins;
549 Strcpy(p - 3, "y");
550 goto sing;
551 }
552 /* note: cloves / knives from clove / knife */
553 if (!strcmp(p - 6, "knives")) {
554 Strcpy(p - 3, "fe");
555 goto sing;
556 }
557 /* note: nurses, axes but boxes */
558 if (!strcmp(p - 5, "boxes")) {
559 p[-2] = 0;
560 goto sing;
561 }
562 }
563 mins:
564 p[-1] = 0;
565 } else {
566 if (!strcmp(p - 9, "homunculi")) {
567 Strcpy(p - 1, "us"); /* !! makes string
568 * longer */
569 goto sing;
570 }
571 if (!strcmp(p - 5, "teeth")) {
572 Strcpy(p - 5, "tooth");
573 goto sing;
574 }
575 /* here we cannot find the plural suffix */
576 }
577 }
578 sing:
579 if (!strcmp(bp, "amulet of yendor")) {
580 typ = AMULET_OF_YENDOR;
581 goto typfnd;
582 }
583 p = eos(bp);
584 if (!strcmp(p - 5, " mail")) { /* Note: ring mail is not a ring ! */
585 let = ARMOR_SYM;
586 an = bp;
587 goto srch;
588 }
589 for (i = 0; i < sizeof(wrpsym); i++) {
590 int j = strlen(wrp[i]);
591 if (!strncmp(bp, wrp[i], j)) {
592 let = wrpsym[i];
593 bp += j;
594 if (!strncmp(bp, " of ", 4))
595 an = bp + 4;
596 /* else if(*bp) ?? */
597 goto srch;
598 }
599 if (!strcmp(p - j, wrp[i])) {
600 let = wrpsym[i];
601 p -= j;
602 *p = 0;
603 if (p[-1] == ' ')
604 p[-1] = 0;
605 dn = bp;
606 goto srch;
607 }
608 }
609 if (!strcmp(p - 6, " stone")) {
610 p[-6] = 0;
611 let = GEM_SYM;
612 an = bp;
613 goto srch;
614 }
615 if (!strcmp(bp, "very heavy iron ball")) {
616 heavy = 1;
617 typ = HEAVY_IRON_BALL;
618 goto typfnd;
619 }
620 an = bp;
621 srch:
622 if (!an && !dn && !un)
623 goto any;
624 i = 1;
625 if (let)
626 i = bases[letindex(let)];
627 while (i <= NROFOBJECTS && (!let || objects[i].oc_olet == let)) {
628 const char *zn = objects[i].oc_name;
629
630 if (!zn)
631 goto nxti;
632 if (an && strcmp(an, zn))
633 goto nxti;
634 if (dn && (!(zn = objects[i].oc_descr) || strcmp(dn, zn)))
635 goto nxti;
636 if (un && (!(zn = objects[i].oc_uname) || strcmp(un, zn)))
637 goto nxti;
638 typ = i;
639 goto typfnd;
640 nxti:
641 i++;
642 }
643 any:
644 if (!let)
645 let = wrpsym[rn2(sizeof(wrpsym))];
646 typ = probtype(let);
647 typfnd:
648 {
649 struct obj *otmp;
650 let = objects[typ].oc_olet;
651 otmp = mksobj(typ);
652 if (heavy)
653 otmp->owt += 15;
654 if (cnt > 0 && strchr("%?!*)", let) &&
655 (cnt < 4 || (let == WEAPON_SYM && typ <= ROCK && cnt < 20)))
656 otmp->quan = cnt;
657
658 if (spe > 3 && spe > otmp->spe)
659 spe = 0;
660 else if (let == WAND_SYM)
661 spe = otmp->spe;
662 if (spe == 3 && u.uluck < 0)
663 spesgn = -1;
664 if (let != WAND_SYM && spesgn == -1)
665 spe = -spe;
666 if (let == BALL_SYM)
667 spe = 0;
668 else if (let == AMULET_SYM)
669 spe = -1;
670 else if (typ == WAN_WISHING && rn2(10))
671 spe = (rn2(10) ? -1 : 0);
672 otmp->spe = spe;
673
674 if (spesgn == -1)
675 otmp->cursed = 1;
676
677 return (otmp);
678 }
679 }
680