aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 11:00:22 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 11:00:22 +0300
commit537c3776c9a7a2e278d81358a3ec1fbba0913201 (patch)
tree8b7253d1d945c51e026e3bcc40b0eb51f2340448 /crates
parentdad38166305413cadae7c6cb1222bb914b15185e (diff)
downloadfparkan-537c3776c9a7a2e278d81358a3ec1fbba0913201.tar.xz
fparkan-537c3776c9a7a2e278d81358a3ec1fbba0913201.zip
feat(runtime): support successive factory robot orders
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/lib.rs36
-rw-r--r--crates/fparkan-runtime/src/production.rs54
2 files changed, 64 insertions, 26 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 2b8279f6..c74707b1 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -1914,7 +1914,12 @@ fn load_mission_with_options(
.take(plan.reserve_per_scheme)
}));
}
- graph_roots.extend(build_scheme.map(|scheme| resource_name(scheme.as_str().as_bytes())));
+ if let Some(scheme) = build_scheme {
+ graph_roots.extend(
+ std::iter::repeat_with(|| resource_name(scheme.as_str().as_bytes()))
+ .take(production::RESERVE_SLOTS),
+ );
+ }
let scoped_graph_roots = match options.asset_scope {
MissionAssetScope::Full => graph_roots.as_slice(),
MissionAssetScope::FirstMeshPreview => graph_roots.get(..1).unwrap_or_default(),
@@ -2397,14 +2402,11 @@ fn load_mission_with_options(
Vec::new()
};
let (autodemo_reinforcements, production) = if build_scheme.is_some() {
- let control = reserve_controls.into_iter().next().ok_or_else(|| {
- EngineError::Production("missing Control table for build scheme".into())
- })?;
(
None,
Some(production::Production::new(
reserve_start,
- control,
+ reserve_controls,
&object_drafts,
)?),
)
@@ -17957,7 +17959,6 @@ mod tests {
.owner_id,
Some(OwnerId(0))
);
- assert!(order_factory_robot(&mut engine, cockpit, 64_001).is_err());
let next = frame(&mut engine, 66_001).unwrap();
assert!(next.produced_unit.is_none());
let event = next.mission_event.unwrap();
@@ -18020,6 +18021,29 @@ mod tests {
.position,
driven.position
);
+ player_pose.position = [391.3_f32, 740.1, 145.12].map(f32::to_bits);
+ set_transform(&mut engine.world, cockpit.handle, player_pose).unwrap();
+ let mut live = vec![robot];
+ let mut now = 68_001;
+ for _ in 1..production::RESERVE_SLOTS {
+ let duration = order_factory_robot(&mut engine, cockpit, now).unwrap();
+ now += duration;
+ let result = frame(&mut engine, now).unwrap();
+ let new = result.produced_unit.expect("another finished robot");
+ assert!(live.iter().all(|unit| unit.object_index != new.object_index
+ && result.snapshot.objects.contains(&unit.handle)));
+ live.push(new);
+ }
+ assert!(order_factory_robot(&mut engine, cockpit, now).is_err());
+ fparkan_world::request_delete(&mut engine.world, robot.handle).unwrap();
+ let duration = order_factory_robot(&mut engine, cockpit, now).unwrap();
+ let replaced = frame(&mut engine, now + duration).unwrap();
+ let replacement = replaced.produced_unit.expect("reused slot");
+ assert_eq!(replacement.object_index, robot.object_index);
+ assert_ne!(replacement.handle, robot.handle);
+ assert!(live[1..]
+ .iter()
+ .all(|unit| replaced.snapshot.objects.contains(&unit.handle)));
}
#[test]
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs
index b944089a..1e09250c 100644
--- a/crates/fparkan-runtime/src/production.rs
+++ b/crates/fparkan-runtime/src/production.rs
@@ -10,15 +10,20 @@ use fparkan_world::{
handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId,
};
+// shortcut: GPU geometry is prepared for a bounded number of live builds.
+// Lift the cap when the renderer can instantiate new roots during a mission.
+pub(super) const RESERVE_SLOTS: usize = 8;
+
pub(super) struct Production {
- reserve: usize,
- control: MissionControlDeviceTableState,
+ reserve_start: usize,
+ controls: Vec<MissionControlDeviceTableState>,
free_units: Vec<(usize, u32)>,
pending: Option<Order>,
rng: u32,
}
struct Order {
+ slot: usize,
plant: ObjectHandle,
plant_index: usize,
owner: OwnerId,
@@ -28,11 +33,18 @@ struct Order {
impl Production {
pub(super) fn new(
- reserve: usize,
- control: MissionControlDeviceTableState,
+ reserve_start: usize,
+ controls: Vec<MissionControlDeviceTableState>,
drafts: &[MissionObjectDraft],
) -> Result<Self, EngineError> {
- if !drafts[reserve]
+ if controls.len() != RESERVE_SLOTS
+ || drafts.len().checked_sub(reserve_start) != Some(controls.len())
+ {
+ return Err(EngineError::Production(
+ "missing prepared build scheme slots".into(),
+ ));
+ }
+ if !drafts[reserve_start]
.unit_dat_header_flags
.is_some_and(|class| class & MOBILE_UNIT != 0 && class != super::HERO_UNIT_CLASS)
{
@@ -40,7 +52,7 @@ impl Production {
"build scheme must be a mobile Unit DAT".into(),
));
}
- let free_units = drafts[..reserve]
+ let free_units = drafts[..reserve_start]
.iter()
.enumerate()
.filter(|(_, draft)| draft.unit_dat_header_flags == Some(PLANT_CLASS))
@@ -54,8 +66,8 @@ impl Production {
})
.collect();
Ok(Self {
- reserve,
- control,
+ reserve_start,
+ controls,
free_units,
pending: None,
rng: 1,
@@ -96,10 +108,10 @@ impl Production {
let handle = spawn_prepared_unit(
world,
mission,
- self.reserve,
+ order.slot,
order.owner.0,
position,
- self.control.clone(),
+ self.controls[order.slot - self.reserve_start].clone(),
)?;
if let Some((_, count)) = self
.free_units
@@ -109,7 +121,7 @@ impl Production {
*count -= 1;
}
Ok(Some(SpawnedUnit {
- object_index: self.reserve,
+ object_index: order.slot,
handle,
clan: order.owner.0,
}))
@@ -138,15 +150,16 @@ pub fn order_factory_robot(
"factory order already in progress".into(),
));
}
- let draft = &mission.object_drafts[production.reserve];
- if draft
- .original_id
- .is_some_and(|id| handle_by_original_id(&engine.world, id).is_some())
- {
- return Err(EngineError::Production(
- "the previous robot is still active".into(),
- ));
- }
+ let slot = (production.reserve_start..production.reserve_start + production.controls.len())
+ .find(|&index| {
+ mission.object_drafts[index]
+ .original_id
+ .is_some_and(|id| handle_by_original_id(&engine.world, id).is_none())
+ })
+ .ok_or_else(|| {
+ EngineError::Production("all prepared factory robot slots are occupied".into())
+ })?;
+ let draft = &mission.object_drafts[slot];
let player = identity_metadata(&engine.world, cockpit.handle)?;
let owner = player
.owner_id
@@ -182,6 +195,7 @@ pub fn order_factory_robot(
EngineError::Production("scheme size is unsupported by this factory".into())
})?;
production.pending = Some(Order {
+ slot,
plant,
plant_index,
owner,