aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/combat_report.rs
blob: 06082f871bdb4d0b1a5369c1ffbee3793411ee10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
        );
    }
}