]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.invent.c
1 /* $NetBSD: hack.invent.c,v 1.10 2006/03/29 01:19:51 jnemeth 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.10 2006/03/29 01:19:51 jnemeth Exp $");
79 static int lastinvnr
= 51; /* 0 ... 51 */
81 static void assigninvlet(struct obj
*);
82 static char *xprname(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 */
218 while (otmp
->nobj
!= obj
) {
219 if (otmp
->nobj
== NULL
)
220 panic("error in freeobj");
223 otmp
->nobj
= obj
->nobj
;
227 /* Note: freegold throws away its argument! */
238 while (gtmp
->ngold
!= gold
) {
239 if (gtmp
->ngold
== NULL
)
240 panic("error in freegold");
243 gtmp
->ngold
= gold
->ngold
;
255 ftrap
= ftrap
->ntrap
;
257 for (ttmp
= ftrap
; ttmp
->ntrap
!= trap
; ttmp
= ttmp
->ntrap
);
258 ttmp
->ntrap
= trap
->ntrap
;
263 struct wseg
*m_atseg
;
275 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
276 if (mtmp
->mx
== x
&& mtmp
->my
== y
)
280 for (wtmp
= wsegs
[mtmp
->wormno
]; wtmp
; wtmp
= wtmp
->nseg
)
281 if (wtmp
->wx
== x
&& wtmp
->wy
== y
) {
297 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
298 if (otmp
->ox
== x
&& otmp
->oy
== y
)
309 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
310 if (otmp
->ox
== x
&& otmp
->oy
== y
&& otmp
->otyp
== n
)
320 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
332 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
333 if (otmp
->otyp
== type
)
344 if (objchn
->o_id
== id
)
346 objchn
= objchn
->nobj
;
348 return ((struct obj
*) 0);
355 struct trap
*trap
= ftrap
;
357 if (trap
->tx
== x
&& trap
->ty
== y
)
368 struct gold
*gold
= fgold
;
370 if (gold
->gx
== x
&& gold
->gy
== y
)
377 /* make dummy object structure containing gold - for temporary use only */
385 /* should set o_id etc. but otmp will be freed soon */
395 * struct obj *xxx: object to do something with.
396 * (struct obj *) 0 error return: no object.
397 * &zeroobj explicitly no object (as in w-).
401 const char *let
, *word
;
404 char ilet
, ilet1
, ilet2
;
409 xchar allowcnt
= 0; /* 0, 1 or 2 */
410 boolean allowgold
= FALSE
;
411 boolean allowall
= FALSE
;
412 boolean allownone
= FALSE
;
419 let
++, allowgold
= TRUE
;
421 let
++, allowall
= TRUE
;
423 let
++, allownone
= TRUE
;
428 if (bp
> buf
&& bp
[-1] == '-')
432 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
433 if (!*let
|| strchr(let
, otmp
->olet
)) {
434 bp
[foo
++] = flags
.invlet_constant
? otmp
->invlet
: ilet
;
436 /* ugly check: remove inappropriate things */
437 if ((!strcmp(word
, "take off") &&
438 !(otmp
->owornmask
& (W_ARMOR
- W_ARM2
)))
439 || (!strcmp(word
, "wear") &&
440 (otmp
->owornmask
& (W_ARMOR
| W_RING
)))
441 || (!strcmp(word
, "wield") &&
442 (otmp
->owornmask
& W_WEP
))) {
453 if (foo
== 0 && bp
> buf
&& bp
[-1] == ' ')
455 (void) strcpy(lets
, bp
);/* necessary since we destroy buf */
456 if (foo
> 5) { /* compactify string */
460 while ((ilet
= bp
[++foo2
] = bp
[++foo
]) != '\0') {
461 if (ilet
== ilet1
+ 1) {
462 if (ilet1
== ilet2
+ 1)
463 bp
[foo2
- 1] = ilet1
= '-';
464 else if (ilet2
== '-') {
465 bp
[--foo2
] = ++ilet1
;
473 if (!foo
&& !allowall
&& !allowgold
&& !allownone
) {
474 pline("You don't have anything %sto %s.",
475 foox
? "else " : "", word
);
480 pline("What do you want to %s [*]? ", word
);
482 pline("What do you want to %s [%s or ?*]? ",
487 while (digit(ilet
) && allowcnt
) {
489 cnt
= 10 * cnt
+ (ilet
- '0');
492 allowcnt
= 2; /* signal presence of cnt */
496 pline("No count allowed with this command.");
499 if (strchr(quitchars
, ilet
))
500 return ((struct obj
*) 0);
502 return (allownone
? &zeroobj
: (struct obj
*) 0);
506 pline("You cannot %s gold.", word
);
509 if (!(allowcnt
== 2 && cnt
< u
.ugold
))
511 return (mkgoldobj(cnt
));
517 /* he typed a letter (not a space) to more() */
518 } else if (ilet
== '*') {
524 if (flags
.invlet_constant
) {
525 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
526 if (otmp
->invlet
== ilet
)
529 if (ilet
>= 'A' && ilet
<= 'Z')
530 ilet
+= 'z' - 'A' + 1;
532 for (otmp
= invent
; otmp
&& ilet
;
533 ilet
--, otmp
= otmp
->nobj
);
536 pline("You don't have that object.");
539 if (cnt
< 0 || otmp
->quan
< cnt
) {
540 pline("You don't have that many! [You have %u]"
546 if (!allowall
&& let
&& !strchr(let
, otmp
->olet
)) {
547 pline("That is a silly thing to %s.", word
);
550 if (allowcnt
== 2) { /* cnt given */
553 if (cnt
!= otmp
->quan
) {
555 obj
= splitobj(otmp
, (int) cnt
);
567 return (otmp
->unpaid
);
570 /* interactive version of getobj - used for Drop and Identify */
571 /* return the number of times fn was called successfully */
573 ggetobj(word
, fn
, max
)
575 int (*fn
)(struct obj
*);
581 int oletct
= 0, iletct
= 0;
582 boolean allflag
= FALSE
;
583 char olets
[20], ilets
[20];
584 int (*ckfn
)(struct obj
*) =
585 (int (*)(struct obj
*)) 0;
586 xchar allowgold
= (u
.ugold
&& !strcmp(word
, "drop")) ? 1 : 0; /* BAH */
587 if (!invent
&& !allowgold
) {
588 pline("You have nothing to %s.", word
);
591 struct obj
*otmp
= invent
;
595 ilets
[iletct
++] = '$';
598 if (!strchr(ilets
, otmp
->olet
)) {
599 ilets
[iletct
++] = otmp
->olet
;
606 ilets
[iletct
++] = ' ';
608 ilets
[iletct
++] = 'u';
610 ilets
[iletct
++] = 'a';
613 pline("What kinds of thing do you want to %s? [%s] ",
616 if (buf
[0] == '\033') {
622 while ((sym
= *ip
++) != '\0') {
627 (*fn
) (mkgoldobj(u
.ugold
));
629 pline("You have no gold.");
631 } else if (sym
== 'a' || sym
== 'A')
633 else if (sym
== 'u' || sym
== 'U')
635 else if (strchr("!%?[()=*/\"0", sym
)) {
636 if (!strchr(olets
, sym
)) {
637 olets
[oletct
++] = sym
;
641 pline("You don't have any %c's.", sym
);
643 if (allowgold
== 2 && !oletct
)
644 return (1); /* he dropped gold (or at least tried to) */
646 return (askchain(invent
, olets
, allflag
, fn
, ckfn
, max
));
650 * Walk through the chain starting at objchn and ask for all objects
651 * with olet in olets (if nonNULL) and satisfying ckfn (if nonNULL)
652 * whether the action in question (i.e., fn) has to be performed.
653 * If allflag then no questions are asked. Max gives the max nr of
654 * objects to be treated. Return the number of objects treated.
657 askchain(objchn
, olets
, allflag
, fn
, ckfn
, max
)
661 int (*fn
)(struct obj
*);
662 int (*ckfn
)(struct obj
*);
665 struct obj
*otmp
, *otmp2
;
669 for (otmp
= objchn
; otmp
; otmp
= otmp2
) {
675 if (olets
&& *olets
&& !strchr(olets
, otmp
->olet
))
677 if (ckfn
&& !(*ckfn
) (otmp
))
680 pline(xprname(otmp
, ilet
));
681 addtopl(" [nyaq]? ");
700 pline(cnt
? "That was all." : "No applicable objects.");
706 obj_to_let(obj
) /* should of course only be called for things
713 if (flags
.invlet_constant
)
714 return (obj
->invlet
);
716 for (otmp
= invent
; otmp
&& otmp
!= obj
; otmp
= otmp
->nobj
)
719 return (otmp
? ilet
: NOINVSYM
);
726 pline(xprname(obj
, obj_to_let(obj
)));
734 static char li
[BUFSZ
];
736 (void) sprintf(li
, "%c - %s.",
737 flags
.invlet_constant
? obj
->invlet
: let
,
749 /* called with 0 or "": all objects in inventory */
750 /* otherwise: all objects with (serial) letter in lets */
760 morc
= 0; /* just to be sure */
763 pline("Not carrying anything.");
766 cornline(0, (char *) 0);
768 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
769 if (flags
.invlet_constant
)
771 if (!lets
|| !*lets
|| strchr(lets
, ilet
)) {
772 cornline(1, xprname(otmp
, ilet
));
775 if (!flags
.invlet_constant
)
785 { /* free after Robert Viduya */
786 /* Changed to one type only, so he doesnt have to type cr */
791 boolean billx
= inshop() && doinvbill(0);
792 boolean unpd
= FALSE
;
794 if (!invent
&& !u
.ugold
&& !billx
) {
795 pline("You aren't carrying anything.");
802 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
803 if (!strchr(stuff
, otmp
->olet
)) {
804 stuff
[stct
++] = otmp
->olet
;
817 pline("What type of object [%s] do you want an inventory of? ",
820 if (strchr(quitchars
, c
))
828 if (c
== 'x' || c
== 'X') {
832 pline("No used-up objects on the shopping bill.");
835 if ((c
== 'u' || c
== 'U') && !unpd
) {
836 pline("You are not carrying any unpaid objects.");
841 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
) {
842 if (flags
.invlet_constant
)
844 if (c
== otmp
->olet
|| (c
== 'u' && otmp
->unpaid
))
845 stuff
[stct
++] = ilet
;
846 if (!flags
.invlet_constant
)
852 pline("You have no such objects.");
859 /* look at what is here */
863 struct obj
*otmp
= NULL
, *otmp0
= NULL
;
864 struct gold
*gold
= NULL
;
865 const char *verb
= Blind
? "feel" : "see";
870 pline("You try to feel what is lying here on the floor.");
871 if (Levitation
) { /* ab@unido */
872 pline("You cannot reach the floor!");
876 otmp0
= o_at(u
.ux
, u
.uy
);
877 gold
= g_at(u
.ux
, u
.uy
);
879 if (u
.uswallow
|| (!otmp0
&& !gold
)) {
880 pline("You %s no objects here.", verb
);
883 cornline(0, "Things that are here:");
884 for (otmp
= otmp0
; otmp
; otmp
= otmp
->nobj
) {
885 if (otmp
->ox
== u
.ux
&& otmp
->oy
== u
.uy
) {
887 cornline(1, doname(otmp
));
888 if (Blind
&& otmp
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
889 pline("Touching the dead cockatrice is a fatal mistake ...");
890 pline("You die ...");
891 killer
= "dead cockatrice";
900 (void) sprintf(gbuf
, "%ld gold piece%s",
901 gold
->amount
, plur(gold
->amount
));
903 pline("You %s here %s.", verb
, gbuf
);
907 if (ct
== 1 && !gold
) {
908 pline("You %s here %s.", verb
, doname(otmp0
));
909 cornline(3, (char *) 0);
912 cornline(2, (char *) 0);
920 struct obj
*otmp
= fobj
;
921 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
923 if (otmp
->ox
== obj
->ox
&& otmp
->oy
== obj
->oy
&&
924 merged(obj
, otmp
, 1))
928 /* merge obj with otmp and delete obj if types agree */
930 merged(otmp
, obj
, lose
)
931 struct obj
*otmp
, *obj
;
934 if (obj
->otyp
== otmp
->otyp
&&
935 obj
->unpaid
== otmp
->unpaid
&&
936 obj
->spe
== otmp
->spe
&&
937 obj
->dknown
== otmp
->dknown
&&
938 obj
->cursed
== otmp
->cursed
&&
939 (strchr("%*?!", obj
->olet
) ||
940 (obj
->known
== otmp
->known
&&
941 (obj
->olet
== WEAPON_SYM
&& obj
->otyp
< BOOMERANG
)))) {
942 otmp
->quan
+= obj
->quan
;
943 otmp
->owt
+= obj
->owt
;
946 obfree(obj
, otmp
); /* free(obj), bill->otmp */
952 static long goldcounted
;
954 * Gold is no longer displayed; in fact, when you have a lot of money,
955 * it may take a while before you have counted it all.
956 * [Bug: d$ and pickup still tell you how much it was.]
961 if ((goldcounted
+= 100 * (u
.ulevel
+ 1)) >= u
.ugold
) {
964 eps
= rnd((int) (u
.ugold
/ 100 + 1));
965 pline("You probably have about %ld gold pieces.",
967 return (0); /* done */
969 return (1); /* continue */
976 pline("You do not carry any gold.");
977 else if (u
.ugold
<= 500)
978 pline("You are carrying %ld gold pieces.", u
.ugold
);
980 pline("You sit down in order to count your gold pieces.");
982 occupation
= countgold
;
983 occtxt
= "counting your gold";
988 /* --- end of gold counting section --- */
993 pline("You are empty handed.");
1002 if (!uarm
&& !uarmg
&& !uarms
&& !uarmh
)
1003 pline("You are not wearing any armor.");
1009 lets
[ct
++] = obj_to_let(uarm
);
1011 lets
[ct
++] = obj_to_let(uarm2
);
1013 lets
[ct
++] = obj_to_let(uarmh
);
1015 lets
[ct
++] = obj_to_let(uarms
);
1017 lets
[ct
++] = obj_to_let(uarmg
);
1027 if (!uleft
&& !uright
)
1028 pline("You are not wearing any rings.");
1034 lets
[ct
++] = obj_to_let(uleft
);
1036 lets
[ct
++] = obj_to_let(uright
);
1047 return (c
>= '0' && c
<= '9');