author | zuloo |
Sat, 21 Aug 2010 17:19:52 +0200 | |
changeset 3748 | daea2650a5aa |
parent 3599 | 715a43602da8 |
child 3929 | 9a4bbc1f67a2 |
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 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
351 | 21 |
unit uFloat; |
22 |
interface |
|
23 |
||
24 |
{$IFDEF FPC} |
|
3599 | 25 |
{$INLINE ON} |
2599 | 26 |
{$IFDEF ENDIAN_LITTLE} |
351 | 27 |
type hwFloat = record |
28 |
isNegative: boolean; |
|
29 |
case byte of |
|
30 |
0: (Frac, Round: Longword); |
|
31 |
1: (QWordValue : QWord); |
|
32 |
end; |
|
2599 | 33 |
{$ELSE} |
351 | 34 |
type hwFloat = record |
35 |
isNegative: boolean; |
|
36 |
case byte of |
|
37 |
0: (Round, Frac: Longword); |
|
38 |
1: (QWordValue : QWord); |
|
39 |
end; |
|
2599 | 40 |
{$ENDIF} |
351 | 41 |
|
3599 | 42 |
function int2hwFloat (const i: LongInt) : hwFloat; inline; |
351 | 43 |
|
3599 | 44 |
operator + (const z1, z2: hwFloat) z : hwFloat; inline; |
45 |
operator - (const z1, z2: hwFloat) z : hwFloat; inline; |
|
515 | 46 |
operator - (const z1: hwFloat) z : hwFloat; |
351 | 47 |
|
515 | 48 |
operator * (const z1, z2: hwFloat) z : hwFloat; |
3599 | 49 |
operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; inline; |
515 | 50 |
operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; |
51 |
operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
|
351 | 52 |
|
515 | 53 |
operator < (const z1, z2: hwFloat) b : boolean; |
54 |
operator > (const z1, z2: hwFloat) b : boolean; |
|
351 | 55 |
|
2905 | 56 |
function cstr(const z: hwFloat): shortstring; |
515 | 57 |
function hwRound(const t: hwFloat): LongInt; |
58 |
function hwAbs(const t: hwFloat): hwFloat; |
|
3599 | 59 |
function hwSqr(const t: hwFloat): hwFloat; inline; |
60 |
function hwSqrt(const t: hwFloat): hwFloat; inline; |
|
515 | 61 |
function Distance(const dx, dy: hwFloat): hwFloat; |
62 |
function DistanceI(const dx, dy: LongInt): hwFloat; |
|
63 |
function AngleSin(const Angle: Longword): hwFloat; |
|
64 |
function AngleCos(const Angle: Longword): hwFloat; |
|
65 |
function SignAs(const num, signum: hwFloat): hwFloat; |
|
351 | 66 |
|
2599 | 67 |
{$IFDEF FPC} |
538 | 68 |
{$J-} |
2599 | 69 |
{$ENDIF} |
611 | 70 |
{$WARNINGS OFF} |
351 | 71 |
const _1div1024: hwFloat = (isNegative: false; QWordValue: 4194304); |
72 |
_1div10000: hwFloat = (isNegative: false; QWordValue: 429496); |
|
73 |
_1div50000: hwFloat = (isNegative: false; QWordValue: 85899); |
|
74 |
_1div100000: hwFloat = (isNegative: false; QWordValue: 42950); |
|
75 |
_1div3: hwFloat = (isNegative: false; QWordValue: 1431655766); |
|
76 |
hwPi: hwFloat = (isNegative: false; QWordValue: 13493037704); |
|
77 |
_0_000004: hwFloat = (isNegative: false; QWordValue: 17179); |
|
3591 | 78 |
_0_000064: hwFloat = (isNegative: false; QWordValue: 274878); |
351 | 79 |
_0_0002: hwFloat = (isNegative: false; QWordValue: 858993); |
1586 | 80 |
_0_0005: hwFloat = (isNegative: false; QWordValue: 2147484); |
351 | 81 |
_0_001: hwFloat = (isNegative: false; QWordValue: 4294967); |
82 |
_0_003: hwFloat = (isNegative: false; QWordValue: 12884902); |
|
83 |
_0_004: hwFloat = (isNegative: false; QWordValue: 17179869); |
|
84 |
_0_005: hwFloat = (isNegative: false; QWordValue: 21474836); |
|
3428 | 85 |
_0_008: hwFloat = (isNegative: false; QWordValue: 34359738); |
351 | 86 |
_0_01: hwFloat = (isNegative: false; QWordValue: 42949673); |
87 |
_0_02: hwFloat = (isNegative: false; QWordValue: 85899345); |
|
88 |
_0_03: hwFloat = (isNegative: false; QWordValue: 128849018); |
|
89 |
_0_08: hwFloat = (isNegative: false; QWordValue: 343597383); |
|
365 | 90 |
_0_1: hwFloat = (isNegative: false; QWordValue: 429496730); |
351 | 91 |
_0_15: hwFloat = (isNegative: false; QWordValue: 644245094); |
92 |
_0_2: hwFloat = (isNegative: false; QWordValue: 858993459); |
|
93 |
_0_25: hwFloat = (isNegative: false; QWordValue: 1073741824); |
|
94 |
_0_3: hwFloat = (isNegative: false; QWordValue: 1288490189); |
|
95 |
_0_35: hwFloat = (isNegative: false; QWordValue: 1503238553); |
|
2784 | 96 |
_0_375: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3 div 8); |
835
6f567934cc44
Automatically use parachute when vertical speed is high enough
unc0rr
parents:
745
diff
changeset
|
97 |
_0_39: hwFloat = (isNegative: false; QWordValue: 1675037245); |
351 | 98 |
_0_4: hwFloat = (isNegative: false; QWordValue: 1717986918); |
99 |
_0_45: hwFloat = (isNegative: false; QWordValue: 1932735283); |
|
100 |
_0_5: hwFloat = (isNegative: false; QWordValue: 2147483648); |
|
101 |
_0_55: hwFloat = (isNegative: false; QWordValue: 2362232012); |
|
102 |
_0_6: hwFloat = (isNegative: false; QWordValue: 2576980377); |
|
3583 | 103 |
_0_64: hwFloat = (isNegative: false; QWordValue: 2748779064); |
358 | 104 |
_0_7: hwFloat = (isNegative: false; QWordValue: 3006477107); |
351 | 105 |
_0_8: hwFloat = (isNegative: false; QWordValue: 3435973837); |
106 |
_0_84: hwFloat = (isNegative: false; QWordValue: 3607772528); |
|
107 |
_0_87: hwFloat = (isNegative: false; QWordValue: 3736621547); |
|
108 |
_0_9: hwFloat = (isNegative: false; QWordValue: 3865470566); |
|
109 |
_0_93: hwFloat = (isNegative: false; QWordValue: 3994319585); |
|
110 |
_0_96: hwFloat = (isNegative: false; QWordValue: 4123168604); |
|
111 |
_0_995: hwFloat = (isNegative: false; QWordValue: 4273492459); |
|
112 |
_0_999: hwFloat = (isNegative: false; QWordValue: 4290672328); |
|
498 | 113 |
_0: hwFloat = (isNegative: false; QWordValue: 0); |
114 |
_1: hwFloat = (isNegative: false; QWordValue: 4294967296); |
|
513
69e06d710d46
Moving hedgehog could get another hedgehog moving forward
unc0rr
parents:
498
diff
changeset
|
115 |
_1_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3 div 2); |
3584 | 116 |
_1_9: hwFloat = (isNegative: false; QWordValue: 8160437862); |
498 | 117 |
_2: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2); |
118 |
_3: hwFloat = (isNegative: false; QWordValue: 4294967296 * 3); |
|
119 |
_4: hwFloat = (isNegative: false; QWordValue: 4294967296 * 4); |
|
120 |
_5: hwFloat = (isNegative: false; QWordValue: 4294967296 * 5); |
|
121 |
_6: hwFloat = (isNegative: false; QWordValue: 4294967296 * 6); |
|
122 |
_10: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10); |
|
3422
41ae3c48faa0
* some changes/cleanups to portal, still much to do :/ * reverted nemo's temporary loop fix * notice: small loops possible again, so take care :P, bigger onces should be interrupted
sheepluva
parents:
3407
diff
changeset
|
123 |
_12: hwFloat = (isNegative: false; QWordValue: 4294967296 * 12); |
498 | 124 |
_16: hwFloat = (isNegative: false; QWordValue: 4294967296 * 16); |
125 |
_19: hwFloat = (isNegative: false; QWordValue: 4294967296 * 19); |
|
126 |
_20: hwFloat = (isNegative: false; QWordValue: 4294967296 * 20); |
|
127 |
_25: hwFloat = (isNegative: false; QWordValue: 4294967296 * 25); |
|
128 |
_30: hwFloat = (isNegative: false; QWordValue: 4294967296 * 30); |
|
2955
fb361d137524
Tweak to joke in french locale (everyone always fixes the spelling) updated explosive frames from Palewolf, increase explosive fall damage from 30 to 40
nemo
parents:
2948
diff
changeset
|
129 |
_40: hwFloat = (isNegative: false; QWordValue: 4294967296 * 40); |
3036
c6ba6531cb4b
Make barrels a little more likely to blow up. 25% more damage in fall
nemo
parents:
2955
diff
changeset
|
130 |
_50: hwFloat = (isNegative: false; QWordValue: 4294967296 * 50); |
2933 | 131 |
_70: hwFloat = (isNegative: false; QWordValue: 4294967296 * 70); |
498 | 132 |
_128: hwFloat = (isNegative: false; QWordValue: 4294967296 * 128); |
1915 | 133 |
_256: hwFloat = (isNegative: false; QWordValue: 4294967296 * 256); |
1124 | 134 |
_300: hwFloat = (isNegative: false; QWordValue: 4294967296 * 300); |
498 | 135 |
_450: hwFloat = (isNegative: false; QWordValue: 4294967296 * 450); |
611 | 136 |
_1024: hwFloat = (isNegative: false; QWordValue: 4294967296 * 1024); |
137 |
_2048: hwFloat = (isNegative: false; QWordValue: 4294967296 * 2048); |
|
1753 | 138 |
_4096: hwFloat = (isNegative: false; QWordValue: 4294967296 * 4096); |
498 | 139 |
_10000: hwFloat = (isNegative: false; QWordValue: 4294967296 * 10000); |
351 | 140 |
|
141 |
cLittle: hwFloat = (isNegative: false; QWordValue: 1); |
|
967 | 142 |
cHHKick: hwFloat = (isNegative: false; QWordValue: 42949673); // _0_01 |
611 | 143 |
{$WARNINGS ON} |
351 | 144 |
{$ENDIF} |
145 |
||
146 |
{$IFNDEF FPC} |
|
147 |
type hwFloat = Extended; |
|
148 |
{$ENDIF} |
|
149 |
||
150 |
implementation |
|
3407 | 151 |
uses uMisc; |
351 | 152 |
|
3599 | 153 |
|
351 | 154 |
{$IFDEF FPC} |
155 |
||
515 | 156 |
function int2hwFloat (const i: LongInt) : hwFloat; |
351 | 157 |
begin |
498 | 158 |
int2hwFloat.isNegative:= i < 0; |
159 |
int2hwFloat.Round:= abs(i); |
|
160 |
int2hwFloat.Frac:= 0 |
|
351 | 161 |
end; |
162 |
||
515 | 163 |
operator + (const z1, z2: hwFloat) z : hwFloat; |
351 | 164 |
begin |
165 |
if z1.isNegative = z2.isNegative then |
|
166 |
begin |
|
167 |
z.isNegative:= z1.isNegative; |
|
168 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
169 |
end |
|
170 |
else |
|
171 |
if z1.QWordValue > z2.QWordValue then |
|
172 |
begin |
|
173 |
z.isNegative:= z1.isNegative; |
|
174 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
175 |
end else |
|
176 |
begin |
|
177 |
z.isNegative:= z2.isNegative; |
|
178 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
179 |
end |
|
180 |
end; |
|
181 |
||
515 | 182 |
operator - (const z1, z2: hwFloat) z : hwFloat; |
351 | 183 |
begin |
184 |
if z1.isNegative = z2.isNegative then |
|
185 |
if z1.QWordValue > z2.QWordValue then |
|
186 |
begin |
|
187 |
z.isNegative:= z1.isNegative; |
|
188 |
z.QWordValue:= z1.QWordValue - z2.QWordValue |
|
189 |
end else |
|
190 |
begin |
|
191 |
z.isNegative:= not z2.isNegative; |
|
192 |
z.QWordValue:= z2.QWordValue - z1.QWordValue |
|
193 |
end |
|
194 |
else begin |
|
195 |
z.isNegative:= z1.isNegative; |
|
196 |
z.QWordValue:= z1.QWordValue + z2.QWordValue |
|
197 |
end |
|
198 |
end; |
|
199 |
||
515 | 200 |
operator - (const z1: hwFloat) z : hwFloat; |
351 | 201 |
begin |
202 |
z:= z1; |
|
203 |
z.isNegative:= not z.isNegative |
|
204 |
end; |
|
205 |
||
206 |
||
515 | 207 |
operator * (const z1, z2: hwFloat) z : hwFloat; |
351 | 208 |
begin |
209 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
210 |
z.QWordValue:= QWord(z1.Round) * z2.Frac + |
|
211 |
QWord(z1.Frac) * z2.Round + |
|
212 |
((QWord(z1.Frac) * z2.Frac) shr 32); |
|
213 |
z.Round:= z.Round + QWord(z1.Round) * z2.Round; |
|
214 |
end; |
|
215 |
||
515 | 216 |
operator * (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
351 | 217 |
begin |
218 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 219 |
z.QWordValue:= z1.QWordValue * abs(z2) |
351 | 220 |
end; |
221 |
||
515 | 222 |
operator / (const z1: hwFloat; z2: hwFloat) z : hwFloat; |
351 | 223 |
var t: hwFloat; |
224 |
begin |
|
225 |
z.isNegative:= z1.isNegative xor z2.isNegative; |
|
226 |
z.Round:= z1.QWordValue div z2.QWordValue; |
|
227 |
t:= z1 - z2 * z.Round; |
|
228 |
if t.QWordValue = 0 then |
|
229 |
z.Frac:= 0 |
|
230 |
else |
|
231 |
begin |
|
232 |
while ((t.QWordValue and $8000000000000000) = 0) and |
|
233 |
((z2.QWordValue and $8000000000000000) = 0) do |
|
234 |
begin |
|
235 |
t.QWordValue:= t.QWordValue shl 1; |
|
236 |
z2.QWordValue:= z2.QWordValue shl 1 |
|
237 |
end; |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
538
diff
changeset
|
238 |
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
|
239 |
else z.Frac:= 0 |
351 | 240 |
end |
241 |
end; |
|
242 |
||
515 | 243 |
operator / (const z1: hwFloat; const z2: LongInt) z : hwFloat; |
498 | 244 |
begin |
245 |
z.isNegative:= z1.isNegative xor (z2 < 0); |
|
515 | 246 |
z.QWordValue:= z1.QWordValue div abs(z2) |
498 | 247 |
end; |
248 |
||
515 | 249 |
operator < (const z1, z2: hwFloat) b : boolean; |
351 | 250 |
begin |
916 | 251 |
if z1.isNegative xor z2.isNegative then |
351 | 252 |
b:= z1.isNegative |
253 |
else |
|
254 |
if z1.QWordValue = z2.QWordValue then |
|
255 |
b:= false |
|
256 |
else |
|
257 |
b:= (z1.QWordValue < z2.QWordValue) xor z1.isNegative |
|
258 |
end; |
|
259 |
||
515 | 260 |
operator > (const z1, z2: hwFloat) b : boolean; |
351 | 261 |
begin |
916 | 262 |
if z1.isNegative xor z2.isNegative then |
351 | 263 |
b:= z2.isNegative |
264 |
else |
|
265 |
if z1.QWordValue = z2.QWordValue then |
|
266 |
b:= false |
|
267 |
else |
|
268 |
b:= (z1.QWordValue > z2.QWordValue) xor z2.isNegative |
|
269 |
end; |
|
270 |
||
2905 | 271 |
function cstr(const z: hwFloat): shortstring; |
272 |
var tmpstr: shortstring; |
|
351 | 273 |
begin |
274 |
str(z.Round, cstr); |
|
275 |
if z.Frac <> 0 then |
|
276 |
begin |
|
277 |
str(z.Frac / $100000000:1:15, tmpstr); |
|
278 |
delete(tmpstr, 1, 2); |
|
279 |
cstr:= cstr + '.' + tmpstr |
|
280 |
end; |
|
281 |
if z.isNegative then cstr:= '-' + cstr |
|
282 |
end; |
|
283 |
||
515 | 284 |
function hwRound(const t: hwFloat): LongInt; |
351 | 285 |
begin |
2300 | 286 |
if t.isNegative then hwRound:= -(t.Round and $7FFFFFFF) |
287 |
else hwRound:= t.Round and $7FFFFFFF |
|
351 | 288 |
end; |
289 |
||
515 | 290 |
function hwAbs(const t: hwFloat): hwFloat; |
351 | 291 |
begin |
292 |
hwAbs:= t; |
|
293 |
hwAbs.isNegative:= false |
|
294 |
end; |
|
295 |
||
515 | 296 |
function hwSqr(const t: hwFloat): hwFloat; |
351 | 297 |
begin |
1433 | 298 |
hwSqr.isNegative:= false; |
299 |
hwSqr.QWordValue:= |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2933
diff
changeset
|
300 |
((QWord(t.Round) * t.Round) shl 32) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2933
diff
changeset
|
301 |
+ QWord(t.Round) * t.Frac * 2 |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2933
diff
changeset
|
302 |
+ ((QWord(t.Frac) * t.Frac) shr 32); |
351 | 303 |
end; |
304 |
||
515 | 305 |
function hwSqrt(const t: hwFloat): hwFloat; |
738 | 306 |
var l, r: QWord; |
307 |
c: hwFloat; |
|
357 | 308 |
begin |
309 |
hwSqrt.isNegative:= false; |
|
744 | 310 |
|
311 |
if t.Round = 0 then |
|
312 |
begin |
|
313 |
l:= t.QWordValue; |
|
314 |
r:= $100000000 |
|
315 |
end else |
|
316 |
begin |
|
317 |
l:= $100000000; |
|
953
237fc147950c
Fix bug in hwSqrt when calculating square root of number >= 65536
unc0rr
parents:
916
diff
changeset
|
318 |
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
|
319 |
if r > $FFFFFFFFFFFF then r:= $FFFFFFFFFFFF |
744 | 320 |
end; |
321 |
||
738 | 322 |
repeat |
323 |
c.QWordValue:= (l + r) div 2; |
|
324 |
if hwSqr(c).QWordValue > t.QWordValue then r:= c.QWordValue else l:= c.QWordValue |
|
325 |
until r - l <= 1; |
|
744 | 326 |
|
738 | 327 |
hwSqrt.QWordValue:= l |
357 | 328 |
end; |
329 |
||
515 | 330 |
function Distance(const dx, dy: hwFloat): hwFloat; |
351 | 331 |
begin |
738 | 332 |
Distance:= hwSqrt(hwSqr(dx) + hwSqr(dy)) |
351 | 333 |
end; |
334 |
||
515 | 335 |
function DistanceI(const dx, dy: LongInt): hwFloat; |
498 | 336 |
begin |
856 | 337 |
DistanceI:= hwSqrt(int2hwFloat(sqr(dx) + sqr(dy))) |
498 | 338 |
end; |
339 |
||
515 | 340 |
function SignAs(const num, signum: hwFloat): hwFloat; |
498 | 341 |
begin |
856 | 342 |
SignAs.QWordValue:= num.QWordValue; |
498 | 343 |
SignAs.isNegative:= signum.isNegative |
344 |
end; |
|
345 |
||
2599 | 346 |
{$INCLUDE "SinTable.inc"} |
357 | 347 |
|
515 | 348 |
function AngleSin(const Angle: Longword): hwFloat; |
351 | 349 |
begin |
1629 | 350 |
{$IFDEF DEBUGFILE} |
1640 | 351 |
TryDo((Angle >= 0) and (Angle <= 2048), 'Sin param exceeds limits', true); |
1629 | 352 |
{$ENDIF} |
351 | 353 |
AngleSin.isNegative:= false; |
357 | 354 |
if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle] |
355 |
else AngleSin.QWordValue:= SinTable[2048 - Angle] |
|
351 | 356 |
end; |
357 |
||
515 | 358 |
function AngleCos(const Angle: Longword): hwFloat; |
351 | 359 |
begin |
1629 | 360 |
{$IFDEF DEBUGFILE} |
1640 | 361 |
TryDo((Angle >= 0) and (Angle <= 2048), 'Cos param exceeds limits', true); |
1629 | 362 |
{$ENDIF} |
357 | 363 |
AngleCos.isNegative:= Angle > 1024; |
364 |
if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle] |
|
365 |
else AngleCos.QWordValue:= SinTable[Angle - 1024] |
|
351 | 366 |
end; |
367 |
||
368 |
{$ENDIF} |
|
369 |
||
370 |
end. |