forked from cmangos/mangos-tbc
-
Notifications
You must be signed in to change notification settings - Fork 18
/
CFBG.cpp
344 lines (282 loc) · 10.9 KB
/
CFBG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "World.h"
#include "Custom.h"
#include "ObjectMgr.h"
#include "DBCStores.h"
#include "Player.h"
#include "BattleGround/BattleGround.h"
#include "BattleGround/BattleGroundMgr.h"
#include "CPlayer.h"
bool CPlayer::NativeTeam() const
{
return GetTeam() == GetOTeam();
}
void CPlayer::SetFakeValues()
{
m_oRace = GetByteValue(UNIT_FIELD_BYTES_0, 0);
m_oFaction = GetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE);
m_fRace = sCustom.PickFakeRace(getClass(), GetOTeam());
m_fFaction = Player::getFactionForRace(m_fRace);
m_oPlayerBytes = GetUInt32Value(PLAYER_BYTES);
m_oPlayerBytes2 = GetUInt32Value(PLAYER_BYTES_2);
m_fPlayerBytes = sCustom.GetFakePlayerBytes(m_fRace, getGender());
m_fPlayerBytes2 = sCustom.GetFakePlayerBytes2(m_fRace, getGender());
if (!m_fPlayerBytes)
m_fPlayerBytes = m_oPlayerBytes;
if (!m_fPlayerBytes2)
m_fPlayerBytes2 = m_oPlayerBytes2;
}
void CPlayer::RecachePlayersFromBG()
{
BattleGround* bg = GetBattleGround();
if (!bg)
return;
for (auto& itr : bg->GetPlayers())
{
if (Player* player = sObjectMgr.GetPlayer(itr.first))
{
if (!player->ToCPlayer()->NativeTeam())
{
// Erase bg players from source player cache
WorldPacket data(SMSG_INVALIDATE_PLAYER, 8);
data << player->GetObjectGuid();
GetSession()->SendPacket(&data);
// Send bg player data to source player
data = player->ToCPlayer()->BuildNameQuery();
GetSession()->SendPacket(&data);
}
if (!NativeTeam())
{
// Erase source player from bg players cache
WorldPacket data(SMSG_INVALIDATE_PLAYER, 8);
data << GetObjectGuid();
player->GetSession()->SendPacket(&data);
// Send new source data to bg players
data = BuildNameQuery();
player->GetSession()->SendPacket(&data);
}
}
else
{
// Couldn't find bgplayer, recache him for source player in case he logs in again.
WorldPacket data(SMSG_INVALIDATE_PLAYER, 8);
data << itr.first;
GetSession()->SendPacket(&data);
}
}
}
void CPlayer::RecachePlayersFromList()
{
for (auto& itr : m_FakedPlayers)
{
WorldPacket data(SMSG_INVALIDATE_PLAYER, 8);
data << itr;
GetSession()->SendPacket(&data);
if (Player* player = sObjectMgr.GetPlayer(itr))
{
WorldPacket data = player->ToCPlayer()->BuildNameQuery();
GetSession()->SendPacket(&data);
}
}
m_FakedPlayers.clear();
}
WorldPacket CPlayer::BuildNameQuery()
{
WorldPacket data(SMSG_NAME_QUERY_RESPONSE, (8 + 1 + 4 + 4 + 4 + 10));
data << GetObjectGuid(); // player guid
data << GetName(); // player name
data << uint8(0); // realm name
data << uint32(getRace()); // player race
data << uint32(getGender()); // player gender
data << uint32(getClass()); // player class
data << uint8(0); // is not declined
return data;
}
void CPlayer::FakeDisplayID()
{
if (NativeTeam())
return;
PlayerInfo const* info = sObjectMgr.GetPlayerInfo(getRace(), getClass());
if (!info)
{
for (auto i = 0; i < MAX_CLASSES; ++i)
{
info = sObjectMgr.GetPlayerInfo(getRace(), i);
if (info)
break;
}
}
if (!info)
{
sLog.outError("Player %u has incorrect race/class pair. Can't init display ids.", GetGUIDLow());
return;
}
// reset scale before reapply auras
SetObjectScale(DEFAULT_OBJECT_SCALE);
uint8 gender = getGender();
switch (gender)
{
case GENDER_FEMALE:
SetDisplayId(info->displayId_f);
SetNativeDisplayId(info->displayId_f);
break;
case GENDER_MALE:
SetDisplayId(info->displayId_m);
SetNativeDisplayId(info->displayId_m);
break;
default:
sLog.outError("Invalid gender %u for player", gender);
return;
}
SetUInt32Value(PLAYER_BYTES, getFPlayerBytes());
SetUInt32Value(PLAYER_BYTES_2, getFPlayerBytes2());
}
void CPlayer::JoinBattleGround(BattleGround* bg)
{
if (bg->isArena())
return;
if (!NativeTeam())
{
m_FakedPlayers.push_back(GetObjectGuid());
SetByteValue(UNIT_FIELD_BYTES_0, 0, getFRace());
setFaction(getFFaction());
}
SetRecache();
FakeDisplayID();
}
void CPlayer::LeaveBattleGround(BattleGround* bg)
{
if (bg->isArena())
return;
SetByteValue(UNIT_FIELD_BYTES_0, 0, getORace());
setFaction(getOFaction());
InitDisplayIds();
SetFakedPlayers(m_FakedPlayers);
SetRecache();
SetUInt32Value(PLAYER_BYTES, getOPlayerBytes());
SetUInt32Value(PLAYER_BYTES_2, getOPlayerBytes2());
}
bool CPlayer::SendBattleGroundChat(ChatMsg msgtype, std::string message)
{
// Select distance to broadcast to.
float distance = sWorld.getConfig(CONFIG_FLOAT_LISTEN_RANGE_SAY);
if (msgtype == CHAT_MSG_YELL)
sWorld.getConfig(CONFIG_FLOAT_LISTEN_RANGE_YELL);
else if (msgtype == CHAT_MSG_EMOTE)
sWorld.getConfig(CONFIG_FLOAT_LISTEN_RANGE_TEXTEMOTE);
BattleGround* pBattleGround = GetBattleGround();
if (!pBattleGround || pBattleGround->isArena()) // Only fake chat in BG's. CFBG should not interfere with arenas.
return false;
for (auto& itr : pBattleGround->GetPlayers())
{
if (Player* pPlayer = sObjectMgr.GetPlayer(itr.first))
{
if (GetDistance2d(pPlayer->GetPositionX(), pPlayer->GetPositionY()) <= distance)
{
WorldPacket data(SMSG_MESSAGECHAT, 200);
if (GetTeam() == pPlayer->GetTeam())
ChatHandler::BuildChatPacket(data, msgtype, message.c_str(), LANG_UNIVERSAL, GetChatTag(), GetObjectGuid(), GetName());
else if (msgtype != CHAT_MSG_EMOTE)
ChatHandler::BuildChatPacket(data, msgtype, message.c_str(), pPlayer->GetOTeam() == ALLIANCE ? LANG_ORCISH : LANG_COMMON, GetChatTag(), GetObjectGuid(), GetName());
pPlayer->GetSession()->SendPacket(&data);
}
}
}
return true;
}
bool BattleGroundQueue::CheckMixedMatch(BattleGround* bg_template, BattleGroundBracketId bracket_id, uint32 minPlayers, uint32 maxPlayers)
{
if (!sWorld.getConfig(CONFIG_BOOL_CFBG_ENABLED) || !bg_template->isBattleGround())
return false;
// Empty selection pool, we do not want old data.
m_SelectionPools[TEAM_INDEX_ALLIANCE].Init();
m_SelectionPools[TEAM_INDEX_HORDE].Init();
uint32 addedally = 0;
uint32 addedhorde = 0;
uint32 ilevelally = 0;
uint32 ilevelhorde = 0;
std::multimap<uint32, GroupQueueInfo*> ItemLevelSorting;
for (auto& itr : m_QueuedGroups[bracket_id][BG_QUEUE_NORMAL_ALLIANCE])
{
uint32 GroupIlevel = 0;
for (auto& jtr : itr->Players)
if (Player* pPlayer = sObjectMgr.GetPlayer(jtr.first))
GroupIlevel += pPlayer->ToCPlayer()->GetAVGILevel(true);
ItemLevelSorting.insert(std::make_pair(GroupIlevel, itr));
}
for (auto itr = ItemLevelSorting.crbegin(); itr != ItemLevelSorting.crend(); ++itr)
{
GroupQueueInfo* ginfo = itr->second;
if (!ginfo->IsInvitedToBGInstanceGUID)
{
ginfo->GroupTeam = TEAM_NONE;
if (ilevelally == ilevelhorde && addedally == addedhorde)
urand(0, 1) ? ginfo->GroupTeam = ALLIANCE : ginfo->GroupTeam = HORDE;
else if (ilevelally == ilevelhorde && addedally != addedhorde)
addedally < addedhorde ? ginfo->GroupTeam = ALLIANCE : ginfo->GroupTeam = HORDE;
if (ginfo->GroupTeam == TEAM_NONE)
ilevelally < ilevelhorde ? ginfo->GroupTeam = ALLIANCE : ginfo->GroupTeam = HORDE;
uint32 GroupIlevel = 0;
for (auto& jtr : ginfo->Players)
if (Player* pPlayer = sObjectMgr.GetPlayer(jtr.first))
GroupIlevel += pPlayer->ToCPlayer()->GetAVGILevel(true);
if (m_SelectionPools[ginfo->GroupTeam == ALLIANCE ? TEAM_INDEX_ALLIANCE : TEAM_INDEX_HORDE].AddGroup(ginfo, maxPlayers))
{
ginfo->GroupTeam == ALLIANCE ? ilevelally += GroupIlevel : ilevelhorde += GroupIlevel;
ginfo->GroupTeam == ALLIANCE ? addedally += GroupIlevel : addedhorde += GroupIlevel;
}
else
break;
if (m_SelectionPools[TEAM_INDEX_ALLIANCE].GetPlayerCount() >= minPlayers &&
m_SelectionPools[TEAM_INDEX_HORDE].GetPlayerCount() >= minPlayers)
return true;
}
}
if (sBattleGroundMgr.isTesting() &&
(m_SelectionPools[TEAM_INDEX_ALLIANCE].GetPlayerCount() ||
m_SelectionPools[TEAM_INDEX_HORDE].GetPlayerCount()))
return true;
return false;
}
bool BattleGroundQueue::MixPlayersToBG(BattleGround* bg, BattleGroundBracketId bracket_id)
{
if (!sWorld.getConfig(CONFIG_BOOL_CFBG_ENABLED) || bg->isArena())
return false;
int32 allyFree = bg->GetFreeSlotsForTeam(ALLIANCE);
int32 hordeFree = bg->GetFreeSlotsForTeam(HORDE);
uint32 addedally = bg->GetMaxPlayersPerTeam() - bg->GetFreeSlotsForTeam(ALLIANCE);
uint32 addedhorde = bg->GetMaxPlayersPerTeam() - bg->GetFreeSlotsForTeam(HORDE);
for (auto& itr : m_QueuedGroups[bracket_id][BG_QUEUE_NORMAL_ALLIANCE])
{
GroupQueueInfo* ginfo = itr;
if (!ginfo->IsInvitedToBGInstanceGUID)
{
bool makeally = addedally < addedhorde;
if (addedally == addedhorde)
makeally = urand(0, 1);
makeally ? ++addedally : ++addedhorde;
ginfo->GroupTeam = makeally ? ALLIANCE : HORDE;
if (m_SelectionPools[makeally ? TEAM_INDEX_ALLIANCE : TEAM_INDEX_HORDE].AddGroup(ginfo, makeally ? allyFree : hordeFree))
makeally ? addedally += ginfo->Players.size() : addedhorde += ginfo->Players.size();
else
break;
}
}
return true;
}