1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PoolMgr.h"
#include "Containers.h"
#include "Creature.h"
#include "DatabaseEnv.h"
#include "GameObject.h"
#include "Log.h"
#include "Map.h"
#include "ObjectMgr.h"
#include <sstream>
PoolObject::PoolObject(uint64 _guid, float _chance) : guid(_guid), chance(std::fabs(_chance))
{
}
////////////////////////////////////////////////////////////
// template class SpawnedPoolData
SpawnedPoolData::SpawnedPoolData(Map* owner) : mOwner(owner) { }
SpawnedPoolData::~SpawnedPoolData() = default;
// Method that tell amount spawned objects/subpools
uint32 SpawnedPoolData::GetSpawnedObjects(uint32 pool_id) const
{
SpawnedPoolPools::const_iterator itr = mSpawnedPools.find(pool_id);
return itr != mSpawnedPools.end() ? itr->second : 0;
}
// Method that tell if a creature is spawned currently
template<>
TC_GAME_API bool SpawnedPoolData::IsSpawnedObject<Creature>(uint64 db_guid) const
{
return mSpawnedCreatures.find(db_guid) != mSpawnedCreatures.end();
}
// Method that tell if a gameobject is spawned currently
template<>
TC_GAME_API bool SpawnedPoolData::IsSpawnedObject<GameObject>(uint64 db_guid) const
{
return mSpawnedGameobjects.find(db_guid) != mSpawnedGameobjects.end();
}
// Method that tell if a pool is spawned currently
template<>
TC_GAME_API bool SpawnedPoolData::IsSpawnedObject<Pool>(uint64 sub_pool_id) const
{
return mSpawnedPools.find(sub_pool_id) != mSpawnedPools.end();
}
bool SpawnedPoolData::IsSpawnedObject(SpawnObjectType type, uint64 db_guid_or_pool_id) const
{
switch (type)
{
case SPAWN_TYPE_CREATURE:
return IsSpawnedObject<Creature>(db_guid_or_pool_id);
case SPAWN_TYPE_GAMEOBJECT:
return IsSpawnedObject<GameObject>(db_guid_or_pool_id);
default:
ABORT_MSG("Invalid spawn type %u passed to SpawnedPoolData::IsSpawnedObject (with spawnId " UI64FMTD ")", uint32(type), db_guid_or_pool_id);
}
}
template<>
void SpawnedPoolData::AddSpawn<Creature>(uint64 db_guid, uint32 pool_id)
{
mSpawnedCreatures.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::AddSpawn<GameObject>(uint64 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.insert(db_guid);
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::AddSpawn<Pool>(uint64 sub_pool_id, uint32 pool_id)
{
mSpawnedPools[sub_pool_id] = 0;
++mSpawnedPools[pool_id];
}
template<>
void SpawnedPoolData::RemoveSpawn<Creature>(uint64 db_guid, uint32 pool_id)
{
mSpawnedCreatures.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void SpawnedPoolData::RemoveSpawn<GameObject>(uint64 db_guid, uint32 pool_id)
{
mSpawnedGameobjects.erase(db_guid);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
template<>
void SpawnedPoolData::RemoveSpawn<Pool>(uint64 sub_pool_id, uint32 pool_id)
{
mSpawnedPools.erase(sub_pool_id);
uint32& val = mSpawnedPools[pool_id];
if (val > 0)
--val;
}
////////////////////////////////////////////////////////////
// Methods of template class PoolGroup
template <class T>
PoolGroup<T>::PoolGroup(): poolId(0)
{
}
template <class T>
PoolGroup<T>::~PoolGroup() = default;
template <class T>
bool PoolGroup<T>::isEmptyDeepCheck() const
{
return isEmpty();
}
template <>
bool PoolGroup<Pool>::isEmptyDeepCheck() const
{
for (PoolObject const& explicitlyChanced : ExplicitlyChanced)
if (!sPoolMgr->IsEmpty(explicitlyChanced.guid))
return false;
for (PoolObject const& equalChanced : EqualChanced)
if (!sPoolMgr->IsEmpty(equalChanced.guid))
return false;
return true;
}
// Method to add a gameobject/creature guid to the proper list depending on pool type and chance value
template <class T>
void PoolGroup<T>::AddEntry(PoolObject& poolitem, uint32 maxentries)
{
if (poolitem.chance != 0 && maxentries == 1)
ExplicitlyChanced.push_back(poolitem);
else
EqualChanced.push_back(poolitem);
}
// Method to check the chances are proper in this object pool
template <class T>
bool PoolGroup<T>::CheckPool() const
{
if (EqualChanced.empty())
{
float chance = 0;
for (uint32 i = 0; i < ExplicitlyChanced.size(); ++i)
chance += ExplicitlyChanced[i].chance;
if (chance != 100 && chance != 0)
return false;
}
return true;
}
// 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
template<class T>
void PoolGroup<T>::DespawnObject(SpawnedPoolData& spawns, uint64 guid, bool alwaysDeleteRespawnTime)
{
for (size_t i=0; i < EqualChanced.size(); ++i)
{
// if spawned
if (spawns.IsSpawnedObject<T>(EqualChanced[i].guid))
{
if (!guid || EqualChanced[i].guid == guid)
{
Despawn1Object(spawns, EqualChanced[i].guid, alwaysDeleteRespawnTime);
spawns.RemoveSpawn<T>(EqualChanced[i].guid, poolId);
}
}
else if (alwaysDeleteRespawnTime)
RemoveRespawnTimeFromDB(spawns, EqualChanced[i].guid);
}
for (size_t i = 0; i < ExplicitlyChanced.size(); ++i)
{
// spawned
if (spawns.IsSpawnedObject<T>(ExplicitlyChanced[i].guid))
{
if (!guid || ExplicitlyChanced[i].guid == guid)
{
Despawn1Object(spawns, ExplicitlyChanced[i].guid, alwaysDeleteRespawnTime);
spawns.RemoveSpawn<T>(ExplicitlyChanced[i].guid, poolId);
}
}
else if (alwaysDeleteRespawnTime)
RemoveRespawnTimeFromDB(spawns, ExplicitlyChanced[i].guid);
}
}
// Method that is actualy doing the removal job on one creature
template<>
void PoolGroup<Creature>::Despawn1Object(SpawnedPoolData& spawns, uint64 guid, bool alwaysDeleteRespawnTime, bool saveRespawnTime)
{
auto creatureBounds = spawns.GetMap()->GetCreatureBySpawnIdStore().equal_range(guid);
for (auto itr = creatureBounds.first; itr != creatureBounds.second;)
{
Creature* creature = itr->second;
++itr;
// For dynamic spawns, save respawn time here
if (saveRespawnTime && !creature->GetRespawnCompatibilityMode())
creature->SaveRespawnTime();
creature->AddObjectToRemoveList();
}
if (alwaysDeleteRespawnTime)
spawns.GetMap()->RemoveRespawnTime(SPAWN_TYPE_CREATURE, guid, nullptr, true);
}
// Same on one gameobject
template<>
void PoolGroup<GameObject>::Despawn1Object(SpawnedPoolData& spawns, uint64 guid, bool alwaysDeleteRespawnTime, bool saveRespawnTime)
{
auto gameobjectBounds = spawns.GetMap()->GetGameObjectBySpawnIdStore().equal_range(guid);
for (auto itr = gameobjectBounds.first; itr != gameobjectBounds.second;)
{
GameObject* go = itr->second;
++itr;
// For dynamic spawns, save respawn time here
if (saveRespawnTime && !go->GetRespawnCompatibilityMode())
go->SaveRespawnTime();
go->AddObjectToRemoveList();
}
if (alwaysDeleteRespawnTime)
spawns.GetMap()->RemoveRespawnTime(SPAWN_TYPE_GAMEOBJECT, guid, nullptr, true);
}
// Same on one pool
template<>
void PoolGroup<Pool>::Despawn1Object(SpawnedPoolData& spawns, uint64 child_pool_id, bool alwaysDeleteRespawnTime, bool /*saveRespawnTime*/)
{
sPoolMgr->DespawnPool(spawns, child_pool_id, alwaysDeleteRespawnTime);
}
// Method for a pool only to remove any found record causing a circular dependency loop
template<>
void PoolGroup<Pool>::RemoveOneRelation(uint32 child_pool_id)
{
for (PoolObjectList::iterator itr = ExplicitlyChanced.begin(); itr != ExplicitlyChanced.end(); ++itr)
{
if (itr->guid == child_pool_id)
{
ExplicitlyChanced.erase(itr);
break;
}
}
for (PoolObjectList::iterator itr = EqualChanced.begin(); itr != EqualChanced.end(); ++itr)
{
if (itr->guid == child_pool_id)
{
EqualChanced.erase(itr);
break;
}
}
}
template <class T>
void PoolGroup<T>::SpawnObject(SpawnedPoolData& spawns, uint32 limit, uint64 triggerFrom)
{
int count = limit - spawns.GetSpawnedObjects(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;
if (count > 0)
{
PoolObjectList rolledObjects;
rolledObjects.reserve(count);
// roll objects to be spawned
if (!ExplicitlyChanced.empty())
{
float roll = 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.IsSpawnedObject<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.IsSpawnedObject<T>(object.guid);
});
Trinity::Containers::RandomResize(rolledObjects, count);
}
// try to spawn rolled objects
for (PoolObject& obj : rolledObjects)
{
if (obj.guid == triggerFrom)
{
ReSpawn1Object(spawns, &obj);
triggerFrom = 0;
}
else
{
spawns.AddSpawn<T>(obj.guid, poolId);
Spawn1Object(spawns, &obj);
}
}
}
// One spawn one despawn no count increase
if (triggerFrom)
DespawnObject(spawns, triggerFrom);
}
// Method that is actualy doing the spawn job on 1 creature
template <>
void PoolGroup<Creature>::Spawn1Object(SpawnedPoolData& spawns, PoolObject* obj)
{
if (CreatureData const* data = sObjectMgr->GetCreatureData(obj->guid))
{
// Spawn if necessary (loaded grids only)
// We use spawn coords to spawn
if (spawns.GetMap()->IsGridLoaded(data->spawnPoint))
Creature::CreateCreatureFromDB(obj->guid, spawns.GetMap());
}
}
// Same for 1 gameobject
template <>
void PoolGroup<GameObject>::Spawn1Object(SpawnedPoolData& spawns, PoolObject* obj)
{
if (GameObjectData const* data = sObjectMgr->GetGameObjectData(obj->guid))
{
// Spawn if necessary (loaded grids only)
// We use current coords to unspawn, not spawn coords since creature can have changed grid
if (spawns.GetMap()->IsGridLoaded(data->spawnPoint))
if (GameObject* go = GameObject::CreateGameObjectFromDB(obj->guid, spawns.GetMap(), false))
if (go->isSpawnedByDefault())
if (!spawns.GetMap()->AddToMap(go))
delete go;
}
}
// Same for 1 pool
template <>
void PoolGroup<Pool>::Spawn1Object(SpawnedPoolData& spawns, PoolObject* obj)
{
sPoolMgr->SpawnPool(spawns, obj->guid);
}
// Method that does the respawn job on the specified creature
template <>
void PoolGroup<Creature>::ReSpawn1Object(SpawnedPoolData& spawns, PoolObject* obj)
{
Despawn1Object(spawns, obj->guid, false, false);
Spawn1Object(spawns, obj);
}
// Method that does the respawn job on the specified gameobject
template <>
void PoolGroup<GameObject>::ReSpawn1Object(SpawnedPoolData& spawns, PoolObject* obj)
{
Despawn1Object(spawns, obj->guid, false, false);
Spawn1Object(spawns, obj);
}
// Nothing to do for a child Pool
template <>
void PoolGroup<Pool>::ReSpawn1Object(SpawnedPoolData& /*spawns*/, PoolObject* /*obj*/) { }
template <>
void PoolGroup<Creature>::RemoveRespawnTimeFromDB(SpawnedPoolData& spawns, uint64 guid)
{
spawns.GetMap()->RemoveRespawnTime(SPAWN_TYPE_CREATURE, guid, nullptr, true);
}
template <>
void PoolGroup<GameObject>::RemoveRespawnTimeFromDB(SpawnedPoolData& spawns, uint64 guid)
{
spawns.GetMap()->RemoveRespawnTime(SPAWN_TYPE_GAMEOBJECT, guid, nullptr, true);
}
template <>
void PoolGroup<Pool>::RemoveRespawnTimeFromDB(SpawnedPoolData& /*spawns*/, uint64 /*guid*/) { }
////////////////////////////////////////////////////////////
// Methods of class PoolMgr
PoolMgr::PoolMgr() = default;
PoolMgr::~PoolMgr() = default;
void PoolMgr::Initialize()
{
mGameobjectSearchMap.clear();
mCreatureSearchMap.clear();
}
PoolMgr* PoolMgr::instance()
{
static PoolMgr instance;
return &instance;
}
void PoolMgr::LoadFromDB()
{
// Pool templates
{
uint32 oldMSTime = getMSTime();
QueryResult result = WorldDatabase.Query("SELECT entry, max_limit FROM pool_template");
if (!result)
{
mPoolTemplate.clear();
TC_LOG_INFO("server.loading", ">> Loaded 0 object pools. DB table `pool_template` is empty.");
return;
}
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 pool_id = fields[0].GetUInt32();
PoolTemplateData& pPoolTemplate = mPoolTemplate[pool_id];
pPoolTemplate.MaxLimit = fields[1].GetUInt32();
pPoolTemplate.MapId = -1;
++count;
}
while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded {} objects pools in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}
// Creatures
TC_LOG_INFO("server.loading", "Loading Creatures Pooling Data...");
{
uint32 oldMSTime = getMSTime();
// 1 2 3
QueryResult result = WorldDatabase.Query("SELECT spawnId, poolSpawnId, chance FROM pool_members WHERE type = 0");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 creatures in pools. DB table `pool_creature` is empty.");
}
else
{
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint64 guid = fields[0].GetUInt64();
uint32 pool_id = fields[1].GetUInt32();
float chance = fields[2].GetFloat();
CreatureData const* data = sObjectMgr->GetCreatureData(guid);
if (!data)
{
TC_LOG_ERROR("sql.sql", "`pool_creature` has a non existing creature spawn (GUID: {}) defined for pool id ({}), skipped.", guid, pool_id);
continue;
}
auto it = mPoolTemplate.find(pool_id);
if (it == mPoolTemplate.end())
{
TC_LOG_ERROR("sql.sql", "`pool_creature` pool id ({}) is not in `pool_template`, skipped.", pool_id);
continue;
}
if (chance < 0 || chance > 100)
{
TC_LOG_ERROR("sql.sql", "`pool_creature` has an invalid chance ({}) for creature guid ({}) in pool id ({}), skipped.", chance, guid, pool_id);
continue;
}
PoolTemplateData* pPoolTemplate = &mPoolTemplate[pool_id];
if (pPoolTemplate->MapId == -1)
pPoolTemplate->MapId = int32(data->mapId);
if (pPoolTemplate->MapId != int32(data->mapId))
{
TC_LOG_ERROR("sql.sql", "`pool_creature` has creature spawns on multiple different maps for creature guid ({}) in pool id ({}), skipped.", guid, pool_id);
continue;
}
PoolObject plObject = PoolObject(guid, chance);
PoolGroup<Creature>& cregroup = mPoolCreatureGroups[pool_id];
cregroup.SetPoolId(pool_id);
cregroup.AddEntry(plObject, pPoolTemplate->MaxLimit);
SearchPair p(guid, pool_id);
mCreatureSearchMap.insert(p);
++count;
}
while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded {} creatures in pools in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}
}
// Gameobjects
TC_LOG_INFO("server.loading", "Loading Gameobject Pooling Data...");
{
uint32 oldMSTime = getMSTime();
// 1 2 3
QueryResult result = WorldDatabase.Query("SELECT spawnId, poolSpawnId, chance FROM pool_members WHERE type = 1");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 gameobjects in pools. DB table `pool_gameobject` is empty.");
}
else
{
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint64 guid = fields[0].GetUInt64();
uint32 pool_id = fields[1].GetUInt32();
float chance = fields[2].GetFloat();
GameObjectData const* data = sObjectMgr->GetGameObjectData(guid);
if (!data)
{
TC_LOG_ERROR("sql.sql", "`pool_gameobject` has a non existing gameobject spawn (GUID: {}) defined for pool id ({}), skipped.", guid, pool_id);
continue;
}
auto it = mPoolTemplate.find(pool_id);
if (it == mPoolTemplate.end())
{
TC_LOG_ERROR("sql.sql", "`pool_gameobject` pool id ({}) is not in `pool_template`, skipped.", pool_id);
continue;
}
if (chance < 0 || chance > 100)
{
TC_LOG_ERROR("sql.sql", "`pool_gameobject` has an invalid chance ({}) for gameobject guid ({}) in pool id ({}), skipped.", chance, guid, pool_id);
continue;
}
PoolTemplateData* pPoolTemplate = &mPoolTemplate[pool_id];
if (pPoolTemplate->MapId == -1)
pPoolTemplate->MapId = int32(data->mapId);
if (pPoolTemplate->MapId != int32(data->mapId))
{
TC_LOG_ERROR("sql.sql", "`pool_gameobject` has gameobject spawns on multiple different maps for gameobject guid ({}) in pool id ({}), skipped.", guid, pool_id);
continue;
}
PoolObject plObject = PoolObject(guid, chance);
PoolGroup<GameObject>& gogroup = mPoolGameobjectGroups[pool_id];
gogroup.SetPoolId(pool_id);
gogroup.AddEntry(plObject, pPoolTemplate->MaxLimit);
SearchPair p(guid, pool_id);
mGameobjectSearchMap.insert(p);
++count;
}
while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded {} gameobject in pools in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}
}
// Pool of pools
TC_LOG_INFO("server.loading", "Loading Mother Pooling Data...");
{
uint32 oldMSTime = getMSTime();
// 1 2 3
QueryResult result = WorldDatabase.Query("SELECT spawnId, poolSpawnId, chance FROM pool_members WHERE type = 2");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 pools in pools");
}
else
{
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 child_pool_id = fields[0].GetUInt64();
uint32 mother_pool_id = fields[1].GetUInt32();
float chance = fields[2].GetFloat();
{
auto it = mPoolTemplate.find(mother_pool_id);
if (it == mPoolTemplate.end())
{
TC_LOG_ERROR("sql.sql", "`pool_pool` mother_pool id ({}) is not in `pool_template`, skipped.", mother_pool_id);
continue;
}
}
{
auto it = mPoolTemplate.find(child_pool_id);
if (it == mPoolTemplate.end())
{
TC_LOG_ERROR("sql.sql", "`pool_pool` included pool_id ({}) is not in `pool_template`, skipped.", child_pool_id);
continue;
}
}
if (mother_pool_id == child_pool_id)
{
TC_LOG_ERROR("sql.sql", "`pool_pool` pool_id ({}) includes itself, dead-lock detected, skipped.", child_pool_id);
continue;
}
if (chance < 0 || chance > 100)
{
TC_LOG_ERROR("sql.sql", "`pool_pool` has an invalid chance ({}) for pool id ({}) in mother pool id ({}), skipped.", chance, child_pool_id, mother_pool_id);
continue;
}
PoolTemplateData* pPoolTemplateMother = &mPoolTemplate[mother_pool_id];
PoolObject plObject = PoolObject(child_pool_id, chance);
PoolGroup<Pool>& plgroup = mPoolPoolGroups[mother_pool_id];
plgroup.SetPoolId(mother_pool_id);
plgroup.AddEntry(plObject, pPoolTemplateMother->MaxLimit);
SearchPair p(child_pool_id, mother_pool_id);
mPoolSearchMap.insert(p);
++count;
}
while (result->NextRow());
// Now check for circular reference
// All pool_ids are in pool_template
for (auto const& it : mPoolTemplate)
{
std::set<uint32> checkedPools;
for (SearchMap::iterator poolItr = mPoolSearchMap.find(it.first); poolItr != mPoolSearchMap.end(); poolItr = mPoolSearchMap.find(poolItr->second))
{
if (mPoolTemplate[poolItr->first].MapId != -1)
{
if (mPoolTemplate[poolItr->second].MapId == -1)
mPoolTemplate[poolItr->second].MapId = mPoolTemplate[poolItr->first].MapId;
if (mPoolTemplate[poolItr->second].MapId != mPoolTemplate[poolItr->first].MapId)
{
TC_LOG_ERROR("sql.sql", "`pool_pool` has child pools on multiple maps in pool id ({}), skipped.", poolItr->second);
mPoolPoolGroups[poolItr->second].RemoveOneRelation(poolItr->first);
mPoolSearchMap.erase(poolItr);
--count;
break;
}
}
checkedPools.insert(poolItr->first);
if (checkedPools.find(poolItr->second) != checkedPools.end())
{
std::ostringstream ss;
ss << "The pool(s) ";
for (std::set<uint32>::const_iterator itr = checkedPools.begin(); itr != checkedPools.end(); ++itr)
ss << *itr << ' ';
ss << "create(s) a circular reference, which can cause the server to freeze.\nRemoving the last link between mother pool "
<< poolItr->first << " and child pool " << poolItr->second;
TC_LOG_ERROR("sql.sql", "{}", ss.str());
mPoolPoolGroups[poolItr->second].RemoveOneRelation(poolItr->first);
mPoolSearchMap.erase(poolItr);
--count;
break;
}
}
}
TC_LOG_INFO("server.loading", ">> Loaded {} pools in mother pools in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}
}
for (auto const& [poolId, templateData] : mPoolTemplate)
{
if (IsEmpty(poolId))
{
TC_LOG_ERROR("sql.sql", "Pool Id {} is empty (has no creatures and no gameobects and either no child pools or child pools are all empty. The pool will not be spawned", poolId);
continue;
}
ASSERT(templateData.MapId != -1);
}
// The initialize method will spawn all pools not in an event and not in another pool, this is why there is 2 left joins with 2 null checks
TC_LOG_INFO("server.loading", "Starting objects pooling system...");
{
uint32 oldMSTime = getMSTime();
QueryResult result = WorldDatabase.Query("SELECT DISTINCT pool_template.entry, pool_members.spawnId, pool_members.poolSpawnId FROM pool_template"
" LEFT JOIN game_event_pool ON pool_template.entry = game_event_pool.pool_entry"
" LEFT JOIN pool_members ON pool_members.type = 2 AND pool_template.entry = pool_members.spawnId WHERE game_event_pool.pool_entry IS NULL");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Pool handling system initialized, 0 pools spawned.");
}
else
{
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 pool_entry = fields[0].GetUInt32();
uint32 pool_pool_id = fields[1].GetUInt64();
if (IsEmpty(pool_entry))
continue;
if (!CheckPool(pool_entry))
{
if (pool_pool_id)
// The pool is a child pool in pool_pool table. Ideally we should remove it from the pool handler to ensure it never gets spawned,
// however that could recursively invalidate entire chain of mother pools. It can be done in the future but for now we'll do nothing.
TC_LOG_ERROR("sql.sql", "Pool Id {} has no equal chance pooled entites defined and explicit chance sum is not 100. This broken pool is a child pool of Id {} and cannot be safely removed.", pool_entry, fields[2].GetUInt32());
else
TC_LOG_ERROR("sql.sql", "Pool Id {} has no equal chance pooled entites defined and explicit chance sum is not 100. The pool will not be spawned.", pool_entry);
continue;
}
// Don't spawn child pools, they are spawned recursively by their parent pools
if (!pool_pool_id)
{
mAutoSpawnPoolsPerMap[mPoolTemplate[pool_entry].MapId].push_back(pool_entry);
count++;
}
}
while (result->NextRow());
TC_LOG_DEBUG("pool", "Pool handling system initialized, {} pools will be spawned by default in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}
}
}
// Call to spawn a pool, if cache if true the method will spawn only if cached entry is different
// If it's same, the creature is respawned only (added back to map)
template<>
void PoolMgr::SpawnPool<Creature>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid)
{
auto it = mPoolCreatureGroups.find(pool_id);
if (it != mPoolCreatureGroups.end() && !it->second.isEmpty())
it->second.SpawnObject(spawnedPoolData, 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
// If it's same, the gameobject is respawned only (added back to map)
template<>
void PoolMgr::SpawnPool<GameObject>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid)
{
auto it = mPoolGameobjectGroups.find(pool_id);
if (it != mPoolGameobjectGroups.end() && !it->second.isEmpty())
it->second.SpawnObject(spawnedPoolData, 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
// If it's same, the pool is respawned only
template<>
void PoolMgr::SpawnPool<Pool>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 sub_pool_id)
{
auto it = mPoolPoolGroups.find(pool_id);
if (it != mPoolPoolGroups.end() && !it->second.isEmpty())
it->second.SpawnObject(spawnedPoolData, mPoolTemplate[pool_id].MaxLimit, sub_pool_id);
}
void PoolMgr::SpawnPool(SpawnedPoolData& spawnedPoolData, uint32 pool_id)
{
SpawnPool<Pool>(spawnedPoolData, pool_id, 0);
SpawnPool<GameObject>(spawnedPoolData, pool_id, 0);
SpawnPool<Creature>(spawnedPoolData, pool_id, 0);
}
// Call to despawn a pool, all gameobjects/creatures in this pool are removed
void PoolMgr::DespawnPool(SpawnedPoolData& spawnedPoolData, uint32 pool_id, bool alwaysDeleteRespawnTime)
{
{
auto it = mPoolCreatureGroups.find(pool_id);
if (it != mPoolCreatureGroups.end() && !it->second.isEmpty())
it->second.DespawnObject(spawnedPoolData, 0, alwaysDeleteRespawnTime);
}
{
auto it = mPoolGameobjectGroups.find(pool_id);
if (it != mPoolGameobjectGroups.end() && !it->second.isEmpty())
it->second.DespawnObject(spawnedPoolData, 0, alwaysDeleteRespawnTime);
}
{
auto it = mPoolPoolGroups.find(pool_id);
if (it != mPoolPoolGroups.end() && !it->second.isEmpty())
it->second.DespawnObject(spawnedPoolData, 0, alwaysDeleteRespawnTime);
}
}
// Selects proper template overload to call based on passed type
uint32 PoolMgr::IsPartOfAPool(SpawnObjectType type, ObjectGuid::LowType spawnId) const
{
switch (type)
{
case SPAWN_TYPE_CREATURE:
return IsPartOfAPool<Creature>(spawnId);
case SPAWN_TYPE_GAMEOBJECT:
return IsPartOfAPool<GameObject>(spawnId);
case SPAWN_TYPE_AREATRIGGER:
return 0;
default:
ABORT_MSG("Invalid spawn type %u passed to PoolMgr::IsPartOfPool (with spawnId " UI64FMTD ")", uint32(type), spawnId);
return 0;
}
}
bool PoolMgr::IsEmpty(uint32 pool_id) const
{
{
auto it = mPoolGameobjectGroups.find(pool_id);
if (it != mPoolGameobjectGroups.end() && !it->second.isEmptyDeepCheck())
return false;
}
{
auto it = mPoolCreatureGroups.find(pool_id);
if (it != mPoolCreatureGroups.end() && !it->second.isEmptyDeepCheck())
return false;
}
{
auto it = mPoolPoolGroups.find(pool_id);
if (it != mPoolPoolGroups.end() && !it->second.isEmptyDeepCheck())
return false;
}
return true;
}
// Method that check chance integrity of the creatures and gameobjects in this pool
bool PoolMgr::CheckPool(uint32 pool_id) const
{
{
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;
}
return true;
}
// Call to update the pool when a gameobject/creature part of pool [pool_id] is ready to respawn
// Here we cache only the creature/gameobject whose guid is passed as parameter
// Then the spawn pool call will use this cache to decide
template<typename T>
void PoolMgr::UpdatePool(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid_or_pool_id)
{
if (uint32 motherpoolid = IsPartOfAPool<Pool>(pool_id))
SpawnPool<Pool>(spawnedPoolData, motherpoolid, pool_id);
else
SpawnPool<T>(spawnedPoolData, pool_id, db_guid_or_pool_id);
}
template void PoolMgr::UpdatePool<Pool>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid_or_pool_id);
template void PoolMgr::UpdatePool<GameObject>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid_or_pool_id);
template void PoolMgr::UpdatePool<Creature>(SpawnedPoolData& spawnedPoolData, uint32 pool_id, uint64 db_guid_or_pool_id);
void PoolMgr::UpdatePool(SpawnedPoolData& spawnedPoolData, uint32 pool_id, SpawnObjectType type, uint64 spawnId)
{
switch (type)
{
case SPAWN_TYPE_CREATURE:
UpdatePool<Creature>(spawnedPoolData, pool_id, spawnId);
break;
case SPAWN_TYPE_GAMEOBJECT:
UpdatePool<GameObject>(spawnedPoolData, pool_id, spawnId);
break;
default:
ABORT_MSG("Invalid spawn type %u passed to PoolMgr::IsPartOfPool (with spawnId " UI64FMTD ")", uint32(type), spawnId);
}
}
std::unique_ptr<SpawnedPoolData> PoolMgr::InitPoolsForMap(Map* map)
{
std::unique_ptr<SpawnedPoolData> spawnedPoolData = std::make_unique<SpawnedPoolData>(map);
if (std::vector<uint32> const* poolIds = Trinity::Containers::MapGetValuePtr(mAutoSpawnPoolsPerMap, spawnedPoolData->GetMap()->GetId()))
for (uint32 poolId : *poolIds)
SpawnPool(*spawnedPoolData, poolId);
return spawnedPoolData;
}
PoolTemplateData const* PoolMgr::GetPoolTemplate(uint16 pool_id) const
{
return Trinity::Containers::MapGetValuePtr(mPoolTemplate, pool_id);
}
|