author | unc0rr |
Tue, 01 Jul 2008 15:52:10 +0000 | |
changeset 1042 | a0ca4b3d2782 |
parent 967 | 8be3938d73c2 |
child 1066 | 1f1b3686a2b0 |
permissions | -rw-r--r-- |
351 | 1 |
(* |
2 |
* Hedgewars, a Worms style game |
|
883 | 3 |
* Copyright (c) 2007, 2008 Andrey Korotaev <unC0Rr@gmail.com> |
351 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
19 |
unit uFloat; |
|
20 |
interface |
|
21 |
||
22 |
{$IFDEF FPC} |
|
23 |
{$ifdef FPC_LITTLE_ENDIAN} |
|
24 |
type hwFloat = record |
|
25 |
isNegative: boolean; |
|
26 |
case byte of |
|
27 |
0: (Frac, Round: Longword); |
|
28 |
1: (QWordValue : QWord); |
|
29 |
end; |
|
30 |
{$else FPC_LITTLE_ENDIAN} |
|
31 |
type hwFloat = record |
|
32 |
isNegative: boolean; |
|
33 |
case byte of |
|
34 |
0: (Round, Frac: Longword); |
|
35 |
1: (QWordValue : QWord); |
|
36 |
end; |
|
37 |
{$endif FPC_LITTLE_ENDIAN} |
|
38 |
||
515 | 39 |
function int2hwFloat (const i: LongInt) : hwFloat; |
351 | 40 |
|
515 | 41 |
operator + (const z1, z2: hwFloat) z : hwFloat; |
42 |
operator - (const z1, z2: hwFloat) z : hwFloat; |
|
43 |
operator - (const z1: hwFloat) z : hwFloat; |
|
351 | 44 |
|
515 | 45 |
operator * (const z1, z2: hwFloat) z : hwFloat; |
46 |
operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
|
47 |
operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; |
|
48 |
operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
|
351 | 49 |
|
515 | 50 |
operator < (const z1, z2: hwFloat) b : boolean; |
51 |
operator > (const z1, z2: hwFloat) b : boolean; |
|
351 | 52 |
|
515 | 53 |
function cstr(const z: hwFloat): string; |
54 |
function hwRound(const t: hwFloat): LongInt; |
|
55 |
function hwAbs(const t: hwFloat): hwFloat; |
|
56 |
function hwSqr(const t: hwFloat): hwFloat; |
|
57 |
function hwSqrt(const t: hwFloat): hwFloat; |
|
58 |
function Distance(const dx, dy: hwFloat): hwFloat; |
|
59 |
function DistanceI(const dx, dy: LongInt): hwFloat; |
|
60 |
function AngleSin(const Angle: Longword): hwFloat; |
|
61 |
function AngleCos(const Angle: Longword): hwFloat; |
|
62 |
function SignAs(const num, signum: hwFloat): hwFloat; |
|
351 | 63 |
|
538 | 64 |
{$J-} |
611 | 65 |
{$WARNINGS OFF} |
351 | 66 |
const _1div1024: hwFloat = (isNegative: false; QWordValue: 4194304); |
67 |
_1div10000: hwFloat = (isNegative: false; QWordValue: 429496); |
|
68 |
_1div50000: hwFloat = (isNegative: false; QWordValue: 85899); |
|
69 |
_1div100000: hwFloat = (isNegative: false; QWordValue: 42950); |
|
70 |
_1div3: hwFloat = (isNegative: false; QWordValue: 1431655766); |
|
71 |
hwPi: hwFloat = (isNegative: false; QWordValue: 13493037704); |
|
72 |
_0_000004: hwFloat = (isNegative: false; QWordValue: 17179); |
|
73 |
_0_0002: hwFloat = (isNegative: false; QWordValue: 858993); |
|
74 |
_0_001: hwFloat = (isNegative: false; QWordValue: 4294967); |
|
75 |
_0_003: hwFloat = (isNegative: false; QWordValue: 12884902); |
|
76 |
_0_004: hwFloat = (isNegative: false; QWordValue: 17179869); |
|
77 |
_0_005: hwFloat = (isNegative: false; QWordValue: 21474836); |
|
78 |
_0_01: hwFloat = (isNegative: false; QWordValue: 42949673); |
|
79 |
_0_02: hwFloat = (isNegative: false; QWordValue: 85899345); |
|
80 |
_0_03: hwFloat = (isNegative: false; QWordValue: 128849018); |
|
81 |
_0_08: hwFloat = (isNegative: false; QWordValue: 343597383); |
|
365 | 82 |
_0_1: hwFloat = (isNegative: false; QWordValue: 429496730); |
351 | 83 |
_0_15: hwFloat = (isNegative: false; QWordValue: 644245094); |
84 |
_0_2: hwFloat = (isNegative: false; QWordValue: 858993459); |
|
85 |
_0_25: hwFloat = (isNegative: false; QWordValue: 1073741824); |
|
86 |
_0_3: hwFloat = (isNegative: false; QWordValue: 1288490189); |
|
87 |
_0_35: hwFloat = (isNegative: false; QWordValue: 1503238553); |
|
835
6f567934cc44
Automatically use parachute when vertical speed is high enough
unc0rr
parents:
745
diff
changeset
|
88 |
_0_39: hwFloat = (isNegative: false; QWordValue: 1675037245); |
351 | 89 |
_0_4: hwFloat = (isNegative: false; QWordValue: 1717986918); |
90 |
_0_45: hwFloat = (isNegative: false; QWordValue: 1932735283); |
|
91 |
_0_5: hwFloat = (isNegative: false; QWordValue: 2147483648); |
|
92 |
_0_55: hwFloat = (isNegative: false; QWordValue: 2362232012); |
|
93 |
_0_6: hwFloat = (isNegative: false; QWordValue: 2576980377); |
|
358 | 94 |
_0_7: hwFloat = (isNegative: false; QWordValue: 3006477107); |
351 | 95 |
_0_8: hwFloat = (isNegative: false; QWordValue: 3435973837); |
96 |
_0_84: hwFloat = (isNegative: false; QWordValue: 3607772528); |
|
97 |
_0_87: hwFloat = (isNegative: false; QWordValue: 3736621547); |
|
98 |
_0_9: hwFloat = (isNegative: false; QWordValue: 3865470566); |
|
99 |
_0_93: hwFloat = (isNegative: false; QWordValue: 3994319585); |
|
100 |
_0_96: hwFloat = (isNegative: false; QWordValue: 4123168604); |
|
101 |
_0_995: hwFloat = (isNegative: false; QWordValue: 4273492459); |
|
102 |
_0_999: hwFloat = (isNegative: false; QWordValue: 4290672328); |
|
103 |
_1_9: hwFloat = (isNegative: false; QWordValue: 8160437862); |
|
498 | 104 |
_0: hwFloat = (isNegative: false; QWordValue: 0); |
105 |
_1: hwFloat = (isNegative: false; QWordValue: 4294967296); |
|
513
69e06d710d46
Moving hedgehog could get another hedgehog moving forward
unc0rr
parents:
498
diff
changeset
|
106 |
_1_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3 div 2); |
498 | 107 |
_2: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2); |
108 |
_3: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3); |
|
109 |
_4: hwFloat = (isNegative: false; QWordValue: 4294967296 * 4); |
|
110 |
_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 5); |
|
111 |
_6: hwFloat = (isNegative: false; QWordValue: 4294967296 * 6); |
|
112 |
_10: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10); |
|
113 |
_16: hwFloat = (isNegative: false; QWordValue: 4294967296 * 16); |
|
114 |
_19: hwFloat = (isNegative: false; QWordValue: 4294967296 * 19); |
|
115 |
_20: hwFloat = (isNegative: false; QWordValue: 4294967296 * 20); |
|
116 |
_25: hwFloat = (isNegative: false; QWordValue: 4294967296 * 25); |
|
117 |
_30: hwFloat = (isNegative: false; QWordValue: 4294967296 * 30); |
|
118 |
_128: hwFloat = (isNegative: false; QWordValue: 4294967296 * 128); |
|
119 |
_450: hwFloat = (isNegative: false; QWordValue: 4294967296 * 450); |
|
611 | 120 |
_1024: hwFloat = (isNegative: false; QWordValue: 4294967296 * 1024); |
121 |
_2048: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2048); |
|
498 | 122 |
_10000: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10000); |
351 | 123 |
|
124 |
cLittle: hwFloat = (isNegative: false; QWordValue: 1); |
|
967 | 125 |
cHHKick: hwFloat = (isNegative: false; QWordValue: 42949673); // _0_01 |
611 | 126 |
{$WARNINGS ON} |
351 | 127 |
{$ENDIF} |
128 |
||
129 |
{$IFNDEF FPC} |
|
130 |
type hwFloat = Extended; |
|
131 |
{$ENDIF} |
|
132 |
||
133 |
implementation |
|
744 | 134 |
uses uConsts, uMisc; |
351 | 135 |
|
136 |
{$IFDEF FPC} |
|
137 |
||
515 | 138 |
function int2hwFloat (const i: LongInt) : hwFloat; |
351 | 139 |
begin |
498 | 140 |
int2hwFloat.isNegative:= i < 0; |
141 |
int2hwFloat.Round:= abs(i); |
|
142 |
int2hwFloat.Frac:= 0 |
|
351 | 143 |
end; |
144 |
||
515 | 145 |
operator + (const z1, z2: hwFloat) z : hwFloat; |
351 | 146 |
begin |
147 |
if z1.isNegative = z2.isNegative then |
|
148 |
begin |
|
149 |
z.isNegative:= z1.isNegative; |
|
150 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
151 |
end |
|
152 |
else |
|
153 |
if z1.QWordValue > z2.QWordValue then |
|
154 |
begin |
|
155 |
z.isNegative:= z1.isNegative; |
|
156 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
157 |
end else |
|
158 |
begin |
|
159 |
z.isNegative:= z2.isNegative; |
|
160 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
161 |
end |
|
162 |
end; |
|
163 |
||
515 | 164 |
operator - (const z1, z2: hwFloat) z : hwFloat; |
351 | 165 |
begin |
166 |
if z1.isNegative = z2.isNegative then |
|
167 |
if z1.QWordValue > z2.QWordValue then |
|
168 |
begin |
|
169 |
z.isNegative:= z1.isNegative; |
|
170 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
171 |
end else |
|
172 |
begin |
|
173 |
z.isNegative:= not z2.isNegative; |
|
174 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
175 |
end |
|
176 |
else begin |
|
177 |
z.isNegative:= z1.isNegative; |
|
178 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
179 |
end |
|
180 |
end; |
|
181 |
||
515 | 182 |
operator - (const z1: hwFloat) z : hwFloat; |
351 | 183 |
begin |
184 |
z:= z1; |
|
185 |
z.isNegative:= not z.isNegative |
|
186 |
end; |
|
187 |
||
188 |
||
515 | 189 |
operator * (const z1, z2: hwFloat) z : hwFloat; |
351 | 190 |
begin |
191 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
192 |
z.QWordValue:= QWord(z1.Round) * z2.Frac + |
|
193 |
QWord(z1.Frac) * z2.Round + |
|
194 |
((QWord(z1.Frac) * z2.Frac) shr 32); |
|
195 |
z.Round:= z.Round + QWord(z1.Round) * z2.Round; |
|
196 |
end; |
|
197 |
||
515 | 198 |
operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
351 | 199 |
begin |
200 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 201 |
z.QWordValue:= z1.QWordValue * abs(z2) |
351 | 202 |
end; |
203 |
||
515 | 204 |
operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; |
351 | 205 |
var t: hwFloat; |
206 |
begin |
|
207 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
208 |
z.Round:= z1.QWordValue div z2.QWordValue; |
|
209 |
t:= z1 - z2 * z.Round; |
|
210 |
if t.QWordValue = 0 then |
|
211 |
z.Frac:= 0 |
|
212 |
else |
|
213 |
begin |
|
214 |
while ((t.QWordValue and $8000000000000000) = 0) and |
|
215 |
((z2.QWordValue and $8000000000000000) = 0) do |
|
216 |
begin |
|
217 |
t.QWordValue:= t.QWordValue shl 1; |
|
218 |
z2.QWordValue:= z2.QWordValue shl 1 |
|
219 |
end; |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
538
diff
changeset
|
220 |
if z2.Round > 0 then z.Frac:= (t.QWordValue) div (z2.Round) |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
538
diff
changeset
|
221 |
else z.Frac:= 0 |
351 | 222 |
end |
223 |
end; |
|
224 |
||
515 | 225 |
operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
498 | 226 |
begin |
227 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 228 |
z.QWordValue:= z1.QWordValue div abs(z2) |
498 | 229 |
end; |
230 |
||
515 | 231 |
operator < (const z1, z2: hwFloat) b : boolean; |
351 | 232 |
begin |
916 | 233 |
if z1.isNegative xor z2.isNegative then |
351 | 234 |
b:= z1.isNegative |
235 |
else |
|
236 |
if z1.QWordValue = z2.QWordValue then |
|
237 |
b:= false |
|
238 |
else |
|
239 |
b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative |
|
240 |
end; |
|
241 |
||
515 | 242 |
operator > (const z1, z2: hwFloat) b : boolean; |
351 | 243 |
begin |
916 | 244 |
if z1.isNegative xor z2.isNegative then |
351 | 245 |
b:= z2.isNegative |
246 |
else |
|
247 |
if z1.QWordValue = z2.QWordValue then |
|
248 |
b:= false |
|
249 |
else |
|
250 |
b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative |
|
251 |
end; |
|
252 |
||
515 | 253 |
function cstr(const z: hwFloat): string; |
351 | 254 |
var tmpstr: string; |
255 |
begin |
|
256 |
str(z.Round, cstr); |
|
257 |
if z.Frac <> 0 then |
|
258 |
begin |
|
259 |
str(z.Frac / $100000000:1:15, tmpstr); |
|
260 |
delete(tmpstr, 1, 2); |
|
261 |
cstr:= cstr + '.' + tmpstr |
|
262 |
end; |
|
263 |
if z.isNegative then cstr:= '-' + cstr |
|
264 |
end; |
|
265 |
||
515 | 266 |
function hwRound(const t: hwFloat): LongInt; |
351 | 267 |
begin |
268 |
if t.isNegative then hwRound:= -t.Round |
|
269 |
else hwRound:= t.Round |
|
270 |
end; |
|
271 |
||
515 | 272 |
function hwAbs(const t: hwFloat): hwFloat; |
351 | 273 |
begin |
274 |
hwAbs:= t; |
|
275 |
hwAbs.isNegative:= false |
|
276 |
end; |
|
277 |
||
515 | 278 |
function hwSqr(const t: hwFloat): hwFloat; |
351 | 279 |
begin |
738 | 280 |
hwSqr.isNegative:=false; |
281 |
hwSqr.QWordValue:= QWord(t.Round) * t.Frac * 2 + ((QWord(t.Frac) * t.Frac) shr 32); |
|
953
237fc147950c
Fix bug in hwSqrt when calculating square root of number >= 65536
unc0rr
parents:
916
diff
changeset
|
282 |
hwSqr.Round:= hwSqr.Round + t.Round * t.Round |
351 | 283 |
end; |
284 |
||
515 | 285 |
function hwSqrt(const t: hwFloat): hwFloat; |
738 | 286 |
var l, r: QWord; |
287 |
c: hwFloat; |
|
357 | 288 |
begin |
289 |
hwSqrt.isNegative:= false; |
|
744 | 290 |
|
291 |
if t.Round = 0 then |
|
292 |
begin |
|
293 |
l:= t.QWordValue; |
|
294 |
r:= $100000000 |
|
295 |
end else |
|
296 |
begin |
|
297 |
l:= $100000000; |
|
953
237fc147950c
Fix bug in hwSqrt when calculating square root of number >= 65536
unc0rr
parents:
916
diff
changeset
|
298 |
r:= t.QWordValue div 2 + $80000000; // r:= t / 2 + 0.5 |
237fc147950c
Fix bug in hwSqrt when calculating square root of number >= 65536
unc0rr
parents:
916
diff
changeset
|
299 |
if r > $FFFFFFFFFFFF then r:= $FFFFFFFFFFFF |
744 | 300 |
end; |
301 |
||
738 | 302 |
repeat |
303 |
c.QWordValue:= (l + r) div 2; |
|
304 |
if hwSqr(c).QWordValue > t.QWordValue then r:= c.QWordValue else l:= c.QWordValue |
|
305 |
until r - l <= 1; |
|
744 | 306 |
|
738 | 307 |
hwSqrt.QWordValue:= l |
357 | 308 |
end; |
309 |
||
515 | 310 |
function Distance(const dx, dy: hwFloat): hwFloat; |
351 | 311 |
begin |
738 | 312 |
Distance:= hwSqrt(hwSqr(dx) + hwSqr(dy)) |
351 | 313 |
end; |
314 |
||
515 | 315 |
function DistanceI(const dx, dy: LongInt): hwFloat; |
498 | 316 |
begin |
856 | 317 |
DistanceI:= hwSqrt(int2hwFloat(sqr(dx) + sqr(dy))) |
498 | 318 |
end; |
319 |
||
515 | 320 |
function SignAs(const num, signum: hwFloat): hwFloat; |
498 | 321 |
begin |
856 | 322 |
SignAs.QWordValue:= num.QWordValue; |
498 | 323 |
SignAs.isNegative:= signum.isNegative |
324 |
end; |
|
325 |
||
357 | 326 |
{$INCLUDE SinTable.inc} |
327 |
||
515 | 328 |
function AngleSin(const Angle: Longword): hwFloat; |
351 | 329 |
begin |
330 |
AngleSin.isNegative:= false; |
|
357 | 331 |
if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle] |
332 |
else AngleSin.QWordValue:= SinTable[2048 - Angle] |
|
351 | 333 |
end; |
334 |
||
515 | 335 |
function AngleCos(const Angle: Longword): hwFloat; |
351 | 336 |
begin |
357 | 337 |
AngleCos.isNegative:= Angle > 1024; |
338 |
if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle] |
|
339 |
else AngleCos.QWordValue:= SinTable[Angle - 1024] |
|
351 | 340 |
end; |
341 |
||
342 |
{$ENDIF} |
|
343 |
||
344 |
end. |