Source:Hack 1.0/hack.invent.c

From NetHackWiki
Revision as of 22:23, 3 March 2008 by Kernigh bot (talk | contribs) (Hack 1.0/hack.invent.c moved to Source:Hack 1.0/hack.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 hack.invent.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.invent.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.    #include	<stdio.h>
5.    extern struct obj *splitobj();
6.    extern char morc;
7.    #ifndef NOWORM
8.    #include	"def.wseg.h"
9.    
10.   extern struct wseg *wsegs[32];
11.   #endif NOWORM
12.   
13.   struct obj *
14.   addinv(obj) register struct obj *obj; {
15.   	register struct obj *otmp;
16.   	for(otmp = invent; otmp; otmp = otmp->nobj) {
17.   		if(merged(otmp, obj, 0)) return(otmp);
18.   		if(!otmp->nobj) {
19.   			otmp->nobj = obj;
20.   			obj->nobj = 0;
21.   			return(obj);
22.   		}
23.   	}
24.   	invent = obj;
25.   	obj->nobj = 0;
26.   	return(obj);
27.   }
28.   
29.   useup(obj)
30.   register struct obj *obj;
31.   {
32.   	if(obj->quan > 1){
33.   		obj->quan--;
34.   		obj->owt = weight(obj);
35.   	} else {
36.   		setnotworn(obj);
37.   		freeinv(obj);
38.   		obfree(obj, (struct obj *) 0);
39.   	}
40.   }
41.   
42.   freeinv(obj) register struct obj *obj; {
43.   	register struct obj *otmp;
44.   	if(obj == invent) invent = invent->nobj;
45.   	else {
46.   		for(otmp = invent; otmp->nobj != obj; otmp = otmp->nobj)
47.   			if(!otmp->nobj) panic("freeinv");
48.   		otmp->nobj = obj->nobj;
49.   	}
50.   }
51.   
52.   /* destroy object in fobj chain (if unpaid, it remains on the bill) */
53.   delobj(obj) register struct obj *obj; {
54.   	freeobj(obj);
55.   	unpobj(obj);
56.   	obfree(obj, (struct obj *) 0);
57.   }
58.   
59.   /* unlink obj from chain starting with fobj */
60.   freeobj(obj) register struct obj *obj; {
61.   	register struct obj *otmp;
62.   
63.   	if(obj == fobj) fobj = fobj->nobj;
64.   	else {
65.   		for(otmp = fobj; otmp->nobj != obj; otmp = otmp->nobj)
66.   			if(!otmp) panic("error in freeobj");
67.   		otmp->nobj = obj->nobj;
68.   	}
69.   }
70.   
71.   /* Note: freegold throws away its argument! */
72.   freegold(gold) register struct gen *gold; {
73.   	register struct gen *gtmp;
74.   
75.   	if(gold == fgold) fgold = gold->ngen;
76.   	else {
77.   		for(gtmp = fgold; gtmp->ngen != gold; gtmp = gtmp->ngen)
78.   			if(!gtmp) panic("error in freegold");
79.   		gtmp->ngen = gold->ngen;
80.   	}
81.    free((char *) gold);
82.   }
83.   
84.   deltrap(trap)
85.   register struct gen *trap;
86.   {
87.   	register struct gen *gtmp;
88.   
89.   	if(trap==ftrap) ftrap=ftrap->ngen;
90.   	else {
91.   		for(gtmp=ftrap;gtmp->ngen!=trap;gtmp=gtmp->ngen) ;
92.   		gtmp->ngen=trap->ngen;
93.   	}
94.    free((char *) trap);
95.   }
96.   
97.   struct wseg *m_atseg;
98.   
99.   struct monst *
100.  m_at(x,y)
101.  register x,y;
102.  {
103.  	register struct monst *mtmp;
104.  #ifndef NOWORM
105.  	register struct wseg *wtmp;
106.  #endif NOWORM
107.  
108.  	m_atseg = 0;
109.  	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon){
110.  		if(mtmp->mx == x && mtmp->my == y)
111.  			return(mtmp);
112.  #ifndef NOWORM
113.  		if(mtmp->wormno){
114.  		    for(wtmp = wsegs[mtmp->wormno]; wtmp; wtmp = wtmp->nseg)
115.  		    if(wtmp->wx == x && wtmp->wy == y){
116.  			m_atseg = wtmp;
117.  			return(mtmp);
118.  		    }
119.  		}
120.   #endif NOWORM
121.  	}
122.   return(0);
123.  }
124.  
125.  struct obj *
126.  o_at(x,y)
127.  register x,y;
128.  {
129.  	register struct obj *otmp;
130.  
131.  	for(otmp = fobj; otmp; otmp = otmp->nobj)
132.  		if(otmp->ox == x && otmp->oy == y) return(otmp);
133.  	return(0);
134.  }
135.  
136.  struct obj *
137.  sobj_at(n,x,y)
138.  register n,x,y;
139.  {
140.  	register struct obj *otmp;
141.  
142.  	for(otmp = fobj; otmp; otmp = otmp->nobj)
143.  		if(otmp->ox == x && otmp->oy == y && otmp->otyp == n)
144.  			return(otmp);
145.  	return(0);
146.  }
147.  
148.  carried(obj) register struct obj *obj; {
149.  register struct obj *otmp;
150.  	for(otmp = invent; otmp; otmp = otmp->nobj)
151.  		if(otmp == obj) return(1);
152.  	return(0);
153.  }
154.  
155.  struct obj *
156.  o_on(id, objchn) unsigned int id; register struct obj *objchn; {
157.  	while(objchn) {
158.  		if(objchn->o_id == id) return(objchn);
159.  		objchn = objchn->nobj;
160.  	}
161.   return((struct obj *) 0);
162.  }
163.  
164.  struct gen *
165.  g_at(x,y,ptr)
166.  register x,y;
167.  register struct gen *ptr;
168.  {
169.  	while(ptr) {
170.  		if(ptr->gx == x && ptr->gy == y) return(ptr);
171.  		ptr = ptr->ngen;
172.  	}
173.   return(0);
174.  }
175.  
176.  /* getobj returns:
177.  	struct obj *xxx:	object to do something with.
178.  	0				error return: no object.
179.  	-1			explicitly no object (as in w-).
180.  */
181.  struct obj *
182.  getobj(let,word)
183.  register char *let,*word;
184.  {
185.  	register struct obj *otmp;
186.  	register char ilet,ilet1,ilet2;
187.  	char buf[BUFSZ];
188.  	char lets[BUFSZ];
189.  	register int foo = 0, foo2, cnt;
190.  	register char *bp = buf;
191.  	xchar allowcnt = 0;	/* 0, 1 or 2 */
192.  	boolean allowgold = FALSE;
193.  	boolean allowall = FALSE;
194.  	boolean allownone = FALSE;
195.  	xchar foox = 0;
196.  
197.  	if(*let == '0') let++, allowcnt = 1;
198.  	if(*let == '$') let++, allowgold = TRUE;
199.  	if(*let == '#') let++, allowall = TRUE;
200.  	if(*let == '-') let++, allownone = TRUE;
201.  	if(allownone) *bp++ = '-';
202.  	if(allowgold) *bp++ = '$';
203.  	if(bp[-1] == '-') *bp++ = ' ';
204.  
205.  	ilet = 'a';
206.  	for(otmp = invent; otmp; otmp = otmp->nobj){
207.  		if(!*let || index(let, otmp->olet)) {
208.  			bp[foo++] = ilet;
209.  			/* ugly check: remove inappropriate things */
210.  			if((!strcmp(word, "take off") &&
211.  			    !(otmp->owornmask & (W_ARMOR - W_ARM2)))
212.  			|| (!strcmp(word, "wear") &&
213.      (otmp->owornmask & (W_ARMOR | W_RING)))
214.  			|| (!strcmp(word, "wield") &&
215.  			    (otmp->owornmask & W_WEP))) {
216.  				foo--;
217.  				foox++;
218.  			}
219.  		}
220.   if(ilet == 'z') ilet = 'A'; else ilet++;
221.  	}
222.  	bp[foo] = 0;
223.  	if(foo == 0 && bp > buf && bp[-1] == ' ') *--bp = 0;
224.  	(void) strcpy(lets, bp);	/* necessary since we destroy buf */
225.  	if(foo > 5) {			/* compactify string */
226.  		foo = foo2 = 1;
227.  		ilet2 = bp[0];
228.  		ilet1 = bp[1];
229.  		while(ilet = bp[++foo2] = bp[++foo]){
230.  			if(ilet == ilet1+1){
231.  				if(ilet1 == ilet2+1)
232.  					bp[foo2 - 1] = ilet1 = '-';
233.  				else if(ilet2 == '-') {
234.  					bp[--foo2] = ++ilet1;
235.  					continue;
236.  				}
237.  			}
238.  			ilet2 = ilet1;
239.  			ilet1 = ilet;
240.  		}
241.  	}
242.  	if(!foo && !allowall && !allowgold && !allownone) {
243.  		pline("You don't have anything %sto %s.",
244.  			foox ? "else " : "", word);
245.  		return(0);
246.  	}
247.  	for(;;) {
248.  		if(!buf[0])
249.  			pline("What do you want to %s [*]? ", word);
250.  		else
251.  			pline("What do you want to %s [%s or ?*]? ",
252.  				word, buf);
253.  
254.  		cnt = 0;
255.  		ilet = readchar();
256.  		while(digit(ilet) && allowcnt) {
257.  			cnt = 10*cnt + (ilet - '0');
258.  			allowcnt = 2;	/* signal presence of cnt */
259.  			ilet = readchar();
260.  		}
261.  		if(digit(ilet)) {
262.  			pline("No count allowed with this command.");
263.  			continue;
264.  		}
265.  		if(ilet == '\033' || ilet == ' ' || ilet == '\n')
266.  			return((struct obj *)0);
267.  		if(ilet == '-') {
268.  			return((struct obj *)(allownone ? -1 : 0));
269.  		}
270.  		if(ilet == '$') {
271.  			if(!allowgold){
272.  				pline("You cannot %s gold.", word);
273.  				continue;
274.  			}
275.  			otmp = newobj(0);
276.  			/* should set o_id etc. but otmp will be freed soon */
277.  			otmp->olet = '$';
278.  			if(allowcnt == 2 && cnt < u.ugold)
279.  				u.ugold -= cnt;
280.  			else {
281.  				cnt = u.ugold;
282.  				u.ugold = 0;
283.  			}
284.  			flags.botl = 1;
285.  			otmp->quan = cnt;
286.  			return(otmp);
287.  		}
288.  		if(ilet == '?') {
289.  			doinv(lets);
290.  			if(!(ilet = morc)) continue;
291.  			/* he typed a letter (not a space) to more() */
292.  		} else if(ilet == '*') {
293.  			doinv("");
294.  			if(!(ilet = morc)) continue;
295.  			/* ... */
296.  		}
297.  		if(ilet >= 'A' && ilet <= 'Z') ilet += 'z'-'A'+1;
298.  		ilet -= 'a';
299.  		for(otmp = invent; otmp && ilet; ilet--, otmp = otmp->nobj) ;
300.  		if(!otmp) {
301.  			pline("You don't have that object.");
302.  			continue;
303.  		}
304.  		if(cnt < 0 || otmp->quan < cnt) {
305.  			pline("You don't have that many! [You have %d]"
306.  			, otmp->quan);
307.  			continue;
308.  		}
309.   break;
310.  	}
311.  	if(!allowall && let && !index(let,otmp->olet)) {
312.  		pline("That is a silly thing to %s.",word);
313.  		return(0);
314.  	}
315.  	if(allowcnt == 2) {	/* cnt given */
316.  		if(cnt == 0) return(0);
317.  		if(cnt != otmp->quan) {
318.  			register struct obj *obj;
319.  			obj = splitobj(otmp, cnt);
320.  			if(otmp == uwep) setuwep(obj);
321.  		}
322.  	}
323.   return(otmp);
324.  }
325.  
326.  ckunpaid(otmp) register struct obj *otmp; {
327.  	return( otmp->unpaid );
328.  }
329.  
330.  /* interactive version of getobj */
331.  /* used for Drop and Identify */
332.  ggetobj(word, fn, max)
333.  char *word;
334.  int (*fn)(),  max;
335.  {
336.  char buf[BUFSZ];
337.  register char *ip;
338.  register char sym;
339.  register int oletct = 0, iletct = 0;
340.  register boolean allflag = FALSE;
341.  char olets[20], ilets[20];
342.  int (*ckfn)() = (int (*)()) 0;
343.  	if(!invent){
344.  		pline("You have nothing to %s.", word);
345.  		return(0);
346.  	} else {
347.  		register struct obj *otmp = invent;
348.  		register int uflg = 0;
349.  
350.  		ilets[0] = 0;
351.  		while(otmp) {
352.  			if(!index(ilets, otmp->olet)){
353.  				ilets[iletct++] = otmp->olet;
354.  				ilets[iletct] = 0;
355.  			}
356.  			if(otmp->unpaid) uflg = 1;
357.  			otmp = otmp->nobj;
358.  		}
359.  		ilets[iletct++] = ' ';
360.  		if(uflg) ilets[iletct++] = 'u';
361.  		ilets[iletct++] = 'a';
362.  		ilets[iletct] = 0;
363.  	}
364.  	pline("What kinds of thing do you want to %s? [%s] ",
365.  		word, ilets);
366.  	getlin(buf);
367.  	ip = buf;
368.  	olets[0] = 0;
369.  	while(sym = *ip++){
370.  		if(sym == ' ') continue;
371.  		if(sym == 'a') allflag = TRUE; else
372.  		if(sym == 'u') ckfn = ckunpaid; else
373.  		if(index("!%?[()=*/\"0", sym)){
374.  			if(!index(olets, sym)){
375.  				olets[oletct++] = sym;
376.  				olets[oletct] = 0;
377.  			}
378.  		}
379.   else pline("You don't have any %c's.", sym);
380.  	}
381.   return askchain(invent, olets, allflag, fn, ckfn, max);
382.  }
383.  
384.  /* Walk through the chain starting at objchn and ask for all objects
385.     with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
386.     whether the action in question (i.e., fn) has to be performed.
387.     If allflag then no questions are asked. Max gives the max nr of
388.     objects treated.
389.   */
390.  askchain(objchn, olets, allflag, fn, ckfn, max)
391.  struct obj *objchn;
392.  register char *olets;
393.  int allflag;
394.  int (*fn)(), (*ckfn)();
395.  int max;
396.  {
397.  register struct obj *otmp, *otmp2;
398.  register char sym, ilet;
399.  register int cnt = 0;
400.  	ilet = 'a'-1;
401.  	for(otmp = objchn; otmp; otmp = otmp2){
402.  		if(ilet == 'z') ilet = 'A'; else ilet++;
403.  		otmp2 = otmp->nobj;
404.  		if(olets && *olets && !index(olets, otmp->olet)) continue;
405.  		if(ckfn && !(*ckfn)(otmp)) continue;
406.  		if(!allflag) {
407.  			prname(otmp, ilet, 1);
408.  			addtopl(" (ynaq)? ");
409.  			sym = readchar();
410.  		}
411.  		else	sym = 'y';
412.  
413.  		switch(sym){
414.  		case 'a':
415.  			allflag = 1;
416.  		case 'y':
417.  			cnt += (*fn)(otmp);
418.  			if(--max == 0) goto ret;
419.  		case 'n':
420.  		default:
421.  			break;
422.  		case 'q':
423.  			goto ret;
424.  		}
425.  	}
426.  	pline(cnt ? "That was all." : "No applicable objects.");
427.  ret:
428.  	if(!flags.echo) echo(OFF);
429.  	return(cnt);
430.  }
431.  
432.  obj_to_let(obj)
433.  register struct obj *obj;
434.  {
435.  	register struct obj *otmp;
436.  	register char ilet = 'a';
437.  
438.  	for(otmp = invent; otmp && otmp != obj; otmp = otmp->nobj)
439.  		if(++ilet > 'z') ilet = 'A';
440.  	return(otmp ? ilet : 0);
441.  }
442.  
443.  prinv(obj)
444.  register struct obj *obj;
445.  {
446.  	prname(obj, obj_to_let(obj), 1);
447.  }
448.  
449.  prname(obj,let,onelin)
450.  register struct obj *obj;
451.  register char let;
452.  {
453.  	char li[BUFSZ];
454.  
455.  	(void) sprintf(li, " %c - %s.", let, doname(obj));
456.  	switch(onelin) {
457.  	case 1:
458.  		pline(li+1);
459.  		break;
460.  	case 0:
461.  		puts(li+1);
462.  		break;
463.  	case -1:
464.  		cl_end();
465.  		fputs(li, stdout);
466.  		curx += strlen(li);
467.  	}
468.  }
469.  
470.  ddoinv()
471.  {
472.  	doinv((char *) 0);
473.  	return(0);
474.  }
475.  
476.  /* called with "": all objects in inventory; with 0: also usedup ones */
477.  /* otherwise: all objects with (serial) letter in lets */
478.  doinv(lets) register char *lets; {
479.  	register struct obj *otmp;
480.  	register char ilet = 'a';
481.  	int ct = 0;
482.  	int maxlth = 0;
483.  	int lth;
484.  
485.  	if(!invent){
486.  		pline("Not carrying anything");
487.  		if(lets) return;
488.  	}
489.  	if(!flags.oneline) {
490.  	    if(!lets || !*lets)
491.  		for(otmp = invent; otmp; otmp = otmp->nobj) ct++;
492.  	    else
493.  		ct = strlen(lets);
494.  	    if(ct > 1 && ct < ROWNO && (lets || !inshop())){
495.  		for(otmp = invent; otmp; otmp = otmp->nobj) {
496.  		    if(!lets || !*lets || index(lets, ilet)) {
497.  			lth = strlen(doname(otmp));
498.  			if(lth > maxlth) maxlth = lth;
499.  		    }
500.  		    if(++ilet > 'z') ilet = 'A';
501.  		}
502.  		ilet = 'a';
503.  		lth = COLNO - maxlth - 7;
504.  		if(lth < 10) goto clrscr;
505.  		home();
506.  		cl_end();
507.  		flags.topl = 0;
508.  		ct = 0;
509.  		for(otmp = invent; otmp; otmp = otmp->nobj) {
510.  		    if(!lets || !*lets || index(lets, ilet)) {
511.  			curs(lth, ++ct);
512.  			prname(otmp, ilet, -1);
513.  		    }
514.  		    if(++ilet > 'z') ilet = 'A';
515.  		}
516.  		curs(lth, ct+1);
517.  		cl_end();
518.  		cmore();	/* sets morc */
519.  		/* test whether morc is a reasonable answer */
520.  		if(lets && *lets && !index(lets, morc)) morc = 0;
521.  
522.  		home();
523.  		cl_end();
524.  		docorner(lth, ct);
525.  		return;
526.  	    }
527.  	}
528.      clrscr:
529.  	if(ct > 1) cls();
530.  	for(otmp = invent; otmp; otmp = otmp->nobj){
531.  		if(!lets || !*lets || index(lets, ilet))
532.  			prname(otmp, ilet, (ct > 1) ? 0 : 1);
533.  		if(++ilet > 'z') ilet = 'A';
534.  	}
535.  	/* tell doinvbill whether we cleared the screen */
536.  	if(!lets) doinvbill((ct > 1));
537.  	if(ct > 1){
538.  		cgetret();
539.  		docrt();
540.  	} else
541.   morc = 0;	/* %% */
542.  }
543.  
544.  stackobj(obj) register struct obj *obj; {
545.  register struct obj *otmp = fobj;
546.  	for(otmp = fobj; otmp; otmp = otmp->nobj) if(otmp != obj)
547.  	if(otmp->ox == obj->ox && otmp->oy == obj->oy &&
548.  		merged(obj,otmp,1))
549.  			return;
550.  }
551.  
552.  /* merge obj with otmp and delete obj if types agree */
553.  merged(otmp,obj,lose) register struct obj *otmp, *obj; {
554.  	if(otmp->otyp == obj->otyp &&
555.  	  obj->unpaid == otmp->unpaid && obj->spe == otmp->spe &&
556.  	  obj->known == otmp->known && obj->dknown == otmp->dknown &&
557.  	  obj->cursed == otmp->cursed &&
558.  	  ((obj->olet == WEAPON_SYM && obj->otyp < BOOMERANG)
559.  	  || index("%?!*",otmp->olet))){
560.  		otmp->quan += obj->quan;
561.  		otmp->owt += obj->owt;
562.  		if(lose) freeobj(obj);
563.  		obfree(obj,otmp);	/* free(obj), bill->otmp */
564.  		return(1);
565.  	} else	return(0);
566.  }
567.  
568.  doprwep(){
569.  	if(!uwep) pline("You are empty handed.");
570.  	else prinv(uwep);
571.  	return(0);
572.  }
573.  
574.  doprarm(){
575.  	if(!uarm && !uarmg && !uarms && !uarmh)
576.  		pline("You are not wearing any armor.");
577.  	else {
578.  		char lets[6];
579.  		register int ct = 0;
580.  
581.  		if(uarm) lets[ct++] = obj_to_let(uarm);
582.  		if(uarm2) lets[ct++] = obj_to_let(uarm2);
583.  		if(uarmh) lets[ct++] = obj_to_let(uarmh);
584.  		if(uarms) lets[ct++] = obj_to_let(uarms);
585.  		if(uarmg) lets[ct++] = obj_to_let(uarmg);
586.  		lets[ct] = 0;
587.  		doinv(lets);
588.  	}
589.   return(0);
590.  }
591.  
592.  doprring(){
593.  	if(!uleft && !uright)
594.  		pline("You are not wearing any rings.");
595.  	else {
596.  		char lets[3];
597.  		register int ct = 0;
598.  
599.  		if(uleft) lets[ct++] = obj_to_let(uleft);
600.  		if(uright) lets[ct++] = obj_to_let(uright);
601.  		lets[ct] = 0;
602.  		doinv(lets);
603.  	}
604.   return(0);
605.  }
606.  
607.  digit(c) char c; {
608.  	return(c >= '0' && c <= '9');
609.  }