]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.invent.c - version 1.0.3 */
6 extern struct obj
*splitobj();
7 extern struct obj zeroobj
;
9 extern char quitchars
[];
14 extern struct wseg
*wsegs
[32];
19 static int lastinvnr
= 51; /* 0 ... 51 */
22 register struct obj
*otmp
;
26 register struct obj
*obj
;
28 for(i
= 0; i
< 52; i
++) inuse
[i
] = FALSE
;
29 for(obj
= invent
; obj
; obj
= obj
->nobj
) if(obj
!= otmp
) {
31 if('a' <= i
&& i
<= 'z') inuse
[i
- 'a'] = TRUE
; else
32 if('A' <= i
&& i
<= 'Z') inuse
[i
- 'A' + 26] = TRUE
;
33 if(i
== otmp
->invlet
) otmp
->invlet
= 0;
35 if((i
= otmp
->invlet
) &&
36 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
38 for(i
= lastinvnr
+1; i
!= lastinvnr
; i
++) {
39 if(i
== 52) { i
= -1; continue; }
42 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
43 (i
< 26) ? ('a'+i
) : ('A'+i
-26));
49 register struct obj
*obj
;
51 register struct obj
*otmp
;
53 /* merge or attach to end of chain */
58 for(otmp
= invent
; /* otmp */; otmp
= otmp
->nobj
) {
59 if(merged(otmp
, obj
, 0))
68 if(flags
.invlet_constant
) {
71 * The ordering of the chain is nowhere significant
72 * so in case you prefer some other order than the
73 * historical one, change the code below.
75 if(otmp
) { /* find proper place in chain */
77 if((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
81 for(otmp
= invent
; ; otmp
= otmp
->nobj
) {
83 (otmp
->nobj
->invlet
^ 040) > (obj
->invlet
^ 040)){
84 obj
->nobj
= otmp
->nobj
;
96 register struct obj
*obj
;
100 obj
->owt
= weight(obj
);
104 obfree(obj
, (struct obj
*) 0);
109 register struct obj
*obj
;
111 register struct obj
*otmp
;
114 invent
= invent
->nobj
;
116 for(otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
117 if(!otmp
->nobj
) panic("freeinv");
118 otmp
->nobj
= obj
->nobj
;
122 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
123 delobj(obj
) register struct obj
*obj
; {
126 obfree(obj
, (struct obj
*) 0);
129 /* unlink obj from chain starting with fobj */
130 freeobj(obj
) register struct obj
*obj
; {
131 register struct obj
*otmp
;
133 if(obj
== fobj
) fobj
= fobj
->nobj
;
135 for(otmp
= fobj
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
136 if(!otmp
) panic("error in freeobj");
137 otmp
->nobj
= obj
->nobj
;
141 /* Note: freegold throws away its argument! */
142 freegold(gold
) register struct gold
*gold
; {
143 register struct gold
*gtmp
;
145 if(gold
== fgold
) fgold
= gold
->ngold
;
147 for(gtmp
= fgold
; gtmp
->ngold
!= gold
; gtmp
= gtmp
->ngold
)
148 if(!gtmp
) panic("error in freegold");
149 gtmp
->ngold
= gold
->ngold
;
155 register struct trap
*trap
;
157 register struct trap
*ttmp
;
160 ftrap
= ftrap
->ntrap
;
162 for(ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
) ;
163 ttmp
->ntrap
= trap
->ntrap
;
168 struct wseg
*m_atseg
;
174 register struct monst
*mtmp
;
176 register struct wseg
*wtmp
;
180 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
){
181 if(mtmp
->mx
== x
&& mtmp
->my
== y
)
185 for(wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
186 if(wtmp
->wx
== x
&& wtmp
->wy
== y
){
200 register struct obj
*otmp
;
202 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
203 if(otmp
->ox
== x
&& otmp
->oy
== y
) return(otmp
);
211 register struct obj
*otmp
;
213 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
214 if(otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
219 carried(obj
) register struct obj
*obj
; {
220 register struct obj
*otmp
;
221 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
222 if(otmp
== obj
) return(1);
229 register struct obj
*otmp
;
231 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
232 if(otmp
->otyp
== type
)
238 o_on(id
, objchn
) unsigned int id
; register struct obj
*objchn
; {
240 if(objchn
->o_id
== id
) return(objchn
);
241 objchn
= objchn
->nobj
;
243 return((struct obj
*) 0);
250 register struct trap
*trap
= ftrap
;
252 if(trap
->tx
== x
&& trap
->ty
== y
) return(trap
);
262 register struct gold
*gold
= fgold
;
264 if(gold
->gx
== x
&& gold
->gy
== y
) return(gold
);
270 /* make dummy object structure containing gold - for temporary use only */
275 register struct obj
*otmp
;
278 /* should set o_id etc. but otmp will be freed soon */
288 * struct obj *xxx: object to do something with.
289 * (struct obj *) 0 error return: no object.
290 * &zeroobj explicitly no object (as in w-).
294 register char *let
,*word
;
296 register struct obj
*otmp
;
297 register char ilet
,ilet1
,ilet2
;
300 register int foo
= 0, foo2
;
301 register char *bp
= buf
;
302 xchar allowcnt
= 0; /* 0, 1 or 2 */
303 boolean allowgold
= FALSE
;
304 boolean allowall
= FALSE
;
305 boolean allownone
= FALSE
;
309 if(*let
== '0') let
++, allowcnt
= 1;
310 if(*let
== '$') let
++, allowgold
= TRUE
;
311 if(*let
== '#') let
++, allowall
= TRUE
;
312 if(*let
== '-') let
++, allownone
= TRUE
;
313 if(allownone
) *bp
++ = '-';
314 if(allowgold
) *bp
++ = '$';
315 if(bp
> buf
&& bp
[-1] == '-') *bp
++ = ' ';
318 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
){
319 if(!*let
|| index(let
, otmp
->olet
)) {
320 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
322 /* ugly check: remove inappropriate things */
323 if((!strcmp(word
, "take off") &&
324 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
325 || (!strcmp(word
, "wear") &&
326 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
327 || (!strcmp(word
, "wield") &&
328 (otmp
->owornmask
& W_WEP
))) {
333 if(ilet
== 'z') ilet
= 'A'; else ilet
++;
336 if(foo
== 0 && bp
> buf
&& bp
[-1] == ' ') *--bp
= 0;
337 (void) strcpy(lets
, bp
); /* necessary since we destroy buf */
338 if(foo
> 5) { /* compactify string */
342 while(ilet
= bp
[++foo2
] = bp
[++foo
]){
345 bp
[foo2
- 1] = ilet1
= '-';
346 else if(ilet2
== '-') {
347 bp
[--foo2
] = ++ilet1
;
355 if(!foo
&& !allowall
&& !allowgold
&& !allownone
) {
356 pline("You don't have anything %sto %s.",
357 foox
? "else " : "", word
);
362 pline("What do you want to %s [*]? ", word
);
364 pline("What do you want to %s [%s or ?*]? ",
369 while(digit(ilet
) && allowcnt
) {
371 cnt
= 10*cnt
+ (ilet
- '0');
374 allowcnt
= 2; /* signal presence of cnt */
378 pline("No count allowed with this command.");
381 if(index(quitchars
,ilet
))
382 return((struct obj
*)0);
384 return(allownone
? &zeroobj
: (struct obj
*) 0);
388 pline("You cannot %s gold.", word
);
391 if(!(allowcnt
== 2 && cnt
< u
.ugold
))
393 return(mkgoldobj(cnt
));
397 if(!(ilet
= morc
)) continue;
398 /* he typed a letter (not a space) to more() */
399 } else if(ilet
== '*') {
401 if(!(ilet
= morc
)) continue;
404 if(flags
.invlet_constant
) {
405 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
406 if(otmp
->invlet
== ilet
) break;
408 if(ilet
>= 'A' && ilet
<= 'Z') ilet
+= 'z'-'A'+1;
410 for(otmp
= invent
; otmp
&& ilet
;
411 ilet
--, otmp
= otmp
->nobj
) ;
414 pline("You don't have that object.");
417 if(cnt
< 0 || otmp
->quan
< cnt
) {
418 pline("You don't have that many! [You have %u]"
424 if(!allowall
&& let
&& !index(let
,otmp
->olet
)) {
425 pline("That is a silly thing to %s.",word
);
428 if(allowcnt
== 2) { /* cnt given */
429 if(cnt
== 0) return(0);
430 if(cnt
!= otmp
->quan
) {
431 register struct obj
*obj
;
432 obj
= splitobj(otmp
, (int) cnt
);
433 if(otmp
== uwep
) setuwep(obj
);
439 ckunpaid(otmp
) register struct obj
*otmp
; {
440 return( otmp
->unpaid
);
443 /* interactive version of getobj - used for Drop and Identify */
444 /* return the number of times fn was called successfully */
445 ggetobj(word
, fn
, max
)
452 register int oletct
= 0, iletct
= 0;
453 register boolean allflag
= FALSE
;
454 char olets
[20], ilets
[20];
455 int (*ckfn
)() = (int (*)()) 0;
456 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
457 if(!invent
&& !allowgold
){
458 pline("You have nothing to %s.", word
);
461 register struct obj
*otmp
= invent
;
462 register int uflg
= 0;
464 if(allowgold
) ilets
[iletct
++] = '$';
467 if(!index(ilets
, otmp
->olet
)){
468 ilets
[iletct
++] = otmp
->olet
;
471 if(otmp
->unpaid
) uflg
= 1;
474 ilets
[iletct
++] = ' ';
475 if(uflg
) ilets
[iletct
++] = 'u';
476 if(invent
) ilets
[iletct
++] = 'a';
479 pline("What kinds of thing do you want to %s? [%s] ",
482 if(buf
[0] == '\033') {
489 if(sym
== ' ') continue;
492 (*fn
)(mkgoldobj(u
.ugold
));
494 pline("You have no gold.");
497 if(sym
== 'a' || sym
== 'A') allflag
= TRUE
; else
498 if(sym
== 'u' || sym
== 'U') ckfn
= ckunpaid
; else
499 if(index("!%?[()=*/\"0", sym
)){
500 if(!index(olets
, sym
)){
501 olets
[oletct
++] = sym
;
505 else pline("You don't have any %c's.", sym
);
507 if(allowgold
== 2 && !oletct
)
508 return(1); /* he dropped gold (or at least tried to) */
510 return(askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
514 * Walk through the chain starting at objchn and ask for all objects
515 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
516 * whether the action in question (i.e., fn) has to be performed.
517 * If allflag then no questions are asked. Max gives the max nr of
518 * objects to be treated. Return the number of objects treated.
520 askchain(objchn
, olets
, allflag
, fn
, ckfn
, max
)
522 register char *olets
;
524 int (*fn
)(), (*ckfn
)();
527 register struct obj
*otmp
, *otmp2
;
528 register char sym
, ilet
;
529 register int cnt
= 0;
531 for(otmp
= objchn
; otmp
; otmp
= otmp2
){
532 if(ilet
== 'z') ilet
= 'A'; else ilet
++;
534 if(olets
&& *olets
&& !index(olets
, otmp
->olet
)) continue;
535 if(ckfn
&& !(*ckfn
)(otmp
)) continue;
537 pline(xprname(otmp
, ilet
));
538 addtopl(" [nyaq]? ");
548 if(--max
== 0) goto ret
;
556 pline(cnt
? "That was all." : "No applicable objects.");
561 obj_to_let(obj
) /* should of course only be called for things in invent */
562 register struct obj
*obj
;
564 register struct obj
*otmp
;
567 if(flags
.invlet_constant
)
570 for(otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
571 if(++ilet
> 'z') ilet
= 'A';
572 return(otmp
? ilet
: NOINVSYM
);
576 register struct obj
*obj
;
578 pline(xprname(obj
, obj_to_let(obj
)));
583 register struct obj
*obj
;
586 static char li
[BUFSZ
];
588 (void) sprintf(li
, "%c - %s.",
589 flags
.invlet_constant
? obj
->invlet
: let
,
600 /* called with 0 or "": all objects in inventory */
601 /* otherwise: all objects with (serial) letter in lets */
605 register struct obj
*otmp
;
610 morc
= 0; /* just to be sure */
613 pline("Not carrying anything.");
617 cornline(0, (char *) 0);
619 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
620 if(flags
.invlet_constant
) ilet
= otmp
->invlet
;
621 if(!lets
|| !*lets
|| index(lets
, ilet
)) {
622 cornline(1, xprname(otmp
, ilet
));
625 if(!flags
.invlet_constant
) if(++ilet
> 'z') ilet
= 'A';
631 dotypeinv () /* free after Robert Viduya */
632 /* Changed to one type only, so he doesnt have to type cr */
637 register struct obj
*otmp
;
638 boolean billx
= inshop() && doinvbill(0);
639 boolean unpd
= FALSE
;
641 if (!invent
&& !u
.ugold
&& !billx
) {
642 pline ("You aren't carrying anything.");
647 if(u
.ugold
) stuff
[stct
++] = '$';
649 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
650 if (!index (stuff
, otmp
->olet
)) {
651 stuff
[stct
++] = otmp
->olet
;
657 if(unpd
) stuff
[stct
++] = 'u';
658 if(billx
) stuff
[stct
++] = 'x';
662 pline ("What type of object [%s] do you want an inventory of? ",
665 if(index(quitchars
,c
)) return(0);
672 if(c
== 'x' || c
== 'X') {
676 pline("No used-up objects on the shopping bill.");
680 if((c
== 'u' || c
== 'U') && !unpd
) {
681 pline("You are not carrying any unpaid objects.");
687 for (otmp
= invent
; otmp
; otmp
= otmp
-> nobj
) {
688 if(flags
.invlet_constant
) ilet
= otmp
->invlet
;
689 if (c
== otmp
-> olet
|| (c
== 'u' && otmp
-> unpaid
))
690 stuff
[stct
++] = ilet
;
691 if(!flags
.invlet_constant
) if(++ilet
> 'z') ilet
= 'A';
695 pline("You have no such objects.");
702 /* look at what is here */
704 register struct obj
*otmp
, *otmp0
;
705 register struct gold
*gold
;
706 char *verb
= Blind
? "feel" : "see";
711 pline("You try to feel what is lying here on the floor.");
712 if(Levitation
) { /* ab@unido */
713 pline("You cannot reach the floor!");
717 otmp0
= o_at(u
.ux
, u
.uy
);
718 gold
= g_at(u
.ux
, u
.uy
);
721 if(u
.uswallow
|| (!otmp0
&& !gold
)) {
722 pline("You %s no objects here.", verb
);
726 cornline(0, "Things that are here:");
727 for(otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
728 if(otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
730 cornline(1, doname(otmp
));
731 if(Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
732 pline("Touching the dead cockatrice is a fatal mistake ...");
733 pline("You die ...");
734 killer
= "dead cockatrice";
743 (void) sprintf(gbuf
, "%ld gold piece%s",
744 gold
->amount
, plur(gold
->amount
));
746 pline("You %s here %s.", verb
, gbuf
);
751 if(ct
== 1 && !gold
) {
752 pline("You %s here %s.", verb
, doname(otmp0
));
753 cornline(3, (char *) 0);
756 cornline(2, (char *) 0);
760 stackobj(obj
) register struct obj
*obj
; {
761 register struct obj
*otmp
= fobj
;
762 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
) if(otmp
!= obj
)
763 if(otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
768 /* merge obj with otmp and delete obj if types agree */
769 merged(otmp
,obj
,lose
) register struct obj
*otmp
, *obj
; {
770 if(obj
->otyp
== otmp
->otyp
&&
771 obj
->unpaid
== otmp
->unpaid
&&
772 obj
->spe
== otmp
->spe
&&
773 obj
->dknown
== otmp
->dknown
&&
774 obj
->cursed
== otmp
->cursed
&&
775 (index("%*?!", obj
->olet
) ||
776 (obj
->known
== otmp
->known
&&
777 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
778 otmp
->quan
+= obj
->quan
;
779 otmp
->owt
+= obj
->owt
;
780 if(lose
) freeobj(obj
);
781 obfree(obj
,otmp
); /* free(obj), bill->otmp */
787 * Gold is no longer displayed; in fact, when you have a lot of money,
788 * it may take a while before you have counted it all.
789 * [Bug: d$ and pickup still tell you how much it was.]
791 extern int (*occupation
)();
793 static long goldcounted
;
796 if((goldcounted
+= 100*(u
.ulevel
+ 1)) >= u
.ugold
) {
798 if(!rn2(2)) eps
= rnd((int) (u
.ugold
/100 + 1));
799 pline("You probably have about %ld gold pieces.",
801 return(0); /* done */
803 return(1); /* continue */
808 pline("You do not carry any gold.");
809 else if(u
.ugold
<= 500)
810 pline("You are carrying %ld gold pieces.", u
.ugold
);
812 pline("You sit down in order to count your gold pieces.");
814 occupation
= countgold
;
815 occtxt
= "counting your gold";
820 /* --- end of gold counting section --- */
823 if(!uwep
) pline("You are empty handed.");
829 if(!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
830 pline("You are not wearing any armor.");
835 if(uarm
) lets
[ct
++] = obj_to_let(uarm
);
836 if(uarm2
) lets
[ct
++] = obj_to_let(uarm2
);
837 if(uarmh
) lets
[ct
++] = obj_to_let(uarmh
);
838 if(uarms
) lets
[ct
++] = obj_to_let(uarms
);
839 if(uarmg
) lets
[ct
++] = obj_to_let(uarmg
);
847 if(!uleft
&& !uright
)
848 pline("You are not wearing any rings.");
853 if(uleft
) lets
[ct
++] = obj_to_let(uleft
);
854 if(uright
) lets
[ct
++] = obj_to_let(uright
);
862 return(c
>= '0' && c
<= '9');