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.
67 static void movobj(struct obj *, int, int);
68 static int inv_cnt(void);
71 * called on movement: 1. when throwing ball+chain far away 2. when
72 * teleporting 3. when walking out of a lit room
83 newsym(u.udisx, u.udisy);
91 for (x = u.ux - 1; x < u.ux + 2; x++)
92 for (y = u.uy - 1; y < u.uy + 2; y++) {
96 if (!lev->lit && lev->scrsym == '.') {
105 * called: in hack.eat.c: seeoff(0) - blind after eating rotten food in
106 * hack.mon.c: seeoff(0) - blinded by a yellow light in hack.mon.c: seeoff(1)
107 * - swallowed in hack.do.c: seeoff(0) - blind after drinking potion in
108 * hack.do.c: seeoff(1) - go up or down the stairs in hack.trap.c:seeoff(1)
109 * - fall through trapdoor
112 /* 1 to redo @, 0 to leave them *//* 1 means
113 * misc movement, 0 means blindness */
120 if (u.udispl && mode) {
122 levl[u.udisx][u.udisy].scrsym = news0(u.udisx, u.udisy);
130 for (x = u.ux - 1; x < u.ux + 2; x++)
131 for (y = u.uy - 1; y < u.uy + 2; y++) {
135 if (!lev->lit && lev->scrsym == '.')
145 struct monst *mtmp = NULL;
146 struct rm *tmpr, *ust;
147 struct trap *trap = NULL;
148 struct obj *otmp = NULL;
152 if (inv_weight() > 0) {
153 pline("You collapse under your load.");
165 } while (!isok(u.ux + u.dx, u.uy + u.dy) ||
166 IS_ROCK(levl[u.ux + u.dx][u.uy + u.dy].typ));
168 if (!isok(u.ux + u.dx, u.uy + u.dy)) {
174 ust = &levl[u.ux][u.uy];
177 if (!u.uswallow && (trap = t_at(u.ux + u.dx, u.uy + u.dy)) && trap->tseen)
179 if (u.ustuck && !u.uswallow && (u.ux + u.dx != u.ustuck->mx ||
180 u.uy + u.dy != u.ustuck->my)) {
181 if (dist(u.ustuck->mx, u.ustuck->my) > 2) {
182 /* perhaps it fled (or was teleported or ... ) */
186 pline("You cannot escape from it!");
188 pline("You cannot escape from %s!",
194 if (u.uswallow || (mtmp = m_at(u.ux + u.dx, u.uy + u.dy))) {
200 return; /* we just fainted */
202 /* try to attack; note that it might evade */
203 if (attack(u.uswallow ? u.ustuck : mtmp))
206 /* not attacking an animal, so we try to move */
208 if (u.utraptype == TT_PIT) {
209 pline("You are still in a pit.");
212 pline("You are caught in a beartrap.");
213 if ((u.dx && u.dy) || !rn2(5))
218 tmpr = &levl[u.ux + u.dx][u.uy + u.dy];
219 if (IS_ROCK(tmpr->typ) ||
220 (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))) {
225 while ((otmp = sobj_at(ENORMOUS_ROCK, u.ux + u.dx, u.uy + u.dy)) != NULL){
226 xchar rx = u.ux + 2 * u.dx, ry = u.uy + 2 * u.dy;
229 if (isok(rx, ry) && !IS_ROCK(levl[rx][ry].typ) &&
230 (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
231 !sobj_at(ENORMOUS_ROCK, rx, ry)) {
233 pline("You hear a monster behind the rock.");
234 pline("Perhaps that's why you cannot move it.");
237 if ((ttmp = t_at(rx, ry)) != NULL)
238 switch (ttmp->ttyp) {
240 pline("You push the rock into a pit!");
243 pline("It completely fills the pit!");
246 pline("You push the rock and suddenly it disappears!");
250 if (levl[rx][ry].typ == POOL) {
251 levl[rx][ry].typ = ROOM;
254 pline("You push the rock into the water.");
255 pline("Now you can cross the water!");
263 atl(rx, ry, otmp->olet);
265 newsym(u.ux + u.dx, u.uy + u.dy);
268 static long lastmovetime;
270 * note: this var contains garbage initially
271 * and after a restore
273 if (moves > lastmovetime + 2 || moves < lastmovetime)
274 pline("With great effort you move the enormous rock.");
275 lastmovetime = moves;
278 pline("You try to move the enormous rock, but in vain.");
280 if ((!invent || inv_weight() + 90 <= 0) &&
281 (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy + u.dy].typ)
282 && IS_ROCK(levl[u.ux + u.dx][u.uy].typ)))) {
283 pline("However, you can squeeze yourself into a small opening.");
289 if (u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy + u.dy].typ) &&
290 IS_ROCK(levl[u.ux + u.dx][u.uy].typ) &&
291 invent && inv_weight() + 40 > 0) {
292 pline("You are carrying too much to get through.");
297 DIST(u.ux + u.dx, u.uy + u.dy, uchain->ox, uchain->oy) > 2) {
298 if (carried(uball)) {
299 movobj(uchain, u.ux, u.uy);
302 if (DIST(u.ux + u.dx, u.uy + u.dy, uball->ox, uball->oy) < 3) {
303 /* leave ball, move chain under/over ball */
304 movobj(uchain, uball->ox, uball->oy);
307 if (inv_weight() + (int) uball->owt / 2 > 0) {
308 pline("You cannot %sdrag the heavy iron ball.",
309 invent ? "carry all that and also " : "");
313 movobj(uball, uchain->ox, uchain->oy);
314 unpobj(uball); /* BAH %% */
324 if (tmpr->typ == DOOR ||
325 (xupstair == u.ux && yupstair == u.uy) ||
326 (xdnstair == u.ux && ydnstair == u.uy))
329 if (tmpr->typ == POOL && !Levitation)
330 drown(); /* not necessarily fatal */
344 if (tmpr->typ == DOOR)
345 prl1(u.ux + u.dx, u.uy + u.dy);
346 else if (ust->typ == DOOR)
347 nose1(oldx - u.dx, oldy - u.dy);
350 prl1(u.ux + u.dx, u.uy + u.dy);
356 prl1(u.ux + u.dx, u.uy + u.dy);
357 if (tmpr->typ == DOOR) {
367 nose1(oldx - u.dx, oldy - u.dy);
376 dotrap(trap); /* fall into pit, arrow trap, etc. */
379 read_engr_at(u.ux, u.uy);
383 movobj(struct obj *obj, int ox, int oy)
385 /* Some dirty programming to get display right */
397 if (!g_at(u.ux, u.uy) && !o_at(u.ux, u.uy)) {
398 pline("There is nothing here to pick up.");
402 pline("You cannot reach the floor.");
413 struct obj *obj, *obj2;
418 while ((gold = g_at(u.ux, u.uy)) != NULL) {
419 pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
420 u.ugold += gold->amount;
429 /* check for more than one object */
433 for (obj = fobj; obj; obj = obj->nobj)
434 if (obj->ox == u.ux && obj->oy == u.uy)
435 if (!Punished || obj != uchain)
440 pline("There are several objects here.");
442 for (obj = fobj; obj; obj = obj2) {
443 obj2 = obj->nobj; /* perhaps obj will be picked up */
444 if (obj->ox == u.ux && obj->oy == u.uy) {
448 /* do not pick up uchain */
449 if (Punished && obj == uchain)
455 pline("Pick up %s ? [ynaq]", doname(obj));
456 while (!strchr("ynaq ", (c = readchar())))
465 if (obj->otyp == DEAD_COCKATRICE && !uarmg) {
466 pline("Touching the dead cockatrice is a fatal mistake.");
467 pline("You turn to stone.");
468 killer = "cockatrice cadaver";
471 if (obj->otyp == SCR_SCARE_MONSTER) {
476 * Note: perhaps the 1st pickup
477 * failed: you cannot carry any more,
478 * and so we never dropped it - let's
479 * assume that treading on it twice
480 * also destroys the scroll
482 pline("The scroll turns to dust as you pick it up.");
487 wt = inv_weight() + obj->owt;
490 /* see how many we can lift */
491 int savequan = obj->quan;
492 int iw = inv_weight();
494 for (qq = 1; qq < savequan; qq++) {
496 if (iw + weight(obj) > 0)
499 obj->quan = savequan;
501 /* we can carry qq of them */
504 pline("You can only carry %s of the %s lying here.",
505 (qq == 1) ? "one" : "some",
507 (void) splitobj(obj, qq);
509 * note: obj2 is set already, so
510 * we'll never encounter the other
511 * half; if it should be otherwise
518 pline("There %s %s here, but %s.",
519 (obj->quan == 1) ? "is" : "are",
521 !invent ? "it is too heavy for you to lift"
522 : "you cannot carry anymore");
526 if (inv_cnt() >= 52) {
527 pline("Your knapsack cannot accommodate any more items.");
531 pline("You have a little trouble lifting");
535 addtobill(obj); /* sets obj->unpaid if necessary */
537 int pickquan = obj->quan;
540 obj->dknown = 1; /* this is done by
541 * prinv(), but addinv()
542 * needs it already for
544 obj = addinv(obj); /* might merge it with
546 mergquan = obj->quan;
547 obj->quan = pickquan; /* to fool prinv() */
549 obj->quan = mergquan;
555 /* stop running if we see something interesting */
556 /* turn around a corner if that is the only way we can proceed */
557 /* do not turn left or right twice */
561 int x, y, i, x0 = 0, y0 = 0, m0 = 0, i0 = 9;
562 int corrct = 0, noturn = 0;
564 if (Blind || flags.run == 0)
566 if (flags.run == 1 && levl[u.ux][u.uy].typ == ROOM)
569 if (u.ux0 == u.ux + u.dx && u.uy0 == u.uy + u.dy)
572 for (x = u.ux - 1; x <= u.ux + 1; x++)
573 for (y = u.uy - 1; y <= u.uy + 1; y++) {
574 if (x == u.ux && y == u.uy)
578 if ((mtmp = m_at(x, y)) && !mtmp->mimic &&
579 (!mtmp->minvis || See_invisible)) {
580 if (!mtmp->mtame || (x == u.ux + u.dx && y == u.uy + u.dy))
583 mtmp = 0; /* invisible M cannot
585 if (x == u.ux - u.dx && y == u.uy - u.dy)
587 switch (levl[x][y].scrsym) {
594 if (x != u.ux && y != u.uy)
601 if (flags.run == 1 || flags.run == 3) {
602 i = DIST(x, y, u.ux + u.dx, u.uy + u.dy);
605 if (corrct == 1 && DIST(x, y, x0, y0) != 1)
618 goto corr; /* if you must */
619 if (x == u.ux + u.dx && y == u.uy + u.dy)
622 default: /* e.g. objects or trap or stairs */
633 if (corrct > 0 && (flags.run == 4 || flags.run == 5))
636 if (corrct > 1 && flags.run == 2)
638 if ((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
639 (corrct == 1 || (corrct == 2 && i0 == 1))) {
640 /* make sure that we do not turn too far */
642 if (u.dx == y0 - u.uy && u.dy == u.ux - x0)
643 i = 2; /* straight turn right */
645 i = -2; /* straight turn left */
646 } else if (u.dx && u.dy) {
647 if ((u.dx == u.dy && y0 == u.uy) ||
648 (u.dx != u.dy && y0 != u.uy))
649 i = -1; /* half turn left */
651 i = 1; /* half turn right */
653 if ((x0 - u.ux == y0 - u.uy && !u.dy) ||
654 (x0 - u.ux != y0 - u.uy && u.dy))
655 i = 1; /* half turn right */
657 i = -1; /* half turn left */
659 i += u.last_str_turn;
660 if (i <= 2 && i >= -2) {
662 u.dx = x0 - u.ux, u.dy = y0 - u.uy;
667 /* something like lookaround, but we are not running */
668 /* react only to monsters that might hit us */
675 for (x = u.ux - 1; x <= u.ux + 1; x++)
676 for (y = u.uy - 1; y <= u.uy + 1; y++) {
677 if (x == u.ux && y == u.uy)
679 if ((mtmp = m_at(x, y)) && !mtmp->mimic && !mtmp->mtame &&
680 !mtmp->mpeaceful && !strchr("Ea", mtmp->data->mlet) &&
681 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
682 (!mtmp->minvis || See_invisible))
690 cansee(xchar x, xchar y)
692 int dx, dy, adx, ady, sdx, sdy, dmax, d;
700 if (d > u.uhorizon * u.uhorizon)
710 if (dx == 0 || dy == 0 || adx == ady) {
711 dmax = (dx == 0) ? ady : adx;
712 for (d = 1; d <= dmax; d++)
713 if (!rroom(sdx * d, sdy * d))
716 } else if (ady > adx) {
717 for (d = 1; d <= ady; d++) {
718 if (!rroom(sdx * ((d * adx) / ady), sdy * d) ||
719 !rroom(sdx * ((d * adx - 1) / ady + 1), sdy * d))
724 for (d = 1; d <= adx; d++) {
725 if (!rroom(sdx * d, sdy * ((d * ady) / adx)) ||
726 !rroom(sdx * d, sdy * ((d * ady - 1) / adx + 1)))
736 return (IS_ROOM(levl[u.ux + x][u.uy + y].typ));
742 cansee(xchar x, xchar y)
744 if (Blind || u.uswallow)
748 if (levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
758 return ((a > 0) ? 1 : (a == 0) ? 0 : -1);
771 for (y = u.uy - u.uhorizon; y <= u.uy + u.uhorizon; y++)
772 for (x = u.ux - u.uhorizon; x <= u.ux + u.uhorizon; x++) {
789 if (!levl[u.ux][u.uy].lit) {
795 for (seelx = u.ux; levl[seelx - 1][u.uy].lit; seelx--);
796 for (seehx = u.ux; levl[seehx + 1][u.uy].lit; seehx++);
797 for (seely = u.uy; levl[u.ux][seely - 1].lit; seely--);
798 for (seehy = u.uy; levl[u.ux][seehy + 1].lit; seehy++);
800 for (y = seely; y <= seehy; y++)
801 for (x = seelx; x <= seehx; x++) {
804 if (!levl[u.ux][u.uy].lit)
805 seehx = 0; /* seems necessary elsewhere */
808 for (x = u.ux - 1; x <= u.ux + 1; x++)
811 for (x = u.ux - 1; x <= u.ux + 1; x++)
814 for (y = u.uy - 1; y <= u.uy + 1; y++)
817 for (y = u.uy - 1; y <= u.uy + 1; y++)
829 flags.mv = flags.run = 0;
841 else if (u.ustr < 17)
843 else if (u.ustr < 69)
844 return (1); /* up to 18/50 */
845 else if (u.ustr < 118)
856 else if (u.ustr < 16)
858 else if (u.ustr < 18)
860 else if (u.ustr == 18)
861 return (2); /* up to 18 */
862 else if (u.ustr < 94)
863 return (3); /* up to 18/75 */
864 else if (u.ustr < 109)
865 return (4); /* up to 18/90 */
866 else if (u.ustr < 118)
867 return (5); /* up to 18/99 */
872 /* may kill you; cause may be poison or */
873 /* monster like 'A' */
887 losehp(int n, const char *knam)
890 if (u.uhp > u.uhpmax)
891 u.uhpmax = u.uhp; /* perhaps n was negative */
894 killer = knam; /* the thing that killed you */
900 losehp_m(int n, struct monst *mtmp)
910 { /* hit by V or W */
914 pline("Goodbye level %u.", u.ulevel--);
927 struct obj *otmp = invent;
928 int wt = (u.ugold + 500) / 1000;
930 if (Levitation) /* pugh@cornell */
931 carrcap = MAX_CARR_CAP;
933 carrcap = 5 * (((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
934 if (carrcap > MAX_CARR_CAP)
935 carrcap = MAX_CARR_CAP;
936 if (Wounded_legs & LEFT_SIDE)
938 if (Wounded_legs & RIGHT_SIDE)
945 return (wt - carrcap);
951 struct obj *otmp = invent;
963 return (10 * (1L << (u.ulevel - 1)));