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