From 1946543ae29dab0b1f3c265fbcc20841be9ee601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E9=B9=AD?= <18535853+PkllonG@users.noreply.github.com> Date: Fri, 17 Oct 2025 02:29:31 +0800 Subject: [PATCH] refactor(Core): SetTimedFacingToObject (#23142) --- src/server/game/Entities/Unit/Unit.cpp | 50 ++++++------------- src/server/game/Entities/Unit/Unit.h | 4 +- .../ScarletEnclave/chapter5.cpp | 2 +- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 825716624..afc90e1a4 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -20115,24 +20115,6 @@ private: AuraType _auraType; }; -class ResetToHomeOrientation : public BasicEvent -{ -public: - ResetToHomeOrientation(Creature& self) : _self(self) { } - - bool Execute(uint64 /*eventTime*/, uint32 /*updateTime*/) override - { - if (_self.IsInWorld() && _self.FindMap() && _self.IsAlive() && !_self.IsInCombat()) - { - _self.SetFacingTo(_self.GetHomePosition().GetOrientation()); - } - - return true; - } -private: - Creature& _self; -}; - void Unit::CastDelayedSpellWithPeriodicAmount(Unit* caster, uint32 spellId, AuraType auraType, int32 addAmount, uint8 effectIndex) { AuraEffect* aurEff = nullptr; @@ -20358,7 +20340,7 @@ void Unit::SetFacingTo(float ori) init.Launch(); } -void Unit::SetFacingToObject(WorldObject* object) +void Unit::SetFacingToObject(WorldObject* object, Milliseconds timed /*= 0ms*/) { // never face when already moving if (!IsStopped()) @@ -20369,24 +20351,20 @@ void Unit::SetFacingToObject(WorldObject* object) init.MoveTo(GetPositionX(), GetPositionY(), GetPositionZ()); init.SetFacing(GetAngle(object)); // when on transport, GetAngle will still return global coordinates (and angle) that needs transforming init.Launch(); -} -void Unit::SetTimedFacingToObject(WorldObject* object, uint32 time) -{ - // never face when already moving - if (!IsStopped() || !time) - return; - - /// @todo figure out under what conditions creature will move towards object instead of facing it where it currently is. - Movement::MoveSplineInit init(this); - init.MoveTo(GetPositionX(), GetPositionY(), GetPositionZ()); - init.SetFacing(GetAngle(object)); // when on transport, GetAngle will still return global coordinates (and angle) that needs transforming - init.Launch(); - - if (Creature* c = ToCreature()) - c->m_Events.AddEvent(new ResetToHomeOrientation(*c), c->m_Events.CalculateTime(time)); - else - LOG_ERROR("entities.unit", "Unit::SetTimedFacingToObject called on non-creature unit {}. This should never happen.", GetEntry()); + if (timed > 0ms) + { + if (Creature* c = ToCreature()) + { + c->m_Events.AddEventAtOffset([c]() + { + if (c->IsInWorld() && c->FindMap() && c->IsAlive() && !c->IsInCombat()) + c->SetFacingTo(c->GetHomePosition().GetOrientation()); + }, timed); + } + else + LOG_ERROR("entities.unit", "Unit::SetFacingToObject called on non-creature unit {}. This should never happen.", GetEntry()); + } } bool Unit::SetWalk(bool enable) diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 78c2dddce..f2fe6bb35 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -1919,8 +1919,8 @@ public: void SetInFront(WorldObject const* target); void SetFacingTo(float ori); - void SetFacingToObject(WorldObject* object); - void SetTimedFacingToObject(WorldObject* object, uint32 time); // Reset to home orientation after given time + // Reset to home orientation after given time + void SetFacingToObject(WorldObject* object, Milliseconds timed = 0ms); bool isInAccessiblePlaceFor(Creature const* c) const; bool isInFrontInMap(Unit const* target, float distance, float arc = M_PI) const; diff --git a/src/server/scripts/EasternKingdoms/ScarletEnclave/chapter5.cpp b/src/server/scripts/EasternKingdoms/ScarletEnclave/chapter5.cpp index 60a91416b..1ec9da6c2 100644 --- a/src/server/scripts/EasternKingdoms/ScarletEnclave/chapter5.cpp +++ b/src/server/scripts/EasternKingdoms/ScarletEnclave/chapter5.cpp @@ -1269,7 +1269,7 @@ class spell_chapter5_return_to_capital : public SpellScript if (creature) { creature->PauseMovement(5000); - creature->SetTimedFacingToObject(player, 30000); + creature->SetFacingToObject(player, 30s); if (roll_chance_i(30)) {