aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/player_status.rs10
-rw-r--r--crates/fparkan-runtime/src/production.rs66
3 files changed, 61 insertions, 17 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 1c1d61c3..480149f6 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -76,7 +76,7 @@ pub use mission_events::{
pub use player_motion::{
plan_player_motion, plan_player_motion_with_collision, PlayerMotion, PlayerMotionStep,
};
-pub use player_status::{player_status, PlayerStatus};
+pub use player_status::{is_player_clan, player_status, PlayerStatus};
pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons};
pub use production::{
clan_factory_schemes, factory_schemes, order_clan_factory_scheme, order_factory_robot,
diff --git a/crates/fparkan-runtime/src/player_status.rs b/crates/fparkan-runtime/src/player_status.rs
index 637f5254..882ccf33 100644
--- a/crates/fparkan-runtime/src/player_status.rs
+++ b/crates/fparkan-runtime/src/player_status.rs
@@ -1,6 +1,16 @@
use crate::{Engine, MissionControlNodeProviderTableStatus};
use fparkan_world::{handle_by_original_id, identity_metadata, ObjectHandle};
+/// Reports whether this clan is controlled by the player.
+#[must_use]
+pub fn is_player_clan(engine: &Engine, clan: u16) -> bool {
+ engine
+ .loaded
+ .as_ref()
+ .and_then(|mission| mission.mission.clans.get(usize::from(clan)))
+ .is_some_and(|clan| clan.mode == 1)
+}
+
/// Current values for the player's controlled unit and clan.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlayerStatus {
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs
index 434365dc..315dbb5a 100644
--- a/crates/fparkan-runtime/src/production.rs
+++ b/crates/fparkan-runtime/src/production.rs
@@ -16,6 +16,7 @@ use fparkan_prototype::{unit_component_tree, UnitComponentRecord};
use fparkan_world::{
handle_by_original_id, identity_metadata, transform_state, ObjectHandle, OwnerId,
};
+use std::collections::VecDeque;
// 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.
@@ -68,7 +69,7 @@ pub(super) struct Production {
schemes: Vec<NormalizedPath>,
controls: Vec<MissionControlDeviceTableState>,
plants: Vec<(usize, u32)>,
- pending: Option<Order>,
+ pending: VecDeque<Order>,
rng: u32,
}
@@ -177,7 +178,7 @@ impl Production {
schemes,
controls,
plants,
- pending: None,
+ pending: VecDeque::new(),
rng: 1,
})
}
@@ -188,9 +189,29 @@ impl Production {
mission: &mut LoadedMissionState,
now_ms: u32,
) -> Result<Option<SpawnedUnit>, EngineError> {
- let Some(mut order) = self.pending.take() else {
- return Ok(None);
- };
+ for _ in 0..self.pending.len() {
+ let Some(mut order) = self.pending.pop_front() else {
+ break;
+ };
+ if let Some(unit) = self.advance_order(world, mission, now_ms, &mut order)? {
+ return Ok(Some(unit));
+ }
+ if identity_metadata(world, order.plant)
+ .is_ok_and(|identity| identity.owner_id == Some(order.owner))
+ {
+ self.pending.push_back(order);
+ }
+ }
+ Ok(None)
+ }
+
+ fn advance_order(
+ &mut self,
+ world: &mut World,
+ mission: &mut LoadedMissionState,
+ now_ms: u32,
+ order: &mut Order,
+ ) -> Result<Option<SpawnedUnit>, EngineError> {
if !identity_metadata(world, order.plant)
.is_ok_and(|identity| identity.owner_id == Some(order.owner))
{
@@ -221,7 +242,6 @@ impl Production {
ready &= paid.ready();
}
if !ready {
- self.pending = Some(order);
return Ok(None);
}
let pose = transform_state(world, order.plant)?;
@@ -362,7 +382,7 @@ fn order_owned_factory(
"selected factory no longer belongs to the ordering clan".into(),
)
})?;
- if production.pending.is_some() {
+ if production.pending.iter().any(|order| order.plant == plant) {
return Err(EngineError::Production(
"factory order already in progress".into(),
));
@@ -375,6 +395,7 @@ fn order_owned_factory(
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 live = (production.reserve_start..production.reserve_start + production.controls.len())
.filter(|&index| !free_slot(index))
@@ -440,7 +461,7 @@ fn order_owned_factory(
}),
)
};
- production.pending = Some(Order {
+ let order = Order {
slot,
plant,
plant_index,
@@ -448,7 +469,8 @@ fn order_owned_factory(
start_ms: now_ms,
duration_ms,
paid,
- });
+ };
+ production.pending.push_back(order);
Ok(duration_ms)
}
@@ -708,6 +730,24 @@ mod tests {
"{report:?}"
);
assert!(!report.first_factory_completion_ms.contains_key(&0));
+ let cockpit = super::super::starting_cockpit(&engine).unwrap();
+ let factory = handle_by_original_id(&engine.world, OriginalObjectId(5)).unwrap();
+ let duration = order_clan_factory_scheme(&mut engine, cockpit, factory, 0, 40_000).unwrap();
+ assert!(duration > 0);
+ let production = engine.production.as_ref().unwrap();
+ assert_eq!(production.pending.len(), 2);
+ assert_ne!(production.pending[0].slot, production.pending[1].slot);
+ let mut completed = Vec::new();
+ for time in (40_016..=40_000 + duration + 2000).step_by(16) {
+ if let Some(unit) = step_headless(&mut engine, InputSnapshot::default(), time)
+ .unwrap()
+ .produced_unit
+ {
+ completed.push(unit.clan);
+ }
+ }
+ assert!(completed.contains(&0), "{completed:?}");
+ assert!(completed.contains(&1), "{completed:?}");
}
#[test]
@@ -746,13 +786,7 @@ mod tests {
order_factory_robot(&mut engine, cockpit, 100).unwrap(),
PAID_MINIMUM_MS
);
- let order = engine
- .production
- .as_ref()
- .unwrap()
- .pending
- .as_ref()
- .unwrap();
+ let order = engine.production.as_ref().unwrap().pending.front().unwrap();
let cost = &order.paid.as_ref().unwrap().cost;
assert!((cost.ore - 25.12).abs() < 0.001);
assert!((cost.power - 72.7).abs() < 0.001);