From aafdd0c398923f51625b632789ff0c7948b006e8 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 16:28:59 +0400 Subject: feat(ui): stop an active strategic factory order --- crates/fparkan-runtime/src/production/strategic.rs | 46 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'crates/fparkan-runtime/src/production') 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 + ); } } -- cgit v1.2.3