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

pub(super) fn warbot_by_logic_id(
    mission: &MissionDocument,
    drafts: &[MissionObjectDraft],
    world: &World,
    id: u32,
) -> 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 == Some(crate::boarding::WARBOT_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,
    pub(super) center: [u32; 2],
    pub(super) radius: u32,
}

impl ScriptPatrol {
    pub(super) fn from_order(order: Handler15Invocation) -> Option<Self> {
        let (4, 3, Handler15TargetPayload::ByPlace { first, second }) =
            (order.word_1, order.word_2, order.target)
        else {
            return None;
        };
        Some(Self {
            actor_id: order.word_0,
            center: [first, second],
            radius: order.word_3,
        })
    }

    pub(super) fn apply(
        self,
        patrol: &mut UnitPatrol,
        mission: &MissionDocument,
        drafts: &[MissionObjectDraft],
        world: &World,
    ) -> Option<ObjectHandle> {
        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,
        };
        patrol
            .order(world, handle, self.center.map(|axis| axis as f32), radius)
            .then_some(handle)
    }
}

#[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());
    }
}