diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 15:28:59 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 15:28:59 +0300 |
| commit | aafdd0c398923f51625b632789ff0c7948b006e8 (patch) | |
| tree | 34717d5b9750e5d0ccf7e11ff91de66b09cb762c /crates/fparkan-runtime/src/production | |
| parent | cc6bbdfa4aebf844ec7be2e8eb01b7fd1c69d24b (diff) | |
| download | fparkan-aafdd0c398923f51625b632789ff0c7948b006e8.tar.xz fparkan-aafdd0c398923f51625b632789ff0c7948b006e8.zip | |
feat(ui): stop an active strategic factory order
Diffstat (limited to 'crates/fparkan-runtime/src/production')
| -rw-r--r-- | crates/fparkan-runtime/src/production/strategic.rs | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/production/strategic.rs b/crates/fparkan-runtime/src/production/strategic.rs index a6dc21d3..c39566fe 100644 --- a/crates/fparkan-runtime/src/production/strategic.rs +++ b/crates/fparkan-runtime/src/production/strategic.rs @@ -1,10 +1,40 @@ -use super::{loaded_production, owned_factories, player_owner, recent_schemes_for_plant}; +use super::{ + clear_factory_demand, loaded_production, owned_factories, player_owner, production_error, + recent_schemes_for_plant, +}; use crate::{ factory_project_root, warbot_stats::{stats_from_table, WarbotStats}, - Engine, StartingCockpit, + Engine, EngineError, StartingCockpit, }; -use fparkan_world::{registration_sequence, ObjectHandle}; +use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle}; + +/// Stops an order that has not released a robot and frees its reserved slot. +pub fn cancel_clan_factory_order( + engine: &mut Engine, + cockpit: StartingCockpit, + plant: ObjectHandle, +) -> Result<(), EngineError> { + let owner = player_owner(&engine.world, cockpit)?; + if !identity_metadata(&engine.world, plant).is_ok_and(|meta| meta.owner_id == Some(owner)) { + return Err(production_error( + "selected factory no longer belongs to the player's clan", + )); + } + let (Some(mission), Some(production)) = (&mut engine.loaded, &mut engine.production) else { + return Err(production_error("mission has no factory production")); + }; + let index = production + .pending + .iter() + .position(|order| order.plant == plant && order.owner == owner) + .ok_or_else(|| production_error("factory has no active order to cancel"))?; + if production.pending[index].paid.is_some() { + clear_factory_demand(mission, plant)?.current_ore_bits = 0; + } + production.pending.remove(index); + Ok(()) +} #[derive(Clone, Debug, PartialEq, Eq)] pub struct FactoryPanelPlant { @@ -170,5 +200,15 @@ mod tests { let pending = factory_panel_plants(&engine, cockpit, 100 + duration / 2); assert_eq!(pending[0].free_minds, 2); assert_eq!(pending[0].progress, Some(50)); + + cancel_clan_factory_order(&mut engine, cockpit, plant.handle).unwrap(); + let idle = factory_panel_plants(&engine, cockpit, 100 + duration / 2); + assert_eq!(idle[0], *plant); + assert!(cancel_clan_factory_order(&mut engine, cockpit, plant.handle).is_err()); + assert_eq!( + order_clan_factory_scheme(&mut engine, cockpit, plant.handle, scheme, 100 + duration) + .unwrap(), + duration + ); } } |
