From e8e55549d6bd984a01c09eb49882bf3f70090633 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 11:04:55 +0400 Subject: feat(runtime): build a free robot at a captured factory --- crates/fparkan-runtime/src/autodemo.rs | 93 +++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 36 deletions(-) (limited to 'crates/fparkan-runtime/src/autodemo.rs') diff --git a/crates/fparkan-runtime/src/autodemo.rs b/crates/fparkan-runtime/src/autodemo.rs index cd9b14cb..15338b0f 100644 --- a/crates/fparkan-runtime/src/autodemo.rs +++ b/crates/fparkan-runtime/src/autodemo.rs @@ -16,7 +16,7 @@ use std::sync::Arc; const PITCH: f32 = 1.25; const YAW: f32 = std::f32::consts::FRAC_PI_2; const FOLLOW_DISTANCE: f32 = 30.0; -const PLANT_CLASS: u32 = 0x8000_0010; +pub(super) const PLANT_CLASS: u32 = 0x8000_0010; pub(super) const MOBILE_UNIT: u32 = 0x0100_0000; pub(super) fn read_config( @@ -49,7 +49,7 @@ pub(super) struct LiveReinforcements { /// A reserve model activated with a newly registered world object. #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct AutoDemoReinforcement { +pub struct SpawnedUnit { /// Prepared scene object index. pub object_index: usize, /// New world handle. @@ -58,6 +58,9 @@ pub struct AutoDemoReinforcement { pub clan: u16, } +/// A unit created by AutoDemo's replacement policy. +pub type AutoDemoReinforcement = SpawnedUnit; + impl LiveReinforcements { pub(super) fn new( plan: Reinforcements, @@ -157,42 +160,14 @@ impl LiveReinforcements { &mission.terrain, &mut self.rng, )?; - let draft = &mission.object_drafts[index]; - let handle = construct_replacement( + let handle = spawn_prepared_unit( world, - ObjectDraft { - original_id: draft.original_id, - }, + mission, + index, + clan, + position, + self.controls[index - self.reserve_start].clone(), )?; - let pose = TransformState { - position: position.map(f32::to_bits), - orientation: draft.orientation_raw.map(f32::to_bits), - scale: draft.scale.map(f32::to_bits), - }; - if let Err(error) = set_transform(world, handle, pose) - .and_then(|()| set_owner(world, handle, Some(OwnerId(clan)))) - .and_then(|()| register_object(world, handle)) - { - fparkan_world::request_delete(world, handle)?; - return Err(error.into()); - } - mission - .control_device_tables - .retain(|table| transform_state(world, table.handle).is_ok()); - mission - .behavior_control_groupings - .retain(|group| transform_state(world, group.handle).is_ok()); - let mut table = self.controls[index - self.reserve_start].clone(); - table.handle = handle; - mission - .behavior_control_groupings - .push(super::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("mobile reserve"), - ); if let Some(patrol) = &mut mission.unit_patrol { patrol.activate(world, handle); } @@ -200,6 +175,52 @@ impl LiveReinforcements { } } +pub(super) fn spawn_prepared_unit( + world: &mut World, + mission: &mut super::LoadedMissionState, + index: usize, + clan: u16, + position: [f32; 3], + mut table: MissionControlDeviceTableState, +) -> Result { + let draft = &mission.object_drafts[index]; + let handle = construct_replacement( + world, + ObjectDraft { + original_id: draft.original_id, + }, + )?; + let pose = TransformState { + position: position.map(f32::to_bits), + orientation: draft.orientation_raw.map(f32::to_bits), + scale: draft.scale.map(f32::to_bits), + }; + if let Err(error) = set_transform(world, handle, pose) + .and_then(|()| set_owner(world, handle, Some(OwnerId(clan)))) + .and_then(|()| register_object(world, handle)) + { + fparkan_world::request_delete(world, handle)?; + return Err(error.into()); + } + mission + .control_device_tables + .retain(|table| transform_state(world, table.handle).is_ok()); + mission + .behavior_control_groupings + .retain(|group| transform_state(world, group.handle).is_ok()); + table.handle = handle; + mission + .behavior_control_groupings + .push(super::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("mobile reserve"), + ); + Ok(handle) +} + pub(super) fn plan_reinforcements( vfs: &Arc, mission: &super::MissionDocument, -- cgit v1.2.3