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.2 2003/06/17 04:25:24 dillon Exp $ */
10 extern char *nomovemsg;
11 extern char *exclam();
12 extern struct obj *addinv();
13 extern boolean hmon();
15 /* called on movement:
16 1. when throwing ball+chain far away
18 3. when walking out of a lit room
27 newsym(u.udisx, u.udisy);
35 for(x = u.ux-1; x < u.ux+2; x++)
36 for(y = u.uy-1; y < u.uy+2; y++) {
37 if(!isok(x, y)) continue;
39 if(!lev->lit && lev->scrsym == '.') {
48 in hack.eat.c: seeoff(0) - blind after eating rotten food
49 in hack.mon.c: seeoff(0) - blinded by a yellow light
50 in hack.mon.c: seeoff(1) - swallowed
51 in hack.do.c: seeoff(0) - blind after drinking potion
52 in hack.do.c: seeoff(1) - go up or down the stairs
53 in hack.trap.c:seeoff(1) - fall through trapdoor
55 seeoff(mode) /* 1 to redo @, 0 to leave them */
56 { /* 1 means misc movement, 0 means blindness */
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++) {
72 if(!isok(x, y)) continue;
74 if(!lev->lit && lev->scrsym == '.')
91 pline("You collapse under your load.");
103 } while(!isok(u.ux+u.dx, u.uy+u.dy) ||
104 IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ));
106 if(!isok(u.ux+u.dx, u.uy+u.dy)){
112 ust = &levl[u.ux][u.uy];
115 if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
117 if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
118 u.uy+u.dy != u.ustuck->my)) {
119 if(dist(u.ustuck->mx, u.ustuck->my) > 2){
120 /* perhaps it fled (or was teleported or ... ) */
123 if(Blind) pline("You cannot escape from it!");
124 else pline("You cannot escape from %s!",
130 if(u.uswallow || (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
135 if(multi < 0) return; /* we just fainted */
137 /* try to attack; note that it might evade */
138 if(attack(u.uswallow ? u.ustuck : mtmp))
141 /* not attacking an animal, so we try to move */
143 if(u.utraptype == TT_PIT) {
144 pline("You are still in a pit.");
147 pline("You are caught in a beartrap.");
148 if((u.dx && u.dy) || !rn2(5)) u.utrap--;
152 tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
153 if(IS_ROCK(tmpr->typ) ||
154 (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))){
159 while(otmp = sobj_at(ENORMOUS_ROCK, u.ux+u.dx, u.uy+u.dy)) {
160 xchar rx = u.ux+2*u.dx, ry = u.uy+2*u.dy;
163 if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
164 (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
165 !sobj_at(ENORMOUS_ROCK, rx, ry)) {
167 pline("You hear a monster behind the rock.");
168 pline("Perhaps that's why you cannot move it.");
171 if(ttmp = t_at(rx,ry))
174 pline("You push the rock into a pit!");
177 pline("It completely fills the pit!");
180 pline("You push the rock and suddenly it disappears!");
184 if(levl[rx][ry].typ == POOL) {
185 levl[rx][ry].typ = ROOM;
188 pline("You push the rock into the water.");
189 pline("Now you can cross the water!");
196 if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
197 if(Invisible) newsym(u.ux+u.dx, u.uy+u.dy);
199 { static long lastmovetime;
200 /* note: this var contains garbage initially and
202 if(moves > lastmovetime+2 || moves < lastmovetime)
203 pline("With great effort you move the enormous rock.");
204 lastmovetime = moves;
207 pline("You try to move the enormous rock, but in vain.");
209 if((!invent || inv_weight()+90 <= 0) &&
210 (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy+u.dy].typ)
211 && IS_ROCK(levl[u.ux+u.dx][u.uy].typ)))){
212 pline("However, you can squeeze yourself into a small opening.");
218 if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
219 IS_ROCK(levl[u.ux+u.dx][u.uy].typ) &&
220 invent && inv_weight()+40 > 0) {
221 pline("You are carrying too much to get through.");
226 DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy) > 2){
228 movobj(uchain, u.ux, u.uy);
232 if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy) < 3){
233 /* leave ball, move chain under/over ball */
234 movobj(uchain, uball->ox, uball->oy);
238 if(inv_weight() + (int) uball->owt/2 > 0) {
239 pline("You cannot %sdrag the heavy iron ball.",
240 invent ? "carry all that and also " : "");
245 movobj(uball, uchain->ox, uchain->oy);
246 unpobj(uball); /* BAH %% */
256 if(tmpr->typ == DOOR ||
257 (xupstair == u.ux && yupstair == u.uy) ||
258 (xdnstair == u.ux && ydnstair == u.uy))
262 if(tmpr->typ == POOL && !Levitation)
263 drown(); /* not necessarily fatal */
277 if(tmpr->typ == DOOR)
278 prl1(u.ux+u.dx,u.uy+u.dy);
279 else if(ust->typ == DOOR)
280 nose1(oldx-u.dx,oldy-u.dy);
283 prl1(u.ux+u.dx,u.uy+u.dy);
286 if(tmpr->lit) setsee();
288 prl1(u.ux+u.dx,u.uy+u.dy);
289 if(tmpr->typ == DOOR) {
299 nose1(oldx-u.dx,oldy-u.dy);
305 if(!flags.nopick) pickup(1);
306 if(trap) dotrap(trap); /* fall into pit, arrow trap, etc. */
308 if(!Blind) read_engr_at(u.ux,u.uy);
315 /* Some dirty programming to get display right */
325 if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
326 pline("There is nothing here to pick up.");
330 pline("You cannot reach the floor.");
340 struct obj *obj, *obj2;
343 if(Levitation) return;
344 while(gold = g_at(u.ux,u.uy)) {
345 pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
346 u.ugold += gold->amount;
349 if(flags.run) nomul(0);
350 if(Invisible) newsym(u.ux,u.uy);
353 /* check for more than one object */
357 for(obj = fobj; obj; obj = obj->nobj)
358 if(obj->ox == u.ux && obj->oy == u.uy)
359 if(!Punished || obj != uchain)
364 pline("There are several objects here.");
367 for(obj = fobj; obj; obj = obj2) {
368 obj2 = obj->nobj; /* perhaps obj will be picked up */
369 if(obj->ox == u.ux && obj->oy == u.uy) {
370 if(flags.run) nomul(0);
372 /* do not pick up uchain */
373 if(Punished && obj == uchain)
379 pline("Pick up %s ? [ynaq]", doname(obj));
380 while(!index("ynaq ", (c = readchar())))
383 if(c == 'n') continue;
384 if(c == 'a') all = 1;
387 if(obj->otyp == DEAD_COCKATRICE && !uarmg){
388 pline("Touching the dead cockatrice is a fatal mistake.");
389 pline("You turn to stone.");
390 killer = "cockatrice cadaver";
394 if(obj->otyp == SCR_SCARE_MONSTER){
395 if(!obj->spe) obj->spe = 1;
397 /* Note: perhaps the 1st pickup failed: you cannot
398 carry anymore, and so we never dropped it -
399 let's assume that treading on it twice also
400 destroys the scroll */
401 pline("The scroll turns to dust as you pick it up.");
407 wt = inv_weight() + obj->owt;
410 /* see how many we can lift */
411 extern struct obj *splitobj();
412 int savequan = obj->quan;
413 int iw = inv_weight();
415 for(qq = 1; qq < savequan; qq++){
417 if(iw + weight(obj) > 0)
420 obj->quan = savequan;
422 /* we can carry qq of them */
423 if(!qq) goto too_heavy;
424 pline("You can only carry %s of the %s lying here.",
425 (qq == 1) ? "one" : "some",
427 (void) splitobj(obj, qq);
428 /* note: obj2 is set already, so we'll never
429 * encounter the other half; if it should be
430 * otherwise then write
431 * obj2 = splitobj(obj,qq);
436 pline("There %s %s here, but %s.",
437 (obj->quan == 1) ? "is" : "are",
439 !invent ? "it is too heavy for you to lift"
440 : "you cannot carry anymore");
444 if(inv_cnt() >= 52) {
445 pline("Your knapsack cannot accomodate anymore items.");
448 if(wt > -5) pline("You have a little trouble lifting");
450 if(Invisible) newsym(u.ux,u.uy);
451 addtobill(obj); /* sets obj->unpaid if necessary */
452 { int pickquan = obj->quan;
454 if(!Blind) obj->dknown = 1; /* this is done by prinv(),
455 but addinv() needs it already for merging */
456 obj = addinv(obj); /* might merge it with other objects */
457 mergquan = obj->quan;
458 obj->quan = pickquan; /* to fool prinv() */
460 obj->quan = mergquan;
466 /* stop running if we see something interesting */
467 /* turn around a corner if that is the only way we can proceed */
468 /* do not turn left or right twice */
470 int x,y,i,x0,y0,m0,i0 = 9;
471 int corrct = 0, noturn = 0;
474 /* suppress "used before set" message */
477 if(Blind || flags.run == 0) return;
478 if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
480 if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
482 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
483 if(x == u.ux && y == u.uy) continue;
484 if(!levl[x][y].typ) continue;
485 if((mtmp = m_at(x,y)) && !mtmp->mimic &&
486 (!mtmp->minvis || See_invisible)){
487 if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
489 } else mtmp = 0; /* invisible M cannot influence us */
490 if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
491 switch(levl[x][y].scrsym){
498 if(x != u.ux && y != u.uy) break;
499 if(flags.run != 1) goto stop;
500 /* fall into next case */
503 if(flags.run == 1 || flags.run == 3) {
504 i = DIST(x,y,u.ux+u.dx,u.uy+u.dy);
506 if(corrct == 1 && DIST(x,y,x0,y0) != 1)
518 if(flags.run == 1) goto corr; /* if you must */
519 if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
521 default: /* e.g. objects or trap or stairs */
522 if(flags.run == 1) goto corr;
523 if(mtmp) break; /* d */
530 if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
532 if(corrct > 1 && flags.run == 2) goto stop;
533 if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
534 (corrct == 1 || (corrct == 2 && i0 == 1))) {
535 /* make sure that we do not turn too far */
537 if(u.dx == y0-u.uy && u.dy == u.ux-x0)
538 i = 2; /* straight turn right */
540 i = -2; /* straight turn left */
541 } else if(u.dx && u.dy) {
542 if((u.dx == u.dy && y0 == u.uy) ||
543 (u.dx != u.dy && y0 != u.uy))
544 i = -1; /* half turn left */
546 i = 1; /* half turn right */
548 if((x0-u.ux == y0-u.uy && !u.dy) ||
549 (x0-u.ux != y0-u.uy && u.dy))
550 i = 1; /* half turn right */
552 i = -1; /* half turn left */
554 i += u.last_str_turn;
555 if(i <= 2 && i >= -2) {
557 u.dx = x0-u.ux, u.dy = y0-u.uy;
562 /* something like lookaround, but we are not running */
563 /* react only to monsters that might hit us */
568 for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
569 if(x == u.ux && y == u.uy) continue;
570 if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
571 !mtmp->mpeaceful && !index("Ea", mtmp->data->mlet) &&
572 !mtmp->mfroz && !mtmp->msleep && /* aplvax!jcn */
573 (!mtmp->minvis || See_invisible))
580 cansee(x,y) xchar x,y; {
581 int dx,dy,adx,ady,sdx,sdy,dmax,d;
583 if(!isok(x,y)) return(0);
586 if(d > u.uhorizon*u.uhorizon) return(0);
589 dx = x - u.ux; adx = abs(dx); sdx = sgn(dx);
590 dy = y - u.uy; ady = abs(dy); sdy = sgn(dy);
591 if(dx == 0 || dy == 0 || adx == ady){
592 dmax = (dx == 0) ? ady : adx;
593 for(d = 1; d <= dmax; d++)
594 if(!rroom(sdx*d,sdy*d))
597 } else if(ady > adx){
598 for(d = 1; d <= ady; d++){
599 if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
600 !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
605 for(d = 1; d <= adx; d++){
606 if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
607 !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
614 rroom(x,y) int x,y; {
615 return(IS_ROOM(levl[u.ux+x][u.uy+y].typ));
620 cansee(x,y) xchar x,y; {
621 if(Blind || u.uswallow) return(0);
622 if(dist(x,y) < 3) return(1);
623 if(levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
624 y <= seehy) return(1);
630 return((a > 0) ? 1 : (a == 0) ? 0 : -1);
642 for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
643 for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
659 if(!levl[u.ux][u.uy].lit) {
665 for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--);
666 for(seehx = u.ux; levl[seehx+1][u.uy].lit; seehx++);
667 for(seely = u.uy; levl[u.ux][seely-1].lit; seely--);
668 for(seehy = u.uy; levl[u.ux][seehy+1].lit; seehy++);
670 for(y = seely; y <= seehy; y++)
671 for(x = seelx; x <= seehx; x++) {
674 if(!levl[u.ux][u.uy].lit) seehx = 0; /* seems necessary elsewhere */
676 if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
677 if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
678 if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
679 if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
687 if(multi < 0) return;
689 flags.mv = flags.run = 0;
694 if(u.ustr == 3) return(-3);
695 else if(u.ustr < 6) return(-2);
696 else if(u.ustr < 8) return(-1);
697 else if(u.ustr < 17) return(0);
698 else if(u.ustr < 69) return(1); /* up to 18/50 */
699 else if(u.ustr < 118) return(2);
705 if(u.ustr < 6) return(-1);
706 else if(u.ustr < 16) return(0);
707 else if(u.ustr < 18) return(1);
708 else if(u.ustr == 18) return(2); /* up to 18 */
709 else if(u.ustr < 94) return(3); /* up to 18/75 */
710 else if(u.ustr < 109) return(4); /* up to 18/90 */
711 else if(u.ustr < 118) return(5); /* up to 18/99 */
715 losestr(num) /* may kill you; cause may be poison or monster like 'A' */
733 u.uhpmax = u.uhp; /* perhaps n was negative */
736 killer = knam; /* the thing that killed you */
751 losexp() /* hit by V or W */
754 extern long newuexp();
757 pline("Goodbye level %u.", u.ulevel--);
768 struct obj *otmp = invent;
769 int wt = (u.ugold + 500)/1000;
771 if(Levitation) /* pugh@cornell */
772 carrcap = MAX_CARR_CAP;
774 carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
775 if(carrcap > MAX_CARR_CAP) carrcap = MAX_CARR_CAP;
776 if(Wounded_legs & LEFT_SIDE) carrcap -= 10;
777 if(Wounded_legs & RIGHT_SIDE) carrcap -= 10;
783 return(wt - carrcap);
787 struct obj *otmp = invent;
799 return(10*(1L << (u.ulevel-1)));