don't shoot if the target moves out of range while aiming
authoralfadur
Tue, 30 Jun 2020 21:25:48 +0300
changeset 15682 5ce76c658c22
parent 15681 a3f492e601dd
child 15683 255422cd8a44
don't shoot if the target moves out of range while aiming
hedgewars/uGearsHandlersMess.pas
--- a/hedgewars/uGearsHandlersMess.pas	Tue Jun 30 20:02:04 2020 +0300
+++ b/hedgewars/uGearsHandlersMess.pas	Tue Jun 30 21:25:48 2020 +0300
@@ -7228,6 +7228,19 @@
     end
 end;
 
+function CheckSentryAttackRange(Sentry: PGear; targetX, targetY: HwFloat): Boolean;
+var distX, distY: hwFloat;
+begin
+    distX := targetX - Sentry^.X;
+    distY := targetY - Sentry^.Y;
+    CheckSentryAttackRange :=
+        (distX.isNegative = Sentry^.dX.isNegative)
+        and (distX.Round > 24)
+        and (distX.Round < 500)
+        and (hwAbs(distY) < hwAbs(distX))
+        and (TraceAttackPath(Sentry^.X, Sentry^.Y, targetX, targetY, _4, lfLandMask) <= 18);
+end;
+
 procedure doStepSentry(Gear: PGear);
 var HHGear, bullet: PGear;
     distX, distY, invDistance: HwFloat;
@@ -7286,9 +7299,19 @@
         end
         else if Gear^.Tag = sentry_Aiming then
         begin
-            Gear^.WDTimer := 5 + GetRandom(3);
-            Gear^.Tag := sentry_Attacking;
-            Gear^.Timer := 100;
+            if CheckSentryAttackRange(Gear, int2hwFloat(Gear^.Target.X), int2hwFloat(Gear^.Target.Y)) then
+            begin
+                Gear^.WDTimer := 5 + GetRandom(3);
+                Gear^.Tag := sentry_Attacking;
+                Gear^.Timer := 100;
+            end
+            else
+            begin
+                Gear^.Target.X := 0;
+                Gear^.Target.Y := 0;
+                Gear^.Tag := sentry_Idle;
+                Gear^.Timer := 5000;
+            end
         end
         else if Gear^.Tag = sentry_Attacking then
         begin
@@ -7356,13 +7379,7 @@
         and ((CurrentHedgehog^.Gear^.State and (gstMoving or gstHHDriven)) = (gstMoving or gstHHDriven)) then
     begin
         HHGear := CurrentHedgehog^.Gear;
-        distX := HHGear^.X - Gear^.X;
-        distY := HHGear^.Y - Gear^.Y;
-        if (distX.isNegative = Gear^.dX.isNegative)
-            and (distX.Round > 24)
-            and (distX.Round < 500)
-            and (hwAbs(distY) < hwAbs(distX))
-            and (TraceAttackPath(Gear^.X, Gear^.Y, HHGear^.X, HHGear^.Y, _4, lfLandMask) <= 18 ) then
+        if CheckSentryAttackRange(Gear, HHGear^.X, HHGear^.Y) then
         begin
             Gear^.Target.X := hwRound(HHGear^.X);
             Gear^.Target.Y := hwRound(HHGear^.Y);