anchor.diff:    procedure ApplyMagicBrush(MaskKind: TKMTileMaskKind);
     var
       I: Integer;
  -    TileOwnTerKinds: TKMTerrainKindsArray;
  -    TileNodeTerKinds: TKMTerrainKindsArray;
  -    AroundTerKinds: TKMTerrainKindsArray;
  +    tileOwnTerKinds: TKMTerrainKindCorners;
  +    tileNodeTerKinds: TKMTerrainKindCorners;
  +    aroundTerKinds: TKMTerrainKindCorners;
       CollisionFound: Boolean;
       LayerOrder: array of TKMTileMaskInfo;
       MaskType: TKMTileMaskType;
  
positive.code:     end;
    end;
  
    function GetTerKindsAround(aCell: TKMPoint): TKMTerrainKindsArray;
    var
      I: Integer;
      TerKind: TKMTerrainKind;
      TerKindFound: array[0..3] of Boolean;
    begin
      SetLength(Result, 4);
  
      //get all 4 terkind for corners
      for I := 0 to 3 do
      begin
        TerKind := GetCornerTerKind(I, aCell, TerKindFound[I]);
        if TerKindFound[I] then
          Result[I] := TerKind;
      end;
  
      //For corner, where no terkind from around tiles were found - replace it with neighbour corner terkind (there can't be 2 missed terkinds in a row)
      for I := 0 to 3 do
        if not TerKindFound[I] then
          Result[I] := Result[(I+1) mod 4];
  
    end;
  
    function HasCollision(aTerKind1, aTerKind2: TKMTerrainKind): Boolean;
  
negative.code: 
  var
    I,K,M: Integer;
    vertexTKinds: TKMTerrainKindCorners;
    sameTKind, noCustomTK: Boolean;
    terKindCnts, mostTKIs: array[0..3] of Integer;
    mostTKCnt, mostTKI, vrxCnt: Integer;
  begin
    Result := False;
    aEffectiveTKind := tkCustom;
  
    vrxCnt := GetVertexCornerTerKinds(X, Y, vertexTKinds);
    sameTKind := True;
  
    Assert(vrxCnt > 0, 'Can''t find vertex corners');
  
    for K := 0 to vrxCnt - 1 do
    begin
      terKindCnts[K] := 0;
      mostTKIs[K] := 0;
    end;
  
    //Find most popular TerKind
    //Count all TerKinds occurrences first
    for K := 0 to vrxCnt - 1 do
      for M := K + 1 to vrxCnt - 1 do
        if vertexTKinds[K] = vertexTKinds[M] then
          Inc(terKindCnts[K]);
  
    //Get Most popular one index
    mostTKCnt := -1;
    I := 0;
    for K := 0 to vrxCnt - 1 do
      if terKindCnts[K] > mostTKCnt then
      begin
        I := 0;
        mostTKIs[I] := K;
        mostTKCnt := terKindCnts[K];
        Inc(I);
      end
      else
      if terKindCnts[K] = mostTKCnt then
      begin
        mostTKIs[I] := K;
        Inc(I);
      end;
  
    mostTKI := mostTKIs[KaMRandom(I, 'TKMTerrainPainter.TryGetVertexEffectiveTerKind')];
  
    noCustomTK := vertexTKinds[0] <> tkCustom;
    for K := 1 to vrxCnt - 1 do
    begin
      sameTKind := sameTKind and EqualTKinds(vertexTKinds[K], vertexTKinds[K-1]);
      noCustomTK := noCustomTK and (vertexTKinds[K] <> tkCustom);
    end;
  
    //Replace TerKind with most popular one if there all TerKinds are equal or if there is no custom TerKinds
    if sameTKind or noCustomTK then
    begin
      aEffectiveTKind := vertexTKinds[mostTKI];
      Result := True;
    end;
  end;
  