Source:Hack 1.0/hack.objnam.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to hack.objnam.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.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.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include	"hack.h"
4.    #define Sprintf (void) sprintf
5.    #define Strcat  (void) strcat
6.    #define	Strcpy	(void) strcpy
7.    #define	PREFIX	15
8.    extern char *eos();
9.    extern int bases[];
10.   
11.   char *
12.   strprepend(s,pref) register char *s, *pref; {
13.   register int i = strlen(pref);
14.   	if(i > PREFIX) {
15.   		pline("WARNING: prefix too short.");
16.   		return(s);
17.   	}
18.   	s -= i;
19.   	(void) strncpy(s, pref, i);	/* do not copy trailing 0 */
20.   	return(s);
21.   }
22.   
23.   char *
24.   sitoa(a) int a; {
25.   static char buf[13];
26.   	Sprintf(buf, (a < 0) ? "%d" : "+%d", a);
27.   	return(buf);
28.   }
29.   
30.   char *
31.   xname(obj)
32.   register struct obj *obj;
33.   {
34.   static char bufr[BUFSZ];
35.   register char *buf = &(bufr[PREFIX]);	/* leave room for "17 -3 " */
36.   register int nn = objects[obj->otyp].oc_name_known;
37.   register char *an = objects[obj->otyp].oc_name;
38.   register char *dn = objects[obj->otyp].oc_descr;
39.   register char *un = objects[obj->otyp].oc_uname;
40.   register int pl = (obj->quan != 1);
41.   	if(!obj->dknown && !Blind) obj->dknown = 1; /* %% doesnt belong here */
42.   	switch(obj->olet) {
43.   	case AMULET_SYM:
44.   		Strcpy(buf, (obj->spe < 0 && obj->known)
45.   			? "cheap plastic imitation of the " : "");
46.   		Strcat(buf,"Amulet of Yendor");
47.   		break;
48.   	case TOOL_SYM:
49.   		if(!nn) {
50.   			Strcpy(buf, dn);
51.   			break;
52.   		}
53.   		Strcpy(buf,an);
54.   		break;
55.   	case FOOD_SYM:
56.   		if(obj->otyp == CLOVE_OF_GARLIC && pl) {
57.   			pl = 0;
58.   			Strcpy(buf, "cloves of garlic");
59.   			break;
60.   		}
61.   		if(obj->otyp == DEAD_HOMUNCULUS && pl) {
62.   			pl = 0;
63.   			Strcpy(buf, "dead homunculi");
64.   			break;
65.   		}
66.   		/* fungis ? */
67.   		/* fall into next case */
68.   	case WEAPON_SYM:
69.   		if(obj->otyp == WORM_TOOTH && pl) {
70.   			pl = 0;
71.   			Strcpy(buf, "worm teeth");
72.   			break;
73.   		}
74.   		if(obj->otyp == CRYSKNIFE && pl) {
75.   			pl = 0;
76.   			Strcpy(buf, "crysknives");
77.   			break;
78.   		}
79.   		/* fall into next case */
80.   	case ARMOR_SYM:
81.   	case CHAIN_SYM:
82.   	case ROCK_SYM:
83.   		Strcpy(buf,an);
84.   		break;
85.   	case BALL_SYM:
86.   		Sprintf(buf, "%sheavy iron ball",
87.   		  (obj->owt > objects[obj->otyp].oc_weight) ? "very " : "");
88.   		break;
89.   	case POTION_SYM:
90.   		if(nn || un || !obj->dknown) {
91.   			Strcpy(buf, "potion");
92.   			if(pl) {
93.   				pl = 0;
94.   				Strcat(buf, "s");
95.   			}
96.   			if(!obj->dknown) break;
97.   			if(un) {
98.   				Strcat(buf, " called ");
99.   				Strcat(buf, un);
100.  			} else {
101.  				Strcat(buf, " of ");
102.  				Strcat(buf, an);
103.  			}
104.  		} else {
105.  			Strcpy(buf, dn);
106.  			Strcat(buf, " potion");
107.  		}
108.  		break;
109.  	case SCROLL_SYM:
110.  		Strcpy(buf, "scroll");
111.  		if(pl) {
112.  			pl = 0;
113.  			Strcat(buf, "s");
114.  		}
115.  		if(!obj->dknown) break;
116.  		if(nn) {
117.  			Strcat(buf, " of ");
118.  			Strcat(buf, an);
119.  		} else if(un) {
120.  			Strcat(buf, " called ");
121.  			Strcat(buf, un);
122.  		} else {
123.  			Strcat(buf, " labeled ");
124.  			Strcat(buf, dn);
125.  		}
126.  		break;
127.  	case WAND_SYM:
128.  		if(!obj->dknown)
129.  			Sprintf(buf, "wand");
130.  		else if(nn)
131.  			Sprintf(buf, "wand of %s", an);
132.  		else if(un)
133.  			Sprintf(buf, "wand called %s", un);
134.  		else
135.  			Sprintf(buf, "%s wand", dn);
136.  		break;
137.  	case RING_SYM:
138.  		if(!obj->dknown)
139.  			Sprintf(buf, "ring");
140.  		else if(nn)
141.  			Sprintf(buf, "ring of %s", an);
142.  		else if(un)
143.  			Sprintf(buf, "ring called %s", un);
144.  		else
145.  			Sprintf(buf, "%s ring", dn);
146.  		break;
147.  	case GEM_SYM:
148.  		if(!obj->dknown) {
149.  			Strcpy(buf, "gem");
150.  			break;
151.  		}
152.  		if(!nn) {
153.  			Sprintf(buf, "%s gem", dn);
154.  			break;
155.  		}
156.  		if(pl && !strncmp("worthless piece", an, 15)) {
157.  			pl = 0;
158.  			Sprintf(buf, "worthless pieces%s", an+15);
159.  			break;
160.  		}
161.  		Strcpy(buf, an);
162.  		if(obj->otyp >= TURQUOISE && obj->otyp <= JADE)
163.  			Strcat(buf, " stone");
164.  		break;
165.  	default:
166.  		Sprintf(buf,"glorkum %c (0%o) %d %d",
167.  			obj->olet,obj->olet,obj->otyp,obj->spe);
168.  	}
169.  	if(pl) {
170.  		register char *p = eos(buf)-1;
171.  		if(*p == 's' || *p == 'z' || *p == 'x' ||
172.  		    (*p == 'h' && p[-1] == 's'))
173.  			Strcat(buf, "es");	/* boxes */
174.  		else if(*p == 'y' && !index(vowels, p[-1]))
175.  			Strcpy(p, "ies");	/* rubies, zruties */
176.  		else
177.  			Strcat(buf, "s");
178.  	}
179.  	if(obj->onamelth) {
180.  		Strcat(buf, " named ");
181.  		Strcat(buf, ONAME(obj));
182.  	}
183.   return(buf);
184.  }
185.  
186.  char *
187.  doname(obj)
188.  register struct obj *obj;
189.  {
190.  char prefix[PREFIX];
191.  register char *bp = xname(obj);
192.  	if(obj->quan != 1)
193.  		Sprintf(prefix, "%d ", obj->quan);
194.  	else
195.  		Strcpy(prefix, "a ");
196.  	switch(obj->olet) {
197.  	case AMULET_SYM:
198.  		if(strncmp(bp, "cheap ", 6))
199.  			Strcpy(prefix, "the ");
200.  		break;
201.  	case ARMOR_SYM:
202.  		if(obj->owornmask & W_ARMOR)
203.  			Strcat(bp, " (being worn)");
204.  		if(obj->known) {
205.  			Strcat(prefix,
206.  			    sitoa(obj->spe - 10 + objects[obj->otyp].a_ac));
207.  			Strcat(prefix, " ");
208.  		}
209.  		break;
210.  	case WEAPON_SYM:
211.  		if(obj->known) {
212.  			Strcat(prefix, sitoa(obj->spe));
213.  			Strcat(prefix, " ");
214.  		}
215.  		break;
216.  	case WAND_SYM:
217.  		if(obj->known)
218.  			Sprintf(eos(bp), " (%d)", obj->spe);
219.  		break;
220.  	case RING_SYM:
221.  		if(obj->owornmask & W_RINGR) Strcat(bp, " (on right hand)");
222.  		if(obj->owornmask & W_RINGL) Strcat(bp, " (on left hand)");
223.  		if(obj->known && (objects[obj->otyp].bits & SPEC)) {
224.  			Strcat(prefix, sitoa(obj->spe));
225.  			Strcat(prefix, " ");
226.  		}
227.   break;
228.  	}
229.  	if(obj->owornmask & W_WEP)
230.  		Strcat(bp, " (weapon in hand)");
231.  	if(obj->unpaid)
232.  		Strcat(bp, " (unpaid)");
233.  	if(!strcmp(prefix, "a ") && index(vowels, *bp))
234.  		Strcpy(prefix, "an ");
235.  	bp = strprepend(bp, prefix);
236.  	return(bp);
237.  }
238.  
239.  /* used only in hack.fight.c (thitu) */
240.  setan(str,buf)
241.  register char *str,*buf;
242.  {
243.  	if(index(vowels,*str))
244.  		Sprintf(buf, "an %s", str);
245.  	else
246.  		Sprintf(buf, "a %s", str);
247.  }
248.  
249.  char *
250.  aobjnam(otmp,verb) register struct obj *otmp; register char *verb; {
251.  register char *bp = xname(otmp);
252.  char prefix[PREFIX];
253.  	if(otmp->quan != 1) {
254.  		Sprintf(prefix, "%d ", otmp->quan);
255.  		bp = strprepend(bp, prefix);
256.  	}
257.  
258.  	if(verb) {
259.  		/* verb is given in plural (i.e., without trailing s) */
260.  		Strcat(bp, " ");
261.  		if(otmp->quan != 1)
262.  			Strcat(bp, verb);
263.  		else if(!strcmp(verb, "are"))
264.  			Strcat(bp, "is");
265.  		else {
266.  			Strcat(bp, verb);
267.  			Strcat(bp, "s");
268.  		}
269.  	}
270.   return(bp);
271.  }
272.  
273.  char *wrp[] = { "wand", "ring", "potion", "scroll", "gem" };
274.  char wrpsym[] = { WAND_SYM, RING_SYM, POTION_SYM, SCROLL_SYM, GEM_SYM };
275.  
276.  struct obj *
277.  readobjnam(bp) register char *bp; {
278.  register char *p;
279.  register int i;
280.  int cnt, spe, spesgn, typ, heavy;
281.  char let;
282.  char *un, *dn, *an;
283.  /* int the = 0; char *oname = 0; */
284.  	cnt = spe = spesgn = typ = heavy = 0;
285.  	let = 0;
286.  	an = dn = un = 0;
287.  	for(p = bp; *p; p++)
288.  		if('A' <= *p && *p <= 'Z') *p += 'a'-'A';
289.  	if(!strncmp(bp, "the ", 4)){
290.  /*		the = 1; */
291.  		bp += 4;
292.  	} else if(!strncmp(bp, "an ", 3)){
293.  		cnt = 1;
294.  		bp += 3;
295.  	} else if(!strncmp(bp, "a ", 2)){
296.  		cnt = 1;
297.  		bp += 2;
298.  	}
299.  	if(!cnt && digit(*bp)){
300.  		cnt = atoi(bp);
301.  		while(digit(*bp)) bp++;
302.  		while(*bp == ' ') bp++;
303.  	}
304.  	if(!cnt) cnt = 1;		/* %% what with "gems" etc. ? */
305.  
306.  	if(*bp == '+' || *bp == '-'){
307.  		spesgn = (*bp++ == '+') ? 1 : -1;
308.  		spe = atoi(bp);
309.  		while(digit(*bp)) bp++;
310.  		while(*bp == ' ') bp++;
311.  	} else {
312.  		p = rindex(bp, '(');
313.  		if(p) {
314.  			if(p > bp && p[-1] == ' ') p[-1] = 0;
315.  			else *p = 0;
316.  			p++;
317.  			spe = atoi(p);
318.  			while(digit(*p)) p++;
319.  			if(strcmp(p, ")")) spe = 0;
320.  			else spesgn = 1;
321.  		}
322.  	}
323.  	/* now we have the actual name, as delivered by xname, say
324.  		green potions called whisky
325.  		scrolls labeled "QWERTY"
326.  		egg
327.  		dead zruties
328.  		fortune cookies
329.  		very heavy iron ball named hoei
330.  		wand of wishing
331.  		elven cloak
332.  	*/
333.  	for(p = bp; *p; p++) if(!strncmp(p, " named ", 7)) {
334.  		*p = 0;
335.  /*		oname = p+7; */
336.  	}
337.  	for(p = bp; *p; p++) if(!strncmp(p, " called ", 8)) {
338.  		*p = 0;
339.  		un = p+8;
340.  	}
341.  	for(p = bp; *p; p++) if(!strncmp(p, " labeled ", 9)) {
342.  		*p = 0;
343.  		dn = p+9;
344.  	}
345.  
346.  	/* first change to singular if necessary */
347.  	if(cnt != 1) {
348.  		/* find "cloves of garlic", "worthless pieces of blue glass" */
349.  		for(p = bp; *p; p++) if(!strncmp(p, "s of ", 5)){
350.  			while(*p = p[1]) p++;
351.  			goto sing;
352.  		}
353.  		/* remove -s or -es (boxes) or -ies (rubies, zruties) */
354.  		p = eos(bp);
355.  		if(p[-1] == 's') {
356.  			if(p[-2] == 'e') {
357.  				if(p[-3] == 'i') {
358.  					if(!strcmp(p-7, "cookies"))
359.  						goto mins;
360.  					Strcpy(p-3, "y");
361.  					goto sing;
362.  				}
363.  
364.  				/* note: cloves / knives from clove / knife */
365.  				if(!strcmp(p-6, "knives")) {
366.  					Strcpy(p-3, "fe");
367.  					goto sing;
368.  				}
369.  
370.  				/* note: nurses, axes but boxes */
371.  				if(!strcmp(p-5, "boxes")) {
372.  					p[-2] = 0;
373.  					goto sing;
374.  				}
375.  			}
376.  		mins:
377.  			p[-1] = 0;
378.  		} else {
379.  			if(!strcmp(p-9, "homunculi")) {
380.  				Strcpy(p-1, "us"); /* !! makes string longer */
381.  				goto sing;
382.  			}
383.  			if(!strcmp(p-5, "teeth")) {
384.  				Strcpy(p-5, "tooth");
385.  				goto sing;
386.  			}
387.   /* here we cannot find the plural suffix */
388.  		}
389.  	}
390.  sing:
391.  	if(!strcmp(bp, "amulet of yendor")) {
392.  		typ = AMULET_OF_YENDOR;
393.  		goto typfnd;
394.  	}
395.  	p = eos(bp);
396.  	if(!strcmp(p-5, " mail")){	/* Note: ring mail is not a ring ! */
397.  		let = ARMOR_SYM;
398.  		an = bp;
399.  		goto srch;
400.  	}
401.  	for(i = 0; i < sizeof(wrpsym); i++) {
402.  		register int j = strlen(wrp[i]);
403.  		if(!strncmp(bp, wrp[i], j)){
404.  			let = wrpsym[i];
405.  			bp += j;
406.  			if(!strncmp(bp, " of ", 4)) an = bp+4;
407.  			/* else if(*bp) ?? */
408.  			goto srch;
409.  		}
410.  		if(!strcmp(p-j, wrp[i])){
411.  			let = wrpsym[i];
412.  			p -= j;
413.  			*p = 0;
414.  			if(p[-1] == ' ') p[-1] = 0;
415.  			dn = bp;
416.  			goto srch;
417.  		}
418.  	}
419.  	if(!strcmp(p-6, " stone")){
420.  		p[-6] = 0;
421.  		let = GEM_SYM;
422.  		an = bp;
423.  		goto srch;
424.  	}
425.  	if(!strcmp(bp, "very heavy iron ball")){
426.  		heavy = 1;
427.  		typ = HEAVY_IRON_BALL;
428.  		goto typfnd;
429.  	}
430.  	an = bp;
431.  srch:
432.  	if(!an && !dn && !un)
433.  		goto any;
434.  	i = 1;
435.  	if(let) i = bases[letindex(let)];
436.  	while(i <= NROFOBJECTS && (!let || objects[i].oc_olet == let)){
437.  		if(an && strcmp(an, objects[i].oc_name))
438.  			goto nxti;
439.  		if(dn && strcmp(dn, objects[i].oc_descr))
440.  			goto nxti;
441.  		if(un && strcmp(un, objects[i].oc_uname))
442.  			goto nxti;
443.  		typ = i;
444.  		goto typfnd;
445.  	nxti:
446.  		i++;
447.  	}
448.  any:
449.  	if(!let) let = wrpsym[rn2(sizeof(wrpsym))];
450.  	typ = probtype(let);
451.  typfnd:
452.  	{ register struct obj *otmp;
453.  	  extern struct obj *mksobj();
454.  	let = objects[typ].oc_olet;
455.  	if(let == FOOD_SYM && typ >= CORPSE)
456.  	    let = typ-CORPSE+'@'+((typ > CORPSE + 'Z' - '@') ? 'a'-'Z'-1 : 0);
457.  	otmp = mksobj(let, typ);
458.  	if(heavy)
459.  		otmp->owt += 15;
460.  	if(cnt > 0 && index("%?!*)", let) &&
461.  		(cnt < 4 || (let == WEAPON_SYM && typ <= ROCK && cnt < 20)))
462.  		otmp->quan = cnt;
463.  	if(spesgn == -1)
464.  		otmp->cursed = 1;
465.  	if(spe > 3 && spe > otmp->spe)
466.  		spe = 0;
467.  	else if(let == WAND_SYM)
468.  		spe = otmp->spe;
469.  	if(spe == 3 && u.uluck < 0)
470.  		spesgn = -1;
471.  	if(let != WAND_SYM && spesgn == -1)
472.  		spe = -spe;
473.  	if(let == BALL_SYM)
474.  		spe = 0;
475.  	else if(let == AMULET_SYM)
476.  		spe = -1;
477.  	else if(typ == WAN_WISHING && rn2(10))
478.  		spe = 0;
479.  	else if(let == ARMOR_SYM) {
480.  		spe += 10 - objects[typ].a_ac;
481.  		if(spe > 5 && rn2(spe - 5))
482.  			otmp->cursed = 1;
483.  	}
484.  	otmp->spe = spe;
485.  	return(otmp);
486.  	}
487.  }