]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
6 static char rcsid
[] = "$Id: hack.invent.c,v 1.3 1993/08/02 17:17:15 mycroft Exp $";
11 extern struct obj
*splitobj();
12 extern struct obj zeroobj
;
14 extern char quitchars
[];
15 static char *xprname();
19 extern struct wseg
*wsegs
[32];
24 static int lastinvnr
= 51; /* 0 ... 51 */
27 register struct obj
*otmp
;
31 register struct obj
*obj
;
33 for(i
= 0; i
< 52; i
++) inuse
[i
] = FALSE
;
34 for(obj
= invent
; obj
; obj
= obj
->nobj
) if(obj
!= otmp
) {
36 if('a' <= i
&& i
<= 'z') inuse
[i
- 'a'] = TRUE
; else
37 if('A' <= i
&& i
<= 'Z') inuse
[i
- 'A' + 26] = TRUE
;
38 if(i
== otmp
->invlet
) otmp
->invlet
= 0;
40 if((i
= otmp
->invlet
) &&
41 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
43 for(i
= lastinvnr
+1; i
!= lastinvnr
; i
++) {
44 if(i
== 52) { i
= -1; continue; }
47 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
48 (i
< 26) ? ('a'+i
) : ('A'+i
-26));
54 register struct obj
*obj
;
56 register struct obj
*otmp
;
58 /* merge or attach to end of chain */
63 for(otmp
= invent
; /* otmp */; otmp
= otmp
->nobj
) {
64 if(merged(otmp
, obj
, 0))
73 if(flags
.invlet_constant
) {
76 * The ordering of the chain is nowhere significant
77 * so in case you prefer some other order than the
78 * historical one, change the code below.
80 if(otmp
) { /* find proper place in chain */
82 if((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
86 for(otmp
= invent
; ; otmp
= otmp
->nobj
) {
88 (otmp
->nobj
->invlet
^ 040) > (obj
->invlet
^ 040)){
89 obj
->nobj
= otmp
->nobj
;
101 register struct obj
*obj
;
105 obj
->owt
= weight(obj
);
109 obfree(obj
, (struct obj
*) 0);
114 register struct obj
*obj
;
116 register struct obj
*otmp
;
119 invent
= invent
->nobj
;
121 for(otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
122 if(!otmp
->nobj
) panic("freeinv");
123 otmp
->nobj
= obj
->nobj
;
127 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
128 delobj(obj
) register struct obj
*obj
; {
131 obfree(obj
, (struct obj
*) 0);
134 /* unlink obj from chain starting with fobj */
135 freeobj(obj
) register struct obj
*obj
; {
136 register struct obj
*otmp
;
138 if(obj
== fobj
) fobj
= fobj
->nobj
;
140 for(otmp
= fobj
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
141 if(!otmp
) panic("error in freeobj");
142 otmp
->nobj
= obj
->nobj
;
146 /* Note: freegold throws away its argument! */
147 freegold(gold
) register struct gold
*gold
; {
148 register struct gold
*gtmp
;
150 if(gold
== fgold
) fgold
= gold
->ngold
;
152 for(gtmp
= fgold
; gtmp
->ngold
!= gold
; gtmp
= gtmp
->ngold
)
153 if(!gtmp
) panic("error in freegold");
154 gtmp
->ngold
= gold
->ngold
;
160 register struct trap
*trap
;
162 register struct trap
*ttmp
;
165 ftrap
= ftrap
->ntrap
;
167 for(ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
) ;
168 ttmp
->ntrap
= trap
->ntrap
;
173 struct wseg
*m_atseg
;
179 register struct monst
*mtmp
;
181 register struct wseg
*wtmp
;
185 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
){
186 if(mtmp
->mx
== x
&& mtmp
->my
== y
)
190 for(wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
191 if(wtmp
->wx
== x
&& wtmp
->wy
== y
){
205 register struct obj
*otmp
;
207 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
208 if(otmp
->ox
== x
&& otmp
->oy
== y
) return(otmp
);
216 register struct obj
*otmp
;
218 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
219 if(otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
224 carried(obj
) register struct obj
*obj
; {
225 register struct obj
*otmp
;
226 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
227 if(otmp
== obj
) return(1);
234 register struct obj
*otmp
;
236 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
237 if(otmp
->otyp
== type
)
243 o_on(id
, objchn
) unsigned int id
; register struct obj
*objchn
; {
245 if(objchn
->o_id
== id
) return(objchn
);
246 objchn
= objchn
->nobj
;
248 return((struct obj
*) 0);
255 register struct trap
*trap
= ftrap
;
257 if(trap
->tx
== x
&& trap
->ty
== y
) return(trap
);
267 register struct gold
*gold
= fgold
;
269 if(gold
->gx
== x
&& gold
->gy
== y
) return(gold
);
275 /* make dummy object structure containing gold - for temporary use only */
280 register struct obj
*otmp
;
283 /* should set o_id etc. but otmp will be freed soon */
293 * struct obj *xxx: object to do something with.
294 * (struct obj *) 0 error return: no object.
295 * &zeroobj explicitly no object (as in w-).
299 register char *let
,*word
;
301 register struct obj
*otmp
;
302 register char ilet
,ilet1
,ilet2
;
305 register int foo
= 0, foo2
;
306 register char *bp
= buf
;
307 xchar allowcnt
= 0; /* 0, 1 or 2 */
308 boolean allowgold
= FALSE
;
309 boolean allowall
= FALSE
;
310 boolean allownone
= FALSE
;
314 if(*let
== '0') let
++, allowcnt
= 1;
315 if(*let
== '$') let
++, allowgold
= TRUE
;
316 if(*let
== '#') let
++, allowall
= TRUE
;
317 if(*let
== '-') let
++, allownone
= TRUE
;
318 if(allownone
) *bp
++ = '-';
319 if(allowgold
) *bp
++ = '$';
320 if(bp
> buf
&& bp
[-1] == '-') *bp
++ = ' ';
323 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
){
324 if(!*let
|| index(let
, otmp
->olet
)) {
325 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
327 /* ugly check: remove inappropriate things */
328 if((!strcmp(word
, "take off") &&
329 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
330 || (!strcmp(word
, "wear") &&
331 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
332 || (!strcmp(word
, "wield") &&
333 (otmp
->owornmask
& W_WEP
))) {
338 if(ilet
== 'z') ilet
= 'A'; else ilet
++;
341 if(foo
== 0 && bp
> buf
&& bp
[-1] == ' ') *--bp
= 0;
342 (void) strcpy(lets
, bp
); /* necessary since we destroy buf */
343 if(foo
> 5) { /* compactify string */
347 while(ilet
= bp
[++foo2
] = bp
[++foo
]){
350 bp
[foo2
- 1] = ilet1
= '-';
351 else if(ilet2
== '-') {
352 bp
[--foo2
] = ++ilet1
;
360 if(!foo
&& !allowall
&& !allowgold
&& !allownone
) {
361 pline("You don't have anything %sto %s.",
362 foox
? "else " : "", word
);
367 pline("What do you want to %s [*]? ", word
);
369 pline("What do you want to %s [%s or ?*]? ",
374 while(digit(ilet
) && allowcnt
) {
376 cnt
= 10*cnt
+ (ilet
- '0');
379 allowcnt
= 2; /* signal presence of cnt */
383 pline("No count allowed with this command.");
386 if(index(quitchars
,ilet
))
387 return((struct obj
*)0);
389 return(allownone
? &zeroobj
: (struct obj
*) 0);
393 pline("You cannot %s gold.", word
);
396 if(!(allowcnt
== 2 && cnt
< u
.ugold
))
398 return(mkgoldobj(cnt
));
402 if(!(ilet
= morc
)) continue;
403 /* he typed a letter (not a space) to more() */
404 } else if(ilet
== '*') {
406 if(!(ilet
= morc
)) continue;
409 if(flags
.invlet_constant
) {
410 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
411 if(otmp
->invlet
== ilet
) break;
413 if(ilet
>= 'A' && ilet
<= 'Z') ilet
+= 'z'-'A'+1;
415 for(otmp
= invent
; otmp
&& ilet
;
416 ilet
--, otmp
= otmp
->nobj
) ;
419 pline("You don't have that object.");
422 if(cnt
< 0 || otmp
->quan
< cnt
) {
423 pline("You don't have that many! [You have %u]"
429 if(!allowall
&& let
&& !index(let
,otmp
->olet
)) {
430 pline("That is a silly thing to %s.",word
);
433 if(allowcnt
== 2) { /* cnt given */
434 if(cnt
== 0) return(0);
435 if(cnt
!= otmp
->quan
) {
436 register struct obj
*obj
;
437 obj
= splitobj(otmp
, (int) cnt
);
438 if(otmp
== uwep
) setuwep(obj
);
444 ckunpaid(otmp
) register struct obj
*otmp
; {
445 return( otmp
->unpaid
);
448 /* interactive version of getobj - used for Drop and Identify */
449 /* return the number of times fn was called successfully */
450 ggetobj(word
, fn
, max
)
457 register int oletct
= 0, iletct
= 0;
458 register boolean allflag
= FALSE
;
459 char olets
[20], ilets
[20];
460 int (*ckfn
)() = (int (*)()) 0;
461 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
462 if(!invent
&& !allowgold
){
463 pline("You have nothing to %s.", word
);
466 register struct obj
*otmp
= invent
;
467 register int uflg
= 0;
469 if(allowgold
) ilets
[iletct
++] = '$';
472 if(!index(ilets
, otmp
->olet
)){
473 ilets
[iletct
++] = otmp
->olet
;
476 if(otmp
->unpaid
) uflg
= 1;
479 ilets
[iletct
++] = ' ';
480 if(uflg
) ilets
[iletct
++] = 'u';
481 if(invent
) ilets
[iletct
++] = 'a';
484 pline("What kinds of thing do you want to %s? [%s] ",
487 if(buf
[0] == '\033') {
494 if(sym
== ' ') continue;
497 (*fn
)(mkgoldobj(u
.ugold
));
499 pline("You have no gold.");
502 if(sym
== 'a' || sym
== 'A') allflag
= TRUE
; else
503 if(sym
== 'u' || sym
== 'U') ckfn
= ckunpaid
; else
504 if(index("!%?[()=*/\"0", sym
)){
505 if(!index(olets
, sym
)){
506 olets
[oletct
++] = sym
;
510 else pline("You don't have any %c's.", sym
);
512 if(allowgold
== 2 && !oletct
)
513 return(1); /* he dropped gold (or at least tried to) */
515 return(askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
519 * Walk through the chain starting at objchn and ask for all objects
520 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
521 * whether the action in question (i.e., fn) has to be performed.
522 * If allflag then no questions are asked. Max gives the max nr of
523 * objects to be treated. Return the number of objects treated.
525 askchain(objchn
, olets
, allflag
, fn
, ckfn
, max
)
527 register char *olets
;
529 int (*fn
)(), (*ckfn
)();
532 register struct obj
*otmp
, *otmp2
;
533 register char sym
, ilet
;
534 register int cnt
= 0;
536 for(otmp
= objchn
; otmp
; otmp
= otmp2
){
537 if(ilet
== 'z') ilet
= 'A'; else ilet
++;
539 if(olets
&& *olets
&& !index(olets
, otmp
->olet
)) continue;
540 if(ckfn
&& !(*ckfn
)(otmp
)) continue;
542 pline(xprname(otmp
, ilet
));
543 addtopl(" [nyaq]? ");
553 if(--max
== 0) goto ret
;
561 pline(cnt
? "That was all." : "No applicable objects.");
566 obj_to_let(obj
) /* should of course only be called for things in invent */
567 register struct obj
*obj
;
569 register struct obj
*otmp
;
572 if(flags
.invlet_constant
)
575 for(otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
576 if(++ilet
> 'z') ilet
= 'A';
577 return(otmp
? ilet
: NOINVSYM
);
581 register struct obj
*obj
;
583 pline(xprname(obj
, obj_to_let(obj
)));
588 register struct obj
*obj
;
591 static char li
[BUFSZ
];
593 (void) sprintf(li
, "%c - %s.",
594 flags
.invlet_constant
? obj
->invlet
: let
,
605 /* called with 0 or "": all objects in inventory */
606 /* otherwise: all objects with (serial) letter in lets */
610 register struct obj
*otmp
;
615 morc
= 0; /* just to be sure */
618 pline("Not carrying anything.");
622 cornline(0, (char *) 0);
624 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
625 if(flags
.invlet_constant
) ilet
= otmp
->invlet
;
626 if(!lets
|| !*lets
|| index(lets
, ilet
)) {
627 cornline(1, xprname(otmp
, ilet
));
630 if(!flags
.invlet_constant
) if(++ilet
> 'z') ilet
= 'A';
636 dotypeinv () /* free after Robert Viduya */
637 /* Changed to one type only, so he doesnt have to type cr */
642 register struct obj
*otmp
;
643 boolean billx
= inshop() && doinvbill(0);
644 boolean unpd
= FALSE
;
646 if (!invent
&& !u
.ugold
&& !billx
) {
647 pline ("You aren't carrying anything.");
652 if(u
.ugold
) stuff
[stct
++] = '$';
654 for(otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
655 if (!index (stuff
, otmp
->olet
)) {
656 stuff
[stct
++] = otmp
->olet
;
662 if(unpd
) stuff
[stct
++] = 'u';
663 if(billx
) stuff
[stct
++] = 'x';
667 pline ("What type of object [%s] do you want an inventory of? ",
670 if(index(quitchars
,c
)) return(0);
677 if(c
== 'x' || c
== 'X') {
681 pline("No used-up objects on the shopping bill.");
685 if((c
== 'u' || c
== 'U') && !unpd
) {
686 pline("You are not carrying any unpaid objects.");
692 for (otmp
= invent
; otmp
; otmp
= otmp
-> nobj
) {
693 if(flags
.invlet_constant
) ilet
= otmp
->invlet
;
694 if (c
== otmp
-> olet
|| (c
== 'u' && otmp
-> unpaid
))
695 stuff
[stct
++] = ilet
;
696 if(!flags
.invlet_constant
) if(++ilet
> 'z') ilet
= 'A';
700 pline("You have no such objects.");
707 /* look at what is here */
709 register struct obj
*otmp
, *otmp0
;
710 register struct gold
*gold
;
711 char *verb
= Blind
? "feel" : "see";
716 pline("You try to feel what is lying here on the floor.");
717 if(Levitation
) { /* ab@unido */
718 pline("You cannot reach the floor!");
722 otmp0
= o_at(u
.ux
, u
.uy
);
723 gold
= g_at(u
.ux
, u
.uy
);
726 if(u
.uswallow
|| (!otmp0
&& !gold
)) {
727 pline("You %s no objects here.", verb
);
731 cornline(0, "Things that are here:");
732 for(otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
733 if(otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
735 cornline(1, doname(otmp
));
736 if(Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
737 pline("Touching the dead cockatrice is a fatal mistake ...");
738 pline("You die ...");
739 killer
= "dead cockatrice";
748 (void) sprintf(gbuf
, "%ld gold piece%s",
749 gold
->amount
, plur(gold
->amount
));
751 pline("You %s here %s.", verb
, gbuf
);
756 if(ct
== 1 && !gold
) {
757 pline("You %s here %s.", verb
, doname(otmp0
));
758 cornline(3, (char *) 0);
761 cornline(2, (char *) 0);
765 stackobj(obj
) register struct obj
*obj
; {
766 register struct obj
*otmp
= fobj
;
767 for(otmp
= fobj
; otmp
; otmp
= otmp
->nobj
) if(otmp
!= obj
)
768 if(otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
773 /* merge obj with otmp and delete obj if types agree */
774 merged(otmp
,obj
,lose
) register struct obj
*otmp
, *obj
; {
775 if(obj
->otyp
== otmp
->otyp
&&
776 obj
->unpaid
== otmp
->unpaid
&&
777 obj
->spe
== otmp
->spe
&&
778 obj
->dknown
== otmp
->dknown
&&
779 obj
->cursed
== otmp
->cursed
&&
780 (index("%*?!", obj
->olet
) ||
781 (obj
->known
== otmp
->known
&&
782 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
783 otmp
->quan
+= obj
->quan
;
784 otmp
->owt
+= obj
->owt
;
785 if(lose
) freeobj(obj
);
786 obfree(obj
,otmp
); /* free(obj), bill->otmp */
792 * Gold is no longer displayed; in fact, when you have a lot of money,
793 * it may take a while before you have counted it all.
794 * [Bug: d$ and pickup still tell you how much it was.]
796 extern int (*occupation
)();
798 static long goldcounted
;
801 if((goldcounted
+= 100*(u
.ulevel
+ 1)) >= u
.ugold
) {
803 if(!rn2(2)) eps
= rnd((int) (u
.ugold
/100 + 1));
804 pline("You probably have about %ld gold pieces.",
806 return(0); /* done */
808 return(1); /* continue */
813 pline("You do not carry any gold.");
814 else if(u
.ugold
<= 500)
815 pline("You are carrying %ld gold pieces.", u
.ugold
);
817 pline("You sit down in order to count your gold pieces.");
819 occupation
= countgold
;
820 occtxt
= "counting your gold";
825 /* --- end of gold counting section --- */
828 if(!uwep
) pline("You are empty handed.");
834 if(!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
835 pline("You are not wearing any armor.");
840 if(uarm
) lets
[ct
++] = obj_to_let(uarm
);
841 if(uarm2
) lets
[ct
++] = obj_to_let(uarm2
);
842 if(uarmh
) lets
[ct
++] = obj_to_let(uarmh
);
843 if(uarms
) lets
[ct
++] = obj_to_let(uarms
);
844 if(uarmg
) lets
[ct
++] = obj_to_let(uarmg
);
852 if(!uleft
&& !uright
)
853 pline("You are not wearing any rings.");
858 if(uleft
) lets
[ct
++] = obj_to_let(uleft
);
859 if(uright
) lets
[ct
++] = obj_to_let(uright
);
867 return(c
>= '0' && c
<= '9');