1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.invent.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.invent.c,v 1.4 1999/11/16 10:26:36 marcel Exp $ */
6 extern struct obj zeroobj
;
7 extern char quitchars
[];
9 static void assigninvlet(struct obj
*);
10 static struct obj
*mkgoldobj(long);
11 static bool ckunpaid(struct obj
*);
12 static char obj_to_let(struct obj
*);
13 static char *xprname(struct obj
*, char);
14 static void doinv(char *);
15 static bool merged(struct obj
*, struct obj
*, bool);
16 static bool countgold(void);
19 extern struct wseg
*wsegs
[32];
24 static int lastinvnr
= 51; /* 0 ... 51 */
27 assigninvlet(struct obj
*otmp
)
33 for (i
= 0; i
< 52; i
++)
35 for (obj
= invent
; obj
; obj
= obj
->nobj
)
38 if ('a' <= i
&& i
<= 'z')
39 inuse
[i
- 'a'] = TRUE
;
40 else if ('A' <= i
&& i
<= 'Z')
41 inuse
[i
- 'A' + 26] = TRUE
;
42 if (i
== otmp
->invlet
)
45 if ((i
= otmp
->invlet
) &&
46 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
48 for (i
= lastinvnr
+ 1; i
!= lastinvnr
; i
++) {
56 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
57 (i
< 26) ? ('a' + i
) : ('A' + i
- 26));
62 addinv(struct obj
*obj
)
66 /* merge or attach to end of chain */
71 for (otmp
= invent
; /* otmp */; otmp
= otmp
->nobj
) {
72 if (merged(otmp
, obj
, 0))
81 if (flags
.invlet_constant
) {
84 * The ordering of the chain is nowhere significant
85 * so in case you prefer some other order than the
86 * historical one, change the code below.
88 if (otmp
) { /* find proper place in chain */
90 if ((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
94 for (otmp
= invent
;; otmp
= otmp
->nobj
) {
96 (otmp
->nobj
->invlet
^ 040) >
97 (obj
->invlet
^ 040)) {
98 obj
->nobj
= otmp
->nobj
;
110 useup(struct obj
*obj
)
114 obj
->owt
= weight(obj
);
123 freeinv(struct obj
*obj
)
128 invent
= invent
->nobj
;
130 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
133 otmp
->nobj
= obj
->nobj
;
137 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
139 delobj(struct obj
*obj
)
146 /* unlink obj from chain starting with fobj */
148 freeobj(struct obj
*obj
)
155 for (otmp
= fobj
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
157 panic("error in freeobj");
158 otmp
->nobj
= obj
->nobj
;
162 /* Note: freegold throws away its argument! */
164 freegold(struct gold
*gold
)
171 for (gtmp
= fgold
; gtmp
->ngold
!= gold
; gtmp
= gtmp
->ngold
)
173 panic("error in freegold");
174 gtmp
->ngold
= gold
->ngold
;
180 deltrap(struct trap
*trap
)
185 ftrap
= ftrap
->ntrap
;
187 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
)
189 ttmp
->ntrap
= trap
->ntrap
;
194 struct wseg
*m_atseg
;
205 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
206 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
210 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
211 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
227 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
228 if (otmp
->ox
== x
&& otmp
->oy
== y
)
234 sobj_at(int n
, int x
, int y
)
238 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
239 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
245 carried(struct obj
*obj
)
249 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
260 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
261 if (otmp
->otyp
== type
)
267 o_on(unsigned int id
, struct obj
*objchn
)
270 if (objchn
->o_id
== id
)
272 objchn
= objchn
->nobj
;
280 struct trap
*trap
= ftrap
;
283 if (trap
->tx
== x
&& trap
->ty
== y
)
293 struct gold
*gold
= fgold
;
296 if (gold
->gx
== x
&& gold
->gy
== y
)
303 /* make dummy object structure containing gold - for temporary use only */
310 /* should set o_id etc. but otmp will be freed soon */
320 * struct obj *xxx: object to do something with.
321 * NULL error return: no object.
322 * &zeroobj explicitly no object (as in w-).
325 getobj(const char *let
, const char *word
)
328 char ilet
, ilet1
, ilet2
;
333 xchar allowcnt
= 0; /* 0, 1 or 2 */
334 boolean allowgold
= FALSE
;
335 boolean allowall
= FALSE
;
336 boolean allownone
= FALSE
;
343 let
++, allowgold
= TRUE
;
345 let
++, allowall
= TRUE
;
347 let
++, allownone
= TRUE
;
352 if (bp
> buf
&& bp
[-1] == '-')
356 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
357 if (!*let
|| strchr(let
, otmp
->olet
)) {
358 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
360 /* ugly check: remove inappropriate things */
361 if ((!strcmp(word
, "take off") &&
362 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
363 || (!strcmp(word
, "wear") &&
364 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
365 || (!strcmp(word
, "wield") &&
366 (otmp
->owornmask
& W_WEP
))) {
377 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
379 strcpy(lets
, bp
); /* necessary since we destroy buf */
380 if (foo
> 5) { /* compactify string */
384 while ((ilet
= bp
[++foo2
] = bp
[++foo
])) {
385 if (ilet
== ilet1
+ 1) {
386 if (ilet1
== ilet2
+ 1)
387 bp
[foo2
- 1] = ilet1
= '-';
388 else if (ilet2
== '-') {
389 bp
[--foo2
] = ++ilet1
;
397 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
398 pline("You don't have anything %sto %s.",
399 foox
? "else " : "", word
);
404 pline("What do you want to %s [*]? ", word
);
406 pline("What do you want to %s [%s or ?*]? ",
411 while (digit(ilet
) && allowcnt
) {
413 cnt
= 10 * cnt
+ (ilet
- '0');
416 allowcnt
= 2; /* signal presence of cnt */
420 pline("No count allowed with this command.");
423 if (strchr(quitchars
, ilet
))
426 return (allownone
? &zeroobj
: NULL
);
429 pline("You cannot %s gold.", word
);
432 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
434 return (mkgoldobj(cnt
));
440 /* he typed a letter (not a space) to more() */
441 } else if (ilet
== '*') {
447 if (flags
.invlet_constant
) {
448 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
449 if (otmp
->invlet
== ilet
)
452 if (ilet
>= 'A' && ilet
<= 'Z')
453 ilet
+= 'z' - 'A' + 1;
455 for (otmp
= invent
; otmp
&& ilet
;
456 ilet
--, otmp
= otmp
->nobj
)
460 pline("You don't have that object.");
463 if (cnt
< 0 || otmp
->quan
< cnt
) {
464 pline("You don't have that many! [You have %u]" ,
470 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
471 pline("That is a silly thing to %s.", word
);
474 if (allowcnt
== 2) { /* cnt given */
477 if (cnt
!= otmp
->quan
) {
479 obj
= splitobj(otmp
, (int)cnt
);
488 ckunpaid(struct obj
*otmp
)
490 return (otmp
->unpaid
);
493 /* interactive version of getobj - used for Drop and Identify */
494 /* return the number of times fn was called successfully */
496 ggetobj(const char *word
, int (*fn
)(struct obj
*), int max
)
501 int oletct
= 0, iletct
= 0;
502 boolean allflag
= FALSE
;
503 char olets
[20], ilets
[20];
504 bool (*ckfn
)(struct obj
*) = (bool (*)()) 0;
505 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
507 if (!invent
&& !allowgold
) {
508 pline("You have nothing to %s.", word
);
511 struct obj
*otmp
= invent
;
515 ilets
[iletct
++] = '$';
518 if (!strchr(ilets
, otmp
->olet
)) {
519 ilets
[iletct
++] = otmp
->olet
;
526 ilets
[iletct
++] = ' ';
528 ilets
[iletct
++] = 'u';
530 ilets
[iletct
++] = 'a';
533 pline("What kinds of thing do you want to %s? [%s] ",
536 if (buf
[0] == '\033') {
542 while ((sym
= *ip
++)) {
547 (*fn
)(mkgoldobj(u
.ugold
));
549 pline("You have no gold.");
551 } else if (sym
== 'a' || sym
== 'A')
553 else if (sym
== 'u' || sym
== 'U')
555 else if (strchr("!%?[()=*/\"0", sym
)) {
556 if (!strchr(olets
, sym
)) {
557 olets
[oletct
++] = sym
;
561 pline("You don't have any %c's.", sym
);
563 if (allowgold
== 2 && !oletct
)
564 return (1); /* he dropped gold (or at least tried to) */
566 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
570 * Walk through the chain starting at objchn and ask for all objects
571 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
572 * whether the action in question (i.e., fn) has to be performed.
573 * If allflag then no questions are asked. Max gives the max nr of
574 * objects to be treated. Return the number of objects treated.
577 askchain(struct obj
*objchn
, char *olets
, int allflag
,
578 int (*fn
)(struct obj
*), bool (*ckfn
)(struct obj
*), int max
)
580 struct obj
*otmp
, *otmp2
;
585 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
591 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
593 if (ckfn
&& !(*ckfn
)(otmp
))
596 pline("%s", xprname(otmp
, ilet
));
597 addtopl(" [nyaq]? ");
617 pline(cnt
? "That was all." : "No applicable objects.");
622 /* should of course only be called for things in invent */
624 obj_to_let(struct obj
*obj
)
629 if (flags
.invlet_constant
)
630 return (obj
->invlet
);
632 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
635 return (otmp
? ilet
: NOINVSYM
);
639 prinv(struct obj
*obj
)
641 pline("%s", xprname(obj
, obj_to_let(obj
)));
645 xprname(struct obj
*obj
, char let
)
647 static char li
[BUFSZ
];
649 sprintf(li
, "%c - %s.",
650 flags
.invlet_constant
? obj
->invlet
: let
,
662 /* called with 0 or "": all objects in inventory */
663 /* otherwise: all objects with (serial) letter in lets */
672 morc
= 0; /* just to be sure */
674 pline("Not carrying anything.");
680 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
681 if (flags
.invlet_constant
)
683 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
684 cornline(1, xprname(otmp
, ilet
));
687 if (!flags
.invlet_constant
)
696 dotypeinv(void) /* free after Robert Viduya */
698 /* Changed to one type only, so he doesnt have to type cr */
703 boolean billx
= inshop() && doinvbill(0);
704 boolean unpd
= FALSE
;
706 if (!invent
&& !u
.ugold
&& !billx
) {
707 pline("You aren't carrying anything.");
715 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
716 if (!strchr(stuff
, otmp
->olet
)) {
717 stuff
[stct
++] = otmp
->olet
;
730 pline("What type of object [%s] do you want an inventory of? ",
733 if (strchr(quitchars
, c
))
741 if (c
== 'x' || c
== 'X') {
745 pline("No used-up objects on the shopping bill.");
749 if ((c
== 'u' || c
== 'U') && !unpd
) {
750 pline("You are not carrying any unpaid objects.");
756 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
757 if (flags
.invlet_constant
)
759 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
760 stuff
[stct
++] = ilet
;
761 if (!flags
.invlet_constant
)
767 pline("You have no such objects.");
774 /* look at what is here */
778 struct obj
*otmp
, *otmp0
= NULL
;
779 struct gold
*gold
= NULL
;
780 const char *verb
= Blind
? "feel" : "see";
785 pline("You try to feel what is lying here on the floor.");
786 if (Levitation
) { /* ab@unido */
787 pline("You cannot reach the floor!");
791 otmp0
= o_at(u
.ux
, u
.uy
);
792 gold
= g_at(u
.ux
, u
.uy
);
795 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
796 pline("You %s no objects here.", verb
);
800 cornline(0, "Things that are here:");
801 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
802 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
804 cornline(1, doname(otmp
));
805 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
806 pline("Touching the dead cockatrice is a fatal mistake ...");
807 pline("You die ...");
808 killer
= "dead cockatrice";
817 sprintf(gbuf
, "%ld gold piece%s",
818 gold
->amount
, plur(gold
->amount
));
820 pline("You %s here %s.", verb
, gbuf
);
825 if (ct
== 1 && !gold
) {
826 pline("You %s here %s.", verb
, doname(otmp0
));
835 stackobj(struct obj
*obj
)
837 struct obj
*otmp
= fobj
;
839 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
841 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
842 merged(obj
, otmp
, 1))
846 /* merge obj with otmp and delete obj if types agree */
848 merged(struct obj
*otmp
, struct obj
*obj
, bool lose
)
850 if (obj
->otyp
== otmp
->otyp
&&
851 obj
->unpaid
== otmp
->unpaid
&&
852 obj
->spe
== otmp
->spe
&&
853 obj
->dknown
== otmp
->dknown
&&
854 obj
->cursed
== otmp
->cursed
&&
855 (strchr("%*?!", obj
->olet
) ||
856 (obj
->known
== otmp
->known
&&
857 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
858 otmp
->quan
+= obj
->quan
;
859 otmp
->owt
+= obj
->owt
;
862 obfree(obj
, otmp
); /* free(obj), bill->otmp */
869 * Gold is no longer displayed; in fact, when you have a lot of money,
870 * it may take a while before you have counted it all.
871 * [Bug: d$ and pickup still tell you how much it was.]
873 static long goldcounted
;
878 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
881 eps
= rnd((int)(u
.ugold
/ 100 + 1));
882 pline("You probably have about %ld gold pieces.",
884 return (0); /* done */
886 return (1); /* continue */
893 pline("You do not carry any gold.");
894 else if (u
.ugold
<= 500)
895 pline("You are carrying %ld gold pieces.", u
.ugold
);
897 pline("You sit down in order to count your gold pieces.");
899 occupation
= countgold
;
900 occtxt
= "counting your gold";
905 /* --- end of gold counting section --- */
911 pline("You are empty handed.");
920 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
921 pline("You are not wearing any armor.");
927 lets
[ct
++] = obj_to_let(uarm
);
929 lets
[ct
++] = obj_to_let(uarm2
);
931 lets
[ct
++] = obj_to_let(uarmh
);
933 lets
[ct
++] = obj_to_let(uarms
);
935 lets
[ct
++] = obj_to_let(uarmg
);
945 if (!uleft
&& !uright
)
946 pline("You are not wearing any rings.");
952 lets
[ct
++] = obj_to_let(uleft
);
954 lets
[ct
++] = obj_to_let(uright
);
964 return (c
>= '0' && c
<= '9');