From e158fc4edeea986eee9ab52023e38d3a79d10858 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Tue, 10 Nov 2026 19:42:17 -0600 Subject: [PATCH] fix(movement): a bounce path turns round at its ends without reaching them twice PathFollowWaypointsHelper goes on to waypoint 1, or to the one before the last, when it runs off an end of a bounce path. Here the turned path started with the waypoint the object stood on, so that waypoint was reached again at once and its commands ran twice. Co-Authored-By: Claude Fable 5.2 --- dGame/dComponents/MovementAIComponent.cpp | 5 +++++ .../dComponentsTests/MovementAIPathTests.cpp | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 15c83f886..0fd9569f1 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -259,7 +259,12 @@ void MovementAIComponent::Run(const float deltaTime) { m_IsBounced = !m_IsBounced; std::vector waypoints = m_Path->pathWaypoints; if (m_IsBounced) std::ranges::reverse(waypoints); + // It turns round at the end: on to the waypoint before it, not to the end again + // (PathFollowWaypointsHelper: waypoint 1, or count - 2) + const bool turns = waypoints.size() > 1; + if (turns) waypoints.erase(waypoints.begin()); SetPath(waypoints); + if (turns && !m_IsBounced) m_CurrentPathStart = 1; } else if (m_Path->pathBehavior == PathBehavior::Once) { // In this case we intended to follow a path and once we've followed it we camp there, otherwise we'd just wander home again. Stop(); diff --git a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp index 8d2073b5d..bcb244cac 100644 --- a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp @@ -121,3 +121,18 @@ TEST_F(MovementAIPathTest, ChangeWaypointCommandWithoutAWaypointStartsAtTheFirst EXPECT_EQ(reached, (std::vector{ 0, 0, 1, 2 })); } + +// PathFollowWaypointsHelper turns round at an end: the end waypoint is reached once, then the one before it +TEST_F(MovementAIPathTest, BouncePathReachesEachEndOnce) { + AddPath(MakePath("BouncePath", 3, PathBehavior::Bounce)); + Entity entity(304, info); + auto* const movement = Walker(entity); + movement->SetPath("BouncePath"); + Walk(*movement, 120.0f); + + ASSERT_GE(reached.size(), 9u); + reached.resize(9); + EXPECT_EQ(reached, (std::vector{ 0, 1, 2, 1, 0, 1, 2, 1, 0 })); + const std::vector expectedX{ 10.0f, 20.0f, 30.0f, 20.0f, 10.0f, 20.0f, 30.0f, 20.0f, 10.0f }; + for (size_t i = 0; i < expectedX.size(); i++) EXPECT_NEAR(reachedX[i], expectedX[i], 0.01f) << i; +}