diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 20 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_status.rs | 69 |
2 files changed, 89 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 6de6b386..24b6f281 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -48,6 +48,7 @@ mod mesh_collision; mod mining; mod mission_events; mod player_motion; +mod player_status; mod player_weapon; mod production; mod projectile; @@ -68,6 +69,7 @@ pub use mission_events::{ MissionEventTick, MissionNotice, }; pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep}; +pub use player_status::{player_status, PlayerStatus}; pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons}; pub use production::order_factory_robot; pub use projectile::{ @@ -18125,6 +18127,23 @@ mod tests { .autodemo_combat_events .is_empty()); let hero = starting_cockpit(&engine).unwrap(); + let status = player_status(&engine, hero.handle).unwrap(); + assert_eq!(status.health_percent, Some(100)); + let table = loaded_mission_control_device_tables(&engine) + .unwrap() + .iter() + .find(|table| table.handle == hero.handle) + .unwrap(); + let root = control_node_provider_row(table, 0).unwrap(); + let quarter = f32::from_bits(root.initial_amount_bits) * 0.25; + let changed = apply_projectile_hit(&mut engine, hero.handle, quarter) + .unwrap() + .expect("hero damage provider"); + assert!(!changed.1); + assert_eq!( + player_status(&engine, hero.handle).unwrap().health_percent, + Some(75) + ); let enemy = mission_object_handle(&engine, OriginalObjectId(11)).unwrap(); let enemy_pose = transform_state(&engine.world, enemy).unwrap(); let [x, y, z] = enemy_pose.position.map(f32::from_bits); @@ -18160,6 +18179,7 @@ mod tests { enemy_pose.position ); assert!(transform_state(&engine.world, hero.handle).is_err()); + assert_eq!(player_status(&engine, hero.handle), None); assert_eq!(failures, 1); assert_eq!( loaded_mission_script_outcome(&engine), diff --git a/crates/fparkan-runtime/src/player_status.rs b/crates/fparkan-runtime/src/player_status.rs new file mode 100644 index 00000000..52af212c --- /dev/null +++ b/crates/fparkan-runtime/src/player_status.rs @@ -0,0 +1,69 @@ +use crate::{Engine, MissionControlNodeProviderTableStatus}; +use fparkan_world::{identity_metadata, ObjectHandle}; + +/// Current values for the player's controlled unit and clan. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PlayerStatus { + /// Remaining root Control capacity as a percentage. + pub health_percent: Option<u32>, + /// Ore stored in the clan's buffer. + pub ore_in_buffer: Option<f32>, + /// Available power and total power demand. + pub power: Option<(f32, f32)>, +} + +/// Reads the values used by combat and the clan's current distributor. +#[must_use] +pub fn player_status(engine: &Engine, controlled: ObjectHandle) -> Option<PlayerStatus> { + let mission = engine.loaded.as_ref()?; + let owner = identity_metadata(&engine.world, controlled) + .ok()? + .owner_id?; + let health_percent = mission + .control_device_tables + .iter() + .find(|table| table.handle == controlled) + .and_then(|table| { + let MissionControlNodeProviderTableStatus::Available(rows) = + &table.node_provider_status + else { + return None; + }; + let root = rows.first()?; + health_percent( + f32::from_bits(root.current_amount_bits), + f32::from_bits(root.initial_amount_bits), + ) + }); + let resources = mission + .script_bundles + .iter() + .find(|bundle| bundle.clan_index == usize::from(owner.0)) + .and_then(|bundle| bundle.behavior_warm_start); + Some(PlayerStatus { + health_percent, + ore_in_buffer: resources.map(|state| f32::from_bits(state.cleared_words[2])), + power: resources.map(|state| { + let [available, _, _, demand, _] = state.cleared_words.map(f32::from_bits); + (available, demand) + }), + }) +} + +fn health_percent(current: f32, initial: f32) -> Option<u32> { + (current.is_finite() && initial.is_finite() && initial > 0.0) + .then(|| (100.0 * (current / initial).clamp(0.0, 1.0)).round() as u32) +} + +#[cfg(test)] +mod tests { + use super::health_percent; + + #[test] + fn reports_current_capacity_within_display_range() { + assert_eq!(health_percent(23.0, 80.0), Some(29)); + assert_eq!(health_percent(90.0, 80.0), Some(100)); + assert_eq!(health_percent(-5.0, 80.0), Some(0)); + assert_eq!(health_percent(0.0, 0.0), None); + } +} |
