//! Destruction lifecycle and presentation events for damaged units. use crate::{ autodemo::MOBILE_UNIT, ControlResourceAccessError, Engine, MissionControlDeviceTableState, MissionControlNodeProviderRow, MissionControlNodeProviderTableStatus, ObjectHandle, UNIT_DAT_CONTROL_CLASS_HIGH_BIT, }; use fparkan_world::{identity_metadata, request_delete, transform_state}; pub(super) fn destroy( engine: &mut Engine, handle: ObjectHandle, ) -> Result<(), ControlResourceAccessError> { let effect = 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); } Ok(()) } /// Geometry supplied by the destroyed object to its authored explosion. pub enum DestroyedMesh { /// Mounted models after the loss of the whole chassis. Mounted { /// Prepared actor index. object_index: usize, /// Component and node indices of pieces already separated before the chassis failed. depleted_nodes: Vec<(usize, usize)>, }, /// One separated node of a still live warbot. ChassisNode { /// Prepared actor index. object_index: usize, /// Unit DAT component containing the mesh. component_index: usize, /// Local node of that model. node: usize, /// World position of the hull at impact. hull_origin: [f32; 3], }, } /// Position and attached EXPL resource of a destroyed chassis or building part. pub struct UnitDestructionEffect { /// Prepared mesh to move when the effect requests built-in fragments. pub mesh: Option, /// World-space origin of the effect. 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 { engine .destruction_effects .as_mut() .map(std::mem::take) .unwrap_or_default() } /// Explosion descriptors for prepared chassis roots and damageable model parts. pub fn unit_destruction_effect_sources( engine: &Engine, ) -> impl Iterator + '_ { engine.loaded.iter().flat_map(|mission| { mission .object_drafts .iter() .filter_map(|draft| { let class = draft.unit_dat_header_flags?; let id = draft.original_id?; let table = mission .control_device_tables .iter() .find(|table| table.original_id == Some(id))?; let MissionControlNodeProviderTableStatus::Available(rows) = &table.node_provider_status else { return None; }; Some(if class & UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0 { rows.as_slice() } else if class == super::boarding::WARBOT_CLASS { rows.as_slice() } else if class & MOBILE_UNIT != 0 { rows.get(..1).unwrap_or_default() } else { &[] }) }) .flat_map(|rows| rows.iter().filter_map(node_effect)) }) } pub(super) fn part_effect( engine: &Engine, row: &MissionControlNodeProviderRow, contact: super::ProjectileContact, ) -> Option { let (archive, name) = node_effect(row)?; let mesh = match contact.node { Some(super::ProjectileNode::Chassis(_) | super::ProjectileNode::Mounted(_)) if row.current_flags.is_some_and(|flags| flags & 2 == 0) => { let id = identity_metadata(&engine.world, contact.handle) .ok()? .original_id?; let hull = transform_state(&engine.world, contact.handle).ok()?; Some(DestroyedMesh::ChassisNode { object_index: id.0 as usize, component_index: row.unit_component_index, node: row.source_row_index, hull_origin: hull.position.map(f32::from_bits), }) } _ => None, }; Some(UnitDestructionEffect { mesh, position: contact.position, archive, name, }) } pub(super) fn pending_effect( engine: &Engine, handle: ObjectHandle, ) -> Option { 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 object_index = identity_metadata(&engine.world, handle) .ok() .and_then(|identity| identity.original_id) .and_then(|id| { mission .object_drafts .iter() .position(|draft| draft.original_id == Some(id)) }); let position = transform_state(&engine.world, handle) .ok()? .position .map(f32::from_bits); Some(UnitDestructionEffect { mesh: object_index.map(|object_index| { let depleted_nodes = match &table.node_provider_status { MissionControlNodeProviderTableStatus::Available(rows) => rows .iter() .filter(|row| { row.unit_component_index != 0 && row.current_flags.is_some_and(|flags| flags & 0x12 == 0x10) }) .map(|row| (row.unit_component_index, row.source_row_index)) .collect(), MissionControlNodeProviderTableStatus::Unavailable(_) => Vec::new(), }; DestroyedMesh::Mounted { object_index, depleted_nodes, } }), position, archive, name, }) } fn root_effect(table: &MissionControlDeviceTableState) -> Option<(String, String)> { let row = crate::control_node_provider_row(table, 0).ok()?; node_effect(row) } fn node_effect(row: &MissionControlNodeProviderRow) -> Option<(String, String)> { if f32::from_bits(row.initial_amount_bits) <= 0.0 { return None; } 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)) }