diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 21 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/boarding/bunker.rs | 265 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/boarding/remote_control.rs | 15 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/hero_look.rs | 14 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_motion.rs | 33 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 14 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_buildings.rs | 3 |
8 files changed, 350 insertions, 16 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index 95acc7e0..bbcc4615 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -11,6 +11,7 @@ use fparkan_world::{ }; pub(super) const WARBOT_CLASS: u32 = 0x0100_8000; +pub mod bunker; mod remote_control; pub use remote_control::{ can_control_warbot_remotely, control_warbot_remotely, remote_automatic_weapons, @@ -38,6 +39,7 @@ pub(super) struct Boarding { pub(super) cockpit: StartingCockpit, pub(super) hero: StartingCockpit, pub(super) remote: Option<RemoteWarbotControl>, + pub(super) turret: Option<crate::TurretAim>, pub(super) can_fly: bool, pub(super) ground: Option<crate::ground_support::GroundSupport>, pub(super) on_building_floor: bool, @@ -46,7 +48,7 @@ pub(super) struct Boarding { impl Boarding { pub(super) fn controls_chassis(self) -> bool { - self.remote != Some(RemoteWarbotControl::Autopilot) + self.turret.is_none() && self.remote != Some(RemoteWarbotControl::Autopilot) } pub(super) fn controls_weapons(self) -> bool { @@ -183,24 +185,27 @@ pub fn board_friendly_warbot( ..hero_pose }, )?; - start_warbot_control(engine, hero, cockpit, None); + start_player_control(engine, hero, cockpit, None, None); Ok(Some(cockpit)) } -fn start_warbot_control( +fn start_player_control( engine: &mut Engine, hero: StartingCockpit, cockpit: StartingCockpit, remote: Option<RemoteWarbotControl>, + turret: Option<crate::TurretAim>, ) { - let can_fly = crate::player_motion::warbot_can_fly(engine, cockpit.object_index); + let can_fly = + turret.is_none() && crate::player_motion::warbot_can_fly(engine, cockpit.object_index); if let Some(mission) = &mut engine.loaded { mission.boarding = Some(Boarding { cockpit, hero, remote, + turret, can_fly, - ground: (!can_fly) + ground: (turret.is_none() && !can_fly) .then(|| { crate::ground_support::GroundSupport::prepare( &mission.mission_assets, @@ -314,6 +319,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, cockpit: vehicle, hero, remote, + turret, can_fly, on_building_floor, .. @@ -370,6 +376,11 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, mission.boarding = None; mission.player_pitch_bits = 0; mission.hero_walk_velocity = [0.0; 2]; + if turret.is_some() { + if let Some(vfs) = &engine.services.vfs { + mission.hero_look = crate::hero_look::HeroLook::load(vfs.as_ref()); + } + } mission.hero_look.reset(); Ok(Some(hero)) } 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 + ); +} diff --git a/crates/fparkan-runtime/src/boarding/remote_control.rs b/crates/fparkan-runtime/src/boarding/remote_control.rs index ee030e2f..1a881a5d 100644 --- a/crates/fparkan-runtime/src/boarding/remote_control.rs +++ b/crates/fparkan-runtime/src/boarding/remote_control.rs @@ -1,6 +1,6 @@ //! The command-post link to a friendly warbot's cockpit. -use super::{has_working_pilot_control, start_warbot_control}; +use super::{has_working_pilot_control, start_player_control}; use crate::{ component_cockpit, strategic_roster::warbot_rows, @@ -76,14 +76,19 @@ pub fn control_warbot_remotely( { patrol.release_orders_for_manual_control(target); } - start_warbot_control(engine, hero, cockpit, Some(mode)); + start_player_control(engine, hero, cockpit, Some(mode), None); Some(cockpit) } /// The active control mode of a warbot linked to the command post. #[must_use] pub fn remote_warbot_control(engine: &Engine) -> Option<RemoteWarbotControl> { - engine.loaded.as_ref()?.boarding?.remote + let boarding = engine.loaded.as_ref()?.boarding?; + boarding + .turret + .is_none() + .then_some(boarding.remote) + .flatten() } /// The active autonomous weapons' ammunition, charge and firing phases. @@ -100,7 +105,7 @@ pub fn remote_automatic_weapons(engine: &Engine) -> Option<&PlayerWeapons> { } #[cfg(test)] -mod tests { +pub(super) mod tests { use super::*; use crate::{ create, disembark_warbot, frame, frame_with_player_motion, load_gameplay_mission, @@ -111,7 +116,7 @@ mod tests { use fparkan_world::transform_state; use std::sync::Arc; - fn stock_fortresses() -> Engine { + pub(in crate::boarding) fn stock_fortresses() -> Engine { 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( diff --git a/crates/fparkan-runtime/src/hero_look.rs b/crates/fparkan-runtime/src/hero_look.rs index d671f2b7..2e923d20 100644 --- a/crates/fparkan-runtime/src/hero_look.rs +++ b/crates/fparkan-runtime/src/hero_look.rs @@ -16,11 +16,19 @@ pub(super) struct HeroLook { impl HeroLook { pub(super) fn load(vfs: &dyn Vfs) -> Self { + Self::load_profile(vfs, b"hero.tbl", "CICLS_UNKNOWN", "MCMD_ANGLE_Z") + } + + pub(super) fn load_fixed_turret(vfs: &dyn Vfs) -> Self { + Self::load_profile(vfs, b"m1.tbl", "CICLS_TURRET", "MCMD_ANGLE_X") + } + + fn load_profile(vfs: &dyn Vfs, table_name: &[u8], yaw_class: &str, yaw_command: &str) -> Self { let read = |name: &[u8]| { let path = normalize_relative(name, PathPolicy::StrictLegacy).ok()?; vfs.read(&path).ok() }; - let table_bytes = read(b"hero.tbl"); + let table_bytes = read(table_name); let settings_bytes = read(b"Iron_3D.ini"); let table = String::from_utf8_lossy(table_bytes.as_deref().unwrap_or_default()); let settings = String::from_utf8_lossy(settings_bytes.as_deref().unwrap_or_default()); @@ -29,8 +37,8 @@ impl HeroLook { &table, "SCAN_NULL", "SCAN_MOUSE_X", - "CICLS_UNKNOWN", - "MCMD_ANGLE_Z", + yaw_class, + yaw_command, "MAN_WRAP", ) .unwrap_or(0.15), diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index ad030b00..2cf1f62e 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -186,6 +186,7 @@ pub use wheel_animation::WheelAnimation; pub use aim_convergence::{projectile_sight_target, turret_sight_points}; pub use autodemo::{autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView}; +pub use boarding::bunker as bunker_control; pub use boarding::{ board_friendly_warbot, can_control_warbot_remotely, control_warbot_remotely, disembark_warbot, enter_vehicle, player_vehicle, remote_automatic_weapons, remote_warbot_control, diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index be910936..6733f921 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -72,6 +72,7 @@ pub struct PlayerMotionStep { flight_velocity: Option<[f32; 3]>, hero_walk_velocity: Option<[f32; 2]>, hero_look: Option<HeroLook>, + turret: Option<crate::TurretAim>, max_floor_clearance: f32, previous_terrain_height: Option<f32>, terrain_height: Option<f32>, @@ -228,6 +229,7 @@ mod tests { flight_velocity: None, hero_walk_velocity: None, hero_look: None, + turret: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: Some(11.5), @@ -268,6 +270,7 @@ mod tests { flight_velocity: None, hero_walk_velocity: None, hero_look: None, + turret: None, max_floor_clearance: 1.25, previous_terrain_height: Some(10.0), terrain_height: Some(10.0), @@ -293,6 +296,7 @@ mod tests { flight_velocity: None, hero_walk_velocity: None, hero_look: None, + turret: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: None, @@ -429,6 +433,31 @@ fn plan_player_steps( let boarded = mission .boarding .filter(|boarding| boarding.cockpit.handle == cockpit.handle); + if let Some(aim) = boarded.and_then(|boarding| boarding.turret) { + if [input.mouse_x, input.mouse_y] == [0.0; 2] && !mission.hero_look.has_state() { + return Ok(None); + } + if ![input.mouse_x, input.mouse_y] + .into_iter() + .all(f32::is_finite) + { + return Err(EngineError::Movement("invalid gunner input".into())); + } + let (look, aim) = + crate::bunker_control::advance(engine, cockpit, input, mission.hero_look, aim); + let mut step = plan_motion( + &mission.terrain, + pose, + aim.elevation, + 0.0, + None, + [0.0; 2], + PlayerMotion::default(), + )?; + step.hero_look = Some(look); + step.turret = Some(aim); + return Ok(Some(step)); + } let driving = boarded.filter(|boarding| boarding.controls_chassis()); if boarded.is_some() && driving.is_none() { input.movement = [0.0; 2]; @@ -633,6 +662,9 @@ pub(super) fn commit_motion_state( if boarding.cockpit.handle != cockpit.handle { return; } + if let Some(aim) = step.turret { + boarding.turret = Some(aim); + } if let Some(velocity) = step.flight_velocity { boarding.flight_velocity = if blocked { [0.0; 3] } else { velocity }; } else if !blocked && step.sweep.is_some() { @@ -743,6 +775,7 @@ fn plan_motion( flight_velocity, hero_walk_velocity: None, hero_look: None, + turret: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height, terrain_height, diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index 84408c61..4cc35fcc 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -719,11 +719,19 @@ fn firing_effect_row<'a>( } pub(super) fn prepare_unit_weapons(controls: &[Option<PreparedComponentControl>]) -> PlayerWeapons { - PlayerWeapons { - weapons: controls + prepare_component_weapons( + controls .iter() .enumerate() - .filter_map(|(index, control)| Some((index, control.as_ref()?))) + .filter_map(|(index, control)| Some((index, control.as_ref()?))), + ) +} + +pub(super) fn prepare_component_weapons<'a>( + controls: impl Iterator<Item = (usize, &'a PreparedComponentControl)>, +) -> PlayerWeapons { + PlayerWeapons { + weapons: controls .flat_map(|(index, control)| prepare_weapons(index, control)) .collect(), ..PlayerWeapons::default() diff --git a/crates/fparkan-runtime/src/strategic_buildings.rs b/crates/fparkan-runtime/src/strategic_buildings.rs index 2c97c3c9..c62ea4dc 100644 --- a/crates/fparkan-runtime/src/strategic_buildings.rs +++ b/crates/fparkan-runtime/src/strategic_buildings.rs @@ -30,6 +30,7 @@ pub struct BuildingRow { pub size: u32, pub label: u32, pub durability_percent: Option<i32>, + pub manual_control: bool, } /// Buildings of the operator's clan, most recently registered first. @@ -67,6 +68,8 @@ pub fn building_rows( size, label: building_label(class, size), durability_percent: crate::warbot_stats::durability_percent(table), + manual_control: section == BuildingSection::Bunkers + && crate::bunker_control::cockpit(engine, cockpit, handle).is_some(), }; Some((sequence, row)) }) |
