aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_script_init.rs
blob: a827df14eec339f374ad966c94337c67418ab079 (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
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,
    values: &[VarSetDefault],
) -> Option<u8> {
    match (instruction.header_words, instruction.references.as_slice()) {
        // shortcut: resume Init until the live research context is wired.
        ([43, u32::MAX, u32::MAX, u32::MAX, 6, 0, u32::MAX], []) => Some(43),
        // shortcut: resume Init until the native AI base-area state is wired.
        ([68, u32::MAX, u32::MAX, u32::MAX, 6, 1, u32::MAX], [index])
            if matches!(
                values.get(*index as usize),
                Some(VarSetDefault::FloatBits(_))
            ) =>
        {
            Some(68)
        }
        _ => None,
    }
}

#[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 base_area_stub_requires_a_float_operand_and_no_result() {
        let mut instruction = ScriptInstruction {
            header_words: [68, u32::MAX, u32::MAX, u32::MAX, 6, 1, u32::MAX],
            references: vec![0],
        };
        let values = [VarSetDefault::FloatBits(500.0_f32.to_bits())];
        assert_eq!(stubbed_handler(&instruction, &values), Some(68));
        assert_eq!(
            stubbed_handler(&instruction, &[VarSetDefault::Dword(500)]),
            None
        );
        instruction.header_words[1] = 0;
        assert_eq!(stubbed_handler(&instruction, &values), 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
        );
    }
}