An experiment - make bazooka AI use float instead of hwFloat - should be as accurate, but faster.
authornemo
Sun, 17 Apr 2011 12:48:32 -0400
changeset 5151 cbadb9fa52fc
parent 5150 63ad8a648afd
child 5152 589f69a9665c
An experiment - make bazooka AI use float instead of hwFloat - should be as accurate, but faster.
hedgewars/GSHandlers.inc
hedgewars/VGSHandlers.inc
hedgewars/uAIAmmoTests.pas
hedgewars/uFloat.pas
hedgewars/uUtils.pas
hedgewars/uVisualGears.pas
--- a/hedgewars/GSHandlers.inc	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/GSHandlers.inc	Sun Apr 17 12:48:32 2011 -0400
@@ -176,8 +176,8 @@
                 particle := AddVisualGear(hwRound(Gear^.X) - 3 + Random(6), cWaterLine, vgtDroplet);
                 if particle <> nil then
                     begin
-                    particle^.dX := particle^.dX - (Gear^.dX.QWordValue / 42949672960);
-                    particle^.dY := particle^.dY - (Gear^.dY.QWordValue / 21474836480)
+                    particle^.dX := particle^.dX - hwFloat2Float(Gear^.dX);
+                    particle^.dY := particle^.dY - hwFloat2Float(Gear^.dY)
                     end
                 end
             end;
--- a/hedgewars/VGSHandlers.inc	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/VGSHandlers.inc	Sun Apr 17 12:48:32 2011 -0400
@@ -106,7 +106,7 @@
 
 // up-and-down-bounce magic
 s := (GameTicks + Gear^.Timer) mod 4096;
-t := 8 * AngleSin(s mod 2048).QWordValue / 4294967296;
+t := 8 * hwFloat2Float(AngleSin(s mod 2048));
 if (s < 2048) then t := -t;
 
 Gear^.Y := LAND_HEIGHT - 1184 + LongInt(Gear^.Timer mod 8) + t;
@@ -465,8 +465,8 @@
 
 if (Gear^.Hedgehog^.Gear <> nil) then
     begin
-    Gear^.X:= Gear^.Hedgehog^.Gear^.X.QWordValue/4294967296 + (Gear^.Tex^.w div 2  - Gear^.FrameTicks);
-    Gear^.Y:= Gear^.Hedgehog^.Gear^.Y.QWordValue/4294967296 - (16 + Gear^.Tex^.h);
+    Gear^.X:= hwFloat2Float(Gear^.Hedgehog^.Gear^.X) + (Gear^.Tex^.w div 2  - Gear^.FrameTicks);
+    Gear^.Y:= hwFloat2Float(Gear^.Hedgehog^.Gear^.Y) - (16 + Gear^.Tex^.h);
     end;
 
 if Gear^.Timer = 0 then
--- a/hedgewars/uAIAmmoTests.pas	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/uAIAmmoTests.pas	Sun Apr 17 12:48:32 2011 -0400
@@ -121,52 +121,54 @@
 end;
 
 function TestBazooka(Me: PGear; Targ: TPoint; Level: LongInt; var ap: TAttackParams): LongInt;
-var Vx, Vy, r: hwFloat;
+var Vx, Vy, r, mX, mY: real;
     rTime: LongInt;
     Score, EX, EY: LongInt;
     valueResult: LongInt;
 
     function CheckTrace: LongInt;
-    var x, y, dX, dY: hwFloat;
+    var x, y, dX, dY: real;
         t: LongInt;
         value: LongInt;
     begin
-    x:= Me^.X;
-    y:= Me^.Y;
+    x:= mX;
+    y:= mY;
     dX:= Vx;
     dY:= -Vy;
     t:= rTime;
     repeat
       x:= x + dX;
       y:= y + dY;
-      dX:= dX + cWindSpeed;
-      dY:= dY + cGravity;
+      dX:= dX + cWindSpeedf;
+      dY:= dY + cGravityf;
       dec(t)
-    until TestCollExcludingMe(Me, hwRound(x), hwRound(y), 5) or (t <= 0);
-    EX:= hwRound(x);
-    EY:= hwRound(y);
+    until TestCollExcludingMe(Me, trunc(x), trunc(y), 5) or (t <= 0);
+    EX:= trunc(x);
+    EY:= trunc(y);
     value:= RateExplosion(Me, EX, EY, 101);
     if value = 0 then value:= - Metric(Targ.X, Targ.Y, EX, EY) div 64;
     CheckTrace:= value;
     end;
 
 begin
+mX:= hwFloat2Float(Me^.X);
+mY:= hwFloat2Float(Me^.Y);
 ap.Time:= 0;
 rTime:= 350;
 ap.ExplR:= 0;
 valueResult:= BadTurn;
 repeat
   rTime:= rTime + 300 + Level * 50 + random(300);
-  Vx:= - cWindSpeed * rTime * _0_5 + (int2hwFloat(Targ.X + AIrndSign(2)) - Me^.X) / int2hwFloat(rTime);
-  Vy:= cGravity * rTime * _0_5 - (int2hwFloat(Targ.Y) - Me^.Y) / int2hwFloat(rTime);
-  r:= Distance(Vx, Vy);
-  if not (r > _1) then
+  Vx:= - cWindSpeedf * rTime * 0.5 + (Targ.X + AIrndSign(2) - mX) / rTime;
+  Vy:= cGravityf * rTime * 0.5 - (Targ.Y - mY) / rTime;
+  r:= sqrt(sqr(Vx) + sqr(Vy));
+  if not (r > 1) then
      begin
      Score:= CheckTrace;
      if valueResult <= Score then
         begin
         ap.Angle:= DxDy2AttackAngle(Vx, Vy) + AIrndSign(random((Level - 1) * 9));
-        ap.Power:= hwRound(r * cMaxPower) - random((Level - 1) * 17 + 1);
+        ap.Power:= trunc(r * cMaxPower) - random((Level - 1) * 17 + 1);
         ap.ExplR:= 100;
         ap.ExplX:= EX;
         ap.ExplY:= EY;
--- a/hedgewars/uFloat.pas	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/uFloat.pas	Sun Apr 17 12:48:32 2011 -0400
@@ -58,6 +58,7 @@
 
 // Returns an hwFloat that represents the value of integer parameter i
 function int2hwFloat (const i: LongInt) : hwFloat; inline;
+function hwFloat2Float (const i: hwFloat) : extended; inline;
 
 // The implemented operators
 
@@ -188,6 +189,12 @@
 int2hwFloat.Frac:= 0
 end;
 
+function hwFloat2Float (const i: hwFloat) : extended;
+begin
+hwFloat2Float:= i.QWordValue / $100000000;
+if i.isNegative then hwFloat2Float:=hwFloat2Float*-1;
+end;
+
 operator + (const z1, z2: hwFloat) z : hwFloat;
 begin
 if z1.isNegative = z2.isNegative then
--- a/hedgewars/uUtils.pas	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/uUtils.pas	Sun Apr 17 12:48:32 2011 -0400
@@ -42,6 +42,7 @@
 function  DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
 function  DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
 function  DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
+function  DxDy2AttackAngle(const _dY, _dX: extended): LongInt;
 
 procedure SetLittle(var r: hwFloat);
 
@@ -182,6 +183,11 @@
 DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI)
 end;
 
+function DxDy2AttackAngle(const _dY, _dX: extended): LongInt; inline;
+begin
+DxDy2AttackAngle:= trunc(arctan2(_dY, _dX) * (cMaxAngle/pi))
+end;
+
 
 procedure SetLittle(var r: hwFloat);
 begin
--- a/hedgewars/uVisualGears.pas	Sun Apr 17 11:57:46 2011 -0400
+++ b/hedgewars/uVisualGears.pas	Sun Apr 17 12:48:32 2011 -0400
@@ -183,8 +183,8 @@
     vgtExplPart2: begin
                 t:= random(1024);
                 sp:= 0.001 * (random(95) + 70);
-                dx:= AngleSin(t).QWordValue/4294967296 * sp;
-                dy:= AngleCos(t).QWordValue/4294967296 * sp;
+                dx:= hwFloat2Float(AngleSin(t)) * sp;
+                dy:= hwFloat2Float(AngleCos(t)) * sp;
                 if random(2) = 0 then dx := -dx;
                 if random(2) = 0 then dy := -dy;
                 Frame:= 7 - random(3);
@@ -193,8 +193,8 @@
         vgtFire: begin
                 t:= random(1024);
                 sp:= 0.001 * (random(85) + 95);
-                dx:= AngleSin(t).QWordValue/4294967296 * sp;
-                dy:= AngleCos(t).QWordValue/4294967296 * sp;
+                dx:= hwFloat2Float(AngleSin(t)) * sp;
+                dy:= hwFloat2Float(AngleCos(t)) * sp;
                 if random(2) = 0 then dx := -dx;
                 if random(2) = 0 then dy := -dy;
                 FrameTicks:= 650 + random(250);
@@ -203,8 +203,8 @@
          vgtEgg: begin
                 t:= random(1024);
                 sp:= 0.001 * (random(85) + 95);
-                dx:= AngleSin(t).QWordValue/4294967296 * sp;
-                dy:= AngleCos(t).QWordValue/4294967296 * sp;
+                dx:= hwFloat2Float(AngleSin(t)) * sp;
+                dy:= hwFloat2Float(AngleCos(t)) * sp;
                 if random(2) = 0 then dx := -dx;
                 if random(2) = 0 then dy := -dy;
                 FrameTicks:= 650 + random(250);
@@ -284,8 +284,8 @@
      vgtFeather: begin
                 t:= random(1024);
                 sp:= 0.001 * (random(85) + 95);
-                dx:= AngleSin(t).QWordValue/4294967296 * sp;
-                dy:= AngleCos(t).QWordValue/4294967296 * sp;
+                dx:= hwFloat2Float(AngleSin(t)) * sp;
+                dy:= hwFloat2Float(AngleCos(t)) * sp;
                 if random(2) = 0 then dx := -dx;
                 if random(2) = 0 then dy := -dy;
                 FrameTicks:= 650 + random(250);
@@ -309,8 +309,8 @@
                 gear^.Frame:= random(4);
                 t:= random(1024);
                 sp:= 0.001 * (random(85) + 47);
-                dx:= AngleSin(t).QWordValue/4294967296 * sp;
-                dy:= AngleCos(t).QWordValue/4294967296 * sp * -2;
+                dx:= hwFloat2Float(AngleSin(t)) * sp;
+                dy:= hwFloat2Float(AngleCos(t)) * sp * -2;
                 if random(2) = 0 then dx := -dx;
                 end;
       vgtNote: begin