]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.c
1 /* $NetBSD: hack.c,v 1.11 2011/08/07 06:03:45 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.c,v 1.11 2011/08/07 06:03:45 dholland Exp $");
72 static void movobj(struct obj
*, int, int);
73 static int inv_cnt(void);
76 * called on movement: 1. when throwing ball+chain far away 2. when
77 * teleporting 3. when walking out of a lit room
88 newsym(u.udisx, u.udisy);
96 for (x
= u
.ux
- 1; x
< u
.ux
+ 2; x
++)
97 for (y
= u
.uy
- 1; y
< u
.uy
+ 2; y
++) {
101 if (!lev
->lit
&& lev
->scrsym
== '.') {
110 * called: in hack.eat.c: seeoff(0) - blind after eating rotten food in
111 * hack.mon.c: seeoff(0) - blinded by a yellow light in hack.mon.c: seeoff(1)
112 * - swallowed in hack.do.c: seeoff(0) - blind after drinking potion in
113 * hack.do.c: seeoff(1) - go up or down the stairs in hack.trap.c:seeoff(1)
114 * - fall through trapdoor
117 /* 1 to redo @, 0 to leave them *//* 1 means
118 * misc movement, 0 means blindness */
125 if (u
.udispl
&& mode
) {
127 levl
[u
.udisx
][u
.udisy
].scrsym
= news0(u
.udisx
, u
.udisy
);
135 for (x
= u
.ux
- 1; x
< u
.ux
+ 2; x
++)
136 for (y
= u
.uy
- 1; y
< u
.uy
+ 2; y
++) {
140 if (!lev
->lit
&& lev
->scrsym
== '.')
150 struct monst
*mtmp
= NULL
;
151 struct rm
*tmpr
, *ust
;
152 struct trap
*trap
= NULL
;
153 struct obj
*otmp
= NULL
;
157 if (inv_weight() > 0) {
158 pline("You collapse under your load.");
170 } while (!isok(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
) ||
171 IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
+ u
.dy
].typ
));
173 if (!isok(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) {
179 ust
= &levl
[u
.ux
][u
.uy
];
182 if (!u
.uswallow
&& (trap
= t_at(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) && trap
->tseen
)
184 if (u
.ustuck
&& !u
.uswallow
&& (u
.ux
+ u
.dx
!= u
.ustuck
->mx
||
185 u
.uy
+ u
.dy
!= u
.ustuck
->my
)) {
186 if (dist(u
.ustuck
->mx
, u
.ustuck
->my
) > 2) {
187 /* perhaps it fled (or was teleported or ... ) */
191 pline("You cannot escape from it!");
193 pline("You cannot escape from %s!",
199 if (u
.uswallow
|| (mtmp
= m_at(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
))) {
205 return; /* we just fainted */
207 /* try to attack; note that it might evade */
208 if (attack(u
.uswallow
? u
.ustuck
: mtmp
))
211 /* not attacking an animal, so we try to move */
213 if (u
.utraptype
== TT_PIT
) {
214 pline("You are still in a pit.");
217 pline("You are caught in a beartrap.");
218 if ((u
.dx
&& u
.dy
) || !rn2(5))
223 tmpr
= &levl
[u
.ux
+ u
.dx
][u
.uy
+ u
.dy
];
224 if (IS_ROCK(tmpr
->typ
) ||
225 (u
.dx
&& u
.dy
&& (tmpr
->typ
== DOOR
|| ust
->typ
== DOOR
))) {
230 while ((otmp
= sobj_at(ENORMOUS_ROCK
, u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) != NULL
){
231 xchar rx
= u
.ux
+ 2 * u
.dx
, ry
= u
.uy
+ 2 * u
.dy
;
234 if (isok(rx
, ry
) && !IS_ROCK(levl
[rx
][ry
].typ
) &&
235 (levl
[rx
][ry
].typ
!= DOOR
|| !(u
.dx
&& u
.dy
)) &&
236 !sobj_at(ENORMOUS_ROCK
, rx
, ry
)) {
238 pline("You hear a monster behind the rock.");
239 pline("Perhaps that's why you cannot move it.");
242 if ((ttmp
= t_at(rx
, ry
)) != NULL
)
243 switch (ttmp
->ttyp
) {
245 pline("You push the rock into a pit!");
248 pline("It completely fills the pit!");
251 pline("You push the rock and suddenly it disappears!");
255 if (levl
[rx
][ry
].typ
== POOL
) {
256 levl
[rx
][ry
].typ
= ROOM
;
259 pline("You push the rock into the water.");
260 pline("Now you can cross the water!");
268 atl(rx
, ry
, otmp
->olet
);
270 newsym(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
273 static long lastmovetime
;
275 * note: this var contains garbage initially
276 * and after a restore
278 if (moves
> lastmovetime
+ 2 || moves
< lastmovetime
)
279 pline("With great effort you move the enormous rock.");
280 lastmovetime
= moves
;
283 pline("You try to move the enormous rock, but in vain.");
285 if ((!invent
|| inv_weight() + 90 <= 0) &&
286 (!u
.dx
|| !u
.dy
|| (IS_ROCK(levl
[u
.ux
][u
.uy
+ u
.dy
].typ
)
287 && IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
].typ
)))) {
288 pline("However, you can squeeze yourself into a small opening.");
294 if (u
.dx
&& u
.dy
&& IS_ROCK(levl
[u
.ux
][u
.uy
+ u
.dy
].typ
) &&
295 IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
].typ
) &&
296 invent
&& inv_weight() + 40 > 0) {
297 pline("You are carrying too much to get through.");
302 DIST(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
, uchain
->ox
, uchain
->oy
) > 2) {
303 if (carried(uball
)) {
304 movobj(uchain
, u
.ux
, u
.uy
);
307 if (DIST(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
, uball
->ox
, uball
->oy
) < 3) {
308 /* leave ball, move chain under/over ball */
309 movobj(uchain
, uball
->ox
, uball
->oy
);
312 if (inv_weight() + (int) uball
->owt
/ 2 > 0) {
313 pline("You cannot %sdrag the heavy iron ball.",
314 invent
? "carry all that and also " : "");
318 movobj(uball
, uchain
->ox
, uchain
->oy
);
319 unpobj(uball
); /* BAH %% */
329 if (tmpr
->typ
== DOOR
||
330 (xupstair
== u
.ux
&& yupstair
== u
.uy
) ||
331 (xdnstair
== u
.ux
&& ydnstair
== u
.uy
))
334 if (tmpr
->typ
== POOL
&& !Levitation
)
335 drown(); /* not necessarily fatal */
349 if (tmpr
->typ
== DOOR
)
350 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
351 else if (ust
->typ
== DOOR
)
352 nose1(oldx
- u
.dx
, oldy
- u
.dy
);
355 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
361 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
362 if (tmpr
->typ
== DOOR
) {
372 nose1(oldx
- u
.dx
, oldy
- u
.dy
);
381 dotrap(trap
); /* fall into pit, arrow trap, etc. */
384 read_engr_at(u
.ux
, u
.uy
);
388 movobj(struct obj
*obj
, int ox
, int oy
)
390 /* Some dirty programming to get display right */
402 if (!g_at(u
.ux
, u
.uy
) && !o_at(u
.ux
, u
.uy
)) {
403 pline("There is nothing here to pick up.");
407 pline("You cannot reach the floor.");
418 struct obj
*obj
, *obj2
;
423 while ((gold
= g_at(u
.ux
, u
.uy
)) != NULL
) {
424 pline("%ld gold piece%s.", gold
->amount
, plur(gold
->amount
));
425 u
.ugold
+= gold
->amount
;
434 /* check for more than one object */
438 for (obj
= fobj
; obj
; obj
= obj
->nobj
)
439 if (obj
->ox
== u
.ux
&& obj
->oy
== u
.uy
)
440 if (!Punished
|| obj
!= uchain
)
445 pline("There are several objects here.");
447 for (obj
= fobj
; obj
; obj
= obj2
) {
448 obj2
= obj
->nobj
; /* perhaps obj will be picked up */
449 if (obj
->ox
== u
.ux
&& obj
->oy
== u
.uy
) {
453 /* do not pick up uchain */
454 if (Punished
&& obj
== uchain
)
460 pline("Pick up %s ? [ynaq]", doname(obj
));
461 while (!strchr("ynaq ", (c
= readchar())))
470 if (obj
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
471 pline("Touching the dead cockatrice is a fatal mistake.");
472 pline("You turn to stone.");
473 killer
= "cockatrice cadaver";
476 if (obj
->otyp
== SCR_SCARE_MONSTER
) {
481 * Note: perhaps the 1st pickup
482 * failed: you cannot carry anymore,
483 * and so we never dropped it - let's
484 * assume that treading on it twice
485 * also destroys the scroll
487 pline("The scroll turns to dust as you pick it up.");
492 wt
= inv_weight() + obj
->owt
;
495 /* see how many we can lift */
496 int savequan
= obj
->quan
;
497 int iw
= inv_weight();
499 for (qq
= 1; qq
< savequan
; qq
++) {
501 if (iw
+ weight(obj
) > 0)
504 obj
->quan
= savequan
;
506 /* we can carry qq of them */
509 pline("You can only carry %s of the %s lying here.",
510 (qq
== 1) ? "one" : "some",
512 (void) splitobj(obj
, qq
);
514 * note: obj2 is set already, so
515 * we'll never encounter the other
516 * half; if it should be otherwise
523 pline("There %s %s here, but %s.",
524 (obj
->quan
== 1) ? "is" : "are",
526 !invent
? "it is too heavy for you to lift"
527 : "you cannot carry anymore");
531 if (inv_cnt() >= 52) {
532 pline("Your knapsack cannot accommodate anymore items.");
536 pline("You have a little trouble lifting");
540 addtobill(obj
); /* sets obj->unpaid if necessary */
542 int pickquan
= obj
->quan
;
545 obj
->dknown
= 1; /* this is done by
546 * prinv(), but addinv()
547 * needs it already for
549 obj
= addinv(obj
); /* might merge it with
551 mergquan
= obj
->quan
;
552 obj
->quan
= pickquan
; /* to fool prinv() */
554 obj
->quan
= mergquan
;
560 /* stop running if we see something interesting */
561 /* turn around a corner if that is the only way we can proceed */
562 /* do not turn left or right twice */
566 int x
, y
, i
, x0
= 0, y0
= 0, m0
= 0, i0
= 9;
567 int corrct
= 0, noturn
= 0;
569 if (Blind
|| flags
.run
== 0)
571 if (flags
.run
== 1 && levl
[u
.ux
][u
.uy
].typ
== ROOM
)
574 if (u
.ux0
== u
.ux
+ u
.dx
&& u
.uy0
== u
.uy
+ u
.dy
)
577 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
578 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++) {
579 if (x
== u
.ux
&& y
== u
.uy
)
583 if ((mtmp
= m_at(x
, y
)) && !mtmp
->mimic
&&
584 (!mtmp
->minvis
|| See_invisible
)) {
585 if (!mtmp
->mtame
|| (x
== u
.ux
+ u
.dx
&& y
== u
.uy
+ u
.dy
))
588 mtmp
= 0; /* invisible M cannot
590 if (x
== u
.ux
- u
.dx
&& y
== u
.uy
- u
.dy
)
592 switch (levl
[x
][y
].scrsym
) {
599 if (x
!= u
.ux
&& y
!= u
.uy
)
606 if (flags
.run
== 1 || flags
.run
== 3) {
607 i
= DIST(x
, y
, u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
610 if (corrct
== 1 && DIST(x
, y
, x0
, y0
) != 1)
623 goto corr
; /* if you must */
624 if (x
== u
.ux
+ u
.dx
&& y
== u
.uy
+ u
.dy
)
627 default: /* e.g. objects or trap or stairs */
638 if (corrct
> 0 && (flags
.run
== 4 || flags
.run
== 5))
641 if (corrct
> 1 && flags
.run
== 2)
643 if ((flags
.run
== 1 || flags
.run
== 3) && !noturn
&& !m0
&& i0
&&
644 (corrct
== 1 || (corrct
== 2 && i0
== 1))) {
645 /* make sure that we do not turn too far */
647 if (u
.dx
== y0
- u
.uy
&& u
.dy
== u
.ux
- x0
)
648 i
= 2; /* straight turn right */
650 i
= -2; /* straight turn left */
651 } else if (u
.dx
&& u
.dy
) {
652 if ((u
.dx
== u
.dy
&& y0
== u
.uy
) ||
653 (u
.dx
!= u
.dy
&& y0
!= u
.uy
))
654 i
= -1; /* half turn left */
656 i
= 1; /* half turn right */
658 if ((x0
- u
.ux
== y0
- u
.uy
&& !u
.dy
) ||
659 (x0
- u
.ux
!= y0
- u
.uy
&& u
.dy
))
660 i
= 1; /* half turn right */
662 i
= -1; /* half turn left */
664 i
+= u
.last_str_turn
;
665 if (i
<= 2 && i
>= -2) {
667 u
.dx
= x0
- u
.ux
, u
.dy
= y0
- u
.uy
;
672 /* something like lookaround, but we are not running */
673 /* react only to monsters that might hit us */
680 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
681 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++) {
682 if (x
== u
.ux
&& y
== u
.uy
)
684 if ((mtmp
= m_at(x
, y
)) && !mtmp
->mimic
&& !mtmp
->mtame
&&
685 !mtmp
->mpeaceful
&& !strchr("Ea", mtmp
->data
->mlet
) &&
686 !mtmp
->mfroz
&& !mtmp
->msleep
&& /* aplvax!jcn */
687 (!mtmp
->minvis
|| See_invisible
))
695 cansee(xchar x
, xchar y
)
697 int dx
, dy
, adx
, ady
, sdx
, sdy
, dmax
, d
;
705 if (d
> u
.uhorizon
* u
.uhorizon
)
715 if (dx
== 0 || dy
== 0 || adx
== ady
) {
716 dmax
= (dx
== 0) ? ady
: adx
;
717 for (d
= 1; d
<= dmax
; d
++)
718 if (!rroom(sdx
* d
, sdy
* d
))
721 } else if (ady
> adx
) {
722 for (d
= 1; d
<= ady
; d
++) {
723 if (!rroom(sdx
* ((d
* adx
) / ady
), sdy
* d
) ||
724 !rroom(sdx
* ((d
* adx
- 1) / ady
+ 1), sdy
* d
))
729 for (d
= 1; d
<= adx
; d
++) {
730 if (!rroom(sdx
* d
, sdy
* ((d
* ady
) / adx
)) ||
731 !rroom(sdx
* d
, sdy
* ((d
* ady
- 1) / adx
+ 1)))
741 return (IS_ROOM(levl
[u
.ux
+ x
][u
.uy
+ y
].typ
));
747 cansee(xchar x
, xchar y
)
749 if (Blind
|| u
.uswallow
)
753 if (levl
[x
][y
].lit
&& seelx
<= x
&& x
<= seehx
&& seely
<= y
&&
763 return ((a
> 0) ? 1 : (a
== 0) ? 0 : -1);
776 for (y
= u
.uy
- u
.uhorizon
; y
<= u
.uy
+ u
.uhorizon
; y
++)
777 for (x
= u
.ux
- u
.uhorizon
; x
<= u
.ux
+ u
.uhorizon
; x
++) {
794 if (!levl
[u
.ux
][u
.uy
].lit
) {
800 for (seelx
= u
.ux
; levl
[seelx
- 1][u
.uy
].lit
; seelx
--);
801 for (seehx
= u
.ux
; levl
[seehx
+ 1][u
.uy
].lit
; seehx
++);
802 for (seely
= u
.uy
; levl
[u
.ux
][seely
- 1].lit
; seely
--);
803 for (seehy
= u
.uy
; levl
[u
.ux
][seehy
+ 1].lit
; seehy
++);
805 for (y
= seely
; y
<= seehy
; y
++)
806 for (x
= seelx
; x
<= seehx
; x
++) {
809 if (!levl
[u
.ux
][u
.uy
].lit
)
810 seehx
= 0; /* seems necessary elsewhere */
813 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
816 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
819 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++)
822 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++)
834 flags
.mv
= flags
.run
= 0;
846 else if (u
.ustr
< 17)
848 else if (u
.ustr
< 69)
849 return (1); /* up to 18/50 */
850 else if (u
.ustr
< 118)
861 else if (u
.ustr
< 16)
863 else if (u
.ustr
< 18)
865 else if (u
.ustr
== 18)
866 return (2); /* up to 18 */
867 else if (u
.ustr
< 94)
868 return (3); /* up to 18/75 */
869 else if (u
.ustr
< 109)
870 return (4); /* up to 18/90 */
871 else if (u
.ustr
< 118)
872 return (5); /* up to 18/99 */
877 /* may kill you; cause may be poison or */
878 /* monster like 'A' */
892 losehp(int n
, const char *knam
)
895 if (u
.uhp
> u
.uhpmax
)
896 u
.uhpmax
= u
.uhp
; /* perhaps n was negative */
899 killer
= knam
; /* the thing that killed you */
905 losehp_m(int n
, struct monst
*mtmp
)
915 { /* hit by V or W */
919 pline("Goodbye level %u.", u
.ulevel
--);
932 struct obj
*otmp
= invent
;
933 int wt
= (u
.ugold
+ 500) / 1000;
935 if (Levitation
) /* pugh@cornell */
936 carrcap
= MAX_CARR_CAP
;
938 carrcap
= 5 * (((u
.ustr
> 18) ? 20 : u
.ustr
) + u
.ulevel
);
939 if (carrcap
> MAX_CARR_CAP
)
940 carrcap
= MAX_CARR_CAP
;
941 if (Wounded_legs
& LEFT_SIDE
)
943 if (Wounded_legs
& RIGHT_SIDE
)
950 return (wt
- carrcap
);
956 struct obj
*otmp
= invent
;
968 return (10 * (1L << (u
.ulevel
- 1)));