]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
1 /* $NetBSD: hack.invent.c,v 1.8 2003/04/02 18:36:37 jsm 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.8 2003/04/02 18:36:37 jsm Exp $");
79 static int lastinvnr
= 51; /* 0 ... 51 */
81 static void assigninvlet
__P((struct obj
*));
82 static char *xprname
__P((struct obj
*, char));
92 for (i
= 0; i
< 52; i
++)
94 for (obj
= invent
; obj
; obj
= obj
->nobj
)
97 if ('a' <= i
&& i
<= 'z')
98 inuse
[i
- 'a'] = TRUE
;
99 else if ('A' <= i
&& i
<= 'Z')
100 inuse
[i
- 'A' + 26] = TRUE
;
101 if (i
== otmp
->invlet
)
104 if ((i
= otmp
->invlet
) &&
105 (('a' <= i
&& i
<= 'z') || ('A' <= i
&& i
<= 'Z')))
107 for (i
= lastinvnr
+ 1; i
!= lastinvnr
; i
++) {
115 otmp
->invlet
= (inuse
[i
] ? NOINVSYM
:
116 (i
< 26) ? ('a' + i
) : ('A' + i
- 26));
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
;
173 obj
->owt
= weight(obj
);
177 obfree(obj
, (struct obj
*) 0);
188 invent
= invent
->nobj
;
190 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
193 otmp
->nobj
= obj
->nobj
;
197 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
204 obfree(obj
, (struct obj
*) 0);
207 /* unlink obj from chain starting with fobj */
217 for (otmp
= fobj
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
219 panic("error in freeobj");
220 otmp
->nobj
= obj
->nobj
;
224 /* Note: freegold throws away its argument! */
234 for (gtmp
= fgold
; gtmp
->ngold
!= gold
; gtmp
= gtmp
->ngold
)
236 panic("error in freegold");
237 gtmp
->ngold
= gold
->ngold
;
249 ftrap
= ftrap
->ntrap
;
251 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
);
252 ttmp
->ntrap
= trap
->ntrap
;
257 struct wseg
*m_atseg
;
269 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
270 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
274 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
275 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
291 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
292 if (otmp
->ox
== x
&& otmp
->oy
== y
)
303 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
304 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
314 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
326 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
327 if (otmp
->otyp
== type
)
338 if (objchn
->o_id
== id
)
340 objchn
= objchn
->nobj
;
342 return ((struct obj
*) 0);
349 struct trap
*trap
= ftrap
;
351 if (trap
->tx
== x
&& trap
->ty
== y
)
362 struct gold
*gold
= fgold
;
364 if (gold
->gx
== x
&& gold
->gy
== y
)
371 /* make dummy object structure containing gold - for temporary use only */
379 /* should set o_id etc. but otmp will be freed soon */
389 * struct obj *xxx: object to do something with.
390 * (struct obj *) 0 error return: no object.
391 * &zeroobj explicitly no object (as in w-).
395 const char *let
, *word
;
398 char ilet
, ilet1
, ilet2
;
403 xchar allowcnt
= 0; /* 0, 1 or 2 */
404 boolean allowgold
= FALSE
;
405 boolean allowall
= FALSE
;
406 boolean allownone
= FALSE
;
413 let
++, allowgold
= TRUE
;
415 let
++, allowall
= TRUE
;
417 let
++, allownone
= TRUE
;
422 if (bp
> buf
&& bp
[-1] == '-')
426 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
427 if (!*let
|| strchr(let
, otmp
->olet
)) {
428 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
430 /* ugly check: remove inappropriate things */
431 if ((!strcmp(word
, "take off") &&
432 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
433 || (!strcmp(word
, "wear") &&
434 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
435 || (!strcmp(word
, "wield") &&
436 (otmp
->owornmask
& W_WEP
))) {
447 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
449 (void) strcpy(lets
, bp
);/* necessary since we destroy buf */
450 if (foo
> 5) { /* compactify string */
454 while ((ilet
= bp
[++foo2
] = bp
[++foo
]) != '\0') {
455 if (ilet
== ilet1
+ 1) {
456 if (ilet1
== ilet2
+ 1)
457 bp
[foo2
- 1] = ilet1
= '-';
458 else if (ilet2
== '-') {
459 bp
[--foo2
] = ++ilet1
;
467 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
468 pline("You don't have anything %sto %s.",
469 foox
? "else " : "", word
);
474 pline("What do you want to %s [*]? ", word
);
476 pline("What do you want to %s [%s or ?*]? ",
481 while (digit(ilet
) && allowcnt
) {
483 cnt
= 10 * cnt
+ (ilet
- '0');
486 allowcnt
= 2; /* signal presence of cnt */
490 pline("No count allowed with this command.");
493 if (strchr(quitchars
, ilet
))
494 return ((struct obj
*) 0);
496 return (allownone
? &zeroobj
: (struct obj
*) 0);
500 pline("You cannot %s gold.", word
);
503 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
505 return (mkgoldobj(cnt
));
511 /* he typed a letter (not a space) to more() */
512 } else if (ilet
== '*') {
518 if (flags
.invlet_constant
) {
519 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
520 if (otmp
->invlet
== ilet
)
523 if (ilet
>= 'A' && ilet
<= 'Z')
524 ilet
+= 'z' - 'A' + 1;
526 for (otmp
= invent
; otmp
&& ilet
;
527 ilet
--, otmp
= otmp
->nobj
);
530 pline("You don't have that object.");
533 if (cnt
< 0 || otmp
->quan
< cnt
) {
534 pline("You don't have that many! [You have %u]"
540 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
541 pline("That is a silly thing to %s.", word
);
544 if (allowcnt
== 2) { /* cnt given */
547 if (cnt
!= otmp
->quan
) {
549 obj
= splitobj(otmp
, (int) cnt
);
561 return (otmp
->unpaid
);
564 /* interactive version of getobj - used for Drop and Identify */
565 /* return the number of times fn was called successfully */
567 ggetobj(word
, fn
, max
)
569 int (*fn
) __P((struct obj
*));
575 int oletct
= 0, iletct
= 0;
576 boolean allflag
= FALSE
;
577 char olets
[20], ilets
[20];
578 int (*ckfn
) __P((struct obj
*)) =
579 (int (*) __P((struct obj
*))) 0;
580 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
581 if (!invent
&& !allowgold
) {
582 pline("You have nothing to %s.", word
);
585 struct obj
*otmp
= invent
;
589 ilets
[iletct
++] = '$';
592 if (!strchr(ilets
, otmp
->olet
)) {
593 ilets
[iletct
++] = otmp
->olet
;
600 ilets
[iletct
++] = ' ';
602 ilets
[iletct
++] = 'u';
604 ilets
[iletct
++] = 'a';
607 pline("What kinds of thing do you want to %s? [%s] ",
610 if (buf
[0] == '\033') {
616 while ((sym
= *ip
++) != '\0') {
621 (*fn
) (mkgoldobj(u
.ugold
));
623 pline("You have no gold.");
625 } else if (sym
== 'a' || sym
== 'A')
627 else if (sym
== 'u' || sym
== 'U')
629 else if (strchr("!%?[()=*/\"0", sym
)) {
630 if (!strchr(olets
, sym
)) {
631 olets
[oletct
++] = sym
;
635 pline("You don't have any %c's.", sym
);
637 if (allowgold
== 2 && !oletct
)
638 return (1); /* he dropped gold (or at least tried to) */
640 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
644 * Walk through the chain starting at objchn and ask for all objects
645 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
646 * whether the action in question (i.e., fn) has to be performed.
647 * If allflag then no questions are asked. Max gives the max nr of
648 * objects to be treated. Return the number of objects treated.
651 askchain(objchn
, olets
, allflag
, fn
, ckfn
, max
)
655 int (*fn
) __P((struct obj
*));
656 int (*ckfn
) __P((struct obj
*));
659 struct obj
*otmp
, *otmp2
;
663 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
669 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
671 if (ckfn
&& !(*ckfn
) (otmp
))
674 pline(xprname(otmp
, ilet
));
675 addtopl(" [nyaq]? ");
694 pline(cnt
? "That was all." : "No applicable objects.");
700 obj_to_let(obj
) /* should of course only be called for things
707 if (flags
.invlet_constant
)
708 return (obj
->invlet
);
710 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
713 return (otmp
? ilet
: NOINVSYM
);
720 pline(xprname(obj
, obj_to_let(obj
)));
728 static char li
[BUFSZ
];
730 (void) sprintf(li
, "%c - %s.",
731 flags
.invlet_constant
? obj
->invlet
: let
,
743 /* called with 0 or "": all objects in inventory */
744 /* otherwise: all objects with (serial) letter in lets */
754 morc
= 0; /* just to be sure */
757 pline("Not carrying anything.");
760 cornline(0, (char *) 0);
762 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
763 if (flags
.invlet_constant
)
765 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
766 cornline(1, xprname(otmp
, ilet
));
769 if (!flags
.invlet_constant
)
779 { /* free after Robert Viduya */
780 /* Changed to one type only, so he doesnt have to type cr */
785 boolean billx
= inshop() && doinvbill(0);
786 boolean unpd
= FALSE
;
788 if (!invent
&& !u
.ugold
&& !billx
) {
789 pline("You aren't carrying anything.");
796 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
797 if (!strchr(stuff
, otmp
->olet
)) {
798 stuff
[stct
++] = otmp
->olet
;
811 pline("What type of object [%s] do you want an inventory of? ",
814 if (strchr(quitchars
, c
))
822 if (c
== 'x' || c
== 'X') {
826 pline("No used-up objects on the shopping bill.");
829 if ((c
== 'u' || c
== 'U') && !unpd
) {
830 pline("You are not carrying any unpaid objects.");
835 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
836 if (flags
.invlet_constant
)
838 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
839 stuff
[stct
++] = ilet
;
840 if (!flags
.invlet_constant
)
846 pline("You have no such objects.");
853 /* look at what is here */
857 struct obj
*otmp
= NULL
, *otmp0
= NULL
;
858 struct gold
*gold
= NULL
;
859 const char *verb
= Blind
? "feel" : "see";
864 pline("You try to feel what is lying here on the floor.");
865 if (Levitation
) { /* ab@unido */
866 pline("You cannot reach the floor!");
870 otmp0
= o_at(u
.ux
, u
.uy
);
871 gold
= g_at(u
.ux
, u
.uy
);
873 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
874 pline("You %s no objects here.", verb
);
877 cornline(0, "Things that are here:");
878 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
879 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
881 cornline(1, doname(otmp
));
882 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
883 pline("Touching the dead cockatrice is a fatal mistake ...");
884 pline("You die ...");
885 killer
= "dead cockatrice";
894 (void) sprintf(gbuf
, "%ld gold piece%s",
895 gold
->amount
, plur(gold
->amount
));
897 pline("You %s here %s.", verb
, gbuf
);
901 if (ct
== 1 && !gold
) {
902 pline("You %s here %s.", verb
, doname(otmp0
));
903 cornline(3, (char *) 0);
906 cornline(2, (char *) 0);
914 struct obj
*otmp
= fobj
;
915 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
917 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
918 merged(obj
, otmp
, 1))
922 /* merge obj with otmp and delete obj if types agree */
924 merged(otmp
, obj
, lose
)
925 struct obj
*otmp
, *obj
;
928 if (obj
->otyp
== otmp
->otyp
&&
929 obj
->unpaid
== otmp
->unpaid
&&
930 obj
->spe
== otmp
->spe
&&
931 obj
->dknown
== otmp
->dknown
&&
932 obj
->cursed
== otmp
->cursed
&&
933 (strchr("%*?!", obj
->olet
) ||
934 (obj
->known
== otmp
->known
&&
935 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
936 otmp
->quan
+= obj
->quan
;
937 otmp
->owt
+= obj
->owt
;
940 obfree(obj
, otmp
); /* free(obj), bill->otmp */
946 static long goldcounted
;
948 * Gold is no longer displayed; in fact, when you have a lot of money,
949 * it may take a while before you have counted it all.
950 * [Bug: d$ and pickup still tell you how much it was.]
955 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
958 eps
= rnd((int) (u
.ugold
/ 100 + 1));
959 pline("You probably have about %ld gold pieces.",
961 return (0); /* done */
963 return (1); /* continue */
970 pline("You do not carry any gold.");
971 else if (u
.ugold
<= 500)
972 pline("You are carrying %ld gold pieces.", u
.ugold
);
974 pline("You sit down in order to count your gold pieces.");
976 occupation
= countgold
;
977 occtxt
= "counting your gold";
982 /* --- end of gold counting section --- */
987 pline("You are empty handed.");
996 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
997 pline("You are not wearing any armor.");
1003 lets
[ct
++] = obj_to_let(uarm
);
1005 lets
[ct
++] = obj_to_let(uarm2
);
1007 lets
[ct
++] = obj_to_let(uarmh
);
1009 lets
[ct
++] = obj_to_let(uarms
);
1011 lets
[ct
++] = obj_to_let(uarmg
);
1021 if (!uleft
&& !uright
)
1022 pline("You are not wearing any rings.");
1028 lets
[ct
++] = obj_to_let(uleft
);
1030 lets
[ct
++] = obj_to_let(uright
);
1041 return (c
>= '0' && c
<= '9');