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 $ */
4 /* $DragonFly: src/games/hack/hack.invent.c,v 1.5 2006/08/21 19:45:32 pavalos Exp $ */
7 extern struct obj zeroobj
;
8 extern char quitchars
[];
10 static void assigninvlet(struct obj
*);
11 static struct obj
*mkgoldobj(long);
12 static bool ckunpaid(struct obj
*);
13 static char obj_to_let(struct obj
*);
14 static char *xprname(struct obj
*, char);
15 static void doinv(char *);
16 static bool merged(struct obj
*, struct obj
*, bool);
17 static bool countgold(void);
20 extern struct wseg
*wsegs
[32];
25 static int lastinvnr
= 51; /* 0 ... 51 */
28 assigninvlet(struct obj
*otmp
)
34 for (i
= 0; i
< 52; i
++)
36 for (obj
= invent
; obj
; obj
= obj
->nobj
)
39 if ('a' <= i
&& i
<= 'z')
40 inuse
[i
- 'a'] = TRUE
;
41 else if ('A' <= i
&& i
<= 'Z')
42 inuse
[i
- 'A' + 26] = TRUE
;
43 if (i
== otmp
->invlet
)
46 if ((i
= otmp
->invlet
) &&
47 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
49 for (i
= lastinvnr
+ 1; i
!= lastinvnr
; i
++) {
57 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
58 (i
< 26) ? ('a' + i
) : ('A' + i
- 26));
63 addinv(struct obj
*obj
)
67 /* merge or attach to end of chain */
72 for (otmp
= invent
; /* otmp */; otmp
= otmp
->nobj
) {
73 if (merged(otmp
, obj
, 0))
82 if (flags
.invlet_constant
) {
85 * The ordering of the chain is nowhere significant
86 * so in case you prefer some other order than the
87 * historical one, change the code below.
89 if (otmp
) { /* find proper place in chain */
91 if ((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
95 for (otmp
= invent
;; otmp
= otmp
->nobj
) {
97 (otmp
->nobj
->invlet
^ 040) >
98 (obj
->invlet
^ 040)) {
99 obj
->nobj
= otmp
->nobj
;
111 useup(struct obj
*obj
)
115 obj
->owt
= weight(obj
);
124 freeinv(struct obj
*obj
)
129 invent
= invent
->nobj
;
131 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
134 otmp
->nobj
= obj
->nobj
;
138 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
140 delobj(struct obj
*obj
)
147 /* unlink obj from chain starting with fobj */
149 freeobj(struct obj
*obj
)
156 for (otmp
= fobj
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
158 panic("error in freeobj");
159 otmp
->nobj
= obj
->nobj
;
163 /* Note: freegold throws away its argument! */
165 freegold(struct gold
*gold
)
172 for (gtmp
= fgold
; gtmp
->ngold
!= gold
; gtmp
= gtmp
->ngold
)
174 panic("error in freegold");
175 gtmp
->ngold
= gold
->ngold
;
181 deltrap(struct trap
*trap
)
186 ftrap
= ftrap
->ntrap
;
188 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
)
190 ttmp
->ntrap
= trap
->ntrap
;
195 struct wseg
*m_atseg
;
206 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
207 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
211 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
212 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
228 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
229 if (otmp
->ox
== x
&& otmp
->oy
== y
)
235 sobj_at(int n
, int x
, int y
)
239 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
240 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
246 carried(struct obj
*obj
)
250 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
261 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
262 if (otmp
->otyp
== type
)
268 o_on(unsigned int id
, struct obj
*objchn
)
271 if (objchn
->o_id
== id
)
273 objchn
= objchn
->nobj
;
281 struct trap
*trap
= ftrap
;
284 if (trap
->tx
== x
&& trap
->ty
== y
)
294 struct gold
*gold
= fgold
;
297 if (gold
->gx
== x
&& gold
->gy
== y
)
304 /* make dummy object structure containing gold - for temporary use only */
311 /* should set o_id etc. but otmp will be freed soon */
321 * struct obj *xxx: object to do something with.
322 * NULL error return: no object.
323 * &zeroobj explicitly no object (as in w-).
326 getobj(const char *let
, const char *word
)
329 char ilet
, ilet1
, ilet2
;
334 xchar allowcnt
= 0; /* 0, 1 or 2 */
335 boolean allowgold
= FALSE
;
336 boolean allowall
= FALSE
;
337 boolean allownone
= FALSE
;
344 let
++, allowgold
= TRUE
;
346 let
++, allowall
= TRUE
;
348 let
++, allownone
= TRUE
;
353 if (bp
> buf
&& bp
[-1] == '-')
357 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
358 if (!*let
|| strchr(let
, otmp
->olet
)) {
359 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
361 /* ugly check: remove inappropriate things */
362 if ((!strcmp(word
, "take off") &&
363 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
364 || (!strcmp(word
, "wear") &&
365 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
366 || (!strcmp(word
, "wield") &&
367 (otmp
->owornmask
& W_WEP
))) {
378 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
380 strcpy(lets
, bp
); /* necessary since we destroy buf */
381 if (foo
> 5) { /* compactify string */
385 while ((ilet
= bp
[++foo2
] = bp
[++foo
])) {
386 if (ilet
== ilet1
+ 1) {
387 if (ilet1
== ilet2
+ 1)
388 bp
[foo2
- 1] = ilet1
= '-';
389 else if (ilet2
== '-') {
390 bp
[--foo2
] = ++ilet1
;
398 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
399 pline("You don't have anything %sto %s.",
400 foox
? "else " : "", word
);
405 pline("What do you want to %s [*]? ", word
);
407 pline("What do you want to %s [%s or ?*]? ",
412 while (digit(ilet
) && allowcnt
) {
414 cnt
= 10 * cnt
+ (ilet
- '0');
417 allowcnt
= 2; /* signal presence of cnt */
421 pline("No count allowed with this command.");
424 if (strchr(quitchars
, ilet
))
427 return (allownone
? &zeroobj
: NULL
);
430 pline("You cannot %s gold.", word
);
433 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
435 return (mkgoldobj(cnt
));
441 /* he typed a letter (not a space) to more() */
442 } else if (ilet
== '*') {
448 if (flags
.invlet_constant
) {
449 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
450 if (otmp
->invlet
== ilet
)
453 if (ilet
>= 'A' && ilet
<= 'Z')
454 ilet
+= 'z' - 'A' + 1;
456 for (otmp
= invent
; otmp
&& ilet
;
457 ilet
--, otmp
= otmp
->nobj
)
461 pline("You don't have that object.");
464 if (cnt
< 0 || otmp
->quan
< cnt
) {
465 pline("You don't have that many! [You have %u]" ,
471 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
472 pline("That is a silly thing to %s.", word
);
475 if (allowcnt
== 2) { /* cnt given */
478 if (cnt
!= otmp
->quan
) {
480 obj
= splitobj(otmp
, (int)cnt
);
489 ckunpaid(struct obj
*otmp
)
491 return (otmp
->unpaid
);
494 /* interactive version of getobj - used for Drop and Identify */
495 /* return the number of times fn was called successfully */
497 ggetobj(const char *word
, int (*fn
)(struct obj
*), int max
)
502 int oletct
= 0, iletct
= 0;
503 boolean allflag
= FALSE
;
504 char olets
[20], ilets
[20];
505 bool (*ckfn
)(struct obj
*) = (bool (*)()) 0;
506 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
508 if (!invent
&& !allowgold
) {
509 pline("You have nothing to %s.", word
);
512 struct obj
*otmp
= invent
;
516 ilets
[iletct
++] = '$';
519 if (!strchr(ilets
, otmp
->olet
)) {
520 ilets
[iletct
++] = otmp
->olet
;
527 ilets
[iletct
++] = ' ';
529 ilets
[iletct
++] = 'u';
531 ilets
[iletct
++] = 'a';
534 pline("What kinds of thing do you want to %s? [%s] ",
537 if (buf
[0] == '\033') {
543 while ((sym
= *ip
++)) {
548 (*fn
)(mkgoldobj(u
.ugold
));
550 pline("You have no gold.");
552 } else if (sym
== 'a' || sym
== 'A')
554 else if (sym
== 'u' || sym
== 'U')
556 else if (strchr("!%?[()=*/\"0", sym
)) {
557 if (!strchr(olets
, sym
)) {
558 olets
[oletct
++] = sym
;
562 pline("You don't have any %c's.", sym
);
564 if (allowgold
== 2 && !oletct
)
565 return (1); /* he dropped gold (or at least tried to) */
567 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
571 * Walk through the chain starting at objchn and ask for all objects
572 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
573 * whether the action in question (i.e., fn) has to be performed.
574 * If allflag then no questions are asked. Max gives the max nr of
575 * objects to be treated. Return the number of objects treated.
578 askchain(struct obj
*objchn
, char *olets
, int allflag
,
579 int (*fn
)(struct obj
*), bool (*ckfn
)(struct obj
*), int max
)
581 struct obj
*otmp
, *otmp2
;
586 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
592 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
594 if (ckfn
&& !(*ckfn
)(otmp
))
597 pline(xprname(otmp
, ilet
));
598 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(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');