aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 22:23:04 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 22:23:04 +0300
commit7848a77aef2995d855a5b07de138c8b9d3f6fdc8 (patch)
treea7dbc7c1f5cda441b65aad7875c11dcf5e51d176 /crates/fparkan-runtime/src
parent90ea3afec48e2bac7016db08987e5c94878b1993 (diff)
downloadfparkan-7848a77aef2995d855a5b07de138c8b9d3f6fdc8.tar.xz
fparkan-7848a77aef2995d855a5b07de138c8b9d3f6fdc8.zip
feat(ui): draw selected warbot routes on the satellite map
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/satellite_contacts.rs51
-rw-r--r--crates/fparkan-runtime/src/unit_patrol/orders.rs54
2 files changed, 98 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs
index a766e911..f3867ac8 100644
--- a/crates/fparkan-runtime/src/satellite_contacts.rs
+++ b/crates/fparkan-runtime/src/satellite_contacts.rs
@@ -15,11 +15,12 @@ pub struct BuildingContact {
}
/// The original mark for a moving unit within the satellite image.
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnitContact {
pub pixel: [i32; 2],
pub cross: bool,
pub heading: Option<UnitHeading>,
+ pub route: Vec<[i32; 2]>,
}
/// The selected vehicle or unboarded hero's outline and heading stroke.
@@ -55,8 +56,14 @@ pub fn owned_units(
engine: &Engine,
cockpit: StartingCockpit,
extent: f32,
+ selected: Option<ObjectHandle>,
) -> impl Iterator<Item = UnitContact> + '_ {
let boarded = crate::player_vehicle(engine).map(|vehicle| vehicle.handle);
+ let selected = boarded.or(selected);
+ let patrol = engine
+ .loaded
+ .as_ref()
+ .and_then(|mission| mission.unit_patrol.as_ref());
owned_contacts(engine, cockpit, extent, move |draft, class| {
let hero = class == crate::HERO_UNIT_CLASS;
if class & crate::autodemo::MOBILE_UNIT == 0 || (hero && boarded.is_some()) {
@@ -71,7 +78,7 @@ pub fn owned_units(
Some((hero, flags.chassis_type == Some(1)))
})
.map(move |(pixel, handle, pose, (hero, cross))| {
- let selected = boarded == Some(handle);
+ let selected = selected == Some(handle);
UnitContact {
pixel,
cross,
@@ -80,6 +87,13 @@ pub fn owned_units(
} else {
None
},
+ route: patrol.filter(|_| selected).map_or_else(Vec::new, |patrol| {
+ patrol
+ .satellite_route(handle)
+ .into_iter()
+ .filter_map(|xy| project(xy, extent))
+ .collect()
+ }),
}
})
}
@@ -226,18 +240,41 @@ mod tests {
let mut pose = transform_state(&engine.world, handle).unwrap();
let [x, y, _] = pose.position.map(f32::from_bits);
let start = project([x, y], extent).unwrap();
- assert!(!owned_units(&engine, cockpit, extent).any(|contact| contact.pixel == start));
+ assert!(!owned_units(&engine, cockpit, extent, Some(handle))
+ .any(|contact| contact.pixel == start));
set_owner(&mut engine.world, handle, owner).unwrap();
- let captured = owned_units(&engine, cockpit, extent)
+ let destination = [x, y + 50.0];
+ engine
+ .loaded
+ .as_mut()
+ .unwrap()
+ .unit_patrol
+ .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted)
+ .move_to(&engine.world, handle, destination, false);
+ let chosen = owned_units(&engine, cockpit, extent, Some(handle))
+ .find(|contact| contact.pixel == start)
+ .expect("selected warbot mark");
+ assert!(chosen.heading.is_some_and(|heading| heading.selected));
+ assert_eq!(chosen.route, [project(destination, extent).unwrap()]);
+ let captured = owned_units(&engine, cockpit, extent, None)
.find(|contact| contact.pixel == start)
.expect("captured warbot mark");
assert_eq!(captured.heading, None);
+ assert!(captured.route.is_empty());
+ engine
+ .loaded
+ .as_mut()
+ .unwrap()
+ .unit_patrol
+ .as_mut()
+ .unwrap()
+ .clear_move(handle);
pose.position[0] = (x + extent / 8.0).to_bits();
set_transform(&mut engine.world, handle, pose).unwrap();
let moved = project([x + extent / 8.0, y], extent).unwrap();
assert_eq!(moved, [start[0] + 32, start[1]]);
- let units: Vec<_> = owned_units(&engine, cockpit, extent).collect();
+ let units: Vec<_> = owned_units(&engine, cockpit, extent, None).collect();
let moved_contact = UnitContact {
pixel: moved,
..captured
@@ -255,7 +292,7 @@ mod tests {
let vehicle = crate::board_friendly_warbot(&mut engine, Some(handle))
.unwrap()
.unwrap();
- let headings: Vec<_> = owned_units(&engine, vehicle, extent)
+ let headings: Vec<_> = owned_units(&engine, vehicle, extent, None)
.filter(|unit| unit.heading.is_some())
.collect();
assert_eq!(
@@ -266,7 +303,7 @@ mod tests {
}]
);
assert_eq!(crate::disembark_warbot(&mut engine).unwrap(), Some(cockpit));
- let headings: Vec<_> = owned_units(&engine, cockpit, extent)
+ let headings: Vec<_> = owned_units(&engine, cockpit, extent, None)
.filter_map(|unit| unit.heading)
.collect();
assert_eq!(headings, [unit_heading(pose, false, false).unwrap()]);
diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs
index cde2bd09..560f2142 100644
--- a/crates/fparkan-runtime/src/unit_patrol/orders.rs
+++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs
@@ -50,6 +50,32 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool {
}
impl UnitPatrol {
+ pub(crate) fn satellite_route(&self, actor: ObjectHandle) -> Vec<[f32; 2]> {
+ let active = self.units.iter().find(|unit| unit.handle == actor);
+ // iron3d+0x77ae0 prefers the path whenever there are unvisited points.
+ if let Some(unit) = active.filter(|unit| !unit.points.is_empty()) {
+ return unit.points.iter().copied().collect();
+ }
+ let mut points = Vec::new();
+ if let Some(unit) = active {
+ unit.append_point_orders(&mut points);
+ }
+ if let Some((_, queue)) = self
+ .script_orders
+ .iter()
+ .find(|(handle, _)| *handle == actor)
+ {
+ for order in queue {
+ match order {
+ UnitOrder::Move(point) | UnitOrder::Patrol(point, _) => points.push(*point),
+ UnitOrder::Resume(unit) => unit.append_point_orders(&mut points),
+ UnitOrder::Attack(..) | UnitOrder::Shutdown => {}
+ }
+ }
+ }
+ points
+ }
+
pub(crate) fn clear_capture_order(&mut self, unit: ObjectHandle, building: ObjectHandle) {
self.units
.retain(|order| order.handle != unit || order.capture_target != Some(building));
@@ -314,6 +340,16 @@ impl UnitPatrol {
}
}
+impl PatrollingUnit {
+ fn append_point_orders(&self, points: &mut Vec<[f32; 2]>) {
+ if let Some(destinations) = &self.destinations {
+ points.extend(destinations);
+ } else if self.patrols_point(self.center) && self.raid_factory.is_none() {
+ points.push(self.center);
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -334,6 +370,24 @@ mod tests {
}
#[test]
+ fn satellite_route_prefers_remaining_waypoints_to_coordinate_orders() {
+ let mut world = fparkan_world::new(Default::default());
+ let actor = object(&mut world);
+ let enemy = object(&mut world);
+ let mut patrol = UnitPatrol::scripted();
+ patrol.enqueue_script_order(&world, actor, UnitOrder::Move([20.0, 30.0]), false);
+ patrol.enqueue_script_order(&world, actor, UnitOrder::Attack(enemy, 1), true);
+ patrol.enqueue_script_order(&world, actor, UnitOrder::Patrol([40.0, 50.0], 60.0), true);
+ assert_eq!(patrol.satellite_route(actor), [[20.0, 30.0], [40.0, 50.0]]);
+
+ patrol.units[0].points = VecDeque::from([[10.0, 18.0], [20.0, 30.0]]);
+ assert_eq!(patrol.satellite_route(actor), [[10.0, 18.0], [20.0, 30.0]]);
+ patrol.units[0].points.pop_front();
+ assert_eq!(patrol.satellite_route(actor), [[20.0, 30.0]]);
+ assert!(patrol.satellite_route(enemy).is_empty());
+ }
+
+ #[test]
fn move_attack_and_shutdown_wait_for_the_preceding_task() {
let mut world = fparkan_world::new(Default::default());
let actor = object(&mut world);