- Code: Select all
local DMAX = 20
local AREA_SIZE = 80
minetest.register_on_mapgen_init(function(mgparams)
minetest.set_mapgen_params({mgname="singlenode", flags="nolight", flagmask="nolight"})
end)
local cache = {}
local function get_base_surface_at_point(x, z, noise)
local index = 65536*x+z
if cache[index] ~= nil then return cache[index] end
cache[index] = 20*noise:get2d({x=x, y=z})
return cache[index]
end
local function surface_at_point(x, z, noise)
if -AREA_SIZE<x and x<AREA_SIZE and -AREA_SIZE<z and z<AREA_SIZE then
if flat_height~=nil then return flat_height end
local s=0
local n=0
for x1=-AREA_SIZE, AREA_SIZE do
n=n+2
s=s+get_base_surface_at_point(x1, -AREA_SIZE, noise)+get_base_surface_at_point(x1, AREA_SIZE, noise)
end
for y1=-AREA_SIZE+1, AREA_SIZE-1 do
n=n+2
s=s+get_base_surface_at_point(-AREA_SIZE, y1, noise)+get_base_surface_at_point(AREA_SIZE, y1, noise)
end
flat_height = s/n
return s/n
end
return get_base_surface_at_point(x, z, noise)
end
local SMOOTHED = AREA_SIZE+2*DMAX
local HSMOOTHED = AREA_SIZE+DMAX
local INSIDE = AREA_SIZE-DMAX
local function smooth(x, z, noise)
local s=0
local w=0
for xi=-DMAX, DMAX do
for zi=-DMAX, DMAX do
local d2=xi*xi+zi*zi
if d2<DMAX*DMAX then
local w1 = 1-d2/(DMAX*DMAX)
local w2 = 15/16*w1*w1
w = w+w2
s=s+w2*surface_at_point(x+xi, z+zi, noise)
end
end
end
return s/w
end
local function smooth_surface(x, z, noise)
if -SMOOTHED>x or x>SMOOTHED or -SMOOTHED>z or z>SMOOTHED then return surface_at_point(x, z, noise) end
if -INSIDE<x and x<INSIDE and -INSIDE<z and z<INSIDE then return surface_at_point(x, z, noise) end
if -HSMOOTHED>x or x>HSMOOTHED or -HSMOOTHED>z or z>HSMOOTHED then
local s = surface_at_point(x, z, noise)
local s1 = smooth(x, z, noise)
local m = math.max(math.abs(x), math.abs(z))
return ((m-HSMOOTHED)*s+(SMOOTHED-m)*s1)/DMAX
end
return smooth(x, z, noise)
end
minetest.register_on_generated(function(minp, maxp, seed)
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local a = VoxelArea:new{
MinEdge={x=emin.x, y=emin.y, z=emin.z},
MaxEdge={x=emax.x, y=emax.y, z=emax.z},
}
local data = vm:get_data()
local c_grass = minetest.get_content_id("default:dirt_with_grass")
local noise = minetest.get_perlin(12345, 6, 0.5, 256)
local ni = 1
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local y=math.floor(smooth_surface(x, z, noise))
if y<=maxp.y and y>=minp.y then
local vi = a:index(x, y, z)
data[vi] = c_grass
end
end
end
vm:set_data(data)
vm:calc_lighting(
{x=minp.x-16, y=minp.y, z=minp.z-16},
{x=maxp.x+16, y=maxp.y, z=maxp.z+16}
)
vm:write_to_map(data)
end)
License of the code: WTFPL







[/spoiler]



















