AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
PoolGroup< T > Class Template Reference

#include "PoolMgr.h"

Public Member Functions

 PoolGroup ()
 
void SetPoolId (uint32 pool_id)
 
 ~PoolGroup ()
 
bool IsEmpty () const
 
void AddEntry (PoolObject &poolitem, uint32 maxentries)
 
bool CheckPool () const
 
void DespawnObject (ActivePoolData &spawns, ObjectGuid::LowType guid=0)
 
void Despawn1Object (ObjectGuid::LowType guid)
 
void SpawnObject (ActivePoolData &spawns, uint32 limit, uint32 triggerFrom)
 
void Spawn1Object (PoolObject *obj)
 
void ReSpawn1Object (PoolObject *obj)
 
void RemoveOneRelation (uint32 child_pool_id)
 
uint32 GetFirstEqualChancedObjectId ()
 
uint32 GetPoolId () const
 
void Despawn1Object (ObjectGuid::LowType guid)
 
void Despawn1Object (ObjectGuid::LowType guid)
 
void Despawn1Object (uint32 child_pool_id)
 
void Despawn1Object (uint32 quest_id)
 
void RemoveOneRelation (uint32 child_pool_id)
 
void Spawn1Object (PoolObject *obj)
 
void Spawn1Object (PoolObject *obj)
 
void Spawn1Object (PoolObject *obj)
 
void Spawn1Object (PoolObject *obj)
 
void SpawnObject (ActivePoolData &spawns, uint32 limit, uint32 triggerFrom)
 
void ReSpawn1Object (PoolObject *obj)
 
void ReSpawn1Object (PoolObject *obj)
 
void ReSpawn1Object (PoolObject *)
 
void ReSpawn1Object (PoolObject *)
 

Private Types

typedef std::vector< PoolObjectPoolObjectList
 

Private Attributes

uint32 poolId
 
PoolObjectList ExplicitlyChanced
 
PoolObjectList EqualChanced
 

Detailed Description

template<class T>
class PoolGroup< T >

Member Typedef Documentation

◆ PoolObjectList

template<class T >
typedef std::vector<PoolObject> PoolGroup< T >::PoolObjectList
private

Constructor & Destructor Documentation

◆ PoolGroup()

template<class T >
PoolGroup< T >::PoolGroup ( )
inlineexplicit
72: poolId(0) { }
uint32 poolId
Definition: PoolMgr.h:93

◆ ~PoolGroup()

template<class T >
PoolGroup< T >::~PoolGroup ( )
inline
74{};

Member Function Documentation

◆ AddEntry()

template<class T >
void PoolGroup< T >::AddEntry ( PoolObject poolitem,
uint32  maxentries 
)
134{
135 if (poolitem.chance != 0 && maxentries == 1)
136 ExplicitlyChanced.push_back(poolitem);
137 else
138 EqualChanced.push_back(poolitem);
139}
float chance
Definition: PoolMgr.h:34
PoolObjectList ExplicitlyChanced
Definition: PoolMgr.h:94
PoolObjectList EqualChanced
Definition: PoolMgr.h:95

References PoolObject::chance.

Referenced by PoolMgr::LoadFromDB().

◆ CheckPool()

template<class T >
bool PoolGroup< T >::CheckPool
144{
145 if (EqualChanced.empty())
146 {
147 float chance = 0;
148 for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
149 chance += ExplicitlyChanced[i].chance;
150 if (chance != 100 && chance != 0)
151 return false;
152 }
153 return true;
154}
std::uint32_t uint32
Definition: Define.h:107

◆ Despawn1Object() [1/5]

void PoolGroup< Creature >::Despawn1Object ( ObjectGuid::LowType  guid)
192{
193 if (CreatureData const* data = sObjectMgr->GetCreatureData(guid))
194 {
195 sObjectMgr->RemoveCreatureFromGrid(guid, data);
196
197 Map* map = sMapMgr->CreateBaseMap(data->mapid);
198 if (!map->Instanceable())
199 {
200 auto creatureBounds = map->GetCreatureBySpawnIdStore().equal_range(guid);
201 for (auto itr = creatureBounds.first; itr != creatureBounds.second;)
202 {
203 Creature* creature = itr->second;
204 ++itr;
205 creature->AddObjectToRemoveList();
206 }
207 }
208 }
209}
#define sMapMgr
Definition: MapMgr.h:219
#define sObjectMgr
Definition: ObjectMgr.h:1635
Definition: Creature.h:43
Definition: CreatureData.h:370
void AddObjectToRemoveList()
Definition: Object.cpp:2160
Definition: Map.h:311
bool Instanceable() const
Definition: Map.h:445
CreatureBySpawnIdContainer & GetCreatureBySpawnIdStore()
Definition: Map.h:518

References WorldObject::AddObjectToRemoveList(), Map::GetCreatureBySpawnIdStore(), Map::Instanceable(), sMapMgr, and sObjectMgr.

◆ Despawn1Object() [2/5]

void PoolGroup< GameObject >::Despawn1Object ( ObjectGuid::LowType  guid)
214{
215 if (GameObjectData const* data = sObjectMgr->GetGameObjectData(guid))
216 {
217 sObjectMgr->RemoveGameobjectFromGrid(guid, data);
218
219 Map* map = sMapMgr->CreateBaseMap(data->mapid);
220 if (!map->Instanceable())
221 {
222 auto gameobjectBounds = map->GetGameObjectBySpawnIdStore().equal_range(guid);
223 for (auto itr = gameobjectBounds.first; itr != gameobjectBounds.second;)
224 {
225 GameObject* go = itr->second;
226 ++itr;
228 }
229 }
230 }
231}
Definition: GameObject.h:120
Definition: GameObjectData.h:696
GameObjectBySpawnIdContainer & GetGameObjectBySpawnIdStore()
Definition: Map.h:521

References WorldObject::AddObjectToRemoveList(), Map::GetGameObjectBySpawnIdStore(), Map::Instanceable(), sMapMgr, and sObjectMgr.

◆ Despawn1Object() [3/5]

template<class T >
void PoolGroup< T >::Despawn1Object ( ObjectGuid::LowType  guid)

◆ Despawn1Object() [4/5]

void PoolGroup< Pool >::Despawn1Object ( uint32  child_pool_id)
236{
237 sPoolMgr->DespawnPool(child_pool_id);
238}
#define sPoolMgr
Definition: PoolMgr.h:163

References sPoolMgr.

◆ Despawn1Object() [5/5]

void PoolGroup< Quest >::Despawn1Object ( uint32  quest_id)
243{
244 // Creatures
245 QuestRelations* questMap = sObjectMgr->GetCreatureQuestRelationMap();
246 PooledQuestRelationBoundsNC qr = sPoolMgr->mQuestCreatureRelation.equal_range(quest_id);
247 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
248 {
249 QuestRelations::iterator qitr = questMap->find(itr->second);
250 if (qitr == questMap->end())
251 continue;
252 QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
253 for (; qitr != lastElement; ++qitr)
254 {
255 if (qitr->first == itr->second && qitr->second == itr->first)
256 {
257 questMap->erase(qitr); // iterator is now no more valid
258 break; // but we can exit loop since the element is found
259 }
260 }
261 }
262
263 // Gameobjects
264 questMap = sObjectMgr->GetGOQuestRelationMap();
265 qr = sPoolMgr->mQuestGORelation.equal_range(quest_id);
266 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
267 {
268 QuestRelations::iterator qitr = questMap->find(itr->second);
269 if (qitr == questMap->end())
270 continue;
271 QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
272 for (; qitr != lastElement; ++qitr)
273 {
274 if (qitr->first == itr->second && qitr->second == itr->first)
275 {
276 questMap->erase(qitr); // iterator is now no more valid
277 break; // but we can exit loop since the element is found
278 }
279 }
280 }
281}
std::multimap< uint32, uint32 > QuestRelations
Definition: ObjectMgr.h:524
std::pair< PooledQuestRelation::iterator, PooledQuestRelation::iterator > PooledQuestRelationBoundsNC
Definition: PoolMgr.h:100

References sObjectMgr, and sPoolMgr.

◆ DespawnObject()

template<class T >
void PoolGroup< T >::DespawnObject ( ActivePoolData spawns,
ObjectGuid::LowType  guid = 0 
)
161{
162 for (std::size_t i = 0; i < EqualChanced.size(); ++i)
163 {
164 // if spawned
165 if (spawns.IsActiveObject<T>(EqualChanced[i].guid))
166 {
167 if (!guid || EqualChanced[i].guid == guid)
168 {
170 spawns.RemoveObject<T>(EqualChanced[i].guid, poolId);
171 }
172 }
173 }
174
175 for (std::size_t i = 0; i < ExplicitlyChanced.size(); ++i)
176 {
177 // spawned
178 if (spawns.IsActiveObject<T>(ExplicitlyChanced[i].guid))
179 {
180 if (!guid || ExplicitlyChanced[i].guid == guid)
181 {
183 spawns.RemoveObject<T>(ExplicitlyChanced[i].guid, poolId);
184 }
185 }
186 }
187}
bool IsActiveObject(uint32 db_guid_or_pool_id) const
void RemoveObject(uint32 db_guid_or_pool_id, uint32 pool_id)
void Despawn1Object(ObjectGuid::LowType guid)

References ActivePoolData::IsActiveObject(), and ActivePoolData::RemoveObject().

◆ GetFirstEqualChancedObjectId()

template<class T >
uint32 PoolGroup< T >::GetFirstEqualChancedObjectId ( )
inline
86 {
87 if (EqualChanced.empty())
88 return 0;
89 return EqualChanced.front().guid;
90 }

References PoolGroup< T >::EqualChanced.

◆ GetPoolId()

template<class T >
uint32 PoolGroup< T >::GetPoolId ( ) const
inline
91{ return poolId; }

References PoolGroup< T >::poolId.

◆ IsEmpty()

template<class T >
bool PoolGroup< T >::IsEmpty ( ) const
inline

◆ RemoveOneRelation() [1/2]

void PoolGroup< Pool >::RemoveOneRelation ( uint32  child_pool_id)
286{
287 for (PoolObjectList::iterator itr = ExplicitlyChanced.begin(); itr != ExplicitlyChanced.end(); ++itr)
288 {
289 if (itr->guid == child_pool_id)
290 {
291 ExplicitlyChanced.erase(itr);
292 break;
293 }
294 }
295 for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
296 {
297 if (itr->guid == child_pool_id)
298 {
299 EqualChanced.erase(itr);
300 break;
301 }
302 }
303}

◆ RemoveOneRelation() [2/2]

template<class T >
void PoolGroup< T >::RemoveOneRelation ( uint32  child_pool_id)

◆ ReSpawn1Object() [1/5]

void PoolGroup< Pool >::ReSpawn1Object ( PoolObject )
548{
549}

◆ ReSpawn1Object() [2/5]

void PoolGroup< Quest >::ReSpawn1Object ( PoolObject )
554{
555}

◆ ReSpawn1Object() [3/5]

void PoolGroup< Creature >::ReSpawn1Object ( PoolObject obj)
532{
533 Despawn1Object(obj->guid);
534 Spawn1Object(obj);
535}
uint32 guid
Definition: PoolMgr.h:33
void Spawn1Object(PoolObject *obj)

References PoolObject::guid.

◆ ReSpawn1Object() [4/5]

void PoolGroup< GameObject >::ReSpawn1Object ( PoolObject obj)
540{
541 Despawn1Object(obj->guid);
542 Spawn1Object(obj);
543}

References PoolObject::guid.

◆ ReSpawn1Object() [5/5]

template<class T >
void PoolGroup< T >::ReSpawn1Object ( PoolObject obj)

◆ SetPoolId()

template<class T >
void PoolGroup< T >::SetPoolId ( uint32  pool_id)
inline
73{ poolId = pool_id; }

References PoolGroup< T >::poolId.

Referenced by PoolMgr::LoadFromDB().

◆ Spawn1Object() [1/5]

void PoolGroup< Creature >::Spawn1Object ( PoolObject obj)
378{
379 if (CreatureData const* data = sObjectMgr->GetCreatureData(obj->guid))
380 {
381 sObjectMgr->AddCreatureToGrid(obj->guid, data);
382
383 // Spawn if necessary (loaded grids only)
384 Map* map = sMapMgr->CreateBaseMap(data->mapid);
385 // We use spawn coords to spawn
386 if (!map->Instanceable() && map->IsGridLoaded(data->posX, data->posY))
387 {
388 Creature* creature = new Creature;
389 //LOG_DEBUG("pool", "Spawning creature {}", guid);
390 if (!creature->LoadCreatureFromDB(obj->guid, map))
391 {
392 delete creature;
393 return;
394 }
395 }
396 }
397}
bool LoadCreatureFromDB(ObjectGuid::LowType guid, Map *map, bool addToMap=true, bool allowDuplicate=false)
Definition: Creature.cpp:1692
bool IsGridLoaded(float x, float y) const
Definition: Map.h:367

References PoolObject::guid, Map::Instanceable(), Map::IsGridLoaded(), Creature::LoadCreatureFromDB(), sMapMgr, and sObjectMgr.

◆ Spawn1Object() [2/5]

void PoolGroup< GameObject >::Spawn1Object ( PoolObject obj)
402{
403 if (GameObjectData const* data = sObjectMgr->GetGameObjectData(obj->guid))
404 {
405 sObjectMgr->AddGameobjectToGrid(obj->guid, data);
406 // Spawn if necessary (loaded grids only)
407 // this base map checked as non-instanced and then only existed
408 Map* map = sMapMgr->CreateBaseMap(data->mapid);
409 // We use current coords to unspawn, not spawn coords since creature can have changed grid
410 if (!map->Instanceable() && map->IsGridLoaded(data->posX, data->posY))
411 {
412 GameObject* pGameobject = sObjectMgr->IsGameObjectStaticTransport(data->id) ? new StaticTransport() : new GameObject();
413 //LOG_DEBUG("pool", "Spawning gameobject {}", guid);
414 if (!pGameobject->LoadGameObjectFromDB(obj->guid, map, false))
415 {
416 delete pGameobject;
417 return;
418 }
419 else
420 {
421 if (pGameobject->isSpawnedByDefault())
422 map->AddToMap(pGameobject);
423 }
424 }
425 }
426}
bool isSpawnedByDefault() const
Definition: GameObject.h:195
bool LoadGameObjectFromDB(ObjectGuid::LowType guid, Map *map, bool addToMap=true)
Definition: GameObject.cpp:1125
Definition: Transport.h:112
bool AddToMap(T *, bool checkTransport=false)
Definition: Map.cpp:558

References Map::AddToMap(), PoolObject::guid, Map::Instanceable(), Map::IsGridLoaded(), GameObject::isSpawnedByDefault(), GameObject::LoadGameObjectFromDB(), sMapMgr, and sObjectMgr.

◆ Spawn1Object() [3/5]

void PoolGroup< Pool >::Spawn1Object ( PoolObject obj)
431{
432 sPoolMgr->SpawnPool(obj->guid);
433}

References PoolObject::guid, and sPoolMgr.

◆ Spawn1Object() [4/5]

void PoolGroup< Quest >::Spawn1Object ( PoolObject obj)
438{
439 // Creatures
440 QuestRelations* questMap = sObjectMgr->GetCreatureQuestRelationMap();
441 PooledQuestRelationBoundsNC qr = sPoolMgr->mQuestCreatureRelation.equal_range(obj->guid);
442 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
443 {
444 LOG_DEBUG("pool", "PoolGroup<Quest>: Adding quest {} to creature {}", itr->first, itr->second);
445 questMap->insert(QuestRelations::value_type(itr->second, itr->first));
446 }
447
448 // Gameobjects
449 questMap = sObjectMgr->GetGOQuestRelationMap();
450 qr = sPoolMgr->mQuestGORelation.equal_range(obj->guid);
451 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
452 {
453 LOG_DEBUG("pool", "PoolGroup<Quest>: Adding quest {} to GO {}", itr->first, itr->second);
454 questMap->insert(QuestRelations::value_type(itr->second, itr->first));
455 }
456}
#define LOG_DEBUG(filterType__,...)
Definition: Log.h:169

References PoolObject::guid, LOG_DEBUG, sObjectMgr, and sPoolMgr.

◆ Spawn1Object() [5/5]

template<class T >
void PoolGroup< T >::Spawn1Object ( PoolObject obj)

◆ SpawnObject() [1/2]

void PoolGroup< Quest >::SpawnObject ( ActivePoolData spawns,
uint32  limit,
uint32  triggerFrom 
)
460{
461 LOG_DEBUG("pool", "PoolGroup<Quest>: Spawning pool {}", poolId);
462 // load state from db
463 if (!triggerFrom)
464 {
466
467 stmt->SetData(0, poolId);
468
469 PreparedQueryResult result = CharacterDatabase.Query(stmt);
470
471 if (result)
472 {
473 do
474 {
475 uint32 questId = result->Fetch()[0].Get<uint32>();
476 spawns.ActivateObject<Quest>(questId, poolId);
477 PoolObject tempObj(questId, 0.0f);
478 Spawn1Object(&tempObj);
479 --limit;
480 } while (result->NextRow() && limit);
481 return;
482 }
483 }
484
485 ActivePoolObjects currentQuests = spawns.GetActiveQuests();
486 ActivePoolObjects newQuests;
487
488 // always try to select different quests
489 for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
490 {
491 if (spawns.IsActiveObject<Quest>(itr->guid))
492 continue;
493 newQuests.insert(itr->guid);
494 }
495
496 // clear the pool
497 DespawnObject(spawns);
498
499 // recycle minimal amount of quests if possible count is lower than limit
500 if (limit > newQuests.size() && !currentQuests.empty())
501 {
502 do
503 {
505 newQuests.insert(questId);
506 currentQuests.erase(questId);
507 } while (newQuests.size() < limit && !currentQuests.empty()); // failsafe
508 }
509
510 if (newQuests.empty())
511 return;
512
513 // activate <limit> random quests
514 do
515 {
517 spawns.ActivateObject<Quest>(questId, poolId);
518 PoolObject tempObj(questId, 0.0f);
519 Spawn1Object(&tempObj);
520 newQuests.erase(questId);
521 --limit;
522 } while (limit && !newQuests.empty());
523
524 // if we are here it means the pool is initialized at startup and did not have previous saved state
525 if (!triggerFrom)
526 sPoolMgr->SaveQuestsToDB(false, false, true);
527}
std::unordered_set< uint32 > ActivePoolObjects
Definition: PoolMgr.h:42
@ CHAR_SEL_POOL_QUEST_SAVE
Definition: CharacterDatabase.h:341
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:45
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
auto SelectRandomContainerElement(C const &container) -> typename std::add_const< decltype(*std::begin(container))>::type &
Definition: Containers.h:133
Definition: PreparedStatement.h:157
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition: PreparedStatement.h:77
Definition: PoolMgr.h:32
void ActivateObject(uint32 db_guid_or_pool_id, uint32 pool_id)
ActivePoolObjects GetActiveQuests() const
Definition: PoolMgr.h:59
void DespawnObject(ActivePoolData &spawns, ObjectGuid::LowType guid=0)
Definition: PoolMgr.cpp:160
Definition: QuestDef.h:210

References ActivePoolData::ActivateObject(), CHAR_SEL_POOL_QUEST_SAVE, CharacterDatabase, ActivePoolData::GetActiveQuests(), ActivePoolData::IsActiveObject(), LOG_DEBUG, Acore::Containers::SelectRandomContainerElement(), PreparedStatementBase::SetData(), and sPoolMgr.

◆ SpawnObject() [2/2]

template<class T >
void PoolGroup< T >::SpawnObject ( ActivePoolData spawns,
uint32  limit,
uint32  triggerFrom 
)
307{
308 int count = limit - spawns.GetActiveObjectCount(poolId);
309
310 // If triggered from some object respawn this object is still marked as spawned
311 // and also counted into m_SpawnedPoolAmount so we need increase count to be
312 // spawned by 1
313 if (triggerFrom)
314 {
315 ++count;
316 }
317
318 if (count > 0)
319 {
320 PoolObjectList rolledObjects;
321 rolledObjects.reserve(count);
322
323 // roll objects to be spawned
324 if (!ExplicitlyChanced.empty())
325 {
326 float roll = (float)rand_chance();
327
328 for (PoolObject& obj : ExplicitlyChanced)
329 {
330 roll -= obj.chance;
331
332 // Triggering object is marked as spawned at this time and can be also rolled (respawn case)
333 // so this need explicit check for this case
334 if (roll < 0 && (obj.guid == triggerFrom || !spawns.IsActiveObject<T>(obj.guid)))
335 {
336 rolledObjects.push_back(obj);
337 break;
338 }
339 }
340 }
341
342 if (!EqualChanced.empty() && rolledObjects.empty())
343 {
344 std::copy_if(EqualChanced.begin(), EqualChanced.end(), std::back_inserter(rolledObjects), [triggerFrom, &spawns](PoolObject const& object)
345 {
346 return object.guid == triggerFrom || !spawns.IsActiveObject<T>(object.guid);
347 });
348
349 Acore::Containers::RandomResize(rolledObjects, count);
350 }
351
352 // try to spawn rolled objects
353 for (PoolObject& obj : rolledObjects)
354 {
355 if (obj.guid == triggerFrom)
356 {
357 ReSpawn1Object(&obj);
358 triggerFrom = 0;
359 }
360 else
361 {
362 spawns.ActivateObject<T>(obj.guid, poolId);
363 Spawn1Object(&obj);
364 }
365 }
366 }
367
368 // One spawn one despawn no count increase
369 if (triggerFrom)
370 {
371 DespawnObject(spawns, triggerFrom);
372 }
373}
double rand_chance()
Definition: Random.cpp:83
void RandomResize(C &container, std::size_t requestedSize)
Definition: Containers.h:79
uint32 GetActiveObjectCount(uint32 pool_id) const
Definition: PoolMgr.cpp:30
void ReSpawn1Object(PoolObject *obj)
std::vector< PoolObject > PoolObjectList
Definition: PoolMgr.h:70

References ActivePoolData::ActivateObject(), ActivePoolData::GetActiveObjectCount(), ActivePoolData::IsActiveObject(), rand_chance(), and Acore::Containers::RandomResize().

Member Data Documentation

◆ EqualChanced

template<class T >
PoolObjectList PoolGroup< T >::EqualChanced
private

◆ ExplicitlyChanced

template<class T >
PoolObjectList PoolGroup< T >::ExplicitlyChanced
private

Referenced by PoolGroup< T >::IsEmpty().

◆ poolId

template<class T >
uint32 PoolGroup< T >::poolId
private