anchor.diff:  end;
   
   
  +procedure TKMTerrainPainter.GenerateAddnData;
  +var Accuracy: array [1 .. MAX_MAP_SIZE, 1 .. MAX_MAP_SIZE] of Byte;
  +
  +  procedure SetTerrainKindVertex(X,Y: Integer; T:TTerrainKind; aAccuracy:Byte);
  +  begin
  +    if not fTerrain.TileInMapCoords(X,Y) then Exit;
  +    //Skip if already set more accurately
  +    if aAccuracy < Accuracy[Y,X] then Exit;
  +    Land2[Y,X].TerType := T;
  +    Accuracy[Y,X] := aAccuracy;
  +  end;
  +
  +  procedure SetTerrainKindTile(X,Y: Integer; T:TTerrainKind; aAccuracy:Byte);
  +  begin
  +    SetTerrainKindVertex(X  , Y  , T, aAccuracy);
  +    SetTerrainKindVertex(X+1, Y  , T, aAccuracy);
  +    SetTerrainKindVertex(X  , Y+1, T, aAccuracy);
  +    SetTerrainKindVertex(X+1, Y+1, T, aAccuracy);
  +  end;
  +
  +const
  +  SPECIAL_TILES = [24,25,194,198,199,202,206,207,214,216..219,221..233,246]; //Waterfalls and bridges
  +  OTHER_WATER_TILES = [193,208,209,240,244]; //Water tiles not used in painting (fast, straight, etc.)
  +  //Accuracies
  +  ACC_MAX = 5;  //Special tiles
  +  ACC_HIGH = 4; //Primary tiles
  +  ACC_MED = 3; //Random tiling
  +  ACC_LOW = 1; //Edges
  +  ACC_MIN = 0; //Coal random tiling (edges are better in this case)
  +var
  +  I,K,J,Rot: Integer;
  +  A: Byte;
  +  T, T2: TTerrainKind;
  +begin
  +  for I := 1 to MAX_MAP_SIZE do
  +  for K := 1 to MAX_MAP_SIZE do
  +  begin
  +    Land2[I,K].TerType := tkCustom; //Everything custom by default
  +    Accuracy[I,K] := 0;
  +  end;
  +
  +  for I := 1 to MAX_MAP_SIZE do
  +  for K := 1 to MAX_MAP_SIZE do
  +    //Special tiles such as bridges should remain as tkCustom
  +    if fTerrain.Land[I,K].Terrain in SPECIAL_TILES then
  +      SetTerrainKindTile(K, I, tkCustom, ACC_MAX) //Maximum accuracy
  +    else
  +      //Water tiles not used in painting (fast, straight, etc.)
  +      if fTerrain.Land[I,K].Terrain in OTHER_WATER_TILES then
  +        SetTerrainKindTile(K, I, tkWater, ACC_MED) //Same accuracy as random tiling (see below)
  +      else
  +        for T := Low(TTerrainKind) to High(TTerrainKind) do
  +          if T <> tkCustom then
  +          begin
  +            //METHOD 1: Terrain type is the primary tile for this terrain
  +            if fTerrain.Land[I,K].Terrain = Abs(Combo[T,T,1]) then
  +              SetTerrainKindTile(K, I, T, ACC_HIGH);
  +
  +            //METHOD 2: Terrain type is in RandomTiling
  +            for J := 1 to RandomTiling[T,0] do
  +              if fTerrain.Land[I,K].Terrain = RandomTiling[T,J] then
  +              begin
  +                A := ACC_MED; //Random tiling is fairly accurate
  +                if T = tkCoal then A := ACC_MIN; //Random coal tiles are also used for edges, so edges are more accurate
  +                SetTerrainKindTile(K, I, T, A);
  +              end;
  +
  +            //METHOD 3: Edging data
  +            A := ACC_LOW; //Edging data is not as accurate as other methods (some edges reuse the same tiles)
  +            for T2 := Low(TTerrainKind) to High(TTerrainKind) do
  +            begin
  +              //1 vertex is T, 3 vertexes are T2
  +              if fTerrain.Land[I,K].Terrain = Abs(Combo[T,T2,1]) then
  +              begin
  +                Rot := fTerrain.Land[I,K].Rotation mod 4;
  +                if Combo[T,T2,1] < 0 then Rot := (Rot+2) mod 4; //Flip
  +                case Rot of
  +                  0: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                  1: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  2: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                  3: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                end;
  +              end;
  +              //Half T, half T2
  +              if fTerrain.Land[I,K].Terrain = Abs(Combo[T,T2,2]) then
  +              begin
  +                Rot := fTerrain.Land[I,K].Rotation mod 4;
  +                if Combo[T,T2,2] < 0 then Rot := (Rot+2) mod 4; //Flip
  +                case Rot of
  +                  0: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                  1: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  2: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  3: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                end;
  +              end;
  +              //3 vertex are T, 1 vertexes is T2
  +              if fTerrain.Land[I,K].Terrain = Abs(Combo[T,T2,3]) then
  +              begin
  +                Rot := fTerrain.Land[I,K].Rotation mod 4;
  +                if Combo[T,T2,3] < 0 then Rot := (Rot+2) mod 4; //Flip
  +                case Rot of
  +                  0: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T2, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  1: begin
  +                       SetTerrainKindVertex(K,   I,   T2, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  2: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T2, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T, A);
  +                     end;
  +                  3: begin
  +                       SetTerrainKindVertex(K,   I,   T, A);
  +                       SetTerrainKindVertex(K+1, I,   T, A);
  +                       SetTerrainKindVertex(K,   I+1, T, A);
  +                       SetTerrainKindVertex(K+1, I+1, T2, A);
  +                     end;
  +                end;
  +              end;
  +            end;
  +          end;
  +end;
  +
  +
   //Skip the KaM data and load MapEd vertice info
   procedure TKMTerrainPainter.LoadFromFile(FileName: string);
   var
  
positive.code: 
  procedure TKMTerrainPainter.EditBrush(aLoc: TKMPoint; aTile: Byte);
  var
    I,K,Rad: Integer;
  begin
    //Cell below cursor
    MapXc := EnsureRange(round(GameCursor.Float.X+0.5),1,fTerrain.MapX);
  
negative.code:   finally
      S.Free;
    end;
    //We can regenerate the MapEd data if it's missing (won't be as good as the original)
    if not MapEdChunkFound then
    begin
      fLog.AddNoTime('Regenerating missing MapEd data as best as we can');
      GenerateAddnData;
    end;
  end;
  
  
  