]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
1 /* $NetBSD: hack.invent.c,v 1.12 2009/06/07 20:13:18 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.12 2009/06/07 20:13:18 dholland Exp $");
79 static int lastinvnr
= 51; /* 0 ... 51 */
81 static void assigninvlet(struct obj
*);
82 static char *xprname(struct obj
*, char);
85 assigninvlet(struct obj
*otmp
)
91 for (i
= 0; i
< 52; i
++)
93 for (obj
= invent
; obj
; obj
= obj
->nobj
)
96 if ('a' <= i
&& i
<= 'z')
97 inuse
[i
- 'a'] = TRUE
;
98 else if ('A' <= i
&& i
<= 'Z')
99 inuse
[i
- 'A' + 26] = TRUE
;
100 if (i
== otmp
->invlet
)
103 if ((i
= otmp
->invlet
) &&
104 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
106 for (i
= lastinvnr
+ 1; i
!= lastinvnr
; i
++) {
114 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
115 (i
< 26) ? ('a' + i
) : ('A' + i
- 26));
120 addinv(struct obj
*obj
)
124 /* merge or attach to end of chain */
129 for (otmp
= invent
; /* otmp */ ; otmp
= otmp
->nobj
) {
130 if (merged(otmp
, obj
, 0))
139 if (flags
.invlet_constant
) {
142 * The ordering of the chain is nowhere significant
143 * so in case you prefer some other order than the
144 * historical one, change the code below.
146 if (otmp
) { /* find proper place in chain */
148 if ((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
152 for (otmp
= invent
;; otmp
= otmp
->nobj
) {
154 (otmp
->nobj
->invlet
^ 040) > (obj
->invlet
^ 040)) {
155 obj
->nobj
= otmp
->nobj
;
166 useup(struct obj
*obj
)
170 obj
->owt
= weight(obj
);
174 obfree(obj
, (struct obj
*) 0);
179 freeinv(struct obj
*obj
)
184 invent
= invent
->nobj
;
186 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
189 otmp
->nobj
= obj
->nobj
;
193 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
195 delobj(struct obj
*obj
)
199 obfree(obj
, (struct obj
*) 0);
202 /* unlink obj from chain starting with fobj */
204 freeobj(struct obj
*obj
)
212 while (otmp
->nobj
!= obj
) {
213 if (otmp
->nobj
== NULL
)
214 panic("error in freeobj");
217 otmp
->nobj
= obj
->nobj
;
221 /* Note: freegold throws away its argument! */
223 freegold(struct gold
*gold
)
231 while (gtmp
->ngold
!= gold
) {
232 if (gtmp
->ngold
== NULL
)
233 panic("error in freegold");
236 gtmp
->ngold
= gold
->ngold
;
242 deltrap(struct trap
*trap
)
247 ftrap
= ftrap
->ntrap
;
249 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
);
250 ttmp
->ntrap
= trap
->ntrap
;
255 struct wseg
*m_atseg
;
266 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
267 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
271 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
272 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
287 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
288 if (otmp
->ox
== x
&& otmp
->oy
== y
)
294 sobj_at(int n
, int x
, int y
)
298 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
299 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
305 carried(struct obj
*obj
)
308 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
319 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
320 if (otmp
->otyp
== type
)
326 o_on(unsigned int id
, struct obj
*objchn
)
329 if (objchn
->o_id
== id
)
331 objchn
= objchn
->nobj
;
333 return ((struct obj
*) 0);
339 struct trap
*trap
= ftrap
;
341 if (trap
->tx
== x
&& trap
->ty
== y
)
351 struct gold
*gold
= fgold
;
353 if (gold
->gx
== x
&& gold
->gy
== y
)
360 /* make dummy object structure containing gold - for temporary use only */
367 /* should set o_id etc. but otmp will be freed soon */
377 * struct obj *xxx: object to do something with.
378 * (struct obj *) 0 error return: no object.
379 * &zeroobj explicitly no object (as in w-).
382 getobj(const char *let
, const char *word
)
385 char ilet
, ilet1
, ilet2
;
390 xchar allowcnt
= 0; /* 0, 1 or 2 */
391 boolean allowgold
= FALSE
;
392 boolean allowall
= FALSE
;
393 boolean allownone
= FALSE
;
400 let
++, allowgold
= TRUE
;
402 let
++, allowall
= TRUE
;
404 let
++, allownone
= TRUE
;
409 if (bp
> buf
&& bp
[-1] == '-')
413 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
414 if (!*let
|| strchr(let
, otmp
->olet
)) {
415 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
417 /* ugly check: remove inappropriate things */
418 if ((!strcmp(word
, "take off") &&
419 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
420 || (!strcmp(word
, "wear") &&
421 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
422 || (!strcmp(word
, "wield") &&
423 (otmp
->owornmask
& W_WEP
))) {
434 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
436 (void) strcpy(lets
, bp
);/* necessary since we destroy buf */
437 if (foo
> 5) { /* compactify string */
441 while ((ilet
= bp
[++foo2
] = bp
[++foo
]) != '\0') {
442 if (ilet
== ilet1
+ 1) {
443 if (ilet1
== ilet2
+ 1)
444 bp
[foo2
- 1] = ilet1
= '-';
445 else if (ilet2
== '-') {
446 bp
[--foo2
] = ++ilet1
;
454 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
455 pline("You don't have anything %sto %s.",
456 foox
? "else " : "", word
);
461 pline("What do you want to %s [*]? ", word
);
463 pline("What do you want to %s [%s or ?*]? ",
468 while (digit(ilet
) && allowcnt
) {
470 cnt
= 10 * cnt
+ (ilet
- '0');
473 allowcnt
= 2; /* signal presence of cnt */
477 pline("No count allowed with this command.");
480 if (strchr(quitchars
, ilet
))
481 return ((struct obj
*) 0);
483 return (allownone
? &zeroobj
: (struct obj
*) 0);
487 pline("You cannot %s gold.", word
);
490 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
492 return (mkgoldobj(cnt
));
498 /* he typed a letter (not a space) to more() */
499 } else if (ilet
== '*') {
505 if (flags
.invlet_constant
) {
506 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
507 if (otmp
->invlet
== ilet
)
510 if (ilet
>= 'A' && ilet
<= 'Z')
511 ilet
+= 'z' - 'A' + 1;
513 for (otmp
= invent
; otmp
&& ilet
;
514 ilet
--, otmp
= otmp
->nobj
);
517 pline("You don't have that object.");
520 if (cnt
< 0 || otmp
->quan
< cnt
) {
521 pline("You don't have that many! [You have %u]"
527 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
528 pline("That is a silly thing to %s.", word
);
531 if (allowcnt
== 2) { /* cnt given */
534 if (cnt
!= otmp
->quan
) {
536 obj
= splitobj(otmp
, (int) cnt
);
545 ckunpaid(struct obj
*otmp
)
547 return (otmp
->unpaid
);
550 /* interactive version of getobj - used for Drop and Identify */
551 /* return the number of times fn was called successfully */
553 ggetobj(const char *word
, int (*fn
)(struct obj
*), int max
)
558 int oletct
= 0, iletct
= 0;
559 boolean allflag
= FALSE
;
560 char olets
[20], ilets
[20];
561 int (*ckfn
)(struct obj
*) =
562 (int (*)(struct obj
*)) 0;
563 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
564 if (!invent
&& !allowgold
) {
565 pline("You have nothing to %s.", word
);
568 struct obj
*otmp
= invent
;
572 ilets
[iletct
++] = '$';
575 if (!strchr(ilets
, otmp
->olet
)) {
576 ilets
[iletct
++] = otmp
->olet
;
583 ilets
[iletct
++] = ' ';
585 ilets
[iletct
++] = 'u';
587 ilets
[iletct
++] = 'a';
590 pline("What kinds of thing do you want to %s? [%s] ",
593 if (buf
[0] == '\033') {
599 while ((sym
= *ip
++) != '\0') {
604 (*fn
) (mkgoldobj(u
.ugold
));
606 pline("You have no gold.");
608 } else if (sym
== 'a' || sym
== 'A')
610 else if (sym
== 'u' || sym
== 'U')
612 else if (strchr("!%?[()=*/\"0", sym
)) {
613 if (!strchr(olets
, sym
)) {
614 olets
[oletct
++] = sym
;
618 pline("You don't have any %c's.", sym
);
620 if (allowgold
== 2 && !oletct
)
621 return (1); /* he dropped gold (or at least tried to) */
623 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
627 * Walk through the chain starting at objchn and ask for all objects
628 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
629 * whether the action in question (i.e., fn) has to be performed.
630 * If allflag then no questions are asked. Max gives the max nr of
631 * objects to be treated. Return the number of objects treated.
634 askchain(struct obj
*objchn
, char *olets
, int allflag
,
635 int (*fn
)(struct obj
*),
636 int (*ckfn
)(struct obj
*),
639 struct obj
*otmp
, *otmp2
;
643 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
649 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
651 if (ckfn
&& !(*ckfn
) (otmp
))
654 pline(xprname(otmp
, ilet
));
655 addtopl(" [nyaq]? ");
674 pline(cnt
? "That was all." : "No applicable objects.");
679 /* should of course only be called for things in invent */
681 obj_to_let(struct obj
*obj
)
686 if (flags
.invlet_constant
)
687 return (obj
->invlet
);
689 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
692 return (otmp
? ilet
: NOINVSYM
);
696 prinv(struct obj
*obj
)
698 pline(xprname(obj
, obj_to_let(obj
)));
702 xprname(struct obj
*obj
, char let
)
704 static char li
[BUFSZ
];
706 (void) snprintf(li
, sizeof(li
), "%c - %s.",
707 flags
.invlet_constant
? obj
->invlet
: let
,
719 /* called with 0 or "": all objects in inventory */
720 /* otherwise: all objects with (serial) letter in lets */
729 morc
= 0; /* just to be sure */
732 pline("Not carrying anything.");
735 cornline(0, (char *) 0);
737 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
738 if (flags
.invlet_constant
)
740 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
741 cornline(1, xprname(otmp
, ilet
));
744 if (!flags
.invlet_constant
)
754 { /* free after Robert Viduya */
755 /* Changed to one type only, so he doesnt have to type cr */
760 boolean billx
= inshop() && doinvbill(0);
761 boolean unpd
= FALSE
;
763 if (!invent
&& !u
.ugold
&& !billx
) {
764 pline("You aren't carrying anything.");
771 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
772 if (!strchr(stuff
, otmp
->olet
)) {
773 stuff
[stct
++] = otmp
->olet
;
786 pline("What type of object [%s] do you want an inventory of? ",
789 if (strchr(quitchars
, c
))
797 if (c
== 'x' || c
== 'X') {
801 pline("No used-up objects on the shopping bill.");
804 if ((c
== 'u' || c
== 'U') && !unpd
) {
805 pline("You are not carrying any unpaid objects.");
810 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
811 if (flags
.invlet_constant
)
813 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
814 stuff
[stct
++] = ilet
;
815 if (!flags
.invlet_constant
)
821 pline("You have no such objects.");
828 /* look at what is here */
832 struct obj
*otmp
= NULL
, *otmp0
= NULL
;
833 struct gold
*gold
= NULL
;
834 const char *verb
= Blind
? "feel" : "see";
839 pline("You try to feel what is lying here on the floor.");
840 if (Levitation
) { /* ab@unido */
841 pline("You cannot reach the floor!");
845 otmp0
= o_at(u
.ux
, u
.uy
);
846 gold
= g_at(u
.ux
, u
.uy
);
848 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
849 pline("You %s no objects here.", verb
);
852 cornline(0, "Things that are here:");
853 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
854 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
856 cornline(1, doname(otmp
));
857 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
858 pline("Touching the dead cockatrice is a fatal mistake ...");
859 pline("You die ...");
860 killer
= "dead cockatrice";
869 (void) snprintf(gbuf
, sizeof(gbuf
), "%ld gold piece%s",
870 gold
->amount
, plur(gold
->amount
));
872 pline("You %s here %s.", verb
, gbuf
);
876 if (ct
== 1 && !gold
) {
877 pline("You %s here %s.", verb
, doname(otmp0
));
878 cornline(3, (char *) 0);
881 cornline(2, (char *) 0);
886 stackobj(struct obj
*obj
)
888 struct obj
*otmp
= fobj
;
889 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
891 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
892 merged(obj
, otmp
, 1))
896 /* merge obj with otmp and delete obj if types agree */
898 merged(struct obj
*otmp
, struct obj
*obj
, int lose
)
900 if (obj
->otyp
== otmp
->otyp
&&
901 obj
->unpaid
== otmp
->unpaid
&&
902 obj
->spe
== otmp
->spe
&&
903 obj
->dknown
== otmp
->dknown
&&
904 obj
->cursed
== otmp
->cursed
&&
905 (strchr("%*?!", obj
->olet
) ||
906 (obj
->known
== otmp
->known
&&
907 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
908 otmp
->quan
+= obj
->quan
;
909 otmp
->owt
+= obj
->owt
;
912 obfree(obj
, otmp
); /* free(obj), bill->otmp */
918 static long goldcounted
;
920 * Gold is no longer displayed; in fact, when you have a lot of money,
921 * it may take a while before you have counted it all.
922 * [Bug: d$ and pickup still tell you how much it was.]
927 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
930 eps
= rnd((int) (u
.ugold
/ 100 + 1));
931 pline("You probably have about %ld gold pieces.",
933 return (0); /* done */
935 return (1); /* continue */
942 pline("You do not carry any gold.");
943 else if (u
.ugold
<= 500)
944 pline("You are carrying %ld gold pieces.", u
.ugold
);
946 pline("You sit down in order to count your gold pieces.");
948 occupation
= countgold
;
949 occtxt
= "counting your gold";
954 /* --- end of gold counting section --- */
959 pline("You are empty handed.");
968 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
969 pline("You are not wearing any armor.");
975 lets
[ct
++] = obj_to_let(uarm
);
977 lets
[ct
++] = obj_to_let(uarm2
);
979 lets
[ct
++] = obj_to_let(uarmh
);
981 lets
[ct
++] = obj_to_let(uarms
);
983 lets
[ct
++] = obj_to_let(uarmg
);
993 if (!uleft
&& !uright
)
994 pline("You are not wearing any rings.");
1000 lets
[ct
++] = obj_to_let(uleft
);
1002 lets
[ct
++] = obj_to_let(uright
);
1012 return (c
>= '0' && c
<= '9');