improve sentry target selection
authoralfadur
Sat, 11 Jul 2020 20:01:35 +0300
changeset 15718 83c950393d7a
parent 15717 9060532c25f7
child 15719 08e556cad305
improve sentry target selection
hedgewars/uGearsHandlersMess.pas
--- a/hedgewars/uGearsHandlersMess.pas	Sat Jul 11 19:03:14 2020 +0300
+++ b/hedgewars/uGearsHandlersMess.pas	Sat Jul 11 20:01:35 2020 +0300
@@ -7354,6 +7354,22 @@
     PlaySound(sndGun);
 end;
 
+function GetSentryTarget(): PGear;
+var HHGear: PGear;
+begin
+    GetSentryTarget := nil;
+    if (CurrentHedgehog <> nil) then
+    begin
+        HHGear := CurrentHedgehog^.Gear;
+        if HHGear <> nil then
+        begin
+            if ((HHGear^.State and gstHHDriven) <> 0)
+                and (HHGear^.CollisionIndex < 0) then
+                GetSentryTarget := HHGear;
+        end
+    end
+end;
+
 procedure doStepSentryLand(Gear: PGear);
 var HHGear: PGear;
 const sentry_Idle = 0;
@@ -7468,20 +7484,18 @@
         AimSentry(Gear);
 
     if ((GameTicks and $FF) = 0)
-        and (Gear^.Tag in [sentry_Idle, sentry_Walking])
-        and (CurrentHedgehog <> nil)
-        and (CurrentHedgehog^.Gear <> nil)
-        and ((CurrentHedgehog^.Gear^.State and (gstMoving or gstHHDriven)) = (gstMoving or gstHHDriven)) then
+        and (Gear^.Tag in [sentry_Idle, sentry_Walking]) then
     begin
-        HHGear := CurrentHedgehog^.Gear;
-        if CheckSentryAttackRange(Gear, HHGear^.X, HHGear^.Y) then
-        begin
-            Gear^.Target.X := hwRound(HHGear^.X);
-            Gear^.Target.Y := hwRound(HHGear^.Y);
-            Gear^.Karma := GameTicks;
-            Gear^.Tag := sentry_Aiming;
-            Gear^.Timer := 1800 + GetRandom(400);
-        end
+        HHGear := GetSentryTarget();
+        if HHGear <> nil then
+            if CheckSentryAttackRange(Gear, HHGear^.X, HHGear^.Y) then
+            begin
+                Gear^.Target.X := hwRound(HHGear^.X);
+                Gear^.Target.Y := hwRound(HHGear^.Y);
+                Gear^.Karma := GameTicks;
+                Gear^.Tag := sentry_Aiming;
+                Gear^.Timer := 1800 + GetRandom(400);
+            end
     end
 end;
 
@@ -7568,20 +7582,18 @@
         AimSentry(Gear);
 
     if ((GameTicks and $FF) = 0)
-        and (Gear^.Tag in [sentry_Idle, sentry_Walking])
-        and (CurrentHedgehog <> nil)
-        and (CurrentHedgehog^.Gear <> nil)
-        and ((CurrentHedgehog^.Gear^.State and (gstMoving or gstHHDriven)) = (gstMoving or gstHHDriven)) then
+        and (Gear^.Tag in [sentry_Idle, sentry_Walking]) then
     begin
-        HHGear := CurrentHedgehog^.Gear;
-        if CheckSentryAttackRange(Gear, HHGear^.X, HHGear^.Y) then
-        begin
-            Gear^.Target.X := hwRound(HHGear^.X);
-            Gear^.Target.Y := hwRound(HHGear^.Y);
-            Gear^.Karma := GameTicks;
-            Gear^.Tag := sentry_Aiming;
-            Gear^.Timer := 1800 + GetRandom(400);
-        end
+        HHGear := GetSentryTarget();
+        if HHGear <> nil then
+            if CheckSentryAttackRange(Gear, HHGear^.X, HHGear^.Y) then
+            begin
+                Gear^.Target.X := hwRound(HHGear^.X);
+                Gear^.Target.Y := hwRound(HHGear^.Y);
+                Gear^.Karma := GameTicks;
+                Gear^.Tag := sentry_Aiming;
+                Gear^.Timer := 1800 + GetRandom(400);
+            end
     end
 end;