This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 422
/
Pet.cpp
2079 lines (1755 loc) · 68.7 KB
/
Pet.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011-2019 Project SkyFire <http://www.projectskyfire.org/>
* Copyright (C) 2008-2019 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2019 MaNGOS <https://www.getmangos.eu/>
*
* 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 3 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 "Common.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "WorldPacket.h"
#include "ObjectMgr.h"
#include "SpellMgr.h"
#include "Pet.h"
#include "Formulas.h"
#include "SpellAuras.h"
#include "SpellAuraEffects.h"
#include "CreatureAI.h"
#include "Unit.h"
#include "Util.h"
#include "Group.h"
#include "Opcodes.h"
#include "WorldSession.h"
#define PET_XP_FACTOR 0.05f
Pet::Pet(Player* owner, PetType type) :
Guardian(NULL, owner, true), m_usedTalentCount(0), m_removed(false),
m_petType(type), m_duration(0), m_auraRaidUpdateMask(0), m_loading(false),
m_declinedname(NULL)
{
ASSERT(m_owner->GetTypeId() == TYPEID_PLAYER);
m_unitTypeMask |= UNIT_MASK_PET;
if (type == HUNTER_PET)
m_unitTypeMask |= UNIT_MASK_HUNTER_PET;
if (!(m_unitTypeMask & UNIT_MASK_CONTROLABLE_GUARDIAN))
{
m_unitTypeMask |= UNIT_MASK_CONTROLABLE_GUARDIAN;
InitCharmInfo();
}
m_name = "Pet";
m_regenTimer = PET_FOCUS_REGEN_INTERVAL;
}
Pet::~Pet()
{
delete m_declinedname;
}
void Pet::AddToWorld()
{
///- Register the pet for guid lookup
if (!IsInWorld())
{
///- Register the pet for guid lookup
sObjectAccessor->AddObject(this);
Unit::AddToWorld();
AIM_Initialize();
}
// Prevent stuck pets when zoning. Pets default to "follow" when added to world
// so we'll reset flags and let the AI handle things
if (GetCharmInfo() && GetCharmInfo()->HasCommandState(COMMAND_FOLLOW))
{
GetCharmInfo()->SetIsCommandAttack(false);
GetCharmInfo()->SetIsCommandFollow(false);
GetCharmInfo()->SetIsAtStay(false);
GetCharmInfo()->SetIsFollowing(false);
GetCharmInfo()->SetIsReturning(false);
}
}
void Pet::RemoveFromWorld()
{
///- Remove the pet from the accessor
if (IsInWorld())
{
///- Don't call the function for Creature, normal mobs + totems go in a different storage
Unit::RemoveFromWorld();
sObjectAccessor->RemoveObject(this);
}
}
bool Pet::LoadPetFromDB(Player* owner, uint32 petEntry, uint32 petnumber, bool current)
{
m_loading = true;
uint32 ownerid = owner->GetGUIDLow();
PreparedStatement* stmt;
PreparedQueryResult result;
if (petnumber)
{
// Known petnumber entry
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHAR_PET_BY_ENTRY);
stmt->setUInt32(0, ownerid);
stmt->setUInt32(1, petnumber);
}
else if (current)
{
// Current pet (slot 0)
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHAR_PET_BY_ENTRY_AND_SLOT);
stmt->setUInt32(0, ownerid);
stmt->setUInt8(1, uint8(PET_SAVE_AS_CURRENT));
}
else if (petEntry)
{
// known petEntry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHAR_PET_BY_ENTRY_AND_SLOT_2);
stmt->setUInt32(0, ownerid);
stmt->setUInt32(1, petEntry);
stmt->setUInt8(2, uint8(PET_SAVE_AS_CURRENT));
stmt->setUInt8(3, uint8(PET_SAVE_LAST_STABLE_SLOT));
}
else
{
// Any current or other non-stabled pet (for hunter "call pet")
stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHAR_PET_BY_SLOT);
stmt->setUInt32(0, ownerid);
stmt->setUInt8(1, uint8(PET_SAVE_AS_CURRENT));
stmt->setUInt8(2, uint8(PET_SAVE_LAST_STABLE_SLOT));
}
result = CharacterDatabase.Query(stmt);
if (!result)
{
m_loading = false;
return false;
}
Field* fields = result->Fetch();
// update for case of current pet "slot = 0"
petEntry = fields[1].GetUInt32();
if (!petEntry)
return false;
uint32 summonSpellId = fields[14].GetUInt32();
SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(summonSpellId);
bool isTemporarySummon = spellInfo && spellInfo->GetDuration() > 0;
if (current && isTemporarySummon)
return false;
PetType petType = PetType(fields[15].GetUInt8());
if (petType == HUNTER_PET)
{
CreatureTemplate const* creatureInfo = sObjectMgr->GetCreatureTemplate(petEntry);
if (!creatureInfo || !creatureInfo->IsTameable(owner->CanTameExoticPets()))
return false;
}
uint32 petId = fields[0].GetUInt32();
if (current && owner->IsPetNeedBeTemporaryUnsummoned())
{
owner->SetTemporaryUnsummonedPetNumber(petId);
return false;
}
Map* map = owner->GetMap();
uint32 guid = sObjectMgr->GenerateLowGuid(HIGHGUID_PET);
if (!Create(guid, map, /*owner->GetPhaseMask(),*/ petEntry, petId))
return false;
float px, py, pz;
owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, GetFollowAngle());
Relocate(px, py, pz, owner->GetOrientation());
if (!IsPositionValid())
{
SF_LOG_ERROR("entities.pet", "Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
return false;
}
for (auto itr : owner->GetPhases())
SetPhased(itr, false, true);
setPetType(petType);
setFaction(owner->getFaction());
SetUInt32Value(UNIT_FIELD_CREATED_BY_SPELL, summonSpellId);
CreatureTemplate const* cinfo = GetCreatureTemplate();
if (cinfo->type == CREATURE_TYPE_CRITTER)
{
map->AddToMap(this->ToCreature());
return true;
}
m_charmInfo->SetPetNumber(petId, IsPermanentPetFor(owner));
SetDisplayId(fields[3].GetUInt32());
SetNativeDisplayId(fields[3].GetUInt32());
uint32 petlevel = fields[4].GetUInt16();
SetUInt32Value(UNIT_FIELD_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
SetName(fields[8].GetString());
switch (getPetType())
{
case SUMMON_PET:
petlevel = owner->getLevel();
SetClass(CLASS_MAGE);
SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
// this enables popup window (pet dismiss, cancel)
break;
case HUNTER_PET:
SetSheath(SHEATH_STATE_MELEE);
SetClass(CLASS_WARRIOR);
SetGender(GENDER_NONE);
SetFieldPowerType(POWER_FOCUS);
SetByteFlag(UNIT_FIELD_SHAPESHIFT_FORM, 2, fields[9].GetBool() ? UNIT_CAN_BE_ABANDONED : UNIT_CAN_BE_RENAMED | UNIT_CAN_BE_ABANDONED);
SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
// this enables popup window (pet abandon, cancel)
setPowerType(POWER_FOCUS);
break;
default:
if (!IsPetGhoul())
SF_LOG_ERROR("entities.pet", "Pet have incorrect type (%u) for pet loading.", getPetType());
break;
}
SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, uint32(time(NULL))); // cast can't be helped here
SetCreatorGUID(owner->GetGUID());
InitStatsForLevel(petlevel);
SetUInt32Value(UNIT_FIELD_PET_EXPERIENCE, fields[5].GetUInt32());
SynchronizeLevelWithOwner();
SetReactState(ReactStates(fields[6].GetUInt8()));
SetCanModifyStats(true);
if (getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
else
{
uint32 savedhealth = fields[10].GetUInt32();
uint32 savedmana = fields[11].GetUInt32();
if (!savedhealth && getPetType() == HUNTER_PET)
setDeathState(JUST_DIED);
else
{
SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
SetPower(POWER_MANA, savedmana > uint32(GetMaxPower(POWER_MANA)) ? GetMaxPower(POWER_MANA) : savedmana);
}
}
// set current pet as current
// 0=current
// 1..MAX_PET_STABLES in stable slot
// PET_SAVE_NOT_IN_SLOT(100) = not stable slot (summoning))
if (fields[7].GetUInt8())
{
SQLTransaction trans = CharacterDatabase.BeginTransaction();
stmt = CharacterDatabase.GetPreparedStatement(CHAR_UDP_CHAR_PET_SLOT_BY_SLOT_EXCLUDE_ID);
stmt->setUInt8(0, uint8(PET_SAVE_NOT_IN_SLOT));
stmt->setUInt32(1, ownerid);
stmt->setUInt8(2, uint8(PET_SAVE_AS_CURRENT));
stmt->setUInt32(3, m_charmInfo->GetPetNumber());
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_CHAR_PET_SLOT_BY_ID);
stmt->setUInt8(0, uint8(PET_SAVE_AS_CURRENT));
stmt->setUInt32(1, ownerid);
stmt->setUInt32(2, m_charmInfo->GetPetNumber());
trans->Append(stmt);
CharacterDatabase.CommitTransaction(trans);
}
// Send fake summon spell cast - this is needed for correct cooldown application for spells
// Example: 46584 - without this cooldown (which should be set always when pet is loaded) isn't set clientside
/// @todo pets should be summoned from real cast instead of just faking it?
// TODO FIX THIS GOD DAMN HACK
/*if (summonSpellId)
{
WorldPacket data(SMSG_SPELL_GO, (8+8+4+4+2));
data.append(owner->GetPackGUID());
data.append(owner->GetPackGUID());
data << uint8(0);
data << uint32(summonSpellId);
data << uint32(256); // CAST_FLAG_UNKNOWN3
data << uint32(0);
owner->SendMessageToSet(&data, true);
}
*/
owner->SetMinion(this, true);
map->AddToMap(this->ToCreature());
InitTalentForLevel(); // set original talents points before spell loading
uint32 timediff = uint32(time(NULL) - fields[13].GetUInt32());
_LoadAuras(timediff);
// load action bar, if data broken will fill later by default spells.
if (!isTemporarySummon)
{
m_charmInfo->LoadPetActionBar(fields[12].GetString());
_LoadSpells();
InitTalentForLevel(); // re-init to check talent count
_LoadSpellCooldowns();
LearnPetPassives();
InitLevelupSpellsForLevel();
CastPetAuras(current);
}
CleanupActionBar(); // remove unknown spells from action bar after load
SF_LOG_DEBUG("entities.pet", "New Pet has guid %u", GetGUIDLow());
owner->PetSpellInitialize();
if (owner->GetGroup())
owner->SetGroupUpdateFlag(GROUP_UPDATE_PET);
if (getPetType() == HUNTER_PET)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_PET_DECLINED_NAME);
stmt->setUInt32(0, owner->GetGUIDLow());
stmt->setUInt32(1, GetCharmInfo()->GetPetNumber());
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
{
delete m_declinedname;
m_declinedname = new DeclinedName;
Field* fields2 = result->Fetch();
for (uint8 i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
{
m_declinedname->name[i] = fields2[i].GetString();
}
}
}
//set last used pet number (for use in BG's)
if (owner->GetTypeId() == TYPEID_PLAYER && isControlled() && !isTemporarySummoned() && (getPetType() == SUMMON_PET || getPetType() == HUNTER_PET))
owner->ToPlayer()->SetLastPetNumber(petId);
m_loading = false;
return true;
}
void Pet::SavePetToDB(PetSaveMode mode)
{
if (!GetEntry())
return;
// save only fully controlled creature
if (!isControlled())
return;
// not save not player pets
if (!IS_PLAYER_GUID(GetOwnerGUID()))
return;
Player* owner = GetOwner();
if (!owner)
return;
// not save pet as current if another pet temporary unsummoned
if (mode == PET_SAVE_AS_CURRENT && owner->GetTemporaryUnsummonedPetNumber() &&
owner->GetTemporaryUnsummonedPetNumber() != m_charmInfo->GetPetNumber())
{
// pet will lost anyway at restore temporary unsummoned
if (getPetType() == HUNTER_PET)
return;
// for warlock case
mode = PET_SAVE_NOT_IN_SLOT;
}
uint32 curhealth = GetHealth();
uint32 curmana = GetPower(POWER_MANA);
SQLTransaction trans = CharacterDatabase.BeginTransaction();
// save auras before possibly removing them
_SaveAuras(trans);
// stable and not in slot saves
if (mode > PET_SAVE_AS_CURRENT)
RemoveAllAuras();
_SaveSpells(trans);
_SaveSpellCooldowns(trans);
CharacterDatabase.CommitTransaction(trans);
// current/stable/not_in_slot
if (mode >= PET_SAVE_AS_CURRENT)
{
uint32 ownerLowGUID = GUID_LOPART(GetOwnerGUID());
std::string name = m_name;
CharacterDatabase.EscapeString(name);
trans = CharacterDatabase.BeginTransaction();
// remove current data
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_PET_BY_ID);
stmt->setUInt32(0, m_charmInfo->GetPetNumber());
trans->Append(stmt);
// prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
if (mode <= PET_SAVE_LAST_STABLE_SLOT)
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_UDP_CHAR_PET_SLOT_BY_SLOT);
stmt->setUInt8(0, uint8(PET_SAVE_NOT_IN_SLOT));
stmt->setUInt32(1, ownerLowGUID);
stmt->setUInt8(2, uint8(mode));
trans->Append(stmt);
}
// prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
if (getPetType() == HUNTER_PET && (mode == PET_SAVE_AS_CURRENT || mode > PET_SAVE_LAST_STABLE_SLOT))
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_PET_BY_SLOT);
stmt->setUInt32(0, ownerLowGUID);
stmt->setUInt8(1, uint8(PET_SAVE_AS_CURRENT));
stmt->setUInt8(2, uint8(PET_SAVE_LAST_STABLE_SLOT));
trans->Append(stmt);
}
// save pet
std::ostringstream ss;
ss << "INSERT INTO character_pet (id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, abdata, savetime, CreatedBySpell, PetType) "
<< "VALUES ("
<< m_charmInfo->GetPetNumber() << ','
<< GetEntry() << ','
<< ownerLowGUID << ','
<< GetNativeDisplayId() << ','
<< uint32(getLevel()) << ','
<< GetUInt32Value(UNIT_FIELD_PET_EXPERIENCE) << ','
<< uint32(GetReactState()) << ','
<< uint32(mode) << ", '"
<< name.c_str() << "', "
<< uint32(HasByteFlag(UNIT_FIELD_SHAPESHIFT_FORM, 2, UNIT_CAN_BE_RENAMED) ? 0 : 1) << ','
<< curhealth << ','
<< curmana << ", '";
for (uint32 i = ACTION_BAR_INDEX_START; i < ACTION_BAR_INDEX_END; ++i)
{
ss << uint32(m_charmInfo->GetActionBarEntry(i)->GetType()) << ' '
<< uint32(m_charmInfo->GetActionBarEntry(i)->GetAction()) << ' ';
};
ss << "', "
<< time(NULL) << ','
<< GetUInt32Value(UNIT_FIELD_CREATED_BY_SPELL) << ','
<< uint32(getPetType()) << ')';
trans->Append(ss.str().c_str());
CharacterDatabase.CommitTransaction(trans);
}
// delete
else
{
RemoveAllAuras();
DeleteFromDB(m_charmInfo->GetPetNumber());
}
}
void Pet::DeleteFromDB(uint32 guidlow)
{
SQLTransaction trans = CharacterDatabase.BeginTransaction();
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_PET_BY_ID);
stmt->setUInt32(0, guidlow);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHAR_PET_DECLINEDNAME);
stmt->setUInt32(0, guidlow);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PET_AURAS);
stmt->setUInt32(0, guidlow);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PET_SPELLS);
stmt->setUInt32(0, guidlow);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PET_SPELL_COOLDOWNS);
stmt->setUInt32(0, guidlow);
trans->Append(stmt);
CharacterDatabase.CommitTransaction(trans);
}
void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
{
Creature::setDeathState(s);
if (getDeathState() == CORPSE)
{
if (getPetType() == HUNTER_PET)
{
// pet corpse non lootable and non skinnable
SetUInt32Value(OBJECT_FIELD_DYNAMIC_FLAGS, UNIT_DYNFLAG_NONE);
RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
//SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
}
}
else if (getDeathState() == ALIVE)
{
//RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
CastPetAuras(true);
}
}
void Pet::Update(uint32 diff)
{
if (m_removed) // pet already removed, just wait in remove queue, no updates
return;
if (m_loading)
return;
switch (m_deathState)
{
case CORPSE:
{
if (getPetType() != HUNTER_PET || m_corpseRemoveTime <= time(NULL))
{
Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
return;
}
break;
}
case ALIVE:
{
// unsummon pet that lost owner
Player* owner = GetOwner();
if (!owner || (!IsWithinDistInMap(owner, GetMap()->GetVisibilityRange()) && !isPossessed()) || (isControlled() && !owner->GetPetGUID()))
//if (!owner || (!IsWithinDistInMap(owner, GetMap()->GetVisibilityDistance()) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
{
Remove(PET_SAVE_NOT_IN_SLOT, true);
return;
}
if (isControlled())
{
if (owner->GetPetGUID() != GetGUID())
{
SF_LOG_ERROR("entities.pet", "Pet %u is not pet of owner %s, removed", GetEntry(), GetOwner()->GetName().c_str());
Remove(getPetType() == HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
return;
}
}
if (m_duration > 0)
{
if (uint32(m_duration) > diff)
m_duration -= diff;
else
{
Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
return;
}
}
//regenerate focus for hunter pets or energy for deathknight's ghoul
if (m_regenTimer)
{
if (m_regenTimer > diff)
m_regenTimer -= diff;
else
{
switch (getPowerType())
{
case POWER_FOCUS:
Regenerate(POWER_FOCUS);
m_regenTimer += PET_FOCUS_REGEN_INTERVAL - diff;
if (!m_regenTimer) ++m_regenTimer;
// Reset if large diff (lag) causes focus to get 'stuck'
if (m_regenTimer > PET_FOCUS_REGEN_INTERVAL)
m_regenTimer = PET_FOCUS_REGEN_INTERVAL;
break;
// in creature::update
//case POWER_ENERGY:
// Regenerate(POWER_ENERGY);
// m_regenTimer += CREATURE_REGEN_INTERVAL - diff;
// if (!m_regenTimer) ++m_regenTimer;
// break;
default:
m_regenTimer = 0;
break;
}
}
}
break;
}
default:
break;
}
Creature::Update(diff);
}
void Creature::Regenerate(Powers power)
{
uint32 curValue = GetPower(power);
uint32 maxValue = GetMaxPower(power);
if (curValue >= maxValue)
return;
float addvalue = 0.0f;
switch (power)
{
case POWER_FOCUS:
{
// For hunter pets.
addvalue = 24 * sWorld->getRate(RATE_POWER_FOCUS);
break;
}
case POWER_ENERGY:
{
// For deathknight's ghoul.
addvalue = 20;
break;
}
default:
return;
}
// Apply modifiers (if any).
AuraEffectList const& ModPowerRegenPCTAuras = GetAuraEffectsByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
for (AuraEffectList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
if (Powers((*i)->GetMiscValue()) == power)
AddPct(addvalue, (*i)->GetAmount());
addvalue += GetTotalAuraModifierByMiscValue(SPELL_AURA_MOD_POWER_REGEN, power) * (IsHunterPet()? PET_FOCUS_REGEN_INTERVAL : CREATURE_REGEN_INTERVAL) / (5 * IN_MILLISECONDS);
ModifyPower(power, int32(addvalue));
}
void Pet::Remove(PetSaveMode mode, bool returnreagent)
{
GetOwner()->RemovePet(this, mode, returnreagent);
}
void Pet::GivePetXP(uint32 xp)
{
if (getPetType() != HUNTER_PET)
return;
if (xp < 1)
return;
if (!IsAlive())
return;
uint8 maxlevel = std::min((uint8)sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL), GetOwner()->getLevel());
uint8 petlevel = getLevel();
// If pet is detected to be at, or above(?) the players level, don't hand out XP
if (petlevel >= maxlevel)
return;
uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PET_NEXT_LEVEL_EXPERIENCE);
uint32 curXP = GetUInt32Value(UNIT_FIELD_PET_EXPERIENCE);
uint32 newXP = curXP + xp;
// Check how much XP the pet should receive, and hand off have any left from previous levelups
while (newXP >= nextLvlXP && petlevel < maxlevel)
{
// Subtract newXP from amount needed for nextlevel, and give pet the level
newXP -= nextLvlXP;
++petlevel;
GivePetLevel(petlevel);
nextLvlXP = GetUInt32Value(UNIT_FIELD_PET_NEXT_LEVEL_EXPERIENCE);
}
// Not affected by special conditions - give it new XP
SetUInt32Value(UNIT_FIELD_PET_EXPERIENCE, petlevel < maxlevel ? newXP : 0);
}
void Pet::GivePetLevel(uint8 level)
{
if (!level || level == getLevel())
return;
if (getPetType()==HUNTER_PET)
{
SetUInt32Value(UNIT_FIELD_PET_EXPERIENCE, 0);
SetUInt32Value(UNIT_FIELD_PET_NEXT_LEVEL_EXPERIENCE, uint32(sObjectMgr->GetXPForLevel(level)*PET_XP_FACTOR));
}
InitStatsForLevel(level);
InitLevelupSpellsForLevel();
InitTalentForLevel();
}
bool Pet::CreateBaseAtCreature(Creature* creature)
{
ASSERT(creature);
if (!CreateBaseAtTamed(creature->GetCreatureTemplate(), creature->GetMap()/*, creature->GetPhaseMask()*/))
return false;
Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
if (!IsPositionValid())
{
SF_LOG_ERROR("entities.pet", "Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
return false;
}
CreatureTemplate const* cinfo = GetCreatureTemplate();
if (!cinfo)
{
SF_LOG_ERROR("entities.pet", "CreateBaseAtCreature() failed, creatureInfo is missing!");
return false;
}
SetDisplayId(creature->GetDisplayId());
if (CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
SetName(cFamily->Name);
else
SetName(creature->GetNameForLocaleIdx(sObjectMgr->GetDBCLocaleIndex()));
return true;
}
bool Pet::CreateBaseAtCreatureInfo(CreatureTemplate const* cinfo, Unit* owner)
{
if (!CreateBaseAtTamed(cinfo, owner->GetMap()/*, owner->GetPhaseMask()*/))
return false;
if (CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
SetName(cFamily->Name);
Relocate(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation());
return true;
}
bool Pet::CreateBaseAtTamed(CreatureTemplate const* cinfo, Map* map/*, uint32 phaseMask*/)
{
SF_LOG_DEBUG("entities.pet", "Pet::CreateBaseForTamed");
uint32 guid=sObjectMgr->GenerateLowGuid(HIGHGUID_PET);
uint32 petId = sObjectMgr->GeneratePetNumber();
if (!Create(guid, map, /*phaseMask,*/ cinfo->Entry, petId))
return false;
setPowerType(POWER_FOCUS);
SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
SetUInt32Value(UNIT_FIELD_PET_EXPERIENCE, 0);
SetUInt32Value(UNIT_FIELD_PET_NEXT_LEVEL_EXPERIENCE, uint32(sObjectMgr->GetXPForLevel(getLevel()+1)*PET_XP_FACTOR));
SetUInt32Value(UNIT_FIELD_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
if (cinfo->type == CREATURE_TYPE_BEAST)
{
SetClass(CLASS_WARRIOR);
SetGender(GENDER_NONE);
SetFieldPowerType(POWER_FOCUS);
SetSheath(SHEATH_STATE_MELEE);
SetByteFlag(UNIT_FIELD_SHAPESHIFT_FORM, 2, UNIT_CAN_BE_RENAMED | UNIT_CAN_BE_ABANDONED);
}
return true;
}
/// @todo Move stat mods code to pet passive auras
bool Guardian::InitStatsForLevel(uint8 petlevel)
{
CreatureTemplate const* cinfo = GetCreatureTemplate();
ASSERT(cinfo);
SetLevel(petlevel);
//Determine pet type
PetType petType = MAX_PET_TYPE;
if (IsPet() && GetOwner()->GetTypeId() == TYPEID_PLAYER)
{
if (GetOwner()->getClass() == CLASS_WARLOCK
|| GetOwner()->getClass() == CLASS_SHAMAN // Fire Elemental
|| GetOwner()->getClass() == CLASS_DEATH_KNIGHT) // Risen Ghoul
{
petType = SUMMON_PET;
}
else if (GetOwner()->getClass() == CLASS_HUNTER)
{
petType = HUNTER_PET;
m_unitTypeMask |= UNIT_MASK_HUNTER_PET;
}
else
{
SF_LOG_ERROR("entities.pet", "Unknown type pet %u is summoned by player class %u",
GetEntry(), GetOwner()->getClass());
}
}
uint32 creature_ID = (petType == HUNTER_PET) ? 1 : cinfo->Entry;
SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
SetFloatValue(UNIT_FIELD_MOD_CASTING_SPEED, 1.0f);
SetFloatValue(UNIT_FIELD_MOD_SPELL_HASTE, 1.0f);
//scale
CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
if (cFamily && cFamily->minScale > 0.0f && petType == HUNTER_PET)
{
float scale;
if (getLevel() >= cFamily->maxScaleLevel)
scale = cFamily->maxScale;
else if (getLevel() <= cFamily->minScaleLevel)
scale = cFamily->minScale;
else
scale = cFamily->minScale + float(getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
SetObjectScale(scale);
}
// Resistance
for (uint8 i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(cinfo->resistance[i]));
//health, mana, armor and resistance
PetLevelInfo const* pInfo = sObjectMgr->GetPetLevelInfo(creature_ID, petlevel);
if (pInfo) // exist in DB
{
SetCreateHealth(pInfo->health);
if (petType != HUNTER_PET) //hunter pet use focus
SetCreateMana(pInfo->mana);
if (pInfo->armor > 0)
SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
for (uint8 stat = 0; stat < MAX_STATS; ++stat)
SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
}
else // not exist in DB, use some default fake data
{
// remove elite bonuses included in DB values
CreatureBaseStats const* stats = sObjectMgr->GetCreatureBaseStats(petlevel, cinfo->unit_class);
SetCreateHealth(stats->BaseHealth[(CURRENT_CONTENT_EXP == cinfo->expansion) ? 1 : 0]);
SetCreateMana(stats->BaseMana);
SetCreateStat(STAT_STRENGTH, 22);
SetCreateStat(STAT_AGILITY, 22);
SetCreateStat(STAT_STAMINA, 25);
SetCreateStat(STAT_INTELLECT, 28);
SetCreateStat(STAT_SPIRIT, 27);
}
SetBonusDamage(0);
switch (petType)
{
case SUMMON_PET:
{
// the damage bonus used for pets is either fire or shadow damage, whatever is higher
int32 fire = GetOwner()->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
int32 shadow = GetOwner()->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
int32 val = (fire > shadow) ? fire : shadow;
if (val < 0)
val = 0;
SetBonusDamage(val * 0.15f);
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
//SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
break;
}
case HUNTER_PET:
{
SetUInt32Value(UNIT_FIELD_PET_NEXT_LEVEL_EXPERIENCE, uint32(sObjectMgr->GetXPForLevel(petlevel)*PET_XP_FACTOR));
//these formula may not be correct; however, it is designed to be close to what it should be
//this makes dps 0.5 of pets level
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
//damage range is then petlevel / 2
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
//damage is increased afterwards as strength and pet scaling modify attack power
break;
}
default:
{
switch (GetEntry())
{
case 510: // mage Water Elemental
{
SetBonusDamage(int32(GetOwner()->SpellBaseDamageBonusDone(SPELL_SCHOOL_MASK_FROST) * 0.33f));
break;
}
case 1964: //force of nature
{
if (!pInfo)
SetCreateHealth(30 + 30*petlevel);
float bonusDmg = GetOwner()->SpellBaseDamageBonusDone(SPELL_SCHOOL_MASK_NATURE) * 0.15f;
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel * 2.5f - (petlevel / 2) + bonusDmg));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel * 2.5f + (petlevel / 2) + bonusDmg));
break;
}
case 15352: //earth elemental 36213
{
if (!pInfo)
SetCreateHealth(100 + 120*petlevel);
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
break;
}
case 15438: //fire elemental
{
if (!pInfo)
{
SetCreateHealth(40*petlevel);
SetCreateMana(28 + 10*petlevel);
}
SetBonusDamage(int32(GetOwner()->SpellBaseDamageBonusDone(SPELL_SCHOOL_MASK_FIRE) * 0.5f));
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel * 4 - petlevel));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel * 4 + petlevel));
break;
}
case 19668: // Shadowfiend
{
if (!pInfo)
{
SetCreateMana(28 + 10*petlevel);
SetCreateHealth(28 + 30*petlevel);
}
int32 bonus_dmg = int32(GetOwner()->SpellBaseDamageBonusDone(SPELL_SCHOOL_MASK_SHADOW)* 0.3f);
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float((petlevel * 4 - petlevel) + bonus_dmg));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float((petlevel * 4 + petlevel) + bonus_dmg));
break;
}
case 19833: //Snake Trap - Venomous Snake
{
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float((petlevel / 2) - 25));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float((petlevel / 2) - 18));
break;
}
case 19921: //Snake Trap - Viper
{
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel / 2 - 10));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel / 2));
break;
}
case 29264: // Feral Spirit
{
if (!pInfo)
SetCreateHealth(30*petlevel);
// wolf attack speed is 1.5s
SetAttackTime(BASE_ATTACK, cinfo->baseattacktime);
SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float((petlevel * 4 - petlevel)));
SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float((petlevel * 4 + petlevel)));
SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(GetOwner()->GetArmor()) * 0.35f); // Bonus Armor (35% of player armor)
SetModifierValue(UNIT_MOD_STAT_STAMINA, BASE_VALUE, float(GetOwner()->GetStat(STAT_STAMINA)) * 0.3f); // Bonus Stamina (30% of player stamina)
if (!HasAura(58877))//prevent apply twice for the 2 wolves
AddAura(58877, this);//Spirit Hunt, passive, Spirit Wolves' attacks heal them and their master for 150% of damage done.
break;
}
case 31216: // Mirror Image
{
SetBonusDamage(int32(GetOwner()->SpellBaseDamageBonusDone(SPELL_SCHOOL_MASK_FROST) * 0.33f));
SetDisplayId(GetOwner()->GetDisplayId());
if (!pInfo)
{
SetCreateMana(28 + 30*petlevel);
SetCreateHealth(28 + 10*petlevel);
}
break;
}
case 27829: // Ebon Gargoyle
{
if (!pInfo)