aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs25
-rw-r--r--crates/fparkan-runtime/src/clan_combat_strength.rs61
-rw-r--r--crates/fparkan-runtime/src/lib.rs7
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs22
4 files changed, 115 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index d82014ec..45796a9e 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -86,6 +86,7 @@ pub(super) struct ClanAiEvents {
factory_orders: BTreeMap<(u16, u32), u64>,
factory_jobs: Vec<((u16, Handler2RecordKey), ObjectHandle)>,
factory_rejections: BTreeMap<(u16, u32), u64>,
+ strength_queries: BTreeMap<(usize, u16), u64>,
}
impl LoadedMissionState {
@@ -150,6 +151,25 @@ impl LoadedMissionState {
.scalar_1;
Ok(Some(VarSetDefault::FloatBits(priority.to_bits())))
}
+ ScriptDispatchSelector::Handler(38) => {
+ let (target, mode) = instruction.dword_pair(values)
+ .ok_or("Handler(38) requires a clan and unit-selection mode")?;
+ if mode > 1 {
+ return Err("Handler(38) requires FREE_UNITS or ALL_UNITS".into());
+ }
+ let Ok(target) = u16::try_from(target) else {
+ return Ok(Some(VarSetDefault::Dword(0)));
+ };
+ let sum = self.mission_events.clan_units(world, OwnerId(target), mode == 0)
+ .map(|handle| crate::clan_combat_strength::unit_strength(&self.control_device_tables, handle))
+ .fold(0.0, |sum, contribution| sum + contribution);
+ let count = self.clan_ai_events.strength_queries.entry((clan, target)).or_default();
+ *count += 1;
+ if *count == 1 {
+ eprintln!("sim_coverage script_combat_strength_approx=1 clan={clan} target={target} strength={sum}");
+ }
+ Ok(Some(VarSetDefault::Dword(sum as u32)))
+ }
ScriptDispatchSelector::Handler(59 | 60) => {
crate::script_time::result(instruction, values, clock)
.map(|result| Some(VarSetDefault::Dword(result)))
@@ -661,6 +681,11 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32
}
impl ClanAiEvents {
+ pub(super) fn strength_queries(&self) -> impl Iterator<Item = (usize, u16, u64)> + '_ {
+ self.strength_queries
+ .iter()
+ .map(|(&(clan, target), &count)| (clan, target, count))
+ }
fn log_research_stub(&mut self, clan: usize, handler: u8, script: &str) {
let count = self.research_stubs.entry((clan, handler)).or_default();
*count += 1;
diff --git a/crates/fparkan-runtime/src/clan_combat_strength.rs b/crates/fparkan-runtime/src/clan_combat_strength.rs
new file mode 100644
index 00000000..2ad01692
--- /dev/null
+++ b/crates/fparkan-runtime/src/clan_combat_strength.rs
@@ -0,0 +1,61 @@
+use crate::{
+ control_device_provider_value_from_status, MissionControlDeviceTableState,
+ MissionControlDeviceTableStatus, ObjectHandle,
+};
+
+pub(super) fn unit_strength(
+ tables: &[MissionControlDeviceTableState],
+ handle: ObjectHandle,
+) -> f32 {
+ let Some(table) = tables.iter().find(|table| table.handle == handle) else {
+ return 0.0;
+ };
+ let capacity = table.node_provider_amount_bits.map_or(0.0, f32::from_bits);
+ let MissionControlDeviceTableStatus::Available(devices) = &table.status else {
+ return rating(capacity, 0.0);
+ };
+ let mut grouped = false;
+ let mut weapon_power = 0.0;
+ for device in devices {
+ match device.descriptor.tag {
+ 1 => grouped = true,
+ 2 if grouped && device.factory_tag == 2 => {
+ let (Some(coefficients), Some(volume)) =
+ (device.coefficient_bits, device.device174_bits)
+ else {
+ continue;
+ };
+ if control_device_provider_value_from_status(&table.node_provider_status, device)
+ .is_ok_and(|health| health == 0.0)
+ {
+ continue;
+ }
+ let rounds = f32::from_bits(coefficients[0]).round_ties_even() as i32 as u32;
+ let volume = f32::from_bits(volume);
+ if rounds > 0 && volume.is_finite() {
+ weapon_power += (volume / rounds as f32) * rounds as f32;
+ }
+ }
+ _ => {}
+ }
+ }
+ // shortcut: assume full design ammunition and use the live Control amount
+ // until selector 0x36 scaling and live weapon counters are wired.
+ rating(capacity, weapon_power)
+}
+
+fn rating(capacity: f32, weapon_power: f32) -> f32 {
+ ((weapon_power + 0.8_f32) * capacity) * 0.00001_f32
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn strength_scales_active_weapon_power_by_remaining_capacity() {
+ assert_eq!(rating(200_000.0, 3.2), 8.0);
+ assert_eq!(rating(100_000.0, 3.2), 4.0);
+ assert_eq!(rating(0.0, 3.2), 0.0);
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 480149f6..61ee81be 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -45,6 +45,7 @@ mod autodemo;
mod boarding;
mod building_door;
mod clan_ai_events;
+mod clan_combat_strength;
mod clan_script_init;
mod mesh_collision;
mod mining;
@@ -2908,6 +2909,8 @@ pub struct SimulationReport {
pub script_factory_rejections: Vec<(u16, u32, u64)>,
/// Accepted approximate script construction orders by clan and factory slot.
pub script_factory_orders: Vec<(u16, u32, u64)>,
+ /// Approximate clan strength queries, grouped by caller and target clan.
+ pub script_strength_queries: Vec<(usize, u16, u64)>,
/// First factory completion by clan, in milliseconds.
pub first_factory_completion_ms: std::collections::BTreeMap<u16, u32>,
/// Total requests for mission information messages.
@@ -2949,6 +2952,7 @@ pub fn simulate_mission_report(
clan_problem_starts: Vec::new(),
script_factory_rejections: Vec::new(),
script_factory_orders: Vec::new(),
+ script_strength_queries: Vec::new(),
first_factory_completion_ms: Default::default(),
mission_messages: 0,
autodemo_first_shot_ms: None,
@@ -3025,6 +3029,9 @@ pub fn simulate_mission_report(
report
.script_factory_orders
.extend(loaded.clan_ai_events.factory_orders());
+ report
+ .script_strength_queries
+ .extend(loaded.clan_ai_events.strength_queries());
}
Ok(report)
}
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index d040e4f8..d13b6327 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -152,6 +152,28 @@ pub(super) struct MissionEvents {
}
impl MissionEvents {
+ pub(super) fn clan_units<'a>(
+ &'a self,
+ world: &'a World,
+ owner: OwnerId,
+ free_only: bool,
+ ) -> impl Iterator<Item = ObjectHandle> + 'a {
+ self.placed_clan_objects
+ .iter()
+ .filter_map(move |&(handle, class)| {
+ if free_only
+ && (class != super::boarding::WARBOT_CLASS
+ || self
+ .reserved_units
+ .contains(&(usize::from(owner.0), handle)))
+ {
+ return None;
+ }
+ registration_sequence(world, handle).ok()??;
+ (identity_metadata(world, handle).ok()?.owner_id == Some(owner)).then_some(handle)
+ })
+ }
+
pub(super) fn count_clan_class(&self, world: &World, owner: OwnerId, class: u32) -> u32 {
clan_object_classes(&self.placed_clan_objects, world, owner)
.filter(|&candidate| candidate == class)