diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster/orders.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_cursor.rs | 105 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_orders.rs | 37 | ||||
| -rw-r--r-- | crates/fparkan-terrain/src/lib.rs | 15 |
6 files changed, 171 insertions, 3 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 253920fa..1f2cb172 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -48,6 +48,7 @@ pub mod object_descriptions; pub mod satellite_map; pub mod strategic_balance; pub mod strategic_roster; +pub mod strategy_cursor; pub mod strategy_toolbar; mod turret_elevation; mod ui_configuration; diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 440ead26..64c819a1 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -46,6 +46,16 @@ pub fn hit_test([x, y]: [f32; 2], row_count: usize) -> Option<ListHit> { (row < row_count && y % ROW_PITCH < ROW_HEIGHT).then_some(ListHit::Row(row)) } +/// Whether the roster or its supported order panel covers the pointer. +pub fn covers_pointer([x, y]: [f32; 2], rows: usize) -> bool { + let row_end = ROW_TOP + ROW_PITCH * rows.min(19) as u32; + let [x, y] = [x.round_ties_even(), y.round_ties_even()]; + rows != 0 + && ((ORIGIN[0] as f32..(ORIGIN[0] + EXTENT[0] - 1) as f32).contains(&x) + && (0.0..row_end as f32).contains(&y) + || orders::covers_pointer([x, y], rows)) +} + /// A localized row with the native icon selectors. pub struct Row<'a> { pub label: &'a str, diff --git a/crates/fparkan-assets/src/strategic_roster/orders.rs b/crates/fparkan-assets/src/strategic_roster/orders.rs index 743b4801..4ea7d8d8 100644 --- a/crates/fparkan-assets/src/strategic_roster/orders.rs +++ b/crates/fparkan-assets/src/strategic_roster/orders.rs @@ -33,6 +33,12 @@ fn panel_top(count: usize) -> u32 { ROW_TOP + count.min(19) as u32 * ROW_PITCH } +pub(super) fn covers_pointer([x, y]: [f32; 2], count: usize) -> bool { + let y_top = panel_top(count); + let y_bottom = (y_top + (ORDERS.len() as u32 + 1) * ROW_PITCH).min(480); + (LEFT as f32..RIGHT as f32).contains(&x) && (y_top as f32..y_bottom as f32).contains(&y) +} + pub(super) struct OrderImages { stub: RgbaImage, disabled: RgbaImage, diff --git a/crates/fparkan-assets/src/strategy_cursor.rs b/crates/fparkan-assets/src/strategy_cursor.rs new file mode 100644 index 00000000..392fa894 --- /dev/null +++ b/crates/fparkan-assets/src/strategy_cursor.rs @@ -0,0 +1,105 @@ +//! The original strategic mouse cursor strips in `ui/cursor.cfg`. + +use crate::{ui_configuration, ui_sprite, RgbaImage}; +use fparkan_vfs::Vfs; + +/// Number of adjacent frames loaded by iron3d+0x57d00. +pub const PHASES: u32 = 4; + +/// The cursor modes used for strategic routes. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum StrategyCursor { + /// Ordinary UI pointer. + Arrow, + /// A valid order waypoint. + Place, + /// An invalid order waypoint. + WrongPlace, +} + +impl StrategyCursor { + /// Modes in draw-range order. + pub const ALL: [Self; 3] = [Self::Arrow, Self::Place, Self::WrongPlace]; + + fn key(self) -> &'static str { + match self { + Self::Arrow => "ARROW", + Self::Place => "PLACE", + Self::WrongPlace => "WRONG_PLACE", + } + } +} + +/// Four adjacent frames and their anchoring and phase delay. +pub struct CursorStrip { + pub image: RgbaImage, + pub hotspot: [u32; 2], + pub phase_delay_ms: u32, +} + +/// Loads the route cursor strips from the installed UI archive. +pub fn load(vfs: &dyn Vfs) -> Result<[CursorStrip; 3], String> { + let config = ui_configuration::read(vfs, "ui/cursor.cfg")?; + let config = String::from_utf8_lossy(&config); + let region = |kind: StrategyCursor| { + ui_configuration::object(&config, kind.key()) + .ok_or_else(|| format!("missing cursor {}", kind.key())) + }; + let mut strips = Vec::with_capacity(StrategyCursor::ALL.len()); + let mut page = None; + for kind in StrategyCursor::ALL { + let fields = region(kind)?; + let texture = ui_configuration::field(&fields, "TEXTURE")?; + if page.as_ref().is_none_or(|(current, _)| *current != texture) { + page = Some((texture, ui_sprite::load_ui_page(vfs, texture)?)); + } + let Some((_, atlas)) = &page else { + unreachable!("the cursor atlas was loaded above"); + }; + let number = |key| ui_configuration::number(&fields, key); + let [x, y] = [number("OFFSET_X")?, number("OFFSET_Y")?]; + let [w, h] = [number("WIDTH")?, number("HEIGHT")?]; + let strip_width = w.checked_mul(PHASES).ok_or("cursor strip width overflow")?; + let image = ui_sprite::atlas_region(atlas, [x, y, strip_width, h]) + .ok_or_else(|| format!("cursor {} outside atlas", kind.key()))?; + strips.push(CursorStrip { + image, + hotspot: [number("EXTENT_X")?, number("EXTENT_Y")?], + phase_delay_ms: number("PHASE_DELAY")?, + }); + } + Ok(strips + .try_into() + .unwrap_or_else(|_| unreachable!("one strip per mode"))) +} + +#[cfg(test)] +mod tests { + use super::*; + use fparkan_vfs::DirectoryVfs; + + #[test] + #[ignore = "requires licensed corpus"] + fn original_strategy_cursor_frames_and_hotspots() { + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let strips = load(&DirectoryVfs::new(root)).unwrap(); + assert_eq!( + strips.each_ref().map(|strip| strip.hotspot), + [[0, 0], [8, 8], [8, 8]] + ); + for strip in strips { + assert_eq!([strip.image.width, strip.image.height], [64, 16]); + assert_eq!(strip.phase_delay_ms, 150); + for phase in 0..PHASES { + let offset = (phase * 16 * 4) as usize; + assert!(strip + .image + .rgba8 + .chunks_exact(64 * 4) + .any(|row| row[offset..offset + 16 * 4] + .chunks_exact(4) + .any(|pixel| pixel[3] != 0))); + } + } + } +} diff --git a/crates/fparkan-runtime/src/strategic_orders.rs b/crates/fparkan-runtime/src/strategic_orders.rs index 9e364786..2c40c31f 100644 --- a/crates/fparkan-runtime/src/strategic_orders.rs +++ b/crates/fparkan-runtime/src/strategic_orders.rs @@ -1,7 +1,36 @@ //! Orders issued by the player from the strategic warbot panel. use crate::{strategic_roster::warbot_rows, unit_patrol::UnitPatrol, Engine, StartingCockpit}; -use fparkan_world::ObjectHandle; +use fparkan_terrain::NavigationQuery; +use fparkan_world::{identity_metadata, ObjectHandle}; + +/// Tests a route waypoint using the selected warbot's chassis and the native areal flag. +pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2]) -> bool { + if !x.is_finite() || !y.is_finite() { + return false; + } + let Some(mission) = &engine.loaded else { + return false; + }; + let Some(original) = identity_metadata(&engine.world, actor) + .ok() + .and_then(|meta| meta.original_id) + else { + return false; + }; + let terrain = &mission.terrain; + let Some(areal) = terrain.locate_areal([x, y, 0.0]).ok().flatten() else { + return false; + }; + // iron3d+0x76bc0 allows chassis selector 1 in any resolved areal. + mission + .unit_chassis_flags + .get(&original.0) + .is_some_and(|flags| flags.chassis_type == Some(1)) + || terrain + .areal_logic_flag(areal) + .is_some_and(|flag| flag != 0) +} /// Stops the selected warbot and clears its outstanding movement orders. pub fn stand(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool { @@ -15,7 +44,11 @@ pub fn follow_route( actor: ObjectHandle, points: &[[f32; 2]], ) -> bool { - if points.is_empty() || !points.iter().flatten().all(|value| value.is_finite()) { + if points.is_empty() + || !points + .iter() + .all(|&point| accepts_waypoint(engine, actor, point)) + { return false; } issue(engine, cockpit, actor, Some(points.to_vec())) diff --git a/crates/fparkan-terrain/src/lib.rs b/crates/fparkan-terrain/src/lib.rs index 7e2ae588..659acf86 100644 --- a/crates/fparkan-terrain/src/lib.rs +++ b/crates/fparkan-terrain/src/lib.rs @@ -251,6 +251,7 @@ impl TerrainWorld { ); areals.push(RuntimeAreal { id, + logic_flag: areal.logic_flag, polygon: areal .vertices .iter() @@ -427,6 +428,12 @@ impl TerrainWorld { self.areals.len() } + /// Logic flag from the prefix of this logical area in `Land.map`. + #[must_use] + pub fn areal_logic_flag(&self, id: ArealId) -> Option<u32> { + self.areals.get(id.0 as usize).map(|areal| areal.logic_flag) + } + /// Returns the midpoint of the boundary edge linking two consecutive areas. /// Links through polygon slots have no boundary midpoint. #[must_use] @@ -974,6 +981,7 @@ impl RuntimeTriangle { #[derive(Clone, Debug)] struct RuntimeAreal { id: ArealId, + logic_flag: u32, polygon: Vec<[f32; 2]>, edge_neighbors: Vec<Option<u32>>, } @@ -1280,10 +1288,15 @@ mod tests { #[test] fn locates_areal_and_routes_synthetic_neighbors() { - let map = synthetic_land_map(); + let mut map = synthetic_land_map(); + map.areals[0].logic_flag = 0; + map.areals[1].logic_flag = 1; let world = TerrainWorld::from_land_map(&map).expect("world"); assert_eq!(world.areal_count(), 2); + assert_eq!(world.areal_logic_flag(ArealId(0)), Some(0)); + assert_eq!(world.areal_logic_flag(ArealId(1)), Some(1)); + assert_eq!(world.areal_logic_flag(ArealId(2)), None); assert_eq!( world.route_crossing(ArealId(0), ArealId(1)), Some([1.0, 0.5]) |
