aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/script_patrol.rs
blob: 427dc3977289eba5d1446b7db0e7c27b885ce837 (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
use crate::unit_patrol::UnitPatrol;
use crate::{MissionDocument, MissionObjectDraft};
use fparkan_script::{Handler15Invocation, Handler15TargetPayload};
use fparkan_world::{
    handle_by_original_id, transform_state, ObjectHandle, OriginalObjectId, World,
};

pub(super) fn warbot_by_logic_id(
    mission: &MissionDocument,
    drafts: &[MissionObjectDraft],
    world: &World,
    id: u32,
) -> Option<ObjectHandle> {
    unit_by_logic_id(mission, drafts, world, id, |class| {
        class == crate::boarding::WARBOT_CLASS
    })
}

pub(super) fn unit_by_logic_id(
    mission: &MissionDocument,
    drafts: &[MissionObjectDraft],
    world: &World,
    id: u32,
    matches_class: impl Fn(u32) -> bool,
) -> Option<ObjectHandle> {
    mission
        .objects
        .iter()
        .zip(drafts)
        .enumerate()
        .find_map(|(index, (object, draft))| {
            (id != u32::MAX
                && object.identity_or_clan_raw == id
                && draft.unit_dat_header_flags.is_some_and(&matches_class))
            .then(|| handle_by_original_id(world, OriginalObjectId(index as u32)))
            .flatten()
        })
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct ScriptPatrol {
    pub(super) actor_id: u32,
    target: Handler15TargetPayload,
    pub(super) radius: u32,
}

impl ScriptPatrol {
    pub(super) fn from_order(order: Handler15Invocation) -> Option<Self> {
        if !matches!(
            (order.word_1, order.word_2, order.target),
            (
                4,
                3,
                Handler15TargetPayload::ByPlace { .. } | Handler15TargetPayload::ByLogicId(_)
            )
        ) {
            return None;
        }
        Some(Self {
            actor_id: order.word_0,
            target: order.target,
            radius: order.word_3,
        })
    }

    pub(super) fn apply(
        self,
        patrol: &mut UnitPatrol,
        mission: &MissionDocument,
        drafts: &[MissionObjectDraft],
        world: &World,
    ) -> Option<(ObjectHandle, [u32; 2])> {
        let handle = warbot_by_logic_id(mission, drafts, world, self.actor_id)?;
        // shortcut: use the verified default radius until per-unit patrol profiles are wired.
        let radius = match self.radius {
            0 | u32::MAX => 60.0,
            radius => radius as f32,
        };
        match self.target {
            Handler15TargetPayload::ByPlace { first, second } => {
                let center = [first, second];
                patrol
                    .order(world, handle, center.map(|axis| axis as f32), radius)
                    .then_some((handle, center))
            }
            Handler15TargetPayload::ByLogicId(id) => {
                let target =
                    crate::mission_events::placed_logic_objects(mission, world, id).next()?;
                let [x, y, _] = transform_state(world, target)
                    .ok()?
                    .position
                    .map(f32::from_bits);
                patrol.order_around_object(world, handle, target, [x, y], radius).then(|| {
                    eprintln!("sim_coverage script_patrol_target_approx=1 unit={} target_id={id} target_unit={}", handle.slot, target.slot);
                    (handle, [x as u32, y as u32])
                })
            }
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        create, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
        MissionStartupInput,
    };
    use fparkan_vfs::DirectoryVfs;
    use std::{path::PathBuf, sync::Arc};

    #[test]
    #[ignore = "requires licensed corpus"]
    fn stock_campaign_assigns_patrols_during_init() {
        let root =
            PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed root"));
        let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root)))).unwrap();
        load_gameplay_mission(
            &mut engine,
            MissionRequest {
                key: "MISSIONS/CAMPAIGN/CAMPAIGN.01/Mission.02/data.tma".into(),
                mode: MissionMode::SingleMission,
            },
            MissionStartupInput {
                queue_time_ms: 0,
                research_creation_gate: false,
            },
            None,
        )
        .unwrap();

        let mission = engine.loaded.as_ref().unwrap();
        let patrol = mission.unit_patrol.as_ref().unwrap();
        let live_patrols: Vec<_> = mission
            .mission
            .objects
            .iter()
            .enumerate()
            .filter_map(|(index, _)| {
                handle_by_original_id(&engine.world, OriginalObjectId(index as u32))
            })
            .filter(|&handle| patrol.is_patrolling(handle))
            .collect();
        assert!(!live_patrols.is_empty());
    }
}