diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 168 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/clan_combat_groups.rs | 117 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 43 |
4 files changed, 275 insertions, 63 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 245868e0..99e568c6 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -1,6 +1,9 @@ use crate::clan_problem_conditions::Condition; use crate::clan_transport_schemes::TRANSPORT_CLASS; -use crate::{clan_combat_groups::CombatGroup, clan_targeting::CaptureApproach}; +use crate::{ + clan_combat_groups::{CombatGroup, GroupTarget}, + clan_targeting::CaptureApproach, +}; use crate::{BehaviorOreState, LoadedMissionState, MissionEventStop}; use fparkan_script::{ Handler15TargetPayload, Handler2Record, Handler2RecordInput, Handler2RecordKey, @@ -12,6 +15,7 @@ use std::collections::BTreeMap; const STORAGE_CLASS: u32 = 0x8000_0008; const RESOURCE_ORE: u32 = 0x0200_0100; const BUILDING_PROTECT: u32 = 8; +const PLACE_PROTECT: u32 = 11; const OPTIMAL_TRANSPORT: u32 = 4; const BUILDING_NEEDED: u32 = 3; @@ -84,12 +88,10 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) { }; crate::construction::tick(loaded, &mut engine.world, now_ms); loaded.clan_ai_events.groups.retain(|&(clan, _), group| { - if !identity_metadata(&engine.world, group.target) - .is_ok_and(|identity| identity.owner_id == group.target_owner) - { + if !group.target.valid(&engine.world) { if let Some(patrol) = &mut loaded.unit_patrol { for &member in &group.members { - patrol.clear_object_order(member, group.target); + group.target.clear_order(patrol, member); } } return false; @@ -100,7 +102,7 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) { && loaded .unit_patrol .as_ref() - .is_some_and(|patrol| patrol.has_object_order(member, group.target)) + .is_some_and(|patrol| group.target.has_order(patrol, member)) }); !group.members.is_empty() }); @@ -141,7 +143,7 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) { if let Some(group) = ai.groups.remove(&(clan, key)) { if let Some(patrol) = &mut loaded.unit_patrol { for member in group.members { - patrol.clear_object_order(member, group.target); + group.target.clear_order(patrol, member); } } } @@ -246,6 +248,8 @@ pub(super) struct ClanAiEvents { construction_orders: BTreeMap<(usize, u32, u32), (u64, u64)>, patrol_continues: BTreeMap<(usize, u32), u64>, group_orders: BTreeMap<(usize, u32), u64>, + point_group_orders: BTreeMap<(usize, u32, u32), u64>, + point_patrol_continues: BTreeMap<(usize, u32, u32), u64>, } impl LoadedMissionState { @@ -287,7 +291,7 @@ impl LoadedMissionState { continue; }; let mut problems_event = Some((event_index, event, None::<Handler2Record>)); - // shortcut: only factory protection resumes, on the shared clan interval without native RNG. + // shortcut: protection resumes on the shared clan interval without native RNG. let mut pending = self .clan_ai_events .problems @@ -296,7 +300,7 @@ impl LoadedMissionState { .flat_map(|queue| queue.records()) .rev() .filter(|record| { - record.key.word_0 == BUILDING_PROTECT + matches!(record.key.word_0, BUILDING_PROTECT | PLACE_PROTECT) && self.clan_ai_events.waits.contains_key(&(clan, record.key)) }) .cloned() @@ -326,15 +330,28 @@ impl LoadedMissionState { continue; }; if resume { - let target = problem.key.argument_0; - let count = self - .clan_ai_events - .patrol_continues - .entry((clan, target)) - .or_default(); - *count += 1; - if *count == 1 { - eprintln!("sim_coverage script_factory_patrol_continue_approx=1 clan={clan} target={target}"); + if problem_id == PLACE_PROTECT { + let [x, y] = [problem.key.argument_0, problem.key.argument_1]; + let count = self + .clan_ai_events + .point_patrol_continues + .entry((clan, x, y)) + .or_default(); + *count += 1; + if *count == 1 { + eprintln!("sim_coverage script_point_patrol_continue_approx=1 clan={clan} x={x} y={y}"); + } + } else { + let target = problem.key.argument_0; + let count = self + .clan_ai_events + .patrol_continues + .entry((clan, target)) + .or_default(); + *count += 1; + if *count == 1 { + eprintln!("sim_coverage script_factory_patrol_continue_approx=1 clan={clan} target={target}"); + } } } else { let count = self @@ -449,14 +466,21 @@ impl LoadedMissionState { Ok(Some(VarSetDefault::Dword(target))) } ScriptDispatchSelector::Handler(25) if problem.is_some() => { - let &[mode, hits, target_mode, target] = instruction.references.as_slice() else { - return Err("Handler(25) requires TAKE_BY_HITS and a logical ID".into()); + let &[mode, hits, target_mode, ref target @ ..] = instruction.references.as_slice() else { + return Err("Handler(25) requires TAKE_BY_HITS and a target".into()); }; let word = |index: u32| values.get(index as usize)?.as_dword(); - let (Some(2), Some(requested), Some(0x201), Some(target)) = - (word(mode), word(hits), word(target_mode), word(target)) + let (Some(2), Some(requested)) = (word(mode), word(hits)) else { - return Err("Handler(25) requires TAKE_BY_HITS and a logical ID".into()); + return Err("Handler(25) requires TAKE_BY_HITS".into()); + }; + let target = match (word(target_mode), target) { + (Some(0x201), &[id]) => Handler15TargetPayload::ByLogicId(word(id).ok_or("Handler(25) logical ID must be a DWORD")?), + (Some(0x202), &[x, y]) => Handler15TargetPayload::ByPlace { + first: word(x).ok_or("Handler(25) X must be a DWORD")?, + second: word(y).ok_or("Handler(25) Y must be a DWORD")?, + }, + _ => return Err("Handler(25) requires a logical ID or XY point".into()), }; // shortcut: use free warbots until lower-priority orders can be preempted. let units = self.mission_events.clan_units(world, OwnerId(clan as u16), true) @@ -480,13 +504,15 @@ impl LoadedMissionState { group.dispatch(patrol, world, &self.mission, order) .ok_or("Handler(28) requires a patrol or attack on the group's target")?; let key = problem.as_ref().expect("group order requires a problem").key; - let target = group.target.slot; - let count = self.clan_ai_events.group_orders.entry((clan, target)).or_default(); + let (name, count) = match group.target { + GroupTarget::Object(target, _) => ("script_group_order_approx", self.clan_ai_events.group_orders.entry((clan, target.slot)).or_default()), + GroupTarget::Point([x, y]) => ("script_point_group_order_approx", self.clan_ai_events.point_group_orders.entry((clan, x, y)).or_default()), + }; *count += 1; - eprintln!("sim_coverage script_group_order_approx=1 clan={clan} target={target} order={} units={:?}", order.word_1, group.members.iter().map(|unit| unit.slot).collect::<Vec<_>>()); + eprintln!("sim_coverage {name}=1 clan={clan} target={:?} order={} units={:?}", group.target, order.word_1, group.members.iter().map(|unit| unit.slot).collect::<Vec<_>>()); let groups = &mut self.clan_ai_events.groups; if let Some(assigned) = groups.get_mut(&(clan, key)) - .filter(|assigned| key.word_0 == BUILDING_PROTECT + .filter(|assigned| matches!(key.word_0, BUILDING_PROTECT | PLACE_PROTECT) && assigned.target == group.target) { assigned.members.extend(group.members); @@ -738,14 +764,14 @@ impl LoadedMissionState { } ScriptDispatchSelector::Handler(handler @ (7 | 45)) if instruction.references.is_empty() - && problem.as_ref().is_some_and(|record| record.key.word_0 == BUILDING_PROTECT) => + && problem.as_ref().is_some_and(|record| matches!(record.key.word_0, BUILDING_PROTECT | PLACE_PROTECT)) => { - let key = problem.as_ref().expect("factory protection problem").key; + let key = problem.as_ref().expect("protection problem").key; if handler == 7 { if let Some(assigned) = self.clan_ai_events.groups.remove(&(clan, key)) { if let Some(patrol) = &mut self.unit_patrol { for unit in assigned.members { - patrol.clear_object_order(unit, assigned.target); + assigned.target.clear_order(patrol, unit); } } } @@ -762,7 +788,7 @@ impl LoadedMissionState { Some(1) if problem .as_ref() - .is_some_and(|record| matches!(record.key.word_0, OPTIMAL_TRANSPORT | 6 | BUILDING_PROTECT)) + .is_some_and(|record| matches!(record.key.word_0, OPTIMAL_TRANSPORT | 6 | BUILDING_PROTECT | PLACE_PROTECT)) || !conditions.is_empty() => { solving = true @@ -1126,7 +1152,10 @@ impl LoadedMissionState { .remove(problem.key); } else if solving && (!conditions.is_empty() - || matches!(problem.key.word_0, OPTIMAL_TRANSPORT | BUILDING_PROTECT)) + || matches!( + problem.key.word_0, + OPTIMAL_TRANSPORT | BUILDING_PROTECT | PLACE_PROTECT + )) { let count = self .clan_ai_events @@ -1306,6 +1335,20 @@ impl ClanAiEvents { .map(|(&(clan, target), &count)| (clan, target, count)) } + pub(super) fn point_group_orders(&self) -> impl Iterator<Item = (usize, u32, u32, u64)> + '_ { + self.point_group_orders + .iter() + .map(|(&(clan, x, y), &count)| (clan, x, y, count)) + } + + pub(super) fn point_patrol_continues( + &self, + ) -> impl Iterator<Item = (usize, u32, u32, u64)> + '_ { + self.point_patrol_continues + .iter() + .map(|(&(clan, x, y), &count)| (clan, x, y, count)) + } + pub(super) fn transport_orders(&self) -> impl Iterator<Item = (usize, u32, u64)> + '_ { self.transport_orders .iter() @@ -1425,6 +1468,62 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn licensed_point_protection_patrols_and_resumes_from_stock_start() { + use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput}; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus root"); + let mut engine = + crate::create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + crate::load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/CAMPAIGN/CAMPAIGN.02/Mission.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + + for now in (0..=150_000).step_by(100) { + crate::frame(&mut engine, now).unwrap(); + } + let loaded = engine.loaded.as_ref().unwrap(); + assert!(loaded + .clan_ai_events + .point_group_orders() + .any(|(_, _, _, count)| count > 0)); + assert!(loaded + .clan_ai_events + .point_patrol_continues() + .any(|(_, _, _, count)| count > 1)); + let patrol = loaded.unit_patrol.as_ref().unwrap(); + let group = loaded + .clan_ai_events + .groups + .iter() + .find_map(|(&(_, key), group)| { + (key.word_0 == PLACE_PROTECT && matches!(group.target, GroupTarget::Point(_))) + .then_some(group) + }) + .expect("point protection keeps its group between script invocations"); + assert!(!group.members.is_empty()); + assert!(group + .members + .iter() + .all(|&unit| group.target.has_order(patrol, unit))); + assert!(loaded.clan_ai_events.stops().all(|(stop, _)| { + !stop.reason.starts_with("Handler(25)") && !stop.reason.starts_with("Handler(28)") + })); + } + + #[test] + #[ignore = "requires licensed corpus"] fn licensed_weak_building_gets_scripted_guards_from_stock_start() { use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput}; use fparkan_vfs::DirectoryVfs; @@ -1471,7 +1570,7 @@ mod tests { .then_some(group) }) .expect("weak building receives a guard group"); - assert_eq!(group.target, target); + assert_eq!(group.target, GroupTarget::Object(target, Some(OwnerId(1)))); assert!(!group.members.is_empty()); let patrol = loaded.unit_patrol.as_ref().unwrap(); assert!(group @@ -1535,8 +1634,7 @@ mod tests { owner == *clan && key.word_0 == BUILDING_PROTECT && key.argument_0 == *factory_id }) .unwrap(); - assert_eq!(group.target, factory); - assert_eq!(group.target_owner, Some(OwnerId(1))); + assert_eq!(group.target, GroupTarget::Object(factory, Some(OwnerId(1)))); assert!(!group.members.is_empty()); let patrol = loaded.unit_patrol.as_ref().unwrap(); assert!(group diff --git a/crates/fparkan-runtime/src/clan_combat_groups.rs b/crates/fparkan-runtime/src/clan_combat_groups.rs index 5b4cf453..4a758388 100644 --- a/crates/fparkan-runtime/src/clan_combat_groups.rs +++ b/crates/fparkan-runtime/src/clan_combat_groups.rs @@ -6,8 +6,70 @@ use fparkan_world::{identity_metadata, transform_state, ObjectHandle, OwnerId, W pub(super) struct CombatGroup { pub(super) members: Vec<ObjectHandle>, - pub(super) target: ObjectHandle, - pub(super) target_owner: Option<OwnerId>, + pub(super) target: GroupTarget, +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) enum GroupTarget { + Object(ObjectHandle, Option<OwnerId>), + Point([u32; 2]), +} + +impl GroupTarget { + fn resolve( + world: &World, + mission: &MissionDocument, + target: Handler15TargetPayload, + ) -> Option<Self> { + match target { + Handler15TargetPayload::ByLogicId(id) => { + let handle = + crate::mission_events::placed_logic_objects(mission, world, id).next()?; + Some(Self::Object( + handle, + identity_metadata(world, handle).ok()?.owner_id, + )) + } + Handler15TargetPayload::ByPlace { first, second } => Some(Self::Point([first, second])), + _ => None, + } + } + + fn position(self, world: &World) -> Option<[f32; 2]> { + match self { + Self::Object(handle, _) => { + let [x, y, _] = transform_state(world, handle) + .ok()? + .position + .map(f32::from_bits); + Some([x, y]) + } + Self::Point(point) => Some(point.map(|value| value as f32)), + } + } + + pub(super) fn valid(self, world: &World) -> bool { + match self { + Self::Object(handle, owner) => { + identity_metadata(world, handle).is_ok_and(|identity| identity.owner_id == owner) + } + Self::Point(_) => true, + } + } + + pub(super) fn has_order(self, patrol: &UnitPatrol, unit: ObjectHandle) -> bool { + match self { + Self::Object(target, _) => patrol.has_object_order(unit, target), + Self::Point(point) => patrol.has_point_order(unit, point.map(|value| value as f32)), + } + } + + pub(super) fn clear_order(self, patrol: &mut UnitPatrol, unit: ObjectHandle) { + match self { + Self::Object(target, _) => patrol.clear_object_order(unit, target), + Self::Point(point) => patrol.clear_point_order(unit, point.map(|value| value as f32)), + } + } } pub(super) fn recruit( @@ -15,18 +77,15 @@ pub(super) fn recruit( units: impl Iterator<Item = ObjectHandle>, controls: &[MissionControlDeviceTableState], mission: &MissionDocument, - target_id: u32, + target: Handler15TargetPayload, requested: u32, ) -> (Option<CombatGroup>, f32) { - let Some(target) = - crate::mission_events::placed_logic_objects(mission, world, target_id).next() - else { + let Some(target) = GroupTarget::resolve(world, mission, target) else { return (None, 0.0); }; - let Ok(pose) = transform_state(world, target) else { + let Some([x, y]) = target.position(world) else { return (None, 0.0); }; - let [x, y, _] = pose.position.map(f32::from_bits); let mut candidates: Vec<_> = units .filter_map(|unit| { let [ux, uy, _] = transform_state(world, unit) @@ -37,13 +96,7 @@ pub(super) fn recruit( }) .collect(); let (members, strength) = take_by_hits(&mut candidates, requested); - let group = (strength > 0.0).then(|| CombatGroup { - members, - target, - target_owner: identity_metadata(world, target) - .ok() - .and_then(|identity| identity.owner_id), - }); + let group = (strength > 0.0).then(|| CombatGroup { members, target }); (group, strength) } @@ -72,30 +125,38 @@ impl CombatGroup { mission: &MissionDocument, order: Handler15Invocation, ) -> Option<()> { - let (0, kind @ (3 | 4), 3, Handler15TargetPayload::ByLogicId(target_id)) = - (order.word_0, order.word_1, order.word_2, order.target) - else { + let (0, kind @ (3 | 4), 3) = (order.word_0, order.word_1, order.word_2) else { return None; }; - let target = - crate::mission_events::placed_logic_objects(mission, world, target_id).next()?; + let target = GroupTarget::resolve(world, mission, order.target)?; if target != self.target { return None; } - let index = identity_metadata(world, target).ok()?.original_id?.0 as usize; - let [x, y, _] = transform_state(world, target) - .ok()? - .position - .map(f32::from_bits); + let point = target.position(world)?; + let attack = match (kind, target) { + (3, GroupTarget::Object(handle, _)) => Some(( + handle, + identity_metadata(world, handle).ok()?.original_id?.0 as usize, + )), + (3, GroupTarget::Point(_)) => return None, + _ => None, + }; for &unit in &self.members { - if kind == 3 { - patrol.set_attack_target(world, unit, target, index, [x, y]); + if let Some((handle, index)) = attack { + patrol.set_attack_target(world, unit, handle, index, point); } else { let radius = match order.word_3 { 0 | u32::MAX => 60.0, radius => radius as f32, }; - patrol.order_around_object(world, unit, target, [x, y], radius); + match target { + GroupTarget::Object(handle, _) => { + patrol.order_around_object(world, unit, handle, point, radius); + } + GroupTarget::Point(_) => { + patrol.order(world, unit, point, radius); + } + } } } Some(()) diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 78144fa3..a4f3fdb8 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2970,6 +2970,8 @@ pub struct SimulationReport { pub script_area_strength_queries: Vec<(usize, usize, usize, u64)>, pub script_base_threat_queries: Vec<(usize, u32, u64)>, pub script_group_orders: Vec<(usize, u32, u64)>, + pub script_point_group_orders: Vec<(usize, u32, u32, u64)>, + pub script_point_patrol_continues: Vec<(usize, u32, u32, u64)>, pub script_problem_waits: Vec<(usize, u32, u64)>, pub script_factory_patrol_continues: Vec<(usize, u32, u64)>, pub script_ore_transport_orders: Vec<(usize, u32, u64)>, @@ -3026,6 +3028,8 @@ pub fn simulate_mission_report( script_area_strength_queries: Vec::new(), script_base_threat_queries: Vec::new(), script_group_orders: Vec::new(), + script_point_group_orders: Vec::new(), + script_point_patrol_continues: Vec::new(), script_problem_waits: Vec::new(), script_factory_patrol_continues: Vec::new(), script_ore_transport_orders: Vec::new(), @@ -3134,6 +3138,12 @@ pub fn simulate_mission_report( .script_group_orders .extend(loaded.clan_ai_events.group_orders()); report + .script_point_group_orders + .extend(loaded.clan_ai_events.point_group_orders()); + report + .script_point_patrol_continues + .extend(loaded.clan_ai_events.point_patrol_continues()); + report .script_problem_waits .extend(loaded.clan_ai_events.wait_starts()); report diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 6fb1c92a..8817495e 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -253,6 +253,17 @@ impl UnitPatrol { .retain(|unit| unit.handle != handle || unit.center_object != Some(target)); } + pub(super) fn has_point_order(&self, handle: ObjectHandle, point: [f32; 2]) -> bool { + self.units + .iter() + .any(|unit| unit.handle == handle && unit.patrols_point(point)) + } + + pub(super) fn clear_point_order(&mut self, handle: ObjectHandle, point: [f32; 2]) { + self.units + .retain(|unit| unit.handle != handle || !unit.patrols_point(point)); + } + 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); @@ -512,6 +523,13 @@ fn new_patrol_replaces_a_script_attack_order() { } impl PatrollingUnit { + fn patrols_point(&self, point: [f32; 2]) -> bool { + self.center_object.is_none() + && self.attack_target.is_none() + && self.destinations.is_none() + && self.center == point + } + fn center(&self, world: &World) -> Option<[f32; 2]> { // shortcut: use the target's transform as the patrol center until native building approaches are wired. match self.center_object { @@ -529,6 +547,31 @@ impl PatrollingUnit { #[cfg(test)] #[test] +fn point_patrol_cleanup_preserves_reassigned_orders() { + let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); + let actor = fparkan_world::construct_object( + &mut world, + fparkan_world::ObjectDraft { original_id: None }, + ) + .unwrap(); + fparkan_world::register_object(&mut world, actor).unwrap(); + let mut patrol = UnitPatrol::scripted(); + let point = [1124.0, 783.0]; + patrol.order(&world, actor, point, 60.0); + assert!(patrol.has_point_order(actor, point)); + patrol.clear_point_order(actor, [1125.0, 783.0]); + assert!(patrol.has_point_order(actor, point)); + patrol.move_to(&world, actor, point, false); + assert!(!patrol.has_point_order(actor, point)); + patrol.clear_point_order(actor, point); + assert!(patrol.is_patrolling(actor)); + patrol.order(&world, actor, point, 90.0); + patrol.clear_point_order(actor, point); + assert!(!patrol.is_patrolling(actor)); +} + +#[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( |
