aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 15:56:29 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 15:56:29 +0300
commit2918dbe5c591ab6b1d4a76512b7fc8f258997558 (patch)
treeeba8d582a1357a72ed3895c2ef285ef28b7d59cb /crates/fparkan-runtime
parentfd42fb7e4fa27d2fe03ee8b6532949b7c1b13ace (diff)
downloadfparkan-2918dbe5c591ab6b1d4a76512b7fc8f258997558.tar.xz
fparkan-2918dbe5c591ab6b1d4a76512b7fc8f258997558.zip
feat(ui): run repeating strategic factory orders
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/lib.rs18
-rw-r--r--crates/fparkan-runtime/src/production.rs55
-rw-r--r--crates/fparkan-runtime/src/production/batch.rs126
-rw-r--r--crates/fparkan-runtime/src/production/strategic.rs106
4 files changed, 254 insertions, 51 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index c72f5552..9c90edfa 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -216,7 +216,8 @@ pub use production::{
factory_part_variants, factory_project_components, factory_project_root,
factory_project_schemes, factory_saved_schemes, factory_schemes, order_clan_factory_scheme,
order_factory_design, order_factory_robot, order_factory_scheme,
- remember_strategic_factory_project, FactoryPanelPlant, FactoryPanelProject,
+ remember_strategic_factory_project, start_clan_factory_batch, FactoryPanelPlant,
+ FactoryPanelProject,
};
pub use projectile::{
apply_projectile_contact, apply_projectile_hit, apply_projectile_impact,
@@ -3060,18 +3061,9 @@ pub fn step_headless(
}
}
let (produced_unit, production_failure) =
- if let (Some(production), Some(mission)) = (&mut engine.production, &mut engine.loaded) {
- match production.tick(
- &mut engine.world,
- mission,
- queue_time_ms,
- &mut engine.behavior_rng_state,
- ) {
- Ok(unit) => (unit, None),
- Err(error) => (None, Some(error.to_string())),
- }
- } else {
- (None, None)
+ match production::tick_production(engine, queue_time_ms) {
+ Ok(unit) => (unit, None),
+ Err(error) => (None, Some(error.to_string())),
};
if let (Some(combat), Some(unit)) = (&mut engine.autodemo_combat, produced_unit) {
combat.activate(&engine.world, unit.object_index, unit.handle);
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,
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),
+ )
+ }
+}
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<u8>,
+ pub batch: bool,
pub projects: Vec<usize>,
}
@@ -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);
}
}