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.4 2005/05/22 03:37:05 y0netan1 Exp $ */
10 extern struct obj *addinv();
11 extern boolean hmon();
13 /* called on movement:
14 1. when throwing ball+chain far away
16 3. when walking out of a lit room
25 newsym(u.udisx, u.udisy);
33 for(x = u.ux-1; x < u.ux+2; x++)
34 for(y = u.uy-1; y < u.uy+2; y++) {
35 if(!isok(x, y)) continue;
37 if(!lev->lit && lev->scrsym == '.') {
46 in hack.eat.c: seeoff(0) - blind after eating rotten food
47 in hack.mon.c: seeoff(0) - blinded by a yellow light
48 in hack.mon.c: seeoff(1) - swallowed
49 in hack.do.c: seeoff(0) - blind after drinking potion
50 in hack.do.c: seeoff(1) - go up or down the stairs
51 in hack.trap.c:seeoff(1) - fall through trapdoor
53 seeoff(mode) /* 1 to redo @, 0 to leave them */
54 { /* 1 means misc movement, 0 means blindness */
60 levl[u.udisx][u.udisy].scrsym = news0(u.udisx,u.udisy);
68 for(x = u.ux-1; x < u.ux+2; x++)
69 for(y = u.uy-1; y < u.uy+2; y++) {
70 if(!isok(x, y)) continue;
72 if(!lev->lit && lev->scrsym == '.')
89 pline("You collapse under your load.");
101 } while(!isok(u.ux+u.dx, u.uy+u.dy) ||
102 IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ));
104 if(!isok(u.ux+u.dx, u.uy+u.dy)){
110 ust = &levl[u.ux][u.uy];
113 if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
115 if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
116 u.uy+u.dy != u.ustuck->my)) {
117 if(dist(u.ustuck->mx, u.ustuck->my) > 2){
118 /* perhaps it fled (or was teleported or ... ) */
121 if(Blind) pline("You cannot escape from it!");
122 else pline("You cannot escape from %s!",
128 if(u.uswallow || (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
133 if(multi < 0) return; /* we just fainted */
135 /* try to attack; note that it might evade */
136 if(attack(u.uswallow ? u.ustuck : mtmp))
139 /* not attacking an animal, so we try to move */
141 if(u.utraptype == TT_PIT) {
142 pline("You are still in a pit.");
145 pline("You are caught in a beartrap.");
146 if((u.dx && u.dy) || !rn2(5)) u.utrap--;
150 tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
151 if(IS_ROCK(tmpr->typ) ||
152 (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))){
157 while(otmp = sobj_at(ENORMOUS_ROCK, u.ux+u.dx, u.uy+u.dy)) {
158 xchar rx = u.ux+2*u.dx, ry = u.uy+2*u.dy;
161 if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
162 (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
163 !sobj_at(ENORMOUS_ROCK, rx, ry)) {
165 pline("You hear a monster behind the rock.");
166 pline("Perhaps that's why you cannot move it.");
169 if(ttmp = t_at(rx,ry))
172 pline("You push the rock into a pit!");
175 pline("It completely fills the pit!");
178 pline("You push the rock and suddenly it disappears!");
182 if(levl[rx][ry].typ == POOL) {
183 levl[rx][ry].typ = ROOM;
186 pline("You push the rock into the water.");
187 pline("Now you can cross the water!");
194 if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
195 if(Invisible) newsym(u.ux+u.dx, u.uy+u.dy);
197 { static long lastmovetime;
198 /* note: this var contains garbage initially and
200 if(moves > lastmovetime+2 || moves < lastmovetime)
201 pline("With great effort you move the enormous rock.");
202 lastmovetime = moves;
205 pline("You try to move the enormous rock, but in vain.");
207 if((!invent || inv_weight()+90 <= 0) &&
208 (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy+u.dy].typ)
209 && IS_ROCK(levl[u.ux+u.dx][u.uy].typ)))){
210 pline("However, you can squeeze yourself into a small opening.");
216 if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
217 IS_ROCK(levl[u.ux+u.dx][u.uy].typ) &&
218 invent && inv_weight()+40 > 0) {
219 pline("You are carrying too much to get through.");
224 DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy) > 2){
226 movobj(uchain, u.ux, u.uy);
230 if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy) < 3){
231 /* leave ball, move chain under/over ball */
232 movobj(uchain, uball->ox, uball->oy);
236 if(inv_weight() + (int) uball->owt/2 > 0) {
237 pline("You cannot %sdrag the heavy iron ball.",
238 invent ? "carry all that and also " : "");
243 movobj(uball, uchain->ox, uchain->oy);
244 unpobj(uball); /* BAH %% */
254 if(tmpr->typ == DOOR ||
255 (xupstair == u.ux && yupstair == u.uy) ||
256 (xdnstair == u.ux && ydnstair == u.uy))
260 if(tmpr->typ == POOL && !Levitation)
261 drown(); /* not necessarily fatal */
275 if(tmpr->typ == DOOR)
276 prl1(u.ux+u.dx,u.uy+u.dy);
277 else if(ust->typ == DOOR)
278 nose1(oldx-u.dx,oldy-u.dy);
281 prl1(u.ux+u.dx,u.uy+u.dy);
284 if(tmpr->lit) setsee();
286 prl1(u.ux+u.dx,u.uy+u.dy);
287 if(tmpr->typ == DOOR) {
297 nose1(oldx-u.dx,oldy-u.dy);
303 if(!flags.nopick) pickup(1);
304 if(trap) dotrap(trap); /* fall into pit, arrow trap, etc. */
306 if(!Blind) read_engr_at(u.ux,u.uy);
313 /* Some dirty programming to get display right */
323 if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
324 pline("There is nothing here to pick up.");
328 pline("You cannot reach the floor.");
338 struct obj *obj, *obj2;
341 if(Levitation) return;
342 while(gold = g_at(u.ux,u.uy)) {
343 pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
344 u.ugold += gold->amount;
347 if(flags.run) nomul(0);
348 if(Invisible) newsym(u.ux,u.uy);
351 /* check for more than one object */
355 for(obj = fobj; obj; obj = obj->nobj)
356 if(obj->ox == u.ux && obj->oy == u.uy)
357 if(!Punished || obj != uchain)
362 pline("There are several objects here.");
365 for(obj = fobj; obj; obj = obj2) {
366 obj2 = obj->nobj; /* perhaps obj will be picked up */
367 if(obj->ox == u.ux && obj->oy == u.uy) {
368 if(flags.run) nomul(0);
370 /* do not pick up uchain */
371 if(Punished && obj == uchain)
377 pline("Pick up %s ? [ynaq]", doname(obj));
378 while(!index("ynaq ", (c = readchar())))
381 if(c == 'n') continue;
382 if(c == 'a') all = 1;
385 if(obj->otyp == DEAD_COCKATRICE && !uarmg){
386 pline("Touching the dead cockatrice is a fatal mistake.");
387 pline("You turn to stone.");
388 killer = "cockatrice cadaver";
392 if(obj->otyp == SCR_SCARE_MONSTER){
393 if(!obj->spe) obj->spe = 1;
395 /* Note: perhaps the 1st pickup failed: you cannot
396 carry anymore, and so we never dropped it -
397 let's assume that treading on it twice also
398 destroys the scroll */
399 pline("The scroll turns to dust as you pick it up.");
405 wt = inv_weight() + obj->owt;
408 /* see how many we can lift */
409 extern struct obj *splitobj();
410 int savequan = obj->quan;
411 int iw = inv_weight();
413 for(qq = 1; qq < savequan; qq++){
415 if(iw + weight(obj) > 0)
418 obj->quan = savequan;
420 /* we can carry qq of them */
421 if(!qq) goto too_heavy;
422 pline("You can only carry %s of the %s lying here.",
423 (qq == 1) ? "one" : "some",
425 (void) splitobj(obj, qq);
426 /* note: obj2 is set already, so we'll never
427 * encounter the other half; if it should be
428 * otherwise then write
429 * obj2 = splitobj(obj,qq);
434 pline("There %s %s here, but %s.",
435 (obj->quan == 1) ? "is" : "are",
437 !invent ? "it is too heavy for you to lift"
438 : "you cannot carry anymore");
442 if(inv_cnt() >= 52) {
443 pline("Your knapsack cannot accomodate anymore items.");
446 if(wt > -5) pline("You have a little trouble lifting");
448 if(Invisible) newsym(u.ux,u.uy);
449 addtobill(obj); /* sets obj->unpaid if necessary */
450 { int pickquan = obj->quan;
452 if(!Blind) obj->dknown = 1; /* this is done by prinv(),
453 but addinv() needs it already for merging */
454 obj = addinv(obj); /* might merge it with other objects */
455 mergquan = obj->quan;
456 obj->quan = pickquan; /* to fool prinv() */
458 obj->quan = mergquan;
464 /* stop running if we see something interesting */
465 /* turn around a corner if that is the only way we can proceed */
466 /* do not turn left or right twice */
468 int x,y,i,x0,y0,m0,i0 = 9;
469 int corrct = 0, noturn = 0;
472 /* suppress "used before set" message */
475 if(Blind || flags.run == 0) return;
476 if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
478 if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
480 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
481 if(x == u.ux && y == u.uy) continue;
482 if(!levl[x][y].typ) continue;
483 if((mtmp = m_at(x,y)) && !mtmp->mimic &&
484 (!mtmp->minvis || See_invisible)){
485 if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
487 } else mtmp = 0; /* invisible M cannot influence us */
488 if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
489 switch(levl[x][y].scrsym){
496 if(x != u.ux && y != u.uy) break;
497 if(flags.run != 1) goto stop;
498 /* fall into next case */
501 if(flags.run == 1 || flags.run == 3) {
502 i = DIST(x,y,u.ux+u.dx,u.uy+u.dy);
504 if(corrct == 1 && DIST(x,y,x0,y0) != 1)
516 if(flags.run == 1) goto corr; /* if you must */
517 if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
519 default: /* e.g. objects or trap or stairs */
520 if(flags.run == 1) goto corr;
521 if(mtmp) break; /* d */
528 if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
530 if(corrct > 1 && flags.run == 2) goto stop;
531 if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
532 (corrct == 1 || (corrct == 2 && i0 == 1))) {
533 /* make sure that we do not turn too far */
535 if(u.dx == y0-u.uy && u.dy == u.ux-x0)
536 i = 2; /* straight turn right */
538 i = -2; /* straight turn left */
539 } else if(u.dx && u.dy) {
540 if((u.dx == u.dy && y0 == u.uy) ||
541 (u.dx != u.dy && y0 != u.uy))
542 i = -1; /* half turn left */
544 i = 1; /* half turn right */
546 if((x0-u.ux == y0-u.uy && !u.dy) ||
547 (x0-u.ux != y0-u.uy && u.dy))
548 i = 1; /* half turn right */
550 i = -1; /* half turn left */
552 i += u.last_str_turn;
553 if(i <= 2 && i >= -2) {
555 u.dx = x0-u.ux, u.dy = y0-u.uy;
560 /* something like lookaround, but we are not running */
561 /* react only to monsters that might hit us */
566 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
567 if(x == u.ux && y == u.uy) continue;
568 if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
569 !mtmp->mpeaceful && !index("Ea", mtmp->data->mlet) &&
570 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
571 (!mtmp->minvis || See_invisible))
578 cansee(x,y) xchar x,y; {
579 int dx,dy,adx,ady,sdx,sdy,dmax,d;
581 if(!isok(x,y)) return(0);
584 if(d > u.uhorizon*u.uhorizon) return(0);
587 dx = x - u.ux; adx = abs(dx); sdx = sgn(dx);
588 dy = y - u.uy; ady = abs(dy); sdy = sgn(dy);
589 if(dx == 0 || dy == 0 || adx == ady){
590 dmax = (dx == 0) ? ady : adx;
591 for(d = 1; d <= dmax; d++)
592 if(!rroom(sdx*d,sdy*d))
595 } else if(ady > adx){
596 for(d = 1; d <= ady; d++){
597 if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
598 !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
603 for(d = 1; d <= adx; d++){
604 if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
605 !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
612 rroom(x,y) int x,y; {
613 return(IS_ROOM(levl[u.ux+x][u.uy+y].typ));
618 cansee(x,y) xchar x,y; {
619 if(Blind || u.uswallow) return(0);
620 if(dist(x,y) < 3) return(1);
621 if(levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
622 y <= seehy) return(1);
628 return((a > 0) ? 1 : (a == 0) ? 0 : -1);
640 for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
641 for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
657 if(!levl[u.ux][u.uy].lit) {
663 for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--);
664 for(seehx = u.ux; levl[seehx+1][u.uy].lit; seehx++);
665 for(seely = u.uy; levl[u.ux][seely-1].lit; seely--);
666 for(seehy = u.uy; levl[u.ux][seehy+1].lit; seehy++);
668 for(y = seely; y <= seehy; y++)
669 for(x = seelx; x <= seehx; x++) {
672 if(!levl[u.ux][u.uy].lit) seehx = 0; /* seems necessary elsewhere */
674 if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
675 if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
676 if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
677 if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
685 if(multi < 0) return;
687 flags.mv = flags.run = 0;
692 if(u.ustr == 3) return(-3);
693 else if(u.ustr < 6) return(-2);
694 else if(u.ustr < 8) return(-1);
695 else if(u.ustr < 17) return(0);
696 else if(u.ustr < 69) return(1); /* up to 18/50 */
697 else if(u.ustr < 118) return(2);
703 if(u.ustr < 6) return(-1);
704 else if(u.ustr < 16) return(0);
705 else if(u.ustr < 18) return(1);
706 else if(u.ustr == 18) return(2); /* up to 18 */
707 else if(u.ustr < 94) return(3); /* up to 18/75 */
708 else if(u.ustr < 109) return(4); /* up to 18/90 */
709 else if(u.ustr < 118) return(5); /* up to 18/99 */
713 losestr(num) /* may kill you; cause may be poison or monster like 'A' */
731 u.uhpmax = u.uhp; /* perhaps n was negative */
734 killer = knam; /* the thing that killed you */
749 losexp() /* hit by V or W */
752 extern long newuexp();
755 pline("Goodbye level %u.", u.ulevel--);
766 struct obj *otmp = invent;
767 int wt = (u.ugold + 500)/1000;
769 if(Levitation) /* pugh@cornell */
770 carrcap = MAX_CARR_CAP;
772 carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
773 if(carrcap > MAX_CARR_CAP) carrcap = MAX_CARR_CAP;
774 if(Wounded_legs & LEFT_SIDE) carrcap -= 10;
775 if(Wounded_legs & RIGHT_SIDE) carrcap -= 10;
781 return(wt - carrcap);
785 struct obj *otmp = invent;
797 return(10*(1L << (u.ulevel-1)));