use crate::{ apply_loaded_mission_control_node_delta, projectile_parts::{chassis_part_contact, root_model}, ControlResourceAccessError, Engine, MissionAssets, MissionControlNodeProviderTableStatus, TerrainWorld, CONTROL_CLASS_SOURCE_BULL, UNIT_DAT_CONTROL_CLASS_HIGH_BIT, }; use fparkan_assets::AssetManager; use fparkan_prototype::{resolve_prototype_all, PrototypeSource}; use fparkan_render::LegacyIron3dEulerTransform; use fparkan_resource::{resource_name, CachedResourceRepository}; use fparkan_terrain::{FullSurfaceMask, SurfaceQuery, TerrainError}; use fparkan_vfs::Vfs; use fparkan_world::{handle_by_original_id, transform_state, ObjectHandle}; use std::sync::Arc; pub(super) const ANIMAL_UNIT_CLASS: u32 = 0x2000_0000; /// Prepared visuals and travel parameters for one projectile prototype. pub struct ProjectileAssets { /// Visual, materials and control profile for the single projectile mesh. pub assets: MissionAssets, /// Forward speed in world units per second. pub speed: f32, /// Maximum age on the mission clock, in milliseconds. pub lifetime_ms: u32, /// Initial provider volume of the projectile. pub volume: Option, /// Whether the BULL profile permits bringing the shot onto the sight ray. pub sight_convergence: bool, pub(super) autonomous_fire_distance: f32, } /// Prepares the BULL prototype referenced by a manual weapon. pub fn load_player_projectile( engine: &Engine, archive: &str, name: &str, ) -> Result { let vfs = engine.services.vfs.as_ref().ok_or("missing resource VFS")?; load_projectile(vfs, archive, name) } pub(super) fn load_projectile( vfs: &Arc, archive: &str, name: &str, ) -> Result { if !archive.eq_ignore_ascii_case("objects.rlb") || name.is_empty() || name.bytes().any(|byte| matches!(byte, b'/' | b'\\' | b'.')) { return Err(format!("unsupported projectile key {archive}:{name}")); } let asset_manager = AssetManager::new(CachedResourceRepository::new(vfs.clone())); let prototypes = resolve_prototype_all( asset_manager.repository(), vfs.as_ref(), &resource_name(name.as_bytes()), ) .map_err(|error| error.to_string())?; let [prototype] = prototypes.as_slice() else { return Err("expected a single projectile prototype".into()); }; if prototype.source != PrototypeSource::ObjectsRegistry || prototype.source_type != Some(CONTROL_CLASS_SOURCE_BULL) { return Err("expected an objects.rlb BULL prototype".into()); } let assets = asset_manager .prepare_mission_assets(&[0..1], &prototypes) .map_err(|error| error.to_string())?; let [controls] = assets.object_component_controls.as_slice() else { return Err("missing projectile component".into()); }; let [Some(control)] = controls.as_slice() else { return Err("missing projectile Control profile".into()); }; let speed = f32::from_bits(control.native_profile_words[7]); let lifetime_ms = control.native_profile_words[18]; // Control+0x297b1 forces a nonzero launch mode for this profile flag. let sight_convergence = control.native_profile_words[21] == 0; let autonomous_fire_distance = f32::from_bits(control.native_profile_words[22]); let volume = control.node_data.as_ref().and_then(|data| { let amount = data.rows.first()?.max_volume; (amount.is_finite() && amount > 0.0).then_some(amount) }); if !speed.is_finite() || speed <= 0.0 || lifetime_ms == 0 || lifetime_ms >= 0x8000_0000 { return Err("invalid projectile speed or lifetime".into()); } Ok(ProjectileAssets { assets, speed, lifetime_ms, volume, sight_convergence, autonomous_fire_distance, }) } /// One in-flight projectile with a pose sampled on the mission queue clock. #[derive(Clone, Copy)] pub struct PlayerProjectile { /// World-space center of the projectile. pub position: [f32; 3], /// Unit world-space heading along local +Y. pub direction: [f32; 3], speed: f32, lifetime_ms: u32, born_ms: u32, updated_ms: u32, pub(super) volume: Option, } impl PlayerProjectile { /// Starts a straight-flight projectile at a validated world point. pub fn new( position: [f32; 3], direction: [f32; 3], speed: f32, lifetime_ms: u32, clock_ms: u32, ) -> Option { let length = direction.iter().map(|axis| axis * axis).sum::().sqrt(); if !position.iter().all(|axis| axis.is_finite()) || !length.is_finite() || length <= 0.0 || !speed.is_finite() || speed <= 0.0 || lifetime_ms == 0 { return None; } Some(Self { position, direction: direction.map(|axis| axis / length), speed, lifetime_ms, born_ms: clock_ms, updated_ms: clock_ms, volume: None, }) } /// Sets the launch volume. A pierced shield spends part of this volume in flight. pub fn with_volume(mut self, volume: Option) -> Self { self.volume = volume; self } /// Queue time at launch, retained when the projectile's pose changes. pub fn born_ms(&self) -> u32 { self.born_ms } /// Advances this projectile and returns whether it remains in flight. /// The callback identifies contacts with the world along the traveled segment. pub fn advance( &mut self, clock_ms: u32, mut collides: impl FnMut([[f32; 3]; 2]) -> Result, ) -> Result { let age = clock_ms.wrapping_sub(self.born_ms); let last_age = self.updated_ms.wrapping_sub(self.born_ms); let elapsed_ms = age.min(self.lifetime_ms).saturating_sub(last_age); if elapsed_ms != 0 { let distance = self.speed * (elapsed_ms as f32 * 0.001); let end = std::array::from_fn(|axis| self.position[axis] + self.direction[axis] * distance); if !end.iter().all(|axis| axis.is_finite()) || collides([self.position, end])? { return Ok(false); } self.position = end; self.updated_ms = clock_ms; } Ok(age < self.lifetime_ms) } } /// Tests the projectile segment against the mission terrain triangles. pub fn projectile_terrain_hit( terrain: &TerrainWorld, [start, end]: [[f32; 3]; 2], ) -> Result, TerrainError> { let direction = std::array::from_fn(|axis| end[axis] - start[axis]); // The terrain query reports the ray parameter, so this segment ends at 1. Ok(terrain .raycast(start, direction, FullSurfaceMask(0))? .filter(|hit| hit.distance <= 1.0) .map(|hit| hit.distance)) } /// A live unit reached by a projectile, with the fraction of its travel used. #[derive(Clone, Copy, Debug)] pub struct ProjectileContact { pub handle: ObjectHandle, pub fraction: f32, /// World point on the segment at the detected contact. pub position: [f32; 3], /// Provider row identified by a triangle hit, if its geometry was projected. pub node: Option, } /// Control node hit by a projectile's segment. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ProjectileNode { /// A node of the building model. Building(u32), /// A node of the warbot's chassis model. Chassis(u32), /// A provider index in the assembled warbot's global Control table. Mounted(u32), /// An indestructible communicator field. CaptureBarrier, } impl ProjectileContact { /// Provider key for a building hit, for callers that only track structures. pub fn building_node(self) -> Option { if let Some(ProjectileNode::Building(key)) = self.node { Some(key) } else { None } } /// Applies the volume remaining after crossing any fields along the flight. pub fn apply_body_impact( self, engine: &mut Engine, volume: f32, ) -> Result { let key = match self.node { Some(ProjectileNode::CaptureBarrier) => return Ok(ProjectileImpact::Unresolved), None | Some(ProjectileNode::Chassis(0)) => None, Some( ProjectileNode::Chassis(key) | ProjectileNode::Mounted(key) | ProjectileNode::Building(key), ) => Some(key), }; Ok(match apply_contact_damage(engine, self, key, volume)? { Some((remaining, removed)) => ProjectileImpact::Damage { remaining, removed }, None => ProjectileImpact::Unresolved, }) } } /// Finds the first physical unit or building contact along this flight segment. pub fn projectile_unit_hit( engine: &Engine, shooter: ObjectHandle, segment: [[f32; 3]; 2], ) -> Option { projectile_unit_hit_with_poses(engine, shooter, segment, |index| { engine.autodemo_combat.as_ref()?.poses_for_collision(index) }) } pub(super) fn projectile_unit_hit_with_poses<'a>( engine: &Engine, shooter: ObjectHandle, segment: [[f32; 3]; 2], poses: impl Fn(usize) -> Option<&'a super::weapon_pose::WeaponPoses>, ) -> Option { let mission = engine.loaded.as_ref()?; let passenger = mission.boarding.map(|boarding| boarding.hero.handle); mission .object_drafts .iter() .enumerate() .filter_map(|(index, draft)| { let class = draft.unit_dat_header_flags?; let building = class & UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0; let animal = class == ANIMAL_UNIT_CLASS; if !building && !animal && class & super::autodemo::MOBILE_UNIT == 0 { return None; } let handle = handle_by_original_id(&engine.world, draft.original_id?)?; if handle == shooter || passenger == Some(handle) { return None; } let state = transform_state(&engine.world, handle).ok()?; let (center, radius) = unit_sphere(&mission.mission_assets, index, state)?; let sphere_fraction = segment_sphere_fraction(segment, center, radius); let (fraction, node) = if building { sphere_fraction?; let (fraction, node) = crate::capture_barrier::projectile_contact( mission, index, handle, state, segment, )?; (fraction, Some(node)) } else if class == super::boarding::WARBOT_CLASS { chassis_part_contact( mission, index, handle, state, segment, sphere_fraction, poses(index), )? } else { (sphere_fraction?, None) }; Some(ProjectileContact { handle, fraction, position: segment_point(segment, fraction), node, }) }) .min_by(|a, b| a.fraction.total_cmp(&b.fraction)) } pub(super) fn unit_sphere( assets: &MissionAssets, index: usize, state: fparkan_world::TransformState, ) -> Option<([f32; 3], f32)> { let model = &root_model(assets, index)?.validated; let scale = state.scale.map(f32::from_bits); let transform = LegacyIron3dEulerTransform { translation: state.position.map(f32::from_bits), orientation_radians: state.orientation.map(f32::from_bits), }; let center = transform.try_transform_scaled_point(model.bounding_sphere_center, scale)?; let radius = model.bounding_sphere_radius * scale.into_iter().map(f32::abs).fold(0.0, f32::max); Some((center, radius)) } /// Result of applying a projectile to a building or unit. #[derive(Clone, Copy, Debug, PartialEq)] pub enum ProjectileImpact { /// The shield took the entire hit and the hull was untouched. ShieldAbsorbed, /// The contacted part lost health, or the unit was destroyed. Damage { /// Health left on the contacted part. remaining: f32, /// Whether the impact removed the whole unit. removed: bool, }, /// No damage result was available for the contacted target or part. Unresolved, } /// Applies projectile damage to the contacted part of a building or unit. /// Returns the hull damage result when the hit reached the hull. pub fn apply_projectile_contact( engine: &mut Engine, contact: ProjectileContact, volume: f32, ) -> Result, ControlResourceAccessError> { Ok(match apply_projectile_impact(engine, contact, volume)? { ProjectileImpact::Damage { remaining, removed } => Some((remaining, removed)), ProjectileImpact::ShieldAbsorbed | ProjectileImpact::Unresolved => None, }) } /// Applies a projectile and distinguishes shield absorption from unresolved damage. pub fn apply_projectile_impact( engine: &mut Engine, contact: ProjectileContact, volume: f32, ) -> Result { if contact.node == Some(ProjectileNode::CaptureBarrier) { return Ok(ProjectileImpact::Unresolved); } let unabsorbed = super::projectile_shield::absorb(engine, contact, volume); if volume > 0.0 && unabsorbed == 0.0 { return Ok(ProjectileImpact::ShieldAbsorbed); } contact.apply_body_impact(engine, unabsorbed) } fn apply_contact_damage( engine: &mut Engine, contact: ProjectileContact, key: Option, volume: f32, ) -> Result, ControlResourceAccessError> { let table = engine.loaded.as_ref().and_then(|mission| { mission .control_device_tables .iter() .find(|table| table.handle == contact.handle) }); let volume = super::projectile_damage::resisted_volume( volume, table.and_then(|table| table.projectile_resistance_bits), ); let Some(key) = key else { return apply_projectile_hit(engine, contact.handle, volume); }; let row = table.and_then(|table| super::control_node_provider_row(table, key).ok()); let Some(row) = row else { return Ok(None); }; let remaining = f32::from_bits(row.current_amount_bits); if !volume.is_finite() || volume <= 0.0 || !remaining.is_finite() || remaining <= 0.0 || row.current_flags.is_some_and(|flags| flags & 0x10 != 0) { return Ok(None); } let warbot_part = matches!( contact.node, Some(ProjectileNode::Chassis(_) | ProjectileNode::Mounted(_)) ); if volume >= remaining && warbot_part && row.current_flags.is_some_and(|flags| flags & 1 != 0) { super::destruction::destroy(engine, contact.handle)?; return Ok(Some((0.0, true))); } // shortcut: feed the projectile NDP volume into the native reduction while impact falloff is open. let effect = (volume >= remaining && engine.destruction_effects.is_some()) .then(|| super::destruction::part_effect(engine, row, contact)) .flatten(); let updated = apply_loaded_mission_control_node_delta(engine, contact.handle, key, -volume)?; if updated.is_none() { return Ok(None); } let building_part = matches!(contact.node, Some(ProjectileNode::Building(_))); if building_part || volume >= remaining && warbot_part { let table = engine .loaded .as_mut() .and_then(|mission| { mission .control_device_tables .iter_mut() .find(|table| table.handle == contact.handle) }) .expect("successful damage updated the target table"); if building_part { if let Err(reason) = super::node_stage::update_building_impact_stage(table, key) { eprintln!("sim_coverage building_damage_stage_stub=1 object={} node={key} reason={reason:?}", contact.handle.slot); } } else { match super::node_stage::propagate_chassis_damage(table, key) { Ok(true) => { super::destruction::destroy(engine, contact.handle)?; return Ok(Some((0.0, true))); } Ok(false) => {} Err(reason) => eprintln!("sim_coverage chassis_depletion_cascade_stub=1 object={} node={key} reason={reason:?}", contact.handle.slot), } } } if let (Some(effect), Some(events)) = (effect, engine.destruction_effects.as_mut()) { events.push(effect); } Ok(Some(((remaining - volume).max(0.0), false))) } pub(super) fn segment_point([start, end]: [[f32; 3]; 2], fraction: f32) -> [f32; 3] { std::array::from_fn(|axis| start[axis] + (end[axis] - start[axis]) * fraction) } pub(super) fn segment_sphere_fraction( segment: [[f32; 3]; 2], center: [f32; 3], radius: f32, ) -> Option { segment_sphere_query(segment, center, radius, true) } pub(super) fn segment_sphere_entrance( segment: [[f32; 3]; 2], center: [f32; 3], radius: f32, ) -> Option { segment_sphere_query(segment, center, radius, false) } fn segment_sphere_query( [start, end]: [[f32; 3]; 2], center: [f32; 3], radius: f32, include_overlap: bool, ) -> Option { if !radius.is_finite() || radius <= 0.0 { return None; } let dir: [f32; 3] = std::array::from_fn(|i| end[i] - start[i]); let rel: [f32; 3] = std::array::from_fn(|i| start[i] - center[i]); let dot = |a: [f32; 3], b: [f32; 3]| a.into_iter().zip(b).map(|(a, b)| a * b).sum::(); let c = dot(rel, rel) - radius * radius; if include_overlap && c <= 0.0 { return Some(0.0); } let a = dot(dir, dir); let b = dot(rel, dir); // Control+0x1e9f0 reports overlap without a crossing flag inside the sphere. if !include_overlap && (c < 0.0 || a < 1e-6 || b > 0.0) { return None; } let d = b * b - a * c; let fraction = (-b - d.sqrt()) / a; (0.0..=1.0).contains(&fraction).then_some(fraction) } /// Applies the first pass of combat damage to the root Control provider. /// Returns the remaining volume and whether the world removed the unit. pub fn apply_projectile_hit( engine: &mut Engine, handle: ObjectHandle, volume: f32, ) -> Result, ControlResourceAccessError> { let Some(remaining) = engine.loaded.as_ref().and_then(|mission| { let table = mission .control_device_tables .iter() .find(|table| table.handle == handle)?; let MissionControlNodeProviderTableStatus::Available(rows) = &table.node_provider_status else { return None; }; Some(f32::from_bits(rows.first()?.current_amount_bits)) }) else { return Ok(None); }; if !volume.is_finite() || volume <= 0.0 || !remaining.is_finite() || remaining <= 0.0 { return Ok(None); } // shortcut: use the projectile provider volume directly, with immediate // removal on root depletion until native damage and death timing are wired. if volume >= remaining { super::destruction::destroy(engine, handle)?; return Ok(Some((0.0, true))); } apply_loaded_mission_control_node_delta(engine, handle, 0, -volume)?; Ok(Some((remaining - volume, false))) } #[cfg(test)] mod tests { use super::*; #[test] #[ignore = "requires licensed corpus"] fn licensed_mounted_node_uses_the_stock_warbot_pose_and_provider() { use crate::{ collect_unit_destruction_effects, create, frame, load_gameplay_mission, take_unit_destruction_effects, EngineServices, MissionMode, MissionRequest, MissionStartupInput, }; use fparkan_vfs::DirectoryVfs; use fparkan_world::OriginalObjectId; let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); load_gameplay_mission( &mut engine, MissionRequest { key: "MISSIONS/Autodemo.00/data.tma".into(), mode: MissionMode::AutoDemo, }, MissionStartupInput { queue_time_ms: 0, research_creation_gate: false, }, None, ) .unwrap(); frame(&mut engine, 0).unwrap(); let target = handle_by_original_id(&engine.world, OriginalObjectId(0)).unwrap(); let shooter = handle_by_original_id(&engine.world, OriginalObjectId(1)).unwrap(); let pose = transform_state(&engine.world, target).unwrap(); let mission = engine.loaded.as_ref().unwrap(); let table = mission .control_device_tables .iter() .find(|table| table.handle == target) .unwrap(); let hull_before = crate::control_node_provider_row(table, 0) .unwrap() .current_amount_bits; let (center, radius) = unit_sphere(&mission.mission_assets, 0, pose).unwrap(); let hit = (0..3) .flat_map(|axis| (-9..=9).flat_map(move |a| (-9..=9).map(move |b| (axis, a, b)))) .find_map(|(axis, a, b)| { let mut center = center; center[(axis + 1) % 3] += a as f32 * radius / 8.0; center[(axis + 2) % 3] += b as f32 * radius / 8.0; let mut segment = [center; 2]; segment[0][axis] -= radius * 2.0; segment[1][axis] += radius * 2.0; let hit = projectile_unit_hit(&engine, shooter, segment)?; let Some(ProjectileNode::Mounted(key)) = hit.node else { return None; }; let row = crate::control_node_provider_row(table, key).ok()?; (hit.handle == target && row.current_flags == Some(0) && row.source_row.effect.name[0] != 0) .then_some((segment, hit, row.unit_component_index, row.source_row_index)) }) .expect("a ray hits a mounted part on the stock AutoDemo warbot"); let (segment, contact, component_index, local_node) = hit; collect_unit_destruction_effects(&mut engine); assert_eq!( apply_projectile_contact(&mut engine, contact, f32::MAX).unwrap(), Some((0.0, false)) ); let table = engine .loaded .as_ref() .unwrap() .control_device_tables .iter() .find(|table| table.handle == target) .unwrap(); assert_eq!( crate::control_node_provider_row(table, 0) .unwrap() .current_amount_bits, hull_before ); assert!(crate::depleted_warbot_nodes(&engine) .any(|node| node == (0, component_index, local_node))); assert_ne!( projectile_unit_hit(&engine, shooter, segment).and_then(|hit| hit.node), contact.node ); assert!( matches!(take_unit_destruction_effects(&mut engine).as_slice(), [crate::UnitDestructionEffect { mesh: Some(crate::DestroyedMesh::ChassisNode { component_index: component, node, .. }), .. }] if *component == component_index && *node == local_node) ); } #[test] #[ignore = "requires licensed corpus"] fn licensed_chassis_node_takes_damage_without_destroying_the_warbot() { use crate::{ collect_unit_destruction_effects, create, load_gameplay_mission, starting_cockpit, take_unit_destruction_effects, EngineServices, MissionMode, MissionRequest, MissionStartupInput, }; use fparkan_vfs::DirectoryVfs; use fparkan_world::OriginalObjectId; let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); load_gameplay_mission( &mut engine, MissionRequest { key: "MISSIONS/Single.01/data.tma".into(), mode: MissionMode::SingleMission, }, MissionStartupInput { queue_time_ms: 0, research_creation_gate: false, }, None, ) .unwrap(); let shooter = starting_cockpit(&engine).unwrap().handle; let target = handle_by_original_id(&engine.world, OriginalObjectId(14)).unwrap(); let mission = engine.loaded.as_ref().unwrap(); let pose = transform_state(&engine.world, target).unwrap(); let ([x, y, z], radius) = unit_sphere(&mission.mission_assets, 14, pose).unwrap(); let (contact, node) = (-4..=4) .flat_map(|y_offset| (-4..=4).map(move |z_offset| (y_offset, z_offset))) .find_map(|(y_offset, z_offset)| { let y = y + y_offset as f32 * radius / 5.0; let z = z + z_offset as f32 * radius / 5.0; let segment = [[x - 2.0 * radius, y, z], [x + 2.0 * radius, y, z]]; let hit = projectile_unit_hit(&engine, shooter, segment)?; let Some(ProjectileNode::Chassis(node @ 2..=9)) = hit.node else { return None; }; (hit.handle == target).then_some((hit, node)) }) .expect("a side shot intersects a chassis part of the stock heavy warbot"); let amount = |engine: &Engine, key| { let table = engine .loaded .as_ref() .unwrap() .control_device_tables .iter() .find(|table| table.handle == target) .unwrap(); crate::control_node_provider_row(table, key) .unwrap() .current_amount_bits }; let root_before = amount(&engine, 0); assert!(f32::from_bits(amount(&engine, node)) > 0.0); collect_unit_destruction_effects(&mut engine); assert_eq!( apply_projectile_contact(&mut engine, contact, f32::MAX).unwrap(), Some((0.0, false)) ); assert_eq!(amount(&engine, node), 0.0_f32.to_bits()); assert_eq!(amount(&engine, 0), root_before); assert!(crate::depleted_chassis_nodes(&engine).any(|item| item == (14, node as usize))); assert!( crate::cockpit_instruments::instrument_node_states(&engine, target) .any(|state| state == ((0, node as usize), None)) ); assert!(transform_state(&engine.world, target).is_ok()); let effects = take_unit_destruction_effects(&mut engine); assert_eq!(effects.len(), 1); assert_eq!(effects[0].position, contact.position); assert!( matches!(effects[0].mesh, Some(crate::DestroyedMesh::ChassisNode { object_index: 14, node: key, .. }) if key == node as usize) ); } #[test] #[ignore = "requires licensed corpus"] fn licensed_factory_part_explodes_once_on_depletion() { use crate::{ collect_unit_destruction_effects, create, load_gameplay_mission, take_unit_destruction_effects, unit_destruction_effect_sources, EngineServices, MissionMode, MissionRequest, MissionStartupInput, }; use fparkan_vfs::DirectoryVfs; use fparkan_world::OriginalObjectId; let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); load_gameplay_mission( &mut engine, MissionRequest { key: "MISSIONS/Single.01/data.tma".into(), mode: MissionMode::SingleMission, }, MissionStartupInput { queue_time_ms: 0, research_creation_gate: false, }, None, ) .unwrap(); let plant = handle_by_original_id(&engine.world, OriginalObjectId(5)).unwrap(); let intact_bounds = crate::cockpit_instruments::instrument_bounds(&engine, 5).unwrap(); let table = engine .loaded .as_mut() .unwrap() .control_device_tables .iter_mut() .find(|table| table.handle == plant) .unwrap(); if let crate::MissionControlDeviceTableStatus::Available(devices) = &mut table.status { for device in devices.iter_mut().filter(|device| device.factory_tag == 9) { device.class9_values_bits = Some([0; 6]); } } let pose = transform_state(&engine.world, plant).unwrap(); let assets = &engine.loaded.as_ref().unwrap().mission_assets; let ([x, y, z], radius) = unit_sphere(assets, 5, pose).unwrap(); let shooter = crate::starting_cockpit(&engine).unwrap().handle; let intact_roof = (-8..=8) .flat_map(|i| (-8..=8).map(move |j| (i, j))) .filter_map(|(i, j)| { let x = x + radius * i as f32 / 8.0; let y = y + radius * j as f32 / 8.0; let start = [x, y, z + radius]; let hit = projectile_unit_hit(&engine, shooter, [start, [x, y, z - radius]])?; (hit.handle == plant && hit.node == Some(ProjectileNode::Building(1))) .then_some([start, [x, y, hit.position[2] - 0.25]]) }) .collect::>(); assert!(!intact_roof.is_empty()); let stage_contact = ProjectileContact { handle: plant, fraction: 0.5, position: [1300.0, 627.0, 84.0], node: Some(ProjectileNode::Building(1)), }; assert_eq!( apply_projectile_contact(&mut engine, stage_contact, 30_000.0).unwrap(), Some((20_000.0, false)) ); let table = crate::loaded_mission_control_device_tables(&engine) .unwrap() .iter() .find(|table| table.handle == plant) .unwrap(); assert_eq!( crate::control_node_provider_row(table, 1) .unwrap() .mesh_state .unwrap() .visible_stage, 1 ); assert!( crate::cockpit_instruments::instrument_node_states(&engine, plant) .any(|state| state == ((0, 1), Some(1))) ); assert_eq!( crate::cockpit_instruments::instrument_bounds(&engine, 5), Some(intact_bounds) ); assert_eq!( crate::control_node_provider_row(table, 3) .unwrap() .mesh_state .unwrap() .visible_stage, 0 ); assert!(intact_roof .iter() .any(|&ray| projectile_unit_hit(&engine, shooter, ray).is_none())); let contact = ProjectileContact { handle: plant, fraction: 0.5, position: [1300.0, 627.0, 84.0], node: Some(ProjectileNode::Building(3)), }; collect_unit_destruction_effects(&mut engine); assert_eq!( apply_projectile_contact(&mut engine, contact, 5_000.0).unwrap(), Some((5_000.0, false)) ); assert!(take_unit_destruction_effects(&mut engine).is_empty()); assert_eq!( apply_projectile_contact(&mut engine, contact, 5_000.0).unwrap(), Some((0.0, false)) ); let events = take_unit_destruction_effects(&mut engine); let [effect] = events.as_slice() else { panic!("expected one EXPL at the failed part"); }; assert_eq!(effect.position, contact.position); assert!(effect.mesh.is_none()); assert_eq!(effect.name, "explode_frt_b.exp"); assert!(unit_destruction_effect_sources(&engine) .any(|source| source == (effect.archive.clone(), effect.name.clone()))); assert!(apply_projectile_contact(&mut engine, contact, 5_000.0) .unwrap() .is_none()); assert!(take_unit_destruction_effects(&mut engine).is_empty()); assert!(transform_state(&engine.world, plant).is_ok()); } #[test] #[ignore = "requires licensed corpus"] fn licensed_constructor_animal_takes_projectile_damage() { use crate::{ create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode, MissionRequest, MissionStartupInput, }; use fparkan_vfs::DirectoryVfs; let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); load_gameplay_mission( &mut engine, MissionRequest { key: "MISSIONS/CAMPAIGN/CAMPAIGN.00/Mission.02/data.tma".into(), mode: MissionMode::SingleMission, }, MissionStartupInput { queue_time_ms: 0, research_creation_gate: false, }, None, ) .unwrap(); let shooter = starting_cockpit(&engine).unwrap().handle; let mission = engine.loaded.as_ref().unwrap(); let (index, draft) = mission .object_drafts .iter() .enumerate() .find(|(_, draft)| draft.unit_dat_header_flags == Some(ANIMAL_UNIT_CLASS)) .unwrap(); let creature = handle_by_original_id(&engine.world, draft.original_id.unwrap()).unwrap(); let pose = transform_state(&engine.world, creature).unwrap(); let ([x, y, z], radius) = unit_sphere(&mission.mission_assets, index, pose).unwrap(); let shot = [[x, y, z + 2.0 * radius], [x, y, z]]; let contact = projectile_unit_hit(&engine, shooter, shot).expect("animal contact"); assert_eq!(contact.handle, creature); let table = mission .control_device_tables .iter() .find(|table| table.handle == creature) .unwrap(); let hp = f32::from_bits( crate::control_node_provider_row(table, 0) .unwrap() .current_amount_bits, ); assert!(hp.is_finite() && hp > 0.0); let remaining = hp * 0.5; assert_eq!( apply_projectile_contact(&mut engine, contact, remaining).unwrap(), Some((remaining, false)) ); assert_eq!( apply_projectile_contact(&mut engine, contact, remaining).unwrap(), Some((0.0, true)) ); assert!(transform_state(&engine.world, creature).is_err()); } #[test] fn projectile_uses_queue_age_and_tests_only_the_traveled_segment() { let mut shot = PlayerProjectile::new([0.0; 3], [0.0, 2.0, 0.0], 100.0, 80, u32::MAX - 39).unwrap(); assert!(shot .advance(0, |[from, to]| { assert_eq!(from, [0.0; 3]); assert!((to[1] - 4.0).abs() < 1e-6); Ok(false) }) .unwrap()); assert!(shot.advance(0, |_| panic!("replayed sample")).unwrap()); assert!(!shot .advance(90, |[from, to]| { assert!((from[1] - 4.0).abs() < 1e-6); assert!((to[1] - 8.0).abs() < 1e-6); Ok(true) }) .unwrap()); } } #[test] fn shield_entrance_is_distinct_from_an_initial_overlap() { let center = [0.0; 3]; let exit = [10.0, 4.0, 0.0]; assert_eq!( segment_sphere_entrance([[-10.0, 4.0, 0.0], exit], center, 5.0), Some(0.35) ); assert_eq!( segment_sphere_entrance([[0.0, 4.0, 0.0], exit], center, 5.0), None ); assert_eq!( segment_sphere_entrance([[-3.0, 4.0, 0.0], exit], center, 5.0), Some(0.0) ); assert_eq!( segment_sphere_entrance([[-3.0, 4.0, 0.0], [-10.0, 4.0, 0.0]], center, 5.0), None ); }