Core/Creature: Refactor vendor item code (#167)

* Fix loading referenced vendor items
* Refactored vendor item storage
This commit is contained in:
Roc13x authored and GitHub committed 2020-08-06 01:43:39 +02:00
1 parent 3e66838874
commit ac6604fba7
7 files changed
+94 -85

No files matched your search

@@ -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);
@@ -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;
+29 -19
View File
@@ -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)
{
+4 -13
View File
@@ -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<uint32, uint32> QuestRelation;
typedef std::list<QuestRelation> QuestRelList;
typedef std::vector<QuestRelList> GameEventQuestMap;
typedef std::list<NPCVendorEntry> NPCVendorList;
typedef std::vector<NPCVendorList> GameEventNPCVendorMap;
typedef std::unordered_map<uint32, std::vector<VendorItem>> NPCVendorMap;
typedef std::vector<NPCVendorMap> GameEventNPCVendorMap;
typedef std::map<uint32 /*quest id*/, GameEventQuestToEventConditionNum> QuestIdToEventConditionMap;
typedef std::pair<ObjectGuid::LowType /*guid*/, uint32 /*npcflag*/> GuidNPCFlagPair;
typedef std::list<GuidNPCFlagPair> NPCFlagList;
+45 -43
View File
@@ -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<uint32> *skip_vendors)
int ObjectMgr::LoadReferenceVendor(int32 vendor, int32 item, std::set<uint32>* 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<uint32>* skip_vendors, uint32 ORnpcflag) const
bool ObjectMgr::IsVendorItemValid(uint32 vendor_entry, VendorItem const& vItem, Player* player, std::set<uint32>* 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;
}
+3 -3
View File
@@ -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<uint32> *skip_vendors);
int LoadReferenceVendor(int32 vendor, int32 item, std::set<uint32>* 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<uint32>* skip_vendors = nullptr, uint32 ORnpcflag = 0) const;
bool IsVendorItemValid(uint32 vendor_entry, VendorItem const& vItem, Player* player = nullptr, std::set<uint32>* skip_vendors = nullptr, uint32 ORnpcflag = 0) const;
void LoadScriptNames();
ScriptNameContainer const& GetAllScriptNames() const;
+9 -2
View File
@@ -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);