diff options
Diffstat (limited to 'crates/fparkan-runtime/src/strategic_map_readout.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_map_readout.rs | 148 |
1 files changed, 148 insertions, 0 deletions
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]); + } +} |
