xref: /netbsd-src/games/hack/hack.do.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*
2  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
3  */
4 
5 #ifndef lint
6 static char rcsid[] = "$NetBSD: hack.do.c,v 1.3 1995/03/23 08:29:53 cgd Exp $";
7 #endif /* not lint */
8 
9 /* Contains code for 'd', 'D' (drop), '>', '<' (up, down) and 't' (throw) */
10 
11 #include "hack.h"
12 
13 extern struct obj *splitobj(), *addinv();
14 extern boolean hmon();
15 extern boolean level_exists[];
16 extern struct monst youmonst;
17 extern char *Doname();
18 extern char *nomovemsg;
19 
20 static int drop();
21 
22 dodrop() {
23 	return(drop(getobj("0$#", "drop")));
24 }
25 
26 static int
27 drop(obj) register struct obj *obj; {
28 	if(!obj) return(0);
29 	if(obj->olet == '$') {		/* pseudo object */
30 		register long amount = OGOLD(obj);
31 
32 		if(amount == 0)
33 			pline("You didn't drop any gold pieces.");
34 		else {
35 			mkgold(amount, u.ux, u.uy);
36 			pline("You dropped %ld gold piece%s.",
37 				amount, plur(amount));
38 			if(Invisible) newsym(u.ux, u.uy);
39 		}
40 		free((char *) obj);
41 		return(1);
42 	}
43 	if(obj->owornmask & (W_ARMOR | W_RING)){
44 		pline("You cannot drop something you are wearing.");
45 		return(0);
46 	}
47 	if(obj == uwep) {
48 		if(uwep->cursed) {
49 			pline("Your weapon is welded to your hand!");
50 			return(0);
51 		}
52 		setuwep((struct obj *) 0);
53 	}
54 	pline("You dropped %s.", doname(obj));
55 	dropx(obj);
56 	return(1);
57 }
58 
59 /* Called in several places - should not produce texts */
60 dropx(obj)
61 register struct obj *obj;
62 {
63 	freeinv(obj);
64 	dropy(obj);
65 }
66 
67 dropy(obj)
68 register struct obj *obj;
69 {
70 	if(obj->otyp == CRYSKNIFE)
71 		obj->otyp = WORM_TOOTH;
72 	obj->ox = u.ux;
73 	obj->oy = u.uy;
74 	obj->nobj = fobj;
75 	fobj = obj;
76 	if(Invisible) newsym(u.ux,u.uy);
77 	subfrombill(obj);
78 	stackobj(obj);
79 }
80 
81 /* drop several things */
82 doddrop() {
83 	return(ggetobj("drop", drop, 0));
84 }
85 
86 dodown()
87 {
88 	if(u.ux != xdnstair || u.uy != ydnstair) {
89 		pline("You can't go down here.");
90 		return(0);
91 	}
92 	if(u.ustuck) {
93 		pline("You are being held, and cannot go down.");
94 		return(1);
95 	}
96 	if(Levitation) {
97 		pline("You're floating high above the stairs.");
98 		return(0);
99 	}
100 
101 	goto_level(dlevel+1, TRUE);
102 	return(1);
103 }
104 
105 doup()
106 {
107 	if(u.ux != xupstair || u.uy != yupstair) {
108 		pline("You can't go up here.");
109 		return(0);
110 	}
111 	if(u.ustuck) {
112 		pline("You are being held, and cannot go up.");
113 		return(1);
114 	}
115 	if(!Levitation && inv_weight() + 5 > 0) {
116 		pline("Your load is too heavy to climb the stairs.");
117 		return(1);
118 	}
119 
120 	goto_level(dlevel-1, TRUE);
121 	return(1);
122 }
123 
124 goto_level(newlevel, at_stairs)
125 register int newlevel;
126 register boolean at_stairs;
127 {
128 	register fd;
129 	register boolean up = (newlevel < dlevel);
130 
131 	if(newlevel <= 0) done("escaped");    /* in fact < 0 is impossible */
132 	if(newlevel > MAXLEVEL) newlevel = MAXLEVEL;	/* strange ... */
133 	if(newlevel == dlevel) return;	      /* this can happen */
134 
135 	glo(dlevel);
136 	fd = creat(lock, FMASK);
137 	if(fd < 0) {
138 		/*
139 		 * This is not quite impossible: e.g., we may have
140 		 * exceeded our quota. If that is the case then we
141 		 * cannot leave this level, and cannot save either.
142 		 * Another possibility is that the directory was not
143 		 * writable.
144 		 */
145 		pline("A mysterious force prevents you from going %s.",
146 			up ? "up" : "down");
147 		return;
148 	}
149 
150 	if(Punished) unplacebc();
151 	u.utrap = 0;				/* needed in level_tele */
152 	u.ustuck = 0;				/* idem */
153 	keepdogs();
154 	seeoff(1);
155 	if(u.uswallow)				/* idem */
156 		u.uswldtim = u.uswallow = 0;
157 	flags.nscrinh = 1;
158 	u.ux = FAR;				/* hack */
159 	(void) inshop();			/* probably was a trapdoor */
160 
161 	savelev(fd,dlevel);
162 	(void) close(fd);
163 
164 	dlevel = newlevel;
165 	if(maxdlevel < dlevel)
166 		maxdlevel = dlevel;
167 	glo(dlevel);
168 
169 	if(!level_exists[dlevel])
170 		mklev();
171 	else {
172 		extern int hackpid;
173 
174 		if((fd = open(lock,0)) < 0) {
175 			pline("Cannot open %s .", lock);
176 			pline("Probably someone removed it.");
177 			done("tricked");
178 		}
179 		getlev(fd, hackpid, dlevel);
180 		(void) close(fd);
181 	}
182 
183 	if(at_stairs) {
184 	    if(up) {
185 		u.ux = xdnstair;
186 		u.uy = ydnstair;
187 		if(!u.ux) {		/* entering a maze from below? */
188 		    u.ux = xupstair;	/* this will confuse the player! */
189 		    u.uy = yupstair;
190 		}
191 		if(Punished && !Levitation){
192 			pline("With great effort you climb the stairs.");
193 			placebc(1);
194 		}
195 	    } else {
196 		u.ux = xupstair;
197 		u.uy = yupstair;
198 		if(inv_weight() + 5 > 0 || Punished){
199 			pline("You fall down the stairs.");	/* %% */
200 			losehp(rnd(3), "fall");
201 			if(Punished) {
202 			    if(uwep != uball && rn2(3)){
203 				pline("... and are hit by the iron ball.");
204 				losehp(rnd(20), "iron ball");
205 			    }
206 			    placebc(1);
207 			}
208 			selftouch("Falling, you");
209 		}
210 	    }
211 	    { register struct monst *mtmp = m_at(u.ux, u.uy);
212 	      if(mtmp)
213 		mnexto(mtmp);
214 	    }
215 	} else {	/* trapdoor or level_tele */
216 	    do {
217 		u.ux = rnd(COLNO-1);
218 		u.uy = rn2(ROWNO);
219 	    } while(levl[u.ux][u.uy].typ != ROOM ||
220 			m_at(u.ux,u.uy));
221 	    if(Punished){
222 		if(uwep != uball && !up /* %% */ && rn2(5)){
223 			pline("The iron ball falls on your head.");
224 			losehp(rnd(25), "iron ball");
225 		}
226 		placebc(1);
227 	    }
228 	    selftouch("Falling, you");
229 	}
230 	(void) inshop();
231 	initrack();
232 
233 	losedogs();
234 	{ register struct monst *mtmp;
235 	  if(mtmp = m_at(u.ux, u.uy)) mnexto(mtmp);	/* riv05!a3 */
236 	}
237 	flags.nscrinh = 0;
238 	setsee();
239 	seeobjs();	/* make old cadavers disappear - riv05!a3 */
240 	docrt();
241 	pickup(1);
242 	read_engr_at(u.ux,u.uy);
243 }
244 
245 donull() {
246 	return(1);	/* Do nothing, but let other things happen */
247 }
248 
249 dopray() {
250 	nomovemsg = "You finished your prayer.";
251 	nomul(-3);
252 	return(1);
253 }
254 
255 struct monst *bhit(), *boomhit();
256 dothrow()
257 {
258 	register struct obj *obj;
259 	register struct monst *mon;
260 	register tmp;
261 
262 	obj = getobj("#)", "throw");   /* it is also possible to throw food */
263 				       /* (or jewels, or iron balls ... ) */
264 	if(!obj || !getdir(1))	       /* ask "in what direction?" */
265 		return(0);
266 	if(obj->owornmask & (W_ARMOR | W_RING)){
267 		pline("You can't throw something you are wearing.");
268 		return(0);
269 	}
270 
271 	u_wipe_engr(2);
272 
273 	if(obj == uwep){
274 		if(obj->cursed){
275 			pline("Your weapon is welded to your hand.");
276 			return(1);
277 		}
278 		if(obj->quan > 1)
279 			setuwep(splitobj(obj, 1));
280 		else
281 			setuwep((struct obj *) 0);
282 	}
283 	else if(obj->quan > 1)
284 		(void) splitobj(obj, 1);
285 	freeinv(obj);
286 	if(u.uswallow) {
287 		mon = u.ustuck;
288 		bhitpos.x = mon->mx;
289 		bhitpos.y = mon->my;
290 	} else if(u.dz) {
291 	  if(u.dz < 0) {
292 	    pline("%s hits the ceiling, then falls back on top of your head.",
293 		Doname(obj));		/* note: obj->quan == 1 */
294 	    if(obj->olet == POTION_SYM)
295 		potionhit(&youmonst, obj);
296 	    else {
297 		if(uarmh) pline("Fortunately, you are wearing a helmet!");
298 		losehp(uarmh ? 1 : rnd((int)(obj->owt)), "falling object");
299 		dropy(obj);
300 	    }
301 	  } else {
302 	    pline("%s hits the floor.", Doname(obj));
303 	    if(obj->otyp == EXPENSIVE_CAMERA) {
304 		pline("It is shattered in a thousand pieces!");
305 		obfree(obj, Null(obj));
306 	    } else if(obj->otyp == EGG) {
307 		pline("\"Splash!\"");
308 		obfree(obj, Null(obj));
309 	    } else if(obj->olet == POTION_SYM) {
310 		pline("The flask breaks, and you smell a peculiar odor ...");
311 		potionbreathe(obj);
312 		obfree(obj, Null(obj));
313 	    } else {
314 		dropy(obj);
315 	    }
316 	  }
317 	  return(1);
318 	} else if(obj->otyp == BOOMERANG) {
319 		mon = boomhit(u.dx, u.dy);
320 		if(mon == &youmonst) {		/* the thing was caught */
321 			(void) addinv(obj);
322 			return(1);
323 		}
324 	} else {
325 		if(obj->otyp == PICK_AXE && shkcatch(obj))
326 		    return(1);
327 
328 		mon = bhit(u.dx, u.dy, (obj->otyp == ICE_BOX) ? 1 :
329 			(!Punished || obj != uball) ? 8 : !u.ustuck ? 5 : 1,
330 			obj->olet,
331 			(int (*)()) 0, (int (*)()) 0, obj);
332 	}
333 	if(mon) {
334 		/* awake monster if sleeping */
335 		wakeup(mon);
336 
337 		if(obj->olet == WEAPON_SYM) {
338 			tmp = -1+u.ulevel+mon->data->ac+abon();
339 			if(obj->otyp < ROCK) {
340 				if(!uwep ||
341 				    uwep->otyp != obj->otyp+(BOW-ARROW))
342 					tmp -= 4;
343 				else {
344 					tmp += uwep->spe;
345 				}
346 			} else
347 			if(obj->otyp == BOOMERANG) tmp += 4;
348 			tmp += obj->spe;
349 			if(u.uswallow || tmp >= rnd(20)) {
350 				if(hmon(mon,obj,1) == TRUE){
351 				  /* mon still alive */
352 #ifndef NOWORM
353 				  cutworm(mon,bhitpos.x,bhitpos.y,obj->otyp);
354 #endif NOWORM
355 				} else mon = 0;
356 				/* weapons thrown disappear sometimes */
357 				if(obj->otyp < BOOMERANG && rn2(3)) {
358 					/* check bill; free */
359 					obfree(obj, (struct obj *) 0);
360 					return(1);
361 				}
362 			} else miss(objects[obj->otyp].oc_name, mon);
363 		} else if(obj->otyp == HEAVY_IRON_BALL) {
364 			tmp = -1+u.ulevel+mon->data->ac+abon();
365 			if(!Punished || obj != uball) tmp += 2;
366 			if(u.utrap) tmp -= 2;
367 			if(u.uswallow || tmp >= rnd(20)) {
368 				if(hmon(mon,obj,1) == FALSE)
369 					mon = 0;	/* he died */
370 			} else miss("iron ball", mon);
371 		} else if(obj->olet == POTION_SYM && u.ulevel > rn2(15)) {
372 			potionhit(mon, obj);
373 			return(1);
374 		} else {
375 			if(cansee(bhitpos.x,bhitpos.y))
376 				pline("You miss %s.",monnam(mon));
377 			else pline("You miss it.");
378 			if(obj->olet == FOOD_SYM && mon->data->mlet == 'd')
379 				if(tamedog(mon,obj)) return(1);
380 			if(obj->olet == GEM_SYM && mon->data->mlet == 'u' &&
381 				!mon->mtame){
382 			 if(obj->dknown && objects[obj->otyp].oc_name_known){
383 			  if(objects[obj->otyp].g_val > 0){
384 			    u.uluck += 5;
385 			    goto valuable;
386 			  } else {
387 			    pline("%s is not interested in your junk.",
388 				Monnam(mon));
389 			  }
390 			 } else { /* value unknown to @ */
391 			    u.uluck++;
392 			valuable:
393 			    if(u.uluck > LUCKMAX)	/* dan@ut-ngp */
394 				u.uluck = LUCKMAX;
395 			    pline("%s graciously accepts your gift.",
396 				Monnam(mon));
397 			    mpickobj(mon, obj);
398 			    rloc(mon);
399 			    return(1);
400 			 }
401 			}
402 		}
403 	}
404 		/* the code following might become part of dropy() */
405 	if(obj->otyp == CRYSKNIFE)
406 		obj->otyp = WORM_TOOTH;
407 	obj->ox = bhitpos.x;
408 	obj->oy = bhitpos.y;
409 	obj->nobj = fobj;
410 	fobj = obj;
411 	/* prevent him from throwing articles to the exit and escaping */
412 	/* subfrombill(obj); */
413 	stackobj(obj);
414 	if(Punished && obj == uball &&
415 		(bhitpos.x != u.ux || bhitpos.y != u.uy)){
416 		freeobj(uchain);
417 		unpobj(uchain);
418 		if(u.utrap){
419 			if(u.utraptype == TT_PIT)
420 				pline("The ball pulls you out of the pit!");
421 			else {
422 			    register long side =
423 				rn2(3) ? LEFT_SIDE : RIGHT_SIDE;
424 			    pline("The ball pulls you out of the bear trap.");
425 			    pline("Your %s leg is severely damaged.",
426 				(side == LEFT_SIDE) ? "left" : "right");
427 			    set_wounded_legs(side, 500+rn2(1000));
428 			    losehp(2, "thrown ball");
429 			}
430 			u.utrap = 0;
431 		}
432 		unsee();
433 		uchain->nobj = fobj;
434 		fobj = uchain;
435 		u.ux = uchain->ox = bhitpos.x - u.dx;
436 		u.uy = uchain->oy = bhitpos.y - u.dy;
437 		setsee();
438 		(void) inshop();
439 	}
440 	if(cansee(bhitpos.x, bhitpos.y)) prl(bhitpos.x,bhitpos.y);
441 	return(1);
442 }
443 
444 /* split obj so that it gets size num */
445 /* remainder is put in the object structure delivered by this call */
446 struct obj *
447 splitobj(obj, num) register struct obj *obj; register int num; {
448 register struct obj *otmp;
449 	otmp = newobj(0);
450 	*otmp = *obj;		/* copies whole structure */
451 	otmp->o_id = flags.ident++;
452 	otmp->onamelth = 0;
453 	obj->quan = num;
454 	obj->owt = weight(obj);
455 	otmp->quan -= num;
456 	otmp->owt = weight(otmp);	/* -= obj->owt ? */
457 	obj->nobj = otmp;
458 	if(obj->unpaid) splitbill(obj,otmp);
459 	return(otmp);
460 }
461 
462 more_experienced(exp,rexp)
463 register int exp, rexp;
464 {
465 	extern char pl_character[];
466 
467 	u.uexp += exp;
468 	u.urexp += 4*exp + rexp;
469 	if(exp) flags.botl = 1;
470 	if(u.urexp >= ((pl_character[0] == 'W') ? 1000 : 2000))
471 		flags.beginner = 0;
472 }
473 
474 set_wounded_legs(side, timex)
475 register long side;
476 register int timex;
477 {
478 	if(!Wounded_legs || (Wounded_legs & TIMEOUT))
479 		Wounded_legs |= side + timex;
480 	else
481 		Wounded_legs |= side;
482 }
483 
484 heal_legs()
485 {
486 	if(Wounded_legs) {
487 		if((Wounded_legs & BOTH_SIDES) == BOTH_SIDES)
488 			pline("Your legs feel somewhat better.");
489 		else
490 			pline("Your leg feels somewhat better.");
491 		Wounded_legs = 0;
492 	}
493 }
494