Changeset 47 for trunk/UGame.pas


Ignore:
Timestamp:
Aug 17, 2014, 2:02:43 AM (10 years ago)
Author:
chronos
Message:
  • Modified: Cell neightbors list is now attached to each cell as it will not be possible to easily calucalte it for generic mesh. Neighbour relation will be generated during map generation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r42 r47  
    4141    MovesFrom: TObjectList; // TList<TMove>
    4242    MovesTo: TObjectList; // TList<TMove>
     43    Neighbors: TObjectList; // TList<TCell>
    4344    function GetColor: TColor;
    4445    function GetAvialPower: Integer;
     
    9899    procedure Grow(APlayer: TPlayer); virtual;
    99100    procedure ComputePlayerStats; virtual;
     101    procedure Generate; virtual;
    100102    constructor Create; virtual;
    101103    destructor Destroy; override;
    102     function GetCellNeighbours(Cell: TCell): TCellArray; virtual;
     104    function GetCellNeighbors(Cell: TCell): TCellArray; virtual;
    103105    procedure Paint(Canvas: TCanvas; View: TView); virtual;
    104106    function GetPixelRect: TRect; virtual;
     
    115117    function GetSize: TPoint; override;
    116118    procedure SetSize(AValue: TPoint); override;
     119    function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
     120    function GetCellNeighbors2(Cell: TCell): TCellArray;
    117121  public
    118122    Cells: array of array of TCell;
    119     function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; override;
    120     procedure Assign(Source: TMap); virtual;
     123    procedure Assign(Source: TMap); override;
    121124    procedure LoadFromFile(FileName: string); override;
    122125    procedure SaveToFile(FileName: string); override;
    123126    function IsValidIndex(Index: TPoint): Boolean; override;
    124     function GetCellNeighbours(Cell: TCell): TCellArray; override;
    125127    function PosToCell(Pos: TPoint; View: TView): TCell; override;
    126128    function CellToPos(Cell: TCell): TPoint; override;
    127129    function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;
    128130    procedure Paint(Canvas: TCanvas; View: TView); override;
     131    procedure Generate; override;
    129132    constructor Create; override;
    130133    destructor Destroy; override;
     
    140143    function GetSize: TPoint; override;
    141144    procedure SetSize(AValue: TPoint); override;
     145    function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
     146    function GetCellNeighbours2(Cell: TCell): TCellArray;
    142147  public
    143148    Cells: array of array of TCell;
    144     function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; override;
    145149    function IsValidIndex(Index: TPoint): Boolean; override;
    146150    function PosToCell(Pos: TPoint; View: TView): TCell; override;
    147151    function CellToPos(Cell: TCell): TPoint; override;
    148     function GetCellNeighbours(Cell: TCell): TCellArray; override;
    149152    function GetAllCells: TCellArray; override;
    150153    function GetPixelRect: TRect; override;
     
    359362end;
    360363
    361 function TSquareMap.IsCellsNeighbor(Cell1, Cell2: TCell): Boolean;
     364function TSquareMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
    362365var
    363366  DX: Integer;
     
    427430end;
    428431
    429 function TSquareMap.GetCellNeighbours(Cell: TCell): TCellArray;
     432function TSquareMap.GetCellNeighbours2(Cell: TCell): TCellArray;
    430433var
    431434  X, Y: Integer;
     
    435438  for X := -1 to 1 do
    436439  if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    437   IsCellsNeighbor(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
     440  IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
    438441    SetLength(Result, Length(Result) + 1);
    439442    Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];
     
    608611function TMap.IsCellsNeighbor(Cell1, Cell2: TCell): Boolean;
    609612begin
    610   Result := False;
     613  Result := Cell1.Neighbors.IndexOf(Cell2) <> -1;
    611614end;
    612615
     
    683686end;
    684687
     688procedure TMap.Generate;
     689begin
     690end;
     691
    685692constructor TMap.Create;
    686693begin
     
    695702end;
    696703
    697 function TMap.GetCellNeighbours(Cell: TCell): TCellArray;
    698 begin
    699 
     704function TMap.GetCellNeighbors(Cell: TCell): TCellArray;
     705var
     706  I: Integer;
     707begin
     708  SetLength(Result, Cell.Neighbors.Count);
     709  for I := 0 to Length(Result) - 1 do
     710    Result[I] := TCell(Cell.Neighbors[I]);
    700711end;
    701712
     
    729740function TMap.GetAllCells: TCellArray;
    730741begin
    731 
     742  SetLength(Result, 0);
    732743end;
    733744
     
    861872begin
    862873  Player := nil;
     874  Neighbors := TObjectList.Create;
     875  Neighbors.OwnsObjects := False;
    863876  MovesFrom := TObjectList.Create;
    864877  MovesFrom.OwnsObjects := False;
     
    877890    TUnitMove(MovesTo[I]).Free;
    878891  FreeAndNil(MovesTo);
     892  FreeAndNil(Neighbors);
    879893  inherited Destroy;
    880894end;
     
    940954      // Attack to not owned cell yet
    941955      // Count own possible power
    942       Cells := Game.Map.GetCellNeighbours(AllCells[C]);
     956      Cells := Game.Map.GetCellNeighbors(AllCells[C]);
    943957      TotalPower := 0;
    944958      for I := 0 to Length(Cells) - 1 do
     
    964978      // We need to move available power to borders to be available for attacks
    965979      // or defense
    966       Cells := Game.Map.GetCellNeighbours(AllCells[C]);
     980      Cells := Game.Map.GetCellNeighbors(AllCells[C]);
    967981      CanAttack := 0;
    968982      for I := 0 to Length(Cells) - 1 do
     
    11251139    mtHexagon: Map := THexMap.Create;
    11261140    mtSquare: Map := TSquareMap.Create;
     1141    else Map := TMap.Create;
    11271142  end;
    11281143  Map.Assign(OldMap);
     
    11411156begin
    11421157  I := 0;
     1158  Confirm := True;
    11431159  while (I < Moves.Count) and ((TUnitMove(Moves[I]).CellFrom <> CellFrom) or
    11441160    (TUnitMove(Moves[I]).CellTo <> CellTo)) do Inc(I);
     
    12251241
    12261242procedure TGame.LoadConfig(Config: TXmlConfig; Path: string);
     1243var
     1244  P: TPoint;
    12271245begin
    12281246  with Config do begin
     
    14391457
    14401458procedure THexMap.SetSize(AValue: TPoint);
    1441 var
    1442   X, Y: Integer;
    1443   NewCell: TCell;
    1444   C: Integer;
    14451459begin
    14461460  if (FSize.X <> AValue.X) or (FSize.Y <> AValue.Y) then begin
    1447     // Free previous
    1448     for Y := 0 to FSize.Y - 1 do
    1449     for X := 0 to FSize.X - 1 do begin
    1450       TCell(Cells[Y, X]).Destroy;
    1451     end;
    14521461    FSize := AValue;
    1453     // Allocate and init new
    1454     SetLength(Cells, FSize.Y, FSize.X);
    1455     for Y := 0 to FSize.Y - 1 do
    1456     for X := 0 to FSize.X - 1 do begin
    1457       NewCell := TCell.Create;
    1458       NewCell.Pos := Point(X, Y);
    1459       Cells[Y, X] := NewCell;
    1460     end;
    1461   end;
    1462 end;
    1463 
    1464 function THexMap.IsCellsNeighbor(Cell1, Cell2: TCell): Boolean;
     1462    Generate;
     1463  end;
     1464end;
     1465
     1466function THexMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
    14651467var
    14661468  DX: Integer;
     
    14841486procedure THexMap.Assign(Source: TMap);
    14851487begin
     1488  inherited;
    14861489end;
    14871490
     
    15221525end;
    15231526
    1524 function THexMap.GetCellNeighbours(Cell: TCell): TCellArray;
     1527function THexMap.GetCellNeighbors2(Cell: TCell): TCellArray;
    15251528var
    15261529  X, Y: Integer;
     
    15301533  for X := -1 to 1 do
    15311534  if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    1532   IsCellsNeighbor(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
     1535  IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
    15331536    SetLength(Result, Length(Result) + 1);
    15341537    Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];
     
    16861689end;
    16871690
     1691procedure THexMap.Generate;
     1692var
     1693  X, Y: Integer;
     1694  I: Integer;
     1695  NewCell: TCell;
     1696  NeighCells: TCellArray;
     1697begin
     1698  // Free previous
     1699  for Y := 0 to Length(Cells) - 1 do
     1700  for X := 0 to Length(Cells[Y]) - 1 do begin
     1701    TCell(Cells[Y, X]).Destroy;
     1702  end;
     1703  // Allocate and init new
     1704  SetLength(Cells, FSize.Y, FSize.X);
     1705  for Y := 0 to FSize.Y - 1 do
     1706  for X := 0 to FSize.X - 1 do begin
     1707    NewCell := TCell.Create;
     1708    NewCell.Pos := Point(X, Y);
     1709    Cells[Y, X] := NewCell;
     1710  end;
     1711  // Generate neightbours
     1712  for Y := 0 to FSize.Y - 1 do
     1713  for X := 0 to FSize.X - 1 do begin
     1714    NeighCells := GetCellNeighbors2(Cells[Y, X]);
     1715    for I := 0 to Length(NeighCells) - 1 do
     1716      Cells[Y, X].Neighbors.Add(NeighCells[I]);
     1717  end;
     1718end;
     1719
    16881720constructor THexMap.Create;
    16891721begin
Note: See TracChangeset for help on using the changeset viewer.