1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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,
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<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),
)
}
}
|