aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/satellite_contacts.rs133
1 files changed, 102 insertions, 31 deletions
diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs
index a7c4cb7b..a766e911 100644
--- a/crates/fparkan-runtime/src/satellite_contacts.rs
+++ b/crates/fparkan-runtime/src/satellite_contacts.rs
@@ -1,8 +1,10 @@
//! Live contacts for the satellite map.
use crate::{Engine, MissionObjectDraft, StartingCockpit};
+use fparkan_render::LegacyIron3dEulerTransform;
use fparkan_world::{
- handle_by_original_id, identity_metadata, registration_sequence, transform_state,
+ handle_by_original_id, identity_metadata, registration_sequence, transform_state, ObjectHandle,
+ TransformState,
};
/// The original icon crop and position within the 256-pixel satellite image.
@@ -17,6 +19,14 @@ pub struct BuildingContact {
pub struct UnitContact {
pub pixel: [i32; 2],
pub cross: bool,
+ pub heading: Option<UnitHeading>,
+}
+
+/// The selected vehicle or unboarded hero's outline and heading stroke.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub struct UnitHeading {
+ pub tip_offset: [i32; 2],
+ pub selected: bool,
}
/// Width of the original landscape square used by the map projection.
@@ -37,7 +47,7 @@ pub fn owned_buildings(
extent: f32,
) -> impl Iterator<Item = BuildingContact> + '_ {
owned_contacts(engine, cockpit, extent, |_, class| icon_x(class))
- .map(|(pixel, icon_x)| BuildingContact { pixel, icon_x })
+ .map(|(pixel, _, _, icon_x)| BuildingContact { pixel, icon_x })
}
/// Current marks for the player's vehicles, including newly registered factory output.
@@ -46,8 +56,10 @@ pub fn owned_units(
cockpit: StartingCockpit,
extent: f32,
) -> impl Iterator<Item = UnitContact> + '_ {
+ let boarded = crate::player_vehicle(engine).map(|vehicle| vehicle.handle);
owned_contacts(engine, cockpit, extent, move |draft, class| {
- if class & crate::autodemo::MOBILE_UNIT == 0 || class == crate::HERO_UNIT_CLASS {
+ let hero = class == crate::HERO_UNIT_CLASS;
+ if class & crate::autodemo::MOBILE_UNIT == 0 || (hero && boarded.is_some()) {
return None;
}
// iron3d+0x77f33 reads Behavior property 0x207 (ChassisType).
@@ -56,9 +68,37 @@ pub fn owned_units(
.as_ref()?
.unit_chassis_flags
.get(&draft.original_id?.0)?;
- Some(flags.chassis_type == Some(1))
+ Some((hero, flags.chassis_type == Some(1)))
+ })
+ .map(move |(pixel, handle, pose, (hero, cross))| {
+ let selected = boarded == Some(handle);
+ UnitContact {
+ pixel,
+ cross,
+ heading: if hero || selected {
+ unit_heading(pose, cross, selected)
+ } else {
+ None
+ },
+ }
+ })
+}
+
+fn unit_heading(pose: TransformState, cross: bool, selected: bool) -> Option<UnitHeading> {
+ // iron3d+0x753e0 takes the second column of the unit's world transform.
+ let basis = LegacyIron3dEulerTransform {
+ translation: [0.0; 3],
+ orientation_radians: pose.orientation.map(f32::from_bits),
+ }
+ .try_row_major()?;
+ // iron3d+0x77ae0 scales forward XY by the glyph width and inverts Y.
+ let length = f64::from(if cross { 4 } else { 3 }) * f64::from(1.8_f32);
+ let tip_offset =
+ [basis[1], -basis[5]].map(|axis| (f64::from(axis) * length).round_ties_even() as i32);
+ Some(UnitHeading {
+ tip_offset,
+ selected,
})
- .map(|(pixel, cross)| UnitContact { pixel, cross })
}
fn owned_contacts<'a, T>(
@@ -66,7 +106,7 @@ fn owned_contacts<'a, T>(
cockpit: StartingCockpit,
extent: f32,
marker: impl Fn(&MissionObjectDraft, u32) -> Option<T> + Copy + 'a,
-) -> impl Iterator<Item = ([i32; 2], T)> + 'a {
+) -> impl Iterator<Item = ([i32; 2], ObjectHandle, TransformState, T)> + 'a {
let owner = identity_metadata(&engine.world, cockpit.handle)
.ok()
.and_then(|identity| identity.owner_id);
@@ -79,11 +119,9 @@ fn owned_contacts<'a, T>(
if Some(clan) != owner {
return None;
}
- let [world_x, world_y, _] = transform_state(&engine.world, handle)
- .ok()?
- .position
- .map(f32::from_bits);
- Some((project([world_x, world_y], extent)?, marker))
+ let pose = transform_state(&engine.world, handle).ok()?;
+ let [world_x, world_y, _] = pose.position.map(f32::from_bits);
+ Some((project([world_x, world_y], extent)?, handle, pose, marker))
})
})
}
@@ -116,7 +154,7 @@ fn project([x, y]: [f32; 2], extent: f32) -> Option<[i32; 2]> {
#[cfg(test)]
mod tests {
- use super::{icon_x, project};
+ use super::{icon_x, project, unit_heading, TransformState, UnitHeading};
#[test]
fn satellite_coordinates_match_the_original_inset_and_rounding() {
@@ -130,6 +168,22 @@ mod tests {
}
#[test]
+ fn satellite_heading_uses_chassis_glyph_width_and_inverts_world_y() {
+ let mut pose = TransformState::default();
+ pose.orientation[2] = std::f32::consts::FRAC_PI_2.to_bits();
+ assert_eq!(
+ unit_heading(pose, false, false),
+ Some(UnitHeading {
+ tip_offset: [-5, 0],
+ selected: false,
+ }),
+ );
+ pose.orientation[2] = (-0.6_f32).atan2(0.8).to_bits();
+ assert_eq!(unit_heading(pose, false, true).unwrap().tip_offset, [3, -4]);
+ assert_eq!(unit_heading(pose, true, true).unwrap().tip_offset, [4, -6]);
+ }
+
+ #[test]
#[ignore = "requires licensed corpus"]
fn captured_warbot_mark_uses_its_live_owner_and_position() {
use super::{landscape_extent, owned_units, UnitContact};
@@ -140,6 +194,7 @@ mod tests {
use fparkan_vfs::DirectoryVfs;
use fparkan_world::{
handle_by_original_id, identity_metadata, set_owner, set_transform, transform_state,
+ OriginalObjectId,
};
use std::sync::Arc;
@@ -160,17 +215,7 @@ mod tests {
.unwrap();
let cockpit = starting_cockpit(&engine).unwrap();
let extent = landscape_extent(&engine).unwrap();
- let id = engine
- .loaded
- .as_ref()
- .unwrap()
- .object_drafts
- .iter()
- .find(|draft| draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS))
- .unwrap()
- .original_id
- .unwrap();
- let handle = handle_by_original_id(&engine.world, id).unwrap();
+ let handle = handle_by_original_id(&engine.world, OriginalObjectId(11)).unwrap();
let owner = identity_metadata(&engine.world, cockpit.handle)
.unwrap()
.owner_id;
@@ -183,22 +228,48 @@ mod tests {
let start = project([x, y], extent).unwrap();
assert!(!owned_units(&engine, cockpit, extent).any(|contact| contact.pixel == start));
set_owner(&mut engine.world, handle, owner).unwrap();
- assert!(owned_units(&engine, cockpit, extent).any(|contact| contact
- == UnitContact {
- pixel: start,
- cross: false
- }));
+ let captured = owned_units(&engine, cockpit, extent)
+ .find(|contact| contact.pixel == start)
+ .expect("captured warbot mark");
+ assert_eq!(captured.heading, None);
pose.position[0] = (x + extent / 8.0).to_bits();
set_transform(&mut engine.world, handle, pose).unwrap();
let moved = project([x + extent / 8.0, y], extent).unwrap();
assert_eq!(moved, [start[0] + 32, start[1]]);
let units: Vec<_> = owned_units(&engine, cockpit, extent).collect();
- assert!(units.contains(&UnitContact {
+ let moved_contact = UnitContact {
pixel: moved,
- cross: false
- }));
+ ..captured
+ };
+ assert!(units.contains(&moved_contact));
assert!(!units.iter().any(|contact| contact.pixel == start));
+ assert!(units
+ .iter()
+ .any(|contact| contact.heading.is_some_and(|heading| !heading.selected)));
+
+ let mut hero_pose = transform_state(&engine.world, cockpit.handle).unwrap();
+ hero_pose.position = pose.position;
+ hero_pose.position[0] = (f32::from_bits(pose.position[0]) - 15.0).to_bits();
+ set_transform(&mut engine.world, cockpit.handle, hero_pose).unwrap();
+ let vehicle = crate::board_friendly_warbot(&mut engine, Some(handle))
+ .unwrap()
+ .unwrap();
+ let headings: Vec<_> = owned_units(&engine, vehicle, extent)
+ .filter(|unit| unit.heading.is_some())
+ .collect();
+ assert_eq!(
+ headings,
+ [UnitContact {
+ heading: unit_heading(pose, moved_contact.cross, true),
+ ..moved_contact
+ }]
+ );
+ assert_eq!(crate::disembark_warbot(&mut engine).unwrap(), Some(cockpit));
+ let headings: Vec<_> = owned_units(&engine, cockpit, extent)
+ .filter_map(|unit| unit.heading)
+ .collect();
+ assert_eq!(headings, [unit_heading(pose, false, false).unwrap()]);
}
#[test]