diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-02 15:56:29 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-02 15:56:29 +0300 |
| commit | 2918dbe5c591ab6b1d4a76512b7fc8f258997558 (patch) | |
| tree | eba8d582a1357a72ed3895c2ef285ef28b7d59cb /crates/fparkan-runtime/src/production/batch.rs | |
| parent | fd42fb7e4fa27d2fe03ee8b6532949b7c1b13ace (diff) | |
| download | fparkan-2918dbe5c591ab6b1d4a76512b7fc8f258997558.tar.xz fparkan-2918dbe5c591ab6b1d4a76512b7fc8f258997558.zip | |
feat(ui): run repeating strategic factory orders
Diffstat (limited to 'crates/fparkan-runtime/src/production/batch.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/production/batch.rs | 126 |
1 files changed, 126 insertions, 0 deletions
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<u32, EngineError> { + 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<Option<SpawnedUnit>, 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), + ) + } +} |
