NetBSD/games/hack/hack.do.c

530 lines
12 KiB
C
Raw Normal View History

/* $NetBSD: hack.do.c,v 1.5 2001/03/25 20:43:59 jsm Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
__RCSID("$NetBSD: hack.do.c,v 1.5 2001/03/25 20:43:59 jsm Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
/* Contains code for 'd', 'D' (drop), '>', '<' (up, down) and 't' (throw) */
#include "hack.h"
1997-10-19 20:56:41 +04:00
#include "extern.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
static int drop __P((struct obj *));
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
int
dodrop()
{
return (drop(getobj("0$#", "drop")));
1993-03-21 12:45:37 +03:00
}
static int
1997-10-19 20:56:41 +04:00
drop(obj)
struct obj *obj;
{
if (!obj)
return (0);
if (obj->olet == '$') { /* pseudo object */
long amount = OGOLD(obj);
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (amount == 0)
1993-03-21 12:45:37 +03:00
pline("You didn't drop any gold pieces.");
else {
mkgold(amount, u.ux, u.uy);
pline("You dropped %ld gold piece%s.",
1997-10-19 20:56:41 +04:00
amount, plur(amount));
if (Invisible)
newsym(u.ux, u.uy);
1993-03-21 12:45:37 +03:00
}
free((char *) obj);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (obj->owornmask & (W_ARMOR | W_RING)) {
1993-03-21 12:45:37 +03:00
pline("You cannot drop something you are wearing.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (obj == uwep) {
if (uwep->cursed) {
1993-03-21 12:45:37 +03:00
pline("Your weapon is welded to your hand!");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
setuwep((struct obj *) 0);
}
pline("You dropped %s.", doname(obj));
dropx(obj);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
/* Called in several places - should not produce texts */
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
dropx(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
freeinv(obj);
dropy(obj);
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
dropy(obj)
1997-10-19 20:56:41 +04:00
struct obj *obj;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (obj->otyp == CRYSKNIFE)
1993-03-21 12:45:37 +03:00
obj->otyp = WORM_TOOTH;
obj->ox = u.ux;
obj->oy = u.uy;
obj->nobj = fobj;
fobj = obj;
1997-10-19 20:56:41 +04:00
if (Invisible)
newsym(u.ux, u.uy);
1993-03-21 12:45:37 +03:00
subfrombill(obj);
stackobj(obj);
}
/* drop several things */
1997-10-19 20:56:41 +04:00
int
doddrop()
{
return (ggetobj("drop", drop, 0));
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
dodown()
{
1997-10-19 20:56:41 +04:00
if (u.ux != xdnstair || u.uy != ydnstair) {
1993-03-21 12:45:37 +03:00
pline("You can't go down here.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (u.ustuck) {
1993-03-21 12:45:37 +03:00
pline("You are being held, and cannot go down.");
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (Levitation) {
1993-03-21 12:45:37 +03:00
pline("You're floating high above the stairs.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
goto_level(dlevel + 1, TRUE);
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
doup()
{
1997-10-19 20:56:41 +04:00
if (u.ux != xupstair || u.uy != yupstair) {
1993-03-21 12:45:37 +03:00
pline("You can't go up here.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (u.ustuck) {
1993-03-21 12:45:37 +03:00
pline("You are being held, and cannot go up.");
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (!Levitation && inv_weight() + 5 > 0) {
1993-03-21 12:45:37 +03:00
pline("Your load is too heavy to climb the stairs.");
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
goto_level(dlevel - 1, TRUE);
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
goto_level(newlevel, at_stairs)
1997-10-19 20:56:41 +04:00
int newlevel;
boolean at_stairs;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int fd;
boolean up = (newlevel < dlevel);
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (newlevel <= 0)
done("escaped");/* in fact < 0 is impossible */
if (newlevel > MAXLEVEL)
newlevel = MAXLEVEL; /* strange ... */
if (newlevel == dlevel)
return; /* this can happen */
1993-03-21 12:45:37 +03:00
glo(dlevel);
fd = creat(lock, FMASK);
1997-10-19 20:56:41 +04:00
if (fd < 0) {
1993-03-21 12:45:37 +03:00
/*
* This is not quite impossible: e.g., we may have
* exceeded our quota. If that is the case then we
* cannot leave this level, and cannot save either.
* Another possibility is that the directory was not
* writable.
*/
pline("A mysterious force prevents you from going %s.",
1997-10-19 20:56:41 +04:00
up ? "up" : "down");
1993-03-21 12:45:37 +03:00
return;
}
1997-10-19 20:56:41 +04:00
if (Punished)
unplacebc();
u.utrap = 0; /* needed in level_tele */
u.ustuck = 0; /* idem */
1993-03-21 12:45:37 +03:00
keepdogs();
seeoff(1);
1997-10-19 20:56:41 +04:00
if (u.uswallow) /* idem */
1993-03-21 12:45:37 +03:00
u.uswldtim = u.uswallow = 0;
flags.nscrinh = 1;
1997-10-19 20:56:41 +04:00
u.ux = FAR; /* hack */
(void) inshop(); /* probably was a trapdoor */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
savelev(fd, dlevel);
1993-03-21 12:45:37 +03:00
(void) close(fd);
dlevel = newlevel;
1997-10-19 20:56:41 +04:00
if (maxdlevel < dlevel)
1993-03-21 12:45:37 +03:00
maxdlevel = dlevel;
glo(dlevel);
1997-10-19 20:56:41 +04:00
if (!level_exists[dlevel])
1993-03-21 12:45:37 +03:00
mklev();
else {
if ((fd = open(lock, O_RDONLY)) < 0) {
1993-03-21 12:45:37 +03:00
pline("Cannot open %s .", lock);
pline("Probably someone removed it.");
done("tricked");
}
getlev(fd, hackpid, dlevel);
(void) close(fd);
}
1997-10-19 20:56:41 +04:00
if (at_stairs) {
if (up) {
u.ux = xdnstair;
u.uy = ydnstair;
if (!u.ux) { /* entering a maze from below? */
u.ux = xupstair; /* this will confuse the
* player! */
u.uy = yupstair;
}
if (Punished && !Levitation) {
pline("With great effort you climb the stairs.");
placebc(1);
}
} else {
u.ux = xupstair;
u.uy = yupstair;
if (inv_weight() + 5 > 0 || Punished) {
pline("You fall down the stairs."); /* %% */
losehp(rnd(3), "fall");
if (Punished) {
if (uwep != uball && rn2(3)) {
pline("... and are hit by the iron ball.");
losehp(rnd(20), "iron ball");
}
placebc(1);
}
selftouch("Falling, you");
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
{
struct monst *mtmp = m_at(u.ux, u.uy);
if (mtmp)
mnexto(mtmp);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
} else { /* trapdoor or level_tele */
do {
u.ux = rnd(COLNO - 1);
u.uy = rn2(ROWNO);
} while (levl[u.ux][u.uy].typ != ROOM ||
m_at(u.ux, u.uy));
if (Punished) {
if (uwep != uball && !up /* %% */ && rn2(5)) {
pline("The iron ball falls on your head.");
losehp(rnd(25), "iron ball");
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
placebc(1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
selftouch("Falling, you");
1993-03-21 12:45:37 +03:00
}
(void) inshop();
initrack();
losedogs();
1997-10-19 20:56:41 +04:00
{
struct monst *mtmp;
if ((mtmp = m_at(u.ux, u.uy)) != NULL)
mnexto(mtmp); /* riv05!a3 */
1993-03-21 12:45:37 +03:00
}
flags.nscrinh = 0;
setsee();
1997-10-19 20:56:41 +04:00
seeobjs(); /* make old cadavers disappear - riv05!a3 */
1993-03-21 12:45:37 +03:00
docrt();
pickup(1);
1997-10-19 20:56:41 +04:00
read_engr_at(u.ux, u.uy);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
donull()
{
return (1); /* Do nothing, but let other things happen */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
dopray()
{
1993-03-21 12:45:37 +03:00
nomovemsg = "You finished your prayer.";
nomul(-3);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
dothrow()
{
1997-10-19 20:56:41 +04:00
struct obj *obj;
struct monst *mon;
int tmp;
obj = getobj("#)", "throw"); /* it is also possible to throw food */
/* (or jewels, or iron balls ... ) */
if (!obj || !getdir(1)) /* ask "in what direction?" */
return (0);
if (obj->owornmask & (W_ARMOR | W_RING)) {
1993-03-21 12:45:37 +03:00
pline("You can't throw something you are wearing.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
u_wipe_engr(2);
1997-10-19 20:56:41 +04:00
if (obj == uwep) {
if (obj->cursed) {
1993-03-21 12:45:37 +03:00
pline("Your weapon is welded to your hand.");
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (obj->quan > 1)
1993-03-21 12:45:37 +03:00
setuwep(splitobj(obj, 1));
else
setuwep((struct obj *) 0);
1997-10-19 20:56:41 +04:00
} else if (obj->quan > 1)
1993-03-21 12:45:37 +03:00
(void) splitobj(obj, 1);
freeinv(obj);
1997-10-19 20:56:41 +04:00
if (u.uswallow) {
1993-03-21 12:45:37 +03:00
mon = u.ustuck;
bhitpos.x = mon->mx;
bhitpos.y = mon->my;
1997-10-19 20:56:41 +04:00
} else if (u.dz) {
if (u.dz < 0) {
pline("%s hits the ceiling, then falls back on top of your head.",
Doname(obj)); /* note: obj->quan == 1 */
if (obj->olet == POTION_SYM)
potionhit(&youmonst, obj);
else {
if (uarmh)
pline("Fortunately, you are wearing a helmet!");
losehp(uarmh ? 1 : rnd((int) (obj->owt)), "falling object");
dropy(obj);
}
} else {
pline("%s hits the floor.", Doname(obj));
if (obj->otyp == EXPENSIVE_CAMERA) {
pline("It is shattered in a thousand pieces!");
obfree(obj, Null(obj));
} else if (obj->otyp == EGG) {
pline("\"Splash!\"");
obfree(obj, Null(obj));
} else if (obj->olet == POTION_SYM) {
pline("The flask breaks, and you smell a peculiar odor ...");
potionbreathe(obj);
obfree(obj, Null(obj));
} else {
dropy(obj);
}
}
return (1);
} else if (obj->otyp == BOOMERANG) {
1993-03-21 12:45:37 +03:00
mon = boomhit(u.dx, u.dy);
1997-10-19 20:56:41 +04:00
if (mon == &youmonst) { /* the thing was caught */
1993-03-21 12:45:37 +03:00
(void) addinv(obj);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
} else {
1997-10-19 20:56:41 +04:00
if (obj->otyp == PICK_AXE && shkcatch(obj))
return (1);
1993-03-21 12:45:37 +03:00
mon = bhit(u.dx, u.dy, (obj->otyp == ICE_BOX) ? 1 :
(!Punished || obj != uball) ? 8 : !u.ustuck ? 5 : 1,
1997-10-19 20:56:41 +04:00
obj->olet,
(void (*) __P((struct monst *, struct obj *))) 0,
(int (*) __P((struct obj *, struct obj *))) 0, obj);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (mon) {
1993-03-21 12:45:37 +03:00
/* awake monster if sleeping */
wakeup(mon);
1997-10-19 20:56:41 +04:00
if (obj->olet == WEAPON_SYM) {
tmp = -1 + u.ulevel + mon->data->ac + abon();
if (obj->otyp < ROCK) {
if (!uwep ||
uwep->otyp != obj->otyp + (BOW - ARROW))
1993-03-21 12:45:37 +03:00
tmp -= 4;
else {
tmp += uwep->spe;
}
1997-10-19 20:56:41 +04:00
} else if (obj->otyp == BOOMERANG)
tmp += 4;
1993-03-21 12:45:37 +03:00
tmp += obj->spe;
1997-10-19 20:56:41 +04:00
if (u.uswallow || tmp >= rnd(20)) {
if (hmon(mon, obj, 1) == TRUE) {
/* mon still alive */
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
cutworm(mon, bhitpos.x, bhitpos.y, obj->otyp);
#endif /* NOWORM */
} else
mon = 0;
1993-03-21 12:45:37 +03:00
/* weapons thrown disappear sometimes */
1997-10-19 20:56:41 +04:00
if (obj->otyp < BOOMERANG && rn2(3)) {
1993-03-21 12:45:37 +03:00
/* check bill; free */
obfree(obj, (struct obj *) 0);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
} else
miss(objects[obj->otyp].oc_name, mon);
} else if (obj->otyp == HEAVY_IRON_BALL) {
tmp = -1 + u.ulevel + mon->data->ac + abon();
if (!Punished || obj != uball)
tmp += 2;
if (u.utrap)
tmp -= 2;
if (u.uswallow || tmp >= rnd(20)) {
if (hmon(mon, obj, 1) == FALSE)
1993-03-21 12:45:37 +03:00
mon = 0; /* he died */
1997-10-19 20:56:41 +04:00
} else
miss("iron ball", mon);
} else if (obj->olet == POTION_SYM && u.ulevel > rn2(15)) {
1993-03-21 12:45:37 +03:00
potionhit(mon, obj);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
if (cansee(bhitpos.x, bhitpos.y))
pline("You miss %s.", monnam(mon));
else
pline("You miss it.");
if (obj->olet == FOOD_SYM && mon->data->mlet == 'd')
if (tamedog(mon, obj))
return (1);
if (obj->olet == GEM_SYM && mon->data->mlet == 'u' &&
!mon->mtame) {
if (obj->dknown && objects[obj->otyp].oc_name_known) {
if (objects[obj->otyp].g_val > 0) {
u.uluck += 5;
goto valuable;
} else {
pline("%s is not interested in your junk.",
Monnam(mon));
}
} else { /* value unknown to @ */
u.uluck++;
1993-03-21 12:45:37 +03:00
valuable:
1997-10-19 20:56:41 +04:00
if (u.uluck > LUCKMAX) /* dan@ut-ngp */
u.uluck = LUCKMAX;
pline("%s graciously accepts your gift.",
Monnam(mon));
mpickobj(mon, obj);
rloc(mon);
return (1);
}
1993-03-21 12:45:37 +03:00
}
}
}
1997-10-19 20:56:41 +04:00
/* the code following might become part of dropy() */
if (obj->otyp == CRYSKNIFE)
1993-03-21 12:45:37 +03:00
obj->otyp = WORM_TOOTH;
obj->ox = bhitpos.x;
obj->oy = bhitpos.y;
obj->nobj = fobj;
fobj = obj;
/* prevent him from throwing articles to the exit and escaping */
/* subfrombill(obj); */
stackobj(obj);
1997-10-19 20:56:41 +04:00
if (Punished && obj == uball &&
(bhitpos.x != u.ux || bhitpos.y != u.uy)) {
1993-03-21 12:45:37 +03:00
freeobj(uchain);
unpobj(uchain);
1997-10-19 20:56:41 +04:00
if (u.utrap) {
if (u.utraptype == TT_PIT)
1993-03-21 12:45:37 +03:00
pline("The ball pulls you out of the pit!");
else {
1997-10-19 20:56:41 +04:00
long side =
1993-03-21 12:45:37 +03:00
rn2(3) ? LEFT_SIDE : RIGHT_SIDE;
1997-10-19 20:56:41 +04:00
pline("The ball pulls you out of the bear trap.");
pline("Your %s leg is severely damaged.",
(side == LEFT_SIDE) ? "left" : "right");
set_wounded_legs(side, 500 + rn2(1000));
losehp(2, "thrown ball");
1993-03-21 12:45:37 +03:00
}
u.utrap = 0;
}
unsee();
uchain->nobj = fobj;
fobj = uchain;
u.ux = uchain->ox = bhitpos.x - u.dx;
u.uy = uchain->oy = bhitpos.y - u.dy;
setsee();
(void) inshop();
}
1997-10-19 20:56:41 +04:00
if (cansee(bhitpos.x, bhitpos.y))
prl(bhitpos.x, bhitpos.y);
return (1);
1993-03-21 12:45:37 +03:00
}
/* split obj so that it gets size num */
/* remainder is put in the object structure delivered by this call */
1997-10-19 20:56:41 +04:00
struct obj *
splitobj(obj, num)
struct obj *obj;
int num;
{
struct obj *otmp;
1993-03-21 12:45:37 +03:00
otmp = newobj(0);
*otmp = *obj; /* copies whole structure */
otmp->o_id = flags.ident++;
otmp->onamelth = 0;
obj->quan = num;
obj->owt = weight(obj);
otmp->quan -= num;
otmp->owt = weight(otmp); /* -= obj->owt ? */
obj->nobj = otmp;
1997-10-19 20:56:41 +04:00
if (obj->unpaid)
splitbill(obj, otmp);
return (otmp);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
more_experienced(exp, rexp)
int exp, rexp;
1993-03-21 12:45:37 +03:00
{
u.uexp += exp;
1997-10-19 20:56:41 +04:00
u.urexp += 4 * exp + rexp;
if (exp)
flags.botl = 1;
if (u.urexp >= ((pl_character[0] == 'W') ? 1000 : 2000))
1993-03-21 12:45:37 +03:00
flags.beginner = 0;
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
set_wounded_legs(side, timex)
1997-10-19 20:56:41 +04:00
long side;
int timex;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (!Wounded_legs || (Wounded_legs & TIMEOUT))
1993-03-21 12:45:37 +03:00
Wounded_legs |= side + timex;
else
Wounded_legs |= side;
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
heal_legs()
{
1997-10-19 20:56:41 +04:00
if (Wounded_legs) {
if ((Wounded_legs & BOTH_SIDES) == BOTH_SIDES)
1993-03-21 12:45:37 +03:00
pline("Your legs feel somewhat better.");
else
pline("Your leg feels somewhat better.");
Wounded_legs = 0;
}
}