diff --git a/dScripts/ai/LuaAI/LuaAIMachine.h b/dScripts/ai/LuaAI/LuaAIMachine.h index 64d946fc9..8d4425056 100644 --- a/dScripts/ai/LuaAI/LuaAIMachine.h +++ b/dScripts/ai/LuaAI/LuaAIMachine.h @@ -147,7 +147,7 @@ namespace LuaAI { virtual void GoTo(const NiPoint3& target, float speed) = 0; // self:FollowTarget { targetID, radius, speed, keepFollowing } virtual void FollowTarget(LWOOBJID target, float radius, float speed, bool keepFollowing) = 0; - // self:FaceTarget { target, keepFacingTarget = true } + // self:FaceTarget { target, degreesOff = 5, keepFacingTarget = true }: it keeps facing it until it moves virtual void FaceTarget(LWOOBJID target) = 0; virtual void StopPathing() = 0; virtual void AddTimer(const std::string& name, float time) = 0; diff --git a/dScripts/ai/LuaAI/LuaAIScript.cpp b/dScripts/ai/LuaAI/LuaAIScript.cpp index 1e964b814..db0f6beca 100644 --- a/dScripts/ai/LuaAI/LuaAIScript.cpp +++ b/dScripts/ai/LuaAI/LuaAIScript.cpp @@ -17,6 +17,8 @@ #include "SkillComponent.h" #include "eKillType.h" +#include +#include #include #include #include @@ -45,6 +47,7 @@ namespace { movement->SetCurrentSpeed(speed); movement->SetDestination(target); moving = true; + faceTarget = LWOOBJID_EMPTY; } void FollowTarget(const LWOOBJID target, const float radius, const float speed, const bool keepFollowing) override { @@ -52,18 +55,35 @@ namespace { if (!movement) return; movement->FollowTarget(target, radius, speed, keepFollowing); moving = true; + faceTarget = LWOOBJID_EMPTY; } + // keepFacingTarget: it keeps turning to the target, degreesOff 5, until it moves again void FaceTarget(const LWOOBJID target) override { - const auto* const other = Game::entityManager->GetEntity(target); - if (!other) return; - SetRotation(QuatUtils::LookAt(m_Self->GetPosition(), other->GetPosition())); + faceTarget = target; + KeepFacing(); + } + + void KeepFacing() { + if (faceTarget == LWOOBJID_EMPTY) return; + const auto* const other = Game::entityManager->GetEntity(faceTarget); + if (!other) { + faceTarget = LWOOBJID_EMPTY; + return; + } + const auto facing = QuatUtils::LookAt(m_Self->GetPosition(), other->GetPosition()); + const auto current = m_Self->GetRotation(); + const float dot = std::abs(current.w * facing.w + current.x * facing.x + current.y * facing.y + current.z * facing.z); + // The angle between the two rotations: 2 * acos(|dot|) + constexpr float DEGREES_OFF = 5.0f; + if (2.0f * std::acos(std::min(dot, 1.0f)) * (180.0f / 3.14159265f) > DEGREES_OFF) SetRotation(facing); } void StopPathing() override { auto* const movement = m_Self->GetComponent(); if (movement) movement->Stop(); moving = false; + faceTarget = LWOOBJID_EMPTY; } void AddTimer(const std::string& name, const float time) override { m_Self->AddTimer(name, std::max(time, 0.0f)); } @@ -210,6 +230,8 @@ namespace { // A GoTo or FollowTarget is under way: the arrival is the first frame its movement is done bool moving = false; + // The FaceTarget it keeps facing + LWOOBJID faceTarget = LWOOBJID_EMPTY; private: Entity* m_Self; @@ -317,5 +339,6 @@ void LuaAIScript::OnUpdate(Entity* self) { brain->host.moving = true; } } + brain->host.KeepFacing(); brain->machine.Update(); }