From ac6604fba7b60e8d9d6c4d251e07639b2c2c8106 Mon Sep 17 00:00:00 2001 From: Roc13x Date: Thu, 6 Aug 2020 00:43:39 +0100 Subject: [PATCH] Core/Creature: Refactor vendor item code (#167) * Fix loading referenced vendor items * Refactored vendor item storage --- .../Database/Implementation/WorldDatabase.cpp | 2 +- .../game/Entities/Creature/CreatureData.h | 7 +- src/server/game/Events/GameEventMgr.cpp | 48 ++++++---- src/server/game/Events/GameEventMgr.h | 17 +--- src/server/game/Globals/ObjectMgr.cpp | 88 ++++++++++--------- src/server/game/Globals/ObjectMgr.h | 6 +- src/server/scripts/Commands/cs_npc.cpp | 11 ++- 7 files changed, 94 insertions(+), 85 deletions(-) diff --git a/src/server/database/Database/Implementation/WorldDatabase.cpp b/src/server/database/Database/Implementation/WorldDatabase.cpp index ceaa2f9aba..84ac919955 100644 --- a/src/server/database/Database/Implementation/WorldDatabase.cpp +++ b/src/server/database/Database/Implementation/WorldDatabase.cpp @@ -36,7 +36,7 @@ void WorldDatabaseConnection::DoPrepareStatements() PrepareStatement(WORLD_DEL_GAME_TELE, "DELETE FROM game_tele WHERE name = ?", CONNECTION_ASYNC); PrepareStatement(WORLD_INS_NPC_VENDOR, "INSERT INTO npc_vendor (entry, item, maxcount, incrtime, extendedcost, type) VALUES(?, ?, ?, ?, ?, ?)", CONNECTION_ASYNC); PrepareStatement(WORLD_DEL_NPC_VENDOR, "DELETE FROM npc_vendor WHERE entry = ? AND item = ? AND type = ?", CONNECTION_ASYNC); - PrepareStatement(WORLD_SEL_NPC_VENDOR_REF, "SELECT item, maxcount, incrtime, ExtendedCost, type FROM npc_vendor WHERE entry = ? AND type = ? ORDER BY slot ASC", CONNECTION_SYNCH); + PrepareStatement(WORLD_SEL_NPC_VENDOR_REF, "SELECT item, maxcount, incrtime, ExtendedCost, type FROM npc_vendor WHERE entry = ? ORDER BY slot ASC", CONNECTION_SYNCH); PrepareStatement(WORLD_UPD_CREATURE_MOVEMENT_TYPE, "UPDATE creature SET MovementType = ? WHERE guid = ?", CONNECTION_ASYNC); PrepareStatement(WORLD_UPD_CREATURE_FACTION, "UPDATE creature_template SET faction = ? WHERE entry = ?", CONNECTION_ASYNC); PrepareStatement(WORLD_UPD_CREATURE_NPCFLAG, "UPDATE creature_template SET npcflag = ? WHERE entry = ?", CONNECTION_ASYNC); diff --git a/src/server/game/Entities/Creature/CreatureData.h b/src/server/game/Entities/Creature/CreatureData.h index d4a795a9e8..ebd2560257 100644 --- a/src/server/game/Entities/Creature/CreatureData.h +++ b/src/server/game/Entities/Creature/CreatureData.h @@ -342,8 +342,7 @@ struct CreatureAddon // Vendors struct VendorItem { - VendorItem(uint32 _item, int32 _maxcount, uint32 _incrtime, uint32 _ExtendedCost, uint8 _Type) - : item(_item), maxcount(_maxcount), incrtime(_incrtime), ExtendedCost(_ExtendedCost), Type(_Type) { } + VendorItem() : item(0), maxcount(0), incrtime(0), ExtendedCost(0), Type(0) { } uint32 item; uint32 maxcount; // 0 for infinity item amount @@ -368,9 +367,9 @@ struct VendorItemData } bool Empty() const { return m_items.empty(); } uint32 GetItemCount() const { return m_items.size(); } - void AddItem(uint32 item, int32 maxcount, uint32 ptime, uint32 ExtendedCost, uint8 type) + void AddItem(VendorItem vItem) { - m_items.emplace_back(item, maxcount, ptime, ExtendedCost, type); + m_items.emplace_back(std::move(vItem)); } bool RemoveItem(uint32 item_id, uint8 type); VendorItem const* FindItemCostPair(uint32 item_id, uint32 extendedCost, uint8 type) const; diff --git a/src/server/game/Events/GameEventMgr.cpp b/src/server/game/Events/GameEventMgr.cpp index 88cdb42f7a..01075749fd 100644 --- a/src/server/game/Events/GameEventMgr.cpp +++ b/src/server/game/Events/GameEventMgr.cpp @@ -815,6 +815,7 @@ void GameEventMgr::LoadFromDB() Field* fields = result->Fetch(); uint8 event_id = fields[0].GetUInt8(); + ObjectGuid::LowType guid = fields[1].GetUInt32(); if (event_id >= mGameEventVendors.size()) { @@ -822,14 +823,6 @@ void GameEventMgr::LoadFromDB() continue; } - NPCVendorList& vendors = mGameEventVendors[event_id]; - NPCVendorEntry newEntry; - ObjectGuid::LowType guid = fields[1].GetUInt32(); - newEntry.item = fields[2].GetUInt32(); - newEntry.maxcount = fields[3].GetUInt32(); - newEntry.incrtime = fields[4].GetUInt32(); - newEntry.ExtendedCost = fields[5].GetUInt32(); - newEntry.Type = fields[6].GetUInt8(); // get the event npc flag for checking if the npc will be vendor during the event or not uint32 event_npc_flag = 0; NPCFlagList& flist = mGameEventNPCFlags[event_id]; @@ -841,17 +834,25 @@ void GameEventMgr::LoadFromDB() break; } } - // get creature entry - newEntry.entry = 0; + + uint32 entry = 0; if (CreatureData const* data = sObjectMgr->GetCreatureData(guid)) - newEntry.entry = data->id; + entry = data->id; + + VendorItem vItem; + vItem.item = fields[2].GetUInt32(); + vItem.maxcount = fields[3].GetUInt32(); + vItem.incrtime = fields[4].GetUInt32(); + vItem.ExtendedCost = fields[5].GetUInt32(); + vItem.Type = fields[6].GetUInt8(); // check validity with event's npcflag - if (!sObjectMgr->IsVendorItemValid(newEntry.entry, newEntry.item, newEntry.maxcount, newEntry.incrtime, newEntry.ExtendedCost, newEntry.Type, nullptr, nullptr, event_npc_flag)) + if (!sObjectMgr->IsVendorItemValid(entry, vItem, nullptr, nullptr, event_npc_flag)) continue; - vendors.push_back(newEntry); + NPCVendorMap& vendors = mGameEventVendors[event_id]; + vendors[entry].emplace_back(std::move(vItem)); ++count; } @@ -1232,12 +1233,15 @@ void GameEventMgr::UpdateBattlegroundSettings() void GameEventMgr::UpdateEventNPCVendor(uint16 event_id, bool activate) { - for (NPCVendorList::iterator itr = mGameEventVendors[event_id].begin(); itr != mGameEventVendors[event_id].end(); ++itr) + for (NPCVendorMap::iterator itr = mGameEventVendors[event_id].begin(); itr != mGameEventVendors[event_id].end(); ++itr) { - if (activate) - sObjectMgr->AddVendorItem(itr->entry, itr->item, itr->maxcount, itr->incrtime, itr->ExtendedCost, itr->Type, false); - else - sObjectMgr->RemoveVendorItem(itr->entry, itr->item, itr->Type, false); + for (VendorItem const& vItem : itr->second) + { + if (activate) + sObjectMgr->AddVendorItem(itr->first, vItem, false); + else + sObjectMgr->RemoveVendorItem(itr->first, vItem.item, vItem.Type, false); + } } } @@ -1597,7 +1601,13 @@ void GameEventMgr::UpdateWorldStates(uint16 event_id, bool Activate) } } -GameEventMgr::GameEventMgr() : isSystemInit(false) { } +GameEventMgr::GameEventMgr() : isSystemInit(false) +{ +} + +GameEventMgr::~GameEventMgr() +{ +} void GameEventMgr::HandleQuestComplete(uint32 quest_id) { diff --git a/src/server/game/Events/GameEventMgr.h b/src/server/game/Events/GameEventMgr.h index 81f3f15eba..796e8bf9a9 100644 --- a/src/server/game/Events/GameEventMgr.h +++ b/src/server/game/Events/GameEventMgr.h @@ -85,25 +85,16 @@ struct ModelEquip uint8 equipment_id_prev; }; -struct NPCVendorEntry -{ - uint32 entry; // creature entry - uint32 item; // item id - int32 maxcount; // 0 for infinite - uint32 incrtime; // time for restore items amount if maxcount != 0 - uint32 ExtendedCost; - uint8 Type; // 1 item, 2 currency -}; - class Player; class Creature; class Quest; +struct VendorItem; class TC_GAME_API GameEventMgr { private: GameEventMgr(); - ~GameEventMgr() { } + ~GameEventMgr(); public: static GameEventMgr* instance(); @@ -160,8 +151,8 @@ class TC_GAME_API GameEventMgr typedef std::pair QuestRelation; typedef std::list QuestRelList; typedef std::vector GameEventQuestMap; - typedef std::list NPCVendorList; - typedef std::vector GameEventNPCVendorMap; + typedef std::unordered_map> NPCVendorMap; + typedef std::vector GameEventNPCVendorMap; typedef std::map QuestIdToEventConditionMap; typedef std::pair GuidNPCFlagPair; typedef std::list NPCFlagList; diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index 4b14b62257..50d6a3266e 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -8835,12 +8835,11 @@ void ObjectMgr::LoadCreatureDefaultTrainers() TC_LOG_INFO("server.loading", ">> Loaded " SZFMTD " default trainers in %u ms", _creatureDefaultTrainers.size(), GetMSTimeDiffToNow(oldMSTime)); } -int ObjectMgr::LoadReferenceVendor(int32 vendor, int32 item, uint8 type, std::set *skip_vendors) +int ObjectMgr::LoadReferenceVendor(int32 vendor, int32 item, std::set* skip_vendors) { // find all items from the reference vendor WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_NPC_VENDOR_REF); stmt->setUInt32(0, uint32(item)); - stmt->setUInt8(1, type); PreparedQueryResult result = WorldDatabase.Query(stmt); if (!result) @@ -8855,20 +8854,22 @@ int ObjectMgr::LoadReferenceVendor(int32 vendor, int32 item, uint8 type, std::se // if item is a negative, its a reference if (item_id < 0) - count += LoadReferenceVendor(vendor, -item_id, type, skip_vendors); + count += LoadReferenceVendor(vendor, -item_id, skip_vendors); else { - int32 maxcount = fields[1].GetUInt32(); - uint32 incrtime = fields[2].GetUInt32(); - uint32 ExtendedCost = fields[3].GetUInt32(); - uint8 type = fields[4].GetUInt8(); + VendorItem vItem; + vItem.item = item_id; + vItem.maxcount = fields[1].GetUInt32(); + vItem.incrtime = fields[2].GetUInt32(); + vItem.ExtendedCost = fields[3].GetUInt32(); + vItem.Type = fields[4].GetUInt8(); - if (!IsVendorItemValid(vendor, item_id, maxcount, incrtime, ExtendedCost, type, nullptr, skip_vendors)) + if (!IsVendorItemValid(vendor, vItem, nullptr, skip_vendors)) continue; VendorItemData& vList = _cacheVendorItemStore[vendor]; - vList.AddItem(item_id, maxcount, incrtime, ExtendedCost, type); + vList.AddItem(std::move(vItem)); ++count; } } while (result->NextRow()); @@ -8906,20 +8907,21 @@ void ObjectMgr::LoadVendors() // if item is a negative, its a reference if (item_id < 0) - count += LoadReferenceVendor(entry, -item_id, 0, &skip_vendors); + count += LoadReferenceVendor(entry, -item_id, &skip_vendors); else { - uint32 maxcount = fields[2].GetUInt32(); - uint32 incrtime = fields[3].GetUInt32(); - uint32 ExtendedCost = fields[4].GetUInt32(); - uint8 type = fields[5].GetUInt8(); + VendorItem vItem; + vItem.item = item_id; + vItem.maxcount = fields[2].GetUInt32(); + vItem.incrtime = fields[3].GetUInt32(); + vItem.ExtendedCost = fields[4].GetUInt32(); + vItem.Type = fields[5].GetUInt8(); - if (!IsVendorItemValid(entry, item_id, maxcount, incrtime, ExtendedCost, type, nullptr, &skip_vendors)) + if (!IsVendorItemValid(entry, vItem, nullptr, &skip_vendors)) continue; VendorItemData& vList = _cacheVendorItemStore[entry]; - - vList.AddItem(item_id, maxcount, incrtime, ExtendedCost, type); + vList.AddItem(std::move(vItem)); ++count; } } @@ -9071,21 +9073,21 @@ uint32 ObjectMgr::GetCreatureDefaultTrainer(uint32 creatureId) const return 0; } -void ObjectMgr::AddVendorItem(uint32 entry, uint32 item, int32 maxcount, uint32 incrtime, uint32 extendedCost, uint8 type, bool persist /*= true*/) +void ObjectMgr::AddVendorItem(uint32 entry, VendorItem const& vItem, bool persist /*= true*/) { VendorItemData& vList = _cacheVendorItemStore[entry]; - vList.AddItem(item, maxcount, incrtime, extendedCost, type); + vList.AddItem(vItem); if (persist) { WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_INS_NPC_VENDOR); stmt->setUInt32(0, entry); - stmt->setUInt32(1, item); - stmt->setUInt8(2, maxcount); - stmt->setUInt32(3, incrtime); - stmt->setUInt32(4, extendedCost); - stmt->setUInt8(5, type); + stmt->setUInt32(1, vItem.item); + stmt->setUInt8(2, vItem.maxcount); + stmt->setUInt32(3, vItem.incrtime); + stmt->setUInt32(4, vItem.ExtendedCost); + stmt->setUInt8(5, vItem.Type); WorldDatabase.Execute(stmt); } @@ -9114,7 +9116,7 @@ bool ObjectMgr::RemoveVendorItem(uint32 entry, uint32 item, uint8 type, bool per return true; } -bool ObjectMgr::IsVendorItemValid(uint32 vendor_entry, uint32 id, int32 maxcount, uint32 incrtime, uint32 ExtendedCost, uint8 type, Player* player, std::set* skip_vendors, uint32 ORnpcflag) const +bool ObjectMgr::IsVendorItemValid(uint32 vendor_entry, VendorItem const& vItem, Player* player, std::set* skip_vendors, uint32 ORnpcflag) const { CreatureTemplate const* cInfo = sObjectMgr->GetCreatureTemplate(vendor_entry); if (!cInfo) @@ -9141,41 +9143,41 @@ bool ObjectMgr::IsVendorItemValid(uint32 vendor_entry, uint32 id, int32 maxcount return false; } - if ((type == ITEM_VENDOR_TYPE_ITEM && !sObjectMgr->GetItemTemplate(id)) || - (type == ITEM_VENDOR_TYPE_CURRENCY && !sCurrencyTypesStore.LookupEntry(id))) + if ((vItem.Type == ITEM_VENDOR_TYPE_ITEM && !sObjectMgr->GetItemTemplate(vItem.item)) || + (vItem.Type == ITEM_VENDOR_TYPE_CURRENCY && !sCurrencyTypesStore.LookupEntry(vItem.item))) { if (player) - ChatHandler(player->GetSession()).PSendSysMessage(LANG_ITEM_NOT_FOUND, id, type); + ChatHandler(player->GetSession()).PSendSysMessage(LANG_ITEM_NOT_FOUND, vItem.item, vItem.Type); else - TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` for Vendor (Entry: %u) have in item list non-existed item (%u, type %u), ignore", vendor_entry, id, type); + TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` for Vendor (Entry: %u) have in item list non-existed item (%u, type %u), ignore", vendor_entry, vItem.item, vItem.Type); return false; } - if (ExtendedCost && !sItemExtendedCostStore.LookupEntry(ExtendedCost)) + if (vItem.ExtendedCost && !sItemExtendedCostStore.LookupEntry(vItem.ExtendedCost)) { if (player) - ChatHandler(player->GetSession()).PSendSysMessage(LANG_EXTENDED_COST_NOT_EXIST, ExtendedCost); + ChatHandler(player->GetSession()).PSendSysMessage(LANG_EXTENDED_COST_NOT_EXIST, vItem.ExtendedCost); else - TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has Item (Entry: %u) with wrong ExtendedCost (%u) for vendor (%u), ignore", id, ExtendedCost, vendor_entry); + TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has Item (Entry: %u) with wrong ExtendedCost (%u) for vendor (%u), ignore", vItem.item, vItem.ExtendedCost, vendor_entry); return false; } - if (type == ITEM_VENDOR_TYPE_ITEM) // not applicable to currencies + if (vItem.Type == ITEM_VENDOR_TYPE_ITEM) // not applicable to currencies { - if (maxcount > 0 && incrtime == 0) + if (vItem.maxcount > 0 && vItem.incrtime == 0) { if (player) - ChatHandler(player->GetSession()).PSendSysMessage("MaxCount != 0 (%u) but IncrTime == 0", maxcount); + ChatHandler(player->GetSession()).PSendSysMessage("MaxCount != 0 (%u) but IncrTime == 0", vItem.maxcount); else - TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has `maxcount` (%u) for item %u of vendor (Entry: %u) but `incrtime`=0, ignore", maxcount, id, vendor_entry); + TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has `maxcount` (%u) for item %u of vendor (Entry: %u) but `incrtime`=0, ignore", vItem.maxcount, vItem.item, vendor_entry); return false; } - else if (maxcount == 0 && incrtime > 0) + else if (vItem.maxcount == 0 && vItem.incrtime > 0) { if (player) ChatHandler(player->GetSession()).PSendSysMessage("MaxCount == 0 but IncrTime<>= 0"); else - TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has `maxcount`=0 for item %u of vendor (Entry: %u) but `incrtime`<>0, ignore", id, vendor_entry); + TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` has `maxcount`=0 for item %u of vendor (Entry: %u) but `incrtime`<>0, ignore", vItem.item, vendor_entry); return false; } } @@ -9184,18 +9186,18 @@ bool ObjectMgr::IsVendorItemValid(uint32 vendor_entry, uint32 id, int32 maxcount if (!vItems) return true; // later checks for non-empty lists - if (vItems->FindItemCostPair(id, ExtendedCost, type)) + if (vItems->FindItemCostPair(vItem.item, vItem.ExtendedCost, vItem.Type)) { if (player) - ChatHandler(player->GetSession()).PSendSysMessage(LANG_ITEM_ALREADY_IN_LIST, id, ExtendedCost, type); + ChatHandler(player->GetSession()).PSendSysMessage(LANG_ITEM_ALREADY_IN_LIST, vItem.item, vItem.ExtendedCost, vItem.Type); else - TC_LOG_ERROR("sql.sql", "Table `npc_vendor` has duplicate items %u (with extended cost %u, type %u) for vendor (Entry: %u), ignoring", id, ExtendedCost, type, vendor_entry); + TC_LOG_ERROR("sql.sql", "Table `npc_vendor` has duplicate items %u (with extended cost %u, type %u) for vendor (Entry: %u), ignoring", vItem.item, vItem.ExtendedCost, vItem.Type, vendor_entry); return false; } - if (type == ITEM_VENDOR_TYPE_CURRENCY && maxcount == 0) + if (vItem.Type == ITEM_VENDOR_TYPE_CURRENCY && vItem.maxcount == 0) { - TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` have Item (Entry: %u, type: %u) with missing maxcount for vendor (%u), ignore", id, type, vendor_entry); + TC_LOG_ERROR("sql.sql", "Table `(game_event_)npc_vendor` have Item (Entry: %u, type: %u) with missing maxcount for vendor (%u), ignore", vItem.item, vItem.Type, vendor_entry); return false; } diff --git a/src/server/game/Globals/ObjectMgr.h b/src/server/game/Globals/ObjectMgr.h index 4a4e2bac22..bc56d0f41e 100644 --- a/src/server/game/Globals/ObjectMgr.h +++ b/src/server/game/Globals/ObjectMgr.h @@ -993,7 +993,7 @@ class TC_GAME_API ObjectMgr GameObjectTemplate const* GetGameObjectTemplate(uint32 entry) const; GameObjectTemplateContainer const* GetGameObjectTemplates() const { return &_gameObjectTemplateStore; } - int LoadReferenceVendor(int32 vendor, int32 item, uint8 type, std::set *skip_vendors); + int LoadReferenceVendor(int32 vendor, int32 item, std::set* skip_vendors); void LoadGameObjectTemplate(); void LoadGameObjectTemplateAddons(); @@ -1515,9 +1515,9 @@ class TC_GAME_API ObjectMgr return nullptr; } - void AddVendorItem(uint32 entry, uint32 item, int32 maxcount, uint32 incrtime, uint32 extendedCost, uint8 type, bool persist = true); // for event + void AddVendorItem(uint32 entry, VendorItem const& vItem, bool persist = true); // for event bool RemoveVendorItem(uint32 entry, uint32 item, uint8 type, bool persist = true); // for event - bool IsVendorItemValid(uint32 vendor_entry, uint32 id, int32 maxcount, uint32 ptime, uint32 ExtendedCost, uint8 type, Player* player = nullptr, std::set* skip_vendors = nullptr, uint32 ORnpcflag = 0) const; + bool IsVendorItemValid(uint32 vendor_entry, VendorItem const& vItem, Player* player = nullptr, std::set* skip_vendors = nullptr, uint32 ORnpcflag = 0) const; void LoadScriptNames(); ScriptNameContainer const& GetAllScriptNames() const; diff --git a/src/server/scripts/Commands/cs_npc.cpp b/src/server/scripts/Commands/cs_npc.cpp index 2aa1ea03aa..24429f62d2 100644 --- a/src/server/scripts/Commands/cs_npc.cpp +++ b/src/server/scripts/Commands/cs_npc.cpp @@ -437,13 +437,20 @@ public: uint32 vendor_entry = vendor->GetEntry(); - if (!sObjectMgr->IsVendorItemValid(vendor_entry, itemId, maxcount, incrtime, extendedcost, type, handler->GetSession()->GetPlayer())) + VendorItem vItem; + vItem.item = itemId; + vItem.maxcount = maxcount; + vItem.incrtime = incrtime; + vItem.ExtendedCost = extendedcost; + vItem.Type = type; + + if (!sObjectMgr->IsVendorItemValid(vendor_entry, vItem, handler->GetSession()->GetPlayer())) { handler->SetSentErrorMessage(true); return false; } - sObjectMgr->AddVendorItem(vendor_entry, itemId, maxcount, incrtime, extendedcost, type); + sObjectMgr->AddVendorItem(vendor_entry, vItem); ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(itemId);