Skip to content

Commit

Permalink
Merge branch 'main' into fix/lava_lurker
Browse files Browse the repository at this point in the history
  • Loading branch information
pess0a authored Dec 14, 2024
2 parents 2ce6b29 + 05ff8be commit ebf679a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function rookVillage.onStepIn(creature, item, position, fromPosition)
return true
end

player:teleportTo(Position(player:getPosition().x, player:getPosition().y - 1, player:getPosition().z))
player:teleportTo(Position(player:getPosition().x, player:getPosition().y - 3, player:getPosition().z + 1))
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You don't have any business there anymore.")

return true
Expand Down
22 changes: 17 additions & 5 deletions data/libs/systems/zones.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ function Zone:randomPosition()
logger.error("Zone:randomPosition() - Zone {} has no positions", self:getName())
return nil
end
local destination = positions[math.random(1, #positions)]
local tile = destination:getTile()
while not tile or not tile:isWalkable(false, false, false, false, true) do
destination = positions[math.random(1, #positions)]
tile = destination:getTile()

local validPositions = {}
for _, position in ipairs(positions) do
local tile = position:getTile()
if tile and tile:isWalkable(false, false, false, false, true) then
table.insert(validPositions, position)
else
logger.debug("Zone:randomPosition() - Position {} is invalid (Tile: {}, Walkable: {})", position, tile or "nil", tile and tile:isWalkable(false, false, false, false, true) or "false")
end
end

if #validPositions == 0 then
logger.error("Zone:randomPosition() - No valid positions in Zone {}", self:getName())
return nil
end

local destination = validPositions[math.random(1, #validPositions)]
logger.debug("Zone:randomPosition() - Selected valid position: {}", destination)
return destination
end

Expand Down
57 changes: 14 additions & 43 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,73 +1581,44 @@ bool Monster::getDanceStep(const Position &creaturePos, Direction &moveDirection
uint32_t centerToDist = std::max<uint32_t>(distance_x, distance_y);

// monsters not at targetDistance shouldn't dancestep
if (centerToDist < (uint32_t)targetDistance) {
if (centerToDist < static_cast<uint32_t>(targetDistance)) {
return false;
}

std::vector<Direction> dirList;
if (!keepDistance || offset_y >= 0) {
uint32_t tmpDist = std::max<uint32_t>(distance_x, std::abs((creaturePos.getY() - 1) - centerPos.getY()));
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_NORTH)) {
auto tryAddDirection = [&](Direction direction, int_fast32_t newX, int_fast32_t newY) {
uint32_t tmpDist = std::max<uint32_t>(std::abs(newX - centerPos.getX()), std::abs(newY - centerPos.getY()));
if (tmpDist == centerToDist && canWalkTo(creaturePos, direction)) {
bool result = true;

if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x, creaturePos.y - 1, creaturePos.z), attackedCreature));
result = (!canDoAttackNow || canUseAttack(Position(newX, newY, creaturePos.z), attackedCreature));
}

if (result) {
dirList.push_back(DIRECTION_NORTH);
dirList.emplace_back(direction);
}
}
};

if (!keepDistance || offset_y >= 0) {
tryAddDirection(DIRECTION_NORTH, creaturePos.getX(), creaturePos.getY() - 1);
}

if (!keepDistance || offset_y <= 0) {
uint32_t tmpDist = std::max<uint32_t>(distance_x, std::abs((creaturePos.getY() + 1) - centerPos.getY()));
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_SOUTH)) {
bool result = true;

if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x, creaturePos.y + 1, creaturePos.z), attackedCreature));
}

if (result) {
dirList.push_back(DIRECTION_SOUTH);
}
}
tryAddDirection(DIRECTION_SOUTH, creaturePos.getX(), creaturePos.getY() + 1);
}

if (!keepDistance || offset_x <= 0) {
uint32_t tmpDist = std::max<uint32_t>(std::abs((creaturePos.getX() + 1) - centerPos.getX()), distance_y);
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_EAST)) {
bool result = true;

if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x + 1, creaturePos.y, creaturePos.z), attackedCreature));
}

if (result) {
dirList.push_back(DIRECTION_EAST);
}
}
tryAddDirection(DIRECTION_EAST, creaturePos.getX() + 1, creaturePos.getY());
}

if (!keepDistance || offset_x >= 0) {
uint32_t tmpDist = std::max<uint32_t>(std::abs((creaturePos.getX() - 1) - centerPos.getX()), distance_y);
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_WEST)) {
bool result = true;

if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x - 1, creaturePos.y, creaturePos.z), attackedCreature));
}

if (result) {
dirList.push_back(DIRECTION_WEST);
}
}
tryAddDirection(DIRECTION_WEST, creaturePos.getX() - 1, creaturePos.getY());
}

if (!dirList.empty()) {
std::shuffle(dirList.begin(), dirList.end(), getRandomGenerator());
std::ranges::shuffle(dirList, getRandomGenerator());
moveDirection = dirList[uniform_random(0, dirList.size() - 1)];
return true;
}
Expand Down

0 comments on commit ebf679a

Please sign in to comment.