diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/control_resources.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mobile_budget.rs | 37 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 10 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/repair_zone.rs | 73 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/weapon_resupply.rs | 59 |
6 files changed, 155 insertions, 36 deletions
diff --git a/crates/fparkan-runtime/src/control_resources.rs b/crates/fparkan-runtime/src/control_resources.rs index e682ee5b..3e7f8aab 100644 --- a/crates/fparkan-runtime/src/control_resources.rs +++ b/crates/fparkan-runtime/src/control_resources.rs @@ -133,7 +133,7 @@ pub(super) fn tick(engine: &mut Engine, now_ms: u32) { continue; }; if mobile_owner { - repair_zone::repair_nodes(table, world, pose, &facilities, seconds); + repair_zone::service_unit(table, world, pose, &facilities, seconds); } let object = table.original_id.map_or(table.handle.slot, |id| id.0); let ticks = state.profiles.ticks.entry(object).or_default(); diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 970e8551..98592d6a 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -131,6 +131,7 @@ mod projectile_flight; mod projectile_parts; mod projectile_shield; mod weapon_deployment; +mod weapon_resupply; pub use projectile_shield::{ collect_shield_impacts, shield_effect_sources, take_shield_impacts, ShieldImpact, }; @@ -644,6 +645,8 @@ pub struct EffectiveControlDevice { pub device174_bits: Option<u32>, /// Factory-2 charge at native +0x120, initialized by the weapon reader. pub weapon_charge_bits: Option<u32>, + /// Remaining finite ammunition at Control device offset +0x124. + pub weapon_rounds: Option<u32>, /// Six class-9 budget values at native `+0x98..+0xac`. pub class9_values_bits: Option<[u32; 6]>, /// Six accumulated class-9 changes at native `+0xb0..+0xc4`. @@ -6543,6 +6546,9 @@ fn append_effective_control_device( tag26_extra_rate_bits: (descriptor.tag == 26).then_some(0), device174_bits: (descriptor.tag == 2).then_some(0), weapon_charge_bits: matches!(descriptor.tag, 2 | 30).then_some(coefficient_words[1]), + weapon_rounds: (descriptor.tag == 2) + .then(|| weapon_resupply::capacity(coefficient_words[0])) + .flatten(), class9_values_bits: (descriptor.tag == 9).then_some([0x3f80_0000; 6]), class9_deltas_bits: (descriptor.tag == 9).then_some([0; 6]), native_word_50_bits, @@ -6624,6 +6630,9 @@ fn apply_effective_control_override( if matches!(device.factory_tag, 2 | 30) { device.weapon_charge_bits = Some(coefficient_words[1]); } + if device.factory_tag == 2 && descriptor.tag == 2 { + device.weapon_rounds = weapon_resupply::capacity(coefficient_words[0]); + } if descriptor.fixed_words[6] != u32::MAX { device.native_word_50_bits = Some(descriptor.fixed_words[6]); } @@ -9306,6 +9315,7 @@ mod tests { tag26_extra_rate_bits: (factory_tag == 26).then_some(0), device174_bits: (factory_tag == 2).then_some(0), weapon_charge_bits: matches!(factory_tag, 2 | 30).then_some(coefficients[1]), + weapon_rounds: None, class9_values_bits: (factory_tag == 9).then_some([0x3f80_0000; 6]), class9_deltas_bits: (factory_tag == 9).then_some([0; 6]), native_word_50_bits, diff --git a/crates/fparkan-runtime/src/mobile_budget.rs b/crates/fparkan-runtime/src/mobile_budget.rs index 32e81eae..fb8dad48 100644 --- a/crates/fparkan-runtime/src/mobile_budget.rs +++ b/crates/fparkan-runtime/src/mobile_budget.rs @@ -309,6 +309,9 @@ pub(super) fn read( for weapon in weapons { let device = devices .and_then(|devices| devices.iter().find(|device| matches_weapon(device, weapon))); + if let Some(device) = device { + weapon.rounds_remaining = device.weapon_rounds; + } weapon.destroyed = device .and_then(|device| crate::control_node_provider_row(table?, device.key).ok()) .is_some_and(|row| row.current_flags.is_some_and(|flags| flags & 0x10 != 0)); @@ -378,29 +381,31 @@ pub(super) fn publish( if weapon.destroyed { continue; } - let Some(charge) = &weapon.charge else { - if available { - mission.control_resources.mobile.stub( - object, - format!( - "weapon_link_{}_{}", - weapon.component_index, weapon.control_record_index - ), - ); - } - continue; - }; - if let MissionControlDeviceTableStatus::Available(devices) = &mut table.status { - if let Some(device) = devices + let device = match &mut table.status { + MissionControlDeviceTableStatus::Available(devices) => devices .iter_mut() - .find(|device| matches_weapon(device, weapon)) - { + .find(|device| matches_weapon(device, weapon)), + _ => None, + }; + if let Some(device) = device { + device.weapon_rounds = weapon.rounds_remaining; + if let Some(charge) = &weapon.charge { device.weapon_charge_bits = Some(charge.amount.to_bits()); if charge.capacity > 0.0 { device.multiplier_bits = Some((charge.amount / charge.capacity).to_bits()); } + continue; } } + if available { + mission.control_resources.mobile.stub( + object, + format!( + "weapon_link_{}_{}", + weapon.component_index, weapon.control_record_index + ), + ); + } } } diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index e9d748e6..347e0450 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -56,7 +56,7 @@ pub struct PlayerWeapon { deployment: Option<WeaponDeployment>, barrels: Vec<Barrel>, barrel_index: usize, - rounds_remaining: Option<u32>, + pub(super) rounds_remaining: Option<u32>, shot_hold_ms: f64, reload_ms: f64, phase: FirePhase, @@ -83,14 +83,14 @@ enum FirePhase { } impl PlayerWeapons { - /// Reads the authoritative Control charge before sampling this weapon bank. + /// Reads the authoritative Control charge and ammunition before firing. pub fn read_control_energy(&mut self, engine: &Engine, handle: crate::ObjectHandle) { if let Some(mission) = &engine.loaded { crate::mobile_budget::read(mission, handle, &mut self.weapons); } } - /// Publishes completed shots to Control before its next resource budget. + /// Publishes charge and ammunition consumed by completed shots to Control. pub fn publish_control_energy(&mut self, engine: &mut Engine, handle: crate::ObjectHandle) { if let Some(mission) = &mut engine.loaded { crate::mobile_budget::publish(mission, handle, &mut self.weapons); @@ -545,10 +545,8 @@ fn prepare_weapons( let rounds = f32::from_bits(record.fixed_words[11]).round_ties_even(); let rounds_remaining = if rounds == -1.0 { None - } else if (0.0..2_147_483_648.0).contains(&rounds) { - Some(rounds as u32) } else { - return None; + Some(crate::weapon_resupply::capacity(record.fixed_words[11])?) }; let effect_row = firing_effect_row(control, row); let effect_node = effect_row.and_then(|effect| { diff --git a/crates/fparkan-runtime/src/repair_zone.rs b/crates/fparkan-runtime/src/repair_zone.rs index e137a85e..20a14d85 100644 --- a/crates/fparkan-runtime/src/repair_zone.rs +++ b/crates/fparkan-runtime/src/repair_zone.rs @@ -177,22 +177,14 @@ fn facility_volumes(assets: &MissionAssets, index: usize) -> Vec<RepairVolume> { .collect() } -pub(super) fn repair_nodes( +pub(super) fn service_unit( table: &mut MissionControlDeviceTableState, world: &World, pose: TransformState, facilities: &[ActiveFacility<'_>], seconds: f32, ) { - let Some((amount, maximum)) = table - .node_provider_amount_bits - .zip(table.node_provider_maximum_amount_bits) - .map(|(amount, maximum)| (f32::from_bits(amount), f32::from_bits(maximum))) - else { - return; - }; - if table.node_provider_depletion_marked || !(0.0 < amount && amount < maximum && seconds > 0.0) - { + if table.node_provider_depletion_marked || seconds <= 0.0 { return; } let Some(owner) = identity_metadata(world, table.handle) @@ -213,7 +205,22 @@ pub(super) fn repair_nodes( site.volumes.iter().any(|volume| volume.contains(local)) }); if in_service { - let target = (amount / maximum + seconds * HEALTH_FRACTION_PER_SECOND).min(1.0); + let fraction = seconds * HEALTH_FRACTION_PER_SECOND; + repair_nodes(table, fraction); + crate::weapon_resupply::refill(&mut table.status, fraction); + } +} + +fn repair_nodes(table: &mut MissionControlDeviceTableState, fraction: f32) { + let Some((amount, maximum)) = table + .node_provider_amount_bits + .zip(table.node_provider_maximum_amount_bits) + .map(|(amount, maximum)| (f32::from_bits(amount), f32::from_bits(maximum))) + else { + return; + }; + if 0.0 < amount && amount < maximum { + let target = (amount / maximum + fraction).min(1.0); let _ = distribute_control_node_amount( table, target * maximum - amount, @@ -228,7 +235,8 @@ mod tests { use super::*; use crate::{ apply_loaded_mission_control_node_delta, create, frame, load_gameplay_mission, - starting_cockpit, EngineServices, MissionMode, MissionRequest, MissionStartupInput, + starting_cockpit, starting_player_weapons, EngineServices, MissionControlDeviceTableStatus, + MissionMode, MissionRequest, MissionStartupInput, }; use fparkan_vfs::DirectoryVfs; use fparkan_world::{set_owner, set_transform}; @@ -277,7 +285,17 @@ mod tests { None, ) .unwrap(); - let hero = starting_cockpit(&engine).unwrap().handle; + let cockpit = starting_cockpit(&engine).unwrap(); + let hero = cockpit.handle; + let mut cockpit_weapons = starting_player_weapons(&engine, cockpit); + let slot = cockpit_weapons + .weapons() + .iter() + .position(|weapon| weapon.rounds_remaining().is_some_and(|rounds| rounds > 0)) + .unwrap(); + let weapon = &cockpit_weapons.weapons()[slot]; + let component = weapon.component_index; + let record = weapon.control_record_index; let initial_pose = transform_state(&engine.world, hero).unwrap(); let read_node = |engine: &crate::Engine, key| { let table = engine @@ -333,18 +351,47 @@ mod tests { ) .unwrap(); let damaged = read_node(&engine, damage_key).0; + let table = engine + .loaded + .as_mut() + .unwrap() + .control_device_tables + .iter_mut() + .find(|table| table.handle == hero) + .unwrap(); + let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { + panic!("hero device table unavailable"); + }; + let device = devices + .iter_mut() + .find(|device| { + device.factory_provenance.unit_component_index == component + && device.factory_provenance.typed_record_index == record + }) + .unwrap(); + device.weapon_rounds = Some(0); set_transform(&mut engine.world, hero, actor).unwrap(); set_owner(&mut engine.world, facility, Some(OwnerId(1))).unwrap(); frame(&mut engine, start).unwrap(); frame(&mut engine, start + 1000).unwrap(); assert_eq!(read_node(&engine, damage_key).0, damaged); + cockpit_weapons.read_control_energy(&engine, hero); + assert_eq!(cockpit_weapons.weapons()[slot].rounds_remaining(), Some(0)); set_owner(&mut engine.world, facility, Some(OwnerId(0))).unwrap(); frame(&mut engine, start + 2000).unwrap(); let repaired = read_node(&engine, damage_key).0; assert!(repaired > damaged && repaired <= max); + cockpit_weapons.read_control_energy(&engine, hero); + let rounds = cockpit_weapons.weapons()[slot].rounds_remaining().unwrap(); + assert!(rounds > 0); set_transform(&mut engine.world, hero, initial_pose).unwrap(); frame(&mut engine, start + 3000).unwrap(); assert_eq!(read_node(&engine, damage_key).0, repaired); + cockpit_weapons.read_control_energy(&engine, hero); + assert_eq!( + cockpit_weapons.weapons()[slot].rounds_remaining(), + Some(rounds) + ); } } } diff --git a/crates/fparkan-runtime/src/weapon_resupply.rs b/crates/fparkan-runtime/src/weapon_resupply.rs new file mode 100644 index 00000000..31a23594 --- /dev/null +++ b/crates/fparkan-runtime/src/weapon_resupply.rs @@ -0,0 +1,59 @@ +use crate::MissionControlDeviceTableStatus; + +pub(super) fn capacity(bits: u32) -> Option<u32> { + let rounds = f32::from_bits(bits).round_ties_even(); + (0.0..2_147_483_648.0) + .contains(&rounds) + .then_some(rounds as u32) +} + +pub(super) fn refill(status: &mut MissionControlDeviceTableStatus, fraction: f32) { + let MissionControlDeviceTableStatus::Available(devices) = status else { + return; + }; + for device in devices + .iter_mut() + .filter(|device| device.descriptor.tag == 2) + { + if let (Some(stock), Some(capacity)) = ( + &mut device.weapon_rounds, + capacity(device.descriptor.fixed_words[11]).filter(|&capacity| capacity != 0), + ) { + // Behavior.dll+0x18276 rounds to nearest and grants at least one round. + let added = ((capacity as f32 * fraction).round_ties_even() as u32).max(1); + *stock = stock.saturating_add(added).min(capacity); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::synthetic_effective_control_device; + + #[test] + fn pad_rounds_each_refill_and_leaves_unlimited_weapons_alone() { + let device = |capacity: f32, stock| { + let mut device = + synthetic_effective_control_device(2, 2, capacity.to_bits(), None, None); + device.weapon_rounds = stock; + device + }; + let mut status = MissionControlDeviceTableStatus::Available(vec![ + device(100.0, Some(3)), + device(4.0, Some(3)), + device(0.0, Some(0)), + device(-1.0, None), + ]); + for (seconds, expected) in [(0.01, 4), (0.25, 6), (2.0, 26)] { + refill(&mut status, seconds * 0.1); + let MissionControlDeviceTableStatus::Available(devices) = &status else { + unreachable!(); + }; + assert_eq!(devices[0].weapon_rounds, Some(expected)); + assert_eq!(devices[1].weapon_rounds, Some(4)); + assert_eq!(devices[2].weapon_rounds, Some(0)); + assert_eq!(devices[3].weapon_rounds, None); + } + } +} |
