Changeset 174 for trunk


Ignore:
Timestamp:
Nov 26, 2017, 11:35:25 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Improved generation of random mesh map to avoid overlapping cells polygons and removed links from non-neighboring cells.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UGeometry.pas

    r172 r174  
    9595    constructor Create(const Rect: TGRect<T>); overload;
    9696    function GetRect: TGRect<T>;
     97    function EdgeDistance(Polygon: TGPolygon<T>): Double;
    9798    procedure AddPoint(const P: T);
    9899    procedure Clear;
     
    157158end;
    158159
    159 { TPolygon }
     160{ TGPolygon }
    160161
    161162function TGPolygon<T>.GetPoint(const Index: Integer): T;
     
    282283end;
    283284
    284 { TLine }
     285function TGPolygon<T>.EdgeDistance(Polygon: TGPolygon<T>): Double;
     286var
     287  I, J: Integer;
     288  Dist: Double;
     289begin
     290  Result := Infinity;
     291  for I := 0 to Length(Points) - 1 do
     292  for J := 0 to Length(Polygon.Points) - 1 do begin
     293    Dist := TGLine<T>.Create(Points[I], Polygon.Points[J]).Distance;
     294    if Dist < Result then Result := Dist;
     295  end;
     296end;
     297
     298{ TGLine }
    285299
    286300function TGLine<T>.GetDistance: Double;
  • trunk/UCore.pas

    r170 r174  
    397397procedure TCore.DataModuleDestroy(Sender: TObject);
    398398begin
    399   StoredDimension.Free;;
     399  FreeAndNil(StoredDimension);
    400400  Game.SaveConfig(XMLConfig1, 'Game');
    401401  SaveConfig;
  • trunk/UMap.pas

    r172 r174  
    259259  CellDistance: TCellsDistance;
    260260  Cell: TCell;
     261  Cell2: TCell;
    261262  L1, L2: TLine;
    262263  MP: TPoint;
    263264  LinkLine: TLine;
    264   Polygon: TPolygon;
    265   //LeftLink: TCellsDistance;
    266   //CenterLink: TCellsDistance;
    267   //RightLink: TCellsDistance;
    268265  LeftClosingLine1: TLine;
    269266  LeftClosingLine2: TLine;
     
    283280  LeftCellCommon: TCell;
    284281  RightCellCommon: TCell;
    285   //LeftText: string;
    286   //RightText: string;
    287   NeighborCell: TCell;
     282const
     283  CellGapWidth = 4;
    288284begin
    289285  Clear;
     
    306302  // Calculate distance between all cells
    307303  CellsDistance := TFPGObjectList<TCellsDistance>.Create;
    308   for I1 := 1 to Cells.Count - 1 do
     304  for I1 := 0 to Cells.Count - 1 do
    309305  for I2 := I1 + 1 to Cells.Count - 1 do begin
    310306    NewCellDist := TCellsDistance.Create;
     
    362358      LeftCell := Cell.Neighbors[I];
    363359      RightCell := Cell.Neighbors[(I + 1) mod Cell.Neighbors.Count];
    364       //LeftText := LeftCell.Neighbors.ToString;
    365       //RightText := RightCell.Neighbors.ToString;
    366360      LeftIndex := LeftCell.Neighbors.IndexOf(Cell);
    367361      RightIndex := RightCell.Neighbors.IndexOf(Cell);
     
    406400  end;
    407401
    408   // Compute polygon around cells with sequence sorted by link angle
     402  {// Compute polygon around cells with sequence sorted by link angle
    409403  for Cell in Cells do begin
    410404    // Use whole map first for cell polygon
     
    414408      for NeighborCell in Cell.Neighbors do begin
    415409        LinkLine := TLine.Create(Cell.PosPx, NeighborCell.PosPx);
    416         LinkLine.Distance := LinkLine.Distance - 4;
     410        LinkLine.Distance := LinkLine.Distance - CellGapWidth;
    417411        MP := LinkLine.GetMiddle;
    418412        // Create half plane vector
     
    424418      Cell.Polygon := Polygon;
    425419    end else Cell.Polygon.Clear;
     420  end;}
     421
     422  // Additional polygon by all other cells
     423  for Cell in Cells do begin
     424    Cell.Polygon := TPolygon.Create(TRect.Create(TPoint.Create(0, 0),
     425      TPoint.Create(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y)));
     426    for Cell2 in Cells do
     427    if Cell2 <> Cell then begin
     428          LinkLine := TLine.Create(Cell.PosPx, Cell2.PosPx);
     429          LinkLine.Distance := LinkLine.Distance - CellGapWidth;
     430          MP := LinkLine.GetMiddle;
     431          // Create half plane vector
     432          L1 := TLine.Create(MP, TPoint.Create(MP.X + LinkLine.GetSize.X,
     433            MP.Y + LinkLine.GetSize.Y));
     434
     435          Cell.Polygon.CutLine(L1, Cell.PosPx);
     436    end;
     437  end;
     438
     439  // Remove all neighbor links for cell pairs with non-overlaping polygons
     440  for Cell in Cells do begin
     441    for I := Cell.Neighbors.Count - 1 downto 0 do begin
     442      if Cell.Polygon.EdgeDistance(Cell.Neighbors[I].Polygon) > 2 * CellGapWidth then
     443        Cell.Neighbors.Delete(I);
     444    end;
    426445  end;
    427446
  • trunk/xtactics.lpr

    r170 r174  
    1111  CoolTranslator, TemplateGenerics, UFormPlayer
    1212  { you can add units after this },
    13   SysUtils, UFormMain, UFormMove, UFormNew, UFormCharts, UFormUnitMoves, UGeometryClasses;
     13  SysUtils, UFormMain, UFormMove, UFormNew, UFormCharts, UFormUnitMoves;
    1414
    1515{$R *.res}
Note: See TracChangeset for help on using the changeset viewer.