diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 05:17:14 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 05:17:14 +0300 |
| commit | fbaf44e97a413e1705b1dfb011849e19d4b21fa5 (patch) | |
| tree | a40d7f32a72ceae2a09080df01b99dfa6e6326c1 /crates | |
| parent | 81eb7cd67af54780756a98475145a864b72cbee0 (diff) | |
| download | fparkan-fbaf44e97a413e1705b1dfb011849e19d4b21fa5.tar.xz fparkan-fbaf44e97a413e1705b1dfb011849e19d4b21fa5.zip | |
fix(movement): drive tread material cycles from chassis travel
Bind animated tread batches to their individual hull displacements. Preserve the authored phase sequence and derive nominal cycle length from the Control speed, scale and MAT0 duration.
Checked stock tracked models in the normal AutoDemo window with Vulkan validation clean. Signed travel, stop and respawn are covered by a unit test. Per-track turning and physical cycle length are approximate.
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/chassis_profile.rs | 33 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/tread_animation.rs | 83 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_chassis.rs | 22 |
4 files changed, 122 insertions, 20 deletions
diff --git a/crates/fparkan-assets/src/chassis_profile.rs b/crates/fparkan-assets/src/chassis_profile.rs index e750cff0..9eef4478 100644 --- a/crates/fparkan-assets/src/chassis_profile.rs +++ b/crates/fparkan-assets/src/chassis_profile.rs @@ -7,6 +7,8 @@ pub struct ChassisFlags { pub can_fly: Option<bool>, /// The chassis uses legs to move. pub walk_chassis: Option<bool>, + /// The movement class declared by the chassis profile. + pub chassis_type: Option<u32>, } /// Reads the bound `CanFly` boolean from a native chassis VAR profile. @@ -33,14 +35,18 @@ pub fn decode_chassis_flags(bytes: &[u8]) -> Result<ChassisFlags, DecodeError> { let name = read_lp_bytes(&mut cursor, 64 * 1024)?; read_lp_bytes(&mut cursor, 64 * 1024)?; cursor.read_exact(size as usize)?; - if size == 4 && kind == 2 { - let value = cursor.read_u32_le()? == 1; - match name.split(|byte| *byte == 0).next().unwrap_or_default() { - b"CanFly" => { - flags.can_fly.get_or_insert(value); + if size == 4 { + let value = cursor.read_u32_le()?; + let name = name.split(|byte| *byte == 0).next().unwrap_or_default(); + match (kind, name) { + (2, b"CanFly") => { + flags.can_fly.get_or_insert(value == 1); } - b"WalkChassis" => { - flags.walk_chassis.get_or_insert(value); + (2, b"WalkChassis") => { + flags.walk_chassis.get_or_insert(value == 1); + } + (5, b"ChassisType") => { + flags.chassis_type.get_or_insert(value); } _ => {} } @@ -59,13 +65,17 @@ mod tests { #[test] fn uses_the_bound_boolean_instead_of_its_default() { - let mut profile = 2_u32.to_le_bytes().to_vec(); - for (name, value) in [("CanFly", 1), ("WalkChassis\0", 0)] { - for word in [4_u32, 2, 0, name.len() as u32] { + let mut profile = 3_u32.to_le_bytes().to_vec(); + for (name, kind, default, value) in [ + ("CanFly", 2, 0, 1), + ("WalkChassis\0", 2, 1, 0), + ("ChassisType", 5, 2, 4), + ] { + for word in [4_u32, kind, 0, name.len() as u32] { profile.extend(word.to_le_bytes()); } profile.extend(name.as_bytes()); - for word in [0_u32, 1 - value, value, 0, 1] { + for word in [0_u32, default, value, 0, 1] { profile.extend(word.to_le_bytes()); } } @@ -75,6 +85,7 @@ mod tests { Ok(ChassisFlags { can_fly: Some(true), walk_chassis: Some(false), + chassis_type: Some(4), }) ); profile.truncate(profile.len() - 1); diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index dda56734..627714ba 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -102,12 +102,14 @@ use resource_distribution::advance_behavior_distributor_tick; mod script_host; mod script_patrol; mod script_time; +mod tread_animation; +pub use tread_animation::TreadAnimation; mod turret_aim; pub use turret_aim::{ apply_autonomous_turret_pose, autonomous_turret_aim, has_turret_aim, TurretAim, }; mod unit_chassis; -pub use unit_chassis::loaded_walk_chassis; +pub use unit_chassis::{loaded_tracked_chassis, loaded_walk_chassis}; mod unit_combat; mod unit_motion; pub use unit_motion::{loaded_mission_unit_motion_report, UnitMotionReport, UnitMotionSample}; diff --git a/crates/fparkan-runtime/src/tread_animation.rs b/crates/fparkan-runtime/src/tread_animation.rs new file mode 100644 index 00000000..f83f51fc --- /dev/null +++ b/crates/fparkan-runtime/src/tread_animation.rs @@ -0,0 +1,83 @@ +//! Advance a track's authored material cycle from actual hull travel. + +use crate::{ObjectHandle, TransformState}; + +/// Playback position independent of the scene material clock. +#[derive(Clone)] +pub struct TreadAnimation { + reference_speed: f32, + travel_ms: f64, + previous: Option<(ObjectHandle, [f32; 2])>, +} + +impl TreadAnimation { + /// Uses the chassis's unloaded Control forward limit as its nominal rate. + pub fn new(reference_speed: f32) -> Option<Self> { + (reference_speed.is_finite() && reference_speed > 0.0).then_some(Self { + reference_speed, + travel_ms: 0.0, + previous: None, + }) + } + + /// Integrates signed translation along the hull. A retained position + /// leaves the phase unchanged, and a replacement starts a new cycle. + pub fn sample(&mut self, handle: ObjectHandle, pose: TransformState) { + let [x, y, _] = pose.position.map(f32::from_bits); + match self.previous.replace((handle, [x, y])) { + Some((owner, [old_x, old_y])) if owner == handle => { + let yaw = f32::from_bits(pose.orientation[2]); + let forward = (old_x - x) * yaw.sin() + (y - old_y) * yaw.cos(); + let speed = f32::from_bits(pose.scale[1]).abs() * self.reference_speed; + let travel_ms = f64::from(forward) * 1_000.0 / f64::from(speed); + if travel_ms.is_finite() { + self.travel_ms += travel_ms; + } + } + _ => self.travel_ms = 0.0, + } + } + + /// Maps the current travel to the requested nonzero MAT0 cycle duration. + pub fn phase(&self, period_ms: std::num::NonZeroU16) -> f32 { + let period = f64::from(period_ms.get()); + (self.travel_ms.rem_euclid(period) / period) as f32 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn material_phase_uses_signed_travel_and_stops_with_hull() { + let mut animation = TreadAnimation::new(10.0).unwrap(); + let period = std::num::NonZeroU16::new(200).unwrap(); + let handle = ObjectHandle { + slot: 0, + generation: 1, + }; + let pose = |y| TransformState { + position: [0.0, y, 0.0].map(f32::to_bits), + scale: [1.0, 2.0, 1.0].map(f32::to_bits), + ..TransformState::default() + }; + animation.sample(handle, pose(0.0)); + animation.sample(handle, pose(1.0)); + assert_eq!(animation.phase(period), 0.25); + animation.sample(handle, pose(1.0)); + assert_eq!(animation.phase(period), 0.25); + animation.sample(handle, pose(0.0)); + assert_eq!(animation.phase(period), 0.0); + animation.sample(handle, pose(-1.0)); + assert_eq!(animation.phase(period), 0.75); + animation.sample( + ObjectHandle { + generation: 2, + ..handle + }, + pose(-2.0), + ); + assert_eq!(animation.phase(period), 0.0); + } +} diff --git a/crates/fparkan-runtime/src/unit_chassis.rs b/crates/fparkan-runtime/src/unit_chassis.rs index 25020a4f..15370c8f 100644 --- a/crates/fparkan-runtime/src/unit_chassis.rs +++ b/crates/fparkan-runtime/src/unit_chassis.rs @@ -32,14 +32,20 @@ pub(super) fn can_fly( /// Returns whether a loaded draft has an explicit walking chassis profile. pub fn loaded_walk_chassis(engine: &Engine, object_index: usize) -> bool { - engine.loaded.as_ref().is_some_and(|mission| { - mission - .object_drafts - .get(object_index) - .and_then(|draft| draft.original_id) - .and_then(|id| mission.unit_chassis_flags.get(&id.0)) - .is_some_and(|flags| flags.walk_chassis == Some(true)) - }) + loaded_flags(engine, object_index).is_some_and(|flags| flags.walk_chassis == Some(true)) +} + +/// Returns whether a loaded draft has a tracked chassis profile. +pub fn loaded_tracked_chassis(engine: &Engine, object_index: usize) -> bool { + const TRACKED_CHASSIS: u32 = 4; + loaded_flags(engine, object_index) + .is_some_and(|flags| flags.chassis_type == Some(TRACKED_CHASSIS)) +} + +fn loaded_flags(engine: &Engine, object_index: usize) -> Option<&ChassisFlags> { + let mission = engine.loaded.as_ref()?; + let id = mission.object_drafts.get(object_index)?.original_id?; + mission.unit_chassis_flags.get(&id.0) } fn load_profile( |
