//! 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, pub(super) 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), ) } }