aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/destruction.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/destruction.rs')
-rw-r--r--crates/fparkan-runtime/src/destruction.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/destruction.rs b/crates/fparkan-runtime/src/destruction.rs
new file mode 100644
index 00000000..38a4e38f
--- /dev/null
+++ b/crates/fparkan-runtime/src/destruction.rs
@@ -0,0 +1,80 @@
+//! Presentation events for projectile damage that destroys a unit.
+
+use crate::{autodemo::MOBILE_UNIT, Engine, MissionControlDeviceTableState, ObjectHandle};
+use fparkan_world::transform_state;
+
+/// Position and attached EXPL resource of a chassis at lethal contact.
+pub struct UnitDestructionEffect {
+ /// World-space origin of the destroyed chassis.
+ pub position: [f32; 3],
+ /// Archive containing the explosion description.
+ pub archive: String,
+ /// Name of the explosion description.
+ pub name: String,
+}
+
+/// Collect lethal projectile events when a scene is presenting the mission.
+pub fn collect_unit_destruction_effects(engine: &mut Engine) {
+ engine.destruction_effects.get_or_insert_with(Vec::new);
+}
+
+/// Takes each newly destroyed unit once, after both player and AI damage.
+pub fn take_unit_destruction_effects(engine: &mut Engine) -> Vec<UnitDestructionEffect> {
+ engine
+ .destruction_effects
+ .as_mut()
+ .map(std::mem::take)
+ .unwrap_or_default()
+}
+
+/// One explosion descriptor per prepared mobile chassis, including reserves.
+pub fn unit_destruction_effect_sources(
+ engine: &Engine,
+) -> impl Iterator<Item = (String, String)> + '_ {
+ engine.loaded.iter().flat_map(|mission| {
+ mission.object_drafts.iter().filter_map(|draft| {
+ if !draft
+ .unit_dat_header_flags
+ .is_some_and(|class| class & MOBILE_UNIT != 0)
+ {
+ return None;
+ }
+ let id = draft.original_id?;
+ let table = mission
+ .control_device_tables
+ .iter()
+ .find(|table| table.original_id == Some(id))?;
+ root_effect(table)
+ })
+ })
+}
+
+pub(super) fn pending_effect(
+ engine: &Engine,
+ handle: ObjectHandle,
+) -> Option<UnitDestructionEffect> {
+ engine.destruction_effects.as_ref()?;
+ let mission = engine.loaded.as_ref()?;
+ let table = mission
+ .control_device_tables
+ .iter()
+ .find(|table| table.handle == handle)?;
+ let (archive, name) = root_effect(table)?;
+ let position = transform_state(&engine.world, handle)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ Some(UnitDestructionEffect {
+ position,
+ archive,
+ name,
+ })
+}
+
+fn root_effect(table: &MissionControlDeviceTableState) -> Option<(String, String)> {
+ let row = crate::control_node_provider_row(table, 0).ok()?;
+ let effect = &row.source_row.effect;
+ let archive = crate::bounded_ascii_c_string(&effect.archive)?;
+ let name = crate::bounded_ascii_c_string(&effect.name)?;
+ (!archive.is_empty() && !name.is_empty()).then_some((archive, name))
+}