Skip to content

Commit

Permalink
fix: simplify functions and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Aug 31, 2023
1 parent 1022409 commit 9011074
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 34 deletions.
30 changes: 30 additions & 0 deletions data/scripts/talkactions/god/goto_house.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local sellHouse = TalkAction("/gotohouse")

function sellHouse.onSay(player, words, param)
local targetPlayer = Player(param)
if targetPlayer then
local targetHouse = targetPlayer:getHouse()
if not targetHouse then
targetPlayer:sendCancelMessage(string.format("The player %s not have house.", player:getName()))
return
end

targetPlayer:teleportTo(targetHouse:getExitPosition())
return true
else
local house = player:getHouse()
if not house then
player:sendCancelMessage("You not have house. For goto house of one player use the player name param, usage: /gotohouse playername")
return
end

player:teleportTo(house:getExitPosition())
return true
end

return false
end

sellHouse:separator(" ")
sellHouse:groupType("god")
sellHouse:register()
24 changes: 16 additions & 8 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,9 @@ ReturnValue Game::checkMoveItemToCylinder(Player* player, Cylinder* fromCylinder
}

const Container* topParentContainer = toCylinderContainer->getRootContainer();

if (!item->isStoreItem() && (containerID == ITEM_STORE_INBOX || topParentContainer->getParent() && topParentContainer->getParent()->getContainer() && topParentContainer->getParent()->getContainer()->getID() == ITEM_STORE_INBOX)) {
const auto parentContainer = topParentContainer->getParent() ? topParentContainer->getParent()->getContainer() : nullptr;
auto isStoreInbox = parentContainer && parentContainer->isStoreInbox();
if (!item->isStoreItem() && (containerID == ITEM_STORE_INBOX || isStoreInbox)) {
return RETURNVALUE_CONTAINERNOTENOUGHROOM;
}

Expand All @@ -1605,7 +1606,7 @@ ReturnValue Game::checkMoveItemToCylinder(Player* player, Cylinder* fromCylinder
isValidMoveItem = true;
}

if (topParentContainer->getParent() && topParentContainer->getParent()->getContainer() && (topParentContainer->getParent()->getContainer()->isDepotChest() || topParentContainer->getParent()->getContainer()->getID() == ITEM_STORE_INBOX)) {
if (parentContainer && (parentContainer->isDepotChest() || isStoreInbox)) {
isValidMoveItem = true;
}

Expand Down Expand Up @@ -1860,7 +1861,9 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,

// Actor related actions
if (fromCylinder && actor && toCylinder) {
if (!fromCylinder->getContainer() || !actor->getPlayer() || !toCylinder->getContainer()) {
auto fromContainer = fromCylinder->getContainer();
auto toContainer = toCylinder->getContainer();
if (!fromContainer || !actor->getPlayer() || !toContainer) {
return ret;
}

Expand All @@ -1876,12 +1879,17 @@ ReturnValue Game::internalMoveItem(Cylinder* fromCylinder, Cylinder* toCylinder,
}

// Looting analyser
if (it.isCorpse && toCylinder->getContainer()->getTopParent() == player && item->getIsLootTrackeable()) {
if (it.isCorpse && toContainer->getTopParent() == player && item->getIsLootTrackeable()) {
player->sendLootStats(item, static_cast<uint8_t>(item->getItemCount()));
}

player->onSendContainer(toCylinder->getContainer());
player->onSendContainer(fromCylinder->getContainer());
// StoreInbox update containers
if (toContainer->isStoreInbox()) {
player->onSendContainer(toContainer);
}
if (fromContainer->isStoreInbox()) {
player->onSendContainer(fromContainer);
}
}
}

Expand Down Expand Up @@ -4128,7 +4136,7 @@ void Game::playerSeekInContainer(uint32_t playerId, uint8_t containerId, uint16_
return;
}

if (container->getID() == ITEM_STORE_INBOX) {
if (container->isStoreInbox()) {
auto enumName = magic_enum::enum_name(static_cast<StoreInboxCategory_t>(containerCategory)).data();
container->setAttribute(ItemAttribute_t::STORE_INBOX_CATEGORY, enumName);
g_logger().debug("Setting new container with store inbox category name {}", enumName);
Expand Down
28 changes: 27 additions & 1 deletion src/items/containers/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,39 @@ std::deque<Item*> Container::getStoreInboxFilteredItems() const {
return storeInboxFilteredList;
}

phmap::flat_hash_set<StoreInboxCategory_t> Container::getStoreInboxValidCategories() const {
phmap::flat_hash_set<StoreInboxCategory_t> validCategories;
for (const auto& item : itemlist) {
auto attribute = item->getCustomAttribute("unWrapId");
uint16_t unWrapId = attribute ? static_cast<uint16_t>(attribute->getInteger()) : 0;
if (unWrapId != 0) {
const auto &itemType = Item::items.getItemType(unWrapId);
auto category = magic_enum::enum_cast<StoreInboxCategory_t>(toPascalCase(itemType.m_primaryType));
g_logger().debug("Store unwrap item '{}', primary type {}", unWrapId, toPascalCase(itemType.m_primaryType));
if (category.has_value()) {
g_logger().debug("Adding valid category {}", static_cast<uint8_t>(category.value()));
validCategories.insert(category.value());
}
}
}

return validCategories;
}

Item* Container::getFilteredItemByIndex(size_t index) const {
const auto &filteredItems = getStoreInboxFilteredItems();
if (index >= filteredItems.size()) {
return nullptr;
}

return filteredItems[index];
auto item = filteredItems[index];

auto it = std::find(itemlist.begin(), itemlist.end(), item);
if (it == itemlist.end()) {
return nullptr;
}

return *it;
}

Item* Container::getItemByIndex(size_t index) const {
Expand Down
1 change: 1 addition & 0 deletions src/items/containers/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Container : public Item, public Cylinder {
void addItem(Item* item);
StashContainerList getStowableItems() const;
std::deque<Item*> getStoreInboxFilteredItems() const;
phmap::flat_hash_set<StoreInboxCategory_t> getStoreInboxValidCategories() const;
Item* getFilteredItemByIndex(size_t index) const;
Item* getItemByIndex(size_t index) const;
bool isHoldingItem(const Item* item) const;
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/creatures/creature_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ int CreatureFunctions::luaCreatureTeleportTo(lua_State* L) {
const Position oldPosition = creature->getPosition();
if (auto ret = g_game().internalTeleport(creature, position, pushMovement);
ret != RETURNVALUE_NOERROR) {
g_logger().error("[{}] Failed to teleport creature {}, on position {}, error code: {}", __FUNCTION__, creature->getName(), oldPosition.toString(), getReturnMessage(ret));
g_logger().debug("[{}] Failed to teleport creature {}, on position {}, error code: {}", __FUNCTION__, creature->getName(), oldPosition.toString(), getReturnMessage(ret));
pushBoolean(L, false);
return 1;
}
Expand Down
54 changes: 30 additions & 24 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ namespace {
* @param msg The network message to send the category to.
*/
template <typename T>
void sendContainerCategory(NetworkMessage &msg, std::deque<T> categories = {}, uint8_t categoryType = 0) {
void sendContainerCategory(NetworkMessage &msg, phmap::flat_hash_set<T> categories = {}, uint8_t categoryType = 0) {
msg.addByte(categoryType);
g_logger().debug("Sendding category type '{}', categories total size '{}'", categoryType, categories.size());
msg.addByte(categories.size());
Expand Down Expand Up @@ -4282,6 +4282,7 @@ void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool h
maxItemsToSend = container->capacity();
}

const ItemDeque &itemList = container->getItemList();
if (firstIndex >= containerSize) {
msg.addByte(0x00);
} else if (container->getID() == ITEM_STORE_INBOX && !itemsStoreInboxToSend.empty()) {
Expand All @@ -4293,41 +4294,46 @@ void ProtocolGame::sendContainer(uint8_t cid, const Container* container, bool h
msg.addByte(std::min<uint32_t>(maxItemsToSend, containerSize));

uint32_t i = 0;
const ItemDeque &itemList = container->getItemList();
for (ItemDeque::const_iterator it = itemList.begin() + firstIndex, end = itemList.end(); i < maxItemsToSend && it != end; ++it, ++i) {
AddItem(msg, *it);
}
}

if (!oldProtocol) {
if (container->getID() == ITEM_STORE_INBOX) {
std::deque<StoreInboxCategory_t> validCategories;
for (const auto item : container->getItemList()) {
auto attribute = item->getCustomAttribute("unWrapId");
uint16_t unWrapId = attribute ? static_cast<uint16_t>(attribute->getInteger()) : 0;
if (unWrapId != 0) {
const auto &itemType = Item::items.getItemType(unWrapId);
auto category = magic_enum::enum_cast<StoreInboxCategory_t>(toPascalCase(itemType.m_primaryType));
g_logger().debug("Store unwrap item '{}', primary type {}", unWrapId, toPascalCase(itemType.m_primaryType));
if (category.has_value()) {
g_logger().debug("Adding valid category {}", static_cast<uint8_t>(category.value()));
validCategories.push_back(category.value());
}
// From here on down is for version 13.21+
if (oldProtocol) {
writeToOutputBuffer(msg);
return;
}

if (container->isStoreInbox()) {
auto categories = container->getStoreInboxValidCategories();
const auto enumName = container->getAttribute<std::string>(ItemAttribute_t::STORE_INBOX_CATEGORY);
auto category = magic_enum::enum_cast<StoreInboxCategory_t>(enumName);
if (category.has_value()) {
bool toSendCategory = false;
// Check if category exist in the deque
for (const auto& tempCategory : categories) {
if (tempCategory == category.value()) {
toSendCategory = true;
g_logger().debug("found category {}", toSendCategory);
}
}

const auto enumName = container->getAttribute<std::string>(ItemAttribute_t::STORE_INBOX_CATEGORY);
auto category = magic_enum::enum_cast<StoreInboxCategory_t>(enumName);
if (category.has_value()) {
sendContainerCategory<StoreInboxCategory_t>(msg, validCategories, static_cast<uint8_t>(category.value()));
} else {
sendContainerCategory<StoreInboxCategory_t>(msg, validCategories);
if (!toSendCategory) {
Container* container = player->getContainerByID(cid);
if (container) {
container->removeAttribute(ItemAttribute_t::STORE_INBOX_CATEGORY);
}
}
sendContainerCategory<StoreInboxCategory_t>(msg, categories, static_cast<uint8_t>(category.value()));
} else {
msg.addByte(0x00);
msg.addByte(0x00);
sendContainerCategory<StoreInboxCategory_t>(msg, categories);
}
} else {
msg.addByte(0x00);
msg.addByte(0x00);
}

writeToOutputBuffer(msg);
}

Expand Down

0 comments on commit 9011074

Please sign in to comment.