diff options
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_map_readout.rs | 148 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_roster.rs | 10 |
3 files changed, 156 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 1db75dbc..ad5709b3 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -73,6 +73,7 @@ mod node_stage; mod projectile_capacity; pub mod satellite_contacts; pub mod strategic_balance; +pub mod strategic_map_readout; pub mod strategic_orders; pub mod strategic_roster; pub mod strategic_view; diff --git a/crates/fparkan-runtime/src/strategic_map_readout.rs b/crates/fparkan-runtime/src/strategic_map_readout.rs new file mode 100644 index 00000000..33d24b0f --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_map_readout.rs @@ -0,0 +1,148 @@ +//! Live information for the hovered object on the strategic satellite map. + +use crate::{ + satellite_contacts, + strategic_roster::{order_label, unit_prefix}, + Engine, StartingCockpit, HERO_UNIT_CLASS, +}; +use fparkan_world::identity_metadata; + +/// Object data and original resource selectors for the map footer. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ContactReadout { + pub class: u32, + pub size: u32, + pub chassis: u32, + pub clan: u16, + pub emblem_tint: [u8; 3], + pub name_label: u32, + pub callsign: Option<([char; 3], u32)>, + pub order_label: Option<u32>, + pub durability_percent: Option<i32>, +} + +/// Resolves the hovered map contact. Empty terrain and depleted objects have no readout. +pub fn contact_at( + engine: &Engine, + cockpit: StartingCockpit, + point: [f32; 2], +) -> Option<ContactReadout> { + let handle = satellite_contacts::pick_contact(engine, cockpit, point)?; + let mission = engine.loaded.as_ref()?; + let player = identity_metadata(&engine.world, cockpit.handle) + .ok()? + .owner_id?; + let identity = identity_metadata(&engine.world, handle).ok()?; + let owner = identity.owner_id?; + let original = identity.original_id?.0; + let draft = mission.object_drafts.get(original as usize)?; + let class = draft.unit_dat_header_flags?; + let building = class & 0x8000_0000 != 0; + let root = &draft.unit_components.first()?.resource_raw; + let size = crate::unit_size_class::classify(if building { 3 } else { 4 }, root); + let chassis = mission + .unit_chassis_flags + .get(&original) + .and_then(|flags| flags.chassis_type) + .unwrap_or(0); + let (name_label, callsign, order_label) = if building { + (building_label(class, size), None, None) + } else { + let (role, label) = match class { + 0x0100_2000 => ('T', 0x1838), + 0x0100_4000 => ('B', 0x1839), + 0x0100_8000 => ('W', 0x183a), + 0x0101_0000 => ('C', 0x183b), + HERO_UNIT_CLASS => ('?', 0x1856), + // Animals have map marks but no footer. + _ => return None, + }; + let callsign = (class != HERO_UNIT_CLASS) + .then(|| { + Some(( + unit_prefix(size, chassis, role), + mission.unit_serials.get(handle)?, + )) + }) + .flatten(); + let task = mission + .unit_patrol + .as_ref() + .map_or(0, |patrol| patrol.active_order_code(handle)); + (label, callsign, Some(order_label(task))) + }; + let observer = mission.mission.clans.get(usize::from(player.0))?; + let target = mission.mission.clans.get(usize::from(owner.0))?; + let relation = observer + .relations + .iter() + .find(|entry| entry.other_clan_name.raw == target.name.raw) + .map(|entry| entry.relation_value); + let emblem_tint = emblem_tint(owner == player, relation, building); + let durability_percent = mission + .control_device_tables + .iter() + .find(|table| table.handle == handle) + .and_then(crate::warbot_stats::durability_percent); + Some(ContactReadout { + class, + size, + chassis, + clan: owner.0, + emblem_tint, + name_label, + callsign, + order_label, + durability_percent, + }) +} + +fn emblem_tint(own: bool, relation: Option<i32>, building: bool) -> [u8; 3] { + match (own, relation, building) { + (false, Some(0), true) => [255; 3], + (false, Some(0), false) => [255, 0, 0], + (false, Some(1), _) => [128; 3], + _ => satellite_contacts::OWN_TINT, + } +} + +fn building_label(class: u32, size: u32) -> u32 { + // iron3d+0x33a90 uses the type and Behavior's size selector. + match (class, size) { + (0x8000_0002, _) => 0x1799, + (0x8000_0004, 2..=4) => 0x178d + size, + (0x8000_0008, 2..=4) => 0x1792 + size, + (0x8000_0010, 2..=4) => 0x17a1 + size, + (0x8000_0040, _) => 0x17a8, + (0x8000_0200, 2..=3) => 0x17b5 + size, + (0x8000_0400, 2..=5) => 0x179c + size, + (0x8000_1000, 2..=5) => 0x17b0 + size, + (0x8001_0000, _) => 0x17c6, + (0x8002_0000, _) => 0x17cc, + (0x8004_0000, _) => 0x17d2, + (0x8010_0000, _) => 0x17bd, + (0x8020_0000, _) => 0x17c3, + _ => 0x183d, + } +} + +#[cfg(test)] +mod tests { + use super::{building_label, emblem_tint}; + + #[test] + fn warehouse_size_changes_the_name_while_bunker_class_fixes_it() { + assert_eq!(building_label(0x8000_0008, 2), 0x1794); + assert_eq!(building_label(0x8000_0008, 3), 0x1795); + assert_eq!(building_label(0x8002_0000, 3), 0x17cc); + assert_eq!(building_label(0x8000_0008, 5), 0x183d); + } + + #[test] + fn the_own_clan_keeps_its_blue_emblem() { + assert_eq!(emblem_tint(true, Some(1), false), [128, 128, 255]); + assert_eq!(emblem_tint(false, Some(1), false), [128; 3]); + assert_eq!(emblem_tint(false, Some(0), true), [255; 3]); + assert_eq!(emblem_tint(false, Some(0), false), [255, 0, 0]); + } +} diff --git a/crates/fparkan-runtime/src/strategic_roster.rs b/crates/fparkan-runtime/src/strategic_roster.rs index 07735aff..1e3bd8bd 100644 --- a/crates/fparkan-runtime/src/strategic_roster.rs +++ b/crates/fparkan-runtime/src/strategic_roster.rs @@ -46,7 +46,7 @@ impl UnitSerials { self.slots.insert(handle.slot, (handle.generation, *serial)); } - fn get(&self, handle: ObjectHandle) -> Option<u32> { + pub(super) fn get(&self, handle: ObjectHandle) -> Option<u32> { let &(generation, serial) = self.slots.get(&handle.slot)?; (generation == handle.generation).then_some(serial) } @@ -117,7 +117,7 @@ pub fn warbot_rows(engine: &Engine, cockpit: StartingCockpit) -> Vec<WarbotRow> .map_or(0, |patrol| patrol.active_order_code(handle)); let row = WarbotRow { handle, - prefix: [letter(b"?TSML", size), letter(b"?FSWTAU", chassis), role], + prefix: unit_prefix(size, chassis, role), serial, class_label, order_label: order_label(order), @@ -137,7 +137,11 @@ fn letter(alphabet: &[u8], value: u32) -> char { char::from(alphabet.get(value as usize).copied().unwrap_or(b'?')) } -fn order_label(order: u32) -> u32 { +pub(super) fn unit_prefix(size: u32, chassis: u32, role: char) -> [char; 3] { + [letter(b"?TSML", size), letter(b"?FSWTAU", chassis), role] +} + +pub(super) fn order_label(order: u32) -> u32 { match order { 0 => 0x1824, 2 => 0x1828, |
