Move hedgehog's step routine into separate function, use it in both hedgehog and ai code
authorunc0rr
Sat, 29 Sep 2012 19:26:18 +0400
changeset 7719 eeae1cb6b6bf
parent 7718 97ba379164ec
child 7720 8e6b79a020f8
Move hedgehog's step routine into separate function, use it in both hedgehog and ai code
hedgewars/uAIMisc.pas
hedgewars/uGearsHedgehog.pas
hedgewars/uGearsUtils.pas
--- a/hedgewars/uAIMisc.pas	Fri Sep 28 18:03:32 2012 +0100
+++ b/hedgewars/uAIMisc.pas	Sat Sep 29 19:26:18 2012 +0400
@@ -90,7 +90,7 @@
 var friendlyfactor: LongInt = 300;
 
 implementation
-uses uCollisions, uVariables, uUtils, uLandTexture;
+uses uCollisions, uVariables, uUtils, uLandTexture, uGearsUtils;
 
 var 
     KnownExplosion: record
@@ -731,7 +731,7 @@
             end;
         continue
         end;
-        
+
         // usual walk
         if (Gear^.Message and gmLeft) <> 0 then
             Gear^.dX:= -cLittle
@@ -740,77 +740,10 @@
                 Gear^.dX:=  cLittle
             else
                 exit(false);
-                
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-            begin
-            Gear^.Y:= Gear^.Y - _1;
-            if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
-                Gear^.Y:= Gear^.Y + _6
-            end else Gear^.Y:= Gear^.Y + _5 else
-            end else Gear^.Y:= Gear^.Y + _4 else
-            end else Gear^.Y:= Gear^.Y + _3 else
-            end else Gear^.Y:= Gear^.Y + _2 else
-            end else Gear^.Y:= Gear^.Y + _1
-            end;
-
-        if not TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
-        begin
-            Gear^.X:= Gear^.X + int2hwFloat(hwSign(Gear^.dX));
-            inc(GoInfo.Ticks, cHHStepTicks)
-        end;
 
-        // too scared to reformat this part
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
-            
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
-            
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
-
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
+        if MakeHedgehogsStep(Gear) then
+            inc(GoInfo.Ticks, cHHStepTicks);
 
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
-
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y + _1;
-
-        if TestCollisionYwithGear(Gear, 1) = 0 then
-            begin
-            Gear^.Y:= Gear^.Y - _6;
-            Gear^.dY:= _0;
-            Gear^.State:= Gear^.State or gstMoving
-            end
-        end
-        end
-        end
-        end
-        end
-        end;
         // we have moved for 1 px
         if (pX <> hwRound(Gear^.X)) and ((Gear^.State and gstMoving) = 0) then
             exit(true)
--- a/hedgewars/uGearsHedgehog.pas	Fri Sep 28 18:03:32 2012 +0100
+++ b/hedgewars/uGearsHedgehog.pas	Sat Sep 29 19:26:18 2012 +0400
@@ -741,7 +741,7 @@
         else exit;
 
     StepSoundTimer:= cHHStepTicks;
-   
+
     GHStepTicks:= cHHStepTicks;
     if PrevdX <> hwSign(Gear^.dX) then
         begin
@@ -752,69 +752,10 @@
 
     Gear^.Hedgehog^.visStepPos:= (Gear^.Hedgehog^.visStepPos + 1) and 7;
 
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
-        begin
-        Gear^.Y:= Gear^.Y - _1;
-        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
-            Gear^.Y:= Gear^.Y + _6
-        end else Gear^.Y:= Gear^.Y + _5 else
-        end else Gear^.Y:= Gear^.Y + _4 else
-        end else Gear^.Y:= Gear^.Y + _3 else
-        end else Gear^.Y:= Gear^.Y + _2 else
-        end else Gear^.Y:= Gear^.Y + _1
-        end;
-
-    if (not cArtillery) and ((Gear^.Message and gmPrecise) = 0) and (not TestCollisionXwithGear(Gear, hwSign(Gear^.dX))) then
-        Gear^.X:= Gear^.X + SignAs(_1, Gear^.dX);
-
-   SetAllHHToActive;
+    if (not cArtillery) and ((Gear^.Message and gmPrecise) = 0) then
+        MakeHedgehogsStep(Gear);
 
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y + _1;
-    if TestCollisionYwithGear(Gear, 1) = 0 then
-        begin
-        Gear^.Y:= Gear^.Y - _6;
-        Gear^.dY:= _0;
-        Gear^.State:= Gear^.State or gstMoving;
-        exit
-        end;
-        end
-        end
-        end
-        end
-        end
-        end;
+    SetAllHHToActive;
     AddGearCI(Gear)
     end
 end;
--- a/hedgewars/uGearsUtils.pas	Fri Sep 28 18:03:32 2012 +0100
+++ b/hedgewars/uGearsUtils.pas	Sat Sep 29 19:26:18 2012 +0400
@@ -41,6 +41,8 @@
 procedure CheckCollision(Gear: PGear); inline;
 procedure CheckCollisionWithLand(Gear: PGear); inline;
 
+function MakeHedgehogsStep(Gear: PGear) : boolean;
+
 var doStepHandlers: array[TGearType] of TGearStepProcedure;
 
 
@@ -682,4 +684,73 @@
         Gear^.State := Gear^.State and (not gstCollision)
 end;
 
+function MakeHedgehogsStep(Gear: PGear) : boolean;
+begin
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+    if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then if (TestCollisionYwithGear(Gear, -1) = 0) then
+        begin
+        Gear^.Y:= Gear^.Y - _1;
+        if TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
+            Gear^.Y:= Gear^.Y + _6
+        end else Gear^.Y:= Gear^.Y + _5 else
+        end else Gear^.Y:= Gear^.Y + _4 else
+        end else Gear^.Y:= Gear^.Y + _3 else
+        end else Gear^.Y:= Gear^.Y + _2 else
+        end else Gear^.Y:= Gear^.Y + _1
+        end;
+
+    if not TestCollisionXwithGear(Gear, hwSign(Gear^.dX)) then
+        begin
+        Gear^.X:= Gear^.X + SignAs(_1, Gear^.dX);
+        MakeHedgehogsStep:= true
+        end else
+        MakeHedgehogsStep:= false;
+
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y + _1;
+    if TestCollisionYwithGear(Gear, 1) = 0 then
+        begin
+        Gear^.Y:= Gear^.Y - _6;
+        Gear^.dY:= _0;
+        Gear^.State:= Gear^.State or gstMoving;
+        exit
+        end;
+        end
+        end
+        end
+        end
+        end
+        end;
+end;
+
 end.