From b3467b5093f3f15f655737556a94acbabe90e4b3 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Thu, 6 Jan 2022 21:17:37 +0100 Subject: [PATCH] singleblock mapgen / some indent fixes --- examples/deserialize_options.lua | 10 +++++----- examples/singleblock_mapgen.lua | 11 +++++++++++ init.lua | 5 ++++- mapgens/singleblock.lua | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 examples/singleblock_mapgen.lua create mode 100644 mapgens/singleblock.lua diff --git a/examples/deserialize_options.lua b/examples/deserialize_options.lua index 70e531e..b1b1d11 100644 --- a/examples/deserialize_options.lua +++ b/examples/deserialize_options.lua @@ -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 diff --git a/examples/singleblock_mapgen.lua b/examples/singleblock_mapgen.lua new file mode 100644 index 0000000..8fa76af --- /dev/null +++ b/examples/singleblock_mapgen.lua @@ -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) \ No newline at end of file diff --git a/init.lua b/init.lua index 3946863..af40496 100644 --- a/init.lua +++ b/init.lua @@ -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") \ No newline at end of file diff --git a/mapgens/singleblock.lua b/mapgens/singleblock.lua new file mode 100644 index 0000000..02e8bb7 --- /dev/null +++ b/mapgens/singleblock.lua @@ -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 \ No newline at end of file