-
Notifications
You must be signed in to change notification settings - Fork 293
/
PetHandler.cpp
800 lines (659 loc) · 28.2 KB
/
PetHandler.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
/*
* This file is part of the CMaNGOS Project. 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 "Common.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "ObjectMgr.h"
#include "SpellMgr.h"
#include "Log.h"
#include "Opcodes.h"
#include "Spell.h"
#include "CreatureAI.h"
#include "Util.h"
#include "Pet.h"
#include "SpellAuras.h"
void WorldSession::HandlePetAction(WorldPacket& recv_data)
{
ObjectGuid petGuid;
uint32 data;
ObjectGuid targetGuid;
recv_data >> petGuid;
recv_data >> data;
recv_data >> targetGuid;
uint32 spellid = UNIT_ACTION_BUTTON_ACTION(data);
uint8 flag = UNIT_ACTION_BUTTON_TYPE(data); // delete = 0x07 CastSpell = C1
DETAIL_LOG("HandlePetAction: %s flag is %u, spellid is %u, target %s.", petGuid.GetString().c_str(), uint32(flag), spellid, targetGuid.GetString().c_str());
// used also for charmed creature/player
Unit* pet = _player->GetMap()->GetUnit(petGuid);
if (!pet)
{
sLog.outError("HandlePetAction: %s not exist.", petGuid.GetString().c_str());
return;
}
if (_player->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
{
sLog.outError("HandlePetAction: %s isn't controlled by %s.", petGuid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}
if (!pet->isAlive())
return;
if (pet->GetTypeId() == TYPEID_PLAYER && pet->GetCharmer()->GetTypeId() == TYPEID_PLAYER)
{
// controller player cannot use controlled player's spells
if (flag != (ACT_COMMAND || ACT_REACTION))
return;
}
else if (((Creature*)pet)->IsPet())
{
// pet can have action bar disabled
if (((Pet*)pet)->GetModeFlags() & PET_MODE_DISABLE_ACTIONS)
return;
}
CharmInfo* charmInfo = pet->GetCharmInfo();
if (!charmInfo)
{
sLog.outError("WorldSession::HandlePetAction: object (GUID: %u TypeId: %u) is considered pet-like but doesn't have a charminfo!", pet->GetGUIDLow(), pet->GetTypeId());
return;
}
switch (flag)
{
case ACT_COMMAND: // 0x07
switch (spellid)
{
case COMMAND_STAY: // flat=1792 // STAY
{
pet->StopMoving();
pet->AttackStop(true, true);
pet->GetMotionMaster()->Clear();
((Pet*)pet)->SetStayPosition(true);
((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();
charmInfo->SetCommandState(COMMAND_STAY);
break;
}
case COMMAND_FOLLOW: // spellid=1792 // FOLLOW
{
pet->StopMoving();
pet->AttackStop(true, true);
pet->GetMotionMaster()->Clear();
((Pet*)pet)->SetStayPosition();
((Pet*)pet)->SetIsRetreating(true);
((Pet*)pet)->SetSpellOpener();
charmInfo->SetCommandState(COMMAND_FOLLOW);
break;
}
case COMMAND_ATTACK: // spellid=1792 // ATTACK
{
((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();
Unit* targetUnit = targetGuid ? _player->GetMap()->GetUnit(targetGuid) : nullptr;
if (targetUnit && targetUnit != pet && targetUnit->isTargetableForAttack() && targetUnit->isInAccessablePlaceFor((Creature*)pet))
{
_player->SetInCombatState(true, targetUnit);
// This is true if pet has no target or has target but targets differs.
if (pet->getVictim() != targetUnit)
{
pet->AttackStop();
pet->GetMotionMaster()->Clear();
if (((Creature*)pet)->AI())
{
((Creature*)pet)->AI()->AttackStart(targetUnit);
// 10% chance to play special warlock pet attack talk, else growl
if (((Creature*)pet)->IsPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && roll_chance_i(10))
pet->SendPetTalk((uint32)PET_TALK_ATTACK);
pet->SendPetAIReaction();
}
else
pet->Attack(targetUnit, true);
}
}
break;
}
case COMMAND_ABANDON: // abandon (hunter pet) or dismiss (summoned pet)
{
Creature* petC = (Creature*)pet;
if (petC->IsPet())
{
Pet* p = (Pet*)petC;
if (p->getPetType() == HUNTER_PET)
p->Unsummon(PET_SAVE_AS_DELETED, _player);
else
// dismissing a summoned pet is like killing them (this prevents returning a soulshard...)
p->SetDeathState(CORPSE);
}
else // charmed
_player->Uncharm();
if (petC->IsTemporarySummon()) // special case when pet was temporary summon through DoSummonPossesed
{
petC->ForcedDespawn();
return;
}
((Pet*)pet)->SetStayPosition();
break;
}
default:
sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.", uint32(flag), spellid);
}
break;
case ACT_REACTION: // 0x6
switch (spellid)
{
case REACT_PASSIVE: // passive
{
pet->AttackStop(true, true);
((Pet*)pet)->SetSpellOpener();
}
case REACT_DEFENSIVE: // recovery
case REACT_AGGRESSIVE: // activete
{
charmInfo->SetReactState(ReactStates(spellid));
break;
}
}
break;
case ACT_DISABLED: // 0x81 spell (disabled), ignore
case ACT_PASSIVE: // 0x01
case ACT_ENABLED: // 0xC1 spell
{
((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();
Unit* unit_target = targetGuid ? _player->GetMap()->GetUnit(targetGuid) : nullptr;
// do not cast unknown spells
SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellid);
if (!spellInfo)
{
sLog.outError("WORLD: unknown PET spell id %i", spellid);
return;
}
if (pet->GetCharmInfo() && pet->GetCharmInfo()->GetGlobalCooldownMgr().HasGlobalCooldown(spellInfo))
return;
for (int i = 0; i < MAX_EFFECT_INDEX; ++i)
{
if (spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA
|| spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA_INSTANT
|| spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA_CHANNELED)
return;
}
// do not cast not learned spells
if (!pet->HasSpell(spellid) || IsPassiveSpell(spellInfo))
return;
pet->clearUnitState(UNIT_STAT_MOVING);
Spell* spell = new Spell(pet, spellInfo, false);
SpellCastResult result = spell->CheckPetCast(unit_target);
const SpellRangeEntry* sRange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex);
if (unit_target && !(pet->IsWithinDistInMap(unit_target, sRange->maxRange) && pet->IsWithinLOSInMap(unit_target))
&& !(GetPlayer()->IsFriendlyTo(unit_target) || pet->HasAuraType(SPELL_AURA_MOD_POSSESS)))
{
((Pet*)pet)->SetSpellOpener(spellid, sRange->minRange, sRange->maxRange);
spell->finish(false);
delete spell;
pet->AttackStop();
pet->GetMotionMaster()->Clear();
((Creature*)pet)->AI()->AttackStart(unit_target);
// 10% chance to play special warlock pet attack talk, else growl
if (((Creature*)pet)->IsPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && pet != unit_target && roll_chance_i(10))
pet->SendPetTalk((uint32)PET_TALK_ATTACK);
pet->SendPetAIReaction();
return;
}
// auto turn to target unless possessed
if (result == SPELL_FAILED_UNIT_NOT_INFRONT && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
{
if (unit_target)
{
pet->SetInFront(unit_target);
if (unit_target->GetTypeId() == TYPEID_PLAYER)
pet->SendCreateUpdateToPlayer((Player*)unit_target);
}
else if (Unit* unit_target2 = spell->m_targets.getUnitTarget())
{
pet->SetInFront(unit_target2);
if (unit_target2->GetTypeId() == TYPEID_PLAYER)
pet->SendCreateUpdateToPlayer((Player*)unit_target2);
}
if (Unit* powner = pet->GetCharmerOrOwner())
if (powner->GetTypeId() == TYPEID_PLAYER)
pet->SendCreateUpdateToPlayer((Player*)powner);
result = SPELL_CAST_OK;
}
if (result == SPELL_CAST_OK)
{
((Creature*)pet)->AddCreatureSpellCooldown(spellid);
unit_target = spell->m_targets.getUnitTarget();
if (unit_target && !GetPlayer()->IsFriendlyTo(unit_target) && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
{
// This is true if pet has no target or has target but targets differs.
if (pet->getVictim() != unit_target)
{
pet->AttackStop();
pet->GetMotionMaster()->Clear();
_player->SetInCombatState(true, unit_target);
if (((Creature*)pet)->AI())
{
((Creature*)pet)->AI()->AttackStart(unit_target);
// 10% chance to play special warlock pet attack talk, else growl
if (((Creature*)pet)->IsPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && pet != unit_target && roll_chance_i(10))
pet->SendPetTalk((uint32)PET_TALK_ATTACK);
pet->SendPetAIReaction();
}
else
pet->Attack(unit_target, true);
}
}
((Pet*)pet)->SetSpellOpener();
spell->SpellStart(&(spell->m_targets));
}
else
{
if (pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
Spell::SendCastResult(GetPlayer(), spellInfo, 0, result);
else
{
Unit* owner = pet->GetCharmerOrOwner();
if (owner && owner->GetTypeId() == TYPEID_PLAYER)
Spell::SendCastResult((Player*)owner, spellInfo, 0, result, true);
}
if (!((Creature*)pet)->HasSpellCooldown(spellid))
GetPlayer()->SendClearCooldown(spellid, pet);
((Pet*)pet)->SetSpellOpener();
spell->finish(false);
delete spell;
}
break;
}
default:
sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.", uint32(flag), spellid);
}
}
void WorldSession::HandlePetStopAttack(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Received opcode CMSG_PET_STOP_ATTACK");
ObjectGuid petGuid;
recv_data >> petGuid;
Unit* pet = _player->GetMap()->GetUnit(petGuid); // pet or controlled creature/player
if (!pet)
{
sLog.outError("%s doesn't exist.", petGuid.GetString().c_str());
return;
}
if (_player->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
{
sLog.outError("HandlePetStopAttack: %s isn't charm/pet of %s.", petGuid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}
if (!pet->isAlive())
return;
pet->AttackStop();
}
void WorldSession::HandlePetNameQueryOpcode(WorldPacket& recv_data)
{
DETAIL_LOG("HandlePetNameQuery. CMSG_PET_NAME_QUERY");
uint32 petnumber;
ObjectGuid petguid;
recv_data >> petnumber;
recv_data >> petguid;
SendPetNameQuery(petguid, petnumber);
}
void WorldSession::SendPetNameQuery(ObjectGuid petguid, uint32 petnumber)
{
Creature* pet = _player->GetMap()->GetAnyTypeCreature(petguid);
if (!pet || !pet->GetCharmInfo() || pet->GetCharmInfo()->GetPetNumber() != petnumber)
{
WorldPacket data(SMSG_PET_NAME_QUERY_RESPONSE, (4 + 1 + 4 + 1));
data << uint32(petnumber);
data << uint8(0);
data << uint32(0);
data << uint8(0);
_player->GetSession()->SendPacket(&data);
return;
}
char const* name = pet->GetName();
// creature pets have localization like other creatures
if (!pet->GetOwnerGuid().IsPlayer())
{
int loc_idx = GetSessionDbLocaleIndex();
sObjectMgr.GetCreatureLocaleStrings(pet->GetEntry(), loc_idx, &name);
}
WorldPacket data(SMSG_PET_NAME_QUERY_RESPONSE, (4 + 4 + strlen(name) + 1));
data << uint32(petnumber);
data << name;
data << uint32(pet->GetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP));
if (pet->IsPet() && ((Pet*)pet)->GetDeclinedNames())
{
data << uint8(1);
for (int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
data << ((Pet*)pet)->GetDeclinedNames()->name[i];
}
else
data << uint8(0);
_player->GetSession()->SendPacket(&data);
}
void WorldSession::HandlePetSetAction(WorldPacket& recv_data)
{
DETAIL_LOG("HandlePetSetAction. CMSG_PET_SET_ACTION");
ObjectGuid petGuid;
uint8 count;
recv_data >> petGuid;
Creature* pet = _player->GetMap()->GetAnyTypeCreature(petGuid);
if (!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
{
sLog.outError("HandlePetSetAction: Unknown pet or pet owner.");
return;
}
// pet can have action bar disabled
if (pet->IsPet() && ((Pet*)pet)->GetModeFlags() & PET_MODE_DISABLE_ACTIONS)
return;
CharmInfo* charmInfo = pet->GetCharmInfo();
if (!charmInfo)
{
sLog.outError("WorldSession::HandlePetSetAction: object (GUID: %u TypeId: %u) is considered pet-like but doesn't have a charminfo!", pet->GetGUIDLow(), pet->GetTypeId());
return;
}
count = (recv_data.size() == 24) ? 2 : 1;
uint32 position[2];
uint32 data[2];
bool move_command = false;
for (uint8 i = 0; i < count; ++i)
{
recv_data >> position[i];
recv_data >> data[i];
uint8 act_state = UNIT_ACTION_BUTTON_TYPE(data[i]);
// ignore invalid position
if (position[i] >= MAX_UNIT_ACTION_BAR_INDEX)
return;
// in the normal case, command and reaction buttons can only be moved, not removed
// at moving count ==2, at removing count == 1
// ignore attempt to remove command|reaction buttons (not possible at normal case)
if (act_state == ACT_COMMAND || act_state == ACT_REACTION)
{
if (count == 1)
return;
move_command = true;
}
}
// check swap (at command->spell swap client remove spell first in another packet, so check only command move correctness)
if (move_command)
{
uint8 act_state_0 = UNIT_ACTION_BUTTON_TYPE(data[0]);
if (act_state_0 == ACT_COMMAND || act_state_0 == ACT_REACTION)
{
uint32 spell_id_0 = UNIT_ACTION_BUTTON_ACTION(data[0]);
UnitActionBarEntry const* actionEntry_1 = charmInfo->GetActionBarEntry(position[1]);
if (!actionEntry_1 || spell_id_0 != actionEntry_1->GetAction() ||
act_state_0 != actionEntry_1->GetType())
return;
}
uint8 act_state_1 = UNIT_ACTION_BUTTON_TYPE(data[1]);
if (act_state_1 == ACT_COMMAND || act_state_1 == ACT_REACTION)
{
uint32 spell_id_1 = UNIT_ACTION_BUTTON_ACTION(data[1]);
UnitActionBarEntry const* actionEntry_0 = charmInfo->GetActionBarEntry(position[0]);
if (!actionEntry_0 || spell_id_1 != actionEntry_0->GetAction() ||
act_state_1 != actionEntry_0->GetType())
return;
}
}
for (uint8 i = 0; i < count; ++i)
{
uint32 spell_id = UNIT_ACTION_BUTTON_ACTION(data[i]);
uint8 act_state = UNIT_ACTION_BUTTON_TYPE(data[i]);
DETAIL_LOG("Player %s has changed pet spell action. Position: %u, Spell: %u, State: 0x%X", _player->GetName(), position[i], spell_id, uint32(act_state));
// if it's act for spell (en/disable/cast) and there is a spell given (0 = remove spell) which pet doesn't know, don't add
if (!((act_state == ACT_ENABLED || act_state == ACT_DISABLED || act_state == ACT_PASSIVE) && spell_id && !pet->HasSpell(spell_id)))
{
// sign for autocast
if (act_state == ACT_ENABLED && spell_id)
{
if (pet->isCharmed())
charmInfo->ToggleCreatureAutocast(spell_id, true);
else
((Pet*)pet)->ToggleAutocast(spell_id, true);
}
// sign for no/turn off autocast
else if (act_state == ACT_DISABLED && spell_id)
{
if (pet->isCharmed())
charmInfo->ToggleCreatureAutocast(spell_id, false);
else
((Pet*)pet)->ToggleAutocast(spell_id, false);
}
charmInfo->SetActionBar(position[i], spell_id, ActiveStates(act_state));
}
}
}
void WorldSession::HandlePetRename(WorldPacket& recv_data)
{
DETAIL_LOG("HandlePetRename. CMSG_PET_RENAME");
ObjectGuid petGuid;
uint8 isdeclined;
std::string name;
DeclinedName declinedname;
recv_data >> petGuid;
recv_data >> name;
recv_data >> isdeclined;
Pet* pet = _player->GetMap()->GetPet(petGuid);
// check it!
if (!pet || pet->getPetType() != HUNTER_PET ||
!pet->HasByteFlag(UNIT_FIELD_BYTES_2, 2, UNIT_CAN_BE_RENAMED) ||
pet->GetOwnerGuid() != _player->GetObjectGuid() || !pet->GetCharmInfo())
return;
PetNameInvalidReason res = ObjectMgr::CheckPetName(name);
if (res != PET_NAME_SUCCESS)
{
SendPetNameInvalid(res, name, nullptr);
return;
}
if (sObjectMgr.IsReservedName(name))
{
SendPetNameInvalid(PET_NAME_RESERVED, name, nullptr);
return;
}
pet->SetName(name);
if (_player->GetGroup())
_player->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
pet->RemoveByteFlag(UNIT_FIELD_BYTES_2, 2, UNIT_CAN_BE_RENAMED);
if (isdeclined)
{
for (int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
{
recv_data >> declinedname.name[i];
}
std::wstring wname;
Utf8toWStr(name, wname);
if (!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname, 0), declinedname))
{
SendPetNameInvalid(PET_NAME_DECLENSION_DOESNT_MATCH_BASE_NAME, name, &declinedname);
return;
}
}
CharacterDatabase.BeginTransaction();
if (isdeclined)
{
for (int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
CharacterDatabase.escape_string(declinedname.name[i]);
CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
CharacterDatabase.PExecute("INSERT INTO character_pet_declinedname (id, owner, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%u','%s','%s','%s','%s','%s')",
pet->GetCharmInfo()->GetPetNumber(), _player->GetGUIDLow(), declinedname.name[0].c_str(), declinedname.name[1].c_str(), declinedname.name[2].c_str(), declinedname.name[3].c_str(), declinedname.name[4].c_str());
}
CharacterDatabase.escape_string(name);
CharacterDatabase.PExecute("UPDATE character_pet SET name = '%s', renamed = '1' WHERE owner = '%u' AND id = '%u'", name.c_str(), _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
CharacterDatabase.CommitTransaction();
pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, uint32(time(nullptr)));
}
void WorldSession::HandlePetAbandon(WorldPacket& recv_data)
{
ObjectGuid guid;
recv_data >> guid; // pet guid
DETAIL_LOG("HandlePetAbandon. CMSG_PET_ABANDON pet guid is %s", guid.GetString().c_str());
if (!_player->IsInWorld())
return;
// pet/charmed
if (Creature* pet = _player->GetMap()->GetAnyTypeCreature(guid))
{
if (pet->IsPet())
{
if (pet->GetObjectGuid() == _player->GetPetGuid())
pet->ModifyPower(POWER_HAPPINESS, -50000);
((Pet*)pet)->Unsummon(PET_SAVE_AS_DELETED, _player);
}
else if (pet->GetObjectGuid() == _player->GetCharmGuid())
{
_player->Uncharm();
}
}
}
void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)
{
DETAIL_LOG("CMSG_PET_UNLEARN");
ObjectGuid guid;
recvPacket >> guid; // Pet guid
Pet* pet = _player->GetPet();
if (!pet || guid != pet->GetObjectGuid())
{
sLog.outError("HandlePetUnlearnOpcode. %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}
if (pet->getPetType() != HUNTER_PET || pet->m_usedTalentCount == 0)
return;
CharmInfo* charmInfo = pet->GetCharmInfo();
if (!charmInfo)
{
sLog.outError("WorldSession::HandlePetUnlearnOpcode: %s is considered pet-like but doesn't have a charminfo!", pet->GetGuidStr().c_str());
return;
}
pet->resetTalents();
_player->SendTalentsInfoData(true);
}
void WorldSession::HandlePetSpellAutocastOpcode(WorldPacket& recvPacket)
{
DETAIL_LOG("CMSG_PET_SPELL_AUTOCAST");
ObjectGuid guid;
uint32 spellid;
uint8 state; // 1 for on, 0 for off
recvPacket >> guid >> spellid >> state;
Creature* pet = _player->GetMap()->GetAnyTypeCreature(guid);
if (!pet || (guid != _player->GetPetGuid() && guid != _player->GetCharmGuid()))
{
sLog.outError("HandlePetSpellAutocastOpcode. %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}
// do not add not learned spells/ passive spells
if (!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
return;
CharmInfo* charmInfo = pet->GetCharmInfo();
if (!charmInfo)
{
sLog.outError("WorldSession::HandlePetSpellAutocastOpcod: %s is considered pet-like but doesn't have a charminfo!", guid.GetString().c_str());
return;
}
if (pet->isCharmed())
// state can be used as boolean
pet->GetCharmInfo()->ToggleCreatureAutocast(spellid, !!state);
else
((Pet*)pet)->ToggleAutocast(spellid, !!state);
charmInfo->SetSpellAutocast(spellid, !!state);
}
void WorldSession::HandlePetCastSpellOpcode(WorldPacket& recvPacket)
{
DETAIL_LOG("WORLD: CMSG_PET_CAST_SPELL");
ObjectGuid guid;
uint32 spellid;
uint8 cast_count;
uint8 unk_flags; // flags (if 0x02 - some additional data are received)
recvPacket >> guid >> cast_count >> spellid >> unk_flags;
DEBUG_LOG("WORLD: CMSG_PET_CAST_SPELL, %s, cast_count: %u, spellid %u, unk_flags %u", guid.GetString().c_str(), cast_count, spellid, unk_flags);
Creature* pet = _player->GetMap()->GetAnyTypeCreature(guid);
if (!pet || (guid != _player->GetPetGuid() && guid != _player->GetCharmGuid()))
{
sLog.outError("HandlePetCastSpellOpcode: %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}
SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellid);
if (!spellInfo)
{
sLog.outError("WORLD: unknown PET spell id %i", spellid);
return;
}
if (pet->GetCharmInfo() && pet->GetCharmInfo()->GetGlobalCooldownMgr().HasGlobalCooldown(spellInfo))
return;
Aura* triggeredByAura = pet->GetTriggeredByClientAura(spellid);
// do not cast not learned spells
if ((!triggeredByAura && !pet->HasSpell(spellid)) || IsPassiveSpell(spellInfo))
return;
SpellCastTargets targets;
recvPacket >> targets.ReadForCaster(pet);
pet->clearUnitState(UNIT_STAT_MOVING);
Spell* spell = new Spell(pet, spellInfo, triggeredByAura ? true : false, pet->GetObjectGuid(), triggeredByAura ? triggeredByAura->GetSpellProto() : nullptr);
spell->m_cast_count = cast_count; // probably pending spell cast
spell->m_targets = targets;
SpellCastResult result = triggeredByAura ? SPELL_CAST_OK : spell->CheckPetCast(nullptr);
if (result == SPELL_CAST_OK)
{
pet->AddCreatureSpellCooldown(spellid);
spell->SpellStart(&(spell->m_targets), triggeredByAura);
}
else
{
Unit* owner = pet->GetCharmerOrOwner();
if (owner && owner->GetTypeId() == TYPEID_PLAYER && !triggeredByAura)
Spell::SendCastResult((Player*)owner, spellInfo, 0, result, true);
if (!pet->HasSpellCooldown(spellid) && !triggeredByAura)
GetPlayer()->SendClearCooldown(spellid, pet);
spell->finish(false);
delete spell;
}
}
void WorldSession::SendPetNameInvalid(uint32 error, const std::string& name, DeclinedName* declinedName)
{
WorldPacket data(SMSG_PET_NAME_INVALID, 4 + name.size() + 1 + 1);
data << uint32(error);
data << name;
if (declinedName)
{
data << uint8(1);
for (uint32 i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
data << declinedName->name[i];
}
else
data << uint8(0);
SendPacket(&data);
}
void WorldSession::HandlePetLearnTalent(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: CMSG_PET_LEARN_TALENT");
ObjectGuid guid;
uint32 talent_id, requested_rank;
recv_data >> guid >> talent_id >> requested_rank;
_player->LearnPetTalent(guid, talent_id, requested_rank);
_player->SendTalentsInfoData(true);
}
void WorldSession::HandleLearnPreviewTalentsPet(WorldPacket& recv_data)
{
DEBUG_LOG("CMSG_LEARN_PREVIEW_TALENTS_PET");
ObjectGuid guid;
recv_data >> guid;
uint32 talentsCount;
recv_data >> talentsCount;
uint32 talentId, talentRank;
for (uint32 i = 0; i < talentsCount; ++i)
{
recv_data >> talentId >> talentRank;
_player->LearnPetTalent(guid, talentId, talentRank);
}
_player->SendTalentsInfoData(true);
}