hedgewars/uTeams.pas
changeset 549 4278a80140a8
parent 548 ac1e32b538fd
child 550 f6a18d2bab00
equal deleted inserted replaced
548:ac1e32b538fd 549:4278a80140a8
    24 type PHHAmmo = ^THHAmmo;
    24 type PHHAmmo = ^THHAmmo;
    25      THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo;
    25      THHAmmo = array[0..cMaxSlotIndex, 0..cMaxSlotAmmoIndex] of TAmmo;
    26 
    26 
    27 type PHedgehog = ^THedgehog;
    27 type PHedgehog = ^THedgehog;
    28      PTeam     = ^TTeam;
    28      PTeam     = ^TTeam;
       
    29      PClan     = ^TClan;
    29      THedgehog = record
    30      THedgehog = record
    30                  Name: string[MAXNAMELEN];
    31                  Name: string[MAXNAMELEN];
    31                  Gear: PGear;
    32                  Gear: PGear;
    32                  NameTag, HealthTag: PSDL_Surface;
    33                  NameTag, HealthTag: PSDL_Surface;
    33                  Ammo: PHHAmmo;
    34                  Ammo: PHHAmmo;
    40                  BotLevel  : LongWord; // 0 - Human player
    41                  BotLevel  : LongWord; // 0 - Human player
    41                  DamageGiven: Longword;
    42                  DamageGiven: Longword;
    42                  MaxStepDamage: Longword;
    43                  MaxStepDamage: Longword;
    43                  end;
    44                  end;
    44      TTeam = record
    45      TTeam = record
    45              Color, AdjColor: Longword;
    46              Clan: PClan;
    46              TeamName: string[MAXNAMELEN];
    47              TeamName: string[MAXNAMELEN];
    47              ExtDriven: boolean;
    48              ExtDriven: boolean;
    48              Binds: TBinds;
    49              Binds: TBinds;
    49              Hedgehogs: array[0..cMaxHHIndex] of THedgehog;
    50              Hedgehogs: array[0..cMaxHHIndex] of THedgehog;
    50              CurrHedgehog: LongInt;
    51              CurrHedgehog: LongInt;
    57              TeamHealthBarWidth: LongInt;
    58              TeamHealthBarWidth: LongInt;
    58              DrawHealthY: LongInt;
    59              DrawHealthY: LongInt;
    59              AttackBar: LongWord;
    60              AttackBar: LongWord;
    60              HedgehogsNumber: byte;
    61              HedgehogsNumber: byte;
    61              end;
    62              end;
       
    63      TClan = record
       
    64              Color, AdjColor: Longword;
       
    65              Teams: array[0..Pred(cMaxTeams)] of PTeam;
       
    66              TeamsNumber: Longword;
       
    67              ClanHealth: LongInt;
       
    68              end;
    62 
    69 
    63 var CurrentTeam: PTeam = nil;
    70 var CurrentTeam: PTeam = nil;
    64     TeamsArray: array[0..Pred(cMaxTeams)] of PTeam;
    71     TeamsArray: array[0..Pred(cMaxTeams)] of PTeam;
    65     TeamsCount: Longword = 0;
    72     TeamsCount: Longword = 0;
       
    73     ClansArray: array[0..Pred(cMaxTeams)] of PClan;
       
    74     ClansCount: Longword = 0;
    66     CurMinAngle, CurMaxAngle: Longword;
    75     CurMinAngle, CurMaxAngle: Longword;
    67 
    76 
    68 function AddTeam: PTeam;
    77 function AddTeam(TeamColor: Longword): PTeam;
    69 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
    78 procedure ApplyAmmoChanges(var Hedgehog: THedgehog);
    70 procedure SwitchHedgehog;
    79 procedure SwitchHedgehog;
    71 procedure InitTeams;
    80 procedure InitTeams;
    72 function  TeamSize(p: PTeam): Longword;
    81 function  TeamSize(p: PTeam): Longword;
    73 procedure RecountTeamHealth(team: PTeam);
    82 procedure RecountTeamHealth(team: PTeam);
    81 const MaxTeamHealth: LongInt = 0;
    90 const MaxTeamHealth: LongInt = 0;
    82 
    91 
    83 procedure FreeTeamsList; forward;
    92 procedure FreeTeamsList; forward;
    84 
    93 
    85 function CheckForWin: boolean;
    94 function CheckForWin: boolean;
    86 var team, AliveTeam: PTeam;
    95 var team: PTeam;
       
    96     AliveClan: PClan;
    87     s: shortstring;
    97     s: shortstring;
    88     t, AliveCount: LongInt;
    98     t, AliveCount: LongInt;
    89 begin
    99 begin
    90 AliveCount:= 0;
   100 AliveCount:= 0;
    91 for t:= 0 to Pred(TeamsCount) do
   101 for t:= 0 to Pred(ClansCount) do
    92     if TeamsArray[t]^.TeamHealth > 0 then
   102     if ClansArray[t]^.ClanHealth > 0 then
    93        begin
   103        begin
    94        inc(AliveCount);
   104        inc(AliveCount);
    95        AliveTeam:= TeamsArray[t]
   105        AliveClan:= ClansArray[t]
    96        end;
   106        end;
    97 
   107 
    98 if AliveCount >= 2 then exit(false);
   108 if AliveCount >= 2 then exit(false);
    99 CheckForWin:= true;
   109 CheckForWin:= true;
   100 
   110 
   103    begin // draw
   113    begin // draw
   104    AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState);
   114    AddCaption(trmsg[sidDraw], $FFFFFF, capgrpGameState);
   105    SendStat(siGameResult, trmsg[sidDraw]);
   115    SendStat(siGameResult, trmsg[sidDraw]);
   106    AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000)
   116    AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000)
   107    end else // win
   117    end else // win
   108    begin
   118    with AliveClan^ do
   109    s:= Format(trmsg[sidWinner], TeamsArray[0]^.TeamName);
   119      begin
   110    AddCaption(s, $FFFFFF, capgrpGameState);
   120      if TeamsNumber = 1 then
   111    SendStat(siGameResult, s);
   121         s:= Format(trmsg[sidWinner], Teams[0]^.TeamName)  // team wins
   112    AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000)
   122      else
   113    end;
   123         s:= Format(trmsg[sidWinner], Teams[0]^.TeamName); // clan wins
       
   124 
       
   125      AddCaption(s, $FFFFFF, capgrpGameState);
       
   126      SendStat(siGameResult, s);
       
   127      AddGear(0, 0, gtATFinishGame, 0, _0, _0, 2000)
       
   128      end;
   114 SendStats
   129 SendStats
   115 end;
   130 end;
   116 
   131 
   117 procedure SwitchHedgehog;
   132 procedure SwitchHedgehog;
   118 var th: LongInt;
   133 var th: LongInt;
   165                           else SetBinds(CurrentTeam^.Binds);
   180                           else SetBinds(CurrentTeam^.Binds);
   166 bShowFinger:= true;
   181 bShowFinger:= true;
   167 TurnTimeLeft:= cHedgehogTurnTime
   182 TurnTimeLeft:= cHedgehogTurnTime
   168 end;
   183 end;
   169 
   184 
   170 function AddTeam: PTeam;
   185 function AddTeam(TeamColor: Longword): PTeam;
   171 var Result: PTeam;
   186 var Result: PTeam;
       
   187     c: LongInt;
   172 begin
   188 begin
   173 TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true);
   189 TryDo(TeamsCount <= cMaxTeams, 'Too many teams', true);
   174 New(Result);
   190 New(Result);
   175 TryDo(Result <> nil, 'AddTeam: Result = nil', true);
   191 TryDo(Result <> nil, 'AddTeam: Result = nil', true);
   176 FillChar(Result^, sizeof(TTeam), 0);
   192 FillChar(Result^, sizeof(TTeam), 0);
   177 Result^.AttackBar:= 2;
   193 Result^.AttackBar:= 2;
   178 Result^.CurrHedgehog:= cMaxHHIndex;
   194 Result^.CurrHedgehog:= cMaxHHIndex;
   179 
   195 
   180 TeamsArray[TeamsCount]:= Result;
   196 TeamsArray[TeamsCount]:= Result;
   181 inc(TeamsCount);
   197 inc(TeamsCount);
       
   198 
       
   199 c:= Pred(ClansCount);
       
   200 while (c >= 0) and (ClansArray[c]^.Color <> TeamColor) do dec(c);
       
   201 if c < 0 then
       
   202    begin
       
   203    new(Result^.Clan);
       
   204    FillChar(Result^.Clan^, sizeof(TClan), 0);
       
   205    ClansArray[ClansCount]:= Result^.Clan;
       
   206    inc(ClansCount);
       
   207    with Result^.Clan^ do
       
   208         begin
       
   209         Color:= TeamColor;
       
   210         AdjColor:= Color;
       
   211         AdjustColor(AdjColor);
       
   212         end
       
   213    end else
       
   214    begin
       
   215    Result^.Clan:= ClansArray[c];
       
   216    end;
       
   217 
       
   218 with Result^.Clan^ do
       
   219     begin
       
   220     Teams[TeamsNumber]:= Result;
       
   221     inc(TeamsNumber)
       
   222     end;
   182 
   223 
   183 CurrentTeam:= Result;
   224 CurrentTeam:= Result;
   184 AddTeam:= Result
   225 AddTeam:= Result
   185 end;
   226 end;
   186 
   227 
   242      s:= trammo[Ammoz[AmmoType].NameId];
   283      s:= trammo[Ammoz[AmmoType].NameId];
   243      if Count <> AMMO_INFINITE then
   284      if Count <> AMMO_INFINITE then
   244         s:= s + ' (' + IntToStr(Count) + ')';
   285         s:= s + ' (' + IntToStr(Count) + ')';
   245      if (Propz and ammoprop_Timerable) <> 0 then
   286      if (Propz and ammoprop_Timerable) <> 0 then
   246         s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds];
   287         s:= s + ', ' + inttostr(Timer div 1000) + ' ' + trammo[sidSeconds];
   247      AddCaption(s, Team^.Color, capgrpAmmoinfo);
   288      AddCaption(s, Team^.Clan^.Color, capgrpAmmoinfo);
   248      if (Propz and ammoprop_NeedTarget) <> 0
   289      if (Propz and ammoprop_NeedTarget) <> 0
   249         then begin
   290         then begin
   250         Gear^.State:= Gear^.State or      gstHHChooseTarget;
   291         Gear^.State:= Gear^.State or      gstHHChooseTarget;
   251         isCursorVisible:= true
   292         isCursorVisible:= true
   252         end else begin
   293         end else begin
   265 for i:= 0 to cMaxHHIndex do
   306 for i:= 0 to cMaxHHIndex do
   266     if p^.Hedgehogs[i].Gear <> nil then inc(Result);
   307     if p^.Hedgehogs[i].Gear <> nil then inc(Result);
   267 TeamSize:= Result
   308 TeamSize:= Result
   268 end;
   309 end;
   269 
   310 
       
   311 procedure RecountClanHealth(clan: PClan);
       
   312 var i: LongInt;
       
   313 begin
       
   314 with clan^ do
       
   315     begin
       
   316     ClanHealth:= 0;
       
   317     for i:= 0 to Pred(TeamsNumber) do
       
   318         inc(ClanHealth, Teams[i]^.TeamHealth)
       
   319     end
       
   320 end;
       
   321 
   270 procedure RecountTeamHealth(team: PTeam);
   322 procedure RecountTeamHealth(team: PTeam);
   271 var i: LongInt;
   323 var i: LongInt;
   272 begin
   324 begin
   273 with team^ do
   325 with team^ do
   274      begin
   326      begin
   281         begin
   333         begin
   282         MaxTeamHealth:= TeamHealthBarWidth;
   334         MaxTeamHealth:= TeamHealthBarWidth;
   283         RecountAllTeamsHealth;
   335         RecountAllTeamsHealth;
   284         end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth
   336         end else TeamHealthBarWidth:= (TeamHealthBarWidth * cTeamHealthWidth) div MaxTeamHealth
   285      end;
   337      end;
       
   338 
       
   339 RecountClanHealth(team^.Clan);
       
   340 
   286 // FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously
   341 // FIXME: at the game init, gtTeamHealthSorters are created for each team, and they work simultaneously
   287 AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0)
   342 AddGear(0, 0, gtTeamHealthSorter, 0, _0, _0, 0)
   288 end;
   343 end;
   289 
   344 
   290 procedure RestoreTeamsFromSave;
   345 procedure RestoreTeamsFromSave;