pub type Fixed = i32;
pub type Angle = u32;
pub const FRACBITS: i32 = 16;
pub const FRACUNIT: Fixed = 1 << FRACBITS;
pub const FINEANGLES: usize = 8192;
pub const FINEMASK: usize = FINEANGLES - 1;
pub const ANGLETOFINESHIFT: u32 = 19;
pub const ANG45: Angle = 0x20000000;
pub const ANG90: Angle = 0x40000000;
pub const ANG180: Angle = 0x80000000;
pub const ANG270: Angle = 0xc0000000;
pub const SCREENWIDTH: usize = 320;
pub const SCREENHEIGHT: usize = 200;
pub const TICRATE: u32 = 35;
pub const MAXPLAYERS: usize = 4;
pub const MELEERANGE: Fixed = 64 * FRACUNIT;
pub const MISSILERANGE: Fixed = 2048 * FRACUNIT;
pub const USERANGE: Fixed = 64 * FRACUNIT;
#[inline]
pub fn fixed_mul(a: Fixed, b: Fixed) -> Fixed {
((a as i64 * b as i64) >> FRACBITS) as Fixed
}
#[inline]
pub fn fixed_div(a: Fixed, b: Fixed) -> Fixed {
if b == 0 || (a.unsigned_abs() >> 14) >= b.unsigned_abs() {
return if (a ^ b) < 0 { i32::MIN } else { i32::MAX };
}
(((a as i64) << FRACBITS) / b as i64) as Fixed
}
#[inline(always)]
#[allow(clippy::indexing_slicing)]
pub fn finesine(index: usize) -> Fixed {
crate::tables::FINESINE[index & FINEMASK]
}
#[inline(always)]
#[allow(clippy::indexing_slicing)]
pub fn finecosine(index: usize) -> Fixed {
crate::tables::FINESINE[(index & FINEMASK) + FINEANGLES / 4]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_mul_identity() {
assert_eq!(fixed_mul(FRACUNIT, FRACUNIT), FRACUNIT);
}
#[test]
fn fixed_mul_basic() {
assert_eq!(fixed_mul(2 * FRACUNIT, 3 * FRACUNIT), 6 * FRACUNIT);
}
#[test]
fn fixed_mul_negative() {
assert_eq!(fixed_mul(-2 * FRACUNIT, 3 * FRACUNIT), -6 * FRACUNIT);
}
#[test]
fn fixed_div_basic() {
assert_eq!(fixed_div(6 * FRACUNIT, 3 * FRACUNIT), 2 * FRACUNIT);
}
#[test]
fn fixed_div_overflow() {
let result = fixed_div(i32::MAX, 1);
assert_eq!(result, i32::MAX);
}
#[test]
fn fixed_div_by_zero() {
assert_eq!(fixed_div(FRACUNIT, 0), i32::MAX);
}
#[test]
fn trig_sine_zero() {
assert_eq!(crate::tables::FINESINE[0], 25);
}
#[test]
fn trig_sine_90() {
assert_eq!(crate::tables::FINESINE[FINEANGLES / 4], 65535);
}
#[test]
fn trig_cosine_zero() {
assert_eq!(finecosine(0), 65535);
}
#[test]
fn trig_sine_180() {
assert_eq!(crate::tables::FINESINE[FINEANGLES / 2], -25);
}
}