close Warning: Can't synchronize with repository "(default)" (No changeset 184 in the repository). Look in the Trac log for more information.

Changeset 165 for trunk/UGame.pas


Ignore:
Timestamp:
Nov 22, 2017, 4:48:33 PM (7 years ago)
Author:
chronos
Message:
  • Added: Optimization phase for voronoi map generation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r164 r165  
    77uses
    88  Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite,
    9   DOM, Math, LazFileUtils, UXMLUtils, Dialogs, Types, LCLType, LCLIntf, fgl;
     9  DOM, Math, LazFileUtils, UXMLUtils, Dialogs, Types, LCLType, LCLIntf, fgl,
     10  UGeometry;
    1011
    1112const
     
    6061    Mark: Boolean; // Temporary value
    6162    Weight: Integer; // Temporary value
     63    Angle: Double; // Temporary value
    6264    Links: TCellLinks;
     65    procedure ConnectTo(Cell: TCell);
     66    procedure DisconnectFrom(Cell: TCell);
    6367    procedure Check;
    6468    function NeighboringToVoid: Boolean;
     
    9397    procedure ClearMark;
    9498    procedure ClearWeight;
     99    function ToString: ansistring; override;
    95100  end;
    96101
     
    115120    procedure LoadFromNode(Node: TDOMNode);
    116121    procedure SaveToNode(Node: TDOMNode);
     122  end;
     123
     124  { TCellLinkParams }
     125
     126  TCellLinkParams = class
     127    Cell1: TCell;
     128    Cell2: TCell;
     129    Distance: Double;
     130    Angle: Double;
    117131  end;
    118132
     
    187201    FNewCellId: Integer;
    188202    function GetNewCellId: Integer; virtual;
     203    procedure SortNeighborsByAngle;
    189204  public
    190205    Game: TGame;
     
    10321047        Player.Game.Map.PaintCell(Canvas, Cell.MapCell.PosPx, '', View, Cell.MapCell);
    10331048      end;
    1034 
    1035       {// Draw links to neighbors
     1049    end;
     1050
     1051{    // Draw links to neighbors
     1052    for Cell in Cells do begin
    10361053      for NeighCell in Cell.MapCell.Neighbors do begin
    10371054        Pen.Color := clYellow;
     
    10391056        LineTo(View.CellToCanvasPos(NeighCell.PosPx));
    10401057      end;
    1041       }
    1042     end;
    1043 
     1058
     1059      Font.Color := clRed;
     1060      Brush.Style := bsClear;
     1061      TextOut(View.CellToCanvasPos(Cell.MapCell.PosPx).X,
     1062        View.CellToCanvasPos(Cell.MapCell.PosPx).Y, IntToStr(Cell.MapCell.Id));
     1063    end;
     1064 }
    10441065    // Draw arrows
    10451066    Pen.Color := clCream;
     
    11661187end;
    11671188
     1189function TCells.ToString: ansistring;
     1190var
     1191  C: TCell;
     1192begin
     1193  Result := '';
     1194  for C in Self do
     1195    Result := Result + IntToStr(C.Id) + ', ';
     1196end;
     1197
    11681198{ TPlayers }
    11691199
     
    13531383    FSize := AValue;
    13541384    if FUpdateCount = 0 then Generate;
     1385  end;
     1386end;
     1387
     1388function CompareCellAngle(const C1, C2: TCell): Integer;
     1389begin
     1390  if C1.Angle < C2.Angle then Result := -1
     1391  else if C1.Angle > C2.Angle then Result := 1
     1392  else Result := 0;
     1393end;
     1394
     1395procedure TMap.SortNeighborsByAngle;
     1396var
     1397  Cell: TCell;
     1398  NeighborCell: TCell;
     1399begin
     1400  for Cell in Cells do begin
     1401    for NeighborCell in Cell.Neighbors do
     1402      NeighborCell.Angle := ArcTan2Point(Point(
     1403        NeighborCell.PosPx.X - Cell.PosPx.X,
     1404        NeighborCell.PosPx.Y - Cell.PosPx.Y));
     1405
     1406    Cell.Neighbors.Sort(CompareCellAngle);
    13551407  end;
    13561408end;
     
    18961948  FPower := AValue;
    18971949  //Check;
     1950end;
     1951
     1952procedure TCell.ConnectTo(Cell: TCell);
     1953begin
     1954  Cell.Neighbors.Add(Self);
     1955  Neighbors.Add(Cell);
     1956end;
     1957
     1958procedure TCell.DisconnectFrom(Cell: TCell);
     1959var
     1960  I: Integer;
     1961begin
     1962  I := Cell.Neighbors.IndexOf(Self);
     1963  if I >= 0 then Cell.Neighbors.Delete(I) else
     1964    raise Exception.Create('Can''t disconnect neigboring cells.');
     1965  I := Neighbors.IndexOf(Cell);
     1966  if I >= 0 then Neighbors.Delete(I)
     1967    else Exception.Create('Can''t disconnect neigboring cells.');
    18981968end;
    18991969
Note: See TracChangeset for help on using the changeset viewer.