From 5f0b8c076b40f8eca67e2745367c731f803914f4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 11:09:58 +0400 Subject: feat(ui): select units from the strategic warbot roster --- crates/fparkan-runtime/src/lib.rs | 8 + crates/fparkan-runtime/src/satellite_contacts.rs | 2 +- crates/fparkan-runtime/src/strategic_roster.rs | 215 +++++++++++++++++++++++ crates/fparkan-runtime/src/unit_patrol/orders.rs | 27 +++ crates/fparkan-runtime/src/unit_size_class.rs | 2 +- crates/fparkan-runtime/src/unit_spawn.rs | 8 +- 6 files changed, 255 insertions(+), 7 deletions(-) create mode 100644 crates/fparkan-runtime/src/strategic_roster.rs (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index cb8c53d5..96a39732 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -72,6 +72,7 @@ mod node_amount; mod node_stage; pub mod satellite_contacts; pub mod strategic_balance; +pub mod strategic_roster; pub mod strategic_view; pub use clan_clock::note_mission_pause_elapsed; use control_profile::project_initial as project_control_owner_profile_fca0; @@ -1532,6 +1533,7 @@ struct LoadedMissionState { asset_plan: MissionAssetPlan, object_drafts: Vec, unit_chassis_flags: std::collections::BTreeMap, + unit_serials: strategic_roster::UnitSerials, behavior_distributors: Vec, behavior_ore_states: Vec, behavior_profiles: Vec, @@ -2888,6 +2890,11 @@ node_provider_distribution_suspended: false, let free_research = research::FreeResearch::new(&object_drafts[..reserve_start]); let chassis_resources = CachedResourceRepository::new(vfs.clone()); let unit_chassis_flags = unit_chassis::load_flags(&chassis_resources, &object_drafts); + let unit_serials = strategic_roster::UnitSerials::placed( + &engine.world, + &object_drafts[..reserve_start], + &handles, + ); engine.loaded = Some(LoadedMissionState { summary: summary.clone(), mission, @@ -2900,6 +2907,7 @@ node_provider_distribution_suspended: false, asset_plan: mission_asset_plan, object_drafts, unit_chassis_flags, + unit_serials, behavior_distributors, behavior_ore_states, behavior_profiles, diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs index 68957250..e984baf3 100644 --- a/crates/fparkan-runtime/src/satellite_contacts.rs +++ b/crates/fparkan-runtime/src/satellite_contacts.rs @@ -68,7 +68,7 @@ pub fn visible_units( selected: Option, ) -> impl Iterator + '_ { let boarded = crate::player_vehicle(engine).map(|vehicle| vehicle.handle); - let selected = boarded.or(selected); + let selected = selected.or(boarded); let patrol = engine .loaded .as_ref() diff --git a/crates/fparkan-runtime/src/strategic_roster.rs b/crates/fparkan-runtime/src/strategic_roster.rs new file mode 100644 index 00000000..acf22590 --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_roster.rs @@ -0,0 +1,215 @@ +//! Clan unit registration and the live rows of the strategic warbot list. + +use crate::{Engine, MissionObjectDraft, StartingCockpit}; +use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle, OwnerId, World}; +use std::collections::HashMap; + +const CATEGORY: u32 = 0x0101_8000; +const WARBOT: u32 = 0x0100_8000; +const COMMAND_WARBOT: u32 = 0x0101_0000; + +#[derive(Default)] +pub(super) struct UnitSerials { + counts: HashMap, + slots: HashMap, +} + +impl UnitSerials { + pub(super) fn placed( + world: &World, + drafts: &[MissionObjectDraft], + handles: &[ObjectHandle], + ) -> Self { + let mut serials = Self::default(); + for (draft, &handle) in drafts.iter().zip(handles) { + if let (Some(class), Some(owner)) = ( + draft.unit_dat_header_flags, + identity_metadata(world, handle) + .ok() + .and_then(|meta| meta.owner_id), + ) { + serials.register(handle, owner, class); + } + } + serials + } + + pub(super) fn register(&mut self, handle: ObjectHandle, owner: OwnerId, class: u32) { + if class & crate::autodemo::MOBILE_UNIT == 0 + && class != crate::projectile::ANIMAL_UNIT_CLASS + { + return; + } + // iron3d+0x761a0 advances the naming counter of the registering clan. + let serial = self.counts.entry(owner).or_default(); + *serial += 1; + self.slots.insert(handle.slot, (handle.generation, *serial)); + } + + fn get(&self, handle: ObjectHandle) -> Option { + let &(generation, serial) = self.slots.get(&handle.slot)?; + (generation == handle.generation).then_some(serial) + } +} + +/// Snapshot of an eligible unit for the strategic list. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct WarbotRow { + /// Unit represented by the row, including its generation. + pub handle: ObjectHandle, + /// Three ASCII letters identifying size, chassis, and class. + pub prefix: [char; 3], + /// Stable number assigned at creation in the original clan. + pub serial: u32, + /// String table ID for the name suffix. + pub class_label: u32, + /// String table ID for the active order. + pub order_label: u32, + /// Display class, from Behavior property 0x201. + pub size: u32, + /// Locomotion type, from Behavior property 0x207. + pub chassis: u32, + /// Whether the unit is eligible for automatic selection. + pub selectable: bool, +} + +/// Returns the player's warbots in registration order, including any entries whose controller is depleted. +pub fn warbot_rows(engine: &Engine, cockpit: StartingCockpit) -> Vec { + let Some((mission, owner)) = engine.loaded.as_ref().zip( + identity_metadata(&engine.world, cockpit.handle) + .ok() + .and_then(|meta| meta.owner_id), + ) else { + return Vec::new(); + }; + let mut rows: Vec<_> = mission + .control_device_tables + .iter() + .filter_map(|table| { + let handle = table.handle; + let draft = mission.object_drafts.get(table.original_id?.0 as usize)?; + let class = draft.unit_dat_header_flags?; + if class == 0 + || class & CATEGORY != class + || identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) + { + return None; + } + let (role, class_label) = match class { + WARBOT => ('W', 0x183a), + COMMAND_WARBOT => ('C', 0x183b), + _ => return None, + }; + let sequence = registration_sequence(&engine.world, handle).ok()??; + let serial = mission.unit_serials.get(handle)?; + let size = + crate::unit_size_class::classify(4, &draft.unit_components.first()?.resource_raw); + let chassis = mission + .unit_chassis_flags + .get(&table.original_id?.0) + .and_then(|flags| flags.chassis_type) + .unwrap_or(0); + let order = mission + .unit_patrol + .as_ref() + .map_or(0, |patrol| patrol.active_order_code(handle)); + let row = WarbotRow { + handle, + prefix: [letter(b"?TSML", size), letter(b"?FSWTAU", chassis), role], + serial, + class_label, + order_label: order_label(order), + size, + chassis, + selectable: !table.node_provider_depletion_marked, + }; + Some((sequence, row)) + }) + .collect(); + rows.sort_unstable_by_key(|&(sequence, _)| sequence); + rows.into_iter().map(|(_, row)| row).collect() +} + +fn letter(alphabet: &[u8], value: u32) -> char { + char::from(alphabet.get(value as usize).copied().unwrap_or(b'?')) +} + +fn order_label(order: u32) -> u32 { + match order { + 0 => 0x1824, + 2 => 0x1828, + 3 => 0x182c, + 4 => 0x182d, + 0x11 => 0x1833, + 0x13 => 0x1826, + 0x14 => 0x1829, + _ => 0x1835, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn numbers_all_mobile_classes_per_creation_clan_without_reusing_a_stale_handle() { + let mut serials = UnitSerials::default(); + let handle = |slot, generation| ObjectHandle { slot, generation }; + let local = OwnerId(0); + serials.register(handle(0, 1), local, 0x8001_0000); + serials.register(handle(1, 1), local, 0x0100_2000); + serials.register(handle(2, 1), local, 0x0100_4000); + serials.register(handle(3, 1), OwnerId(1), WARBOT); + serials.register(handle(4, 1), local, WARBOT); + assert_eq!(serials.get(handle(4, 1)), Some(3)); + assert_eq!(serials.get(handle(3, 1)), Some(1)); + serials.register(handle(4, 2), local, COMMAND_WARBOT); + assert_eq!(serials.get(handle(4, 1)), None); + assert_eq!(serials.get(handle(4, 2)), Some(4)); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_two_fortresses_roster_uses_live_clan_and_original_serials() { + use crate::{ + create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::set_owner; + use std::sync::Arc; + + 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 cockpit = starting_cockpit(&engine).unwrap(); + let rows = warbot_rows(&engine, cockpit); + let labels: Vec<_> = rows + .iter() + .map(|row| (row.prefix, row.serial, row.order_label)) + .collect(); + assert_eq!( + labels, + [ + (['L', 'T', 'W'], 3, 0x1824), + (['L', 'T', 'W'], 4, 0x1824), + (['M', 'T', 'W'], 5, 0x1824) + ] + ); + + set_owner(&mut engine.world, rows[0].handle, None).unwrap(); + assert_eq!(warbot_rows(&engine, cockpit), rows[1..]); + } +} diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index 560f2142..8e9260a1 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -50,6 +50,33 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn active_order_code(&self, actor: ObjectHandle) -> u32 { + if self.is_leaving_factory(actor) { + return 0x14; + } + if self.is_shut_down(actor) { + return UnitOrder::Shutdown.opcode(); + } + let Some(unit) = self.units.iter().find(|unit| unit.handle == actor) else { + return 0; + }; + if unit.capture_target.is_some() { + 0x11 + } else if unit.attack_target.is_some() || unit.raid_factory.is_some() { + 3 + } else if unit + .destinations + .as_ref() + .is_some_and(|points| !points.is_empty()) + { + 2 + } else if unit.destinations.is_none() { + 4 + } else { + 0 + } + } + pub(crate) fn satellite_route(&self, actor: ObjectHandle) -> Vec<[f32; 2]> { let active = self.units.iter().find(|unit| unit.handle == actor); // iron3d+0x77ae0 prefers the path whenever there are unvisited points. diff --git a/crates/fparkan-runtime/src/unit_size_class.rs b/crates/fparkan-runtime/src/unit_size_class.rs index 8f9a2d93..a786c610 100644 --- a/crates/fparkan-runtime/src/unit_size_class.rs +++ b/crates/fparkan-runtime/src/unit_size_class.rs @@ -30,7 +30,7 @@ pub(super) fn query( Ok(classify(kind, resource)) } -fn classify(kind: u32, name: &[u8]) -> u32 { +pub(super) fn classify(kind: u32, name: &[u8]) -> u32 { let name = &name[..name .iter() .position(|&byte| byte == 0) diff --git a/crates/fparkan-runtime/src/unit_spawn.rs b/crates/fparkan-runtime/src/unit_spawn.rs index 09345192..7af5db10 100644 --- a/crates/fparkan-runtime/src/unit_spawn.rs +++ b/crates/fparkan-runtime/src/unit_spawn.rs @@ -60,11 +60,9 @@ pub(super) fn spawn_prepared_unit( .behavior_control_groupings .push(crate::project_mission_behavior_control_grouping(&table)); mission.control_device_tables.push(table); - mission.mission_events.register_unit( - world, - handle, - draft.unit_dat_header_flags.expect("prepared reserve"), - ); + let class = draft.unit_dat_header_flags.expect("prepared reserve"); + mission.mission_events.register_unit(world, handle, class); + mission.unit_serials.register(handle, OwnerId(clan), class); mission.note_clan_registration(OwnerId(clan), handle); Ok(handle) } -- cgit v1.2.3