Changeset 156 for trunk


Ignore:
Timestamp:
Nov 16, 2017, 6:08:29 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Improved code for symetric map generation.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r155 r156  
    112112    Map: TMap;
    113113    function FindByCells(Cell1, Cell2: TCell): TCellLink;
     114    function AddLink(Cell1, Cell2: TCell): TCellLink;
    114115    procedure LoadFromNode(Node: TDOMNode);
    115116    procedure SaveToNode(Node: TDOMNode);
     
    204205    procedure ComputePlayerStats; virtual;
    205206    procedure Generate; virtual;
     207    procedure MakeSymetric;
    206208    procedure Clear;
    207209    constructor Create; virtual;
     
    734736var
    735737  I: Integer;
    736   LastState: Boolean;
    737738begin
    738739  for I := 0 to Cells.Count - 1 do begin
     
    743744  end;
    744745  FreeAndNil(Cells);
    745   if Assigned(Map) then begin
    746     // To remove itself from list we need disable owning to not be called twice
    747     try
    748       LastState := Map.CellLinks.FreeObjects;
    749       Map.CellLinks.FreeObjects := False;
    750       Map.CellLinks.Remove(Self);
    751     finally
    752       Map.CellLinks.FreeObjects := LastState;
    753     end;
    754   end;
    755746  inherited Destroy;
    756747end;
     
    772763  if I < Count then Result := TCellLink(Items[I])
    773764    else Result := nil;
     765end;
     766
     767function TCellLinks.AddLink(Cell1, Cell2: TCell): TCellLink;
     768begin
     769  Result := TCellLink.Create;
     770  Cell1.Neighbors.Add(Cell2);
     771  Cell1.Links.Add(Result);
     772  Cell2.Neighbors.Add(Cell1);
     773  Cell2.Links.Add(Result);
     774  SetLength(Result.Points, 2);
     775  Result.Cells.Add(Cell1);
     776  Result.Points[0] := Cell1.PosPx;
     777  Result.Cells.Add(Cell2);
     778  Result.Points[1] := Cell2.PosPx;
     779  Result.Map := Map;
     780  Map.CellLinks.Add(Result);
    774781end;
    775782
     
    962969var
    963970  I: Integer;
    964   C: Integer;
    965971  Cell: TPlayerCell;
    966972  PosFrom, PosTo: TPoint;
     
    15961602  NewCell: TCell;
    15971603begin
    1598   // Free previous
    1599   Cells.Count := 0;
     1604  Clear;
     1605
    16001606  // Allocate and init new
    16011607  Cells.Count := FSize.Y * FSize.X;
     
    16111617end;
    16121618
     1619procedure TMap.MakeSymetric;
     1620var
     1621  C: Integer;
     1622  CellLink: TCellLink;
     1623  OtherCell1: TCell;
     1624  OtherCell2: TCell;
     1625  OppositeCell: TCell;
     1626begin
     1627  // Generic way to create two sides symetric map independent to shape
     1628  for C := 0 to (Cells.Count div 2) - 1 do begin
     1629    TCell(Cells[C]).Terrain := TCell(Cells[Cells.Count - 1 - C]).Terrain;
     1630    TCell(Cells[C]).Power := TCell(Cells[Cells.Count - 1 - C]).Power;
     1631
     1632    Continue; // TODO
     1633
     1634    for CellLink in TCell(Cells[C]).Links do
     1635      CellLinks.Remove(CellLink);
     1636    TCell(Cells[C]).Links.Clear;
     1637    OppositeCell := TCell(Cells[Cells.Count - 1 - C]);
     1638    for CellLink in OppositeCell.Links do begin
     1639      OtherCell1 := Cells[Cells.Count - 1 - Cells.IndexOf(CellLink.Cells[0])];
     1640      OtherCell2 := Cells[Cells.Count - 1 - Cells.IndexOf(CellLink.Cells[1])];
     1641      CellLinks.AddLink(OtherCell1, OtherCell2);
     1642    end;
     1643  end;
     1644end;
     1645
    16131646procedure TMap.Clear;
    16141647begin
    16151648  CellLinks.Clear;
    16161649  Cells.Clear;
     1650  FNewCellId := 1;
    16171651end;
    16181652
     
    19682002  FreeAndNil(MovesTo);
    19692003  for I := Links.Count - 1 downto 0 do
    1970     TCellLink(Links[I]).Free;
     2004    FMap.CellLinks.Remove(Links[I]);
    19712005  FreeAndNil(Links);
    19722006  for I := Neighbors.Count - 1 downto 0 do
     
    20372071var
    20382072  NewNode: TDOMNode;
    2039   Move: TUnitMove;
    20402073begin
    20412074  Id := ReadInteger(Node, 'Id', 0);
     
    26482681procedure TPlayer.MoveAll;
    26492682var
    2650   I: Integer;
    26512683  AttackerPower: Integer;
    26522684  DefenderPower: Integer;
     
    29152947  I: Integer;
    29162948  J: Integer;
    2917   NewLink: TCellLink;
    29182949begin
    29192950  List := TCells.Create;
     
    29482979        // Check if link doesn't exist already
    29492980        if not Assigned(FoundCell1.Links.FindByCells(FoundCell1, FoundCell2)) then begin
    2950           NewLink := TCellLink.Create;
    2951           FoundCell1.Neighbors.Add(FoundCell2);
    2952           FoundCell1.Links.Add(NewLink);
    2953           FoundCell2.Neighbors.Add(FoundCell1);
    2954           FoundCell2.Links.Add(NewLink);
    2955           SetLength(NewLink.Points, 2);
    2956           NewLink.Cells.Add(FoundCell1);
    2957           NewLink.Points[0] := FoundCell1.PosPx;
    2958           NewLink.Cells.Add(FoundCell2);
    2959           NewLink.Points[1] := FoundCell2.PosPx;
    2960           NewLink.Map := Map;
    2961           Map.CellLinks.Add(NewLink);
     2981          Map.CellLinks.AddLink(FoundCell1, FoundCell2);
    29622982          Inc(BridgeCount);
    29632983        end;
     
    31523172  RootNode: TDOMNode;
    31533173  I: Integer;
    3154   Move: TUnitMove;
    31553174begin
    31563175  Self.FileName := FileName;
     
    34043423var
    34053424  I: Integer;
    3406   C: Integer;
    34073425  LastAreaCount: Integer;
    34083426  Player: TPlayer;
     
    34263444  end;
    34273445
    3428   if SymetricMap then begin
    3429     for C := 0 to (Map.Cells.Count div 2) - 1 do begin
    3430       TCell(Map.Cells[C]).Terrain :=
    3431         TCell(Map.Cells[Map.Cells.Count - 1 - C]).Terrain;
    3432       TCell(Map.Cells[C]).Power :=
    3433         TCell(Map.Cells[Map.Cells.Count - 1 - C]).Power;
    3434     end;
    3435   end;
     3446  if SymetricMap then Map.MakeSymetric;
    34363447
    34373448  Players.Assign(PlayersSetting);
     
    34683479var
    34693480  I: Integer;
    3470   C: Integer;
    34713481  Cell: TCell;
    34723482  PosFrom, PosTo: TPoint;
  • trunk/UMap.pas

    r145 r156  
    146146  PX, PY: Double;
    147147begin
    148   // Free previous
    149   Cells.Count := 0;
    150   FNewCellId := 1;
     148  Clear;
    151149
    152150  // Allocate and init new
     
    185183  NewCell: TCell;
    186184begin
    187   // Free previous
    188   Cells.Count := 0;
    189   FNewCellId := 1;
     185  Clear;
     186
    190187  // Allocate and init new
    191188  Cells.Count := Size.Y * Size.X;
     
    244241  NewCell: TCell;
    245242begin
    246   // Free previous
    247   Cells.Count := 0;
    248   FNewCellId := 1;
     243  Clear;
     244
    249245  // Allocate and init new
    250246  Cells.Count := Size.Y * Size.X;
     
    290286  NewCell: TCell;
    291287begin
    292   // Free previous
    293   Cells.Count := 0;
    294   FNewCellId := 1;
     288  Clear;
     289
    295290  // Allocate and init new
    296291  Cells.Count := Size.Y * Size.X;
Note: See TracChangeset for help on using the changeset viewer.