From 8a512e5c760dd8e11e325a1a1ac6219be3c7baf7 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Sun, 9 Jul 2023 20:38:47 -0700 Subject: [PATCH] MovementAiComponent pass --- dDatabase/Tables/CDPhysicsComponentTable.cpp | 7 +- dGame/dComponents/MovementAIComponent.cpp | 248 +++++-------------- dGame/dComponents/MovementAIComponent.h | 57 ++--- 3 files changed, 94 insertions(+), 218 deletions(-) diff --git a/dDatabase/Tables/CDPhysicsComponentTable.cpp b/dDatabase/Tables/CDPhysicsComponentTable.cpp index bb21ed7f..0a7e2005 100644 --- a/dDatabase/Tables/CDPhysicsComponentTable.cpp +++ b/dDatabase/Tables/CDPhysicsComponentTable.cpp @@ -37,10 +37,7 @@ CDPhysicsComponentTable::~CDPhysicsComponentTable() { } CDPhysicsComponent* CDPhysicsComponentTable::GetByID(unsigned int componentID) { - for (auto e : m_entries) { - if (e.first == componentID) return e.second; - } - - return nullptr; + auto itr = m_entries.find(componentID); + return itr != m_entries.end() ? itr->second : nullptr; } diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index f8a54068..7c97c85a 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -10,10 +10,16 @@ #include "EntityManager.h" #include "SimplePhysicsComponent.h" #include "CDClientManager.h" +#include "Game.h" +#include "dLogger.h" +#include "SimplePhysicsComponent.h" +#include "ControllablePhysicsComponent.h" #include "CDComponentsRegistryTable.h" #include "CDPhysicsComponentTable.h" #include "CDMovementAIComponentTable.h" +#include "Entity.h" +#include "BaseCombatAIComponent.h" std::map MovementAIComponent::m_PhysicsSpeedCache = {}; @@ -23,14 +29,9 @@ MovementAIComponent::MovementAIComponent(Entity* parent, int32_t componentId) : m_BaseCombatAI = nullptr; - m_BaseCombatAI = m_ParentEntity->GetComponent(); - - m_BaseSpeed = GetBaseSpeed(m_ParentEntity->GetLOT()); - - m_NextWaypoint = GetCurrentPosition(); m_Acceleration = 0.4f; m_Interrupted = false; - m_PullPoint = {}; + m_PullPoint = NiPoint3::ZERO; m_HaltDistance = 0; m_Timer = 0; m_CurrentSpeed = 0; @@ -40,22 +41,21 @@ MovementAIComponent::MovementAIComponent(Entity* parent, int32_t componentId) : } void MovementAIComponent::Startup() { - + m_BaseCombatAI = m_ParentEntity->GetComponent(); + m_NextWaypoint = GetCurrentPosition(); } void MovementAIComponent::LoadConfigData() { bool useWanderDB = m_ParentEntity->GetVar(u"usewanderdb"); - if (!useWanderDB) { - const auto wanderOverride = m_ParentEntity->GetVarAs(u"wanderRadius"); + if (useWanderDB) return; + const auto wanderOverride = m_ParentEntity->GetVarAs(u"wanderRadius"); - if (wanderOverride != 0.0f) { - m_Info.wanderRadius = wanderOverride; - } - } + if (wanderOverride != 0.0f) m_Info.wanderRadius = wanderOverride; } void MovementAIComponent::LoadTemplateData() { + m_BaseSpeed = GetBaseSpeed(m_ParentEntity->GetLOT()); if (m_ComponentId == -1) return; auto* movementAiComponentTable = CDClientManager::Instance().GetTable(); auto movementEntries = movementAiComponentTable->Query([this](CDMovementAIComponent entry) {return (entry.id == this->m_ComponentId); }); @@ -88,11 +88,11 @@ void MovementAIComponent::Update(const float deltaTime) { const auto speed = deltaTime * 2.5f; - NiPoint3 velocity; - - velocity.x = (m_PullPoint.x - source.x) * speed; - velocity.y = (m_PullPoint.y - source.y) * speed; - velocity.z = (m_PullPoint.z - source.z) * speed; + NiPoint3 velocity( + (m_PullPoint.x - source.x) * speed, + (m_PullPoint.y - source.y) * speed, + (m_PullPoint.z - source.z) * speed + ); SetPosition(source + velocity); @@ -103,28 +103,24 @@ void MovementAIComponent::Update(const float deltaTime) { return; } - if (AtFinalWaypoint()) // Are we done? - { - return; - } + // Are we done? + if (AtFinalWaypoint()) return; if (m_HaltDistance > 0) { - if (Vector3::DistanceSquared(ApproximateLocation(), GetDestination()) < m_HaltDistance * m_HaltDistance) // Prevent us from hugging the target - { + // Prevent us from hugging the target + if (Vector3::DistanceSquared(ApproximateLocation(), GetDestination()) < m_HaltDistance * m_HaltDistance) { Stop(); return; } } - if (m_Timer > 0) { + if (m_Timer > 0.0f) { m_Timer -= deltaTime; - if (m_Timer > 0) { - return; - } + if (m_Timer > 0.0f) return; - m_Timer = 0; + m_Timer = 0.0f; } const auto source = GetCurrentWaypoint(); @@ -189,10 +185,6 @@ nextAction: EntityManager::Instance()->SerializeEntity(m_ParentEntity); } -const MovementAIInfo& MovementAIComponent::GetInfo() const { - return m_Info; -} - bool MovementAIComponent::AdvanceWaypointIndex() { if (m_PathIndex >= m_CurrentPath.size()) { return false; @@ -204,37 +196,23 @@ bool MovementAIComponent::AdvanceWaypointIndex() { } NiPoint3 MovementAIComponent::GetCurrentWaypoint() const { - if (m_PathIndex >= m_CurrentPath.size()) { - return GetCurrentPosition(); - } - - return m_CurrentPath[m_PathIndex]; -} - -NiPoint3 MovementAIComponent::GetNextWaypoint() const { - return m_NextWaypoint; -} - -NiPoint3 MovementAIComponent::GetCurrentPosition() const { - return m_ParentEntity->GetPosition(); + return m_PathIndex >= m_CurrentPath.size() ? GetCurrentPosition() : m_CurrentPath[m_PathIndex]; } NiPoint3 MovementAIComponent::ApproximateLocation() const { auto source = GetCurrentPosition(); - if (m_Done) { - return source; - } + if (m_Done) return source; auto destination = m_NextWaypoint; - auto factor = m_TotalTime > 0 ? (m_TotalTime - m_Timer) / m_TotalTime : 0; + auto factor = m_TotalTime > 0.0f ? (m_TotalTime - m_Timer) / m_TotalTime : 0.0f; - auto x = source.x + factor * (destination.x - source.x); - auto y = source.y + factor * (destination.y - source.y); - auto z = source.z + factor * (destination.z - source.z); - - NiPoint3 approximation = NiPoint3(x, y, z); + NiPoint3 approximation = NiPoint3( + source.x + factor * (destination.x - source.x), + source.y + factor * (destination.y - source.y), + source.z + factor * (destination.z - source.z) + ); if (dpWorld::Instance().IsLoaded()) { approximation.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(approximation); @@ -263,32 +241,23 @@ bool MovementAIComponent::Warp(const NiPoint3& point) { return true; } -float MovementAIComponent::GetTimer() const { - return m_Timer; -} - -bool MovementAIComponent::AtFinalWaypoint() const { - return m_Done; -} - void MovementAIComponent::Stop() { - if (m_Done) { - return; - } + if (m_Done) return; SetPosition(ApproximateLocation()); SetVelocity(NiPoint3::ZERO); - m_TotalTime = m_Timer = 0; + m_TotalTime = 0.0f; + m_Timer = 0.0f; m_Done = true; - m_CurrentPath = {}; + m_CurrentPath.clear(); m_PathIndex = 0; - m_CurrentSpeed = 0; + m_CurrentSpeed = 0.0f; EntityManager::Instance()->SerializeEntity(m_ParentEntity); } @@ -300,11 +269,9 @@ void MovementAIComponent::PullToPoint(const NiPoint3& point) { m_PullPoint = point; } -void MovementAIComponent::SetPath(std::vector path) { - std::reverse(path.begin(), path.end()); - - for (const auto& point : path) { - m_Queue.push(point); +void MovementAIComponent::SetPath(const std::vector& path) { + for (auto itr = path.rbegin(); itr != path.rend(); ++itr) { + m_Queue.push(*itr); } SetDestination(m_Queue.top()); @@ -312,7 +279,7 @@ void MovementAIComponent::SetPath(std::vector path) { m_Queue.pop(); } -float MovementAIComponent::GetBaseSpeed(LOT lot) { +float MovementAIComponent::GetBaseSpeed(const LOT lot) { // Check if the lot is in the cache const auto& it = m_PhysicsSpeedCache.find(lot); @@ -320,30 +287,19 @@ float MovementAIComponent::GetBaseSpeed(LOT lot) { return it->second; } - CDComponentsRegistryTable* componentRegistryTable = CDClientManager::Instance().GetTable(); - CDPhysicsComponentTable* physicsComponentTable = CDClientManager::Instance().GetTable(); + auto* componentRegistryTable = CDClientManager::Instance().GetTable(); + auto* physicsComponentTable = CDClientManager::Instance().GetTable(); int32_t componentID; CDPhysicsComponent* physicsComponent = nullptr; componentID = componentRegistryTable->GetByIDAndType(lot, eReplicaComponentType::CONTROLLABLE_PHYSICS, -1); - if (componentID != -1) { - physicsComponent = physicsComponentTable->GetByID(componentID); - - goto foundComponent; + if (componentID == -1) { + componentID = componentRegistryTable->GetByIDAndType(lot, eReplicaComponentType::SIMPLE_PHYSICS, -1); } - componentID = componentRegistryTable->GetByIDAndType(lot, eReplicaComponentType::SIMPLE_PHYSICS, -1); - - if (componentID != -1) { - physicsComponent = physicsComponentTable->GetByID(componentID); - - goto foundComponent; - } - -foundComponent: - + physicsComponent = physicsComponentTable->GetByID(componentID); // Client defaults speed to 10 and if the speed is also null in the table, it defaults to 10. float speed = 10.0f; @@ -358,46 +314,18 @@ foundComponent: return speed; } -void MovementAIComponent::SetPosition(const NiPoint3& value) { - auto* controllablePhysicsComponent = m_ParentEntity->GetComponent(); - - if (controllablePhysicsComponent != nullptr) { - controllablePhysicsComponent->SetPosition(value); - - return; - } - - auto* simplePhysicsComponent = m_ParentEntity->GetComponent(); - - if (simplePhysicsComponent != nullptr) { - simplePhysicsComponent->SetPosition(value); - } +void MovementAIComponent::SetPosition(const NiPoint3& value) const { + m_ParentEntity->SetPosition(value); } -void MovementAIComponent::SetRotation(const NiQuaternion& value) { - if (m_LockRotation) { - return; - } - - auto* controllablePhysicsComponent = m_ParentEntity->GetComponent(); - - if (controllablePhysicsComponent != nullptr) { - controllablePhysicsComponent->SetRotation(value); - - return; - } - - auto* simplePhysicsComponent = m_ParentEntity->GetComponent(); - - if (simplePhysicsComponent != nullptr) { - simplePhysicsComponent->SetRotation(value); - } +void MovementAIComponent::SetRotation(const NiQuaternion& value) const { + if (!m_LockRotation) m_ParentEntity->SetRotation(value); } -void MovementAIComponent::SetVelocity(const NiPoint3& value) { +void MovementAIComponent::SetVelocity(const NiPoint3& value) const { auto* controllablePhysicsComponent = m_ParentEntity->GetComponent(); - if (controllablePhysicsComponent != nullptr) { + if (controllablePhysicsComponent) { controllablePhysicsComponent->SetVelocity(value); return; @@ -405,26 +333,17 @@ void MovementAIComponent::SetVelocity(const NiPoint3& value) { auto* simplePhysicsComponent = m_ParentEntity->GetComponent(); - if (simplePhysicsComponent != nullptr) { + if (simplePhysicsComponent) { simplePhysicsComponent->SetVelocity(value); } } void MovementAIComponent::SetDestination(const NiPoint3& value) { - if (m_Interrupted) { - return; - } - - /*if (Vector3::DistanceSquared(value, GetDestination()) < 2 * 2) - { - return; - }*/ + if (m_Interrupted) return; const auto location = ApproximateLocation(); - if (!AtFinalWaypoint()) { - SetPosition(location); - } + if (!AtFinalWaypoint()) SetPosition(location); std::vector computedPath; @@ -446,17 +365,15 @@ void MovementAIComponent::SetDestination(const NiPoint3& value) { } } - if (computedPath.empty()) // Somehow failed - { - return; - } + // Somehow failed + if (computedPath.empty()) return; m_CurrentPath.clear(); m_CurrentPath.push_back(location); // Simply path - for (auto point : computedPath) { + for (auto& point : computedPath) { if (dpWorld::Instance().IsLoaded()) { point.y = dpWorld::Instance().GetNavMesh()->GetHeightAtPoint(point); } @@ -464,21 +381,18 @@ void MovementAIComponent::SetDestination(const NiPoint3& value) { m_CurrentPath.push_back(point); } - m_CurrentPath.push_back(computedPath[computedPath.size() - 1]); + m_CurrentPath.push_back(computedPath.back()); m_PathIndex = 0; - m_TotalTime = m_Timer = 0; + m_TotalTime = 0.0f; + m_Timer = 0.0f; m_Done = false; } NiPoint3 MovementAIComponent::GetDestination() const { - if (m_CurrentPath.empty()) { - return GetCurrentPosition(); - } - - return m_CurrentPath[m_CurrentPath.size() - 1]; + return m_CurrentPath.empty() ? GetCurrentPosition() : m_CurrentPath.back(); } void MovementAIComponent::SetSpeed(const float value) { @@ -486,38 +400,6 @@ void MovementAIComponent::SetSpeed(const float value) { m_Acceleration = value / 5; } -float MovementAIComponent::GetSpeed() const { - return m_Speed; -} - -void MovementAIComponent::SetAcceleration(const float value) { - m_Acceleration = value; -} - -float MovementAIComponent::GetAcceleration() const { - return m_Acceleration; -} - -void MovementAIComponent::SetHaltDistance(const float value) { - m_HaltDistance = value; -} - -float MovementAIComponent::GetHaltDistance() const { - return m_HaltDistance; -} - -void MovementAIComponent::SetCurrentSpeed(float value) { - m_CurrentSpeed = value; -} - -float MovementAIComponent::GetCurrentSpeed() const { - return m_CurrentSpeed; -} - -void MovementAIComponent::SetLockRotation(bool value) { - m_LockRotation = value; -} - -bool MovementAIComponent::GetLockRotation() const { - return m_LockRotation; +NiPoint3 MovementAIComponent::GetCurrentPosition() const { + return m_ParentEntity->GetPosition(); } diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index 82689ba7..51f77bfa 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -3,18 +3,16 @@ * Copyright 2018 */ -#ifndef MOVEMENTAICOMPONENT_H -#define MOVEMENTAICOMPONENT_H +#ifndef __MOVEMENTAICOMPONENT_H__ +#define __MOVEMENTAICOMPONENT_H__ +#pragma once + +#include +#include +#include -#include "BitStream.h" -#include "Entity.h" -#include "GameMessages.h" -#include "EntityManager.h" -#include "Game.h" -#include "dLogger.h" #include "Component.h" #include "eReplicaComponentType.h" -#include class ControllablePhysicsComponent; class BaseCombatAIComponent; @@ -23,7 +21,6 @@ class BaseCombatAIComponent; * Information that describes the different variables used to make an entity move around */ struct MovementAIInfo { - // copy assignment MovementAIInfo& operator=(const MovementAIInfo& other) = default; @@ -74,7 +71,7 @@ public: * Returns the basic settings that this entity uses to move around * @return the basic settings that this entity uses to move around */ - const MovementAIInfo& GetInfo() const; + const MovementAIInfo& GetInfo() const { return m_Info; }; /** * Set a destination point for the entity to move towards @@ -92,61 +89,61 @@ public: * Sets the max speed at which this entity may run * @param value the speed value to set */ - void SetSpeed(float value); + void SetSpeed(const float value); /** * Returns the max speed at which this entity may run * @return the max speed at which this entity may run */ - float GetSpeed() const; + float GetSpeed() const { return m_Speed; }; /** * Sets how fast the entity will accelerate when not running at full speed * @param value the acceleration to set */ - void SetAcceleration(float value); + void SetAcceleration(const float value) { m_Acceleration = value; } /** * Returns the current speed at which this entity accelerates when not running at full speed * @return the current speed at which this entity accelerates when not running at full speed */ - float GetAcceleration() const; + float GetAcceleration() const { return m_Acceleration; }; /** * Sets the halting distance (the distance at which we consider the target to be reached) * @param value the halting distance to set */ - void SetHaltDistance(float value); + void SetHaltDistance(const float value) { m_HaltDistance = value; } /** * Returns the current halting distance (the distance at which we consider the target to be reached) * @return the current halting distance */ - float GetHaltDistance() const; + float GetHaltDistance() const { return m_HaltDistance; }; /** * Sets the speed the entity is currently running at * @param value the speed value to set */ - void SetCurrentSpeed(float value); + void SetCurrentSpeed(const float value) { m_CurrentSpeed = value; } /** * Returns the speed the entity is currently running at * @return the speed the entity is currently running at */ - float GetCurrentSpeed() const; + float GetCurrentSpeed() const { return m_CurrentSpeed; }; /** * Locks the rotation of this entity in place, depending on the argument * @param value if true, the entity will be rotationally locked */ - void SetLockRotation(bool value); + void SetLockRotation(const bool value) { m_LockRotation = value; } /** * Returns whether this entity is currently rotationally locked * @return true if the entity is rotationally locked, false otherwise */ - bool GetLockRotation() const; + bool GetLockRotation() const { return m_LockRotation; }; /** * Attempts to update the waypoint index, making the entity move to the next waypoint @@ -164,7 +161,7 @@ public: * Returns the waypoint this entity is supposed to move towards next * @return the waypoint this entity is supposed to move towards next */ - NiPoint3 GetNextWaypoint() const; + NiPoint3 GetNextWaypoint() const { return m_NextWaypoint; }; /** * Returns the current position of this entity @@ -190,13 +187,13 @@ public: * Returns the time it will take to reach the final waypoint according to the current speed * @return the time it will take to reach the final waypoint according to the current speed */ - float GetTimer() const; + float GetTimer() const { return m_Timer; }; /** * Returns if the entity is at its final waypoint * @return if the entity is at its final waypoint */ - bool AtFinalWaypoint() const; + bool AtFinalWaypoint() const { return m_Done; }; /** * Renders the entity stationary @@ -214,14 +211,14 @@ public: * Sets a path to follow for the AI * @param path the path to follow */ - void SetPath(std::vector path); + void SetPath(const std::vector& path); /** * Returns the base speed from the DB for a given LOT * @param lot the lot to check for * @return the base speed of the lot */ - static float GetBaseSpeed(LOT lot); + static float GetBaseSpeed(const LOT lot); void SetMoveInfo(const MovementAIInfo& value); private: @@ -230,19 +227,19 @@ private: * Sets the current position of the entity * @param value the position to set */ - void SetPosition(const NiPoint3& value); + void SetPosition(const NiPoint3& value) const; /** * Sets the current rotation of the entity * @param value the rotation to set */ - void SetRotation(const NiQuaternion& value); + void SetRotation(const NiQuaternion& value) const; /** * Sets the current velocity of the entityes * @param value the velocity to set */ - void SetVelocity(const NiPoint3& value); + void SetVelocity(const NiPoint3& value) const; /** * Base information regarding the movement information for this entity @@ -337,4 +334,4 @@ private: int32_t m_ComponentId; }; -#endif // MOVEMENTAICOMPONENT_H +#endif // __MOVEMENTAICOMPONENT_H__