aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/mobile_budget.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 17:52:41 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 17:52:41 +0300
commitf7c5c3bffd565bb6b0995a220ecb7788471b7848 (patch)
tree09c33ae6a42b491035c3f0cf53bdefb604da72ff /crates/fparkan-runtime/src/mobile_budget.rs
parent5f4850555ac2fd62009cffd5e98ea04c081bbaf7 (diff)
downloadfparkan-f7c5c3bffd565bb6b0995a220ecb7788471b7848.tar.xz
fparkan-f7c5c3bffd565bb6b0995a220ecb7788471b7848.zip
fix(control): select authored drive states for mobile power demand
Diffstat (limited to 'crates/fparkan-runtime/src/mobile_budget.rs')
-rw-r--r--crates/fparkan-runtime/src/mobile_budget.rs128
1 files changed, 125 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/mobile_budget.rs b/crates/fparkan-runtime/src/mobile_budget.rs
index 1df2ab80..32e81eae 100644
--- a/crates/fparkan-runtime/src/mobile_budget.rs
+++ b/crates/fparkan-runtime/src/mobile_budget.rs
@@ -7,6 +7,7 @@ use crate::{
ControlResourceAccessError, EffectiveControlDevice, MissionAssets,
MissionControlDeviceTableState, MissionControlDeviceTableStatus, TransformState,
};
+use fparkan_assets::{ControlDriveStates, PreparedControlDrive};
use std::collections::BTreeMap;
#[derive(Clone, Debug)]
@@ -172,6 +173,30 @@ pub(super) fn unmodeled_motion_rate() -> f32 {
0.0
}
+#[derive(Default)]
+pub(super) struct DriveStateSelection {
+ current: usize,
+ destination: Option<usize>,
+}
+
+impl DriveStateSelection {
+ fn advance(&mut self, states: &ControlDriveStates, velocity: [f32; 6]) -> Option<f32> {
+ if let Some(destination) = states.destination(self.current, velocity) {
+ self.destination = Some(destination);
+ }
+ if let Some(destination) = self.destination {
+ let next = states.next_state(self.current, destination);
+ self.destination = next
+ .filter(|&next| next != destination)
+ .map(|_| destination);
+ if let Some(next) = next {
+ self.current = next;
+ }
+ }
+ states.factor(self.current)
+ }
+}
+
#[derive(Clone, Copy)]
pub(super) struct DriveMotion {
local_speed: f32,
@@ -186,6 +211,7 @@ impl DriveMotion {
[previous, current]: [TransformState; 2],
frame_seconds: f32,
can_fly: bool,
+ selection: &mut DriveStateSelection,
) -> Option<Self> {
let object = table.original_id?.0 as usize;
let profile = table.owner_profile.as_ref()?;
@@ -194,7 +220,6 @@ impl DriveMotion {
.get(object)?
.get(profile.unit_component_index)?
.as_ref()?;
- let state_factor = f32::from_bits(control.uniform_drive_factor_bits?);
let limits = profile.original_words[6..9]
.iter()
.map(|&word| f32::from_bits(word));
@@ -204,11 +229,26 @@ impl DriveMotion {
});
let (sin, cos) = f32::from_bits(current.orientation[2]).sin_cos();
// shortcut: use the frame's realized displacement until native motion publishes its local vector.
- let local_speed = largest_component([
+ let local_velocity = [
(cos * dx + sin * dy) / frame_seconds,
(-sin * dx + cos * dy) / frame_seconds,
if can_fly { dz / frame_seconds } else { 0.0 },
- ])?;
+ ];
+ let local_speed = largest_component(local_velocity)?;
+ let state_factor = match &control.drive {
+ PreparedControlDrive::Uniform(bits) => f32::from_bits(*bits),
+ PreparedControlDrive::Automatic(states) => {
+ let [x, y, z] = local_velocity;
+ let [roll, pitch, yaw] = std::array::from_fn(|axis| {
+ let delta = f32::from_bits(current.orientation[axis])
+ - f32::from_bits(previous.orientation[axis]);
+ use std::f32::consts::{PI, TAU};
+ ((delta + PI).rem_euclid(TAU) - PI) / frame_seconds
+ });
+ selection.advance(states, [x, y, z, roll, pitch, yaw])?
+ }
+ PreparedControlDrive::Unavailable => return None,
+ };
(profile_limit > 0.0 && state_factor.is_finite() && !state_factor.is_subnormal()).then_some(
Self {
local_speed,
@@ -441,6 +481,88 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
+ fn licensed_spider_drive_uses_the_authored_gait_and_resource_budget() {
+ use crate::{
+ create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
+ MissionStartupInput,
+ };
+ use fparkan_vfs::DirectoryVfs;
+ use fparkan_world::OriginalObjectId;
+ use std::sync::Arc;
+
+ let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
+ let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
+ load_gameplay_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/Single.01/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+ let mission = engine.loaded.as_ref().unwrap();
+ let control = mission.mission_assets.object_component_controls[58][0]
+ .as_ref()
+ .expect("stock 12spd1 chassis CTL");
+ let PreparedControlDrive::Automatic(states) = &control.drive else {
+ panic!("stock spider selects its drive state using velocity");
+ };
+ let mut selection = DriveStateSelection::default();
+ // Consecutive 0x51c0 calls on the GOG profile, applying the first
+ // queued state after each call as in 0xc317–0xc37a.
+ for (forward, yaw, expected) in [
+ (0.0, 0.0, &[0, 0][..]),
+ (0.5, 0.0, &[0, 0]),
+ (6.0, 0.0, &[15, 16, 15, 16, 15]),
+ (6.0, 0.25, &[16, 15, 16, 15, 16]),
+ (13.0, 0.0, &[19, 20, 21, 22, 19]),
+ (0.0, 0.0, &[20, 21, 22, 0, 0, 0]),
+ (-6.0, 0.0, &[17, 18, 17, 18, 17]),
+ (0.0, 0.0, &[18, 0, 0, 0, 0, 0]),
+ (0.0, 0.25, &[8, 9, 10, 11, 12]),
+ (0.0, 0.0, &[13, 14, 0, 0, 0, 0]),
+ ] {
+ let velocity = [0.0, forward, 0.0, 0.0, 0.0, yaw];
+ for &state in expected {
+ let factor = selection.advance(states, velocity).unwrap();
+ assert_eq!(selection.current, state, "{velocity:?}");
+ assert_eq!(factor, if state == 0 { 0.0 } else { 1.0 });
+ }
+ }
+
+ let mut first_position = None;
+ let mut moving_budget = false;
+ for now in (0..=40_000).step_by(100) {
+ frame(&mut engine, now).unwrap();
+ let mission = engine.loaded.as_ref().unwrap();
+ if let Some(table) = mission.control_device_tables.iter().find(|table| {
+ table.original_id == Some(OriginalObjectId(58))
+ && fparkan_world::transform_state(&engine.world, table.handle).is_ok()
+ }) {
+ let position = fparkan_world::transform_state(&engine.world, table.handle)
+ .unwrap()
+ .position;
+ let initial = first_position.get_or_insert(position);
+ let horizontal_move = position[..2] != initial[..2];
+ moving_budget |=
+ horizontal_move && f32::from_bits(table.current_output_rate_5e0_bits) > 0.0;
+ }
+ }
+ let report = &engine.loaded.as_ref().unwrap().control_resources.mobile;
+ assert!(!report.stubs.contains_key(&(58, "motion_factory_5".into())));
+ assert!(
+ moving_budget,
+ "the first released spider must spend power while walking"
+ );
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
fn licensed_autodemo_flyer_uses_its_drive_budget_in_motion() {
use crate::{
create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,