mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/Pools: fix pool memory usage (#21138)
Change vector to unordered map to save memory and startup time
This commit is contained in:
@@ -556,23 +556,10 @@ void PoolGroup<Quest>::ReSpawn1Object(PoolObject* /*obj*/) { }
|
||||
////////////////////////////////////////////////////////////
|
||||
// Methods of class PoolMgr
|
||||
|
||||
PoolMgr::PoolMgr() : max_pool_id(0) { }
|
||||
PoolMgr::PoolMgr() { }
|
||||
|
||||
void PoolMgr::Initialize()
|
||||
{
|
||||
QueryResult result = WorldDatabase.Query("SELECT MAX(entry) FROM pool_template");
|
||||
if (result)
|
||||
{
|
||||
Field* fields = result->Fetch();
|
||||
max_pool_id = fields[0].GetUInt32();
|
||||
}
|
||||
|
||||
mPoolTemplate.resize(max_pool_id + 1);
|
||||
mPoolCreatureGroups.resize(max_pool_id + 1);
|
||||
mPoolGameobjectGroups.resize(max_pool_id + 1);
|
||||
mPoolPoolGroups.resize(max_pool_id + 1);
|
||||
mPoolQuestGroups.resize(max_pool_id + 1);
|
||||
|
||||
mQuestSearchMap.clear();
|
||||
mGameobjectSearchMap.clear();
|
||||
mCreatureSearchMap.clear();
|
||||
@@ -645,9 +632,10 @@ void PoolMgr::LoadFromDB()
|
||||
TC_LOG_ERROR("sql.sql", "`pool_creature` has a non existing creature spawn (GUID: " UI64FMTD ") defined for pool id (%u), skipped.", guid, pool_id);
|
||||
continue;
|
||||
}
|
||||
if (pool_id > max_pool_id)
|
||||
auto it = mPoolTemplate.find(pool_id);
|
||||
if (it == mPoolTemplate.end())
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_creature` pool id (%u) is out of range compared to max pool id in `pool_template`, skipped.", pool_id);
|
||||
TC_LOG_ERROR("sql.sql", "`pool_creature` pool id (%u) is not in `pool_template`, skipped.", pool_id);
|
||||
continue;
|
||||
}
|
||||
if (chance < 0 || chance > 100)
|
||||
@@ -712,9 +700,10 @@ void PoolMgr::LoadFromDB()
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pool_id > max_pool_id)
|
||||
auto it = mPoolTemplate.find(pool_id);
|
||||
if (it == mPoolTemplate.end())
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_gameobject` pool id (%u) is out of range compared to max pool id in `pool_template`, skipped.", pool_id);
|
||||
TC_LOG_ERROR("sql.sql", "`pool_gameobject` pool id (%u) is not in `pool_template`, skipped.", pool_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -764,15 +753,21 @@ void PoolMgr::LoadFromDB()
|
||||
uint32 mother_pool_id = fields[1].GetUInt32();
|
||||
float chance = fields[2].GetFloat();
|
||||
|
||||
if (mother_pool_id > max_pool_id)
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_pool` mother_pool id (%u) is out of range compared to max pool id in `pool_template`, skipped.", mother_pool_id);
|
||||
continue;
|
||||
auto it = mPoolTemplate.find(mother_pool_id);
|
||||
if (it == mPoolTemplate.end())
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_pool` mother_pool id (%u) is not in `pool_template`, skipped.", mother_pool_id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (child_pool_id > max_pool_id)
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_pool` included pool_id (%u) is out of range compared to max pool id in `pool_template`, skipped.", child_pool_id);
|
||||
continue;
|
||||
auto it = mPoolTemplate.find(child_pool_id);
|
||||
if (it == mPoolTemplate.end())
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_pool` included pool_id (%u) is not in `pool_template`, skipped.", child_pool_id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (mother_pool_id == child_pool_id)
|
||||
{
|
||||
@@ -797,10 +792,11 @@ void PoolMgr::LoadFromDB()
|
||||
while (result->NextRow());
|
||||
|
||||
// Now check for circular reference
|
||||
for (uint32 i=0; i < max_pool_id; ++i)
|
||||
// All pool_ids are in pool_template
|
||||
for (auto const it : mPoolTemplate)
|
||||
{
|
||||
std::set<uint32> checkedPools;
|
||||
for (SearchMap::iterator poolItr = mPoolSearchMap.find(i); poolItr != mPoolSearchMap.end(); poolItr = mPoolSearchMap.find(poolItr->second))
|
||||
for (SearchMap::iterator poolItr = mPoolSearchMap.find(it.first); poolItr != mPoolSearchMap.end(); poolItr = mPoolSearchMap.find(poolItr->second))
|
||||
{
|
||||
checkedPools.insert(poolItr->first);
|
||||
if (checkedPools.find(poolItr->second) != checkedPools.end())
|
||||
@@ -863,9 +859,10 @@ void PoolMgr::LoadFromDB()
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pool_id > max_pool_id)
|
||||
auto it = mPoolTemplate.find(pool_id);
|
||||
if (it == mPoolTemplate.end())
|
||||
{
|
||||
TC_LOG_ERROR("sql.sql", "`pool_quest` pool id (%u) is out of range compared to max pool id in `pool_template`, skipped.", pool_id);
|
||||
TC_LOG_ERROR("sql.sql", "`pool_quest` pool id (%u) is not in `pool_template`, skipped.", pool_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -971,10 +968,10 @@ void PoolMgr::SaveQuestsToDB()
|
||||
|
||||
for (PoolGroupQuestMap::iterator itr = mPoolQuestGroups.begin(); itr != mPoolQuestGroups.end(); ++itr)
|
||||
{
|
||||
if (itr->isEmpty())
|
||||
if (itr->second.isEmpty())
|
||||
continue;
|
||||
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_QUEST_POOL_SAVE);
|
||||
stmt->setUInt32(0, itr->GetPoolId());
|
||||
stmt->setUInt32(0, itr->second.GetPoolId());
|
||||
trans->Append(stmt);
|
||||
}
|
||||
|
||||
@@ -996,12 +993,12 @@ void PoolMgr::ChangeDailyQuests()
|
||||
{
|
||||
for (PoolGroupQuestMap::iterator itr = mPoolQuestGroups.begin(); itr != mPoolQuestGroups.end(); ++itr)
|
||||
{
|
||||
if (Quest const* quest = sObjectMgr->GetQuestTemplate(itr->GetFirstEqualChancedObjectId()))
|
||||
if (Quest const* quest = sObjectMgr->GetQuestTemplate(itr->second.GetFirstEqualChancedObjectId()))
|
||||
{
|
||||
if (quest->IsWeekly())
|
||||
continue;
|
||||
|
||||
UpdatePool<Quest>(itr->GetPoolId(), 1); // anything non-zero means don't load from db
|
||||
UpdatePool<Quest>(itr->second.GetPoolId(), 1); // anything non-zero means don't load from db
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1012,12 +1009,12 @@ void PoolMgr::ChangeWeeklyQuests()
|
||||
{
|
||||
for (PoolGroupQuestMap::iterator itr = mPoolQuestGroups.begin(); itr != mPoolQuestGroups.end(); ++itr)
|
||||
{
|
||||
if (Quest const* quest = sObjectMgr->GetQuestTemplate(itr->GetFirstEqualChancedObjectId()))
|
||||
if (Quest const* quest = sObjectMgr->GetQuestTemplate(itr->second.GetFirstEqualChancedObjectId()))
|
||||
{
|
||||
if (quest->IsDaily())
|
||||
continue;
|
||||
|
||||
UpdatePool<Quest>(itr->GetPoolId(), 1);
|
||||
UpdatePool<Quest>(itr->second.GetPoolId(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1029,8 +1026,9 @@ void PoolMgr::ChangeWeeklyQuests()
|
||||
template<>
|
||||
void PoolMgr::SpawnPool<Creature>(uint32 pool_id, uint64 db_guid)
|
||||
{
|
||||
if (!mPoolCreatureGroups[pool_id].isEmpty())
|
||||
mPoolCreatureGroups[pool_id].SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, db_guid);
|
||||
auto it = mPoolCreatureGroups.find(pool_id);
|
||||
if (it != mPoolCreatureGroups.end() && !it->second.isEmpty())
|
||||
it->second.SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, db_guid);
|
||||
}
|
||||
|
||||
// Call to spawn a pool, if cache if true the method will spawn only if cached entry is different
|
||||
@@ -1038,8 +1036,9 @@ void PoolMgr::SpawnPool<Creature>(uint32 pool_id, uint64 db_guid)
|
||||
template<>
|
||||
void PoolMgr::SpawnPool<GameObject>(uint32 pool_id, uint64 db_guid)
|
||||
{
|
||||
if (!mPoolGameobjectGroups[pool_id].isEmpty())
|
||||
mPoolGameobjectGroups[pool_id].SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, db_guid);
|
||||
auto it = mPoolGameobjectGroups.find(pool_id);
|
||||
if (it != mPoolGameobjectGroups.end() && !it->second.isEmpty())
|
||||
it->second.SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, db_guid);
|
||||
}
|
||||
|
||||
// Call to spawn a pool, if cache if true the method will spawn only if cached entry is different
|
||||
@@ -1047,16 +1046,18 @@ void PoolMgr::SpawnPool<GameObject>(uint32 pool_id, uint64 db_guid)
|
||||
template<>
|
||||
void PoolMgr::SpawnPool<Pool>(uint32 pool_id, uint64 sub_pool_id)
|
||||
{
|
||||
if (!mPoolPoolGroups[pool_id].isEmpty())
|
||||
mPoolPoolGroups[pool_id].SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, sub_pool_id);
|
||||
auto it = mPoolPoolGroups.find(pool_id);
|
||||
if (it != mPoolPoolGroups.end() && !it->second.isEmpty())
|
||||
it->second.SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, sub_pool_id);
|
||||
}
|
||||
|
||||
// Call to spawn a pool
|
||||
template<>
|
||||
void PoolMgr::SpawnPool<Quest>(uint32 pool_id, uint64 quest_id)
|
||||
{
|
||||
if (!mPoolQuestGroups[pool_id].isEmpty())
|
||||
mPoolQuestGroups[pool_id].SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, quest_id);
|
||||
auto it = mPoolQuestGroups.find(pool_id);
|
||||
if (it != mPoolQuestGroups.end() && !it->second.isEmpty())
|
||||
it->second.SpawnObject(mSpawnedData, mPoolTemplate[pool_id].MaxLimit, quest_id);
|
||||
}
|
||||
|
||||
void PoolMgr::SpawnPool(uint32 pool_id)
|
||||
@@ -1070,27 +1071,52 @@ void PoolMgr::SpawnPool(uint32 pool_id)
|
||||
// Call to despawn a pool, all gameobjects/creatures in this pool are removed
|
||||
void PoolMgr::DespawnPool(uint32 pool_id)
|
||||
{
|
||||
if (!mPoolCreatureGroups[pool_id].isEmpty())
|
||||
mPoolCreatureGroups[pool_id].DespawnObject(mSpawnedData);
|
||||
|
||||
if (!mPoolGameobjectGroups[pool_id].isEmpty())
|
||||
mPoolGameobjectGroups[pool_id].DespawnObject(mSpawnedData);
|
||||
|
||||
if (!mPoolPoolGroups[pool_id].isEmpty())
|
||||
mPoolPoolGroups[pool_id].DespawnObject(mSpawnedData);
|
||||
|
||||
if (!mPoolQuestGroups[pool_id].isEmpty())
|
||||
mPoolQuestGroups[pool_id].DespawnObject(mSpawnedData);
|
||||
{
|
||||
auto it = mPoolCreatureGroups.find(pool_id);
|
||||
if (it != mPoolCreatureGroups.end() && !it->second.isEmpty())
|
||||
it->second.DespawnObject(mSpawnedData);
|
||||
}
|
||||
{
|
||||
auto it = mPoolGameobjectGroups.find(pool_id);
|
||||
if (it != mPoolGameobjectGroups.end() && !it->second.isEmpty())
|
||||
it->second.DespawnObject(mSpawnedData);
|
||||
}
|
||||
{
|
||||
auto it = mPoolPoolGroups.find(pool_id);
|
||||
if (it != mPoolPoolGroups.end() && !it->second.isEmpty())
|
||||
it->second.DespawnObject(mSpawnedData);
|
||||
}
|
||||
{
|
||||
auto it = mPoolQuestGroups.find(pool_id);
|
||||
if (it != mPoolQuestGroups.end() && !it->second.isEmpty())
|
||||
it->second.DespawnObject(mSpawnedData);
|
||||
}
|
||||
}
|
||||
|
||||
// Method that check chance integrity of the creatures and gameobjects in this pool
|
||||
bool PoolMgr::CheckPool(uint32 pool_id) const
|
||||
{
|
||||
return pool_id <= max_pool_id &&
|
||||
mPoolGameobjectGroups[pool_id].CheckPool() &&
|
||||
mPoolCreatureGroups[pool_id].CheckPool() &&
|
||||
mPoolPoolGroups[pool_id].CheckPool() &&
|
||||
mPoolQuestGroups[pool_id].CheckPool();
|
||||
{
|
||||
auto it = mPoolGameobjectGroups.find(pool_id);
|
||||
if (it != mPoolGameobjectGroups.end() && !it->second.CheckPool())
|
||||
return false;
|
||||
}
|
||||
{
|
||||
auto it = mPoolCreatureGroups.find(pool_id);
|
||||
if (it != mPoolCreatureGroups.end() && !it->second.CheckPool())
|
||||
return false;
|
||||
}
|
||||
{
|
||||
auto it = mPoolPoolGroups.find(pool_id);
|
||||
if (it != mPoolPoolGroups.end() && !it->second.CheckPool())
|
||||
return false;
|
||||
}
|
||||
{
|
||||
auto it = mPoolQuestGroups.find(pool_id);
|
||||
if (it != mPoolQuestGroups.end() && !it->second.CheckPool())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Call to update the pool when a gameobject/creature part of pool [pool_id] is ready to respawn
|
||||
|
||||
@@ -144,12 +144,11 @@ class TC_GAME_API PoolMgr
|
||||
template<typename T>
|
||||
void SpawnPool(uint32 pool_id, uint64 db_guid_or_pool_id);
|
||||
|
||||
uint32 max_pool_id;
|
||||
typedef std::vector<PoolTemplateData> PoolTemplateDataMap;
|
||||
typedef std::vector<PoolGroup<Creature> > PoolGroupCreatureMap;
|
||||
typedef std::vector<PoolGroup<GameObject> > PoolGroupGameObjectMap;
|
||||
typedef std::vector<PoolGroup<Pool> > PoolGroupPoolMap;
|
||||
typedef std::vector<PoolGroup<Quest> > PoolGroupQuestMap;
|
||||
typedef std::unordered_map<uint32, PoolTemplateData> PoolTemplateDataMap;
|
||||
typedef std::unordered_map<uint32, PoolGroup<Creature>> PoolGroupCreatureMap;
|
||||
typedef std::unordered_map<uint32, PoolGroup<GameObject>> PoolGroupGameObjectMap;
|
||||
typedef std::unordered_map<uint32, PoolGroup<Pool>> PoolGroupPoolMap;
|
||||
typedef std::unordered_map<uint32, PoolGroup<Quest>> PoolGroupQuestMap;
|
||||
typedef std::pair<uint64, uint32> SearchPair;
|
||||
typedef std::map<uint64, uint32> SearchMap;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user