Skip to content

Commit

Permalink
Remove cast and fix hasParent bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo committed Nov 8, 2024
1 parent 835962b commit 1bc0fb0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ std::string Container::getName(bool addArticle /* = false*/) const

bool Container::hasContainerParent() const
{
return getID() != ITEM_BROWSEFIELD && !dynamic_cast<const Player*>(getParent());
if (getID() == ITEM_BROWSEFIELD) {
return false;
}

if (hasParent()) {
if (auto creature = getParent()->getCreature()) {
return !creature->getPlayer();
}
}
return true;
}

void Container::addItem(Item* item)
Expand Down
8 changes: 4 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ void Game::playerMoveUpContainer(uint32_t playerId, uint8_t cid)
}

player->addContainer(cid, parentContainer);
player->sendContainer(cid, parentContainer, parentContainer->hasContainerParent(), player->getContainerIndex(cid));
player->sendContainer(cid, parentContainer, player->getContainerIndex(cid));
}

void Game::playerUpdateContainer(uint32_t playerId, uint8_t cid)
Expand All @@ -2386,7 +2386,7 @@ void Game::playerUpdateContainer(uint32_t playerId, uint8_t cid)
return;
}

player->sendContainer(cid, container, container->hasContainerParent(), player->getContainerIndex(cid));
player->sendContainer(cid, container, player->getContainerIndex(cid));
}

void Game::playerRotateItem(uint32_t playerId, const Position& pos, uint8_t stackPos, const uint16_t spriteId)
Expand Down Expand Up @@ -2548,7 +2548,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position& pos)
player->closeContainer(dummyContainerId);
} else {
player->addContainer(dummyContainerId, container);
player->sendContainer(dummyContainerId, container, false, 0);
player->sendContainer(dummyContainerId, container, 0);
}
}

Expand All @@ -2569,7 +2569,7 @@ void Game::playerSeekInContainer(uint32_t playerId, uint8_t containerId, uint16_
}

player->setContainerIndex(containerId, index);
player->sendContainer(containerId, container, container->hasContainerParent(), index);
player->sendContainer(containerId, container, index);
}

void Game::playerUpdateHouseWindow(uint32_t playerId, uint8_t listId, uint32_t windowTextId, const std::string& text)
Expand Down
5 changes: 2 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ void Player::sendRemoveContainerItem(const Container* container, uint16_t slot)
uint16_t& firstIndex = openContainer.index;
if (firstIndex > 0 && firstIndex >= container->size() - 1) {
firstIndex -= container->capacity();
sendContainer(it.first, container, false, firstIndex);
sendContainer(it.first, container, firstIndex);
}

client->sendRemoveContainerItem(it.first, std::max<uint16_t>(slot, firstIndex),
Expand Down Expand Up @@ -1451,11 +1451,10 @@ void Player::onSendContainer(const Container* container)
return;
}

bool hasContainerParent = container->hasContainerParent();
for (const auto& it : openContainers) {
const OpenContainer& openContainer = it.second;
if (openContainer.container == container) {
client->sendContainer(it.first, container, hasContainerParent, openContainer.index);
client->sendContainer(it.first, container, openContainer.index);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,10 @@ class Player final : public Creature, public Cylinder
void sendAddContainerItem(const Container* container, const Item* item);
void sendUpdateContainerItem(const Container* container, uint16_t slot, const Item* newItem);
void sendRemoveContainerItem(const Container* container, uint16_t slot);
void sendContainer(uint8_t cid, const Container* container, bool hasContainerParent, uint16_t firstIndex)
void sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex)
{
if (client) {
client->sendContainer(cid, container, hasContainerParent, firstIndex);
client->sendContainer(cid, container, firstIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ void ProtocolGame::sendIcons(uint32_t icons)
writeToOutputBuffer(msg);
}

void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool hasContainerParent, uint16_t firstIndex)
void ProtocolGame::sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex)
{
NetworkMessage msg;
msg.addByte(0x6E);
Expand All @@ -1904,7 +1904,7 @@ void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool h
}

msg.addByte(container->capacity());
msg.addByte(hasContainerParent ? 0x01 : 0x00);
msg.addByte(container->hasContainerParent() ? 0x01 : 0x00);
msg.addByte(0x00); // show search icon (boolean)
msg.addByte(container->isUnlocked() ? 0x01 : 0x00); // Drag and drop
msg.addByte(container->hasPagination() ? 0x01 : 0x00); // Pagination
Expand Down
2 changes: 1 addition & 1 deletion src/protocolgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class ProtocolGame final : public Protocol
void sendUpdateContainerItem(uint8_t cid, uint16_t slot, const Item* item);
void sendRemoveContainerItem(uint8_t cid, uint16_t slot, const Item* lastItem);

void sendContainer(uint8_t cid, const Container* container, bool hasContainerParent, uint16_t firstIndex);
void sendContainer(uint8_t cid, const Container* container, uint16_t firstIndex);
void sendEmptyContainer(uint8_t cid);
void sendCloseContainer(uint8_t cid);

Expand Down

0 comments on commit 1bc0fb0

Please sign in to comment.