From c215363e86a260e9455469ffffab9e84263fb276 Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 19 Aug 2025 18:02:42 +0200 Subject: Core/BattlePets: Refactor BattlePetMgr::SendUpdates to use std::span argument --- src/server/game/BattlePets/BattlePetMgr.cpp | 53 +++++++++++------------ src/server/game/BattlePets/BattlePetMgr.h | 2 +- src/server/game/Server/Packets/BattlePetPackets.h | 2 +- 3 files changed, 27 insertions(+), 30 deletions(-) (limited to 'src/server') diff --git a/src/server/game/BattlePets/BattlePetMgr.cpp b/src/server/game/BattlePets/BattlePetMgr.cpp index 05e7939097f..b1a0acad7e0 100644 --- a/src/server/game/BattlePets/BattlePetMgr.cpp +++ b/src/server/game/BattlePets/BattlePetMgr.cpp @@ -479,9 +479,8 @@ void BattlePetMgr::AddPet(uint32 species, uint32 display, uint16 breed, BattlePe _pets[pet.PacketInfo.Guid.GetCounter()] = std::move(pet); - std::vector> updates; - updates.push_back(std::ref(pet)); - SendUpdates(std::move(updates), true); + std::array, 1> updates = { pet }; + SendUpdates(updates, true); player->UpdateCriteria(CriteriaType::UniquePetsOwned); player->UpdateCriteria(CriteriaType::LearnedNewPet, species); @@ -687,9 +686,8 @@ void BattlePetMgr::ChangeBattlePetQuality(ObjectGuid guid, BattlePetBreedQuality if (pet->SaveInfo != BATTLE_PET_NEW) pet->SaveInfo = BATTLE_PET_CHANGED; - std::vector> updates; - updates.push_back(std::ref(*pet)); - SendUpdates(std::move(updates), false); + std::array, 1> updates = { *pet }; + SendUpdates(updates, false); // UF::PlayerData::CurrentBattlePetBreedQuality isn't updated (Intended) // _owner->GetPlayer()->SetCurrentBattlePetBreedQuality(qualityValue); @@ -750,9 +748,8 @@ void BattlePetMgr::GrantBattlePetExperience(ObjectGuid guid, uint16 xp, BattlePe if (pet->SaveInfo != BATTLE_PET_NEW) pet->SaveInfo = BATTLE_PET_CHANGED; - std::vector> updates; - updates.push_back(std::ref(*pet)); - SendUpdates(std::move(updates), false); + std::array, 1> updates = { *pet }; + SendUpdates(updates, false); } void BattlePetMgr::GrantBattlePetLevel(ObjectGuid guid, uint16 grantedLevels) @@ -789,29 +786,31 @@ void BattlePetMgr::GrantBattlePetLevel(ObjectGuid guid, uint16 grantedLevels) if (pet->SaveInfo != BATTLE_PET_NEW) pet->SaveInfo = BATTLE_PET_CHANGED; - std::vector> updates; - updates.push_back(std::ref(*pet)); - SendUpdates(std::move(updates), false); + std::array, 1> updates = { *pet }; + SendUpdates(updates, false); } void BattlePetMgr::HealBattlePetsPct(uint8 pct) { // TODO: After each Pet Battle, any injured companion will automatically // regain 50 % of the damage that was taken during combat - std::vector> updates; + std::vector> updates; - for (auto& pet : _pets) - if (pet.second.PacketInfo.Health != pet.second.PacketInfo.MaxHealth) - { - pet.second.PacketInfo.Health += CalculatePct(pet.second.PacketInfo.MaxHealth, pct); - // don't allow Health to be greater than MaxHealth - pet.second.PacketInfo.Health = std::min(pet.second.PacketInfo.Health, pet.second.PacketInfo.MaxHealth); - if (pet.second.SaveInfo != BATTLE_PET_NEW) - pet.second.SaveInfo = BATTLE_PET_CHANGED; - updates.push_back(std::ref(pet.second)); - } + for (auto& [_, pet] : _pets) + { + if (pet.PacketInfo.Health == pet.PacketInfo.MaxHealth) + continue; - SendUpdates(std::move(updates), false); + pet.PacketInfo.Health += CalculatePct(pet.PacketInfo.MaxHealth, pct); + // don't allow Health to be greater than MaxHealth + pet.PacketInfo.Health = std::min(pet.PacketInfo.Health, pet.PacketInfo.MaxHealth); + if (pet.SaveInfo != BATTLE_PET_NEW) + pet.SaveInfo = BATTLE_PET_CHANGED; + + updates.push_back(pet); + } + + SendUpdates(updates, false); } void BattlePetMgr::UpdateBattlePetData(ObjectGuid guid) @@ -885,12 +884,10 @@ void BattlePetMgr::SendJournal() _owner->SendPacket(battlePetJournal.Write()); } -void BattlePetMgr::SendUpdates(std::vector> pets, bool petAdded) +void BattlePetMgr::SendUpdates(std::span const> pets, bool petAdded) { WorldPackets::BattlePet::BattlePetUpdates updates; - for (BattlePet& pet : pets) - updates.Pets.push_back(std::ref(pet.PacketInfo)); - + std::ranges::transform(pets, std::back_inserter(updates.Pets), &BattlePet::PacketInfo); updates.PetAdded = petAdded; _owner->SendPacket(updates.Write()); } diff --git a/src/server/game/BattlePets/BattlePetMgr.h b/src/server/game/BattlePets/BattlePetMgr.h index 5d4378c07ed..4a35cfe0923 100644 --- a/src/server/game/BattlePets/BattlePetMgr.h +++ b/src/server/game/BattlePets/BattlePetMgr.h @@ -193,7 +193,7 @@ public: void DismissPet(); void SendJournal(); - void SendUpdates(std::vector> pets, bool petAdded); + void SendUpdates(std::span const> pets, bool petAdded); void SendError(BattlePetError error, uint32 creatureId); void SendJournalLockStatus(); diff --git a/src/server/game/Server/Packets/BattlePetPackets.h b/src/server/game/Server/Packets/BattlePetPackets.h index 158ab2e5092..2ae8cbd7f97 100644 --- a/src/server/game/Server/Packets/BattlePetPackets.h +++ b/src/server/game/Server/Packets/BattlePetPackets.h @@ -116,7 +116,7 @@ namespace WorldPackets WorldPacket const* Write() override; - std::vector> Pets; + std::vector> Pets; bool PetAdded = false; }; -- cgit v1.2.3