aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/autodemo_combat.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 15:49:49 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 15:49:49 +0300
commitd7d51ac4bdedad7c9d611c343877b54468c9ea4d (patch)
treece63e45ceb4bef988d097bac3f261db60d8fe20a /crates/fparkan-runtime/src/autodemo_combat.rs
parent406d17ca79a0b29e062c60ad87c1dbe44f844444 (diff)
downloadfparkan-d7d51ac4bdedad7c9d611c343877b54468c9ea4d.tar.xz
fparkan-d7d51ac4bdedad7c9d611c343877b54468c9ea4d.zip
feat(combat): damage the building part struck by a projectile
Diffstat (limited to 'crates/fparkan-runtime/src/autodemo_combat.rs')
-rw-r--r--crates/fparkan-runtime/src/autodemo_combat.rs55
1 files changed, 50 insertions, 5 deletions
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 {