aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/strategic_view.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/strategic_view.rs')
-rw-r--r--crates/fparkan-runtime/src/strategic_view.rs109
1 files changed, 85 insertions, 24 deletions
diff --git a/crates/fparkan-runtime/src/strategic_view.rs b/crates/fparkan-runtime/src/strategic_view.rs
index 250f943e..daf6f17f 100644
--- a/crates/fparkan-runtime/src/strategic_view.rs
+++ b/crates/fparkan-runtime/src/strategic_view.rs
@@ -5,6 +5,8 @@ use fparkan_render::{LegacyIron3dEulerTransform, RawCameraTransform};
use fparkan_world::{identity_metadata, registration_sequence, transform_state, ObjectHandle};
use std::f32::consts::FRAC_PI_2;
+mod vehicle_camera;
+
/// Horizontal field of view of the strategic camera.
pub const COMMAND_FOV: f32 = 1.04;
@@ -13,7 +15,7 @@ pub const COMMAND_FOV: f32 = 1.04;
pub enum CommandPost {
/// Hero at their own clan's bunker communicator.
Bunker(ObjectHandle),
- /// The vehicle the hero is currently piloting.
+ /// A vehicle providing a mobile command link.
Warbot(ObjectHandle),
}
@@ -25,13 +27,8 @@ pub fn command_post(engine: &Engine, cockpit: StartingCockpit) -> Option<Command
if vehicle.handle != cockpit.handle {
return None;
}
- let table = mission.control_device_tables.iter().find(|table| {
- table.handle == vehicle.handle && !table.node_provider_depletion_marked
- })?;
- let MissionControlDeviceTableStatus::Available(devices) = &table.status else {
- return None;
- };
- return has_command_link(devices).then_some(CommandPost::Warbot(vehicle.handle));
+ return vehicle_command_link(engine, vehicle.handle)
+ .then_some(CommandPost::Warbot(vehicle.handle));
}
let owner = identity_metadata(&engine.world, cockpit.handle)
@@ -66,6 +63,47 @@ pub fn command_post(engine: &Engine, cockpit: StartingCockpit) -> Option<Command
})
}
+/// Connects the hero to a selected allied mobile command center within reach.
+pub fn mobile_command_post(
+ engine: &Engine,
+ cockpit: StartingCockpit,
+ target: ObjectHandle,
+) -> Option<CommandPost> {
+ if crate::player_vehicle(engine).is_some() {
+ return None;
+ }
+ let hero_owner = identity_metadata(&engine.world, cockpit.handle)
+ .ok()?
+ .owner_id?;
+ let metadata = identity_metadata(&engine.world, target).ok()?;
+ let draft = engine
+ .loaded
+ .as_ref()?
+ .object_drafts
+ .get(metadata.original_id?.0 as usize)?;
+ if metadata.owner_id != Some(hero_owner)
+ || draft.unit_dat_header_flags != Some(crate::mission_events::MOBILE_HQ_CLASS)
+ || !vehicle_command_link(engine, target)
+ {
+ return None;
+ }
+ let hero = post_position(engine, cockpit.handle)?;
+ let position = post_position(engine, target)?;
+ crate::boarding::vehicle_interaction_distance_sq(hero, position)?;
+ Some(CommandPost::Warbot(target))
+}
+
+fn vehicle_command_link(engine: &Engine, handle: ObjectHandle) -> bool {
+ engine.loaded.as_ref().is_some_and(|mission| {
+ mission.control_device_tables.iter().any(|table| {
+ table.handle == handle
+ && !table.node_provider_depletion_marked
+ && matches!(&table.status, MissionControlDeviceTableStatus::Available(devices)
+ if has_command_link(devices))
+ })
+ })
+}
+
fn has_command_link(devices: &[crate::EffectiveControlDevice]) -> bool {
// iron3d+0x773a0 and Control+0x2c3c0 search the current tags from index zero.
let index = devices.iter().position(|device| device.descriptor.tag == 1);
@@ -88,7 +126,7 @@ fn post_position(engine: &Engine, handle: ObjectHandle) -> Option<[f32; 3]> {
)
}
-/// Held directions of the bunker camera during the current frame.
+/// Held command camera directions during the current frame.
#[derive(Clone, Copy, Default)]
pub struct CommandCameraInput {
/// Motion along the camera's right, forward and vertical axes.
@@ -142,6 +180,7 @@ pub struct CommandCamera {
position: [f32; 3],
yaw: f32,
tilt: f32,
+ follow_distance: f32,
axes: [CameraAxis; 5],
}
@@ -157,6 +196,7 @@ impl CommandCamera {
position: post_position(engine, handle)?,
yaw: FRAC_PI_2,
tilt: 1.0,
+ follow_distance: 0.0,
axes: [CameraAxis::default(); 5],
})
}
@@ -170,28 +210,45 @@ impl CommandCamera {
input: CommandCameraInput,
building_height: impl FnOnce([[f32; 3]; 2]) -> Option<f32>,
) -> Option<RawCameraTransform> {
- let terrain = &engine.loaded.as_ref()?.terrain;
+ let mission = engine.loaded.as_ref()?;
+ let terrain = &mission.terrain;
let [min, max] = terrain.surface_bounds()?;
- let clearance = match self.post {
+ match self.post {
CommandPost::Bunker(_) => {
self.advance_bunker(dt, input, max);
- 36.0
}
CommandPost::Warbot(handle) => {
- self.position = post_position(engine, handle)?;
- 2.5
+ if !vehicle_command_link(engine, handle) {
+ return None;
+ }
+ let target = post_position(engine, handle)?;
+ let state = transform_state(&engine.world, handle).ok()?;
+ let index = identity_metadata(&engine.world, handle)
+ .ok()?
+ .original_id?
+ .0 as usize;
+ let (_, radius) =
+ crate::projectile::unit_sphere(&mission.mission_assets, index, state)?;
+ self.advance_vehicle(dt, input.rotation, max, target, radius);
}
- };
+ }
let [x, y, z] = self.position;
let ground = terrain.height_at([x, y]).ok()?;
- // iron3d+0xa1470 casts from the map's upper plane and includes roofs.
- let surface = building_height([[x, y, max[2]], [x, y, ground.unwrap_or(min[2])]])
- .or(ground)
- .unwrap_or(0.0);
- self.position[2] = z.max(surface + clearance);
- if let CommandPost::Bunker(handle) = self.post {
- self.position[2] = self.position[2].min(surface + clearance + 200.0);
- self.limit_to_bunker(post_position(engine, handle)?);
+ let surface = match self.post {
+ CommandPost::Bunker(handle) => {
+ // Only the bunker passes the roof flags to iron3d+0xa1470.
+ let surface =
+ building_height([[x, y, max[2]], [x, y, ground.unwrap_or(min[2])]]).or(ground);
+ self.limit_to_bunker(post_position(engine, handle)?);
+ surface
+ }
+ CommandPost::Warbot(_) => ground,
+ }
+ .unwrap_or(0.0);
+ // The last camera update at iron3d+0x37720 applies this band in both modes.
+ self.position[2] = z.clamp(surface + 36.0, surface + 236.0);
+ if !self.position.into_iter().all(f32::is_finite) {
+ return None;
}
let matrix = LegacyIron3dEulerTransform {
translation: self.position,
@@ -222,7 +279,6 @@ impl CommandCamera {
fn move_bunker(&mut self, dt: f32, input: CommandCameraInput, map_max: [f32; 3]) {
let [right, forward, up] = input.translation;
- let [yaw, tilt] = input.rotation;
let right = self.axes[0].step(right, 125.0, dt);
let forward = self.axes[1].step(forward, 125.0, dt);
let up = self.axes[2].step(up, 62.5, dt);
@@ -238,6 +294,10 @@ impl CommandCamera {
}
}
self.position[2] += up;
+ self.rotate(dt, input.rotation);
+ }
+
+ fn rotate(&mut self, dt: f32, [yaw, tilt]: [i8; 2]) {
self.yaw += self.axes[3].step(yaw, 1.5, dt);
let next_tilt = self.tilt + self.axes[4].step(tilt, 1.5, dt);
if 0.0 < next_tilt && next_tilt < FRAC_PI_2 {
@@ -294,6 +354,7 @@ mod tests {
position: origin,
yaw: FRAC_PI_2,
tilt: 1.0,
+ follow_distance: 0.0,
axes: [CameraAxis::default(); 5],
};
let input = CommandCameraInput {