diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster/orders.rs | 32 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategy_cursor.rs | 9 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/satellite_contacts.rs | 23 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_orders.rs | 53 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 3 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 79 |
7 files changed, 171 insertions, 30 deletions
diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 64c819a1..6c755300 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -173,7 +173,7 @@ impl RosterImages { &self, rows: &[Row<'_>], card: &[Option<CardLine<'_>>], - orders: Option<([&str; 3], Option<RosterOrder>)>, + orders: Option<([&str; 4], Option<RosterOrder>)>, ) -> Result<RgbaImage, String> { let mut panel = self.background.clone(); if let Some(row) = rows.iter().find(|row| row.selected) { diff --git a/crates/fparkan-assets/src/strategic_roster/orders.rs b/crates/fparkan-assets/src/strategic_roster/orders.rs index 4ea7d8d8..6e6e1e22 100644 --- a/crates/fparkan-assets/src/strategic_roster/orders.rs +++ b/crates/fparkan-assets/src/strategic_roster/orders.rs @@ -12,9 +12,25 @@ pub enum RosterOrder { Stand, /// Draw a route and send it to the unit. FollowRoute, + /// Patrol a point or a live object. + Patrol, } -const ORDERS: [RosterOrder; 2] = [RosterOrder::Stand, RosterOrder::FollowRoute]; +const ORDERS: [RosterOrder; 3] = [ + RosterOrder::Stand, + RosterOrder::FollowRoute, + RosterOrder::Patrol, +]; + +impl RosterOrder { + fn slot(self) -> u32 { + match self { + Self::Stand => 0, + Self::FollowRoute => 1, + Self::Patrol => 3, + } + } +} /// Returns the visible command at the pointer, excluding borders and row gaps. pub fn order_at([x, y]: [f32; 2], count: usize) -> Option<RosterOrder> { @@ -24,8 +40,8 @@ pub fn order_at([x, y]: [f32; 2], count: usize) -> Option<RosterOrder> { } let y = (y as u32).checked_sub(panel_top(count) + ROW_PITCH)?; ORDERS - .get((y / ROW_PITCH) as usize) - .copied() + .into_iter() + .find(|order| order.slot() == y / ROW_PITCH) .filter(|_| y % ROW_PITCH < ROW_HEIGHT) } @@ -35,7 +51,7 @@ fn panel_top(count: usize) -> u32 { 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); + let y_bottom = (y_top + (RosterOrder::Patrol.slot() + 2) * ROW_PITCH).min(480); (LEFT as f32..RIGHT as f32).contains(&x) && (y_top as f32..y_bottom as f32).contains(&y) } @@ -65,7 +81,7 @@ impl OrderImages { roster: &RosterImages, panel: &mut RgbaImage, count: usize, - [title, stand, follow]: [&str; 3], + [title, stand, follow, patrol]: [&str; 4], hovered: Option<RosterOrder>, ) -> Result<(), String> { let x = LEFT - ORIGIN[0]; @@ -105,8 +121,8 @@ impl OrderImages { [128; 3], ); } - for (index, (order, label)) in ORDERS.into_iter().zip([stand, follow]).enumerate() { - let y = y + (index as u32 + 1) * ROW_PITCH; + for (order, label) in ORDERS.into_iter().zip([stand, follow, patrol]) { + let y = y + (order.slot() + 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 [ @@ -146,5 +162,7 @@ mod tests { 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)); + assert_eq!(order_at([250.0, 238.0], 3), None); + assert_eq!(order_at([250.0, 258.0], 3), Some(RosterOrder::Patrol)); } } diff --git a/crates/fparkan-assets/src/strategy_cursor.rs b/crates/fparkan-assets/src/strategy_cursor.rs index 392fa894..c38a5bc9 100644 --- a/crates/fparkan-assets/src/strategy_cursor.rs +++ b/crates/fparkan-assets/src/strategy_cursor.rs @@ -15,17 +15,20 @@ pub enum StrategyCursor { Place, /// An invalid order waypoint. WrongPlace, + /// An eligible patrol point or object. + Guard, } impl StrategyCursor { /// Modes in draw-range order. - pub const ALL: [Self; 3] = [Self::Arrow, Self::Place, Self::WrongPlace]; + pub const ALL: [Self; 4] = [Self::Arrow, Self::Place, Self::WrongPlace, Self::Guard]; fn key(self) -> &'static str { match self { Self::Arrow => "ARROW", Self::Place => "PLACE", Self::WrongPlace => "WRONG_PLACE", + Self::Guard => "GUARD", } } } @@ -38,7 +41,7 @@ pub struct CursorStrip { } /// Loads the route cursor strips from the installed UI archive. -pub fn load(vfs: &dyn Vfs) -> Result<[CursorStrip; 3], String> { +pub fn load(vfs: &dyn Vfs) -> Result<[CursorStrip; StrategyCursor::ALL.len()], String> { let config = ui_configuration::read(vfs, "ui/cursor.cfg")?; let config = String::from_utf8_lossy(&config); let region = |kind: StrategyCursor| { @@ -85,7 +88,7 @@ mod tests { let strips = load(&DirectoryVfs::new(root)).unwrap(); assert_eq!( strips.each_ref().map(|strip| strip.hotspot), - [[0, 0], [8, 8], [8, 8]] + [[0, 0], [8, 8], [8, 8], [8, 8]] ); for strip in strips { assert_eq!([strip.image.width, strip.image.height], [64, 16]); diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index 17030fc1..31181bc1 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -45,6 +45,29 @@ pub fn landscape_extent(engine: &Engine) -> Option<f32> { (extent.is_finite() && extent > 0.0).then_some(extent) } +/// Picks a live unit within 40 world units or a building within 80, in registry order. +pub fn pick_contact( + engine: &Engine, + cockpit: StartingCockpit, + [x, y]: [f32; 2], +) -> Option<ObjectHandle> { + let extent = landscape_extent(engine)?; + visible_contacts(engine, cockpit, extent, |_, class| { + let unit = unit_class(class); + (unit || icon_x(class).is_some()).then_some(unit) + }) + .filter_map(|(_, handle, pose, _, unit)| { + let [u, v, _] = pose.position.map(f32::from_bits); + let limit = if unit { 40.0 } else { 80.0 }; + let distance = f64::from(u - x).hypot(f64::from(v - y)); + (distance < limit).then_some(())?; + let sequence = registration_sequence(&engine.world, handle).ok()??; + Some((!unit, sequence, handle)) + }) + .min_by_key(|&(building, sequence, _)| (building, sequence)) + .map(|(_, _, handle)| handle) +} + /// Building marks for the player's own clan and its radar contacts. pub fn visible_buildings( engine: &Engine, diff --git a/crates/fparkan-runtime/src/strategic_orders.rs b/crates/fparkan-runtime/src/strategic_orders.rs index 2c40c31f..d49fa963 100644 --- a/crates/fparkan-runtime/src/strategic_orders.rs +++ b/crates/fparkan-runtime/src/strategic_orders.rs @@ -1,8 +1,21 @@ //! Orders issued by the player from the strategic warbot panel. -use crate::{strategic_roster::warbot_rows, unit_patrol::UnitPatrol, Engine, StartingCockpit}; +use crate::{ + strategic_roster::warbot_rows, + unit_patrol::{UnitOrder, UnitPatrol}, + Engine, StartingCockpit, +}; use fparkan_terrain::NavigationQuery; -use fparkan_world::{identity_metadata, ObjectHandle}; +use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle}; + +/// Where the warbot is to patrol. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum PatrolTarget { + /// Fixed world coordinates. + Point([f32; 2]), + /// Live object whose position determines the patrol center. + Object(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 { @@ -34,7 +47,7 @@ pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2]) /// 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) + issue(engine, cockpit, actor, UnitOrder::Stand) } /// Replaces the selected warbot's order with the confirmed sequence of world points. @@ -51,14 +64,42 @@ pub fn follow_route( { return false; } - issue(engine, cockpit, actor, Some(points.to_vec())) + issue(engine, cockpit, actor, UnitOrder::Route(points.to_vec())) +} + +/// Whether an object may be used as the center of a patrol. +pub fn accepts_patrol_object(engine: &Engine, target: ObjectHandle) -> bool { + registration_sequence(&engine.world, target) + .ok() + .flatten() + .is_some() + && engine.loaded.as_ref().is_some_and(|mission| { + !mission + .control_device_tables + .iter() + .any(|table| table.handle == target && table.node_provider_depletion_marked) + }) +} + +/// Replaces the selected warbot's order with a patrol around a point or an object. +pub fn patrol_at( + engine: &mut Engine, + cockpit: StartingCockpit, + actor: ObjectHandle, + target: PatrolTarget, +) -> bool { + let valid = match target { + PatrolTarget::Point(point) => accepts_waypoint(engine, actor, point), + PatrolTarget::Object(target) => accepts_patrol_object(engine, target), + }; + valid && issue(engine, cockpit, actor, UnitOrder::StrategicPatrol(target)) } fn issue( engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle, - route: Option<Vec<[f32; 2]>>, + order: UnitOrder, ) -> bool { if !warbot_rows(engine, cockpit) .iter() @@ -72,6 +113,6 @@ fn issue( mission .unit_patrol .get_or_insert_with(UnitPatrol::scripted) - .command_order(&engine.world, actor, route); + .command_order(&engine.world, actor, order); true } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 4f708af3..5e083403 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -23,6 +23,7 @@ use std::collections::{BTreeMap, HashSet, VecDeque}; mod orders; pub(super) use orders::supports_order; +pub(super) use orders::UnitOrder; #[derive(Clone)] pub(super) struct UnitPatrol { @@ -45,7 +46,7 @@ pub(super) struct UnitPatrol { } #[derive(Clone)] -struct PatrollingUnit { +pub(super) struct PatrollingUnit { handle: ObjectHandle, center: [f32; 2], center_object: Option<ObjectHandle>, diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index fec2731c..48a849f3 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -1,16 +1,20 @@ use super::{PatrollingUnit, UnitPatrol}; -use crate::{MissionControlDeviceTableState, MissionDocument, MissionObjectDraft}; +use crate::{ + strategic_orders::PatrolTarget, MissionControlDeviceTableState, MissionDocument, + MissionObjectDraft, +}; use fparkan_script::{Handler15Invocation, Handler15TargetPayload}; use fparkan_world::{identity_metadata, transform_state, ObjectHandle, World}; use std::collections::VecDeque; #[derive(Clone)] -pub(super) enum UnitOrder { +pub(crate) enum UnitOrder { Move([f32; 2]), Attack(ObjectHandle, usize), Patrol([f32; 2], f32), Stand, Route(Vec<[f32; 2]>), + StrategicPatrol(PatrolTarget), Shutdown, Resume(Box<PatrollingUnit>), } @@ -20,7 +24,7 @@ impl UnitOrder { match self { Self::Move(_) | Self::Route(_) => 2, Self::Attack(..) => 3, - Self::Patrol(..) => 4, + Self::Patrol(..) | Self::StrategicPatrol(_) => 4, Self::Shutdown => 0x13, Self::Stand => 0x15, Self::Resume(_) => unreachable!("a suspended order has already started"), @@ -30,6 +34,7 @@ impl UnitOrder { fn targets_object(&self, target: ObjectHandle) -> bool { match self { Self::Attack(handle, _) => *handle == target, + Self::StrategicPatrol(PatrolTarget::Object(handle)) => *handle == target, Self::Resume(unit) => unit.has_object_order(target), _ => false, } @@ -53,13 +58,7 @@ 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); + pub(crate) fn command_order(&mut self, world: &World, actor: ObjectHandle, order: UnitOrder) { self.script_orders.retain(|(unit, _)| *unit != actor); if self.is_leaving_factory(actor) { self.script_orders.push((actor, VecDeque::from([order]))); @@ -118,6 +117,8 @@ impl UnitPatrol { UnitOrder::Move(point) | UnitOrder::Patrol(point, _) => points.push(*point), UnitOrder::Resume(unit) => unit.append_point_orders(&mut points), UnitOrder::Route(route) => points.extend(route), + UnitOrder::StrategicPatrol(PatrolTarget::Point(point)) => points.push(*point), + UnitOrder::StrategicPatrol(PatrolTarget::Object(_)) => {} UnitOrder::Attack(..) | UnitOrder::Shutdown | UnitOrder::Stand => {} } } @@ -304,6 +305,23 @@ impl UnitPatrol { } return true; } + UnitOrder::StrategicPatrol(target) => { + // iron3d+0x7a390 supplies this radius for both target forms. + let radius = 300.0; + match *target { + PatrolTarget::Point(point) => { + self.order(world, actor, point, radius); + } + PatrolTarget::Object(target) => { + let Ok(pose) = transform_state(world, target) else { + return false; + }; + let [x, y, _] = pose.position.map(f32::from_bits); + self.order_around_object(world, actor, target, [x, y], radius); + } + } + return true; + } UnitOrder::Move(point) => { self.move_to(world, actor, *point, false); eprintln!( @@ -462,7 +480,7 @@ mod tests { 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.command_order(&world, actor, UnitOrder::Stand); patrol.advance_script_orders(&world, &[]); assert_eq!(patrol.active_order_code(actor), 0x15); assert!(!patrol.is_shut_down(actor)); @@ -470,12 +488,49 @@ mod tests { 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())); + patrol.command_order(&world, actor, UnitOrder::Route(points.clone())); assert_eq!(patrol.active_order_code(actor), 2); assert_eq!(patrol.satellite_route(actor), points); } #[test] + fn command_panel_patrol_replaces_the_route_and_tracks_an_object_center() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let target = object(&mut world); + let mut patrol = UnitPatrol::scripted(); + patrol.command_order(&world, actor, UnitOrder::Route(vec![[45.0, 60.0]])); + + let point = [100.0, 200.0]; + patrol.command_order( + &world, + actor, + UnitOrder::StrategicPatrol(PatrolTarget::Point(point)), + ); + assert_eq!(patrol.active_order_code(actor), 4); + assert_eq!(patrol.satellite_route(actor), [point]); + assert_eq!(patrol.attack_limit(&world, actor).unwrap().radius, 300.0); + + place(&mut world, target, 250.0); + patrol.command_order( + &world, + actor, + UnitOrder::StrategicPatrol(PatrolTarget::Object(target)), + ); + assert!(patrol.has_object_order(actor, target)); + assert_eq!( + patrol.attack_limit(&world, actor).unwrap().center, + [250.0, 0.0] + ); + place(&mut world, target, 360.0); + assert_eq!( + patrol.attack_limit(&world, actor).unwrap().center, + [360.0, 0.0] + ); + assert_eq!(patrol.active_order_code(actor), 4); + } + + #[test] fn move_attack_and_shutdown_wait_for_the_preceding_task() { let mut world = fparkan_world::new(Default::default()); let actor = object(&mut world); |
