aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 03:54:04 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 03:54:04 +0300
commit683d970343383d63eeb22219080c190769f888b1 (patch)
tree7dd144180881f1ed5656fa7959d2656a485e1be0 /crates
parent0a4aada114bdcce5dba69bdbb88fa0ed44cf6733 (diff)
downloadfparkan-683d970343383d63eeb22219080c190769f888b1.tar.xz
fparkan-683d970343383d63eeb22219080c190769f888b1.zip
fix(movement): carry frame time across route waypoints
Route following now spends the same frame interval on turning and all reachable waypoints. Go, Patrol and pursuit keep their existing speeds and share this step. Stop counting the obsolete per-waypoint time loss. Verified with the corner route regression, workspace checks, release AutoDemo in a window, and two-minute AutoDemo and Single.01 smoke runs. Obstacle avoidance, locomotion animation and comparison with the original remain open.
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/construction.rs2
-rw-r--r--crates/fparkan-runtime/src/lib.rs1
-rw-r--r--crates/fparkan-runtime/src/route_motion.rs103
-rw-r--r--crates/fparkan-runtime/src/unit_motion.rs30
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs76
5 files changed, 115 insertions, 97 deletions
diff --git a/crates/fparkan-runtime/src/construction.rs b/crates/fparkan-runtime/src/construction.rs
index 74206196..d0b4c69e 100644
--- a/crates/fparkan-runtime/src/construction.rs
+++ b/crates/fparkan-runtime/src/construction.rs
@@ -392,7 +392,7 @@ fn destination(
) -> Option<[f32; 3]> {
let [x, y] = xy.map(|x| x as f32);
let position = [x, y, terrain.height_at([x, y]).ok()??];
- crate::unit_patrol::route_points(terrain, from, position).ok()??;
+ crate::route_motion::route_points(terrain, from, position).ok()??;
Some(position)
}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index cf5625fc..a4444860 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -93,6 +93,7 @@ mod production;
mod projectile;
mod research;
mod resource_distribution;
+mod route_motion;
use resource_distribution::advance_behavior_distributor_tick;
mod script_host;
mod script_patrol;
diff --git a/crates/fparkan-runtime/src/route_motion.rs b/crates/fparkan-runtime/src/route_motion.rs
new file mode 100644
index 00000000..3d831fa0
--- /dev/null
+++ b/crates/fparkan-runtime/src/route_motion.rs
@@ -0,0 +1,103 @@
+//! Follow a terrain route with a shared time budget for turning and travel.
+
+use crate::{EngineError, TerrainWorld, TransformState};
+use fparkan_terrain::{NavigationQuery, RouteRequest};
+use std::collections::VecDeque;
+
+pub(super) fn route_points(
+ terrain: &TerrainWorld,
+ from: [f32; 3],
+ goal: [f32; 3],
+) -> Result<Option<VecDeque<[f32; 2]>>, EngineError> {
+ let route = terrain
+ .route(RouteRequest { start: from, goal })
+ .map_err(EngineError::Terrain)?;
+ Ok(route.and_then(|route| {
+ let mut points = route
+ .areas
+ .windows(2)
+ .map(|pair| terrain.route_crossing(pair[0], pair[1]))
+ .collect::<Option<VecDeque<_>>>()?;
+ points.push_back([goal[0], goal[1]]);
+ Some(points)
+ }))
+}
+
+pub(super) fn turn_toward(yaw: f32, [dx, dy]: [f32; 2], seconds: f32) -> (f32, f32) {
+ let goal_yaw = (-dx).atan2(dy);
+ let turn = (goal_yaw - yaw + std::f32::consts::PI).rem_euclid(std::f32::consts::TAU)
+ - std::f32::consts::PI;
+ let turn_budget = 2.0 * seconds;
+ (
+ yaw + turn.clamp(-turn_budget, turn_budget),
+ seconds.min(turn.abs() * 0.5),
+ )
+}
+
+pub(super) fn follow_route(
+ mut pose: TransformState,
+ points: &mut VecDeque<[f32; 2]>,
+ speed: f32,
+ mut seconds: f32,
+ mut height_at: impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>,
+) -> Result<TransformState, EngineError> {
+ let [mut x, mut y, mut z] = pose.position.map(f32::from_bits);
+ while seconds > 0.0 {
+ let Some(&target) = points.front() else { break };
+ let [dx, dy] = [target[0] - x, target[1] - y];
+ let distance = dx.hypot(dy);
+ if distance <= 0.05 {
+ points.pop_front();
+ continue;
+ }
+ let (yaw, turn_time) = turn_toward(f32::from_bits(pose.orientation[2]), [dx, dy], seconds);
+ pose.orientation[2] = yaw.to_bits();
+ seconds -= turn_time;
+ if seconds == 0.0 || speed <= 0.0 {
+ break;
+ }
+ let travel_time = distance / speed;
+ let reached = travel_time <= seconds;
+ let xy = if reached {
+ target
+ } else {
+ let fraction = speed * seconds / distance;
+ [x + dx * fraction, y + dy * fraction]
+ };
+ let Some(ground) = height_at(xy)? else {
+ points.clear();
+ break;
+ };
+ let offset = height_at([x, y])?.map_or(0.0, |height| z - height);
+ [x, y] = xy;
+ z = ground + offset;
+ pose.position = [x, y, z].map(f32::to_bits);
+ if !reached {
+ break;
+ }
+ points.pop_front();
+ seconds -= travel_time;
+ }
+ Ok(pose)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn one_frame_crosses_waypoints_and_spends_time_turning() {
+ let start = TransformState {
+ position: [0.0_f32, 0.0, 2.0].map(f32::to_bits),
+ ..Default::default()
+ };
+ let mut route = VecDeque::from([[0.0, 0.0], [0.0, 1.0], [4.0, 1.0]]);
+ let end = follow_route(start, &mut route, 2.0, 2.0, |[x, y]| Ok(Some(x + y))).unwrap();
+ let [x, y, z] = end.position.map(f32::from_bits);
+ let expected_x = 3.0 - std::f32::consts::FRAC_PI_2;
+ assert!((x - expected_x).abs() < 0.000_01);
+ assert_eq!(y, 1.0);
+ assert!((z - (x + y + 2.0)).abs() < 0.000_01);
+ assert_eq!(route, [[4.0, 1.0]]);
+ }
+}
diff --git a/crates/fparkan-runtime/src/unit_motion.rs b/crates/fparkan-runtime/src/unit_motion.rs
index 101a6208..14c5e848 100644
--- a/crates/fparkan-runtime/src/unit_motion.rs
+++ b/crates/fparkan-runtime/src/unit_motion.rs
@@ -9,9 +9,6 @@ pub struct UnitMotionSample {
pub updates: u64,
pub first_ms: u32,
pub last_ms: u32,
- pub discarded_waypoints: u64,
- /// Sum of unused frame intervals, individually rounded to microseconds.
- pub discarded_time_us: u64,
}
/// Counts persist after orders end or the object leaves the world.
@@ -32,19 +29,6 @@ impl UnitMotionReport {
sample.updates += 1;
sample.last_ms = now;
}
-
- pub(super) fn discard_waypoint_time(&mut self, object: CombatObject, seconds: f32) {
- let time_us = (f64::from(seconds.max(0.0)) * 1_000_000.0).round() as u64;
- if time_us == 0 {
- return;
- }
- let sample = self
- .objects
- .get_mut(&object)
- .expect("recorded motion update");
- sample.discarded_waypoints += 1;
- sample.discarded_time_us += time_us;
- }
}
/// Reads observations shared by the interactive and headless mission paths.
@@ -58,7 +42,7 @@ mod tests {
use super::*;
#[test]
- fn counts_updates_and_unused_time_separately_for_reused_slots() {
+ fn counts_updates_separately_for_reused_slots() {
let object = CombatObject {
slot: 4,
generation: 1,
@@ -70,9 +54,7 @@ mod tests {
};
let mut report = UnitMotionReport::default();
report.update(object, 0);
- report.discard_waypoint_time(object, 0.125);
report.update(object, 500);
- report.discard_waypoint_time(object, 0.0);
report.update(replacement, 1_000);
assert_eq!(
report.objects[&object],
@@ -80,17 +62,14 @@ mod tests {
updates: 2,
first_ms: 0,
last_ms: 500,
- discarded_waypoints: 1,
- discarded_time_us: 125_000,
}
);
assert_eq!(report.objects[&replacement].updates, 1);
- assert_eq!(report.objects[&replacement].discarded_time_us, 0);
}
#[test]
#[ignore = "requires licensed corpus"]
- fn licensed_stock_patrol_reports_controller_updates_and_lost_time() {
+ fn licensed_stock_patrol_reports_controller_updates() {
use crate::{
create, load_gameplay_mission, simulate_mission_report, EngineServices, MissionMode,
MissionRequest, MissionStartupInput,
@@ -127,9 +106,6 @@ mod tests {
.unit_motion
.objects
.values()
- .any(|sample| sample.updates > 1
- && sample.last_ms > sample.first_ms
- && sample.discarded_waypoints > 0
- && sample.discarded_time_us > 0));
+ .any(|sample| sample.updates > 1 && sample.last_ms > sample.first_ms));
}
}
diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs
index 6e875c2a..0cbb4959 100644
--- a/crates/fparkan-runtime/src/unit_patrol.rs
+++ b/crates/fparkan-runtime/src/unit_patrol.rs
@@ -3,13 +3,14 @@ use super::{
boarding::WARBOT_CLASS,
next_crt_rand,
patrol_points::{PatrolPointReport, PatrolPoints},
+ route_motion::{follow_route, route_points, turn_toward},
unit_combat::CombatTarget,
unit_speed::{order_speed, UnitSpeedKind, UnitSpeedReport},
EngineError, LoadedMissionState, MissionDocument, MissionObjectDraft, MissionScriptBundle,
ObjectHandle, TerrainWorld, TransformState, World,
};
use fparkan_script::{Handler15TargetPayload, ScriptDispatchSelector, VarSet};
-use fparkan_terrain::{NavigationQuery, RouteRequest, SurfaceQuery};
+use fparkan_terrain::{NavigationQuery, SurfaceQuery};
use fparkan_world::{identity_metadata, set_transform, transform_state};
use std::collections::{BTreeMap, HashSet, VecDeque};
@@ -543,7 +544,7 @@ impl UnitPatrol {
if let Some(enemy) = enemy.filter(|enemy| enemy.in_range) {
let mut after = before;
let (yaw, _) = turn_toward(
- before,
+ f32::from_bits(before.orientation[2]),
[enemy.center[0] - from[0], enemy.center[1] - from[1]],
seconds,
);
@@ -585,23 +586,9 @@ impl UnitPatrol {
};
unit.retry_ms = now_ms.saturating_add(1_000);
}
- let Some(&target) = unit.points.front() else {
- continue;
- };
- let dx = target[0] - from[0];
- let dy = target[1] - from[1];
- let distance = dx.hypot(dy);
- if distance <= 0.05 {
- unit.points.pop_front();
- state
- .motion_report
- .discard_waypoint_time(motion_object, seconds);
+ if unit.points.is_empty() {
continue;
}
- let mut after = before;
- // shortcut: turn and non-place orders still use the temporary controller.
- let (yaw, turn_seconds) = turn_toward(before, [dx, dy], seconds);
- after.orientation[2] = yaw.to_bits();
let speed = if (place_patrol && enemy.is_none()) || unit.destinations.is_some() {
let kind = if place_patrol {
UnitSpeedKind::PatrolPlace
@@ -624,27 +611,9 @@ impl UnitPatrol {
} else {
9.0
};
- let travel = speed * (seconds - turn_seconds);
- if travel > 0.0 {
- let fraction = (travel / distance).min(1.0);
- let xy = [from[0] + dx * fraction, from[1] + dy * fraction];
- if let Some(ground) = terrain.height_at(xy).map_err(EngineError::Terrain)? {
- let old_ground = terrain
- .height_at([from[0], from[1]])
- .map_err(EngineError::Terrain)?;
- let z = ground + old_ground.map_or(0.0, |height| from[2] - height);
- after.position = [xy[0], xy[1], z].map(f32::to_bits);
- if fraction == 1.0 {
- unit.points.pop_front();
- state.motion_report.discard_waypoint_time(
- motion_object,
- seconds - turn_seconds - distance / speed,
- );
- }
- } else {
- unit.points.clear();
- }
- }
+ let after = follow_route(before, &mut unit.points, speed, seconds, |xy| {
+ terrain.height_at(xy).map_err(EngineError::Terrain)
+ })?;
if after != before {
poses.push((unit.handle, before, after));
}
@@ -854,18 +823,6 @@ fn patrol_limits_new_targets_and_ends_pursuit_at_the_outer_boundary() {
assert!(!patrol.allows_attack([650.0, 902.0, 0.0]));
}
-fn turn_toward(pose: TransformState, [dx, dy]: [f32; 2], seconds: f32) -> (f32, f32) {
- let yaw = f32::from_bits(pose.orientation[2]);
- let goal_yaw = (-dx).atan2(dy);
- let turn = (goal_yaw - yaw + std::f32::consts::PI).rem_euclid(std::f32::consts::TAU)
- - std::f32::consts::PI;
- let turn_budget = 2.0 * seconds;
- (
- yaw + turn.clamp(-turn_budget, turn_budget),
- seconds.min(turn.abs() * 0.5),
- )
-}
-
fn choose_route(
terrain: &TerrainWorld,
from: [f32; 3],
@@ -887,25 +844,6 @@ fn choose_route(
Ok(VecDeque::new())
}
-pub(super) fn route_points(
- terrain: &TerrainWorld,
- from: [f32; 3],
- goal: [f32; 3],
-) -> Result<Option<VecDeque<[f32; 2]>>, EngineError> {
- let route = terrain
- .route(RouteRequest { start: from, goal })
- .map_err(EngineError::Terrain)?;
- Ok(route.and_then(|route| {
- let mut points = route
- .areas
- .windows(2)
- .map(|pair| terrain.route_crossing(pair[0], pair[1]))
- .collect::<Option<VecDeque<_>>>()?;
- points.push_back([goal[0], goal[1]]);
- Some(points)
- }))
-}
-
impl PreparedPatrol {
pub(super) fn apply_poses(&self, world: &mut World) -> Result<(), EngineError> {
// These handles were read during preparation and the world has yet to step.