anchor.diff:    end;
   
     FlattenTerrain(Loc);
  -  //If tile stonemason is standing on becomes unwalkable, flatten it too so he doesn't get stuck all the time
  -  if not CheckHeightPass(KMPointBelow(Loc),CanWalk) then
  -    FlattenTerrain(KMPointBelow(Loc));
   end;
   
   
  
positive.code: 
      //We UpdateTransition when the stone becomes grass, Bits can never = 15
      //The tile in center is fully mined and one below has Stoncutter on it,
      //hence there cant be any tile surrounded by stones frmo all sides
      Assert(Bits < 15);
      Land[Y,X].Terrain  := TileID[Bits];
      Land[Y,X].Rotation := RotID[Bits];
  
negative.code: end;
  
  
  //Interpolate between surrounding 8 vertices (X and Y, no diagonals)
  //Also it is FlattenTerrain duty to preserve walkability if there are units standing
  procedure TTerrain.FlattenTerrain(Loc:TKMPoint; aRebuildWalkConnects:boolean=true);
  
    //If tiles with units standing on them become unwalkable we should try to fix them
    procedure EnsureWalkable(aX,aY:word);
    begin
      //We did not recalculated passability yet, hence tile has CanWalk but CheckHeightPass=False already
      if (Land[aY,aX].IsUnit <> nil)
      and CheckPassability(KMPoint(aX,aY),CanWalk)
      and not CheckHeightPass(KMPoint(aX,aY),CanWalk)
      and (fGame.GameState <> gsEditor) //Allow units to become "stuck" in MapEd, as height changing is allowed anywhere
      then
        //This recursive call should be garanteed to exit, as eventually the terrain will be flat enough
        FlattenTerrain(KMPoint(aX,aY), False); //WalkConnect should be done at the end
    end;
  
    function GetAverage(X,Y:word):byte;
    begin
      Result := Round(Mix(Land[Y,X].Height, (Land[Max(Y-1,1),X].Height + Land[Min(Y+1,MapY),X].Height + Land[Y,Max(X-1,1)].Height + Land[Loc.Y,Min(Loc.X+1,MapX)].Height) / 4, 0.5));
    end;
  
  var i,k: word; V1,V2,V3,V4:byte;
  begin
    Assert(TileInMapCoords(Loc.X, Loc.Y));
  
    V1 := GetAverage(Loc.X,Loc.Y);
    V2 := GetAverage(Loc.X+1,Loc.Y);
    V3 := GetAverage(Loc.X,Loc.Y+1);
    V4 := GetAverage(Loc.X+1,Loc.Y+1);
  
    if CanElevate in Land[Loc.Y,Loc.X].Passability then     Land[Loc.Y,Loc.X].Height := V1;
    if CanElevate in Land[Loc.Y,Loc.X+1].Passability then   Land[Loc.Y,Loc.X+1].Height := V2;
    if CanElevate in Land[Loc.Y+1,Loc.X].Passability then   Land[Loc.Y+1,Loc.X].Height := V3;
    if CanElevate in Land[Loc.Y+1,Loc.X+1].Passability then Land[Loc.Y+1,Loc.X+1].Height := V4;
  
    //All 9 tiles around and including this one could have become unwalkable and made a unit stuck, so check them all
    for i:=Max(Loc.Y-1,1) to Min(Loc.Y+1,MapY-1) do
      for k:=Max(Loc.X-1,1) to Min(Loc.X+1,MapX-1) do
        EnsureWalkable(k,i);
  
    RebuildLighting(Loc.X-2,Loc.X+3,Loc.Y-2,Loc.Y+3);
    RecalculatePassabilityAround(Loc); //Changing height will affect the cells around this one
  