loud_walking/mapgen.lua

310 lines
8.9 KiB
Lua

local node = loud_walking.node
local data = {}
local p2data = {} -- vm rotation data buffer
local lightmap = {}
local vm, emin, emax, a, csize, heightmap, biomemap
local div_sz_x, div_sz_z, minp, maxp, terrain, cave
local base_terrain_noise = {offset = 0,
scale = 20, seed = 8829, spread = {x = 40, y = 40, z = 40},
octaves = 6, persist = 0.4, lacunarity = 2}
local cave_noise = {offset = 0, scale = 1,
seed = -3977, spread = {x = 30, y = 30, z = 30}, octaves = 3,
persist = 0.8, lacunarity = 2}
local tree_biomes = {}
tree_biomes["deciduous_forest"] = {"deciduous_trees"}
tree_biomes["coniferous_forest"] = {"conifer_trees"}
tree_biomes["rainforest"] = {"jungle_trees"}
local biome_terrain_scale = {}
biome_terrain_scale["deciduous_forest"] = 0.5
biome_terrain_scale["coniferous_forest"] = 0.75
biome_terrain_scale["rainforest"] = 0.33
local function place_schematic(pos, schem, center)
local yslice = {}
if schem.yslice_prob then
for _, ys in pairs(schem.yslice_prob) do
yslice[ys.ypos] = ys.prob
end
end
if center then
pos.x = pos.x - math.floor(schem.size.x / 2)
pos.z = pos.z - math.floor(schem.size.z / 2)
end
for z = 0, schem.size.z - 1 do
for x = 0, schem.size.x - 1 do
local ivm = a:index(pos.x + x, pos.y, pos.z + z)
local isch = z * schem.size.y * schem.size.x + x + 1
for y = 0, schem.size.y - 1 do
if yslice[y] or 255 >= math.random(255) then
local prob = schem.data[isch].prob or schem.data[isch].param1 or 255
if prob >= math.random(255) and schem.data[isch].name ~= "air" then
data[ivm] = node(schem.data[isch].name)
end
local param2 = schem.data[isch].param2 or 0
p2data[ivm] = param2
end
ivm = ivm + a.ystride
isch = isch + schem.size.x
end
end
end
end
local function is_pod(x, y, z)
local min_x = math.floor((x + 32) / csize.x)
local min_y = math.floor((y + 32) / csize.y)
local min_z = math.floor((z + 32) / csize.z)
if min_x % 2 == 0 and min_y % 2 == 0 and min_z % 2 == 0 then
return true
end
--if min_x % 2 == 1 and min_y % 4 == 0 and min_z % 2 == 1 then
-- return true
--elseif min_x % 2 == 0 and min_y % 4 == 2 and min_z % 2 == 0 then
-- return true
--end
return false
end
local function connection(x, y, z)
local min_x = math.floor((x + 32) / csize.x)
local min_y = math.floor((y + 32) / csize.y)
local min_z = math.floor((z + 32) / csize.z)
--local seed_noise = minetest.get_perlin({offset = 0, scale = 32768,
--seed = 5202, spread = {x = 80, y = 80, z = 80}, octaves = 2,
--persist = 0.4, lacunarity = 2})
--math.randomseed(seed_noise:get2d({x=minp.x, y=minp.z}))
local ct = min_x % 2 + min_y % 2 + min_z % 2
local r = min_x % 2 + 2 * (min_y % 2) + 4 * (min_z % 2)
if ct == 1 then
return r
end
return nil
end
local function get_height(dx, dz, biome, index)
local terr
if index == true then
local terrain_noise = table.copy(base_terrain_noise)
terrain_noise.scale = terrain_noise.scale * biome_terrain_scale[biome]
terr = math.floor(minetest.get_perlin(terrain_noise):get2d({x=dx, y=dz}) + 0.5)
-- Still need csize here...
dx = (dx + 32) % 80
dz = (dz + 32) % 80
elseif not index then
index = dz * csize.x + dx + 1
terr = math.floor(terrain[index] + 0.5)
else
terr = math.floor(terrain[index] + 0.5)
end
local d = 38 - math.abs(math.abs(dx - 39.5) - math.abs(dz - 39.5))
if math.abs(terr) > d then
if terr > 0 then
terr = math.floor(d + 0.5)
else
terr = math.floor(0.5 - d)
end
end
return terr
end
local function get_biome(px, pz)
return "deciduous_forest"
end
function loud_walking.generate(p_minp, p_maxp, seed)
minp, maxp = p_minp, p_maxp
vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
vm:get_data(data)
vm:set_lighting({day = 15, night = 0}, minp, maxp)
lightmap = vm:get_light_data()
--p2data = vm:get_param2_data()
a = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
csize = vector.add(vector.subtract(maxp, minp), 1)
local write = false
-- Deal with memory issues. This, of course, is supposed to be automatic.
local mem = math.floor(collectgarbage("count")/1024)
if mem > 300 then
print("Manually collecting garbage...")
collectgarbage("collect")
end
local px = math.floor((minp.x + 32) / csize.x)
local pz = math.floor((minp.z + 32) / csize.z)
local biome = get_biome(px, pz)
local top = node("default:dirt_with_grass")
-- use the same seed (based on perlin noise).
local seed_noise = minetest.get_perlin({offset = 0, scale = 32768,
seed = 5202, spread = {x = 80, y = 80, z = 80}, octaves = 2,
persist = 0.4, lacunarity = 2})
math.randomseed(seed_noise:get2d({x=minp.x, y=minp.z}))
local terrain_noise = table.copy(base_terrain_noise)
terrain_noise.scale = terrain_noise.scale * biome_terrain_scale[biome]
terrain = minetest.get_perlin_map(terrain_noise, csize):get2dMap_flat(minp)
cave = minetest.get_perlin_map(cave_noise, csize):get3dMap_flat(minp)
local pod = is_pod(minp.x, minp.y, minp.z)
local connection = connection(minp.x, minp.y, minp.z)
local index = 0
local index3d = 0
for z = minp.z, maxp.z do
local dz = z - minp.z
for x = minp.x, maxp.x do
index = index + 1
local dx = x - minp.x
index3d = dz * csize.y * csize.x + dx + 1
local ivm = a:index(x, minp.y, z)
local terr = get_height(dx, dz, biome, index)
local in_cave = false
for y = minp.y, maxp.y do
local dy = y - minp.y
if pod then
if math.min(dx, csize.x - dx) + math.min(dy, csize.y - dy) + math.min(dz, csize.z - dz) < 40 then
data[ivm] = node("air")
--lightmap[ivm] = 0
in_cave = false
elseif math.min(dx, csize.x - dx) + math.min(dy, csize.y - dy) + math.min(dz, csize.z - dz) < 41 then
if dy < terr + 40 then
data[ivm] = node("loud_walking:scrith")
lightmap[ivm] = 0
else
data[ivm] = node("default:glass")
end
in_cave = false
write = true
elseif (dx == 0 or dx == csize.x - 1) or (dz == 0 or dz == csize.z - 1) or (dy == 0 or dy == csize.y - 1) then
if math.abs(dy - 42) < 2 and (dz == 40 or dx == 40) then
data[ivm] = node("air")
else
if dy < terr + 40 then
data[ivm] = node("loud_walking:scrith")
lightmap[ivm] = 0
else
data[ivm] = node("default:glass")
end
write = true
end
in_cave = false
elseif dy > terr + 40 then
data[ivm] = node("air")
in_cave = false
elseif cave[index3d] ^ 2 > 0.1 + dy / 40 then
if terr + 40 >= dy and not in_cave and dy > terr + 30 then
data[ivm] = node("default:dirt")
else
data[ivm] = node("air")
end
in_cave = true
lightmap[ivm] = 0
elseif dy > terr + 39 then
data[ivm] = top
lightmap[ivm] = 0
in_cave = false
write = true
elseif dy > terr + 37 then
data[ivm] = node("default:dirt")
lightmap[ivm] = 0
in_cave = false
write = true
else
data[ivm] = node("default:stone")
lightmap[ivm] = 0
in_cave = false
write = true
end
elseif connection and dy == 40 and ((dx == 40 and connection % 4 == 0) or (dz == 40 and connection % 2 == 1)) then
data[ivm] = node("default:stone")
lightmap[ivm] = 0
write = true
end
ivm = ivm + a.ystride
index3d = index3d + csize.x
end
end
end
if pod then
for dz = 0, 75, 5 do
for dx = 0, 75, 5 do
if math.random(2) == 1 then
local x = minp.x + dx + math.random(0, 4)
local z = minp.z + dz + math.random(0, 4)
local y = minp.y + get_height(x - minp.x, z - minp.z, biome) + 40
local ivm = a:index(x, y, z)
if data[ivm + a.ystride] == node("air") and (data[ivm] == node("default:dirt") or data[ivm] == node("default:dirt_with_grass") or data[ivm] == node("default:dirt_with_snow")) then
if tree_biomes[biome] then
local tree_type = tree_biomes[biome][math.random(#tree_biomes[biome])]
local schem = loud_walking.schematics[tree_type][math.random(#loud_walking.schematics[tree_type])]
local pos = {x=x, y=y, z=z}
-- The minetest schematic functions don't seem very accurate.
place_schematic(pos, schem, true)
end
end
end
end
end
end
if write then
vm:set_data(data)
minetest.generate_ores(vm, minp, maxp)
minetest.generate_decorations(vm, minp, maxp)
--vm:set_param2_data(p2data)
--vm:set_lighting({day = 15, night = 0})
vm:set_light_data(lightmap)
vm:calc_lighting(minp, maxp, false)
vm:update_liquids()
vm:write_to_map()
end
vm, a, heightmap, biomemap = nil, nil, nil, nil
end
function loud_walking.respawn(player)
local success = false
while not success do
local px = math.random(-10, 10) * 2
local pz = math.random(-10, 10) * 2
-- How do we get csize before generation?
local dx = math.random(80) - 1
local dz = math.random(80) - 1
local x = (dx - 32) + 80 * px
local z = (dz - 32) + 80 * pz
local y = get_height(x, z, get_biome(px, pz), true) + 8
local pos = {x=x,y=y,z=z}
local node = minetest.get_node(pos)
if node.name ~= "air" then
player:setpos(pos)
success = true
end
--print(node.name)
end
end