bool Monster::getDistanceStep(const Position& targetPos, Direction& dir, bool flee /* = false */)
{
const Position& creaturePos = getPosition();
int_fast32_t dx = Position::getDistanceX(creaturePos, targetPos);
int_fast32_t dy = Position::getDistanceY(creaturePos, targetPos);
int32_t distance = std::max<int32_t>(dx, dy);
if (!flee && (distance > mType->targetDistance || !g_game.isSightClear(creaturePos, targetPos, true))) {
return false; // let the A* calculate it
} else if (!flee && distance == mType->targetDistance) {
return true; // we don't really care here, since it's what we wanted to reach (a dancestep will take of dancing in that position)
}
int_fast32_t offsetx = Position::getOffsetX(creaturePos, targetPos);
int_fast32_t offsety = Position::getOffsetY(creaturePos, targetPos);
if (dx <= 1 && dy <= 1) {
//seems like a target is near, it this case we need to slow down our movements (as a monster)
if (stepDuration < 2) {
stepDuration++;
}
} else if (stepDuration > 0) {
stepDuration--;
}
if (offsetx == 0 && offsety == 0) {
return getRandomStep(creaturePos, dir); // player is "on" the monster so let's get some random step and rest will be taken care later.
}
if (dx == dy) {
//player is diagonal to the monster
if (offsetx >= 1 && offsety >= 1) {
// player is NW
//escape to SE, S or E [and some extra]
bool s = canWalkTo(creaturePos, SOUTH);
bool e = canWalkTo(creaturePos, EAST);
if (s && e) {
dir = boolean_random() ? SOUTH : EAST;
return true;
} else if (s) {
dir = SOUTH;
return true;
} else if (e) {
dir = EAST;
return true;
} else if (canWalkTo(creaturePos, SOUTHEAST)) {
dir = SOUTHEAST;
return true;
}
/* fleeing */
bool n = canWalkTo(creaturePos, NORTH);
bool w = canWalkTo(creaturePos, WEST);
if (flee) {
if (n && w) {
dir = boolean_random() ? NORTH : WEST;
return true;
} else if (n) {
dir = NORTH;
return true;
} else if (w) {
dir = WEST;
return true;
}
}
/* end of fleeing */
if (w && canWalkTo(creaturePos, SOUTHWEST)) {
dir = WEST;
} else if (n && canWalkTo(creaturePos, NORTHEAST)) {
dir = NORTH;
}
return true;
} else if (offsetx <= -1 && offsety <= -1) {
//player is SE
//escape to NW , W or N [and some extra]
bool w = canWalkTo(creaturePos, WEST);
bool n = canWalkTo(creaturePos, NORTH);
if (w && n) {
dir = boolean_random() ? WEST : NORTH;
return true;
} else if (w) {
dir = WEST;
return true;
} else if (n) {
dir = NORTH;
return true;
}
if (canWalkTo(creaturePos, NORTHWEST)) {
dir = NORTHWEST;
return true;
}
/* fleeing */
bool s = canWalkTo(creaturePos, SOUTH);
bool e = canWalkTo(creaturePos, EAST);
if (flee) {
if (s && e) {
dir = boolean_random() ? SOUTH : EAST;
return true;
} else if (s) {
dir = SOUTH;
return true;
} else if (e) {
dir = EAST;
return true;
}
}
/* end of fleeing */
if (s && canWalkTo(creaturePos, SOUTHWEST)) {
dir = SOUTH;
} else if (e && canWalkTo(creaturePos, NORTHEAST)) {
dir = EAST;
}
return true;
} else if (offsetx >= 1 && offsety <= -1) {
//player is SW
//escape to NE, N, E [and some extra]
bool n = canWalkTo(creaturePos, NORTH);
bool e = canWalkTo(creaturePos, EAST);
if (n && e) {
dir = boolean_random() ? NORTH : EAST;
return true;
} else if (n) {
dir = NORTH;
return true;
} else if (e) {
dir = EAST;
return true;
}
if (canWalkTo(creaturePos, NORTHEAST)) {
dir = NORTHEAST;
return true;
}
/* fleeing */
bool s = canWalkTo(creaturePos, SOUTH);
bool w = canWalkTo(creaturePos, WEST);
if (flee) {
if (s && w) {
dir = boolean_random() ? SOUTH : WEST;
return true;
} else if (s) {
dir = SOUTH;
return true;
} else if (w) {
dir = WEST;
return true;
}
}
/* end of fleeing */
if (w && canWalkTo(creaturePos, NORTHWEST)) {
dir = WEST;
} else if (s && canWalkTo(creaturePos, SOUTHEAST)) {
dir = SOUTH;
}
return true;
} else if (offsetx <= -1 && offsety >= 1) {
// player is NE
//escape to SW, S, W [and some extra]
bool w = canWalkTo(creaturePos, WEST);
bool s = canWalkTo(creaturePos, SOUTH);
if (w && s) {
dir = boolean_random() ? WEST : SOUTH;
return true;
} else if (w) {
dir = WEST;
return true;
} else if (s) {
dir = SOUTH;
return true;
} else if (canWalkTo(creaturePos, SOUTHWEST)) {
dir = SOUTHWEST;
return true;
}
/* fleeing */
bool n = canWalkTo(creaturePos, NORTH);
bool e = canWalkTo(creaturePos, EAST);
if (flee) {
if (n && e) {
dir = boolean_random() ? NORTH : EAST;
return true;
} else if (n) {
dir = NORTH;
return true;
} else if (e) {
dir = EAST;
return true;
}
}
/* end of fleeing */
if (e && canWalkTo(creaturePos, SOUTHEAST)) {
dir = EAST;
} else if (n && canWalkTo(creaturePos, NORTHWEST)) {
dir = NORTH;
}
return true;
}
}
//Now let's decide where the player is located to the monster (what direction) so we can decide where to escape
>>here was code what simply checked player posittions<<
return true;
}