aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/strategic_view.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 08:52:38 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 08:52:38 +0300
commit0e1317235e7db3fa1b20dac2a4f12565a31e8cd0 (patch)
treef0be6ba752084f58fd30c658810ae3c3db439f15 /crates/fparkan-runtime/src/strategic_view.rs
parent9f360911f4af29307b566c0acacf1f5661ea9970 (diff)
downloadfparkan-0e1317235e7db3fa1b20dac2a4f12565a31e8cd0.tar.xz
fparkan-0e1317235e7db3fa1b20dac2a4f12565a31e8cd0.zip
feat(ui): enter strategic view from a command post
Diffstat (limited to 'crates/fparkan-runtime/src/strategic_view.rs')
-rw-r--r--crates/fparkan-runtime/src/strategic_view.rs154
1 files changed, 154 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/strategic_view.rs b/crates/fparkan-runtime/src/strategic_view.rs
new file mode 100644
index 00000000..b22555f6
--- /dev/null
+++ b/crates/fparkan-runtime/src/strategic_view.rs
@@ -0,0 +1,154 @@
+//! Sources of the player's strategic camera.
+
+use crate::{Engine, MissionControlDeviceTableStatus, StartingCockpit, SurfaceQuery};
+use fparkan_render::{LegacyIron3dEulerTransform, RawCameraTransform};
+use fparkan_world::{identity_metadata, registration_sequence, transform_state, ObjectHandle};
+use std::f32::consts::FRAC_PI_2;
+
+/// The object providing the command interface.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum CommandPost {
+ /// Hero at their own clan's bunker communicator.
+ Bunker(ObjectHandle),
+ /// The vehicle the hero is currently piloting.
+ Warbot(ObjectHandle),
+}
+
+/// Returns the source of strategic control available to the player.
+#[must_use]
+pub fn command_post(engine: &Engine, cockpit: StartingCockpit) -> Option<CommandPost> {
+ let mission = engine.loaded.as_ref()?;
+ if let Some(vehicle) = crate::player_vehicle(engine) {
+ 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));
+ }
+
+ let owner = identity_metadata(&engine.world, cockpit.handle)
+ .ok()?
+ .owner_id?;
+ let position = transform_state(&engine.world, cockpit.handle)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ mission
+ .object_drafts
+ .iter()
+ .enumerate()
+ .find_map(|(index, draft)| {
+ let class = draft.unit_dat_header_flags?;
+ if !matches!(class, 0x8001_0000 | 0x8002_0000 | 0x8004_0000) {
+ return None;
+ }
+ let handle = crate::mission_object_handle(engine, draft.original_id?)?;
+ if identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) {
+ return None;
+ }
+ let pose = transform_state(&engine.world, handle).ok()?;
+ crate::mission_events::on_communicator(
+ &mission.mission_assets,
+ index,
+ class,
+ pose,
+ position,
+ )
+ .then_some(CommandPost::Bunker(handle))
+ })
+}
+
+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);
+ // Control+0x2b410 uses the separate cached factory-1 pointer and the
+ // descriptor currently applied to that device.
+ index.is_some_and(|index| index > 0)
+ && devices
+ .iter()
+ .rfind(|device| device.factory_tag == 1)
+ .is_some_and(|device| device.descriptor.fixed_words[2] & 0x0800_0000 != 0)
+}
+
+/// Initial external viewpoint of a command post, sampled from the current world.
+#[must_use]
+pub fn command_view(engine: &Engine, post: CommandPost) -> Option<RawCameraTransform> {
+ let (handle, clearance) = match post {
+ CommandPost::Bunker(handle) => (handle, 36.0),
+ CommandPost::Warbot(handle) => (handle, 2.5),
+ };
+ registration_sequence(&engine.world, handle).ok()??;
+ let [x, y, z] = transform_state(&engine.world, handle)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ let terrain = engine.loaded.as_ref()?.terrain.height_at([x, y]).ok()??;
+ // iron3d+0x63ec0/+0x64140 start at the post, looking north.
+ // The first external update sets pitch and raises the eye out of terrain.
+ let matrix = LegacyIron3dEulerTransform {
+ translation: [x, y, z.max(terrain + clearance)],
+ orientation_radians: [0.0, FRAC_PI_2 - 1.0, FRAC_PI_2],
+ }
+ .try_row_major()?;
+ Some(RawCameraTransform {
+ words: matrix.map(f32::to_bits),
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::tests::synthetic_effective_control_device;
+ use crate::{
+ create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode,
+ MissionRequest, MissionStartupInput,
+ };
+ use fparkan_vfs::DirectoryVfs;
+ use fparkan_world::OriginalObjectId;
+ use std::sync::Arc;
+
+ #[test]
+ fn command_link_requires_a_nonzero_match_and_the_cached_pilot_flag() {
+ let device =
+ |factory, current| synthetic_effective_control_device(factory, current, 0, None, None);
+ let generator = device(19, 19);
+ let mut pilot = device(1, 1);
+ pilot.descriptor.fixed_words[2] = 0x0800_0000;
+ assert!(!has_command_link(&[pilot.clone(), generator.clone()]));
+ assert!(has_command_link(&[generator.clone(), pilot.clone()]));
+
+ let mut last_pilot = device(1, 1);
+ last_pilot.descriptor.fixed_words[2] = 0x0400_0000;
+ assert!(!has_command_link(&[generator, pilot, last_pilot]));
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
+ fn licensed_starting_bunker_supplies_strategy_camera() {
+ let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
+ let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
+ load_gameplay_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/Single.01/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+ let hero = starting_cockpit(&engine).unwrap();
+ let bunker = crate::mission_object_handle(&engine, OriginalObjectId(0)).unwrap();
+ let post = command_post(&engine, hero);
+ assert_eq!(post, Some(CommandPost::Bunker(bunker)));
+ assert!(command_view(&engine, post.unwrap()).is_some());
+ }
+}