fix(Core/Pooling): Fixed less and less objects from pools being spawned the longer the server is running (#5572)

This commit is contained in:
UltraNix
2021-05-08 20:39:09 +02:00
committed by GitHub
parent 75c75a40d4
commit 44babc3c3a
26 changed files with 136 additions and 105 deletions

View File

@@ -64,33 +64,63 @@ namespace acore
namespace Containers
{
template<class T>
void RandomResizeList(std::list<T>& list, uint32 size)
// replace with std::size in C++17
template<class C>
constexpr inline std::size_t Size(C const& container)
{
size_t list_size = list.size();
while (list_size > size)
{
typename std::list<T>::iterator itr = list.begin();
std::advance(itr, urand(0, list_size - 1));
list.erase(itr);
--list_size;
}
return container.size();
}
template<class T, class Predicate>
void RandomResizeList(std::list<T>& list, Predicate& predicate, uint32 size)
template<class T, std::size_t size>
constexpr inline std::size_t Size(T const(&)[size]) noexcept
{
return size;
}
// resizes <container> to have at most <requestedSize> elements
// if it has more than <requestedSize> elements, the elements to keep are selected randomly
template<class C>
void RandomResize(C& container, std::size_t requestedSize)
{
static_assert(std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<typename C::iterator>::iterator_category>::value, "Invalid container passed to acore::Containers::RandomResize");
if (Size(container) <= requestedSize)
{
return;
}
auto keepIt = std::begin(container), curIt = std::begin(container);
uint32 elementsToKeep = requestedSize, elementsToProcess = Size(container);
while (elementsToProcess)
{
// this element has chance (elementsToKeep / elementsToProcess) of being kept
if (urand(1, elementsToProcess) <= elementsToKeep)
{
if (keepIt != curIt)
*keepIt = std::move(*curIt);
++keepIt;
--elementsToKeep;
}
++curIt;
--elementsToProcess;
}
container.erase(keepIt, std::end(container));
}
template<class C, class Predicate>
void RandomResize(C& container, Predicate&& predicate, std::size_t requestedSize)
{
//! First use predicate filter
std::list<T> listCopy;
for (typename std::list<T>::iterator itr = list.begin(); itr != list.end(); ++itr)
if (predicate(*itr))
listCopy.push_back(*itr);
C containerCopy;
std::copy_if(std::begin(container), std::end(container), std::inserter(containerCopy, std::end(containerCopy)), predicate);
if (size)
RandomResizeList(listCopy, size);
if (requestedSize)
{
RandomResize(containerCopy, requestedSize);
}
list = listCopy;
container = std::move(containerCopy);
}
/* Select a random element from a container. Note: make sure you explicitly empty check the container */

View File

@@ -261,7 +261,7 @@ public:
targetList.reverse();
if (targetType == SELECT_TARGET_RANDOM)
acore::Containers::RandomResizeList(targetList, maxTargets);
acore::Containers::RandomResize(targetList, maxTargets);
else
targetList.resize(maxTargets);
}

View File

@@ -107,7 +107,7 @@ public:
// We need to use a copy of SummonList here, otherwise original SummonList would be modified
StorageType listCopy = storage_;
acore::Containers::RandomResizeList<ObjectGuid, Predicate>(listCopy, predicate, max);
acore::Containers::RandomResize(listCopy, predicate, max);
for (StorageType::iterator i = listCopy.begin(); i != listCopy.end(); ++i)
{
Creature* summon = ObjectAccessor::GetCreature(*me, *i);

View File

@@ -734,7 +734,7 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
caster = unit->SummonTrigger(unit->GetPositionX(), unit->GetPositionY(), unit->GetPositionZ(), unit->GetOrientation(), 5000);
if (e.action.cast.targetsLimit > 0 && targets->size() > e.action.cast.targetsLimit)
acore::Containers::RandomResizeList(*targets, e.action.cast.targetsLimit);
acore::Containers::RandomResize(*targets, e.action.cast.targetsLimit);
for (ObjectList::const_iterator itr = targets->begin(); itr != targets->end(); ++itr)
{
@@ -793,7 +793,7 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
break;
if (e.action.cast.targetsLimit > 0 && targets->size() > e.action.cast.targetsLimit)
acore::Containers::RandomResizeList(*targets, e.action.cast.targetsLimit);
acore::Containers::RandomResize(*targets, e.action.cast.targetsLimit);
for (ObjectList::const_iterator itr = targets->begin(); itr != targets->end(); ++itr)
{
@@ -3680,7 +3680,7 @@ ObjectList* SmartScript::GetTargets(SmartScriptHolder const& e, Unit* invoker /*
l->push_back(*itr);
if (e.target.playerRange.maxCount > 0)
acore::Containers::RandomResizeList(*l, e.target.playerRange.maxCount);
acore::Containers::RandomResize(*l, e.target.playerRange.maxCount);
}
delete units;
@@ -3800,7 +3800,7 @@ ObjectList* SmartScript::GetTargets(SmartScriptHolder const& e, Unit* invoker /*
l->push_back(*itr);
if (e.target.o > 0)
acore::Containers::RandomResizeList(*l, e.target.o);
acore::Containers::RandomResize(*l, e.target.o);
delete units;
break;
@@ -3842,7 +3842,7 @@ ObjectList* SmartScript::GetTargets(SmartScriptHolder const& e, Unit* invoker /*
}
if (e.target.roleSelection.resize > 0)
acore::Containers::RandomResizeList(*l, e.target.roleSelection.resize);
acore::Containers::RandomResize(*l, e.target.roleSelection.resize);
delete units;
break;

View File

@@ -141,34 +141,6 @@ bool PoolGroup<T>::CheckPool() const
return true;
}
template <class T>
PoolObject* PoolGroup<T>::RollOne(ActivePoolData& spawns, uint32 triggerFrom)
{
if (!ExplicitlyChanced.empty())
{
float roll = (float)rand_chance();
for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
{
roll -= ExplicitlyChanced[i].chance;
// Triggering object is marked as spawned at this time and can be also rolled (respawn case)
// so this need explicit check for this case
if (roll < 0 && (ExplicitlyChanced[i].guid == triggerFrom || !spawns.IsActiveObject<T>(ExplicitlyChanced[i].guid)))
return &ExplicitlyChanced[i];
}
}
if (!EqualChanced.empty())
{
int32 index = irand(0, EqualChanced.size() - 1);
// Triggering object is marked as spawned at this time and can be also rolled (respawn case)
// so this need explicit check for this case
if (EqualChanced[index].guid == triggerFrom || !spawns.IsActiveObject<T>(EqualChanced[index].guid))
return &EqualChanced[index];
}
return nullptr;
}
// Main method to despawn a creature or gameobject in a pool
// If no guid is passed, the pool is just removed (event end case)
// If guid is filled, cache will be used and no removal will occur, it just fill the cache
@@ -321,40 +293,70 @@ void PoolGroup<Pool>::RemoveOneRelation(uint32 child_pool_id)
template <class T>
void PoolGroup<T>::SpawnObject(ActivePoolData& spawns, uint32 limit, uint32 triggerFrom)
{
uint32 lastDespawned = 0;
int count = limit - spawns.GetActiveObjectCount(poolId);
// If triggered from some object respawn this object is still marked as spawned
// and also counted into m_SpawnedPoolAmount so we need increase count to be
// spawned by 1
if (triggerFrom)
++count;
// This will try to spawn the rest of pool, not guaranteed
for (int i = 0; i < count; ++i)
{
PoolObject* obj = RollOne(spawns, triggerFrom);
if (!obj)
continue;
if (obj->guid == lastDespawned)
continue;
++count;
}
if (obj->guid == triggerFrom)
{
ReSpawn1Object(obj);
triggerFrom = 0;
continue;
}
spawns.ActivateObject<T>(obj->guid, poolId);
Spawn1Object(obj);
if (count > 0)
{
PoolObjectList rolledObjects;
rolledObjects.reserve(count);
if (triggerFrom)
// roll objects to be spawned
if (!ExplicitlyChanced.empty())
{
// One spawn one despawn no count increase
DespawnObject(spawns, triggerFrom);
lastDespawned = triggerFrom;
triggerFrom = 0;
float roll = (float)rand_chance();
for (PoolObject& obj : ExplicitlyChanced)
{
roll -= obj.chance;
// Triggering object is marked as spawned at this time and can be also rolled (respawn case)
// so this need explicit check for this case
if (roll < 0 && (/*obj.guid == triggerFrom ||*/ !spawns.IsActiveObject<T>(obj.guid)))
{
rolledObjects.push_back(obj);
break;
}
}
}
if (!EqualChanced.empty() && rolledObjects.empty())
{
std::copy_if(EqualChanced.begin(), EqualChanced.end(), std::back_inserter(rolledObjects), [/*triggerFrom, */&spawns](PoolObject const& object)
{
return /*object.guid == triggerFrom ||*/ !spawns.IsActiveObject<T>(object.guid);
});
acore::Containers::RandomResize(rolledObjects, count);
}
// try to spawn rolled objects
for (PoolObject& obj : rolledObjects)
{
if (obj.guid == triggerFrom)
{
ReSpawn1Object(&obj);
triggerFrom = 0;
}
else
{
spawns.ActivateObject<T>(obj.guid, poolId);
Spawn1Object(&obj);
}
}
}
// One spawn one despawn no count increase
if (triggerFrom)
{
DespawnObject(spawns, triggerFrom);
}
}

View File

@@ -64,7 +64,6 @@ public:
bool isEmpty() const { return ExplicitlyChanced.empty() && EqualChanced.empty(); }
void AddEntry(PoolObject& poolitem, uint32 maxentries);
bool CheckPool() const;
PoolObject* RollOne(ActivePoolData& spawns, uint32 triggerFrom);
void DespawnObject(ActivePoolData& spawns, ObjectGuid::LowType guid = 0);
void Despawn1Object(ObjectGuid::LowType guid);
void SpawnObject(ActivePoolData& spawns, uint32 limit, uint32 triggerFrom);

View File

@@ -1197,7 +1197,7 @@ void Spell::SelectImplicitConeTargets(SpellEffIndex effIndex, SpellImplicitTarge
if ((*j)->IsAffectedOnSpell(m_spellInfo))
maxTargets += (*j)->GetAmount();
acore::Containers::RandomResizeList(targets, maxTargets);
acore::Containers::RandomResize(targets, maxTargets);
}
for (std::list<WorldObject*>::iterator itr = targets.begin(); itr != targets.end(); ++itr)
@@ -1280,7 +1280,7 @@ void Spell::SelectImplicitAreaTargets(SpellEffIndex effIndex, SpellImplicitTarge
if ((*j)->IsAffectedOnSpell(m_spellInfo))
maxTargets += (*j)->GetAmount();
acore::Containers::RandomResizeList(targets, maxTargets);
acore::Containers::RandomResize(targets, maxTargets);
}
for (std::list<WorldObject*>::iterator itr = targets.begin(); itr != targets.end(); ++itr)

View File

@@ -104,7 +104,7 @@ public:
std::list<uint32> helpersList;
for (uint8 i = 0; i < MAX_HELPERS_COUNT; ++i)
helpersList.push_back(helpersEntries[i]);
acore::Containers::RandomResizeList(helpersList, MAX_ACTIVE_HELPERS);
acore::Containers::RandomResize(helpersList, MAX_ACTIVE_HELPERS);
uint8 j = 0;
for (std::list<uint32>::const_iterator itr = helpersList.begin(); itr != helpersList.end(); ++itr, ++j)

View File

@@ -626,7 +626,7 @@ public:
void FilterTargets(std::list<WorldObject*>& targets)
{
targets.remove_if(SpectralBlastCheck(GetCaster()->GetVictim()));
acore::Containers::RandomResizeList(targets, 1);
acore::Containers::RandomResize(targets, 1);
}
void HandleDummy(SpellEffIndex effIndex)

View File

@@ -394,7 +394,7 @@ public:
void FilterTargets(std::list<WorldObject*>& targets)
{
acore::Containers::RandomResizeList(targets, GetCaster()->GetAI()->GetData(DATA_NEGATIVE_ENERGY_TARGETS));
acore::Containers::RandomResize(targets, GetCaster()->GetAI()->GetData(DATA_NEGATIVE_ENERGY_TARGETS));
}
void HandleScriptEffect(SpellEffIndex effIndex)

View File

@@ -137,7 +137,7 @@ public:
targetList.push_back((*itr)->getTarget());
}
acore::Containers::RandomResizeList(targetList, 5);
acore::Containers::RandomResize(targetList, 5);
for (std::list<Unit*>::iterator itr = targetList.begin(); itr != targetList.end(); ++itr)
DoCast(*itr, SPELL_DRAIN_MANA);

View File

@@ -420,7 +420,7 @@ public:
if (Player* p = itr->GetSource())
if (p->IsAlive() && p != me->GetVictim() && p->GetGUID() != _offtankGUID && !p->IsGameMaster() && p->GetDistance(me) < 100.0f && !p->HasAura(SPELL_UNCONTROLLABLE_FRENZY))
myList.push_back(p);
acore::Containers::RandomResizeList(myList, Is25ManRaid() ? 3 : 2);
acore::Containers::RandomResize(myList, Is25ManRaid() ? 3 : 2);
if (myList.size() > 1)
{
Talk(SAY_PACT_OF_THE_DARKFALLEN);
@@ -446,7 +446,7 @@ public:
if (!myList.empty())
{
acore::Containers::RandomResizeList(myList, 1);
acore::Containers::RandomResize(myList, 1);
Player* target = myList.front();
Talk(EMOTE_SWARMING_SHADOWS, target);
Talk(SAY_SWARMING_SHADOWS);
@@ -468,7 +468,7 @@ public:
if (p->IsAlive() && p != me->GetVictim() && p->GetGUID() != _offtankGUID && !p->IsGameMaster() && !p->HasAura(SPELL_PACT_OF_THE_DARKFALLEN) && !p->HasAura(SPELL_UNCONTROLLABLE_FRENZY))
myList.push_back(p);
acore::Containers::RandomResizeList<Player*>(myList, uint32(Is25ManRaid() ? 4 : 2));
acore::Containers::RandomResize(myList, uint32(Is25ManRaid() ? 4 : 2));
for (std::list<Player*>::iterator itr = myList.begin(); itr != myList.end(); ++itr)
me->CastSpell(*itr, SPELL_TWILIGHT_BLOODBOLT, false);
me->CastSpell(me, SPELL_TWILIGHT_BLOODBOLT_TARGET, false);
@@ -723,7 +723,7 @@ public:
{
uint32 targetCount = (targets.size() + 2) / 3;
targets.remove_if(BloodboltHitCheck(static_cast<LanaThelAI*>(GetCaster()->GetAI())));
acore::Containers::RandomResizeList(targets, targetCount);
acore::Containers::RandomResize(targets, targetCount);
// mark targets now, effect hook has missile travel time delay (might cast next in that time)
for (std::list<WorldObject*>::const_iterator itr = targets.begin(); itr != targets.end(); ++itr)
GetCaster()->GetAI()->SetGUID((*itr)->GetGUID(), GUID_BLOODBOLT);

View File

@@ -1315,7 +1315,7 @@ public:
targets.push_back(target);
}
else
acore::Containers::RandomResizeList(targets, 3);
acore::Containers::RandomResize(targets, 3);
}
void Register() override

View File

@@ -2208,7 +2208,7 @@ public:
void SelectTarget(std::list<WorldObject*>& targets)
{
targets.remove_if(IgbExplosionCheck(GetCaster()));
acore::Containers::RandomResizeList(targets, 1);
acore::Containers::RandomResize(targets, 1);
}
void Register() override

View File

@@ -1257,7 +1257,7 @@ public:
}
targets.remove_if(acore::UnitAuraCheck(true, sSpellMgr->GetSpellIdForDifficulty(SPELL_UNBOUND_PLAGUE, GetCaster())));
acore::Containers::RandomResizeList(targets, 1);
acore::Containers::RandomResize(targets, 1);
}
void HandleScript(SpellEffIndex /*effIndex*/)

View File

@@ -908,10 +908,10 @@ public:
uint32 maxSize = uint32(GetCaster()->GetMap()->GetSpawnMode() & 1 ? 3 : 1);
healList.remove_if(UnchainedMagicTargetSelector(false));
if (healList.size() > maxSize)
acore::Containers::RandomResizeList(healList, maxSize);
acore::Containers::RandomResize(healList, maxSize);
dpsList.remove_if(UnchainedMagicTargetSelector(true));
if (dpsList.size() > maxSize)
acore::Containers::RandomResizeList(dpsList, maxSize);
acore::Containers::RandomResize(dpsList, maxSize);
unitList.splice(unitList.begin(), healList);
unitList.splice(unitList.begin(), dpsList);
}

View File

@@ -1368,7 +1368,7 @@ public:
return;
summoners = list_copy;
}
acore::Containers::RandomResizeList(summoners, 2);
acore::Containers::RandomResize(summoners, 2);
for (uint32 i = 0; i < 3; ++i)
caster->CastSpell(summoners.front(), SPELL_SUMMON_SUPPRESSER, true);

View File

@@ -2159,7 +2159,7 @@ public:
void RemoveAliveTarget(std::list<WorldObject*>& targets)
{
targets.remove_if(AliveCheck());
acore::Containers::RandomResizeList(targets, 2);
acore::Containers::RandomResize(targets, 2);
}
void Land(SpellEffIndex /*effIndex*/)

View File

@@ -426,7 +426,7 @@ public:
targets.push_back(itr->GetSource());
targets.remove_if(acore::ObjectTypeIdCheck(TYPEID_PLAYER, false));
targets.remove_if(acore::UnitAuraCheck(true, SPELL_FLASH_FREEZE_TRAPPED_PLAYER));
acore::Containers::RandomResizeList(targets, (RAID_MODE(2,3)));
acore::Containers::RandomResize(targets, (RAID_MODE(2,3)));
for (std::list<Unit*>::const_iterator itr = targets.begin(); itr != targets.end(); ++itr)
{
float prevZ = (*itr)->GetPositionZ();
@@ -1273,7 +1273,7 @@ public:
{
targets.remove_if(acore::ObjectTypeIdCheck(TYPEID_PLAYER, false));
targets.remove_if(acore::UnitAuraCheck(true, SPELL_FLASH_FREEZE_TRAPPED_PLAYER));
acore::Containers::RandomResizeList(targets, 1);
acore::Containers::RandomResize(targets, 1);
}
void Register() override

View File

@@ -1193,7 +1193,7 @@ public:
void FilterTargets(std::list<WorldObject*>& targets)
{
targets.remove_if(GhoulTargetCheck(GetSpellInfo()->Id == 70790));
acore::Containers::RandomResizeList(targets, 2);
acore::Containers::RandomResize(targets, 2);
}
void HandleScript(SpellEffIndex effIndex)

View File

@@ -199,7 +199,7 @@ public:
void FilterTargets(std::list<WorldObject*>& unitList)
{
acore::Containers::RandomResizeList(unitList, 1);
acore::Containers::RandomResize(unitList, 1);
}
void HandleDummy(SpellEffIndex effIndex)

View File

@@ -241,7 +241,7 @@ public:
void FilterTargets(std::list<WorldObject*>& unitList)
{
acore::Containers::RandomResizeList(unitList, 1);
acore::Containers::RandomResize(unitList, 1);
}
void HandleScriptEffect(SpellEffIndex effIndex)

View File

@@ -993,7 +993,7 @@ public:
targetList.push_back(target);
}
acore::Containers::RandomResizeList(targetList, 5);
acore::Containers::RandomResize(targetList, 5);
for (std::list<Unit*>::const_iterator itr = targetList.begin(); itr != targetList.end(); ++itr)
GetCaster()->CastSpell(*itr, SPELL_NETHER_BEAM_DAMAGE, true);
}

View File

@@ -1050,7 +1050,7 @@ public:
void FilterTargets(std::list<WorldObject*>& targets)
{
acore::Containers::RandomResizeList(targets, 2);
acore::Containers::RandomResize(targets, 2);
}
void HandleDummy(SpellEffIndex /*effIndex*/)

View File

@@ -768,7 +768,7 @@ public:
void FilterTargets(std::list<WorldObject*>& targets)
{
targets.remove(GetCaster());
acore::Containers::RandomResizeList(targets, _count);
acore::Containers::RandomResize(targets, _count);
}
void Register() override

View File

@@ -652,7 +652,7 @@ public:
void CountTargets(std::list<WorldObject*>& targetList)
{
acore::Containers::RandomResizeList(targetList, GetSpellValue()->MaxAffectedTargets);
acore::Containers::RandomResize(targetList, GetSpellValue()->MaxAffectedTargets);
_targetCount = targetList.size();
}