-- Loud Walking terrain.lua -- Copyright Duane Robertson (duane@duanerobertson.com), 2017 -- Distributed under the LGPLv2.1 (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) dofile(loud_walking.path..'/deco.lua') -- These seem to help the y loop some (but not much). local node = loud_walking.node local math_abs = math.abs local math_floor = math.floor local biomes = loud_walking.biomes local get_decoration = loud_walking.get_decoration local place_schematic = loud_walking.place_schematic local cloud_i = 0.5 local glass = {"loud_walking:sky_scrith", "loud_walking:cloud_scrith", "loud_walking:transparent_scrith"} local terrain_p = {offset = 0, scale = 10, seed = 8829, spread = {x = 40, y = 40, z = 40}, octaves = 6, persist = 0.4, lacunarity = 2} local terrain_noise local terrain_map = {} local cave_p = {offset = 0, scale = 1, seed = -3977, spread = {x = 30, y = 30, z = 30}, octaves = 3, persist = 0.8, lacunarity = 2} local cave_noise local cave_map = {} local cloud_p = {offset = 0, scale = 1, seed = -7874, spread = {x = 30, y = 30, z = 30}, octaves = 3, persist = 0.8, lacunarity = 2} local cloud_noise local cloud_map = {} local pod_size = {x=loud_walking.pod_size_x, y=loud_walking.pod_size_y, z=loud_walking.pod_size_z} local half_pod = {x=math_floor(pod_size.x / 2), y=math_floor(pod_size.y / 2), z=math_floor(pod_size.z / 2)} local bridge_size = loud_walking.bridge_size local vert_sep = loud_walking.vertical_sep local fcsize = {x=pod_size.x + bridge_size, z=pod_size.z + bridge_size} local bevel = half_pod.y local room_size = 20 local control_off = math_floor(room_size / 4) local biome_look = {} local cave_look = {} local ore_look = {} local tree_spacing = 5 loud_walking.fcsize = table.copy(fcsize) loud_walking.pod_size = table.copy(pod_size) loud_walking.bevel = bevel 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 loud_walking.biome_names = {} local biome_names = loud_walking.biome_names loud_walking.biome_names['common'] = { } loud_walking.biome_names['uncommon'] = { } for n, _ in pairs(loud_walking.biomes) do if string.find(n, 'ocean') or string.find(n, 'dunes') or string.find(n, 'shore') or string.find(n, 'swamp') or string.find(n, 'underground') then loud_walking.biome_names['uncommon'][#loud_walking.biome_names['uncommon']+1] = n else loud_walking.biome_names['common'][#loud_walking.biome_names['common']+1] = n end end local cave_stones = { 'default:stone', --"loud_walking:stone_with_moss", --"loud_walking:stone_with_lichen", --"loud_walking:stone_with_algae", --"loud_walking:stone_with_salt", } local mushroom_stones = { } local function get_biome(x, y, z) local px = math_floor(x / fcsize.x) local pz = math_floor(z / fcsize.z) if px % 10 == 6 and pz % 10 == 6 then return "control" else local hash = px * 1000 + pz if not biome_look[hash] then -- 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=px, y=pz})) local rarity = "common" if math.random(5) == 1 then rarity = "uncommon" end biome_look[hash] = biome_names[rarity][math.random(#loud_walking.biome_names[rarity])] local cave_lining if math.random(3) ~= 1 then cave_lining = cave_stones[math.random(#cave_stones)] end cave_look[hash] = cave_lining local sr = math.random(100) if sr == 1 then ore_look[hash] = 'default:stone_with_mese' elseif sr <= 3 then ore_look[hash] = 'default:stone_with_diamond' elseif sr <= 7 then ore_look[hash] = 'default:stone_with_gold' elseif sr <= 15 then ore_look[hash] = 'default:stone_with_copper' elseif sr <= 31 then ore_look[hash] = 'default:stone_with_iron' elseif sr <= 63 then ore_look[hash] = 'default:stone_with_coal' else ore_look[hash] = 'default:stone' end end --return 'deciduous_forest', cave_look[hash], ore_look[hash] return biome_look[hash], cave_look[hash], ore_look[hash] end end loud_walking.get_biome = get_biome function loud_walking.get_zone(pos, dist_z) local center_z if not dist_z then center_z = math.floor((pos.z + loud_walking.center_add) / loud_walking.wild_limits) * loud_walking.wild_limits dist_z = math.abs(center_z - (pos.z + loud_walking.max_height)) end local center_x = math.floor((pos.x + loud_walking.center_add) / loud_walking.wild_limits) * loud_walking.wild_limits local dist_x = math.abs(center_x - (pos.x + loud_walking.max_height)) --print(dist_x, dist_z, center_x, center_z) local dist1 = math.max(dist_x, dist_z) if dist1 >= loud_walking.suburb_limits + loud_walking.half_road_size + 1 then -- in the wild return nil, dist_x, dist_z, dist1 elseif dist1 < loud_walking.city_limits - loud_walking.half_road_size then -- in the city return 2, dist_x, dist_z, dist1 else -- in the suburbs return 1, dist_x, dist_z, dist1 end end local function get_height(fdx, fdz, index, terrain_scale, ocean) local terr = math_floor(terrain_map[index] * (terrain_scale or 1) + 0.5) if ocean and terr > 0 then terr = math_floor(terr * (terrain_scale + 1.5) / 4 + 0.5) end local d = - math_abs(math_abs(fdx - (half_pod.x - 0.5)) - math_abs(fdz - (half_pod.z - 0.5))) if math_abs(fdx - half_pod.x) > math_abs(fdz - half_pod.z) then d = d + half_pod.x - 2 else d = d + half_pod.z - 2 end if math_abs(terr) > d then if terr > 0 then terr = math_floor(d + 0.5) elseif not ocean then terr = math_floor(0.5 - d) end end return terr end loud_walking.terrain = function(minp, maxp, data, p2data, area, baseline, heightmap, schem) if not (minp and maxp and data and p2data and area and type(data) == 'table' and type(p2data) == 'table') then return end local extent_bottom = loud_walking.extent_bottom local extent_top = loud_walking.extent_top if maxp.y < baseline + extent_bottom or minp.y > baseline + extent_top then return end if not loud_walking.csize then loud_walking.csize = vector.add(vector.subtract(maxp, minp), 1) if not loud_walking.csize then return end end local csize = loud_walking.csize if not (terrain_noise and cloud_noise and cave_noise) then terrain_noise = minetest.get_perlin_map(terrain_p, {x=csize.x, y=csize.z}) cave_noise = minetest.get_perlin_map(cave_p, csize) cloud_noise = minetest.get_perlin_map(cloud_p, {x=csize.x, y=csize.z}) if not terrain_noise then return end end for k, v in pairs(loud_walking.tree_map) do loud_walking.tree_map[k] = nil end for z = minp.z, maxp.z, tree_spacing do for x = minp.x, maxp.x, tree_spacing do loud_walking.tree_map[ (x + math.random(tree_spacing - 1)) .. ',' .. (z + math.random(tree_spacing - 1)) ] = true end end local ground = half_pod.y local t1 = os.clock() local index = 0 local index3d = 0 local last_biome, last_px, last_py, last_pz, node_top, node_filler, node_water_top, node_water, depth_top, depth_water_top, depth_filler, node_stone, ocean, swamp, beach, dunes, height local biome_name, cave_lining, room_type, room_type_below, ore_type, land_biome terrain_map = terrain_noise:get2dMap_flat({x=minp.x, y=minp.z}, terrain_map) cave_map = cave_noise:get3dMap_flat(minp, cave_map) cloud_map = cloud_noise:get2dMap_flat({x=minp.x, y=minp.z}, cloud_map) for z = minp.z, maxp.z do local dz = z - minp.z local fdz = z % fcsize.z local pz = math_floor(z / fcsize.z) for x = minp.x, maxp.x do index = index + 1 local dx = x - minp.x local fdx = x % fcsize.x local px = math_floor(x / fcsize.x) local in_cave = false index3d = dz * csize.y * csize.x + dx + 1 local ivm = area:index(x, minp.y, z) local cave_height = 0 last_py = nil if px ~= last_px or pz ~= last_pz then biome_name, cave_lining, ore_type = get_biome(x, 0, z) land_biome = biome_name end if biome_name ~= 'control' and biome_name ~= last_biome then --node_top = biomes[biome_name].node_top or "default:dirt_with_grass" --node_filler = biomes[biome_name].node_filler or "default:dirt" --node_water = biomes[biome_name].node_water or "default:water_source" --node_water_top = biomes[biome_name].node_water_top or biomes[biome_name].node_water or "default:water_source" --depth_top = biomes[biome_name].depth_top or 1 --depth_water_top = biomes[biome_name].depth_water_top or 1 --depth_filler = biomes[biome_name].depth_filler or 1 --node_stone = biomes[biome_name].node_stone or "default:stone" ocean = string.find(biome_name, "ocean") and true or false swamp = string.find(biome_name, "swamp") and true or false beach = string.find(biome_name, "beach") and true or false dunes = string.find(biome_name, "dunes") and true or false end index = (z - minp.z) * csize.x + (x - minp.x) + 1 height = half_pod.y - 5 if not (biome_name == "underground" or biome_name == 'control') then height = get_height(fdx, fdz, index, biomes[biome_name].terrain_scale or 1, ocean) or height if ocean then height = height - 10 end end if ocean then local l_biome = string.gsub(biome_name, '_ocean$', '') if biomes[l_biome] then land_biome = l_biome end end for y = minp.y, maxp.y do local dy = y - minp.y local fdy = y - baseline + half_pod.y local eff_biome = (y < half_pod.y) and biome_name or land_biome if biome_name == 'control' then local room_px = math_floor((math_abs(fdx - half_pod.x) - 3) / room_size) local room_py = math_floor(fdy / 5) local room_pz = math_floor((math_abs(fdz - half_pod.z) - 3) / room_size) room_type = math_floor((math_abs(room_pz * 1000000 + room_py * 1000 + room_px) % 17) / 3) room_type_below = math_floor((math_abs(room_pz * 1000000 + (room_py - 1) * 1000 + room_px) % 17) / 3) if room_type_below == 1 and room_type == 3 then room_type = 0 end end if not (data[ivm] == node['air'] or data[ivm] == node['ignore']) then -- nop elseif biome_name == "control" and math_abs(fdx - half_pod.x) < 3 and math_abs(fdz - half_pod.z) < 3 then data[ivm] = node["loud_walking:air_ladder"] elseif fdz >= pod_size.z or fdx >= pod_size.x or fdy >= pod_size.y then if (fdy == half_pod.y and fdx == half_pod.x) or (fdy == half_pod.y and fdz == half_pod.z) then data[ivm] = node['loud_walking:scrith'] else --data[ivm] = node['loud_walking:vacuum'] end elseif math.min(fdx, pod_size.x - fdx) + math.min(fdy, pod_size.y - fdy) + math.min(fdz, pod_size.z - fdz) < bevel then --data[ivm] = node['loud_walking:vacuum'] in_cave = false elseif (fdx == 0 or fdx == pod_size.x - 1) or (fdz == 0 or fdz == pod_size.z - 1) or (fdy == 0 or fdy == pod_size.y - 1) or math.min(fdx, pod_size.x - fdx) + math.min(fdy, pod_size.y - fdy) + math.min(fdz, pod_size.z - fdz) < bevel + 1 then if math_abs(fdy - half_pod.y - 2) < 2 and (fdz == half_pod.z or fdx == half_pod.x) then --data[ivm] = node['loud_walking:vacuum'] else if biome_name == "control" then data[ivm] = node[glass[3]] elseif fdy < half_pod.y then data[ivm] = node["loud_walking:scrith"] elseif biome_name == "underground" then data[ivm] = node["loud_walking:scrith"] elseif fdy == pod_size.y - 1 then data[ivm] = node[glass[cloud_map[index] < cloud_i and 1 or 2]] else data[ivm] = node[glass[1]] end end elseif fdz == 0 and fdz == pod_size.z - 1 or fdx == 0 and fdx == pod_size.x - 1 or fdy == 0 and fdy == pod_size.y - 1 then data[ivm] = node['loud_walking:scrith'] elseif biome_name == "control" and fdy < pod_size.y then if (math_abs(fdx - half_pod.x) < 3 or math_abs(fdz - half_pod.z) < 3) then -- corridor if fdy % 5 == 0 then data[ivm] = node["loud_walking:control_floor"] end elseif ((math_abs(fdx - half_pod.x) % room_size == 3) or (math_abs(fdz - half_pod.z) % room_size == 3)) then if fdy % 5 == 0 then data[ivm] = node["loud_walking:control_floor"] elseif ((math_abs(fdx - half_pod.x) % room_size == 3 and (math_abs(fdz - half_pod.z) - (math_floor(room_size / 2) + 2)) % room_size > 3) or (math_abs(fdz - half_pod.z) % room_size == 3 and (math_abs(fdx - half_pod.x) - (math_floor(room_size / 2) + 2)) % room_size > 3)) then data[ivm] = node["loud_walking:control_wall"] end elseif fdy % 5 == 0 then if room_type == 1 then if room_type_below == 1 then data[ivm] = node["loud_walking:control_floor_alert_both"] else data[ivm] = node["loud_walking:control_floor_alert_up"] end elseif room_type_below == 1 then data[ivm] = node["loud_walking:control_floor_alert_down"] elseif room_type == 3 then data[ivm] = node["loud_walking:control_floor_growth"] else data[ivm] = node["loud_walking:control_floor"] end elseif room_type == 2 and fdy < pod_size.y then if math_abs(fdx - half_pod.x) % 4 < 3 and math_abs(fdz - half_pod.z) % 4 < 3 then data[ivm] = node["loud_walking:air_ladder"] end elseif room_type == 3 then if fdy % 5 == 1 then local sr2 = math.random(20) if sr2 == 1 then data[ivm] = node["loud_walking:control_plant_1"] elseif sr2 == 2 then data[ivm] = node["loud_walking:control_plant_2"] end end elseif room_type == 4 then if fdy % 5 == 4 and (((math_abs(fdx - half_pod.x) % room_size == 4 or math_abs(fdx - half_pod.x) % room_size == 2) and (math_abs(fdz - half_pod.z) - (math_floor(room_size / 2) + 2)) % room_size > 3) or ((math_abs(fdz - half_pod.z) % room_size == 4 or math_abs(fdz - half_pod.z) % room_size == 2) and (math_abs(fdx - half_pod.x) - (math_floor(room_size / 2) + 2)) % room_size > 3)) then data[ivm] = node["loud_walking:controls"] end else -- nop end elseif (((fdx == (half_pod.x - control_off) or fdx == (half_pod.x + control_off)) and fdz >= (half_pod.z - control_off) and fdz <= (half_pod.z + control_off)) or ((fdz == (half_pod.z - control_off) or fdz == (half_pod.z + control_off)) and fdx >= (half_pod.x - control_off) and fdx <= (half_pod.x + control_off))) and fdx ~= half_pod.x and fdz ~= half_pod.z and fdy == pod_size.y - 2 then data[ivm] = node["loud_walking:controls"] elseif (((fdx == (half_pod.x - control_off) or fdx == (half_pod.x + control_off)) and fdz >= (half_pod.z - control_off) and fdz <= (half_pod.z + control_off)) or ((fdz == (half_pod.z - control_off) or fdz == (half_pod.z + control_off)) and fdx >= (half_pod.x - control_off) and fdx <= (half_pod.x + control_off))) and fdx ~= half_pod.x and fdz ~= half_pod.z and fdy > pod_size.y - control_off then data[ivm] = node[glass[3]] elseif fdz >= (half_pod.z - control_off) and fdz <= (half_pod.z + control_off) and fdx >= (half_pod.x - control_off) and fdx <= (half_pod.x + control_off) and fdy == pod_size.y - control_off then data[ivm] = node[glass[3]] elseif not in_cave and (ocean or swamp or beach) and fdy > height + ground and fdy <= half_pod.y and fdy == height + ground + 1 then -- ** water decorations ** --local deco = get_decoration(biome_name) --if deco then -- data[ivm] = node[deco] --end elseif not in_cave and fdy == height + ground + 1 then if biomes[eff_biome].special_trees and loud_walking.tree_map[ x .. ',' .. z ] and (eff_biome ~= 'savanna' or math.random(20) == 1) then local i = 1 for j = 1, 100 do local sr = math.random(10) if sr > 1 or i == #biomes[eff_biome].special_trees then schem[#schem+1] = {name=biomes[eff_biome].special_trees[i], pos={x=x,y=y,z=z}, bound={fpos={x=fdx-x, y=fdy-y, z=fdz-z}}} break else i = i + 1 end end else local deco = get_decoration(eff_biome) if deco then data[ivm] = node[deco] end end elseif (ocean or swamp or beach) and fdy > height + ground and fdy <= half_pod.y and fdy >= half_pod.y - (biomes[biome_name].depth_water_top or 1) then data[ivm] = node[biomes[biome_name].node_water_top or biomes[biome_name].node_water or "default:water_source"] in_cave = false elseif (ocean or swamp or beach) and fdy > height + ground and fdy <= half_pod.y then data[ivm] = node[biomes[biome_name].node_water or "default:water_source"] in_cave = false elseif fdy > height + ground then --data[ivm] = node["air"] in_cave = false elseif cave_map[index3d] ^ 2 > (biome_name == "underground" and 0.5 or 1.3 - math.sin(fdy / (half_pod.y * 0.2))) then cave_height = cave_height + 1 if height + ground >= fdy and not in_cave and fdy > height + ground - 10 then data[ivm] = node[biomes[eff_biome].node_top or "default:dirt_with_grass"] elseif fdy == 1 then if not cave_lining and not ocean and not swamp and not beach and biome_name ~= "glacier" and math.random(6) == 1 then data[ivm] = node["default:lava_source"] elseif ocean or swamp or beach then data[ivm] = node[biomes[eff_biome].node_filler or "default:dirt"] end elseif (ocean or swamp or beach) and not in_cave and (biomes[eff_biome].node_stone or "default:stone") == "default:stone" and fdy < half_pod.y and math.random(20) == 1 then data[ivm] = node["loud_walking:glowing_fungal_stone"] elseif (ocean or swamp or beach) and fdy < half_pod.y then data[ivm] = node[biomes[biome_name].node_water or "default:water_source"] elseif cave_height == 3 and (biomes[eff_biome].node_filler or "default:dirt") == "default:dirt" and mushroom_stones[data[ivm - 3 * area.ystride]] and math.random(40) == 1 then data[ivm] = node["loud_walking:giant_mushroom_cap"] data[ivm - area.ystride] = node["loud_walking:giant_mushroom_stem"] data[ivm - 2 * area.ystride] = node["loud_walking:giant_mushroom_stem"] data[ivm - 3 * area.ystride] = node[biomes[eff_biome].node_filler or "default:dirt"] elseif cave_height == 2 and (biomes[eff_biome].node_filler or "default:dirt") == "default:dirt" and mushroom_stones[data[ivm - 2 * area.ystride]] and math.random(20) == 1 then data[ivm] = node["loud_walking:huge_mushroom_cap"] data[ivm - area.ystride] = node["loud_walking:giant_mushroom_stem"] data[ivm - 2 * area.ystride] = node[biomes[eff_biome].node_filler or "default:dirt"] elseif not in_cave and (biomes[eff_biome].node_stone or "default:stone") == "default:stone" and not cave_lining and math.random(10) == 1 then data[ivm] = node["loud_walking:stalagmite"] elseif not in_cave and (biomes[eff_biome].node_stone or "default:stone") == "default:ice" and math.random(10) == 1 then data[ivm] = node["loud_walking:icicle_up"] else --data[ivm] = node["air"] end in_cave = true elseif cave_lining and cave_map[index3d] ^ 2 > (biome_name == "underground" and 0.4 or 1.2 - math.sin(fdy / (half_pod.y * 0.2))) then data[ivm] = node[cave_lining] elseif fdy > height + ground - (biomes[eff_biome].depth_top or 1) then data[ivm] = node[biomes[eff_biome].node_top or "default:dirt_with_grass"] in_cave = false elseif fdy > height + ground - (biomes[eff_biome].depth_filler or 1) - (biomes[eff_biome].depth_top or 1) then data[ivm] = node[biomes[eff_biome].node_filler or "default:dirt"] in_cave = false else data[ivm] = node[biomes[eff_biome].node_stone or "default:stone"] if math.random(100) == 1 then data[ivm] = node[ore_type] end if in_cave and (biomes[eff_biome].node_stone or "default:stone") == "default:stone" and math.random(20) == 1 then data[ivm] = node["loud_walking:glowing_fungal_stone"] elseif in_cave and not (ocean or swamp or beach) and (biomes[eff_biome].node_stone or "default:stone") == "default:stone" and not cave_lining and math.random(10) == 1 then data[ivm - area.ystride] = node["loud_walking:stalactite"] elseif in_cave and not (ocean or swamp or beach) and (biomes[eff_biome].node_stone or "default:stone") == "default:ice" and math.random(10) == 1 then data[ivm - area.ystride] = node["loud_walking:icicle_down"] end in_cave = false end if not in_cave then cave_height = 0 end last_biome = biome_name ivm = ivm + area.ystride index3d = index3d + csize.x end last_px = px end last_pz = pz end local t2 = os.clock() --last_px = nil --last_pz = nil --for z = minp.z, maxp.z do -- local fdz = z % fcsize.z -- local pz = math_floor(z / fcsize.z) -- for x = minp.x, maxp.x do -- local fdx = x % fcsize.x -- local px = math_floor(x / fcsize.x) -- last_py = nil -- for y = minp.y, maxp.y do -- if fdz % tree_space == 0 and fdx % tree_space == 0 then -- local fdy = y - baseline + half_pod.y -- local pod = fdz < pod_size.z and fdx < pod_size.x and fdy > 0 and fdy < pod_size.y -- if px ~= last_px or pz ~= last_pz then -- biome_name, cave_lining = get_biome(x, y, z) -- ocean = string.find(biome_name, "ocean") and true or false -- swamp = string.find(biome_name, "swamp") and true or false -- node_top = biomes[biome_name].node_top or "default:dirt_with_grass" -- end -- local rx = x + math.random(tree_space) - 1 -- local rz = z + math.random(tree_space) - 1 -- if rz <= maxp.z and rx <= maxp.x then -- local index = (rz - minp.z) * csize.x + (rx - minp.x) + 1 -- height = get_height(rx, rz, index, biomes[biome_name].terrain_scale, ocean) -- --if biome_name ~= 'control' and pod and fdy == height + ground and biomes[biome_name].special_tree_prob and math.random(biomes[biome_name].special_tree_prob) == 1 then -- if biome_name ~= 'control' and pod and fdy == height + ground then -- local ivm = area:index(rx, y, rz) -- data[ivm] = node['default:glass'] -- if (swamp or data[ivm + area.ystride] ~= node["default:water_source"]) and (data[ivm] == node[node_top]) then -- if biomes[biome_name].special_trees then -- local tree_type = biomes[biome_name].special_trees[math.random(#biomes[biome_name].special_trees)] -- --schem[#schem+1] = {name=tree_type, pos={x=rx, y=y, z=rz}} -- else -- -- regular schematics? -- end -- end -- end -- end -- end -- end -- end --end end