use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AxisPoint {
x: i64,
y: i64,
}
impl AxisPoint {
pub const fn new(x: i64, y: i64) -> Self {
Self { x, y }
}
pub const fn x(self) -> i64 {
self.x
}
pub const fn y(self) -> i64 {
self.y
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct TargetBox {
center: AxisPoint,
half_width: u64,
half_height: u64,
}
impl TargetBox {
pub const fn new(center: AxisPoint, half_width: u64, half_height: u64) -> Self {
Self {
center,
half_width,
half_height,
}
}
pub const fn center(self) -> AxisPoint {
self.center
}
pub const fn half_width(self) -> u64 {
self.half_width
}
pub const fn half_height(self) -> u64 {
self.half_height
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VerticalDirection {
Down,
Up,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct VerticalAttack {
origin: AxisPoint,
direction: VerticalDirection,
reach: u64,
half_width: u64,
force: u64,
}
impl VerticalAttack {
pub fn new(
origin: AxisPoint,
direction: VerticalDirection,
reach: u64,
half_width: u64,
force: u64,
) -> Result<Self, AttackError> {
if reach == 0 {
return Err(AttackError::ZeroReach);
}
if force == 0 {
return Err(AttackError::ZeroForce);
}
Ok(Self {
origin,
direction,
reach,
half_width,
force,
})
}
pub const fn origin(self) -> AxisPoint {
self.origin
}
pub const fn direction(self) -> VerticalDirection {
self.direction
}
pub const fn reach(self) -> u64 {
self.reach
}
pub const fn half_width(self) -> u64 {
self.half_width
}
pub const fn force(self) -> u64 {
self.force
}
pub fn resolve(self, target: &TargetBox) -> Result<AttackOutcome, AttackError> {
let horizontal_span = self
.half_width
.checked_add(target.half_width)
.ok_or(AttackError::HorizontalSpanOverflow)?;
let horizontal_offset = self.origin.x.abs_diff(target.center.x);
let centering_basis_points = if horizontal_span == 0 {
if horizontal_offset != 0 {
return Ok(AttackOutcome::Miss(MissReason::HorizontalOutside));
}
10_000
} else {
if horizontal_offset >= horizontal_span {
return Ok(AttackOutcome::Miss(MissReason::HorizontalOutside));
}
let remaining = horizontal_span - horizontal_offset;
u16::try_from(u128::from(remaining) * 10_000 / u128::from(horizontal_span))
.map_err(|_| AttackError::ForceOverflow)?
};
let surface_y = facing_surface(*target, self.direction)?;
let distance = match self.direction {
VerticalDirection::Down => {
if self.origin.y < surface_y {
return Ok(AttackOutcome::Miss(MissReason::TargetBehind));
}
self.origin.y.abs_diff(surface_y)
}
VerticalDirection::Up => {
if self.origin.y > surface_y {
return Ok(AttackOutcome::Miss(MissReason::TargetBehind));
}
self.origin.y.abs_diff(surface_y)
}
};
if distance > self.reach {
return Ok(AttackOutcome::Miss(MissReason::BeyondReach));
}
let transmitted_force =
u128::from(self.force) * u128::from(centering_basis_points) / 10_000;
let transmitted_force =
u64::try_from(transmitted_force).map_err(|_| AttackError::ForceOverflow)?;
let impulse_y = match self.direction {
VerticalDirection::Down => -i128::from(transmitted_force),
VerticalDirection::Up => i128::from(transmitted_force),
};
Ok(AttackOutcome::Hit(Impact {
axis_x: self.origin.x,
contact_y: surface_y,
distance,
centering_basis_points,
transmitted_force,
impulse_y,
}))
}
}
fn facing_surface(target: TargetBox, direction: VerticalDirection) -> Result<i64, AttackError> {
let center = i128::from(target.center.y);
let offset = i128::from(target.half_height);
let surface = match direction {
VerticalDirection::Down => center + offset,
VerticalDirection::Up => center - offset,
};
i64::try_from(surface).map_err(|_| AttackError::SurfaceOverflow)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AttackOutcome {
Hit(Impact),
Miss(MissReason),
}
impl AttackOutcome {
pub const fn is_hit(self) -> bool {
matches!(self, Self::Hit(_))
}
pub const fn impact(self) -> Option<Impact> {
match self {
Self::Hit(impact) => Some(impact),
Self::Miss(_) => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MissReason {
HorizontalOutside,
TargetBehind,
BeyondReach,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Impact {
axis_x: i64,
contact_y: i64,
distance: u64,
centering_basis_points: u16,
transmitted_force: u64,
impulse_y: i128,
}
impl Impact {
pub const fn axis_x(self) -> i64 {
self.axis_x
}
pub const fn contact_y(self) -> i64 {
self.contact_y
}
pub const fn distance(self) -> u64 {
self.distance
}
pub const fn centering_basis_points(self) -> u16 {
self.centering_basis_points
}
pub const fn transmitted_force(self) -> u64 {
self.transmitted_force
}
pub const fn impulse_x(self) -> i128 {
0
}
pub const fn impulse_y(self) -> i128 {
self.impulse_y
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AttackError {
ZeroReach,
ZeroForce,
HorizontalSpanOverflow,
SurfaceOverflow,
ForceOverflow,
}
impl Display for AttackError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::ZeroReach => "vertical attack reach must be greater than zero",
Self::ZeroForce => "vertical attack force must be greater than zero",
Self::HorizontalSpanOverflow => "combined horizontal span overflowed",
Self::SurfaceOverflow => "target facing-surface coordinate overflowed",
Self::ForceOverflow => "centered force arithmetic overflowed",
})
}
}
impl Error for AttackError {}
#[cfg(test)]
mod tests {
use super::*;
fn downward(x: i64) -> VerticalAttack {
VerticalAttack::new(
AxisPoint::new(x, 100),
VerticalDirection::Down,
100,
10,
1_000,
)
.unwrap()
}
fn target(x: i64) -> TargetBox {
TargetBox::new(AxisPoint::new(x, 20), 10, 10)
}
#[test]
fn centered_downward_hit_transfers_full_vertical_force() {
let impact = downward(0).resolve(&target(0)).unwrap().impact().unwrap();
assert_eq!(impact.distance(), 70);
assert_eq!(impact.centering_basis_points(), 10_000);
assert_eq!(impact.transmitted_force(), 1_000);
assert_eq!(impact.impulse_x(), 0);
assert_eq!(impact.impulse_y(), -1_000);
}
#[test]
fn horizontal_offset_reduces_force() {
let impact = downward(10).resolve(&target(0)).unwrap().impact().unwrap();
assert_eq!(impact.centering_basis_points(), 5_000);
assert_eq!(impact.transmitted_force(), 500);
}
#[test]
fn horizontal_boundary_is_a_miss() {
assert_eq!(
downward(20).resolve(&target(0)).unwrap(),
AttackOutcome::Miss(MissReason::HorizontalOutside)
);
}
#[test]
fn target_beyond_reach_is_a_miss() {
let far = TargetBox::new(AxisPoint::new(0, -20), 10, 10);
assert_eq!(
downward(0).resolve(&far).unwrap(),
AttackOutcome::Miss(MissReason::BeyondReach)
);
}
#[test]
fn target_on_wrong_side_is_a_miss() {
let above = TargetBox::new(AxisPoint::new(0, 120), 10, 10);
assert_eq!(
downward(0).resolve(&above).unwrap(),
AttackOutcome::Miss(MissReason::TargetBehind)
);
}
#[test]
fn upward_hit_has_positive_impulse() {
let attack =
VerticalAttack::new(AxisPoint::new(0, -100), VerticalDirection::Up, 100, 5, 200)
.unwrap();
let target = TargetBox::new(AxisPoint::new(0, -20), 5, 10);
let impact = attack.resolve(&target).unwrap().impact().unwrap();
assert_eq!(impact.distance(), 70);
assert_eq!(impact.impulse_y(), 200);
}
#[test]
fn invalid_and_overflowing_geometry_is_rejected() {
assert_eq!(
VerticalAttack::new(AxisPoint::default(), VerticalDirection::Down, 0, 1, 1)
.unwrap_err(),
AttackError::ZeroReach
);
let attack = VerticalAttack::new(
AxisPoint::new(0, i64::MAX),
VerticalDirection::Down,
1,
u64::MAX,
1,
)
.unwrap();
let target = TargetBox::new(AxisPoint::new(0, i64::MAX), 1, 1);
assert_eq!(
attack.resolve(&target).unwrap_err(),
AttackError::HorizontalSpanOverflow
);
let attack = VerticalAttack::new(
AxisPoint::new(0, i64::MAX),
VerticalDirection::Down,
1,
0,
1,
)
.unwrap();
let target = TargetBox::new(AxisPoint::new(0, i64::MAX), 0, 1);
assert_eq!(
attack.resolve(&target).unwrap_err(),
AttackError::SurfaceOverflow
);
}
}