Changeset 54 for trunk


Ignore:
Timestamp:
Aug 17, 2014, 4:40:08 PM (10 years ago)
Author:
chronos
Message:
  • Added: Triangular cell type map.
  • Modified: Pixel size of entire map is computed generally in TMap class method GetPixelRect.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r53 r54  
    1313  HexCellMulX = 1.12;
    1414  HexCellMulY = 1.292;
    15   SquareCellMulX = 1.1;
    16   SquareCellMulY = 1.1;
     15  SquareCellMulX = 1.05;
     16  SquareCellMulY = 1.05;
     17  TriangleCellMulX = 0.55;
     18  TriangleCellMulY = 1.05;
    1719  MaxPlayerCount = 8;
    1820
     
    126128    function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray;
    127129  public
    128     procedure Assign(Source: TMap); override;
    129130    procedure LoadFromFile(FileName: string); override;
    130131    procedure SaveToFile(FileName: string); override;
    131132    function IsValidIndex(Index: TPoint): Boolean; override;
    132     function GetPixelRect: TRect; override;
    133133    procedure Generate; override;
    134     constructor Create; override;
    135     destructor Destroy; override;
    136134  end;
    137135
     
    143141  public
    144142    function IsValidIndex(Index: TPoint): Boolean; override;
    145     function GetPixelRect: TRect; override;
    146143    procedure Generate; override;
    147     constructor Create; override;
    148     destructor Destroy; override;
     144  end;
     145
     146  { TTriangleMap }
     147
     148  TTriangleMap = class(TMap)
     149  private
     150    function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPointArray;
     151  public
     152    function IsValidIndex(Index: TPoint): Boolean; override;
     153    procedure Generate; override;
    149154  end;
    150155
     
    186191    FCellFrom: TCell;
    187192    FCellTo: TCell;
    188     FDestroying: Boolean;
    189193    procedure SetCellFrom(AValue: TCell);
    190194    procedure SetCellTo(AValue: TCell);
     
    206210  TGrowAmount = (gaByOne, gaBySquareRoot);
    207211  TGrowCells = (gcNone, gcPlayerCities, gcPlayerAll);
    208   TMapType = (mtNone, mtHexagon, mtSquare);
     212  TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle);
    209213  TWinObjective = (woDefeatAllOponents, woDefeatAllOponentsCities,
    210214    woSpecialCaptureCell, woStayAliveForDefinedTurns);
     
    341345end;
    342346
     347{ TTriangleMap }
     348
     349function TTriangleMap.GetTrianglePolygon(Pos: TPoint; Size: TPoint;
     350  Reverse: Boolean): TPointArray;
     351var
     352  Rev: Integer;
     353begin
     354  if Reverse then Rev := -1
     355    else Rev := 1;
     356  SetLength(Result, 3);
     357  Result[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     358  Result[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     359  Result[2] := Point(Trunc(Pos.X), Trunc(Pos.Y + (Size.Y * 1.2) / 2 * Rev));
     360end;
     361
     362function TTriangleMap.IsValidIndex(Index: TPoint): Boolean;
     363begin
     364  Result := (Index.X >= 0) and (Index.X < Size.X) and
     365    (Index.Y >= 0) and (Index.Y < Size.Y);
     366end;
     367
     368procedure TTriangleMap.Generate;
     369var
     370  X, Y: Integer;
     371  Rev: Integer;
     372  Reverse: Boolean;
     373  NewCell: TCell;
     374begin
     375  inherited;
     376  // Free previous
     377  Cells.Count := 0;
     378  // Allocate and init new
     379  Cells.Count := FSize.Y * FSize.X;
     380  for Y := 0 to FSize.Y - 1 do
     381  for X := 0 to FSize.X - 1 do begin
     382    NewCell := TCell.Create;
     383    Reverse := Boolean(X mod 2) xor Boolean(Y mod 2);
     384    if Reverse then Rev := -1
     385      else Rev := 1;
     386    NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * TriangleCellMulX),
     387      Trunc((Y * DefaultCellSize.Y * TriangleCellMulY) - (0.1 * Rev * DefaultCellSize.Y)));
     388    NewCell.Polygon := GetTrianglePolygon(NewCell.PosPx, DefaultCellSize, Reverse);
     389    Cells[Y * FSize.X + X] := NewCell;
     390  end;
     391
     392  // Generate neighbours
     393  for Y := 0 to self.FSize.Y - 1 do
     394  for X := 0 to FSize.X - 1 do
     395  with TCell(Cells[Y * FSize.X + X]) do begin
     396    if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1
     397      else Rev := 1;
     398    if IsValidIndex(Point(X + 1, Y + 0)) then
     399      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X + 1)]));
     400    if IsValidIndex(Point(X + 0, Y - 1 * Rev)) then
     401      Neighbors.Add(TCell(Cells[(Y - 1 * Rev) * FSize.X + (X + 0)]));
     402    if IsValidIndex(Point(X - 1, Y + 0)) then
     403      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X - 1)]));
     404  end;
     405end;
     406
    343407{ TSquareMap }
    344408
     
    346410var
    347411  X, Y: Integer;
    348   I: Integer;
    349412  NewCell: TCell;
    350   NeighCells: TCellArray;
    351413begin
    352414  inherited;
     
    367429  for X := 0 to FSize.X - 1 do
    368430  with TCell(Cells[Y * FSize.X + X]) do begin
    369     if IsValidIndex(Point(Y + 0, X + 1)) then
     431    if IsValidIndex(Point(X + 1, Y + 0)) then
    370432      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X + 1)]));
    371     if IsValidIndex(Point(Y + 1, X + 0)) then
     433    if IsValidIndex(Point(X + 0, Y + 1)) then
    372434      Neighbors.Add(TCell(Cells[(Y + 1) * FSize.X + (X + 0)]));
    373     if IsValidIndex(Point(Y + 0, X - 1)) then
     435    if IsValidIndex(Point(X - 1, Y + 0)) then
    374436      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X - 1)]));
    375     if IsValidIndex(Point(Y - 1, X + 0)) then
     437    if IsValidIndex(Point(X + 0, Y - 1)) then
    376438      Neighbors.Add(TCell(Cells[(Y - 1) * FSize.X + (X + 0)]));
    377439  end;
     
    391453  Result[2] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
    392454  Result[3] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
    393 end;
    394 
    395 function TSquareMap.GetPixelRect: TRect;
    396 begin
    397   Result := Bounds(Trunc(-0.5 * DefaultCellSize.X),
    398     Trunc(-0.5 * DefaultCellSize.Y),
    399     Trunc((Size.X + 0.5) * DefaultCellSize.X),
    400     Trunc(((Size.Y + 0.5) * 0.78) * DefaultCellSize.Y));
    401 end;
    402 
    403 constructor TSquareMap.Create;
    404 begin
    405   inherited;
    406 end;
    407 
    408 destructor TSquareMap.Destroy;
    409 begin
    410   inherited Destroy;
    411455end;
    412456
     
    484528  Size := Source.Size;
    485529  DefaultCellSize := Source.DefaultCellSize;
    486   FSize := Source.Size;
     530  //FSize := Source.Size;
    487531
    488532  // Copy all cells
     
    530574  I: Integer;
    531575  Addition: Integer;
    532   Cells: TCellArray;
    533 begin
    534   Cells := GetAllCells;
    535   for I := 0 to Length(Cells) - 1 do
     576begin
     577for I := 0 to Cells.Count - 1 do
    536578  with TCell(Cells[I]) do begin
    537579    if (Player = APlayer) and ((Game.GrowCells = gcPlayerAll) or
     
    590632procedure TMap.ComputePlayerStats;
    591633var
    592   Cells: TCellArray;
    593   I: Integer;
    594 begin
    595   Cells := GetAllCells;
    596   for I := 0 to Length(Cells) - 1 do
    597   with Cells[I] do begin
     634  I: Integer;
     635begin
     636  for I := 0 to Cells.Count - 1 do
     637  with TCell(Cells[I]) do begin
    598638    if Assigned(Player) then begin
    599639      Player.TotalCells := Player.TotalCells + 1;
     
    630670  DefaultCellSize := Point(62, 62);
    631671  Cells := TObjectList.create;
     672  Size := Point(0, 0);
    632673end;
    633674
     
    650691function TMap.GetPixelRect: TRect;
    651692var
    652   Cells: TCellArray;
    653   I: Integer;
    654   CellPos: TPoint;
     693  I: Integer;
     694  CellRect: TRect;
    655695begin
    656696  Result := Rect(0, 0, 0, 0);
    657   // This is generic iterative algorithm to determine map pixel size
    658   Cells := GetAllCells;
    659   for I := 0 to Length(Cells) - 1 do begin
    660     CellPos := CellToPos(Cells[I]);
    661     if I = 0 then Result := Rect(CellPos.X, CellPos.Y, CellPos.X, CellPos.Y)
     697  // This is generic algorithm to determine pixel size of entire map
     698  for I := 0 to Cells.Count - 1 do begin
     699    CellRect := GetPolygonRect(TCell(Cells[I]).Polygon);
     700    if I = 0 then Result := CellRect
    662701      else begin
    663         if CellPos.X > Result.Right then Result.Right := CellPos.X;
    664         if CellPos.Y > Result.Bottom then Result.Bottom := CellPos.Y;
    665         if CellPos.X < Result.Left then Result.Left := CellPos.X;
    666         if CellPos.Y < Result.Top then  Result.Top := CellPos.Y;
     702        if CellRect.Right > Result.Right then Result.Right := CellRect.Right;
     703        if CellRect.Bottom > Result.Bottom then Result.Bottom := CellRect.Bottom;
     704        if CellRect.Left < Result.Left then Result.Left := CellRect.Left;
     705        if CellRect.Top < Result.Top then  Result.Top := CellRect.Top;
    667706      end;
    668707  end;
     
    889928procedure TPlayer.ComputerTurn;
    890929var
    891   AllCells: TCellArray;
     930  AllCells: TObjectList;
    892931  Cells: TCellArray;
    893932  X, Y: Integer;
     
    899938  CanAttack: Integer;
    900939begin
    901   AllCells := Game.Map.GetAllCells;
    902   for C := 0 to Length(AllCells) - 1 do
    903   with AllCells[C] do begin
     940  AllCells := Game.Map.Cells;
     941  for C := 0 to AllCells.Count - 1 do
     942  with TCell(AllCells[C]) do begin
    904943    if (Terrain <> ttVoid) and (Player <> Self) then begin
    905944      // Attack to not owned cell yet
    906945      // Count own possible power
    907       Cells := Game.Map.GetCellNeighbors(AllCells[C]);
     946      Cells := Game.Map.GetCellNeighbors(TCell(AllCells[C]));
    908947      TotalPower := 0;
    909948      for I := 0 to Length(Cells) - 1 do
     
    920959          if Cells[I].GetAvialPower < AttackPower then
    921960            AttackPower := Cells[I].GetAvialPower;
    922           Game.SetMove(Cells[I], AllCells[C], AttackPower);
     961          Game.SetMove(Cells[I], TCell(AllCells[C]), AttackPower);
    923962          TotalAttackPower := TotalAttackPower + AttackPower;
    924963        end;
     
    929968      // We need to move available power to borders to be available for attacks
    930969      // or defense
    931       Cells := Game.Map.GetCellNeighbors(AllCells[C]);
     970      Cells := Game.Map.GetCellNeighbors(TCell(AllCells[C]));
    932971      CanAttack := 0;
    933972      for I := 0 to Length(Cells) - 1 do
     
    939978        // For simplicty just try to balance inner area cells power
    940979        for I := 0 to Length(Cells) - 1 do
    941         if (Cells[I].Player = Self) and (Cells[I].Power < AllCells[C].GetAvialPower) then begin
    942           Game.SetMove(AllCells[C], Cells[I], (AllCells[C].GetAvialPower - Cells[I].Power) div 2);
     980        if (Cells[I].Player = Self) and (Cells[I].Power < TCell(AllCells[C]).GetAvialPower) then begin
     981          Game.SetMove(TCell(AllCells[C]), Cells[I], (TCell(AllCells[C]).GetAvialPower - Cells[I].Power) div 2);
    943982        end;
    944983      end;
     
    950989var
    951990  NewSelectedCell: TCell;
    952   TopLeft: TPoint;
    953   BottomRight: TPoint;
    954991begin
    955992  NewSelectedCell := Game.Map.PosToCell(CanvasToCellPos(Pos), Self);
     
    10901127    mtHexagon: Map := THexMap.Create;
    10911128    mtSquare: Map := TSquareMap.Create;
     1129    mtTriangle: Map := TTriangleMap.Create;
    10921130    else Map := TMap.Create;
    10931131  end;
     
    14221460end;
    14231461
    1424 procedure THexMap.Assign(Source: TMap);
    1425 begin
    1426   inherited;
    1427 end;
    1428 
    14291462procedure THexMap.LoadFromFile(FileName: string);
    14301463var
     
    15601593end;
    15611594
    1562 constructor THexMap.Create;
    1563 begin
    1564   inherited;
    1565   FSize := Point(0, 0);
    1566 end;
    1567 
    1568 destructor THexMap.Destroy;
    1569 begin
    1570   inherited Destroy;
    1571 end;
    1572 
    1573 function THexMap.GetPixelRect: TRect;
    1574 begin
    1575   Result := Bounds(Trunc(-0.5 * DefaultCellSize.X),
    1576     Trunc(-0.5 * DefaultCellSize.Y),
    1577     Trunc((Size.X + 0.5) * DefaultCellSize.X),
    1578     Trunc(((Size.Y + 0.5) * 0.78) * DefaultCellSize.Y));
    1579 end;
    1580 
    15811595end.
    15821596
Note: See TracChangeset for help on using the changeset viewer.