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