Add separate TotalRounds var for Lua to count the *actual* number of rounds that have been played
authorWuzzy <Wuzzy2@mail.ru>
Sat, 11 Aug 2018 21:43:55 +0200
changeset 13656 56a925256039
parent 13655 34aeaa627400
child 13657 de2b7bc00d40
Add separate TotalRounds var for Lua to count the *actual* number of rounds that have been played The internal TotalRounds (now TotalRoundsPre) is really WTF to use from a Lua standpoint and rarely useful. Scripters usually want to know the real number of rounds.
hedgewars/uGears.pas
hedgewars/uGearsHandlersMess.pas
hedgewars/uGearsList.pas
hedgewars/uScript.pas
hedgewars/uStats.pas
hedgewars/uTeams.pas
--- a/hedgewars/uGears.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uGears.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -135,7 +135,7 @@
                     dec(Gear^.Hedgehog^.InitialHealth)  // does not need a minimum check since <= 1 basically disables it
                 end;
             // Apply SD health decrease as soon as SD starts
-            if (TotalRounds > cSuddenDTurns - 1) then
+            if (TotalRoundsPre > cSuddenDTurns - 1) then
                 begin
                 inc(tmp, cHealthDecrease);
                 if (GameFlags and gfResetHealth) <> 0 then
@@ -319,7 +319,7 @@
     if (not bBetweenTurns) and (not isInMultiShoot) then
         begin
         // Start Sudden Death water rise in the 2nd round of Sudden Death
-        if TotalRounds = cSuddenDTurns + 1 then
+        if TotalRoundsPre = cSuddenDTurns + 1 then
             bWaterRising:= true;
         if bWaterRising and (cWaterRise > 0) then
             AddGear(0, 0, gtWaterUp, 0, _0, _0, 0)^.Tag:= cWaterRise;
@@ -337,11 +337,11 @@
         begin
         if (cWaterRise <> 0) or (cHealthDecrease <> 0) then
              begin
-            if (TotalRounds = cSuddenDTurns) and (not SuddenDeath) and (not isInMultiShoot) then
+            if (TotalRoundsPre = cSuddenDTurns) and (not SuddenDeath) and (not isInMultiShoot) then
                 StartSuddenDeath()
-            else if (TotalRounds < cSuddenDTurns) and (not isInMultiShoot) then
+            else if (TotalRoundsPre < cSuddenDTurns) and (not isInMultiShoot) then
                 begin
-                i:= cSuddenDTurns - TotalRounds;
+                i:= cSuddenDTurns - TotalRoundsPre;
                 s:= ansistring(inttostr(i));
                 if i = 1 then
                     AddCaption(trmsg[sidRoundSD], capcolDefault, capgrpGameState)
@@ -351,7 +351,7 @@
             end;
             if bBetweenTurns
             or isInMultiShoot
-            or (TotalRounds = -1) then
+            or (TotalRoundsPre = -1) then
                 inc(step)
             else
                 begin
--- a/hedgewars/uGearsHandlersMess.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uGearsHandlersMess.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -6027,7 +6027,7 @@
                 end;
             inc(Gear^.Timer);
             end;
-        if Gear^.Tag <= TotalRounds then
+        if Gear^.Tag <= TotalRoundsPre then
             Gear^.Pos:= 3;
         end;
 
--- a/hedgewars/uGearsList.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uGearsList.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -725,7 +725,7 @@
                 gear^.Radius:= 13;
                 gear^.Health:= 200;
                 gear^.Timer:= 0;
-                gear^.Tag:= TotalRounds + 3;
+                gear^.Tag:= TotalRoundsPre + 3;
                 gear^.Pos:= 1;
                 end;
 }
--- a/hedgewars/uScript.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uScript.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -3614,7 +3614,7 @@
 ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
 ScriptSetInteger('ReadyTimeLeft', ReadyTimeLeft);
 ScriptSetInteger('GameTime', GameTicks);
-ScriptSetInteger('TotalRounds', TotalRounds);
+ScriptSetInteger('TotalRounds', TotalRoundsReal);
 ScriptSetInteger('WaterLine', cWaterLine);
 if isCursorVisible and (not bShowAmmoMenu) then
     begin
--- a/hedgewars/uStats.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uStats.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -22,7 +22,8 @@
 interface
 uses uConsts, uTypes;
 
-var TotalRounds: LongInt; // Number of rounds played (-1 if game not started)
+var TotalRoundsPre: LongInt; // Helper variable for calculating start of Sudden Death and more. Starts at -1 and is incremented on the turn BEFORE the turn which marks the start of the next round. Always -1 while in hog placing phase
+    TotalRoundsReal: LongInt; // Total number of rounds played (-1 if not started or in hog placing phase). Exported to Lua as 'TotalRounds'
     FinishedTurnsTotal: LongInt;
     SendGameResultOn : boolean = true;
     SendRankingStatsOn : boolean = true;
@@ -424,7 +425,8 @@
     isTurnSkipped:= false;
     vpHurtSameClan:= nil;
     vpHurtEnemy:= nil;
-    TotalRounds:= -1;
+    TotalRoundsPre:= -1;
+    TotalRoundsReal:= -1;
     FinishedTurnsTotal:= -1;
 end;
 
--- a/hedgewars/uTeams.pas	Sat Aug 11 18:36:22 2018 +0200
+++ b/hedgewars/uTeams.pas	Sat Aug 11 21:43:55 2018 +0200
@@ -51,7 +51,7 @@
 
 var TeamsGameOver: boolean;
     NextClan: boolean;
-    SwapClan: LongInt;
+    SwapClanPre, SwapClanReal: LongInt;
 
 function CheckForWin: boolean;
 var AliveClan: PClan;
@@ -204,15 +204,18 @@
                 inc(c);
                 if c = ClansCount then
                     c:= 0;
+                if c = SwapClanReal then
+                    inc(TotalRoundsReal);
                 NextClan:= true;
                 end;
             end;
 
         with ClansArray[c]^ do
             begin
-            if (c = SwapClan) and (not PlacingHogs) and ((Succ(CurrTeam) mod TeamsNumber) = TagTeamIndex) then
+            if (not PlacingHogs) and ((Succ(CurrTeam) mod TeamsNumber) = TagTeamIndex) then
                 begin
-                inc(TotalRounds);
+                if c = SwapClanPre then
+                    inc(TotalRoundsPre);
                 end;
             end;
         end
@@ -221,9 +224,12 @@
         inc(c);
         if c = ClansCount then
             c:= 0;
-        if (c = SwapClan) and (not PlacingHogs) then
+        if (not PlacingHogs) then
             begin
-            inc(TotalRounds);
+            if c = SwapClanPre then
+                inc(TotalRoundsPre);
+            if c = SwapClanReal then
+                inc(TotalRoundsReal);
             end;
         end;
 
@@ -305,13 +311,22 @@
 
     end;
 
-// Determine clan ID to check to determine whether to increase TotalRounds
-if (SwapClan = -1) and (not PlacingHogs) then
+if not PlacingHogs then
+    if (TotalRoundsReal = -1) then
+        TotalRoundsReal:= 0;
+
+// Determine clan ID to check to determine whether to increase TotalRoundsPre/TotalRoundsReal
+if (not PlacingHogs) then
     begin
-    if (GameFlags and gfRandomOrder) <> 0 then
-        SwapClan:= 0
-    else
-        SwapClan:= ClansCount - 1;
+    if SwapClanPre = -1 then
+        begin
+        if (GameFlags and gfRandomOrder) <> 0 then
+            SwapClanPre:= 0
+        else
+            SwapClanPre:= ClansCount - 1;
+        end;
+    if SwapClanReal = -1 then
+        SwapClanReal:= CurrentTeam^.Clan^.ClanIndex;
     end;
 
 inc(CurrentTeam^.Clan^.TurnNumber);
@@ -900,7 +915,8 @@
 LocalAmmo:= -1;
 TeamsGameOver:= false;
 NextClan:= true;
-SwapClan:= -1;
+SwapClanPre:= -1;
+SwapClanReal:= -1;
 MaxTeamHealth:= 0;
 end;
 
@@ -946,7 +962,8 @@
     end;
 TeamsCount:= 0;
 ClansCount:= 0;
-SwapClan:= -1;
+SwapClanPre:= -1;
+SwapClanReal:= -1;
 end;
 
 end.