- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
authorunc0rr
Sat, 27 Jan 2007 14:06:29 +0000
changeset 370 c75410fe3133
parent 369 2aed85310727
child 371 731ad6d27bd1
- Repair bots: they can walk and use bazooka, possible cannot jump (why?) - Calculate real angle based on direction vector
hedgewars/GSHandlers.inc
hedgewars/uAI.pas
hedgewars/uAIAmmoTests.pas
hedgewars/uAIMisc.pas
hedgewars/uGears.pas
hedgewars/uMisc.pas
--- a/hedgewars/GSHandlers.inc	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/GSHandlers.inc	Sat Jan 27 14:06:29 2007 +0000
@@ -554,7 +554,7 @@
                 begin
                 X:= Gear^.X;
                 Y:= Gear^.Y;
-                if RopePoints.Count = 0 then RopePoints.HookAngle:= 0;//DxDy2Angle32(Gear^.dY, Gear^.dX);
+                if RopePoints.Count = 0 then RopePoints.HookAngle:= DxDy2Angle32(Gear^.dY, Gear^.dX);
                 b:= (cc * HHGear^.dY) > (cs * HHGear^.dX);
                 dLen:= len
                 end;
--- a/hedgewars/uAI.pas	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/uAI.pas	Sat Jan 27 14:06:29 2007 +0000
@@ -46,7 +46,6 @@
     a, aa: TAmmoType;
 begin
 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
-
 for i:= 0 to Pred(Targets.Count) do
     if (Targets.ar[i].Score >= 0) then
        begin
@@ -117,7 +116,7 @@
 
     AddAction(Actions, Me^.Message, aim_push, 10, 0, 0);
     if (Me^.Message and gm_Left) <> 0 then AddAction(Actions, aia_WaitXL, hwRound(Me^.X), 0, 0, 0)
-                                     else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
+                                      else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
     AddAction(Actions, Me^.Message, aim_release, 0, 0, 0);
     steps:= 0;
     if ((Me^.State and gstAttacked) = 0) then TestAmmos(Actions, Me);
@@ -145,7 +144,6 @@
                   if (Me^.dX < 0) then AddAction(MadeActions, aia_WaitXL, hwRound(AltMe.X), 0, 0, 0)
                                  else AddAction(MadeActions, aia_WaitXR, hwRound(AltMe.X), 0, 0, 0);
                   end;
-
        if not CanGo then break;
        inc(steps);
        Actions.actions[Actions.Count - 2].Param:= hwRound(Me^.X);
--- a/hedgewars/uAIAmmoTests.pas	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/uAIAmmoTests.pas	Sat Jan 27 14:06:29 2007 +0000
@@ -20,19 +20,19 @@
 interface
 uses SDLh, uGears, uConsts, uFloat;
 
-(*function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
-function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
-function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
-function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
-function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
-function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
+(*function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
+function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
+function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
+function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
+function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 *)
 type TAmmoTestProc = function (Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 const AmmoTests: array[TAmmoType] of TAmmoTestProc =
                  (
 {amGrenade}       nil,//TestGrenade,
 {amClusterBomb}   nil,
-{amBazooka}       nil,//TestBazooka,
+{amBazooka}       @TestBazooka,
 {amUFO}           nil,
 {amShotgun}       nil,//TestShotgun,
 {amPickHammer}    nil,
@@ -54,37 +54,40 @@
 
 implementation
 uses uMisc, uAIMisc, uLand;
-{
+
 function Metric(x1, y1, x2, y2: integer): integer;
 begin
-Result:= abs(x1 - x2) + abs(y1 - y2)
+Metric:= abs(x1 - x2) + abs(y1 - y2)
 end;
 
-function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 var Vx, Vy, r: hwFloat;
     rTime: hwFloat;
     Score, EX, EY: integer;
+    Result: integer;
 
     function CheckTrace: integer;
     var x, y, dX, dY: hwFloat;
         t: integer;
+        Result: integer;
     begin
-    x:= Me.X;
-    y:= Me.Y;
+    x:= Me^.X;
+    y:= Me^.Y;
     dX:= Vx;
     dY:= -Vy;
-    t:= trunc(rTime);
+    t:= hwRound(rTime);
     repeat
       x:= x + dX;
       y:= y + dY;
       dX:= dX + cWindSpeed;
       dY:= dY + cGravity;
       dec(t)
-    until TestColl(round(x), round(y), 5) or (t <= 0);
-    EX:= round(x);
-    EY:= round(y);
-    Result:= RateExplosion(Me, round(x), round(y), 101);
-    if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64
+    until TestColl(hwRound(x), hwRound(y), 5) or (t <= 0);
+    EX:= hwRound(x);
+    EY:= hwRound(y);
+    Result:= RateExplosion(Me, EX, EY, 101);
+    if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, EX, EY) div 64;
+    CheckTrace:= Result
     end;
 
 begin
@@ -93,28 +96,28 @@
 ExplR:= 0;
 Result:= BadTurn;
 repeat
-  rTime:= rTime + 300 + Level * 50 + random * 200;
-  Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime;
-  Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime;
-  r:= sqr(Vx) + sqr(Vy);
-  if r <= 1 then
+  rTime:= rTime + 300 + Level * 50 + random(200);
+  Vx:= - cWindSpeed * rTime * _0_5 + (Targ.X - hwRound(Me^.X)) / rTime;
+  Vy:= cGravity * rTime * _0_5 - (Targ.Y - hwRound(Me^.Y)) / rTime;
+  r:= Distance(Vx, Vy);
+  if not (r > 1) then
      begin
      Score:= CheckTrace;
      if Result <= Score then
         begin
-        r:= sqrt(r);
-        Angle:= DxDy2AttackAngle(Vx, Vy) + rndSign(random((Level - 1) * 8));
-        Power:= round(r * cMaxPower) - random((Level - 1) * 15 + 1);
+        Angle:= DxDy2AttackAngle(Vx, Vy) + AIrndSign(random((Level - 1) * 8));
+        Power:= hwRound(r * cMaxPower) - random((Level - 1) * 15 + 1);
         ExplR:= 100;
         ExplX:= EX;
         ExplY:= EY;
         Result:= Score
         end;
      end
-until (rTime >= 4500)
+until (rTime > 4500);
+TestBazooka:= Result
 end;
-
-function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+{
+function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 const tDelta = 24;
 var Vx, Vy, r: hwFloat;
     Score, EX, EY: integer;
@@ -167,7 +170,7 @@
 until (TestTime = 5000)
 end;
 
-function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 var Vx, Vy, x, y: hwFloat;
 begin       
 ExplR:= 0;
@@ -197,7 +200,7 @@
 Result:= BadTurn
 end;
 
-function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 var Vx, Vy, x, y, t: hwFloat;
     d: Longword;
 begin
@@ -226,7 +229,7 @@
                                          else Result:= Low(integer)
 end;
 
-function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 begin
 ExplR:= 0;
 if (Level > 2) and (abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) >= 25) then
@@ -241,7 +244,7 @@
 if Result <= 0 then Result:= BadTurn
 end;
 
-function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
+function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; var Time: Longword; var Angle, Power: integer; var ExplX, ExplY, ExplR: integer): integer;
 var i: integer;
 begin
 ExplR:= 0;
--- a/hedgewars/uAIMisc.pas	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/uAIMisc.pas	Sat Jan 27 14:06:29 2007 +0000
@@ -44,7 +44,7 @@
 function RateExplosion(Me: PGear; x, y, r: integer): integer;
 function RateShove(Me: PGear; x, y, r, power: integer): integer;
 function HHGo(Gear, AltGear: PGear; var GoInfo: TGoInfo): boolean;
-//function rndSign(num: integer): integer;
+function AIrndSign(num: integer): integer;
 
 var ThinkingHH: PGear;
     Targets: TTargets;
@@ -346,7 +346,7 @@
 
    if not TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
       begin
-      Gear^.X:= Gear^.X + Gear^.dX;
+      Gear^.X:= Gear^.X + hwSign(Gear^.dX);
       inc(GoInfo.Ticks, cHHStepTicks)
       end;
    if not TestCollisionYwithGear(Gear, 1) then
@@ -388,10 +388,10 @@
 HHJump(AltGear, jmpHJump, GoInfo)
 end;
 
-{function rndSign(num: integer): integer;
+function AIrndSign(num: integer): integer;
 begin
-if random(2) = 0 then Result:=   num
-                 else Result:= - num
-end;  }
+if random(2) = 0 then AIrndSign:=   num
+                 else AIrndSign:= - num
+end;  
 
 end.
--- a/hedgewars/uGears.pas	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/uGears.pas	Sat Jan 27 14:06:29 2007 +0000
@@ -557,7 +557,7 @@
            gtCloud: DrawSprite(sprCloud   , hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.State, Surface);
        gtAmmo_Bomb: DrawSprite(sprBomb , hwRound(Gear^.X) - 8 + WorldDx, hwRound(Gear^.Y) - 8 + WorldDy, hwRound(Gear^.DirAngle), Surface);
         gtHedgehog: DrawHH(Gear, Surface);
-    gtAmmo_Grenade: DrawSprite(sprGrenade , hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY.QWordValue, Gear^.dX.QWordValue), Surface);
+    gtAmmo_Grenade: DrawSprite(sprGrenade , hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY, Gear^.dX), Surface);
        gtHealthTag: if Gear^.Surf <> nil then DrawCentered(hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.Surf, Surface);
            gtGrave: DrawSpriteFromRect(PHedgehog(Gear^.Hedgehog)^.Team^.GraveRect, hwRound(Gear^.X) + WorldDx - 16, hwRound(Gear^.Y) + WorldDy - 16, 32, (GameTicks shr 7) and 7, Surface);
              gtUFO: DrawSprite(sprUFO, hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, (GameTicks shr 7) mod 4, Surface);
@@ -582,7 +582,7 @@
                        begin
                        DrawRopeLine(hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy,
                                     hwRound(PHedgehog(Gear^.Hedgehog)^.Gear^.X) + WorldDx, hwRound(PHedgehog(Gear^.Hedgehog)^.Gear^.Y) + WorldDy);
-                       DrawSprite(sprRopeHook, hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY.QWordValue, Gear^.dX.QWordValue), Surface);
+                       DrawSprite(sprRopeHook, hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY, Gear^.dX), Surface);
                        end;
                     end;
        gtExplosion: DrawSprite(sprExplosion50, hwRound(Gear^.X) + WorldDx, hwRound(Gear^.Y) + WorldDy, Gear^.State, Surface);
@@ -597,7 +597,7 @@
      gtClusterBomb: DrawSprite(sprClusterBomb, hwRound(Gear^.X) - 8 + WorldDx, hwRound(Gear^.Y) - 8 + WorldDy, hwRound(Gear^.DirAngle), Surface);
          gtCluster: DrawSprite(sprClusterParticle, hwRound(Gear^.X) - 8 + WorldDx, hwRound(Gear^.Y) - 8 + WorldDy, 0, Surface);
            gtFlame: DrawSprite(sprFlame, hwRound(Gear^.X) - 8 + WorldDx, hwRound(Gear^.Y) - 8 + WorldDy,(GameTicks div 128 + Gear^.Angle) mod 8, Surface);
-         gtAirBomb: DrawSprite(sprAirBomb , hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY.QWordValue, Gear^.dX.QWordValue), Surface);
+         gtAirBomb: DrawSprite(sprAirBomb , hwRound(Gear^.X) - 16 + WorldDx, hwRound(Gear^.Y) - 16 + WorldDy, DxDy2Angle32(Gear^.dY, Gear^.dX), Surface);
        gtAirAttack: DrawSprite(sprAirplane, hwRound(Gear^.X) - 60 + WorldDx, hwRound(Gear^.Y) - 25 + WorldDy, 0, Surface);
               end;
       Gear:= Gear^.NextGear
--- a/hedgewars/uMisc.pas	Fri Jan 26 22:36:21 2007 +0000
+++ b/hedgewars/uMisc.pas	Sat Jan 27 14:06:29 2007 +0000
@@ -92,8 +92,8 @@
 procedure SDLTry(Assert: boolean; isFatal: boolean);
 function IntToStr(n: LongInt): shortstring;
 function FloatToStr(n: hwFloat): shortstring;
-function DxDy2Angle32(const _dY, _dX: Extended): integer;
-function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
+function DxDy2Angle32(const _dY, _dX: hwFloat): integer;
+function DxDy2AttackAngle(const _dY, _dX: hwFloat): integer;
 procedure AdjustColor(var Color: Longword);
 {$IFDEF DEBUGFILE}
 procedure AddFileLog(s: shortstring);
@@ -177,16 +177,26 @@
 end;
 {$ENDIF}
 
-function DxDy2Angle32(const _dY, _dX: Extended): integer;
+function DxDy2Angle32(const _dY, _dX: hwFloat): integer;
 const _16divPI: Extended = 16/pi;
+var dY, dX: Extended;
 begin
-DxDy2Angle32:= trunc(arctan2(_dY, _dX) * _16divPI) and $1f
+dY:= _dY.QWordValue / $100000000;
+if _dY.isNegative then dY:= - dY;
+dX:= _dX.QWordValue / $100000000;
+if _dX.isNegative then dX:= - dX;
+DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f
 end;
 
-function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
+function DxDy2AttackAngle(const _dY, _dX: hwFloat): integer;
 const MaxAngleDivPI: Extended = cMaxAngle/pi;
+var dY, dX: Extended;
 begin
-DxDy2AttackAngle:= trunc(arctan2(_dY, _dX) * MaxAngleDivPI) mod cMaxAngle
+dY:= _dY.QWordValue / $100000000;
+if _dY.isNegative then dY:= - dY;
+dX:= _dX.QWordValue / $100000000;
+if _dX.isNegative then dX:= - dX;
+DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) mod cMaxAngle
 end;
 
 procedure SetKB(n: Longword);