Source:NetHack 2.2a/objnam.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to objnam.c from the source code of NetHack 2.2a. To link to a particular line, write [[NetHack 2.2a/objnam.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

Screenshots and source code from Hack are used under the CWI license.

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