File size: 4,812 Bytes
aab6d6e
df6d1b6
e454eb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc66c4
 
 
1d3de83
 
 
 
 
cbc66c4
 
 
 
1d3de83
 
 
 
 
cbc66c4
 
 
 
 
 
059b365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e454eb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654bf33
 
 
 
 
 
 
 
 
aab6d6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e454eb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df6d1b6
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use core::ops::{Add, Div, Mul, Neg, Sub};
use std::ops::{AddAssign, SubAssign};

/// 16.16 fixed-point number used throughout WA for coordinates and velocities.
///
/// The game uses `0x10000` (65536) to represent `1.0`.
/// For example, a position of `0x30000` means 3.0 world units.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct Fixed(pub i32);

impl Fixed {
    pub const FRACTIONAL_BITS: u32 = 16;
    pub const SCALE: i32 = 1 << Self::FRACTIONAL_BITS; // 0x10000 = 65536

    pub const ZERO: Self = Self(0);
    pub const ONE: Self = Self(Self::SCALE);

    #[inline]
    pub const fn from_raw(raw: i32) -> Self {
        Self(raw)
    }

    #[inline]
    pub const fn to_raw(self) -> i32 {
        self.0
    }

    #[inline]
    pub const fn from_int(n: i32) -> Self {
        Self(n << Self::FRACTIONAL_BITS)
    }

    #[inline]
    pub const fn to_int(self) -> i32 {
        self.0 >> Self::FRACTIONAL_BITS
    }

    #[inline]
    pub fn from_f32(f: f32) -> Self {
        Self((f * Self::SCALE as f32) as i32)
    }

    #[inline]
    pub fn to_f32(self) -> f32 {
        self.0 as f32 / Self::SCALE as f32
    }

    #[inline]
    pub fn max(self, other: Self) -> Self {
        if self > other {
            self
        } else {
            other
        }
    }

    #[inline]
    pub fn min(self, other: Self) -> Self {
        if self < other {
            self
        } else {
            other
        }
    }

    #[inline]
    pub fn abs(self) -> Self {
        Self(self.0.abs())
    }

    /// Floor to integer boundary: clears the fractional bits.
    /// `Fixed(0x38000).floor()` = `Fixed(0x30000)`.
    #[inline]
    pub const fn floor(self) -> Self {
        Self(self.0 & !0xFFFF)
    }

    /// Pixel center: floor + 0.5. Used by line rasterizers to snap to pixel centers.
    #[inline]
    pub const fn pixel_center(self) -> Self {
        Self((self.0 & !0xFFFF) + 0x8000)
    }

    /// Round to nearest integer (half-up): `(self + 0.5).to_int()`.
    #[inline]
    pub const fn round_to_int(self) -> i32 {
        (self.0 + 0x8000) >> Self::FRACTIONAL_BITS
    }

    /// Fixed-point division for line clipping: `(self << 16) / rhs`.
    ///
    /// This is NOT the same as `Fixed / Fixed` (which is `(self << 16) / rhs`
    /// treating both as Fixed). This divides two Fixed values and returns a
    /// Fixed-point ratio suitable for interpolation.
    #[inline]
    pub fn div_raw(self, rhs: Self) -> Self {
        if rhs.0 == 0 {
            return Self::ZERO;
        }
        Self((((self.0 as i64) << 16) / rhs.0 as i64) as i32)
    }

    /// Fixed-point multiply returning Fixed: `(self * rhs) >> 16`.
    #[inline]
    pub fn mul_raw(self, rhs: Self) -> Self {
        Self(((self.0 as i64 * rhs.0 as i64) >> 16) as i32)
    }

    /// Half a pixel (0.5 in Fixed).
    pub const HALF: Self = Self(0x8000);
}

impl Add for Fixed {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        Self(self.0 + rhs.0)
    }
}

impl Sub for Fixed {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Self(self.0 - rhs.0)
    }
}

impl Mul for Fixed {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        // Use i64 to avoid overflow during multiplication
        Self(((self.0 as i64 * rhs.0 as i64) >> Self::FRACTIONAL_BITS) as i32)
    }
}

/// Scale a Fixed value by a plain integer (no shift — just `raw * n`).
impl Mul<i32> for Fixed {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: i32) -> Self {
        Self(self.0 * rhs)
    }
}

impl Div for Fixed {
    type Output = Self;
    /// Fixed-point division: (self << 16) / rhs, using i64 intermediate.
    #[inline]
    fn div(self, rhs: Self) -> Self {
        Self((((self.0 as i64) << Self::FRACTIONAL_BITS) / rhs.0 as i64) as i32)
    }
}

/// Divide a Fixed value by a plain integer (no shift — just `raw / n`).
impl Div<i32> for Fixed {
    type Output = Self;
    #[inline]
    fn div(self, rhs: i32) -> Self {
        Self(self.0 / rhs)
    }
}

impl Neg for Fixed {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        Self(-self.0)
    }
}

impl core::fmt::Debug for Fixed {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Fixed({:.4} raw=0x{:08x})", self.to_f32(), self.0)
    }
}

impl core::fmt::Display for Fixed {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:.4}", self.to_f32())
    }
}

impl AddAssign for Fixed {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl SubAssign for Fixed {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
    }
}