anchor.diff:      ComputeNearbyPoints(K);
     end;
     FindClosestPolygon();
  -end;
  -
  -procedure TKMNavMesh.TieUpPolygonsWithTiles();
  -//var
  +end;
  +
  +procedure TKMNavMesh.TieUpPolygonsWithTiles();
  +//var
   //  X,Y,Polygon: Integer;
   begin
     {
  
positive.code:   // Get starting index of each polygon
    for X := 1 to fPolyCount - 1 do // 0. polygon is reserved / unused
      fPolygons[X].Poly2PointStart := fPolygons[X-1].Poly2PointStart + fPolygons[X-1].Poly2PointCnt;
    //}
  end;
  
  //Render debug symbols
  procedure TKMNavMesh.Paint(const aRect: TKMRect);
  const
    COLOR_WHITE = $FFFFFF;
    COLOR_BLACK = $000000;
    COLOR_GREEN = $00FF00;
    COLOR_RED = $7700FF;
    COLOR_YELLOW = $00FFFF;
    COLOR_BLUE = $FF0000;
  
    function GetCommonPoints(aIdx1, aIdx2: Word; var aPoint1, aPoint2: TKMPoint): Boolean;
    var
      FirstPoint: Boolean;
      K,L: Integer;
    begin
      Result := False;
      FirstPoint := True;
      for K := 0 to 2 do
      for L := 0 to 2 do
        if (fPolygons[aIdx1].Indices[K] = fPolygons[aIdx2].Indices[L]) then
        begin
          if FirstPoint then
            aPoint1 := fNodes[ fPolygons[aIdx2].Indices[L] ]
          else
          begin
            aPoint2 := fNodes[ fPolygons[aIdx2].Indices[L] ];
            Result := True;
            Exit;
          end;
          FirstPoint := False;
        end;
    end;
  
  var
    K, L: Integer;
    p1,p2: TKMPoint;
  
    Owner: TKMHandID;
    DefLines: TKMDefenceLines;
    DefencePosArr: TKMDefencePosArr;
    FFF: TForwardFF;
  begin
    //AfterMissionInit();
    if not AI_GEN_NAVMESH OR not OVERLAY_NAVMESH then
      Exit;
    //{
    if fNavMeshGenerator.Paint(aRect) then
      Exit;
    //}
    //fNavMeshGenerator.Paint(aRect);
    // EXTRACT POLYGONS
    //{ Triangles and connection of NavMesh
    for K := 1 to fPolyCount - 1 do
      with fPolygons[K] do
      begin
        gRenderAux.TriangleOnTerrain(
          fNodes[ Indices[0] ].X,
          fNodes[ Indices[0] ].Y,
          fNodes[ Indices[1] ].X,
          fNodes[ Indices[1] ].Y,
          fNodes[ Indices[2] ].X,
          fNodes[ Indices[2] ].Y, $50000000 OR COLOR_BLACK);
        for L := 0 to NearbyCount - 1 do
          if GetCommonPoints(K, Nearby[L], p1, p2) then
            gRenderAux.LineOnTerrain(p1, p2, $50000000 OR COLOR_BLUE)
          else
          begin
            gRenderAux.TriangleOnTerrain(
              fNodes[ Indices[0] ].X,
              fNodes[ Indices[0] ].Y,
              fNodes[ Indices[1] ].X,
              fNodes[ Indices[1] ].Y,
              fNodes[ Indices[2] ].X,
              fNodes[ Indices[2] ].Y, $90000000 OR COLOR_WHITE);
          end;
        p1.X := Round( (fNodes[ Indices[0] ].X + fNodes[ Indices[1] ].X + fNodes[ Indices[2] ].X) / 3 );
        p1.Y := Round( (fNodes[ Indices[0] ].Y + fNodes[ Indices[1] ].Y + fNodes[ Indices[2] ].Y) / 3 );
        gRenderAux.Text(p1.X, p1.Y + 1, IntToStr(K), $FFFFFFFF);
      end;
    //}
    { Center points and transitions of polygons
    for K := 0 to fPolyCount - 1 do
      with fPolygons[K] do
      begin
        gRenderAux.Quad(CenterPoint.X, CenterPoint.Y, $AAFFFFFF);
        for L := 0 to NearbyCount - 1 do
          gRenderAux.Quad(NearbyPoints[L].X, NearbyPoints[L].Y, $AA000000);
      end;
    //}
  
    //{ DEFENCE SYSTEM
    // Show this defences only in case that show combat AI is not enabled;
    // when it is we need existing results not the actual (defences are updated each 1 min so it may be different)
    if OVERLAY_NAVMESH AND OVERLAY_DEFENCES AND not OVERLAY_AI_COMBAT then
    begin
      Owner := gMySpectator.HandID;
      FFF := TForwardFF.Create(true);
      try
        if FFF.FindDefenceLines(Owner, DefLines) then
          for K := 0 to DefLines.Count - 1 do
          begin
            L := DefLines.Lines[K].Polygon;
            gRenderAux.TriangleOnTerrain(
              fNodes[fPolygons[L].Indices[0]].X,
              fNodes[fPolygons[L].Indices[0]].Y,
              fNodes[fPolygons[L].Indices[1]].X,
              fNodes[fPolygons[L].Indices[1]].Y,
              fNodes[fPolygons[L].Indices[2]].X,
              fNodes[fPolygons[L].Indices[2]].Y, $300000FF);
          end;
        if FFF.FindDefensivePolygons(Owner, DefencePosArr) then
          for K := 0 to Length(DefencePosArr) - 1 do
          begin
            p1 := DefencePosArr[K].DirPoint.Loc;
            gRenderAux.CircleOnTerrain(p1.X, p1.Y, 2, $0900FFFF, $FFFFFFFF);
          end;
      finally
        FreeAndNil(FFF);
      end;
    end;
    //}
  end;
  
  
  end.
  
negative.code:     if (fPoint2PolygonArr[K - fMapX - 1] = 0) then AddToQueue(K - fMapX - 1, fPoint2PolygonArr[K]);
      if (fPoint2PolygonArr[K + fMapX + 1] = 0) then AddToQueue(K + fMapX + 1, fPoint2PolygonArr[K]);
    end;
  end;
  
  procedure TKMNavMesh.TieUpTilesWithPolygons();
    procedure GetNodesSortedByY(aIdx: Integer; var a,b,c: TKMPoint);
    begin
      a := fNodes[ fPolygons[aIdx].Indices[0] ];
      b := fNodes[ fPolygons[aIdx].Indices[1] ];
  