aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/combat_report.rs144
-rw-r--r--crates/fparkan-runtime/src/lib.rs5
-rw-r--r--crates/fparkan-runtime/src/unit_combat.rs107
3 files changed, 244 insertions, 12 deletions
diff --git a/crates/fparkan-runtime/src/combat_report.rs b/crates/fparkan-runtime/src/combat_report.rs
new file mode 100644
index 00000000..06082f87
--- /dev/null
+++ b/crates/fparkan-runtime/src/combat_report.rs
@@ -0,0 +1,144 @@
+use fparkan_world::{identity_metadata, ObjectHandle, World};
+use std::collections::BTreeMap;
+
+/// Identity retained by combat diagnostics after an object has been removed.
+#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
+pub struct CombatObject {
+ pub slot: u32,
+ pub generation: u32,
+ pub original_id: Option<u32>,
+}
+
+impl CombatObject {
+ pub(super) fn read(world: &World, handle: ObjectHandle) -> Self {
+ Self {
+ slot: handle.slot,
+ generation: handle.generation,
+ original_id: identity_metadata(world, handle)
+ .ok()
+ .and_then(|identity| identity.original_id)
+ .map(|id| id.0),
+ }
+ }
+}
+
+impl std::fmt::Display for CombatObject {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(formatter, "{}:{}", self.slot, self.generation)
+ }
+}
+
+/// Event count and endpoints on the mission clock, including time zero.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct CombatOccurrences {
+ pub count: u64,
+ pub first_ms: u32,
+ pub last_ms: u32,
+}
+
+fn record<K: Ord>(
+ counts: &mut BTreeMap<K, CombatOccurrences>,
+ key: K,
+ now: u32,
+ count: u64,
+) -> bool {
+ let first = !counts.contains_key(&key);
+ let total = counts.entry(key).or_insert(CombatOccurrences {
+ count: 0,
+ first_ms: now,
+ last_ms: now,
+ });
+ total.count += count;
+ total.last_ms = now;
+ first
+}
+
+/// Autonomous combat observations shared by the window and headless runtime.
+/// Empty weapon banks are included as availability diagnostics, not proof of a missing rule.
+#[derive(Clone, Debug, Default, Eq, PartialEq)]
+pub struct CombatReport {
+ pub shots: BTreeMap<(CombatObject, CombatObject), CombatOccurrences>,
+ pub destroyed: BTreeMap<(CombatObject, CombatObject), CombatOccurrences>,
+ pub unavailable: BTreeMap<(CombatObject, &'static str), CombatOccurrences>,
+}
+
+impl CombatReport {
+ pub(super) fn shot(&mut self, shooter: CombatObject, target: CombatObject, now: u32) {
+ if record(&mut self.shots, (shooter, target), now, 1) {
+ eprintln!("unit_combat_first_shot_ms={now} shooter={shooter} target={target} shooter_id={:?} target_id={:?}", shooter.original_id, target.original_id);
+ }
+ }
+
+ pub(super) fn destroyed(&mut self, shooter: CombatObject, target: CombatObject, now: u32) {
+ if record(&mut self.destroyed, (shooter, target), now, 1) {
+ eprintln!("unit_combat_first_destroyed_ms={now} shooter={shooter} target={target} shooter_id={:?} target_id={:?}", shooter.original_id, target.original_id);
+ }
+ }
+
+ pub(super) fn unavailable(
+ &mut self,
+ object: CombatObject,
+ reason: &'static str,
+ now: u32,
+ count: u64,
+ ) -> bool {
+ let first = record(&mut self.unavailable, (object, reason), now, count);
+ if first {
+ eprintln!("unit_combat_unavailable={count} object={object} original_id={:?} reason={reason} first_ms={now}", object.original_id);
+ }
+ first
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn preserves_zero_time_and_separates_reused_slots_and_failure_reasons() {
+ let shooter = CombatObject {
+ slot: 7,
+ generation: 0,
+ original_id: Some(19),
+ };
+ let target = CombatObject {
+ slot: 9,
+ generation: 0,
+ original_id: Some(23),
+ };
+ let replacement = CombatObject {
+ generation: 1,
+ ..target
+ };
+ let mut report = CombatReport::default();
+ report.shot(shooter, target, 0);
+ report.shot(shooter, target, 50);
+ report.destroyed(shooter, target, 100);
+ report.shot(shooter, replacement, 150);
+ report.unavailable(shooter, "projectile_contact", 160, 2);
+ report.unavailable(shooter, "projectile_contact", 170, 3);
+ report.unavailable(shooter, "projectile_advance", 170, 1);
+ assert_eq!(
+ report.shots[&(shooter, target)],
+ CombatOccurrences {
+ count: 2,
+ first_ms: 0,
+ last_ms: 50
+ }
+ );
+ assert_eq!(report.shots[&(shooter, replacement)].first_ms, 150);
+ assert_eq!(report.destroyed[&(shooter, target)].count, 1);
+ assert_eq!(
+ report.unavailable[&(shooter, "projectile_contact")],
+ CombatOccurrences {
+ count: 5,
+ first_ms: 160,
+ last_ms: 170
+ }
+ );
+ assert_eq!(
+ report.unavailable[&(shooter, "projectile_advance")].count,
+ 1
+ );
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 103c669e..6311683b 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -51,6 +51,8 @@ mod clan_area_strength;
mod clan_build_limits;
mod clan_building_queries;
mod clan_clock;
+mod combat_report;
+pub use combat_report::{CombatObject, CombatOccurrences, CombatReport};
mod control_profile;
mod control_resources;
mod mobile_budget;
@@ -2993,6 +2995,7 @@ pub struct SimulationReport {
pub patrol_speeds: PatrolSpeedReport,
pub control_profiles: ControlProfileReport,
pub mobile_budget: MobileBudgetReport,
+ pub combat: CombatReport,
/// Approximate clan strength queries, grouped by caller and target clan.
pub script_strength_queries: Vec<(usize, u16, u64)>,
pub script_capture_queries: Vec<(usize, u64)>,
@@ -3074,6 +3077,7 @@ pub fn simulate_mission_report(
patrol_speeds: PatrolSpeedReport::default(),
control_profiles: ControlProfileReport::default(),
mobile_budget: MobileBudgetReport::default(),
+ combat: CombatReport::default(),
script_strength_queries: Vec::new(),
script_capture_queries: Vec::new(),
script_attack_queries: Vec::new(),
@@ -3201,6 +3205,7 @@ pub fn simulate_mission_report(
}
if let Some(combat) = &engine.autodemo_combat {
report.weapon_poses = combat.pose_report().clone();
+ report.combat = combat.report.clone();
}
for problem in loaded.clan_ai_events.pending_problems() {
*report
diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs
index ed6a2758..36d18ee8 100644
--- a/crates/fparkan-runtime/src/unit_combat.rs
+++ b/crates/fparkan-runtime/src/unit_combat.rs
@@ -1,6 +1,7 @@
use super::{
apply_projectile_contact,
autodemo::MOBILE_UNIT,
+ combat_report::{CombatObject, CombatReport},
player_weapon::prepare_unit_weapons,
projectile::{load_projectile, segment_sphere_fraction, unit_sphere},
projectile_terrain_hit, projectile_unit_hit,
@@ -70,6 +71,7 @@ pub(super) struct AutoDemoCombat {
projectiles: Vec<AutoDemoProjectileType>,
flights: Vec<Flight>,
pose_failures: super::WeaponPoseReport,
+ pub(super) report: CombatReport,
pub(super) unavailable: usize,
}
@@ -93,6 +95,7 @@ struct Unit {
struct Flight {
shooter: ObjectHandle,
+ shooter_identity: CombatObject,
projectile: usize,
pose: PlayerProjectile,
}
@@ -131,6 +134,7 @@ impl AutoDemoCombat {
projectiles: Vec::new(),
flights: Vec::new(),
pose_failures: super::WeaponPoseReport::default(),
+ report: CombatReport::default(),
unavailable: 0,
};
for (index, (draft, &handle)) in drafts.iter().zip(handles).enumerate() {
@@ -140,6 +144,11 @@ impl AutoDemoCombat {
{
continue;
}
+ let object = CombatObject {
+ slot: handle.slot,
+ generation: handle.generation,
+ original_id: draft.original_id.map(|id| id.0),
+ };
let weapons = prepare_unit_weapons(
assets
.object_component_controls
@@ -162,6 +171,7 @@ impl AutoDemoCombat {
.or_else(|| {
let Ok(data) = load_projectile(vfs, archive, name) else {
battle.unavailable += 1;
+ battle.report.unavailable(object, "projectile_load", 0, 1);
return None;
};
battle.projectiles.push(AutoDemoProjectileType {
@@ -182,6 +192,9 @@ impl AutoDemoCombat {
})
.collect();
battle.unavailable += usize::from(weapons.weapons().is_empty());
+ if weapons.weapons().is_empty() {
+ battle.report.unavailable(object, "empty_weapon_bank", 0, 1);
+ }
let units = if index < reserve_start {
&mut battle.units
} else {
@@ -332,14 +345,19 @@ impl AutoDemoCombat {
});
if let Some(hit) = contact {
let data = &self.projectiles[flight.projectile];
+ // Capture the target before applying damage, which may remove it.
+ let target = CombatObject::read(&engine.world, hit.handle);
match data
.volume
.map(|volume| apply_projectile_contact(engine, hit, volume))
{
- Some(Ok(Some((_, true)))) => events.push(AutoDemoCombatEvent::Destroyed {
- shooter: flight.shooter,
- target: hit.handle,
- }),
+ Some(Ok(Some((_, true)))) => {
+ self.report.destroyed(flight.shooter_identity, target, now);
+ events.push(AutoDemoCombatEvent::Destroyed {
+ shooter: flight.shooter,
+ target: hit.handle,
+ });
+ }
Some(Ok(Some((remaining, false)))) => {
if let Some(node) = hit.building_node {
events.push(AutoDemoCombatEvent::BuildingHit {
@@ -350,13 +368,25 @@ impl AutoDemoCombat {
});
}
}
- _ => unavailable += 1,
+ result => {
+ unavailable += 1;
+ let reason = match &result {
+ None => "projectile_volume",
+ Some(Ok(_)) => "damage_unavailable",
+ Some(Err(_)) => "damage_error",
+ };
+ if self.report.unavailable(flight.shooter_identity, reason, now, 1) {
+ eprintln!("unit_combat_contact object={} target={target} target_id={:?} node={:?} result={result:?}", flight.shooter_identity, target.original_id, hit.building_node);
+ }
+ }
}
}
match stays {
Ok(stays) => stays,
Err(_) => {
unavailable += 1;
+ self.report
+ .unavailable(flight.shooter_identity, "projectile_advance", now, 1);
false
}
}
@@ -374,6 +404,7 @@ impl AutoDemoCombat {
let Ok(state) = transform_state(&engine.world, unit.handle) else {
continue;
};
+ let shooter = CombatObject::read(&engine.world, unit.handle);
if unit.weapons.weapons().is_empty() {
continue;
}
@@ -447,7 +478,12 @@ impl AutoDemoCombat {
};
clear(from, to, radius)
}) {
- let (Some((target, to, radius)), Some(projectile)) = (aim, unit.slots[slot]) else {
+ let Some((target, to, radius)) = aim else {
+ continue;
+ };
+ let Some(projectile) = unit.slots[slot] else {
+ unavailable += 1;
+ self.report.unavailable(shooter, "projectile_slot", now, 1);
continue;
};
let Some(from) = poses.muzzle(state, weapon.component_index, barrel) else {
@@ -464,14 +500,21 @@ impl AutoDemoCombat {
{
self.flights.push(Flight {
shooter: unit.handle,
+ shooter_identity: shooter,
projectile,
pose,
});
+ self.report
+ .shot(shooter, CombatObject::read(&engine.world, target), now);
events.push(AutoDemoCombatEvent::Shot {
shooter: unit.handle,
target,
projectile,
});
+ } else {
+ unavailable += 1;
+ self.report
+ .unavailable(shooter, "projectile_launch", now, 1);
}
}
missing += due_missing;
@@ -488,6 +531,8 @@ impl AutoDemoCombat {
);
}
*count += missing;
+ self.report
+ .unavailable(shooter, "weapon_pose", now, missing);
unavailable += missing as usize;
}
}
@@ -532,7 +577,7 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
- fn licensed_autodemo_shots_start_outside_the_hull_center() {
+ fn licensed_autodemo_combat_preserves_muzzles_and_event_identities() {
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
load_gameplay_mission(
@@ -548,17 +593,36 @@ mod tests {
None,
)
.unwrap();
+ let mut observed_shots = 0;
+ let mut observed_unavailable = engine.autodemo_combat.as_ref().unwrap().unavailable as u64;
for now in (0..=120_000).step_by(50) {
let frame = frame(&mut engine, now).unwrap();
+ observed_unavailable += frame.autodemo_combat_unavailable as u64;
let shots = frame
.autodemo_combat_events
.iter()
.filter(|event| matches!(event, AutoDemoCombatEvent::Shot { .. }))
.count();
- if shots == 0 {
- continue;
- }
+ observed_shots += shots as u64;
let combat = engine.autodemo_combat.as_ref().unwrap();
+ assert_eq!(
+ combat
+ .report
+ .shots
+ .values()
+ .map(|total| total.count)
+ .sum::<u64>(),
+ observed_shots
+ );
+ assert_eq!(
+ combat
+ .report
+ .unavailable
+ .values()
+ .map(|total| total.count)
+ .sum::<u64>(),
+ observed_unavailable
+ );
assert!(combat.flights.len() >= shots);
for flight in combat.flights.iter().rev().take(shots) {
let shooter = combat
@@ -583,9 +647,28 @@ mod tests {
assert!(separation > 0.01, "projectile still starts at hull center");
}
assert!(combat.pose_report().missing.is_empty());
- return;
+ if let Some(AutoDemoCombatEvent::Destroyed { target, .. }) = frame
+ .autodemo_combat_events
+ .iter()
+ .find(|event| matches!(event, AutoDemoCombatEvent::Destroyed { .. }))
+ {
+ assert!(identity_metadata(&engine.world, *target).is_err());
+ let ((_, retired), total) = combat
+ .report
+ .destroyed
+ .iter()
+ .find(|((_, object), _)| {
+ object.slot == target.slot && object.generation == target.generation
+ })
+ .expect("removed target is still reported");
+ assert!(retired.original_id.is_some());
+ assert_eq!(total.first_ms, now);
+ assert_eq!(total.count, 1);
+ assert!(observed_shots > 0);
+ return;
+ }
}
- panic!("stock AutoDemo did not fire");
+ panic!("stock AutoDemo did not destroy a unit");
}
#[test]