-
Notifications
You must be signed in to change notification settings - Fork 293
/
NPCHandler.cpp
992 lines (806 loc) · 31.1 KB
/
NPCHandler.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
/*
* 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 "Language.h"
#include "Database/DatabaseEnv.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "Opcodes.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "SpellMgr.h"
#include "Player.h"
#include "GossipDef.h"
#include "ScriptMgr.h"
#include "Creature.h"
#include "Pet.h"
#include "Guild.h"
#include "GuildMgr.h"
#include "Chat.h"
enum StableResultCode
{
STABLE_ERR_MONEY = 0x01, // "you don't have enough money"
STABLE_ERR_STABLE = 0x06, // currently used in most fail cases
STABLE_SUCCESS_STABLE = 0x08, // stable success
STABLE_SUCCESS_UNSTABLE = 0x09, // unstable/swap success
STABLE_SUCCESS_BUY_SLOT = 0x0A, // buy slot success
STABLE_ERR_EXOTIC = 0x0C, // "you are unable to control exotic creatures"
};
void WorldSession::HandleTabardVendorActivateOpcode(WorldPacket& recv_data)
{
ObjectGuid guid;
recv_data >> guid;
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_TABARDDESIGNER);
if (!unit)
{
DEBUG_LOG("WORLD: HandleTabardVendorActivateOpcode - %s not found or you can't interact with him.", guid.GetString().c_str());
return;
}
SendTabardVendorActivate(guid);
}
void WorldSession::SendTabardVendorActivate(ObjectGuid guid) const
{
WorldPacket data(MSG_TABARDVENDOR_ACTIVATE, 8);
data << ObjectGuid(guid);
SendPacket(data);
}
void WorldSession::HandleBankerActivateOpcode(WorldPacket& recv_data)
{
ObjectGuid guid;
DEBUG_LOG("WORLD: Received opcode CMSG_BANKER_ACTIVATE");
recv_data >> guid;
if (!CheckBanker(guid))
return;
SendShowBank(guid);
}
void WorldSession::SendShowBank(ObjectGuid guid) const
{
WorldPacket data(SMSG_SHOW_BANK, 8);
data << ObjectGuid(guid);
SendPacket(data);
}
void WorldSession::SendShowMailBox(ObjectGuid guid) const
{
WorldPacket data(SMSG_SHOW_MAILBOX, 8);
data << ObjectGuid(guid);
SendPacket(data);
}
void WorldSession::HandleTrainerListOpcode(WorldPacket& recv_data)
{
ObjectGuid guid;
recv_data >> guid;
SendTrainerList(guid);
}
void WorldSession::SendTrainerList(ObjectGuid guid) const
{
std::string str = GetMangosString(LANG_NPC_TAINER_HELLO);
SendTrainerList(guid, str);
}
static void SendTrainerSpellHelper(WorldPacket& data, TrainerSpell const* tSpell, TrainerSpellState state, float fDiscountMod, bool can_learn_primary_prof, uint32 reqLevel)
{
bool primary_prof_first_rank = sSpellMgr.IsPrimaryProfessionFirstRankSpell(tSpell->learnedSpell);
SpellChainNode const* chain_node = sSpellMgr.GetSpellChainNode(tSpell->learnedSpell);
data << uint32(tSpell->spell); // learned spell (or cast-spell in profession case)
data << uint8(state == TRAINER_SPELL_GREEN_DISABLED ? TRAINER_SPELL_GREEN : state);
data << uint32(floor(tSpell->spellCost * fDiscountMod));
data << uint32(primary_prof_first_rank && can_learn_primary_prof ? 1 : 0);
// primary prof. learn confirmation dialog
data << uint32(primary_prof_first_rank ? 1 : 0); // must be equal prev. field to have learn button in enabled state
data << uint8(reqLevel);
data << uint32(tSpell->reqSkill);
data << uint32(tSpell->reqSkillValue);
data << uint32(!tSpell->IsCastable() && chain_node ? (chain_node->prev ? chain_node->prev : chain_node->req) : 0);
data << uint32(!tSpell->IsCastable() && chain_node && chain_node->prev ? chain_node->req : 0);
data << uint32(0);
}
void WorldSession::SendTrainerList(ObjectGuid guid, const std::string& strTitle) const
{
DEBUG_LOG("WORLD: SendTrainerList");
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_TRAINER);
if (!unit)
{
DEBUG_LOG("WORLD: SendTrainerList - %s not found or you can't interact with him.", guid.GetString().c_str());
return;
}
// trainer list loaded at check;
if (!unit->IsTrainerOf(_player, true))
return;
CreatureInfo const* ci = unit->GetCreatureInfo();
if (!ci)
return;
TrainerSpellData const* cSpells = unit->GetTrainerSpells();
TrainerSpellData const* tSpells = unit->GetTrainerTemplateSpells();
if (!cSpells && !tSpells)
{
DEBUG_LOG("WORLD: SendTrainerList - Training spells not found for %s", guid.GetString().c_str());
return;
}
uint32 maxcount = (cSpells ? cSpells->spellList.size() : 0) + (tSpells ? tSpells->spellList.size() : 0);
uint32 trainer_type = cSpells && cSpells->trainerType ? cSpells->trainerType : (tSpells ? tSpells->trainerType : 0);
WorldPacket data(SMSG_TRAINER_LIST, 8 + 4 + 4 + maxcount * 38 + strTitle.size() + 1);
data << ObjectGuid(guid);
data << uint32(trainer_type);
size_t count_pos = data.wpos();
data << uint32(maxcount);
// reputation discount
float fDiscountMod = _player->GetReputationPriceDiscount(unit);
bool can_learn_primary_prof = GetPlayer()->GetFreePrimaryProfessionPoints() > 0;
uint32 count = 0;
if (cSpells)
{
for (TrainerSpellMap::const_iterator itr = cSpells->spellList.begin(); itr != cSpells->spellList.end(); ++itr)
{
TrainerSpell const* tSpell = &itr->second;
uint32 reqLevel = 0;
if (!_player->IsSpellFitByClassAndRace(tSpell->learnedSpell, &reqLevel))
continue;
if (tSpell->conditionId && !sObjectMgr.IsPlayerMeetToCondition(tSpell->conditionId, GetPlayer(), unit->GetMap(), unit, CONDITION_FROM_TRAINER))
continue;
reqLevel = tSpell->isProvidedReqLevel ? tSpell->reqLevel : std::max(reqLevel, tSpell->reqLevel);
TrainerSpellState state = _player->GetTrainerSpellState(tSpell, reqLevel);
SendTrainerSpellHelper(data, tSpell, state, fDiscountMod, can_learn_primary_prof, reqLevel);
++count;
}
}
if (tSpells)
{
for (TrainerSpellMap::const_iterator itr = tSpells->spellList.begin(); itr != tSpells->spellList.end(); ++itr)
{
TrainerSpell const* tSpell = &itr->second;
uint32 reqLevel = 0;
if (!_player->IsSpellFitByClassAndRace(tSpell->learnedSpell, &reqLevel))
continue;
if (tSpell->conditionId && !sObjectMgr.IsPlayerMeetToCondition(tSpell->conditionId, GetPlayer(), unit->GetMap(), unit, CONDITION_FROM_TRAINER))
continue;
reqLevel = tSpell->isProvidedReqLevel ? tSpell->reqLevel : std::max(reqLevel, tSpell->reqLevel);
TrainerSpellState state = _player->GetTrainerSpellState(tSpell, reqLevel);
SendTrainerSpellHelper(data, tSpell, state, fDiscountMod, can_learn_primary_prof, reqLevel);
++count;
}
}
data << strTitle;
data.put<uint32>(count_pos, count);
SendPacket(data);
}
void WorldSession::HandleTrainerBuySpellOpcode(WorldPacket& recv_data)
{
ObjectGuid guid;
uint32 spellId = 0;
recv_data >> guid >> spellId;
DEBUG_LOG("WORLD: Received opcode CMSG_TRAINER_BUY_SPELL Trainer: %s, learn spell id is: %u", guid.GetString().c_str(), spellId);
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_TRAINER);
if (!unit)
{
DEBUG_LOG("WORLD: HandleTrainerBuySpellOpcode - %s not found or you can't interact with him.", guid.GetString().c_str());
return;
}
if (!unit->IsTrainerOf(_player, true))
return;
// check present spell in trainer spell list
TrainerSpellData const* cSpells = unit->GetTrainerSpells();
TrainerSpellData const* tSpells = unit->GetTrainerTemplateSpells();
if (!cSpells && !tSpells)
return;
// Try find spell in npc_trainer
TrainerSpell const* trainer_spell = cSpells ? cSpells->Find(spellId) : nullptr;
// Not found, try find in npc_trainer_template
if (!trainer_spell && tSpells)
trainer_spell = tSpells->Find(spellId);
// Not found anywhere, cheating?
if (!trainer_spell)
return;
// can't be learn, cheat? Or double learn with lags...
uint32 reqLevel = 0;
if (!_player->IsSpellFitByClassAndRace(trainer_spell->learnedSpell, &reqLevel))
return;
reqLevel = trainer_spell->isProvidedReqLevel ? trainer_spell->reqLevel : std::max(reqLevel, trainer_spell->reqLevel);
if (_player->GetTrainerSpellState(trainer_spell, reqLevel) != TRAINER_SPELL_GREEN)
return;
// apply reputation discount
uint32 nSpellCost = uint32(floor(trainer_spell->spellCost * _player->GetReputationPriceDiscount(unit)));
// check money requirement
if (_player->GetMoney() < nSpellCost)
return;
_player->ModifyMoney(-int32(nSpellCost));
SendPlaySpellVisual(guid, 0xB3); // visual effect on trainer
WorldPacket data(SMSG_PLAY_SPELL_IMPACT, 8 + 4); // visual effect on player
data << _player->GetObjectGuid();
data << uint32(0x016A); // index from SpellVisualKit.dbc
SendPacket(data);
// learn explicitly or cast explicitly
// TODO - Are these spells really cast correctly this way?
if (trainer_spell->IsCastable())
_player->CastSpell(_player, trainer_spell->spell, TRIGGERED_OLD_TRIGGERED);
else
_player->learnSpell(spellId, false);
data.Initialize(SMSG_TRAINER_BUY_SUCCEEDED, 12);
data << ObjectGuid(guid);
data << uint32(spellId); // should be same as in packet from client
SendPacket(data);
}
void WorldSession::HandleGossipHelloOpcode(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Received opcode CMSG_GOSSIP_HELLO");
ObjectGuid guid;
recv_data >> guid;
Creature* pCreature = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_NONE);
if (!pCreature)
{
DEBUG_LOG("WORLD: HandleGossipHelloOpcode - %s not found or you can't interact with him.", guid.GetString().c_str());
return;
}
pCreature->StopMoving();
if (pCreature->isSpiritGuide())
pCreature->SendAreaSpiritHealerQueryOpcode(_player);
if (!sScriptMgr.OnGossipHello(_player, pCreature))
{
_player->PrepareGossipMenu(pCreature, pCreature->GetCreatureInfo()->GossipMenuId);
_player->SendPreparedGossip(pCreature);
}
}
void WorldSession::HandleGossipSelectOptionOpcode(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: CMSG_GOSSIP_SELECT_OPTION");
uint32 gossipListId;
uint32 menuId;
ObjectGuid guid;
std::string code;
recv_data >> guid >> menuId >> gossipListId;
if (_player->PlayerTalkClass->GossipOptionCoded(gossipListId))
{
recv_data >> code;
DEBUG_LOG("Gossip code: %s", code.c_str());
}
uint32 sender = _player->PlayerTalkClass->GossipOptionSender(gossipListId);
uint32 action = _player->PlayerTalkClass->GossipOptionAction(gossipListId);
if (guid.IsAnyTypeCreature())
{
Creature* pCreature = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_NONE);
if (!pCreature)
{
DEBUG_LOG("WORLD: HandleGossipSelectOptionOpcode - %s not found or you can't interact with it.", guid.GetString().c_str());
return;
}
if (!sScriptMgr.OnGossipSelect(_player, pCreature, sender, action, code.empty() ? nullptr : code.c_str()))
_player->OnGossipSelect(pCreature, gossipListId, menuId);
}
else if (guid.IsGameObject())
{
GameObject* pGo = GetPlayer()->GetGameObjectIfCanInteractWith(guid);
if (!pGo)
{
DEBUG_LOG("WORLD: HandleGossipSelectOptionOpcode - %s not found or you can't interact with it.", guid.GetString().c_str());
return;
}
if (!sScriptMgr.OnGossipSelect(_player, pGo, sender, action, code.empty() ? nullptr : code.c_str()))
_player->OnGossipSelect(pGo, gossipListId, menuId);
}
}
void WorldSession::HandleSpiritHealerActivateOpcode(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: CMSG_SPIRIT_HEALER_ACTIVATE");
ObjectGuid guid;
recv_data >> guid;
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_SPIRITHEALER);
if (!unit)
{
DEBUG_LOG("WORLD: HandleSpiritHealerActivateOpcode - %s not found or you can't interact with him.", guid.GetString().c_str());
return;
}
SendSpiritResurrect();
}
void WorldSession::SendSpiritResurrect() const
{
_player->ResurrectPlayer(0.5f, true);
_player->DurabilityLossAll(0.25f, true);
// get corpse nearest graveyard
WorldSafeLocsEntry const* corpseGrave = nullptr;
Corpse* corpse = _player->GetCorpse();
if (corpse)
corpseGrave = sObjectMgr.GetClosestGraveYard(
corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetMapId(), _player->GetTeam());
// now can spawn bones
_player->SpawnCorpseBones();
// teleport to nearest from corpse graveyard, if different from nearest to player ghost
if (corpseGrave)
{
WorldSafeLocsEntry const* ghostGrave = sObjectMgr.GetClosestGraveYard(
_player->GetPositionX(), _player->GetPositionY(), _player->GetPositionZ(), _player->GetMapId(), _player->GetTeam());
if (corpseGrave != ghostGrave)
_player->TeleportTo(corpseGrave->map_id, corpseGrave->x, corpseGrave->y, corpseGrave->z, _player->GetOrientation());
// or update at original position
else
{
_player->GetCamera().UpdateVisibilityForOwner();
_player->UpdateObjectVisibility();
}
}
// or update at original position
else
{
_player->GetCamera().UpdateVisibilityForOwner();
_player->UpdateObjectVisibility();
}
}
void WorldSession::HandleBinderActivateOpcode(WorldPacket& recv_data)
{
ObjectGuid npcGuid;
recv_data >> npcGuid;
if (!GetPlayer()->IsInWorld() || !GetPlayer()->isAlive())
return;
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(npcGuid, UNIT_NPC_FLAG_INNKEEPER);
if (!unit)
{
DEBUG_LOG("WORLD: HandleBinderActivateOpcode - %s not found or you can't interact with him.", npcGuid.GetString().c_str());
return;
}
SendBindPoint(unit);
}
void WorldSession::SendBindPoint(Creature* npc) const
{
// prevent set homebind to instances in any case
if (GetPlayer()->GetMap()->Instanceable())
return;
// send spell for bind 3286 bind magic
npc->CastSpell(_player, 3286, TRIGGERED_OLD_TRIGGERED); // Bind
WorldPacket data(SMSG_TRAINER_BUY_SUCCEEDED, (8 + 4));
data << npc->GetObjectGuid();
data << uint32(3286); // Bind
SendPacket(data);
_player->PlayerTalkClass->CloseGossip();
}
void WorldSession::HandleListStabledPetsOpcode(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Recv MSG_LIST_STABLED_PETS");
ObjectGuid npcGUID;
recv_data >> npcGUID;
if (!CheckStableMaster(npcGUID))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
SendStablePet(npcGUID);
}
void WorldSession::SendStablePet(ObjectGuid guid) const
{
DEBUG_LOG("WORLD: Recv MSG_LIST_STABLED_PETS Send.");
WorldPacket data(MSG_LIST_STABLED_PETS, 200); // guess size
data << guid;
Pet* pet = _player->GetPet();
size_t wpos = data.wpos();
data << uint8(0); // place holder for slot show number
data << uint8(GetPlayer()->m_stableSlots);
uint8 num = 0; // counter for place holder
PetSaveMode firstSlot = PET_SAVE_FIRST_STABLE_SLOT; // have to be changed to PET_SAVE_AS_CURRENT if pet is not currently summoned
// not let move dead pet in slot
if (pet && pet->isAlive() && pet->getPetType() == HUNTER_PET)
{
data << uint32(pet->GetCharmInfo()->GetPetNumber());
data << uint32(pet->GetEntry());
data << uint32(pet->getLevel());
data << pet->GetName(); // petname
data << uint8(1); // 1 = current, 2/3 = in stable (any from 4,5,... create problems with proper show)
++num;
}
else
firstSlot = PET_SAVE_AS_CURRENT; // have to send also unsummoned pet
// 0 1 2 3 4 5
QueryResult* result = CharacterDatabase.PQuery("SELECT owner, slot, id, entry, level, name FROM character_pet WHERE owner = '%u' AND slot >= '%u' AND slot <= '%u' ORDER BY slot",
_player->GetGUIDLow(), uint32(firstSlot), uint32(PET_SAVE_LAST_STABLE_SLOT));
if (result)
{
do
{
Field* fields = result->Fetch();
data << uint32(fields[2].GetUInt32()); // petnumber
data << uint32(fields[3].GetUInt32()); // creature entry
data << uint32(fields[4].GetUInt32()); // level
data << fields[5].GetString(); // name
data << uint8(fields[1].GetUInt32() + 1); // slot
++num;
}
while (result->NextRow());
delete result;
}
data.put<uint8>(wpos, num); // set real data to placeholder
SendPacket(data);
}
void WorldSession::SendStableResult(uint8 res) const
{
WorldPacket data(SMSG_STABLE_RESULT, 1);
data << uint8(res);
SendPacket(data);
}
bool WorldSession::CheckStableMaster(ObjectGuid guid) const
{
// spell case or GM
if (guid == GetPlayer()->GetObjectGuid())
{
// command case will return only if player have real access to command
if (!GetPlayer()->HasAuraType(SPELL_AURA_OPEN_STABLE) && !ChatHandler(GetPlayer()).FindCommand("stable"))
{
DEBUG_LOG("%s attempt open stable in cheating way.", guid.GetString().c_str());
return false;
}
}
// stable master case
else
{
if (!GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_STABLEMASTER))
{
DEBUG_LOG("Stablemaster %s not found or you can't interact with him.", guid.GetString().c_str());
return false;
}
}
return true;
}
void WorldSession::HandleStablePet(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Recv CMSG_STABLE_PET");
ObjectGuid npcGUID;
recv_data >> npcGUID;
if (!GetPlayer()->isAlive())
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
if (!CheckStableMaster(npcGUID))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
Pet* pet = _player->GetPet();
// can't place in stable dead pet
if (pet)
{
bool stop = false;
if (!pet->isAlive())
{
_player->SendPetTameFailure(PETTAME_DEAD);
stop = true;
}
if (!stop && pet->getPetType() != HUNTER_PET)
{
_player->SendPetTameFailure(PETTAME_INVALIDCREATURE);
stop = true;
}
if (stop)
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
else
{
SpellCastResult loadResult = Pet::TryLoadFromDB(_player, 0, 0, true, HUNTER_PET);
if (loadResult != SPELL_CAST_OK)
{
if (loadResult == SPELL_FAILED_TARGETS_DEAD)
_player->SendPetTameFailure(PETTAME_DEAD);
if (loadResult == SPELL_FAILED_BAD_TARGETS)
_player->SendPetTameFailure(PETTAME_INVALIDCREATURE);
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
uint32 free_slot = 1;
QueryResult* result = CharacterDatabase.PQuery("SELECT owner,slot,id FROM character_pet WHERE owner = '%u' AND slot >= '%u' AND slot <= '%u' ORDER BY slot ",
_player->GetGUIDLow(), PET_SAVE_FIRST_STABLE_SLOT, PET_SAVE_LAST_STABLE_SLOT);
if (result)
{
do
{
Field* fields = result->Fetch();
uint32 slot = fields[1].GetUInt32();
// slots ordered in query, and if not equal then free
if (slot != free_slot)
break;
// this slot not free, skip
++free_slot;
}
while (result->NextRow());
delete result;
}
if (free_slot > 0 && free_slot <= GetPlayer()->m_stableSlots)
{
if (pet)
pet->Unsummon(PetSaveMode(free_slot), _player);
else
{
// change pet slot directly in database
CharacterDatabase.BeginTransaction();
static SqlStatementID ChangePetSlot_ID;
SqlStatement ChangePetSlot = CharacterDatabase.CreateStatement(ChangePetSlot_ID, "UPDATE character_pet SET slot = ? WHERE owner = ? AND slot = ? ");
ChangePetSlot.PExecute(free_slot, _player->GetObjectGuid().GetCounter(), uint32(PET_SAVE_AS_CURRENT));
CharacterDatabase.CommitTransaction();
}
SendStableResult(STABLE_SUCCESS_STABLE);
_player->SetTemporaryUnsummonedPetNumber(0);
}
else
SendStableResult(STABLE_ERR_STABLE);
}
void WorldSession::HandleUnstablePet(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Recv CMSG_UNSTABLE_PET.");
ObjectGuid npcGUID;
uint32 petnumber;
recv_data >> npcGUID >> petnumber;
if (!CheckStableMaster(npcGUID))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
uint32 creature_id = 0;
uint32 slot = 0;
QueryResult* result = CharacterDatabase.PQuery("SELECT entry, slot FROM character_pet WHERE owner = '%u' AND id = '%u' AND slot >='%u' AND slot <= '%u'",
_player->GetGUIDLow(), petnumber, PET_SAVE_FIRST_STABLE_SLOT, PET_SAVE_LAST_STABLE_SLOT);
if (result)
{
Field* fields = result->Fetch();
creature_id = fields[0].GetUInt32();
slot = fields[1].GetUInt32();
delete result;
}
if (!creature_id)
{
sLog.outError("HandleUnstablePet> %s trying to unstable a pet with invalid creature id!", _player->GetGuidStr().c_str());
SendStableResult(STABLE_ERR_STABLE);
return;
}
if (slot == uint32(PET_SAVE_AS_CURRENT))
{
sLog.outError("HandleUnstablePet> %s trying to unstable current pet!", _player->GetGuidStr().c_str());
SendStableResult(STABLE_ERR_STABLE);
return;
}
CreatureInfo const* creatureInfo = ObjectMgr::GetCreatureTemplate(creature_id);
if (!creatureInfo || !creatureInfo->isTameable(_player->CanTameExoticPets()))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
// found if a pet is actually summoned
Pet* pet = _player->GetPet();
if (pet)
{
bool stop = false;
if (!pet->isAlive())
{
_player->SendPetTameFailure(PETTAME_DEAD);
stop = true;
}
if (!stop && pet->getPetType() != HUNTER_PET)
{
_player->SendPetTameFailure(PETTAME_ANOTHERSUMMONACTIVE);
stop = true;
}
if (stop)
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
pet->Unsummon(PetSaveMode(slot), _player);
}
else
{
// try to find if pet is actually temporary unsummoned
SpellCastResult loadResult = Pet::TryLoadFromDB(_player, 0, 0, true, HUNTER_PET);
if (loadResult != SPELL_CAST_OK)
{
if (loadResult == SPELL_FAILED_TARGETS_DEAD)
{
_player->SendPetTameFailure(PETTAME_DEAD);
sLog.outError("UnStablePet> trying to unstable a dead pet. Should not be possible!");
return;
}
if (loadResult == SPELL_FAILED_BAD_TARGETS)
{
_player->SendPetTameFailure(PETTAME_ANOTHERSUMMONACTIVE);
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
else
{
// change pet slot directly in database
CharacterDatabase.BeginTransaction();
static SqlStatementID ChangePetSlot_ID;
SqlStatement ChangePetSlot = CharacterDatabase.CreateStatement(ChangePetSlot_ID, "UPDATE character_pet SET slot = ? WHERE owner = ? AND slot = ? ");
ChangePetSlot.PExecute(slot, _player->GetObjectGuid().GetCounter(), uint32(PET_SAVE_AS_CURRENT));
CharacterDatabase.CommitTransaction();
}
}
Pet* newpet = new Pet(HUNTER_PET);
if (!newpet->LoadPetFromDB(_player, creature_id, petnumber))
{
delete newpet;
newpet = nullptr;
// load failed but it may be normal if the pet is set as temporary unsummoned by any reason
if (!_player->GetTemporaryUnsummonedPetNumber())
{
// load really failed
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
SendStableResult(STABLE_SUCCESS_UNSTABLE);
}
void WorldSession::HandleBuyStableSlot(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Recv CMSG_BUY_STABLE_SLOT.");
ObjectGuid npcGUID;
recv_data >> npcGUID;
if (!CheckStableMaster(npcGUID))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
if (GetPlayer()->m_stableSlots < MAX_PET_STABLES)
{
StableSlotPricesEntry const* SlotPrice = sStableSlotPricesStore.LookupEntry(GetPlayer()->m_stableSlots + 1);
if (_player->GetMoney() >= SlotPrice->Price)
{
++GetPlayer()->m_stableSlots;
_player->ModifyMoney(-int32(SlotPrice->Price));
SendStableResult(STABLE_SUCCESS_BUY_SLOT);
}
else
SendStableResult(STABLE_ERR_MONEY);
}
else
SendStableResult(STABLE_ERR_STABLE);
}
void WorldSession::HandleStableRevivePet(WorldPacket& /* recv_data */)
{
DEBUG_LOG("HandleStableRevivePet: Not implemented");
}
void WorldSession::HandleStableSwapPet(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: Recv CMSG_STABLE_SWAP_PET.");
ObjectGuid npcGUID;
uint32 pet_number;
recv_data >> npcGUID >> pet_number;
if (!CheckStableMaster(npcGUID))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
Pet* pet = _player->GetPet();
if (pet)
{
bool stop = false;
if (!pet->isAlive())
{
_player->SendPetTameFailure(PETTAME_DEAD);
stop = true;
}
if (!stop && pet->getPetType() != HUNTER_PET)
{
_player->SendPetTameFailure(PETTAME_INVALIDCREATURE);
stop = true;
}
if (stop)
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
else
{
SpellCastResult loadResult = Pet::TryLoadFromDB(_player, 0, 0, true, HUNTER_PET);
if (loadResult != SPELL_CAST_OK)
{
if (loadResult == SPELL_FAILED_TARGETS_DEAD)
_player->SendPetTameFailure(PETTAME_DEAD);
if (loadResult == SPELL_FAILED_BAD_TARGETS)
_player->SendPetTameFailure(PETTAME_INVALIDCREATURE);
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
// find swapped pet slot in stable
QueryResult* result = CharacterDatabase.PQuery("SELECT slot,entry FROM character_pet WHERE owner = '%u' AND id = '%u'",
_player->GetGUIDLow(), pet_number);
if (!result)
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
Field* fields = result->Fetch();
uint32 slot = fields[0].GetUInt32();
uint32 creature_id = fields[1].GetUInt32();
delete result;
if (!creature_id)
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
CreatureInfo const* creatureInfo = ObjectMgr::GetCreatureTemplate(creature_id);
if (!creatureInfo || !creatureInfo->isTameable(_player->CanTameExoticPets()))
{
SendStableResult(STABLE_ERR_STABLE);
return;
}
// move alive pet to slot or delete dead pet
if (pet)
pet->Unsummon(PetSaveMode(slot), _player);
else
{
// change pet slot directly in memory
CharacterDatabase.BeginTransaction();
static SqlStatementID ChangePetSlot_ID;
SqlStatement ChangePetSlot = CharacterDatabase.CreateStatement(ChangePetSlot_ID, "UPDATE character_pet SET slot = ? WHERE owner = ? AND slot = ? ");
ChangePetSlot.PExecute(slot, _player->GetObjectGuid().GetCounter(), uint32(PET_SAVE_AS_CURRENT));
CharacterDatabase.CommitTransaction();
}
// summon unstabled pet
Pet* newpet = new Pet;
if (!newpet->LoadPetFromDB(_player, creature_id, pet_number))
{
delete newpet;
// load failed but it may be normal if the pet is set as temporary unsummoned by any reason
if (!_player->GetTemporaryUnsummonedPetNumber())
{
// load really failed
SendStableResult(STABLE_ERR_STABLE);
return;
}
}
SendStableResult(STABLE_SUCCESS_UNSTABLE);
}
void WorldSession::HandleRepairItemOpcode(WorldPacket& recv_data)
{
DEBUG_LOG("WORLD: CMSG_REPAIR_ITEM");
ObjectGuid npcGuid;
ObjectGuid itemGuid;
uint8 guildBank; // new in 2.3.2, bool that means from guild bank money
recv_data >> npcGuid >> itemGuid >> guildBank;
Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(npcGuid, UNIT_NPC_FLAG_REPAIR);
if (!unit)
{
DEBUG_LOG("WORLD: HandleRepairItemOpcode - %s not found or you can't interact with him.", npcGuid.GetString().c_str());
return;
}
// reputation discount
float discountMod = _player->GetReputationPriceDiscount(unit);
uint32 TotalCost = 0;
if (itemGuid)
{
DEBUG_LOG("ITEM: %s repair of %s", npcGuid.GetString().c_str(), itemGuid.GetString().c_str());
Item* item = _player->GetItemByGuid(itemGuid);
if (item)
TotalCost = _player->DurabilityRepair(item->GetPos(), true, discountMod, (guildBank > 0));
}
else
{
DEBUG_LOG("ITEM: %s repair all items", npcGuid.GetString().c_str());
TotalCost = _player->DurabilityRepairAll(true, discountMod, (guildBank > 0));
}
if (guildBank)
{
uint32 GuildId = _player->GetGuildId();
if (!GuildId)
return;
Guild* pGuild = sGuildMgr.GetGuildById(GuildId);
if (!pGuild)
return;
pGuild->LogBankEvent(GUILD_BANK_LOG_REPAIR_MONEY, 0, _player->GetGUIDLow(), TotalCost);
pGuild->SendMoneyInfo(this, _player->GetGUIDLow());
}
}