[9033] Fixed percent mana regneration from spell 53228 and ranks buff.
[getmangos.git] / src / game / BattleGroundMgr.h
blob7b646c6f8c97faebe2de48ec5b2bf21f3b7f8c15
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef __BATTLEGROUNDMGR_H
20 #define __BATTLEGROUNDMGR_H
22 #include "Common.h"
23 #include "Policies/Singleton.h"
24 #include "Utilities/EventProcessor.h"
25 #include "BattleGround.h"
26 #include "ace/Recursive_Thread_Mutex.h"
28 typedef std::map<uint32, BattleGround*> BattleGroundSet;
30 //this container can't be deque, because deque doesn't like removing the last element - if you remove it, it invalidates next iterator and crash appears
31 typedef std::list<BattleGround*> BGFreeSlotQueueType;
33 typedef UNORDERED_MAP<uint32, BattleGroundTypeId> BattleMastersMap;
34 typedef UNORDERED_MAP<uint32, BattleGroundEventIdx> CreatureBattleEventIndexesMap;
35 typedef UNORDERED_MAP<uint32, BattleGroundEventIdx> GameObjectBattleEventIndexesMap;
37 #define BATTLEGROUND_ARENA_POINT_DISTRIBUTION_DAY 86400 // seconds in a day
38 #define COUNT_OF_PLAYERS_TO_AVERAGE_WAIT_TIME 10
40 struct GroupQueueInfo; // type predefinition
41 struct PlayerQueueInfo // stores information for players in queue
43 uint32 LastOnlineTime; // for tracking and removing offline players from queue after 5 minutes
44 GroupQueueInfo * GroupInfo; // pointer to the associated groupqueueinfo
47 struct GroupQueueInfo // stores information about the group in queue (also used when joined as solo!)
49 std::map<uint64, PlayerQueueInfo*> Players; // player queue info map
50 uint32 Team; // Player team (ALLIANCE/HORDE)
51 BattleGroundTypeId BgTypeId; // battleground type id
52 bool IsRated; // rated
53 uint8 ArenaType; // 2v2, 3v3, 5v5 or 0 when BG
54 uint32 ArenaTeamId; // team id if rated match
55 uint32 JoinTime; // time when group was added
56 uint32 RemoveInviteTime; // time when we will remove invite for players in group
57 uint32 IsInvitedToBGInstanceGUID; // was invited to certain BG
58 uint32 ArenaTeamRating; // if rated match, inited to the rating of the team
59 uint32 OpponentsTeamRating; // for rated arena matches
62 enum BattleGroundQueueGroupTypes
64 BG_QUEUE_PREMADE_ALLIANCE = 0,
65 BG_QUEUE_PREMADE_HORDE = 1,
66 BG_QUEUE_NORMAL_ALLIANCE = 2,
67 BG_QUEUE_NORMAL_HORDE = 3
69 #define BG_QUEUE_GROUP_TYPES_COUNT 4
71 class BattleGround;
72 class BattleGroundQueue
74 public:
75 BattleGroundQueue();
76 ~BattleGroundQueue();
78 void Update(BattleGroundTypeId bgTypeId, BGQueueIdBasedOnLevel queue_id, uint8 arenaType = 0, bool isRated = false, uint32 minRating = 0);
80 void FillPlayersToBG(BattleGround* bg, BGQueueIdBasedOnLevel queue_id);
81 bool CheckPremadeMatch(BGQueueIdBasedOnLevel queue_id, uint32 MinPlayersPerTeam, uint32 MaxPlayersPerTeam);
82 bool CheckNormalMatch(BattleGround* bg_template, BGQueueIdBasedOnLevel queue_id, uint32 minPlayers, uint32 maxPlayers);
83 bool CheckSkirmishForSameFaction(BGQueueIdBasedOnLevel queue_id, uint32 minPlayersPerTeam);
84 GroupQueueInfo * AddGroup(Player* leader, Group* group, BattleGroundTypeId bgTypeId, uint8 ArenaType, bool isRated, bool isPremade, uint32 ArenaRating, uint32 ArenaTeamId = 0);
85 void RemovePlayer(const uint64& guid, bool decreaseInvitedCount);
86 bool IsPlayerInvited(const uint64& pl_guid, const uint32 bgInstanceGuid, const uint32 removeTime);
87 bool GetPlayerGroupInfoData(const uint64& guid, GroupQueueInfo* ginfo);
88 void PlayerInvitedToBGUpdateAverageWaitTime(GroupQueueInfo* ginfo, BGQueueIdBasedOnLevel queue_id);
89 uint32 GetAverageQueueWaitTime(GroupQueueInfo* ginfo, BGQueueIdBasedOnLevel queue_id);
91 private:
92 //mutex that should not allow changing private data, nor allowing to update Queue during private data change.
93 ACE_Recursive_Thread_Mutex m_Lock;
96 typedef std::map<uint64, PlayerQueueInfo> QueuedPlayersMap;
97 QueuedPlayersMap m_QueuedPlayers;
99 //we need constant add to begin and constant remove / add from the end, therefore deque suits our problem well
100 typedef std::list<GroupQueueInfo*> GroupsQueueType;
103 This two dimensional array is used to store All queued groups
104 First dimension specifies the bgTypeId
105 Second dimension specifies the player's group types -
106 BG_QUEUE_PREMADE_ALLIANCE is used for premade alliance groups and alliance rated arena teams
107 BG_QUEUE_PREMADE_HORDE is used for premade horde groups and horde rated arena teams
108 BG_QUEUE_NORMAL_ALLIANCE is used for normal (or small) alliance groups or non-rated arena matches
109 BG_QUEUE_NORMAL_HORDE is used for normal (or small) horde groups or non-rated arena matches
111 GroupsQueueType m_QueuedGroups[MAX_BATTLEGROUND_QUEUES][BG_QUEUE_GROUP_TYPES_COUNT];
113 // class to select and invite groups to bg
114 class SelectionPool
116 public:
117 void Init();
118 bool AddGroup(GroupQueueInfo *ginfo, uint32 desiredCount);
119 bool KickGroup(uint32 size);
120 uint32 GetPlayerCount() const {return PlayerCount;}
121 public:
122 GroupsQueueType SelectedGroups;
123 private:
124 uint32 PlayerCount;
127 //one selection pool for horde, other one for alliance
128 SelectionPool m_SelectionPools[BG_TEAMS_COUNT];
130 bool InviteGroupToBG(GroupQueueInfo * ginfo, BattleGround * bg, uint32 side);
131 uint32 m_WaitTimes[BG_TEAMS_COUNT][MAX_BATTLEGROUND_QUEUES][COUNT_OF_PLAYERS_TO_AVERAGE_WAIT_TIME];
132 uint32 m_WaitTimeLastPlayer[BG_TEAMS_COUNT][MAX_BATTLEGROUND_QUEUES];
133 uint32 m_SumOfWaitTimes[BG_TEAMS_COUNT][MAX_BATTLEGROUND_QUEUES];
137 This class is used to invite player to BG again, when minute lasts from his first invitation
138 it is capable to solve all possibilities
140 class BGQueueInviteEvent : public BasicEvent
142 public:
143 BGQueueInviteEvent(const uint64& pl_guid, uint32 BgInstanceGUID, BattleGroundTypeId BgTypeId, uint8 arenaType, uint32 removeTime) :
144 m_PlayerGuid(pl_guid), m_BgInstanceGUID(BgInstanceGUID), m_BgTypeId(BgTypeId), m_ArenaType(arenaType), m_RemoveTime(removeTime)
147 virtual ~BGQueueInviteEvent() {};
149 virtual bool Execute(uint64 e_time, uint32 p_time);
150 virtual void Abort(uint64 e_time);
151 private:
152 uint64 m_PlayerGuid;
153 uint32 m_BgInstanceGUID;
154 BattleGroundTypeId m_BgTypeId;
155 uint8 m_ArenaType;
156 uint32 m_RemoveTime;
160 This class is used to remove player from BG queue after 1 minute 20 seconds from first invitation
161 We must store removeInvite time in case player left queue and joined and is invited again
162 We must store bgQueueTypeId, because battleground can be deleted already, when player entered it
164 class BGQueueRemoveEvent : public BasicEvent
166 public:
167 BGQueueRemoveEvent(const uint64& pl_guid, uint32 bgInstanceGUID, BattleGroundTypeId BgTypeId, BattleGroundQueueTypeId bgQueueTypeId, uint32 removeTime)
168 : m_PlayerGuid(pl_guid), m_BgInstanceGUID(bgInstanceGUID), m_RemoveTime(removeTime), m_BgTypeId(BgTypeId), m_BgQueueTypeId(bgQueueTypeId)
171 virtual ~BGQueueRemoveEvent() {}
173 virtual bool Execute(uint64 e_time, uint32 p_time);
174 virtual void Abort(uint64 e_time);
175 private:
176 uint64 m_PlayerGuid;
177 uint32 m_BgInstanceGUID;
178 uint32 m_RemoveTime;
179 BattleGroundTypeId m_BgTypeId;
180 BattleGroundQueueTypeId m_BgQueueTypeId;
183 class BattleGroundMgr
185 public:
186 /* Construction */
187 BattleGroundMgr();
188 ~BattleGroundMgr();
189 void Update(uint32 diff);
191 /* Packet Building */
192 void BuildPlayerJoinedBattleGroundPacket(WorldPacket *data, Player *plr);
193 void BuildPlayerLeftBattleGroundPacket(WorldPacket *data, const uint64& guid);
194 void BuildBattleGroundListPacket(WorldPacket *data, const uint64& guid, Player *plr, BattleGroundTypeId bgTypeId, uint8 fromWhere);
195 void BuildGroupJoinedBattlegroundPacket(WorldPacket *data, BattleGroundTypeId bgTypeId);
196 void BuildUpdateWorldStatePacket(WorldPacket *data, uint32 field, uint32 value);
197 void BuildPvpLogDataPacket(WorldPacket *data, BattleGround *bg);
198 void BuildBattleGroundStatusPacket(WorldPacket *data, BattleGround *bg, uint8 QueueSlot, uint8 StatusID, uint32 Time1, uint32 Time2, uint8 arenatype);
199 void BuildPlaySoundPacket(WorldPacket *data, uint32 soundid);
201 /* Battlegrounds */
202 BattleGround* GetBattleGroundThroughClientInstance(uint32 instanceId, BattleGroundTypeId bgTypeId);
203 BattleGround* GetBattleGround(uint32 InstanceID, BattleGroundTypeId bgTypeId); //there must be uint32 because MAX_BATTLEGROUND_TYPE_ID means unknown
205 BattleGround* GetBattleGroundTemplate(BattleGroundTypeId bgTypeId);
206 BattleGround* CreateNewBattleGround(BattleGroundTypeId bgTypeId, BGQueueIdBasedOnLevel queue_id, uint8 arenaType, bool isRated);
208 uint32 CreateBattleGround(BattleGroundTypeId bgTypeId, bool IsArena, uint32 MinPlayersPerTeam, uint32 MaxPlayersPerTeam, uint32 LevelMin, uint32 LevelMax, char* BattleGroundName, uint32 MapID, float Team1StartLocX, float Team1StartLocY, float Team1StartLocZ, float Team1StartLocO, float Team2StartLocX, float Team2StartLocY, float Team2StartLocZ, float Team2StartLocO);
210 void AddBattleGround(uint32 InstanceID, BattleGroundTypeId bgTypeId, BattleGround* BG) { m_BattleGrounds[bgTypeId][InstanceID] = BG; };
211 void RemoveBattleGround(uint32 instanceID, BattleGroundTypeId bgTypeId) { m_BattleGrounds[bgTypeId].erase(instanceID); }
212 uint32 CreateClientVisibleInstanceId(BattleGroundTypeId bgTypeId, BGQueueIdBasedOnLevel queue_id);
214 void CreateInitialBattleGrounds();
215 void DeleteAllBattleGrounds();
217 void SendToBattleGround(Player *pl, uint32 InstanceID, BattleGroundTypeId bgTypeId);
219 /* Battleground queues */
220 //these queues are instantiated when creating BattlegroundMrg
221 BattleGroundQueue m_BattleGroundQueues[MAX_BATTLEGROUND_QUEUE_TYPES]; // public, because we need to access them in BG handler code
223 BGFreeSlotQueueType BGFreeSlotQueue[MAX_BATTLEGROUND_TYPE_ID];
225 void ScheduleQueueUpdate(uint32 arenaRating, uint8 arenaType, BattleGroundQueueTypeId bgQueueTypeId, BattleGroundTypeId bgTypeId, BGQueueIdBasedOnLevel queue_id);
226 uint32 GetMaxRatingDifference() const;
227 uint32 GetRatingDiscardTimer() const;
228 uint32 GetPrematureFinishTime() const;
230 void InitAutomaticArenaPointDistribution();
231 void DistributeArenaPoints();
232 void ToggleArenaTesting();
233 void ToggleTesting();
235 void LoadBattleMastersEntry();
236 BattleGroundTypeId GetBattleMasterBG(uint32 entry) const
238 BattleMastersMap::const_iterator itr = mBattleMastersMap.find(entry);
239 if (itr != mBattleMastersMap.end())
240 return itr->second;
241 return BATTLEGROUND_TYPE_NONE;
244 void LoadBattleEventIndexes();
245 const BattleGroundEventIdx GetCreatureEventIndex(uint32 dbTableGuidLow) const
247 CreatureBattleEventIndexesMap::const_iterator itr = m_CreatureBattleEventIndexMap.find(dbTableGuidLow);
248 if(itr != m_CreatureBattleEventIndexMap.end())
249 return itr->second;
250 return m_CreatureBattleEventIndexMap.find(-1)->second;
252 const BattleGroundEventIdx GetGameObjectEventIndex(uint32 dbTableGuidLow) const
254 GameObjectBattleEventIndexesMap::const_iterator itr = m_GameObjectBattleEventIndexMap.find(dbTableGuidLow);
255 if(itr != m_GameObjectBattleEventIndexMap.end())
256 return itr->second;
257 return m_GameObjectBattleEventIndexMap.find(-1)->second;
260 bool isArenaTesting() const { return m_ArenaTesting; }
261 bool isTesting() const { return m_Testing; }
263 static bool IsArenaType(BattleGroundTypeId bgTypeId);
264 static bool IsBattleGroundType(BattleGroundTypeId bgTypeId) { return !BattleGroundMgr::IsArenaType(bgTypeId); }
265 static BattleGroundQueueTypeId BGQueueTypeId(BattleGroundTypeId bgTypeId, uint8 arenaType);
266 static BattleGroundTypeId BGTemplateId(BattleGroundQueueTypeId bgQueueTypeId);
267 static uint8 BGArenaType(BattleGroundQueueTypeId bgQueueTypeId);
269 static bool IsBGWeekend(BattleGroundTypeId bgTypeId);
270 private:
271 ACE_Thread_Mutex SchedulerLock;
272 BattleMastersMap mBattleMastersMap;
273 CreatureBattleEventIndexesMap m_CreatureBattleEventIndexMap;
274 GameObjectBattleEventIndexesMap m_GameObjectBattleEventIndexMap;
276 /* Battlegrounds */
277 BattleGroundSet m_BattleGrounds[MAX_BATTLEGROUND_TYPE_ID];
278 std::vector<uint64> m_QueueUpdateScheduler;
279 std::set<uint32> m_ClientBattleGroundIds[MAX_BATTLEGROUND_TYPE_ID][MAX_BATTLEGROUND_QUEUES]; //the instanceids just visible for the client
280 uint32 m_NextRatingDiscardUpdate;
281 time_t m_NextAutoDistributionTime;
282 uint32 m_AutoDistributionTimeChecker;
283 bool m_ArenaTesting;
284 bool m_Testing;
287 #define sBattleGroundMgr MaNGOS::Singleton<BattleGroundMgr>::Instance()
288 #endif