Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active February 18, 2024 18:26
Show Gist options
  • Save sfan5/1a124eb496a39734f53fb4daffc47bee to your computer and use it in GitHub Desktop.
Save sfan5/1a124eb496a39734f53fb4daffc47bee to your computer and use it in GitHub Desktop.
local DEMO_GENNOTIFY = true
assert(minetest.register_mapgen_script)
minetest.register_mapgen_script(minetest.get_modpath(minetest.get_current_modname()) .. "/inner.lua")
minetest.set_mapgen_setting("mg_name", "singlenode", true)
minetest.register_on_respawnplayer(function(player)
player:set_pos(vector.new(0, 1.5, 0))
return true
end)
minetest.register_on_newplayer(function(player)
player:set_pos(vector.new(0, 1.5, 0))
end)
-- Demonstrates the use of user-defined gennotify
if DEMO_GENNOTIFY then
minetest.set_gen_notify({custom=true}, nil, {"flatgen:place_light"})
minetest.register_on_generated(function(minp, maxp, blockseed)
local g = minetest.get_mapgen_object("gennotify")
--print(dump(g))
local array = g.custom["flatgen:place_light"] or {}
for _, pos in ipairs(array) do
minetest.place_node(pos, {name="default:torch", param2=1})
end
end)
end
local GROUND_LEVEL = 1
local DEMO_GENNOTIFY = true
local c_filler = minetest.get_content_id("default:stone")
local global_buffer = {}
local function profile(descr, func)
local pt = 0
local b = {}
return function(...)
local dt = minetest.get_us_time()
local r = {func(...)}
dt = minetest.get_us_time() - dt
table.insert(b, dt)
if pt ~= os.time() then
pt = os.time()
local s = 0
for _, n in ipairs(b) do
s = s + n
end
print(("%s took %.1fms (%d samples)"):format(descr, s / #b / 1000, #b))
b = {}
end
return unpack(r)
end
end
local function generate(vm, minp, maxp, blockseed)
local va
do
local pos1, pos2 = vm:get_emerged_area()
va = VoxelArea:new({MinEdge = pos1, MaxEdge = pos2})
end
if minp.y > GROUND_LEVEL then
return
end
local data = vm:get_data(global_buffer)
for i in va:iter(minp.x, minp.y, minp.z, maxp.x, math.min(maxp.y, GROUND_LEVEL), maxp.z) do
data[i] = c_filler
end
vm:set_data(data)
minetest.generate_ores(vm, minp, maxp)
vm:calc_lighting()
-- Demonstrates the use of user-defined gennotify
if DEMO_GENNOTIFY then
local pos = {}
if maxp.y > GROUND_LEVEL then
for x = minp.x, maxp.x, 16 do
for z = minp.z, maxp.z, 16 do
pos[#pos+1] = vector.new(x, GROUND_LEVEL+1, z)
end
end
end
minetest.save_gen_notify("flatgen:place_light", pos)
end
end
minetest.register_on_generated(generate)
--minetest.register_on_generated(profile("generate", generate))
name = flatgen
description = Flat mapgen based on new, innovative technologies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment