diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 08:52:38 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 08:52:38 +0300 |
| commit | 0e1317235e7db3fa1b20dac2a4f12565a31e8cd0 (patch) | |
| tree | f0be6ba752084f58fd30c658810ae3c3db439f15 /crates | |
| parent | 9f360911f4af29307b566c0acacf1f5661ea9970 (diff) | |
| download | fparkan-0e1317235e7db3fa1b20dac2a4f12565a31e8cd0.tar.xz fparkan-0e1317235e7db3fa1b20dac2a4f12565a31e8cd0.zip | |
feat(ui): enter strategic view from a command post
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/strategy_toolbar.rs | 32 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_view.rs | 154 |
3 files changed, 187 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/strategy_toolbar.rs b/crates/fparkan-assets/src/strategy_toolbar.rs index 5e4d04a8..4c49e7f0 100644 --- a/crates/fparkan-assets/src/strategy_toolbar.rs +++ b/crates/fparkan-assets/src/strategy_toolbar.rs @@ -16,6 +16,30 @@ const LOCK_GLYPH_OFFSET: [u32; 2] = [18, 7]; // iron3d+0x9bf49 sets the ordinary enabled glyph to ARGB ff80ff80. const ENABLED_GLYPH: [u8; 3] = [128, 255, 128]; +/// Actions available immediately upon entering strategic mode. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ToolbarAction { + /// Return to the hero in the suit. + Hero, + /// Toggle the satellite map. + Map, +} + +/// Tests the native button bounds in the original 640×480 screen space. +#[must_use] +pub fn command_button_at([x, y]: [f32; 2]) -> Option<ToolbarAction> { + if !(0.0..EXTENT[0] as f32).contains(&x.round_ties_even()) { + return None; + } + let y = y.round_ties_even(); + [(0, ToolbarAction::Hero), (10, ToolbarAction::Map)] + .into_iter() + .find_map(|(index, action)| { + let top = BUTTON_TOPS[index] as f32; + (top..top + 29.0).contains(&y).then_some(action) + }) +} + /// Composes the fully expanded toolbar in its resting state. pub fn load_expanded_toolbar(vfs: &dyn Vfs) -> Result<RgbaImage, String> { let [head, separator, button, lock, lock_icon, glyphs @ ..] = load_ui_sprites( @@ -86,6 +110,14 @@ mod tests { use fparkan_vfs::DirectoryVfs; #[test] + fn command_buttons_use_their_original_row_bounds() { + assert_eq!(command_button_at([16.0, 12.0]), Some(ToolbarAction::Hero)); + assert_eq!(command_button_at([45.0, 427.0]), Some(ToolbarAction::Map)); + assert_eq!(command_button_at([46.0, 400.0]), None); + assert_eq!(command_button_at([25.0, 428.0]), None); + } + + #[test] #[ignore = "requires licensed corpus"] fn expanded_toolbar_loads_the_native_buttons_in_order() { let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap(); diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index bb560d91..efa4f272 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -71,6 +71,7 @@ mod mobile_budget; mod node_amount; mod node_stage; pub mod satellite_contacts; +pub mod strategic_view; pub use clan_clock::note_mission_pause_elapsed; use control_profile::project_initial as project_control_owner_profile_fca0; pub use control_profile::{ControlProfileReport, ControlProfileScalars}; diff --git a/crates/fparkan-runtime/src/strategic_view.rs b/crates/fparkan-runtime/src/strategic_view.rs new file mode 100644 index 00000000..b22555f6 --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_view.rs @@ -0,0 +1,154 @@ +//! Sources of the player's strategic camera. + +use crate::{Engine, MissionControlDeviceTableStatus, StartingCockpit, SurfaceQuery}; +use fparkan_render::{LegacyIron3dEulerTransform, RawCameraTransform}; +use fparkan_world::{identity_metadata, registration_sequence, transform_state, ObjectHandle}; +use std::f32::consts::FRAC_PI_2; + +/// The object providing the command interface. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CommandPost { + /// Hero at their own clan's bunker communicator. + Bunker(ObjectHandle), + /// The vehicle the hero is currently piloting. + Warbot(ObjectHandle), +} + +/// Returns the source of strategic control available to the player. +#[must_use] +pub fn command_post(engine: &Engine, cockpit: StartingCockpit) -> Option<CommandPost> { + let mission = engine.loaded.as_ref()?; + if let Some(vehicle) = crate::player_vehicle(engine) { + if vehicle.handle != cockpit.handle { + return None; + } + let table = mission.control_device_tables.iter().find(|table| { + table.handle == vehicle.handle && !table.node_provider_depletion_marked + })?; + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + return None; + }; + return has_command_link(devices).then_some(CommandPost::Warbot(vehicle.handle)); + } + + let owner = identity_metadata(&engine.world, cockpit.handle) + .ok()? + .owner_id?; + let position = transform_state(&engine.world, cockpit.handle) + .ok()? + .position + .map(f32::from_bits); + mission + .object_drafts + .iter() + .enumerate() + .find_map(|(index, draft)| { + let class = draft.unit_dat_header_flags?; + if !matches!(class, 0x8001_0000 | 0x8002_0000 | 0x8004_0000) { + return None; + } + let handle = crate::mission_object_handle(engine, draft.original_id?)?; + if identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) { + return None; + } + let pose = transform_state(&engine.world, handle).ok()?; + crate::mission_events::on_communicator( + &mission.mission_assets, + index, + class, + pose, + position, + ) + .then_some(CommandPost::Bunker(handle)) + }) +} + +fn has_command_link(devices: &[crate::EffectiveControlDevice]) -> bool { + // iron3d+0x773a0 and Control+0x2c3c0 search the current tags from index zero. + let index = devices.iter().position(|device| device.descriptor.tag == 1); + // Control+0x2b410 uses the separate cached factory-1 pointer and the + // descriptor currently applied to that device. + index.is_some_and(|index| index > 0) + && devices + .iter() + .rfind(|device| device.factory_tag == 1) + .is_some_and(|device| device.descriptor.fixed_words[2] & 0x0800_0000 != 0) +} + +/// Initial external viewpoint of a command post, sampled from the current world. +#[must_use] +pub fn command_view(engine: &Engine, post: CommandPost) -> Option<RawCameraTransform> { + let (handle, clearance) = match post { + CommandPost::Bunker(handle) => (handle, 36.0), + CommandPost::Warbot(handle) => (handle, 2.5), + }; + registration_sequence(&engine.world, handle).ok()??; + let [x, y, z] = transform_state(&engine.world, handle) + .ok()? + .position + .map(f32::from_bits); + let terrain = engine.loaded.as_ref()?.terrain.height_at([x, y]).ok()??; + // iron3d+0x63ec0/+0x64140 start at the post, looking north. + // The first external update sets pitch and raises the eye out of terrain. + let matrix = LegacyIron3dEulerTransform { + translation: [x, y, z.max(terrain + clearance)], + orientation_radians: [0.0, FRAC_PI_2 - 1.0, FRAC_PI_2], + } + .try_row_major()?; + Some(RawCameraTransform { + words: matrix.map(f32::to_bits), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::synthetic_effective_control_device; + use crate::{ + create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::OriginalObjectId; + use std::sync::Arc; + + #[test] + fn command_link_requires_a_nonzero_match_and_the_cached_pilot_flag() { + let device = + |factory, current| synthetic_effective_control_device(factory, current, 0, None, None); + let generator = device(19, 19); + let mut pilot = device(1, 1); + pilot.descriptor.fixed_words[2] = 0x0800_0000; + assert!(!has_command_link(&[pilot.clone(), generator.clone()])); + assert!(has_command_link(&[generator.clone(), pilot.clone()])); + + let mut last_pilot = device(1, 1); + last_pilot.descriptor.fixed_words[2] = 0x0400_0000; + assert!(!has_command_link(&[generator, pilot, last_pilot])); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_starting_bunker_supplies_strategy_camera() { + 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 hero = starting_cockpit(&engine).unwrap(); + let bunker = crate::mission_object_handle(&engine, OriginalObjectId(0)).unwrap(); + let post = command_post(&engine, hero); + assert_eq!(post, Some(CommandPost::Bunker(bunker))); + assert!(command_view(&engine, post.unwrap()).is_some()); + } +} |
