aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_building_queries.rs
blob: c179c2e17644acce42fa181b53559fb4218295b7 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{
    mission_events::MissionEvents, MissionControlDeviceTableState, MissionDocument,
    MissionObjectDraft,
};
use fparkan_world::{handle_by_original_id, transform_state, OwnerId, World};

pub(super) fn weakest_enemy_of_class(
    mission: &MissionDocument,
    roster: &MissionEvents,
    drafts: &[MissionObjectDraft],
    controls: &[MissionControlDeviceTableState],
    world: &World,
    caller: usize,
    class: u32,
    radius: f32,
) -> Option<(u32, f32)> {
    let source = mission.clans.get(caller)?;
    let enemies: Vec<_> = mission
        .clans
        .iter()
        .enumerate()
        .filter(|(index, candidate)| {
            *index != caller
                && source.relations.iter().any(|relation| {
                    relation.relation_value == 0
                        && relation.other_clan_name.raw == candidate.name.raw
                })
        })
        .flat_map(|(index, _)| roster.clan_units(world, OwnerId(index as u16), false))
        .collect();
    // shortcut: use mission order until the native ArealMap building lists are wired.
    let candidates = drafts.iter().filter_map(|draft| {
        if class & 0x8000_0000 == 0
            || draft.unit_dat_header_flags != Some(class)
            || draft.identity_or_clan_raw == u32::MAX
        {
            return None;
        }
        let handle = handle_by_original_id(world, draft.original_id?)?;
        if !enemies.contains(&handle) {
            return None;
        }
        let position = transform_state(world, handle)
            .ok()?
            .position
            .map(f32::from_bits);
        let strength = crate::clan_targeting::area_strength(
            world,
            drafts,
            controls,
            enemies.iter().copied(),
            position,
            radius,
        );
        Some((draft.identity_or_clan_raw, strength))
    });
    crate::clan_targeting::least_defended(candidates)
}

#[cfg(test)]
mod tests {
    #[test]
    #[ignore = "requires licensed corpus"]
    fn licensed_stock_convoy_uses_size_class_and_assigns_an_attack() {
        use crate::{EngineServices, MissionMode, MissionRequest, MissionStartupInput};
        use fparkan_vfs::DirectoryVfs;
        use fparkan_world::{identity_metadata, set_owner, OwnerId};
        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.03/Mission.02/data.tma".into(),
                mode: MissionMode::SingleMission,
            },
            MissionStartupInput {
                queue_time_ms: 0,
                research_creation_gate: false,
            },
            None,
        )
        .unwrap();
        let mut heard = Vec::new();
        for now in (0..=900_000).step_by(100) {
            heard.extend(crate::frame(&mut engine, now).unwrap().clan_notices);
            if engine
                .loaded
                .as_ref()
                .unwrap()
                .clan_ai_events
                .class_target_queries()
                .any(|(clan, _, _, _)| clan == 2)
            {
                break;
            }
        }
        assert_eq!(
            heard
                .iter()
                .filter(|&&notice| notice == crate::MissionNotice::Information(0))
                .count(),
            1
        );
        let loaded = engine.loaded.as_ref().unwrap();
        let actor = loaded
            .mission_events
            .logic_objects(&loaded.mission, &engine.world, 15)
            .next()
            .unwrap();
        let target = loaded
            .mission_events
            .logic_objects(&loaded.mission, &engine.world, 0x8000_0001)
            .next()
            .unwrap();
        assert_eq!(
            loaded
                .unit_patrol
                .as_ref()
                .unwrap()
                .attack_target(actor)
                .map(|(unit, _)| unit),
            Some(target)
        );
        assert!(loaded
            .clan_ai_events
            .class_target_queries()
            .any(|(clan, class, id, count)| {
                clan == 2 && class == 0x8001_0000 && id == 0x8000_0001 && count > 0
            }));
        assert!(loaded
            .clan_ai_events
            .size_class_queries()
            .any(|(clan, id, count)| clan == 1 && id == 0x8000_0003 && count > 0));
        assert!(loaded.clan_ai_events.stops().all(|(stop, _)| ![
            "Handler(30)",
            "Handler(71)",
            "Handler(72)"
        ]
        .iter()
        .any(|handler| stop.reason.starts_with(handler))));

        let classified = loaded
            .mission_events
            .logic_objects(&loaded.mission, &engine.world, 0x8000_0003)
            .next()
            .unwrap();
        let original_owner = identity_metadata(&engine.world, classified)
            .unwrap()
            .owner_id;
        // Debug ownership change checks the descriptor binding, separately from the stock-start attack.
        set_owner(&mut engine.world, classified, Some(OwnerId(3))).unwrap();
        assert_eq!(
            crate::unit_size_class::query(
                &loaded.mission,
                &loaded.mission_events,
                &loaded.object_drafts,
                &engine.world,
                0x8000_0003
            )
            .unwrap(),
            2
        );
        set_owner(&mut engine.world, classified, original_owner).unwrap();
        assert_eq!(
            crate::unit_size_class::query(
                &loaded.mission,
                &loaded.mission_events,
                &loaded.object_drafts,
                &engine.world,
                u32::MAX
            )
            .unwrap(),
            u32::MAX
        );
    }
}