mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
feat(ai): shared Lua AI keeps facing its FaceTarget
FaceTarget with keepFacingTarget turned once. The binding now keeps turning to the target each frame while it is more than 5 degrees off, until the next GoTo, FollowTarget or StopPathing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#include "SkillComponent.h"
|
||||
#include "eKillType.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <unordered_map>
|
||||
@@ -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<MovementAIComponent>();
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user