Changeset 158 for trunk/UGame.pas


Ignore:
Timestamp:
Nov 19, 2017, 1:02:02 AM (7 years ago)
Author:
chronos
Message:
  • Fixed: Show error message if not all players were placed to the map.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r157 r158  
    9898
    9999  TCellLink = class
    100     Allocated: Boolean;
    101100    Points: array of TPoint;
    102101    Cells: TCells;
     
    184183    procedure SetSize(AValue: TPoint); virtual;
    185184  protected
     185    FPixelRect: TRect;
    186186    FNewCellId: Integer;
    187187    function GetNewCellId: Integer; virtual;
     
    216216    destructor Destroy; override;
    217217    procedure Paint(Canvas: TCanvas; View: TView); virtual;
    218     function GetPixelRect: TRect; virtual;
     218    function CalculatePixelRect: TRect; virtual;
    219219    procedure ForEachCells(Method: TMethod); virtual;
    220220    property Size: TPoint read GetSize write SetSize;
     221    property PixelRect: TRect read FPixelRect;
    221222  end;
    222223
     
    315316    Moves: TUnitMoves;
    316317    Computer: TComputer;
     318    function IsAlive: Boolean;
    317319    procedure Clear;
    318320    procedure LoadFromNode(Node: TDOMNode);
     
    350352
    351353  TPlayers = class(TFPGObjectList<TPlayer>)
     354  public
    352355    Game: TGame;
    353356    NewPlayerId: Integer;
     357    function GetAliveCount: Integer;
    354358    function FindById(Id: Integer): TPlayer;
    355359    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
     
    362366    procedure LoadConfig(Config: TXmlConfig; Path: string);
    363367    procedure SaveConfig(Config: TXmlConfig; Path: string);
     368    function GetAlivePlayers: TPlayerArray;
     369    function GetAlivePlayersWithCities: TPlayerArray;
    364370  end;
    365371
     
    478484    procedure SaveToFile(FileName: string);
    479485    procedure ComputePlayerStats;
    480     function GetAlivePlayers: TPlayerArray;
    481     function GetAlivePlayersWithCities: TPlayerArray;
    482486    procedure NextTurn;
    483487    procedure CheckWinObjective;
     
    732736constructor TCellLink.Create;
    733737begin
    734   Allocated := True;
    735738  Cells := TCells.Create;
    736739  Cells.FreeObjects := False;
     
    739742destructor TCellLink.Destroy;
    740743var
    741   Links: TCellLinks;
    742   FirstLink: TCellLink;
    743744  I: Integer;
    744745begin
     
    746747    if TCell(Cells[I]).Neighbors.Remove(TCell(Cells[1 - I])) = -1 then
    747748      raise Exception.Create(SCellRemoveNeighborError);
    748     Links := TCell(Cells[I]).Links;
    749     if Links.Count > 0 then
    750     FirstLink := TCellLink(Links[0]);
    751749    if TCell(Cells[I]).Links.Remove(Self) = -1 then
    752750      raise Exception.Create(SCellRemoveNeighborError);
    753751  end;
    754752  FreeAndNil(Cells);
    755   Allocated := False;
    756753  inherited Destroy;
    757754end;
     
    11591156{ TPlayers }
    11601157
     1158function TPlayers.GetAliveCount: Integer;
     1159var
     1160  Player: TPlayer;
     1161begin
     1162  Result := 0;
     1163  for Player in Self do
     1164    if Player.IsAlive then Inc(Result);
     1165end;
     1166
    11611167function TPlayers.FindById(Id: Integer): TPlayer;
    11621168var
     
    13541360    msRectangle: Result := False;
    13551361    msImage: begin
    1356       Rect := GetPixelRect;
     1362      Rect := PixelRect;
    13571363      with Image.Picture.Bitmap do begin
    13581364        Pos := Point(Trunc(Coord.X / (Rect.Right - Rect.Left) * Width),
     
    13631369    end;
    13641370    msRounded: begin
    1365       Rect := GetPixelRect;
     1371      Rect := PixelRect;
    13661372      Center := Point(Rect.Left + (Rect.Right - Rect.Left) div 2,
    13671373        Rect.Top + (Rect.Bottom - Rect.Top) div 2);
     
    15941600procedure TMap.ComputePlayerStats;
    15951601var
    1596   I: Integer;
    1597 begin
    1598   for I := 0 to Cells.Count - 1 do
    1599   with TCell(Cells[I]) do begin
     1602  Cell: TCell;
     1603begin
     1604  for Cell in Cells do
     1605  with Cell do begin
    16001606    if Assigned(Player) then begin
    16011607      Player.TotalCells := Player.TotalCells + 1;
     
    16211627    NewCell := TCell.Create;
    16221628    NewCell.Map := Self;
    1623     NewCell.PosPx := Point(X, Y);
     1629    NewCell.PosPx := Point(X * DefaultCellSize.X, Y * DefaultCellSize.Y);
    16241630    NewCell.Id := GetNewCellId;
     1631    SetLength(NewCell.Polygon, 1);
     1632    NewCell.Polygon[0] := NewCell.PosPx;
    16251633    Cells[Y * FSize.X + X] := NewCell;
    16261634  end;
     1635  FPixelRect := CalculatePixelRect;
    16271636end;
    16281637
     
    17291738end;
    17301739
    1731 function TMap.GetPixelRect: TRect;
     1740function TMap.CalculatePixelRect: TRect;
    17321741var
    17331742  I: Integer;
     
    25392548  MapRect: TRect;
    25402549begin
    2541   MapRect := Game.Map.GetPixelRect;
     2550  MapRect := Game.Map.PixelRect;
    25422551  SourceRect := Bounds(MapRect.Left + (MapRect.Right - MapRect.Left) div 2 - (SourceRect.Right - SourceRect.Left) div 2,
    25432552    MapRect.Top + (MapRect.Bottom - MapRect.Top) div 2 - (SourceRect.Bottom - SourceRect.Top) div 2,
     
    29122921end;
    29132922
     2923function TPlayer.IsAlive: Boolean;
     2924begin
     2925  Result := (TotalCells > 0) and Assigned(StartCell);
     2926end;
     2927
    29142928procedure TPlayer.CheckCounterMove(Move: TUnitMove);
    29152929var
     
    30143028  with TMapArea(Areas[I]) do begin
    30153029    GetBorderCells(BorderList);
    3016     for J := 0 to 1 do begin
     3030    for J := 0 to 4 do begin
    30173031
    30183032    Cell := TCell(BorderList[Random(BorderList.Count)]);
     
    30863100    View.Clear;
    30873101    View.Zoom := 1;
    3088     if Assigned(ControlPlayer) then View.CenterPlayerCity(ControlPlayer)
     3102    if Assigned(ControlPlayer) and Assigned(ControlPlayer.StartCell) then
     3103      View.CenterPlayerCity(ControlPlayer)
    30893104      else View.CenterMap;
    30903105  end;
     
    30963111  Cell: TCell;
    30973112  List: TCells;
     3113  I: Integer;
    30983114begin
    30993115  with Player do begin
     
    31123128      List.FreeObjects := False;
    31133129      Map.Cells.GetCellsWithWeight(List, Round(LongestDistance * 0.6), Round(LongestDistance * 0.8));
    3114       StartCell := List[Random(List.Count)];
     3130
     3131      // Remove cells already allocated to different player
     3132      for I := List.Count - 1 downto 0 do
     3133        if Assigned(TCell(List[I]).Player) then
     3134          List.Delete(I);
     3135
     3136      if List.Count > 0 then
     3137        StartCell := List[Random(List.Count)];
    31153138    finally
    31163139      FreeAndNil(List);
     
    33533376end;
    33543377
    3355 function TGame.GetAlivePlayers: TPlayerArray;
    3356 var
    3357   I: Integer;
     3378function TPlayers.GetAlivePlayers: TPlayerArray;
     3379var
     3380  Player: TPlayer;
    33583381begin
    33593382  SetLength(Result, 0);
    3360   for I := 0 to Players.Count - 1 do
    3361     if TPlayer(Players[I]).TotalCells > 0 then begin
     3383  for Player in Self do
     3384    if Player.IsAlive then begin
    33623385      SetLength(Result, Length(Result) + 1);
    3363       Result[Length(Result) - 1] := TPlayer(Players[I]);
    3364     end;
    3365 end;
    3366 
    3367 function TGame.GetAlivePlayersWithCities: TPlayerArray;
    3368 var
    3369   I: Integer;
     3386      Result[Length(Result) - 1] := Player;
     3387    end;
     3388end;
     3389
     3390function TPlayers.GetAlivePlayersWithCities: TPlayerArray;
     3391var
     3392  Player: TPlayer;
    33703393begin
    33713394  SetLength(Result, 0);
    3372   for I := 0 to Players.Count - 1 do
    3373     if TPlayer(Players[I]).TotalCities > 0 then begin
     3395  for Player in Self do
     3396    if Player.TotalCities > 0 then begin
    33743397      SetLength(Result, Length(Result) + 1);
    3375       Result[Length(Result) - 1] := TPlayer(Players[I]);
     3398      Result[Length(Result) - 1] := Player;
    33763399    end;
    33773400end;
     
    33923415    if Assigned(FOnPlayerChange) then
    33933416      FOnPlayerChange(Self);
    3394   until CurrentPlayer.TotalCells > 0;
     3417  until CurrentPlayer.IsAlive;
    33953418  if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin
    33963419    Inc(TurnCounter);
     
    34133436  Winner := nil;
    34143437  if WinObjective = woDefeatAllOponents then begin
    3415     AlivePlayers := GetAlivePlayers;
     3438    AlivePlayers := Players.GetAlivePlayers;
    34163439    if (Length(AlivePlayers) <= 1) then begin
    34173440      if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);
     
    34203443  end else
    34213444  if WinObjective = woDefeatAllOponentsCities then begin
    3422     AlivePlayers := GetAlivePlayersWithCities;
     3445    AlivePlayers := Players.GetAlivePlayersWithCities;
    34233446    if (Length(AlivePlayers) <= 1) then begin
    34243447      if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);
     
    35033526    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
    35043527      SelectPlayerStartCell(Player);
    3505       if SymetricMap and (I = 1) then
    3506         StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]);
    3507 
    3508       StartCell.Terrain := ttCity;
    3509       StartCell.Player := Player;
    3510       StartCell.Power := Player.StartUnits;
     3528      if Assigned(Player.StartCell) then begin
     3529        if SymetricMap and (I = 1) then
     3530          StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]);
     3531
     3532        StartCell.Terrain := ttCity;
     3533        StartCell.Player := Player;
     3534        StartCell.Power := Player.StartUnits;
     3535      end;
    35113536    end;
    35123537    PlayerMap.CheckVisibility;
     
    35173542
    35183543  InitClients;
     3544  ComputePlayerStats;
    35193545end;
    35203546
Note: See TracChangeset for help on using the changeset viewer.