summaryrefslogtreecommitdiff
path: root/src/server/game/Pools/PoolMgr.h
blob: c4b1af00b80b0e0cd727a09d9ca3018d02e16955 (plain)
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
/*
 * Copyright (C) 2016+     AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
 * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
 */

#ifndef ACORE_POOLHANDLER_H
#define ACORE_POOLHANDLER_H

#include "Creature.h"
#include "Define.h"
#include "GameObject.h"
#include "QuestDef.h"

struct PoolTemplateData
{
    uint32  MaxLimit;
};

struct PoolObject
{
    uint32  guid;
    float   chance;
    PoolObject(uint32 _guid, float _chance): guid(_guid), chance(fabs(_chance)) {}
};

class Pool                                                  // for Pool of Pool case
{
};

typedef std::unordered_set<uint32> ActivePoolObjects;
typedef std::map<uint32, uint32> ActivePoolPools;

class ActivePoolData
{
public:
    template<typename T>
    bool IsActiveObject(uint32 db_guid_or_pool_id) const;

    uint32 GetActiveObjectCount(uint32 pool_id) const;

    template<typename T>
    void ActivateObject(uint32 db_guid_or_pool_id, uint32 pool_id);

    template<typename T>
    void RemoveObject(uint32 db_guid_or_pool_id, uint32 pool_id);

    ActivePoolObjects GetActiveQuests() const { return mActiveQuests; } // a copy of the set
private:
    ActivePoolObjects mSpawnedCreatures;
    ActivePoolObjects mSpawnedGameobjects;
    ActivePoolObjects mActiveQuests;
    ActivePoolPools   mSpawnedPools;
};

template <class T>
class PoolGroup
{
    typedef std::vector<PoolObject> PoolObjectList;
public:
    explicit PoolGroup() : poolId(0) { }
    void SetPoolId(uint32 pool_id) { poolId = pool_id; }
    ~PoolGroup() {};
    bool isEmpty() const { return ExplicitlyChanced.empty() && EqualChanced.empty(); }
    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()
    {
        if (EqualChanced.empty())
            return 0;
        return EqualChanced.front().guid;
    }
    uint32 GetPoolId() const { return poolId; }
private:
    uint32 poolId;
    PoolObjectList ExplicitlyChanced;
    PoolObjectList EqualChanced;
};

typedef std::multimap<uint32, uint32> PooledQuestRelation;
typedef std::pair<PooledQuestRelation::const_iterator, PooledQuestRelation::const_iterator> PooledQuestRelationBounds;
typedef std::pair<PooledQuestRelation::iterator, PooledQuestRelation::iterator> PooledQuestRelationBoundsNC;

class PoolMgr
{
private:
    PoolMgr();
    ~PoolMgr() {};

public:
    static PoolMgr* instance();

    void LoadFromDB();
    void LoadQuestPools();
    void SaveQuestsToDB(bool daily, bool weekly, bool other);

    void Initialize();

    template<typename T>
    uint32 IsPartOfAPool(uint32 db_guid_or_pool_id) const;

    template<typename T>
    bool IsSpawnedObject(uint32 db_guid_or_pool_id) const { return mSpawnedData.IsActiveObject<T>(db_guid_or_pool_id); }

    bool CheckPool(uint32 pool_id) const;

    void SpawnPool(uint32 pool_id);
    void DespawnPool(uint32 pool_id);

    template<typename T>
    void UpdatePool(uint32 pool_id, uint32 db_guid_or_pool_id);

    void ChangeDailyQuests();
    void ChangeWeeklyQuests();

    PooledQuestRelation mQuestCreatureRelation;
    PooledQuestRelation mQuestGORelation;

private:
    template<typename T>
    void SpawnPool(uint32 pool_id, uint32 db_guid_or_pool_id);

    typedef std::unordered_map<uint32, PoolTemplateData>      PoolTemplateDataMap;
    typedef std::unordered_map<uint32, PoolGroup<Creature>>   PoolGroupCreatureMap;
    typedef std::unordered_map<uint32, PoolGroup<GameObject>> PoolGroupGameObjectMap;
    typedef std::unordered_map<uint32, PoolGroup<Pool>>       PoolGroupPoolMap;
    typedef std::unordered_map<uint32, PoolGroup<Quest>>      PoolGroupQuestMap;
    typedef std::pair<uint32, uint32>           SearchPair;
    typedef std::map<uint32, uint32>            SearchMap;

    PoolTemplateDataMap    mPoolTemplate;
    PoolGroupCreatureMap   mPoolCreatureGroups;
    PoolGroupGameObjectMap mPoolGameobjectGroups;
    PoolGroupPoolMap       mPoolPoolGroups;
    PoolGroupQuestMap      mPoolQuestGroups;
    SearchMap mCreatureSearchMap;
    SearchMap mGameobjectSearchMap;
    SearchMap mPoolSearchMap;
    SearchMap mQuestSearchMap;

    // dynamic data
    ActivePoolData mSpawnedData;
};

#define sPoolMgr PoolMgr::instance()

// Method that tell if the creature is part of a pool and return the pool id if yes
template<>
inline uint32 PoolMgr::IsPartOfAPool<Creature>(uint32 db_guid) const
{
    SearchMap::const_iterator itr = mCreatureSearchMap.find(db_guid);
    if (itr != mCreatureSearchMap.end())
        return itr->second;

    return 0;
}

// Method that tell if the gameobject is part of a pool and return the pool id if yes
template<>
inline uint32 PoolMgr::IsPartOfAPool<GameObject>(uint32 db_guid) const
{
    SearchMap::const_iterator itr = mGameobjectSearchMap.find(db_guid);
    if (itr != mGameobjectSearchMap.end())
        return itr->second;

    return 0;
}

// Method that tell if the quest is part of another pool and return the pool id if yes
template<>
inline uint32 PoolMgr::IsPartOfAPool<Quest>(uint32 questId) const
{
    SearchMap::const_iterator itr = mQuestSearchMap.find(questId);
    if (itr != mQuestSearchMap.end())
        return itr->second;

    return 0;
}

// Method that tell if the pool is part of another pool and return the pool id if yes
template<>
inline uint32 PoolMgr::IsPartOfAPool<Pool>(uint32 pool_id) const
{
    SearchMap::const_iterator itr = mPoolSearchMap.find(pool_id);
    if (itr != mPoolSearchMap.end())
        return itr->second;

    return 0;
}

#endif