TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PoolMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://www.mangosproject.org/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_POOLHANDLER_H
20 #define TRINITY_POOLHANDLER_H
21 
22 #include "Define.h"
23 #include "Creature.h"
24 #include "GameObject.h"
25 #include "QuestDef.h"
26 
28 {
30 };
31 
32 struct PoolObject
33 {
35  float chance;
36  PoolObject(uint64 _guid, float _chance) : guid(_guid), chance(std::fabs(_chance)) { }
37 };
38 
39 class Pool // for Pool of Pool case
40 {
41 };
42 
43 typedef std::set<uint64> ActivePoolObjects;
44 typedef std::map<uint64, uint32> ActivePoolPools;
45 
47 {
48  public:
49  template<typename T>
50  bool IsActiveObject(uint64 db_guid_or_pool_id) const;
51 
52  uint32 GetActiveObjectCount(uint32 pool_id) const;
53 
54  template<typename T>
55  void ActivateObject(uint64 db_guid_or_pool_id, uint32 pool_id);
56 
57  template<typename T>
58  void RemoveObject(uint64 db_guid_or_pool_id, uint32 pool_id);
59 
60  ActivePoolObjects GetActiveQuests() const { return mActiveQuests; } // a copy of the set
61  private:
66 };
67 
68 template <class T>
70 {
71  typedef std::vector<PoolObject> PoolObjectList;
72  public:
73  explicit PoolGroup() : poolId(0) { }
74  void SetPoolId(uint32 pool_id) { poolId = pool_id; }
75  ~PoolGroup() { };
76  bool isEmpty() const { return ExplicitlyChanced.empty() && EqualChanced.empty(); }
77  void AddEntry(PoolObject& poolitem, uint32 maxentries);
78  bool CheckPool() const;
79  PoolObject* RollOne(ActivePoolData& spawns, uint64 triggerFrom);
80  void DespawnObject(ActivePoolData& spawns, uint64 guid=0);
81  void Despawn1Object(uint64 guid);
82  void SpawnObject(ActivePoolData& spawns, uint32 limit, uint64 triggerFrom);
83 
84  void Spawn1Object(PoolObject* obj);
85  void ReSpawn1Object(PoolObject* obj);
86  void RemoveOneRelation(uint32 child_pool_id);
88  {
89  if (EqualChanced.empty())
90  return 0;
91  return EqualChanced.front().guid;
92  }
93  uint32 GetPoolId() const { return poolId; }
94  private:
96  PoolObjectList ExplicitlyChanced;
97  PoolObjectList EqualChanced;
98 };
99 
100 typedef std::multimap<uint32, uint32> PooledQuestRelation;
101 typedef std::pair<PooledQuestRelation::const_iterator, PooledQuestRelation::const_iterator> PooledQuestRelationBounds;
102 typedef std::pair<PooledQuestRelation::iterator, PooledQuestRelation::iterator> PooledQuestRelationBoundsNC;
103 
105 {
106  private:
107  PoolMgr();
108  ~PoolMgr() { };
109 
110  public:
111  static PoolMgr* instance();
112 
113  void LoadFromDB();
114  void LoadQuestPools();
115  void SaveQuestsToDB();
116 
117  void Initialize();
118 
119  template<typename T>
120  uint32 IsPartOfAPool(uint64 db_guid_or_pool_id) const;
121 
122  template<typename T>
123  bool IsSpawnedObject(uint64 db_guid_or_pool_id) const { return mSpawnedData.IsActiveObject<T>(db_guid_or_pool_id); }
124 
125  bool CheckPool(uint32 pool_id) const;
126 
127  void SpawnPool(uint32 pool_id);
128  void DespawnPool(uint32 pool_id);
129 
130  template<typename T>
131  void UpdatePool(uint32 pool_id, uint64 db_guid_or_pool_id);
132 
133  void ChangeDailyQuests();
134  void ChangeWeeklyQuests();
135 
138 
139  private:
140  template<typename T>
141  void SpawnPool(uint32 pool_id, uint64 db_guid_or_pool_id);
142 
144  typedef std::vector<PoolTemplateData> PoolTemplateDataMap;
145  typedef std::vector<PoolGroup<Creature> > PoolGroupCreatureMap;
146  typedef std::vector<PoolGroup<GameObject> > PoolGroupGameObjectMap;
147  typedef std::vector<PoolGroup<Pool> > PoolGroupPoolMap;
148  typedef std::vector<PoolGroup<Quest> > PoolGroupQuestMap;
149  typedef std::pair<uint64, uint32> SearchPair;
150  typedef std::map<uint64, uint32> SearchMap;
151 
152  PoolTemplateDataMap mPoolTemplate;
153  PoolGroupCreatureMap mPoolCreatureGroups;
154  PoolGroupGameObjectMap mPoolGameobjectGroups;
155  PoolGroupPoolMap mPoolPoolGroups;
156  PoolGroupQuestMap mPoolQuestGroups;
159  SearchMap mPoolSearchMap;
160  SearchMap mQuestSearchMap;
161 
162  // dynamic data
164 };
165 
166 #define sPoolMgr PoolMgr::instance()
167 
168 // Method that tell if the creature is part of a pool and return the pool id if yes
169 template<>
170 inline uint32 PoolMgr::IsPartOfAPool<Creature>(uint64 db_guid) const
171 {
172  SearchMap::const_iterator itr = mCreatureSearchMap.find(db_guid);
173  if (itr != mCreatureSearchMap.end())
174  return itr->second;
175 
176  return 0;
177 }
178 
179 // Method that tell if the gameobject is part of a pool and return the pool id if yes
180 template<>
181 inline uint32 PoolMgr::IsPartOfAPool<GameObject>(uint64 db_guid) const
182 {
183  SearchMap::const_iterator itr = mGameobjectSearchMap.find(db_guid);
184  if (itr != mGameobjectSearchMap.end())
185  return itr->second;
186 
187  return 0;
188 }
189 
190 // Method that tell if the quest is part of another pool and return the pool id if yes
191 template<>
192 inline uint32 PoolMgr::IsPartOfAPool<Quest>(uint64 pool_id) const
193 {
194  SearchMap::const_iterator itr = mQuestSearchMap.find(pool_id);
195  if (itr != mQuestSearchMap.end())
196  return itr->second;
197 
198  return 0;
199 }
200 
201 // Method that tell if the pool is part of another pool and return the pool id if yes
202 template<>
203 inline uint32 PoolMgr::IsPartOfAPool<Pool>(uint64 pool_id) const
204 {
205  SearchMap::const_iterator itr = mPoolSearchMap.find(pool_id);
206  if (itr != mPoolSearchMap.end())
207  return itr->second;
208 
209  return 0;
210 }
211 
212 #endif
PooledQuestRelation mQuestGORelation
Definition: PoolMgr.h:137
~PoolGroup()
Definition: PoolMgr.h:75
uint64 GetFirstEqualChancedObjectId()
Definition: PoolMgr.h:87
std::vector< PoolGroup< GameObject > > PoolGroupGameObjectMap
Definition: PoolMgr.h:146
PoolGroup()
Definition: PoolMgr.h:73
SearchMap mCreatureSearchMap
Definition: PoolMgr.h:157
PoolObject(uint64 _guid, float _chance)
Definition: PoolMgr.h:36
SearchMap mGameobjectSearchMap
Definition: PoolMgr.h:158
std::pair< PooledQuestRelation::iterator, PooledQuestRelation::iterator > PooledQuestRelationBoundsNC
Definition: PoolMgr.h:102
Definition: PoolMgr.h:27
PoolObjectList EqualChanced
Definition: PoolMgr.h:97
PoolObjectList ExplicitlyChanced
Definition: PoolMgr.h:96
uint32 max_pool_id
Definition: PoolMgr.h:143
void LoadFromDB()
Definition: AddonMgr.cpp:40
std::multimap< uint32, uint32 > PooledQuestRelation
Definition: PoolMgr.h:100
std::set< uint64 > ActivePoolObjects
Definition: PoolMgr.h:43
PooledQuestRelation mQuestCreatureRelation
Definition: PoolMgr.h:136
STL namespace.
PoolGroupPoolMap mPoolPoolGroups
Definition: PoolMgr.h:155
PoolGroupQuestMap mPoolQuestGroups
Definition: PoolMgr.h:156
Definition: PoolMgr.h:46
std::vector< PoolGroup< Creature > > PoolGroupCreatureMap
Definition: PoolMgr.h:145
std::map< uint64, uint32 > SearchMap
Definition: PoolMgr.h:150
ActivePoolObjects mSpawnedCreatures
Definition: PoolMgr.h:62
bool isEmpty() const
Definition: PoolMgr.h:76
Definition: PoolMgr.h:69
Definition: PoolMgr.h:32
~PoolMgr()
Definition: PoolMgr.h:108
Definition: PoolMgr.h:39
uint32_t uint32
Definition: Define.h:150
static void Initialize(PCASC_SALSA20 pState, LPBYTE pbKey, DWORD cbKeyLength, LPBYTE pbVector)
Definition: CascDecrypt.cpp:81
uint64_t uint64
Definition: Define.h:149
PoolTemplateDataMap mPoolTemplate
Definition: PoolMgr.h:152
ActivePoolPools mSpawnedPools
Definition: PoolMgr.h:65
ActivePoolObjects mSpawnedGameobjects
Definition: PoolMgr.h:63
ActivePoolData mSpawnedData
Definition: PoolMgr.h:163
Definition: PoolMgr.h:104
ActivePoolObjects mActiveQuests
Definition: PoolMgr.h:64
std::vector< PoolGroup< Quest > > PoolGroupQuestMap
Definition: PoolMgr.h:148
std::pair< PooledQuestRelation::const_iterator, PooledQuestRelation::const_iterator > PooledQuestRelationBounds
Definition: PoolMgr.h:101
#define TC_GAME_API
Definition: Define.h:134
bool IsSpawnedObject(uint64 db_guid_or_pool_id) const
Definition: PoolMgr.h:123
std::vector< PoolGroup< Pool > > PoolGroupPoolMap
Definition: PoolMgr.h:147
uint32 MaxLimit
Definition: PoolMgr.h:29
std::vector< PoolObject > PoolObjectList
Definition: PoolMgr.h:71
std::pair< uint64, uint32 > SearchPair
Definition: PoolMgr.h:149
PoolGroupCreatureMap mPoolCreatureGroups
Definition: PoolMgr.h:153
float chance
Definition: PoolMgr.h:35
std::vector< PoolTemplateData > PoolTemplateDataMap
Definition: PoolMgr.h:144
uint64 guid
Definition: PoolMgr.h:34
ActivePoolObjects GetActiveQuests() const
Definition: PoolMgr.h:60
void SetPoolId(uint32 pool_id)
Definition: PoolMgr.h:74
PoolGroupGameObjectMap mPoolGameobjectGroups
Definition: PoolMgr.h:154
SearchMap mQuestSearchMap
Definition: PoolMgr.h:160
SearchMap mPoolSearchMap
Definition: PoolMgr.h:159
std::map< uint64, uint32 > ActivePoolPools
Definition: PoolMgr.h:44
void RemoveObject(T *object)
Definition: ObjectAccessor.h:104
uint32 GetPoolId() const
Definition: PoolMgr.h:93
uint32 poolId
Definition: PoolMgr.h:95