From bce87aaa062bce78d7521d19eddd90ae82aa9211 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Sun, 6 Aug 2023 20:25:22 -0700 Subject: [PATCH] use indexes for pathing --- dGame/dComponents/MovementAIComponent.cpp | 24 +++++++++++-------- dGame/dComponents/MovementAIComponent.h | 11 ++++++++- .../ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp | 6 ++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 1d386de3..c2873f99 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -47,6 +47,7 @@ MovementAIComponent::MovementAIComponent(Entity* parent, MovementAIInfo info) : m_TimeTravelled = 0; m_CurrentSpeed = 0; m_MaxSpeed = 0; + m_CurrentPathWaypointIndex = 0; m_LockRotation = false; } @@ -123,14 +124,11 @@ void MovementAIComponent::Update(const float deltaTime) { SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint)); } else { // Check if there are more waypoints in the queue, if so set our next destination to the next waypoint - if (m_CurrentPath.empty()) { + if (AdvancePathWaypointIndex()) { Stop(); - return; } - SetDestination(m_CurrentPath.top()); - - m_CurrentPath.pop(); + SetDestination(GetCurrentPathWaypoint()); } nextAction: @@ -140,6 +138,11 @@ nextAction: Game::entityManager->SerializeEntity(m_Parent); } +bool MovementAIComponent::AdvancePathWaypointIndex() { + if (m_CurrentPathWaypointIndex < m_CurrentPath.size()) m_CurrentPathWaypointIndex++; + return m_CurrentPathWaypointIndex < m_CurrentPath.size(); +} + const MovementAIInfo& MovementAIComponent::GetInfo() const { return m_Info; } @@ -209,11 +212,12 @@ void MovementAIComponent::Stop() { m_AtFinalWaypoint = true; m_InterpolatedWaypoints.clear(); - while (!m_CurrentPath.empty()) m_CurrentPath.pop(); + m_CurrentPath.clear(); m_PathIndex = 0; m_CurrentSpeed = 0; + m_CurrentPathWaypointIndex = 0; Game::entityManager->SerializeEntity(m_Parent); } @@ -227,11 +231,11 @@ void MovementAIComponent::PullToPoint(const NiPoint3& point) { void MovementAIComponent::SetPath(std::vector path) { if (path.empty()) return; - std::for_each(path.rbegin(), path.rend() - 1, [this](const NiPoint3& point) { - this->m_CurrentPath.push(point); - }); + m_CurrentPath = path; - SetDestination(path.front()); + m_CurrentPathWaypointIndex = 0; + + SetDestination(m_CurrentPath.front()); } float MovementAIComponent::GetBaseSpeed(LOT lot) { diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index 4a4e4c0a..cc63a9ea 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -191,6 +191,10 @@ public: */ void SetPath(std::vector path); + bool AdvancePathWaypointIndex(); + + const NiPoint3& GetCurrentPathWaypoint() const { return m_CurrentPathWaypointIndex < m_CurrentPath.size() ? m_CurrentPath.at(m_CurrentPathWaypointIndex) : m_Parent->GetPosition(); } + /** * Returns the base speed from the DB for a given LOT * @param lot the lot to check for @@ -301,7 +305,12 @@ private: /** * The path from the current position to the destination. */ - std::stack m_CurrentPath; + std::vector m_CurrentPath; + + /** + * The index of the current waypoint in the path + */ + uint32_t m_CurrentPathWaypointIndex; }; #endif // MOVEMENTAICOMPONENT_H diff --git a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp index 66171b95..c18ebb9f 100644 --- a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp +++ b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp @@ -301,9 +301,9 @@ void SGCannon::OnActivityTimerDone(Entity* self, const std::string& name) { pathWaypoints.push_back(waypoint.position); } - if (GeneralUtils::GenerateRandomNumber(0, 1) < 0.5f) { - std::reverse(pathWaypoints.begin(), pathWaypoints.end()); - } + // if (GeneralUtils::GenerateRandomNumber(0, 1) < 0.5f) { + // std::reverse(pathWaypoints.begin(), pathWaypoints.end()); + // } movementAI->SetPath(pathWaypoints);