attempt to move it to an adjacent square before doing so. If there are no appropriate adjacent squares then try to make it stay submerged. If it can't stay submerged because the terrain changed then teleport it away.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@5738 c06c8d41-db1a-0410-9941-cceddc491573
AV3TMWHWB3XBXQCT34UPMZBSIIKVXIGWQPNEFU4CZSBS3ZOF2CUQC
WDQ3ST3BQ7BMW2IXQMSJVRG7GK2EG444JLKII24FB7IJ5YF5KT5QC
D7SLVLRNCYCBDYYRANHDG3JYEF25CFCSUY5FMF5KXVD5D4UZSDDAC
OONYLF4DAPLIYLBNNRW74IVT5BBTWI4XHQBXSNSPVRX3FTKJBTRAC
QNIQ2NBBIERVCA2YTD3O3P6QPJ5M6VDVGGX7V2BWXTW5553T4PVAC
K2CS6TCX2NDVL2ASEHGP4J4K4IJ6FP3ANNKTSIWVG43HPYSBX6ZQC
SVY2PTCLXR3KNPQAWXVXTTGCC5DR334HOAKHYO3VDDRWM2BWMALAC
RM2JXW3ATVYRYHF3NMG5ALGI64OJ7IP2F3MDUDPUT5TBKSSN4KVQC
3HGELZU7NELOQ635HZO6IJIYLBSNCJ5VPH46IE22KA3OSLEFK7AQC
OSGS3PH2L5CBTDVZCZS6OCFQNA4A7RMEXBYJQB7DDZBYYJW7QSSAC
7AMQN7MITMXBNVDAK5VOXTQ4TZIAOD6ZLOFJG7GQMBTY23Y2BKSAC
X5WLJCJVW55SXZVP7IKP7ADCJIGNKN4PKAXFECVR6TNK7XSMZR7QC
NWJ5IHZJXYE4I7CKSDNMTLGTIKOWK42LC2UYZL2TLXSZLM67WKRAC
5XSXMOBGXFLTIQE6WDXWWFVDOTUPZSIQ2FWT3YI5QMVU6D76IUYQC
GTXKQTORYHZ7XB2VIH6372UM5GMWAN7IVRXWY5FGBCHFGBV6D6NAC
{
// If the monster can't stay submerged in the new terrain
// and there aren't any adjacent squares where it can
// stay submerged then move it.
if (mgrd(you.pos()) != NON_MONSTER
&& !mons_is_submerged( &menv[ mgrd(you.pos()) ] ))
{
monster_teleport( &menv[ mgrd(you.pos()) ], true, false);
}
// Don't unsubmerge if the player is right on top,
// if the monster is too damaged or if the monster
// is afraid.
if (monster->pos() == you.pos()
|| monster->hit_points <= monster->max_hit_points / 2
// Don't unsubmerge if the monster is too damaged or
// if the monster is afraid.
if (monster->hit_points <= monster->max_hit_points / 2
static bool _prepare_del_ench(monsters* mons, const mon_enchant &me)
{
if (me.ench != ENCH_SUBMERGED)
return true;
if (mons->pos() != you.pos())
return true;
// Monster un-submerging while under player. Try to move to an
// adjacent square in which the monster could have been submerged
// and have it unusbmerge from there.
coord_def pos, target_square;
int okay_squares = 0;
for (pos.x = you.x_pos - 1; pos.x <= you.x_pos + 1; pos.x++)
for (pos.y = you.y_pos - 1; pos.y <= you.y_pos + 1; pos.y++)
{
if (pos == you.pos())
continue;
if (in_bounds(pos) && mgrd(pos) == NON_MONSTER
&& monster_can_submerge(mons, grd(pos)))
{
if (one_chance_in(++okay_squares))
target_square = pos;
}
}
if (okay_squares > 0)
{
int mnum = mgrd(mons->pos());
mgrd(mons->pos()) = NON_MONSTER;
mgrd(target_square) = mnum;
mons->x = target_square.x;
mons->y = target_square.y;
return true;
}
// No available adjacent squares from which the monster could also
// have unsubmerged. Can it just stay submerged where it is?
if ( monster_can_submerge(mons, grd( mons->pos() )) )
return false;
// The terrain changed and the monster can't remain submerged.
// Try to move to an adjacent square where it would be happy.
for (pos.x = you.x_pos - 1; pos.x <= you.x_pos + 1; pos.x++)
for (pos.y = you.y_pos - 1; pos.y <= you.y_pos + 1; pos.y++)
{
if (pos == you.pos())
continue;
if (in_bounds(pos) && mgrd(pos) == NON_MONSTER
&& monster_habitable_grid(mons, grd(pos))
&& trap_type_at_xy(pos.x, pos.y) == NUM_TRAPS)
{
if (one_chance_in(++okay_squares))
target_square = pos;
}
}
if (okay_squares > 0)
{
int mnum = mgrd(mons->pos());
mgrd(mons->pos()) = NON_MONSTER;
mgrd(target_square) = mnum;
mons->x = target_square.x;
mons->y = target_square.y;
}
return ( true );
}