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