From faa78b9e078ab06117affbbcb8696cd3f9015731 Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Mon, 9 Oct 2023 22:07:13 +0200 Subject: [PATCH] Core/Units: moved spell cast request includes out of unit class and use unique_ptr instead of optionals --- src/server/game/Entities/Unit/Unit.cpp | 22 +++++++------- src/server/game/Entities/Unit/Unit.h | 28 ++--------------- src/server/game/Handlers/SpellHandler.cpp | 5 +-- src/server/game/Spells/SpellCastRequest.h | 37 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 src/server/game/Spells/SpellCastRequest.h diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index be522d78c3..3201190a91 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -71,6 +71,7 @@ #include "Spell.h" #include "SpellAuraEffects.h" #include "SpellAuras.h" +#include "SpellCastRequest.h" #include "SpellHistory.h" #include "SpellInfo.h" #include "SpellMgr.h" @@ -293,7 +294,6 @@ bool DispelableAura::RollDispel() const return roll_chance_i(_chance); } - Unit::Unit(bool isWorldObject) : WorldObject(isWorldObject), m_lastSanctuaryTime(0), LastCharmerGUID(), m_ControlledByPlayer(false), movespline(std::make_unique()), @@ -14290,13 +14290,13 @@ bool Unit::CanExecutePendingSpellCastRequest(SpellInfo const* spellInfo) const return true; } -void Unit::RequestSpellCast(PendingSpellCastRequest castRequest, SpellInfo const* spellInfo) +void Unit::RequestSpellCast(std::unique_ptr castRequest, SpellInfo const* spellInfo) { // We are overriding an already existing spell cast request so inform the client that the old cast is being replaced - if (_pendingSpellCastRequest.has_value()) + if (_pendingSpellCastRequest) CancelPendingCastRequest(); - _pendingSpellCastRequest = castRequest; + _pendingSpellCastRequest = std::move(castRequest); // If we can process the cast request right now, do it. if (CanExecutePendingSpellCastRequest(spellInfo)) @@ -14305,7 +14305,7 @@ void Unit::RequestSpellCast(PendingSpellCastRequest castRequest, SpellInfo const void Unit::CancelPendingCastRequest() { - if (!_pendingSpellCastRequest.has_value()) + if (!_pendingSpellCastRequest) return; // We have to inform the client that the cast has been canceled to avoid the cast button to stay highlightened @@ -14319,7 +14319,7 @@ void Unit::CancelPendingCastRequest() ToPlayer()->SendDirectMessage(&data); } - _pendingSpellCastRequest.reset(); + _pendingSpellCastRequest = nullptr; } // A spell can be queued up within 400 milliseconds before global cooldown expires or the cast finishes @@ -14340,14 +14340,14 @@ bool Unit::CanRequestSpellCast(SpellInfo const* spellInfo) const void Unit::ProcessPendingSpellCastRequest() { - if (!_pendingSpellCastRequest.has_value()) + if (!_pendingSpellCastRequest) return; // Sanity check. If the player requested an invalid spell cast, just skip the request. SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(_pendingSpellCastRequest->CastRequest.SpellID); if (!spellInfo) { - _pendingSpellCastRequest.reset(); + _pendingSpellCastRequest = nullptr; return; } @@ -14365,7 +14365,7 @@ void Unit::ProcessPendingSpellCastRequest() if (_pendingSpellCastRequest->CastItemData.has_value()) { ProcessItemCast(*_pendingSpellCastRequest, targets); - _pendingSpellCastRequest.reset(); + _pendingSpellCastRequest = nullptr; return; } @@ -14408,7 +14408,7 @@ void Unit::ProcessPendingSpellCastRequest() if (!allow) { - _pendingSpellCastRequest.reset(); + _pendingSpellCastRequest = nullptr; return; } } @@ -14419,7 +14419,7 @@ void Unit::ProcessPendingSpellCastRequest() // can't use our own spells when we're in possession of another unit, if (caster->isPossessing()) { - _pendingSpellCastRequest.reset(); + _pendingSpellCastRequest = nullptr; return; } diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 01bff114d9..9aa241d2ab 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -68,6 +68,7 @@ struct FactionTemplateEntry; struct LiquidData; struct LiquidTypeEntry; struct MountCapabilityEntry; +struct PendingSpellCastRequest; struct SpellValue; class Aura; @@ -772,29 +773,6 @@ struct PositionUpdateInfo bool Turned = false; }; -struct SpellCastRequestItemData -{ - SpellCastRequestItemData(uint8 bagSlot, uint8 slot, ObjectGuid castItem) : - BagSlot(bagSlot), Slot(slot), CastItem(castItem) - { - } - - uint8 BagSlot = 0; - uint8 Slot = 0; - ObjectGuid CastItem; -}; - -struct PendingSpellCastRequest -{ - PendingSpellCastRequest(WorldPackets::Spells::SpellCastRequest&& castRequest, Optional castItemData = {}) : - CastRequest(castRequest), CastItemData(castItemData) - { - } - - WorldPackets::Spells::SpellCastRequest CastRequest; - Optional CastItemData; -}; - // delay time next attack to prevent client attack animation problems #define ATTACK_DISPLAY_DELAY 200 #define MAX_PLAYER_STEALTH_DETECT_RANGE 30.0f // max distance for detection targets by player @@ -1804,7 +1782,7 @@ class TC_GAME_API Unit : public WorldObject void SetIgnoringCombat(bool apply) { _isIgnoringCombat = apply; } // Queues up a spell cast request that has been received via packet and processes it whenever possible. - void RequestSpellCast(PendingSpellCastRequest castRequest, SpellInfo const* spellInfo); + void RequestSpellCast(std::unique_ptr castRequest, SpellInfo const* spellInfo); void CancelPendingCastRequest(); bool CanRequestSpellCast(SpellInfo const* spell) const; @@ -1957,7 +1935,7 @@ class TC_GAME_API Unit : public WorldObject bool _isIgnoringCombat; - Optional _pendingSpellCastRequest; + std::unique_ptr _pendingSpellCastRequest; void ProcessPendingSpellCastRequest(); void ProcessItemCast(PendingSpellCastRequest const& castRequest, SpellCastTargets const& targets); bool CanExecutePendingSpellCastRequest(SpellInfo const* spellInfo) const; diff --git a/src/server/game/Handlers/SpellHandler.cpp b/src/server/game/Handlers/SpellHandler.cpp index a81c963115..f92da7b84f 100644 --- a/src/server/game/Handlers/SpellHandler.cpp +++ b/src/server/game/Handlers/SpellHandler.cpp @@ -36,6 +36,7 @@ #include "ScriptMgr.h" #include "Spell.h" #include "SpellAuraEffects.h" +#include "SpellCastRequest.h" #include "SpellMgr.h" #include "SpellPackets.h" #include "Totem.h" @@ -103,7 +104,7 @@ void WorldSession::HandleUseItemOpcode(WorldPackets::Spells::UseItem& packet) return; if (user->CanRequestSpellCast(spellInfo)) - user->RequestSpellCast(PendingSpellCastRequest(std::move(packet.Cast), SpellCastRequestItemData(packet.PackSlot, packet.Slot, packet.CastItem)), spellInfo); + user->RequestSpellCast(std::make_unique(std::move(packet.Cast), SpellCastRequestItemData(packet.PackSlot, packet.Slot, packet.CastItem)), spellInfo); } void WorldSession::HandleOpenItemOpcode(WorldPacket& recvPacket) @@ -274,7 +275,7 @@ void WorldSession::HandleCastSpellOpcode(WorldPackets::Spells::CastSpell& cast) return; if (mover->CanRequestSpellCast(spellInfo)) - mover->RequestSpellCast(PendingSpellCastRequest(std::move(cast.Cast)), spellInfo); + mover->RequestSpellCast(std::make_unique(std::move(cast.Cast)), spellInfo); else if (mover->IsPlayer()) Spell::SendCastResult(mover->ToPlayer(), spellInfo, cast.Cast.CastID, SPELL_FAILED_SPELL_IN_PROGRESS); } diff --git a/src/server/game/Spells/SpellCastRequest.h b/src/server/game/Spells/SpellCastRequest.h new file mode 100644 index 0000000000..621e251a1a --- /dev/null +++ b/src/server/game/Spells/SpellCastRequest.h @@ -0,0 +1,37 @@ +/* + * This file is part of the TrinityCore 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, see . + */ + +#include "SpellPacketsCommon.h" + +struct SpellCastRequestItemData +{ + SpellCastRequestItemData(uint8 bagSlot, uint8 slot, ObjectGuid castItem) : + BagSlot(bagSlot), Slot(slot), CastItem(castItem) { } + + uint8 BagSlot = 0; + uint8 Slot = 0; + ObjectGuid CastItem; +}; + +struct PendingSpellCastRequest +{ + PendingSpellCastRequest(WorldPackets::Spells::SpellCastRequest&& castRequest, Optional castItemData = { }) : + CastRequest(castRequest), CastItemData(castItemData) { } + + WorldPackets::Spells::SpellCastRequest CastRequest; + Optional CastItemData; +};