Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,40 +1413,63 @@ void Monster::onThinkSound(uint32_t interval) {
}
}

bool Monster::pushItem(const std::shared_ptr<Item> &item, const Direction &nextDirection) {
const Position &centerPos = item->getPosition();
for (const auto &[x, y] : getPushItemLocationOptions(nextDirection)) {
Position tryPos(centerPos.x + x, centerPos.y + y, centerPos.z);
std::shared_ptr<Tile> tile = g_game().map.getTile(tryPos);
if (tile && g_game().canThrowObjectTo(centerPos, tryPos) && g_game().internalMoveItem(item->getParent(), tile, INDEX_WHEREEVER, item, item->getItemCount(), nullptr) == RETURNVALUE_NOERROR) {
bool Monster::pushItem(const std::shared_ptr<Item> &item, const Direction &dir) {
if (!item) {
return false;
}

auto fromTile = item->getTile();
if (!fromTile) {
return false;
}

const Position &fromPos = fromTile->getPosition();
std::shared_ptr<Cylinder> fromCyl = fromTile;

for (auto [dx, dy] : getPushItemLocationOptions(dir)) {
Position toPos(fromPos.x + dx, fromPos.y + dy, fromPos.z);
auto toTile = g_game().map.getTile(toPos);

if (toTile && g_game().canThrowObjectTo(fromPos, toPos) && g_game().internalMoveItem(fromCyl, toTile, INDEX_WHEREEVER, item, item->getItemCount(), nullptr) == RETURNVALUE_NOERROR) {
return true;
}
}
return false;
}

void Monster::pushItems(const std::shared_ptr<Tile> &tile, const Direction &nextDirection) {
// We can not use iterators here since we can push the item to another tile
// which will invalidate the iterator.
// start from the end to minimize the amount of traffic
if (const auto &items = tile->getItemList()) {
uint32_t moveCount = 0;
uint32_t removeCount = 0;
int32_t downItemSize = tile->getDownItemCount();
for (int32_t i = downItemSize; --i >= 0;) {
const auto &item = items->at(i);
if (item && item->hasProperty(CONST_PROP_MOVABLE) && (item->hasProperty(CONST_PROP_BLOCKPATH) || item->hasProperty(CONST_PROP_BLOCKSOLID)) && item->canBeMoved()) {
if (moveCount < 20 && pushItem(item, nextDirection)) {
++moveCount;
} else if (!item->isCorpse() && g_game().internalRemoveItem(item) == RETURNVALUE_NOERROR) {
++removeCount;
}
}
if (!tile) {
return;
}

const auto* items = tile->getItemList();
if (!items || items->empty()) {
return;
}

uint32_t moveCount = 0;
uint32_t removeCount = 0;
int32_t downItemSize = tile->getDownItemCount();

for (int32_t i = downItemSize; --i >= 0;) {
const auto &item = items->at(i);
if (!item || !item->hasProperty(CONST_PROP_MOVABLE) || !item->canBeMoved()) {
continue;
}
if (removeCount > 0) {
g_game().addMagicEffect(tile->getPosition(), CONST_ME_POFF);
if (!item->hasProperty(CONST_PROP_BLOCKPATH) && !item->hasProperty(CONST_PROP_BLOCKSOLID)) {
continue;
}

if (moveCount < 20 && pushItem(item, nextDirection)) {
++moveCount;
} else if (removeCount < 10 && !item->isCorpse() && g_game().internalRemoveItem(item) == RETURNVALUE_NOERROR) {
++removeCount;
}
}

if (removeCount > 0) {
g_game().addMagicEffect(tile->getPosition(), CONST_ME_POFF);
}
}

bool Monster::pushCreature(const std::shared_ptr<Creature> &creature) {
Expand Down