aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_capture_event.rs
blob: 4410f7922874b753b8ee89217e00517781d10d5a (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
//! Script notifications for a building's former owner.

use crate::LoadedMissionState;
use fparkan_script::{ScriptDispatchSelector, VarSetDefault};
use fparkan_world::{identity_metadata, ObjectHandle, OwnerId, World};

pub(super) fn dispatch(
    mission: &mut LoadedMissionState,
    world: &World,
    building: ObjectHandle,
    former_owner: OwnerId,
) {
    let Some(id) = mission
        .mission_events
        .logic_id(&mission.mission, world, building)
    else {
        return;
    };
    if id == u32::MAX {
        return;
    }
    let clan = usize::from(former_owner.0);
    let Some(bundle) = mission
        .script_bundles
        .iter()
        .find(|bundle| bundle.clan_index == clan && bundle.behavior_warm_start.is_some())
    else {
        return;
    };
    let Some(event) =
        bundle.package.events.iter().position(|event| {
            event.name_raw == b"Fort_Captured\0" && !event.instructions.is_empty()
        })
    else {
        return;
    };
    let Some(varset) = mission.script_varset.as_ref().map(|set| &set.declarations) else {
        return;
    };
    let Some(values) = mission
        .script_varset_states
        .iter_mut()
        .find(|state| state.clan_index == clan)
        .map(|state| &mut state.values)
    else {
        return;
    };
    let Some(sender) = varset
        .declarations
        .iter()
        .position(|cell| cell.name == "dCurrentSender")
        .and_then(|index| values.get_mut(index))
    else {
        return;
    };
    *sender = VarSetDefault::Dword(id);
    let result = varset.execute_package_event_prefix(
        &bundle.package,
        event,
        bundle.formula_table.as_ref(),
        values,
        |_, instruction, values| {
            let lookup = |index: u32| {
                let id = values.get(index as usize)?.as_dword()?;
                mission
                    .mission_events
                    .logic_objects(&mission.mission, world, id)
                    .next()
            };
            match instruction.dispatch_selector() {
                ScriptDispatchSelector::Handler(63) => {
                    let &[id] = instruction.references.as_slice() else {
                        return Err("Handler(63) requires a building ID".into());
                    };
                    let inside = lookup(id).is_some_and(|handle| {
                        crate::clan_targeting::inside_clan_area(
                            &mission.mission,
                            &mission.object_drafts,
                            world,
                            clan,
                            mission.clan_ai_events.base_radius_limit(clan),
                            handle,
                        )
                    });
                    Ok(Some(VarSetDefault::Dword(u32::from(inside))))
                }
                ScriptDispatchSelector::Handler(66) => {
                    let &[id, output] = instruction.references.as_slice() else {
                        return Err("Handler(66) requires a building ID and weight output".into());
                    };
                    let Some(class) = lookup(id)
                        .and_then(|handle| identity_metadata(world, handle).ok()?.original_id)
                        .and_then(|id| mission.object_drafts.get(id.0 as usize))
                        .and_then(|draft| draft.unit_dat_header_flags)
                    else {
                        return Ok(None);
                    };
                    let cell = values
                        .get_mut(output as usize)
                        .filter(|value| matches!(value, VarSetDefault::FloatBits(_)))
                        .ok_or("Handler(66) requires a float weight output")?;
                    *cell = VarSetDefault::FloatBits(building_weight(class).to_bits());
                    Ok(Some(VarSetDefault::Dword(class)))
                }
                ScriptDispatchSelector::Handler(2) => {
                    mission.clan_ai_events.submit_problem(
                        clan,
                        &bundle.package,
                        varset,
                        values,
                        &instruction.references,
                    )?;
                    Ok(None)
                }
                selector => Err(format!("{selector:?}")),
            }
        },
    );
    if let Err((event, instruction, reason)) = result {
        mission
            .clan_ai_events
            .report_stop(bundle, event, instruction, reason);
    }
}

fn building_weight(class: u32) -> f32 {
    match class {
        0x8000_0002 => 0.04,
        0x8000_0010 => 0.05,
        _ => 0.0,
    }
}