From da458f8a991644a4033d3b529f23a2dd98a0329d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 16:18:58 +0400 Subject: feat(game): order robots remotely at allied factories --- crates/fparkan-runtime/src/lib.rs | 43 ++++++++++++- crates/fparkan-runtime/src/production.rs | 101 ++++++++++++++++++++++++------- 2 files changed, 120 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index f1c02355..232df8b0 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -71,7 +71,10 @@ pub use mission_events::{ pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep}; pub use player_status::{player_status, PlayerStatus}; pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons}; -pub use production::{factory_schemes, order_factory_robot, order_factory_scheme}; +pub use production::{ + clan_factory_schemes, factory_schemes, order_clan_factory_scheme, order_factory_robot, + order_factory_scheme, +}; pub use projectile::{ apply_projectile_hit, load_player_projectile, projectile_terrain_hit, projectile_unit_hit, PlayerProjectile, ProjectileAssets, ProjectileContact, @@ -18116,6 +18119,44 @@ mod tests { assert!(draft.resource_name_raw.ends_with(b"scr_pre2.dat")); } + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_two_fortresses_orders_remotely_from_the_starting_bunker() { + let vfs: Arc = Arc::new(DirectoryVfs::new(licensed_root("IS"))); + let mut engine = create(EngineServices::new(vfs)).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 hero = starting_cockpit(&engine).unwrap(); + let start = transform_state(&engine.world, hero.handle).unwrap(); + let own = mission_object_handle(&engine, OriginalObjectId(5)).unwrap(); + let enemy = mission_object_handle(&engine, OriginalObjectId(9)).unwrap(); + assert!(factory_schemes(&engine, hero).is_err()); + let choices = clan_factory_schemes(&engine, hero).unwrap(); + assert_eq!(choices.len(), 2); + assert!(choices.iter().all(|(factory, _, _)| *factory == own)); + assert!(order_clan_factory_scheme(&mut engine, hero, enemy, 0, 1).is_err()); + let duration = order_clan_factory_scheme(&mut engine, hero, own, 1, 1).unwrap(); + let result = frame(&mut engine, duration + 1) + .unwrap() + .produced_unit + .unwrap(); + assert_eq!(result.clan, 0); + let draft = &loaded_mission_object_drafts(&engine).unwrap()[result.object_index]; + assert!(draft.resource_name_raw.ends_with(b"scr_pre2.dat")); + assert_eq!(transform_state(&engine.world, hero.handle).unwrap(), start); + } + #[test] #[ignore = "requires licensed corpus"] fn licensed_two_fortresses_ai_defends_when_the_player_approaches() { diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index eeb4d914..6f2f7bbb 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -283,9 +283,32 @@ pub fn factory_schemes( cockpit: StartingCockpit, ) -> Result, EngineError> { let (mission, production) = loaded_production(engine)?; - let (plant, _, _, _) = factory_at_communicator(&engine.world, mission, production, cockpit)?; + let (plant, _) = factory_at_communicator(&engine.world, mission, production, cockpit)?; + Ok(schemes_for_plant(mission, production, plant).collect()) +} + +/// Lists the live factories of the player's clan and matching configured schemes. +pub fn clan_factory_schemes( + engine: &Engine, + cockpit: StartingCockpit, +) -> Result, EngineError> { + let (mission, production) = loaded_production(engine)?; + let owner = player_owner(&engine.world, cockpit)?; + Ok(owned_factories(&engine.world, mission, production, owner) + .flat_map(|(index, factory, _)| { + schemes_for_plant(mission, production, index) + .map(move |(scheme, path)| (factory, scheme, path)) + }) + .collect()) +} + +fn schemes_for_plant<'a>( + mission: &'a LoadedMissionState, + production: &'a Production, + plant: usize, +) -> impl Iterator + 'a { let plant_key = &mission.object_drafts[plant].unit_components[0].resource_raw; - Ok(production + production .schemes .iter() .enumerate() @@ -294,7 +317,6 @@ pub fn factory_schemes( free_duration_ms(plant_key, &draft.unit_components[0].resource_raw)?; Some((index, path.as_str())) }) - .collect()) } /// Orders a particular configured scheme using the current friendly factory. @@ -303,14 +325,31 @@ pub fn order_factory_scheme( cockpit: StartingCockpit, scheme: usize, now_ms: u32, +) -> Result { + let (mission, production) = loaded_production(engine)?; + let (_, plant) = factory_at_communicator(&engine.world, mission, production, cockpit)?; + order_clan_factory_scheme(engine, cockpit, plant, scheme, now_ms) +} + +/// Places an order at a live factory owned by the player's clan. +pub fn order_clan_factory_scheme( + engine: &mut Engine, + cockpit: StartingCockpit, + plant: ObjectHandle, + scheme: usize, + now_ms: u32, ) -> Result { let (Some(mission), Some(production)) = (&mut engine.loaded, &mut engine.production) else { return Err(EngineError::Production( "mission has no prepared robot design; use --build-scheme to choose one".into(), )); }; - let (plant_index, plant, owner, is_free) = - factory_at_communicator(&engine.world, mission, production, cockpit)?; + let owner = player_owner(&engine.world, cockpit)?; + let (plant_index, _, is_free) = owned_factories(&engine.world, mission, production, owner) + .find(|&(_, handle, _)| handle == plant) + .ok_or_else(|| { + EngineError::Production("selected factory no longer belongs to the player".into()) + })?; if production.pending.is_some() { return Err(EngineError::Production( "factory order already in progress".into(), @@ -418,31 +457,47 @@ fn factory_at_communicator( mission: &LoadedMissionState, production: &Production, cockpit: StartingCockpit, -) -> Result<(usize, ObjectHandle, OwnerId, bool), EngineError> { - let owner = identity_metadata(world, cockpit.handle)? - .owner_id - .ok_or_else(|| EngineError::Production("player clan is unavailable".into()))?; +) -> Result<(usize, ObjectHandle), EngineError> { + let owner = player_owner(world, cockpit)?; let position = transform_state(world, cockpit.handle)? .position .map(f32::from_bits); - production - .plants - .iter() - .find_map(|&(index, slots)| { - let plant = handle_by_original_id(world, mission.object_drafts[index].original_id?)?; - (identity_metadata(world, plant).ok()?.owner_id == Some(owner) - && on_communicator( - &mission.mission_assets, - index, - PLANT_CLASS, - transform_state(world, plant).ok()?, - position, - )) - .then_some((index, plant, owner, slots != 0)) + owned_factories(world, mission, production, owner) + .find_map(|(index, plant, _)| { + on_communicator( + &mission.mission_assets, + index, + PLANT_CLASS, + transform_state(world, plant).ok()?, + position, + ) + .then_some((index, plant)) }) .ok_or_else(|| EngineError::Production("stand on a friendly factory communicator".into())) } +fn player_owner(world: &World, cockpit: StartingCockpit) -> Result { + identity_metadata(world, cockpit.handle)? + .owner_id + .ok_or_else(|| EngineError::Production("player clan is unavailable".into())) +} + +fn owned_factories<'a>( + world: &'a World, + mission: &'a LoadedMissionState, + production: &'a Production, + owner: OwnerId, +) -> impl Iterator + 'a { + production.plants.iter().filter_map(move |&(index, slots)| { + let plant = handle_by_original_id(world, mission.object_drafts[index].original_id?)?; + (identity_metadata(world, plant).ok()?.owner_id == Some(owner)).then_some(( + index, + plant, + slots != 0, + )) + }) +} + fn production_error(error: impl std::fmt::Display) -> EngineError { EngineError::Production(error.to_string()) } -- cgit v1.2.3