singleblock mapgen / some indent fixes

This commit is contained in:
BuckarooBanzay 2022-01-06 21:17:37 +01:00
parent 6392bf0993
commit b3467b5093
4 changed files with 52 additions and 6 deletions

View File

@ -17,12 +17,12 @@ local options = {
}
},
-- replace certain nodes with others
-- replace certain nodes with others
replace = {
["default:dirt"] = "default:mese"
},
-- bulk set param2 for certain nodes, useful for mass-coloring
-- bulk set param2 for certain nodes, useful for mass-coloring
set_param2 = {
["unifiedbricks:brickblock"] = 15
}
@ -39,7 +39,7 @@ local options = {
end
end,
-- placement mode "replace": replace the whole mapblock, "add": replace only air nodes
-- placement mode "replace": replace the whole mapblock, "add": replace only air nodes
mode = "replace"
}
@ -53,6 +53,6 @@ local filename = minetest.get_modpath("my_mod") .. "/schematics/my_mapblock"
local success, msg = mapblock_lib.deserialize(mapblock_pos, filename, options)
if not success then
-- not successful, abort with error
error(msg)
-- not successful, abort with error
error(msg)
end

View File

@ -0,0 +1,11 @@
-- singleblock mapgen example, places a single mapblock _everywhere_
local MP = minetest.get_modpath("my_mod")
-- create a mapgen function for a single mapblock
local fn = mapblock_lib.mapgens.singleblock({
filename = MP .. "/schemas/mymapblock"
})
-- register it
minetest.register_on_generated(fn)

View File

@ -1,5 +1,6 @@
mapblock_lib = {
schema_path = minetest.get_worldpath() .. "/mapblocks"
schema_path = minetest.get_worldpath() .. "/mapblocks",
mapgens = {}
}
-- create global schema_path
@ -28,3 +29,5 @@ dofile(MP .. "/validate.lua")
dofile(MP .. "/display.lua")
dofile(MP .. "/chatcommands/single.lua")
dofile(MP .. "/chatcommands/multi.lua")
dofile(MP .. "/mapgens/singleblock.lua")

32
mapgens/singleblock.lua Normal file
View File

@ -0,0 +1,32 @@
---------
-- singleblock mapgen helper
--- creates a new mapgen-function that generates a single mapblock,
--- the resulting function can be passed to `minetest.register_on_generated`
-- @see singleblock_mapgen.lua
-- @param cfg configuration table
-- @param cfg.filename the filename of the mapblock to use
-- @param cfg.filter an optional filter function of the type `fn(blockpos)`, returning true means the mapblock is placed
-- @param cfg.options optional table for `mapblock_lib.deserialize`
function mapblock_lib.mapgens.singleblock(cfg)
cfg.filter = cfg.filter or function() return true end
cfg.options = cfg.options or { use_cache = true }
assert(cfg.filename, "missing schema filename")
return function(minp, maxp)
local min_block = mapblock_lib.get_mapblock(minp)
local max_block = mapblock_lib.get_mapblock(maxp)
for x=min_block.x, max_block.x do
for y=min_block.y, max_block.y do
for z=min_block.z, max_block.z do
local mapblock_pos = {x=x, y=y, z=z}
local do_place = cfg.filter(mapblock_pos)
if do_place then
mapblock_lib.deserialize(mapblock_pos, cfg.filename, cfg.options)
end
end
end
end
end
end