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