diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/destruction.rs | 80 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 11 |
4 files changed, 105 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)) +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 9d7deec8..1ed90b32 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -75,6 +75,11 @@ mod clan_script_init; mod clan_targeting; mod construction; mod construction_site; +mod destruction; +pub use destruction::{ + collect_unit_destruction_effects, take_unit_destruction_effects, + unit_destruction_effect_sources, UnitDestructionEffect, +}; mod flight_altitude; mod ground_support; mod laboratory; @@ -1397,6 +1402,7 @@ pub struct Engine { autodemo_combat: Option<unit_combat::AutoDemoCombat>, autodemo_reinforcements: Option<autodemo::LiveReinforcements>, production: Option<production::Production>, + destruction_effects: Option<Vec<UnitDestructionEffect>>, } struct LoadedMissionState { @@ -1785,6 +1791,7 @@ pub fn create(services: EngineServices) -> Result<Engine, EngineError> { autodemo_combat: None, autodemo_reinforcements: None, production: None, + destruction_effects: None, }) } @@ -2733,6 +2740,9 @@ node_provider_distribution_suspended: false, engine.autodemo_combat = autodemo_combat; engine.autodemo_reinforcements = autodemo_reinforcements; engine.production = production; + if let Some(effects) = engine.destruction_effects.as_mut() { + effects.clear(); + } let free_research = research::FreeResearch::new(&object_drafts[..reserve_start]); let chassis_resources = CachedResourceRepository::new(vfs.clone()); let unit_chassis_flags = unit_chassis::load_flags(&chassis_resources, &object_drafts); diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 15ffce85..2ad708eb 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -379,7 +379,11 @@ pub fn apply_projectile_hit( // shortcut: use the projectile provider volume directly, with immediate // removal on root depletion until native damage and death timing are wired. if volume >= remaining { + let effect = super::destruction::pending_effect(engine, handle); request_delete(&mut engine.world, handle).map_err(ControlResourceAccessError::World)?; + if let (Some(effect), Some(events)) = (effect, engine.destruction_effects.as_mut()) { + events.push(effect); + } return Ok(Some((0.0, true))); } apply_loaded_mission_control_node_delta(engine, handle, 0, -volume)?; diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index 777aee89..f1e86aab 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -720,9 +720,12 @@ mod tests { let mut observed_aimed_turret = false; let mut observed_finished_segment = false; let mut observed_weapon_effect = false; + crate::collect_unit_destruction_effects(&mut engine); let mut observed_unavailable = engine.autodemo_combat.as_ref().unwrap().unavailable as u64; for now in (0..=120_000).step_by(50) { let frame = frame(&mut engine, now).unwrap(); + let destruction_effects = crate::take_unit_destruction_effects(&mut engine); + assert!(crate::take_unit_destruction_effects(&mut engine).is_empty()); observed_unavailable += frame.autodemo_combat_unavailable as u64; let shots = frame .autodemo_combat_events @@ -822,6 +825,14 @@ mod tests { .iter() .find(|event| matches!(event, AutoDemoCombatEvent::Destroyed { .. })) { + assert!(destruction_effects.iter().any(|event| { + event.archive == "system.rlb" + && event.name == "explode_rbr_b.exp" + && event + .position + .iter() + .all(|coordinate| coordinate.is_finite()) + })); assert!(identity_metadata(&engine.world, *target).is_err()); let ((_, retired), total) = combat .report |
