]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.c
1 /* $NetBSD: hack.c,v 1.8 2009/06/07 18:30:39 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.8 2009/06/07 18:30:39 dholland Exp $");
73 * called on movement: 1. when throwing ball+chain far away 2. when
74 * teleporting 3. when walking out of a lit room
85 newsym(u.udisx, u.udisy);
93 for (x
= u
.ux
- 1; x
< u
.ux
+ 2; x
++)
94 for (y
= u
.uy
- 1; y
< u
.uy
+ 2; y
++) {
98 if (!lev
->lit
&& lev
->scrsym
== '.') {
107 * called: in hack.eat.c: seeoff(0) - blind after eating rotten food in
108 * hack.mon.c: seeoff(0) - blinded by a yellow light in hack.mon.c: seeoff(1)
109 * - swallowed in hack.do.c: seeoff(0) - blind after drinking potion in
110 * hack.do.c: seeoff(1) - go up or down the stairs in hack.trap.c:seeoff(1)
111 * - fall through trapdoor
114 /* 1 to redo @, 0 to leave them *//* 1 means
115 * misc movement, 0 means blindness */
122 if (u
.udispl
&& mode
) {
124 levl
[u
.udisx
][u
.udisy
].scrsym
= news0(u
.udisx
, u
.udisy
);
132 for (x
= u
.ux
- 1; x
< u
.ux
+ 2; x
++)
133 for (y
= u
.uy
- 1; y
< u
.uy
+ 2; y
++) {
137 if (!lev
->lit
&& lev
->scrsym
== '.')
147 struct monst
*mtmp
= NULL
;
148 struct rm
*tmpr
, *ust
;
149 struct trap
*trap
= NULL
;
150 struct obj
*otmp
= NULL
;
154 if (inv_weight() > 0) {
155 pline("You collapse under your load.");
167 } while (!isok(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
) ||
168 IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
+ u
.dy
].typ
));
170 if (!isok(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) {
176 ust
= &levl
[u
.ux
][u
.uy
];
179 if (!u
.uswallow
&& (trap
= t_at(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) && trap
->tseen
)
181 if (u
.ustuck
&& !u
.uswallow
&& (u
.ux
+ u
.dx
!= u
.ustuck
->mx
||
182 u
.uy
+ u
.dy
!= u
.ustuck
->my
)) {
183 if (dist(u
.ustuck
->mx
, u
.ustuck
->my
) > 2) {
184 /* perhaps it fled (or was teleported or ... ) */
188 pline("You cannot escape from it!");
190 pline("You cannot escape from %s!",
196 if (u
.uswallow
|| (mtmp
= m_at(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
))) {
202 return; /* we just fainted */
204 /* try to attack; note that it might evade */
205 if (attack(u
.uswallow
? u
.ustuck
: mtmp
))
208 /* not attacking an animal, so we try to move */
210 if (u
.utraptype
== TT_PIT
) {
211 pline("You are still in a pit.");
214 pline("You are caught in a beartrap.");
215 if ((u
.dx
&& u
.dy
) || !rn2(5))
220 tmpr
= &levl
[u
.ux
+ u
.dx
][u
.uy
+ u
.dy
];
221 if (IS_ROCK(tmpr
->typ
) ||
222 (u
.dx
&& u
.dy
&& (tmpr
->typ
== DOOR
|| ust
->typ
== DOOR
))) {
227 while ((otmp
= sobj_at(ENORMOUS_ROCK
, u
.ux
+ u
.dx
, u
.uy
+ u
.dy
)) != NULL
){
228 xchar rx
= u
.ux
+ 2 * u
.dx
, ry
= u
.uy
+ 2 * u
.dy
;
231 if (isok(rx
, ry
) && !IS_ROCK(levl
[rx
][ry
].typ
) &&
232 (levl
[rx
][ry
].typ
!= DOOR
|| !(u
.dx
&& u
.dy
)) &&
233 !sobj_at(ENORMOUS_ROCK
, rx
, ry
)) {
235 pline("You hear a monster behind the rock.");
236 pline("Perhaps that's why you cannot move it.");
239 if ((ttmp
= t_at(rx
, ry
)) != NULL
)
240 switch (ttmp
->ttyp
) {
242 pline("You push the rock into a pit!");
245 pline("It completely fills the pit!");
248 pline("You push the rock and suddenly it disappears!");
252 if (levl
[rx
][ry
].typ
== POOL
) {
253 levl
[rx
][ry
].typ
= ROOM
;
256 pline("You push the rock into the water.");
257 pline("Now you can cross the water!");
265 atl(rx
, ry
, otmp
->olet
);
267 newsym(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
270 static long lastmovetime
;
272 * note: this var contains garbage initially
273 * and after a restore
275 if (moves
> lastmovetime
+ 2 || moves
< lastmovetime
)
276 pline("With great effort you move the enormous rock.");
277 lastmovetime
= moves
;
280 pline("You try to move the enormous rock, but in vain.");
282 if ((!invent
|| inv_weight() + 90 <= 0) &&
283 (!u
.dx
|| !u
.dy
|| (IS_ROCK(levl
[u
.ux
][u
.uy
+ u
.dy
].typ
)
284 && IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
].typ
)))) {
285 pline("However, you can squeeze yourself into a small opening.");
291 if (u
.dx
&& u
.dy
&& IS_ROCK(levl
[u
.ux
][u
.uy
+ u
.dy
].typ
) &&
292 IS_ROCK(levl
[u
.ux
+ u
.dx
][u
.uy
].typ
) &&
293 invent
&& inv_weight() + 40 > 0) {
294 pline("You are carrying too much to get through.");
299 DIST(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
, uchain
->ox
, uchain
->oy
) > 2) {
300 if (carried(uball
)) {
301 movobj(uchain
, u
.ux
, u
.uy
);
304 if (DIST(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
, uball
->ox
, uball
->oy
) < 3) {
305 /* leave ball, move chain under/over ball */
306 movobj(uchain
, uball
->ox
, uball
->oy
);
309 if (inv_weight() + (int) uball
->owt
/ 2 > 0) {
310 pline("You cannot %sdrag the heavy iron ball.",
311 invent
? "carry all that and also " : "");
315 movobj(uball
, uchain
->ox
, uchain
->oy
);
316 unpobj(uball
); /* BAH %% */
326 if (tmpr
->typ
== DOOR
||
327 (xupstair
== u
.ux
&& yupstair
== u
.uy
) ||
328 (xdnstair
== u
.ux
&& ydnstair
== u
.uy
))
331 if (tmpr
->typ
== POOL
&& !Levitation
)
332 drown(); /* not necessarily fatal */
346 if (tmpr
->typ
== DOOR
)
347 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
348 else if (ust
->typ
== DOOR
)
349 nose1(oldx
- u
.dx
, oldy
- u
.dy
);
352 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
358 prl1(u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
359 if (tmpr
->typ
== DOOR
) {
369 nose1(oldx
- u
.dx
, oldy
- u
.dy
);
378 dotrap(trap
); /* fall into pit, arrow trap, etc. */
381 read_engr_at(u
.ux
, u
.uy
);
385 movobj(struct obj
*obj
, int ox
, int oy
)
387 /* Some dirty programming to get display right */
399 if (!g_at(u
.ux
, u
.uy
) && !o_at(u
.ux
, u
.uy
)) {
400 pline("There is nothing here to pick up.");
404 pline("You cannot reach the floor.");
415 struct obj
*obj
, *obj2
;
420 while ((gold
= g_at(u
.ux
, u
.uy
)) != NULL
) {
421 pline("%ld gold piece%s.", gold
->amount
, plur(gold
->amount
));
422 u
.ugold
+= gold
->amount
;
431 /* check for more than one object */
435 for (obj
= fobj
; obj
; obj
= obj
->nobj
)
436 if (obj
->ox
== u
.ux
&& obj
->oy
== u
.uy
)
437 if (!Punished
|| obj
!= uchain
)
442 pline("There are several objects here.");
444 for (obj
= fobj
; obj
; obj
= obj2
) {
445 obj2
= obj
->nobj
; /* perhaps obj will be picked up */
446 if (obj
->ox
== u
.ux
&& obj
->oy
== u
.uy
) {
450 /* do not pick up uchain */
451 if (Punished
&& obj
== uchain
)
457 pline("Pick up %s ? [ynaq]", doname(obj
));
458 while (!strchr("ynaq ", (c
= readchar())))
467 if (obj
->otyp
== DEAD_COCKATRICE
&& !uarmg
) {
468 pline("Touching the dead cockatrice is a fatal mistake.");
469 pline("You turn to stone.");
470 killer
= "cockatrice cadaver";
473 if (obj
->otyp
== SCR_SCARE_MONSTER
) {
478 * Note: perhaps the 1st pickup
479 * failed: you cannot carry anymore,
480 * and so we never dropped it - let's
481 * assume that treading on it twice
482 * also destroys the scroll
484 pline("The scroll turns to dust as you pick it up.");
489 wt
= inv_weight() + obj
->owt
;
492 /* see how many we can lift */
493 int savequan
= obj
->quan
;
494 int iw
= inv_weight();
496 for (qq
= 1; qq
< savequan
; qq
++) {
498 if (iw
+ weight(obj
) > 0)
501 obj
->quan
= savequan
;
503 /* we can carry qq of them */
506 pline("You can only carry %s of the %s lying here.",
507 (qq
== 1) ? "one" : "some",
509 (void) splitobj(obj
, qq
);
511 * note: obj2 is set already, so
512 * we'll never encounter the other
513 * half; if it should be otherwise
520 pline("There %s %s here, but %s.",
521 (obj
->quan
== 1) ? "is" : "are",
523 !invent
? "it is too heavy for you to lift"
524 : "you cannot carry anymore");
528 if (inv_cnt() >= 52) {
529 pline("Your knapsack cannot accommodate anymore items.");
533 pline("You have a little trouble lifting");
537 addtobill(obj
); /* sets obj->unpaid if necessary */
539 int pickquan
= obj
->quan
;
542 obj
->dknown
= 1; /* this is done by
543 * prinv(), but addinv()
544 * needs it already for
546 obj
= addinv(obj
); /* might merge it with
548 mergquan
= obj
->quan
;
549 obj
->quan
= pickquan
; /* to fool prinv() */
551 obj
->quan
= mergquan
;
557 /* stop running if we see something interesting */
558 /* turn around a corner if that is the only way we can proceed */
559 /* do not turn left or right twice */
563 int x
, y
, i
, x0
= 0, y0
= 0, m0
= 0, i0
= 9;
564 int corrct
= 0, noturn
= 0;
566 if (Blind
|| flags
.run
== 0)
568 if (flags
.run
== 1 && levl
[u
.ux
][u
.uy
].typ
== ROOM
)
571 if (u
.ux0
== u
.ux
+ u
.dx
&& u
.uy0
== u
.uy
+ u
.dy
)
574 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
575 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++) {
576 if (x
== u
.ux
&& y
== u
.uy
)
580 if ((mtmp
= m_at(x
, y
)) && !mtmp
->mimic
&&
581 (!mtmp
->minvis
|| See_invisible
)) {
582 if (!mtmp
->mtame
|| (x
== u
.ux
+ u
.dx
&& y
== u
.uy
+ u
.dy
))
585 mtmp
= 0; /* invisible M cannot
587 if (x
== u
.ux
- u
.dx
&& y
== u
.uy
- u
.dy
)
589 switch (levl
[x
][y
].scrsym
) {
596 if (x
!= u
.ux
&& y
!= u
.uy
)
600 /* fall into next case */
603 if (flags
.run
== 1 || flags
.run
== 3) {
604 i
= DIST(x
, y
, u
.ux
+ u
.dx
, u
.uy
+ u
.dy
);
607 if (corrct
== 1 && DIST(x
, y
, x0
, y0
) != 1)
620 goto corr
; /* if you must */
621 if (x
== u
.ux
+ u
.dx
&& y
== u
.uy
+ u
.dy
)
624 default: /* e.g. objects or trap or stairs */
635 if (corrct
> 0 && (flags
.run
== 4 || flags
.run
== 5))
638 if (corrct
> 1 && flags
.run
== 2)
640 if ((flags
.run
== 1 || flags
.run
== 3) && !noturn
&& !m0
&& i0
&&
641 (corrct
== 1 || (corrct
== 2 && i0
== 1))) {
642 /* make sure that we do not turn too far */
644 if (u
.dx
== y0
- u
.uy
&& u
.dy
== u
.ux
- x0
)
645 i
= 2; /* straight turn right */
647 i
= -2; /* straight turn left */
648 } else if (u
.dx
&& u
.dy
) {
649 if ((u
.dx
== u
.dy
&& y0
== u
.uy
) ||
650 (u
.dx
!= u
.dy
&& y0
!= u
.uy
))
651 i
= -1; /* half turn left */
653 i
= 1; /* half turn right */
655 if ((x0
- u
.ux
== y0
- u
.uy
&& !u
.dy
) ||
656 (x0
- u
.ux
!= y0
- u
.uy
&& u
.dy
))
657 i
= 1; /* half turn right */
659 i
= -1; /* half turn left */
661 i
+= u
.last_str_turn
;
662 if (i
<= 2 && i
>= -2) {
664 u
.dx
= x0
- u
.ux
, u
.dy
= y0
- u
.uy
;
669 /* something like lookaround, but we are not running */
670 /* react only to monsters that might hit us */
677 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
678 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++) {
679 if (x
== u
.ux
&& y
== u
.uy
)
681 if ((mtmp
= m_at(x
, y
)) && !mtmp
->mimic
&& !mtmp
->mtame
&&
682 !mtmp
->mpeaceful
&& !strchr("Ea", mtmp
->data
->mlet
) &&
683 !mtmp
->mfroz
&& !mtmp
->msleep
&& /* aplvax!jcn */
684 (!mtmp
->minvis
|| See_invisible
))
692 cansee(xchar x
, xchar y
)
694 int dx
, dy
, adx
, ady
, sdx
, sdy
, dmax
, d
;
702 if (d
> u
.uhorizon
* u
.uhorizon
)
712 if (dx
== 0 || dy
== 0 || adx
== ady
) {
713 dmax
= (dx
== 0) ? ady
: adx
;
714 for (d
= 1; d
<= dmax
; d
++)
715 if (!rroom(sdx
* d
, sdy
* d
))
718 } else if (ady
> adx
) {
719 for (d
= 1; d
<= ady
; d
++) {
720 if (!rroom(sdx
* ((d
* adx
) / ady
), sdy
* d
) ||
721 !rroom(sdx
* ((d
* adx
- 1) / ady
+ 1), sdy
* d
))
726 for (d
= 1; d
<= adx
; d
++) {
727 if (!rroom(sdx
* d
, sdy
* ((d
* ady
) / adx
)) ||
728 !rroom(sdx
* d
, sdy
* ((d
* ady
- 1) / adx
+ 1)))
738 return (IS_ROOM(levl
[u
.ux
+ x
][u
.uy
+ y
].typ
));
744 cansee(xchar x
, xchar y
)
746 if (Blind
|| u
.uswallow
)
750 if (levl
[x
][y
].lit
&& seelx
<= x
&& x
<= seehx
&& seely
<= y
&&
760 return ((a
> 0) ? 1 : (a
== 0) ? 0 : -1);
773 for (y
= u
.uy
- u
.uhorizon
; y
<= u
.uy
+ u
.uhorizon
; y
++)
774 for (x
= u
.ux
- u
.uhorizon
; x
<= u
.ux
+ u
.uhorizon
; x
++) {
791 if (!levl
[u
.ux
][u
.uy
].lit
) {
797 for (seelx
= u
.ux
; levl
[seelx
- 1][u
.uy
].lit
; seelx
--);
798 for (seehx
= u
.ux
; levl
[seehx
+ 1][u
.uy
].lit
; seehx
++);
799 for (seely
= u
.uy
; levl
[u
.ux
][seely
- 1].lit
; seely
--);
800 for (seehy
= u
.uy
; levl
[u
.ux
][seehy
+ 1].lit
; seehy
++);
802 for (y
= seely
; y
<= seehy
; y
++)
803 for (x
= seelx
; x
<= seehx
; x
++) {
806 if (!levl
[u
.ux
][u
.uy
].lit
)
807 seehx
= 0; /* seems necessary elsewhere */
810 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
813 for (x
= u
.ux
- 1; x
<= u
.ux
+ 1; x
++)
816 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++)
819 for (y
= u
.uy
- 1; y
<= u
.uy
+ 1; y
++)
831 flags
.mv
= flags
.run
= 0;
843 else if (u
.ustr
< 17)
845 else if (u
.ustr
< 69)
846 return (1); /* up to 18/50 */
847 else if (u
.ustr
< 118)
858 else if (u
.ustr
< 16)
860 else if (u
.ustr
< 18)
862 else if (u
.ustr
== 18)
863 return (2); /* up to 18 */
864 else if (u
.ustr
< 94)
865 return (3); /* up to 18/75 */
866 else if (u
.ustr
< 109)
867 return (4); /* up to 18/90 */
868 else if (u
.ustr
< 118)
869 return (5); /* up to 18/99 */
874 /* may kill you; cause may be poison or */
875 /* monster like 'A' */
889 losehp(int n
, const char *knam
)
892 if (u
.uhp
> u
.uhpmax
)
893 u
.uhpmax
= u
.uhp
; /* perhaps n was negative */
896 killer
= knam
; /* the thing that killed you */
902 losehp_m(int n
, struct monst
*mtmp
)
912 { /* hit by V or W */
916 pline("Goodbye level %u.", u
.ulevel
--);
929 struct obj
*otmp
= invent
;
930 int wt
= (u
.ugold
+ 500) / 1000;
932 if (Levitation
) /* pugh@cornell */
933 carrcap
= MAX_CARR_CAP
;
935 carrcap
= 5 * (((u
.ustr
> 18) ? 20 : u
.ustr
) + u
.ulevel
);
936 if (carrcap
> MAX_CARR_CAP
)
937 carrcap
= MAX_CARR_CAP
;
938 if (Wounded_legs
& LEFT_SIDE
)
940 if (Wounded_legs
& RIGHT_SIDE
)
947 return (wt
- carrcap
);
953 struct obj
*otmp
= invent
;
965 return (10 * (1L << (u
.ulevel
- 1)));