diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 11 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster/orders.rs | 144 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/satellite_contacts.rs | 3 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_orders.rs | 44 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_roster.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 5 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 65 |
8 files changed, 271 insertions, 3 deletions
diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 0faeb7e4..440ead26 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -9,6 +9,10 @@ use crate::{ }; use fparkan_vfs::Vfs; +mod orders; +use orders::OrderImages; +pub use orders::{order_at, RosterOrder}; + /// List origin and size in the original logical screen coordinates. pub const ORIGIN: [u32; 2] = [51, 0]; pub const EXTENT: [u32; 2] = [319, 480]; @@ -63,6 +67,7 @@ pub struct RosterImages { end: RgbaImage, class_icon: RgbaImage, chassis_icons: [RgbaImage; 4], + orders: OrderImages, } impl RosterImages { @@ -149,6 +154,7 @@ impl RosterImages { end, class_icon: icon(49, 110)?, chassis_icons: [icon(97, 94)?, icon(49, 94)?, icon(65, 94)?, icon(81, 94)?], + orders: OrderImages::load(vfs)?, }) } @@ -157,6 +163,7 @@ impl RosterImages { &self, rows: &[Row<'_>], card: &[Option<CardLine<'_>>], + orders: Option<([&str; 3], Option<RosterOrder>)>, ) -> Result<RgbaImage, String> { let mut panel = self.background.clone(); if let Some(row) = rows.iter().find(|row| row.selected) { @@ -210,6 +217,10 @@ impl RosterImages { tint, ); } + if let Some((labels, hovered)) = orders { + self.orders + .paint(self, &mut panel, rows.len(), labels, hovered)?; + } Ok(panel) } diff --git a/crates/fparkan-assets/src/strategic_roster/orders.rs b/crates/fparkan-assets/src/strategic_roster/orders.rs new file mode 100644 index 00000000..743b4801 --- /dev/null +++ b/crates/fparkan-assets/src/strategic_roster/orders.rs @@ -0,0 +1,144 @@ +use super::{stamp, RosterImages, ORIGIN, ROW_HEIGHT, ROW_PITCH, ROW_TOP}; +use crate::{ui_sprite::load_ui_sprites, RgbaImage}; +use fparkan_vfs::Vfs; + +const LEFT: u32 = 100; +const RIGHT: u32 = 367; + +/// Commands supported by the strategic warbot panel. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RosterOrder { + /// Stop in place. + Stand, + /// Draw a route and send it to the unit. + FollowRoute, +} + +const ORDERS: [RosterOrder; 2] = [RosterOrder::Stand, RosterOrder::FollowRoute]; + +/// Returns the visible command at the pointer, excluding borders and row gaps. +pub fn order_at([x, y]: [f32; 2], count: usize) -> Option<RosterOrder> { + let [x, y] = [x.round_ties_even(), y.round_ties_even()]; + if !(LEFT as f32..RIGHT as f32).contains(&x) || !(0.0..480.0).contains(&y) { + return None; + } + let y = (y as u32).checked_sub(panel_top(count) + ROW_PITCH)?; + ORDERS + .get((y / ROW_PITCH) as usize) + .copied() + .filter(|_| y % ROW_PITCH < ROW_HEIGHT) +} + +fn panel_top(count: usize) -> u32 { + ROW_TOP + count.min(19) as u32 * ROW_PITCH +} + +pub(super) struct OrderImages { + stub: RgbaImage, + disabled: RgbaImage, + arrows: [RgbaImage; 2], +} + +impl OrderImages { + pub(super) fn load(vfs: &dyn Vfs) -> Result<Self, String> { + let [stub, disabled] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + ["ccres_ending_stub", "ccres_long_button_off"], + )?; + let arrows = load_ui_sprites(vfs, "ui/hq.cfg", ["scroll_up_icon", "scroll_down_icon"])?; + Ok(Self { + stub, + disabled, + arrows, + }) + } + + pub(super) fn paint( + &self, + roster: &RosterImages, + panel: &mut RgbaImage, + count: usize, + [title, stand, follow]: [&str; 3], + hovered: Option<RosterOrder>, + ) -> Result<(), String> { + let x = LEFT - ORIGIN[0]; + let y = panel_top(count); + let title = roster.font.centered_line(title, 158, ROW_HEIGHT as u16)?; + for (sprite, left, right) in [ + (&self.stub, 0, 9), + (&roster.icon_frame, 8, 167), + (&title, 8, 166), + ] { + stamp( + panel, + sprite, + [x + left, y, x + right, y + ROW_HEIGHT], + [255; 3], + ); + } + for (index, icon) in self.arrows.iter().enumerate() { + let left = 166 + 50 * index as u32; + stamp( + panel, + &self.disabled, + [x + left, y, x + left + 51, y + ROW_HEIGHT], + [255; 3], + ); + let icon_left = x + left + 50_u32.saturating_sub(icon.width) / 2; + let icon_top = y + ROW_HEIGHT.saturating_sub(icon.height) / 2; + stamp( + panel, + icon, + [ + icon_left, + icon_top, + icon_left + icon.width, + icon_top + icon.height, + ], + [128; 3], + ); + } + for (index, (order, label)) in ORDERS.into_iter().zip([stand, follow]).enumerate() { + let y = y + (index as u32 + 1) * ROW_PITCH; + let bottom = y + ROW_HEIGHT; + let label = roster.font.centered_line(label, 247, ROW_HEIGHT as u16)?; + for (sprite, left, right) in [ + (&self.stub, 0, 9), + (&roster.emitter[1], 8, 15), + (&roster.ray, 14, 262), + (&roster.end, 261, 267), + ] { + stamp(panel, sprite, [x + left, y, x + right, bottom], [255; 3]); + } + if hovered == Some(order) { + stamp( + panel, + &RgbaImage { + width: 1, + height: 1, + rgba8: vec![0, 128, 0, 128], + }, + [x + 16, y + 3, x + 260, bottom - 3], + [255; 3], + ); + } + stamp(panel, &label, [x + 14, y, x + 261, bottom], [255; 3]); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hit_test_follows_the_last_warbot_row() { + assert_eq!(order_at([250.0, 197.0], 3), None); + assert_eq!(order_at([250.0, 198.0], 3), Some(RosterOrder::Stand)); + assert_eq!(order_at([250.0, 217.0], 3), None); + assert_eq!(order_at([250.0, 218.0], 3), Some(RosterOrder::FollowRoute)); + assert_eq!(order_at([250.0, 218.0], 4), Some(RosterOrder::Stand)); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 0cc96e6f..1db75dbc 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -73,6 +73,7 @@ mod node_stage; mod projectile_capacity; pub mod satellite_contacts; pub mod strategic_balance; +pub mod strategic_orders; pub mod strategic_roster; pub mod strategic_view; pub mod warbot_stats; diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index e984baf3..17030fc1 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -229,7 +229,8 @@ fn icon_x(class: u32) -> Option<u32> { }) } -fn project([x, y]: [f32; 2], extent: f32) -> Option<[i32; 2]> { +/// Projects world XY onto the original 256-pixel map, including the inverted Y axis. +pub fn project([x, y]: [f32; 2], extent: f32) -> Option<[i32; 2]> { if !x.is_finite() || !y.is_finite() || !extent.is_finite() || extent <= 0.0 { return None; } diff --git a/crates/fparkan-runtime/src/strategic_orders.rs b/crates/fparkan-runtime/src/strategic_orders.rs new file mode 100644 index 00000000..9e364786 --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_orders.rs @@ -0,0 +1,44 @@ +//! 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; + +/// Stops the selected warbot and clears its outstanding movement orders. +pub fn stand(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool { + issue(engine, cockpit, actor, None) +} + +/// Replaces the selected warbot's order with the confirmed sequence of world points. +pub fn follow_route( + engine: &mut Engine, + cockpit: StartingCockpit, + actor: ObjectHandle, + points: &[[f32; 2]], +) -> bool { + if points.is_empty() || !points.iter().flatten().all(|value| value.is_finite()) { + return false; + } + issue(engine, cockpit, actor, Some(points.to_vec())) +} + +fn issue( + engine: &mut Engine, + cockpit: StartingCockpit, + actor: ObjectHandle, + route: Option<Vec<[f32; 2]>>, +) -> bool { + if !warbot_rows(engine, cockpit) + .iter() + .any(|row| row.handle == actor && row.selectable) + { + return false; + } + let Some(mission) = engine.loaded.as_mut() else { + return false; + }; + mission + .unit_patrol + .get_or_insert_with(UnitPatrol::scripted) + .command_order(&engine.world, actor, route); + true +} diff --git a/crates/fparkan-runtime/src/strategic_roster.rs b/crates/fparkan-runtime/src/strategic_roster.rs index df615e0a..07735aff 100644 --- a/crates/fparkan-runtime/src/strategic_roster.rs +++ b/crates/fparkan-runtime/src/strategic_roster.rs @@ -146,6 +146,7 @@ fn order_label(order: u32) -> u32 { 0x11 => 0x1833, 0x13 => 0x1826, 0x14 => 0x1829, + 0x15 => 0x1827, _ => 0x1835, } } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 44e83986..4f708af3 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -32,6 +32,7 @@ pub(super) struct UnitPatrol { animal_migration: Vec<AnimalMigration>, migration_rng: u32, shut_down: HashSet<ObjectHandle>, + standing: HashSet<ObjectHandle>, script_orders: Vec<(ObjectHandle, VecDeque<orders::UnitOrder>)>, script_order_counts: BTreeMap<(u32, u32), u64>, clock_ms: u32, @@ -168,6 +169,7 @@ impl UnitPatrol { animal_migration: Vec::new(), migration_rng: 1, shut_down: HashSet::new(), + standing: HashSet::new(), script_orders: Vec::new(), script_order_counts: BTreeMap::new(), clock_ms: 0, @@ -189,6 +191,7 @@ impl UnitPatrol { ) -> bool { self.script_orders.retain(|(unit, _)| *unit != handle); self.shut_down.remove(&handle); + self.standing.remove(&handle); self.units .retain(|unit| transform_state(world, unit.handle).is_ok()); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { @@ -365,6 +368,7 @@ impl UnitPatrol { pub(super) fn shut_down(&mut self, handle: ObjectHandle) -> bool { self.script_orders.retain(|(unit, _)| *unit != handle); + self.standing.remove(&handle); // shortcut: stop autonomous movement and fire until the native shutdown task is modeled. self.units.retain(|unit| unit.handle != handle); self.shut_down.insert(handle) @@ -498,6 +502,7 @@ impl UnitPatrol { animal_migration: Vec::new(), migration_rng: 1, shut_down: HashSet::new(), + standing: HashSet::new(), script_orders: Vec::new(), script_order_counts: BTreeMap::new(), clock_ms: 0, diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index 8e9260a1..fec2731c 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -9,6 +9,8 @@ pub(super) enum UnitOrder { Move([f32; 2]), Attack(ObjectHandle, usize), Patrol([f32; 2], f32), + Stand, + Route(Vec<[f32; 2]>), Shutdown, Resume(Box<PatrollingUnit>), } @@ -16,10 +18,11 @@ pub(super) enum UnitOrder { impl UnitOrder { fn opcode(&self) -> u32 { match self { - Self::Move(_) => 2, + Self::Move(_) | Self::Route(_) => 2, Self::Attack(..) => 3, Self::Patrol(..) => 4, Self::Shutdown => 0x13, + Self::Stand => 0x15, Self::Resume(_) => unreachable!("a suspended order has already started"), } } @@ -50,6 +53,21 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn command_order( + &mut self, + world: &World, + actor: ObjectHandle, + route: Option<Vec<[f32; 2]>>, + ) { + let order = route.map_or(UnitOrder::Stand, UnitOrder::Route); + self.script_orders.retain(|(unit, _)| *unit != actor); + if self.is_leaving_factory(actor) { + self.script_orders.push((actor, VecDeque::from([order]))); + } else { + self.start_script_order(world, actor, order); + } + } + pub(crate) fn active_order_code(&self, actor: ObjectHandle) -> u32 { if self.is_leaving_factory(actor) { return 0x14; @@ -57,6 +75,9 @@ impl UnitPatrol { if self.is_shut_down(actor) { return UnitOrder::Shutdown.opcode(); } + if self.standing.contains(&actor) { + return UnitOrder::Stand.opcode(); + } let Some(unit) = self.units.iter().find(|unit| unit.handle == actor) else { return 0; }; @@ -96,7 +117,8 @@ impl UnitPatrol { match order { UnitOrder::Move(point) | UnitOrder::Patrol(point, _) => points.push(*point), UnitOrder::Resume(unit) => unit.append_point_orders(&mut points), - UnitOrder::Attack(..) | UnitOrder::Shutdown => {} + UnitOrder::Route(route) => points.extend(route), + UnitOrder::Attack(..) | UnitOrder::Shutdown | UnitOrder::Stand => {} } } } @@ -161,6 +183,8 @@ impl UnitPatrol { queue.push_front(UnitOrder::Resume(Box::new(self.units.remove(index)))); } else if self.is_shut_down(actor) { queue.push_front(UnitOrder::Shutdown); + } else if self.standing.contains(&actor) { + queue.push_front(UnitOrder::Stand); } self.start_script_order(world, actor, order); } @@ -268,6 +292,18 @@ impl UnitPatrol { return true; } match &order { + UnitOrder::Stand => { + self.units.retain(|unit| unit.handle != actor); + self.shut_down.remove(&actor); + self.standing.insert(actor); + return true; + } + UnitOrder::Route(route) => { + for (index, &point) in route.iter().enumerate() { + self.move_to(world, actor, point, index != 0); + } + return true; + } UnitOrder::Move(point) => { self.move_to(world, actor, *point, false); eprintln!( @@ -309,6 +345,8 @@ impl UnitPatrol { world: &World, controls: &[MissionControlDeviceTableState], ) { + self.standing + .retain(|&actor| transform_state(world, actor).is_ok()); self.units.retain(|unit| { let Some((target, _)) = unit.attack_target else { return true; @@ -415,6 +453,29 @@ mod tests { } #[test] + fn command_panel_stops_then_replaces_the_route_with_confirmed_points() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let other = object(&mut world); + let mut patrol = UnitPatrol::scripted(); + patrol.enqueue_script_order(&world, actor, UnitOrder::Move([10.0, 10.0]), false); + patrol.enqueue_script_order(&world, actor, UnitOrder::Shutdown, true); + patrol.move_to(&world, other, [40.0, 40.0], false); + + patrol.command_order(&world, actor, None); + patrol.advance_script_orders(&world, &[]); + assert_eq!(patrol.active_order_code(actor), 0x15); + assert!(!patrol.is_shut_down(actor)); + assert!(patrol.satellite_route(actor).is_empty()); + assert_eq!(patrol.satellite_route(other), [[40.0, 40.0]]); + + let points = vec![[20.0, 30.0], [30.0, 40.0]]; + patrol.command_order(&world, actor, Some(points.clone())); + assert_eq!(patrol.active_order_code(actor), 2); + assert_eq!(patrol.satellite_route(actor), points); + } + + #[test] fn move_attack_and_shutdown_wait_for_the_preceding_task() { let mut world = fparkan_world::new(Default::default()); let actor = object(&mut world); |
