Changeset 210 for trunk


Ignore:
Timestamp:
May 23, 2018, 6:09:20 PM (7 years ago)
Author:
chronos
Message:
  • Fixed: New turn counting if only one player is alive.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r208 r210  
    354354    Game: TGame;
    355355    NewPlayerId: Integer;
    356     function GetAliveCount: Integer;
    357356    function FindById(Id: Integer): TPlayer;
    358357    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
     
    360359    procedure LoadFromNode(Node: TDOMNode);
    361360    procedure SaveToNode(Node: TDOMNode);
    362     constructor Create;
     361    constructor Create(FreeObjects: Boolean = True);
    363362    function GetFirstHuman: TPlayer;
    364363    procedure Assign(Source: TPlayers);
    365364    procedure LoadConfig(Config: TXmlConfig; Path: string);
    366365    procedure SaveConfig(Config: TXmlConfig; Path: string);
    367     function GetAlivePlayers: TPlayerArray;
     366    function GetAliveCount: Integer;
     367    procedure GetAlivePlayers(Players: TPlayers); overload;
     368    function GetAlivePlayers: TPlayerArray; overload;
    368369    function GetAlivePlayersWithCities: TPlayerArray;
    369370  end;
     
    10891090end;
    10901091
     1092procedure TPlayers.GetAlivePlayers(Players: TPlayers);
     1093var
     1094  Player: TPlayer;
     1095begin
     1096  Players.Clear;
     1097  for Player in Self do
     1098    if Player.IsAlive then Players.Add(Player);
     1099end;
     1100
    10911101function TPlayers.FindById(Id: Integer): TPlayer;
    10921102var
     
    11481158end;
    11491159
    1150 constructor TPlayers.Create;
    1151 begin
    1152   inherited Create;
     1160constructor TPlayers.Create(FreeObjects: Boolean = True);
     1161begin
     1162  inherited;
    11531163  NewPlayerId := 1;
    11541164end;
     
    31553165procedure TGame.NextPlayer;
    31563166var
    3157   PrevPlayer: TPlayer;
     3167  AlivePlayers: TPlayers;
     3168  NewPlayerIndex: Integer;
    31583169begin
    31593170  // Finalize current player
     
    31633174  ComputePlayerStats;
    31643175
    3165   // Select new player
    3166   PrevPlayer := CurrentPlayer;
    3167   // Skip dead players
    3168   repeat
    3169     CurrentPlayer := Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count];
    3170   until CurrentPlayer.IsAlive;
    3171   if Assigned(FOnPlayerChange) then
    3172     FOnPlayerChange(Self);
    3173 
    3174   if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin
     3176  // Select new player from alive players
     3177  AlivePlayers := TPlayers.Create(False);
     3178  Players.GetAlivePlayers(AlivePlayers);
     3179  NewPlayerIndex := AlivePlayers.IndexOf(CurrentPlayer) + 1;
     3180  if NewPlayerIndex >= AlivePlayers.Count then begin
     3181    // Start of turn
    31753182    Inc(TurnCounter);
    31763183    RecordTurnStats;
    31773184    if Assigned(FOnNewTurn) then
    31783185      FOnNewTurn(Self);
    3179   end;
     3186    NewPlayerIndex := NewPlayerIndex mod AlivePlayers.Count;
     3187  end;
     3188  CurrentPlayer := AlivePlayers[NewPlayerIndex];
     3189
     3190  if Assigned(FOnPlayerChange) then
     3191    FOnPlayerChange(Self);
    31803192  CheckWinObjective;
    31813193  CurrentPlayer.PlayerMap.CheckVisibility;
Note: See TracChangeset for help on using the changeset viewer.