diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 18:57:36 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 18:57:36 +0300 |
| commit | 1fd51e5bbcfb4d5590fbc1ded8308fc40f6158f4 (patch) | |
| tree | 9ef8276c399e238a2a692862de09f54bf5dcee65 /crates/fparkan-runtime/src/boarding/bunker.rs | |
| parent | 077bead07a76908ac4e0e2b84de3940256cf6fd1 (diff) | |
| download | fparkan-1fd51e5bbcfb4d5590fbc1ded8308fc40f6158f4.tar.xz fparkan-1fd51e5bbcfb4d5590fbc1ded8308fc40f6158f4.zip | |
feat(ui): enable manual control from the bunker list
Use the active Control camera record and its separate CPT origin and direction for the fixed turret. The original hand control opens the cockpit, its two weapon slots and aim tracks while leaving the operator at the command post.
Verified from the stock Two Fortresses start against the GOG cockpit with Vulkan validation. The licensed tests cover the native camera pose, mounted weapon selection and stationary bunker and operator.
Diffstat (limited to 'crates/fparkan-runtime/src/boarding/bunker.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding/bunker.rs | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/boarding/bunker.rs b/crates/fparkan-runtime/src/boarding/bunker.rs new file mode 100644 index 00000000..a69a2d93 --- /dev/null +++ b/crates/fparkan-runtime/src/boarding/bunker.rs @@ -0,0 +1,265 @@ +//! Manual operation of a fixed gun mount from the clan's command post. + +use super::{has_working_pilot_control, start_player_control}; +use crate::{ + hero_look::{turret_pitch, HeroLook}, + strategic_buildings::BuildingSection, + strategic_view::command_post, + Engine, MissionControlDeviceTableStatus, PlayerMotion, RemoteWarbotControl, StartingCockpit, + TurretAim, +}; +use fparkan_assets::{ + turret_elevation_row, MissionAssets, PreparedComponentControl, PreparedControlPoint, + PreparedControlTypedRecord, +}; +use fparkan_world::{identity_metadata, ObjectHandle}; +use std::f32::consts::{PI, TAU}; + +/// The camera's projection record and its separate CPT origin and direction. +pub fn camera_binding( + control: &PreparedComponentControl, +) -> Option<(&PreparedControlTypedRecord, [&PreparedControlPoint; 2])> { + let record = control + .typed_records + .iter() + .find(|record| record.tag == 4)?; + let row = control + .timeline_rows + .get(*record.values.first()? as usize)?; + let points = &control.point_list.as_ref()?.points; + let origin = points.get(row.raw_parameters[0] as usize)?; + let direction = points.get(row.raw_parameters[1] as usize)?; + let usable = [origin, direction].into_iter().all(|point| { + usize::try_from(point.raw_signed_field_04).is_ok_and(|node| node < control.model_node_count) + }); + (usable && crate::has_turret_aim(control) && turret_elevation_row(control).is_some()) + .then_some((record, [origin, direction])) +} + +fn camera_component(assets: &MissionAssets, object: usize) -> Option<usize> { + assets + .object_component_controls + .get(object)? + .iter() + .position(|control| control.as_ref().and_then(camera_binding).is_some()) +} + +/// Prepare only the weapons mounted on the manually operated turret. +pub fn prepared_weapons( + engine: &Engine, +) -> impl Iterator<Item = (usize, crate::PlayerWeapons)> + '_ { + engine.loaded.iter().flat_map(|mission| { + mission + .object_drafts + .iter() + .enumerate() + .filter_map(|(object, draft)| { + if !BuildingSection::Bunkers.matches(draft.unit_dat_header_flags?) { + return None; + } + let turret = camera_component(&mission.mission_assets, object)?; + let controls = mission + .mission_assets + .object_component_controls + .get(object)?; + let tree = fparkan_prototype::unit_component_tree(&draft.unit_components).ok()?; + let weapons = crate::player_weapon::prepare_component_weapons( + tree.iter().enumerate().filter_map(|(component, node)| { + (component == turret || node.parent_index == Some(turret)) + .then(|| Some((component, controls.get(component)?.as_ref()?))) + .flatten() + }), + ); + (!weapons.weapons().is_empty()).then_some((object, weapons)) + }) + }) +} + +/// Resolves the first active tag-4 device of an operated allied bunker. +pub fn cockpit( + engine: &Engine, + operator: StartingCockpit, + target: ObjectHandle, +) -> Option<StartingCockpit> { + let mission = engine.loaded.as_ref()?; + if mission.boarding.is_some() || command_post(engine, operator).is_none() { + return None; + } + let metadata = identity_metadata(&engine.world, target).ok()?; + if metadata.owner_id? + != identity_metadata(&engine.world, operator.handle) + .ok()? + .owner_id? + { + return None; + } + let object_index = metadata.original_id?.0 as usize; + let draft = mission.object_drafts.get(object_index)?; + if !BuildingSection::Bunkers.matches(draft.unit_dat_header_flags?) { + return None; + } + let table = mission + .control_device_tables + .iter() + .find(|table| table.handle == target)?; + if table.node_provider_depletion_marked || !has_working_pilot_control(table) { + return None; + } + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + return None; + }; + let device = devices.iter().find(|device| device.descriptor.tag == 4)?; + let component_index = device.factory_provenance.unit_component_index; + let control = mission.mission_assets.object_component_controls[object_index] + .get(component_index)? + .as_ref()?; + let (_, [origin, _]) = camera_binding(control)?; + Some(StartingCockpit { + handle: target, + object_index, + component_index, + node_index: usize::try_from(origin.raw_signed_field_04).ok()?, + node_point: origin.position, + }) +} + +/// Takes over a fixed turret, leaving the operator at the command post. +pub fn enter( + engine: &mut Engine, + operator: StartingCockpit, + target: ObjectHandle, +) -> Option<StartingCockpit> { + let camera = cockpit(engine, operator, target)?; + let look = HeroLook::load_fixed_turret(engine.services.vfs.as_ref()?.as_ref()); + let row = turret_elevation_row(component_control(engine, camera)?)?; + // The manual elevation starts at the input channel's center. + let aim = TurretAim { + azimuth: 0.0, + elevation: turret_pitch(row, 0.0, 0.5 - row.initial_blend)?, + }; + start_player_control( + engine, + operator, + camera, + Some(RemoteWarbotControl::Manual), + Some(aim), + ); + let mission = engine.loaded.as_mut()?; + mission.player_pitch_bits = aim.elevation.to_bits(); + mission.hero_look = look; + Some(camera) +} + +/// Current manual aim of a fixed turret, shared by the camera, mesh and muzzles. +pub fn aim(engine: &Engine, camera: StartingCockpit) -> Option<TurretAim> { + let boarding = engine.loaded.as_ref()?.boarding?; + (boarding.cockpit == camera) + .then_some(boarding.turret) + .flatten() +} + +fn component_control( + engine: &Engine, + camera: StartingCockpit, +) -> Option<&PreparedComponentControl> { + engine + .loaded + .as_ref()? + .mission_assets + .object_component_controls + .get(camera.object_index)? + .get(camera.component_index)? + .as_ref() +} + +pub(crate) fn advance( + engine: &Engine, + camera: StartingCockpit, + input: PlayerMotion, + mut look: HeroLook, + aim: TurretAim, +) -> (HeroLook, TurretAim) { + let [delta, blend] = look.advance( + [input.mouse_x, input.mouse_y], + f32::INFINITY, + input.look_around, + input.center_view, + ); + let row = component_control(engine, camera).and_then(turret_elevation_row); + let elevation = row + .and_then(|row| turret_pitch(row, aim.elevation, blend)) + .unwrap_or(aim.elevation); + ( + look, + TurretAim { + azimuth: (aim.azimuth + delta + PI).rem_euclid(TAU) - PI, + elevation, + }, + ) +} + +#[cfg(test)] +#[test] +#[ignore = "requires licensed corpus"] +fn stock_bunker_aims_its_mounted_weapons_without_moving_the_operator() { + use crate::{ + disembark_warbot, frame, frame_with_player_motion, plan_player_motion, starting_cockpit, + }; + use fparkan_world::transform_state; + + let mut engine = super::remote_control::tests::stock_fortresses(); + frame(&mut engine, 1).unwrap(); + let hero = starting_cockpit(&engine).unwrap(); + let target = match command_post(&engine, hero).unwrap() { + crate::strategic_view::CommandPost::Bunker(handle) => handle, + _ => panic!("the hero starts at the bunker"), + }; + let initial_hero = transform_state(&engine.world, hero.handle).unwrap(); + let initial_bunker = transform_state(&engine.world, target).unwrap(); + let camera = enter(&mut engine, hero, target).unwrap(); + assert_eq!(camera.object_index, 0); + assert_eq!(camera.component_index, 3); + assert_eq!(camera.node_index, 5); + let (_, weapons) = prepared_weapons(&engine) + .find(|(object, _)| *object == camera.object_index) + .unwrap(); + assert_eq!( + weapons + .weapons() + .iter() + .map(|weapon| weapon.component_index) + .collect::<Vec<_>>(), + [4, 5] + ); + + let initial_aim = aim(&engine, camera).unwrap(); + let step = plan_player_motion( + &engine, + camera, + PlayerMotion { + movement: [1.0, 1.0], + mouse_x: 35.0, + mouse_y: -35.0, + delta_seconds: 0.02, + ..PlayerMotion::default() + }, + ) + .unwrap() + .unwrap(); + frame_with_player_motion(&mut engine, 21, camera, step, false).unwrap(); + let aim = aim(&engine, camera).unwrap(); + assert!(aim.azimuth.abs() > 0.05 && (aim.elevation - initial_aim.elevation).abs() > 0.05); + assert_eq!( + transform_state(&engine.world, hero.handle).unwrap(), + initial_hero + ); + assert_eq!( + transform_state(&engine.world, target).unwrap(), + initial_bunker + ); + assert_eq!(disembark_warbot(&mut engine).unwrap(), Some(hero)); + assert_eq!( + transform_state(&engine.world, hero.handle).unwrap(), + initial_hero + ); +} |
