From 2918dbe5c591ab6b1d4a76512b7fc8f258997558 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 16:56:29 +0400 Subject: feat(ui): run repeating strategic factory orders --- crates/fparkan-runtime/src/production/batch.rs | 126 +++++++++++++++++++++ crates/fparkan-runtime/src/production/strategic.rs | 106 +++++++++++++---- 2 files changed, 208 insertions(+), 24 deletions(-) create mode 100644 crates/fparkan-runtime/src/production/batch.rs (limited to 'crates/fparkan-runtime/src/production') diff --git a/crates/fparkan-runtime/src/production/batch.rs b/crates/fparkan-runtime/src/production/batch.rs new file mode 100644 index 00000000..c0fb2690 --- /dev/null +++ b/crates/fparkan-runtime/src/production/batch.rs @@ -0,0 +1,126 @@ +//! Repeated player orders keep their scheme after the current assembly has released. + +use super::{ + loaded_production, order_clan_factory_scheme, order_owned_factory, player_owner, + production_error, SpawnedUnit, +}; +use crate::{Engine, EngineError, LoadedMissionState, StartingCockpit, World}; +use fparkan_world::{identity_metadata, ObjectHandle, OwnerId}; + +pub(super) struct BatchOrder { + pub(super) plant: ObjectHandle, + pub(super) owner: OwnerId, + scheme: usize, +} + +/// Starts a repeating order at an idle factory and reserves one clan slot. +pub fn start_clan_factory_batch( + engine: &mut Engine, + cockpit: StartingCockpit, + plant: ObjectHandle, + scheme: usize, + now_ms: u32, +) -> Result { + let owner = player_owner(&engine.world, cockpit)?; + let (mission, production) = loaded_production(engine)?; + if factory_depleted(mission, plant) { + return Err(production_error("selected factory is depleted")); + } + if production.free_minds(&engine.world, mission, owner) == 0 { + return Err(production_error("clan has no free slots for new units")); + } + let duration = order_clan_factory_scheme(engine, cockpit, plant, scheme, now_ms)?; + engine + .production + .as_mut() + .expect("factory accepted order") + .batches + .push(BatchOrder { + plant, + owner, + scheme, + }); + Ok(duration) +} + +pub(crate) fn tick_production( + engine: &mut Engine, + now_ms: u32, +) -> Result, EngineError> { + let (Some(production), Some(mission)) = (&mut engine.production, &mut engine.loaded) else { + return Ok(None); + }; + let unit = production.tick( + &mut engine.world, + mission, + now_ms, + &mut engine.behavior_rng_state, + )?; + let mut batches = std::mem::take(&mut production.batches); + batches.retain(|batch| batch.advance(engine, now_ms)); + engine + .production + .as_mut() + .expect("loaded production") + .batches = batches; + Ok(unit) +} + +impl BatchOrder { + fn advance(&self, engine: &mut Engine, now_ms: u32) -> bool { + let Ok((mission, production)) = loaded_production(engine) else { + return false; + }; + if !identity_metadata(&engine.world, self.plant) + .is_ok_and(|meta| meta.owner_id == Some(self.owner)) + || factory_depleted(mission, self.plant) + { + return false; + } + if production.is_building_at(self.plant, self.owner) + || production.free_minds(&engine.world, mission, self.owner) == 0 + || production + .vacant_slot(&engine.world, mission, production.slot_starts[self.scheme]) + .is_none() + { + return true; + } + match order_owned_factory(engine, self.owner, self.plant, self.scheme, now_ms, &[]) { + Ok(_) => true, + Err(error) => { + eprintln!( + "factory_batch_stopped plant={} reason={error}", + self.plant.slot + ); + false + } + } + } +} + +pub(super) fn factory_depleted(mission: &LoadedMissionState, plant: ObjectHandle) -> bool { + mission + .control_device_tables + .iter() + .any(|table| table.handle == plant && table.node_provider_depletion_marked) +} + +impl super::Production { + pub(super) fn free_minds( + &self, + world: &World, + mission: &LoadedMissionState, + owner: OwnerId, + ) -> u32 { + let capacity = mission + .ai_startup_states + .iter() + .find(|state| state.clan_index == usize::from(owner.0)) + .map_or(0, |state| state.record_slots.len()); + crate::clan_minds::free_slots( + capacity, + mission.mission_events.clan_classes(world, owner), + self.reserved_minds(world, owner), + ) + } +} diff --git a/crates/fparkan-runtime/src/production/strategic.rs b/crates/fparkan-runtime/src/production/strategic.rs index cbbafee0..5b4e08f9 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, schemes_for_plant, + batch::factory_depleted, clear_factory_demand, loaded_production, owned_factories, + player_owner, production_error, recent_schemes_for_plant, schemes_for_plant, }; use crate::{ factory_project_root, @@ -9,7 +9,7 @@ use crate::{ }; use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle}; -/// Stops an order that has not released a robot and frees its reserved slot. +/// Stops the active order and ends any repeating production at the factory. pub fn cancel_clan_factory_order( engine: &mut Engine, cockpit: StartingCockpit, @@ -24,15 +24,26 @@ pub fn cancel_clan_factory_order( let (Some(mission), Some(production)) = (&mut engine.loaded, &mut engine.production) else { return Err(production_error("mission has no factory production")); }; - let index = production + let order = 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; + .position(|order| order.plant == plant && order.owner == owner); + let batch = production + .batches + .iter() + .position(|batch| batch.plant == plant && batch.owner == owner); + if order.is_none() && batch.is_none() { + return Err(production_error("factory has no active order to cancel")); + } + if let Some(index) = order { + if production.pending[index].paid.is_some() { + clear_factory_demand(mission, plant)?.current_ore_bits = 0; + } + production.pending.remove(index); + } + if let Some(index) = batch { + production.batches.remove(index); } - production.pending.remove(index); Ok(()) } @@ -70,6 +81,7 @@ pub struct FactoryPanelPlant { pub label_id: u32, pub free_minds: u32, pub progress: Option, + pub batch: bool, pub projects: Vec, } @@ -84,23 +96,10 @@ pub fn factory_panel_plants( let Ok(owner) = player_owner(&engine.world, cockpit) else { return Vec::new(); }; - let capacity = mission - .ai_startup_states - .iter() - .find(|state| state.clan_index == usize::from(owner.0)) - .map_or(0, |state| state.record_slots.len()); - let free_minds = crate::clan_minds::free_slots( - capacity, - mission.mission_events.clan_classes(&engine.world, owner), - production.reserved_minds(&engine.world, owner), - ); + let free_minds = production.free_minds(&engine.world, mission, owner); let mut plants: Vec<_> = owned_factories(&engine.world, mission, production, owner) .filter_map(|(index, handle, _)| { - if mission - .control_device_tables - .iter() - .any(|table| table.handle == handle && table.node_provider_depletion_marked) - { + if factory_depleted(mission, handle) { return None; } let sequence = registration_sequence(&engine.world, handle).ok()??; @@ -136,6 +135,10 @@ pub fn factory_panel_plants( label_id: label, free_minds, progress, + batch: production + .batches + .iter() + .any(|batch| batch.plant == handle && batch.owner == owner), projects, }, )) @@ -252,5 +255,60 @@ mod tests { .all(|order| { order.slot == engine.production.as_ref().unwrap().slot_starts[scheme] })); + + cancel_clan_factory_order(&mut engine, cockpit, plant.handle).unwrap(); + let states = &mut engine.loaded.as_mut().unwrap().ai_startup_states; + let ai_index = states + .iter() + .position(|state| state.clan_index == 0) + .unwrap(); + let startup = &mut states[ai_index]; + let capacity = startup.record_slots.len(); + let extra_slots = startup.record_slots.split_off(capacity - 2); + let started = 100 + duration; + let time = super::super::start_clan_factory_batch( + &mut engine, + cockpit, + plant.handle, + scheme, + started, + ) + .unwrap(); + assert!(factory_panel_plants(&engine, cockpit, started)[0].batch); + assert!( + order_clan_factory_scheme(&mut engine, cockpit, plant.handle, other, started).is_err() + ); + + let completed_at = (started..=started + time + 1_000) + .step_by(100) + .find(|&now| { + super::super::tick_production(&mut engine, now) + .unwrap() + .is_some() + }) + .expect("initial batch order releases its machine"); + let full = &factory_panel_plants(&engine, cockpit, completed_at)[0]; + assert!(full.batch); + assert_eq!(full.free_minds, 0); + assert_eq!(full.progress, None); + + engine.loaded.as_mut().unwrap().ai_startup_states[ai_index] + .record_slots + .extend(extra_slots); + super::super::tick_production(&mut engine, completed_at + 100).unwrap(); + let queue = engine.production.as_ref().unwrap(); + assert_eq!(queue.pending.len(), 1); + let start_slot = queue.slot_starts[scheme]; + assert!( + (start_slot..start_slot + super::super::RESERVE_SLOTS).contains(&queue.pending[0].slot) + ); + assert_eq!(queue.pending[0].owner, fparkan_world::OwnerId(0)); + assert!(factory_panel_plants(&engine, cockpit, completed_at + 100)[0].batch); + + cancel_clan_factory_order(&mut engine, cockpit, plant.handle).unwrap(); + super::super::tick_production(&mut engine, completed_at + 200).unwrap(); + let stopped = &factory_panel_plants(&engine, cockpit, completed_at + 200)[0]; + assert!(!stopped.batch); + assert_eq!(stopped.progress, None); } } -- cgit v1.2.3