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