add biomegen for lua mapgens

This commit is contained in:
cora 2022-03-25 23:59:40 +01:00
parent bc34d97699
commit 4bd2f3aa93
11 changed files with 1166 additions and 12 deletions

165
biomegen/LICENSE Normal file
View File

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

114
biomegen/README.md Normal file
View File

@ -0,0 +1,114 @@
# Biomegen
Biome generator mod for Minetest, reproducing closely the biome generator provided by Minetest's core, but in Lua. Also includes an optional elevation adjustment parameter.
It allows to use the biome systems on Lua mapgens (that do no allow to use core biome system). Since it reads registered biomes and decorations, it is compatible with all mods adding biomes/decos.
Created by Gaël de Sailly in November 2020, licensed under LGPLv3.0.
# Include it in your mapgen
`biomegen` should be triggered during mapgen function, after the loop, but before writing to the map.
Your mapgen should generate only these 4 nodes:
- Stone (`mapgen_stone` / `default:stone`)
- Water (`mapgen_water_source` / `default:water_source`)
- River water (`mapgen_river_water_source` / `default:river_water_source`)
- Air (`air`)
All other nodes will be ignored, no biome will be placed ontop of them.
You should add `biomegen` as a dependancy of your mod (optional or mandatory).
## Functions
Usual parameters:
- `data`: Data containing the generated mapchunk
- `area`: VoxelArea helper object for data. `area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})`
- `vm`: VoxelManip object
- `minp`: minimal coordinates of the chunk being generated, e.g. `{x=48, y=-32, z=208}`
- `maxp`: maximal coordinates of the chunk being generated, e.g. `{x=127, y=47, z=287}`
- `seed`: world-specific seed
### `biomegen.generate_all(data, area, vm, minp, maxp, seed)`
All-in-one function to generate *biomes*, *decorations*, *ores* and *dust*. Includes a call to `vm:set_data` so no need to do it again. Using core function `minetest.generate_ores` for ores, so does not support biome-specific ores.
### `biomegen.generate_biomes(data, area, minp, maxp)`
Generates biomes in `data`, according to biomes that have been registered using `minetest.register_biome`.
### `biomegen.place_all_decos(data, area, vm, minp, maxp, seed)`
Generates decorations directly in `vm` (but reads `data` to know where to place them), according to decorations that have been registered using `minetest.register_decoration`.
### `biomegen.dust_top_nodes(data, area, vm, minp, maxp)`
Drops 'dust' (usually snow) on biomes that require it. Like above, generates directly in `vm` but reads from `data`. If you used `place_all_decos` to generate decorations, you should update `data` from the `vm`:
```lua
vm:get_data(data)
```
### `biomegen.set_elevation_chill(ec)`
Sets elevation chill coefficient. `0` means temperature does not depend on elevation (behaviour of core's biomegen). Usual values `0`-`0.5`.
## Examples
### Using `biomegen.generate_all`
```lua
local data = {}
minetest.register_on_generated(function(minp, maxp, seed)
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
vm:get_data(data)
------------------------
-- [MAPGEN LOOP HERE] --
------------------------
-- Generate biomes, decorations, ores and dust
biomegen.generate_all(data, area, vm, minp, maxp, seed)
-- Calculate lighting for what has been created.
vm:calc_lighting()
-- Write what has been created to the world.
vm:write_to_map()
-- Liquid nodes were placed so set them flowing.
vm:update_liquids()
end)
```
### Equivalent with all functions
```lua
local data = {}
minetest.register_on_generated(function(minp, maxp, seed)
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
vm:get_data(data)
------------------------
-- [MAPGEN LOOP HERE] --
------------------------
-- Generate biomes in 'data', using biomegen mod
biomegen.generate_biomes(data, area, minp, maxp)
-- Write content ID data back to the voxelmanip.
vm:set_data(data)
-- Generate ores using core's function
minetest.generate_ores(vm, minp, maxp)
-- Generate decorations in VM (needs 'data' for reading)
biomegen.place_all_decos(data, area, vm, minp, maxp, seed)
-- Update data array to have ores/decorations
vm:get_data(data)
-- Add biome dust in VM (needs 'data' for reading)
biomegen.dust_top_nodes(data, area, vm, minp, maxp)
-- Calculate lighting for what has been created.
vm:calc_lighting()
-- Write what has been created to the world.
vm:write_to_map()
-- Liquid nodes were placed so set them flowing.
vm:update_liquids()
end)
```
### Mapgen example
I have made a [modified version of `lvm_example`](https://github.com/Gael-de-Sailly/lvm_example/tree/biomegen) (mod originally by Paramat) to provide a minimal working example of a mapgen using `biomegen`. Try it!

71
biomegen/biomelist.lua Normal file
View File

@ -0,0 +1,71 @@
-- biomelist.lua
local core_cid = minetest.get_content_id
local function cid(name)
if not name then
return
end
local result
pcall(function() --< try
result = core_cid(name)
end)
if not result then
print("[biomegen] Node " .. name .. " not found!")
end
return result
end
local function make_biomelist()
local biomes = {}
for _, a in pairs(minetest.registered_biomes) do
local b = {}
b.name = a.name
biomes[b.name] = b
if a.node_dust then
b.node_dust_name = a.node_dust
b.node_dust = cid(a.node_dust)
end
b.node_top = cid(a.node_top) or cid("mapgen_stone")
b.depth_top = a.depth_top or 0
b.node_filler = cid(a.node_filler) or cid("mapgen_stone")
b.depth_filler = a.depth_filler or 0
b.node_stone = cid(a.node_stone) or cid("mapgen_stone")
b.node_water_top = cid(a.node_water_top) or cid("mapgen_water_source")
b.depth_water_top = a.depth_water_top or 0
b.node_water = cid(a.node_water) or cid("mapgen_water_source")
b.node_river_water = cid(a.node_river_water) or cid("mapgen_river_water_source")
b.node_riverbed = cid(a.node_riverbed) or cid("mapgen_stone")
b.depth_riverbed = a.depth_riverbed or 0
-- b.node_cave_liquid = ...
-- b.node_dungeon = ...
-- b.node_dungeon_alt = ...
-- b.node_dungeon_stair = ...
b.min_pos = a.min_pos or {x=-31000, y=-31000, z=-31000}
if a.y_min then
b.min_pos.y = math.max(b.min_pos.y, a.y_min)
end
b.max_pos = a.max_pos or {x=31000, y=31000, z=31000}
if a.y_max then
b.max_pos.y = math.min(b.max_pos.y, a.y_max)
end
b.vertical_blend = a.vertical_blend or 0
b.heat_point = a.heat_point or 50
b.humidity_point = a.humidity_point or 50
end
return biomes
end
return make_biomelist

234
biomegen/decorations.lua Normal file
View File

@ -0,0 +1,234 @@
-- decorations.lua
local emptynodes = {
air = true,
ignore = true,
}
local core_cid = minetest.get_content_id
local function cid(name)
if not name then
return
end
local result
pcall(function() --< try
result = core_cid(name)
end)
if not result then
print("[biomegen] Node " .. name .. " not found!")
end
return result
end
local function generate_deco_simple(deco, vm, pr, p, ceiling)
local emin, emax = vm:get_emerged_area()
local place_offset_y = deco.place_offset_y
if ceiling then
if p.y - place_offset_y - deco.height_max < emin.y then
return 0
elseif p.y - 1 - place_offset_y > emax.y then
return 0
end
else
if p.y + place_offset_y + deco.height_max > emax.y then
return 0
elseif p.y + 1 + place_offset_y < emin.y then
return 0
end
end
local decos = deco.decoration
if #decos == 0 then
return 0
end
local nodename = decos[pr:next(1,#decos)]
local height = deco.vary_height and pr:next(deco.height, deco.height_max) or deco.height
local param2 = deco.vary_param2 and pr:next(deco.param2, deco.param2_max) or deco.param2
local force_placement = deco.flags.force_placement == true
local direction = ceiling and -1 or 1
p.y = p.y + place_offset_y * direction
for i=1, height do
p.y = p.y + direction
local node = vm:get_node_at(p)
if not force_placement and not emptynodes[node.name] then
break
end
node.name = nodename
node.param2 = param2
vm:set_node_at(p, node)
end
return 1
end
local function get_schematic_size(schem)
if type(schem) == "table" then
return schem.size
elseif type(schem) == "string" then
local mts = io.open(schem)
if not mts then
return {x=0, y=0, z=0}
end
mts:seek('set', 6)
local sx1, sx2, sy1, sy2, sz1, sz2 = mts:read(6):byte()
mts:close()
return {x=sx1*256+sx2, y=sy1*256+sy2, z=sz1*256+sz2}
end
return {x=0, y=0, z=0}
end
local function generate_deco_schematic(deco, vm, pr, p, ceiling)
local force_placement = deco.flags.force_placement == true
local direction = ceiling and -1 or 1
if not deco.flags.place_center_y then
if ceiling then
local size = get_schematic_size(schem)
p.y = p.y - deco.place_offset_y - size.y + 1
else
p.y = p.y + deco.place_offset_y
end
end
minetest.place_schematic_on_vmanip(vm, p, deco.schematic, deco.rotation, deco.replacements, force_placement, deco.schem_flags)
return 1
end
local function parse_node_list(raw_list)
if not raw_list then
return {}
end
local ilist = {}
if type(raw_list) == "string" then
raw_list = {raw_list}
end
for i, node in ipairs(raw_list) do
if node:sub(1, 6) == "group:" then
local groupname = node:sub(7, -1)
for name, ndef in pairs(minetest.registered_nodes) do
if ndef.groups and ndef.groups[groupname] and ndef.groups[groupname] > 0 then
local id = cid(name)
if id then
ilist[id] = true
end
end
end
else
local id = cid(node)
if id then
ilist[id] = true
end
end
end
return ilist
end
local function make_decolist()
local decos = {}
for i, a in pairs(minetest.registered_decorations) do
local b = {}
decos[i] = b
b.name = a.name or "unnamed " .. i
b.deco_type = a.deco_type or "simple"
b.place_on = parse_node_list(a.place_on)
b.sidelen = a.sidelen or 8
b.fill_ratio = a.fill_ratio or 0.02
local np = a.noise_params
b.use_noise = false
if np then
b.use_noise = true
b.noise = minetest.get_perlin(np)
end
b.use_biomes = false
if a.biomes then
local biomes_raw = a.biomes
b.use_biomes = true
if type(biomes_raw) == "table" then
local biomes = {}
b.biomes = biomes
for i, biome in pairs(biomes_raw) do
if type(biome) == "number" then
biome = minetest.get_biome_name(biome)
end
biomes[biome] = true
end
else
if type(biomes_raw) == "number" then
biomes_raw = minetest.get_biome_name(biomes_raw)
end
b.biomes = {[biomes_raw] = true}
end
end
b.y_min = a.y_min or -31000
b.y_max = a.y_max or 31000
b.spawn_by = parse_node_list(a.spawn_by)
b.num_spawn_by = a.num_spawn_by or 0
local flags_raw = a.flags or ""
local flags = {}
b.flags = flags
for i, flag in ipairs(flags_raw:split()) do
flag = flag:trim()
local status = true
if flag:sub(1,2) == "no" then
flag = flag:sub(3,-1)
status = false
end
flags[flag] = status
end
if b.deco_type == "simple" then
local a_deco = a.decoration
if type(a_deco) == "string" then
a_deco = {a_deco}
end
local b_deco = {}
for _, deco in ipairs(a_deco) do
if cid(deco) then
table.insert(b_deco, deco)
end
end
b.decoration = b_deco
b.height = a.height or 1
b.height_max = math.max(a.height_max or b.height, b.height)
b.vary_height = b.height < b.height_max
b.param2 = a.param2 or 0
b.param2_max = math.max(a.params2_max or b.param2, b.height)
b.vary_param2 = b.param2 < b.param2_max
b.place_offset_y = a.place_offset_y or 0
b.generate = generate_deco_simple
elseif b.deco_type == "schematic" then
b.schematic = a.schematic
b.replacements = a.replacements or {}
b.rotation = a.rotation or 0
b.place_offset_y = a.place_offset_y or 0
local schem_flags = {}
for _, flag in ipairs({'place_center_x', 'place_center_y', 'place_center_z'}) do
if flags[flag] then
table.insert(schem_flags, flag)
end
end
b.schem_flags = table.concat(schem_flags, ',')
b.generate = generate_deco_schematic
end
end
return decos
end
return make_decolist

569
biomegen/init.lua Normal file
View File

@ -0,0 +1,569 @@
-- biomegen/init.lua
local make_biomelist = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/biomelist.lua")
local make_decolist = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/decorations.lua")
local np_filler_depth = {
offset = 0,
scale = 1.2,
spread = {x=150, y=150, z=150},
seed = 261,
octaves = 3,
persist = 0.7,
lacunarity = 2.0,
}
local nobj_filler_depth, nobj_heat, nobj_heat_blend, nobj_humid, nobj_humid_blend
local nvals_filler_depth = {}
local nvals_heat = {}
local nvals_heat_blend = {}
local nvals_humid = {}
local nvals_humid_blend = {}
local water_level = tonumber(minetest.get_mapgen_setting('water_level'))
local elevation_chill = 0
local function set_elevation_chill(ec)
elevation_chill = ec
end
local init = false
local c_ignore
local c_air
local c_stone
local c_water
local c_rwater
local biomes, decos
local function initialize(chulens)
print("[biomegen] Initializing")
init = true
local noiseparams = minetest.get_mapgen_setting_noiseparams
local chulens2d = {x=chulens.x, y=chulens.z, z=1}
local np_heat = noiseparams('mg_biome_np_heat')
water_level = multi_map.get_absolute_centerpoint()
np_heat.offset = np_heat.offset + water_level*elevation_chill
nobj_filler_depth = minetest.get_perlin_map(np_filler_depth, chulens2d)
nobj_heat = minetest.get_perlin_map(np_heat, chulens2d)
nobj_heat_blend = minetest.get_perlin_map(noiseparams('mg_biome_np_heat_blend'), chulens2d)
nobj_humid = minetest.get_perlin_map(noiseparams('mg_biome_np_humidity'), chulens2d)
nobj_humid_blend = minetest.get_perlin_map(noiseparams('mg_biome_np_humidity_blend'), chulens2d)
c_ignore = minetest.get_content_id("ignore")
c_air = minetest.get_content_id("air")
c_stone = minetest.get_content_id("mapgen_stone")
c_water = minetest.get_content_id("mapgen_water_source")
c_rwater = minetest.get_content_id("mapgen_river_water_source")
biomes = make_biomelist()
decos = make_decolist()
end
local biomemap = {}
local heatmap = {}
local humidmap = {}
local function calculate_noises(minp)
local minp2d = {x=minp.x, y=minp.z}
nobj_filler_depth:get_2d_map_flat(minp2d, nvals_filler_depth)
nobj_heat:get_2d_map_flat(minp2d, nvals_heat)
nobj_heat_blend:get_2d_map_flat(minp2d, nvals_heat_blend)
nobj_humid:get_2d_map_flat(minp2d, nvals_humid)
nobj_humid_blend:get_2d_map_flat(minp2d, nvals_humid_blend)
for i, heat in ipairs(nvals_heat) do -- use nvals_heat to iterate, could have been another one
heatmap[i] = heat + nvals_heat_blend[i]
humidmap[i] = nvals_humid[i] + nvals_humid_blend[i]
end
end
local function calc_biome_from_noise(heat, humid, pos)
local biome_closest = nil
local biome_closest_blend = nil
local dist_min = 31000
local dist_min_blend = 31000
for i, biome in pairs(biomes) do
local min_pos, max_pos = biome.min_pos, biome.max_pos
if pos.y >= min_pos.y and pos.y <= max_pos.y+biome.vertical_blend
and pos.x >= min_pos.x and pos.x <= max_pos.x
and pos.z >= min_pos.z and pos.z <= max_pos.z then
local d_heat = heat - biome.heat_point
local d_humid = humid - biome.humidity_point
local dist = d_heat*d_heat + d_humid*d_humid -- Pythagorean distance
if pos.y <= max_pos.y then -- Within y limits of biome
if dist < dist_min then
dist_min = dist
biome_closest = biome
end
elseif dist < dist_min_blend then -- Blend area above biome
dist_min_blend = dist
biome_closest_blend = biome
end
end
end
-- Carefully tune pseudorandom seed variation to avoid single node dither
-- and create larger scale blending patterns similar to horizontal biome
-- blend.
local seed = math.floor(pos.y + (heat+humid) * 0.9)
local rng = PseudoRandom(seed)
if biome_closest_blend and dist_min_blend <= dist_min
and rng:next(0, biome_closest_blend.vertical_blend) >= pos.y - biome_closest_blend.max_pos.y then
return biome_closest_blend
end
return biome_closest
end
local function get_biome_at_index(i, pos)
water_level = multi_map.get_absolute_centerpoint()
local heat = heatmap[i] - math.max(pos.y, water_level)*elevation_chill
local humid = humidmap[i]
return calc_biome_from_noise(heat, humid, pos)
end
local function generate_biomes(data, a, minp, maxp)
water_level = multi_map.get_absolute_centerpoint(multi_map.get_layer(minp.y))
local chulens = {x=maxp.x-minp.x+1, y=maxp.y-minp.y+1, z=maxp.z-minp.z+1}
local index = 1
if not init then
initialize(chulens)
end
calculate_noises(minp)
for z=minp.z, maxp.z do
for x=minp.x, maxp.x do
local biome = nil
local water_biome = nil
local biome_stone = c_stone
local depth_top = 0
local base_filler = 0
local depth_water_top = 0
local depth_riverbed = 0
local biome_y_min = -31000
local y_start = maxp.y
local vi = a:index(x, maxp.y, z)
local ystride = a.ystride
local c_above = data[vi+ystride]
if c_above == c_ignore then
y_start = y_start - 1
c_above = data[vi]
vi = vi - ystride
end
local air_above = c_above == c_air
local river_water_above = c_above == c_rwater
local water_above = c_above == c_water or river_water_above
biomemap[index] = nil
local nplaced = (air_above or water_above) and 0 or 31000
for y=y_start, minp.y-1, -1 do
local c = data[vi]
local is_stone_surface = (c == c_stone) and
(air_above or water_above or not biome or y < biome_y_min)
local is_water_surface = (c == c_water or c == c_rwater) and
(air_above or not biome or y < biome_y_min)
if is_stone_surface or is_water_surface then
biome = get_biome_at_index(index, {x=x, y=y, z=z})
biome_stone = biome.node_stone
if not biomemap[index] and is_stone_surface then
biomemap[index] = biome
end
if not water_biome and is_water_surface then
water_biome = biome
end
depth_top = biome.depth_top
base_filler = math.max(depth_top + biome.depth_filler + nvals_filler_depth[index], 0)
depth_water_top = biome.depth_water_top
depth_riverbed = biome.depth_riverbed
biome_y_min = biome.min_pos.y
end
if c == c_stone or c == biome_stone then
local c_below = data[vi-ystride]
if c_below == c_air or c_below == c_rwater or c_below == c_water then
nplaced = 31000
end
if river_water_above then
if nplaced < depth_riverbed then
data[vi] = biome.node_riverbed
nplaced = nplaced + 1
else
nplaced = 31000
river_water_above = false
end
elseif nplaced < depth_top then
data[vi] = biome.node_top
nplaced = nplaced + 1
elseif nplaced < base_filler then
data[vi] = biome.node_filler
nplaced = nplaced + 1
else
data[vi] = biome_stone
nplaced = 31000
end
air_above = false
water_above = false
elseif c == c_water then
if y > water_level-depth_water_top then
data[vi] = biome.node_water_top
else
data[vi] = biome.node_water
end
nplaced = 0
air_above = false
water_above = true
elseif c == c_rwater then
data[vi] = biome.node_river_water
nplaced = 0
air_above = false
water_above = true
river_water_above = true
elseif c == c_air then
nplaced = 0
air_above = true
water_above = false
else
nplaced = 31000
air_above = false
water_above = false
end
vi = vi - ystride
end
if not biomemap[index] then
biomemap[index] = water_biome
end
index = index + 1
end
end
end
-- Walkable, liquid, and dustable: memoization tables for better performance
local walkable = setmetatable({}, {
__index = function(t, c)
local is_walkable = false
local ndef = minetest.registered_nodes[minetest.get_name_from_content_id(c)]
if ndef and ndef.walkable then
is_walkable = true
end
t[c] = is_walkable
return is_walkable
end,
})
local liquid = setmetatable({}, {
__index = function(t, c)
local is_liquid = false
local ndef = minetest.registered_nodes[minetest.get_name_from_content_id(c)]
if ndef and ndef.liquidtype then
is_liquid = ndef.liquidtype ~= "none"
end
t[c] = is_liquid
return is_liquid
end,
})
local function can_place_deco(deco, data, vi, pattern)
if not deco.place_on[data[vi]] then
return false
elseif deco.num_spawn_by <= 0 then
return true
end
local spawn_by = deco.spawn_by
local nneighs = deco.num_spawn_by
for i, incr in ipairs(pattern) do
vi = vi + incr
if spawn_by[data[vi]] then
nneighs = nneighs - 1
if nneighs < 1 then
return true
end
end
end
return false
end
local function place_deco(deco, data, a, vm, minp, maxp, blockseed)
local ps = PcgRandom(blockseed + 53)
local carea_size = maxp.x - minp.x + 1
local sidelen = deco.sidelen
if carea_size % sidelen > 0 then
sidelen = carea_size
end
local divlen = carea_size / sidelen - 1
local area = sidelen*sidelen
local ystride, zstride = a.ystride, a.zstride
local pattern = {1, zstride, -1, -1, -zstride, -zstride, 1, 1, ystride, zstride, zstride, -1, -1, -zstride, -zstride, 1} -- Successive increments to iterate over 16 neighbouring nodes
for z0=0, divlen do
for x0=0, divlen do
local p2d_center = {x=minp.x+sidelen*(x0+0.5), y=minp.z+sidelen*(z0+0.5)}
local p2d_min = {x=minp.x+sidelen*x0, y=minp.z+sidelen*z0}
local p2d_max = {x=minp.x+sidelen*(x0+1)-1, y=minp.z+sidelen*(z0+1)-1}
local cover = false
local nval = deco.use_noise and deco.noise:get_2d(p2d_center) or deco.fill_ratio
local deco_count = 0
if nval >= 10 then
cover = true
deco_count = area
else
local deco_count_f = area * nval
if deco_count_f >= 1 then
deco_count = deco_count_f
elseif deco_count_f > 0 and ps:next(1, 1000) <= deco_count_f * 1000 then
deco_count = 1
end
end
local x = p2d_min.x - 1
local z = p2d_min.y
for i=1, deco_count do
if not cover then
x = ps:next(p2d_min.x, p2d_max.x)
z = ps:next(p2d_min.y, p2d_max.y)
else
x = x + 1
if x == p2d_max.x + 1 then
z = z + 1
x = p2d_min.x
end
end
local mapindex = carea_size * (z - minp.z) + (x - minp.x)
if deco.flags["all_floors"] == true and deco.flags["all_ceilings"] == true then
local biome_ok = true
if deco.use_biomes and #biomemap > 0 then
local biome_here = biomemap[mapindex]
if biome_here and not deco.biomes[biome_here.name] then
biome_ok = false
end
end
if biome_ok then
local size = (maxp.x - minp.x + 1) / 2
local floors = {}
local ceilings = {}
local is_walkable = false
local vi = a:index(x, maxp.y, z)
local walkable_above = walkable[data[vi]]
for y = maxp.y-1, minp.y, -1 do
vi = vi - ystride
is_walkable = walkable[data[vi]]
if is_walkable and not walkable_above then
table.insert(floors, y)
elseif walkable_above and not walkable then
table.insert(ceilings, y)
end
walkable_above = is_walkable
end
if deco.flags["all_floors"] then
for _, y in ipairs(floors) do
if y >= biome.y_min and y <= biome.y_max then
local pos = {x=x, y=y, z=z}
if can_place_deco(deco, data, vi, pattern) then
deco:generate(vm, ps, pos, false)
end
end
end
end
if deco.flags["all_ceilings"] then
for _, y in ipairs(ceilings) do
if y >= biome.y_min and y <= biome.y_max then
local pos = {x=x, y=y, z=z}
if can_place_deco(deco, data, vi, pattern) then
deco:generate(vm, ps, pos, true)
end
end
end
end
end
else
local y = -31000
if deco.flags["liquid_surface"] == true then
local vi = a:index(x, maxp.y, z)
for yi=maxp.y, minp.y, -1 do
local c = data[vi]
if walkable[c] then
break
elseif liquid[c] then
y = yi
break
end
vi = vi - ystride
end
else
local vi = a:index(x, maxp.y, z)
for yi=maxp.y, minp.y, -1 do
if walkable[data[vi]] then
y = yi
break
end
vi = vi - ystride
end
end
if y >= deco.y_min and y <= deco.y_max and y >= minp.y and y <= maxp.y then
local biome_ok = true
if deco.use_biomes and #biomemap > 0 then
local biome_here = biomemap[mapindex]
if biome_here and not deco.biomes[biome_here.name] then
biome_ok = false
end
end
if biome_ok then
local pos = {x=x, y=y, z=z}
if can_place_deco(deco, data, a:index(x,y,z), pattern) then
deco:generate(vm, ps, pos, false)
end
end
end
end
end
end
end
return 0
end
local function get_blockseed(p, seed)
return seed + p.z * 38134234 + p.y * 42123 + p.x * 23
end
local function place_all_decos(data, a, vm, minp, maxp, seed)
local emin = vm:get_emerged_area()
local blockseed = get_blockseed(emin, seed)
local nplaced = 0
for i, deco in pairs(decos) do
nplaced = nplaced + place_deco(deco, data, a, vm, minp, maxp, blockseed)
end
return nplaced
end
local dustable = setmetatable({}, {
__index = function(t, c)
local is_dustable = false
local ndef = minetest.registered_nodes[minetest.get_name_from_content_id(c)]
if ndef and ndef.walkable then
local dtype = ndef.drawtype
if dtype and dtype == "normal" or dtype == "allfaces" or dtype == "allfaces_optional" or dtype == "glasslike" or dtype == "glasslike_framed" or dtype == "glasslike_framed_optional" then
is_dustable = true
end
end
t[c] = is_dustable
return is_dustable
end,
})
local function dust_top_nodes(data, a, vm, minp, maxp)
water_level = multi_map.get_absolute_centerpoint()
if maxp.y < water_level then
return
end
local full_maxp = a.MaxEdge
local index = 1
local ystride = a.ystride
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
local biome = biomemap[index]
if biome and biome.node_dust then
local vi = a:index(x, full_maxp.y, z)
local c_full_max = data[vi]
local y_start
if c_full_max == c_air then
y_start = full_maxp.y - 1
elseif c_full_max == c_ignore then
vi = a:index(x, maxp.y, z)
local c_max = data[vi]
if c_max == c_air then
y_start = maxp.y
end
end
if y_start then -- workaround for the 'continue' statement
vi = a:index(x, y_start, z)
local y = y_start
for y0=y_start, minp.y-1, -1 do
if data[vi] ~= c_air then
y = y0
break
end
vi = vi - ystride
end
local c = data[vi]
if dustable[c] and c ~= biome.node_dust then
local pos = {x=x, y=y+1, z=z}
vm:set_node_at(pos, {name=biome.node_dust_name})
end
end
end
index = index + 1
end
end
end
biomegen = {
set_elevation_chill = set_elevation_chill,
calculate_noises = calculate_noises,
get_biome_at_index = get_biome_at_index,
calc_biome_from_noise = calc_biome_from_noise,
generate_biomes = generate_biomes,
place_all_decos = place_all_decos,
dust_top_nodes = dust_top_nodes,
}
function biomegen.generate_all(data, a, vm, minp, maxp, seed)
water_level = multi_map.get_absolute_centerpoint()
generate_biomes(data, a, minp, maxp)
vm:set_data(data)
place_all_decos(data, a, vm, minp, maxp, seed)
minetest.generate_ores(vm, minp, maxp)
vm:get_data(data)
dust_top_nodes(data, a, vm, minp, maxp)
end

3
biomegen/mod.conf Normal file
View File

@ -0,0 +1,3 @@
name = biomegen
author = Gael-de-Sailly
title = Lua Biome Generator

View File

@ -394,6 +394,9 @@ minetest.register_on_generated(function(minp, maxp,seed)
else
for i,f in ipairs(t) do
f.generator(multi_map.current_layer, vm, area, vm_data, minp, maxp, offset_minp, offset_maxp, f.arguments,seed)
if biomegen and ( not f.arguments or ( f.arguments and not f.arguments.nobiomoes ) ) then
biomegen.generate_all(vm_data, area, vm, minp, maxp, seed)
end
end
end

View File

@ -1,3 +1,3 @@
name = multi_map_core
description = multi_map core engine and API for mineclone
depends = mcl_core
depends = mcl_core, biomegen

View File

@ -15,7 +15,7 @@ multi_map.register_fallback_generator("Default Simple", mmgen_simple.generate)
multi_map.register_generator(0, mmgen_testauri.generate )
multi_map.register_generator(1, mmgen_mytest.generate, {stone="mcl_core:stone",dirt="mcl_core:dirt",grass="mcl_core:dirt_with_grass",water="mcl_core:water_source",air="air"})
multi_map.register_generator(2, mmgen_simple.generate, {nodetype="mcl_core:dirt"})
multi_map.register_generator(2, mmgen_simple.generate, {nodetype="mcl_core:stone"})
multi_map.register_generator(3, mmgen_simple.generate, {nodetype="mcl_core:obsidian"})
multi_map.register_generator(4, mmgen_levels.generate)

View File

@ -33,7 +33,7 @@ local floatper = math.pi / FLOATPER
-- Generate function
function mmgen_levels.generate(current_layer, vm, area, data, minp, maxp, offset_minp, offset_maxp, params)
function mmgen_levels.generate(current_layer, vm, area, data, minp, maxp, offset_minp, offset_maxp, param,seed)
if not params then params = {} end
@ -136,7 +136,7 @@ function mmgen_levels.generate(current_layer, vm, area, data, minp, maxp, offset
data[vi] = c_sand
under[si] = 0
else
data[vi] = c_dirt
--data[vi] = c_dirt
under[si] = 1
end
elseif relative_y > (params.ysurfmin or YSURFMIN) and relative_y <= (params.ywater or YWATER) then
@ -149,7 +149,7 @@ function mmgen_levels.generate(current_layer, vm, area, data, minp, maxp, offset
under[si] = 0
else -- air, possibly just above surface
if under[si] == 1 then
data[viu] = c_grass
--data[viu] = c_grass
end
stable[si] = 0
under[si] = 0
@ -157,7 +157,7 @@ function mmgen_levels.generate(current_layer, vm, area, data, minp, maxp, offset
elseif y == y1 + 1 then
if density <= 0 and relative_y > (params.ywater or YWATER) then -- air, possibly just above surface
if under[si] == 1 then
data[viu] = c_grass
--data[viu] = c_grass
end
end
end

View File

@ -1,9 +1,5 @@
mmgen_mytest = {}
--multi_map.register_global_3dmap(
-- "terrain",
-- {offset=0, scale=1, spread={x=200, y=125, z=200}, seed=1235, octaves=5, persist=0.6}
--)
function mmgen_mytest.generate(current_layer, vm, a, data, minp, maxp, offset_minp, offset_maxp,arg,seed)
local t1 = os.clock()
@ -43,5 +39,4 @@ function mmgen_mytest.generate(current_layer, vm, a, data, minp, maxp, offset_mi
end
end
end
biomegen.generate_all(data, a, vm, minp, maxp, seed)
end