aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-03 10:34:53 +0300
committerValentin Popov <valentin@popov.link>2026-12-03 10:34:53 +0300
commitf4286bc592d629288f5fa7a58277a84474af6154 (patch)
tree333af29a34abeac865d79d0b347dfe393ac01249 /crates/fparkan-runtime
parentbd3e9f385eae92ae92a153bff6385f92d3ea76b7 (diff)
downloadfparkan-f4286bc592d629288f5fa7a58277a84474af6154.tar.xz
fparkan-f4286bc592d629288f5fa7a58277a84474af6154.zip
fix(runtime): select the local starting hero for mission control
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/lib.rs38
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs134
2 files changed, 139 insertions, 33 deletions
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<Ob
handle_by_original_id(&engine.world, id)
}
-/// Camera point of the single unambiguous starting hero in a mission.
+/// Camera point of the local player's starting hero.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StartingCockpit {
/// Live world object whose transform drives the viewpoint.
@@ -3893,21 +3890,14 @@ pub struct StartingCockpit {
#[must_use]
pub fn starting_cockpit(engine: &Engine) -> Option<StartingCockpit> {
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<ObjectHandle> {
+ // 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)| {
@@ -1208,6 +1228,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 {
format_version: 1,