author | unc0rr |
Mon, 17 Nov 2008 20:35:11 +0000 | |
changeset 1501 | a0e56fdf10cd |
parent 1433 | 4af291d5d79c |
child 1586 | 2c6f1d1f43c1 |
permissions | -rw-r--r-- |
351 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy 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); |
|
1433 | 102 |
_0_998: hwFloat = (isNegative: false; QWordValue: 4294967296); |
351 | 103 |
_0_999: hwFloat = (isNegative: false; QWordValue: 4290672328); |
104 |
_1_9: hwFloat = (isNegative: false; QWordValue: 8160437862); |
|
498 | 105 |
_0: hwFloat = (isNegative: false; QWordValue: 0); |
106 |
_1: hwFloat = (isNegative: false; QWordValue: 4294967296); |
|
513
69e06d710d46
Moving hedgehog could get another hedgehog moving forward
unc0rr
parents:
498
diff
changeset
|
107 |
_1_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3 div 2); |
498 | 108 |
_2: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2); |
109 |
_3: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3); |
|
110 |
_4: hwFloat = (isNegative: false; QWordValue: 4294967296 * 4); |
|
111 |
_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 5); |
|
112 |
_6: hwFloat = (isNegative: false; QWordValue: 4294967296 * 6); |
|
113 |
_10: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10); |
|
114 |
_16: hwFloat = (isNegative: false; QWordValue: 4294967296 * 16); |
|
115 |
_19: hwFloat = (isNegative: false; QWordValue: 4294967296 * 19); |
|
116 |
_20: hwFloat = (isNegative: false; QWordValue: 4294967296 * 20); |
|
117 |
_25: hwFloat = (isNegative: false; QWordValue: 4294967296 * 25); |
|
118 |
_30: hwFloat = (isNegative: false; QWordValue: 4294967296 * 30); |
|
119 |
_128: hwFloat = (isNegative: false; QWordValue: 4294967296 * 128); |
|
1124 | 120 |
_300: hwFloat = (isNegative: false; QWordValue: 4294967296 * 300); |
498 | 121 |
_450: hwFloat = (isNegative: false; QWordValue: 4294967296 * 450); |
611 | 122 |
_1024: hwFloat = (isNegative: false; QWordValue: 4294967296 * 1024); |
123 |
_2048: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2048); |
|
498 | 124 |
_10000: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10000); |
351 | 125 |
|
126 |
cLittle: hwFloat = (isNegative: false; QWordValue: 1); |
|
967 | 127 |
cHHKick: hwFloat = (isNegative: false; QWordValue: 42949673); // _0_01 |
611 | 128 |
{$WARNINGS ON} |
351 | 129 |
{$ENDIF} |
130 |
||
131 |
{$IFNDEF FPC} |
|
132 |
type hwFloat = Extended; |
|
133 |
{$ENDIF} |
|
134 |
||
135 |
implementation |
|
744 | 136 |
uses uConsts, uMisc; |
351 | 137 |
|
138 |
{$IFDEF FPC} |
|
139 |
||
515 | 140 |
function int2hwFloat (const i: LongInt) : hwFloat; |
351 | 141 |
begin |
498 | 142 |
int2hwFloat.isNegative:= i < 0; |
143 |
int2hwFloat.Round:= abs(i); |
|
144 |
int2hwFloat.Frac:= 0 |
|
351 | 145 |
end; |
146 |
||
515 | 147 |
operator + (const z1, z2: hwFloat) z : hwFloat; |
351 | 148 |
begin |
149 |
if z1.isNegative = z2.isNegative then |
|
150 |
begin |
|
151 |
z.isNegative:= z1.isNegative; |
|
152 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
153 |
end |
|
154 |
else |
|
155 |
if z1.QWordValue > z2.QWordValue then |
|
156 |
begin |
|
157 |
z.isNegative:= z1.isNegative; |
|
158 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
159 |
end else |
|
160 |
begin |
|
161 |
z.isNegative:= z2.isNegative; |
|
162 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
163 |
end |
|
164 |
end; |
|
165 |
||
515 | 166 |
operator - (const z1, z2: hwFloat) z : hwFloat; |
351 | 167 |
begin |
168 |
if z1.isNegative = z2.isNegative then |
|
169 |
if z1.QWordValue > z2.QWordValue then |
|
170 |
begin |
|
171 |
z.isNegative:= z1.isNegative; |
|
172 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
173 |
end else |
|
174 |
begin |
|
175 |
z.isNegative:= not z2.isNegative; |
|
176 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
177 |
end |
|
178 |
else begin |
|
179 |
z.isNegative:= z1.isNegative; |
|
180 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
181 |
end |
|
182 |
end; |
|
183 |
||
515 | 184 |
operator - (const z1: hwFloat) z : hwFloat; |
351 | 185 |
begin |
186 |
z:= z1; |
|
187 |
z.isNegative:= not z.isNegative |
|
188 |
end; |
|
189 |
||
190 |
||
515 | 191 |
operator * (const z1, z2: hwFloat) z : hwFloat; |
351 | 192 |
begin |
193 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
194 |
z.QWordValue:= QWord(z1.Round) * z2.Frac + |
|
195 |
QWord(z1.Frac) * z2.Round + |
|
196 |
((QWord(z1.Frac) * z2.Frac) shr 32); |
|
197 |
z.Round:= z.Round + QWord(z1.Round) * z2.Round; |
|
198 |
end; |
|
199 |
||
515 | 200 |
operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
351 | 201 |
begin |
202 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 203 |
z.QWordValue:= z1.QWordValue * abs(z2) |
351 | 204 |
end; |
205 |
||
515 | 206 |
operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; |
351 | 207 |
var t: hwFloat; |
208 |
begin |
|
209 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
210 |
z.Round:= z1.QWordValue div z2.QWordValue; |
|
211 |
t:= z1 - z2 * z.Round; |
|
212 |
if t.QWordValue = 0 then |
|
213 |
z.Frac:= 0 |
|
214 |
else |
|
215 |
begin |
|
216 |
while ((t.QWordValue and $8000000000000000) = 0) and |
|
217 |
((z2.QWordValue and $8000000000000000) = 0) do |
|
218 |
begin |
|
219 |
t.QWordValue:= t.QWordValue shl 1; |
|
220 |
z2.QWordValue:= z2.QWordValue shl 1 |
|
221 |
end; |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
538
diff
changeset
|
222 |
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
|
223 |
else z.Frac:= 0 |
351 | 224 |
end |
225 |
end; |
|
226 |
||
515 | 227 |
operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
498 | 228 |
begin |
229 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 230 |
z.QWordValue:= z1.QWordValue div abs(z2) |
498 | 231 |
end; |
232 |
||
515 | 233 |
operator < (const z1, z2: hwFloat) b : boolean; |
351 | 234 |
begin |
916 | 235 |
if z1.isNegative xor z2.isNegative then |
351 | 236 |
b:= z1.isNegative |
237 |
else |
|
238 |
if z1.QWordValue = z2.QWordValue then |
|
239 |
b:= false |
|
240 |
else |
|
241 |
b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative |
|
242 |
end; |
|
243 |
||
515 | 244 |
operator > (const z1, z2: hwFloat) b : boolean; |
351 | 245 |
begin |
916 | 246 |
if z1.isNegative xor z2.isNegative then |
351 | 247 |
b:= z2.isNegative |
248 |
else |
|
249 |
if z1.QWordValue = z2.QWordValue then |
|
250 |
b:= false |
|
251 |
else |
|
252 |
b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative |
|
253 |
end; |
|
254 |
||
515 | 255 |
function cstr(const z: hwFloat): string; |
351 | 256 |
var tmpstr: string; |
257 |
begin |
|
258 |
str(z.Round, cstr); |
|
259 |
if z.Frac <> 0 then |
|
260 |
begin |
|
261 |
str(z.Frac / $100000000:1:15, tmpstr); |
|
262 |
delete(tmpstr, 1, 2); |
|
263 |
cstr:= cstr + '.' + tmpstr |
|
264 |
end; |
|
265 |
if z.isNegative then cstr:= '-' + cstr |
|
266 |
end; |
|
267 |
||
515 | 268 |
function hwRound(const t: hwFloat): LongInt; |
351 | 269 |
begin |
270 |
if t.isNegative then hwRound:= -t.Round |
|
271 |
else hwRound:= t.Round |
|
272 |
end; |
|
273 |
||
515 | 274 |
function hwAbs(const t: hwFloat): hwFloat; |
351 | 275 |
begin |
276 |
hwAbs:= t; |
|
277 |
hwAbs.isNegative:= false |
|
278 |
end; |
|
279 |
||
515 | 280 |
function hwSqr(const t: hwFloat): hwFloat; |
351 | 281 |
begin |
1433 | 282 |
hwSqr.isNegative:= false; |
283 |
hwSqr.QWordValue:= |
|
284 |
((QWord(t.Round) * t.Round) shl 32) |
|
285 |
+ QWord(t.Round) * t.Frac * 2 |
|
286 |
+ ((QWord(t.Frac) * t.Frac) shr 32); |
|
351 | 287 |
end; |
288 |
||
515 | 289 |
function hwSqrt(const t: hwFloat): hwFloat; |
738 | 290 |
var l, r: QWord; |
291 |
c: hwFloat; |
|
357 | 292 |
begin |
293 |
hwSqrt.isNegative:= false; |
|
744 | 294 |
|
295 |
if t.Round = 0 then |
|
296 |
begin |
|
297 |
l:= t.QWordValue; |
|
298 |
r:= $100000000 |
|
299 |
end else |
|
300 |
begin |
|
301 |
l:= $100000000; |
|
953
237fc147950c
Fix bug in hwSqrt when calculating square root of number >= 65536
unc0rr
parents:
916
diff
changeset
|
302 |
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
|
303 |
if r > $FFFFFFFFFFFF then r:= $FFFFFFFFFFFF |
744 | 304 |
end; |
305 |
||
738 | 306 |
repeat |
307 |
c.QWordValue:= (l + r) div 2; |
|
308 |
if hwSqr(c).QWordValue > t.QWordValue then r:= c.QWordValue else l:= c.QWordValue |
|
309 |
until r - l <= 1; |
|
744 | 310 |
|
738 | 311 |
hwSqrt.QWordValue:= l |
357 | 312 |
end; |
313 |
||
515 | 314 |
function Distance(const dx, dy: hwFloat): hwFloat; |
351 | 315 |
begin |
738 | 316 |
Distance:= hwSqrt(hwSqr(dx) + hwSqr(dy)) |
351 | 317 |
end; |
318 |
||
515 | 319 |
function DistanceI(const dx, dy: LongInt): hwFloat; |
498 | 320 |
begin |
856 | 321 |
DistanceI:= hwSqrt(int2hwFloat(sqr(dx) + sqr(dy))) |
498 | 322 |
end; |
323 |
||
515 | 324 |
function SignAs(const num, signum: hwFloat): hwFloat; |
498 | 325 |
begin |
856 | 326 |
SignAs.QWordValue:= num.QWordValue; |
498 | 327 |
SignAs.isNegative:= signum.isNegative |
328 |
end; |
|
329 |
||
357 | 330 |
{$INCLUDE SinTable.inc} |
331 |
||
515 | 332 |
function AngleSin(const Angle: Longword): hwFloat; |
351 | 333 |
begin |
334 |
AngleSin.isNegative:= false; |
|
357 | 335 |
if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle] |
336 |
else AngleSin.QWordValue:= SinTable[2048 - Angle] |
|
351 | 337 |
end; |
338 |
||
515 | 339 |
function AngleCos(const Angle: Longword): hwFloat; |
351 | 340 |
begin |
357 | 341 |
AngleCos.isNegative:= Angle > 1024; |
342 |
if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle] |
|
343 |
else AngleCos.QWordValue:= SinTable[Angle - 1024] |
|
351 | 344 |
end; |
345 |
||
346 |
{$ENDIF} |
|
347 |
||
348 |
end. |