Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: browseField crash, improve readability, fix shadowing #3031

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
}

void SpawnMonster::cleanup() {
for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end(); ) {
for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end();) {
const auto &monster = it->second;
if (!monster || monster->isRemoved()) {
auto spawnIt = spawnMonsterMap.find(it->first);
Expand Down
4 changes: 2 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4123,7 +4123,7 @@ void Game::playerMoveUpContainer(uint32_t playerId, uint8_t cid) {

auto it = browseFields.find(tile);
if (it == browseFields.end() || it->second.expired()) {
parentContainer = Container::create(tile);
parentContainer = Container::createBrowseField(tile);
browseFields[tile] = parentContainer;
} else {
parentContainer = it->second.lock();
Expand Down Expand Up @@ -4688,7 +4688,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position &pos) {

auto it = browseFields.find(tile);
if (it == browseFields.end() || it->second.expired()) {
container = Container::create(tile);
container = Container::createBrowseField(tile);
browseFields[tile] = container;
} else {
container = it->second.lock();
Expand Down
30 changes: 23 additions & 7 deletions src/items/containers/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,36 @@ std::shared_ptr<Container> Container::create(uint16_t type, uint16_t size, bool
return std::make_shared<Container>(type, size, unlocked, pagination);
}

std::shared_ptr<Container> Container::create(const std::shared_ptr<Tile> &tile) {
auto container = std::make_shared<Container>(ITEM_BROWSEFIELD, 30, false, true);
std::shared_ptr<Container> Container::createBrowseField(const std::shared_ptr<Tile> &tile) {
const auto &newContainer = create(ITEM_BROWSEFIELD, 30, false, true);
if (!newContainer || !tile) {
return nullptr;
}

const TileItemVector* itemVector = tile->getItemList();
if (itemVector) {
for (const auto &item : *itemVector) {
if (((item->getContainer() || item->hasProperty(CONST_PROP_MOVABLE)) || (item->isWrapable() && !item->hasProperty(CONST_PROP_MOVABLE) && !item->hasProperty(CONST_PROP_BLOCKPATH))) && !item->hasAttribute(ItemAttribute_t::UNIQUEID)) {
container->itemlist.push_front(item);
item->setParent(container);
if (!item) {
continue;
}

// Checks if the item has an internal container, is movable, or is packable without blocking the path.
bool isItemValid = item->getContainer() || item->hasProperty(CONST_PROP_MOVABLE) || (item->isWrapable() && !item->hasProperty(CONST_PROP_MOVABLE) && !item->hasProperty(CONST_PROP_BLOCKPATH));

// If the item has a unique ID or is not valid, skip to the next item.
if (item->hasAttribute(ItemAttribute_t::UNIQUEID) || !isItemValid) {
continue;
}

// Add the item to the new container and set its parent.
newContainer->itemlist.push_front(item);
item->setParent(newContainer);
}
}

container->setParent(tile);
return container;
// Set the parent of the new container to be the tile.
newContainer->setParent(tile);
return newContainer;
}

Container::~Container() {
Expand Down
16 changes: 15 additions & 1 deletion src/items/containers/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,21 @@ class Container : public Item, public Cylinder {

static std::shared_ptr<Container> create(uint16_t type);
static std::shared_ptr<Container> create(uint16_t type, uint16_t size, bool unlocked = true, bool pagination = false);
static std::shared_ptr<Container> create(const std::shared_ptr<Tile> &type);

/**
* @brief Creates a container for browse field functionality with items from a specified tile.
*
* This function generates a new container specifically for browse field use,
* populating it with items that meet certain criteria from the provided tile. Items
* that can be included must either have an internal container, be movable, or be
* wrapable without blocking path and without a unique ID.
*
* @param tile A shared pointer to the Tile from which items will be sourced.
* @return std::shared_ptr<Container> Returns a shared pointer to the newly created Container if successful; otherwise, returns nullptr.
*
* @note This function will return nullptr if the newContainer could not be created or if the tile pointer is null.
*/
static std::shared_ptr<Container> createBrowseField(const std::shared_ptr<Tile> &type);

// non-copyable
Container(const Container &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4615,7 +4615,7 @@ void ProtocolGame::sendUnjustifiedPoints(const uint8_t &dayProgress, const uint8
}

void ProtocolGame::sendContainer(uint8_t cid, const std::shared_ptr<Container> &container, bool hasParent, uint16_t firstIndex) {
if (!player) {
if (!player || !container) {
return;
}

Expand Down