xref: /csrg-svn/games/hack/hack.c (revision 41228)
1*41228Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2*41228Sbostic /* hack.c - version 1.0.3 */
3*41228Sbostic 
4*41228Sbostic #include "hack.h"
5*41228Sbostic #include <stdio.h>
6*41228Sbostic 
7*41228Sbostic extern char news0();
8*41228Sbostic extern char *nomovemsg;
9*41228Sbostic extern char *exclam();
10*41228Sbostic extern struct obj *addinv();
11*41228Sbostic extern boolean hmon();
12*41228Sbostic 
13*41228Sbostic /* called on movement:
14*41228Sbostic 	1. when throwing ball+chain far away
15*41228Sbostic 	2. when teleporting
16*41228Sbostic 	3. when walking out of a lit room
17*41228Sbostic  */
unsee()18*41228Sbostic unsee() {
19*41228Sbostic 	register x,y;
20*41228Sbostic 	register struct rm *lev;
21*41228Sbostic 
22*41228Sbostic /*
23*41228Sbostic 	if(u.udispl){
24*41228Sbostic 		u.udispl = 0;
25*41228Sbostic 		newsym(u.udisx, u.udisy);
26*41228Sbostic 	}
27*41228Sbostic */
28*41228Sbostic #ifndef QUEST
29*41228Sbostic 	if(seehx){
30*41228Sbostic 		seehx = 0;
31*41228Sbostic 	} else
32*41228Sbostic #endif QUEST
33*41228Sbostic 	for(x = u.ux-1; x < u.ux+2; x++)
34*41228Sbostic 	  for(y = u.uy-1; y < u.uy+2; y++) {
35*41228Sbostic 		if(!isok(x, y)) continue;
36*41228Sbostic 		lev = &levl[x][y];
37*41228Sbostic 		if(!lev->lit && lev->scrsym == '.') {
38*41228Sbostic 			lev->scrsym =' ';
39*41228Sbostic 			lev->new = 1;
40*41228Sbostic 			on_scr(x,y);
41*41228Sbostic 		}
42*41228Sbostic 	}
43*41228Sbostic }
44*41228Sbostic 
45*41228Sbostic /* called:
46*41228Sbostic 	in hack.eat.c: seeoff(0) - blind after eating rotten food
47*41228Sbostic 	in hack.mon.c: seeoff(0) - blinded by a yellow light
48*41228Sbostic 	in hack.mon.c: seeoff(1) - swallowed
49*41228Sbostic 	in hack.do.c:  seeoff(0) - blind after drinking potion
50*41228Sbostic 	in hack.do.c:  seeoff(1) - go up or down the stairs
51*41228Sbostic 	in hack.trap.c:seeoff(1) - fall through trapdoor
52*41228Sbostic  */
seeoff(mode)53*41228Sbostic seeoff(mode)	/* 1 to redo @, 0 to leave them */
54*41228Sbostic {	/* 1 means misc movement, 0 means blindness */
55*41228Sbostic 	register x,y;
56*41228Sbostic 	register struct rm *lev;
57*41228Sbostic 
58*41228Sbostic 	if(u.udispl && mode){
59*41228Sbostic 		u.udispl = 0;
60*41228Sbostic 		levl[u.udisx][u.udisy].scrsym = news0(u.udisx,u.udisy);
61*41228Sbostic 	}
62*41228Sbostic #ifndef QUEST
63*41228Sbostic 	if(seehx) {
64*41228Sbostic 		seehx = 0;
65*41228Sbostic 	} else
66*41228Sbostic #endif QUEST
67*41228Sbostic 	if(!mode) {
68*41228Sbostic 		for(x = u.ux-1; x < u.ux+2; x++)
69*41228Sbostic 			for(y = u.uy-1; y < u.uy+2; y++) {
70*41228Sbostic 				if(!isok(x, y)) continue;
71*41228Sbostic 				lev = &levl[x][y];
72*41228Sbostic 				if(!lev->lit && lev->scrsym == '.')
73*41228Sbostic 					lev->seen = 0;
74*41228Sbostic 			}
75*41228Sbostic 	}
76*41228Sbostic }
77*41228Sbostic 
domove()78*41228Sbostic domove()
79*41228Sbostic {
80*41228Sbostic 	xchar oldx,oldy;
81*41228Sbostic 	register struct monst *mtmp;
82*41228Sbostic 	register struct rm *tmpr,*ust;
83*41228Sbostic 	struct trap *trap;
84*41228Sbostic 	register struct obj *otmp;
85*41228Sbostic 
86*41228Sbostic 	u_wipe_engr(rnd(5));
87*41228Sbostic 
88*41228Sbostic 	if(inv_weight() > 0){
89*41228Sbostic 		pline("You collapse under your load.");
90*41228Sbostic 		nomul(0);
91*41228Sbostic 		return;
92*41228Sbostic 	}
93*41228Sbostic 	if(u.uswallow) {
94*41228Sbostic 		u.dx = u.dy = 0;
95*41228Sbostic 		u.ux = u.ustuck->mx;
96*41228Sbostic 		u.uy = u.ustuck->my;
97*41228Sbostic 	} else {
98*41228Sbostic 		if(Confusion) {
99*41228Sbostic 			do {
100*41228Sbostic 				confdir();
101*41228Sbostic 			} while(!isok(u.ux+u.dx, u.uy+u.dy) ||
102*41228Sbostic 			    IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ));
103*41228Sbostic 		}
104*41228Sbostic 		if(!isok(u.ux+u.dx, u.uy+u.dy)){
105*41228Sbostic 			nomul(0);
106*41228Sbostic 			return;
107*41228Sbostic 		}
108*41228Sbostic 	}
109*41228Sbostic 
110*41228Sbostic 	ust = &levl[u.ux][u.uy];
111*41228Sbostic 	oldx = u.ux;
112*41228Sbostic 	oldy = u.uy;
113*41228Sbostic 	if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
114*41228Sbostic 		nomul(0);
115*41228Sbostic 	if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
116*41228Sbostic 		u.uy+u.dy != u.ustuck->my)) {
117*41228Sbostic 		if(dist(u.ustuck->mx, u.ustuck->my) > 2){
118*41228Sbostic 			/* perhaps it fled (or was teleported or ... ) */
119*41228Sbostic 			u.ustuck = 0;
120*41228Sbostic 		} else {
121*41228Sbostic 			if(Blind) pline("You cannot escape from it!");
122*41228Sbostic 			else pline("You cannot escape from %s!",
123*41228Sbostic 				monnam(u.ustuck));
124*41228Sbostic 			nomul(0);
125*41228Sbostic 			return;
126*41228Sbostic 		}
127*41228Sbostic 	}
128*41228Sbostic 	if(u.uswallow || (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
129*41228Sbostic 	/* attack monster */
130*41228Sbostic 
131*41228Sbostic 		nomul(0);
132*41228Sbostic 		gethungry();
133*41228Sbostic 		if(multi < 0) return;	/* we just fainted */
134*41228Sbostic 
135*41228Sbostic 		/* try to attack; note that it might evade */
136*41228Sbostic 		if(attack(u.uswallow ? u.ustuck : mtmp))
137*41228Sbostic 			return;
138*41228Sbostic 	}
139*41228Sbostic 	/* not attacking an animal, so we try to move */
140*41228Sbostic 	if(u.utrap) {
141*41228Sbostic 		if(u.utraptype == TT_PIT) {
142*41228Sbostic 			pline("You are still in a pit.");
143*41228Sbostic 			u.utrap--;
144*41228Sbostic 		} else {
145*41228Sbostic 			pline("You are caught in a beartrap.");
146*41228Sbostic 			if((u.dx && u.dy) || !rn2(5)) u.utrap--;
147*41228Sbostic 		}
148*41228Sbostic 		return;
149*41228Sbostic 	}
150*41228Sbostic 	tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
151*41228Sbostic 	if(IS_ROCK(tmpr->typ) ||
152*41228Sbostic 	   (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))){
153*41228Sbostic 		flags.move = 0;
154*41228Sbostic 		nomul(0);
155*41228Sbostic 		return;
156*41228Sbostic 	}
157*41228Sbostic 	while(otmp = sobj_at(ENORMOUS_ROCK, u.ux+u.dx, u.uy+u.dy)) {
158*41228Sbostic 		register xchar rx = u.ux+2*u.dx, ry = u.uy+2*u.dy;
159*41228Sbostic 		register struct trap *ttmp;
160*41228Sbostic 		nomul(0);
161*41228Sbostic 		if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
162*41228Sbostic 		    (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
163*41228Sbostic 		    !sobj_at(ENORMOUS_ROCK, rx, ry)) {
164*41228Sbostic 			if(m_at(rx,ry)) {
165*41228Sbostic 			    pline("You hear a monster behind the rock.");
166*41228Sbostic 			    pline("Perhaps that's why you cannot move it.");
167*41228Sbostic 			    goto cannot_push;
168*41228Sbostic 			}
169*41228Sbostic 			if(ttmp = t_at(rx,ry))
170*41228Sbostic 			    switch(ttmp->ttyp) {
171*41228Sbostic 			    case PIT:
172*41228Sbostic 				pline("You push the rock into a pit!");
173*41228Sbostic 				deltrap(ttmp);
174*41228Sbostic 				delobj(otmp);
175*41228Sbostic 				pline("It completely fills the pit!");
176*41228Sbostic 				continue;
177*41228Sbostic 			    case TELEP_TRAP:
178*41228Sbostic 				pline("You push the rock and suddenly it disappears!");
179*41228Sbostic 				delobj(otmp);
180*41228Sbostic 				continue;
181*41228Sbostic 			    }
182*41228Sbostic 			if(levl[rx][ry].typ == POOL) {
183*41228Sbostic 				levl[rx][ry].typ = ROOM;
184*41228Sbostic 				mnewsym(rx,ry);
185*41228Sbostic 				prl(rx,ry);
186*41228Sbostic 				pline("You push the rock into the water.");
187*41228Sbostic 				pline("Now you can cross the water!");
188*41228Sbostic 				delobj(otmp);
189*41228Sbostic 				continue;
190*41228Sbostic 			}
191*41228Sbostic 			otmp->ox = rx;
192*41228Sbostic 			otmp->oy = ry;
193*41228Sbostic 			/* pobj(otmp); */
194*41228Sbostic 			if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
195*41228Sbostic 			if(Invisible) newsym(u.ux+u.dx, u.uy+u.dy);
196*41228Sbostic 
197*41228Sbostic 			{ static long lastmovetime;
198*41228Sbostic 			/* note: this var contains garbage initially and
199*41228Sbostic 			   after a restore */
200*41228Sbostic 			if(moves > lastmovetime+2 || moves < lastmovetime)
201*41228Sbostic 			pline("With great effort you move the enormous rock.");
202*41228Sbostic 			lastmovetime = moves;
203*41228Sbostic 			}
204*41228Sbostic 		} else {
205*41228Sbostic 		    pline("You try to move the enormous rock, but in vain.");
206*41228Sbostic 	    cannot_push:
207*41228Sbostic 		    if((!invent || inv_weight()+90 <= 0) &&
208*41228Sbostic 			(!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy+u.dy].typ)
209*41228Sbostic 					&& IS_ROCK(levl[u.ux+u.dx][u.uy].typ)))){
210*41228Sbostic 			pline("However, you can squeeze yourself into a small opening.");
211*41228Sbostic 			break;
212*41228Sbostic 		    } else
213*41228Sbostic 			return;
214*41228Sbostic 		}
215*41228Sbostic 	    }
216*41228Sbostic 	if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
217*41228Sbostic 		IS_ROCK(levl[u.ux+u.dx][u.uy].typ) &&
218*41228Sbostic 		invent && inv_weight()+40 > 0) {
219*41228Sbostic 		pline("You are carrying too much to get through.");
220*41228Sbostic 		nomul(0);
221*41228Sbostic 		return;
222*41228Sbostic 	}
223*41228Sbostic 	if(Punished &&
224*41228Sbostic 	   DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy) > 2){
225*41228Sbostic 		if(carried(uball)) {
226*41228Sbostic 			movobj(uchain, u.ux, u.uy);
227*41228Sbostic 			goto nodrag;
228*41228Sbostic 		}
229*41228Sbostic 
230*41228Sbostic 		if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy) < 3){
231*41228Sbostic 			/* leave ball, move chain under/over ball */
232*41228Sbostic 			movobj(uchain, uball->ox, uball->oy);
233*41228Sbostic 			goto nodrag;
234*41228Sbostic 		}
235*41228Sbostic 
236*41228Sbostic 		if(inv_weight() + (int) uball->owt/2 > 0) {
237*41228Sbostic 			pline("You cannot %sdrag the heavy iron ball.",
238*41228Sbostic 			invent ? "carry all that and also " : "");
239*41228Sbostic 			nomul(0);
240*41228Sbostic 			return;
241*41228Sbostic 		}
242*41228Sbostic 
243*41228Sbostic 		movobj(uball, uchain->ox, uchain->oy);
244*41228Sbostic 		unpobj(uball);		/* BAH %% */
245*41228Sbostic 		uchain->ox = u.ux;
246*41228Sbostic 		uchain->oy = u.uy;
247*41228Sbostic 		nomul(-2);
248*41228Sbostic 		nomovemsg = "";
249*41228Sbostic 	nodrag:	;
250*41228Sbostic 	}
251*41228Sbostic 	u.ux += u.dx;
252*41228Sbostic 	u.uy += u.dy;
253*41228Sbostic 	if(flags.run) {
254*41228Sbostic 		if(tmpr->typ == DOOR ||
255*41228Sbostic 		(xupstair == u.ux && yupstair == u.uy) ||
256*41228Sbostic 		(xdnstair == u.ux && ydnstair == u.uy))
257*41228Sbostic 			nomul(0);
258*41228Sbostic 	}
259*41228Sbostic 
260*41228Sbostic 	if(tmpr->typ == POOL && !Levitation)
261*41228Sbostic 		drown();	/* not necessarily fatal */
262*41228Sbostic 
263*41228Sbostic /*
264*41228Sbostic 	if(u.udispl) {
265*41228Sbostic 		u.udispl = 0;
266*41228Sbostic 		newsym(oldx,oldy);
267*41228Sbostic 	}
268*41228Sbostic */
269*41228Sbostic 	if(!Blind) {
270*41228Sbostic #ifdef QUEST
271*41228Sbostic 		setsee();
272*41228Sbostic #else
273*41228Sbostic 		if(ust->lit) {
274*41228Sbostic 			if(tmpr->lit) {
275*41228Sbostic 				if(tmpr->typ == DOOR)
276*41228Sbostic 					prl1(u.ux+u.dx,u.uy+u.dy);
277*41228Sbostic 				else if(ust->typ == DOOR)
278*41228Sbostic 					nose1(oldx-u.dx,oldy-u.dy);
279*41228Sbostic 			} else {
280*41228Sbostic 				unsee();
281*41228Sbostic 				prl1(u.ux+u.dx,u.uy+u.dy);
282*41228Sbostic 			}
283*41228Sbostic 		} else {
284*41228Sbostic 			if(tmpr->lit) setsee();
285*41228Sbostic 			else {
286*41228Sbostic 				prl1(u.ux+u.dx,u.uy+u.dy);
287*41228Sbostic 				if(tmpr->typ == DOOR) {
288*41228Sbostic 					if(u.dy) {
289*41228Sbostic 						prl(u.ux-1,u.uy);
290*41228Sbostic 						prl(u.ux+1,u.uy);
291*41228Sbostic 					} else {
292*41228Sbostic 						prl(u.ux,u.uy-1);
293*41228Sbostic 						prl(u.ux,u.uy+1);
294*41228Sbostic 					}
295*41228Sbostic 				}
296*41228Sbostic 			}
297*41228Sbostic 			nose1(oldx-u.dx,oldy-u.dy);
298*41228Sbostic 		}
299*41228Sbostic #endif QUEST
300*41228Sbostic 	} else {
301*41228Sbostic 		pru();
302*41228Sbostic 	}
303*41228Sbostic 	if(!flags.nopick) pickup(1);
304*41228Sbostic 	if(trap) dotrap(trap);		/* fall into pit, arrow trap, etc. */
305*41228Sbostic 	(void) inshop();
306*41228Sbostic 	if(!Blind) read_engr_at(u.ux,u.uy);
307*41228Sbostic }
308*41228Sbostic 
movobj(obj,ox,oy)309*41228Sbostic movobj(obj, ox, oy)
310*41228Sbostic register struct obj *obj;
311*41228Sbostic register int ox, oy;
312*41228Sbostic {
313*41228Sbostic 	/* Some dirty programming to get display right */
314*41228Sbostic 	freeobj(obj);
315*41228Sbostic 	unpobj(obj);
316*41228Sbostic 	obj->nobj = fobj;
317*41228Sbostic 	fobj = obj;
318*41228Sbostic 	obj->ox = ox;
319*41228Sbostic 	obj->oy = oy;
320*41228Sbostic }
321*41228Sbostic 
dopickup()322*41228Sbostic dopickup(){
323*41228Sbostic 	if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
324*41228Sbostic 		pline("There is nothing here to pick up.");
325*41228Sbostic 		return(0);
326*41228Sbostic 	}
327*41228Sbostic 	if(Levitation) {
328*41228Sbostic 		pline("You cannot reach the floor.");
329*41228Sbostic 		return(1);
330*41228Sbostic 	}
331*41228Sbostic 	pickup(0);
332*41228Sbostic 	return(1);
333*41228Sbostic }
334*41228Sbostic 
pickup(all)335*41228Sbostic pickup(all)
336*41228Sbostic {
337*41228Sbostic 	register struct gold *gold;
338*41228Sbostic 	register struct obj *obj, *obj2;
339*41228Sbostic 	register int wt;
340*41228Sbostic 
341*41228Sbostic 	if(Levitation) return;
342*41228Sbostic 	while(gold = g_at(u.ux,u.uy)) {
343*41228Sbostic 		pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
344*41228Sbostic 		u.ugold += gold->amount;
345*41228Sbostic 		flags.botl = 1;
346*41228Sbostic 		freegold(gold);
347*41228Sbostic 		if(flags.run) nomul(0);
348*41228Sbostic 		if(Invisible) newsym(u.ux,u.uy);
349*41228Sbostic 	}
350*41228Sbostic 
351*41228Sbostic 	/* check for more than one object */
352*41228Sbostic 	if(!all) {
353*41228Sbostic 		register int ct = 0;
354*41228Sbostic 
355*41228Sbostic 		for(obj = fobj; obj; obj = obj->nobj)
356*41228Sbostic 			if(obj->ox == u.ux && obj->oy == u.uy)
357*41228Sbostic 				if(!Punished || obj != uchain)
358*41228Sbostic 					ct++;
359*41228Sbostic 		if(ct < 2)
360*41228Sbostic 			all++;
361*41228Sbostic 		else
362*41228Sbostic 			pline("There are several objects here.");
363*41228Sbostic 	}
364*41228Sbostic 
365*41228Sbostic 	for(obj = fobj; obj; obj = obj2) {
366*41228Sbostic 	    obj2 = obj->nobj;	/* perhaps obj will be picked up */
367*41228Sbostic 	    if(obj->ox == u.ux && obj->oy == u.uy) {
368*41228Sbostic 		if(flags.run) nomul(0);
369*41228Sbostic 
370*41228Sbostic 		/* do not pick up uchain */
371*41228Sbostic 		if(Punished && obj == uchain)
372*41228Sbostic 			continue;
373*41228Sbostic 
374*41228Sbostic 		if(!all) {
375*41228Sbostic 			char c;
376*41228Sbostic 
377*41228Sbostic 			pline("Pick up %s ? [ynaq]", doname(obj));
378*41228Sbostic 			while(!index("ynaq ", (c = readchar())))
379*41228Sbostic 				bell();
380*41228Sbostic 			if(c == 'q') return;
381*41228Sbostic 			if(c == 'n') continue;
382*41228Sbostic 			if(c == 'a') all = 1;
383*41228Sbostic 		}
384*41228Sbostic 
385*41228Sbostic 		if(obj->otyp == DEAD_COCKATRICE && !uarmg){
386*41228Sbostic 		    pline("Touching the dead cockatrice is a fatal mistake.");
387*41228Sbostic 		    pline("You turn to stone.");
388*41228Sbostic 		    killer = "cockatrice cadaver";
389*41228Sbostic 		    done("died");
390*41228Sbostic 		}
391*41228Sbostic 
392*41228Sbostic 		if(obj->otyp == SCR_SCARE_MONSTER){
393*41228Sbostic 		  if(!obj->spe) obj->spe = 1;
394*41228Sbostic 		  else {
395*41228Sbostic 		    /* Note: perhaps the 1st pickup failed: you cannot
396*41228Sbostic 			carry anymore, and so we never dropped it -
397*41228Sbostic 			let's assume that treading on it twice also
398*41228Sbostic 			destroys the scroll */
399*41228Sbostic 		    pline("The scroll turns to dust as you pick it up.");
400*41228Sbostic 		    delobj(obj);
401*41228Sbostic 		    continue;
402*41228Sbostic 		  }
403*41228Sbostic 		}
404*41228Sbostic 
405*41228Sbostic 		wt = inv_weight() + obj->owt;
406*41228Sbostic 		if(wt > 0) {
407*41228Sbostic 			if(obj->quan > 1) {
408*41228Sbostic 				/* see how many we can lift */
409*41228Sbostic 				extern struct obj *splitobj();
410*41228Sbostic 				int savequan = obj->quan;
411*41228Sbostic 				int iw = inv_weight();
412*41228Sbostic 				int qq;
413*41228Sbostic 				for(qq = 1; qq < savequan; qq++){
414*41228Sbostic 					obj->quan = qq;
415*41228Sbostic 					if(iw + weight(obj) > 0)
416*41228Sbostic 						break;
417*41228Sbostic 				}
418*41228Sbostic 				obj->quan = savequan;
419*41228Sbostic 				qq--;
420*41228Sbostic 				/* we can carry qq of them */
421*41228Sbostic 				if(!qq) goto too_heavy;
422*41228Sbostic 			pline("You can only carry %s of the %s lying here.",
423*41228Sbostic 					(qq == 1) ? "one" : "some",
424*41228Sbostic 					doname(obj));
425*41228Sbostic 				(void) splitobj(obj, qq);
426*41228Sbostic 				/* note: obj2 is set already, so we'll never
427*41228Sbostic 				 * encounter the other half; if it should be
428*41228Sbostic 				 * otherwise then write
429*41228Sbostic 				 *	obj2 = splitobj(obj,qq);
430*41228Sbostic 				 */
431*41228Sbostic 				goto lift_some;
432*41228Sbostic 			}
433*41228Sbostic 		too_heavy:
434*41228Sbostic 			pline("There %s %s here, but %s.",
435*41228Sbostic 				(obj->quan == 1) ? "is" : "are",
436*41228Sbostic 				doname(obj),
437*41228Sbostic 				!invent ? "it is too heavy for you to lift"
438*41228Sbostic 					: "you cannot carry anymore");
439*41228Sbostic 			break;
440*41228Sbostic 		}
441*41228Sbostic 	lift_some:
442*41228Sbostic 		if(inv_cnt() >= 52) {
443*41228Sbostic 		    pline("Your knapsack cannot accomodate anymore items.");
444*41228Sbostic 		    break;
445*41228Sbostic 		}
446*41228Sbostic 		if(wt > -5) pline("You have a little trouble lifting");
447*41228Sbostic 		freeobj(obj);
448*41228Sbostic 		if(Invisible) newsym(u.ux,u.uy);
449*41228Sbostic 		addtobill(obj);       /* sets obj->unpaid if necessary */
450*41228Sbostic 		{ int pickquan = obj->quan;
451*41228Sbostic 		  int mergquan;
452*41228Sbostic 		if(!Blind) obj->dknown = 1;	/* this is done by prinv(),
453*41228Sbostic 				 but addinv() needs it already for merging */
454*41228Sbostic 		obj = addinv(obj);    /* might merge it with other objects */
455*41228Sbostic 		  mergquan = obj->quan;
456*41228Sbostic 		  obj->quan = pickquan;	/* to fool prinv() */
457*41228Sbostic 		prinv(obj);
458*41228Sbostic 		  obj->quan = mergquan;
459*41228Sbostic 		}
460*41228Sbostic 	    }
461*41228Sbostic 	}
462*41228Sbostic }
463*41228Sbostic 
464*41228Sbostic /* stop running if we see something interesting */
465*41228Sbostic /* turn around a corner if that is the only way we can proceed */
466*41228Sbostic /* do not turn left or right twice */
lookaround()467*41228Sbostic lookaround(){
468*41228Sbostic register x,y,i,x0,y0,m0,i0 = 9;
469*41228Sbostic register int corrct = 0, noturn = 0;
470*41228Sbostic register struct monst *mtmp;
471*41228Sbostic #ifdef lint
472*41228Sbostic 	/* suppress "used before set" message */
473*41228Sbostic 	x0 = y0 = 0;
474*41228Sbostic #endif lint
475*41228Sbostic 	if(Blind || flags.run == 0) return;
476*41228Sbostic 	if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
477*41228Sbostic #ifdef QUEST
478*41228Sbostic 	if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
479*41228Sbostic #endif QUEST
480*41228Sbostic 	for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
481*41228Sbostic 		if(x == u.ux && y == u.uy) continue;
482*41228Sbostic 		if(!levl[x][y].typ) continue;
483*41228Sbostic 		if((mtmp = m_at(x,y)) && !mtmp->mimic &&
484*41228Sbostic 		    (!mtmp->minvis || See_invisible)){
485*41228Sbostic 			if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
486*41228Sbostic 				goto stop;
487*41228Sbostic 		} else mtmp = 0; /* invisible M cannot influence us */
488*41228Sbostic 		if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
489*41228Sbostic 		switch(levl[x][y].scrsym){
490*41228Sbostic 		case '|':
491*41228Sbostic 		case '-':
492*41228Sbostic 		case '.':
493*41228Sbostic 		case ' ':
494*41228Sbostic 			break;
495*41228Sbostic 		case '+':
496*41228Sbostic 			if(x != u.ux && y != u.uy) break;
497*41228Sbostic 			if(flags.run != 1) goto stop;
498*41228Sbostic 			/* fall into next case */
499*41228Sbostic 		case CORR_SYM:
500*41228Sbostic 		corr:
501*41228Sbostic 			if(flags.run == 1 || flags.run == 3) {
502*41228Sbostic 				i = DIST(x,y,u.ux+u.dx,u.uy+u.dy);
503*41228Sbostic 				if(i > 2) break;
504*41228Sbostic 				if(corrct == 1 && DIST(x,y,x0,y0) != 1)
505*41228Sbostic 					noturn = 1;
506*41228Sbostic 				if(i < i0) {
507*41228Sbostic 					i0 = i;
508*41228Sbostic 					x0 = x;
509*41228Sbostic 					y0 = y;
510*41228Sbostic 					m0 = mtmp ? 1 : 0;
511*41228Sbostic 				}
512*41228Sbostic 			}
513*41228Sbostic 			corrct++;
514*41228Sbostic 			break;
515*41228Sbostic 		case '^':
516*41228Sbostic 			if(flags.run == 1) goto corr;	/* if you must */
517*41228Sbostic 			if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
518*41228Sbostic 			break;
519*41228Sbostic 		default:	/* e.g. objects or trap or stairs */
520*41228Sbostic 			if(flags.run == 1) goto corr;
521*41228Sbostic 			if(mtmp) break;		/* d */
522*41228Sbostic 		stop:
523*41228Sbostic 			nomul(0);
524*41228Sbostic 			return;
525*41228Sbostic 		}
526*41228Sbostic 	}
527*41228Sbostic #ifdef QUEST
528*41228Sbostic 	if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
529*41228Sbostic #endif QUEST
530*41228Sbostic 	if(corrct > 1 && flags.run == 2) goto stop;
531*41228Sbostic 	if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
532*41228Sbostic 		(corrct == 1 || (corrct == 2 && i0 == 1))) {
533*41228Sbostic 		/* make sure that we do not turn too far */
534*41228Sbostic 		if(i0 == 2) {
535*41228Sbostic 		    if(u.dx == y0-u.uy && u.dy == u.ux-x0)
536*41228Sbostic 			i = 2;		/* straight turn right */
537*41228Sbostic 		    else
538*41228Sbostic 			i = -2;		/* straight turn left */
539*41228Sbostic 		} else if(u.dx && u.dy) {
540*41228Sbostic 		    if((u.dx == u.dy && y0 == u.uy) ||
541*41228Sbostic 			(u.dx != u.dy && y0 != u.uy))
542*41228Sbostic 			i = -1;		/* half turn left */
543*41228Sbostic 		    else
544*41228Sbostic 			i = 1;		/* half turn right */
545*41228Sbostic 		} else {
546*41228Sbostic 		    if((x0-u.ux == y0-u.uy && !u.dy) ||
547*41228Sbostic 			(x0-u.ux != y0-u.uy && u.dy))
548*41228Sbostic 			i = 1;		/* half turn right */
549*41228Sbostic 		    else
550*41228Sbostic 			i = -1;		/* half turn left */
551*41228Sbostic 		}
552*41228Sbostic 		i += u.last_str_turn;
553*41228Sbostic 		if(i <= 2 && i >= -2) {
554*41228Sbostic 			u.last_str_turn = i;
555*41228Sbostic 			u.dx = x0-u.ux, u.dy = y0-u.uy;
556*41228Sbostic 		}
557*41228Sbostic 	}
558*41228Sbostic }
559*41228Sbostic 
560*41228Sbostic /* something like lookaround, but we are not running */
561*41228Sbostic /* react only to monsters that might hit us */
monster_nearby()562*41228Sbostic monster_nearby() {
563*41228Sbostic register int x,y;
564*41228Sbostic register struct monst *mtmp;
565*41228Sbostic 	if(!Blind)
566*41228Sbostic 	for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
567*41228Sbostic 		if(x == u.ux && y == u.uy) continue;
568*41228Sbostic 		if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
569*41228Sbostic 			!mtmp->mpeaceful && !index("Ea", mtmp->data->mlet) &&
570*41228Sbostic 			!mtmp->mfroz && !mtmp->msleep &&  /* aplvax!jcn */
571*41228Sbostic 			(!mtmp->minvis || See_invisible))
572*41228Sbostic 			return(1);
573*41228Sbostic 	}
574*41228Sbostic 	return(0);
575*41228Sbostic }
576*41228Sbostic 
577*41228Sbostic #ifdef QUEST
cansee(x,y)578*41228Sbostic cansee(x,y) xchar x,y; {
579*41228Sbostic register int dx,dy,adx,ady,sdx,sdy,dmax,d;
580*41228Sbostic 	if(Blind) return(0);
581*41228Sbostic 	if(!isok(x,y)) return(0);
582*41228Sbostic 	d = dist(x,y);
583*41228Sbostic 	if(d < 3) return(1);
584*41228Sbostic 	if(d > u.uhorizon*u.uhorizon) return(0);
585*41228Sbostic 	if(!levl[x][y].lit)
586*41228Sbostic 		return(0);
587*41228Sbostic 	dx = x - u.ux;	adx = abs(dx);	sdx = sgn(dx);
588*41228Sbostic 	dy = y - u.uy;  ady = abs(dy);	sdy = sgn(dy);
589*41228Sbostic 	if(dx == 0 || dy == 0 || adx == ady){
590*41228Sbostic 		dmax = (dx == 0) ? ady : adx;
591*41228Sbostic 		for(d = 1; d <= dmax; d++)
592*41228Sbostic 			if(!rroom(sdx*d,sdy*d))
593*41228Sbostic 				return(0);
594*41228Sbostic 		return(1);
595*41228Sbostic 	} else if(ady > adx){
596*41228Sbostic 		for(d = 1; d <= ady; d++){
597*41228Sbostic 			if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
598*41228Sbostic 			   !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
599*41228Sbostic 				return(0);
600*41228Sbostic 		}
601*41228Sbostic 		return(1);
602*41228Sbostic 	} else {
603*41228Sbostic 		for(d = 1; d <= adx; d++){
604*41228Sbostic 			if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
605*41228Sbostic 			   !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
606*41228Sbostic 				return(0);
607*41228Sbostic 		}
608*41228Sbostic 		return(1);
609*41228Sbostic 	}
610*41228Sbostic }
611*41228Sbostic 
rroom(x,y)612*41228Sbostic rroom(x,y) register int x,y; {
613*41228Sbostic 	return(IS_ROOM(levl[u.ux+x][u.uy+y].typ));
614*41228Sbostic }
615*41228Sbostic 
616*41228Sbostic #else
617*41228Sbostic 
cansee(x,y)618*41228Sbostic cansee(x,y) xchar x,y; {
619*41228Sbostic 	if(Blind || u.uswallow) return(0);
620*41228Sbostic 	if(dist(x,y) < 3) return(1);
621*41228Sbostic 	if(levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
622*41228Sbostic 		y <= seehy) return(1);
623*41228Sbostic 	return(0);
624*41228Sbostic }
625*41228Sbostic #endif QUEST
626*41228Sbostic 
sgn(a)627*41228Sbostic sgn(a) register int a; {
628*41228Sbostic 	return((a > 0) ? 1 : (a == 0) ? 0 : -1);
629*41228Sbostic }
630*41228Sbostic 
631*41228Sbostic #ifdef QUEST
setsee()632*41228Sbostic setsee()
633*41228Sbostic {
634*41228Sbostic 	register x,y;
635*41228Sbostic 
636*41228Sbostic 	if(Blind) {
637*41228Sbostic 		pru();
638*41228Sbostic 		return;
639*41228Sbostic 	}
640*41228Sbostic 	for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
641*41228Sbostic 		for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
642*41228Sbostic 			if(cansee(x,y))
643*41228Sbostic 				prl(x,y);
644*41228Sbostic 	}
645*41228Sbostic }
646*41228Sbostic 
647*41228Sbostic #else
648*41228Sbostic 
setsee()649*41228Sbostic setsee()
650*41228Sbostic {
651*41228Sbostic 	register x,y;
652*41228Sbostic 
653*41228Sbostic 	if(Blind) {
654*41228Sbostic 		pru();
655*41228Sbostic 		return;
656*41228Sbostic 	}
657*41228Sbostic 	if(!levl[u.ux][u.uy].lit) {
658*41228Sbostic 		seelx = u.ux-1;
659*41228Sbostic 		seehx = u.ux+1;
660*41228Sbostic 		seely = u.uy-1;
661*41228Sbostic 		seehy = u.uy+1;
662*41228Sbostic 	} else {
663*41228Sbostic 		for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--);
664*41228Sbostic 		for(seehx = u.ux; levl[seehx+1][u.uy].lit; seehx++);
665*41228Sbostic 		for(seely = u.uy; levl[u.ux][seely-1].lit; seely--);
666*41228Sbostic 		for(seehy = u.uy; levl[u.ux][seehy+1].lit; seehy++);
667*41228Sbostic 	}
668*41228Sbostic 	for(y = seely; y <= seehy; y++)
669*41228Sbostic 		for(x = seelx; x <= seehx; x++) {
670*41228Sbostic 			prl(x,y);
671*41228Sbostic 	}
672*41228Sbostic 	if(!levl[u.ux][u.uy].lit) seehx = 0; /* seems necessary elsewhere */
673*41228Sbostic 	else {
674*41228Sbostic 	    if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
675*41228Sbostic 	    if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
676*41228Sbostic 	    if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
677*41228Sbostic 	    if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
678*41228Sbostic 	}
679*41228Sbostic }
680*41228Sbostic #endif QUEST
681*41228Sbostic 
nomul(nval)682*41228Sbostic nomul(nval)
683*41228Sbostic register nval;
684*41228Sbostic {
685*41228Sbostic 	if(multi < 0) return;
686*41228Sbostic 	multi = nval;
687*41228Sbostic 	flags.mv = flags.run = 0;
688*41228Sbostic }
689*41228Sbostic 
abon()690*41228Sbostic abon()
691*41228Sbostic {
692*41228Sbostic 	if(u.ustr == 3) return(-3);
693*41228Sbostic 	else if(u.ustr < 6) return(-2);
694*41228Sbostic 	else if(u.ustr < 8) return(-1);
695*41228Sbostic 	else if(u.ustr < 17) return(0);
696*41228Sbostic 	else if(u.ustr < 69) return(1);	/* up to 18/50 */
697*41228Sbostic 	else if(u.ustr < 118) return(2);
698*41228Sbostic 	else return(3);
699*41228Sbostic }
700*41228Sbostic 
dbon()701*41228Sbostic dbon()
702*41228Sbostic {
703*41228Sbostic 	if(u.ustr < 6) return(-1);
704*41228Sbostic 	else if(u.ustr < 16) return(0);
705*41228Sbostic 	else if(u.ustr < 18) return(1);
706*41228Sbostic 	else if(u.ustr == 18) return(2);	/* up to 18 */
707*41228Sbostic 	else if(u.ustr < 94) return(3);		/* up to 18/75 */
708*41228Sbostic 	else if(u.ustr < 109) return(4);	/* up to 18/90 */
709*41228Sbostic 	else if(u.ustr < 118) return(5);	/* up to 18/99 */
710*41228Sbostic 	else return(6);
711*41228Sbostic }
712*41228Sbostic 
losestr(num)713*41228Sbostic losestr(num)	/* may kill you; cause may be poison or monster like 'A' */
714*41228Sbostic register num;
715*41228Sbostic {
716*41228Sbostic 	u.ustr -= num;
717*41228Sbostic 	while(u.ustr < 3) {
718*41228Sbostic 		u.ustr++;
719*41228Sbostic 		u.uhp -= 6;
720*41228Sbostic 		u.uhpmax -= 6;
721*41228Sbostic 	}
722*41228Sbostic 	flags.botl = 1;
723*41228Sbostic }
724*41228Sbostic 
losehp(n,knam)725*41228Sbostic losehp(n,knam)
726*41228Sbostic register n;
727*41228Sbostic register char *knam;
728*41228Sbostic {
729*41228Sbostic 	u.uhp -= n;
730*41228Sbostic 	if(u.uhp > u.uhpmax)
731*41228Sbostic 		u.uhpmax = u.uhp;	/* perhaps n was negative */
732*41228Sbostic 	flags.botl = 1;
733*41228Sbostic 	if(u.uhp < 1) {
734*41228Sbostic 		killer = knam;	/* the thing that killed you */
735*41228Sbostic 		done("died");
736*41228Sbostic 	}
737*41228Sbostic }
738*41228Sbostic 
losehp_m(n,mtmp)739*41228Sbostic losehp_m(n,mtmp)
740*41228Sbostic register n;
741*41228Sbostic register struct monst *mtmp;
742*41228Sbostic {
743*41228Sbostic 	u.uhp -= n;
744*41228Sbostic 	flags.botl = 1;
745*41228Sbostic 	if(u.uhp < 1)
746*41228Sbostic 		done_in_by(mtmp);
747*41228Sbostic }
748*41228Sbostic 
losexp()749*41228Sbostic losexp()	/* hit by V or W */
750*41228Sbostic {
751*41228Sbostic 	register num;
752*41228Sbostic 	extern long newuexp();
753*41228Sbostic 
754*41228Sbostic 	if(u.ulevel > 1)
755*41228Sbostic 		pline("Goodbye level %u.", u.ulevel--);
756*41228Sbostic 	else
757*41228Sbostic 		u.uhp = -1;
758*41228Sbostic 	num = rnd(10);
759*41228Sbostic 	u.uhp -= num;
760*41228Sbostic 	u.uhpmax -= num;
761*41228Sbostic 	u.uexp = newuexp();
762*41228Sbostic 	flags.botl = 1;
763*41228Sbostic }
764*41228Sbostic 
inv_weight()765*41228Sbostic inv_weight(){
766*41228Sbostic register struct obj *otmp = invent;
767*41228Sbostic register int wt = (u.ugold + 500)/1000;
768*41228Sbostic register int carrcap;
769*41228Sbostic 	if(Levitation)			/* pugh@cornell */
770*41228Sbostic 		carrcap = MAX_CARR_CAP;
771*41228Sbostic 	else {
772*41228Sbostic 		carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
773*41228Sbostic 		if(carrcap > MAX_CARR_CAP) carrcap = MAX_CARR_CAP;
774*41228Sbostic 		if(Wounded_legs & LEFT_SIDE) carrcap -= 10;
775*41228Sbostic 		if(Wounded_legs & RIGHT_SIDE) carrcap -= 10;
776*41228Sbostic 	}
777*41228Sbostic 	while(otmp){
778*41228Sbostic 		wt += otmp->owt;
779*41228Sbostic 		otmp = otmp->nobj;
780*41228Sbostic 	}
781*41228Sbostic 	return(wt - carrcap);
782*41228Sbostic }
783*41228Sbostic 
inv_cnt()784*41228Sbostic inv_cnt(){
785*41228Sbostic register struct obj *otmp = invent;
786*41228Sbostic register int ct = 0;
787*41228Sbostic 	while(otmp){
788*41228Sbostic 		ct++;
789*41228Sbostic 		otmp = otmp->nobj;
790*41228Sbostic 	}
791*41228Sbostic 	return(ct);
792*41228Sbostic }
793*41228Sbostic 
794*41228Sbostic long
newuexp()795*41228Sbostic newuexp()
796*41228Sbostic {
797*41228Sbostic 	return(10*(1L << (u.ulevel-1)));
798*41228Sbostic }
799