xref: /dragonfly/games/hack/hack.apply.c (revision c69bf40f)
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.apply.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.apply.c,v 1.4.2.1 2001/02/18 02:20:07 kris Exp $ */
4 /* $DragonFly: src/games/hack/hack.apply.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
5 
6 #include "hack.h"
7 #include "def.edog.h"
8 extern char quitchars[];
9 
10 static void		 use_camera(struct obj *);
11 static bool		 in_ice_box(struct obj *);
12 static bool		 ck_ice_box(struct obj *);
13 static int		 out_ice_box(struct obj *);
14 static void		 use_ice_box(struct obj *);
15 static struct monst	*bchit(int, int, int, char);
16 static void		 use_whistle(struct obj *);
17 static void		 use_magic_whistle(struct obj *);
18 static bool		 dig(void);
19 static int		 use_pick_axe(struct obj *);
20 
21 int
22 doapply(void)
23 {
24 	struct obj *obj;
25 	int res = 1;
26 
27 	obj = getobj("(", "use or apply");
28 	if (!obj)
29 		return (0);
30 
31 	switch (obj->otyp) {
32 	case EXPENSIVE_CAMERA:
33 		use_camera(obj);
34 		break;
35 	case ICE_BOX:
36 		use_ice_box(obj);
37 		break;
38 	case PICK_AXE:
39 		res = use_pick_axe(obj);
40 		break;
41 
42 	case MAGIC_WHISTLE:
43 		if (pl_character[0] == 'W' || u.ulevel > 9) {
44 			use_magic_whistle(obj);
45 			break;
46 		}
47 		/* fall into next case */
48 	case WHISTLE:
49 		use_whistle(obj);
50 		break;
51 
52 	case CAN_OPENER:
53 		if (!carrying(TIN)) {
54 			pline("You have no can to open.");
55 			goto xit;
56 		}
57 		pline("You cannot open a tin without eating its contents.");
58 		pline("In order to eat, use the 'e' command.");
59 		if (obj != uwep)
60 			pline("Opening the tin will be much easier if you wield the can-opener.");
61 		goto xit;
62 
63 	default:
64 		pline("Sorry, I don't know how to use that.");
65 xit:
66 		nomul(0);
67 		return (0);
68 	}
69 	nomul(0);
70 	return (res);
71 }
72 
73 static void
74 use_camera(struct obj *obj __unused)
75 {
76 	struct monst *mtmp;
77 
78 	if (!getdir(1)) {	/* ask: in what direction? */
79 		flags.move = multi = 0;
80 		return;
81 	}
82 	if (u.uswallow) {
83 		pline("You take a picture of %s's stomach.", monnam(u.ustuck));
84 		return;
85 	}
86 	if (u.dz) {
87 		pline("You take a picture of the %s.",
88 		    (u.dz > 0) ? "floor" : "ceiling");
89 		return;
90 	}
91 	if ((mtmp = bchit(u.dx, u.dy, COLNO, '!')) != NULL) {
92 		if (mtmp->msleep) {
93 			mtmp->msleep = 0;
94 			pline("The flash awakens %s.", monnam(mtmp));	/* a3 */
95 		} else if (mtmp->data->mlet != 'y')
96 			if (mtmp->mcansee || mtmp->mblinded) {
97 				int tmp = dist(mtmp->mx, mtmp->my);
98 				int tmp2;
99 				if (cansee(mtmp->mx, mtmp->my))
100 					pline("%s is blinded by the flash!", Monnam(mtmp));
101 				setmangry(mtmp);
102 				if (tmp < 9 && !mtmp->isshk && rn2(4)) {
103 					mtmp->mflee = 1;
104 					if (rn2(4))
105 						mtmp->mfleetim = rnd(100);
106 				}
107 				if (tmp < 3)
108 					mtmp->mcansee = mtmp->mblinded = 0;
109 				else {
110 					tmp2 = mtmp->mblinded;
111 					tmp2 += rnd(1 + 50 / tmp);
112 					if (tmp2 > 127)
113 						tmp2 = 127;
114 					mtmp->mblinded = tmp2;
115 					mtmp->mcansee = 0;
116 				}
117 			}
118 	}
119 }
120 
121 static
122 struct obj *current_ice_box;	/* a local variable of use_ice_box, to be
123 				used by its local procedures in/ck_ice_box */
124 static bool
125 in_ice_box(struct obj *obj)
126 {
127 	if (obj == current_ice_box ||
128 	    (Punished && (obj == uball || obj == uchain))) {
129 		pline("You must be kidding.");
130 		return (0);
131 	}
132 	if (obj->owornmask & (W_ARMOR | W_RING)) {
133 		pline("You cannot refrigerate something you are wearing.");
134 		return (0);
135 	}
136 	if (obj->owt + current_ice_box->owt > 70) {
137 		pline("It won't fit.");
138 		return (1);	/* be careful! */
139 	}
140 	if (obj == uwep) {
141 		if (uwep->cursed) {
142 			pline("Your weapon is welded to your hand!");
143 			return (0);
144 		}
145 		setuwep(NULL);
146 	}
147 	current_ice_box->owt += obj->owt;
148 	freeinv(obj);
149 	obj->o_cnt_id = current_ice_box->o_id;
150 	obj->nobj = fcobj;
151 	fcobj = obj;
152 	obj->age = moves - obj->age;	/* actual age */
153 	return (1);
154 }
155 
156 static bool
157 ck_ice_box(struct obj *obj)
158 {
159 	return (obj->o_cnt_id == current_ice_box->o_id);
160 }
161 
162 static int
163 out_ice_box(struct obj *obj)
164 {
165 	struct obj *otmp;
166 
167 	if (obj == fcobj)
168 		fcobj = fcobj->nobj;
169 	else {
170 		for (otmp = fcobj; otmp->nobj != obj; otmp = otmp->nobj)
171 			if (!otmp->nobj)
172 				panic("out_ice_box");
173 		otmp->nobj = obj->nobj;
174 	}
175 	current_ice_box->owt -= obj->owt;
176 	obj->age = moves - obj->age;	/* simulated point of time */
177 	addinv(obj);
178 	return (0);
179 }
180 
181 static void
182 use_ice_box(struct obj *obj)
183 {
184 	int cnt = 0;
185 	struct obj *otmp;
186 
187 	current_ice_box = obj;	/* for use by in/out_ice_box */
188 	for (otmp = fcobj; otmp; otmp = otmp->nobj)
189 		if (otmp->o_cnt_id == obj->o_id)
190 			cnt++;
191 	if (!cnt)
192 		pline("Your ice-box is empty.");
193 	else {
194 		pline("Do you want to take something out of the ice-box? [yn] ");
195 		if (readchar() == 'y')
196 			if (askchain(fcobj, NULL, 0, out_ice_box, ck_ice_box, 0))
197 				return;
198 		pline("That was all. Do you wish to put something in? [yn] ");
199 		if (readchar() != 'y')
200 			return;
201 	}
202 	/* call getobj: 0: allow cnt; #: allow all types; %: expect food */
203 	otmp = getobj("0#%", "put in");
204 	if (!otmp || !in_ice_box(otmp))
205 		flags.move = multi = 0;
206 }
207 
208 static
209 struct monst *
210 bchit(int ddx, int ddy, int range, char sym)
211 {
212 	struct monst *mtmp = NULL;
213 	int bchx = u.ux, bchy = u.uy;
214 
215 	if (sym)
216 		Tmp_at(-1, sym);	/* open call */
217 	while (range--) {
218 		bchx += ddx;
219 		bchy += ddy;
220 		if ((mtmp = m_at(bchx, bchy)))
221 			break;
222 		if (!ZAP_POS(levl[bchx][bchy].typ)) {
223 			bchx -= ddx;
224 			bchy -= ddy;
225 			break;
226 		}
227 		if (sym)
228 			Tmp_at(bchx, bchy);
229 	}
230 	if (sym)
231 		Tmp_at(-1, -1);
232 	return (mtmp);
233 }
234 
235 static void
236 use_whistle(struct obj *obj __unused)
237 {
238 	struct monst *mtmp = fmon;
239 
240 	pline("You produce a high whistling sound.");
241 	while (mtmp) {
242 		if (dist(mtmp->mx, mtmp->my) < u.ulevel * 20) {
243 			if (mtmp->msleep)
244 				mtmp->msleep = 0;
245 			if (mtmp->mtame)
246 				EDOG(mtmp)->whistletime = moves;
247 		}
248 		mtmp = mtmp->nmon;
249 	}
250 }
251 
252 static void
253 use_magic_whistle(struct obj *obj __unused)
254 {
255 	struct monst *mtmp = fmon;
256 
257 	pline("You produce a strange whistling sound.");
258 	while (mtmp) {
259 		if (mtmp->mtame)
260 			mnexto(mtmp);
261 		mtmp = mtmp->nmon;
262 	}
263 }
264 
265 static int dig_effort;	/* effort expended on current pos */
266 static uchar dig_level;
267 static coord dig_pos;
268 static boolean dig_down;
269 
270 static
271 bool
272 dig(void)
273 {
274 	struct rm *lev;
275 	int dpx = dig_pos.x, dpy = dig_pos.y;
276 
277 	/* perhaps a nymph stole his pick-axe while he was busy digging */
278 	/* or perhaps he teleported away */
279 	if (u.uswallow || !uwep || uwep->otyp != PICK_AXE ||
280 	    dig_level != dlevel ||
281 	    ((dig_down && (dpx != u.ux || dpy != u.uy)) ||
282 	     (!dig_down && dist(dpx, dpy) > 2)))
283 		return (0);
284 
285 	dig_effort += 10 + abon() + uwep->spe + rn2(5);
286 	if (dig_down) {
287 		if (!xdnstair) {
288 			pline("The floor here seems too hard to dig in.");
289 			return (0);
290 		}
291 		if (dig_effort > 250) {
292 			dighole();
293 			return (0);	/* done with digging */
294 		}
295 		if (dig_effort > 50) {
296 			struct trap *ttmp = t_at(dpx, dpy);
297 
298 			if (!ttmp) {
299 				ttmp = maketrap(dpx, dpy, PIT);
300 				ttmp->tseen = 1;
301 				pline("You have dug a pit.");
302 				u.utrap = rn1(4, 2);
303 				u.utraptype = TT_PIT;
304 				return (0);
305 			}
306 		}
307 	} else if (dig_effort > 100) {
308 		const char *digtxt;
309 		struct obj *obj;
310 
311 		lev = &levl[dpx][dpy];
312 		if ((obj = sobj_at(ENORMOUS_ROCK, dpx, dpy)) != NULL) {
313 			fracture_rock(obj);
314 			digtxt = "The rock falls apart.";
315 		} else if (!lev->typ || lev->typ == SCORR) {
316 			lev->typ = CORR;
317 			digtxt = "You succeeded in cutting away some rock.";
318 		} else if (lev->typ == HWALL || lev->typ == VWALL
319 		    || lev->typ == SDOOR) {
320 			lev->typ = xdnstair ? DOOR : ROOM;
321 			digtxt = "You just made an opening in the wall.";
322 		} else
323 			digtxt = "Now what exactly was it that you were digging in?";
324 		mnewsym(dpx, dpy);
325 		prl(dpx, dpy);
326 		pline("%s", digtxt);	/* after mnewsym & prl */
327 		return (0);
328 	} else {
329 		if (IS_WALL(levl[dpx][dpy].typ)) {
330 			int rno = inroom(dpx, dpy);
331 
332 			if (rno >= 0 && rooms[rno].rtype >= 8) {
333 				pline("This wall seems too hard to dig into.");
334 				return (0);
335 			}
336 		}
337 		pline("You hit the rock with all your might.");
338 	}
339 	return (1);
340 }
341 
342 /* When will hole be finished? Very rough indication used by shopkeeper. */
343 int
344 holetime(void)
345 {
346 	return ((occupation == dig) ? (250 - dig_effort) / 20 : -1);
347 }
348 
349 void
350 dighole(void)
351 {
352 	struct trap *ttmp = t_at(u.ux, u.uy);
353 
354 	if (!xdnstair) {
355 		pline("The floor here seems too hard to dig in.");
356 	} else {
357 		if (ttmp)
358 			ttmp->ttyp = TRAPDOOR;
359 		else
360 			ttmp = maketrap(u.ux, u.uy, TRAPDOOR);
361 		ttmp->tseen = 1;
362 		pline("You've made a hole in the floor.");
363 		if (!u.ustuck) {
364 			if (inshop())
365 				shopdig(1);
366 			pline("You fall through ...");
367 			if (u.utraptype == TT_PIT) {
368 				u.utrap = 0;
369 				u.utraptype = 0;
370 			}
371 			goto_level(dlevel + 1, FALSE);
372 		}
373 	}
374 }
375 
376 static int
377 use_pick_axe(struct obj *obj)
378 {
379 	char dirsyms[12];
380 	char *dsp = dirsyms, *sdp = sdir;
381 	struct monst *mtmp;
382 	struct rm *lev;
383 	int rx, ry, res = 0;
384 
385 	if (obj != uwep) {
386 		if (uwep && uwep->cursed) {
387 			/* Andreas Bormann - ihnp4!decvax!mcvax!unido!ab */
388 			pline("Since your weapon is welded to your hand,");
389 			pline("you cannot use that pick-axe.");
390 			return (0);
391 		}
392 		pline("You now wield %s.", doname(obj));
393 		setuwep(obj);
394 		res = 1;
395 	}
396 	while (*sdp) {
397 		movecmd(*sdp);	/* sets u.dx and u.dy and u.dz */
398 		rx = u.ux + u.dx;
399 		ry = u.uy + u.dy;
400 		if (u.dz > 0 || (u.dz == 0 && isok(rx, ry) &&
401 		    (IS_ROCK(levl[rx][ry].typ)
402 		    || sobj_at(ENORMOUS_ROCK, rx, ry))))
403 			*dsp++ = *sdp;
404 		sdp++;
405 	}
406 	*dsp = 0;
407 	pline("In what direction do you want to dig? [%s] ", dirsyms);
408 	if (!getdir(0))		/* no txt */
409 		return (res);
410 	if (u.uswallow && attack(u.ustuck))	/* return(1) */
411 		;
412 	else if (u.dz < 0)
413 		pline("You cannot reach the ceiling.");
414 	else if (u.dz == 0) {
415 		if (Confusion)
416 			confdir();
417 		rx = u.ux + u.dx;
418 		ry = u.uy + u.dy;
419 		if ((mtmp = m_at(rx, ry)) && attack(mtmp))
420 			return (1);
421 		if (!isok(rx, ry)) {
422 			pline("Clash!");
423 			return (1);
424 		}
425 		lev = &levl[rx][ry];
426 		if (lev->typ == DOOR)
427 			pline("Your %s against the door.",
428 			    aobjnam(obj, "clang"));
429 		else if (!IS_ROCK(lev->typ)
430 		    && !sobj_at(ENORMOUS_ROCK, rx, ry)) {
431 			/* ACCESSIBLE or POOL */
432 			pline("You swing your %s through thin air.",
433 			    aobjnam(obj, NULL));
434 		} else {
435 			if (dig_pos.x != rx || dig_pos.y != ry
436 			    || dig_level != dlevel || dig_down) {
437 				dig_down = FALSE;
438 				dig_pos.x = rx;
439 				dig_pos.y = ry;
440 				dig_level = dlevel;
441 				dig_effort = 0;
442 				pline("You start digging.");
443 			} else
444 				pline("You continue digging.");
445 			occupation = dig;
446 			occtxt = "digging";
447 		}
448 	} else if (Levitation) {
449 		pline("You cannot reach the floor.");
450 	} else {
451 		if (dig_pos.x != u.ux || dig_pos.y != u.uy
452 		    || dig_level != dlevel || !dig_down) {
453 			dig_down = TRUE;
454 			dig_pos.x = u.ux;
455 			dig_pos.y = u.uy;
456 			dig_level = dlevel;
457 			dig_effort = 0;
458 			pline("You start digging in the floor.");
459 			if (inshop())
460 				shopdig(0);
461 		} else
462 			pline("You continue digging in the floor.");
463 		occupation = dig;
464 		occtxt = "digging";
465 	}
466 	return (1);
467 }
468