diff options
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile_shield.rs | 179 |
2 files changed, 106 insertions, 80 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index eef3a158..f53a3ae2 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -1453,7 +1453,7 @@ pub struct Engine { autodemo_reinforcements: Option<autodemo::LiveReinforcements>, production: Option<production::Production>, destruction_effects: Option<Vec<UnitDestructionEffect>>, - shield_impacts: Option<projectile_shield::ShieldImpacts>, + shield_impacts: Option<Vec<ShieldImpact>>, } struct LoadedMissionState { @@ -1471,6 +1471,7 @@ struct LoadedMissionState { behavior_ore_states: Vec<BehaviorOreState>, behavior_profiles: Vec<BehaviorProfileState>, control_device_tables: Vec<MissionControlDeviceTableState>, + shield_spheres: Vec<Option<([f32; 3], f32)>>, behavior_control_groupings: Vec<MissionBehaviorControlGroupingState>, research_trf_tables: Vec<MissionResearchTrfTable>, free_research: research::FreeResearch, @@ -2787,6 +2788,9 @@ node_provider_distribution_suspended: false, if let Some(effects) = engine.destruction_effects.as_mut() { effects.clear(); } + if let Some(impacts) = engine.shield_impacts.as_mut() { + impacts.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); @@ -2797,6 +2801,7 @@ node_provider_distribution_suspended: false, build_categories, prototype_graph, prototype_report, + shield_spheres: projectile_shield::prepare_shield_spheres(&mission_assets, &object_drafts), mission_assets, asset_plan: mission_asset_plan, object_drafts, diff --git a/crates/fparkan-runtime/src/projectile_shield.rs b/crates/fparkan-runtime/src/projectile_shield.rs index a0875278..541a6cb0 100644 --- a/crates/fparkan-runtime/src/projectile_shield.rs +++ b/crates/fparkan-runtime/src/projectile_shield.rs @@ -2,11 +2,11 @@ use crate::{ control_device_budget_coefficient, control_device_selector_zero, fixed_words_ascii_c_string, - projectile::unit_sphere, weapon_pose::WeaponPoses, Engine, MissionControlDeviceTableStatus, - ProjectileContact, + projectile::unit_sphere, turret_aim::hull_local_point, weapon_pose::WeaponPoses, Engine, + MissionAssets, MissionControlDeviceTableStatus, MissionObjectDraft, ProjectileContact, }; use fparkan_render::LegacyIron3dEulerTransform; -use fparkan_world::transform_state; +use fparkan_world::{transform_state, TransformState}; /// One shield pulse triggered on the side that intercepted a projectile. pub struct ShieldImpact { @@ -18,41 +18,33 @@ pub struct ShieldImpact { pub world_from_effect: [[f32; 4]; 4], } -pub(super) struct ShieldImpacts { - pulses: Vec<ShieldImpact>, - local_spheres: Vec<Option<([f32; 3], f32)>>, -} - -/// Enables collection of transient shield pulses for the presentation layer. -pub fn collect_shield_impacts(engine: &mut Engine) { - let local_spheres = engine.loaded.as_ref().map_or_else(Vec::new, |mission| { - let assets = &mission.mission_assets; - mission - .object_drafts - .iter() - .enumerate() - .map(|(index, draft)| { - if draft.unit_dat_header_flags != Some(crate::boarding::WARBOT_CLASS) { - return None; - } - let controls = assets.object_component_controls.get(index)?; - if !controls +pub(super) fn prepare_shield_spheres( + assets: &MissionAssets, + drafts: &[MissionObjectDraft], +) -> Vec<Option<([f32; 3], f32)>> { + drafts + .iter() + .enumerate() + .map(|(index, draft)| { + let controls = assets.object_component_controls.get(index)?; + if draft.unit_dat_header_flags != Some(crate::boarding::WARBOT_CLASS) + || !controls .iter() .flatten() .flat_map(|control| &control.typed_records) .any(|record| record.tag == 9) - { - return None; - } - let poses = WeaponPoses::new(draft, assets, index); - enclosing_sphere(&poses.initial_model_spheres(assets, index)) - }) - .collect() - }); - engine.shield_impacts = Some(ShieldImpacts { - pulses: Vec::new(), - local_spheres, - }); + { + return None; + } + let poses = WeaponPoses::new(draft, assets, index); + enclosing_sphere(&poses.initial_model_spheres(assets, index)) + }) + .collect() +} + +/// Enables collection of transient shield pulses for the presentation layer. +pub fn collect_shield_impacts(engine: &mut Engine) { + engine.shield_impacts = Some(Vec::new()); } /// Drains shield pulses accumulated since the last presentation update. @@ -60,7 +52,7 @@ pub fn take_shield_impacts(engine: &mut Engine) -> Vec<ShieldImpact> { engine .shield_impacts .as_mut() - .map_or_else(Vec::new, |impacts| std::mem::take(&mut impacts.pulses)) + .map_or_else(Vec::new, std::mem::take) } /// Finds authored shield effects for placed units and prepared spawn slots. @@ -119,21 +111,22 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) - let Ok(pose) = transform_state(&engine.world, hit.handle) else { return volume; }; - let Some((center, radius)) = unit_sphere(&mission.mission_assets, index, pose) else { + let Some((center, radius)) = mission + .shield_spheres + .get(index) + .copied() + .flatten() + .and_then(|sphere| world_sphere(sphere, pose)) + .or_else(|| unit_sphere(&mission.mission_assets, index, pose)) + else { return volume; }; - let Some(basis) = (LegacyIron3dEulerTransform { - translation: [0.0; 3], - orientation_radians: pose.orientation.map(f32::from_bits), - }) - .try_row_major() else { - return volume; + let shield_pose = TransformState { + position: center.map(f32::to_bits), + scale: [1.0_f32.to_bits(); 3], + ..pose }; - let offset = std::array::from_fn::<_, 3, _>(|axis| hit.position[axis] - center[axis]); - let local = std::array::from_fn(|axis| { - offset[0] * basis[axis] + offset[1] * basis[4 + axis] + offset[2] * basis[8 + axis] - }); - let Some(sector) = contact_sector(local) else { + let Some(sector) = hull_local_point(shield_pose, hit.position).and_then(contact_sector) else { return volume; }; let Ok(multiplier) = control_device_budget_coefficient(strength, sector) else { @@ -164,31 +157,32 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) - deltas[sector] = (f32::from_bits(deltas[sector]) + (updated - fraction)).to_bits(); if let Some(impacts) = engine.shield_impacts.as_mut() { if let Some(source) = shield_effect_source(&shield.descriptor.fixed_words) { - let display_sphere = || { - let (center, radius) = *impacts.local_spheres.get(index)?.as_ref()?; - let scale = pose.scale.map(f32::from_bits); - let transform = LegacyIron3dEulerTransform { - translation: pose.position.map(f32::from_bits), - orientation_radians: pose.orientation.map(f32::from_bits), - }; - Some(( - transform.try_transform_scaled_point(center, scale)?, - radius * scale.into_iter().map(f32::abs).fold(0.0, f32::max), - )) - }; - let (display_center, display_radius) = display_sphere().unwrap_or((center, radius)); - // Keep the direction of the actual contact on the collision - // sphere until the field has its own collision surface. - impacts.pulses.push(ShieldImpact { + let offset = std::array::from_fn(|axis| hit.position[axis] - center[axis]); + impacts.push(ShieldImpact { object_index: index, source, - world_from_effect: effect_transform(display_center, offset, display_radius), + world_from_effect: effect_transform(center, offset, radius), }); } } remaining } +fn world_sphere( + (center, radius): ([f32; 3], f32), + pose: TransformState, +) -> Option<([f32; 3], f32)> { + let scale = pose.scale.map(f32::from_bits); + let transform = LegacyIron3dEulerTransform { + translation: pose.position.map(f32::from_bits), + orientation_radians: pose.orientation.map(f32::from_bits), + }; + Some(( + transform.try_transform_scaled_point(center, scale)?, + radius * scale.into_iter().map(f32::abs).fold(0.0, f32::max), + )) +} + fn enclosing_sphere(parts: &[([f32; 3], f32)]) -> Option<([f32; 3], f32)> { // AniMesh+0x9510 weights model centers by the authored radii. A second // pass then puts even the most distant model sphere inside the result. @@ -301,7 +295,7 @@ mod tests { ) .unwrap(); let mission = engine.loaded.as_ref().unwrap(); - let (contact, capacity, health) = mission + let (contact, sector, capacity, health) = mission .control_device_tables .iter() .find_map(|table| { @@ -310,35 +304,48 @@ mod tests { }; let shield = devices.iter().rfind(|device| device.factory_tag == 9)?; let strength = devices.iter().rfind(|device| device.factory_tag == 21)?; + let pose = transform_state(&engine.world, table.handle).ok()?; + let index = table.original_id?.0 as usize; + let (center, _) = world_sphere(mission.shield_spheres[index]?, pose)?; + let (hull_center, _) = unit_sphere(&mission.mission_assets, index, pose)?; + let position = std::array::from_fn(|axis| (center[axis] + hull_center[axis]) * 0.5); + let at = |origin: [f32; 3]| TransformState { + position: origin.map(f32::to_bits), + scale: [1.0_f32.to_bits(); 3], + ..pose + }; + let sector = hull_local_point(at(center), position).and_then(contact_sector)?; + let hull_sector = + hull_local_point(at(hull_center), position).and_then(contact_sector)?; + if sector == hull_sector { + return None; + } let capacity = control_device_selector_zero(shield).ok()? - * f32::from_bits(shield.class9_values_bits?[0]) - * control_device_budget_coefficient(strength, 0).ok()?; + * f32::from_bits(shield.class9_values_bits?[sector]) + * control_device_budget_coefficient(strength, sector).ok()?; if capacity <= 0.0 { return None; } - let pose = transform_state(&engine.world, table.handle).ok()?; - let (center, radius) = - unit_sphere(&mission.mission_assets, table.original_id?.0 as usize, pose)?; - let basis = LegacyIron3dEulerTransform { - translation: center, - orientation_radians: pose.orientation.map(f32::from_bits), - }; - let position = basis.try_transform_scaled_point([0.0, radius, 0.0], [1.0; 3])?; let hit = ProjectileContact { handle: table.handle, fraction: 0.5, position, node: None, }; - Some((hit, capacity, table.node_provider_amount_bits?)) + Some((hit, sector, capacity, table.node_provider_amount_bits?)) }) - .expect("stock shield with a charged front sector"); + .expect("stock shield whose assembled center changes the selected sector"); + // Headless combat and rendering must spend charge from the same side. + assert_eq!( + apply_projectile_impact(&mut engine, contact, capacity * 0.25).unwrap(), + ProjectileImpact::ShieldAbsorbed + ); collect_shield_impacts(&mut engine); - let (center, radius) = engine.shield_impacts.as_ref().unwrap().local_spheres[0] + let (center, radius) = engine.loaded.as_ref().unwrap().shield_spheres[0] .expect("stock AutoDemo warbot enclosure"); assert!(center[2] > 0.0 && radius > 6.0); assert_eq!( - apply_projectile_impact(&mut engine, contact, capacity * 0.5).unwrap(), + apply_projectile_impact(&mut engine, contact, capacity * 0.25).unwrap(), ProjectileImpact::ShieldAbsorbed ); let impacts = take_shield_impacts(&mut engine); @@ -361,6 +368,20 @@ mod tests { .find(|table| table.handle == contact.handle) .unwrap(); assert_eq!(table.node_provider_amount_bits, Some(health)); + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + panic!("missing shield devices"); + }; + let shield = devices + .iter() + .rfind(|device| device.factory_tag == 9) + .unwrap(); + let charges = shield.class9_values_bits.unwrap().map(f32::from_bits); + let mut expected = [1.0; 6]; + expected[sector] = 0.5; + assert!(charges + .iter() + .zip(expected) + .all(|(a, b)| (a - b).abs() < 1e-6)); assert!((absorb(&mut engine, contact, capacity * 0.5 + 20.0) - 20.0).abs() < 0.001); assert_eq!(take_shield_impacts(&mut engine).len(), 1); assert_eq!(absorb(&mut engine, contact, capacity * 0.5), capacity * 0.5); |
