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 *)
 
void ReSpawn1Object (PoolObject *)
 
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 
)
133{
134 if (poolitem.chance != 0 && maxentries == 1)
135 ExplicitlyChanced.push_back(poolitem);
136 else
137 EqualChanced.push_back(poolitem);
138}
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
143{
144 if (EqualChanced.empty())
145 {
146 float chance = 0;
147 for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
148 chance += ExplicitlyChanced[i].chance;
149 if (chance != 100 && chance != 0)
150 return false;
151 }
152 return true;
153}
std::uint32_t uint32
Definition: Define.h:108

◆ Despawn1Object() [1/5]

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

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

◆ Despawn1Object() [2/5]

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

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)
235{
236 sPoolMgr->DespawnPool(child_pool_id);
237}
#define sPoolMgr
Definition: PoolMgr.h:163

References sPoolMgr.

◆ Despawn1Object() [5/5]

void PoolGroup< Quest >::Despawn1Object ( uint32  quest_id)
242{
243 // Creatures
244 QuestRelations* questMap = sObjectMgr->GetCreatureQuestRelationMap();
245 PooledQuestRelationBoundsNC qr = sPoolMgr->mQuestCreatureRelation.equal_range(quest_id);
246 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
247 {
248 QuestRelations::iterator qitr = questMap->find(itr->second);
249 if (qitr == questMap->end())
250 continue;
251 QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
252 for (; qitr != lastElement; ++qitr)
253 {
254 if (qitr->first == itr->second && qitr->second == itr->first)
255 {
256 questMap->erase(qitr); // iterator is now no more valid
257 break; // but we can exit loop since the element is found
258 }
259 }
260 }
261
262 // Gameobjects
263 questMap = sObjectMgr->GetGOQuestRelationMap();
264 qr = sPoolMgr->mQuestGORelation.equal_range(quest_id);
265 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
266 {
267 QuestRelations::iterator qitr = questMap->find(itr->second);
268 if (qitr == questMap->end())
269 continue;
270 QuestRelations::iterator lastElement = questMap->upper_bound(itr->second);
271 for (; qitr != lastElement; ++qitr)
272 {
273 if (qitr->first == itr->second && qitr->second == itr->first)
274 {
275 questMap->erase(qitr); // iterator is now no more valid
276 break; // but we can exit loop since the element is found
277 }
278 }
279 }
280}
std::multimap< uint32, uint32 > QuestRelations
Definition: ObjectMgr.h:518
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 
)
160{
161 for (size_t i = 0; i < EqualChanced.size(); ++i)
162 {
163 // if spawned
164 if (spawns.IsActiveObject<T>(EqualChanced[i].guid))
165 {
166 if (!guid || EqualChanced[i].guid == guid)
167 {
169 spawns.RemoveObject<T>(EqualChanced[i].guid, poolId);
170 }
171 }
172 }
173
174 for (size_t i = 0; i < ExplicitlyChanced.size(); ++i)
175 {
176 // spawned
177 if (spawns.IsActiveObject<T>(ExplicitlyChanced[i].guid))
178 {
179 if (!guid || ExplicitlyChanced[i].guid == guid)
180 {
182 spawns.RemoveObject<T>(ExplicitlyChanced[i].guid, poolId);
183 }
184 }
185 }
186}
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)
285{
286 for (PoolObjectList::iterator itr = ExplicitlyChanced.begin(); itr != ExplicitlyChanced.end(); ++itr)
287 {
288 if (itr->guid == child_pool_id)
289 {
290 ExplicitlyChanced.erase(itr);
291 break;
292 }
293 }
294 for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
295 {
296 if (itr->guid == child_pool_id)
297 {
298 EqualChanced.erase(itr);
299 break;
300 }
301 }
302}

◆ RemoveOneRelation() [2/2]

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

◆ ReSpawn1Object() [1/5]

void PoolGroup< Creature >::ReSpawn1Object ( PoolObject )
531{
532 // Creature is still on map, nothing to do
533}

◆ ReSpawn1Object() [2/5]

void PoolGroup< GameObject >::ReSpawn1Object ( PoolObject )
538{
539 // Gameobject is still on map, nothing to do
540}

◆ ReSpawn1Object() [3/5]

void PoolGroup< Pool >::ReSpawn1Object ( PoolObject )
545{
546}

◆ ReSpawn1Object() [4/5]

void PoolGroup< Quest >::ReSpawn1Object ( PoolObject )
551{
552}

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

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

◆ Spawn1Object() [2/5]

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

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)
430{
431 sPoolMgr->SpawnPool(obj->guid);
432}

References PoolObject::guid, and sPoolMgr.

◆ Spawn1Object() [4/5]

void PoolGroup< Quest >::Spawn1Object ( PoolObject obj)
437{
438 // Creatures
439 QuestRelations* questMap = sObjectMgr->GetCreatureQuestRelationMap();
440 PooledQuestRelationBoundsNC qr = sPoolMgr->mQuestCreatureRelation.equal_range(obj->guid);
441 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
442 {
443 LOG_DEBUG("pool", "PoolGroup<Quest>: Adding quest {} to creature {}", itr->first, itr->second);
444 questMap->insert(QuestRelations::value_type(itr->second, itr->first));
445 }
446
447 // Gameobjects
448 questMap = sObjectMgr->GetGOQuestRelationMap();
449 qr = sPoolMgr->mQuestGORelation.equal_range(obj->guid);
450 for (PooledQuestRelation::iterator itr = qr.first; itr != qr.second; ++itr)
451 {
452 LOG_DEBUG("pool", "PoolGroup<Quest>: Adding quest {} to GO {}", itr->first, itr->second);
453 questMap->insert(QuestRelations::value_type(itr->second, itr->first));
454 }
455}
#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 
)
459{
460 LOG_DEBUG("pool", "PoolGroup<Quest>: Spawning pool {}", poolId);
461 // load state from db
462 if (!triggerFrom)
463 {
465
466 stmt->SetData(0, poolId);
467
468 PreparedQueryResult result = CharacterDatabase.Query(stmt);
469
470 if (result)
471 {
472 do
473 {
474 uint32 questId = result->Fetch()[0].Get<uint32>();
475 spawns.ActivateObject<Quest>(questId, poolId);
476 PoolObject tempObj(questId, 0.0f);
477 Spawn1Object(&tempObj);
478 --limit;
479 } while (result->NextRow() && limit);
480 return;
481 }
482 }
483
484 ActivePoolObjects currentQuests = spawns.GetActiveQuests();
485 ActivePoolObjects newQuests;
486
487 // always try to select different quests
488 for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
489 {
490 if (spawns.IsActiveObject<Quest>(itr->guid))
491 continue;
492 newQuests.insert(itr->guid);
493 }
494
495 // clear the pool
496 DespawnObject(spawns);
497
498 // recycle minimal amount of quests if possible count is lower than limit
499 if (limit > newQuests.size() && !currentQuests.empty())
500 {
501 do
502 {
504 newQuests.insert(questId);
505 currentQuests.erase(questId);
506 } while (newQuests.size() < limit && !currentQuests.empty()); // failsafe
507 }
508
509 if (newQuests.empty())
510 return;
511
512 // activate <limit> random quests
513 do
514 {
516 spawns.ActivateObject<Quest>(questId, poolId);
517 PoolObject tempObj(questId, 0.0f);
518 Spawn1Object(&tempObj);
519 newQuests.erase(questId);
520 --limit;
521 } while (limit && !newQuests.empty());
522
523 // if we are here it means the pool is initialized at startup and did not have previous saved state
524 if (!triggerFrom)
525 sPoolMgr->SaveQuestsToDB(false, false, true);
526}
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:46
@ CHAR_SEL_POOL_QUEST_SAVE
Definition: CharacterDatabase.h:341
std::unordered_set< uint32 > ActivePoolObjects
Definition: PoolMgr.h:42
auto SelectRandomContainerElement(C const &container) -> typename std::add_const< decltype(*std::begin(container))>::type &
Definition: Containers.h:135
Definition: PreparedStatement.h:158
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition: PreparedStatement.h:78
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:159
void Spawn1Object(PoolObject *obj)
Definition: QuestDef.h:207

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