1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.c,v 1.4 1999/11/16 10:26:35 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.c,v 1.6 2007/05/13 18:33:55 swildner Exp $ */
8 static void movobj(struct obj *, int, int);
10 static bool rroom(int, int);
12 static int inv_cnt(void);
14 /* called on movement:
15 * 1. when throwing ball+chain far away
17 * 3. when walking out of a lit room
30 for (x = u.ux - 1; x < u.ux + 2; x++)
31 for (y = u.uy - 1; y < u.uy + 2; y++) {
35 if (!lev->lit && lev->scrsym == '.') {
44 * in hack.eat.c: seeoff(0) - blind after eating rotten food
45 * in hack.mon.c: seeoff(0) - blinded by a yellow light
46 * in hack.mon.c: seeoff(1) - swallowed
47 * in hack.do.c: seeoff(0) - blind after drinking potion
48 * in hack.do.c: seeoff(1) - go up or down the stairs
49 * in hack.trap.c:seeoff(1) - fall through trapdoor
51 * 1 to redo @, 0 to leave them *//* 1 means
52 * misc movement, 0 means blindness
60 if (u.udispl && mode) {
62 levl[u.udisx][u.udisy].scrsym = news0(u.udisx, u.udisy);
70 for (x = u.ux - 1; x < u.ux + 2; x++)
71 for (y = u.uy - 1; y < u.uy + 2; y++) {
75 if (!lev->lit && lev->scrsym == '.')
85 struct monst *mtmp = NULL;
86 struct rm *tmpr, *ust;
87 struct trap *trap = NULL;
92 if (inv_weight() > 0) {
93 pline("You collapse under your load.");
105 } while (!isok(u.ux + u.dx, u.uy + u.dy) ||
106 IS_ROCK(levl[u.ux + u.dx][u.uy + u.dy].typ));
108 if (!isok(u.ux + u.dx, u.uy + u.dy)) {
114 ust = &levl[u.ux][u.uy];
118 (trap = t_at(u.ux + u.dx, u.uy + u.dy)) && trap->tseen)
120 if (u.ustuck && !u.uswallow && (u.ux + u.dx != u.ustuck->mx ||
121 u.uy + u.dy != u.ustuck->my)) {
122 if (dist(u.ustuck->mx, u.ustuck->my) > 2) {
123 /* perhaps it fled (or was teleported or ... ) */
127 pline("You cannot escape from it!");
129 pline("You cannot escape from %s!",
135 if (u.uswallow || (mtmp = m_at(u.ux + u.dx, u.uy + u.dy))) {
140 if (multi < 0) /* we just fainted */
143 /* try to attack; note that it might evade */
144 if (attack(u.uswallow ? u.ustuck : mtmp))
147 /* not attacking an animal, so we try to move */
149 if (u.utraptype == TT_PIT) {
150 pline("You are still in a pit.");
153 pline("You are caught in a beartrap.");
154 if ((u.dx && u.dy) || !rn2(5))
159 tmpr = &levl[u.ux + u.dx][u.uy + u.dy];
160 if (IS_ROCK(tmpr->typ) ||
161 (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))) {
166 while ((otmp = sobj_at(ENORMOUS_ROCK, u.ux + u.dx, u.uy + u.dy)) != NULL) {
168 xchar rx = u.ux + 2 * u.dx, ry = u.uy + 2 * u.dy;
170 if (isok(rx, ry) && !IS_ROCK(levl[rx][ry].typ) &&
171 (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
172 !sobj_at(ENORMOUS_ROCK, rx, ry)) {
174 pline("You hear a monster behind the rock.");
175 pline("Perhaps that's why you cannot move it.");
178 if ((ttmp = t_at(rx, ry)) != NULL)
179 switch (ttmp->ttyp) {
181 pline("You push the rock into a pit!");
184 pline("It completely fills the pit!");
187 pline("You push the rock and suddenly it disappears!");
191 if (levl[rx][ry].typ == POOL) {
192 levl[rx][ry].typ = ROOM;
195 pline("You push the rock into the water.");
196 pline("Now you can cross the water!");
203 atl(rx, ry, otmp->olet);
205 newsym(u.ux + u.dx, u.uy + u.dy);
208 static long lastmovetime;
209 /* note: this var contains garbage initially and
211 if (moves > lastmovetime + 2 || moves < lastmovetime)
212 pline("With great effort you move the enormous rock.");
213 lastmovetime = moves;
216 pline("You try to move the enormous rock, but in vain.");
218 if ((!invent || inv_weight() + 90 <= 0) &&
220 (IS_ROCK(levl[u.ux][u.uy + u.dy].typ)
221 && IS_ROCK(levl[u.ux + u.dx][u.uy].typ)))) {
222 pline("However, you can squeeze yourself into a small opening.");
228 if (u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy + u.dy].typ) &&
229 IS_ROCK(levl[u.ux + u.dx][u.uy].typ) &&
230 invent && inv_weight() + 40 > 0) {
231 pline("You are carrying too much to get through.");
236 DIST(u.ux + u.dx, u.uy + u.dy, uchain->ox, uchain->oy) > 2) {
237 if (carried(uball)) {
238 movobj(uchain, u.ux, u.uy);
242 if (DIST(u.ux + u.dx, u.uy + u.dy, uball->ox, uball->oy) < 3) {
243 /* leave ball, move chain under/over ball */
244 movobj(uchain, uball->ox, uball->oy);
248 if (inv_weight() + (int)uball->owt / 2 > 0) {
249 pline("You cannot %sdrag the heavy iron ball.",
250 invent ? "carry all that and also " : "");
255 movobj(uball, uchain->ox, uchain->oy);
256 unpobj(uball); /* BAH %% */
266 if (tmpr->typ == DOOR ||
267 (xupstair == u.ux && yupstair == u.uy) ||
268 (xdnstair == u.ux && ydnstair == u.uy))
272 if (tmpr->typ == POOL && !Levitation)
273 drown(); /* not necessarily fatal */
281 if (tmpr->typ == DOOR)
282 prl1(u.ux + u.dx, u.uy + u.dy);
283 else if (ust->typ == DOOR)
284 nose1(oldx - u.dx, oldy - u.dy);
287 prl1(u.ux + u.dx, u.uy + u.dy);
293 prl1(u.ux + u.dx, u.uy + u.dy);
294 if (tmpr->typ == DOOR) {
304 nose1(oldx - u.dx, oldy - u.dy);
312 dotrap(trap); /* fall into pit, arrow trap, etc. */
315 read_engr_at(u.ux, u.uy);
319 movobj(struct obj *obj, int ox, int oy)
321 /* Some dirty programming to get display right */
333 if (!g_at(u.ux, u.uy) && !o_at(u.ux, u.uy)) {
334 pline("There is nothing here to pick up.");
338 pline("You cannot reach the floor.");
349 struct obj *obj, *obj2;
354 while ((gold = g_at(u.ux, u.uy))) {
355 pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
356 u.ugold += gold->amount;
365 /* check for more than one object */
369 for (obj = fobj; obj; obj = obj->nobj)
370 if (obj->ox == u.ux && obj->oy == u.uy)
371 if (!Punished || obj != uchain)
376 pline("There are several objects here.");
379 for (obj = fobj; obj; obj = obj2) {
380 obj2 = obj->nobj; /* perhaps obj will be picked up */
381 if (obj->ox == u.ux && obj->oy == u.uy) {
385 /* do not pick up uchain */
386 if (Punished && obj == uchain)
392 pline("Pick up %s ? [ynaq]", doname(obj));
393 while (!strchr("ynaq ", (c = readchar())))
403 if (obj->otyp == DEAD_COCKATRICE && !uarmg) {
404 pline("Touching the dead cockatrice is a fatal mistake.");
405 pline("You turn to stone.");
406 killer = "cockatrice cadaver";
410 if (obj->otyp == SCR_SCARE_MONSTER) {
414 /* Note: perhaps the 1st pickup failed: you cannot
415 * carry anymore, and so we never dropped it -
416 * let's assume that treading on it twice also
417 * destroys the scroll */
418 pline("The scroll turns to dust as you pick it up.");
424 wt = inv_weight() + obj->owt;
427 /* see how many we can lift */
428 int savequan = obj->quan;
429 int iw = inv_weight();
431 for (qq = 1; qq < savequan; qq++) {
433 if (iw + weight(obj) > 0)
436 obj->quan = savequan;
438 /* we can carry qq of them */
441 pline("You can only carry %s of the %s lying here.",
442 (qq == 1) ? "one" : "some",
445 /* note: obj2 is set already, so we'll never
446 * encounter the other half; if it should be
447 * otherwise then write
448 * obj2 = splitobj(obj, qq);
453 pline("There %s %s here, but %s.",
454 (obj->quan == 1) ? "is" : "are",
456 !invent ? "it is too heavy for you to lift"
457 : "you cannot carry anymore");
461 if (inv_cnt() >= 52) {
462 pline("Your knapsack cannot accommodate anymore items.");
466 pline("You have a little trouble lifting");
470 addtobill(obj); /* sets obj->unpaid if necessary */
472 int pickquan = obj->quan;
474 if (!Blind) /* this is done by prinv(), */
475 obj->dknown = 1;/* but addinv() needs it */
476 /* already for merging */
477 obj = addinv(obj); /* might merge it with other objects */
478 mergquan = obj->quan;
479 obj->quan = pickquan; /* to fool prinv() */
481 obj->quan = mergquan;
487 /* stop running if we see something interesting */
488 /* turn around a corner if that is the only way we can proceed */
489 /* do not turn left or right twice */
493 int x, y, i, x0, y0, m0, i0 = 9;
494 int corrct = 0, noturn = 0;
497 /* suppress "used before set" message */
499 if (Blind || flags.run == 0)
501 if (flags.run == 1 && levl[u.ux][u.uy].typ == ROOM)
504 if (u.ux0 == u.ux + u.dx && u.uy0 == u.uy + u.dy)
507 for (x = u.ux - 1; x <= u.ux + 1; x++)
508 for (y = u.uy - 1; y <= u.uy + 1; y++) {
509 if (x == u.ux && y == u.uy)
513 if ((mtmp = m_at(x, y)) && !mtmp->mimic &&
514 (!mtmp->minvis || See_invisible)) {
516 (x == u.ux + u.dx && y == u.uy + u.dy))
518 } else /* invisible M cannot influence us */
520 if (x == u.ux - u.dx && y == u.uy - u.dy)
522 switch (levl[x][y].scrsym) {
529 if (x != u.ux && y != u.uy)
533 /* fall into next case */
536 if (flags.run == 1 || flags.run == 3) {
537 i = DIST(x, y, u.ux + u.dx, u.uy + u.dy);
541 DIST(x, y, x0, y0) != 1)
553 if (flags.run == 1) /* if you must */
555 if (x == u.ux + u.dx && y == u.uy + u.dy)
558 default: /* e.g. objects or trap or stairs */
569 if (corrct > 0 && (flags.run == 4 || flags.run == 5))
572 if (corrct > 1 && flags.run == 2)
574 if ((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
575 (corrct == 1 || (corrct == 2 && i0 == 1))) {
576 /* make sure that we do not turn too far */
578 if (u.dx == y0 - u.uy && u.dy == u.ux - x0)
579 i = 2; /* straight turn right */
581 i = -2; /* straight turn left */
582 } else if (u.dx && u.dy) {
583 if ((u.dx == u.dy && y0 == u.uy) ||
584 (u.dx != u.dy && y0 != u.uy))
585 i = -1; /* half turn left */
587 i = 1; /* half turn right */
589 if ((x0 - u.ux == y0 - u.uy && !u.dy) ||
590 (x0 - u.ux != y0 - u.uy && u.dy))
591 i = 1; /* half turn right */
593 i = -1; /* half turn left */
595 i += u.last_str_turn;
596 if (i <= 2 && i >= -2) {
598 u.dx = x0 - u.ux, u.dy = y0 - u.uy;
603 /* something like lookaround, but we are not running */
604 /* react only to monsters that might hit us */
612 for (x = u.ux - 1; x <= u.ux + 1; x++)
613 for (y = u.uy - 1; y <= u.uy + 1; y++) {
614 if (x == u.ux && y == u.uy)
616 if ((mtmp = m_at(x, y)) && !mtmp->mimic &&
619 !strchr("Ea", mtmp->data->mlet) &&
620 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
621 (!mtmp->minvis || See_invisible))
629 cansee(xchar x, xchar y)
631 int dx, dy, adx, ady, sdx, sdy, dmax, d;
640 if (d > u.uhorizon * u.uhorizon)
650 if (dx == 0 || dy == 0 || adx == ady) {
651 dmax = (dx == 0) ? ady : adx;
652 for (d = 1; d <= dmax; d++)
653 if (!rroom(sdx * d, sdy * d))
656 } else if (ady > adx) {
657 for (d = 1; d <= ady; d++) {
658 if (!rroom(sdx * ((d * adx) / ady), sdy * d) ||
659 !rroom(sdx * ((d * adx - 1) / ady + 1), sdy * d))
664 for (d = 1; d <= adx; d++) {
665 if (!rroom(sdx * d, sdy * ((d * ady) / adx)) ||
666 !rroom(sdx * d, sdy * ((d * ady - 1) / adx + 1)))
676 return (IS_ROOM(levl[u.ux + x][u.uy + y].typ));
682 cansee(xchar x, xchar y)
684 if (Blind || u.uswallow)
688 if (levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
698 return ((a > 0) ? 1 : (a == 0) ? 0 : -1);
711 for (y = u.uy - u.uhorizon; y <= u.uy + u.uhorizon; y++)
712 for (x = u.ux - u.uhorizon; x <= u.ux + u.uhorizon; x++) {
729 if (!levl[u.ux][u.uy].lit) {
735 for (seelx = u.ux; levl[seelx - 1][u.uy].lit; seelx--) ;
736 for (seehx = u.ux; levl[seehx + 1][u.uy].lit; seehx++) ;
737 for (seely = u.uy; levl[u.ux][seely - 1].lit; seely--) ;
738 for (seehy = u.uy; levl[u.ux][seehy + 1].lit; seehy++) ;
740 for (y = seely; y <= seehy; y++)
741 for (x = seelx; x <= seehx; x++)
744 if (!levl[u.ux][u.uy].lit) /* seems necessary elsewhere */
748 for (x = u.ux - 1; x <= u.ux + 1; x++)
751 for (x = u.ux - 1; x <= u.ux + 1; x++)
754 for (y = u.uy - 1; y <= u.uy + 1; y++)
757 for (y = u.uy - 1; y <= u.uy + 1; y++)
769 flags.mv = flags.run = 0;
781 else if (u.ustr < 17)
783 else if (u.ustr < 69) /* up to 18/50 */
785 else if (u.ustr < 118)
796 else if (u.ustr < 16)
798 else if (u.ustr < 18)
800 else if (u.ustr == 18) /* up to 18 */
802 else if (u.ustr < 94) /* up to 18/75 */
804 else if (u.ustr < 109) /* up to 18/90 */
806 else if (u.ustr < 118) /* up to 18/99 */
812 /* may kill you; cause may be poison or monster like 'A' */
826 losehp(int n, const char *knam)
829 if (u.uhp > u.uhpmax)
830 u.uhpmax = u.uhp; /* perhaps n was negative */
833 killer = knam; /* the thing that killed you */
839 losehp_m(int n, struct monst *mtmp)
848 losexp(void) /* hit by V or W */
853 pline("Goodbye level %u.", u.ulevel--);
866 struct obj *otmp = invent;
867 int wt = (u.ugold + 500) / 1000;
870 if (Levitation) /* pugh@cornell */
871 carrcap = MAX_CARR_CAP;
873 carrcap = 5 * (((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
874 if (carrcap > MAX_CARR_CAP)
875 carrcap = MAX_CARR_CAP;
876 if (Wounded_legs & LEFT_SIDE)
878 if (Wounded_legs & RIGHT_SIDE)
885 return (wt - carrcap);
891 struct obj *otmp = invent;
904 return (10 * (1L << (u.ulevel - 1)));