Fix some King Mode bugs causing minions not always being properly killed
authorWuzzy <Wuzzy2@mail.ru>
Mon, 10 Dec 2018 02:30:14 +0100
changeset 14419 87562a68c41f
parent 14418 f9a3cfdec1df
child 14420 352ea82355ae
Fix some King Mode bugs causing minions not always being properly killed
ChangeLog.txt
hedgewars/uGears.pas
hedgewars/uGearsHandlersMess.pas
hedgewars/uGearsList.pas
hedgewars/uTeams.pas
hedgewars/uTypes.pas
--- a/ChangeLog.txt	Sun Dec 09 22:28:46 2018 +0100
+++ b/ChangeLog.txt	Mon Dec 10 02:30:14 2018 +0100
@@ -3,6 +3,8 @@
 =============== 1.0.0 (unreleased) =================
  + New chat command: “/help room” (shows room chat commands within the game)
  + Colorize switching arrows, pointing arrow and target cross in clan color
+ * King Mode: Fix team sometimes not being killed properly if king drowned
+ * King Mode: Kill resurrected minions if king is not alive
  * Hide most HUD elements in cinematic mode
 
 Lua API:
--- a/hedgewars/uGears.pas	Sun Dec 09 22:28:46 2018 +0100
+++ b/hedgewars/uGears.pas	Mon Dec 10 02:30:14 2018 +0100
@@ -64,7 +64,7 @@
     delay2: LongWord;
     step: (stInit, stDelay, stChDmg, stSweep, stTurnStats, stChWin1,
     stTurnReact, stAfterDelay, stChWin2, stWater, stChWin3,
-    stHealth, stSpawn, stNTurn);
+    stChKing, stHealth, stSpawn, stNTurn);
     NewTurnTick: LongWord;
     //SDMusic: shortstring;
 
@@ -86,7 +86,7 @@
             CheckNoDamage:= false;
 
             dmg:= Gear^.Damage;
-            if Gear^.Health < dmg then
+            if (Gear^.Health < dmg) then
                 begin
                 Gear^.Active:= true;
                 Gear^.Health:= 0
@@ -105,7 +105,15 @@
             RenderHealth(Gear^.Hedgehog^);
             RecountTeamHealth(Gear^.Hedgehog^.Team);
 
+            end
+        else if ((GameFlags and gfKing) <> 0) and (not Gear^.Hedgehog^.Team^.hasKing) then
+            begin
+            Gear^.Active:= true;
+            Gear^.Health:= 0;
+            RenderHealth(Gear^.Hedgehog^);
+            RecountTeamHealth(Gear^.Hedgehog^.Team);
             end;
+
         if (not isInMultiShoot) then
             Gear^.Karma:= 0;
         Gear^.Damage:= 0
@@ -114,6 +122,25 @@
     end;
 end;
 
+function CheckMinionsDie: boolean;
+var Gear: PGear;
+begin
+    CheckMinionsDie:= false;
+    if (GameFlags and gfKing) = 0 then
+        exit;
+
+    Gear:= GearsList;
+    while Gear <> nil do
+        begin
+        if (Gear^.Kind = gtHedgehog) and (not Gear^.Hedgehog^.King) and (not Gear^.Hedgehog^.Team^.hasKing) then
+            begin
+            CheckMinionsDie:= true;
+            exit;
+            end;
+        Gear:= Gear^.NextGear;
+        end;
+end;
+
 procedure HealthMachine;
 var Gear: PGear;
     team: PTeam;
@@ -357,10 +384,17 @@
         inc(step)
         end;
 
+    stChKing:
+        begin
+        if (not isInMultiShoot) and (CheckMinionsDie) then
+            step:= stChDmg
+        else
+            inc(step);
+        end;
     stHealth:
         begin
         if (cWaterRise <> 0) or (cHealthDecrease <> 0) then
-             begin
+            begin
             if (TotalRoundsPre = cSuddenDTurns) and (not SuddenDeath) and (not isInMultiShoot) then
                 StartSuddenDeath()
             else if (TotalRoundsPre < cSuddenDTurns) and (not isInMultiShoot) then
--- a/hedgewars/uGearsHandlersMess.pas	Sun Dec 09 22:28:46 2018 +0100
+++ b/hedgewars/uGearsHandlersMess.pas	Mon Dec 10 02:30:14 2018 +0100
@@ -6023,6 +6023,8 @@
                 RenderHealth(resgear^.Hedgehog^);
                 RecountTeamHealth(resgear^.Hedgehog^.Team);
                 resgear^.Hedgehog^.Effects[heResurrected]:= 1;
+                if resgear^.Hedgehog^.King then
+                    resgear^.Hedgehog^.Team^.hasKing:= true;
                 { Reviving a hog implies its clan is now alive, too. }
                 resgear^.Hedgehog^.Team^.Clan^.DeathLogged:= false;
                 s:= ansistring(resgear^.Hedgehog^.Name);
--- a/hedgewars/uGearsList.pas	Sun Dec 09 22:28:46 2018 +0100
+++ b/hedgewars/uGearsList.pas	Mon Dec 10 02:30:14 2018 +0100
@@ -771,7 +771,6 @@
 procedure DeleteGear(Gear: PGear);
 var team: PTeam;
     t,i: Longword;
-    k: boolean;
     cakeData: PCakeData;
     iterator: PGear;
 begin
@@ -857,19 +856,14 @@
 
         if Gear^.Hedgehog^.King then
             begin
-            // are there any other kings left? Just doing nil check.  Presumably a mortally wounded king will get reaped soon enough
-            k:= false;
+            Gear^.Hedgehog^.Team^.hasKing:= false;
             for i:= 0 to Pred(team^.Clan^.TeamsNumber) do
-                if (team^.Clan^.Teams[i]^.Hedgehogs[0].Gear <> nil) then
-                    k:= true;
-            if not k then
-                for i:= 0 to Pred(team^.Clan^.TeamsNumber) do
-                    with team^.Clan^.Teams[i]^ do
-                        for t:= 0 to cMaxHHIndex do
-                            if Hedgehogs[t].Gear <> nil then
-                                Hedgehogs[t].Gear^.Health:= 0
-                            else if (Hedgehogs[t].GearHidden <> nil) then
-                                Hedgehogs[t].GearHidden^.Health:= 0  // hog is still hidden. if tardis should return though, lua, eh...
+                with team^.Clan^.Teams[i]^ do
+                    for t:= 0 to cMaxHHIndex do
+                        if Hedgehogs[t].Gear <> nil then
+                            Hedgehogs[t].Gear^.Health:= 0
+                        else if (Hedgehogs[t].GearHidden <> nil) then
+                            Hedgehogs[t].GearHidden^.Health:= 0  // hog is still hidden. if tardis should return though, lua, eh...
             end;
 
         // should be not CurrentHedgehog, but hedgehog of the last gear which caused damage to this hog
--- a/hedgewars/uTeams.pas	Sun Dec 09 22:28:46 2018 +0100
+++ b/hedgewars/uTeams.pas	Mon Dec 10 02:30:14 2018 +0100
@@ -560,6 +560,7 @@
         // Some initial King buffs
         if (GameFlags and gfKing) <> 0 then
             begin
+            hasKing:= true;
             Hedgehogs[0].King:= true;
             Hedgehogs[0].Hat:= 'crown';
             Hedgehogs[0].Effects[hePoisoned] := 0;
--- a/hedgewars/uTypes.pas	Sun Dec 09 22:28:46 2018 +0100
+++ b/hedgewars/uTypes.pas	Mon Dec 10 02:30:14 2018 +0100
@@ -444,6 +444,7 @@
             voicepack: PVoicepack;
             PlayerHash: shortstring;   // md5 hash of player name. For temporary enabling of hats as thank you. Hashed for privacy of players
             stats: TTeamStats;
+            hasKing: boolean; // true if team has a living king
             hasGone: boolean;
             skippedTurns: Longword;
             isGoneFlagPendingToBeSet, isGoneFlagPendingToBeUnset: boolean;