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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "ObjectMgr.h"
#include "BattlePetMgr.h"
#include "Containers.h"
#include "Player.h"
#include "WorldSession.h"
void BattlePetMgr::BattlePet::CalculateStats()
{
float health = 0.0f;
float power = 0.0f;
float speed = 0.0f;
// get base breed stats
auto breedState = _battlePetBreedStates.find(PacketInfo.Breed);
if (breedState == _battlePetBreedStates.end()) // non existing breed id
return;
health = breedState->second[STATE_STAT_STAMINA];
power = breedState->second[STATE_STAT_POWER];
speed = breedState->second[STATE_STAT_SPEED];
// modify stats depending on species - not all pets have this
auto speciesState = _battlePetSpeciesStates.find(PacketInfo.Species);
if (speciesState != _battlePetSpeciesStates.end())
{
health += speciesState->second[STATE_STAT_STAMINA];
power += speciesState->second[STATE_STAT_POWER];
speed += speciesState->second[STATE_STAT_SPEED];
}
// modify stats by quality
for (auto itr : sBattlePetBreedQualityStore)
{
if (itr->Quality == PacketInfo.Quality)
{
health *= itr->Modifier;
power *= itr->Modifier;
speed *= itr->Modifier;
break;
}
// TOOD: add check if pet has existing quality
}
// scale stats depending on level
health *= PacketInfo.Level;
power *= PacketInfo.Level;
speed *= PacketInfo.Level;
// set stats
// round, ceil or floor? verify this
PacketInfo.MaxHealth = uint32((round(health / 20) + 100));
PacketInfo.Power = uint32(round(power / 100));
PacketInfo.Speed = uint32(round(speed / 100));
}
std::unordered_map<uint16 /*BreedID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/, std::hash<std::underlying_type<BattlePetState>::type> >> BattlePetMgr::_battlePetBreedStates;
std::unordered_map<uint32 /*SpeciesID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/, std::hash<std::underlying_type<BattlePetState>::type> >> BattlePetMgr::_battlePetSpeciesStates;
std::unordered_map<uint32 /*SpeciesID*/, std::unordered_set<uint8 /*breed*/>> BattlePetMgr::_availableBreedsPerSpecies;
std::unordered_map<uint32 /*SpeciesID*/, uint8 /*quality*/> BattlePetMgr::_defaultQualityPerSpecies;
void BattlePetMgr::Initialize()
{
if (QueryResult result = LoginDatabase.Query("SELECT MAX(guid) FROM battle_pets"))
sObjectMgr->GetGenerator<HighGuid::BattlePet>().Set((*result)[0].GetUInt64() + 1);
for (auto itr : sBattlePetBreedStateStore)
_battlePetBreedStates[itr->BreedID][BattlePetState(itr->State)] = itr->Value;
for (auto itr : sBattlePetSpeciesStateStore)
_battlePetSpeciesStates[itr->SpeciesID][BattlePetState(itr->State)] = itr->Value;
LoadAvailablePetBreeds();
LoadDefaultPetQualities();
}
void BattlePetMgr::LoadAvailablePetBreeds()
{
QueryResult result = WorldDatabase.Query("SELECT speciesId, breedId FROM battle_pet_breeds");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 battle pet breeds. DB table `battle_pet_breeds` is empty.");
return;
}
uint32 count = 0;
do
{
Field* fields = result->Fetch();
uint32 speciesId = fields[0].GetUInt32();
uint16 breedId = fields[1].GetUInt16();
if (!sBattlePetSpeciesStore.LookupEntry(speciesId))
{
TC_LOG_ERROR("sql.sql", "Non-existing BattlePetSpecies.db2 entry %u was referenced in `battle_pet_breeds` by row (%u, %u).", speciesId, speciesId, breedId);
continue;
}
// TODO: verify breed id (3 - 12 (male) or 3 - 22 (male and female)) if needed
_availableBreedsPerSpecies[speciesId].insert(breedId);
++count;
} while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded %u battle pet breeds.", count);
}
void BattlePetMgr::LoadDefaultPetQualities()
{
QueryResult result = WorldDatabase.Query("SELECT speciesId, quality FROM battle_pet_quality");
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 battle pet qualities. DB table `battle_pet_quality` is empty.");
return;
}
do
{
Field* fields = result->Fetch();
uint32 speciesId = fields[0].GetUInt32();
uint8 quality = fields[1].GetUInt8();
if (!sBattlePetSpeciesStore.LookupEntry(speciesId))
{
TC_LOG_ERROR("sql.sql", "Non-existing BattlePetSpecies.db2 entry %u was referenced in `battle_pet_quality` by row (%u, %u).", speciesId, speciesId, quality);
continue;
}
// TODO: verify quality (0 - 3 for player pets or 0 - 5 for both player and tamer pets) if needed
_defaultQualityPerSpecies[speciesId] = quality;
} while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded %u battle pet qualities.", uint32(_defaultQualityPerSpecies.size()));
}
uint16 BattlePetMgr::RollPetBreed(uint32 species)
{
auto itr = _availableBreedsPerSpecies.find(species);
if (itr == _availableBreedsPerSpecies.end())
return 3; // default B/B
return Trinity::Containers::SelectRandomContainerElement(itr->second);
}
uint8 BattlePetMgr::GetDefaultPetQuality(uint32 species)
{
auto itr = _defaultQualityPerSpecies.find(species);
if (itr == _defaultQualityPerSpecies.end())
return 0; // default poor
return itr->second;
}
BattlePetMgr::BattlePetMgr(WorldSession* owner)
{
_owner = owner;
for (uint8 i = 0; i < MAX_PET_BATTLE_SLOTS; ++i)
{
WorldPackets::BattlePet::BattlePetSlot slot;
slot.Index = i;
_slots.push_back(slot);
}
}
void BattlePetMgr::LoadFromDB(PreparedQueryResult pets, PreparedQueryResult slots)
{
if (pets)
{
do
{
Field* fields = pets->Fetch();
uint32 species = fields[1].GetUInt32();
if (BattlePetSpeciesEntry const* speciesEntry = sBattlePetSpeciesStore.LookupEntry(species))
{
if (GetPetCount(species) >= MAX_BATTLE_PETS_PER_SPECIES)
{
TC_LOG_ERROR("misc", "Battlenet account with id %u has more than 3 battle pets of species %u", _owner->GetBattlenetAccountId(), species);
continue;
}
BattlePet pet;
pet.PacketInfo.Guid = ObjectGuid::Create<HighGuid::BattlePet>(fields[0].GetUInt64());
pet.PacketInfo.Species = species;
pet.PacketInfo.Breed = fields[2].GetUInt16();
pet.PacketInfo.Level = fields[3].GetUInt16();
pet.PacketInfo.Exp = fields[4].GetUInt16();
pet.PacketInfo.Health = fields[5].GetUInt32();
pet.PacketInfo.Quality = fields[6].GetUInt8();
pet.PacketInfo.Flags = fields[7].GetUInt16();
pet.PacketInfo.Name = fields[8].GetString();
pet.PacketInfo.CreatureID = speciesEntry->CreatureID;
pet.SaveInfo = BATTLE_PET_UNCHANGED;
pet.CalculateStats();
_pets[pet.PacketInfo.Guid.GetCounter()] = pet;
}
} while (pets->NextRow());
}
if (slots)
{
uint8 i = 0; // slots->GetRowCount() should equal MAX_BATTLE_PET_SLOTS
do
{
Field* fields = slots->Fetch();
_slots[i].Index = fields[0].GetUInt8();
auto itr = _pets.find(fields[1].GetUInt64());
if (itr != _pets.end())
_slots[i].Pet = itr->second.PacketInfo;
_slots[i].Locked = fields[2].GetBool();
i++;
} while (slots->NextRow());
}
}
void BattlePetMgr::SaveToDB(SQLTransaction& trans)
{
PreparedStatement* stmt = nullptr;
for (auto itr = _pets.begin(); itr != _pets.end();)
{
switch (itr->second.SaveInfo)
{
case BATTLE_PET_NEW:
stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_BATTLE_PETS);
stmt->setUInt64(0, itr->first);
stmt->setUInt32(1, _owner->GetBattlenetAccountId());
stmt->setUInt32(2, itr->second.PacketInfo.Species);
stmt->setUInt16(3, itr->second.PacketInfo.Breed);
stmt->setUInt16(4, itr->second.PacketInfo.Level);
stmt->setUInt16(5, itr->second.PacketInfo.Exp);
stmt->setUInt32(6, itr->second.PacketInfo.Health);
stmt->setUInt8(7, itr->second.PacketInfo.Quality);
stmt->setUInt16(8, itr->second.PacketInfo.Flags);
stmt->setString(9, itr->second.PacketInfo.Name);
trans->Append(stmt);
itr->second.SaveInfo = BATTLE_PET_UNCHANGED;
++itr;
break;
case BATTLE_PET_CHANGED:
stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BATTLE_PETS);
stmt->setUInt16(0, itr->second.PacketInfo.Level);
stmt->setUInt16(1, itr->second.PacketInfo.Exp);
stmt->setUInt32(2, itr->second.PacketInfo.Health);
stmt->setUInt8(3, itr->second.PacketInfo.Quality);
stmt->setUInt16(4, itr->second.PacketInfo.Flags);
stmt->setString(5, itr->second.PacketInfo.Name);
stmt->setUInt32(6, _owner->GetBattlenetAccountId());
stmt->setUInt64(7, itr->first);
trans->Append(stmt);
itr->second.SaveInfo = BATTLE_PET_UNCHANGED;
++itr;
break;
case BATTLE_PET_REMOVED:
stmt = LoginDatabase.GetPreparedStatement(LOGIN_DEL_BATTLE_PETS);
stmt->setUInt32(0, _owner->GetBattlenetAccountId());
stmt->setUInt64(1, itr->first);
trans->Append(stmt);
itr = _pets.erase(itr);
break;
default:
++itr;
break;
}
}
stmt = LoginDatabase.GetPreparedStatement(LOGIN_DEL_BATTLE_PET_SLOTS);
stmt->setUInt32(0, _owner->GetBattlenetAccountId());
trans->Append(stmt);
for (WorldPackets::BattlePet::BattlePetSlot const& slot : _slots)
{
stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_BATTLE_PET_SLOTS);
stmt->setUInt8(0, slot.Index);
stmt->setUInt32(1, _owner->GetBattlenetAccountId());
stmt->setUInt64(2, slot.Pet.Guid.GetCounter());
stmt->setBool(3, slot.Locked);
trans->Append(stmt);
}
}
BattlePetMgr::BattlePet* BattlePetMgr::GetPet(ObjectGuid guid)
{
auto itr = _pets.find(guid.GetCounter());
if (itr != _pets.end())
return &itr->second;
return nullptr;
}
void BattlePetMgr::AddPet(uint32 species, uint32 creatureId, uint16 breed, uint8 quality, uint16 level /*= 1*/)
{
BattlePetSpeciesEntry const* battlePetSpecies = sBattlePetSpeciesStore.LookupEntry(species);
if (!battlePetSpecies) // should never happen
return;
BattlePet pet;
pet.PacketInfo.Guid = ObjectGuid::Create<HighGuid::BattlePet>(sObjectMgr->GetGenerator<HighGuid::BattlePet>().Generate());
pet.PacketInfo.Species = species;
pet.PacketInfo.CreatureID = creatureId;
pet.PacketInfo.Level = level;
pet.PacketInfo.Exp = 0;
pet.PacketInfo.Flags = 0;
pet.PacketInfo.Breed = breed;
pet.PacketInfo.Quality = quality;
pet.PacketInfo.Name = "";
pet.CalculateStats();
pet.PacketInfo.Health = pet.PacketInfo.MaxHealth;
pet.SaveInfo = BATTLE_PET_NEW;
_pets[pet.PacketInfo.Guid.GetCounter()] = pet;
std::vector<BattlePet> updates;
updates.push_back(pet);
SendUpdates(updates, true);
_owner->GetPlayer()->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_OWN_BATTLE_PET, species);
}
void BattlePetMgr::RemovePet(ObjectGuid guid)
{
BattlePet* pet = GetPet(guid);
if (!pet)
return;
pet->SaveInfo = BATTLE_PET_REMOVED;
// spell is not unlearned on retail
/*if (GetPetCount(pet->PacketInfo.Species) == 0)
if (BattlePetSpeciesEntry const* speciesEntry = sBattlePetSpeciesStore.LookupEntry(pet->PacketInfo.Species))
_owner->GetPlayer()->RemoveSpell(speciesEntry->SummonSpellID);*/
}
uint8 BattlePetMgr::GetPetCount(uint32 species) const
{
uint8 count = 0;
for (auto& itr : _pets)
if (itr.second.PacketInfo.Species == species && itr.second.SaveInfo != BATTLE_PET_REMOVED)
count++;
return count;
}
void BattlePetMgr::UnlockSlot(uint8 slot)
{
if (!_slots[slot].Locked)
return;
_slots[slot].Locked = false;
WorldPackets::BattlePet::PetBattleSlotUpdates updates;
updates.Slots.push_back(_slots[slot]);
updates.AutoSlotted = false; // what's this?
updates.NewSlot = true; // causes the "new slot unlocked" bubble to appear
_owner->SendPacket(updates.Write());
}
std::vector<BattlePetMgr::BattlePet> BattlePetMgr::GetLearnedPets() const
{
std::vector<BattlePet> pets;
for (auto& pet : _pets)
if (pet.second.SaveInfo != BATTLE_PET_REMOVED)
pets.push_back(pet.second);
return pets;
}
void BattlePetMgr::CageBattlePet(ObjectGuid guid)
{
BattlePet* pet = GetPet(guid);
if (!pet)
return;
ItemPosCountVec dest;
if (_owner->GetPlayer()->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, BATTLE_PET_CAGE_ITEM_ID, 1) != EQUIP_ERR_OK)
return;
Item* item = _owner->GetPlayer()->StoreNewItem(dest, BATTLE_PET_CAGE_ITEM_ID, true);
if (!item)
return;
item->SetModifier(ITEM_MODIFIER_BATTLE_PET_SPECIES_ID, pet->PacketInfo.Species);
item->SetModifier(ITEM_MODIFIER_BATTLE_PET_BREED_DATA, pet->PacketInfo.Breed | (pet->PacketInfo.Quality << 24));
item->SetModifier(ITEM_MODIFIER_BATTLE_PET_LEVEL, pet->PacketInfo.Level);
item->SetModifier(ITEM_MODIFIER_BATTLE_PET_DISPLAY_ID, pet->PacketInfo.CreatureID);
// FIXME: "You create: ." - item name missing in chat
_owner->GetPlayer()->SendNewItem(item, 1, true, true);
RemovePet(guid);
WorldPackets::BattlePet::BattlePetDeleted deletePet;
deletePet.PetGuid = guid;
_owner->SendPacket(deletePet.Write());
}
void BattlePetMgr::HealBattlePetsPct(uint8 pct)
{
// TODO: After each Pet Battle, any injured companion will automatically
// regain 50 % of the damage that was taken during combat
std::vector<BattlePet> updates;
for (auto& pet : _pets)
if (pet.second.PacketInfo.Health != pet.second.PacketInfo.MaxHealth)
{
pet.second.PacketInfo.Health += CalculatePct(pet.second.PacketInfo.MaxHealth, pct);
// don't allow Health to be greater than MaxHealth
pet.second.PacketInfo.Health = std::min(pet.second.PacketInfo.Health, pet.second.PacketInfo.MaxHealth);
if (pet.second.SaveInfo != BATTLE_PET_NEW)
pet.second.SaveInfo = BATTLE_PET_CHANGED;
updates.push_back(pet.second);
}
SendUpdates(updates, false);
}
void BattlePetMgr::SummonPet(ObjectGuid guid)
{
BattlePet* pet = GetPet(guid);
if (!pet)
return;
BattlePetSpeciesEntry const* speciesEntry = sBattlePetSpeciesStore.LookupEntry(pet->PacketInfo.Species);
if (!speciesEntry)
return;
// TODO: set proper CreatureID for spell DEFAULT_SUMMON_BATTLE_PET_SPELL (default EffectMiscValueA is 40721 - Murkimus the Gladiator)
_owner->GetPlayer()->CastSpell(_owner->GetPlayer(), speciesEntry->SummonSpellID ? speciesEntry->SummonSpellID : uint32(DEFAULT_SUMMON_BATTLE_PET_SPELL));
// TODO: set pet level, quality... update fields
}
void BattlePetMgr::SendUpdates(std::vector<BattlePet> pets, bool petAdded)
{
WorldPackets::BattlePet::BattlePetUpdates updates;
for (auto pet : pets)
updates.Pets.push_back(pet.PacketInfo);
updates.PetAdded = petAdded;
_owner->SendPacket(updates.Write());
}
void BattlePetMgr::SendError(BattlePetError error, uint32 creatureId)
{
WorldPackets::BattlePet::BattlePetError battlePetError;
battlePetError.Result = error;
battlePetError.CreatureID = creatureId;
_owner->SendPacket(battlePetError.Write());
}
|