add obsidian_lining option, clean up mapgen code

This commit is contained in:
FaceDeer 2018-05-12 17:03:43 -06:00
parent 79c500d44f
commit 70a9f562b7
5 changed files with 128 additions and 40 deletions

@ -1,6 +1,9 @@
Note: voxelarea_iterator.lua is separately licensed under the LGPL, see that file for details.
The rest of this mod is covered by:
MIT License
Copyright (c) 2017 FaceDeer
Copyright (c) 2018 FaceDeer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

@ -26,5 +26,6 @@ end
setting("int", "spread", 400, "Approximate spacing between magma conduits")
setting("bool", "remove_default_lava", true, "Removes default mapgen lava")
setting("bool", "ameliorate_floods", true, "Ameliorate lava floods on the surface")
setting("bool", "obsidian_lining", true, "Add an obsidian lining to magma conduits")
setting("int", "upper_limit", 512, "Upper extent of magma conduits")
setting("int", "lower_limit", -31000, "Lower extent of magma conduits")

@ -5,6 +5,7 @@ local modpath = minetest.get_modpath(minetest.get_current_modname())
--load companion lua files
dofile(modpath.."/config.lua")
dofile(modpath.."/voxelarea_iterator.lua")
if magma_conduits.config.remove_default_lava then
minetest.register_alias_force("mapgen_lava_source", "air") -- veins of lava are far more realistic
@ -25,6 +26,7 @@ minetest.register_ore({
"default:stone_with_coal",
"default:stone_with_iron",
"default:stone_with_copper",
"default:stone_with_tin",
"default:stone_with_gold",
"default:stone_with_diamond",
"default:dirt",
@ -52,31 +54,50 @@ minetest.register_ore({
-------------------------------------------------------------------------------------------------
-- Ameliorate lava floods on the surface world by removing lava that's poised to spill
if not magma_conduits.config.ameliorate_floods then return end
if not (magma_conduits.config.ameliorate_floods or magma_conduits.config.obsidian_lining) then return end
local ameliorate_floods = magma_conduits.config.ameliorate_floods
local obsidian_lining = magma_conduits.config.obsidian_lining
local c_air = minetest.get_content_id("air")
local c_lava = minetest.get_content_id("default:lava_source")
local c_stone = minetest.get_content_id("default:stone")
local c_obsidian = minetest.get_content_id("default:obsidian")
local water_level = tonumber(minetest.get_mapgen_setting("water_level"))
local is_adjacent_to_air = function(area, data, pos)
return (data[area:index(pos.x+1, pos.y, pos.z)] == c_air
or data[area:index(pos.x-1, pos.y, pos.z)] == c_air
or data[area:index(pos.x, pos.y, pos.z+1)] == c_air
or data[area:index(pos.x, pos.y, pos.z-1)] == c_air
or data[area:index(pos.x, pos.y-1, pos.z)] == c_air)
local is_adjacent_to_air = function(area, data, x, y, z)
return (data[area:index(x+1, y, z)] == c_air
or data[area:index(x-1, y, z)] == c_air
or data[area:index(x, y, z+1)] == c_air
or data[area:index(x, y, z-1)] == c_air
or data[area:index(x, y-1, z)] == c_air)
end
magma_conduits.remove_unsupported_lava = function(area, data, vi)
local remove_unsupported_lava
remove_unsupported_lava = function(area, data, vi, x, y, z)
--if too far from water level, abort. Caverns are on their own.
if y < water_level or y > 512 or not area:contains(x, y, z) then return end
if data[vi] == c_lava then
local pos = area:position(vi)
if is_adjacent_to_air(area, data, pos) then
if is_adjacent_to_air(area, data, x, y, z) then
data[vi] = c_air
magma_conduits.remove_unsupported_lava(area, data, area:index(pos.x+1, pos.y, pos.z))
magma_conduits.remove_unsupported_lava(area, data, area:index(pos.x-1, pos.y, pos.z))
magma_conduits.remove_unsupported_lava(area, data, area:index(pos.x, pos.y, pos.z+1))
magma_conduits.remove_unsupported_lava(area, data, area:index(pos.x, pos.y, pos.z-1))
magma_conduits.remove_unsupported_lava(area, data, area:index(pos.x, pos.y+1, pos.z))
for pi, x2, y2, z2 in area:iter_xyz(x-1, y, z-1, x+1, y+1, z+1) do
if pi ~= vi and area:containsi(pi) then
remove_unsupported_lava(area, data, pi, x2, y2, z2)
end
end
end
end
end
local obsidianize = function(area, data, vi, x, y, z, minp, maxp)
if data[vi] == c_lava then
for pi in area:iter(math.max(x-1, minp.x), math.max(y-1, minp.y), math.max(z-1, minp.z),
math.min(x+1, maxp.x), math.min(y+1, maxp.y), math.min(z+1, maxp.z)) do
if data[pi] == c_stone then
data[pi] = c_obsidian
end
end
end
end
@ -85,33 +106,20 @@ local data = {}
minetest.register_on_generated(function(minp, maxp, seed)
--if too far from water level, abort. Caverns are on their own.
if minp.y > 512 or maxp.y < water_level then
return
end
--easy reference to commonly used values
local t_start = os.clock()
local x_max = maxp.x
local y_max = maxp.y
local z_max = maxp.z
local x_min = minp.x
local y_min = minp.y
local z_min = minp.z
-- if minp.y > 512 or maxp.y < water_level then
-- return
-- end
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
vm:get_data(data)
for z = z_min, z_max do -- for each xy plane progressing northwards
--structure loop, hollows out the cavern
for y = y_min, y_max do -- for each x row progressing upwards
if y > water_level then
local vi = area:index(x_min, y, z) --current node index
for x = x_min, x_max do -- for each node do
magma_conduits.remove_unsupported_lava(area, data, vi)
vi = vi + 1
end
for vi, x, y, z in area:iterp_xyz(minp, maxp) do
if ameliorate_floods then
remove_unsupported_lava(area, data, vi, x, y, z)
end
if obsidian_lining then
obsidianize(area, data, vi, x, y, z, minp, maxp)
end
end

@ -2,4 +2,5 @@ magma_conduits_spread (Approximate spacing between magma conduits) int 400
magma_conduits_upper_limit (Upper extent of magma conduits) int 512
magma_conduits_lower_limit (Lower extent of magma conduits) int -31000
magma_conduits_ameliorate_floods (Ameliorate lava floods on the surface) bool true
magma_conduits_obsidian_lining (Adds an obsidian lining to magma conduits) bool true
magma_conduits_remove_default_lava (Remove default mapgen lava) bool true

75
voxelarea_iterator.lua Normal file

@ -0,0 +1,75 @@
-- These functions are a minor modification of the voxel area iterator defined in the built in voxelarea.lua lua code.
-- As such, this file is separately licened under the LGPL as follows:
-- License of Minetest source code
-------------------------------
--Minetest
--Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU Lesser General Public License for more details.
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
function VoxelArea:iter_xyz(minx, miny, minz, maxx, maxy, maxz)
local i = self:index(minx, miny, minz) - 1
local x = minx - 1 -- subtracting one because x gets incremented before it gets returned the first time.
local xrange = maxx - minx + 1
local nextaction = i + 1 + xrange
local y = 0
local yrange = maxy - miny + 1
local yreqstride = self.ystride - xrange
local z = 0
local zrange = maxz - minz + 1
local multistride = self.zstride - ((yrange - 1) * self.ystride + xrange)
return function()
-- continue i until it needs to jump
i = i + 1
x = x + 1
if i ~= nextaction then
return i, x, y+miny, z+minz
end
-- continue y until maxy is exceeded
y = y + 1
x = minx -- new line
if y ~= yrange then
-- set i to index(minx, miny + y, minz + z) - 1
i = i + yreqstride
nextaction = i + xrange
return i, x, y+miny, z+minz
end
-- continue z until maxz is exceeded
z = z + 1
if z == zrange then
-- cuboid finished, return nil
return
end
-- set i to index(minx, miny, minz + z) - 1
i = i + multistride
y = 0
nextaction = i + xrange
return i, x, y+miny, z+minz
end
end
function VoxelArea:iterp_xyz(minp, maxp)
return self:iter_xyz(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z)
end