aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/clan_script_init.rs24
-rw-r--r--crates/fparkan-runtime/src/lib.rs69
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs7
-rw-r--r--crates/fparkan-runtime/src/script_patrol.rs33
-rw-r--r--crates/fparkan-runtime/src/unit_combat.rs (renamed from crates/fparkan-runtime/src/autodemo_combat.rs)3
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs43
6 files changed, 127 insertions, 52 deletions
diff --git a/crates/fparkan-runtime/src/clan_script_init.rs b/crates/fparkan-runtime/src/clan_script_init.rs
index 98d899c2..2d5ac129 100644
--- a/crates/fparkan-runtime/src/clan_script_init.rs
+++ b/crates/fparkan-runtime/src/clan_script_init.rs
@@ -1,8 +1,11 @@
use crate::script_patrol::ScriptPatrol;
-use fparkan_script::{ScriptDispatchSelector, ScriptInstruction, VarSet, VarSetDefault};
+use fparkan_script::{
+ Handler15TargetPayload, ScriptDispatchSelector, ScriptInstruction, VarSet, VarSetDefault,
+};
pub(super) enum InitOrder {
Patrol(ScriptPatrol),
+ Shutdown(u32),
Reserve {
clan: usize,
id: u32,
@@ -24,22 +27,27 @@ pub(super) fn reservation(
Some((id, flag != 0))
}
-pub(super) fn patrol_order(
+pub(super) fn unit_order(
declarations: &VarSet,
instruction: &ScriptInstruction,
values: &[VarSetDefault],
-) -> Option<ScriptPatrol> {
+) -> Option<InitOrder> {
if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(15)
|| instruction.header_words[1] != u32::MAX
|| instruction.header_words[3] != u32::MAX
{
return None;
}
- ScriptPatrol::from_order(
- declarations
- .resolve_handler15_with_values(instruction, values)
- .ok()?,
- )
+ let order = declarations
+ .resolve_handler15_with_values(instruction, values)
+ .ok()?;
+ if matches!(
+ (order.word_1, order.word_2, order.target),
+ (0x13, 3, Handler15TargetPayload::None)
+ ) {
+ return Some(InitOrder::Shutdown(order.word_0));
+ }
+ ScriptPatrol::from_order(order).map(InitOrder::Patrol)
}
pub(super) fn stubbed_handler(instruction: &ScriptInstruction) -> Option<u8> {
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 6bd697a8..624560a3 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -42,7 +42,6 @@ use std::num::NonZeroUsize;
use std::sync::Arc;
mod autodemo;
-mod autodemo_combat;
mod boarding;
mod building_door;
mod clan_script_init;
@@ -57,12 +56,12 @@ mod projectile;
mod research;
mod script_patrol;
mod script_time;
+mod unit_combat;
mod unit_patrol;
pub use autodemo::{
autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView, SpawnedUnit,
};
-pub use autodemo_combat::{AutoDemoCombatEvent, AutoDemoProjectileType};
pub use boarding::{board_friendly_warbot, disembark_warbot, player_vehicle};
pub use building_door::{has_proximity_doors, sample_proximity_doors};
pub use fparkan_assets::MissionAssets;
@@ -87,6 +86,7 @@ pub use projectile::{
projectile_unit_hit, PlayerProjectile, ProjectileAssets, ProjectileContact,
};
pub use research::order_lab_research;
+pub use unit_combat::{AutoDemoCombatEvent, AutoDemoProjectileType};
const MISSION_DECODED_PAYLOAD_CACHE_ENTRIES: usize = 256;
const MAX_MISSION_AI_RECORD_SLOTS: usize = 1_000_000;
@@ -1327,7 +1327,7 @@ pub struct Engine {
world: World,
loaded: Option<LoadedMissionState>,
behavior_rng_state: u32,
- autodemo_combat: Option<autodemo_combat::AutoDemoCombat>,
+ autodemo_combat: Option<unit_combat::AutoDemoCombat>,
autodemo_reinforcements: Option<autodemo::LiveReinforcements>,
production: Option<production::Production>,
}
@@ -2442,22 +2442,25 @@ fn load_mission_with_options(
&object_drafts[..reserve_start],
&handles[..reserve_start],
),
- MissionMode::SingleMission => (init_orders
+ MissionMode::SingleMission => (init_orders.iter().any(|order| {
+ matches!(
+ order,
+ clan_script_init::InitOrder::Patrol(_)
+ | clan_script_init::InitOrder::Shutdown(_)
+ )
+ }) || loaded_scripts
+ .bundles
.iter()
- .any(|order| matches!(order, clan_script_init::InitOrder::Patrol(_)))
- || loaded_scripts
- .bundles
- .iter()
- .find(|bundle| bundle.clan_index == 0)
- .is_some_and(|bundle| {
- bundle.package.events.iter().any(|event| {
- event.name_raw.as_slice() == b"Mission\0"
- && event.instructions.iter().any(|instruction| {
- instruction.dispatch_selector()
- == fparkan_script::ScriptDispatchSelector::Handler(15)
- })
- })
- }))
+ .find(|bundle| bundle.clan_index == 0)
+ .is_some_and(|bundle| {
+ bundle.package.events.iter().any(|event| {
+ event.name_raw.as_slice() == b"Mission\0"
+ && event.instructions.iter().any(|instruction| {
+ instruction.dispatch_selector()
+ == fparkan_script::ScriptDispatchSelector::Handler(15)
+ })
+ })
+ }))
.then(unit_patrol::UnitPatrol::scripted),
}
} else {
@@ -2537,10 +2540,28 @@ fn load_mission_with_options(
clan_script_init::InitOrder::Reserve { clan, id, reserved } => {
mission_events.set_reservation(&mission, &new_runtime_world, clan, id, reserved);
}
+ clan_script_init::InitOrder::Shutdown(id) => {
+ let Some(patrol) = &mut unit_patrol else {
+ continue;
+ };
+ if let Some(handle) = script_patrol::warbot_by_logic_id(
+ &mission,
+ &object_drafts,
+ &new_runtime_world,
+ id,
+ ) {
+ if patrol.shut_down(handle) {
+ eprintln!(
+ "sim_coverage unit_shutdown_approx=1 unit={} id={id}",
+ handle.slot
+ );
+ }
+ }
+ }
}
}
let autodemo_combat = (unit_patrol.is_some() || guard_clans.contains(&true)).then(|| {
- autodemo_combat::AutoDemoCombat::new(
+ unit_combat::AutoDemoCombat::new(
&vfs,
&mission,
&object_drafts,
@@ -2917,7 +2938,7 @@ pub fn autodemo_projectile_types(engine: &Engine) -> &[AutoDemoProjectileType] {
engine
.autodemo_combat
.as_ref()
- .map_or(&[], autodemo_combat::AutoDemoCombat::types)
+ .map_or(&[], unit_combat::AutoDemoCombat::types)
}
/// Active autonomous flights in the current world.
@@ -2927,7 +2948,7 @@ pub fn autodemo_projectiles(
engine
.autodemo_combat
.iter()
- .flat_map(autodemo_combat::AutoDemoCombat::flights)
+ .flat_map(unit_combat::AutoDemoCombat::flights)
}
/// Counts autonomous weapons or projectile profiles that could not be prepared.
@@ -2945,7 +2966,7 @@ pub fn mission_guard_fallback_count(engine: &Engine) -> usize {
engine
.autodemo_combat
.as_ref()
- .map_or(0, autodemo_combat::AutoDemoCombat::guard_clan_count)
+ .map_or(0, unit_combat::AutoDemoCombat::guard_clan_count)
}
/// Counts AI clans approximating the scripted raid on a hostile factory.
@@ -8138,9 +8159,9 @@ fn resolve_init_states(
return Some(Some(write));
}
if let Some(order) =
- clan_script_init::patrol_order(&varset.declarations, instruction, values)
+ clan_script_init::unit_order(&varset.declarations, instruction, values)
{
- init_orders.push(clan_script_init::InitOrder::Patrol(order));
+ init_orders.push(order);
return Some(None);
}
if let Some((id, reserved)) = clan_script_init::reservation(instruction, values)
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index d63ea43c..1d4c7856 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -653,10 +653,9 @@ impl LoadedMissionState {
.ok()
.flatten()
.is_none()
- || self
- .unit_patrol
- .as_ref()
- .is_some_and(|patrol| patrol.is_patrolling(handle))
+ || self.unit_patrol.as_ref().is_some_and(|patrol| {
+ patrol.is_patrolling(handle) || patrol.is_shut_down(handle)
+ })
|| self
.boarding
.is_some_and(|boarding| boarding.cockpit.handle == handle)
diff --git a/crates/fparkan-runtime/src/script_patrol.rs b/crates/fparkan-runtime/src/script_patrol.rs
index 6d349c9b..d67874c6 100644
--- a/crates/fparkan-runtime/src/script_patrol.rs
+++ b/crates/fparkan-runtime/src/script_patrol.rs
@@ -3,6 +3,26 @@ use crate::{MissionDocument, MissionObjectDraft};
use fparkan_script::{Handler15Invocation, Handler15TargetPayload};
use fparkan_world::{handle_by_original_id, ObjectHandle, OriginalObjectId, World};
+pub(super) fn warbot_by_logic_id(
+ mission: &MissionDocument,
+ drafts: &[MissionObjectDraft],
+ world: &World,
+ id: u32,
+) -> Option<ObjectHandle> {
+ mission
+ .objects
+ .iter()
+ .zip(drafts)
+ .enumerate()
+ .find_map(|(index, (object, draft))| {
+ (id != u32::MAX
+ && object.identity_or_clan_raw == id
+ && draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS))
+ .then(|| handle_by_original_id(world, OriginalObjectId(index as u32)))
+ .flatten()
+ })
+}
+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct ScriptPatrol {
pub(super) actor_id: u32,
@@ -31,18 +51,7 @@ impl ScriptPatrol {
drafts: &[MissionObjectDraft],
world: &World,
) -> Option<ObjectHandle> {
- let (index, _) =
- mission
- .objects
- .iter()
- .zip(drafts)
- .enumerate()
- .find(|(_, (object, draft))| {
- self.actor_id != u32::MAX
- && object.identity_or_clan_raw == self.actor_id
- && draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS)
- })?;
- let handle = handle_by_original_id(world, OriginalObjectId(index as u32))?;
+ let handle = warbot_by_logic_id(mission, drafts, world, self.actor_id)?;
// shortcut: use the verified default radius until per-unit patrol profiles are wired.
let radius = match self.radius {
0 | u32::MAX => 60.0,
diff --git a/crates/fparkan-runtime/src/autodemo_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs
index fea7a8ab..56141775 100644
--- a/crates/fparkan-runtime/src/autodemo_combat.rs
+++ b/crates/fparkan-runtime/src/unit_combat.rs
@@ -246,6 +246,9 @@ impl AutoDemoCombat {
.collect();
live.iter()
.filter_map(|&(handle, clan, from, _, position)| {
+ if patrol.is_some_and(|patrol| patrol.is_shut_down(handle)) {
+ return None;
+ }
let limit = patrol.and_then(|patrol| patrol.attack_limit(handle));
if limit.is_some_and(|limit| !limit.allows_attack(position)) {
return None;
diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs
index 205af4ea..8be60d89 100644
--- a/crates/fparkan-runtime/src/unit_patrol.rs
+++ b/crates/fparkan-runtime/src/unit_patrol.rs
@@ -1,19 +1,21 @@
use super::{
autodemo::{MOBILE_UNIT, PLANT_CLASS},
- autodemo_combat::CombatTarget,
boarding::WARBOT_CLASS,
- next_crt_rand, EngineError, MissionDocument, MissionObjectDraft, MissionScriptBundle,
- ObjectHandle, TerrainWorld, TransformState, World,
+ next_crt_rand,
+ unit_combat::CombatTarget,
+ EngineError, MissionDocument, MissionObjectDraft, MissionScriptBundle, ObjectHandle,
+ TerrainWorld, TransformState, World,
};
use fparkan_script::{Handler15TargetPayload, ScriptDispatchSelector, VarSet};
use fparkan_terrain::{NavigationQuery, RouteRequest, SurfaceQuery};
use fparkan_world::{set_transform, transform_state};
-use std::collections::VecDeque;
+use std::collections::{HashSet, VecDeque};
#[derive(Clone)]
pub(super) struct UnitPatrol {
center: Option<[f32; 2]>,
units: Vec<PatrollingUnit>,
+ shut_down: HashSet<ObjectHandle>,
clock_ms: u32,
rng: u32,
pub(super) factory_raid_clans: usize,
@@ -132,6 +134,7 @@ impl UnitPatrol {
Self {
center: None,
units: Vec::new(),
+ shut_down: HashSet::new(),
clock_ms: 0,
rng: 1,
factory_raid_clans: 0,
@@ -145,6 +148,7 @@ impl UnitPatrol {
center: [f32; 2],
radius: f32,
) -> bool {
+ self.shut_down.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) {
@@ -174,6 +178,16 @@ impl UnitPatrol {
self.units.iter().any(|unit| unit.handle == handle)
}
+ pub(super) fn shut_down(&mut self, handle: ObjectHandle) -> bool {
+ // 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)
+ }
+
+ pub(super) fn is_shut_down(&self, handle: ObjectHandle) -> bool {
+ self.shut_down.contains(&handle)
+ }
+
pub(super) fn raid_factory(&self, handle: ObjectHandle) -> Option<(ObjectHandle, usize)> {
self.units
.iter()
@@ -230,6 +244,7 @@ impl UnitPatrol {
Some(Self {
center: Some(center),
units,
+ shut_down: HashSet::new(),
clock_ms: 0,
rng: 1,
factory_raid_clans: 0,
@@ -324,6 +339,26 @@ impl UnitPatrol {
}
}
+#[cfg(test)]
+#[test]
+fn shutdown_clears_patrol_until_a_new_order_arrives() {
+ let mut world = fparkan_world::new(fparkan_world::WorldConfig::default());
+ let handle = fparkan_world::construct_object(
+ &mut world,
+ fparkan_world::ObjectDraft { original_id: None },
+ )
+ .unwrap();
+ fparkan_world::register_object(&mut world, handle).unwrap();
+ let mut patrol = UnitPatrol::scripted();
+ assert!(patrol.order(&world, handle, [20.0, 30.0], 60.0));
+ assert!(patrol.shut_down(handle));
+ assert!(patrol.is_shut_down(handle));
+ assert!(!patrol.is_patrolling(handle));
+ assert!(patrol.order(&world, handle, [35.0, 45.0], 60.0));
+ assert!(!patrol.is_shut_down(handle));
+ assert!(patrol.is_patrolling(handle));
+}
+
fn distance_to_patrol([x, y]: [f32; 2], [px, py, _]: [f32; 3]) -> f32 {
// shortcut: use the horizontal distance until patrol orders carry their native Z.
(px - x).hypot(py - y)