diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 17:00:05 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 17:00:05 +0300 |
| commit | f3299bc40b6ced7b654255c8391ef7b0ee24aeef (patch) | |
| tree | 9e29faabdf0efbf9dadbc55940dd76c16cc99a54 /crates/fparkan-runtime | |
| parent | 1e0128c6de44aa73e446451a27469e9873bc7e93 (diff) | |
| download | fparkan-f3299bc40b6ced7b654255c8391ef7b0ee24aeef.tar.xz fparkan-f3299bc40b6ced7b654255c8391ef7b0ee24aeef.zip | |
feat(control): switch onboard self-repair from the cockpit
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/repair_system.rs | 80 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/research.rs | 91 |
3 files changed, 152 insertions, 23 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 6150fa44..e3bf3024 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -133,6 +133,7 @@ mod weapon_deployment; pub use projectile_shield::{ collect_shield_impacts, shield_effect_sources, take_shield_impacts, ShieldImpact, }; +mod repair_system; mod repair_zone; mod research; mod resource_distribution; @@ -197,7 +198,8 @@ pub use projectile::{ }; pub use projectile_flight::{ProjectileFlightHit, ProjectileFlightStep}; pub use projectile_parts::{depleted_chassis_nodes, depleted_warbot_nodes}; -pub use research::order_lab_research; +pub use repair_system::toggle_self_repair; +pub use research::{at_research_laboratory, order_lab_research}; pub use robot_designer::{load_gameplay_mission, load_gameplay_mission_with_build_parts}; pub use unit_combat::{ autonomous_firing_effect_phases, AutoDemoCombatEvent, AutoDemoProjectileType, diff --git a/crates/fparkan-runtime/src/repair_system.rs b/crates/fparkan-runtime/src/repair_system.rs new file mode 100644 index 00000000..8fa6c09a --- /dev/null +++ b/crates/fparkan-runtime/src/repair_system.rs @@ -0,0 +1,80 @@ +//! State switch for the controlled unit's Control repair device. + +use crate::{Engine, MissionControlDeviceTableStatus, StartingCockpit}; + +/// Switches the selected unit's self-repair device when it is present. +pub fn toggle_self_repair(engine: &mut Engine, cockpit: StartingCockpit) -> Option<bool> { + let mission = engine.loaded.as_mut()?; + let table = mission + .control_device_tables + .iter_mut() + .find(|table| table.handle == cockpit.handle)?; + let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { + return None; + }; + let device = devices.iter_mut().find(|device| { + device.key == 0 && device.descriptor.tag == 15 && device.factory_tag == 15 + })?; + // Control+0x22c90 handles the inverse switch command 0x40. + let enabled = device.native_word_50_bits? != 0x20; + device.native_word_50_bits = Some(if enabled { 0x20 } else { 0 }); + Some(enabled) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + apply_loaded_mission_control_node_delta, create, frame, load_gameplay_mission, + player_status, starting_cockpit, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_hero_repairs_only_when_switched_on() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + 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 hero = starting_cockpit(&engine).unwrap(); + apply_loaded_mission_control_node_delta(&mut engine, hero.handle, 0, -250.0).unwrap(); + let health = |engine: &Engine| { + player_status(engine, hero.handle) + .and_then(|status| status.health_percent) + .unwrap() + }; + let damaged = health(&engine); + for now in (0..=5_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + assert_eq!(health(&engine), damaged); + assert_eq!(toggle_self_repair(&mut engine, hero), Some(true)); + for now in (5_050..=15_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + let repaired = health(&engine); + assert!( + repaired > damaged && repaired < 100, + "{damaged} -> {repaired}" + ); + assert_eq!(toggle_self_repair(&mut engine, hero), Some(false)); + for now in (15_050..=20_000).step_by(50) { + frame(&mut engine, now).unwrap(); + } + assert_eq!(health(&engine), repaired); + } +} diff --git a/crates/fparkan-runtime/src/research.rs b/crates/fparkan-runtime/src/research.rs index 3510a295..fee17b1f 100644 --- a/crates/fparkan-runtime/src/research.rs +++ b/crates/fparkan-runtime/src/research.rs @@ -81,6 +81,60 @@ impl FreeResearch { } } +fn laboratory_at<'a>( + mission: &'a LoadedMissionState, + world: &World, + owner: OwnerId, + position: [f32; 3], + require_remaining: bool, +) -> Option<(usize, ObjectHandle, &'a MissionObjectDraft)> { + mission + .free_research + .remaining + .iter() + .enumerate() + .find_map(|(slot, &(index, count))| { + if require_remaining && count == 0 { + return None; + } + let draft = &mission.object_drafts[index]; + let lab = handle_by_original_id(world, draft.original_id?)?; + (identity_metadata(world, lab).ok()?.owner_id == Some(owner) + && on_communicator( + &mission.mission_assets, + index, + LAB_CLASS, + transform_state(world, lab).ok()?, + position, + )) + .then_some((slot, lab, draft)) + }) +} + +/// Whether the player is at a friendly communicator registered for free research. +pub fn at_research_laboratory(engine: &Engine, cockpit: StartingCockpit) -> bool { + let Some(mission) = &engine.loaded else { + return false; + }; + let Some(owner) = identity_metadata(&engine.world, cockpit.handle) + .ok() + .and_then(|identity| identity.owner_id) + else { + return false; + }; + let Ok(pose) = transform_state(&engine.world, cockpit.handle) else { + return false; + }; + laboratory_at( + mission, + &engine.world, + owner, + pose.position.map(f32::from_bits), + false, + ) + .is_some() +} + /// Starts the first available free technology at the player's laboratory. /// /// # Errors @@ -104,27 +158,7 @@ pub fn order_lab_research( let position = transform_state(&engine.world, cockpit.handle)? .position .map(f32::from_bits); - let (quota, lab, draft) = mission - .free_research - .remaining - .iter() - .enumerate() - .find_map(|(slot, &(index, count))| { - if count == 0 { - return None; - } - let draft = &mission.object_drafts[index]; - let lab = handle_by_original_id(&engine.world, draft.original_id?)?; - (identity_metadata(&engine.world, lab).ok()?.owner_id == Some(owner) - && on_communicator( - &mission.mission_assets, - index, - LAB_CLASS, - transform_state(&engine.world, lab).ok()?, - position, - )) - .then_some((slot, lab, draft)) - }) + let (quota, lab, draft) = laboratory_at(mission, &engine.world, owner, position, true) .ok_or_else(|| { fail("stand on a friendly laboratory communicator with free research remaining") })?; @@ -205,14 +239,26 @@ mod tests { ) .unwrap(); let hero = starting_cockpit(&engine).unwrap(); + assert!(!at_research_laboratory(&engine, hero)); + let lab = handle_by_original_id(&engine.world, OriginalObjectId(5)).unwrap(); + let floor = crate::mission_events::communicator_triangles( + &engine.loaded.as_ref().unwrap().mission_assets, + 5, + LAB_CLASS, + transform_state(&engine.world, lab).unwrap(), + ) + .unwrap()[0]; let mut pose = transform_state(&engine.world, hero.handle).unwrap(); - pose.position = [950.7_f32, 869.4, 69.98].map(f32::to_bits); + pose.position = std::array::from_fn(|axis| { + ((floor[0][axis] + floor[1][axis] + floor[2][axis]) / 3.0).to_bits() + }); set_transform(&mut engine.world, hero.handle, pose).unwrap(); assert!(order_lab_research(&mut engine, hero, 1_000).is_err()); assert_eq!( capture_neutral_building(&mut engine, hero).unwrap(), Some(OriginalObjectId(5)) ); + assert!(at_research_laboratory(&engine, hero)); let (component, duration) = order_lab_research(&mut engine, hero, 1_000).unwrap(); assert_eq!((component.as_str(), duration), ("e_tur_bt_01", 5_000)); assert!(order_lab_research(&mut engine, hero, 1_001).is_err()); @@ -234,6 +280,7 @@ mod tests { assert!(mission.free_research.learned.contains(&(OwnerId(0), row))); assert!(!mission.free_research.learned.contains(&(OwnerId(1), row))); assert!(order_lab_research(&mut engine, hero, 6_001).is_err()); + assert!(at_research_laboratory(&engine, hero)); } } |
