Skip to content

Commit

Permalink
MapGen: extend minetest with .register_on_dungeon_generated(). Re…
Browse files Browse the repository at this point in the history
…lates to #1140, #1141
  • Loading branch information
alek13 committed Dec 10, 2023
1 parent b34947f commit a4097ec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
8 changes: 6 additions & 2 deletions mods/lord/Core/builtin/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

-- As for now, this mod used only for translations of builtin functionality.
-- Translations need to be removed after it appears in MT (#1187).
local mod_path = minetest.get_modpath(minetest.get_current_modname())
local require = function(name) return dofile(mod_path .. "/src/" .. name:gsub("%.", "/") .. ".lua") end

require("mapgen")

-- Translations (`./locale/__builtin.ru.tr`) need to be removed after it appears in MT (#1187).
34 changes: 34 additions & 0 deletions mods/lord/Core/builtin/src/mapgen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


local on_generated_is_registered = false
local on_dungeon_generated_handlers = {}

--- @param callback fun(minp:Position, maxp:Position, data:table, area:VoxelArea, room_centers:Position[])
minetest.register_on_dungeon_generated = function(callback)
table.insert(on_dungeon_generated_handlers, callback)

if on_generated_is_registered then
return
end

minetest.set_gen_notify("dungeon")
minetest.register_on_generated(function(minp, maxp, seed)
local notify = minetest.get_mapgen_object("gennotify")
if not notify or not notify.dungeon then
return
end

local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new({ MinEdge = emin, MaxEdge = emax })
local data = vm:get_data()

for _, on_dungeon_generated_handler in pairs(on_dungeon_generated_handlers) do
on_dungeon_generated_handler(minp, maxp, data, area, notify.dungeon)
end

vm:set_data(data)
vm:calc_lighting()
vm:write_to_map()
end)
on_generated_is_registered = true
end
20 changes: 3 additions & 17 deletions mods/lord/World/Generation/buildings/src/dungeons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,16 @@ local function find_room_with_space(room_centers, area, data)
return nil
end

minetest.set_gen_notify("dungeon")

minetest.register_on_generated(function(minp, maxp, seed)
minetest.register_on_dungeon_generated(function(minp, maxp, data, area, room_centers)
if maxp.y < TOMB_Y_MIN or minp.y > TOMB_Y_MAX then
return
end

local notify = minetest.get_mapgen_object("gennotify")
if not notify or not notify.dungeon then
return
end

local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new({ MinEdge = emin, MaxEdge = emax })
local data = vm:get_data()

if #notify.dungeon >= TOMB.MIN_ROOMS and math.random(TOMB.CHANCE) == 1 then
local place_to = find_room_with_space(notify.dungeon, area, data)
if #room_centers >= TOMB.MIN_ROOMS and math.random(TOMB.CHANCE) == 1 then
local place_to = find_room_with_space(room_centers, area, data)
if place_to then
place_tomb(place_to.x, place_to.y, place_to.z, area, data)
end
end

vm:set_data(data)
vm:calc_lighting()
vm:write_to_map()
end)

0 comments on commit a4097ec

Please sign in to comment.