# HG changeset patch
# User nemo
# Date 1368155516 14400
# Node ID fcb29a13b1b18994bbaba273c5c7a868e4b3e206
# Parent  35103b1a014cb99e7a5fc8baef7d2fe8ee965782
Adjust barrel damage so AI can actually bat to explode them.  Fix state flag check.  Remove unneeded dmgMod var.  Cancel dX for low dX barrel tracing.

diff -r 35103b1a014c -r fcb29a13b1b1 hedgewars/GSHandlers.inc
--- a/hedgewars/GSHandlers.inc	Thu May 09 18:19:17 2013 -0400
+++ b/hedgewars/GSHandlers.inc	Thu May 09 23:11:56 2013 -0400
@@ -1427,12 +1427,8 @@
     if (Gear^.Health = 0) then
         begin
         dxdy:= hwAbs(Gear^.dX)+hwAbs(Gear^.dY);
-        if (dxdy > _0_35) and 
-           ((TestCollisionYwithGear(Gear, 1) <> 0) or
-             TestCollisionXwithGear(Gear, 1) or
-            (TestCollisionYwithGear(Gear, -1) <> 0) or
-             TestCollisionXwithGear(Gear, -1)) then
-            inc(Gear^.Damage, hwRound(dxdy * _25));
+        if (dxdy > _0_4) and (Gear^.State and gstCollision <> 0) then
+            inc(Gear^.Damage, hwRound(dxdy * _50));
 
         if ((GameTicks and $FF) = 0) and (Gear^.Damage > random(30)) then
             begin
@@ -1582,23 +1578,21 @@
         DeleteCI(Gear);
         AllInactive := false;
         dxdy:= hwAbs(Gear^.dX)+hwAbs(Gear^.dY);
-        if (dxdy > _0_35) then
+        doStepFallingGear(Gear);
+        if (Gear^.State and gstCollision <> 0) and(dxdy > _0_4) then
             begin
             if (TestCollisionYwithGear(Gear, 1) <> 0) then
                 begin
                 Gear^.State := Gear^.State or gsttmpFlag;
-                inc(Gear^.Damage, hwRound(dxdy * _25));
                 for i:= min(12, hwRound(dxdy*_10)) downto 0 do
                     begin
                     particle := AddVisualGear(hwRound(Gear^.X) - 5 + Random(10), hwRound(Gear^.Y) + 12,vgtDust);
                     if particle <> nil then
                         particle^.dX := particle^.dX + (Gear^.dX.QWordValue / 21474836480)
                     end
-                end
-            else if TestCollisionXwithGear(Gear, 1) or (TestCollisionYwithGear(Gear, -1) <> 0) or TestCollisionXwithGear(Gear, -1) then
-                inc(Gear^.Damage, hwRound(dxdy * _25))
+                end;
+            inc(Gear^.Damage, hwRound(dxdy * _50))
             end;
-        doStepFallingGear(Gear);
         CalcRotationDirAngle(Gear);
         //CheckGearDrowning(Gear)
         end
@@ -1637,23 +1631,19 @@
     dec(Gear^.Health, Gear^.Damage);
     Gear^.Damage := 0;
     if Gear^.Health <= 0 then
-        Gear^.doStep := @doStepCase;
-    // Hand off to doStepCase for the explosion
-
+        doStepCase(Gear);
 end;
 
 procedure doStepCase(Gear: PGear);
 var
     i, x, y: LongInt;
     k: TGearType;
-    exBoom: boolean;
     dX, dY: HWFloat;
     hog: PHedgehog;
     sparkles: PVisualGear;
     gi: PGear;
 begin
     k := Gear^.Kind;
-    exBoom := false;
 
     if (Gear^.Message and gmDestroy) > 0 then
         begin
@@ -1667,6 +1657,35 @@
         exit
         end;
 
+    if ((Gear^.Kind <> gtExplosives) and (Gear^.Damage > 0)) or (Gear^.Health<=0) then
+        begin
+        x := hwRound(Gear^.X);
+        y := hwRound(Gear^.Y);
+        hog:= Gear^.Hedgehog;
+
+        DeleteGear(Gear);
+        // <-- delete gear!
+
+        if k = gtCase then
+            begin
+            doMakeExplosion(x, y, 25, hog, EXPLAutoSound);
+            for i:= 0 to 63 do
+                AddGear(x, y, gtFlame, 0, _0, _0, 0);
+            end
+        else if k = gtExplosives then
+                begin
+                doMakeExplosion(x, y, 75, hog, EXPLAutoSound);
+                for i:= 0 to 31 do
+                    begin
+                    dX := AngleCos(i * 64) * _0_5 * (getrandomf + _1);
+                    dY := AngleSin(i * 64) * _0_5 * (getrandomf + _1);
+                    AddGear(x, y, gtFlame, 0, dX, dY, 0);
+                    AddGear(x, y, gtFlame, gstTmpFlag, -dX, -dY, 0);
+                    end
+                end;
+            exit
+        end;
+
     if k = gtExplosives then
         begin
         //if V > _0_03 then Gear^.State:= Gear^.State or gstAnimation;
@@ -1677,15 +1696,13 @@
             end
         else Gear^.dX:= _0;
 
-        if (Gear^.Health > 0) and ((Gear^.Health * 100 div cBarrelHealth) < random(90)) and ((GameTicks and $FF) = 0) then
+        if ((Gear^.Health * 100 div cBarrelHealth) < random(90)) and ((GameTicks and $FF) = 0) then
             if (cBarrelHealth div Gear^.Health) > 2 then
                 AddVisualGear(hwRound(Gear^.X) - 16 + Random(32), hwRound(Gear^.Y) - 2, vgtSmoke)
-        else
-            AddVisualGear(hwRound(Gear^.X) - 16 + Random(32), hwRound(Gear^.Y) - 2, vgtSmokeWhite);
+            else
+                AddVisualGear(hwRound(Gear^.X) - 16 + Random(32), hwRound(Gear^.Y) - 2, vgtSmokeWhite);
         dec(Gear^.Health, Gear^.Damage);
         Gear^.Damage := 0;
-        if Gear^.Health <= 0 then
-            exBoom := true;
         end
     else
         begin
@@ -1737,34 +1754,6 @@
             end
         end;
 
-    if (Gear^.Damage > 0) or exBoom then
-        begin
-        x := hwRound(Gear^.X);
-        y := hwRound(Gear^.Y);
-        hog:= Gear^.Hedgehog;
-
-        DeleteGear(Gear);
-        // <-- delete gear!
-
-        if k = gtCase then
-            begin
-            doMakeExplosion(x, y, 25, hog, EXPLAutoSound);
-            for i:= 0 to 63 do
-                AddGear(x, y, gtFlame, 0, _0, _0, 0);
-            end
-        else if k = gtExplosives then
-                begin
-                doMakeExplosion(x, y, 75, hog, EXPLAutoSound);
-                for i:= 0 to 31 do
-                    begin
-                    dX := AngleCos(i * 64) * _0_5 * (getrandomf + _1);
-                    dY := AngleSin(i * 64) * _0_5 * (getrandomf + _1);
-                    AddGear(x, y, gtFlame, 0, dX, dY, 0);
-                    AddGear(x, y, gtFlame, gstTmpFlag, -dX, -dY, 0);
-                    end
-                end;
-            exit
-        end;
 
     if (Gear^.dY.QWordValue <> 0)
     or (TestCollisionYwithGear(Gear, 1) = 0) then
diff -r 35103b1a014c -r fcb29a13b1b1 hedgewars/uAIAmmoTests.pas
--- a/hedgewars/uAIAmmoTests.pas	Thu May 09 18:19:17 2013 -0400
+++ b/hedgewars/uAIAmmoTests.pas	Thu May 09 23:11:56 2013 -0400
@@ -679,12 +679,11 @@
 end;
 
 function TestDesertEagle(Me: PGear; Targ: TTarget; Level: LongInt; var ap: TAttackParams): LongInt;
-var Vx, Vy, x, y, t, dmgMod: real;
+var Vx, Vy, x, y, t: real;
     d: Longword;
     fallDmg, valueResult: LongInt;
 begin
 if Level > 4 then exit(BadTurn);
-dmgMod:= 0.01 * hwFloat2Float(cDamageModifier) * cDamagePercent;
 Level:= Level; // avoid compiler hint
 ap.ExplR:= 1;
 ap.Time:= 0;
@@ -723,12 +722,11 @@
 
 
 function TestSniperRifle(Me: PGear; Targ: TTarget; Level: LongInt; var ap: TAttackParams): LongInt;
-var Vx, Vy, x, y, t, dmg, dmgMod: real;
+var Vx, Vy, x, y, t, dmg: real;
     d: Longword;
     fallDmg: LongInt;
 begin
 if Level > 3 then exit(BadTurn);
-dmgMod:= 0.01 * hwFloat2Float(cDamageModifier) * cDamagePercent;
 Level:= Level; // avoid compiler hint
 ap.ExplR:= 0;
 ap.Time:= 0;
diff -r 35103b1a014c -r fcb29a13b1b1 hedgewars/uAIMisc.pas
--- a/hedgewars/uAIMisc.pas	Thu May 09 18:19:17 2013 -0400
+++ b/hedgewars/uAIMisc.pas	Thu May 09 23:11:56 2013 -0400
@@ -371,8 +371,6 @@
     odX:= dX;
     odY:= dY;
     skipLandCheck:= true;
-    if x - eX < 0 then dX:= -dX;
-    if y - eY < 0 then dY:= -dY;
     // ok. attempt approximate search for an unbroken trajectory into water.  if it continues far enough, assume out of map
     rCorner:= r * 0.75;
     while true do
@@ -395,7 +393,7 @@
                     if ((Kind = gtMine) and (dxdy > 0.35)) or 
                        ((Kind = gtExplosives) and 
                             (((State and gstTmpFlag <> 0) and (dxdy > 0.35)) or
-                             ((State and gstTmpFlag <> 0) and 
+                             ((State and gstTmpFlag = 0) and 
                                 ((abs(odX) > 0.15) or ((abs(odY) > 0.15) and 
                                 (abs(odX) > 0.02))) and (dxdy > 0.35)))) then
                         begin
@@ -444,14 +442,14 @@
                 else 
                     begin
                     dxdy:= abs(dX)+abs(dY);
-                    if ((Kind = gtMine) and (dxdy > 0.35)) or 
+                    if ((Kind = gtMine) and (dxdy > 0.4)) or 
                        ((Kind = gtExplosives) and 
-                            (((State and gstTmpFlag <> 0) and (dxdy > 0.35)) or
-                             ((State and gstTmpFlag <> 0) and 
+                            (((State and gstTmpFlag <> 0) and (dxdy > 0.4)) or
+                             ((State and gstTmpFlag = 0) and 
                                 ((abs(odX) > 0.15) or ((abs(odY) > 0.15) and 
                                 (abs(odX) > 0.02))) and (dxdy > 0.35)))) then
                         begin
-                        dmg := trunc(dxdy * 25);
+                        dmg := trunc(dxdy * 50);
                         exit(dmg)
                         end
                     else if (Kind = gtExplosives) and not((abs(odX) > 0.15) or ((abs(odY) > 0.15) and (abs(odX) > 0.02))) and (dY > 0.2) then
@@ -521,14 +519,18 @@
                 pX:= Point.x;
                 pY:= Point.y;
                 fallDmg:= 0;
-                if (Flags and afTrackFall <> 0) and (dmg < abs(Score)) then
+                if (Flags and afTrackFall <> 0) and (Score > 0) and (dmg < Score) then
                     begin
                     dX:= (0.005 * dmg + 0.01) / Density;
                     dY:= dX;
-                    if (Kind = gtExplosives) and 
+                    if (Kind = gtExplosives) and (State and gstTmpFlag = 0) and 
                        (((abs(dY) > 0.15) and (abs(dX) < 0.02)) or
                         ((abs(dY) < 0.15) and (abs(dX) < 0.15))) then
                         dX:= 0;
+
+                    if pX - x < 0 then dX:= -dX;
+                    if pY - y < 0 then dY:= -dY;
+
                     if (x and LAND_WIDTH_MASK = 0) and ((y+cHHRadius+2) and LAND_HEIGHT_MASK = 0) and
                        (Land[y+cHHRadius+2, x] and lfIndestructible <> 0) then
                          fallDmg:= trunc(TraceFall(x, y, pX, pY, dX, dY, 0, Targets.ar[i]) * dmgMod)
@@ -607,8 +609,13 @@
                 pY:= Point.y-2;
                 fallDmg:= 0;
                 if (Flags and afSetSkip <> 0) then skip:= true;
-                if (Flags and afTrackFall <> 0) and (Score > 0) then
-                    fallDmg:= trunc(TraceShoveFall(pX, pY, dX, dY, Targets.ar[i]) * dmgMod);
+                if not(dead) and (Flags and afTrackFall <> 0) and (Score > 0) and (power < Score) then
+                    if (Kind = gtExplosives) and (State and gstTmpFlag = 0) and 
+                       (((abs(dY) > 0.15) and (abs(dX) < 0.02)) or
+                        ((abs(dY) < 0.15) and (abs(dX) < 0.15))) then
+                        fallDmg:= trunc(TraceShoveFall(pX, pY, 0, dY, Targets.ar[i]) * dmgMod)
+                    else
+                        fallDmg:= trunc(TraceShoveFall(pX, pY, dX, dY, Targets.ar[i]) * dmgMod);
                 if Kind = gtHedgehog then
                     begin
                     if fallDmg < 0 then // drowning. score healthier hogs higher, since their death is more likely to benefit the AI
@@ -697,16 +704,23 @@
                 end;
             if dmg > 0 then
                 begin
-                pX:= Point.x;
-                pY:= Point.y;
-                dX:= gdX * dmg / Density;
-                dY:= gdY * dmg / Density;
-                if dX < 0 then dX:= dX - 0.01
-                else dX:= dX + 0.01;
-                if (x and LAND_WIDTH_MASK = 0) and ((y+cHHRadius+2) and LAND_HEIGHT_MASK = 0) and
-                   (Land[y+cHHRadius+2, x] and lfIndestructible <> 0) then
-                     fallDmg:= trunc(TraceFall(x, y, pX, pY, dX, dY, 0, Targets.ar[i]) * dmgMod)
-                else fallDmg:= trunc(TraceFall(x, y, pX, pY, dX, dY, erasure, Targets.ar[i]) * dmgMod);
+                if not(dead) and (Score > 0) and (dmg < Score) then
+                    begin
+                    pX:= Point.x;
+                    pY:= Point.y;
+                    dX:= gdX * dmg / Density;
+                    dY:= gdY * dmg / Density;
+                    if dX < 0 then dX:= dX - 0.01
+                    else dX:= dX + 0.01;
+                    if (Kind = gtExplosives) and (State and gstTmpFlag = 0) and 
+                       (((abs(dY) > 0.15) and (abs(dX) < 0.02)) or
+                        ((abs(dY) < 0.15) and (abs(dX) < 0.15))) then 
+                       dX:= 0;
+                    if (x and LAND_WIDTH_MASK = 0) and ((y+cHHRadius+2) and LAND_HEIGHT_MASK = 0) and
+                       (Land[y+cHHRadius+2, x] and lfIndestructible <> 0) then
+                         fallDmg:= trunc(TraceFall(x, y, pX, pY, dX, dY, 0, Targets.ar[i]) * dmgMod)
+                    else fallDmg:= trunc(TraceFall(x, y, pX, pY, dX, dY, erasure, Targets.ar[i]) * dmgMod)
+                    end;
                 if Kind = gtHedgehog then
                     begin
                     if fallDmg < 0 then // drowning. score healthier hogs higher, since their death is more likely to benefit the AI