[7022] Added support for packets > 64 kb
[getmangos.git] / src / game / BattleGroundMgr.cpp
blobfc6266eb28be3ef92b7fdefb528fd1362ab693e5
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 #include "Common.h"
20 #include "Player.h"
21 #include "BattleGroundMgr.h"
22 #include "BattleGroundAV.h"
23 #include "BattleGroundAB.h"
24 #include "BattleGroundEY.h"
25 #include "BattleGroundWS.h"
26 #include "BattleGroundNA.h"
27 #include "BattleGroundBE.h"
28 #include "BattleGroundAA.h"
29 #include "BattleGroundRL.h"
30 #include "SharedDefines.h"
31 #include "Policies/SingletonImp.h"
32 #include "MapManager.h"
33 #include "Map.h"
34 #include "MapInstanced.h"
35 #include "ObjectMgr.h"
36 #include "ProgressBar.h"
37 #include "World.h"
38 #include "Chat.h"
39 #include "ArenaTeam.h"
41 INSTANTIATE_SINGLETON_1( BattleGroundMgr );
43 /*********************************************************/
44 /*** BATTLEGROUND QUEUE SYSTEM ***/
45 /*********************************************************/
47 BattleGroundQueue::BattleGroundQueue()
49 //queues are empty, we don't have to call clear()
50 /* for (int i = 0; i < MAX_BATTLEGROUND_QUEUES; i++)
52 //m_QueuedPlayers[i].Horde = 0;
53 //m_QueuedPlayers[i].Alliance = 0;
54 //m_QueuedPlayers[i].AverageTime = 0;
55 }*/
58 BattleGroundQueue::~BattleGroundQueue()
60 for (int i = 0; i < MAX_BATTLEGROUND_QUEUES; i++)
62 m_QueuedPlayers[i].clear();
63 for(QueuedGroupsList::iterator itr = m_QueuedGroups[i].begin(); itr!= m_QueuedGroups[i].end(); ++itr)
65 delete (*itr);
67 m_QueuedGroups[i].clear();
71 // initialize eligible groups from the given source matching the given specifications
72 void BattleGroundQueue::EligibleGroups::Init(BattleGroundQueue::QueuedGroupsList *source, uint32 BgTypeId, uint32 side, uint32 MaxPlayers, uint8 ArenaType, bool IsRated, uint32 MinRating, uint32 MaxRating, uint32 DisregardTime, uint32 excludeTeam)
74 // clear from prev initialization
75 clear();
76 BattleGroundQueue::QueuedGroupsList::iterator itr, next;
77 // iterate through the source
78 for(itr = source->begin(); itr!= source->end(); itr = next)
80 next = itr;
81 ++next;
82 if( (*itr)->BgTypeId == BgTypeId && // bg type must match
83 (*itr)->ArenaType == ArenaType && // arena type must match
84 (*itr)->IsRated == IsRated && // israted must match
85 (*itr)->IsInvitedToBGInstanceGUID == 0 && // leave out already invited groups
86 (*itr)->Team == side && // match side
87 (*itr)->Players.size() <= MaxPlayers && // the group must fit in the bg
88 ( !excludeTeam || (*itr)->ArenaTeamId != excludeTeam ) && // if excludeTeam is specified, leave out those arena team ids
89 ( !IsRated || (*itr)->Players.size() == MaxPlayers ) && // if rated, then pass only if the player count is exact NEEDS TESTING! (but now this should never happen)
90 ( (*itr)->JoinTime <= DisregardTime // pass if disregard time is greater than join time
91 || (*itr)->ArenaTeamRating == 0 // pass if no rating info
92 || ( (*itr)->ArenaTeamRating >= MinRating // pass if matches the rating range
93 && (*itr)->ArenaTeamRating <= MaxRating ) ) )
95 // the group matches the conditions
96 // insert it in order of groupsize, and join time
97 uint32 size = (*itr)->Players.size();
98 uint32 jointime = (*itr)->JoinTime;
99 bool inserted = false;
101 for(std::list<GroupQueueInfo *>::iterator elig_itr = begin(); elig_itr != end(); ++elig_itr)
103 // if the next one's size is smaller, then insert
104 // also insert if the next one's size is equal, but it joined the queue later
105 if( ((*elig_itr)->Players.size()<size) ||
106 ((*elig_itr)->Players.size() == size && (*elig_itr)->JoinTime > jointime) )
108 insert(elig_itr,(*itr));
109 inserted = true;
110 break;
113 // if not inserted -> this is the smallest group -> push_back
114 if(!inserted)
116 push_back((*itr));
122 // remove group from eligible groups
123 // used when building selection pools
124 void BattleGroundQueue::EligibleGroups::RemoveGroup(GroupQueueInfo * ginfo)
126 for(std::list<GroupQueueInfo *>::iterator itr = begin(); itr != end(); ++itr)
128 if((*itr)==ginfo)
130 erase(itr);
131 return;
136 // selection pool initialization, used to clean up from prev selection
137 void BattleGroundQueue::SelectionPool::Init()
139 SelectedGroups.clear();
140 MaxGroup = 0;
141 PlayerCount = 0;
144 // get the maximal group from the selection pool
145 // used when building the pool, and have to remove the largest
146 GroupQueueInfo * BattleGroundQueue::SelectionPool::GetMaximalGroup()
148 if(SelectedGroups.empty())
150 sLog.outError("Getting max group when selection pool is empty, this should never happen.");
151 MaxGroup = NULL;
152 return 0;
154 // actually select the max group if it's not set
155 if(MaxGroup==0 && !SelectedGroups.empty())
157 uint32 max_size = 0;
158 for(std::list<GroupQueueInfo *>::iterator itr = SelectedGroups.begin(); itr != SelectedGroups.end(); ++itr)
160 if(max_size<(*itr)->Players.size())
162 MaxGroup =(*itr);
163 max_size = MaxGroup->Players.size();
167 return MaxGroup;
170 // remove group info from selection pool
171 // used when building selection pools and have to remove maximal group
172 void BattleGroundQueue::SelectionPool::RemoveGroup(GroupQueueInfo *ginfo)
174 // uninitiate max group info if needed
175 if(MaxGroup == ginfo)
176 MaxGroup = 0;
177 // find what to remove
178 for(std::list<GroupQueueInfo *>::iterator itr = SelectedGroups.begin(); itr != SelectedGroups.end(); ++itr)
180 if((*itr)==ginfo)
182 SelectedGroups.erase(itr);
183 // decrease selected players count
184 PlayerCount -= ginfo->Players.size();
185 return;
190 // add group to selection
191 // used when building selection pools
192 void BattleGroundQueue::SelectionPool::AddGroup(GroupQueueInfo * ginfo)
194 SelectedGroups.push_back(ginfo);
195 // increase selected players count
196 PlayerCount+=ginfo->Players.size();
197 if(!MaxGroup || ginfo->Players.size() > MaxGroup->Players.size())
199 // update max group info if needed
200 MaxGroup = ginfo;
204 // add group to bg queue with the given leader and bg specifications
205 GroupQueueInfo * BattleGroundQueue::AddGroup(Player *leader, uint32 BgTypeId, uint8 ArenaType, bool isRated, uint32 arenaRating, uint32 arenateamid)
207 uint32 queue_id = leader->GetBattleGroundQueueIdFromLevel();
209 // create new ginfo
210 // cannot use the method like in addplayer, because that could modify an in-queue group's stats
211 // (e.g. leader leaving queue then joining as individual again)
212 GroupQueueInfo* ginfo = new GroupQueueInfo;
213 ginfo->BgTypeId = BgTypeId;
214 ginfo->ArenaType = ArenaType;
215 ginfo->ArenaTeamId = arenateamid;
216 ginfo->IsRated = isRated;
217 ginfo->IsInvitedToBGInstanceGUID = 0; // maybe this should be modifiable by function arguments to enable selection of running instances?
218 ginfo->JoinTime = getMSTime();
219 ginfo->Team = leader->GetTeam();
220 ginfo->ArenaTeamRating = arenaRating;
221 ginfo->OpponentsTeamRating = 0; //initialize it to 0
223 ginfo->Players.clear();
225 m_QueuedGroups[queue_id].push_back(ginfo);
227 // return ginfo, because it is needed to add players to this group info
228 return ginfo;
231 void BattleGroundQueue::AddPlayer(Player *plr, GroupQueueInfo *ginfo)
233 uint32 queue_id = plr->GetBattleGroundQueueIdFromLevel();
235 //if player isn't in queue, he is added, if already is, then values are overwritten, no memory leak
236 PlayerQueueInfo& info = m_QueuedPlayers[queue_id][plr->GetGUID()];
237 info.InviteTime = 0;
238 info.LastInviteTime = 0;
239 info.LastOnlineTime = getMSTime();
240 info.GroupInfo = ginfo;
242 // add the pinfo to ginfo's list
243 ginfo->Players[plr->GetGUID()] = &info;
246 void BattleGroundQueue::RemovePlayer(uint64 guid, bool decreaseInvitedCount)
248 Player *plr = objmgr.GetPlayer(guid);
250 uint32 queue_id = 0;
251 QueuedPlayersMap::iterator itr;
252 GroupQueueInfo * group;
253 QueuedGroupsList::iterator group_itr;
254 bool IsSet = false;
255 if(plr)
257 queue_id = plr->GetBattleGroundQueueIdFromLevel();
259 itr = m_QueuedPlayers[queue_id].find(guid);
260 if(itr != m_QueuedPlayers[queue_id].end())
261 IsSet = true;
264 if(!IsSet)
266 // either player is offline, or he levelled up to another queue category
267 // sLog.outError("Battleground: removing offline player from BG queue - this might not happen, but it should not cause crash");
268 for (uint32 i = 0; i < MAX_BATTLEGROUND_QUEUES; i++)
270 itr = m_QueuedPlayers[i].find(guid);
271 if(itr != m_QueuedPlayers[i].end())
273 queue_id = i;
274 IsSet = true;
275 break;
280 // couldn't find the player in bg queue, return
281 if(!IsSet)
283 sLog.outError("Battleground: couldn't find player to remove.");
284 return;
287 group = itr->second.GroupInfo;
289 for(group_itr=m_QueuedGroups[queue_id].begin(); group_itr != m_QueuedGroups[queue_id].end(); ++group_itr)
291 if(group == (GroupQueueInfo*)(*group_itr))
292 break;
295 // variables are set (what about leveling up when in queue????)
296 // remove player from group
297 // if only player there, remove group
299 // remove player queue info from group queue info
300 std::map<uint64, PlayerQueueInfo*>::iterator pitr = group->Players.find(guid);
302 if(pitr != group->Players.end())
303 group->Players.erase(pitr);
305 // check for iterator correctness
306 if (group_itr != m_QueuedGroups[queue_id].end() && itr != m_QueuedPlayers[queue_id].end())
308 // used when player left the queue, NOT used when porting to bg
309 if (decreaseInvitedCount)
311 // if invited to bg, and should decrease invited count, then do it
312 if(group->IsInvitedToBGInstanceGUID)
314 BattleGround* bg = sBattleGroundMgr.GetBattleGround(group->IsInvitedToBGInstanceGUID);
315 if (bg)
316 bg->DecreaseInvitedCount(group->Team);
317 if (bg && !bg->GetPlayersSize() && !bg->GetInvitedCount(ALLIANCE) && !bg->GetInvitedCount(HORDE))
319 // no more players on battleground, set delete it
320 bg->SetDeleteThis();
323 // update the join queue, maybe now the player's group fits in a queue!
324 // not yet implemented (should store bgTypeId in group queue info?)
326 // remove player queue info
327 m_QueuedPlayers[queue_id].erase(itr);
328 // remove group queue info if needed
330 //if we left BG queue(not porting) OR if arena team left queue for rated match
331 if((decreaseInvitedCount && !group->ArenaType) || (group->ArenaType && group->IsRated && group->Players.empty()))
332 AnnounceWorld(group, guid, false);
334 if(group->Players.empty())
336 m_QueuedGroups[queue_id].erase(group_itr);
337 delete group;
339 // NEEDS TESTING!
340 // group wasn't empty, so it wasn't deleted, and player have left a rated queue -> everyone from the group should leave too
341 // don't remove recursively if already invited to bg!
342 else if(!group->IsInvitedToBGInstanceGUID && decreaseInvitedCount && group->IsRated)
344 // remove next player, this is recursive
345 // first send removal information
346 if(Player *plr2 = objmgr.GetPlayer(group->Players.begin()->first))
348 BattleGround * bg = sBattleGroundMgr.GetBattleGroundTemplate(group->BgTypeId);
349 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(group->BgTypeId,group->ArenaType);
350 uint32 queueSlot = plr2->GetBattleGroundQueueIndex(bgQueueTypeId);
351 plr2->RemoveBattleGroundQueueId(bgQueueTypeId); // must be called this way, because if you move this call to queue->removeplayer, it causes bugs
352 WorldPacket data;
353 sBattleGroundMgr.BuildBattleGroundStatusPacket(&data, bg, plr2->GetTeam(), queueSlot, STATUS_NONE, 0, 0);
354 plr2->GetSession()->SendPacket(&data);
356 // then actually delete, this may delete the group as well!
357 RemovePlayer(group->Players.begin()->first,decreaseInvitedCount);
362 void BattleGroundQueue::AnnounceWorld(GroupQueueInfo *ginfo, uint64 playerGUID, bool isAddedToQueue)
365 if(ginfo->ArenaType) //if Arena
367 if( sWorld.getConfig(CONFIG_ARENA_QUEUE_ANNOUNCER_ENABLE) && ginfo->IsRated)
369 BattleGround* bg = sBattleGroundMgr.GetBattleGroundTemplate(ginfo->BgTypeId);
370 if(!bg)
371 return;
373 char const* bgName = bg->GetName();
374 if(isAddedToQueue)
375 sWorld.SendWorldText(LANG_ARENA_QUEUE_ANNOUNCE_WORLD_JOIN, bgName, ginfo->ArenaType, ginfo->ArenaType, ginfo->ArenaTeamRating);
376 else
377 sWorld.SendWorldText(LANG_ARENA_QUEUE_ANNOUNCE_WORLD_EXIT, bgName, ginfo->ArenaType, ginfo->ArenaType, ginfo->ArenaTeamRating);
380 else //if BG
382 if( sWorld.getConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_ENABLE) )
384 Player *plr = objmgr.GetPlayer(playerGUID);
385 if(!plr)
386 return;
388 BattleGround* bg = sBattleGroundMgr.GetBattleGroundTemplate(ginfo->BgTypeId);
389 if(!bg)
390 return;
392 uint32 queue_id = plr->GetBattleGroundQueueIdFromLevel();
393 char const* bgName = bg->GetName();
395 uint32 q_min_level = Player::GetMinLevelForBattleGroundQueueId(queue_id);
396 uint32 q_max_level = Player::GetMaxLevelForBattleGroundQueueId(queue_id);
398 // replace hardcoded max level by player max level for nice output
399 if(q_max_level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
400 q_max_level = sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL);
402 int8 MinPlayers = bg->GetMinPlayersPerTeam();
404 uint8 qHorde = 0;
405 uint8 qAlliance = 0;
407 uint32 bgTypeId = ginfo->BgTypeId;
408 QueuedPlayersMap::iterator itr;
409 for(itr = m_QueuedPlayers[queue_id].begin(); itr!= m_QueuedPlayers[queue_id].end(); ++itr)
411 if(itr->second.GroupInfo->BgTypeId == bgTypeId)
413 switch(itr->second.GroupInfo->Team)
415 case HORDE:
416 qHorde++; break;
417 case ALLIANCE:
418 qAlliance++; break;
419 default:
420 break;
425 // Show queue status to player only (when joining queue)
426 if(sWorld.getConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_PLAYERONLY))
428 ChatHandler(plr).PSendSysMessage(LANG_BG_QUEUE_ANNOUNCE_SELF,
429 bgName, q_min_level, q_max_level, qAlliance, MinPlayers, qHorde, MinPlayers);
431 // System message
432 else
434 sWorld.SendWorldText(LANG_BG_QUEUE_ANNOUNCE_WORLD,
435 bgName, q_min_level, q_max_level, qAlliance, MinPlayers, qHorde, MinPlayers);
441 bool BattleGroundQueue::InviteGroupToBG(GroupQueueInfo * ginfo, BattleGround * bg, uint32 side)
443 // set side if needed
444 if(side)
445 ginfo->Team = side;
447 if(!ginfo->IsInvitedToBGInstanceGUID)
449 // not yet invited
450 // set invitation
451 ginfo->IsInvitedToBGInstanceGUID = bg->GetInstanceID();
452 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(bg->GetTypeID(), bg->GetArenaType());
453 // loop through the players
454 for(std::map<uint64,PlayerQueueInfo*>::iterator itr = ginfo->Players.begin(); itr != ginfo->Players.end(); ++itr)
456 // set status
457 itr->second->InviteTime = getMSTime();
458 itr->second->LastInviteTime = getMSTime();
460 // get the player
461 Player* plr = objmgr.GetPlayer(itr->first);
462 // if offline, skip him
463 if(!plr)
464 continue;
466 // invite the player
467 sBattleGroundMgr.InvitePlayer(plr, bg->GetInstanceID(),ginfo->Team);
469 WorldPacket data;
471 uint32 queueSlot = plr->GetBattleGroundQueueIndex(bgQueueTypeId);
473 sLog.outDebug("Battleground: invited plr %s (%u) to BG instance %u queueindex %u bgtype %u, I can't help it if they don't press the enter battle button.",plr->GetName(),plr->GetGUIDLow(),bg->GetInstanceID(),queueSlot,bg->GetTypeID());
475 // send status packet
476 sBattleGroundMgr.BuildBattleGroundStatusPacket(&data, bg, side?side:plr->GetTeam(), queueSlot, STATUS_WAIT_JOIN, INVITE_ACCEPT_WAIT_TIME, 0);
477 plr->GetSession()->SendPacket(&data);
479 return true;
482 return false;
485 // this function is responsible for the selection of queued groups when trying to create new battlegrounds
486 bool BattleGroundQueue::BuildSelectionPool(uint32 bgTypeId, uint32 queue_id, uint32 MinPlayers, uint32 MaxPlayers, SelectionPoolBuildMode mode, uint8 ArenaType, bool isRated, uint32 MinRating, uint32 MaxRating, uint32 DisregardTime, uint32 excludeTeam)
488 uint32 side;
489 switch(mode)
491 case NORMAL_ALLIANCE:
492 case ONESIDE_ALLIANCE_TEAM1:
493 case ONESIDE_ALLIANCE_TEAM2:
494 side = ALLIANCE;
495 break;
496 case NORMAL_HORDE:
497 case ONESIDE_HORDE_TEAM1:
498 case ONESIDE_HORDE_TEAM2:
499 side = HORDE;
500 break;
501 default:
502 //unknown mode, return false
503 sLog.outDebug("Battleground: unknown selection pool build mode, returning...");
504 return false;
505 break;
508 // inititate the groups eligible to create the bg
509 m_EligibleGroups.Init(&(m_QueuedGroups[queue_id]), bgTypeId, side, MaxPlayers, ArenaType, isRated, MinRating, MaxRating, DisregardTime, excludeTeam);
510 // init the selected groups (clear)
511 m_SelectionPools[mode].Init();
512 while(!(m_EligibleGroups.empty()))
514 sLog.outDebug("m_EligibleGroups is not empty, continue building selection pool");
515 // in decreasing group size, add groups to join if they fit in the MaxPlayersPerTeam players
516 for(EligibleGroups::iterator itr= m_EligibleGroups.begin(); itr!=m_EligibleGroups.end(); ++itr)
518 // get the maximal not yet checked group
519 GroupQueueInfo * MaxGroup = (*itr);
520 // if it fits in the maxplayer size, add it
521 if( (m_SelectionPools[mode].GetPlayerCount() + MaxGroup->Players.size()) <= MaxPlayers )
523 m_SelectionPools[mode].AddGroup(MaxGroup);
526 if(m_SelectionPools[mode].GetPlayerCount()>=MinPlayers)
528 // the selection pool is set, return
529 sLog.outDebug("pool build succeeded, return true");
530 return true;
532 // if the selection pool's not set, then remove the group with the highest player count, and try again with the rest.
533 GroupQueueInfo * MaxGroup = m_SelectionPools[mode].GetMaximalGroup();
534 m_EligibleGroups.RemoveGroup(MaxGroup);
535 m_SelectionPools[mode].RemoveGroup(MaxGroup);
537 // failed to build a selection pool matching the given values
538 return false;
541 // used to remove the Enter Battle window if the battle has already, but someone still has it
542 // (this can happen in arenas mainly, since the preparation is shorter than the timer for the bgqueueremove event
543 void BattleGroundQueue::BGEndedRemoveInvites(BattleGround *bg)
545 uint32 queue_id = bg->GetQueueType();
546 uint32 bgInstanceId = bg->GetInstanceID();
547 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(bg->GetTypeID(), bg->GetArenaType());
548 QueuedGroupsList::iterator itr, next;
549 for(itr = m_QueuedGroups[queue_id].begin(); itr != m_QueuedGroups[queue_id].end(); itr = next)
551 // must do this way, because the groupinfo will be deleted when all playerinfos are removed
552 GroupQueueInfo * ginfo = (*itr);
553 next = itr;
554 ++next;
555 // if group was invited to this bg instance, then remove all references
556 if(ginfo->IsInvitedToBGInstanceGUID == bgInstanceId)
558 // after removing this much playerinfos, the ginfo will be deleted, so we'll use a for loop
559 uint32 to_remove = ginfo->Players.size();
560 uint32 team = ginfo->Team;
561 for(int i = 0; i < to_remove; ++i)
563 // always remove the first one in the group
564 std::map<uint64, PlayerQueueInfo * >::iterator itr2 = ginfo->Players.begin();
565 if(itr2 == ginfo->Players.end())
567 sLog.outError("Empty Players in ginfo, this should never happen!");
568 return;
571 // get the player
572 Player * plr = objmgr.GetPlayer(itr2->first);
573 if(!plr)
575 sLog.outError("Player offline when trying to remove from GroupQueueInfo, this should never happen.");
576 continue;
579 // get the queueslot
580 uint32 queueSlot = plr->GetBattleGroundQueueIndex(bgQueueTypeId);
581 if (queueSlot < PLAYER_MAX_BATTLEGROUND_QUEUES) // player is in queue
583 plr->RemoveBattleGroundQueueId(bgQueueTypeId);
584 // remove player from queue, this might delete the ginfo as well! don't use that pointer after this!
585 RemovePlayer(itr2->first, true);
586 // this is probably unneeded, since this player was already invited -> does not fit when initing eligible groups
587 // but updateing the queue can't hurt
588 Update(bgQueueTypeId, bg->GetQueueType());
589 // send info to client
590 WorldPacket data;
591 sBattleGroundMgr.BuildBattleGroundStatusPacket(&data, bg, team, queueSlot, STATUS_NONE, 0, 0);
592 plr->GetSession()->SendPacket(&data);
600 this method is called when group is inserted, or player / group is removed from BG Queue - there is only one player's status changed, so we don't use while(true) cycles to invite whole queue
601 it must be called after fully adding the members of a group to ensure group joining
602 should be called after removeplayer functions in some cases
604 void BattleGroundQueue::Update(uint32 bgTypeId, uint32 queue_id, uint8 arenatype, bool isRated, uint32 arenaRating)
606 if (queue_id >= MAX_BATTLEGROUND_QUEUES)
608 //this is error, that caused crashes (not in , but now it shouldn't)
609 sLog.outError("BattleGroundQueue::Update() called for non existing queue type - this can cause crash, pls report problem, if this is the last line of error log before crash");
610 return;
613 //if no players in queue ... do nothing
614 if (m_QueuedGroups[queue_id].empty())
615 return;
617 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(bgTypeId, arenatype);
619 //battleground with free slot for player should be always the last in this queue
620 BGFreeSlotQueueType::iterator itr, next;
621 for (itr = sBattleGroundMgr.BGFreeSlotQueue[bgTypeId].begin(); itr != sBattleGroundMgr.BGFreeSlotQueue[bgTypeId].end(); itr = next)
623 next = itr;
624 ++next;
625 // battleground is running, so if:
626 // DO NOT allow queue manager to invite new player to running arena
627 if ((*itr)->isBattleGround() && (*itr)->GetTypeID() == bgTypeId && (*itr)->GetQueueType() == queue_id && (*itr)->GetStatus() > STATUS_WAIT_QUEUE && (*itr)->GetStatus() < STATUS_WAIT_LEAVE)
629 //we must check both teams
630 BattleGround* bg = *itr; //we have to store battleground pointer here, because when battleground is full, it is removed from free queue (not yet implemented!!)
631 // and iterator is invalid
633 for(QueuedGroupsList::iterator itr = m_QueuedGroups[queue_id].begin(); itr != m_QueuedGroups[queue_id].end(); ++itr)
635 // did the group join for this bg type?
636 if((*itr)->BgTypeId != bgTypeId)
637 continue;
638 // if so, check if fits in
639 if(bg->GetFreeSlotsForTeam((*itr)->Team) >= (*itr)->Players.size())
641 // if group fits in, invite it
642 InviteGroupToBG((*itr),bg,(*itr)->Team);
646 if (!bg->HasFreeSlots())
648 //remove BG from BGFreeSlotQueue
649 bg->RemoveFromBGFreeSlotQueue();
654 // finished iterating through the bgs with free slots, maybe we need to create a new bg
656 BattleGround * bg_template = sBattleGroundMgr.GetBattleGroundTemplate(bgTypeId);
657 if(!bg_template)
659 sLog.outError("Battleground: Update: bg template not found for %u", bgTypeId);
660 return;
663 // get the min. players per team, properly for larger arenas as well. (must have full teams for arena matches!)
664 uint32 MinPlayersPerTeam = bg_template->GetMinPlayersPerTeam();
665 uint32 MaxPlayersPerTeam = bg_template->GetMaxPlayersPerTeam();
666 if(bg_template->isArena())
668 if(sBattleGroundMgr.isArenaTesting())
670 MaxPlayersPerTeam = 1;
671 MinPlayersPerTeam = 1;
673 else
675 switch(arenatype)
677 case ARENA_TYPE_2v2:
678 MaxPlayersPerTeam = 2;
679 MinPlayersPerTeam = 2;
680 break;
681 case ARENA_TYPE_3v3:
682 MaxPlayersPerTeam = 3;
683 MinPlayersPerTeam = 3;
684 break;
685 case ARENA_TYPE_5v5:
686 MaxPlayersPerTeam = 5;
687 MinPlayersPerTeam = 5;
688 break;
693 // found out the minimum and maximum ratings the newly added team should battle against
694 // arenaRating is the rating of the latest joined team
695 uint32 arenaMinRating = (arenaRating <= sBattleGroundMgr.GetMaxRatingDifference()) ? 0 : arenaRating - sBattleGroundMgr.GetMaxRatingDifference();
696 // if no rating is specified, set maxrating to 0
697 uint32 arenaMaxRating = (arenaRating == 0)? 0 : arenaRating + sBattleGroundMgr.GetMaxRatingDifference();
698 uint32 discardTime = 0;
699 // if max rating difference is set and the time past since server startup is greater than the rating discard time
700 // (after what time the ratings aren't taken into account when making teams) then
701 // the discard time is current_time - time_to_discard, teams that joined after that, will have their ratings taken into account
702 // else leave the discard time on 0, this way all ratings will be discarded
703 if(sBattleGroundMgr.GetMaxRatingDifference() && getMSTime() >= sBattleGroundMgr.GetRatingDiscardTimer())
704 discardTime = getMSTime() - sBattleGroundMgr.GetRatingDiscardTimer();
706 // try to build the selection pools
707 bool bAllyOK = BuildSelectionPool(bgTypeId, queue_id, MinPlayersPerTeam, MaxPlayersPerTeam, NORMAL_ALLIANCE, arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime);
708 if(bAllyOK)
709 sLog.outDebug("Battleground: ally pool succesfully build");
710 else
711 sLog.outDebug("Battleground: ally pool wasn't created");
712 bool bHordeOK = BuildSelectionPool(bgTypeId, queue_id, MinPlayersPerTeam, MaxPlayersPerTeam, NORMAL_HORDE, arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime);
713 if(bHordeOK)
714 sLog.outDebug("Battleground: horde pool succesfully built");
715 else
716 sLog.outDebug("Battleground: horde pool wasn't created");
718 // if selection pools are ready, create the new bg
719 if (bAllyOK && bHordeOK)
721 BattleGround * bg2 = 0;
722 // special handling for arenas
723 if(bg_template->isArena())
725 // Find a random arena, that can be created
726 uint8 arenas[] = {BATTLEGROUND_NA, BATTLEGROUND_BE, BATTLEGROUND_RL};
727 uint32 arena_num = urand(0,2);
728 if( !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[arena_num%3])) &&
729 !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[(arena_num+1)%3])) &&
730 !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[(arena_num+2)%3])) )
732 sLog.outError("Battleground: couldn't create any arena instance!");
733 return;
736 // set the MaxPlayersPerTeam values based on arenatype
737 // setting the min player values isn't needed, since we won't be using that value later on.
738 if(sBattleGroundMgr.isArenaTesting())
740 bg2->SetMaxPlayersPerTeam(1);
741 bg2->SetMaxPlayers(2);
743 else
745 switch(arenatype)
747 case ARENA_TYPE_2v2:
748 bg2->SetMaxPlayersPerTeam(2);
749 bg2->SetMaxPlayers(4);
750 break;
751 case ARENA_TYPE_3v3:
752 bg2->SetMaxPlayersPerTeam(3);
753 bg2->SetMaxPlayers(6);
754 break;
755 case ARENA_TYPE_5v5:
756 bg2->SetMaxPlayersPerTeam(5);
757 bg2->SetMaxPlayers(10);
758 break;
759 default:
760 break;
764 else
766 // create new battleground
767 bg2 = sBattleGroundMgr.CreateNewBattleGround(bgTypeId);
768 if( sWorld.getConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_ENABLE) )
770 char const* bgName = bg2->GetName();
771 uint32 q_min_level = Player::GetMinLevelForBattleGroundQueueId(queue_id);
772 uint32 q_max_level = Player::GetMaxLevelForBattleGroundQueueId(queue_id);
773 if(q_max_level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
774 q_max_level = sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL);
775 sWorld.SendWorldText(LANG_BG_STARTED_ANNOUNCE_WORLD, bgName, q_min_level, q_max_level);
779 if(!bg2)
781 sLog.outError("Battleground: couldn't create bg %u",bgTypeId);
782 return;
785 // start the joining of the bg
786 bg2->SetStatus(STATUS_WAIT_JOIN);
787 bg2->SetQueueType(queue_id);
788 // initialize arena / rating info
789 bg2->SetArenaType(arenatype);
790 // set rating
791 bg2->SetRated(isRated);
793 std::list<GroupQueueInfo* >::iterator itr;
795 // invite groups from horde selection pool
796 for(itr = m_SelectionPools[NORMAL_HORDE].SelectedGroups.begin(); itr != m_SelectionPools[NORMAL_HORDE].SelectedGroups.end(); ++itr)
798 InviteGroupToBG((*itr),bg2,HORDE);
801 // invite groups from ally selection pools
802 for(itr = m_SelectionPools[NORMAL_ALLIANCE].SelectedGroups.begin(); itr != m_SelectionPools[NORMAL_ALLIANCE].SelectedGroups.end(); ++itr)
804 InviteGroupToBG((*itr),bg2,ALLIANCE);
807 if (isRated)
809 std::list<GroupQueueInfo* >::iterator itr_alliance = m_SelectionPools[NORMAL_ALLIANCE].SelectedGroups.begin();
810 std::list<GroupQueueInfo* >::iterator itr_horde = m_SelectionPools[NORMAL_HORDE].SelectedGroups.begin();
811 (*itr_alliance)->OpponentsTeamRating = (*itr_horde)->ArenaTeamRating;
812 sLog.outDebug("setting oposite teamrating for team %u to %u", (*itr_alliance)->ArenaTeamId, (*itr_alliance)->OpponentsTeamRating);
813 (*itr_horde)->OpponentsTeamRating = (*itr_alliance)->ArenaTeamRating;
814 sLog.outDebug("setting oposite teamrating for team %u to %u", (*itr_horde)->ArenaTeamId, (*itr_horde)->OpponentsTeamRating);
817 // start the battleground
818 bg2->StartBattleGround();
821 // there weren't enough players for a "normal" match
822 // if arena, enable horde versus horde or alliance versus alliance teams here
824 else if(bg_template->isArena())
826 bool bOneSideHordeTeam1 = false, bOneSideHordeTeam2 = false;
827 bool bOneSideAllyTeam1 = false, bOneSideAllyTeam2 = false;
828 bOneSideHordeTeam1 = BuildSelectionPool(bgTypeId, queue_id,MaxPlayersPerTeam,MaxPlayersPerTeam,ONESIDE_HORDE_TEAM1,arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime);
829 if(bOneSideHordeTeam1)
831 // one team has been selected, find out if other can be selected too
832 std::list<GroupQueueInfo* >::iterator itr;
833 // temporarily change the team side to enable building the next pool excluding the already selected groups
834 for(itr = m_SelectionPools[ONESIDE_HORDE_TEAM1].SelectedGroups.begin(); itr != m_SelectionPools[ONESIDE_HORDE_TEAM1].SelectedGroups.end(); ++itr)
835 (*itr)->Team=ALLIANCE;
837 bOneSideHordeTeam2 = BuildSelectionPool(bgTypeId, queue_id,MaxPlayersPerTeam,MaxPlayersPerTeam,ONESIDE_HORDE_TEAM2,arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime, (*(m_SelectionPools[ONESIDE_HORDE_TEAM1].SelectedGroups.begin()))->ArenaTeamId);
839 // change back the team to horde
840 for(itr = m_SelectionPools[ONESIDE_HORDE_TEAM1].SelectedGroups.begin(); itr != m_SelectionPools[ONESIDE_HORDE_TEAM1].SelectedGroups.end(); ++itr)
841 (*itr)->Team=HORDE;
843 if(!bOneSideHordeTeam2)
844 bOneSideHordeTeam1 = false;
846 if(!bOneSideHordeTeam1)
848 // check for one sided ally
849 bOneSideAllyTeam1 = BuildSelectionPool(bgTypeId, queue_id,MaxPlayersPerTeam,MaxPlayersPerTeam,ONESIDE_ALLIANCE_TEAM1,arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime);
850 if(bOneSideAllyTeam1)
852 // one team has been selected, find out if other can be selected too
853 std::list<GroupQueueInfo* >::iterator itr;
854 // temporarily change the team side to enable building the next pool excluding the already selected groups
855 for(itr = m_SelectionPools[ONESIDE_ALLIANCE_TEAM1].SelectedGroups.begin(); itr != m_SelectionPools[ONESIDE_ALLIANCE_TEAM1].SelectedGroups.end(); ++itr)
856 (*itr)->Team=HORDE;
858 bOneSideAllyTeam2 = BuildSelectionPool(bgTypeId, queue_id,MaxPlayersPerTeam,MaxPlayersPerTeam,ONESIDE_ALLIANCE_TEAM2,arenatype, isRated, arenaMinRating, arenaMaxRating, discardTime,(*(m_SelectionPools[ONESIDE_ALLIANCE_TEAM1].SelectedGroups.begin()))->ArenaTeamId);
860 // change back the team to ally
861 for(itr = m_SelectionPools[ONESIDE_ALLIANCE_TEAM1].SelectedGroups.begin(); itr != m_SelectionPools[ONESIDE_ALLIANCE_TEAM1].SelectedGroups.end(); ++itr)
862 (*itr)->Team=ALLIANCE;
865 if(!bOneSideAllyTeam2)
866 bOneSideAllyTeam1 = false;
868 // 1-sided BuildSelectionPool() will work, because the MinPlayersPerTeam == MaxPlayersPerTeam in every arena!!!!
869 if( (bOneSideHordeTeam1 && bOneSideHordeTeam2) ||
870 (bOneSideAllyTeam1 && bOneSideAllyTeam2) )
872 // which side has enough players?
873 uint32 side = 0;
874 SelectionPoolBuildMode mode1, mode2;
875 // find out what pools are we using
876 if(bOneSideAllyTeam1 && bOneSideAllyTeam2)
878 side = ALLIANCE;
879 mode1 = ONESIDE_ALLIANCE_TEAM1;
880 mode2 = ONESIDE_ALLIANCE_TEAM2;
882 else
884 side = HORDE;
885 mode1 = ONESIDE_HORDE_TEAM1;
886 mode2 = ONESIDE_HORDE_TEAM2;
889 // create random arena
890 uint8 arenas[] = {BATTLEGROUND_NA, BATTLEGROUND_BE, BATTLEGROUND_RL};
891 uint32 arena_num = urand(0,2);
892 BattleGround* bg2 = NULL;
893 if( !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[arena_num%3])) &&
894 !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[(arena_num+1)%3])) &&
895 !(bg2 = sBattleGroundMgr.CreateNewBattleGround(arenas[(arena_num+2)%3])) )
897 sLog.outError("Could not create arena.");
898 return;
901 sLog.outDebug("Battleground: One-faction arena created.");
902 // init stats
903 if(sBattleGroundMgr.isArenaTesting())
905 bg2->SetMaxPlayersPerTeam(1);
906 bg2->SetMaxPlayers(2);
908 else
910 switch(arenatype)
912 case ARENA_TYPE_2v2:
913 bg2->SetMaxPlayersPerTeam(2);
914 bg2->SetMaxPlayers(4);
915 break;
916 case ARENA_TYPE_3v3:
917 bg2->SetMaxPlayersPerTeam(3);
918 bg2->SetMaxPlayers(6);
919 break;
920 case ARENA_TYPE_5v5:
921 bg2->SetMaxPlayersPerTeam(5);
922 bg2->SetMaxPlayers(10);
923 break;
924 default:
925 break;
929 bg2->SetRated(isRated);
931 // assigned team of the other group
932 uint32 other_side;
933 if(side == ALLIANCE)
934 other_side = HORDE;
935 else
936 other_side = ALLIANCE;
938 // start the joining of the bg
939 bg2->SetStatus(STATUS_WAIT_JOIN);
940 bg2->SetQueueType(queue_id);
941 // initialize arena / rating info
942 bg2->SetArenaType(arenatype);
944 std::list<GroupQueueInfo* >::iterator itr;
946 // invite players from the first group as horde players (actually green team)
947 for(itr = m_SelectionPools[mode1].SelectedGroups.begin(); itr != m_SelectionPools[mode1].SelectedGroups.end(); ++itr)
949 InviteGroupToBG((*itr),bg2,HORDE);
952 // invite players from the second group as ally players (actually gold team)
953 for(itr = m_SelectionPools[mode2].SelectedGroups.begin(); itr != m_SelectionPools[mode2].SelectedGroups.end(); ++itr)
955 InviteGroupToBG((*itr),bg2,ALLIANCE);
958 if (isRated)
960 std::list<GroupQueueInfo* >::iterator itr_alliance = m_SelectionPools[mode1].SelectedGroups.begin();
961 std::list<GroupQueueInfo* >::iterator itr_horde = m_SelectionPools[mode2].SelectedGroups.begin();
962 (*itr_alliance)->OpponentsTeamRating = (*itr_horde)->ArenaTeamRating;
963 (*itr_horde)->OpponentsTeamRating = (*itr_alliance)->ArenaTeamRating;
966 bg2->StartBattleGround();
971 /*********************************************************/
972 /*** BATTLEGROUND QUEUE EVENTS ***/
973 /*********************************************************/
975 bool BGQueueInviteEvent::Execute(uint64 /*e_time*/, uint32 /*p_time*/)
977 Player* plr = objmgr.GetPlayer( m_PlayerGuid );
979 // player logged off (we should do nothing, he is correctly removed from queue in another procedure)
980 if (!plr)
981 return true;
983 // Player can be in another BG queue and must be removed in normal way in any case
984 // // player is already in battleground ... do nothing (battleground queue status is deleted when player is teleported to BG)
985 // if (plr->GetBattleGroundId() > 0)
986 // return true;
988 BattleGround* bg = sBattleGroundMgr.GetBattleGround(m_BgInstanceGUID);
989 if (!bg)
990 return true;
992 uint32 queueSlot = plr->GetBattleGroundQueueIndex(bg->GetTypeID());
993 if (queueSlot < PLAYER_MAX_BATTLEGROUND_QUEUES) // player is in queue
995 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(bg->GetTypeID(), bg->GetArenaType());
996 uint32 queueSlot = plr->GetBattleGroundQueueIndex(bgQueueTypeId);
997 if (queueSlot < PLAYER_MAX_BATTLEGROUND_QUEUES) // player is in queue
999 // check if player is invited to this bg ... this check must be here, because when player leaves queue and joins another, it would cause a problems
1000 BattleGroundQueue::QueuedPlayersMap const& qpMap = sBattleGroundMgr.m_BattleGroundQueues[bgQueueTypeId].m_QueuedPlayers[plr->GetBattleGroundQueueIdFromLevel()];
1001 BattleGroundQueue::QueuedPlayersMap::const_iterator qItr = qpMap.find(m_PlayerGuid);
1002 if (qItr != qpMap.end() && qItr->second.GroupInfo->IsInvitedToBGInstanceGUID == m_BgInstanceGUID)
1004 WorldPacket data;
1005 sBattleGroundMgr.BuildBattleGroundStatusPacket(&data, bg, qItr->second.GroupInfo->Team, queueSlot, STATUS_WAIT_JOIN, INVITE_ACCEPT_WAIT_TIME/2, 0);
1006 plr->GetSession()->SendPacket(&data);
1010 return true; //event will be deleted
1013 void BGQueueInviteEvent::Abort(uint64 /*e_time*/)
1015 //this should not be called
1016 sLog.outError("Battleground invite event ABORTED!");
1019 bool BGQueueRemoveEvent::Execute(uint64 /*e_time*/, uint32 /*p_time*/)
1021 Player* plr = objmgr.GetPlayer( m_PlayerGuid );
1022 if (!plr)
1023 // player logged off (we should do nothing, he is correctly removed from queue in another procedure)
1024 return true;
1026 BattleGround* bg = sBattleGroundMgr.GetBattleGround(m_BgInstanceGUID);
1027 if (!bg)
1028 return true;
1030 sLog.outDebug("Battleground: removing player %u from bg queue for instance %u because of not pressing enter battle in time.",plr->GetGUIDLow(),m_BgInstanceGUID);
1032 uint32 bgQueueTypeId = sBattleGroundMgr.BGQueueTypeId(bg->GetTypeID(), bg->GetArenaType());
1033 uint32 queueSlot = plr->GetBattleGroundQueueIndex(bgQueueTypeId);
1034 if (queueSlot < PLAYER_MAX_BATTLEGROUND_QUEUES) // player is in queue
1036 // check if player is invited to this bg ... this check must be here, because when player leaves queue and joins another, it would cause a problems
1037 BattleGroundQueue::QueuedPlayersMap::iterator qMapItr = sBattleGroundMgr.m_BattleGroundQueues[bgQueueTypeId].m_QueuedPlayers[plr->GetBattleGroundQueueIdFromLevel()].find(m_PlayerGuid);
1038 if (qMapItr != sBattleGroundMgr.m_BattleGroundQueues[bgQueueTypeId].m_QueuedPlayers[plr->GetBattleGroundQueueIdFromLevel()].end() && qMapItr->second.GroupInfo && qMapItr->second.GroupInfo->IsInvitedToBGInstanceGUID == m_BgInstanceGUID)
1040 if (qMapItr->second.GroupInfo->IsRated)
1042 ArenaTeam * at = objmgr.GetArenaTeamById(qMapItr->second.GroupInfo->ArenaTeamId);
1043 if (at)
1045 sLog.outDebug("UPDATING memberLost's personal arena rating for %u by opponents rating: %u", GUID_LOPART(plr->GetGUID()), qMapItr->second.GroupInfo->OpponentsTeamRating);
1046 at->MemberLost(plr, qMapItr->second.GroupInfo->OpponentsTeamRating);
1047 at->SaveToDB();
1050 plr->RemoveBattleGroundQueueId(bgQueueTypeId);
1051 sBattleGroundMgr.m_BattleGroundQueues[bgQueueTypeId].RemovePlayer(m_PlayerGuid, true);
1052 sBattleGroundMgr.m_BattleGroundQueues[bgQueueTypeId].Update(bgQueueTypeId, bg->GetQueueType());
1053 WorldPacket data;
1054 sBattleGroundMgr.BuildBattleGroundStatusPacket(&data, bg, m_PlayersTeam, queueSlot, STATUS_NONE, 0, 0);
1055 plr->GetSession()->SendPacket(&data);
1058 else
1059 sLog.outDebug("Battleground: Player was already removed from queue");
1061 //event will be deleted
1062 return true;
1065 void BGQueueRemoveEvent::Abort(uint64 /*e_time*/)
1067 //this should not be called
1068 sLog.outError("Battleground remove event ABORTED!");
1071 /*********************************************************/
1072 /*** BATTLEGROUND MANAGER ***/
1073 /*********************************************************/
1075 BattleGroundMgr::BattleGroundMgr()
1077 m_BattleGrounds.clear();
1078 m_AutoDistributePoints = (bool)sWorld.getConfig(CONFIG_ARENA_AUTO_DISTRIBUTE_POINTS);
1079 m_MaxRatingDifference = sWorld.getConfig(CONFIG_ARENA_MAX_RATING_DIFFERENCE);
1080 m_RatingDiscardTimer = sWorld.getConfig(CONFIG_ARENA_RATING_DISCARD_TIMER);
1081 m_PrematureFinishTimer = sWorld.getConfig(CONFIG_BATTLEGROUND_PREMATURE_FINISH_TIMER);
1082 m_NextRatingDiscardUpdate = m_RatingDiscardTimer;
1083 m_AutoDistributionTimeChecker = 0;
1084 m_ArenaTesting = false;
1087 BattleGroundMgr::~BattleGroundMgr()
1089 BattleGroundSet::iterator itr, next;
1090 for(itr = m_BattleGrounds.begin(); itr != m_BattleGrounds.end(); itr = next)
1092 next = itr;
1093 ++next;
1094 BattleGround * bg = itr->second;
1095 m_BattleGrounds.erase(itr);
1096 delete bg;
1098 m_BattleGrounds.clear();
1101 // used to update running battlegrounds, and delete finished ones
1102 void BattleGroundMgr::Update(time_t diff)
1104 BattleGroundSet::iterator itr, next;
1105 for(itr = m_BattleGrounds.begin(); itr != m_BattleGrounds.end(); itr = next)
1107 next = itr;
1108 ++next;
1109 itr->second->Update(diff);
1110 // use the SetDeleteThis variable
1111 // direct deletion caused crashes
1112 if(itr->second->m_SetDeleteThis)
1114 BattleGround * bg = itr->second;
1115 m_BattleGrounds.erase(itr);
1116 delete bg;
1119 // if rating difference counts, maybe force-update queues
1120 if(m_MaxRatingDifference)
1122 // it's time to force update
1123 if(m_NextRatingDiscardUpdate < diff)
1125 // forced update for level 70 rated arenas
1126 m_BattleGroundQueues[BATTLEGROUND_QUEUE_2v2].Update(BATTLEGROUND_AA,6,ARENA_TYPE_2v2,true,0);
1127 m_BattleGroundQueues[BATTLEGROUND_QUEUE_3v3].Update(BATTLEGROUND_AA,6,ARENA_TYPE_3v3,true,0);
1128 m_BattleGroundQueues[BATTLEGROUND_QUEUE_5v5].Update(BATTLEGROUND_AA,6,ARENA_TYPE_5v5,true,0);
1129 m_NextRatingDiscardUpdate = m_RatingDiscardTimer;
1131 else
1132 m_NextRatingDiscardUpdate -= diff;
1134 if(m_AutoDistributePoints)
1136 if(m_AutoDistributionTimeChecker < diff)
1138 if(sWorld.GetGameTime() > m_NextAutoDistributionTime)
1140 DistributeArenaPoints();
1141 m_NextAutoDistributionTime = sWorld.GetGameTime() + BATTLEGROUND_ARENA_POINT_DISTRIBUTION_DAY * sWorld.getConfig(CONFIG_ARENA_AUTO_DISTRIBUTE_INTERVAL_DAYS);
1142 CharacterDatabase.PExecute("UPDATE saved_variables SET NextArenaPointDistributionTime = '"I64FMTD"'", m_NextAutoDistributionTime);
1144 m_AutoDistributionTimeChecker = 600000; // check 10 minutes
1146 else
1147 m_AutoDistributionTimeChecker -= diff;
1151 void BattleGroundMgr::BuildBattleGroundStatusPacket(WorldPacket *data, BattleGround *bg, uint32 team, uint8 QueueSlot, uint8 StatusID, uint32 Time1, uint32 Time2, uint32 arenatype, uint8 israted)
1153 // we can be in 3 queues in same time...
1154 if(StatusID == 0)
1156 data->Initialize(SMSG_BATTLEFIELD_STATUS, 4*3);
1157 *data << uint32(QueueSlot); // queue id (0...2)
1158 *data << uint64(0);
1159 return;
1162 data->Initialize(SMSG_BATTLEFIELD_STATUS, (4+1+1+4+2+4+1+4+4+4));
1163 *data << uint32(QueueSlot); // queue id (0...2) - player can be in 3 queues in time
1164 // uint64 in client
1165 *data << uint64( uint64(arenatype ? arenatype : bg->GetArenaType()) | (uint64(0x0D) << 8) | (uint64(bg->GetTypeID()) << 16) | (uint64(0x1F90) << 48) );
1166 *data << uint32(0); // unknown
1167 // alliance/horde for BG and skirmish/rated for Arenas
1168 *data << uint8(bg->isArena() ? ( israted ? israted : bg->isRated() ) : bg->GetTeamIndexByTeamId(team));
1169 /* *data << uint8(arenatype ? arenatype : bg->GetArenaType()); // team type (0=BG, 2=2x2, 3=3x3, 5=5x5), for arenas // NOT PROPER VALUE IF ARENA ISN'T RUNNING YET!!!!
1170 switch(bg->GetTypeID()) // value depends on bg id
1172 case BATTLEGROUND_AV:
1173 *data << uint8(1);
1174 break;
1175 case BATTLEGROUND_WS:
1176 *data << uint8(2);
1177 break;
1178 case BATTLEGROUND_AB:
1179 *data << uint8(3);
1180 break;
1181 case BATTLEGROUND_NA:
1182 *data << uint8(4);
1183 break;
1184 case BATTLEGROUND_BE:
1185 *data << uint8(5);
1186 break;
1187 case BATTLEGROUND_AA:
1188 *data << uint8(6);
1189 break;
1190 case BATTLEGROUND_EY:
1191 *data << uint8(7);
1192 break;
1193 case BATTLEGROUND_RL:
1194 *data << uint8(8);
1195 break;
1196 default: // unknown
1197 *data << uint8(0);
1198 break;
1201 if(bg->isArena() && (StatusID == STATUS_WAIT_QUEUE))
1202 *data << uint32(BATTLEGROUND_AA); // all arenas I don't think so.
1203 else
1204 *data << uint32(bg->GetTypeID()); // BG id from DBC
1206 *data << uint16(0x1F90); // unk value 8080
1207 *data << uint32(bg->GetInstanceID()); // instance id
1209 if(bg->isBattleGround())
1210 *data << uint8(bg->GetTeamIndexByTeamId(team)); // team
1211 else
1212 *data << uint8(israted?israted:bg->isRated()); // is rated battle
1214 *data << uint32(StatusID); // status
1215 switch(StatusID)
1217 case STATUS_WAIT_QUEUE: // status_in_queue
1218 *data << uint32(Time1); // average wait time, milliseconds
1219 *data << uint32(Time2); // time in queue, updated every minute?
1220 break;
1221 case STATUS_WAIT_JOIN: // status_invite
1222 *data << uint32(bg->GetMapId()); // map id
1223 *data << uint32(Time1); // time to remove from queue, milliseconds
1224 break;
1225 case STATUS_IN_PROGRESS: // status_in_progress
1226 *data << uint32(bg->GetMapId()); // map id
1227 *data << uint32(Time1); // 0 at bg start, 120000 after bg end, time to bg auto leave, milliseconds
1228 *data << uint32(Time2); // time from bg start, milliseconds
1229 *data << uint8(0x1); // unk sometimes 0x0!
1230 break;
1231 default:
1232 sLog.outError("Unknown BG status!");
1233 break;
1237 void BattleGroundMgr::BuildPvpLogDataPacket(WorldPacket *data, BattleGround *bg)
1239 uint8 type = (bg->isArena() ? 1 : 0);
1240 // last check on 2.4.1
1241 data->Initialize(MSG_PVP_LOG_DATA, (1+1+4+40*bg->GetPlayerScoresSize()));
1242 *data << uint8(type); // seems to be type (battleground=0/arena=1)
1244 if(type) // arena
1246 // it seems this must be according to BG_WINNER_A/H and _NOT_ BG_TEAM_A/H
1247 for(int i = 1; i >= 0; --i)
1249 *data << uint32(3000-bg->m_ArenaTeamRatingChanges[i]); // rating change: showed value - 3000
1250 *data << uint32(3999); // huge thanks for TOM_RUS for this!
1251 sLog.outDebug("rating change: %d", bg->m_ArenaTeamRatingChanges[i]);
1253 for(int i = 1; i >= 0; --i)
1255 uint32 at_id = bg->m_ArenaTeamIds[i];
1256 ArenaTeam * at = objmgr.GetArenaTeamById(at_id);
1257 if(at)
1258 *data << at->GetName();
1259 else//*/
1260 *data << (uint8)0;
1264 if(bg->GetWinner() == 2)
1266 *data << uint8(0); // bg in progress
1268 else
1270 *data << uint8(1); // bg ended
1271 *data << uint8(bg->GetWinner()); // who win
1274 *data << (int32)(bg->GetPlayerScoresSize());
1276 for(std::map<uint64, BattleGroundScore*>::const_iterator itr = bg->GetPlayerScoresBegin(); itr != bg->GetPlayerScoresEnd(); ++itr)
1278 *data << (uint64)itr->first;
1279 *data << (int32)itr->second->KillingBlows;
1280 Player *plr = objmgr.GetPlayer(itr->first);
1281 uint32 team = bg->GetPlayerTeam(itr->first);
1282 if(!team && plr) team = plr->GetTeam();
1283 if(type == 0)
1285 *data << (int32)itr->second->HonorableKills;
1286 *data << (int32)itr->second->Deaths;
1287 *data << (int32)(itr->second->BonusHonor);
1289 else
1291 // that part probably wrong
1292 if(plr)
1294 if(team == HORDE)
1295 *data << uint8(0);
1296 else if(team == ALLIANCE)
1298 *data << uint8(1);
1300 else
1301 *data << uint8(0);
1303 else
1304 *data << uint8(0);
1306 *data << (int32)itr->second->DamageDone; // damage done
1307 *data << (int32)itr->second->HealingDone; // healing done
1308 switch(bg->GetTypeID()) // battleground specific things
1310 case BATTLEGROUND_AV:
1311 *data << (uint32)0x00000005; // count of next fields
1312 *data << (uint32)((BattleGroundAVScore*)itr->second)->GraveyardsAssaulted; // GraveyardsAssaulted
1313 *data << (uint32)((BattleGroundAVScore*)itr->second)->GraveyardsDefended; // GraveyardsDefended
1314 *data << (uint32)((BattleGroundAVScore*)itr->second)->TowersAssaulted; // TowersAssaulted
1315 *data << (uint32)((BattleGroundAVScore*)itr->second)->TowersDefended; // TowersDefended
1316 *data << (uint32)((BattleGroundAVScore*)itr->second)->MinesCaptured; // MinesCaptured
1317 break;
1318 case BATTLEGROUND_WS:
1319 *data << (uint32)0x00000002; // count of next fields
1320 *data << (uint32)((BattleGroundWGScore*)itr->second)->FlagCaptures; // flag captures
1321 *data << (uint32)((BattleGroundWGScore*)itr->second)->FlagReturns; // flag returns
1322 break;
1323 case BATTLEGROUND_AB:
1324 *data << (uint32)0x00000002; // count of next fields
1325 *data << (uint32)((BattleGroundABScore*)itr->second)->BasesAssaulted; // bases asssulted
1326 *data << (uint32)((BattleGroundABScore*)itr->second)->BasesDefended; // bases defended
1327 break;
1328 case BATTLEGROUND_EY:
1329 *data << (uint32)0x00000001; // count of next fields
1330 *data << (uint32)((BattleGroundEYScore*)itr->second)->FlagCaptures; // flag captures
1331 break;
1332 case BATTLEGROUND_NA:
1333 case BATTLEGROUND_BE:
1334 case BATTLEGROUND_AA:
1335 case BATTLEGROUND_RL:
1336 case BATTLEGROUND_SA: // wotlk
1337 case BATTLEGROUND_DS: // wotlk
1338 case BATTLEGROUND_RV: // wotlk
1339 *data << (int32)0; // 0
1340 break;
1341 default:
1342 sLog.outDebug("Unhandled MSG_PVP_LOG_DATA for BG id %u", bg->GetTypeID());
1343 *data << (int32)0;
1344 break;
1349 void BattleGroundMgr::BuildGroupJoinedBattlegroundPacket(WorldPacket *data, uint32 bgTypeId)
1351 /*bgTypeId is:
1352 0 - Your group has joined a battleground queue, but you are not eligible
1353 1 - Your group has joined the queue for AV
1354 2 - Your group has joined the queue for WS
1355 3 - Your group has joined the queue for AB
1356 4 - Your group has joined the queue for NA
1357 5 - Your group has joined the queue for BE Arena
1358 6 - Your group has joined the queue for All Arenas
1359 7 - Your group has joined the queue for EotS*/
1360 data->Initialize(SMSG_GROUP_JOINED_BATTLEGROUND, 4);
1361 *data << uint32(bgTypeId);
1364 void BattleGroundMgr::BuildUpdateWorldStatePacket(WorldPacket *data, uint32 field, uint32 value)
1366 data->Initialize(SMSG_UPDATE_WORLD_STATE, 4+4);
1367 *data << uint32(field);
1368 *data << uint32(value);
1371 void BattleGroundMgr::BuildPlaySoundPacket(WorldPacket *data, uint32 soundid)
1373 data->Initialize(SMSG_PLAY_SOUND, 4);
1374 *data << uint32(soundid);
1377 void BattleGroundMgr::BuildPlayerLeftBattleGroundPacket(WorldPacket *data, Player *plr)
1379 data->Initialize(SMSG_BATTLEGROUND_PLAYER_LEFT, 8);
1380 *data << uint64(plr->GetGUID());
1383 void BattleGroundMgr::BuildPlayerJoinedBattleGroundPacket(WorldPacket *data, Player *plr)
1385 data->Initialize(SMSG_BATTLEGROUND_PLAYER_JOINED, 8);
1386 *data << uint64(plr->GetGUID());
1389 void BattleGroundMgr::InvitePlayer(Player* plr, uint32 bgInstanceGUID, uint32 team)
1391 // set invited player counters:
1392 BattleGround* bg = GetBattleGround(bgInstanceGUID);
1393 if(!bg)
1394 return;
1395 bg->IncreaseInvitedCount(team);
1397 plr->SetInviteForBattleGroundQueueType(BGQueueTypeId(bg->GetTypeID(),bg->GetArenaType()), bgInstanceGUID);
1399 // set the arena teams for rated matches
1400 if(bg->isArena() && bg->isRated())
1402 switch(bg->GetArenaType())
1404 case ARENA_TYPE_2v2:
1405 bg->SetArenaTeamIdForTeam(team, plr->GetArenaTeamId(0));
1406 break;
1407 case ARENA_TYPE_3v3:
1408 bg->SetArenaTeamIdForTeam(team, plr->GetArenaTeamId(1));
1409 break;
1410 case ARENA_TYPE_5v5:
1411 bg->SetArenaTeamIdForTeam(team, plr->GetArenaTeamId(2));
1412 break;
1413 default:
1414 break;
1418 // create invite events:
1419 //add events to player's counters ---- this is not good way - there should be something like global event processor, where we should add those events
1420 BGQueueInviteEvent* inviteEvent = new BGQueueInviteEvent(plr->GetGUID(), bgInstanceGUID);
1421 plr->m_Events.AddEvent(inviteEvent, plr->m_Events.CalculateTime(INVITE_ACCEPT_WAIT_TIME/2));
1422 BGQueueRemoveEvent* removeEvent = new BGQueueRemoveEvent(plr->GetGUID(), bgInstanceGUID, team);
1423 plr->m_Events.AddEvent(removeEvent, plr->m_Events.CalculateTime(INVITE_ACCEPT_WAIT_TIME));
1426 BattleGround * BattleGroundMgr::GetBattleGroundTemplate(uint32 bgTypeId)
1428 return BGFreeSlotQueue[bgTypeId].empty() ? NULL : BGFreeSlotQueue[bgTypeId].back();
1431 // create a new battleground that will really be used to play
1432 BattleGround * BattleGroundMgr::CreateNewBattleGround(uint32 bgTypeId)
1434 BattleGround *bg = NULL;
1436 // get the template BG
1437 BattleGround *bg_template = GetBattleGroundTemplate(bgTypeId);
1439 if(!bg_template)
1441 sLog.outError("BattleGround: CreateNewBattleGround - bg template not found for %u", bgTypeId);
1442 return 0;
1445 // create a copy of the BG template
1446 switch(bgTypeId)
1448 case BATTLEGROUND_AV:
1449 bg = new BattleGroundAV(*(BattleGroundAV*)bg_template);
1450 break;
1451 case BATTLEGROUND_WS:
1452 bg = new BattleGroundWS(*(BattleGroundWS*)bg_template);
1453 break;
1454 case BATTLEGROUND_AB:
1455 bg = new BattleGroundAB(*(BattleGroundAB*)bg_template);
1456 break;
1457 case BATTLEGROUND_NA:
1458 bg = new BattleGroundNA(*(BattleGroundNA*)bg_template);
1459 break;
1460 case BATTLEGROUND_BE:
1461 bg = new BattleGroundBE(*(BattleGroundBE*)bg_template);
1462 break;
1463 case BATTLEGROUND_AA:
1464 bg = new BattleGroundAA(*(BattleGroundAA*)bg_template);
1465 break;
1466 case BATTLEGROUND_EY:
1467 bg = new BattleGroundEY(*(BattleGroundEY*)bg_template);
1468 break;
1469 case BATTLEGROUND_RL:
1470 bg = new BattleGroundRL(*(BattleGroundRL*)bg_template);
1471 break;
1472 default:
1473 //bg = new BattleGround;
1474 return 0;
1475 break; // placeholder for non implemented BG
1478 // generate a new instance id
1479 bg->SetInstanceID(MapManager::Instance().GenerateInstanceId()); // set instance id
1481 // reset the new bg (set status to status_wait_queue from status_none)
1482 bg->Reset();
1484 /* will be setup in BG::Update() when the first player is ported in
1485 if(!(bg->SetupBattleGround()))
1487 sLog.outError("BattleGround: CreateNewBattleGround: SetupBattleGround failed for bg %u", bgTypeId);
1488 delete bg;
1489 return 0;
1493 // add BG to free slot queue
1494 bg->AddToBGFreeSlotQueue();
1496 // add bg to update list
1497 AddBattleGround(bg->GetInstanceID(), bg);
1499 return bg;
1502 // used to create the BG templates
1503 uint32 BattleGroundMgr::CreateBattleGround(uint32 bgTypeId, 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)
1505 // Create the BG
1506 BattleGround *bg = NULL;
1508 switch(bgTypeId)
1510 case BATTLEGROUND_AV: bg = new BattleGroundAV; break;
1511 case BATTLEGROUND_WS: bg = new BattleGroundWS; break;
1512 case BATTLEGROUND_AB: bg = new BattleGroundAB; break;
1513 case BATTLEGROUND_NA: bg = new BattleGroundNA; break;
1514 case BATTLEGROUND_BE: bg = new BattleGroundBE; break;
1515 case BATTLEGROUND_AA: bg = new BattleGroundAA; break;
1516 case BATTLEGROUND_EY: bg = new BattleGroundEY; break;
1517 case BATTLEGROUND_RL: bg = new BattleGroundRL; break;
1518 default:bg = new BattleGround; break; // placeholder for non implemented BG
1521 bg->SetMapId(MapID);
1523 bg->Reset();
1525 BattlemasterListEntry const *bl = sBattlemasterListStore.LookupEntry(bgTypeId);
1526 //in previous method is checked if exists entry in sBattlemasterListStore, so no check needed
1527 if (bl)
1529 bg->SetArenaorBGType(bl->type == TYPE_ARENA);
1532 bg->SetTypeID(bgTypeId);
1533 bg->SetInstanceID(0); // template bg, instance id is 0
1534 bg->SetMinPlayersPerTeam(MinPlayersPerTeam);
1535 bg->SetMaxPlayersPerTeam(MaxPlayersPerTeam);
1536 bg->SetMinPlayers(MinPlayersPerTeam*2);
1537 bg->SetMaxPlayers(MaxPlayersPerTeam*2);
1538 bg->SetName(BattleGroundName);
1539 bg->SetTeamStartLoc(ALLIANCE, Team1StartLocX, Team1StartLocY, Team1StartLocZ, Team1StartLocO);
1540 bg->SetTeamStartLoc(HORDE, Team2StartLocX, Team2StartLocY, Team2StartLocZ, Team2StartLocO);
1541 bg->SetLevelRange(LevelMin, LevelMax);
1543 //add BattleGround instance to FreeSlotQueue (.back() will return the template!)
1544 bg->AddToBGFreeSlotQueue();
1546 // do NOT add to update list, since this is a template battleground!
1548 // return some not-null value, bgTypeId is good enough for me
1549 return bgTypeId;
1552 void BattleGroundMgr::CreateInitialBattleGrounds()
1554 float AStartLoc[4];
1555 float HStartLoc[4];
1556 uint32 MaxPlayersPerTeam, MinPlayersPerTeam, MinLvl, MaxLvl, start1, start2;
1557 BattlemasterListEntry const *bl;
1558 WorldSafeLocsEntry const *start;
1560 uint32 count = 0;
1562 // 0 1 2 3 4 5 6 7 8
1563 QueryResult *result = WorldDatabase.Query("SELECT id, MinPlayersPerTeam,MaxPlayersPerTeam,MinLvl,MaxLvl,AllianceStartLoc,AllianceStartO,HordeStartLoc,HordeStartO FROM battleground_template");
1565 if(!result)
1567 barGoLink bar(1);
1569 bar.step();
1571 sLog.outString();
1572 sLog.outErrorDb(">> Loaded 0 battlegrounds. DB table `battleground_template` is empty.");
1573 return;
1576 barGoLink bar(result->GetRowCount());
1580 Field *fields = result->Fetch();
1581 bar.step();
1583 uint32 bgTypeID = fields[0].GetUInt32();
1585 // can be overwrited by values from DB
1586 bl = sBattlemasterListStore.LookupEntry(bgTypeID);
1587 if(!bl)
1589 sLog.outError("Battleground ID %u not found in BattlemasterList.dbc. Battleground not created.",bgTypeID);
1590 continue;
1593 MaxPlayersPerTeam = bl->maxplayersperteam;
1594 MinPlayersPerTeam = bl->maxplayersperteam/2;
1595 MinLvl = bl->minlvl;
1596 MaxLvl = bl->maxlvl;
1598 if(fields[1].GetUInt32())
1599 MinPlayersPerTeam = fields[1].GetUInt32();
1601 if(fields[2].GetUInt32())
1602 MaxPlayersPerTeam = fields[2].GetUInt32();
1604 if(fields[3].GetUInt32())
1605 MinLvl = fields[3].GetUInt32();
1607 if(fields[4].GetUInt32())
1608 MaxLvl = fields[4].GetUInt32();
1610 start1 = fields[5].GetUInt32();
1612 start = sWorldSafeLocsStore.LookupEntry(start1);
1613 if(start)
1615 AStartLoc[0] = start->x;
1616 AStartLoc[1] = start->y;
1617 AStartLoc[2] = start->z;
1618 AStartLoc[3] = fields[6].GetFloat();
1620 else if(bgTypeID == BATTLEGROUND_AA)
1622 AStartLoc[0] = 0;
1623 AStartLoc[1] = 0;
1624 AStartLoc[2] = 0;
1625 AStartLoc[3] = fields[6].GetFloat();
1627 else
1629 sLog.outErrorDb("Table `battleground_template` for id %u have non-existed WorldSafeLocs.dbc id %u in field `AllianceStartLoc`. BG not created.",bgTypeID,start1);
1630 continue;
1633 start2 = fields[7].GetUInt32();
1635 start = sWorldSafeLocsStore.LookupEntry(start2);
1636 if(start)
1638 HStartLoc[0] = start->x;
1639 HStartLoc[1] = start->y;
1640 HStartLoc[2] = start->z;
1641 HStartLoc[3] = fields[8].GetFloat();
1643 else if(bgTypeID == BATTLEGROUND_AA)
1645 HStartLoc[0] = 0;
1646 HStartLoc[1] = 0;
1647 HStartLoc[2] = 0;
1648 HStartLoc[3] = fields[8].GetFloat();
1650 else
1652 sLog.outErrorDb("Table `battleground_template` for id %u have non-existed WorldSafeLocs.dbc id %u in field `HordeStartLoc`. BG not created.",bgTypeID,start2);
1653 continue;
1656 //sLog.outDetail("Creating battleground %s, %u-%u", bl->name[sWorld.GetDBClang()], MinLvl, MaxLvl);
1657 if(!CreateBattleGround(bgTypeID, MinPlayersPerTeam, MaxPlayersPerTeam, MinLvl, MaxLvl, bl->name[sWorld.GetDefaultDbcLocale()], bl->mapid[0], AStartLoc[0], AStartLoc[1], AStartLoc[2], AStartLoc[3], HStartLoc[0], HStartLoc[1], HStartLoc[2], HStartLoc[3]))
1658 continue;
1660 ++count;
1661 } while (result->NextRow());
1663 delete result;
1665 sLog.outString();
1666 sLog.outString( ">> Loaded %u battlegrounds", count );
1669 void BattleGroundMgr::InitAutomaticArenaPointDistribution()
1671 if(m_AutoDistributePoints)
1673 sLog.outDebug("Initializing Automatic Arena Point Distribution");
1674 QueryResult * result = CharacterDatabase.Query("SELECT NextArenaPointDistributionTime FROM saved_variables");
1675 if(!result)
1677 sLog.outDebug("Battleground: Next arena point distribution time not found in SavedVariables, reseting it now.");
1678 m_NextAutoDistributionTime = sWorld.GetGameTime() + BATTLEGROUND_ARENA_POINT_DISTRIBUTION_DAY * sWorld.getConfig(CONFIG_ARENA_AUTO_DISTRIBUTE_INTERVAL_DAYS);
1679 CharacterDatabase.PExecute("INSERT INTO saved_variables (NextArenaPointDistributionTime) VALUES ('"I64FMTD"')", m_NextAutoDistributionTime);
1681 else
1683 m_NextAutoDistributionTime = (*result)[0].GetUInt64();
1684 delete result;
1686 sLog.outDebug("Automatic Arena Point Distribution initialized.");
1690 void BattleGroundMgr::DistributeArenaPoints()
1692 // used to distribute arena points based on last week's stats
1693 sWorld.SendGlobalText("Flushing Arena points based on team ratings, this may take a few minutes. Please stand by...", NULL);
1695 sWorld.SendGlobalText("Distributing arena points to players...", NULL);
1697 //temporary structure for storing maximum points to add values for all players
1698 std::map<uint32, uint32> PlayerPoints;
1700 //at first update all points for all team members
1701 for(ObjectMgr::ArenaTeamMap::iterator team_itr = objmgr.GetArenaTeamMapBegin(); team_itr != objmgr.GetArenaTeamMapEnd(); ++team_itr)
1703 if(ArenaTeam * at = team_itr->second)
1705 at->UpdateArenaPointsHelper(PlayerPoints);
1709 //cycle that gives points to all players
1710 for (std::map<uint32, uint32>::iterator plr_itr = PlayerPoints.begin(); plr_itr != PlayerPoints.end(); ++plr_itr)
1712 //update to database
1713 CharacterDatabase.PExecute("UPDATE characters SET arena_pending_points = '%u' WHERE `guid` = '%u'", plr_itr->second, plr_itr->first);
1714 //add points if player is online
1715 Player* pl = objmgr.GetPlayer(plr_itr->first);
1716 if (pl)
1717 pl->ModifyArenaPoints(plr_itr->second);
1720 PlayerPoints.clear();
1722 sWorld.SendGlobalText("Finished setting arena points for online players.", NULL);
1724 sWorld.SendGlobalText("Modifying played count, arena points etc. for loaded arena teams, sending updated stats to online players...", NULL);
1725 for(ObjectMgr::ArenaTeamMap::iterator titr = objmgr.GetArenaTeamMapBegin(); titr != objmgr.GetArenaTeamMapEnd(); ++titr)
1727 if(ArenaTeam * at = titr->second)
1729 at->FinishWeek(); // set played this week etc values to 0 in memory, too
1730 at->SaveToDB(); // save changes
1731 at->NotifyStatsChanged(); // notify the players of the changes
1735 sWorld.SendGlobalText("Modification done.", NULL);
1737 sWorld.SendGlobalText("Done flushing Arena points.", NULL);
1740 void BattleGroundMgr::BuildBattleGroundListPacket(WorldPacket *data, uint64 guid, Player* plr, uint32 bgTypeId)
1742 uint32 PlayerLevel = 10;
1744 if(plr)
1745 PlayerLevel = plr->getLevel();
1747 data->Initialize(SMSG_BATTLEFIELD_LIST);
1748 *data << uint64(guid); // battlemaster guid
1749 *data << uint32(bgTypeId); // battleground id
1750 if(bgTypeId == BATTLEGROUND_AA) // arena
1752 *data << uint8(5); // unk
1753 *data << uint32(0); // unk
1755 else // battleground
1757 *data << uint8(0x00); // unk
1759 size_t count_pos = data->wpos();
1760 uint32 count = 0;
1761 *data << uint32(0x00); // number of bg instances
1763 for(std::map<uint32, BattleGround*>::iterator itr = m_BattleGrounds.begin(); itr != m_BattleGrounds.end(); ++itr)
1765 if(itr->second->GetTypeID() == bgTypeId && (PlayerLevel >= itr->second->GetMinLevel()) && (PlayerLevel <= itr->second->GetMaxLevel()))
1767 *data << uint32(itr->second->GetInstanceID());
1768 ++count;
1771 data->put<uint32>( count_pos , count);
1775 void BattleGroundMgr::SendToBattleGround(Player *pl, uint32 instanceId)
1777 BattleGround *bg = GetBattleGround(instanceId);
1778 if(bg)
1780 uint32 mapid = bg->GetMapId();
1781 float x, y, z, O;
1782 uint32 team = pl->GetBGTeam();
1783 if(team==0)
1784 team = pl->GetTeam();
1785 bg->GetTeamStartLoc(team, x, y, z, O);
1787 sLog.outDetail("BATTLEGROUND: Sending %s to map %u, X %f, Y %f, Z %f, O %f", pl->GetName(), mapid, x, y, z, O);
1788 pl->TeleportTo(mapid, x, y, z, O);
1790 else
1792 sLog.outError("player %u trying to port to non-existent bg instance %u",pl->GetGUIDLow(), instanceId);
1796 void BattleGroundMgr::SendAreaSpiritHealerQueryOpcode(Player *pl, BattleGround *bg, uint64 guid)
1798 WorldPacket data(SMSG_AREA_SPIRIT_HEALER_TIME, 12);
1799 uint32 time_ = 30000 - bg->GetLastResurrectTime(); // resurrect every 30 seconds
1800 if(time_ == uint32(-1))
1801 time_ = 0;
1802 data << guid << time_;
1803 pl->GetSession()->SendPacket(&data);
1806 void BattleGroundMgr::RemoveBattleGround(uint32 instanceID)
1808 BattleGroundSet::iterator itr = m_BattleGrounds.find(instanceID);
1809 if(itr!=m_BattleGrounds.end())
1810 m_BattleGrounds.erase(itr);
1813 bool BattleGroundMgr::IsArenaType(uint32 bgTypeId) const
1815 return ( bgTypeId == BATTLEGROUND_AA ||
1816 bgTypeId == BATTLEGROUND_BE ||
1817 bgTypeId == BATTLEGROUND_NA ||
1818 bgTypeId == BATTLEGROUND_RL );
1821 bool BattleGroundMgr::IsBattleGroundType(uint32 bgTypeId) const
1823 return !IsArenaType(bgTypeId);
1826 uint32 BattleGroundMgr::BGQueueTypeId(uint32 bgTypeId, uint8 arenaType) const
1828 switch(bgTypeId)
1830 case BATTLEGROUND_WS:
1831 return BATTLEGROUND_QUEUE_WS;
1832 case BATTLEGROUND_AB:
1833 return BATTLEGROUND_QUEUE_AB;
1834 case BATTLEGROUND_AV:
1835 return BATTLEGROUND_QUEUE_AV;
1836 case BATTLEGROUND_EY:
1837 return BATTLEGROUND_QUEUE_EY;
1838 case BATTLEGROUND_AA:
1839 case BATTLEGROUND_NA:
1840 case BATTLEGROUND_RL:
1841 case BATTLEGROUND_BE:
1842 switch(arenaType)
1844 case ARENA_TYPE_2v2:
1845 return BATTLEGROUND_QUEUE_2v2;
1846 case ARENA_TYPE_3v3:
1847 return BATTLEGROUND_QUEUE_3v3;
1848 case ARENA_TYPE_5v5:
1849 return BATTLEGROUND_QUEUE_5v5;
1850 default:
1851 return 0;
1853 default:
1854 return 0;
1858 uint32 BattleGroundMgr::BGTemplateId(uint32 bgQueueTypeId) const
1860 switch(bgQueueTypeId)
1862 case BATTLEGROUND_QUEUE_WS:
1863 return BATTLEGROUND_WS;
1864 case BATTLEGROUND_QUEUE_AB:
1865 return BATTLEGROUND_AB;
1866 case BATTLEGROUND_QUEUE_AV:
1867 return BATTLEGROUND_AV;
1868 case BATTLEGROUND_QUEUE_EY:
1869 return BATTLEGROUND_EY;
1870 case BATTLEGROUND_QUEUE_2v2:
1871 case BATTLEGROUND_QUEUE_3v3:
1872 case BATTLEGROUND_QUEUE_5v5:
1873 return BATTLEGROUND_AA;
1874 default:
1875 return 0;
1879 uint8 BattleGroundMgr::BGArenaType(uint32 bgQueueTypeId) const
1881 switch(bgQueueTypeId)
1883 case BATTLEGROUND_QUEUE_2v2:
1884 return ARENA_TYPE_2v2;
1885 case BATTLEGROUND_QUEUE_3v3:
1886 return ARENA_TYPE_3v3;
1887 case BATTLEGROUND_QUEUE_5v5:
1888 return ARENA_TYPE_5v5;
1889 default:
1890 return 0;
1894 void BattleGroundMgr::ToggleArenaTesting()
1896 m_ArenaTesting = !m_ArenaTesting;
1897 if(m_ArenaTesting)
1898 sWorld.SendGlobalText("Arenas are set to 1v1 for debugging. So, don't join as group.", NULL);
1899 else
1900 sWorld.SendGlobalText("Arenas are set to normal playercount.", NULL);