]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
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 void assigninvlet(struct obj
*);
83 static char *xprname(struct obj
*, char);
86 assigninvlet(struct obj
*otmp
)
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));
121 addinv(struct obj
*obj
)
125 /* merge or attach to end of chain */
130 for (otmp
= invent
; /* otmp */ ; otmp
= otmp
->nobj
) {
131 if (merged(otmp
, obj
, 0))
140 if (flags
.invlet_constant
) {
143 * The ordering of the chain is nowhere significant
144 * so in case you prefer some other order than the
145 * historical one, change the code below.
147 if (otmp
) { /* find proper place in chain */
149 if ((invent
->invlet
^ 040) > (obj
->invlet
^ 040)) {
153 for (otmp
= invent
;; otmp
= otmp
->nobj
) {
155 (otmp
->nobj
->invlet
^ 040) > (obj
->invlet
^ 040)) {
156 obj
->nobj
= otmp
->nobj
;
167 useup(struct obj
*obj
)
171 obj
->owt
= weight(obj
);
175 obfree(obj
, (struct obj
*) 0);
180 freeinv(struct obj
*obj
)
185 invent
= invent
->nobj
;
187 for (otmp
= invent
; otmp
->nobj
!= obj
; otmp
= otmp
->nobj
)
190 otmp
->nobj
= obj
->nobj
;
194 /* destroy object in fobj chain (if unpaid, it remains on the bill) */
196 delobj(struct obj
*obj
)
200 obfree(obj
, (struct obj
*) 0);
203 /* unlink obj from chain starting with fobj */
205 freeobj(struct obj
*obj
)
213 while (otmp
->nobj
!= obj
) {
214 if (otmp
->nobj
== NULL
)
215 panic("error in freeobj");
218 otmp
->nobj
= obj
->nobj
;
222 /* Note: freegold throws away its argument! */
224 freegold(struct gold
*gold
)
232 while (gtmp
->ngold
!= gold
) {
233 if (gtmp
->ngold
== NULL
)
234 panic("error in freegold");
237 gtmp
->ngold
= gold
->ngold
;
243 deltrap(struct trap
*trap
)
248 ftrap
= ftrap
->ntrap
;
250 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
);
251 ttmp
->ntrap
= trap
->ntrap
;
256 struct wseg
*m_atseg
;
267 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
268 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
272 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
273 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
288 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
289 if (otmp
->ox
== x
&& otmp
->oy
== y
)
295 sobj_at(int n
, int x
, int y
)
299 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
300 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
306 carried(struct obj
*obj
)
309 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
320 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
321 if (otmp
->otyp
== type
)
327 o_on(unsigned int id
, struct obj
*objchn
)
330 if (objchn
->o_id
== id
)
332 objchn
= objchn
->nobj
;
334 return ((struct obj
*) 0);
340 struct trap
*trap
= ftrap
;
342 if (trap
->tx
== x
&& trap
->ty
== y
)
352 struct gold
*gold
= fgold
;
354 if (gold
->gx
== x
&& gold
->gy
== y
)
361 /* make dummy object structure containing gold - for temporary use only */
368 /* should set o_id etc. but otmp will be freed soon */
378 * struct obj *xxx: object to do something with.
379 * (struct obj *) 0 error return: no object.
380 * &zeroobj explicitly no object (as in w-).
383 getobj(const char *let
, const char *word
)
386 char ilet
, ilet1
, ilet2
;
391 xchar allowcnt
= 0; /* 0, 1 or 2 */
392 boolean allowgold
= FALSE
;
393 boolean allowall
= FALSE
;
394 boolean allownone
= FALSE
;
401 let
++, allowgold
= TRUE
;
403 let
++, allowall
= TRUE
;
405 let
++, allownone
= TRUE
;
410 if (bp
> buf
&& bp
[-1] == '-')
414 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
415 if (!*let
|| strchr(let
, otmp
->olet
)) {
416 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
418 /* ugly check: remove inappropriate things */
419 if ((!strcmp(word
, "take off") &&
420 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
421 || (!strcmp(word
, "wear") &&
422 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
423 || (!strcmp(word
, "wield") &&
424 (otmp
->owornmask
& W_WEP
))) {
435 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
437 (void) strcpy(lets
, bp
);/* necessary since we destroy buf */
438 if (foo
> 5) { /* compactify string */
442 while ((ilet
= bp
[++foo2
] = bp
[++foo
]) != '\0') {
443 if (ilet
== ilet1
+ 1) {
444 if (ilet1
== ilet2
+ 1)
445 bp
[foo2
- 1] = ilet1
= '-';
446 else if (ilet2
== '-') {
447 bp
[--foo2
] = ++ilet1
;
455 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
456 pline("You don't have anything %sto %s.",
457 foox
? "else " : "", word
);
462 pline("What do you want to %s [*]? ", word
);
464 pline("What do you want to %s [%s or ?*]? ",
469 while (digit(ilet
) && allowcnt
) {
471 cnt
= 10 * cnt
+ (ilet
- '0');
474 allowcnt
= 2; /* signal presence of cnt */
478 pline("No count allowed with this command.");
481 if (strchr(quitchars
, ilet
))
482 return ((struct obj
*) 0);
484 return (allownone
? &zeroobj
: (struct obj
*) 0);
488 pline("You cannot %s gold.", word
);
491 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
493 return (mkgoldobj(cnt
));
499 /* he typed a letter (not a space) to more() */
500 } else if (ilet
== '*') {
506 if (flags
.invlet_constant
) {
507 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
508 if (otmp
->invlet
== ilet
)
511 if (ilet
>= 'A' && ilet
<= 'Z')
512 ilet
+= 'z' - 'A' + 1;
514 for (otmp
= invent
; otmp
&& ilet
;
515 ilet
--, otmp
= otmp
->nobj
);
518 pline("You don't have that object.");
521 if (cnt
< 0 || otmp
->quan
< cnt
) {
522 pline("You don't have that many! [You have %u]"
528 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
529 pline("That is a silly thing to %s.", word
);
532 if (allowcnt
== 2) { /* cnt given */
535 if (cnt
!= otmp
->quan
) {
537 obj
= splitobj(otmp
, (int) cnt
);
546 ckunpaid(struct obj
*otmp
)
548 return (otmp
->unpaid
);
551 /* interactive version of getobj - used for Drop and Identify */
552 /* return the number of times fn was called successfully */
554 ggetobj(const char *word
, int (*fn
)(struct obj
*), int max
)
559 unsigned oletct
= 0, iletct
= 0;
560 boolean allflag
= FALSE
;
561 char olets
[20], ilets
[20];
562 int (*ckfn
)(struct obj
*) =
563 (int (*)(struct obj
*)) 0;
564 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
565 if (!invent
&& !allowgold
) {
566 pline("You have nothing to %s.", word
);
569 struct obj
*otmp
= invent
;
573 ilets
[iletct
++] = '$';
576 if (!strchr(ilets
, otmp
->olet
)) {
577 ilets
[iletct
++] = otmp
->olet
;
584 ilets
[iletct
++] = ' ';
586 ilets
[iletct
++] = 'u';
588 ilets
[iletct
++] = 'a';
590 assert(iletct
< sizeof(ilets
));
592 pline("What kinds of thing do you want to %s? [%s] ",
595 if (buf
[0] == '\033') {
601 while ((sym
= *ip
++) != '\0') {
606 (*fn
) (mkgoldobj(u
.ugold
));
608 pline("You have no gold.");
610 } else if (sym
== 'a' || sym
== 'A')
612 else if (sym
== 'u' || sym
== 'U')
614 else if (strchr("!%?[()=*/\"0", sym
)) {
615 if (!strchr(olets
, sym
)) {
616 olets
[oletct
++] = sym
;
619 assert(oletct
< sizeof(olets
));
621 pline("You don't have any %c's.", sym
);
623 if (allowgold
== 2 && !oletct
)
624 return (1); /* he dropped gold (or at least tried to) */
626 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
630 * Walk through the chain starting at objchn and ask for all objects
631 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
632 * whether the action in question (i.e., fn) has to be performed.
633 * If allflag then no questions are asked. Max gives the max nr of
634 * objects to be treated. Return the number of objects treated.
637 askchain(struct obj
*objchn
, char *olets
, int allflag
,
638 int (*fn
)(struct obj
*),
639 int (*ckfn
)(struct obj
*),
642 struct obj
*otmp
, *otmp2
;
646 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
652 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
654 if (ckfn
&& !(*ckfn
) (otmp
))
657 pline(xprname(otmp
, ilet
));
658 addtopl(" [nyaq]? ");
677 pline(cnt
? "That was all." : "No applicable objects.");
682 /* should of course only be called for things in invent */
684 obj_to_let(struct obj
*obj
)
689 if (flags
.invlet_constant
)
690 return (obj
->invlet
);
692 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
695 return (otmp
? ilet
: NOINVSYM
);
699 prinv(struct obj
*obj
)
701 pline(xprname(obj
, obj_to_let(obj
)));
705 xprname(struct obj
*obj
, char let
)
707 static char li
[BUFSZ
];
709 (void) snprintf(li
, sizeof(li
), "%c - %s.",
710 flags
.invlet_constant
? obj
->invlet
: let
,
722 /* called with 0 or "": all objects in inventory */
723 /* otherwise: all objects with (serial) letter in lets */
732 morc
= 0; /* just to be sure */
735 pline("Not carrying anything.");
738 cornline(0, (char *) 0);
740 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
741 if (flags
.invlet_constant
)
743 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
744 cornline(1, xprname(otmp
, ilet
));
747 if (!flags
.invlet_constant
)
752 assert(ct
< sizeof(any
));
758 { /* free after Robert Viduya */
759 /* Changed to one type only, so he doesnt have to type cr */
764 boolean billx
= inshop() && doinvbill(0);
765 boolean unpd
= FALSE
;
767 if (!invent
&& !u
.ugold
&& !billx
) {
768 pline("You aren't carrying anything.");
775 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
776 if (!strchr(stuff
, otmp
->olet
)) {
777 stuff
[stct
++] = otmp
->olet
;
788 assert(stct
< sizeof(stuff
));
791 pline("What type of object [%s] do you want an inventory of? ",
794 if (strchr(quitchars
, c
))
802 if (c
== 'x' || c
== 'X') {
806 pline("No used-up objects on the shopping bill.");
809 if ((c
== 'u' || c
== 'U') && !unpd
) {
810 pline("You are not carrying any unpaid objects.");
815 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
816 if (flags
.invlet_constant
)
818 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
819 stuff
[stct
++] = ilet
;
820 if (!flags
.invlet_constant
)
825 assert(stct
< sizeof(stuff
));
828 pline("You have no such objects.");
835 /* look at what is here */
839 struct obj
*otmp
= NULL
, *otmp0
= NULL
;
840 struct gold
*gold
= NULL
;
841 const char *verb
= Blind
? "feel" : "see";
846 pline("You try to feel what is lying here on the floor.");
847 if (Levitation
) { /* ab@unido */
848 pline("You cannot reach the floor!");
852 otmp0
= o_at(u
.ux
, u
.uy
);
853 gold
= g_at(u
.ux
, u
.uy
);
855 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
856 pline("You %s no objects here.", verb
);
859 cornline(0, "Things that are here:");
860 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
861 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
863 cornline(1, doname(otmp
));
864 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
865 pline("Touching the dead cockatrice is a fatal mistake ...");
866 pline("You die ...");
867 killer
= "dead cockatrice";
876 (void) snprintf(gbuf
, sizeof(gbuf
), "%ld gold piece%s",
877 gold
->amount
, plur(gold
->amount
));
879 pline("You %s here %s.", verb
, gbuf
);
883 if (ct
== 1 && !gold
) {
884 pline("You %s here %s.", verb
, doname(otmp0
));
885 cornline(3, (char *) 0);
888 cornline(2, (char *) 0);
893 stackobj(struct obj
*obj
)
895 struct obj
*otmp
= fobj
;
896 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
898 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
899 merged(obj
, otmp
, 1))
903 /* merge obj with otmp and delete obj if types agree */
905 merged(struct obj
*otmp
, struct obj
*obj
, int lose
)
907 if (obj
->otyp
== otmp
->otyp
&&
908 obj
->unpaid
== otmp
->unpaid
&&
909 obj
->spe
== otmp
->spe
&&
910 obj
->dknown
== otmp
->dknown
&&
911 obj
->cursed
== otmp
->cursed
&&
912 (strchr("%*?!", obj
->olet
) ||
913 (obj
->known
== otmp
->known
&&
914 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
915 otmp
->quan
+= obj
->quan
;
916 otmp
->owt
+= obj
->owt
;
919 obfree(obj
, otmp
); /* free(obj), bill->otmp */
925 static long goldcounted
;
927 * Gold is no longer displayed; in fact, when you have a lot of money,
928 * it may take a while before you have counted it all.
929 * [Bug: d$ and pickup still tell you how much it was.]
934 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
937 eps
= rnd((int) (u
.ugold
/ 100 + 1));
938 pline("You probably have about %ld gold pieces.",
940 return (0); /* done */
942 return (1); /* continue */
949 pline("You do not carry any gold.");
950 else if (u
.ugold
<= 500)
951 pline("You are carrying %ld gold pieces.", u
.ugold
);
953 pline("You sit down in order to count your gold pieces.");
955 occupation
= countgold
;
956 occtxt
= "counting your gold";
961 /* --- end of gold counting section --- */
966 pline("You are empty handed.");
975 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
976 pline("You are not wearing any armor.");
982 lets
[ct
++] = obj_to_let(uarm
);
984 lets
[ct
++] = obj_to_let(uarm2
);
986 lets
[ct
++] = obj_to_let(uarmh
);
988 lets
[ct
++] = obj_to_let(uarms
);
990 lets
[ct
++] = obj_to_let(uarmg
);
1000 if (!uleft
&& !uright
)
1001 pline("You are not wearing any rings.");
1007 lets
[ct
++] = obj_to_let(uleft
);
1009 lets
[ct
++] = obj_to_let(uright
);
1019 return (c
>= '0' && c
<= '9');