aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/projectile.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/projectile.rs')
-rw-r--r--crates/fparkan-runtime/src/projectile.rs103
1 files changed, 82 insertions, 21 deletions
diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs
index 55e5da46..720aad08 100644
--- a/crates/fparkan-runtime/src/projectile.rs
+++ b/crates/fparkan-runtime/src/projectile.rs
@@ -103,6 +103,7 @@ pub struct PlayerProjectile {
lifetime_ms: u32,
born_ms: u32,
updated_ms: u32,
+ pub(super) volume: Option<f32>,
}
impl PlayerProjectile {
@@ -131,9 +132,16 @@ impl PlayerProjectile {
lifetime_ms,
born_ms: clock_ms,
updated_ms: clock_ms,
+ volume: None,
})
}
+ /// Sets the launch volume. A pierced shield spends part of this volume in flight.
+ pub fn with_volume(mut self, volume: Option<f32>) -> Self {
+ self.volume = volume;
+ self
+ }
+
/// Queue time at launch, retained when the projectile's pose changes.
pub fn born_ms(&self) -> u32 {
self.born_ms
@@ -209,9 +217,30 @@ impl ProjectileContact {
None
}
}
+
+ /// Applies the volume remaining after crossing any fields along the flight.
+ pub fn apply_body_impact(
+ self,
+ engine: &mut Engine,
+ volume: f32,
+ ) -> Result<ProjectileImpact, ControlResourceAccessError> {
+ let key = match self.node {
+ Some(ProjectileNode::CaptureBarrier) => return Ok(ProjectileImpact::Unresolved),
+ None | Some(ProjectileNode::Chassis(0)) => None,
+ Some(
+ ProjectileNode::Chassis(key)
+ | ProjectileNode::Mounted(key)
+ | ProjectileNode::Building(key),
+ ) => Some(key),
+ };
+ Ok(match apply_contact_damage(engine, self, key, volume)? {
+ Some((remaining, removed)) => ProjectileImpact::Damage { remaining, removed },
+ None => ProjectileImpact::Unresolved,
+ })
+ }
}
-/// Finds the first unit or building intersected by this flight segment.
+/// Finds the first physical unit or building contact along this flight segment.
pub fn projectile_unit_hit(
engine: &Engine,
shooter: ObjectHandle,
@@ -261,7 +290,6 @@ pub(super) fn projectile_unit_hit_with_poses<'a>(
handle,
state,
segment,
- (center, radius),
sphere_fraction,
poses(index),
)?
@@ -271,9 +299,7 @@ pub(super) fn projectile_unit_hit_with_poses<'a>(
Some(ProjectileContact {
handle,
fraction,
- position: std::array::from_fn(|axis| {
- segment[0][axis] + (segment[1][axis] - segment[0][axis]) * fraction
- }),
+ position: segment_point(segment, fraction),
node,
})
})
@@ -331,25 +357,14 @@ pub fn apply_projectile_impact(
contact: ProjectileContact,
volume: f32,
) -> Result<ProjectileImpact, ControlResourceAccessError> {
- let key = match contact.node {
- Some(ProjectileNode::CaptureBarrier) => return Ok(ProjectileImpact::Unresolved),
- None | Some(ProjectileNode::Chassis(0)) => None,
- Some(
- ProjectileNode::Chassis(key)
- | ProjectileNode::Mounted(key)
- | ProjectileNode::Building(key),
- ) => Some(key),
- };
+ if contact.node == Some(ProjectileNode::CaptureBarrier) {
+ return Ok(ProjectileImpact::Unresolved);
+ }
let unabsorbed = super::projectile_shield::absorb(engine, contact, volume);
if volume > 0.0 && unabsorbed == 0.0 {
return Ok(ProjectileImpact::ShieldAbsorbed);
}
- Ok(
- match apply_contact_damage(engine, contact, key, unabsorbed)? {
- Some((remaining, removed)) => ProjectileImpact::Damage { remaining, removed },
- None => ProjectileImpact::Unresolved,
- },
- )
+ contact.apply_body_impact(engine, unabsorbed)
}
fn apply_contact_damage(
@@ -433,10 +448,31 @@ fn apply_contact_damage(
Ok(Some(((remaining - volume).max(0.0), false)))
}
+pub(super) fn segment_point([start, end]: [[f32; 3]; 2], fraction: f32) -> [f32; 3] {
+ std::array::from_fn(|axis| start[axis] + (end[axis] - start[axis]) * fraction)
+}
+
pub(super) fn segment_sphere_fraction(
+ segment: [[f32; 3]; 2],
+ center: [f32; 3],
+ radius: f32,
+) -> Option<f32> {
+ segment_sphere_query(segment, center, radius, true)
+}
+
+pub(super) fn segment_sphere_entrance(
+ segment: [[f32; 3]; 2],
+ center: [f32; 3],
+ radius: f32,
+) -> Option<f32> {
+ segment_sphere_query(segment, center, radius, false)
+}
+
+fn segment_sphere_query(
[start, end]: [[f32; 3]; 2],
center: [f32; 3],
radius: f32,
+ include_overlap: bool,
) -> Option<f32> {
if !radius.is_finite() || radius <= 0.0 {
return None;
@@ -445,11 +481,15 @@ pub(super) fn segment_sphere_fraction(
let rel: [f32; 3] = std::array::from_fn(|i| start[i] - center[i]);
let dot = |a: [f32; 3], b: [f32; 3]| a.into_iter().zip(b).map(|(a, b)| a * b).sum::<f32>();
let c = dot(rel, rel) - radius * radius;
- if c <= 0.0 {
+ if include_overlap && c <= 0.0 {
return Some(0.0);
}
let a = dot(dir, dir);
let b = dot(rel, dir);
+ // Control+0x1e9f0 reports overlap without a crossing flag inside the sphere.
+ if !include_overlap && (c < 0.0 || a < 1e-6 || b > 0.0) {
+ return None;
+ }
let d = b * b - a * c;
let fraction = (-b - d.sqrt()) / a;
(0.0..=1.0).contains(&fraction).then_some(fraction)
@@ -866,3 +906,24 @@ mod tests {
.unwrap());
}
}
+#[test]
+fn shield_entrance_is_distinct_from_an_initial_overlap() {
+ let center = [0.0; 3];
+ let exit = [10.0, 4.0, 0.0];
+ assert_eq!(
+ segment_sphere_entrance([[-10.0, 4.0, 0.0], exit], center, 5.0),
+ Some(0.35)
+ );
+ assert_eq!(
+ segment_sphere_entrance([[0.0, 4.0, 0.0], exit], center, 5.0),
+ None
+ );
+ assert_eq!(
+ segment_sphere_entrance([[-3.0, 4.0, 0.0], exit], center, 5.0),
+ Some(0.0)
+ );
+ assert_eq!(
+ segment_sphere_entrance([[-3.0, 4.0, 0.0], [-10.0, 4.0, 0.0]], center, 5.0),
+ None
+ );
+}