anchor.diff:  begin
     //Make sure it is within map, roads can be built on edge
     Result := TileInMapCoords(aX, aY);
  +
     case aFieldType of
  -    ft_Road:  Result := Result and (tpMakeRoads  in Land[aY, aX].Passability);
  +    ft_Road:  Result := Result and (tpMakeRoads in Land[aY, aX].Passability);
       ft_Corn,
       ft_Wine:  Result := Result and TileGoodForField(aX, aY);
       else      Result := False;
  
positive.code:     else
        Result := fncNone;
    end;
    function GetFenceEnabled(X, Y: SmallInt): Boolean;
    begin
      Result := True;
      if not TileInMapCoords(X,Y) then exit;
      if (TileIsCornField(Loc) and TileIsCornField(KMPoint(X,Y)))or //Both are Corn
         (TileIsWineField(Loc) and TileIsWineField(KMPoint(X,Y)))or //Both are Wine
        ((Land[Loc.Y,Loc.X].TileLock in [tlFenced, tlDigged]) and (Land[Y,X].TileLock in [tlFenced, tlDigged])) then //Both are either house fence
        Result := False;
    end;
  begin
   if not TileInMapCoords(Loc.X, Loc.Y) then exit;
  
    Land[Loc.Y,Loc.X].Fence := GetFenceType;
    if Land[Loc.Y,Loc.X].Fence = fncNone then
      Land[Loc.Y,Loc.X].FenceSide := 0
    else
    begin
      Land[Loc.Y,Loc.X].FenceSide := Byte(GetFenceEnabled(Loc.X,Loc.Y-1)) + //N
                                     Byte(GetFenceEnabled(Loc.X-1,Loc.Y)) * 2 + //E
                                     Byte(GetFenceEnabled(Loc.X+1,Loc.Y)) * 4 + //W
                                     Byte(GetFenceEnabled(Loc.X,Loc.Y+1)) * 8; //S
    end;
  
    if CheckSurrounding then
    begin
      UpdateFences(KMPoint(Loc.X-1, Loc.Y), False);
      UpdateFences(KMPoint(Loc.X+1, Loc.Y), False);
      UpdateFences(KMPoint(Loc.X, Loc.Y-1), False);
      UpdateFences(KMPoint(Loc.X, Loc.Y+1), False);
    end;
  end;
  
  
  {Cursor position should be converted to tile-coords respecting tile heights}
  function TKMTerrain.ConvertCursorToMapCoord(inX,inY:single):single;
  var ii:integer; Xc,Yc:integer; Tmp:integer; Ycoef:array[-2..4]of single;
  begin
    Xc := EnsureRange(round(inX+0.5),1,fMapX-1); //Cell below cursor without height check
    Yc := EnsureRange(round(inY+0.5),1,fMapY-1);
  
    for ii:=-2 to 4 do //make an array of tile heights above and below cursor (-2..4)
    begin
      Tmp := EnsureRange(Yc+ii,1,fMapY);
      Ycoef[ii] := (Yc-1)+ii-(Land[Tmp,Xc].Height*(1-frac(inX))
                             +Land[Tmp,Xc+1].Height*frac(inX))/CELL_HEIGHT_DIV;
    end;
  
    Result := Yc; //Assign something incase following code returns nothing
  
    for ii:=-2 to 3 do //check if cursor in a tile and adjust it there
      if InRange(inY, Ycoef[ii], Ycoef[ii+1]) then
      begin
        Result := Yc+ii-(Ycoef[ii+1]-inY) / (Ycoef[ii+1]-Ycoef[ii]);
        break;
      end;
  
  
negative.code: 
  
  procedure TKMTerrain.AddHouseRemainder(Loc: TKMPoint; aHouseType: THouseType; aBuildState: THouseBuildState);
  var
    I, K: Integer;
    HA:   THouseArea;
  begin
    HA := gRes.Houses[aHouseType].BuildArea;
  
    if aBuildState in [hbs_Stone, hbs_Done] then //only leave rubble if the construction was well underway (stone and above)
    begin
      //Leave rubble
      for I := 2 to 4 do
        for K := 2 to 4 do
          if (HA[I - 1, K] <> 0) and (HA[I, K - 1] <> 0)
          and (HA[I - 1, K - 1] <> 0) and (HA[I, K] <> 0) then
            Land[Loc.Y + I - 4, Loc.X + K - 3].Obj := 68 + KaMRandom(6);
  
      //Leave dug terrain
      for I := 1 to 4 do
        for K := 1 to 4 do
          if HA[I, K] <> 0 then
          begin
            Land[Loc.Y + I - 4, Loc.X + K - 3].TileOverlay := to_Dig3;
            Land[Loc.Y + I - 4, Loc.X + K - 3].TileLock    := tlNone;
          end;
    end else
    begin
      //For glyphs leave nothing
      for I := 1 to 4 do
        for K:=1 to 4 do
          if HA[I, K] <> 0 then
            Land[Loc.Y + I - 4, Loc.X + K - 3].TileLock := tlNone;
    end;
  
    UpdatePassability(KMRect(Loc.X - 3, Loc.Y - 4, Loc.X + 2, Loc.Y + 1));
    UpdateWalkConnect([wcWalk, wcRoad, wcWork],
                      KMRect(Loc.X - 3, Loc.Y - 4, Loc.X + 2, Loc.Y + 1),
                      (aBuildState in [hbs_Stone, hbs_Done])); //Rubble objects block diagonals
  end;
  
  
  