From f4286bc592d629288f5fa7a58277a84474af6154 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 3 Dec 2026 11:34:53 +0400 Subject: fix(runtime): select the local starting hero for mission control --- crates/fparkan-runtime/src/lib.rs | 38 +++----- crates/fparkan-runtime/src/mission_events.rs | 134 +++++++++++++++++++++++++-- 2 files changed, 139 insertions(+), 33 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index a432ed17..d5c5b30f 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2871,16 +2871,13 @@ node_provider_distribution_suspended: false, }; let local_hero = (request.mode == MissionMode::SingleMission) .then(|| { - let mut heroes = - object_drafts[..reserve_start] - .iter() - .zip(&handles) - .filter(|(draft, _)| { - draft.unit_dat_header_flags == Some(HERO_UNIT_CLASS) - && draft.raw_after_resource == 0 - }); - let (_, &hero) = heroes.next()?; - heroes.next().is_none().then_some(hero) + mission_events.starting_hero(&new_runtime_world, fparkan_world::OwnerId(0), |handle| { + control_device_tables.iter().any(|table| { + table.handle == handle + && table.native_control_class.is_some() + && !table.node_provider_depletion_marked + }) + }) }) .flatten(); engine.world = new_runtime_world; @@ -3874,7 +3871,7 @@ pub fn mission_object_handle(engine: &Engine, id: OriginalObjectId) -> Option Option { let mission = engine.loaded.as_ref()?; - if mission.script_host_mode != MissionMode::SingleMission { - return None; - } - // shortcut: pick only a unique hero until native local ownership is wired. - let mut heroes = mission - .object_drafts - .iter() - .enumerate() - .filter(|(_, object)| object.unit_dat_header_flags == Some(HERO_UNIT_CLASS)); - let (object_index, hero) = heroes.next()?; - if heroes.next().is_some() { + let hero = mission.local_hero?; + let id = fparkan_world::identity_metadata(&engine.world, hero) + .ok()? + .original_id?; + if mission_object_handle(engine, id)? != hero { return None; } - let handle = mission_object_handle(engine, hero.original_id?)?; - component_cockpit(mission, object_index, handle) + component_cockpit(mission, id.0 as usize, hero) } fn component_cockpit( diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 4721a258..f0bfe89f 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -190,6 +190,27 @@ impl MissionEvents { .count() as u32 } + pub(super) fn starting_hero( + &self, + world: &World, + owner: OwnerId, + active_control: impl Fn(ObjectHandle) -> bool, + ) -> Option { + // iron3d+0x73030 walks the registered objects and accepts the first + // owned class covered by 0x01020000 with a non-depleted Control object. + self.placed_clan_objects + .iter() + .find_map(|&(handle, class)| { + let matches_class = class & super::HERO_UNIT_CLASS == class; + let alive = registration_sequence(world, handle) + .ok() + .flatten() + .is_some(); + let owned = identity_metadata(world, handle).ok()?.owner_id == Some(owner); + (matches_class && alive && owned && active_control(handle)).then_some(handle) + }) + } + pub(super) fn clan_classes<'a>( &'a self, world: &'a World, @@ -716,20 +737,19 @@ impl LoadedMissionState { if self.boarding.is_some() || self.script_host_mode != super::MissionMode::SingleMission { return false; } - let Some(hero) = self.object_drafts.iter().find(|draft| { - draft.unit_dat_header_flags == Some(super::HERO_UNIT_CLASS) - && draft.raw_after_resource == 0 - }) else { + let Some(hero_handle) = self.local_hero else { return false; }; - let Some(hero_position) = hero - .original_id - .and_then(|id| handle_by_original_id(world, id)) - .and_then(|hero| transform_state(world, hero).ok()) - .map(|pose| pose.position.map(f32::from_bits)) + let Some(hero) = identity_metadata(world, hero_handle) + .ok() + .and_then(|identity| self.object_drafts.get(identity.original_id?.0 as usize)) else { return false; }; + let Ok(pose) = transform_state(world, hero_handle) else { + return false; + }; + let hero_position = pose.position.map(f32::from_bits); let clearance = crate::player_motion::spawn_floor_clearance(&self.terrain, hero.position) .unwrap_or_default(); self.object_drafts.iter().enumerate().any(|(index, draft)| { @@ -1207,6 +1227,102 @@ mod tests { ); } + #[test] + fn hero_selection_follows_the_first_owned_eligible_object() { + let mut world = new(WorldConfig); + let mut events = MissionEvents::default(); + let hero_class = crate::HERO_UNIT_CLASS; + let [enemy, depleted, other_class, hero, later] = [ + (1, hero_class), + (0, hero_class), + (0, super::MOBILE_HQ_CLASS), + (0, hero_class), + (0, hero_class), + ] + .map(|(owner, class)| { + let handle = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap(); + set_owner(&mut world, handle, Some(OwnerId(owner))).unwrap(); + register_object(&mut world, handle).unwrap(); + events.placed_clan_objects.push((handle, class)); + handle + }); + assert_eq!( + events.starting_hero(&world, OwnerId(0), |handle| handle != depleted), + Some(hero) + ); + assert_eq!( + events.starting_hero(&world, OwnerId(0), |handle| handle != depleted + && handle != hero), + Some(later) + ); + assert_eq!( + events.starting_hero(&world, OwnerId(1), |_| true), + Some(enemy) + ); + assert_eq!( + events.starting_hero(&world, OwnerId(0), |handle| handle == other_class), + None + ); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_campaign_heroes_turn_and_walk_from_their_start() { + use crate::{ + create, frame, frame_with_player_motion, load_gameplay_mission, plan_player_motion, + starting_cockpit, EngineServices, MissionMode, MissionRequest, MissionStartupInput, + PlayerMotion, + }; + use fparkan_vfs::DirectoryVfs; + use std::{path::PathBuf, sync::Arc}; + + let root = + PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus")); + let vfs = Arc::new(DirectoryVfs::new(&root)); + for (campaign, count) in [4, 4, 4, 4, 2, 2].into_iter().enumerate() { + for mission in 1..=count { + let key = format!( + "MISSIONS/CAMPAIGN/CAMPAIGN.{campaign:02}/Mission.{mission:02}/data.tma" + ); + let mut engine = create(EngineServices::new(vfs.clone())).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: key.clone(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap_or_else(|error| panic!("{key}: {error}")); + let cockpit = + starting_cockpit(&engine).unwrap_or_else(|| panic!("{key}: no cockpit")); + frame(&mut engine, 0).unwrap(); + let initial = + fparkan_world::transform_state(&engine.world, cockpit.handle).unwrap(); + let motion = plan_player_motion( + &engine, + cockpit, + PlayerMotion { + movement: [-1.0, 0.0], + mouse_x: 32.0, + delta_seconds: 0.1, + ..PlayerMotion::default() + }, + ) + .unwrap_or_else(|error| panic!("{key}: {error}")) + .unwrap_or_else(|| panic!("{key}: no input step")); + frame_with_player_motion(&mut engine, 100, cockpit, motion, false).unwrap(); + let moved = fparkan_world::transform_state(&engine.world, cockpit.handle).unwrap(); + assert_ne!(moved.position[..2], initial.position[..2], "{key}: walk"); + assert_ne!(moved.orientation, initial.orientation, "{key}: turn"); + } + } + } + #[test] fn free_unit_selection_skips_busy_reserved_foreign_and_removed_entries() { let mission = crate::MissionDocument { -- cgit v1.2.3