aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs45
-rw-r--r--crates/fparkan-runtime/src/clan_minds.rs170
-rw-r--r--crates/fparkan-runtime/src/lib.rs7
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs10
-rw-r--r--crates/fparkan-runtime/src/production.rs11
5 files changed, 225 insertions, 18 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index 455ee91f..d191029c 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -206,8 +206,8 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec<MissionNotice
.push(((order.owner.0, order.problem), order.plant));
}
eprintln!(
- "script_factory_order_approx clan={} plant={} duration_ms={duration} queue_time_ms={now_ms}",
- order.owner.0, order.plant.slot,
+ "script_factory_order_approx clan={} plant={} class={:#010x} selection={} duration_ms={duration} queue_time_ms={now_ms}",
+ order.owner.0, order.plant.slot, order.class, order.selection,
);
}
Err(error) => {
@@ -271,6 +271,7 @@ pub(super) struct ClanAiEvents {
point_group_orders: BTreeMap<(usize, u32, u32), u64>,
point_patrol_continues: BTreeMap<(usize, u32, u32), u64>,
free_unit_queries: BTreeMap<(usize, u32, u32), u64>,
+ free_mind_queries: BTreeMap<usize, (u32, u32, u32, u64)>,
group_recruitments: BTreeMap<(usize, u32), u64>,
class_target_queries: BTreeMap<(usize, u32, u32), u64>,
size_class_queries: BTreeMap<(usize, u32), u64>,
@@ -1173,13 +1174,23 @@ impl LoadedMissionState {
ScriptDispatchSelector::Handler(49)
if instruction.references.is_empty() =>
{
- let slots = self
+ let capacity = self
.ai_startup_states
.iter()
- .find(|state| state.clan_index == clan);
- Ok(Some(VarSetDefault::Dword(
- slots.map_or(0, |state| count_free_minds(&state.record_slots)),
- )))
+ .find(|state| state.clan_index == clan)
+ .map_or(0, |state| state.record_slots.len());
+ let owner = OwnerId(clan as u16);
+ let reserved = production.map_or(0, |production| production.reserved_minds(world, owner))
+ + orders.iter().filter(|order| order.owner == owner).count();
+ let free = crate::clan_minds::free_slots(
+ capacity, self.mission_events.clan_classes(world, owner), reserved,
+ );
+ let query = self.clan_ai_events.free_mind_queries.entry(clan).or_default();
+ *query = (capacity as u32, free, reserved as u32, query.3 + 1);
+ if query.3 == 1 {
+ eprintln!("sim_coverage script_free_minds_approx=1 clan={clan} capacity={capacity} free={free} reserved={reserved}");
+ }
+ Ok(Some(VarSetDefault::Dword(free)))
}
ScriptDispatchSelector::Handler(34) => {
let &[index] = instruction.references.as_slice() else {
@@ -1383,10 +1394,6 @@ fn problem_operands<'a>(
))
}
-fn count_free_minds(slots: &[u32]) -> u32 {
- slots.iter().filter(|&&word| word == u32::MAX).count() as u32
-}
-
fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32 {
ores.iter()
.filter(|ore| {
@@ -1401,6 +1408,16 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32
}
impl ClanAiEvents {
+ pub(super) fn free_mind_queries(
+ &self,
+ ) -> impl Iterator<Item = (usize, u32, u32, u32, u64)> + '_ {
+ self.free_mind_queries
+ .iter()
+ .map(|(&clan, &(capacity, free, reserved, count))| {
+ (clan, capacity, free, reserved, count)
+ })
+ }
+
pub(super) fn base_threat_queries(&self) -> impl Iterator<Item = (usize, u32, u64)> + '_ {
self.base_threat_queries
.iter()
@@ -1970,12 +1987,6 @@ mod tests {
}
#[test]
- fn free_mind_count_uses_only_the_native_sentinel() {
- assert_eq!(count_free_minds(&[]), 0);
- assert_eq!(count_free_minds(&[u32::MAX, 0, u32::MAX, 1, 0xfffe]), 2);
- }
-
- #[test]
fn problem_operands_resolve_live_cells_and_numeric_types() {
let varset = fparkan_script::parse_varset(
b"VAR(DWORD, PBM_ROBOT_NEEDED, 6)\nVAR(float, priority, 0)\nVAR(DWORD, arg, 0)\n",
diff --git a/crates/fparkan-runtime/src/clan_minds.rs b/crates/fparkan-runtime/src/clan_minds.rs
new file mode 100644
index 00000000..0f14f0a4
--- /dev/null
+++ b/crates/fparkan-runtime/src/clan_minds.rs
@@ -0,0 +1,170 @@
+/// Project the native mind slots from registered mobile units and factory orders.
+pub(super) fn free_slots(
+ capacity: usize,
+ classes: impl Iterator<Item = u32>,
+ reserved: usize,
+) -> u32 {
+ let live = classes
+ .filter(|class| class & super::autodemo::MOBILE_UNIT != 0)
+ .count();
+ capacity.saturating_sub(live.saturating_add(reserved)) as u32
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn stock_clan_occupancy_and_factory_reservation_match_native_slots() {
+ let units = [
+ 0x0100_2000,
+ 0x0100_8000,
+ 0x0100_8000,
+ 0x0100_8000,
+ 0x0100_8000,
+ ];
+ assert_eq!(free_slots(9, units.into_iter(), 0), 4);
+ assert_eq!(free_slots(9, units.into_iter(), 1), 3);
+ // Replacing the factory reservation with a newborn keeps the same occupancy.
+ assert_eq!(free_slots(9, units.into_iter().chain([0x0100_8000]), 0), 3);
+ assert_eq!(free_slots(9, units.into_iter(), 4), 0);
+ assert_eq!(free_slots(9, units.into_iter(), 5), 0);
+ }
+
+ #[test]
+ fn hero_uses_a_mind_and_buildings_do_not() {
+ let classes = [0x0102_0000, 0x0100_4000, 0x8000_0008, 0x8000_0010];
+ assert_eq!(free_slots(9, classes.into_iter(), 0), 7);
+ assert_eq!(free_slots(0, classes.into_iter(), 0), 0);
+ }
+
+ #[test]
+ fn captured_and_deleted_units_release_the_former_clans_slots() {
+ use fparkan_world::{
+ construct_object, new, register_object, request_delete, set_owner, step, InputSnapshot,
+ ObjectDraft, OwnerId, WorldConfig,
+ };
+ let mut world = new(WorldConfig);
+ let mut events = crate::mission_events::MissionEvents::default();
+ let unit = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap();
+ register_object(&mut world, unit).unwrap();
+ set_owner(&mut world, unit, Some(OwnerId(1))).unwrap();
+ events.register_unit(&world, unit, 0x0100_8000);
+ assert_eq!(free_slots(9, events.clan_classes(&world, OwnerId(1)), 0), 8);
+ set_owner(&mut world, unit, Some(OwnerId(0))).unwrap();
+ assert_eq!(free_slots(9, events.clan_classes(&world, OwnerId(1)), 0), 9);
+ assert_eq!(free_slots(9, events.clan_classes(&world, OwnerId(0)), 0), 8);
+ request_delete(&mut world, unit).unwrap();
+ step(&mut world, &InputSnapshot).unwrap();
+ assert_eq!(free_slots(9, events.clan_classes(&world, OwnerId(0)), 0), 9);
+ }
+
+ #[test]
+ #[ignore = "requires licensed mission resources"]
+ fn licensed_stock_factory_reserves_a_mind_until_birth_or_ownership_loss() {
+ use crate::{
+ create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
+ MissionStartupInput,
+ };
+ use fparkan_vfs::DirectoryVfs;
+ use fparkan_world::{identity_metadata, set_owner, OwnerId};
+ use std::sync::Arc;
+ let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
+ let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
+ load_gameplay_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/Single.01/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+ let available = |engine: &crate::Engine, owner: OwnerId| {
+ let loaded = engine.loaded.as_ref().unwrap();
+ let capacity = loaded
+ .ai_startup_states
+ .iter()
+ .find(|state| state.clan_index == usize::from(owner.0))
+ .unwrap()
+ .record_slots
+ .len();
+ free_slots(
+ capacity,
+ loaded.mission_events.clan_classes(&engine.world, owner),
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .reserved_minds(&engine.world, owner),
+ )
+ };
+ assert_eq!(available(&engine, OwnerId(0)), 3);
+ assert_eq!(available(&engine, OwnerId(1)), 4);
+ frame(&mut engine, 0).unwrap();
+ assert_eq!(
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .reserved_minds(&engine.world, OwnerId(1)),
+ 1
+ );
+ assert_eq!(available(&engine, OwnerId(1)), 3);
+ for now_ms in (100..=20_000).step_by(100) {
+ frame(&mut engine, now_ms).unwrap();
+ }
+ assert_eq!(
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .reserved_minds(&engine.world, OwnerId(1)),
+ 0
+ );
+ assert_eq!(available(&engine, OwnerId(1)), 3);
+ for now_ms in (20_100..=28_000).step_by(100) {
+ frame(&mut engine, now_ms).unwrap();
+ }
+ assert_eq!(
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .reserved_minds(&engine.world, OwnerId(1)),
+ 1
+ );
+ assert_eq!(available(&engine, OwnerId(1)), 2);
+ let loaded = engine.loaded.as_ref().unwrap();
+ let plant = loaded
+ .mission_events
+ .clan_units(&engine.world, OwnerId(1), false)
+ .find(|&unit| {
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .is_building_at(unit, OwnerId(1))
+ })
+ .unwrap();
+ assert_eq!(
+ identity_metadata(&engine.world, plant).unwrap().owner_id,
+ Some(OwnerId(1))
+ );
+ // Debug ownership transfer tests cancellation; it is not mission acceptance.
+ set_owner(&mut engine.world, plant, Some(OwnerId(0))).unwrap();
+ assert_eq!(available(&engine, OwnerId(1)), 3);
+ assert_eq!(
+ engine
+ .production
+ .as_ref()
+ .unwrap()
+ .reserved_minds(&engine.world, OwnerId(0)),
+ 0
+ );
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index accf0716..730c52ab 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -54,6 +54,7 @@ mod clan_clock;
pub use clan_clock::note_mission_pause_elapsed;
mod clan_combat_groups;
mod clan_combat_strength;
+mod clan_minds;
mod clan_mining;
mod clan_problem_conditions;
mod clan_research;
@@ -2944,6 +2945,8 @@ pub struct SimulationReport {
pub script_point_group_orders: Vec<(usize, u32, u32, u64)>,
pub script_point_patrol_continues: Vec<(usize, u32, u32, u64)>,
pub script_free_unit_queries: Vec<(usize, u32, u32, u64)>,
+ /// Clan, capacity, latest free/reserved mind counts, and approximate query count.
+ pub script_free_mind_queries: Vec<(usize, u32, u32, u32, u64)>,
pub script_group_recruitments: Vec<(usize, u32, u64)>,
pub script_unit_order_counts: Vec<(u32, u32, u64)>,
pub script_class_target_queries: Vec<(usize, u32, u32, u64)>,
@@ -3013,6 +3016,7 @@ pub fn simulate_mission_report(
script_point_group_orders: Vec::new(),
script_point_patrol_continues: Vec::new(),
script_free_unit_queries: Vec::new(),
+ script_free_mind_queries: Vec::new(),
script_group_recruitments: Vec::new(),
script_unit_order_counts: Vec::new(),
script_class_target_queries: Vec::new(),
@@ -3154,6 +3158,9 @@ pub fn simulate_mission_report(
.script_free_unit_queries
.extend(loaded.clan_ai_events.free_unit_queries());
report
+ .script_free_mind_queries
+ .extend(loaded.clan_ai_events.free_mind_queries());
+ report
.script_group_recruitments
.extend(loaded.clan_ai_events.group_recruitments());
report
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index b200d0b9..a198bfdd 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -181,11 +181,19 @@ impl MissionEvents {
}
pub(super) fn count_clan_class(&self, world: &World, owner: OwnerId, class: u32) -> u32 {
- clan_object_classes(&self.placed_clan_objects, world, owner)
+ self.clan_classes(world, owner)
.filter(|&candidate| candidate == class)
.count() as u32
}
+ pub(super) fn clan_classes<'a>(
+ &'a self,
+ world: &'a World,
+ owner: OwnerId,
+ ) -> impl Iterator<Item = u32> + 'a {
+ clan_object_classes(&self.placed_clan_objects, world, owner)
+ }
+
pub(super) fn first_available_placed_unit(
&self,
mission: &super::MissionDocument,
diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs
index a6752712..05914d73 100644
--- a/crates/fparkan-runtime/src/production.rs
+++ b/crates/fparkan-runtime/src/production.rs
@@ -134,6 +134,17 @@ impl PaidOrder {
}
impl Production {
+ pub(super) fn reserved_minds(&self, world: &World, owner: OwnerId) -> usize {
+ self.pending
+ .iter()
+ .filter(|order| {
+ order.owner == owner
+ && identity_metadata(world, order.plant)
+ .is_ok_and(|identity| identity.owner_id == Some(owner))
+ })
+ .count()
+ }
+
pub(super) fn is_building_at(&self, plant: ObjectHandle, owner: OwnerId) -> bool {
self.pending
.iter()