Source:NetHack 2.2a/hack.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to hack.c from the source code of NetHack 2.2a. To link to a particular line, write [[NetHack 2.2a/hack.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

Screenshots and source code from Hack are used under the CWI license.

1.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2.    
3.    #include <stdio.h>
4.    #include "hack.h"
5.    #ifdef UNIX
6.    static	char	SCCS_Id[] = "@(#)hack.c	2.2\t87/12/01";
7.    #endif
8.    extern char news0();
9.    extern char *nomovemsg;
10.   extern char *exclam();
11.   extern struct obj *addinv();
12.   extern boolean hmon();
13.   
14.   /* called on movement:
15.   	1. when throwing ball+chain far away
16.   	2. when teleporting
17.   	3. when walking out of a lit room
18.    */
19.   unsee() {
20.   	register x,y;
21.   	register struct rm *lev;
22.   
23.   /*
24.   	if(u.udispl){
25.   		u.udispl = 0;
26.   		newsym(u.udisx, u.udisy);
27.   	}
28.   */
29.   #ifndef QUEST
30.   	if(seehx){
31.   		seehx = 0;
32.   	} else
33.   #endif
34.   	for(x = u.ux-1; x < u.ux+2; x++)
35.   	  for(y = u.uy-1; y < u.uy+2; y++) {
36.   		if(!isok(x, y)) continue;
37.   		lev = &levl[x][y];
38.   		if(!lev->lit && lev->scrsym == ROOM_SYM) {
39.   			lev->scrsym = STONE_SYM;
40.   			lev->new = 1;
41.   			on_scr(x,y);
42.   		}
43.   	}
44.   }
45.   
46.   /* called:
47.   	in apply.c:  seeoff(0)	- when taking a picture of yourself
48.   				- when donning a blindfold
49.   	in do.c:     seeoff(0)	- blind after drinking potion
50.   	in do.c:     seeoff(1)	- go up or down the stairs
51.   	in eat.c:    seeoff(0)	- blind after eating rotten food
52.   	in mhitu.c:  seeoff(0)	- blinded by a yellow light
53.   	in mon.c:    seeoff(1)	- swallowed
54.   	in potion.c: seeoff(0)	- quaffing or sniffing a potion of blindness
55.   	in spell.c:  seeoff(0)	- due to a cursed spellbook
56.   	in trap.c:   seeoff(1)	- fall through trapdoor
57.   	in wizard.c: seeoff(0)	- hit by a cream pie.
58.    */
59.   seeoff(mode)	/* 1 to redo @, 0 to leave them */
60.   {	/* 1 means misc movement, 0 means blindness */
61.   	register x,y;
62.   	register struct rm *lev;
63.   
64.   	if(u.udispl && mode){
65.   		u.udispl = 0;
66.   		levl[u.udisx][u.udisy].scrsym = news0(u.udisx,u.udisy);
67.   	}
68.   #ifndef QUEST
69.   	if(seehx) {
70.   		seehx = 0;
71.   	} else
72.   #endif
73.   	if(!mode) {
74.   		for(x = u.ux-1; x < u.ux+2; x++)
75.   			for(y = u.uy-1; y < u.uy+2; y++) {
76.   				if(!isok(x, y)) continue;
77.   				lev = &levl[x][y];
78.   				if(!lev->lit && lev->scrsym == ROOM_SYM)
79.   					lev->seen = 0;
80.   			}
81.   	}
82.   }
83.   
84.   static
85.   moverock() {
86.   	register xchar rx, ry;
87.   	register struct obj *otmp;
88.   	register struct trap *ttmp;
89.   	register struct	monst *mtmp;
90.   	struct monst *m_at();
91.   
92.   	while(otmp = sobj_at(ENORMOUS_ROCK, u.ux+u.dx, u.uy+u.dy)) {
93.   		rx = u.ux+2*u.dx;
94.   		ry = u.uy+2*u.dy;
95.   		nomul(0);
96.   		if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
97.   		    (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) &&
98.   		    !sobj_at(ENORMOUS_ROCK, rx, ry)) {
99.   			if((mtmp = m_at(rx,ry))) {
100.  			    if(canseemon(mtmp))
101.  				pline("There's %s on the other side.", monnam(mtmp));
102.  			    else
103.  				pline("You hear a monster behind the rock.");
104.  			    pline("Perhaps that's why you cannot move it.");
105.  			    goto cannot_push;
106.  			}
107.  			if(ttmp = t_at(rx,ry))
108.  			    switch(ttmp->ttyp) {
109.  			    case PIT:
110.  				pline("You push the rock into a pit!");
111.  				deltrap(ttmp);
112.  				delobj(otmp);
113.  				pline("It completely fills the pit!");
114.  				continue;
115.  			    case TELEP_TRAP:
116.  				pline("You push the rock and suddenly it disappears!");
117.  				delobj(otmp);
118.  				continue;
119.  			    }
120.  			if(levl[rx][ry].typ == POOL) {
121.  				levl[rx][ry].typ = ROOM;
122.  				mnewsym(rx,ry);
123.  				prl(rx,ry);
124.  				pline("You push the rock into the water.");
125.  				pline("Now you can cross the water!");
126.  				delobj(otmp);
127.  				continue;
128.  			}
129.  			otmp->ox = rx;
130.  			otmp->oy = ry;
131.  			/* pobj(otmp); */
132.  			if(cansee(rx,ry)) atl(rx,ry,otmp->olet);
133.  			if(Invisible) newsym(u.ux+u.dx, u.uy+u.dy);
134.  
135.  			{ static long lastmovetime;
136.  			/* note: this var contains garbage initially and
137.  			   after a restore */
138.  			if(moves > lastmovetime+2 || moves < lastmovetime)
139.  			pline("With great effort you move the enormous rock.");
140.  			lastmovetime = moves;
141.  			}
142.  		} else {
143.  		    pline("You try to move the enormous rock, but in vain.");
144.  	    cannot_push:
145.  #ifdef KAA
146.  		    if (u.usym=='9') {
147.  # ifdef DGKMOD
148.  			if(!flags.pickup)
149.  				pline("You easily can push it aside.");
150.  			else
151.  # endif
152.  				pline("However, you easily can pick it up.");
153.  			break;
154.  		    }
155.  #endif
156.  		    if((!invent || inv_weight()+90 <= 0) &&
157.  			(!u.dx || !u.dy || (IS_ROCK(levl[u.ux][u.uy+u.dy].typ)
158.  					&& IS_ROCK(levl[u.ux+u.dx][u.uy].typ)))){
159.  			pline("However, you can squeeze yourself into a small opening.");
160.  			break;
161.  		    } else
162.  			return (-1);
163.  		}
164.  	}
165.  	return (0);
166.  }
167.  
168.  domove()
169.  {
170.  	register struct monst *mtmp;
171.  	register struct rm *tmpr,*ust;
172.  	struct trap *trap;
173.  
174.  	u_wipe_engr(rnd(5));
175.  
176.  	if(inv_weight() > 0){
177.  		pline("You collapse under your load.");
178.  		nomul(0);
179.  		return;
180.  	}
181.  	if(u.uswallow) {
182.  		u.dx = u.dy = 0;
183.  		u.ux = u.ustuck->mx;
184.  		u.uy = u.ustuck->my;
185.  	} else {
186.  		if(Confusion) {
187.  			do {
188.  				confdir();
189.  			} while(!isok(u.ux+u.dx, u.uy+u.dy) ||
190.  			    IS_ROCK(levl[u.ux+u.dx][u.uy+u.dy].typ));
191.  		}
192.  		if(!isok(u.ux+u.dx, u.uy+u.dy)){
193.  			nomul(0);
194.  			return;
195.  		}
196.  	}
197.  
198.  	ust = &levl[u.ux][u.uy];
199.  	u.ux0 = u.ux;
200.  	u.uy0 = u.uy;
201.  	if(!u.uswallow && (trap = t_at(u.ux+u.dx, u.uy+u.dy)) && trap->tseen)
202.  		nomul(0);
203.  	if(u.ustuck && !u.uswallow && (u.ux+u.dx != u.ustuck->mx ||
204.  		u.uy+u.dy != u.ustuck->my)) {
205.  		if(dist(u.ustuck->mx, u.ustuck->my) > 2){
206.  			/* perhaps it fled (or was teleported or ... ) */
207.  			u.ustuck = 0;
208.  		} else {
209.  			if(Blind) pline("You cannot escape from it!");
210.  			else pline("You cannot escape from %s!",
211.  				monnam(u.ustuck));
212.  			nomul(0);
213.  			return;
214.  		}
215.  	}
216.  	if(u.uswallow || (mtmp = m_at(u.ux+u.dx,u.uy+u.dy))) {
217.  	/* attack monster */
218.  
219.  #ifdef SAFE_ATTACK
220.  		/* Don't attack if you're running */
221.  		if (flags.run && !mtmp->mimic
222.  		&& (Blind ? Telepat : (!mtmp->minvis || See_invisible))) {
223.  			nomul(0);
224.  			flags.move = 0;
225.  			return;
226.  		}
227.  #endif
228.  		nomul(0);
229.  		gethungry();
230.  		if(multi < 0) return;	/* we just fainted */
231.  
232.  		/* try to attack; note that it might evade */
233.  		if(attack(u.uswallow ? u.ustuck : mtmp))
234.  			return;
235.  	}
236.  	/* not attacking an animal, so we try to move */
237.  	if(u.utrap) {
238.  		if(u.utraptype == TT_PIT) {
239.  			pline("You are still in a pit.");
240.  			u.utrap--;
241.  #ifdef SPIDERS
242.  		} else if (u.utraptype == TT_WEB) {
243.  		    	pline("You are stuck to the web.");
244.  			u.utrap--;
245.  #endif
246.  		} else {
247.  			pline("You are caught in a beartrap.");
248.  			if((u.dx && u.dy) || !rn2(5)) u.utrap--;
249.  		}
250.  		return;
251.  	}
252.  	tmpr = &levl[u.ux+u.dx][u.uy+u.dy];
253.  	if(IS_ROCK(tmpr->typ) ||
254.  	   (u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))){
255.  		flags.move = 0;
256.  		nomul(0);
257.  		return;
258.  	}
259.  	if(moverock() < 0) return;
260.  	if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
261.  		IS_ROCK(levl[u.ux+u.dx][u.uy].typ) &&
262.  		invent && inv_weight()+40 > 0) {
263.  		pline("You are carrying too much to get through.");
264.  		nomul(0);
265.  		return;
266.  	}
267.  	if(Punished &&
268.  	   DIST(u.ux+u.dx, u.uy+u.dy, uchain->ox, uchain->oy) > 2){
269.  		if(carried(uball)) {
270.  			movobj(uchain, u.ux, u.uy);
271.  			goto nodrag;
272.  		}
273.  
274.  		if(DIST(u.ux+u.dx, u.uy+u.dy, uball->ox, uball->oy) < 3){
275.  			/* leave ball, move chain under/over ball */
276.  			movobj(uchain, uball->ox, uball->oy);
277.  			goto nodrag;
278.  		}
279.  
280.  		if(inv_weight() + (int) uball->owt/2 > 0) {
281.  			pline("You cannot %sdrag the heavy iron ball.",
282.  			invent ? "carry all that and also " : "");
283.  			nomul(0);
284.  			return;
285.  		}
286.  
287.  		movobj(uball, uchain->ox, uchain->oy);
288.  		unpobj(uball);		/* BAH %% */
289.  		uchain->ox = u.ux;
290.  		uchain->oy = u.uy;
291.  		nomul(-2);
292.  		nomovemsg = "";
293.  	nodrag:	;
294.  	}
295.  	u.ux += u.dx;
296.  	u.uy += u.dy;
297.  	if(flags.run) {
298.  		if(tmpr->typ == DOOR ||
299.  		(xupstair == u.ux && yupstair == u.uy) ||
300.  		(xdnstair == u.ux && ydnstair == u.uy)
301.  #ifdef FOUNTAINS
302.  		|| IS_FOUNTAIN(levl[u.ux][u.uy].typ)
303.  #endif
304.  #ifdef NEWCLASS
305.  		|| IS_THRONE(levl[u.ux][u.uy].typ)
306.  #endif
307.  		)
308.  			nomul(0);
309.  	}
310.  
311.  	if(tmpr->typ == POOL && !Levitation)
312.  		drown();	/* not necessarily fatal */
313.  
314.  /*
315.  	if(u.udispl) {
316.  		u.udispl = 0;
317.  		newsym(u.ux0,u.uy0);
318.  	}
319.  */
320.  	if(!Blind) {
321.  #ifdef QUEST
322.  		setsee();
323.  #else
324.  		if(ust->lit) {
325.  			if(tmpr->lit) {
326.  				if(tmpr->typ == DOOR)
327.  					prl1(u.ux+u.dx,u.uy+u.dy);
328.  				else if(ust->typ == DOOR)
329.  					nose1(u.ux0-u.dx,u.uy0-u.dy);
330.  			} else {
331.  				unsee();
332.  				prl1(u.ux+u.dx,u.uy+u.dy);
333.  			}
334.  		} else {
335.  			if(tmpr->lit) setsee();
336.  			else {
337.  				prl1(u.ux+u.dx,u.uy+u.dy);
338.  				if(tmpr->typ == DOOR) {
339.  					if(u.dy) {
340.  						prl(u.ux-1,u.uy);
341.  						prl(u.ux+1,u.uy);
342.  					} else {
343.  						prl(u.ux,u.uy-1);
344.  						prl(u.ux,u.uy+1);
345.  					}
346.  				}
347.  			}
348.  			nose1(u.ux0-u.dx,u.uy0-u.dy);
349.  		}
350.  #endif /* QUEST /**/
351.  	} else {
352.  		pru();
353.  	}
354.  	if(!flags.nopick) pickup(1);
355.  	if(trap) dotrap(trap);		/* fall into pit, arrow trap, etc. */
356.  	(void) inshop();
357.  	if(!Blind) read_engr_at(u.ux,u.uy);
358.  }
359.  
360.  movobj(obj, ox, oy)
361.  register struct obj *obj;
362.  register int ox, oy;
363.  {
364.  	/* Some dirty programming to get display right */
365.  	freeobj(obj);
366.  	unpobj(obj);
367.  	obj->nobj = fobj;
368.  	fobj = obj;
369.  	obj->ox = ox;
370.  	obj->oy = oy;
371.  }
372.  
373.  dopickup(){
374.  	/* uswallow case added by GAN 01/29/87 */
375.  	if(u.uswallow)  {
376.  		pline("You pick up %s's tongue.",monnam(u.ustuck));
377.  		pline("But it's kind of slimy, so you drop it.");
378.  		return(1);
379.  	}
380.  	if(!g_at(u.ux,u.uy) && !o_at(u.ux,u.uy)) {
381.  		pline("There is nothing here to pick up.");
382.  		return(0);
383.  	}
384.  	if(Levitation) {
385.  		pline("You cannot reach the floor.");
386.  		return(1);
387.  	}
388.  	pickup(0);
389.  	return(1);
390.  }
391.  
392.  pickup(all)
393.  {
394.  	register struct gold *gold;
395.  	register struct obj *obj, *obj2;
396.  	register int wt;
397.  	char buf[BUFSZ];
398.  	register char *ip;
399.  	register char sym;
400.  	register int oletct = 0, iletct = 0;
401.  	char olets[20], ilets[20];
402.  
403.  	if(Levitation) return;
404.  #ifdef DGKMOD
405.  	if (all && !flags.pickup) {
406.  		int ct = 0;
407.  
408.  		for (obj = fobj; obj; obj = obj->nobj)
409.  			if (obj->ox == u.ux && obj->oy == u.uy)
410.  				if (!Punished || obj != uchain)
411.  					ct++;
412.  		/* If gold is the only thing here, pick it up.
413.  		 */
414.  		if (!ct && g_at(u.ux, u.uy)) {
415.  			if (flags.run) nomul(0);
416.  			while (gold = g_at(u.ux,u.uy)) {
417.  				pline("%ld gold piece%s.", gold->amount,
418.  					plur(gold->amount));
419.  				u.ugold += gold->amount;
420.  				flags.botl = 1;
421.  				freegold(gold);
422.  			}
423.  			if (Invisible) newsym(u.ux,u.uy);
424.  		}
425.  
426.  		/* If there are objects here, take a look.
427.  		 */
428.  		if (ct) {
429.  			if (flags.run)
430.  				nomul(0);
431.  			nscr();
432.  			if (ct < 5)
433.  				dolook();
434.  			else
435.  				pline("There are several objects here.");
436.  		}
437.  		return;
438.  	}
439.  #endif
440.  	while(gold = g_at(u.ux,u.uy)) {
441.  		pline("%ld gold piece%s.", gold->amount, plur(gold->amount));
442.  		u.ugold += gold->amount;
443.  		flags.botl = 1;
444.  		freegold(gold);
445.  		if(flags.run) nomul(0);
446.  		if(Invisible) newsym(u.ux,u.uy);
447.  	}
448.  	/* check for more than one object */
449.  	if(!all) {
450.  		register int ct = 0;
451.  
452.  		for(obj = fobj; obj; obj = obj->nobj)
453.  			if(obj->ox == u.ux && obj->oy == u.uy) ct++;
454.  		if(g_at(u.ux,u.uy))
455.  			ct++;
456.  		if(ct < 2)
457.  			all++;
458.  		else
459.  			pline("There are several objects here.");
460.  	}
461.  
462.  	/* added by GAN 10/24/86 to allow selective picking up */
463.  	if(!all)  {
464.  		register struct obj *otmp = fobj;
465.  
466.  		if(g_at(u.ux,u.uy)) ilets[iletct++] = GOLD_SYM;
467.  		ilets[iletct] = 0;
468.  		while(otmp) {
469.  			if(!index(ilets, otmp->olet) &&
470.  			   otmp->ox == u.ux && otmp->oy == u.uy)  {
471.  				ilets[iletct++] = otmp->olet;
472.  				ilets[iletct] = 0;
473.  			}
474.  			otmp = otmp->nobj;
475.  		}        
476.  		if(iletct == 1)
477.  			strcpy(buf,ilets);
478.  		else  {
479.  			ilets[iletct++] = ' ';
480.  			ilets[iletct++] = 'a';
481.  			ilets[iletct++] = 'A';
482.  			ilets[iletct] = 0;
483.  
484.  			if(iletct = 3)    
485.  			pline("What kinds of thing do you want to pick up? [%s] ", ilets);
486.  			getlin(buf);
487.  			if(buf[0] == '\033') {
488.  				clrlin();
489.  				return;
490.  			}
491.  #ifdef KJSMODS
492.  			else if(!buf[0]) strcpy(buf,"A");
493.  #endif
494.  		}
495.  		ip = buf;
496.  		olets[0] = 0;
497.  		while(sym = *ip++){
498.  			/* new A function (selective all) added by
499.  			 * GAN 01/09/87
500.  			 */
501.  			if(sym == 'A')  {
502.  				for(oletct = 0; ilets[oletct] != ' '; oletct++)
503.  					olets[oletct] = ilets[oletct];
504.  				olets[oletct] = 0;
505.  				break;
506.  			}
507.  			if(sym == ' ') continue;
508.  			if(sym == 'a') all++; else
509.  			if(index(ilets, sym)){
510.  				if(!index(olets, sym)){
511.  					olets[oletct++] = sym;
512.  					olets[oletct] = 0;
513.  				}
514.  			}
515.  			else pline("There are no %c's here.", sym);
516.  		}        
517.  	}
518.  
519.  	if(all || index(olets, GOLD_SYM))
520.  		while(gold = g_at(u.ux,u.uy)) {
521.  			pline("%ld gold piece%s.", gold->amount,
522.  			   plur(gold->amount));
523.  			u.ugold += gold->amount;
524.  			flags.botl = 1;
525.  			freegold(gold);
526.  			if(flags.run) nomul(0);
527.  			if(Invis) newsym(u.ux,u.uy);
528.  	}
529.  
530.  
531.  	for(obj = fobj; obj; obj = obj2) {
532.  	    obj2 = obj->nobj;   /* perhaps obj will be picked up */
533.  	    if(obj->ox == u.ux && obj->oy == u.uy) {
534.  		if(flags.run) nomul(0);
535.  
536.  		if(!all)  {
537.  			char c;
538.  		       
539.  			if(!index(olets,obj->olet)) continue;
540.   
541.  			pline("Pick up %s ? [ynaq]", doname(obj));
542.  			while(!index("ynaq ", (c = readchar())))
543.  				bell();
544.  			if(c == 'q') return;
545.  			if(c == 'n') continue;
546.  			if(c == 'a') all = 1;
547.  		}
548.   
549.  		if(obj->otyp == DEAD_COCKATRICE && !uarmg && u.usym != 'c') {
550.  		    pline("Touching the dead cockatrice is a fatal mistake.");
551.  		    pline("You turn to stone.");
552.  		    pline("You die...");
553.  		    killer = "cockatrice cadaver";
554.  		    done("died");
555.  		}        
556.   
557.  		if(obj->otyp == SCR_SCARE_MONSTER){
558.  		  if(!obj->spe) obj->spe = 1;
559.  		  else {
560.  		    /* Note: perhaps the 1st pickup failed: you cannot
561.  			carry anymore, and so we never dropped it -
562.  			lets assume that treading on it twice also
563.  			destroys the scroll */
564.  		    pline("The scroll turns to dust as you pick it up.");
565.  #ifdef KAA
566.  			if(!(objects[SCR_SCARE_MONSTER].oc_name_known) &&
567.  			   !(objects[SCR_SCARE_MONSTER].oc_uname))
568.  				docall(obj);
569.  #endif
570.  		    delobj(obj);
571.  		    continue;
572.  		  }
573.  		}
574.  		   
575.  		/* do not pick up uchain */
576.  		if(Punished && obj == uchain)
577.  			continue;
578.  
579.  				  
580.  		wt = inv_weight() + obj->owt;
581.  		if(wt > 0) {
582.  			if(obj->quan > 1) {
583.  				/* see how many we can lift */
584.  				extern struct obj *splitobj();
585.  				int savequan = obj->quan;
586.  				int iw = inv_weight();
587.  				int qq;
588.  				for(qq = 1; qq < savequan; qq++){
589.  					obj->quan = qq;
590.  					if(iw + weight(obj) > 0)
591.  						break;
592.  				}
593.  				obj->quan = savequan;
594.  				qq--;
595.  				/* we can carry qq of them */
596.  				if(!qq) goto too_heavy;
597.  			pline("You can only carry %s of the %s lying here.",
598.  					(qq == 1) ? "one" : "some",
599.  					doname(obj));
600.  				{
601.  				register struct obj *obj3;
602.  
603.  				obj3 = splitobj(obj, qq);
604.  				if(obj3->otyp == SCR_SCARE_MONSTER)
605.  					if(obj3->spe) obj->spe = 0;
606.  				}
607.  				/* note: obj2 is set already, so well never
608.  				 * encounter the other half; if it should be
609.  				 * otherwise then write
610.  				 *      obj2 = splitobj(obj,qq);
611.  				 */
612.  				goto lift_some;
613.  			}          
614.  		too_heavy:
615.  			pline("There %s %s here, but %s.",
616.  				(obj->quan == 1) ? "is" : "are",
617.  				doname(obj),
618.  				!invent ? "it is too heavy for you to lift"
619.  				/* There is no such word as "anymore". KAA */
620.  					: "you cannot carry any more");
621.  				if(obj->otyp == SCR_SCARE_MONSTER)
622.  					if(obj->spe) obj->spe = 0;
623.  			break;
624.  		}
625.  	lift_some:
626.  		if(inv_cnt() >= 52) {
627.  		    pline("Your knapsack cannot accommodate any more items.");
628.  				if(obj->otyp == SCR_SCARE_MONSTER)
629.  					if(obj->spe) obj->spe = 0;
630.  		    break;
631.  		}
632.  		freeobj(obj);
633.  		if(Invisible) newsym(u.ux,u.uy);
634.  		addtobill(obj);       /* sets obj->unpaid if necessary */
635.  		if(wt > -5) pline("You have a little trouble lifting");
636.  		{ int pickquan = obj->quan;
637.  		  int mergquan;
638.  #ifdef KAA
639.  		if(!Blind) if(obj->olet != WEAPON_SYM) obj->dknown = 1;
640.  #else
641.  		if(!Blind) obj->dknown = 1;     /* this is done by prinv(),
642.  				 but addinv() needs it already for merging */
643.  #endif
644.  		obj = addinv(obj);    /* might merge it with other objects */
645.  		  mergquan = obj->quan;
646.  		  obj->quan = pickquan; /* to fool prinv() */
647.  		prinv(obj);
648.  		  obj->quan = mergquan;
649.  		}
650.  	    }
651.  	}
652.  }
653.  
654.  /* stop running if we see something interesting */
655.  /* turn around a corner if that is the only way we can proceed */
656.  /* do not turn left or right twice */
657.  lookaround(){
658.  register x,y,i,x0,y0,m0,i0 = 9;
659.  register int corrct = 0, noturn = 0;
660.  register struct monst *mtmp;
661.  #ifdef LINT
662.  	/* suppress "used before set" message */
663.  	x0 = y0 = 0;
664.  #endif
665.  	if(Blind || flags.run == 0) return;
666.  	if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
667.  #ifdef QUEST
668.  	if(u.ux0 == u.ux+u.dx && u.uy0 == u.uy+u.dy) goto stop;
669.  #endif
670.  	for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
671.  		if(x == u.ux && y == u.uy) continue;
672.  		if(!levl[x][y].typ) continue;
673.  		if((mtmp = m_at(x,y)) && !mtmp->mimic &&
674.  		    (!mtmp->minvis || See_invisible)){
675.  			if(!mtmp->mtame || (x == u.ux+u.dx && y == u.uy+u.dy))
676.  				goto stop;
677.  		} else mtmp = 0; /* invisible M cannot influence us */
678.  		if(x == u.ux-u.dx && y == u.uy-u.dy) continue;
679.  		{
680.  		register uchar sym = levl[x][y].scrsym;
681.  
682.  		if (sym == VWALL_SYM || sym == HWALL_SYM
683.  		    || sym == ROOM_SYM || sym == STONE_SYM
684.  		    || IS_CORNER(sym))
685.  			continue;
686.  		else if (sym == DOOR_SYM) {
687.  			if(x != u.ux && y != u.uy) continue;
688.  			if(flags.run != 1) goto stop;
689.  			goto corr;
690.  		} else if (sym == CORR_SYM) {
691.  		corr:
692.  			if(flags.run == 1 || flags.run == 3) {
693.  				i = DIST(x,y,u.ux+u.dx,u.uy+u.dy);
694.  				if(i > 2) continue;
695.  				if(corrct == 1 && DIST(x,y,x0,y0) != 1)
696.  					noturn = 1;
697.  				if(i < i0) {
698.  					i0 = i;
699.  					x0 = x;
700.  					y0 = y;
701.  					m0 = mtmp ? 1 : 0;
702.  				}
703.  			}
704.  			corrct++;
705.  			continue;
706.  		} else if (sym == TRAP_SYM) {
707.  			if(flags.run == 1) goto corr;	/* if you must */
708.  			if(x == u.ux+u.dx && y == u.uy+u.dy) goto stop;
709.  			continue;
710.  		} else {		/* e.g. objects or trap or stairs */
711.  			if(flags.run == 1) goto corr;
712.  			if(mtmp) continue;		/* d */
713.  		}
714.  		stop:
715.  			nomul(0);
716.  			return;
717.  		}
718.  	}
719.  #ifdef QUEST
720.  	if(corrct > 0 && (flags.run == 4 || flags.run == 5)) goto stop;
721.  #endif
722.  	if(corrct > 1 && flags.run == 2) goto stop;
723.  	if((flags.run == 1 || flags.run == 3) && !noturn && !m0 && i0 &&
724.  		(corrct == 1 || (corrct == 2 && i0 == 1))) {
725.  		/* make sure that we do not turn too far */
726.  		if(i0 == 2) {
727.  		    if(u.dx == y0-u.uy && u.dy == u.ux-x0)
728.  			i = 2;		/* straight turn right */
729.  		    else
730.  			i = -2;		/* straight turn left */
731.  		} else if(u.dx && u.dy) {
732.  		    if((u.dx == u.dy && y0 == u.uy) ||
733.  			(u.dx != u.dy && y0 != u.uy))
734.  			i = -1;		/* half turn left */
735.  		    else
736.  			i = 1;		/* half turn right */
737.  		} else {
738.  		    if((x0-u.ux == y0-u.uy && !u.dy) ||
739.  			(x0-u.ux != y0-u.uy && u.dy))
740.  			i = 1;		/* half turn right */
741.  		    else
742.  			i = -1;		/* half turn left */
743.  		}
744.  		i += u.last_str_turn;
745.  		if(i <= 2 && i >= -2) {
746.  			u.last_str_turn = i;
747.  			u.dx = x0-u.ux, u.dy = y0-u.uy;
748.  		}
749.  	}
750.  }
751.  
752.  /* something like lookaround, but we are not running */
753.  /* react only to monsters that might hit us */
754.  monster_nearby() {
755.  register int x,y;
756.  register struct monst *mtmp;
757.  	if(!Blind)
758.  	for(x = u.ux-1; x <= u.ux+1; x++) for(y = u.uy-1; y <= u.uy+1; y++){
759.  		if(x == u.ux && y == u.uy) continue;
760.  		if((mtmp = m_at(x,y)) && !mtmp->mimic && !mtmp->mtame &&
761.  			!mtmp->mpeaceful && !index("Ea", mtmp->data->mlet) &&
762.  			!mtmp->mfroz && !mtmp->msleep &&  /* aplvax!jcn */
763.  			(!mtmp->minvis || See_invisible))
764.  			return(1);
765.  	}
766.  	return(0);
767.  }
768.  
769.  #ifdef QUEST
770.  cansee(x,y) xchar x,y; {
771.  register int dx,dy,adx,ady,sdx,sdy,dmax,d;
772.  	if(Blind) return(0);
773.  	if(!isok(x,y)) return(0);
774.  	d = dist(x,y);
775.  	if(d < 3) return(1);
776.  	if(d > u.uhorizon*u.uhorizon) return(0);
777.  	if(!levl[x][y].lit)
778.  		return(0);
779.  	dx = x - u.ux;	adx = abs(dx);	sdx = sgn(dx);
780.  	dy = y - u.uy;  ady = abs(dy);	sdy = sgn(dy);
781.  	if(dx == 0 || dy == 0 || adx == ady){
782.  		dmax = (dx == 0) ? ady : adx;
783.  		for(d = 1; d <= dmax; d++)
784.  			if(!rroom(sdx*d,sdy*d))
785.  				return(0);
786.  		return(1);
787.  	} else if(ady > adx){
788.  		for(d = 1; d <= ady; d++){
789.  			if(!rroom(sdx*( (d*adx)/ady ), sdy*d) ||
790.  			   !rroom(sdx*( (d*adx-1)/ady+1 ), sdy*d))
791.  				return(0);
792.  		}
793.  		return(1);
794.  	} else {
795.  		for(d = 1; d <= adx; d++){
796.  			if(!rroom(sdx*d, sdy*( (d*ady)/adx )) ||
797.  			   !rroom(sdx*d, sdy*( (d*ady-1)/adx+1 )))
798.  				return(0);
799.  		}
800.  		return(1);
801.  	}
802.  }
803.  
804.  rroom(x,y) register int x,y; {
805.  	return(IS_ROOM(levl[u.ux+x][u.uy+y].typ));
806.  }
807.  
808.  #else
809.  
810.  cansee(x,y) xchar x,y; {
811.  	if(Blind || u.uswallow) return(0);
812.  	if(dist(x,y) < 3) return(1);
813.  	if(levl[x][y].lit && seelx <= x && x <= seehx && seely <= y &&
814.  		y <= seehy) return(1);
815.  	return(0);
816.  }
817.  #endif /* QUEST /**/
818.  
819.  sgn(a) register int a; {
820.  	return((a > 0) ? 1 : (a == 0) ? 0 : -1);
821.  }
822.  
823.  #ifdef QUEST
824.  setsee()
825.  {
826.  	register x,y;
827.  
828.  	if(Blind) {
829.  		pru();
830.  		return;
831.  	}
832.  	for(y = u.uy-u.uhorizon; y <= u.uy+u.uhorizon; y++)
833.  		for(x = u.ux-u.uhorizon; x <= u.ux+u.uhorizon; x++) {
834.  			if(cansee(x,y))
835.  				prl(x,y);
836.  	}
837.  }
838.  
839.  #else
840.  
841.  setsee()
842.  {
843.  	register x,y;
844.  
845.  	if(Blind) {
846.  		pru();
847.  		return;
848.  	}
849.  	if(!levl[u.ux][u.uy].lit) {
850.  		seelx = u.ux-1;
851.  		seehx = u.ux+1;
852.  		seely = u.uy-1;
853.  		seehy = u.uy+1;
854.  	} else {
855.  		for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--);
856.  		for(seehx = u.ux; levl[seehx+1][u.uy].lit; seehx++);
857.  		for(seely = u.uy; levl[u.ux][seely-1].lit; seely--);
858.  		for(seehy = u.uy; levl[u.ux][seehy+1].lit; seehy++);
859.  	}
860.  	for(y = seely; y <= seehy; y++)
861.  		for(x = seelx; x <= seehx; x++) {
862.  			prl(x,y);
863.  	}
864.  	if(!levl[u.ux][u.uy].lit) seehx = 0; /* seems necessary elsewhere */
865.  	else {
866.  	    if(seely == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seely-1);
867.  	    if(seehy == u.uy) for(x = u.ux-1; x <= u.ux+1; x++) prl(x,seehy+1);
868.  	    if(seelx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seelx-1,y);
869.  	    if(seehx == u.ux) for(y = u.uy-1; y <= u.uy+1; y++) prl(seehx+1,y);
870.  	}
871.  }
872.  #endif /* QUEST /**/
873.  
874.  nomul(nval)
875.  register nval;
876.  {
877.  #ifdef DGKMOD
878.  	if(multi < nval) return;	/* This is a bug fix by ab@unido */
879.  #else
880.  	if(multi < 0) return;
881.  #endif
882.  	multi = nval;
883.  	flags.mv = flags.run = 0;
884.  }
885.  
886.  abon()
887.  {
888.  #ifdef KAA
889.  	if (u.usym != '@') return(mons[u.umonnum].mlevel-3);
890.  #endif
891.  	if(u.ustr == 3) return(-3);
892.  	else if(u.ustr < 6) return(-2);
893.  	else if(u.ustr < 8) return(-1);
894.  	else if(u.ustr < 17) return(0);
895.  	else if(u.ustr < 69) return(1);	/* up to 18/50 */
896.  	else if(u.ustr < 118) return(2);
897.  	else return(3);
898.  }
899.  
900.  dbon()
901.  {
902.  	if (u.usym != '@') return(0);
903.  
904.  	if(u.ustr < 6) return(-1);
905.  	else if(u.ustr < 16) return(0);
906.  	else if(u.ustr < 18) return(1);
907.  	else if(u.ustr == 18) return(2);	/* up to 18 */
908.  	else if(u.ustr < 94) return(3);		/* up to 18/75 */
909.  	else if(u.ustr < 109) return(4);	/* up to 18/90 */
910.  	else if(u.ustr < 118) return(5);	/* up to 18/99 */
911.  	else return(6);
912.  }
913.  
914.  losestr(num)	/* may kill you; cause may be poison or monster like 'A' */
915.  register num;
916.  {
917.  	u.ustr -= num;
918.  	while(u.ustr < 3) {
919.  		u.ustr++;
920.  		u.uhp -= 6;
921.  		u.uhpmax -= 6;
922.  	}
923.  	flags.botl = 1;
924.  }
925.  
926.  losehp(n,knam)
927.  register n;
928.  register char *knam;
929.  {
930.  #ifdef KAA
931.  	if (u.mtimedone) {
932.  		u.mh -= n;
933.  		if (u.mhmax < u.mh) u.mhmax = u.mh;
934.  		flags.botl = 1;
935.  		if (u.mh < 1) rehumanize();
936.  		return;
937.  	}
938.  #endif
939.  	u.uhp -= n;
940.  	if(u.uhp > u.uhpmax)
941.  		u.uhpmax = u.uhp;	/* perhaps n was negative */
942.  	flags.botl = 1;
943.  	if(u.uhp < 1) {
944.  		killer = knam;	/* the thing that killed you */
945.  		pline("You die...");
946.  		done("died");
947.  	}
948.  }
949.  
950.  losehp_m(n,mtmp)
951.  register n;
952.  register struct monst *mtmp;
953.  {
954.  #ifdef KAA
955.  	if (u.mtimedone) {
956.  		u.mh -= n;
957.  		flags.botl = 1;
958.  		if (u.mh < 1) rehumanize();
959.  		return;
960.  	}
961.  #endif
962.  	u.uhp -= n;
963.  	flags.botl = 1;
964.  	if(u.uhp < 1)
965.  		done_in_by(mtmp);
966.  }
967.  
968.  losexp()	/* hit by V or W */
969.  {
970.  	register num;
971.  	extern long newuexp();
972.  
973.  	if (u.usym == 'V' || u.usym=='W') return;
974.  
975.  	if(u.ulevel > 1)
976.  		pline("Goodbye level %u.", u.ulevel--);
977.  	else
978.  		u.uhp = -1;
979.  	num = rnd(10);
980.  	u.uhp -= num;
981.  	u.uhpmax -= num;
982.  #ifdef SPELLS
983.  	num = rnd(u.ulevel/2+1) + 1;		/* M. Stephenson */
984.  	u.uen -= num;
985.  	if (u.uen < 0)		u.uen = 0;
986.  	u.uenmax -= num;
987.  	if (u.uenmax < 0)	u.uenmax = 0;
988.  #endif
989.  	u.uexp = newuexp();
990.  	flags.botl = 1;
991.  }
992.  
993.  inv_weight(){
994.  register struct obj *otmp = invent;
995.  register int wt = (u.ugold + 500)/1000;
996.  register int carrcap;
997.  #ifdef KAA
998.  	if (u.mtimedone) {
999.  		if (u.usym == '9') carrcap = MAX_CARR_CAP * 4;
1000. 		else if (u.usym == 'N') carrcap = MAX_CARR_CAP;
1001. 		else if (mons[u.umonnum].mlevel <= 3)
1002. 			carrcap = 5*mons[u.umonnum].mlevel + 30;
1003. 		else carrcap = 5*mons[u.umonnum].mlevel + 100;
1004. 	}
1005. #endif
1006. 	if(Levitation)			/* pugh@cornell */
1007. 		carrcap = MAX_CARR_CAP;
1008. 	else {
1009. #ifdef HARD
1010. 		carrcap = 5*(((u.ustr > 18) ? 20 : u.ustr) + u.ulevel);
1011. #else
1012. 	        carrcap = 5*u.ulevel;      /* New strength stewr 870807 */
1013. 		if (u.ustr < 19) carrcap += 5*u.ustr;
1014. 		if (u.ustr > 18) carrcap += u.ustr - 18 + 90;
1015. 		if (u.ustr > 68) carrcap += u.ustr - 68;
1016. 		if (u.ustr > 93) carrcap += u.ustr - 93;
1017. 		if (u.ustr > 108) carrcap += 2*(u.ustr - 108);
1018. 		if (u.ustr > 113) carrcap += 5*(u.ustr - 113);
1019. 		if (u.ustr == 118) carrcap += 100;
1020. #endif
1021. 		if(carrcap > MAX_CARR_CAP) carrcap = MAX_CARR_CAP;
1022. 		if(Wounded_legs & LEFT_SIDE) carrcap -= 10;
1023. 		if(Wounded_legs & RIGHT_SIDE) carrcap -= 10;
1024. 	}
1025. 	while(otmp){
1026. 		wt += otmp->owt;
1027. 		otmp = otmp->nobj;
1028. 	}
1029. 	return(wt - carrcap);
1030. }
1031. 
1032. inv_cnt(){
1033. register struct obj *otmp = invent;
1034. register int ct = 0;
1035. 	while(otmp){
1036. 		ct++;
1037. 		otmp = otmp->nobj;
1038. 	}
1039. 	return(ct);
1040. }
1041. 
1042. long
1043. newuexp()
1044. {
1045. 	return(10*(1L << (u.ulevel-1)));
1046. }