Source:NetHack 1.4f/invent.c

From NetHackWiki
Revision as of 01:05, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 1.4f/invent.c moved to Source:NetHack 1.4f/invent.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

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

The Harlow distribution of NetHack 1.4f does not contain this file; it contains one identical to its counterpart in NetHack 1.3d. This is probably due to difficulties in applying the original patch as distributed on Usenet. See How to apply the NetHack 1.4f patch for instructions to obtain this file.

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

1.    /*	SCCS Id: @(#)invent.c	1.4	87/08/08
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* invent.c - version 1.0.3 */
4.    
5.    #include	<stdio.h>
6.    #include	"hack.h"
7.    extern struct obj *splitobj();
8.    extern struct obj zeroobj;
9.    extern char morc;
10.   extern char quitchars[];
11.   static char *xprname();
12.   
13.   #ifndef NOWORM
14.   #include	"wseg.h"
15.   extern struct wseg *wsegs[32];
16.   #endif
17.   
18.   #define	NOINVSYM	'#'
19.   
20.   int lastinvnr = 51;	/* 0 ... 51 */
21.   
22.   static
23.   assigninvlet(otmp)
24.   register struct obj *otmp;
25.   {
26.   	boolean inuse[52];
27.   	register int i;
28.   	register struct obj *obj;
29.   
30.   	for(i = 0; i < 52; i++) inuse[i] = FALSE;
31.   	for(obj = invent; obj; obj = obj->nobj) if(obj != otmp) {
32.   		i = obj->invlet;
33.   		if('a' <= i && i <= 'z') inuse[i - 'a'] = TRUE; else
34.   		if('A' <= i && i <= 'Z') inuse[i - 'A' + 26] = TRUE;
35.   		if(i == otmp->invlet) otmp->invlet = 0;
36.   	}
37.   	if((i = otmp->invlet) &&
38.   	    (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')))
39.   		return;
40.   	for(i = lastinvnr+1; i != lastinvnr; i++) {
41.   		if(i == 52) { i = -1; continue; }
42.   		if(!inuse[i]) break;
43.   	}
44.   	otmp->invlet = (inuse[i] ? NOINVSYM :
45.   			(i < 26) ? ('a'+i) : ('A'+i-26));
46.   	lastinvnr = i;
47.   }
48.   
49.   struct obj *
50.   addinv(obj)
51.   register struct obj *obj;
52.   {
53.   	register struct obj *otmp;
54.   
55.   	/* merge or attach to end of chain */
56.   	if(!invent) {
57.   		invent = obj;
58.   		otmp = 0;
59.   	} else
60.   	for(otmp = invent; /* otmp */; otmp = otmp->nobj) {
61.   		if(merged(otmp, obj, 0))
62.   			return(otmp);
63.   		if(!otmp->nobj) {
64.   			otmp->nobj = obj;
65.   			break;
66.   		}
67.   	}
68.   	obj->nobj = 0;
69.   
70.   	if(flags.invlet_constant) {
71.   		assigninvlet(obj);
72.   		/*
73.   		 * The ordering of the chain is nowhere significant
74.   		 * so in case you prefer some other order than the
75.   		 * historical one, change the code below.
76.   		 */
77.   		if(otmp) {	/* find proper place in chain */
78.   			otmp->nobj = 0;
79.   			if((invent->invlet ^ 040) > (obj->invlet ^ 040)) {
80.   				obj->nobj = invent;
81.   				invent = obj;
82.   			} else
83.   			for(otmp = invent; ; otmp = otmp->nobj) {
84.   			    if(!otmp->nobj ||
85.   				(otmp->nobj->invlet ^ 040) > (obj->invlet ^ 040)){
86.   				obj->nobj = otmp->nobj;
87.   				otmp->nobj = obj;
88.   				break;
89.   			    }
90.   			}
91.   		}
92.   	}
93.   
94.   	return(obj);
95.   }
96.   
97.   useup(obj)
98.   register struct obj *obj;
99.   {
100.  	if(obj->quan > 1){
101.  		obj->quan--;
102.  		obj->owt = weight(obj);
103.  	} else {
104.  		setnotworn(obj);
105.  		freeinv(obj);
106.  		obfree(obj, (struct obj *) 0);
107.  	}
108.  }
109.  
110.  freeinv(obj)
111.  register struct obj *obj;
112.  {
113.  	register struct obj *otmp;
114.  
115.  	if(obj == invent)
116.  		invent = invent->nobj;
117.  	else {
118.  		for(otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
119.  			if(!otmp->nobj) panic("freeinv");
120.  		otmp->nobj = obj->nobj;
121.  	}
122.  }
123.  
124.  /* destroy object in fobj chain (if unpaid, it remains on the bill) */
125.  delobj(obj) register struct obj *obj; {
126.  	freeobj(obj);
127.  	unpobj(obj);
128.  	obfree(obj, (struct obj *) 0);
129.  }
130.  
131.  /* unlink obj from chain starting with fobj */
132.  freeobj(obj) register struct obj *obj; {
133.  	register struct obj *otmp;
134.  
135.  	if(obj == fobj) fobj = fobj->nobj;
136.  	else {
137.  		for(otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
138.  			if(!otmp) panic("error in freeobj");
139.  		otmp->nobj = obj->nobj;
140.  	}
141.  }
142.  
143.  /* Note: freegold throws away its argument! */
144.  freegold(gold) register struct gold *gold; {
145.  	register struct gold *gtmp;
146.  
147.  	if(gold == fgold) fgold = gold->ngold;
148.  	else {
149.  		for(gtmp = fgold; gtmp->ngold != gold; gtmp = gtmp->ngold)
150.  			if(!gtmp) panic("error in freegold");
151.  		gtmp->ngold = gold->ngold;
152.  	}
153.  	free((char *) gold);
154.  }
155.  
156.  deltrap(trap)
157.  register struct trap *trap;
158.  {
159.  	register struct trap *ttmp;
160.  
161.  	if(trap == ftrap)
162.  		ftrap = ftrap->ntrap;
163.  	else {
164.  		for(ttmp = ftrap; ttmp->ntrap != trap; ttmp = ttmp->ntrap) ;
165.  		ttmp->ntrap = trap->ntrap;
166.  	}
167.  	free((char *) trap);
168.  }
169.  
170.  struct wseg *m_atseg;
171.  
172.  struct monst *
173.  m_at(x,y)
174.  register x,y;
175.  {
176.  	register struct monst *mtmp;
177.  #ifndef NOWORM
178.  	register struct wseg *wtmp;
179.  #endif
180.  
181.  	m_atseg = 0;
182.  	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon){
183.  		if(mtmp->mx == x && mtmp->my == y)
184.  			return(mtmp);
185.  #ifndef NOWORM
186.  		if(mtmp->wormno){
187.  		    for(wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
188.  		    if(wtmp->wx == x && wtmp->wy == y){
189.  			m_atseg = wtmp;
190.  			return(mtmp);
191.  		    }
192.  		}
193.  #endif
194.  	}
195.  	return(0);
196.  }
197.  
198.  struct obj *
199.  o_at(x,y)
200.  register x,y;
201.  {
202.  	register struct obj *otmp;
203.  
204.  	for(otmp = fobj; otmp; otmp = otmp->nobj)
205.  		if(otmp->ox == x && otmp->oy == y) return(otmp);
206.  	return(0);
207.  }
208.  
209.  struct obj *
210.  sobj_at(n,x,y)
211.  register n,x,y;
212.  {
213.  	register struct obj *otmp;
214.  
215.  	for(otmp = fobj; otmp; otmp = otmp->nobj)
216.  		if(otmp->ox == x && otmp->oy == y && otmp->otyp == n)
217.  			return(otmp);
218.  	return(0);
219.  }
220.  
221.  carried(obj) register struct obj *obj; {
222.  register struct obj *otmp;
223.  	for(otmp = invent; otmp; otmp = otmp->nobj)
224.  		if(otmp == obj) return(1);
225.  	return(0);
226.  }
227.  
228.  struct obj *
229.  carrying(type)
230.  register int type;
231.  {
232.  	register struct obj *otmp;
233.  
234.  	for(otmp = invent; otmp; otmp = otmp->nobj)
235.  		if(otmp->otyp == type)
236.  			return(otmp);
237.  	return((struct obj *) 0);
238.  }
239.  
240.  struct obj *
241.  o_on(id, objchn) unsigned int id; register struct obj *objchn; {
242.  	while(objchn) {
243.  		if(objchn->o_id == id) return(objchn);
244.  		objchn = objchn->nobj;
245.  	}
246.  	return((struct obj *) 0);
247.  }
248.  
249.  struct trap *
250.  t_at(x,y)
251.  register x,y;
252.  {
253.  	register struct trap *trap = ftrap;
254.  	while(trap) {
255.  		if(trap->tx == x && trap->ty == y) return(trap);
256.  		trap = trap->ntrap;
257.  	}
258.  	return(0);
259.  }
260.  
261.  struct gold *
262.  g_at(x,y)
263.  register x,y;
264.  {
265.  	register struct gold *gold = fgold;
266.  	while(gold) {
267.  		if(gold->gx == x && gold->gy == y) return(gold);
268.  		gold = gold->ngold;
269.  	}
270.  	return(0);
271.  }
272.  
273.  /* make dummy object structure containing gold - for temporary use only */
274.  struct obj *
275.  mkgoldobj(q)
276.  register long q;
277.  {
278.  	register struct obj *otmp;
279.  
280.  	otmp = newobj(0);
281.  	/* should set o_id etc. but otmp will be freed soon */
282.  	otmp->olet = GOLD_SYM;
283.  	u.ugold -= q;
284.  	OGOLD(otmp) = q;
285.  	flags.botl = 1;
286.  	return(otmp);
287.  }
288.  
289.  /*
290.   * getobj returns:
291.   *	struct obj *xxx:	object to do something with.
292.   *	(struct obj *) 0	error return: no object.
293.   *	&zeroobj		explicitly no object (as in w-).
294.   */
295.  struct obj *
296.  getobj(let,word)
297.  register char *let,*word;
298.  {
299.  	register struct obj *otmp;
300.  	register char ilet,ilet1,ilet2;
301.  	char buf[BUFSZ];
302.  	char lets[BUFSZ];
303.  	register int foo = 0, foo2;
304.  	register char *bp = buf;
305.  	xchar allowcnt = 0;	/* 0, 1 or 2 */
306.  	boolean allowgold = FALSE;
307.  	boolean allowall = FALSE;
308.  	boolean allownone = FALSE;
309.  	xchar foox = 0;
310.  	long cnt;
311.  
312.  	if(*let == '0') let++, allowcnt = 1;
313.  	if(*let == GOLD_SYM) let++, allowgold = TRUE;
314.  	if(*let == '#') let++, allowall = TRUE;
315.  	if(*let == '-') let++, allownone = TRUE;
316.  	if(allownone) *bp++ = '-';
317.  	if(allowgold) *bp++ = GOLD_SYM;
318.  	if(bp > buf && bp[-1] == '-') *bp++ = ' ';
319.  
320.  	ilet = 'a';
321.  	for(otmp = invent; otmp; otmp = otmp->nobj){
322.  	    if(!*let || index(let, otmp->olet)) {
323.  		bp[foo++] = flags.invlet_constant ? otmp->invlet : ilet;
324.  
325.  		/* ugly check: remove inappropriate things */
326.  		if((!strcmp(word, "take off") &&
327.  		    !(otmp->owornmask & (W_ARMOR - W_ARM2)))
328.  		|| (!strcmp(word, "wear") &&
329.  		    (otmp->owornmask & (W_ARMOR | W_RING)))
330.  		|| (!strcmp(word, "wield") &&
331.  		    (otmp->owornmask & W_WEP))
332.  #ifdef MARKER
333.  		|| (!strcmp(word, "write with") &&
334.  		    (otmp->olet == TOOL_SYM && otmp->otyp != MAGIC_MARKER))
335.  #endif
336.  		    ) {
337.  			foo--;
338.  			foox++;
339.  		}
340.  	    }
341.  	    if(ilet == 'z') ilet = 'A'; else ilet++;
342.  	}
343.  	bp[foo] = 0;
344.  	if(foo == 0 && bp > buf && bp[-1] == ' ') *--bp = 0;
345.  	(void) strcpy(lets, bp);	/* necessary since we destroy buf */
346.  	if(foo > 5) {			/* compactify string */
347.  		foo = foo2 = 1;
348.  		ilet2 = bp[0];
349.  		ilet1 = bp[1];
350.  		while(ilet = bp[++foo2] = bp[++foo]){
351.  			if(ilet == ilet1+1){
352.  				if(ilet1 == ilet2+1)
353.  					bp[foo2 - 1] = ilet1 = '-';
354.  				else if(ilet2 == '-') {
355.  					bp[--foo2] = ++ilet1;
356.  					continue;
357.  				}
358.  			}
359.  			ilet2 = ilet1;
360.  			ilet1 = ilet;
361.  		}
362.  	}
363.  	if(!foo && !allowall && !allowgold && !allownone) {
364.  		pline("You don't have anything %sto %s.",
365.  			foox ? "else " : "", word);
366.  		return(0);
367.  	}
368.  	for(;;) {
369.  		if(!buf[0]) {
370.  #ifdef REDO
371.  		    if(!in_doagain)
372.  #endif
373.  			pline("What do you want to %s [*]? ", word);
374.  		} else {
375.  #ifdef REDO
376.  		    if(!in_doagain)
377.  #endif
378.  			pline("What do you want to %s [%s or ?*]? ",
379.  				word, buf);
380.  		}
381.  		cnt = 0;
382.  		ilet = readchar();
383.  		while(digit(ilet) && allowcnt) {
384.  #ifdef REDO
385.  			if (ilet != '?' && ilet != '*')	savech(ilet);
386.  #endif
387.  			cnt = 10*cnt + (ilet - '0');
388.  			allowcnt = 2;	/* signal presence of cnt */
389.  			ilet = readchar();
390.  		}
391.  		if(digit(ilet)) {
392.  			pline("No count allowed with this command.");
393.  			continue;
394.  		}
395.  		if(index(quitchars,ilet)) {
396.  			pline("Never mind.");
397.  			return((struct obj *)0);
398.  		}
399.  		if(ilet == '-') {
400.  			return(allownone ? &zeroobj : (struct obj *) 0);
401.  		}
402.  		if(ilet == GOLD_SYM) {
403.  			if(!allowgold){
404.  				pline("You cannot %s gold.", word);
405.  				continue;
406.  			}
407.  			if(!(allowcnt == 2 && cnt < u.ugold))
408.  				cnt = u.ugold;
409.  			return(mkgoldobj(cnt));
410.  		}
411.  		if(ilet == '?') {
412.  			doinv(lets);
413.  			if(!(ilet = morc)) continue;
414.  			/* he typed a letter (not a space) to more() */
415.  		} else if(ilet == '*') {
416.  			doinv((char *) 0);
417.  			if(!(ilet = morc)) continue;
418.  			/* ... */
419.  		}
420.  #ifdef REDO
421.  		if (ilet != '?' && ilet != '*')	savech(ilet);
422.  #endif
423.  		if(flags.invlet_constant) {
424.  			for(otmp = invent; otmp; otmp = otmp->nobj)
425.  				if(otmp->invlet == ilet) break;
426.  		} else {
427.  			if(ilet >= 'A' && ilet <= 'Z') ilet += 'z'-'A'+1;
428.  			ilet -= 'a';
429.  			for(otmp = invent; otmp && ilet;
430.  					ilet--, otmp = otmp->nobj) ;
431.  		}
432.  		if(!otmp) {
433.  			pline("You don't have that object.");
434.  			continue;
435.  		}
436.  		if(cnt < 0 || otmp->quan < cnt) {
437.  			pline("You don't have that many! [You have %u]"
438.  			, otmp->quan);
439.  			continue;
440.  		}
441.  		break;
442.  	}
443.  	if(!allowall && let && !index(let,otmp->olet)) {
444.  		pline("That is a silly thing to %s.",word);
445.  		return(0);
446.  	}
447.  	if(allowcnt == 2) {	/* cnt given */
448.  		if(cnt == 0) return(0);
449.  		if(cnt != otmp->quan) {
450.  			register struct obj *obj;
451.  			obj = splitobj(otmp, (int) cnt);
452.  			if(otmp == uwep) setuwep(obj);
453.  		}
454.  	}
455.  	return(otmp);
456.  }
457.  
458.  ckunpaid(otmp) register struct obj *otmp; {
459.  	return( otmp->unpaid );
460.  }
461.  
462.  /* interactive version of getobj - used for Drop and Identify */
463.  /* return the number of times fn was called successfully */
464.  ggetobj(word, fn, max)
465.  char *word;
466.  int (*fn)(),  max;
467.  {
468.  char buf[BUFSZ];
469.  register char *ip;
470.  register char sym;
471.  register int oletct = 0, iletct = 0;
472.  register boolean allflag = FALSE;
473.  char olets[20], ilets[20];
474.  int (*ckfn)() = (int (*)()) 0;
475.  xchar allowgold = (u.ugold && !strcmp(word, "drop")) ? 1 : 0;	/* BAH */
476.  	if(!invent && !allowgold){
477.  		pline("You have nothing to %s.", word);
478.  		return(0);
479.  	} else {
480.  		register struct obj *otmp = invent;
481.  		register int uflg = 0;
482.  
483.  		if(allowgold) ilets[iletct++] = GOLD_SYM;
484.  		ilets[iletct] = 0;
485.  		while(otmp) {
486.  			if(!index(ilets, otmp->olet)){
487.  				ilets[iletct++] = otmp->olet;
488.  				ilets[iletct] = 0;
489.  			}
490.  			if(otmp->unpaid) uflg = 1;
491.  			otmp = otmp->nobj;
492.  		}
493.  		ilets[iletct++] = ' ';
494.  		if(uflg) ilets[iletct++] = 'u';
495.  		if(invent) ilets[iletct++] = 'a';
496.  		ilets[iletct] = 0;
497.  	}
498.  	pline("What kinds of thing do you want to %s? [%s] ",
499.  		word, ilets);
500.  	getlin(buf);
501.  	if(buf[0] == '\033') {
502.  		clrlin();
503.  		return(0);
504.  	}
505.  	ip = buf;
506.  	olets[0] = 0;
507.  	while(sym = *ip++){
508.  		if(sym == ' ') continue;
509.  		if(sym == GOLD_SYM) {
510.  			if(allowgold == 1)
511.  				(*fn)(mkgoldobj(u.ugold));
512.  			else if(!u.ugold)
513.  				pline("You have no gold.");
514.  			allowgold = 2;
515.  		} else
516.  		if(sym == 'a' || sym == 'A') allflag = TRUE; else
517.  		if(sym == 'u' || sym == 'U') ckfn = ckunpaid; else
518.  #ifdef SPELLS
519.  		if(index("!%?[()=*/+\"0", sym)){
520.  #else
521.  		if(index("!%?[()=*/\"0", sym)){
522.  #endif
523.  			if(!index(olets, sym)){
524.  				olets[oletct++] = sym;
525.  				olets[oletct] = 0;
526.  			}
527.  		}
528.  		else pline("You don't have any %c's.", sym);
529.  	}
530.  	if(allowgold == 2 && !oletct)
531.  		return(1);	/* he dropped gold (or at least tried to) */
532.  	else
533.  		return(askchain(invent, olets, allflag, fn, ckfn, max));
534.  }
535.  
536.  /*
537.   * Walk through the chain starting at objchn and ask for all objects
538.   * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
539.   * whether the action in question (i.e., fn) has to be performed.
540.   * If allflag then no questions are asked. Max gives the max nr of
541.   * objects to be treated. Return the number of objects treated.
542.   */
543.  askchain(objchn, olets, allflag, fn, ckfn, max)
544.  struct obj *objchn;
545.  register char *olets;
546.  int allflag;
547.  int (*fn)(), (*ckfn)();
548.  int max;
549.  {
550.  register struct obj *otmp, *otmp2;
551.  register char sym, ilet;
552.  register int cnt = 0;
553.  #ifdef SORTING
554.  	/* changes so the askchain is interrogated in the order specified.
555.  	 * For example, if a person specifies =/ then first all rings will be
556.  	 * asked about followed by all wands -dgk
557.  	 */
558.  nextclass:
559.  #endif
560.  	ilet = 'a'-1;
561.  	for(otmp = objchn; otmp; otmp = otmp2){
562.  		if(ilet == 'z') ilet = 'A'; else ilet++;
563.  		otmp2 = otmp->nobj;
564.  #ifdef SORTING
565.  		if (olets && *olets && otmp->olet != *olets) continue;
566.  #else
567.  		if(olets && *olets && !index(olets, otmp->olet)) continue;
568.  #endif
569.  		if(ckfn && !(*ckfn)(otmp)) continue;
570.  		if(!allflag) {
571.  			pline(xprname(otmp, ilet));
572.  			addtopl(" [nyaq]? ");
573.  			sym = readchar();
574.  		}
575.  		else	sym = 'y';
576.  
577.  		switch(sym){
578.  		case 'a':
579.  			allflag = 1;
580.  		case 'y':
581.  			cnt += (*fn)(otmp);
582.  			if(--max == 0) goto ret;
583.  		case 'n':
584.  		default:
585.  			break;
586.  		case 'q':
587.  			goto ret;
588.  		}
589.  	}
590.  #ifdef SORTING
591.  	if (olets && *olets && *++olets)
592.  		goto nextclass;
593.  #endif
594.  	pline(cnt ? "That was all." : "No applicable objects.");
595.  ret:
596.  	return(cnt);
597.  }
598.  
599.  obj_to_let(obj)	/* should of course only be called for things in invent */
600.  register struct obj *obj;
601.  {
602.  	register struct obj *otmp;
603.  	register char ilet;
604.  
605.  	if(flags.invlet_constant)
606.  		return(obj->invlet);
607.  	ilet = 'a';
608.  	for(otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
609.  		if(++ilet > 'z') ilet = 'A';
610.  	return(otmp ? ilet : NOINVSYM);
611.  }
612.  
613.  prinv(obj)
614.  register struct obj *obj;
615.  {
616.  	pline(xprname(obj, obj_to_let(obj)));
617.  }
618.  
619.  static char *
620.  xprname(obj,let)
621.  register struct obj *obj;
622.  register char let;
623.  {
624.  	static char li[BUFSZ];
625.  
626.  	(void) sprintf(li, "%c - %s.",
627.  		flags.invlet_constant ? obj->invlet : let,
628.  		doname(obj));
629.  	return(li);
630.  }
631.  
632.  ddoinv()
633.  {
634.  	doinv((char *) 0);
635.  	return(0);
636.  }
637.  
638.  #ifdef SORTING
639.  # ifdef SPELLS
640.  char inv_order[] = "\")[%?+/=!(*0_`";	/* to be safe, include _ and ` */
641.  # else
642.  char inv_order[] = "\")[%?/=!(*0_`";
643.  # endif
644.  extern char *let_to_name();
645.  #endif
646.  
647.  /* called with 0 or "": all objects in inventory */
648.  /* otherwise: all objects with (serial) letter in lets */
649.  doinv(lets)
650.  register char *lets;
651.  {
652.  	register struct obj *otmp;
653.  	register char ilet;
654.  	int ct = 0;
655.  	char any[BUFSZ];
656.  #ifdef SORTING
657.  	char *invlet = inv_order;
658.  	int classcount = 0;
659.  #endif /* SORTING /**/
660.  
661.  	morc = 0;		/* just to be sure */
662.  
663.  	if(!invent){
664.  		pline("Not carrying anything.");
665.  		return;
666.  	}
667.  
668.  	cornline(0, (char *) 0);
669.  #ifdef SORTING
670.  nextclass:
671.  	classcount = 0;
672.  	ilet = 'a';
673.  	for(otmp = invent; otmp; otmp = otmp->nobj) {
674.  		if(flags.invlet_constant) ilet = otmp->invlet;
675.  		if(!lets || !*lets || index(lets, ilet)) {
676.  			if (!flags.sortpack || otmp->olet == *invlet) {
677.  				if (flags.sortpack && !classcount) {
678.  					cornline(1, let_to_name(*invlet));
679.  					classcount++;
680.  				}
681.  				cornline(1, xprname(otmp, ilet));
682.  				any[ct++] = ilet;
683.  			}
684.  		}
685.  		if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
686.  	}
687.  	if (flags.sortpack && *++invlet) goto nextclass;
688.  #else
689.  	ilet = 'a';
690.  	for(otmp = invent; otmp; otmp = otmp->nobj) {
691.  	    if(flags.invlet_constant) ilet = otmp->invlet;
692.  	    if(!lets || !*lets || index(lets, ilet)) {
693.  		    cornline(1, xprname(otmp, ilet));
694.  		    any[ct++] = ilet;
695.  	    }
696.  	    if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
697.  	}
698.  #endif /* SORTING /**/
699.  	any[ct] = 0;
700.  	cornline(2, any);
701.  }
702.  
703.  dotypeinv ()				/* free after Robert Viduya */
704.  /* Changed to one type only, so he doesnt have to type cr */
705.  {
706.      char c, ilet;
707.      char stuff[BUFSZ];
708.      register int stct;
709.      register struct obj *otmp;
710.      boolean billx = inshop() && doinvbill(0);
711.      boolean unpd = FALSE;
712.  
713.  	if (!invent && !u.ugold && !billx) {
714.  	    pline ("You aren't carrying anything.");
715.  	    return(0);
716.  	}
717.  
718.  	stct = 0;
719.  	if(u.ugold) stuff[stct++] = GOLD_SYM;
720.  	stuff[stct] = 0;
721.  	for(otmp = invent; otmp; otmp = otmp->nobj) {
722.  	    if (!index (stuff, otmp->olet)) {
723.  		stuff[stct++] = otmp->olet;
724.  		stuff[stct] = 0;
725.  	    }
726.  	    if(otmp->unpaid)
727.  		unpd = TRUE;
728.  	}
729.  	if(unpd) stuff[stct++] = 'u';
730.  	if(billx) stuff[stct++] = 'x';
731.  	stuff[stct] = 0;
732.  
733.  	if(stct > 1) {
734.  #ifdef REDO
735.  	  if (!in_doagain)
736.  #endif
737.  	    pline ("What type of object [%s] do you want an inventory of? ",
738.  		stuff);
739.  	    c = readchar();
740.  #ifdef REDO
741.  	    savech(c);
742.  #endif
743.  	    if(index(quitchars,c)) return(0);
744.  	} else
745.  	    c = stuff[0];
746.  
747.  	if(c == GOLD_SYM)
748.  	    return(doprgold());
749.  
750.  	if(c == 'x' || c == 'X') {
751.  	    if(billx)
752.  		(void) doinvbill(1);
753.  	    else
754.  		pline("No used-up objects on the shopping bill.");
755.  	    return(0);
756.  	}
757.  
758.  	if((c == 'u' || c == 'U') && !unpd) {
759.  		pline("You are not carrying any unpaid objects.");
760.  		return(0);
761.  	}
762.  
763.  	stct = 0;
764.  	ilet = 'a';
765.  	for (otmp = invent; otmp; otmp = otmp -> nobj) {
766.  	    if(flags.invlet_constant) ilet = otmp->invlet;
767.  	    if (c == otmp -> olet || (c == 'u' && otmp -> unpaid))
768.  		stuff[stct++] = ilet;
769.  	    if(!flags.invlet_constant) if(++ilet > 'z') ilet = 'A';
770.  	}
771.  	stuff[stct] = '\0';
772.  	if(stct == 0)
773.  		pline("You have no such objects.");
774.  	else
775.  		doinv (stuff);
776.  
777.  	return(0);
778.  }
779.  
780.  /* look at what is here */
781.  dolook() {
782.      register struct obj *otmp, *otmp0;
783.      register struct gold *gold;
784.      char *verb = Blind ? "feel" : "see";
785.      int	ct = 0;
786.      int fd = 0;
787.  
788.  #ifdef KAA
789.      read_engr_at(u.ux, u.uy); /* Eric Backus */
790.  #endif
791.      if(!u.uswallow) {
792.  	otmp0 = o_at(u.ux, u.uy);
793.  	gold = g_at(u.ux, u.uy);
794.      }  else  {
795.  	pline("You %s no objects here.", verb);
796.  	return(!!Blind);
797.      }
798.  
799.      /* added by GAN 10/30/86 */
800.  #ifdef FOUNTAINS
801.      if(IS_FOUNTAIN(levl[u.ux][u.uy].typ))  {
802.  	fd++;
803.  	pline("There is a fountain here.");
804.      }
805.  #endif
806.  #ifdef NEWCLASS
807.      if(IS_THRONE(levl[u.ux][u.uy].typ))  {
808.  	fd++;
809.  	pline("There is an opulent throne here.");
810.      }    
811.  #endif
812.      if(u.ux == xupstair && u.uy == yupstair)  {
813.  	fd++;
814.  	pline("There is a stairway up here.");
815.      }
816.      if(u.ux == xdnstair && u.uy == ydnstair)  {
817.  	fd++;
818.  	cornline(1, "There is a stairway down here.");
819.      }
820.      if(Blind)  {
821.  	 pline("You try to feel what is lying here on the floor.");
822.  	 if(Levitation)  {
823.  		pline("But you can't reach it!");
824.  		return(0);
825.  	 }
826.      }
827.   
828.      if(!otmp0 && !gold) {
829.  	if(Blind || !fd)
830.  		pline("You %s no objects here.", verb);
831.  	return(!!Blind);
832.      }
833.  
834.      cornline(0, "Things that are here:");
835.      for(otmp = otmp0; otmp; otmp = otmp->nobj) {
836.  	if(otmp->ox == u.ux && otmp->oy == u.uy) {
837.  	    ct++;
838.  	    cornline(1, doname(otmp));
839.  	    if(Blind && otmp->otyp == DEAD_COCKATRICE && !uarmg) {
840.  		pline("Touching the dead cockatrice is a fatal mistake ...");
841.  		pline("You die ...");
842.  		killer = "dead cockatrice";
843.  		done("died");
844.  	    }
845.  	}
846.      }
847.  
848.      if(gold) {
849.  	char gbuf[30];
850.  
851.  	(void) sprintf(gbuf, "%ld gold piece%s",
852.  		gold->amount, plur(gold->amount));
853.  	if(!ct++)
854.  	    pline("You %s here %s.", verb, gbuf);
855.  	else
856.  	    cornline(1, gbuf);
857.      }
858.  
859.      if(ct == 1 && !gold) {
860.  	pline("You %s here %s.", verb, doname(otmp0));
861.  	cornline(3, (char *) 0);
862.      }
863.      if(ct > 1)
864.  	cornline(2, (char *) 0);
865.      return(!!Blind);
866.  }
867.  
868.  stackobj(obj) register struct obj *obj; {
869.  register struct obj *otmp = fobj;
870.  	for(otmp = fobj; otmp; otmp = otmp->nobj) if(otmp != obj)
871.  	if(otmp->ox == obj->ox && otmp->oy == obj->oy &&
872.  		merged(obj,otmp,1))
873.  			return;
874.  }
875.  
876.  /* merge obj with otmp and delete obj if types agree */
877.  merged(otmp,obj,lose) register struct obj *otmp, *obj; {
878.  	if(obj->otyp == otmp->otyp &&
879.  	  obj->unpaid == otmp->unpaid &&
880.  	  obj->spe == otmp->spe &&
881.  	  obj->dknown == otmp->dknown &&
882.  	  obj->cursed == otmp->cursed &&
883.  #ifdef SPELLS
884.  	  (index("%*?!+", obj->olet) ||
885.  #else
886.  	  (index("%*?!", obj->olet) ||
887.  #endif
888.  	    (obj->known == otmp->known &&
889.  		(obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)))) {
890.  		otmp->quan += obj->quan;
891.  		otmp->owt += obj->owt;
892.  		if(lose) freeobj(obj);
893.  		obfree(obj,otmp);	/* free(obj), bill->otmp */
894.  		return(1);
895.  	} else	return(0);
896.  }
897.  
898.  /*
899.   * Gold is no longer displayed; in fact, when you have a lot of money,
900.   * it may take a while before you have counted it all.
901.   * [Bug: d$ and pickup still tell you how much it was.]
902.   */
903.  extern int (*occupation)();
904.  extern char *occtxt;
905.  static long goldcounted;
906.  
907.  countgold(){
908.  	if((goldcounted += 100*(u.ulevel + 1)) >= u.ugold) {
909.  		long eps = 0;
910.  		if(!rn2(2)) eps = rnd((int) (u.ugold/100 + 1));
911.  		pline("You probably have about %ld gold pieces.",
912.  			u.ugold + eps);
913.  		return(0);	/* done */
914.  	}
915.  	return(1);		/* continue */
916.  }
917.  
918.  doprgold(){
919.  	if(!u.ugold)
920.  		pline("You do not carry any gold.");
921.  	else if(u.ugold <= 500)
922.  		pline("You are carrying %ld gold piece%s.", u.ugold, plur(u.ugold));
923.  	else {
924.  		pline("You sit down in order to count your gold pieces.");
925.  		goldcounted = 500;
926.  		occupation = countgold;
927.  		occtxt = "counting your gold";
928.  	}
929.  	return(1);
930.  }
931.  
932.  /* --- end of gold counting section --- */
933.  
934.  doprwep(){
935.  	if(!uwep) pline("You are empty handed.");
936.  	else prinv(uwep);
937.  	return(0);
938.  }
939.  
940.  doprarm(){
941.  	if(!uarm && !uarmg && !uarms && !uarmh)
942.  		pline("You are not wearing any armor.");
943.  	else {
944.  		char lets[6];
945.  		register int ct = 0;
946.  
947.  		if(uarm) lets[ct++] = obj_to_let(uarm);
948.  		if(uarm2) lets[ct++] = obj_to_let(uarm2);
949.  		if(uarmh) lets[ct++] = obj_to_let(uarmh);
950.  		if(uarms) lets[ct++] = obj_to_let(uarms);
951.  		if(uarmg) lets[ct++] = obj_to_let(uarmg);
952.  		lets[ct] = 0;
953.  		doinv(lets);
954.  	}
955.  	return(0);
956.  }
957.  
958.  doprring(){
959.  	if(!uleft && !uright)
960.  		pline("You are not wearing any rings.");
961.  	else {
962.  		char lets[3];
963.  		register int ct = 0;
964.  
965.  		if(uleft) lets[ct++] = obj_to_let(uleft);
966.  		if(uright) lets[ct++] = obj_to_let(uright);
967.  		lets[ct] = 0;
968.  		doinv(lets);
969.  	}
970.  	return(0);
971.  }
972.  
973.  digit(c) char c; {
974.  	return(c >= '0' && c <= '9');
975.  }
976.  
977.  /*
978.   * useupf(obj)
979.   * uses up an object that's on the floor
980.   */
981.  useupf(obj)
982.  register struct obj *obj;
983.  {
984.  	if(obj->quan > 1)  {
985.  		obj->quan--;
986.  		obj->owt = weight(obj);
987.  	}  else delobj(obj);
988.  }
989.  
990.  #ifdef SORTING
991.  /*
992.   * Convert from a symbol to a string for printing object classes
993.   *
994.   * Names from objects.h
995.   * char obj_symbols[] = {
996.   *	ILLOBJ_SYM, AMULET_SYM, FOOD_SYM, WEAPON_SYM, TOOL_SYM,
997.   *	BALL_SYM, CHAIN_SYM, ROCK_SYM, ARMOR_SYM, POTION_SYM, SCROLL_SYM,
998.   *	WAND_SYM, [SPBOOK_SYM], RING_SYM, GEM_SYM, 0 };
999.   */
1000. #define Sprintf (void) sprintf
1001. 
1002. extern char obj_symbols[];
1003. static char *names[] = {"Illegal objects", "Amulets", "Comestibles", "Weapons",
1004. 			"Tools", "Iron balls", "Chains", "Rocks", "Armor",
1005. 			"Potions", "Scrolls", "Wands",
1006. #ifdef SPELLS
1007. 			"Spellbooks",
1008. #endif
1009. 			"Rings", "Gems"};
1010. char *
1011. let_to_name(let)
1012. char let;
1013. {
1014. 	char *pos = index(obj_symbols, let);
1015. 	extern char *HI, *HE;
1016. 	/* buffer size is len(HI) + len(HE) + max(len(names[])) + 1 */
1017. 	static char buf[4 + 4 + 15 + 1];
1018. 
1019. 	if (pos == NULL) pos = obj_symbols;
1020. 	if (HI && HE)
1021. 	    Sprintf(buf, "%s%s%s", HI, names[pos - obj_symbols], HE);
1022. 	else
1023. 	    Sprintf(buf, "%s", names[pos - obj_symbols]);
1024. 	return (buf);
1025. }
1026. #endif /* SORTING /**/