aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 15:18:58 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 15:18:58 +0300
commitda458f8a991644a4033d3b529f23a2dd98a0329d (patch)
treeed30560fc3326c6c96b99ecb4329405a04963609 /crates
parent051dfd8f2c8254b4d519c6d806471b859245bbd7 (diff)
downloadfparkan-da458f8a991644a4033d3b529f23a2dd98a0329d.tar.xz
fparkan-da458f8a991644a4033d3b529f23a2dd98a0329d.zip
feat(game): order robots remotely at allied factories
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/lib.rs43
-rw-r--r--crates/fparkan-runtime/src/production.rs101
2 files changed, 120 insertions, 24 deletions
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,
@@ -18118,6 +18121,44 @@ mod tests {
#[test]
#[ignore = "requires licensed corpus"]
+ fn licensed_two_fortresses_orders_remotely_from_the_starting_bunker() {
+ let vfs: Arc<dyn Vfs> = 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() {
let vfs: Arc<dyn Vfs> = Arc::new(DirectoryVfs::new(licensed_root("IS")));
let mut engine = create(EngineServices::new(vfs)).unwrap();
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<Vec<(usize, &str)>, 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<Vec<(ObjectHandle, usize, &str)>, 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<Item = (usize, &'a str)> + '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.
@@ -304,13 +326,30 @@ pub fn order_factory_scheme(
scheme: usize,
now_ms: u32,
) -> Result<u32, EngineError> {
+ 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<u32, EngineError> {
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<OwnerId, EngineError> {
+ 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<Item = (usize, ObjectHandle, bool)> + '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())
}