aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_capture_event.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 18:13:13 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 18:13:13 +0300
commit72ae7666d428c783a03b5378658890947f205de0 (patch)
tree7ab19d6b3104c906a84d5cf9839b103735d27e47 /crates/fparkan-runtime/src/clan_capture_event.rs
parentf7c5c3bffd565bb6b0995a220ecb7788471b7848 (diff)
downloadfparkan-72ae7666d428c783a03b5378658890947f205de0.tar.xz
fparkan-72ae7666d428c783a03b5378658890947f205de0.zip
fix(ai): dispatch building loss to the former clan
Diffstat (limited to 'crates/fparkan-runtime/src/clan_capture_event.rs')
-rw-r--r--crates/fparkan-runtime/src/clan_capture_event.rs132
1 files changed, 132 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_capture_event.rs b/crates/fparkan-runtime/src/clan_capture_event.rs
new file mode 100644
index 00000000..4410f792
--- /dev/null
+++ b/crates/fparkan-runtime/src/clan_capture_event.rs
@@ -0,0 +1,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,
+ }
+}