nexus/move.c

1503 lines
32 KiB
C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "attack.h"
#include "defs.h"
#include "flag.h"
#include "io.h"
#include "lf.h"
#include "map.h"
#include "move.h"
#include "nexus.h"
#include "objects.h"
#include "text.h"
extern lifeform_t *player;
extern int statdirty;
extern int needredraw;
extern enum GAMEMODE gamemode;
extern enum ERROR reason;
extern void *rdata;
int canandwillmove(lifeform_t *lf, int dir, enum ERROR *error) {
if (isplayer(lf)) {
if (canmove(lf, dir, error)) {
return B_TRUE;
}
} else {
if (canmove(lf, dir, error) && willmove(lf, dir, error)) {
return B_TRUE;
}
}
return B_FALSE;
}
int canmove(lifeform_t *lf, int dir, enum ERROR *error) {
cell_t *cell;
flag_t *f;
// default
if (error) {
*error = E_OK;
rdata = NULL;
}
if (isburdened(lf) >= BR_OVERLOADED) {
if (error) *error = E_TOOHEAVY;
return B_FALSE;
}
// check if we are paralyzed, frozen, etc
if (isimmobile(lf)) {
if (error) *error = E_CANTMOVE;
return B_FALSE;
}
cell = getcellindir(lf->cell, dir);
f = lfhasflag(lf, F_GRABBEDBY);
if (f) {
lifeform_t *lf2;
lf2 = findlf(NULL, f->val[0]);
if (lf2 && (lf2 != cell->lf)) {
if (error) {
rdata = lf2;
*error = E_GRABBEDBY;
}
return B_FALSE;
}
}
return cellwalkable(lf, cell, error);
}
// lf is the one moving, lf2 is the one who is being forced to swap
int canswapwith(lifeform_t *lf, lifeform_t *lf2) {
// player can never be forced to swap
if (isplayer(lf2)) {
return B_FALSE;
}
// cannot swap if lf's cell is dangerous to lf2
if (celldangerous(lf2, lf->cell, B_FALSE, NULL)) {
return B_FALSE;
}
// allies can always swap
if (areallies(lf, lf2)) {
return B_TRUE;
}
if (isplayer(lf) && !areenemies(lf, lf2)) {
// player can swap with peaceful lgs
// if they are a lot smaller
if (getlfsize(lf) - getlfsize(lf2) >= 2) {
return B_TRUE;
}
}
return B_FALSE;
}
// will populate rdata
// onlyifknown = true means "check for _known_ dangerous objects"
// onlyifknown = false means "check for _any dangerous objects, doesn't matter if we know about them"
int celldangerous(lifeform_t *lf, cell_t *cell, int onlyifknown, enum ERROR *error) {
enum IQBRACKET iq;
int include_nonobvious = B_FALSE;
flag_t *f;
object_t *o;
// default
if (error) {
*error = E_OK;
rdata = NULL;
}
// obvious things that you can see
if (!onlyifknown || (haslos(lf, cell) && !lfhasflag(lf, F_UNDEAD))) {
for (o = cell->obpile->first ; o ; o = o->next) {
f = hasflag(o->flags, F_WALKDAM);
if (f) {
// are we immune to this?
if (!lfhasflagval(lf, F_DTIMMUNE, f->val[0], NA, NA, NULL)) {
if (error) {
*error = E_AVOIDOB;
rdata = o;
}
return B_TRUE;
}
}
}
}
// non-obvious things - only include these if we are smart enough
if (!onlyifknown) {
include_nonobvious = B_TRUE;
} else {
iq = getiqname(getattr(lf, A_IQ), NULL);
if ((iq >= IQ_AVERAGE) && haslos(lf, cell)) {
if (!lfhasflag(lf, F_UNDEAD)) {
include_nonobvious = B_TRUE;
}
}
}
if (include_nonobvious) {
for (o = cell->obpile->first ; o ; o = o->next) {
// don't walk on sharp objects without boots
if (hasflag(o->flags, F_SHARP)) {
if (!getequippedob(lf->pack, BP_FEET)) {
if (error) {
*error = E_AVOIDOB;
rdata = o;
}
return B_TRUE;
}
}
}
}
return B_FALSE;
}
int cellwalkable(lifeform_t *lf, cell_t *cell, enum ERROR *error) {
object_t *o;
// default
if (error) {
*error = E_OK;
rdata = NULL;
}
if (!cell) {
if (error) *error = E_OFFMAP;
return B_FALSE;
}
if (cell->type->solid) {
// mover is noncorporeal?
if (lf && lfhasflag(lf, F_NONCORPOREAL)) {
// ok
} else if (cell->lf && (cell->lf != lf)) { // someone (else) in the wall?
// ok
} else {
if (error) *error = E_WALLINWAY;
return B_FALSE;
}
}
// must check for lf before checking for impassable objects,
// so that we are able to attack monsters embedded in walls.
if (cell->lf && (cell->lf != lf)) {
if (error) *error = E_LFINWAY;
return B_FALSE;
}
for (o = cell->obpile->first ; o ; o = o->next) {
if (isimpassableob(o, lf)) {
if (lf) {
if ((lf->race->material->id == MT_GAS) ||
(lf->race->material->id == MT_SLIME)) {
// ok
} else if (lfhasflag(lf, F_NONCORPOREAL)) {
// ok
} else {
rdata = o;
if (error) {
if (isdoor(o, NULL)) {
*error = E_DOORINWAY;
} else {
*error = E_OBINWAY;
}
}
return B_FALSE;
}
} else {
rdata = o;
if (error) {
if (isdoor(o, NULL)) {
*error = E_DOORINWAY;
} else {
*error = E_OBINWAY;
}
}
return B_FALSE;
}
}
}
return B_TRUE;
}
int diropposite(int dir) {
switch (dir) {
case D_N:
return D_S;
case D_E:
return D_W;
case D_S:
return D_N;
case D_W:
return D_E;
case DC_N:
return DC_S;
case DC_NE:
return DC_SW;
case DC_E:
return DC_W;
case DC_SE:
return DC_NW;
case DC_S:
return DC_N;
case DC_SW:
return DC_NE;
case DC_W:
return DC_E;
case DC_NW:
return DC_SE;
}
// should never happen!
return dir;
}
void dorandommove(lifeform_t *lf, int badmovesok) {
int dir;
int tries = 0;
int moveok;
enum ERROR why;
// find a valid direction
dir = getrandomdir(DT_COMPASS);
moveok = canandwillmove(lf, dir, &why);
if (!moveok && badmovesok) {
switch (why) {
// actually okay to move into someone
case E_WALLINWAY:
case E_LFINWAY:
moveok = B_TRUE;
break;
default:
break;
}
}
while (!moveok) {
// try next direction...
if (++dir > DC_NW) dir = DC_N;
if (++tries >= MAXDIR_COMPASS) {
rest(lf, B_TRUE);
return;
}
// check this direction...
moveok = canandwillmove(lf, dir, &why);
if (!moveok && badmovesok) {
switch (why) {
case E_WALLINWAY:
case E_LFINWAY:
moveok = B_TRUE;
break;
default:
break;
}
}
}
trymove(lf, dir, B_TRUE);
}
// src is where something is
// dst is what we are going away from
// wantcheck is whether to check for dangerous things before considering a direction valid
int getdiraway(cell_t *src, cell_t *dst, lifeform_t *srclf, int wantcheck, int dirtype) {
int d;
cell_t *c;
int maxdist=-1,bestdir=D_NONE;
int dist[MAXDIR_COMPASS];
int poss[MAXDIR_COMPASS];
int nposs;
enum ERROR error;
for (d = DC_N; d <= DC_NW; d++) {
dist[d - DC_N] = -1;
}
for (d = DC_N; d <= DC_NW; d++) {
int thisdist = -1;
int ok = B_FALSE;
c = getcellindir(src, d);
if (!c) continue;
if (c == dst) {
// destination is the thing we're fleeing from!
thisdist = 0;
} else {
if (wantcheck) {
if (srclf) {
if (canandwillmove(srclf, d, &error)) {
ok = B_TRUE;
} else if (error == E_DOORINWAY) {
ok = B_TRUE;
}
} else {
ok = B_TRUE;
}
} else {
if (srclf) {
if (cellwalkable(srclf, c, &error)) {
ok = B_TRUE;
} else if (error == E_DOORINWAY) {
ok = B_TRUE;
}
} else {
ok = B_TRUE;
}
}
if (ok) {
if (dirtype == DT_ORTH) {
thisdist = getcelldistorth(c, dst);
} else {
thisdist = getcelldist(c, dst);
}
} else {
thisdist = -1;
}
}
dist[d - DC_N] = thisdist;
if (thisdist > maxdist) {
maxdist = thisdist;
bestdir = d;
}
}
nposs = 0;
for (d = DC_N; d <= DC_NW; d++) {
if (dist[d - DC_N] != -1) {
if (dist[d - DC_N] == maxdist) {
poss[nposs] = d;
nposs++;
}
}
}
if (nposs <= 0) {
return D_NONE;
}
bestdir = poss[rnd(0,nposs-1)];
reason = E_OK;
return bestdir;
}
int getdirtowards(cell_t *src, cell_t *dst, lifeform_t *srclf, int wantcheck, int dirtype) {
int d;
cell_t *c;
int mindist=9999,bestdir=D_NONE;
int dist[MAXDIR_COMPASS];
int poss[MAXDIR_COMPASS];
int nposs;
enum ERROR error;
for (d = DC_N; d <= DC_NW; d++) {
dist[d - DC_N] = -1;
}
for (d = DC_N; d <= DC_NW; d++) {
int ok = B_FALSE;
c = getcellindir(src, d);
if (!c) continue;
if (c == dst) {
dist[d - DC_N] = 0;
mindist = 0;
break;
}
if (wantcheck) {
if (srclf) {
if (canandwillmove(srclf, d, &error)) {
ok = B_TRUE;
} else if (error == E_DOORINWAY) {
ok = B_TRUE;
}
} else {
ok = B_TRUE;
}
} else {
if (srclf) {
if (cellwalkable(srclf, c, &error)) {
ok = B_TRUE;
} else if (error == E_DOORINWAY) {
ok = B_TRUE;
}
} else {
ok = B_TRUE;
}
}
if (ok) {
int thisdist;
if (dirtype == DT_ORTH) {
thisdist = getcelldistorth(c, dst);
} else {
thisdist = getcelldist(c, dst);
}
dist[d - DC_N] = thisdist;
if (thisdist < mindist) {
mindist = thisdist;
}
} else {
dist[d - DC_N] = -1; // ie. invalid
}
}
// handle ties
nposs = 0;
for (d = DC_N; d <= DC_NW; d++) {
if (dist[d - DC_N] != -1) {
if (dist[d - DC_N] == mindist) {
poss[nposs] = d;
nposs++;
}
}
}
if (nposs <= 0) {
return D_NONE;
}
bestdir = poss[rnd(0,nposs-1)];
reason = E_OK;
return bestdir;
}
int knockback(lifeform_t *lf, int dir, int howfar, lifeform_t *pusher) {
int i;
char lfname[BUFLEN];
int seen;
getlfname(lf,lfname);
if (cansee(player, lf)) {
seen = B_TRUE;
} else {
seen = B_FALSE;
}
if (dir == D_NONE) {
// failed!
return B_TRUE;
}
// if levitating (not flying), knocked back further.
if (lfhasflag(lf, F_LEVITATING)) {
howfar *= 2;
}
for (i = 0; i < howfar; i++) {
if (canmove(lf, dir, &reason)) {
if ((i == 0) && seen) {
msg("%s %s knocked backwards!",lfname,isplayer(lf) ? "are" : "is");
}
trymove(lf, dir, B_FALSE);
}
if (reason != E_OK) {
// failed to move
switch (reason) {
case E_WALLINWAY:
case E_OFFMAP:
msg("%s slam%s into a wall!",lfname,isplayer(lf) ? "" : "s");
losehp(lf, rnd(1,6), DT_BASH, pusher, "slamming into a wall");
// stop moving
i = howfar;
break;
default:
break;
}
}
}
return B_FALSE;
}
// see 'movetowards' for description of dirtype
int moveawayfrom(lifeform_t *lf, cell_t *dst, int dirtype ) {
int dir;
int rv = B_TRUE;
if (isblind(lf)) {
dorandommove(lf, B_TRUE);
return B_FALSE;
}
// move away from them
dir = getdiraway(lf->cell, dst, lf, B_TRUE, dirtype);
if (dir == D_NONE) {
rv = B_TRUE;
} else {
rv = trymove(lf, dir, B_TRUE);
}
return rv;
}
// IMPORTANT: don't modify lf's flagpile during this code!
// particularly don't remove flags...
void moveeffects(lifeform_t *lf) {
flag_t *f;
if (isbleeding(lf)) {
if (rnd(1,2) == 1) {
bleed(lf);
}
}
f = lfhasflag(lf, F_PAIN);
if (f) {
if (isplayer(lf)) {
msg("Your body is wracked with pain!");
} else if (cansee(player, lf)) {
char lfname[BUFLEN];
getlfname(lf, lfname);
msg("%s convulses in pain!",lfname);
}
losehp(lf, roll(f->text), f->val[0], NULL, "extreme pain");
}
if (isdead(lf)) return;
}
// returns TRUE if something happened
int movelf(lifeform_t *lf, cell_t *newcell) {
object_t *o,*nexto;
char obname[BUFLEN],lfname[BUFLEN],buf[BUFLEN];
lifeform_t *l;
int didmsg = B_FALSE;
flag_t *f;
int changedlev = B_FALSE;
getlfname(lf, lfname);
if (newcell->map != lf->cell->map) {
changedlev = B_TRUE;
}
// update current cell
lf->cell->lf = NULL;
// if required, relink lifeform to new map
if (newcell->map != lf->cell->map) {
if (isplayer(lf)) {
statdirty = B_TRUE;
}
relinklf(lf, newcell->map);
}
// update lifeform
lf->cell = newcell;
// nothing should be in new cell..
assert(!newcell->lf);
// update new cell
newcell->lf = lf;
// update light
if (changedlev && isplayer(lf)) {
calclight(lf->cell->map);
}
moveeffects(lf);
// remove grabs
f = lfhasflag(lf, F_GRABBING);
if (f) {
lifeform_t *grabee;
grabee = findlf(NULL, f->val[0]);
assert(grabee);
killflagsofid(grabee->flags, F_GRABBEDBY);
killflagsofid(lf->flags, F_GRABBING);
}
// check ground objects
if (!isairborne(lf)) {
for (o = newcell->obpile->first ; o ; o = nexto ) {
nexto = o->next;
f = hasflag(o->flags, F_SHARP);
if (f && hasbp(lf, BP_FEET)) {
object_t *boots;
// has boots on?
boots = getequippedob(lf->pack, BP_FEET);
if (!boots) {
// take damage
getobname(o, obname, 1);
if (isplayer(lf)) {
msg("Ow - you step on %s!",obname);
didmsg = B_TRUE;
} else if (haslos(player, newcell)) {
msg("%s steps on %s!",lfname, obname);
didmsg = B_TRUE;
}
sprintf(buf, "stepping on %s", obname);
losehp(lf, rnd(f->val[0],f->val[1]), DT_SLASH, NULL, buf);
}
}
f = hasflag(o->flags, F_CRUSHABLE);
if (f) {
enum LFSIZE crushsize;
crushsize = f->val[0];
if (getlfsize(lf) >= crushsize) {
// crunch it broken glass
getobname(o, obname, 1);
// special case
if (o->type->id == OT_BROKENGLASS) {
if (o->amt > 1) {
char *newname;
// we want 'xx steps on some pieces of broken glass'
// not 'xx steps on 5 pieces of broken glass'
newname = makeplural(obname);
newname = strrep(newname, "a ", "some ", NULL);
strcpy(obname, newname);
free(newname);
}
}
if (isplayer(lf)) {
msg("You crush %s underfoot.",obname);
didmsg = B_TRUE;
} else if (haslos(player, newcell)) {
msg("%s crushes %s.",lfname, obname);
didmsg = B_TRUE;
}
// kill object
removeob(o, o->amt);
}
} // end if crushable
} // end foreach object in cell
} // end if !flying
// update where player knows
// (but without a map you will then slowly forget it)
if (isplayer(lf)) {
updateknowncells();
}
// does anyone else see you?
if ((gamemode == GM_GAMESTARTED)) {
for (l = newcell->map->lf ; l ; l = l->next) {
if (l != lf) {
if (haslos(l, newcell)) {
int dointerrupt = B_FALSE;
if (isplayer(l)) {
if (areenemies(lf, l)) {
if (lfhasflag(l, F_RUNNING) || lfhasflag(l, F_RESTING)) {
char lfname2[BUFLEN];
getlfname(lf, lfname);
sprintf(lfname2, "%s",noprefix(lfname));
msg("%s %s comes into view.",isvowel(lfname2[0]) ? "An" : "A", lfname2);
}
dointerrupt = B_TRUE;
}
} else {
if (isplayer(lf) && areallies(lf, l)) {
// remember player's last known loc
f = lfhasflag(l, F_PETOF);
if (f) {
f->val[1] = player->cell->x;
f->val[2] = player->cell->y;
}
}
dointerrupt = B_TRUE;
}
if (dointerrupt) {
interrupt(l);
}
}
}
}
}
return didmsg;
}
// basically this is a warpper for 'movelf' which
// does other game things like telling the player
// what is here.
int moveto(lifeform_t *lf, cell_t *newcell, int onpurpose) {
char lfname[BUFLEN];
int didmsg;
int dontclearmsg = B_FALSE;
int predark = B_FALSE,postdark = B_FALSE;
if (!onpurpose || !isplayer(lf)) {
dontclearmsg = B_TRUE;
}
assert(!newcell->lf);
getlfname(lf, lfname);
// is current cell dark?
if (isplayer(lf)) {
if (!haslos(lf, lf->cell) && !isblind(lf)) {
predark = B_TRUE;
}
}
// actually do the move
didmsg = movelf(lf, newcell);
if (isplayer(lf)) {
// is new cell dark?
if (!haslos(lf, lf->cell) && !isblind(lf)) {
postdark = B_TRUE;
} else {
killflagsofid(lf->flags, F_DONEDARKMSG);
}
// just moved into a dark area - announce it.
if (postdark && !predark && !lfhasflag(lf, F_DONEDARKMSG)) {
msg("It is pitch black!");
addflag(lf->flags, F_DONEDARKMSG, B_TRUE, NA, NA, NULL);
dontclearmsg = B_TRUE;
}
}
if (dontclearmsg) {
didmsg = B_TRUE;
}
// tell player about things
if (!isdead(lf)) {
// some lifeforms can go through things
if (lf->race->material->id == MT_GAS) {
object_t *o;
char obname[BUFLEN];
for (o = newcell->obpile->first ; o ; o = o->next) {
if (isimpassableob(o, lf)) {
getobname(o, obname, o->amt);
if (isplayer(lf)) {
msg("You seep around %s.", obname);
} else if (haslos(player, newcell)) {
msg("%s seeps around %s.", lfname, obname);
}
}
}
} else if (lf->race->material->id == MT_SLIME) {
object_t *o;
char obname[BUFLEN];
for (o = newcell->obpile->first ; o ; o = o->next) {
if (isimpassableob(o, lf)) {
getobname(o, obname, o->amt);
if (isplayer(lf)) {
msg("You seep under %s.", obname);
} else if (haslos(player, newcell)) {
msg("%s seeps under %s.", lfname, obname);
}
}
}
}
// see objects on ground
if (isplayer(lf)) {
int numobs;
numobs = countobs(newcell->obpile);
if ((numobs == 0) && !newcell->writing) {
// just clear the message buffer
if (!didmsg) clearmsg();
} else { // tell player what is here
dolook(newcell);
}
}
}
// make some noise
// (stealth check to avoid this)
if (!lfhasflag(lf, F_SILENTMOVE)) {
if (!skillcheck(lf, SC_STEALTH, 20, 0)) {
if (isairborne(lf)) {
makenoise(lf, N_FLY);
} else {
makenoise(lf, N_WALK);
}
}
}
// slip on blood in new cell?
if (!isairborne(lf)) {
int slip;
object_t *slipob;
slip = getslipperyness(newcell, &slipob);
if (slip && !skillcheck(lf, SC_SLIP, slip, 0)) {
slipon(lf, slipob);
}
}
return B_FALSE;
}
// dirtype etermines whether to use compass or orthogonal direction to
// measure distance when determining which way is "towards"
//
// in general:
// use orthogonal for voluntary movement (eg. monster moving
// towards player), compass
//
// use compass for involuntary movement (eg. being knocked back by
// an explosion)
//
int movetowards(lifeform_t *lf, cell_t *dst, int dirtype) {
int dir;
int rv = B_TRUE;
if (isblind(lf)) {
dorandommove(lf, B_TRUE);
return B_FALSE;
}
// move towards them
dir = getdirtowards(lf->cell, dst, lf, B_TRUE, dirtype);
if (dir != D_NONE) {
rv = trymove(lf, dir, B_TRUE);
}
return rv;
}
int opendoorat(lifeform_t *lf, cell_t *c) {
object_t *o;
int rv;
o = hasobwithflag(c->obpile, F_DOOR);
if (o) {
rv = opendoor(lf, o);
} else {
rv = B_TRUE;
}
return rv;
}
int opendoor(lifeform_t *lf, object_t *o) {
cell_t *doorcell;
char buf[BUFLEN];
char obname[BUFLEN];
flag_t *f;
doorcell = o->pile->where;
assert(doorcell);
getobname(o, obname, 1);
if (!isdoor(o, NULL)) {
return B_TRUE;
}
if (lf && lfhasflagval(lf, F_NOBODYPART, BP_HANDS, NA, NA, NULL)) {
if (isplayer(lf)) {
msg("You have no hands with which to open the door!");
}
return B_TRUE;
}
f = hasflag(o->flags, F_OPEN);
if (f) {
if (lf && isplayer(lf)) {
msg("It is already open!");
}
return B_TRUE;
} else {
if (lf) {
taketime(lf, getactspeed(lf));
touch(lf, o);
}
// locked?
if (hasflag(o->flags, F_LOCKED)) {
if (lf && isplayer(lf)) {
msg("The door is locked.");
}
return B_TRUE;
} else {
int openit = B_TRUE;
f = hasflag(o->flags, F_JAMMED);
if (f) {
f->val[0] --;
// loosen a bit
if (lf && isplayer(lf)) {
msg("You yank on the door but it holds fast.");
}
if (f->val[0] <= 0) {
killflag(f);
}
openit = B_FALSE; // don't open the door
}
if (openit) {
cell_t *where;
// open it
addflag(o->flags, F_OPEN, B_TRUE, NA, NA, NULL);
f = hasflag(o->flags, F_IMPASSABLE);
if (f) killflag(f);
f = hasflag(o->flags, F_BLOCKSVIEW);
if (f) killflag(f);
if (lf) {
if (isplayer(lf)) {
msg("You open %s.",obname);
} else {
if (cansee(player, lf) && isadjacent(lf->cell, doorcell)) {
getlfname(lf, buf);
capitalise(buf);
msg("%s opens %s.",buf, obname);
} else if (haslos(player, doorcell)) {
capitalise(obname);
msg("%s opens.",obname);
}
}
}
where = getoblocation(o);
if (!haslos(player, where)) {
// don't anonuce this is we can see it.
// normally 'noise()' takes case of this by
// checking if we have LOS to the lifeform making
// sound, but in this case it's the door making
// the sound, not the lf.
noise(where, NULL, "a door opening.", NULL);
}
if (player && haslos(player, where)) {
needredraw = B_TRUE;
drawscreen();
}
}
return B_FALSE;
}
}
return B_FALSE;
}
int closedoorat(lifeform_t *lf, cell_t *c) {
object_t *o;
int rv;
o = hasobwithflag(c->obpile, F_DOOR);
if (o) {
rv = closedoor(lf, o);
} else {
rv = B_TRUE;
}
return rv;
}
int closedoor(lifeform_t *lf, object_t *o) {
cell_t *cell;
char buf[BUFLEN];
char obname[BUFLEN];
object_t *oo;
flag_t *f;
cell = getoblocation(o);
getobname(o, obname, 1);
if (!isdoor(o, NULL)) {
if (isplayer(lf)) {
msg("There is nothing to close there!");
}
return B_TRUE;
}
if (lf && lfhasflagval(lf, F_NOBODYPART, BP_HANDS, NA, NA, NULL)) {
if (isplayer(lf)) {
msg("You have no hands with which to close the door!");
}
return B_TRUE;
}
if (cell->lf) {
if (lf && isplayer(lf)) {
char inwayname[BUFLEN];
getlfname(cell->lf, inwayname);
msg("%s is in the way!", haslos(lf, cell) ? inwayname : "Something");
}
return B_TRUE;
}
// any solid object other than the door?
for (oo = cell->obpile->first ; oo ; oo = oo->next) {
if ((oo != o) && (getmaterialstate(oo->material->id) == MS_SOLID)) {
if (lf && isplayer(lf)) {
char inwayname[BUFLEN];
getobname(oo, inwayname, oo->amt);
msg("%s %s in the way!", haslos(lf, cell) ? inwayname : "Something",
(haslos(lf,cell) && (oo->amt > 1)) ? "are" : "is" );
}
return B_TRUE;
}
}
f = hasflag(o->flags, F_OPEN);
if (!f) {
if (lf && (isplayer(lf))) {
msg("It is already closed!");
}
return B_TRUE;
} else {
// close it
killflag(f);
addflag(o->flags, F_IMPASSABLE, B_TRUE, NA, NA, NULL);
addflag(o->flags, F_BLOCKSVIEW, B_TRUE, NA, NA, NULL);
if (lf) {
if (isplayer(lf)) {
msg("You close %s.", obname);
} else {
if (cansee(player, lf) && isadjacent(lf->cell, cell)) {
getlfname(lf, buf);
capitalise(buf);
msg("%s closes %s.",buf, obname);
} else if (haslos(player, cell)) {
capitalise(obname);
msg("%s closes.",obname);
}
}
taketime(lf, getactspeed(lf));
touch(lf, o);
}
}
return B_FALSE;
}
int tryrun(lifeform_t *lf, int dir) {
if (!trymove(lf, dir, B_TRUE)) {
addflag(lf->flags, F_RUNNING, dir, NA, NA, NULL);
}
return B_FALSE;
}
// try to pull lifeform towards cell c (or next to it)
int pullnextto(lifeform_t *lf, cell_t *c) {
int dir;
cell_t *dst = NULL;
cell_t *newdst = NULL;
dst = c;
while (dst->lf) {
dir = getdirtowards(dst, lf->cell, lf, B_FALSE, DT_COMPASS);
if (dir == D_NONE) {
return B_TRUE;
} else {
dst = getcellindir(dst, dir);
if (dst == lf->cell) {
return B_TRUE;
}
}
}
// is the path clear?
if (!dst) {
return B_TRUE;
}
if (!haslof(lf->cell, dst, B_FALSE, &newdst)) {
if (newdst) {
// update destination
dst = newdst;
} else {
return B_TRUE;
}
}
if (isplayer(lf) || cansee(player, lf)) {
char buf[BUFLEN];
getlfname(lf, buf);
msg("%s %s pulled %s!", buf, isplayer(lf) ? "are" : "is",
isairborne(lf) ? "through the air" :
"along the ground");
}
movelf(lf, dst);
return B_FALSE;
}
// teleport somewhere, along with puffs of smoke etc
int teleportto(lifeform_t *lf, cell_t *c, int wantsmoke) {
char buf[BUFLEN];
// can't teleport on top of something else
if (c->lf) {
if (isplayer(lf)) {
msg("You feel a wrenching sensation.");
}
return B_TRUE;
}
if (!isplayer(lf) && cansee(player, lf)) {
getlfname(lf, buf);
if (wantsmoke) {
msg("%s disappears in a cloud of smoke!", buf);
} else {
msg("%s vanishes!", buf);
}
}
if (wantsmoke) {
addob(lf->cell->obpile, "cloud of smoke");
}
movelf(lf, c);
// addob(lf->cell->obpile, "cloud of smoke");
if (cansee(player, lf)) {
redraw(); // redraw screen
}
if (isplayer(lf)) {
msg("Suddenly, your surroundings appear different!");
} else if (cansee(player, lf)) {
getlfname(lf, buf);
msg("%s appears!", buf);
}
// show any objects here, just like if we moved.
// BUT don't let dolook() clear the msg bar if there are
// no objects here.
if (isplayer(lf) && countobs(lf->cell->obpile)) {
dolook(lf->cell);
}
return B_FALSE;
}
int trymove(lifeform_t *lf, int dir, int onpurpose) {
cell_t *cell;
enum ERROR errcode;
char buf[BUFLEN];
cell = getcellindir(lf->cell, dir);
if (canandwillmove(lf, dir, &errcode)) {
object_t *o,*slipob;
int slip;
// check for cursed objects + animals
// do this AFTER checking if we will move, so that
// they will actually try the move and fail (this lets
// the player find out about the cursed object).
//
// note however that if a monster is chasing a player (ie
// has F_TARGET,player) then they will simply avoid the cursed
// object rather than failing the movement.
for (o = cell->obpile->first ; o ; o = o->next) {
if (!isplayer(lf)) {
if (o->blessed == B_CURSED) {
if (lfhasflag(lf, F_ANIMAL)) {
if (!isairborne(lf)) {
if (cansee(player, lf)) {
char lfname[BUFLEN];
getlfname(lf,lfname);
getobname(o, buf, o->amt);
msg("%s %s away from %s!", lfname, isplayer(lf) ? "shy" : "shies", buf);
o->blessknown = B_TRUE;
}
taketime(lf, getmovespeed(lf));
reason = E_OK;
// avoid this object in future
sprintf(buf, "%ld",o->id);
addflag(lf->flags, F_AVOIDCURSEDOB, NA, NA, NA, buf);
return B_FALSE;
}
}
}
}
}
// slipping on blood before moving?
if (!isairborne(lf)) {
slip = getslipperyness(lf->cell, &slipob);
if (slip && !skillcheck(lf, SC_SLIP, slip, 0)) {
slipon(lf, slipob);
// don't move
reason = E_OK;
return B_FALSE;
}
}
// gravboosted
if (lfhasflag(lf, F_GRAVBOOSTED)) {
// make a saving throw to move
if (!skillcheck(lf, SC_STR, 25, 0)) {
if (isplayer(lf)) {
msg("You try to move but are unable to lift your feet!");
} else if (cansee(player, lf)) {
char lfname[BUFLEN];
getlfname(lf, lfname);
msg("%s tries to move but is unable to lift its feet!",lfname);
}
reason = E_OK;
taketime(lf, getmovespeed(lf));
return B_FALSE;
}
}
// move to new cell
reason = E_OK;
moveto(lf, cell, onpurpose);
taketime(lf, getmovespeed(lf));
} else {
object_t *inway = NULL;
int door, dooropen;
reason = errcode;
switch (errcode) {
case E_OFFMAP:
if (isplayer(lf)) {
msg("You can't go any further in that direction.");
} else if (cansee(player, lf)) {
getlfname(lf, buf);
msg("%s %ss around randomly.", buf, getmoveverb(lf));
}
break;
case E_WALLINWAY:
if (isplayer(lf)) {
msg("Ouch! You %s into a wall.", getmoveverb(lf));
} else if (cansee(player, lf)) {
getlfname(lf, buf);
msg("%s %ss into a wall.", buf, getmoveverb(lf));
}
//if (isblind(lf) || !haslos(lf, cell)) {
if (!haslos(lf, cell)) {
if (isplayer(lf)) {
// only take damage if we didn't know about this
if (!cell->known) {
sprintf(buf, "%sing into a wall", getmoveverb(lf));
losehp(lf, 1, DT_BASH, NULL, buf);
// we now know there is a wall there.
cell->known = B_TRUE;
taketime(lf, getmovespeed(lf));
}
} else {
sprintf(buf, "%sing into a wall", getmoveverb(lf));
losehp(lf, 1, DT_BASH, NULL, buf);
taketime(lf, getmovespeed(lf));
}
}
break;
case E_DOORINWAY:
inway = (object_t *)rdata;
door = isdoor(inway, &dooropen);
if (door && !dooropen) {
if (isblind(lf)) {
// run into it
if (isplayer(lf)) {
if (cell->known) {
// try to open it
opendoor(lf, inway);
} else {
msg("Ouch! You %s into a door.", getmoveverb(lf));
cell->known = B_TRUE;
sprintf(buf, "%sing into a door", getmoveverb(lf));
losehp(lf, 1, DT_BASH, NULL, buf);
taketime(lf, getmovespeed(lf));
}
} else {
if (cansee(player, lf)) {
getlfname(lf, buf);
msg("%s %ss into a wall.", buf, getmoveverb(lf));
}
sprintf(buf, "%sing into a door", getmoveverb(lf));
losehp(lf, 1, DT_BASH, NULL, buf);
taketime(lf, getmovespeed(lf));
}
} else {
// try to open it
if (!opendoor(lf, inway)) {
// opening a door counts as a successful move.
reason = E_OK;
}
}
}
break;
case E_OBINWAY:
inway = (object_t *)rdata;
// can we push this?
if (ispushable(inway)) {
if (canpush(lf, inway, dir)) {
// push it!
push(lf, inway, dir);
} else {
if (isplayer(lf)) {
char obname[BUFLEN];
getobname(inway, obname, 1);
switch (reason) {
case E_TOOHEAVY:
msg("The %s is too heavy to move.",strchr(obname, ' ')+1);
break;
case E_INSUBSTANTIAL:
msg("You cannot push %s without a physical body!",strchr(obname, ' ')+1);
break;
default:
msg("The %s won't move for some reason.",strchr(obname, ' ')+1);
break;
}
}
}
} else { // somethign random is in the way
if (isplayer(lf) && inway) {
char obname[BUFLEN];
getobname(inway, obname, 1);
msg("There is %s in your way.",obname);
}
}
break;
case E_LFINWAY:
if (canswapwith(lf, cell->lf)) {
lifeform_t *lfinway;
cell_t *oldcell;
// otherwise swap locations.
// make lf who is there vanish temporarily...
lfinway = cell->lf;
cell->lf = NULL;
lfinway->cell = NULL;
// remember your cell
oldcell = lf->cell;
// move you..
moveto(lf, cell, onpurpose);
taketime(lf, getmovespeed(lf));
// move them...
lfinway->cell = oldcell;
oldcell->lf = lfinway;
reason = E_OK;
if (isplayer(lf)) {
char lfname[BUFLEN];
getlfname(lfinway, lfname);
msg("You swap places with %s.", lfname);
}
} else {
// attack!
return attackcell(lf, cell);
}
break;
case E_CANTMOVE:
if (isplayer(lf)) {
msg("You cannot move!");
}
taketime(lf, getmovespeed(lf));
break;
case E_GRABBEDBY:
if (rdata) {
lifeform_t *grabbedby;
char gbname[BUFLEN];
// skill check to escape.
grabbedby = (lifeform_t *)rdata;
getlfname(grabbedby, gbname);
if (!cell->lf && skillcheckvs(lf, SC_STR, 0, grabbedby, SC_STR, 0)) {
// broke free
killflagsofid(lf->flags, F_GRABBEDBY);
killflagsofid(grabbedby->flags, F_GRABBING);
// move - don't clear the 'you break free from' msg
moveto(lf, cell, B_TRUE);
} else {
if (isplayer(lf)) {
msg("You cannot get away from %s!",gbname);
}
}
} else {
if (isplayer(lf)) {
msg("You cannot get away from whatever is holding you!");
}
}
taketime(lf, getmovespeed(lf));
break;
case E_TOOHEAVY:
if (isplayer(lf)) {
msg("Your load is too heavy to move with!");
}
taketime(lf, getmovespeed(lf));
break;
default:
break;
}
if (reason == E_OK) {
return B_FALSE;
} else {
return B_TRUE;
}
}
return B_FALSE;
}
int willmove(lifeform_t *lf, int dir, enum ERROR *error) {
cell_t *cell;
enum IQBRACKET iq;
object_t *o;
char buf[BUFLEN];
//object_t *o;
iq = getiqname(getattr(lf, A_IQ), NULL);
// default
if (error) {
*error = E_OK;
rdata = NULL;
}
cell = getcellindir(lf->cell, dir);
if (celldangerous(lf, cell, B_TRUE, error)) {
if (error) *error = E_WONT;
return B_FALSE;
}
// don't attack other monsters
if (cell->lf) { // if someone is in the way
if (!isplayer(lf)) { // if we are a monster
// if the person in the way isn't our enemy...
if (!areenemies(lf, cell->lf)) {
// if they are an ally...
if (canswapwith(lf, cell->lf)) {
return B_TRUE;
}
if (error) *error = E_WONT;
return B_FALSE;
}
}
}
// for intelligent things...
if (iq >= IQ_AVERAGE) {
// don't move if in pain
if (lfhasflag(lf, F_PAIN)) {
if (error) *error = E_WONT;
return B_FALSE;
}
}
// look for avoided objects (because they are cursed).
for (o = cell->obpile->first ; o ; o = o->next) {
flag_t *f;
sprintf(buf, "%ld",o->id);
f = lfhasflagval(lf, F_AVOIDCURSEDOB, NA, NA, NA, buf);
if (f) {
// still cursed?
if (iscursed(o)) {
if (error) *error = E_WONT;
return B_FALSE;
} else {
// remove the flag.
killflag(f);
}
}
}
return B_TRUE;
}