diff --git a/src/server/game/Entities/Pet/Pet.cpp b/src/server/game/Entities/Pet/Pet.cpp index d87a7fd08..c799da1d0 100644 --- a/src/server/game/Entities/Pet/Pet.cpp +++ b/src/server/game/Entities/Pet/Pet.cpp @@ -25,6 +25,7 @@ #include "InstanceScript.h" #include "Log.h" #include "ObjectMgr.h" +#include "PetPackets.h" #include "Player.h" #include "QueryHolder.h" #include "ScriptMgr.h" @@ -1863,10 +1864,10 @@ bool Pet::learnSpell(uint32 spell_id) if (!m_loading) { - WorldPacket data(SMSG_PET_LEARNED_SPELL, 4); - data << uint32(spell_id); - GetOwner()->GetSession()->SendPacket(&data); - GetOwner()->PetSpellInitialize(); + WorldPackets::Pet::PetLearnedSpell packet; + packet.SpellID = spell_id; + m_owner->SendDirectMessage(packet.Write()); + m_owner->PetSpellInitialize(); } return true; @@ -1917,9 +1918,9 @@ bool Pet::unlearnSpell(uint32 spell_id, bool learn_prev, bool clear_ab) { if (!m_loading) { - WorldPacket data(SMSG_PET_REMOVED_SPELL, 4); - data << uint32(spell_id); - GetOwner()->GetSession()->SendPacket(&data); + WorldPackets::Pet::PetUnlearnedSpell packet; + packet.SpellID = spell_id; + m_owner->SendDirectMessage(packet.Write()); } return true; @@ -2179,25 +2180,22 @@ uint8 Pet::GetMaxTalentPointsForLevel(uint8 level) void Pet::ToggleAutocast(SpellInfo const* spellInfo, bool apply) { + ASSERT(spellInfo); + if (!spellInfo->IsAutocastable()) return; - uint32 spellid = spellInfo->Id; - - PetSpellMap::iterator itr = m_spells.find(spellid); + PetSpellMap::iterator itr = m_spells.find(spellInfo->Id); if (itr == m_spells.end()) return; - uint32 i; + auto autospellItr = std::find(m_autospells.begin(), m_autospells.end(), spellInfo->Id); if (apply) { - for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i) - ; // just search - - if (i == m_autospells.size()) + if (autospellItr == m_autospells.end()) { - m_autospells.push_back(spellid); + m_autospells.push_back(spellInfo->Id); if (itr->second.active != ACT_ENABLED) { @@ -2209,13 +2207,9 @@ void Pet::ToggleAutocast(SpellInfo const* spellInfo, bool apply) } else { - AutoSpellList::iterator itr2 = m_autospells.begin(); - for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i, ++itr2) - ; // just search - - if (i < m_autospells.size()) + if (autospellItr != m_autospells.end()) { - m_autospells.erase(itr2); + m_autospells.erase(autospellItr); if (itr->second.active != ACT_DISABLED) { itr->second.active = ACT_DISABLED; diff --git a/src/server/game/Handlers/MiscHandler.cpp b/src/server/game/Handlers/MiscHandler.cpp index a53fbdee5..bbfed5792 100644 --- a/src/server/game/Handlers/MiscHandler.cpp +++ b/src/server/game/Handlers/MiscHandler.cpp @@ -1530,7 +1530,7 @@ void WorldSession::HandleMoveSetCanFlyAckOpcode(WorldPacket& recv_data) _player->m_mover->m_movementInfo.flags = movementInfo.GetMovementFlags(); } -void WorldSession::HandleRequestPetInfoOpcode(WorldPacket& /*recv_data */) +void WorldSession::HandleRequestPetInfo(WorldPackets::Pet::RequestPetInfo& /*packet*/) { /* LOG_DEBUG("network.opcode", "WORLD: CMSG_REQUEST_PET_INFO"); diff --git a/src/server/game/Handlers/PetHandler.cpp b/src/server/game/Handlers/PetHandler.cpp index 7cd4bf940..6d9b1c1af 100644 --- a/src/server/game/Handlers/PetHandler.cpp +++ b/src/server/game/Handlers/PetHandler.cpp @@ -25,6 +25,7 @@ #include "ObjectMgr.h" #include "Opcodes.h" #include "Pet.h" +#include "PetPackets.h" #include "Player.h" #include "QueryHolder.h" #include "Spell.h" @@ -35,19 +36,14 @@ #include "WorldPacket.h" #include "WorldSession.h" -void WorldSession::HandleDismissCritter(WorldPacket& recvData) +void WorldSession::HandleDismissCritter(WorldPackets::Pet::DismissCritter& packet) { - ObjectGuid guid; - recvData >> guid; - - LOG_DEBUG("network", "WORLD: Received CMSG_DISMISS_CRITTER for {}", guid.ToString()); - - Unit* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid); + Unit* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, packet.CritterGUID); if (!pet) { LOG_DEBUG("network", "Vanitypet ({}) does not exist - player {} ({} / account: {}) attempted to dismiss it (possibly lagged out)", - guid.ToString(), GetPlayer()->GetName(), GetPlayer()->GetGUID().ToString(), GetAccountId()); + packet.CritterGUID.ToString(), GetPlayer()->GetName(), GetPlayer()->GetGUID().ToString(), GetAccountId()); return; } @@ -128,24 +124,19 @@ void WorldSession::HandlePetAction(WorldPacket& recvData) } } -void WorldSession::HandlePetStopAttack(WorldPacket& recvData) +void WorldSession::HandlePetStopAttack(WorldPackets::Pet::PetStopAttack& packet) { - ObjectGuid guid; - recvData >> guid; - - LOG_DEBUG("network", "WORLD: Received CMSG_PET_STOP_ATTACK for {}", guid.ToString()); - - Unit* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid); + Unit* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, packet.PetGUID); if (!pet) { - LOG_ERROR("network.opcode", "HandlePetStopAttack: Pet {} does not exist", guid.ToString()); + LOG_ERROR("network.opcode", "HandlePetStopAttack: Pet {} does not exist", packet.PetGUID.ToString()); return; } if (pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm()) { - LOG_ERROR("network.opcode", "HandlePetStopAttack: Pet {} isn't a pet or charmed creature of player {}", guid.ToString(), GetPlayer()->GetName()); + LOG_ERROR("network.opcode", "HandlePetStopAttack: Pet {} isn't a pet or charmed creature of player {}", packet.PetGUID.ToString(), GetPlayer()->GetName()); return; } @@ -927,17 +918,13 @@ void WorldSession::HandlePetRename(WorldPacket& recvData) pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, uint32(GameTime::GetGameTime().count())); // cast can't be helped } -void WorldSession::HandlePetAbandon(WorldPacket& recvData) +void WorldSession::HandlePetAbandon(WorldPackets::Pet::PetAbandon& packet) { - ObjectGuid guid; - recvData >> guid; //pet guid - LOG_DEBUG("network.opcode", "HandlePetAbandon. CMSG_PET_ABANDON pet is {}", guid.ToString()); - if (!_player->IsInWorld()) return; // pet/charmed - Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid); + Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, packet.PetGUID); if (pet && pet->ToPet() && pet->ToPet()->getPetType() == HUNTER_PET) { if (pet->IsPet()) @@ -955,62 +942,59 @@ void WorldSession::HandlePetAbandon(WorldPacket& recvData) } } -void WorldSession::HandlePetSpellAutocastOpcode(WorldPacket& recvPacket) +void WorldSession::HandlePetSpellAutocastOpcode(WorldPackets::Pet::PetSpellAutocast& packet) { - LOG_DEBUG("network.opcode", "CMSG_PET_SPELL_AUTOCAST"); - ObjectGuid guid; - uint32 spellId; - uint8 state; //1 for on, 0 for off - recvPacket >> guid >> spellId >> state; - - if (!_player->GetGuardianPet() && !_player->GetCharm()) - return; - - if (guid.IsPlayer()) - return; - - SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellId); - if (!spellInfo) - return; - - Creature* checkPet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid); - if (!checkPet || (checkPet != _player->GetGuardianPet() && checkPet != _player->GetCharm())) + Creature* checkPet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, packet.PetGUID); + if (!checkPet) { - LOG_ERROR("network.opcode", "HandlePetSpellAutocastOpcode.Pet {} isn't pet of player {} .", guid.ToString(), GetPlayer()->GetName()); + LOG_ERROR("entities.pet", "WorldSession::HandlePetSpellAutocastOpcode: Pet %s not found.", packet.PetGUID.ToString().c_str()); + return; + } + + SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(packet.SpellID); + if (!spellInfo) + { + LOG_ERROR("spells.pet", "WorldSession::HandlePetSpellAutocastOpcode: Unknown spell id %u used by %s.", packet.SpellID, packet.PetGUID.ToString().c_str()); + return; + } + + if (checkPet != _player->GetGuardianPet() && checkPet != _player->GetCharm()) + { + LOG_ERROR("entities.pet", "WorldSession::HandlePetSpellAutocastOpcode: %s isn't pet of player %s (%s).", + packet.PetGUID.ToString().c_str(), GetPlayer()->GetName().c_str(), GetPlayer()->GetGUID().ToString().c_str()); return; } Unit::ControlSet petsSet; - if (checkPet->GetEntry() != guid.GetEntry()) + if (checkPet->GetEntry() != packet.PetGUID.GetEntry()) petsSet.insert(checkPet); else petsSet = _player->m_Controlled; // Xinef: loop all pets with same entry (fixes partial state change for feral spirits) - for (Unit::ControlSet::const_iterator itr = petsSet.begin(); itr != petsSet.end(); ++itr) + for (Unit* pet : petsSet) { - Unit* pet = *itr; - if (checkPet->GetEntry() == guid.GetEntry() && pet->GetEntry() != guid.GetEntry()) + if (checkPet->GetEntry() == packet.PetGUID.GetEntry() && pet->GetEntry() != packet.PetGUID.GetEntry()) continue; // do not add not learned spells/ passive spells - if (!pet->HasSpell(spellId) || !spellInfo->IsAutocastable()) + if (!pet->HasSpell(packet.SpellID) || !spellInfo->IsAutocastable()) continue; CharmInfo* charmInfo = pet->GetCharmInfo(); if (!charmInfo) { - LOG_ERROR("network.opcode", "WorldSession::HandlePetSpellAutocastOpcod: object ({} TypeId: {}) is considered pet-like but doesn't have a charminfo!", + LOG_ERROR("network.opcode", "WorldSession::HandlePetSpellAutocastOpcode: object ({} TypeId: {}) is considered pet-like but doesn't have a charminfo!", pet->GetGUID().ToString(), pet->GetTypeId()); continue; } - if (pet->IsPet()) - ((Pet*)pet)->ToggleAutocast(spellInfo, state); + if (Pet* summon = pet->ToPet()) + summon->ToggleAutocast(spellInfo, packet.AutocastEnabled); else - pet->GetCharmInfo()->ToggleCreatureAutocast(spellInfo, state); + charmInfo->ToggleCreatureAutocast(spellInfo, packet.AutocastEnabled); - charmInfo->SetSpellAutocast(spellInfo, state); + charmInfo->SetSpellAutocast(spellInfo, packet.AutocastEnabled); } } diff --git a/src/server/game/Server/Packets/AllPackets.h b/src/server/game/Server/Packets/AllPackets.h index 694037e2b..f8dc886e1 100644 --- a/src/server/game/Server/Packets/AllPackets.h +++ b/src/server/game/Server/Packets/AllPackets.h @@ -21,6 +21,7 @@ #include "BankPackets.h" #include "CharacterPackets.h" #include "ChatPackets.h" +#include "PetPackets.h" #include "CombatLogPackets.h" #include "GuildPackets.h" #include "MiscPackets.h" diff --git a/src/server/game/Server/Packets/PetPackets.cpp b/src/server/game/Server/Packets/PetPackets.cpp new file mode 100644 index 000000000..8028bce25 --- /dev/null +++ b/src/server/game/Server/Packets/PetPackets.cpp @@ -0,0 +1,52 @@ +/* + * This file is part of the AzerothCore 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 Affero 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 Affero 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 . + */ + +#include "PetPackets.h" + +void WorldPackets::Pet::DismissCritter::Read() +{ + _worldPacket >> CritterGUID; +} + +void WorldPackets::Pet::PetAbandon::Read() +{ + _worldPacket >> PetGUID; +} + +void WorldPackets::Pet::PetStopAttack::Read() +{ + _worldPacket >> PetGUID; +} + +void WorldPackets::Pet::PetSpellAutocast::Read() +{ + _worldPacket >> PetGUID; + _worldPacket >> SpellID; + _worldPacket >> AutocastEnabled; +} + +WorldPacket const* WorldPackets::Pet::PetLearnedSpell::Write() +{ + _worldPacket << uint32(SpellID); + return &_worldPacket; +} + +WorldPacket const* WorldPackets::Pet::PetUnlearnedSpell::Write() +{ + _worldPacket << uint32(SpellID); + return &_worldPacket; +} diff --git a/src/server/game/Server/Packets/PetPackets.h b/src/server/game/Server/Packets/PetPackets.h new file mode 100644 index 000000000..ccc1bb4d1 --- /dev/null +++ b/src/server/game/Server/Packets/PetPackets.h @@ -0,0 +1,100 @@ +/* + * This file is part of the AzerothCore 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 Affero 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 Affero 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 . + */ + +#ifndef PetPackets_h__ +#define PetPackets_h__ + +#include "Packet.h" +#include "ObjectGuid.h" + +namespace WorldPackets +{ + namespace Pet + { + class DismissCritter final : public ClientPacket + { + public: + DismissCritter(WorldPacket&& packet) : ClientPacket(CMSG_DISMISS_CRITTER, std::move(packet)) { } + + void Read() override; + + ObjectGuid CritterGUID; + }; + + class PetAbandon final : public ClientPacket + { + public: + PetAbandon(WorldPacket&& packet) : ClientPacket(CMSG_PET_ABANDON, std::move(packet)) { } + + void Read() override; + + ObjectGuid PetGUID; + }; + + class PetStopAttack final : public ClientPacket + { + public: + PetStopAttack(WorldPacket&& packet) : ClientPacket(CMSG_PET_STOP_ATTACK, std::move(packet)) { } + + void Read() override; + + ObjectGuid PetGUID; + }; + + class PetSpellAutocast final : public ClientPacket + { + public: + PetSpellAutocast(WorldPacket&& packet) : ClientPacket(CMSG_PET_SPELL_AUTOCAST, std::move(packet)) { } + + void Read() override; + + ObjectGuid PetGUID; + uint32 SpellID = 0; + bool AutocastEnabled = false; + }; + + class PetLearnedSpell final : public ServerPacket + { + public: + PetLearnedSpell() : ServerPacket(SMSG_PET_LEARNED_SPELL, 4) { } + + WorldPacket const* Write() override; + + uint32 SpellID = 0; + }; + + class PetUnlearnedSpell final : public ServerPacket + { + public: + PetUnlearnedSpell() : ServerPacket(SMSG_PET_UNLEARNED_SPELL, 4) { } + + WorldPacket const* Write() override; + + uint32 SpellID = 0; + }; + + class RequestPetInfo final : public ClientPacket + { + public: + RequestPetInfo(WorldPacket&& packet) : ClientPacket(CMSG_REQUEST_PET_INFO, std::move(packet)) { } + + void Read() override { } + }; + } +} + +#endif // PetPackets_h__ diff --git a/src/server/game/Server/Protocol/Opcodes.cpp b/src/server/game/Server/Protocol/Opcodes.cpp index 34a9cbdd0..3691f1f42 100644 --- a/src/server/game/Server/Protocol/Opcodes.cpp +++ b/src/server/game/Server/Protocol/Opcodes.cpp @@ -761,8 +761,8 @@ void OpcodeTable::Initialize() /*0x276*/ DEFINE_HANDLER(MSG_QUEST_PUSH_RESULT, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleQuestPushResult ); /*0x277*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_PLAY_MUSIC, STATUS_NEVER); /*0x278*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_PLAY_OBJECT_SOUND, STATUS_NEVER); - /*0x279*/ DEFINE_HANDLER(CMSG_REQUEST_PET_INFO, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleRequestPetInfoOpcode ); - /*0x27A*/ DEFINE_HANDLER(CMSG_FAR_SIGHT, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleFarSightOpcode ); + /*0x279*/ DEFINE_HANDLER(CMSG_REQUEST_PET_INFO, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleRequestPetInfo ); + /*0x27A*/ DEFINE_HANDLER(CMSG_FAR_SIGHT, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleFarSightOpcode ); /*0x27B*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_SPELLDISPELLOG, STATUS_NEVER); /*0x27C*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_DAMAGE_CALC_LOG, STATUS_NEVER); /*0x27D*/ DEFINE_HANDLER(CMSG_ENABLE_DAMAGE_LOG, STATUS_NEVER, PROCESS_INPLACE, &WorldSession::Handle_NULL ); @@ -1306,7 +1306,7 @@ void OpcodeTable::Initialize() /*0x497*/ DEFINE_HANDLER(CMSG_FLOOD_GRACE_CHEAT, STATUS_NEVER, PROCESS_INPLACE, &WorldSession::Handle_NULL ); /*0x498*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_SERVER_FIRST_ACHIEVEMENT, STATUS_NEVER); /*0x499*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_PET_LEARNED_SPELL, STATUS_NEVER); - /*0x49A*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_PET_REMOVED_SPELL, STATUS_NEVER); + /*0x49A*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_PET_UNLEARNED_SPELL, STATUS_NEVER); /*0x49B*/ DEFINE_HANDLER(CMSG_CHANGE_SEATS_ON_CONTROLLED_VEHICLE, STATUS_LOGGEDIN, PROCESS_INPLACE, &WorldSession::HandleChangeSeatsOnControlledVehicle ); /*0x49C*/ DEFINE_HANDLER(CMSG_HEARTH_AND_RESURRECT, STATUS_LOGGEDIN, PROCESS_THREADSAFE, &WorldSession::HandleHearthAndResurrect ); /*0x49D*/ DEFINE_SERVER_OPCODE_HANDLER(SMSG_ON_CANCEL_EXPECTED_RIDE_VEHICLE_AURA, STATUS_NEVER); diff --git a/src/server/game/Server/Protocol/Opcodes.h b/src/server/game/Server/Protocol/Opcodes.h index 5f428fddc..c3b06bfe2 100644 --- a/src/server/game/Server/Protocol/Opcodes.h +++ b/src/server/game/Server/Protocol/Opcodes.h @@ -1205,7 +1205,7 @@ enum Opcodes : uint16 CMSG_FLOOD_GRACE_CHEAT = 0x497, SMSG_SERVER_FIRST_ACHIEVEMENT = 0x498, SMSG_PET_LEARNED_SPELL = 0x499, - SMSG_PET_REMOVED_SPELL = 0x49A, + SMSG_PET_UNLEARNED_SPELL = 0x49A, CMSG_CHANGE_SEATS_ON_CONTROLLED_VEHICLE = 0x49B, CMSG_HEARTH_AND_RESURRECT = 0x49C, SMSG_ON_CANCEL_EXPECTED_RIDE_VEHICLE_AURA = 0x49D, diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index 520805264..0f2268864 100644 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -139,6 +139,15 @@ namespace WorldPackets { class RandomRollClient; } + + namespace Pet + { + class DismissCritter; + class PetAbandon; + class PetStopAttack; + class PetSpellAutocast; + class RequestPetInfo; + } } enum AccountDataType @@ -858,14 +867,14 @@ public: // opcodes handlers //Pet void HandlePetAction(WorldPacket& recvData); - void HandlePetStopAttack(WorldPacket& recvData); + void HandlePetStopAttack(WorldPackets::Pet::PetStopAttack& packet); void HandlePetActionHelper(Unit* pet, ObjectGuid guid1, uint32 spellid, uint16 flag, ObjectGuid guid2); void HandlePetNameQuery(WorldPacket& recvData); void HandlePetSetAction(WorldPacket& recvData); - void HandlePetAbandon(WorldPacket& recvData); + void HandlePetAbandon(WorldPackets::Pet::PetAbandon& packet); void HandlePetRename(WorldPacket& recvData); void HandlePetCancelAuraOpcode(WorldPacket& recvPacket); - void HandlePetSpellAutocastOpcode(WorldPacket& recvPacket); + void HandlePetSpellAutocastOpcode(WorldPackets::Pet::PetSpellAutocast& packet); void HandlePetCastSpellOpcode(WorldPacket& recvPacket); void HandlePetLearnTalent(WorldPacket& recvPacket); void HandleLearnPreviewTalentsPet(WorldPacket& recvPacket); @@ -877,7 +886,7 @@ public: // opcodes handlers void HandleSetPlayerDeclinedNames(WorldPacket& recvData); void HandleTotemDestroyed(WorldPackets::Totem::TotemDestroyed& totemDestroyed); - void HandleDismissCritter(WorldPacket& recvData); + void HandleDismissCritter(WorldPackets::Pet::DismissCritter& dismissCritter); //Battleground void HandleBattlemasterHelloOpcode(WorldPacket& recvData); @@ -962,7 +971,7 @@ public: // opcodes handlers void HandleCancelMountAuraOpcode(WorldPacket& recvData); void HandleSelfResOpcode(WorldPacket& recvData); void HandleComplainOpcode(WorldPacket& recvData); - void HandleRequestPetInfoOpcode(WorldPacket& recvData); + void HandleRequestPetInfo(WorldPackets::Pet::RequestPetInfo& packet); // Socket gem void HandleSocketOpcode(WorldPacket& recvData);