- TeamHealthSorter refactored to know about clans
authorunc0rr
Fri, 13 Jul 2007 20:00:42 +0000
changeset 557 a7d49d5e8257
parent 556 49675457d76e
child 558 b2b840eeb10a
- TeamHealthSorter refactored to know about clans - Smooth healthbar width changing
hedgewars/GSHandlers.inc
hedgewars/uTeams.pas
--- a/hedgewars/GSHandlers.inc	Fri Jul 13 19:59:49 2007 +0000
+++ b/hedgewars/GSHandlers.inc	Fri Jul 13 20:00:42 2007 +0000
@@ -848,11 +848,13 @@
 end;
 
 ////////////////////////////////////////////////////////////////////////////////
-var thexchar: array[0..5] of record
-                             oy, ny: LongInt;
+const cSorterWorkTime = 640;
+var thexchar: array[0..cMaxTeams] of
+                             record
+                             dy, ny, dw: LongInt;
                              team: PTeam;
+                             SortFactor: QWord;
                              end;
-    thexchcnt: Longword;
     currsorter: PGear;
 
 procedure doStepTeamHealthSorterWork(Gear: PGear);
@@ -861,11 +863,14 @@
 AllInactive:= false;
 dec(Gear^.Timer);
 if (Gear^.Timer and 15) = 0 then
-   for i:= 0 to Pred(thexchcnt) do
+   for i:= 0 to Pred(TeamsCount) do
        with thexchar[i] do
+            begin
             {$WARNINGS OFF}
-            team^.DrawHealthY:= ny + (oy - ny) * Gear^.Timer div 640;
+            team^.DrawHealthY:= ny + dy * Gear^.Timer div 640;
+            team^.TeamHealthBarWidth:= team^.NewTeamHealthBarWidth + dw * Gear^.Timer div cSorterWorkTime;
             {$WARNINGS ON}
+            end;
 if (Gear^.Timer = 0) or (currsorter <> Gear) then
    begin
    if currsorter = Gear then currsorter:= nil;
@@ -875,36 +880,43 @@
 
 procedure doStepTeamHealthSorter(Gear: PGear);
 var i, t: Longword;
+    b: boolean;
 begin
 AllInactive:= false;
-i:= 0;
+
 for t:= 0 to Pred(TeamsCount) do
-   with TeamsArray[t]^ do
+  with thexchar[t] do
       begin
-      thexchar[i].oy:= DrawHealthY;
-      thexchar[i].team:= TeamsArray[t];
-      inc(i);
+      dy:= TeamsArray[t]^.DrawHealthY;
+      dw:= TeamsArray[t]^.TeamHealthBarWidth - TeamsArray[t]^.NewTeamHealthBarWidth;
+      team:= TeamsArray[t];
+      SortFactor:= TeamsArray[t]^.Clan^.ClanHealth;
+      SortFactor:= (SortFactor shl  3) + TeamsArray[t]^.Clan^.ClanIndex;
+      SortFactor:= (SortFactor shl 30) + TeamsArray[t]^.TeamHealth;
       end;
 
-thexchcnt:= i;
-for i:= 1 to thexchcnt do
-    for t:= 0 to thexchcnt - 2 do
-        if thexchar[t].team^.TeamHealthBarWidth > thexchar[Succ(t)].team^.TeamHealthBarWidth then
+repeat
+    b:= true;
+    for t:= 0 to TeamsCount - 2 do
+        if (thexchar[t].SortFactor > thexchar[Succ(t)].SortFactor) then
            begin
-           thexchar[5]:= thexchar[t];
+           thexchar[cMaxTeams]:= thexchar[t];
            thexchar[t]:= thexchar[Succ(t)];
-           thexchar[Succ(t)]:= thexchar[5]
-           end;
-           
+           thexchar[Succ(t)]:= thexchar[cMaxTeams];
+           b:= false
+           end
+until b;
+
 t:= cScreenHeight - 4;
-for i:= 0 to Pred(thexchcnt) do
+for i:= 0 to Pred(TeamsCount) do
     with thexchar[i] do
          begin
          dec(t, team^.HealthRect.h + 2);
-         ny:= t
+         ny:= t;
+         dy:= dy - ny
          end;
          
-Gear^.Timer:= 640;
+Gear^.Timer:= cSorterWorkTime;
 Gear^.doStep:= @doStepTeamHealthSorterWork;
 currsorter:= Gear
 end;
--- a/hedgewars/uTeams.pas	Fri Jul 13 19:59:49 2007 +0000
+++ b/hedgewars/uTeams.pas	Fri Jul 13 20:00:42 2007 +0000
@@ -55,7 +55,8 @@
              GraveName: string;
              FortName: string;
              TeamHealth: LongInt;
-             TeamHealthBarWidth: LongInt;
+             TeamHealthBarWidth,
+             NewTeamHealthBarWidth: LongInt;
              DrawHealthY: LongInt;
              AttackBar: LongWord;
              HedgehogsNumber: Longword;
@@ -293,21 +294,20 @@
 begin
 with team^ do
      begin
-     TeamHealthBarWidth:= 0;
+     NewTeamHealthBarWidth:= 0;
      for i:= 0 to cMaxHHIndex do
          if Hedgehogs[i].Gear <> nil then
-            inc(TeamHealthBarWidth, Hedgehogs[i].Gear^.Health);
-     TeamHealth:= TeamHealthBarWidth;
-     if TeamHealthBarWidth > MaxTeamHealth then
+            inc(NewTeamHealthBarWidth, Hedgehogs[i].Gear^.Health);
+     TeamHealth:= NewTeamHealthBarWidth;
+     if NewTeamHealthBarWidth > MaxTeamHealth then
         begin
-        MaxTeamHealth:= TeamHealthBarWidth;
+        MaxTeamHealth:= NewTeamHealthBarWidth;
         RecountAllTeamsHealth;
-        end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth
+        end else NewTeamHealthBarWidth:= (NewTeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth
      end;
 
 RecountClanHealth(team^.Clan);
 
-// FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously
 AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0)
 end;