Source:NetHack 1.3d/objnam.c

From NetHackWiki
Jump to: navigation, search

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