diff options
Diffstat (limited to 'crates/fparkan-runtime/src/production.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/production.rs | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index 8c3b2c13..50acdd94 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -24,7 +24,10 @@ use fparkan_world::{ use std::collections::VecDeque; use std::sync::Arc; +mod batch; mod strategic; +pub use batch::start_clan_factory_batch; +pub(super) use batch::tick_production; pub use strategic::{ cancel_clan_factory_order, factory_panel_plants, factory_panel_project, remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject, @@ -49,6 +52,7 @@ pub(super) struct Production { plants: Vec<(usize, u32)>, transferred_projects: std::collections::BTreeMap<(usize, u16), Vec<usize>>, pending: VecDeque<Order>, + batches: Vec<batch::BatchOrder>, clock_ticks: std::collections::BTreeMap<(u16, u32), u64>, buildings: BuildingFootprints, rng: u32, @@ -188,6 +192,7 @@ impl Production { plants: Vec::new(), transferred_projects: Default::default(), pending: VecDeque::new(), + batches: Vec::new(), clock_ticks: Default::default(), buildings: BuildingFootprints::default(), rng: 1, @@ -280,8 +285,13 @@ impl Production { eprintln!("sim_coverage script_factory_clock_approx=1 clan={} plant={} (active task timer; idle owner phases remain open)", order.owner.0, order.plant.slot); } } - if let Some(unit) = self.advance_order(world, mission, now_ms, &mut order)? { - return Ok(Some(unit)); + match self.advance_order(world, mission, now_ms, &mut order) { + Ok(Some(unit)) => return Ok(Some(unit)), + Ok(None) => {} + Err(error) => { + self.batches.retain(|batch| batch.plant != order.plant); + return Err(error); + } } if owned { self.pending.push_back(order); @@ -622,6 +632,13 @@ pub fn order_factory_design( { return Err(production_error("unknown factory scheme")); } + if production + .batches + .iter() + .any(|batch| batch.plant == plant && batch.owner == owner) + { + return Err(production_error("factory batch is already in progress")); + } order_owned_factory(engine, owner, plant, scheme, now_ms, changes) } @@ -650,18 +667,12 @@ fn order_owned_factory( "factory order already in progress".into(), )); } - if scheme >= production.schemes.len() { - return Err(EngineError::Production("unknown factory scheme".into())); - } - let slot_start = production.slot_starts[scheme]; - let free_slot = |index: usize| { - mission.object_drafts[index] - .original_id - .is_some_and(|id| handle_by_original_id(&engine.world, id).is_none()) - && !production.pending.iter().any(|order| order.slot == index) - }; - let slot = (slot_start..slot_start + RESERVE_SLOTS) - .find(|&index| free_slot(index)) + let slot_start = *production + .slot_starts + .get(scheme) + .ok_or_else(|| production_error("unknown factory scheme"))?; + let slot = production + .vacant_slot(&engine.world, mission, slot_start) .ok_or_else(|| { EngineError::Production("all prepared factory robot slots are occupied".into()) })?; @@ -747,6 +758,22 @@ fn order_owned_factory( Ok(duration_ms) } +impl Production { + fn vacant_slot( + &self, + world: &World, + mission: &LoadedMissionState, + start: usize, + ) -> Option<usize> { + (start..start + RESERVE_SLOTS).find(|&index| { + mission.object_drafts[index] + .original_id + .is_some_and(|id| handle_by_original_id(world, id).is_none()) + && !self.pending.iter().any(|order| order.slot == index) + }) + } +} + pub(super) fn order_script_factory( engine: &mut Engine, owner: OwnerId, |
