From d7d51ac4bdedad7c9d611c343877b54468c9ea4d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 16:49:49 +0400 Subject: feat(combat): damage the building part struck by a projectile --- crates/fparkan-runtime/src/autodemo_combat.rs | 55 ++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) (limited to 'crates/fparkan-runtime/src/autodemo_combat.rs') diff --git a/crates/fparkan-runtime/src/autodemo_combat.rs b/crates/fparkan-runtime/src/autodemo_combat.rs index ddf83a50..5a864eb9 100644 --- a/crates/fparkan-runtime/src/autodemo_combat.rs +++ b/crates/fparkan-runtime/src/autodemo_combat.rs @@ -1,5 +1,5 @@ use super::{ - apply_projectile_hit, + apply_projectile_contact, autodemo::MOBILE_UNIT, player_weapon::prepare_unit_weapons, projectile::{load_projectile, unit_sphere}, @@ -46,6 +46,17 @@ pub enum AutoDemoCombatEvent { /// Unit removed from the world. target: ObjectHandle, }, + /// An autonomous shot changed a building part's health. + BuildingHit { + /// Owner of the weapon. + shooter: ObjectHandle, + /// Building struck by the projectile. + target: ObjectHandle, + /// Provider row associated with the struck part. + node: u32, + /// Remaining health of the part as raw f32 bits. + remaining_bits: u32, + }, } pub(super) struct AutoDemoCombat { @@ -269,13 +280,22 @@ impl AutoDemoCombat { let data = &self.projectiles[flight.projectile]; match data .volume - .map(|volume| apply_projectile_hit(engine, hit.handle, volume)) + .map(|volume| apply_projectile_contact(engine, hit, volume)) { Some(Ok(Some((_, true)))) => events.push(AutoDemoCombatEvent::Destroyed { shooter: flight.shooter, target: hit.handle, }), - Some(Ok(Some((_, false)))) => {} + Some(Ok(Some((remaining, false)))) => { + if let Some(node) = hit.building_node { + events.push(AutoDemoCombatEvent::BuildingHit { + shooter: flight.shooter, + target: hit.handle, + node, + remaining_bits: remaining.to_bits(), + }); + } + } _ => unavailable += 1, } } @@ -293,9 +313,10 @@ impl AutoDemoCombat { let targets = self.targets(&engine.world, &mission.mission_assets); for unit in &mut self.units { // shortcut: armed AI units defend their position while their Init is incomplete. - let guard = identity_metadata(&engine.world, unit.handle) + let owner = identity_metadata(&engine.world, unit.handle) .ok() - .and_then(|identity| identity.owner_id) + .and_then(|identity| identity.owner_id); + let guard = owner .is_some_and(|owner| self.guard_clans.get(usize::from(owner.0)) == Some(&true)); if mission.script_host_mode == super::MissionMode::SingleMission && !guard @@ -320,6 +341,30 @@ impl AutoDemoCombat { None } } + }) + .or_else(|| { + let (factory, index) = + mission.unit_patrol.as_ref()?.raid_factory(unit.handle)?; + let target_owner = identity_metadata(&engine.world, factory).ok()?.owner_id?; + if self + .hostile + .get(usize::from(owner?.0))? + .get(usize::from(target_owner.0)) + != Some(&true) + { + return None; + } + let state = transform_state(&engine.world, unit.handle).ok()?; + let (from, _) = unit_sphere(&mission.mission_assets, unit.index, state)?; + let target = transform_state(&engine.world, factory).ok()?; + let (to, _) = unit_sphere(&mission.mission_assets, index, target)?; + if (to[0] - from[0]).hypot(to[1] - from[1]) > FIRE_RANGE { + return None; + } + // shortcut: aim at the root bounds center until the native attack task selects a node. + projectile_terrain_hit(&mission.terrain, [from, to]) + .is_ok_and(|ground| ground.is_none()) + .then_some((factory, from, to)) }); for (slot, _, _) in unit.weapons.fire(aim.is_some(), now) { let (Some((target, from, to)), Some(projectile)) = (aim, unit.slots[slot]) else { -- cgit v1.2.3