feat: Add a bunch of behaviors (All rotations, DoDamage, OnTimer, OnStartup, MoveBackToStart) and make models move based on normals, not world axes (#2038)

* add rotation behavior logic (not working correctly for multiple axes)

* Fix FetchContent_Declare speed

* remove manual quaternion math

* feat: working rotations

* remove flag

* feat: make directional movements based on model rotation and not world axes

* poggers rotation on the strip

* Add OnStartup and OnTimer

* go home works

* Add DoDamage behavior and dont try and start the same node multiple times

* fix: pause bbb models

* interrupt rotations and save interrupt count across resets
This commit is contained in:
David Markowitz
2026-10-05 10:33:45 -07:00
committed by GitHub
parent 66e2790193
commit fc6a7fbd52
20 changed files with 465 additions and 127 deletions

View File

@@ -588,7 +588,7 @@ void DestroyableComponent::Damage(uint32_t damage, const LWOOBJID source, uint32
// Client does the same check, so we're doing it too
auto* const modelComponent = m_Parent->GetComponent<ModelComponent>();
if (modelComponent) {
modelComponent->OnHit();
modelComponent->OnHit(source);
// Don't actually deal the damage so the model doesn't die
return;
}

View File

@@ -1,8 +1,12 @@
#include "ModelComponent.h"
#include <cmath>
#include "Entity.h"
#include "Game.h"
#include "Logger.h"
#include "dMath.h"
#include "BehaviorStates.h"
#include "ControlBehaviorMsgs.h"
@@ -12,6 +16,7 @@
#include "SimplePhysicsComponent.h"
#include "eMissionTaskType.h"
#include "eObjectBits.h"
#include "DestroyableComponent.h"
#include "Database.h"
#include "DluAssert.h"
@@ -42,8 +47,13 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::ResetModelToDefaults&
if (reset.bResetPos) m_Parent->SetPosition(m_OriginalPosition);
if (reset.bResetRot) m_Parent->SetRotation(m_OriginalRotation);
m_Parent->SetVelocity(NiPoint3Constant::ZERO);
// Save and increment the interrupt count so other behaviors on this model move to their next action instead of getting stuck
const auto moveInterruptCount = m_Move.interruptCount + 1;
m_Move = MoveState{};
m_Move.interruptCount = moveInterruptCount;
SyncLinearVelocity();
ResetRotationState(m_Parent->GetRotation());
m_Speed = 3.0f;
m_NumListeningInteract = 0;
m_NumActiveAttack = 0;
@@ -71,11 +81,21 @@ bool ModelComponent::OnRequestUse(GameMessages::RequestUse& requestUse) {
void ModelComponent::Update(float deltaTime) {
if (m_IsPaused) return;
m_DamageCooldown -= deltaTime;
// Arrived once this frame's movement reached or passed the target
if (m_Move.target && (*m_Move.target - m_Parent->GetPosition()).DotProduct(m_Move.targetDirection) <= 0.0f) {
m_Parent->SetPosition(*m_Move.target);
m_Move.target.reset();
}
for (auto& behavior : m_Behaviors) {
behavior.Update(deltaTime, *this);
}
// Done after all behaviors so the heading reflects any rotation applied this frame
SyncLinearVelocity();
if (!m_RestartAtEndOfFrame) return;
GameMessages::ResetModelToDefaults reset{};
@@ -284,41 +304,102 @@ void ModelComponent::RemoveUnSmash() {
m_NumActiveUnSmash--;
}
bool ModelComponent::TrySetVelocity(const NiPoint3& velocity) const {
auto currentVelocity = m_Parent->GetVelocity();
bool ModelComponent::TryStartMove(const int axis, const float direction, const float speed) {
if (axis < 0 || axis > 2 || direction == 0.0f || speed <= 0.0f) return false;
if (m_Move.velocity[axis] != 0.0f) return false;
// If we're currently moving on an axis, prevent the move so only 1 behavior can have control over an axis
if (velocity != NiPoint3Constant::ZERO) {
const auto [x, y, z] = velocity * m_Speed;
if (x != 0.0f) {
if (currentVelocity.x != 0.0f) return false;
currentVelocity.x = x;
} else if (y != 0.0f) {
if (currentVelocity.y != 0.0f) return false;
currentVelocity.y = y;
} else if (z != 0.0f) {
if (currentVelocity.z != 0.0f) return false;
currentVelocity.z = z;
}
} else {
currentVelocity = velocity;
}
m_Parent->SetVelocity(currentVelocity);
m_Move.target.reset();
m_Move.velocity[axis] = std::copysign(speed, direction);
return true;
}
void ModelComponent::SetVelocity(const NiPoint3& velocity) const {
m_Parent->SetVelocity(velocity);
void ModelComponent::StopMove(const int axis) {
if (axis < 0 || axis > 2) return;
m_Move.velocity[axis] = 0.0f;
}
void ModelComponent::OnChatMessageReceived(const std::string& sMessage) {
for (auto& behavior : m_Behaviors) behavior.OnChatMessageReceived(sMessage);
void ModelComponent::StartMoveTo(const NiPoint3& target, const float speed) {
m_Move.velocity = NiPoint3Constant::ZERO;
m_Move.interruptCount++;
m_Move.target = target;
m_Move.targetSpeed = speed;
}
void ModelComponent::OnHit() {
void ModelComponent::SyncLinearVelocity() {
const auto& rotation = m_Parent->GetRotation();
m_Move.basis = { QuatUtils::Right(rotation), QuatUtils::Up(rotation), QuatUtils::Forward(rotation) };
NiPoint3 velocity = NiPoint3Constant::ZERO;
for (int axis = 0; axis < 3; axis++) velocity += m_Move.basis[axis] * m_Move.velocity[axis];
if (m_Move.target) {
const auto toTarget = *m_Move.target - m_Parent->GetPosition();
const float distance = toTarget.Length();
m_Move.targetDirection = distance > 0.0f ? toTarget / distance : NiPoint3Constant::ZERO;
velocity += m_Move.targetDirection * m_Move.targetSpeed;
}
// Leave velocity alone unless a move owns it, e.g. pets are driven elsewhere
const bool isMoving = velocity != NiPoint3Constant::ZERO;
if (!isMoving && !m_Move.wasMoving) return;
m_Move.wasMoving = isMoving;
// Setting velocity always marks it dirty for serialization
if (velocity != m_Parent->GetVelocity()) m_Parent->SetVelocity(velocity);
}
bool ModelComponent::TryStartRotation(const int axis, const float direction, const float speed) {
if (axis < 0 || axis > 2 || direction == 0.0f || speed <= 0.0f) return false;
if (m_Rotation.velocity[axis] != 0.0f) return false;
// Rebase only when nothing is rotating so simultaneous rotations stay relative to the same base
if (m_Rotation.velocity == NiPoint3Constant::ZERO) ResetRotationState(m_Parent->GetRotation());
m_Rotation.degrees[axis] = std::fmod(m_Rotation.degrees[axis], 360.0f);
m_Rotation.actionStart[axis] = m_Rotation.degrees[axis];
m_Rotation.velocity[axis] = std::copysign(BASE_ANGULAR_SPEED * speed, direction);
SyncAngularVelocity();
return true;
}
void ModelComponent::SetRotationProgress(const int axis, const float degrees) {
if (axis < 0 || axis > 2) return;
m_Rotation.degrees[axis] = m_Rotation.actionStart[axis] + degrees;
// Whole turns wrap to exactly 0 so e.g. 720 degrees yields exactly the base rotation
const NiPoint3 radians(
Math::DegToRad(std::fmod(m_Rotation.degrees.x, 360.0f)),
Math::DegToRad(std::fmod(m_Rotation.degrees.y, 360.0f)),
Math::DegToRad(std::fmod(m_Rotation.degrees.z, 360.0f))
);
m_Parent->SetRotation(QuatUtils::FromEuler(radians) * m_Rotation.base);
}
void ModelComponent::StopRotation(const int axis) {
if (axis < 0 || axis > 2) return;
m_Rotation.velocity[axis] = 0.0f;
SyncAngularVelocity();
}
void ModelComponent::SyncAngularVelocity() const {
GameMessages::SetAngularVelocity setAngVel{};
setAngVel.target = m_Parent->GetObjectID();
setAngVel.angVelocity = m_Rotation.velocity * Math::DegToRad(1.0f);
setAngVel.Send();
}
void ModelComponent::ResetRotationState(const NiQuaternion& newBase) {
m_Rotation = RotationState{ .base = newBase };
SyncAngularVelocity();
}
void ModelComponent::OnChatMessageReceived(const std::string& sMessage, const LWOOBJID sender) {
for (auto& behavior : m_Behaviors) behavior.OnChatMessageReceived(sMessage, sender);
}
void ModelComponent::OnHit(const LWOOBJID attacker) {
for (auto& behavior : m_Behaviors) {
behavior.OnHit();
behavior.OnHit(attacker);
}
}
@@ -362,3 +443,14 @@ bool ModelComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& re
return true;
}
void ModelComponent::DoDamage(const LWOOBJID target) {
// dont do damage if we've done damage recently
if (m_DamageCooldown > 0.0f) return;
m_DamageCooldown = 1.0f;
auto* const initiator = Game::entityManager->GetEntity(target);
if (initiator) {
auto* const destComp = initiator->GetComponent<DestroyableComponent>();
if (destComp) destComp->Damage(1, GetParent()->GetObjectID());
}
}

View File

@@ -1,7 +1,9 @@
#pragma once
#include <array>
#include <cmath>
#include <map>
#include <optional>
#include "dCommonVars.h"
#include "RakNetTypes.h"
@@ -139,20 +141,41 @@ public:
void Resume();
// Attempts to set the velocity of an axis for movement.
// If the axis currently has a velocity of zero, returns true.
// If the axis is currently controlled by a behavior, returns false.
bool TrySetVelocity(const NiPoint3& velocity) const;
// Attempts to claim a local axis (0 = right, 1 = up, 2 = forward) for movement in direction (+1 or -1) at speed units per second.
// Returns false if the axis is already controlled by a behavior. Cancels any active StartMoveTo.
bool TryStartMove(const int axis, const float direction, const float speed);
// Force sets the velocity to a value.
void SetVelocity(const NiPoint3& velocity) const;
// Releases the local axis so another behavior can move along it.
void StopMove(const int axis);
void OnChatMessageReceived(const std::string& sMessage);
// World space direction of the local axis used for the most recently applied velocity.
const NiPoint3& GetMoveAxis(const int axis) const { return m_Move.basis[axis]; }
void OnHit();
// Interrupts all active moves and moves in a straight line to target at speed units per second.
void StartMoveTo(const NiPoint3& target, const float speed);
// Sets the speed of the model
void SetSpeed(const float newSpeed) { m_Speed = newSpeed; }
// Whether a StartMoveTo move has yet to arrive or be overridden.
bool IsMovingToTarget() const noexcept { return m_Move.target.has_value(); }
// Changes whenever active moves are interrupted, so strips can tell their move was cancelled.
uint32_t GetMoveInterruptCount() const noexcept { return m_Move.interruptCount; }
// Attempts to claim a world axis (0 = x, 1 = y, 2 = z) for rotation in direction (+1 or -1) at the given behavior speed.
// Returns false if the axis is already controlled by a behavior.
bool TryStartRotation(const int axis, const float direction, const float speed);
// Sets how many signed degrees the active rotation on axis has progressed and updates the entity.
void SetRotationProgress(const int axis, const float degrees);
// Releases the axis so another behavior can rotate it.
void StopRotation(const int axis);
// Degrees per second of the active rotation on axis.
float GetAngularSpeed(const int axis) const noexcept { return std::abs(m_Rotation.velocity[axis]); }
void OnChatMessageReceived(const std::string& sMessage, const LWOOBJID sender);
void OnHit(const LWOOBJID attacker);
// Whether or not to restart at the end of the frame
void RestartAtEndOfFrame() { m_RestartAtEndOfFrame = true; }
@@ -164,7 +187,56 @@ public:
// Decrements the number of strips listening for an attack.
// If this is the last strip removing an attack, it will reset the factions to the default of -1.
void RemoveAttack();
void DoDamage(const LWOOBJID target);
private:
// Degrees per second per unit of behavior speed
static constexpr float BASE_ANGULAR_SPEED = 15.0f;
struct RotationState {
// The rotation that degrees is applied on top of
NiQuaternion base = QuatUtils::IDENTITY;
// Accumulated signed degrees per world axis since base was set
NiPoint3 degrees{};
// degrees at the moment the current rotation on each axis started
NiPoint3 actionStart{};
// Signed degrees per second per world axis. Non-zero means a behavior owns that axis.
NiPoint3 velocity{};
};
struct MoveState {
// Signed units per second along local right, up and forward. Non-zero means a behavior owns that axis.
NiPoint3 velocity{};
// Right, up and forward in world space as of the last velocity update
std::array<NiPoint3, 3> basis{ NiPoint3Constant::UNIT_X, NiPoint3Constant::UNIT_Y, NiPoint3Constant::UNIT_Z };
// Whether the last velocity update came from an active move
bool wasMoving{ false };
// World position of an active StartMoveTo move
std::optional<NiPoint3> target;
// Units per second of the move to target
float targetSpeed{};
// Direction to target as of the last velocity update, zero once there
NiPoint3 targetDirection{};
// Incremented each time StartMoveTo interrupts the active moves
uint32_t interruptCount{};
};
// Sends the client-side angular velocity for the currently active rotation axes.
void SyncAngularVelocity() const;
// Recomputes the linear velocity from the active move axes and the current rotation.
void SyncLinearVelocity();
// Clears all rotation state and makes rotation relative to newBase.
void ResetRotationState(const NiQuaternion& newBase);
// Loads a behavior from the database.
void LoadBehavior(const LWOOBJID behaviorID, const size_t index, const bool isIndexed);
@@ -208,9 +280,12 @@ private:
*/
LWOOBJID m_userModelID;
// The speed at which this model moves
float m_Speed{ 3.0f };
// Whether or not to restart at the end of the frame.
bool m_RestartAtEndOfFrame{ false };
RotationState m_Rotation;
MoveState m_Move;
float m_DamageCooldown { 0.0f };
};

View File

@@ -802,13 +802,13 @@ const std::map<LWOOBJID, LWOOBJID>& PropertyManagementComponent::GetModels() con
return models;
}
void PropertyManagementComponent::OnChatMessageReceived(const std::string& sMessage) const {
void PropertyManagementComponent::OnChatMessageReceived(const std::string& sMessage, const LWOOBJID sender) const {
for (const auto& modelID : models | std::views::keys) {
auto* const model = Game::entityManager->GetEntity(modelID);
if (!model) continue;
auto* const modelComponent = model->GetComponent<ModelComponent>();
if (!modelComponent) continue;
modelComponent->OnChatMessageReceived(sMessage);
modelComponent->OnChatMessageReceived(sMessage, sender);
}
}

View File

@@ -165,7 +165,7 @@ public:
LWOOBJID GetId() const noexcept { return propertyId; }
void OnChatMessageReceived(const std::string& sMessage) const;
void OnChatMessageReceived(const std::string& sMessage, const LWOOBJID sender) const;
private:
/**
* This

View File

@@ -16,6 +16,8 @@
#include "Amf3.h"
SimplePhysicsComponent::SimplePhysicsComponent(Entity* parent, const int32_t componentID) : PhysicsComponent(parent, componentID) {
RegisterMsg(&SimplePhysicsComponent::OnGetAngularVelocity);
RegisterMsg(&SimplePhysicsComponent::OnSetAngularVelocity);
RegisterMsg(&SimplePhysicsComponent::OnGetObjectReportInfo);
m_Position = m_Parent->GetDefaultPosition();
@@ -38,6 +40,7 @@ SimplePhysicsComponent::~SimplePhysicsComponent() {
}
void SimplePhysicsComponent::Update(const float deltaTime) {
// Rotation is driven by ModelComponent; angular velocity is only relayed to clients.
if (m_Velocity == NiPoint3Constant::ZERO) return;
m_Position += m_Velocity * deltaTime;
m_DirtyPosition = true;
@@ -52,8 +55,12 @@ void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
outBitStream.Write(m_DirtyVelocity || bIsInitialUpdate);
if (m_DirtyVelocity || bIsInitialUpdate) {
outBitStream.Write(m_Velocity);
outBitStream.Write(m_AngularVelocity);
outBitStream.Write(m_Velocity.x);
outBitStream.Write(m_Velocity.y);
outBitStream.Write(m_Velocity.z);
outBitStream.Write(m_AngularVelocity.x);
outBitStream.Write(m_AngularVelocity.y);
outBitStream.Write(m_AngularVelocity.z);
m_DirtyVelocity = false;
}
@@ -85,3 +92,16 @@ bool SimplePhysicsComponent::OnGetObjectReportInfo(GameMessages::GetObjectReport
info.PushDebug<AMFStringValue>("Climbable Type") = StringifiedEnum::ToString(m_ClimbableType).data();
return true;
}
bool SimplePhysicsComponent::OnSetAngularVelocity(GameMessages::SetAngularVelocity& setAngVel) {
m_DirtyVelocity |= setAngVel.bForceFlagDirty || (m_AngularVelocity != setAngVel.angVelocity);
m_AngularVelocity = setAngVel.angVelocity;
LOG_DEBUG("Angular velocity is now %f %f %f", m_AngularVelocity.x, m_AngularVelocity.y, m_AngularVelocity.z);
Game::entityManager->SerializeEntity(m_Parent);
return true;
}
bool SimplePhysicsComponent::OnGetAngularVelocity(GameMessages::GetAngularVelocity& getAngVel) {
getAngVel.angVelocity = m_AngularVelocity;
return true;
}

View File

@@ -22,6 +22,11 @@ enum class eClimbableType : int32_t {
CLIMBABLE_TYPE_WALL_STICK
};
namespace GameMessages {
struct SetAngularVelocity;
struct GetAngularVelocity;
}
/**
* Component that serializes locations of entities to the client
@@ -61,6 +66,9 @@ public:
*/
void SetAngularVelocity(const NiPoint3& value) { m_AngularVelocity = value; m_DirtyVelocity = true; }
bool OnSetAngularVelocity(GameMessages::SetAngularVelocity& setAngVel);
bool OnGetAngularVelocity(GameMessages::GetAngularVelocity& getAngVel);
/**
* Returns the physics motion state
* @return the physics motion state