AI player checks if it can use tardis before selecting it
authorWuzzy <almikes@aol.com>
Fri, 20 Oct 2017 13:27:48 +0200
changeset 12712 31f7e0a31736
parent 12711 20dbb3a03e61
child 12713 927b35f59a46
AI player checks if it can use tardis before selecting it
hedgewars/uAI.pas
hedgewars/uGearsHandlersMess.pas
hedgewars/uGearsUtils.pas
--- a/hedgewars/uAI.pas	Wed Oct 18 20:33:20 2017 -0400
+++ b/hedgewars/uAI.pas	Fri Oct 20 13:27:48 2017 +0200
@@ -31,7 +31,8 @@
 implementation
 uses uConsts, SDLh, uAIMisc, uAIAmmoTests, uAIActions,
     uAmmos, uTypes,
-    uVariables, uCommands, uUtils, uDebug, uAILandMarks;
+    uVariables, uCommands, uUtils, uDebug, uAILandMarks,
+    uGearsUtils;
 
 var BestActions: TActions;
     CanUseAmmo: array [TAmmoType] of boolean;
@@ -473,7 +474,7 @@
 
             // Hog has no idea what to do. Use tardis or skip
             if not bonuses.activity then
-                if ((HHHasAmmo(Me^.Hedgehog^, amTardis) > 0)) and (random(3) < 1) then
+                if ((HHHasAmmo(Me^.Hedgehog^, amTardis) > 0)) and (CanUseTardis(Me^.Hedgehog^.Gear)) and (random(3) < 1) then
                     // Tardis brings hog to a random place. Perfect for clueless AI
                     begin
                     AddAction(BestActions, aia_Weapon, Longword(amTardis), 80, 0, 0);
--- a/hedgewars/uGearsHandlersMess.pas	Wed Oct 18 20:33:20 2017 -0400
+++ b/hedgewars/uGearsHandlersMess.pas	Fri Oct 20 13:27:48 2017 +0200
@@ -5854,46 +5854,17 @@
 end;
 
 procedure doStepTardis(Gear: PGear);
-var i,j,cnt: LongWord;
-    HH: PHedgehog;
+var HH: PHedgehog;
 begin
-(*
-    Conditions for not activating.
-    1. Hog is last of his clan
-    2. Sudden Death is in play
-    3. Hog is a king
-*)
     HH:= Gear^.Hedgehog;
-    if HH^.Gear <> nil then
-    if (HH^.Gear = nil) or (HH^.King) or (SuddenDeathDmg) then
-        begin
-        if HH^.Gear <> nil then
-            begin
-            HH^.Gear^.Message := HH^.Gear^.Message and (not gmAttack);
-            HH^.Gear^.State:= HH^.Gear^.State and (not gstAttacking);
-            end;
+    if (not CanUseTardis(HH^.Gear)) then
+    begin
+        HH^.Gear^.Message := HH^.Gear^.Message and (not gmAttack);
+        HH^.Gear^.State:= HH^.Gear^.State and (not gstAttacking);
         PlaySound(sndDenied);
-        DeleteGear(gear);
+        DeleteGear(Gear);
         exit
-        end;
-    cnt:= 0;
-    for j:= 0 to Pred(HH^.Team^.Clan^.TeamsNumber) do
-        for i:= 0 to Pred(HH^.Team^.Clan^.Teams[j]^.HedgehogsNumber) do
-            if (HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear <> nil)
-            and ((HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.State and gstDrowning) = 0)
-            and (HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.Health > HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.Damage) then
-                inc(cnt);
-    if cnt < 2 then
-        begin
-        if HH^.Gear <> nil then
-            begin
-            HH^.Gear^.Message := HH^.Gear^.Message and (not gmAttack);
-            HH^.Gear^.State:= HH^.Gear^.State and (not gstAttacking);
-            end;
-            PlaySound(sndDenied);
-            DeleteGear(gear);
-            exit
-        end;
+    end;
     Gear^.SoundChannel := LoopSound(sndTardis);
     Gear^.doStep:= @doStepTardisWarp
 end;
--- a/hedgewars/uGearsUtils.pas	Wed Oct 18 20:33:20 2017 -0400
+++ b/hedgewars/uGearsUtils.pas	Fri Oct 20 13:27:48 2017 +0200
@@ -47,6 +47,7 @@
 function  GearsNear(X, Y: hwFloat; Kind: TGearType; r: LongInt): PGearArrayS;
 procedure SpawnBoxOfSmth;
 procedure ShotgunShot(Gear: PGear);
+function  CanUseTardis(HHGear: PGear): boolean;
 
 procedure SetAllToActive;
 procedure SetAllHHToActive(Ice: boolean);
@@ -1172,6 +1173,35 @@
     DrawExplosion(hwRound(Gear^.X), hwRound(Gear^.Y), cShotgunRadius)
 end;
 
+// Returns true if the given hog gear can use the tardis
+function CanUseTardis(HHGear: PGear): boolean;
+var usable: boolean;
+    i, j, cnt: LongInt;
+    HH: PHedgehog;
+begin
+(*
+    Conditions for not activating.
+    1. Hog is last of his clan
+    2. Sudden Death is in play
+    3. Hog is a king
+*)
+    usable:= true;
+    HH:= HHGear^.Hedgehog;
+    if HHGear <> nil then
+    if (HHGear = nil) or (HH^.King) or (SuddenDeathDmg) then
+        usable:= false;
+    cnt:= 0;
+    for j:= 0 to Pred(HH^.Team^.Clan^.TeamsNumber) do
+        for i:= 0 to Pred(HH^.Team^.Clan^.Teams[j]^.HedgehogsNumber) do
+            if (HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear <> nil)
+            and ((HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.State and gstDrowning) = 0)
+            and (HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.Health > HH^.Team^.Clan^.Teams[j]^.Hedgehogs[i].Gear^.Damage) then
+                inc(cnt);
+    if (cnt < 2) then
+        usable:= false;
+    CanUseTardis:= usable;
+end;
+
 procedure AmmoShove(Ammo: PGear; Damage, Power: LongInt);
 var t: PGearArray;
     Gear: PGear;