aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/lib.rs1
-rw-r--r--crates/fparkan-runtime/src/satellite_contacts.rs3
-rw-r--r--crates/fparkan-runtime/src/strategic_orders.rs44
-rw-r--r--crates/fparkan-runtime/src/strategic_roster.rs1
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs5
-rw-r--r--crates/fparkan-runtime/src/unit_patrol/orders.rs65
6 files changed, 116 insertions, 3 deletions
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);