diff --git a/dGame/dComponents/MovementAIComponent.cpp b/dGame/dComponents/MovementAIComponent.cpp index 0fd9569f1..22d843db0 100644 --- a/dGame/dComponents/MovementAIComponent.cpp +++ b/dGame/dComponents/MovementAIComponent.cpp @@ -162,6 +162,19 @@ void MovementAIComponent::Run(const float deltaTime) { if (m_Paused) return; + if (m_WaitingAtWaypoint) { + if (m_BaseCombatAI && m_BaseCombatAI->GetState() != AiState::idle) { + // A fight ends the wait + m_WaitingAtWaypoint = false; + } else { + m_WaypointDelay -= deltaTime; + if (m_WaypointDelay > 0.0f) return; + m_WaitingAtWaypoint = false; + if (RunWaypointCommands(m_DelayedWaypoint, m_DelayedCommand) && ContinuePath()) Game::entityManager->SerializeEntity(m_Parent); + return; + } + } + auto* const quickBuildComponent = m_Parent->GetComponent(); if (quickBuildComponent && quickBuildComponent->GetState() != eQuickBuildState::COMPLETED) return; @@ -244,50 +257,12 @@ void MovementAIComponent::Run(const float deltaTime) { if (!m_BaseCombatAI || (idleNotResetting)) { // Check if there are more waypoints in the queue, if so set our next destination to the next waypoint const auto waypointNum = m_IsBounced ? m_CurrentPath.size() : m_CurrentPathStart + m_CurrentPathWaypointCount - m_CurrentPath.size() - 1; - RunWaypointCommands(waypointNum); - if (m_PathChangedByCommand) { - // SetPath already has it heading for the new path's start waypoint - m_PathChangedByCommand = false; + if (!RunWaypointCommands(waypointNum)) { + // A delay: it stands here, then goes on with the waypoint's other commands Game::entityManager->SerializeEntity(m_Parent); return; } - if (m_CurrentPath.empty()) { - if (m_Path) { - if (m_Path->pathBehavior == PathBehavior::Loop) { - SetPath(m_Path->pathWaypoints); - } else if (m_Path->pathBehavior == PathBehavior::Bounce) { - 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(); - return; - } - } else { - Stop(); - if (m_FollowedTarget != LWOOBJID_EMPTY) { - GameMessages::GetPosition getPos; - if (!getPos.Send(m_FollowedTarget)) { - LOG("Target %llu does not exist anymore to follow", m_FollowedTarget); - m_FollowedTarget = LWOOBJID_EMPTY; - } else { - SetDestination(getPos.pos); - } - } - return; - } - } else { - SetDestination(m_CurrentPath.top().position); - - m_CurrentPath.pop(); - } + if (!ContinuePath()) return; } else { Stop(); } @@ -296,6 +271,52 @@ void MovementAIComponent::Run(const float deltaTime) { Game::entityManager->SerializeEntity(m_Parent); } +bool MovementAIComponent::ContinuePath() { + if (m_PathChangedByCommand) { + // SetPath already has it heading for the new path's start waypoint + m_PathChangedByCommand = false; + return true; + } + if (m_CurrentPath.empty()) { + if (m_Path) { + if (m_Path->pathBehavior == PathBehavior::Loop) { + SetPath(m_Path->pathWaypoints); + } else if (m_Path->pathBehavior == PathBehavior::Bounce) { + 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(); + return false; + } + } else { + Stop(); + if (m_FollowedTarget != LWOOBJID_EMPTY) { + GameMessages::GetPosition getPos; + if (!getPos.Send(m_FollowedTarget)) { + LOG("Target %llu does not exist anymore to follow", m_FollowedTarget); + m_FollowedTarget = LWOOBJID_EMPTY; + } else { + SetDestination(getPos.pos); + } + } + return false; + } + } else { + SetDestination(m_CurrentPath.top().position); + + m_CurrentPath.pop(); + } + return true; +} + const MovementAIInfo& MovementAIComponent::GetInfo() const { return m_Info; } @@ -388,6 +409,7 @@ void MovementAIComponent::PullToPoint(const NiPoint3& point) { void MovementAIComponent::SetPath(std::vector path) { if (path.empty()) return; m_CurrentPathStart = 0; + m_WaitingAtWaypoint = false; while (!m_CurrentPath.empty()) m_CurrentPath.pop(); std::for_each(path.rbegin(), path.rend() - 1, [this](const PathWaypoint& point) { this->m_CurrentPath.push(point); @@ -561,15 +583,18 @@ void MovementAIComponent::SetMaxSpeed(const float value) { m_Acceleration = value / 5.0f; } -void MovementAIComponent::RunWaypointCommands(uint32_t waypointNum) { - m_Parent->GetScript()->OnWaypointReached(m_Parent, waypointNum); - // A callback may add another - const auto callbacks = m_WaypointReachedCallbacks; - for (const auto& callback : callbacks) callback(waypointNum); +bool MovementAIComponent::RunWaypointCommands(const uint32_t waypointNum, const uint32_t firstCommand) { + if (firstCommand == 0) { + m_Parent->GetScript()->OnWaypointReached(m_Parent, waypointNum); + // A callback may add another + const auto callbacks = m_WaypointReachedCallbacks; + for (const auto& callback : callbacks) callback(waypointNum); + } - if (!m_Path || waypointNum >= m_Path->pathWaypoints.size()) return; + if (!m_Path || waypointNum >= m_Path->pathWaypoints.size()) return true; const auto& commands = m_Path->pathWaypoints[waypointNum].commands; - for (const auto& [command, data] : commands) { + for (uint32_t i = firstCommand; i < commands.size(); i++) { + const auto& [command, data] = commands[i]; LOG_DEBUG("%s %s %s", StringifiedEnum::ToString(command).data(), m_Path->pathName.c_str(), data.c_str()); switch (command) { case eWaypointCommandType::INVALID: break; @@ -604,8 +629,14 @@ void MovementAIComponent::RunWaypointCommands(uint32_t waypointNum) { break; } case eWaypointCommandType::DELAY: { - // Pause(GeneralUtils::TryParse(data).value_or(0.0f)); - break; + // The commands after it wait until the delay is over (LWOMovementAIComponent::ProcessDelayAction + // sets the delay and stops LWOMovementAIComponent::msgArrived at this command) + m_WaypointDelay = GeneralUtils::TryParse(data).value_or(0.0f); + m_DelayedWaypoint = waypointNum; + m_DelayedCommand = i + 1; + m_WaitingAtWaypoint = true; + SetVelocity(NiPoint3Constant::ZERO); + return false; } case eWaypointCommandType::EMOTE: { // m_Delay = RenderComponent::GetAnimationTime(m_Parent, data); @@ -634,6 +665,7 @@ void MovementAIComponent::RunWaypointCommands(uint32_t waypointNum) { case eWaypointCommandType::PLAY_SOUND: break; } } + return true; } bool MovementAIComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo) { diff --git a/dGame/dComponents/MovementAIComponent.h b/dGame/dComponents/MovementAIComponent.h index 40494d7a8..fcd8df39f 100644 --- a/dGame/dComponents/MovementAIComponent.h +++ b/dGame/dComponents/MovementAIComponent.h @@ -269,10 +269,10 @@ private: float GetGroundHeight(const NiPoint3& point) const; /** - * @brief - * Runs the commands on a waypoint if a path exists + * Runs the commands on a waypoint if a path exists, from one of them on + * @return false when a delay holds the object at the waypoint; the commands after it run once it is over */ - void RunWaypointCommands(uint32_t waypointNum); + bool RunWaypointCommands(uint32_t waypointNum, uint32_t firstCommand = 0); /** * Sets the current position of the entity @@ -393,6 +393,16 @@ private: // The level path's waypoint the walked waypoints start at uint32_t m_CurrentPathStart{ 0 }; + // A waypoint's delay command: the time left, and the waypoint and command to go on with + bool m_WaitingAtWaypoint{ false }; + float m_WaypointDelay{ 0.0f }; + uint32_t m_DelayedWaypoint{ 0 }; + uint32_t m_DelayedCommand{ 0 }; + + // Where to after a waypoint's commands: the next waypoint, the path again, or nowhere. False when nothing is + // left to serialize + bool ContinuePath(); + // A waypoint's changeWP put the object on another path: it heads for that path's start waypoint bool m_PathChangedByCommand{ false }; diff --git a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp index bcb244cac..ea88422c7 100644 --- a/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp @@ -136,3 +136,47 @@ TEST_F(MovementAIPathTest, BouncePathReachesEachEndOnce) { 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; } + +// LWOMovementAIComponent::ProcessDelayAction: the object stands at the waypoint for the delay, and the commands after +// the delay wait for it +TEST_F(MovementAIPathTest, DelayCommandHoldsTheObjectAtTheWaypoint) { + auto path = MakePath("DelayPath", 2, PathBehavior::Once); + path.pathWaypoints[0].commands.push_back({ eWaypointCommandType::DELAY, "5" }); + path.pathWaypoints[0].commands.push_back({ eWaypointCommandType::CHANGE_WAYPOINT, "AfterDelay,1" }); + AddPath(path); + auto after = MakePath("AfterDelay", 2, PathBehavior::Once); + for (auto& waypoint : after.pathWaypoints) waypoint.position.z = 50.0f; + AddPath(after); + + Entity entity(305, info); + auto* const movement = Walker(entity); + movement->SetPath("DelayPath"); + while (reached.empty()) movement->Run(0.05f); + ASSERT_EQ(reached, (std::vector{ 0 })); + + // Four seconds on it has not moved: the command after the delay has not run either + Walk(*movement, 4.0f); + EXPECT_NEAR(entity.GetPosition().x, 10.0f, 0.01f); + EXPECT_NEAR(entity.GetPosition().z, 0.0f, 0.01f); + EXPECT_EQ(movement->GetVelocity(), NiPoint3Constant::ZERO); + + // The delay over, the next command sends it to the other path; the waypoint is not reached a second time + Walk(*movement, 60.0f); + EXPECT_EQ(reached, (std::vector{ 0, 1 })); + EXPECT_NEAR(entity.GetPosition().x, 20.0f, 0.01f); + EXPECT_NEAR(entity.GetPosition().z, 50.0f, 0.01f); +} + +TEST_F(MovementAIPathTest, DelayAtEachEndOfALoopPath) { + auto path = MakePath("DelayLoop", 2, PathBehavior::Loop); + path.pathWaypoints[1].commands.push_back({ eWaypointCommandType::DELAY, "2" }); + AddPath(path); + + Entity entity(306, info); + auto* const movement = Walker(entity); + movement->SetPath("DelayLoop"); + Walk(*movement, 120.0f); + ASSERT_GE(reached.size(), 4u); + reached.resize(4); + EXPECT_EQ(reached, (std::vector{ 0, 1, 0, 1 })); +}