[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / BattleGroundMgr.h
blob73352f6690a8ae941beeff03606df06b723a4c33
1 /*
2 * Copyright (C) 2005-2010 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 "DBCEnums.h"
26 #include "BattleGround.h"
27 #include "ace/Recursive_Thread_Mutex.h"
29 typedef std::map<uint32, BattleGround*> BattleGroundSet;
31 //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
32 typedef std::list<BattleGround*> BGFreeSlotQueueType;
34 typedef UNORDERED_MAP<uint32, BattleGroundTypeId> BattleMastersMap;
35 typedef UNORDERED_MAP<uint32, BattleGroundEventIdx> CreatureBattleEventIndexesMap;
36 typedef UNORDERED_MAP<uint32, BattleGroundEventIdx> GameObjectBattleEventIndexesMap;
38 #define BATTLEGROUND_ARENA_POINT_DISTRIBUTION_DAY 86400 // seconds in a day
39 #define COUNT_OF_PLAYERS_TO_AVERAGE_WAIT_TIME 10
41 struct GroupQueueInfo; // type predefinition
42 struct PlayerQueueInfo // stores information for players in queue
44 uint32 LastOnlineTime; // for tracking and removing offline players from queue after 5 minutes
45 GroupQueueInfo * GroupInfo; // pointer to the associated groupqueueinfo
48 struct GroupQueueInfo // stores information about the group in queue (also used when joined as solo!)
50 std::map<uint64, PlayerQueueInfo*> Players; // player queue info map
51 uint32 Team; // Player team (ALLIANCE/HORDE)
52 BattleGroundTypeId BgTypeId; // battleground type id
53 bool IsRated; // rated
54 uint8 ArenaType; // 2v2, 3v3, 5v5 or 0 when BG
55 uint32 ArenaTeamId; // team id if rated match
56 uint32 JoinTime; // time when group was added
57 uint32 RemoveInviteTime; // time when we will remove invite for players in group
58 uint32 IsInvitedToBGInstanceGUID; // was invited to certain BG
59 uint32 ArenaTeamRating; // if rated match, inited to the rating of the team
60 uint32 OpponentsTeamRating; // for rated arena matches
63 enum BattleGroundQueueGroupTypes
65 BG_QUEUE_PREMADE_ALLIANCE = 0,
66 BG_QUEUE_PREMADE_HORDE = 1,
67 BG_QUEUE_NORMAL_ALLIANCE = 2,
68 BG_QUEUE_NORMAL_HORDE = 3
70 #define BG_QUEUE_GROUP_TYPES_COUNT 4
72 class BattleGround;
73 class BattleGroundQueue
75 public:
76 BattleGroundQueue();
77 ~BattleGroundQueue();
79 void Update(BattleGroundTypeId bgTypeId, BattleGroundBracketId bracket_id, uint8 arenaType = 0, bool isRated = false, uint32 minRating = 0);
81 void FillPlayersToBG(BattleGround* bg, BattleGroundBracketId bracket_id);
82 bool CheckPremadeMatch(BattleGroundBracketId bracket_id, uint32 MinPlayersPerTeam, uint32 MaxPlayersPerTeam);
83 bool CheckNormalMatch(BattleGround* bg_template, BattleGroundBracketId bracket_id, uint32 minPlayers, uint32 maxPlayers);
84 bool CheckSkirmishForSameFaction(BattleGroundBracketId bracket_id, uint32 minPlayersPerTeam);
85 GroupQueueInfo * AddGroup(Player* leader, Group* group, BattleGroundTypeId bgTypeId, PvPDifficultyEntry const* backetEntry, uint8 ArenaType, bool isRated, bool isPremade, uint32 ArenaRating, uint32 ArenaTeamId = 0);
86 void RemovePlayer(const uint64& guid, bool decreaseInvitedCount);
87 bool IsPlayerInvited(const uint64& pl_guid, const uint32 bgInstanceGuid, const uint32 removeTime);
88 bool GetPlayerGroupInfoData(const uint64& guid, GroupQueueInfo* ginfo);
89 void PlayerInvitedToBGUpdateAverageWaitTime(GroupQueueInfo* ginfo, BattleGroundBracketId bracket_id);
90 uint32 GetAverageQueueWaitTime(GroupQueueInfo* ginfo, BattleGroundBracketId bracket_id);
92 private:
93 //mutex that should not allow changing private data, nor allowing to update Queue during private data change.
94 ACE_Recursive_Thread_Mutex m_Lock;
97 typedef std::map<uint64, PlayerQueueInfo> QueuedPlayersMap;
98 QueuedPlayersMap m_QueuedPlayers;
100 //we need constant add to begin and constant remove / add from the end, therefore deque suits our problem well
101 typedef std::list<GroupQueueInfo*> GroupsQueueType;
104 This two dimensional array is used to store All queued groups
105 First dimension specifies the bgTypeId
106 Second dimension specifies the player's group types -
107 BG_QUEUE_PREMADE_ALLIANCE is used for premade alliance groups and alliance rated arena teams
108 BG_QUEUE_PREMADE_HORDE is used for premade horde groups and horde rated arena teams
109 BG_QUEUE_NORMAL_ALLIANCE is used for normal (or small) alliance groups or non-rated arena matches
110 BG_QUEUE_NORMAL_HORDE is used for normal (or small) horde groups or non-rated arena matches
112 GroupsQueueType m_QueuedGroups[MAX_BATTLEGROUND_BRACKETS][BG_QUEUE_GROUP_TYPES_COUNT];
114 // class to select and invite groups to bg
115 class SelectionPool
117 public:
118 void Init();
119 bool AddGroup(GroupQueueInfo *ginfo, uint32 desiredCount);
120 bool KickGroup(uint32 size);
121 uint32 GetPlayerCount() const {return PlayerCount;}
122 public:
123 GroupsQueueType SelectedGroups;
124 private:
125 uint32 PlayerCount;
128 //one selection pool for horde, other one for alliance
129 SelectionPool m_SelectionPools[BG_TEAMS_COUNT];
131 bool InviteGroupToBG(GroupQueueInfo * ginfo, BattleGround * bg, uint32 side);
132 uint32 m_WaitTimes[BG_TEAMS_COUNT][MAX_BATTLEGROUND_BRACKETS][COUNT_OF_PLAYERS_TO_AVERAGE_WAIT_TIME];
133 uint32 m_WaitTimeLastPlayer[BG_TEAMS_COUNT][MAX_BATTLEGROUND_BRACKETS];
134 uint32 m_SumOfWaitTimes[BG_TEAMS_COUNT][MAX_BATTLEGROUND_BRACKETS];
138 This class is used to invite player to BG again, when minute lasts from his first invitation
139 it is capable to solve all possibilities
141 class BGQueueInviteEvent : public BasicEvent
143 public:
144 BGQueueInviteEvent(const uint64& pl_guid, uint32 BgInstanceGUID, BattleGroundTypeId BgTypeId, uint8 arenaType, uint32 removeTime) :
145 m_PlayerGuid(pl_guid), m_BgInstanceGUID(BgInstanceGUID), m_BgTypeId(BgTypeId), m_ArenaType(arenaType), m_RemoveTime(removeTime)
148 virtual ~BGQueueInviteEvent() {};
150 virtual bool Execute(uint64 e_time, uint32 p_time);
151 virtual void Abort(uint64 e_time);
152 private:
153 uint64 m_PlayerGuid;
154 uint32 m_BgInstanceGUID;
155 BattleGroundTypeId m_BgTypeId;
156 uint8 m_ArenaType;
157 uint32 m_RemoveTime;
161 This class is used to remove player from BG queue after 1 minute 20 seconds from first invitation
162 We must store removeInvite time in case player left queue and joined and is invited again
163 We must store bgQueueTypeId, because battleground can be deleted already, when player entered it
165 class BGQueueRemoveEvent : public BasicEvent
167 public:
168 BGQueueRemoveEvent(const uint64& pl_guid, uint32 bgInstanceGUID, BattleGroundTypeId BgTypeId, BattleGroundQueueTypeId bgQueueTypeId, uint32 removeTime)
169 : m_PlayerGuid(pl_guid), m_BgInstanceGUID(bgInstanceGUID), m_RemoveTime(removeTime), m_BgTypeId(BgTypeId), m_BgQueueTypeId(bgQueueTypeId)
172 virtual ~BGQueueRemoveEvent() {}
174 virtual bool Execute(uint64 e_time, uint32 p_time);
175 virtual void Abort(uint64 e_time);
176 private:
177 uint64 m_PlayerGuid;
178 uint32 m_BgInstanceGUID;
179 uint32 m_RemoveTime;
180 BattleGroundTypeId m_BgTypeId;
181 BattleGroundQueueTypeId m_BgQueueTypeId;
184 class BattleGroundMgr
186 public:
187 /* Construction */
188 BattleGroundMgr();
189 ~BattleGroundMgr();
190 void Update(uint32 diff);
192 /* Packet Building */
193 void BuildPlayerJoinedBattleGroundPacket(WorldPacket *data, Player *plr);
194 void BuildPlayerLeftBattleGroundPacket(WorldPacket *data, const uint64& guid);
195 void BuildBattleGroundListPacket(WorldPacket *data, const uint64& guid, Player *plr, BattleGroundTypeId bgTypeId, uint8 fromWhere);
196 void BuildGroupJoinedBattlegroundPacket(WorldPacket *data, BattleGroundTypeId bgTypeId);
197 void BuildUpdateWorldStatePacket(WorldPacket *data, uint32 field, uint32 value);
198 void BuildPvpLogDataPacket(WorldPacket *data, BattleGround *bg);
199 void BuildBattleGroundStatusPacket(WorldPacket *data, BattleGround *bg, uint8 QueueSlot, uint8 StatusID, uint32 Time1, uint32 Time2, uint8 arenatype);
200 void BuildPlaySoundPacket(WorldPacket *data, uint32 soundid);
202 /* Battlegrounds */
203 BattleGround* GetBattleGroundThroughClientInstance(uint32 instanceId, BattleGroundTypeId bgTypeId);
204 BattleGround* GetBattleGround(uint32 InstanceID, BattleGroundTypeId bgTypeId); //there must be uint32 because MAX_BATTLEGROUND_TYPE_ID means unknown
206 BattleGround* GetBattleGroundTemplate(BattleGroundTypeId bgTypeId);
207 BattleGround* CreateNewBattleGround(BattleGroundTypeId bgTypeId, PvPDifficultyEntry const* bracketEntry, uint8 arenaType, bool isRated);
209 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);
211 void AddBattleGround(uint32 InstanceID, BattleGroundTypeId bgTypeId, BattleGround* BG) { m_BattleGrounds[bgTypeId][InstanceID] = BG; };
212 void RemoveBattleGround(uint32 instanceID, BattleGroundTypeId bgTypeId) { m_BattleGrounds[bgTypeId].erase(instanceID); }
213 uint32 CreateClientVisibleInstanceId(BattleGroundTypeId bgTypeId, BattleGroundBracketId bracket_id);
215 void CreateInitialBattleGrounds();
216 void DeleteAllBattleGrounds();
218 void SendToBattleGround(Player *pl, uint32 InstanceID, BattleGroundTypeId bgTypeId);
220 /* Battleground queues */
221 //these queues are instantiated when creating BattlegroundMrg
222 BattleGroundQueue m_BattleGroundQueues[MAX_BATTLEGROUND_QUEUE_TYPES]; // public, because we need to access them in BG handler code
224 BGFreeSlotQueueType BGFreeSlotQueue[MAX_BATTLEGROUND_TYPE_ID];
226 void ScheduleQueueUpdate(uint32 arenaRating, uint8 arenaType, BattleGroundQueueTypeId bgQueueTypeId, BattleGroundTypeId bgTypeId, BattleGroundBracketId bracket_id);
227 uint32 GetMaxRatingDifference() const;
228 uint32 GetRatingDiscardTimer() const;
229 uint32 GetPrematureFinishTime() const;
231 void InitAutomaticArenaPointDistribution();
232 void DistributeArenaPoints();
233 void ToggleArenaTesting();
234 void ToggleTesting();
236 void LoadBattleMastersEntry();
237 BattleGroundTypeId GetBattleMasterBG(uint32 entry) const
239 BattleMastersMap::const_iterator itr = mBattleMastersMap.find(entry);
240 if (itr != mBattleMastersMap.end())
241 return itr->second;
242 return BATTLEGROUND_TYPE_NONE;
245 void LoadBattleEventIndexes();
246 const BattleGroundEventIdx GetCreatureEventIndex(uint32 dbTableGuidLow) const
248 CreatureBattleEventIndexesMap::const_iterator itr = m_CreatureBattleEventIndexMap.find(dbTableGuidLow);
249 if(itr != m_CreatureBattleEventIndexMap.end())
250 return itr->second;
251 return m_CreatureBattleEventIndexMap.find(-1)->second;
253 const BattleGroundEventIdx GetGameObjectEventIndex(uint32 dbTableGuidLow) const
255 GameObjectBattleEventIndexesMap::const_iterator itr = m_GameObjectBattleEventIndexMap.find(dbTableGuidLow);
256 if(itr != m_GameObjectBattleEventIndexMap.end())
257 return itr->second;
258 return m_GameObjectBattleEventIndexMap.find(-1)->second;
261 bool isArenaTesting() const { return m_ArenaTesting; }
262 bool isTesting() const { return m_Testing; }
264 static bool IsArenaType(BattleGroundTypeId bgTypeId);
265 static bool IsBattleGroundType(BattleGroundTypeId bgTypeId) { return !BattleGroundMgr::IsArenaType(bgTypeId); }
266 static BattleGroundQueueTypeId BGQueueTypeId(BattleGroundTypeId bgTypeId, uint8 arenaType);
267 static BattleGroundTypeId BGTemplateId(BattleGroundQueueTypeId bgQueueTypeId);
268 static uint8 BGArenaType(BattleGroundQueueTypeId bgQueueTypeId);
270 static bool IsBGWeekend(BattleGroundTypeId bgTypeId);
271 private:
272 ACE_Thread_Mutex SchedulerLock;
273 BattleMastersMap mBattleMastersMap;
274 CreatureBattleEventIndexesMap m_CreatureBattleEventIndexMap;
275 GameObjectBattleEventIndexesMap m_GameObjectBattleEventIndexMap;
277 /* Battlegrounds */
278 BattleGroundSet m_BattleGrounds[MAX_BATTLEGROUND_TYPE_ID];
279 std::vector<uint64> m_QueueUpdateScheduler;
280 std::set<uint32> m_ClientBattleGroundIds[MAX_BATTLEGROUND_TYPE_ID][MAX_BATTLEGROUND_BRACKETS]; //the instanceids just visible for the client
281 uint32 m_NextRatingDiscardUpdate;
282 time_t m_NextAutoDistributionTime;
283 uint32 m_AutoDistributionTimeChecker;
284 bool m_ArenaTesting;
285 bool m_Testing;
288 #define sBattleGroundMgr MaNGOS::Singleton<BattleGroundMgr>::Instance()
289 #endif