1 /* $NetBSD: hack.invent.c,v 1.13 2009/06/29 23:05:33 dholland Exp $ */
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 #include <sys/cdefs.h>
66 __RCSID("$NetBSD: hack.invent.c,v 1.13 2009/06/29 23:05:33 dholland Exp $");
80 static int lastinvnr
= 51; /* 0 ... 51 */
82 static char *xprname(struct obj
*, char);
83 static void doinv(char *);
84 static int merged(struct obj
*, struct obj
*, int);
87 assigninvlet(struct obj
*otmp
)
93 for (i
= 0; i
< 52; i
++)
95 for (obj
= invent
; obj
; obj
= obj
->nobj
)
98 if ('a' <= i
&& i
<= 'z')
99 inuse
[i
- 'a'] = TRUE
;
100 else if ('A' <= i
&& i
<= 'Z')
101 inuse
[i
- 'A' + 26] = TRUE
;
102 if (i
== otmp
->invlet
)
105 if ((i
= otmp
->invlet
) &&
106 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
108 for (i
= lastinvnr
+ 1; i
!= lastinvnr
; i
++) {
116 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
117 (i
< 26) ? ('a' + i
) : ('A' + i
- 26));
122 addinv(struct obj
*obj
)
126 /* merge or attach to end of chain */
131 for (otmp
= invent
; /* otmp */ ; otmp
= otmp
->nobj
) {
132 if (merged(otmp
, obj
, 0))
141 if (flags
.invlet_constant
) {
144 * The ordering of the chain is nowhere significant
145 * so in case you prefer some other order than the
146 * historical one, change the code below.
148 if (otmp
) { /* find proper place in chain */
150 if ((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
154 for (otmp
= invent
;; otmp
= otmp
->nobj
) {
156 (otmp
->nobj
->invlet
^ 040) > (obj
->invlet
^ 040)) {
157 obj
->nobj
= otmp
->nobj
;
168 useup(struct obj
*obj
)
172 obj
->owt
= weight(obj
);
176 obfree(obj
, (struct obj
*) 0);
181 freeinv(struct obj
*obj
)
186 invent
= invent
->nobj
;
188 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
191 otmp
->nobj
= obj
->nobj
;
195 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
197 delobj(struct obj
*obj
)
201 obfree(obj
, (struct obj
*) 0);
204 /* unlink obj from chain starting with fobj */
206 freeobj(struct obj
*obj
)
214 while (otmp
->nobj
!= obj
) {
215 if (otmp
->nobj
== NULL
)
216 panic("error in freeobj");
219 otmp
->nobj
= obj
->nobj
;
223 /* Note: freegold throws away its argument! */
225 freegold(struct gold
*gold
)
233 while (gtmp
->ngold
!= gold
) {
234 if (gtmp
->ngold
== NULL
)
235 panic("error in freegold");
238 gtmp
->ngold
= gold
->ngold
;
244 deltrap(struct trap
*trap
)
249 ftrap
= ftrap
->ntrap
;
251 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
);
252 ttmp
->ntrap
= trap
->ntrap
;
257 struct wseg
*m_atseg
;
268 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
269 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
273 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
274 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
289 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
290 if (otmp
->ox
== x
&& otmp
->oy
== y
)
296 sobj_at(int n
, int x
, int y
)
300 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
301 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
307 carried(struct obj
*obj
)
310 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
321 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
322 if (otmp
->otyp
== type
)
328 o_on(unsigned int id
, struct obj
*objchn
)
331 if (objchn
->o_id
== id
)
333 objchn
= objchn
->nobj
;
335 return ((struct obj
*) 0);
341 struct trap
*trap
= ftrap
;
343 if (trap
->tx
== x
&& trap
->ty
== y
)
353 struct gold
*gold
= fgold
;
355 if (gold
->gx
== x
&& gold
->gy
== y
)
362 /* make dummy object structure containing gold - for temporary use only */
369 /* should set o_id etc. but otmp will be freed soon */
379 * struct obj *xxx: object to do something with.
380 * (struct obj *) 0 error return: no object.
381 * &zeroobj explicitly no object (as in w-).
384 getobj(const char *let
, const char *word
)
387 char ilet
, ilet1
, ilet2
;
392 xchar allowcnt
= 0; /* 0, 1 or 2 */
393 boolean allowgold
= FALSE
;
394 boolean allowall
= FALSE
;
395 boolean allownone
= FALSE
;
402 let
++, allowgold
= TRUE
;
404 let
++, allowall
= TRUE
;
406 let
++, allownone
= TRUE
;
411 if (bp
> buf
&& bp
[-1] == '-')
415 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
416 if (!*let
|| strchr(let
, otmp
->olet
)) {
417 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
419 /* ugly check: remove inappropriate things */
420 if ((!strcmp(word
, "take off") &&
421 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
422 || (!strcmp(word
, "wear") &&
423 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
424 || (!strcmp(word
, "wield") &&
425 (otmp
->owornmask
& W_WEP
))) {
436 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
438 (void) strcpy(lets
, bp
);/* necessary since we destroy buf */
439 if (foo
> 5) { /* compactify string */
443 while ((ilet
= bp
[++foo2
] = bp
[++foo
]) != '\0') {
444 if (ilet
== ilet1
+ 1) {
445 if (ilet1
== ilet2
+ 1)
446 bp
[foo2
- 1] = ilet1
= '-';
447 else if (ilet2
== '-') {
448 bp
[--foo2
] = ++ilet1
;
456 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
457 pline("You don't have anything %sto %s.",
458 foox
? "else " : "", word
);
463 pline("What do you want to %s [*]? ", word
);
465 pline("What do you want to %s [%s or ?*]? ",
470 while (digit(ilet
) && allowcnt
) {
472 cnt
= 10 * cnt
+ (ilet
- '0');
475 allowcnt
= 2; /* signal presence of cnt */
479 pline("No count allowed with this command.");
482 if (strchr(quitchars
, ilet
))
483 return ((struct obj
*) 0);
485 return (allownone
? &zeroobj
: (struct obj
*) 0);
489 pline("You cannot %s gold.", word
);
492 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
494 return (mkgoldobj(cnt
));
500 /* he typed a letter (not a space) to more() */
501 } else if (ilet
== '*') {
507 if (flags
.invlet_constant
) {
508 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
509 if (otmp
->invlet
== ilet
)
512 if (ilet
>= 'A' && ilet
<= 'Z')
513 ilet
+= 'z' - 'A' + 1;
515 for (otmp
= invent
; otmp
&& ilet
;
516 ilet
--, otmp
= otmp
->nobj
);
519 pline("You don't have that object.");
522 if (cnt
< 0 || otmp
->quan
< cnt
) {
523 pline("You don't have that many! [You have %u]"
529 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
530 pline("That is a silly thing to %s.", word
);
533 if (allowcnt
== 2) { /* cnt given */
536 if (cnt
!= otmp
->quan
) {
538 obj
= splitobj(otmp
, (int) cnt
);
547 ckunpaid(struct obj
*otmp
)
549 return (otmp
->unpaid
);
552 /* interactive version of getobj - used for Drop and Identify */
553 /* return the number of times fn was called successfully */
555 ggetobj(const char *word
, int (*fn
)(struct obj
*), int max
)
560 unsigned oletct
= 0, iletct
= 0;
561 boolean allflag
= FALSE
;
562 char olets
[20], ilets
[20];
563 int (*ckfn
)(struct obj
*) =
564 (int (*)(struct obj
*)) 0;
565 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
566 if (!invent
&& !allowgold
) {
567 pline("You have nothing to %s.", word
);
570 struct obj
*otmp
= invent
;
574 ilets
[iletct
++] = '$';
577 if (!strchr(ilets
, otmp
->olet
)) {
578 ilets
[iletct
++] = otmp
->olet
;
585 ilets
[iletct
++] = ' ';
587 ilets
[iletct
++] = 'u';
589 ilets
[iletct
++] = 'a';
591 assert(iletct
< sizeof(ilets
));
593 pline("What kinds of thing do you want to %s? [%s] ",
596 if (buf
[0] == '\033') {
602 while ((sym
= *ip
++) != '\0') {
607 (*fn
) (mkgoldobj(u
.ugold
));
609 pline("You have no gold.");
611 } else if (sym
== 'a' || sym
== 'A')
613 else if (sym
== 'u' || sym
== 'U')
615 else if (strchr("!%?[()=*/\"0", sym
)) {
616 if (!strchr(olets
, sym
)) {
617 olets
[oletct
++] = sym
;
620 assert(oletct
< sizeof(olets
));
622 pline("You don't have any %c's.", sym
);
624 if (allowgold
== 2 && !oletct
)
625 return (1); /* he dropped gold (or at least tried to) */
627 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
631 * Walk through the chain starting at objchn and ask for all objects
632 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
633 * whether the action in question (i.e., fn) has to be performed.
634 * If allflag then no questions are asked. Max gives the max nr of
635 * objects to be treated. Return the number of objects treated.
638 askchain(struct obj
*objchn
, char *olets
, int allflag
,
639 int (*fn
)(struct obj
*),
640 int (*ckfn
)(struct obj
*),
643 struct obj
*otmp
, *otmp2
;
647 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
653 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
655 if (ckfn
&& !(*ckfn
) (otmp
))
658 pline(xprname(otmp
, ilet
));
659 addtopl(" [nyaq]? ");
678 pline(cnt
? "That was all." : "No applicable objects.");
683 /* should of course only be called for things in invent */
685 obj_to_let(struct obj
*obj
)
690 if (flags
.invlet_constant
)
691 return (obj
->invlet
);
693 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
696 return (otmp
? ilet
: NOINVSYM
);
700 prinv(struct obj
*obj
)
702 pline(xprname(obj
, obj_to_let(obj
)));
706 xprname(struct obj
*obj
, char let
)
708 static char li
[BUFSZ
];
710 (void) snprintf(li
, sizeof(li
), "%c - %s.",
711 flags
.invlet_constant
? obj
->invlet
: let
,
723 /* called with 0 or "": all objects in inventory */
724 /* otherwise: all objects with (serial) letter in lets */
733 morc
= 0; /* just to be sure */
736 pline("Not carrying anything.");
739 cornline(0, (char *) 0);
741 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
742 if (flags
.invlet_constant
)
744 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
745 cornline(1, xprname(otmp
, ilet
));
748 if (!flags
.invlet_constant
)
753 assert(ct
< sizeof(any
));
759 { /* free after Robert Viduya */
760 /* Changed to one type only, so he doesnt have to type cr */
765 boolean billx
= inshop() && doinvbill(0);
766 boolean unpd
= FALSE
;
768 if (!invent
&& !u
.ugold
&& !billx
) {
769 pline("You aren't carrying anything.");
776 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
777 if (!strchr(stuff
, otmp
->olet
)) {
778 stuff
[stct
++] = otmp
->olet
;
789 assert(stct
< sizeof(stuff
));
792 pline("What type of object [%s] do you want an inventory of? ",
795 if (strchr(quitchars
, c
))
803 if (c
== 'x' || c
== 'X') {
807 pline("No used-up objects on the shopping bill.");
810 if ((c
== 'u' || c
== 'U') && !unpd
) {
811 pline("You are not carrying any unpaid objects.");
816 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
817 if (flags
.invlet_constant
)
819 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
820 stuff
[stct
++] = ilet
;
821 if (!flags
.invlet_constant
)
826 assert(stct
< sizeof(stuff
));
829 pline("You have no such objects.");
836 /* look at what is here */
840 struct obj
*otmp
= NULL
, *otmp0
= NULL
;
841 struct gold
*gold
= NULL
;
842 const char *verb
= Blind
? "feel" : "see";
847 pline("You try to feel what is lying here on the floor.");
848 if (Levitation
) { /* ab@unido */
849 pline("You cannot reach the floor!");
853 otmp0
= o_at(u
.ux
, u
.uy
);
854 gold
= g_at(u
.ux
, u
.uy
);
856 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
857 pline("You %s no objects here.", verb
);
860 cornline(0, "Things that are here:");
861 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
862 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
864 cornline(1, doname(otmp
));
865 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
866 pline("Touching the dead cockatrice is a fatal mistake ...");
867 pline("You die ...");
868 killer
= "dead cockatrice";
877 (void) snprintf(gbuf
, sizeof(gbuf
), "%ld gold piece%s",
878 gold
->amount
, plur(gold
->amount
));
880 pline("You %s here %s.", verb
, gbuf
);
884 if (ct
== 1 && !gold
) {
885 pline("You %s here %s.", verb
, doname(otmp0
));
886 cornline(3, (char *) 0);
889 cornline(2, (char *) 0);
894 stackobj(struct obj
*obj
)
896 struct obj
*otmp
= fobj
;
897 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
899 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
900 merged(obj
, otmp
, 1))
904 /* merge obj with otmp and delete obj if types agree */
906 merged(struct obj
*otmp
, struct obj
*obj
, int lose
)
908 if (obj
->otyp
== otmp
->otyp
&&
909 obj
->unpaid
== otmp
->unpaid
&&
910 obj
->spe
== otmp
->spe
&&
911 obj
->dknown
== otmp
->dknown
&&
912 obj
->cursed
== otmp
->cursed
&&
913 (strchr("%*?!", obj
->olet
) ||
914 (obj
->known
== otmp
->known
&&
915 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
916 otmp
->quan
+= obj
->quan
;
917 otmp
->owt
+= obj
->owt
;
920 obfree(obj
, otmp
); /* free(obj), bill->otmp */
926 static long goldcounted
;
928 * Gold is no longer displayed; in fact, when you have a lot of money,
929 * it may take a while before you have counted it all.
930 * [Bug: d$ and pickup still tell you how much it was.]
935 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
938 eps
= rnd((int) (u
.ugold
/ 100 + 1));
939 pline("You probably have about %ld gold pieces.",
941 return (0); /* done */
943 return (1); /* continue */
950 pline("You do not carry any gold.");
951 else if (u
.ugold
<= 500)
952 pline("You are carrying %ld gold pieces.", u
.ugold
);
954 pline("You sit down in order to count your gold pieces.");
956 occupation
= countgold
;
957 occtxt
= "counting your gold";
962 /* --- end of gold counting section --- */
967 pline("You are empty handed.");
976 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
977 pline("You are not wearing any armor.");
983 lets
[ct
++] = obj_to_let(uarm
);
985 lets
[ct
++] = obj_to_let(uarm2
);
987 lets
[ct
++] = obj_to_let(uarmh
);
989 lets
[ct
++] = obj_to_let(uarms
);
991 lets
[ct
++] = obj_to_let(uarmg
);
1001 if (!uleft
&& !uright
)
1002 pline("You are not wearing any rings.");
1008 lets
[ct
++] = obj_to_let(uleft
);
1010 lets
[ct
++] = obj_to_let(uright
);
1020 return (c
>= '0' && c
<= '9');