aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_script_init.rs
blob: 2d5ac129792771e6f1788f821c4948edbbdfede6 (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
use crate::script_patrol::ScriptPatrol;
use fparkan_script::{
    Handler15TargetPayload, ScriptDispatchSelector, ScriptInstruction, VarSet, VarSetDefault,
};

pub(super) enum InitOrder {
    Patrol(ScriptPatrol),
    Shutdown(u32),
    Reserve {
        clan: usize,
        id: u32,
        reserved: bool,
    },
}

pub(super) fn reservation(
    instruction: &ScriptInstruction,
    values: &[VarSetDefault],
) -> Option<(u32, bool)> {
    if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(51)
        || instruction.header_words[1] != u32::MAX
        || instruction.header_words[3] != u32::MAX
    {
        return None;
    }
    let (id, flag) = instruction.dword_pair(values)?;
    Some((id, flag != 0))
}

pub(super) fn unit_order(
    declarations: &VarSet,
    instruction: &ScriptInstruction,
    values: &[VarSetDefault],
) -> Option<InitOrder> {
    if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(15)
        || instruction.header_words[1] != u32::MAX
        || instruction.header_words[3] != u32::MAX
    {
        return None;
    }
    let order = declarations
        .resolve_handler15_with_values(instruction, values)
        .ok()?;
    if matches!(
        (order.word_1, order.word_2, order.target),
        (0x13, 3, Handler15TargetPayload::None)
    ) {
        return Some(InitOrder::Shutdown(order.word_0));
    }
    ScriptPatrol::from_order(order).map(InitOrder::Patrol)
}

pub(super) fn stubbed_handler(instruction: &ScriptInstruction) -> Option<u8> {
    // shortcut: resume Init after the catalog refresh until the live research context is wired.
    (instruction.header_words == [43, u32::MAX, u32::MAX, u32::MAX, 6, 0, u32::MAX]
        && instruction.references.is_empty())
    .then_some(43)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn only_the_no_result_research_refresh_is_stubbed() {
        let mut instruction = ScriptInstruction {
            header_words: [43, u32::MAX, u32::MAX, u32::MAX, 6, 0, u32::MAX],
            references: Vec::new(),
        };
        assert_eq!(stubbed_handler(&instruction), Some(43));
        instruction.header_words[1] = 0;
        assert_eq!(stubbed_handler(&instruction), None);
    }

    #[test]
    fn reservation_reads_logical_id_and_treats_every_nonzero_flag_as_set() {
        let instruction = ScriptInstruction {
            header_words: [51, u32::MAX, u32::MAX, u32::MAX, 6, 2, u32::MAX],
            references: vec![1, 0],
        };
        assert_eq!(
            reservation(
                &instruction,
                &[VarSetDefault::Dword(2), VarSetDefault::Dword(12)]
            ),
            Some((12, true))
        );
        assert_eq!(
            reservation(
                &instruction,
                &[VarSetDefault::Dword(0), VarSetDefault::Dword(13)]
            ),
            Some((13, false))
        );
        assert_eq!(
            reservation(
                &instruction,
                &[VarSetDefault::FloatBits(0), VarSetDefault::Dword(13)]
            ),
            None
        );
    }
}