diff options
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/capture_barrier.rs | 215 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 5 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mission_events.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile.rs | 23 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile_parts.rs | 17 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 4 |
6 files changed, 257 insertions, 13 deletions
diff --git a/crates/fparkan-runtime/src/capture_barrier.rs b/crates/fparkan-runtime/src/capture_barrier.rs new file mode 100644 index 00000000..db63bcaa --- /dev/null +++ b/crates/fparkan-runtime/src/capture_barrier.rs @@ -0,0 +1,215 @@ +//! The authored sliding field around a building communicator. + +use crate::projectile_parts::{model_triangle_hit, root_model}; +use crate::{Engine, LoadedMissionState, MissionAssets, ProjectileNode, TransformState}; +use fparkan_animation::AnimationTime; +use fparkan_assets::{PreparedComponentControl, PreparedControlTimelineRow}; +use fparkan_msh::node38_sampled_hierarchy_at_times; +use fparkan_world::{transform_state, ObjectHandle, World}; + +pub(super) struct CaptureBarrier { + building: ObjectHandle, + operator: ObjectHandle, + previous: Option<(u32, [u32; 2])>, + blend: f32, + opening: bool, +} + +fn rows(control: &PreparedComponentControl) -> impl Iterator<Item = &PreparedControlTimelineRow> { + control + .typed_records + .iter() + .filter(|record| record.tag == 13) + .flat_map(|record| &record.values) + .filter_map(|&row| control.timeline_rows.get(row as usize)) + .filter(|row| row.flags & 4 == 0) +} + +fn control(assets: &MissionAssets, index: usize) -> Option<&PreparedComponentControl> { + assets + .object_component_controls + .get(index)? + .first()? + .as_ref() +} + +impl CaptureBarrier { + pub(super) fn start( + assets: &MissionAssets, + building: ObjectHandle, + index: usize, + operator: ObjectHandle, + ) -> Option<Self> { + rows(control(assets, index)?).next()?; + Some(Self { + building, + operator, + previous: None, + blend: 0.0, + opening: false, + }) + } +} + +pub(super) fn advance(mission: &mut LoadedMissionState, world: &World, now: u32) { + let Some(barrier) = &mut mission.capture_barrier else { + return; + }; + let Ok(pose) = transform_state(world, barrier.operator) else { + mission.capture_barrier = None; + return; + }; + let [x, y, _] = pose.position; + let Some((last, previous_xy)) = barrier.previous.replace((now, [x, y])) else { + return; + }; + // shortcut: keep the field down while the operator stays at the station. + // Opening on resumed movement stands in for leaving the original factory UI. + barrier.opening |= previous_xy != [x, y]; + let direction = if barrier.opening { -1.0 } else { 1.0 }; + let dt = now.wrapping_sub(last) as f32 / 1_000.0; + barrier.blend = (barrier.blend + direction * dt * crate::building_door::DOOR_BLEND_PER_SECOND) + .clamp(0.0, 1.0); + if barrier.opening && barrier.blend == 0.0 { + mission.capture_barrier = None; + } +} + +fn blend(mission: &LoadedMissionState, building: ObjectHandle) -> f32 { + mission + .capture_barrier + .as_ref() + .filter(|barrier| barrier.building == building) + .map_or(0.0, |barrier| barrier.blend) +} + +fn frame(row: &PreparedControlTimelineRow, blend: f32) -> f32 { + let blend = row.initial_blend + (1.0 - row.initial_blend) * blend; + let blend = if row.flags & 2 == 0 { + blend + } else { + 1.0 - blend + }; + row.frame_a + (row.frame_b - row.frame_a) * blend +} + +/// Applies the capture field's animated node times to the rendered building. +pub fn sample_capture_barrier( + engine: &Engine, + building: ObjectHandle, + control: &PreparedComponentControl, + times: &mut [Option<f32>], +) { + let Some(mission) = engine.loaded.as_ref() else { + return; + }; + let blend = blend(mission, building); + if blend > 0.0 { + for row in rows(control) { + if let Some(time) = times.get_mut(row.node_index) { + *time = Some(frame(row, blend)); + } + } + } +} + +pub(super) fn projectile_contact( + mission: &LoadedMissionState, + index: usize, + building: ObjectHandle, + state: TransformState, + segment: [[f32; 3]; 2], +) -> Option<(f32, ProjectileNode)> { + let assets = &mission.mission_assets; + let fallback = || { + crate::projectile_parts::root_triangle_hit(assets, index, state, segment, |_| true) + .map(|(fraction, node)| (fraction, ProjectileNode::Building(node))) + }; + let Some(control) = control(assets, index).filter(|control| rows(control).next().is_some()) + else { + return fallback(); + }; + let model = &root_model(assets, index)?.validated; + let mut times = vec![None; model.node_count]; + let blend = blend(mission, building); + for row in rows(control) { + if let Some(time) = times.get_mut(row.node_index) { + *time = Some(AnimationTime(frame(row, blend))); + } + } + let Some(poses) = node38_sampled_hierarchy_at_times(model, ×) else { + return fallback(); + }; + let (fraction, node) = model_triangle_hit(model, poses, state, segment, |_| true)?; + let node = if times[node as usize].is_some() { + ProjectileNode::CaptureBarrier + } else { + ProjectileNode::Building(node) + }; + Some((fraction, node)) +} + +#[cfg(test)] +mod tests { + use crate::{ + apply_projectile_contact, capture_building, create, frame, load_gameplay_mission, + projectile_unit_hit, starting_cockpit, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, ProjectileNode, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{set_transform, transform_state, OriginalObjectId}; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_factory_field_stops_shots_only_when_lowered() { + let root = std::path::PathBuf::from( + std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed root"), + ); + let vfs = Arc::new(DirectoryVfs::new(root)); + let mut engine = create(EngineServices::new(vfs)).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 hero = starting_cockpit(&engine).unwrap(); + let mut pose = transform_state(&engine.world, hero.handle).unwrap(); + let center = [391.2424_f32, 740.083, 140.74289]; + pose.position = center.map(f32::to_bits); + set_transform(&mut engine.world, hero.handle, pose).unwrap(); + let forward = [[center[0], center[1], 141.6], [center[0], 746.0, 141.6]]; + let backward = [forward[1], forward[0]]; + for segment in [forward, backward] { + assert!(projectile_unit_hit(&engine, hero.handle, segment).is_none()); + } + assert_eq!( + capture_building(&mut engine, hero).unwrap(), + Some(OriginalObjectId(0)) + ); + frame(&mut engine, 1).unwrap(); + frame(&mut engine, 501).unwrap(); + for segment in [forward, backward] { + let hit = projectile_unit_hit(&engine, hero.handle, segment).expect("field contact"); + assert_eq!(hit.node, Some(ProjectileNode::CaptureBarrier)); + assert_eq!( + apply_projectile_contact(&mut engine, hit, f32::MAX).unwrap(), + None + ); + } + pose.position[1] = (center[1] + 0.2).to_bits(); + set_transform(&mut engine.world, hero.handle, pose).unwrap(); + frame(&mut engine, 1001).unwrap(); + for segment in [forward, backward] { + assert!(projectile_unit_hit(&engine, hero.handle, segment).is_none()); + } + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 701a4a2e..0dc5e4a5 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -49,6 +49,7 @@ mod boarding; mod builder_supply; mod building_door; mod building_passage; +mod capture_barrier; mod clan_ai_events; mod clan_area_strength; mod clan_build_limits; @@ -144,6 +145,7 @@ pub use boarding::{ board_friendly_warbot, disembark_warbot, player_vehicle, select_boarding_target, }; pub use building_door::{has_proximity_doors, sample_capture_doors, sample_proximity_doors}; +pub use capture_barrier::sample_capture_barrier; pub use fparkan_assets::MissionAssets; pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, BuildingMeshNode}; pub use mining::deploy_mine; @@ -1447,6 +1449,7 @@ struct LoadedMissionState { script_init_states: Vec<MissionScriptInitState>, script_varset_states: Vec<MissionScriptVarSetState>, mission_events: MissionEvents, + capture_barrier: Option<capture_barrier::CaptureBarrier>, clan_ai_events: clan_ai_events::ClanAiEvents, mine_sites: Vec<mining::MineSite>, construction: construction::Construction, @@ -2802,6 +2805,7 @@ node_provider_distribution_suspended: false, script_init_states: loaded_scripts.init_states, script_varset_states: loaded_scripts.varset_states, mission_events, + capture_barrier: None, clan_ai_events, mine_sites, construction, @@ -2904,6 +2908,7 @@ pub fn step_headless( engine.behavior_rng_state = crt; } if let Some(mission) = &mut engine.loaded { + capture_barrier::advance(mission, &engine.world, queue_time_ms); control_resources::tick(mission, &engine.world, queue_time_ms); } let (autodemo_combat_events, autodemo_combat_unavailable) = diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 5108a058..2c817314 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -566,6 +566,12 @@ fn capture_building_at_communicator( player_owner, class, )?; + mission.capture_barrier = crate::capture_barrier::CaptureBarrier::start( + &mission.mission_assets, + handle, + id.0 as usize, + cockpit.handle, + ); Ok(Some((id, MissionNotice::BuildingCaptured { neutral }))) } else { Ok(None) diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 48018933..fc9f6965 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -1,6 +1,6 @@ use crate::{ apply_loaded_mission_control_node_delta, - projectile_parts::{chassis_part_contact, root_model, root_triangle_hit}, + projectile_parts::{chassis_part_contact, root_model}, ControlResourceAccessError, Engine, MissionAssets, MissionControlNodeProviderTableStatus, TerrainWorld, CONTROL_CLASS_SOURCE_BULL, UNIT_DAT_CONTROL_CLASS_HIGH_BIT, }; @@ -189,6 +189,8 @@ pub enum ProjectileNode { Building(u32), /// A node of the warbot's chassis model. Chassis(u32), + /// An indestructible communicator field. + CaptureBarrier, } impl ProjectileContact { @@ -229,9 +231,10 @@ pub fn projectile_unit_hit( let (center, radius) = unit_sphere(&mission.mission_assets, index, state)?; let sphere_fraction = segment_sphere_fraction(segment, center, radius)?; let (fraction, node) = if building { - let (fraction, node) = - root_triangle_hit(&mission.mission_assets, index, state, segment, |_| true)?; - (fraction, Some(ProjectileNode::Building(node))) + 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, @@ -279,6 +282,11 @@ pub fn apply_projectile_contact( contact: ProjectileContact, volume: f32, ) -> Result<Option<(f32, bool)>, ControlResourceAccessError> { + let key = match contact.node { + Some(ProjectileNode::CaptureBarrier) => return Ok(None), + None | Some(ProjectileNode::Chassis(0)) => None, + Some(ProjectileNode::Chassis(key) | ProjectileNode::Building(key)) => Some(key), + }; let volume = super::projectile_shield::absorb(engine, contact, volume); let table = engine.loaded.as_ref().and_then(|mission| { mission @@ -290,11 +298,8 @@ pub fn apply_projectile_contact( volume, table.and_then(|table| table.projectile_resistance_bits), ); - let key = match contact.node { - None | Some(ProjectileNode::Chassis(0)) => { - return apply_projectile_hit(engine, contact.handle, volume); - } - Some(ProjectileNode::Chassis(key) | ProjectileNode::Building(key)) => key, + 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 { diff --git a/crates/fparkan-runtime/src/projectile_parts.rs b/crates/fparkan-runtime/src/projectile_parts.rs index a9beb27f..67582ef9 100644 --- a/crates/fparkan-runtime/src/projectile_parts.rs +++ b/crates/fparkan-runtime/src/projectile_parts.rs @@ -5,9 +5,10 @@ use crate::{ Engine, LoadedMissionState, MissionAssets, MissionControlNodeProviderTableStatus, ObjectHandle, TransformState, }; +use fparkan_animation::NodePoseBuffer; use fparkan_assets::PreparedModel; use fparkan_msh::{ - node38_fallback_hierarchy, query_slot_segment, selected_slot, Group, Lod, NodeId, + node38_fallback_hierarchy, query_slot_segment, selected_slot, Group, Lod, ModelAsset, NodeId, SegmentQueryMasks, }; use fparkan_render::LegacyIron3dEulerTransform; @@ -105,6 +106,18 @@ pub(super) fn root_triangle_hit( accept_node: impl Fn(u32) -> bool, ) -> Option<(f32, u32)> { let model = &root_model(assets, index)?.validated; + // shortcut: use root MSH fallback poses until combat shares live component transforms. + let poses = node38_fallback_hierarchy(model)?; + model_triangle_hit(model, poses, state, segment, accept_node) +} + +pub(super) fn model_triangle_hit( + model: &ModelAsset, + poses: NodePoseBuffer, + state: TransformState, + segment: [[f32; 3]; 2], + accept_node: impl Fn(u32) -> bool, +) -> Option<(f32, u32)> { let root = LegacyIron3dEulerTransform { translation: state.position.map(f32::from_bits), orientation_radians: state.orientation.map(f32::from_bits), @@ -118,8 +131,6 @@ pub(super) fn root_triangle_hit( local[i] = (root[i] * x + root[i + 4] * y + root[i + 8] * z) / scale[i]; } } - // shortcut: use root MSH fallback poses until combat shares live component transforms. - let poses = node38_fallback_hierarchy(model)?; let squared_length: f32 = (0..3) .map(|i| (segment[1][i] - segment[0][i]).powi(2)) .sum(); diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index c7d807cc..03bf5c8d 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -460,7 +460,9 @@ impl AutoDemoCombat { travelled = Some(clip_segment(segment, fraction)); Ok(ground.is_some() || contact.is_some()) }); - if let Some(hit) = contact { + if let Some(hit) = + contact.filter(|hit| hit.node != Some(crate::ProjectileNode::CaptureBarrier)) + { let data = &self.projectiles[flight.projectile]; // Capture the target before applying damage, which may remove it. let target = CombatObject::read(&engine.world, hit.handle); |
