aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs96
-rw-r--r--crates/fparkan-runtime/src/clan_problem_conditions.rs65
-rw-r--r--crates/fparkan-runtime/src/lib.rs6
3 files changed, 155 insertions, 12 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index bc4e7408..284a9bc8 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -1,3 +1,4 @@
+use crate::clan_problem_conditions::Condition;
use crate::{clan_combat_groups::CombatGroup, clan_targeting::CaptureApproach};
use crate::{BehaviorOreState, LoadedMissionState, MissionEventStop};
use fparkan_script::{
@@ -60,12 +61,13 @@ pub(super) fn resolve_captures(
}
}
loaded.clan_ai_events.capture_orders.remove(index);
- if let Some(queue) = loaded
- .clan_ai_events
- .problems
- .get_mut(&usize::from(owner.0))
- {
- queue.remove(key);
+ let clan = usize::from(owner.0);
+ let ai = &mut loaded.clan_ai_events;
+ if !valid_unit || !ai.waits.contains_key(&(clan, key)) {
+ if let Some(queue) = ai.problems.get_mut(&clan) {
+ queue.remove(key);
+ }
+ ai.waits.remove(&(clan, key));
}
}
Ok(captured)
@@ -96,6 +98,34 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) {
});
!group.members.is_empty()
});
+ let ai = &mut loaded.clan_ai_events;
+ ai.waits.retain(|&(clan, key), conditions| {
+ let group = ai.groups.get(&(clan, key));
+ let ready = conditions.iter().any(|&condition| {
+ condition.met(
+ &engine.world,
+ OwnerId(clan as u16),
+ loaded.unit_patrol.as_ref(),
+ group,
+ )
+ });
+ if ready {
+ if let Some(queue) = ai.problems.get_mut(&clan) {
+ queue.remove(key);
+ }
+ 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);
+ }
+ }
+ }
+ eprintln!("script_problem_condition_met clan={clan} problem={} target={} queue_time_ms={now_ms}", key.word_0, key.argument_0);
+ false
+ } else {
+ true
+ }
+ });
let active = &mut loaded.clan_ai_events.factory_jobs;
active.retain(|((clan, _), plant)| {
engine
@@ -169,6 +199,8 @@ pub(super) struct ClanAiEvents {
capture_orders: Vec<(OwnerId, Handler2RecordKey, CaptureApproach)>,
capture_approaches: BTreeMap<(u32, u32), u64>,
groups: BTreeMap<(usize, Handler2RecordKey), CombatGroup>,
+ waits: BTreeMap<(usize, Handler2RecordKey), Vec<Condition>>,
+ wait_starts: BTreeMap<(usize, u32), u64>,
group_orders: BTreeMap<(usize, u32), u64>,
}
@@ -216,6 +248,7 @@ impl LoadedMissionState {
let mut solving = false;
let mut capturer = None;
let mut group = None;
+ let mut conditions = Vec::new();
let result = varset.declarations.execute_event_prefix(
event,
bundle.formula_table.as_ref(),
@@ -502,7 +535,8 @@ impl LoadedMissionState {
Some(1)
if problem
.as_ref()
- .is_some_and(|record| record.key.word_0 == 6) =>
+ .is_some_and(|record| record.key.word_0 == 6)
+ || !conditions.is_empty() =>
{
solving = true
}
@@ -515,20 +549,38 @@ impl LoadedMissionState {
}
Ok(None)
}
- ScriptDispatchSelector::Handler(27) if solving => {
+ ScriptDispatchSelector::Handler(27) if problem.is_some() => {
let [action, mode, id] = instruction.references.as_slice() else {
return Err("Handler(27) requires an action and target".into());
};
let word = |index: u32| {
values.get(index as usize).and_then(|cell| cell.as_dword())
};
- let (Some(2), Some(0), Some(id)) =
+ let (Some(action), Some(mode), Some(id)) =
(word(*action), word(*mode), word(*id))
else {
- return Err(
- "Handler(27) only supports waiting for a factory".into()
- );
+ return Err("Handler(27) requires three DWORD operands".into());
};
+ let key = problem.as_ref().expect("condition requires a problem").key;
+ let condition = match (action, mode) {
+ (1 | 3, 0) if matches!(key.word_0, 7 | 9) => {
+ let target = crate::mission_events::placed_logic_objects(
+ &self.mission, world, id
+ ).next();
+ Some(if action == 1 { Condition::ObjectDestroyed(target) } else {
+ Condition::BuildingCaptured(target.ok_or("Handler(27) capture target is unavailable")?)
+ })
+ }
+ (4 | 5, 1) if id == 0 && self.clan_ai_events.groups.contains_key(&(clan, key)) => {
+ Some(if action == 4 { Condition::GroupDestroyed } else { Condition::GroupIdle })
+ }
+ (2, 0) if solving => None,
+ _ => return Err("Handler(27) action or target mode is unsupported".into()),
+ };
+ if let Some(condition) = condition {
+ conditions.push(condition);
+ return Ok(None);
+ }
let order = orders
.last_mut()
.filter(|order| {
@@ -759,6 +811,19 @@ impl LoadedMissionState {
.entry(clan)
.or_default()
.remove(problem.key);
+ } else if solving && !conditions.is_empty() {
+ let count = self
+ .clan_ai_events
+ .wait_starts
+ .entry((clan, problem.key.word_0))
+ .or_default();
+ *count += 1;
+ if *count == 1 {
+ eprintln!("sim_coverage script_problem_wait_approx=1 clan={clan} problem={} target={} conditions={}", problem.key.word_0, problem.key.argument_0, conditions.len());
+ }
+ self.clan_ai_events
+ .waits
+ .insert((clan, problem.key), conditions);
}
} else if let Some(queue) = self.clan_ai_events.problems.get(&clan) {
pending = ranked_problems(queue.records());
@@ -781,6 +846,7 @@ impl LoadedMissionState {
.clan_ai_events
.groups
.contains_key(&(clan, problem.key))
+ || self.clan_ai_events.waits.contains_key(&(clan, problem.key))
{
continue;
}
@@ -910,6 +976,12 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32
}
impl ClanAiEvents {
+ pub(super) fn wait_starts(&self) -> impl Iterator<Item = (usize, u32, u64)> + '_ {
+ self.wait_starts
+ .iter()
+ .map(|(&(clan, problem), &count)| (clan, problem, count))
+ }
+
fn unit_assigned(&self, unit: ObjectHandle) -> bool {
self.capture_orders
.iter()
diff --git a/crates/fparkan-runtime/src/clan_problem_conditions.rs b/crates/fparkan-runtime/src/clan_problem_conditions.rs
new file mode 100644
index 00000000..bace9ec6
--- /dev/null
+++ b/crates/fparkan-runtime/src/clan_problem_conditions.rs
@@ -0,0 +1,65 @@
+use crate::clan_combat_groups::CombatGroup;
+use crate::unit_patrol::UnitPatrol;
+use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle, OwnerId, World};
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(super) enum Condition {
+ ObjectDestroyed(Option<ObjectHandle>),
+ BuildingCaptured(ObjectHandle),
+ GroupDestroyed,
+ GroupIdle,
+}
+
+impl Condition {
+ pub(super) fn met(
+ self,
+ world: &World,
+ clan: OwnerId,
+ patrol: Option<&UnitPatrol>,
+ group: Option<&CombatGroup>,
+ ) -> bool {
+ let live =
+ |unit| registration_sequence(world, unit).is_ok_and(|sequence| sequence.is_some());
+ match self {
+ Self::ObjectDestroyed(target) => target.is_none_or(|target| !live(target)),
+ Self::BuildingCaptured(target) => {
+ live(target)
+ && identity_metadata(world, target)
+ .is_ok_and(|identity| identity.owner_id == Some(clan))
+ }
+ Self::GroupDestroyed => {
+ group.is_none_or(|group| group.members.iter().all(|&unit| !live(unit)))
+ }
+ Self::GroupIdle => group.is_none_or(|group| {
+ group.members.iter().all(|&unit| {
+ !live(unit) || patrol.is_none_or(|patrol| !patrol.is_patrolling(unit))
+ })
+ }),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use fparkan_world::{
+ construct_object, new, register_object, set_owner, ObjectDraft, WorldConfig,
+ };
+
+ #[test]
+ fn capture_waits_for_the_requested_clan_and_destruction_waits_for_removal() {
+ let mut world = new(WorldConfig);
+ let target = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap();
+ register_object(&mut world, target).unwrap();
+ let clan = OwnerId(1);
+ set_owner(&mut world, target, Some(OwnerId(0))).unwrap();
+ assert!(!Condition::BuildingCaptured(target).met(&world, clan, None, None));
+ assert!(!Condition::ObjectDestroyed(Some(target)).met(&world, clan, None, None));
+ set_owner(&mut world, target, Some(clan)).unwrap();
+ assert!(Condition::BuildingCaptured(target).met(&world, clan, None, None));
+ fparkan_world::request_delete(&mut world, target).unwrap();
+ fparkan_world::step(&mut world, &fparkan_world::InputSnapshot).unwrap();
+ assert!(Condition::ObjectDestroyed(Some(target)).met(&world, clan, None, None));
+ assert!(!Condition::BuildingCaptured(target).met(&world, clan, None, None));
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 6c7f0fe7..56063ca9 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -47,6 +47,7 @@ mod building_door;
mod clan_ai_events;
mod clan_combat_groups;
mod clan_combat_strength;
+mod clan_problem_conditions;
mod clan_script_init;
mod clan_targeting;
mod mesh_collision;
@@ -2931,6 +2932,7 @@ pub struct SimulationReport {
pub script_capture_queries: Vec<(usize, u64)>,
pub script_attack_queries: Vec<(usize, u64)>,
pub script_group_orders: Vec<(usize, u32, u64)>,
+ pub script_problem_waits: Vec<(usize, u32, u64)>,
pub script_capturer_queries: Vec<(usize, u64)>,
pub script_capture_approaches: Vec<(u32, u32, u64)>,
/// First factory completion by clan, in milliseconds.
@@ -2978,6 +2980,7 @@ pub fn simulate_mission_report(
script_capture_queries: Vec::new(),
script_attack_queries: Vec::new(),
script_group_orders: Vec::new(),
+ script_problem_waits: Vec::new(),
script_capturer_queries: Vec::new(),
script_capture_approaches: Vec::new(),
first_factory_completion_ms: Default::default(),
@@ -3069,6 +3072,9 @@ pub fn simulate_mission_report(
.script_group_orders
.extend(loaded.clan_ai_events.group_orders());
report
+ .script_problem_waits
+ .extend(loaded.clan_ai_events.wait_starts());
+ report
.script_capturer_queries
.extend(loaded.clan_ai_events.capturer_queries());
report