diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 15:42:10 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 15:42:10 +0300 |
| commit | fd42fb7e4fa27d2fe03ee8b6532949b7c1b13ace (patch) | |
| tree | 2c6fe8e6441566fd5170caa216fcf224383e9a4a /crates/fparkan-runtime | |
| parent | aafdd0c398923f51625b632789ff0c7948b006e8 (diff) | |
| download | fparkan-fd42fb7e4fa27d2fe03ee8b6532949b7c1b13ace.tar.xz fparkan-fd42fb7e4fa27d2fe03ee8b6532949b7c1b13ace.zip | |
feat(ui): transfer designer projects to strategic factories
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 26 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/production/strategic.rs | 46 |
3 files changed, 66 insertions, 10 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index f073672e..c72f5552 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -215,8 +215,8 @@ pub use production::{ cancel_clan_factory_order, clan_factory_schemes, factory_panel_plants, factory_panel_project, factory_part_variants, factory_project_components, factory_project_root, factory_project_schemes, factory_saved_schemes, factory_schemes, order_clan_factory_scheme, - order_factory_design, order_factory_robot, order_factory_scheme, FactoryPanelPlant, - FactoryPanelProject, + order_factory_design, order_factory_robot, order_factory_scheme, + remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject, }; pub use projectile::{ apply_projectile_contact, apply_projectile_hit, apply_projectile_impact, diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index c6bad31c..8c3b2c13 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -26,8 +26,8 @@ use std::sync::Arc; mod strategic; pub use strategic::{ - cancel_clan_factory_order, factory_panel_plants, factory_panel_project, FactoryPanelPlant, - FactoryPanelProject, + cancel_clan_factory_order, factory_panel_plants, factory_panel_project, + remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject, }; // shortcut: GPU geometry is prepared for a bounded number of live builds per scheme. @@ -47,6 +47,7 @@ pub(super) struct Production { ai_schemes: Vec<crate::clan_factory_schemes::ScriptDesign>, controls: std::collections::BTreeMap<usize, MissionControlDeviceTableState>, plants: Vec<(usize, u32)>, + transferred_projects: std::collections::BTreeMap<(usize, u16), Vec<usize>>, pending: VecDeque<Order>, clock_ticks: std::collections::BTreeMap<(u16, u32), u64>, buildings: BuildingFootprints, @@ -185,6 +186,7 @@ impl Production { ai_schemes, controls: (reserve_start..).zip(controls).collect(), plants: Vec::new(), + transferred_projects: Default::default(), pending: VecDeque::new(), clock_ticks: Default::default(), buildings: BuildingFootprints::default(), @@ -434,7 +436,8 @@ pub fn factory_schemes( ) -> Result<Vec<(usize, &str)>, EngineError> { let (mission, production) = loaded_production(engine)?; let (plant, _) = factory_at_communicator(&engine.world, mission, production, cockpit)?; - Ok(recent_schemes_for_plant(mission, production, plant)) + let owner = player_owner(&engine.world, cockpit)?; + Ok(recent_schemes_for_plant(mission, production, plant, owner)) } /// Lists the live factories of the player's clan and matching configured schemes. @@ -446,7 +449,7 @@ pub fn clan_factory_schemes( let owner = player_owner(&engine.world, cockpit)?; Ok(owned_factories(&engine.world, mission, production, owner) .flat_map(|(index, factory, _)| { - recent_schemes_for_plant(mission, production, index) + recent_schemes_for_plant(mission, production, index, owner) .into_iter() .map(move |(scheme, path)| (factory, scheme, path)) }) @@ -534,9 +537,20 @@ fn recent_schemes_for_plant<'a>( mission: &'a LoadedMissionState, production: &'a Production, plant: usize, + owner: OwnerId, ) -> Vec<(usize, &'a str)> { - let mut recent = schemes_for_plant(mission, production, plant) - .filter(|&(scheme, _)| scheme < production.player_scheme_count) + let history = production.transferred_projects.get(&(plant, owner.0)); + let mut recent = history + .into_iter() + .flatten() + .map(|&id| (id, production.schemes[id].as_str())) + .chain( + schemes_for_plant(mission, production, plant).filter(|&(scheme, _)| { + scheme < production.player_scheme_count + && history.is_none_or(|history| !history.contains(&scheme)) + }), + ) + .take(fparkan_assets::factory_projects::PROJECT_SLOTS) .collect::<Vec<_>>(); if recent.is_empty() { recent.extend( diff --git a/crates/fparkan-runtime/src/production/strategic.rs b/crates/fparkan-runtime/src/production/strategic.rs index c39566fe..cbbafee0 100644 --- a/crates/fparkan-runtime/src/production/strategic.rs +++ b/crates/fparkan-runtime/src/production/strategic.rs @@ -1,6 +1,6 @@ use super::{ clear_factory_demand, loaded_production, owned_factories, player_owner, production_error, - recent_schemes_for_plant, + recent_schemes_for_plant, schemes_for_plant, }; use crate::{ factory_project_root, @@ -36,6 +36,34 @@ pub fn cancel_clan_factory_order( Ok(()) } +/// Places a compatible prepared project in the selected factory's recent list. +pub fn remember_strategic_factory_project( + engine: &mut Engine, + cockpit: StartingCockpit, + plant: ObjectHandle, + scheme: usize, +) -> Result<(), EngineError> { + let index = super::player_factory_index(engine, cockpit, Some(plant))?; + let owner = player_owner(&engine.world, cockpit)?; + let (mission, production) = loaded_production(engine)?; + if !production.player_project(scheme) + || !schemes_for_plant(mission, production, index).any(|(id, _)| id == scheme) + { + return Err(production_error( + "project is incompatible with the selected factory", + )); + } + let production = engine.production.as_mut().expect("loaded production"); + let history = production + .transferred_projects + .entry((index, owner.0)) + .or_default(); + history.retain(|&id| id != scheme); + history.insert(0, scheme); + history.truncate(fparkan_assets::factory_projects::PROJECT_SLOTS); + Ok(()) +} + #[derive(Clone, Debug, PartialEq, Eq)] pub struct FactoryPanelPlant { pub handle: ObjectHandle, @@ -97,7 +125,7 @@ pub fn factory_panel_plants( }); (time.min(collected) * 100.0).round_ties_even() as u8 }); - let projects = recent_schemes_for_plant(mission, production, index) + let projects = recent_schemes_for_plant(mission, production, index, owner) .into_iter() .map(|(id, _)| id) .collect(); @@ -210,5 +238,19 @@ mod tests { .unwrap(), duration ); + let other = plant.projects[1]; + remember_strategic_factory_project(&mut engine, cockpit, plant.handle, other).unwrap(); + let after_transfer = factory_panel_plants(&engine, cockpit, 100 + duration); + assert_eq!(after_transfer[0].projects, [other, scheme]); + assert_eq!(after_transfer[0].free_minds, 2); + assert!(engine + .production + .as_ref() + .unwrap() + .pending + .iter() + .all(|order| { + order.slot == engine.production.as_ref().unwrap().slot_starts[scheme] + })); } } |
