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.5 2006/08/21 19:45:32 pavalos 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++) {
32 if(!isok(x, y)) continue;
34 if(!lev->lit && lev->scrsym == '.') {
43 in hack.eat.c: seeoff(0) - blind after eating rotten food
44 in hack.mon.c: seeoff(0) - blinded by a yellow light
45 in hack.mon.c: seeoff(1) - swallowed
46 in hack.do.c: seeoff(0) - blind after drinking potion
47 in hack.do.c: seeoff(1) - go up or down the stairs
48 in hack.trap.c:seeoff(1) - fall through trapdoor
51 seeoff(bool mode) /* 1 to redo @, 0 to leave them */
52 { /* 1 means misc movement, 0 means blindness */
58 levl[u.udisx][u.udisy].scrsym = news0(u.udisx,u.udisy);
66 for(x = u.ux-1; x < u.ux+2; x++)
67 for(y = u.uy-1; y < u.uy+2; y++) {
68 if(!isok(x, y)) continue;
70 if(!lev->lit && lev->scrsym == '.')
80 struct monst *mtmp = NULL;
82 struct trap *trap = NULL;
88 pline("You collapse under your load.");
100 } while(!isok(u.ux+u.dx, u.uy+u.dy) ||
101 IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ));
103 if(!isok(u.ux+u.dx, u.uy+u.dy)){
109 ust = &levl[u.ux][u.uy];
112 if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
114 if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
115 u.uy+u.dy != u.ustuck->my)) {
116 if(dist(u.ustuck->mx, u.ustuck->my) > 2){
117 /* perhaps it fled (or was teleported or ... ) */
120 if(Blind) pline("You cannot escape from it!");
121 else pline("You cannot escape from %s!",
127 if(u.uswallow || (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
132 if(multi < 0) return; /* we just fainted */
134 /* try to attack; note that it might evade */
135 if(attack(u.uswallow ? u.ustuck : mtmp))
138 /* not attacking an animal, so we try to move */
140 if(u.utraptype == TT_PIT) {
141 pline("You are still in a pit.");
144 pline("You are caught in a beartrap.");
145 if((u.dx && u.dy) || !rn2(5)) u.utrap--;
149 tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
150 if(IS_ROCK(tmpr->typ) ||
151 (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))){
156 while((otmp = sobj_at(ENORMOUS_ROCK, u.ux+u.dx, u.uy+u.dy))) {
157 xchar rx = u.ux+2*u.dx, ry = u.uy+2*u.dy;
160 if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
161 (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
162 !sobj_at(ENORMOUS_ROCK, rx, ry)) {
164 pline("You hear a monster behind the rock.");
165 pline("Perhaps that's why you cannot move it.");
168 if((ttmp = t_at(rx,ry)))
171 pline("You push the rock into a pit!");
174 pline("It completely fills the pit!");
177 pline("You push the rock and suddenly it disappears!");
181 if(levl[rx][ry].typ == POOL) {
182 levl[rx][ry].typ = ROOM;
185 pline("You push the rock into the water.");
186 pline("Now you can cross the water!");
192 if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
193 if(Invisible) newsym(u.ux+u.dx, u.uy+u.dy);
195 { static long lastmovetime;
196 /* note: this var contains garbage initially and
198 if(moves > lastmovetime+2 || moves < lastmovetime)
199 pline("With great effort you move the enormous rock.");
200 lastmovetime = moves;
203 pline("You try to move the enormous rock, but in vain.");
205 if((!invent || inv_weight()+90 <= 0) &&
206 (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy+u.dy].typ)
207 && IS_ROCK(levl[u.ux+u.dx][u.uy].typ)))){
208 pline("However, you can squeeze yourself into a small opening.");
214 if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
215 IS_ROCK(levl[u.ux+u.dx][u.uy].typ) &&
216 invent && inv_weight()+40 > 0) {
217 pline("You are carrying too much to get through.");
222 DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy) > 2){
224 movobj(uchain, u.ux, u.uy);
228 if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy) < 3){
229 /* leave ball, move chain under/over ball */
230 movobj(uchain, uball->ox, uball->oy);
234 if(inv_weight() + (int) uball->owt/2 > 0) {
235 pline("You cannot %sdrag the heavy iron ball.",
236 invent ? "carry all that and also " : "");
241 movobj(uball, uchain->ox, uchain->oy);
242 unpobj(uball); /* BAH %% */
252 if(tmpr->typ == DOOR ||
253 (xupstair == u.ux && yupstair == u.uy) ||
254 (xdnstair == u.ux && ydnstair == u.uy))
258 if(tmpr->typ == POOL && !Levitation)
259 drown(); /* not necessarily fatal */
267 if(tmpr->typ == DOOR)
268 prl1(u.ux+u.dx,u.uy+u.dy);
269 else if(ust->typ == DOOR)
270 nose1(oldx-u.dx,oldy-u.dy);
273 prl1(u.ux+u.dx,u.uy+u.dy);
276 if(tmpr->lit) setsee();
278 prl1(u.ux+u.dx,u.uy+u.dy);
279 if(tmpr->typ == DOOR) {
289 nose1(oldx-u.dx,oldy-u.dy);
295 if(!flags.nopick) pickup(1);
296 if(trap) dotrap(trap); /* fall into pit, arrow trap, etc. */
298 if(!Blind) read_engr_at(u.ux,u.uy);
302 movobj(struct obj *obj, int ox, int oy)
304 /* Some dirty programming to get display right */
316 if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
317 pline("There is nothing here to pick up.");
321 pline("You cannot reach the floor.");
332 struct obj *obj, *obj2;
335 if(Levitation) return;
336 while((gold = g_at(u.ux,u.uy))) {
337 pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
338 u.ugold += gold->amount;
341 if(flags.run) nomul(0);
342 if(Invisible) newsym(u.ux,u.uy);
345 /* check for more than one object */
349 for(obj = fobj; obj; obj = obj->nobj)
350 if(obj->ox == u.ux && obj->oy == u.uy)
351 if(!Punished || obj != uchain)
356 pline("There are several objects here.");
359 for(obj = fobj; obj; obj = obj2) {
360 obj2 = obj->nobj; /* perhaps obj will be picked up */
361 if(obj->ox == u.ux && obj->oy == u.uy) {
362 if(flags.run) nomul(0);
364 /* do not pick up uchain */
365 if(Punished && obj == uchain)
371 pline("Pick up %s ? [ynaq]", doname(obj));
372 while(!index("ynaq ", (c = readchar())))
375 if(c == 'n') continue;
376 if(c == 'a') all = 1;
379 if(obj->otyp == DEAD_COCKATRICE && !uarmg){
380 pline("Touching the dead cockatrice is a fatal mistake.");
381 pline("You turn to stone.");
382 killer = "cockatrice cadaver";
386 if(obj->otyp == SCR_SCARE_MONSTER){
387 if(!obj->spe) obj->spe = 1;
389 /* Note: perhaps the 1st pickup failed: you cannot
390 carry anymore, and so we never dropped it -
391 let's assume that treading on it twice also
392 destroys the scroll */
393 pline("The scroll turns to dust as you pick it up.");
399 wt = inv_weight() + obj->owt;
402 /* see how many we can lift */
403 int savequan = obj->quan;
404 int iw = inv_weight();
406 for(qq = 1; qq < savequan; qq++){
408 if(iw + weight(obj) > 0)
411 obj->quan = savequan;
413 /* we can carry qq of them */
414 if(!qq) goto too_heavy;
415 pline("You can only carry %s of the %s lying here.",
416 (qq == 1) ? "one" : "some",
419 /* note: obj2 is set already, so we'll never
420 * encounter the other half; if it should be
421 * otherwise then write
422 * obj2 = splitobj(obj,qq);
427 pline("There %s %s here, but %s.",
428 (obj->quan == 1) ? "is" : "are",
430 !invent ? "it is too heavy for you to lift"
431 : "you cannot carry anymore");
435 if(inv_cnt() >= 52) {
436 pline("Your knapsack cannot accomodate anymore items.");
439 if(wt > -5) pline("You have a little trouble lifting");
441 if(Invisible) newsym(u.ux,u.uy);
442 addtobill(obj); /* sets obj->unpaid if necessary */
443 { int pickquan = obj->quan;
445 if(!Blind) obj->dknown = 1; /* this is done by prinv(),
446 but addinv() needs it already for merging */
447 obj = addinv(obj); /* might merge it with other objects */
448 mergquan = obj->quan;
449 obj->quan = pickquan; /* to fool prinv() */
451 obj->quan = mergquan;
457 /* stop running if we see something interesting */
458 /* turn around a corner if that is the only way we can proceed */
459 /* do not turn left or right twice */
463 int x,y,i,x0,y0,m0,i0 = 9;
464 int corrct = 0, noturn = 0;
466 /* suppress "used before set" message */
468 if(Blind || flags.run == 0) return;
469 if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
471 if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
473 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
474 if(x == u.ux && y == u.uy) continue;
475 if(!levl[x][y].typ) continue;
476 if((mtmp = m_at(x,y)) && !mtmp->mimic &&
477 (!mtmp->minvis || See_invisible)){
478 if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
480 } else mtmp = 0; /* invisible M cannot influence us */
481 if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
482 switch(levl[x][y].scrsym){
489 if(x != u.ux && y != u.uy) break;
490 if(flags.run != 1) goto stop;
491 /* fall into next case */
494 if(flags.run == 1 || flags.run == 3) {
495 i = DIST(x,y,u.ux+u.dx,u.uy+u.dy);
497 if(corrct == 1 && DIST(x,y,x0,y0) != 1)
509 if(flags.run == 1) goto corr; /* if you must */
510 if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
512 default: /* e.g. objects or trap or stairs */
513 if(flags.run == 1) goto corr;
514 if(mtmp) break; /* d */
521 if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
523 if(corrct > 1 && flags.run == 2) goto stop;
524 if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
525 (corrct == 1 || (corrct == 2 && i0 == 1))) {
526 /* make sure that we do not turn too far */
528 if(u.dx == y0-u.uy && u.dy == u.ux-x0)
529 i = 2; /* straight turn right */
531 i = -2; /* straight turn left */
532 } else if(u.dx && u.dy) {
533 if((u.dx == u.dy && y0 == u.uy) ||
534 (u.dx != u.dy && y0 != u.uy))
535 i = -1; /* half turn left */
537 i = 1; /* half turn right */
539 if((x0-u.ux == y0-u.uy && !u.dy) ||
540 (x0-u.ux != y0-u.uy && u.dy))
541 i = 1; /* half turn right */
543 i = -1; /* half turn left */
545 i += u.last_str_turn;
546 if(i <= 2 && i >= -2) {
548 u.dx = x0-u.ux, u.dy = y0-u.uy;
553 /* something like lookaround, but we are not running */
554 /* react only to monsters that might hit us */
561 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
562 if(x == u.ux && y == u.uy) continue;
563 if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
564 !mtmp->mpeaceful && !index("Ea", mtmp->data->mlet) &&
565 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
566 (!mtmp->minvis || See_invisible))
574 cansee(xchar x, xchar y)
576 int dx,dy,adx,ady,sdx,sdy,dmax,d;
578 if(!isok(x,y)) return(0);
581 if(d > u.uhorizon*u.uhorizon) return(0);
584 dx = x - u.ux; adx = abs(dx); sdx = sgn(dx);
585 dy = y - u.uy; ady = abs(dy); sdy = sgn(dy);
586 if(dx == 0 || dy == 0 || adx == ady){
587 dmax = (dx == 0) ? ady : adx;
588 for(d = 1; d <= dmax; d++)
589 if(!rroom(sdx*d,sdy*d))
592 } else if(ady > adx){
593 for(d = 1; d <= ady; d++){
594 if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
595 !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
600 for(d = 1; d <= adx; d++){
601 if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
602 !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
612 return(IS_ROOM(levl[u.ux+x][u.uy+y].typ));
618 cansee(xchar x, xchar y)
620 if(Blind || u.uswallow) return(0);
621 if(dist(x,y) < 3) return(1);
622 if(levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
623 y <= seehy) return(1);
631 return((a > 0) ? 1 : (a == 0) ? 0 : -1);
644 for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
645 for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
662 if(!levl[u.ux][u.uy].lit) {
668 for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--);
669 for(seehx = u.ux; levl[seehx+1][u.uy].lit; seehx++);
670 for(seely = u.uy; levl[u.ux][seely-1].lit; seely--);
671 for(seehy = u.uy; levl[u.ux][seehy+1].lit; seehy++);
673 for(y = seely; y <= seehy; y++)
674 for(x = seelx; x <= seehx; x++) {
677 if(!levl[u.ux][u.uy].lit) seehx = 0; /* seems necessary elsewhere */
679 if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
680 if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
681 if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
682 if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
690 if(multi < 0) return;
692 flags.mv = flags.run = 0;
698 if(u.ustr == 3) return(-3);
699 else if(u.ustr < 6) return(-2);
700 else if(u.ustr < 8) return(-1);
701 else if(u.ustr < 17) return(0);
702 else if(u.ustr < 69) return(1); /* up to 18/50 */
703 else if(u.ustr < 118) return(2);
710 if(u.ustr < 6) return(-1);
711 else if(u.ustr < 16) return(0);
712 else if(u.ustr < 18) return(1);
713 else if(u.ustr == 18) return(2); /* up to 18 */
714 else if(u.ustr < 94) return(3); /* up to 18/75 */
715 else if(u.ustr < 109) return(4); /* up to 18/90 */
716 else if(u.ustr < 118) return(5); /* up to 18/99 */
721 losestr(int num) /* may kill you; cause may be poison or monster like 'A' */
733 losehp(int n, const char *knam)
737 u.uhpmax = u.uhp; /* perhaps n was negative */
740 killer = knam; /* the thing that killed you */
746 losehp_m(int n, struct monst *mtmp)
755 losexp(void) /* hit by V or W */
760 pline("Goodbye level %u.", u.ulevel--);
773 struct obj *otmp = invent;
774 int wt = (u.ugold + 500)/1000;
776 if(Levitation) /* pugh@cornell */
777 carrcap = MAX_CARR_CAP;
779 carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
780 if(carrcap > MAX_CARR_CAP) carrcap = MAX_CARR_CAP;
781 if(Wounded_legs & LEFT_SIDE) carrcap -= 10;
782 if(Wounded_legs & RIGHT_SIDE) carrcap -= 10;
788 return(wt - carrcap);
794 struct obj *otmp = invent;
806 return(10*(1L << (u.ulevel-1)));