Merge pull request 'since registered_generators was a table, not an array, table.sort was having no effect and so priority was being ignored. Generators were being run in an arbitrary order.' (#2604) from FaceDeer/MineClone2:master into master

Reviewed-on: MineClone2/MineClone2#2604
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-09-02 10:02:46 +00:00
commit e5d4e410ae
1 changed files with 15 additions and 10 deletions

View File

@ -1,6 +1,5 @@
mcl_mapgen_core = {} mcl_mapgen_core = {}
local registered_generators = {} local registered_generators = {}
local registered_generators_count = 0
local lvm, nodes, param2 = 0, 0, 0 local lvm, nodes, param2 = 0, 0, 0
local lvm_buffer = {} local lvm_buffer = {}
@ -1831,7 +1830,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed)
end end
local area = VoxelArea:new({MinEdge=e1, MaxEdge=e2}) local area = VoxelArea:new({MinEdge=e1, MaxEdge=e2})
for _, rec in pairs(registered_generators) do for _, rec in ipairs(registered_generators) do
if rec.vf then if rec.vf then
local lvm_used0, shadow0 = rec.vf(vm, data, data2, e1, e2, area, p1, p2, blockseed) local lvm_used0, shadow0 = rec.vf(vm, data, data2, e1, e2, area, p1, p2, blockseed)
if lvm_used0 then if lvm_used0 then
@ -1856,7 +1855,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed)
end end
if nodes > 0 then if nodes > 0 then
for _, rec in pairs(registered_generators) do for _, rec in ipairs(registered_generators) do
if rec.nf then if rec.nf then
rec.nf(p1, p2, blockseed) rec.nf(p1, p2, blockseed)
end end
@ -1867,7 +1866,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed)
end) end)
function minetest.register_on_generated(node_function) function minetest.register_on_generated(node_function)
mcl_mapgen_core.register_generator("mod_"..minetest.get_current_modname().."_"..tostring(registered_generators_count+1), nil, node_function) mcl_mapgen_core.register_generator("mod_"..minetest.get_current_modname().."_"..tostring(#registered_generators+1), nil, node_function)
end end
function mcl_mapgen_core.register_generator(id, lvm_function, node_function, priority, needs_param2) function mcl_mapgen_core.register_generator(id, lvm_function, node_function, priority, needs_param2)
@ -1880,24 +1879,30 @@ function mcl_mapgen_core.register_generator(id, lvm_function, node_function, pri
if needs_param2 then param2 = param2 + 1 end if needs_param2 then param2 = param2 + 1 end
local new_record = { local new_record = {
id = id,
i = priority, i = priority,
vf = lvm_function, vf = lvm_function,
nf = node_function, nf = node_function,
needs_param2 = needs_param2, needs_param2 = needs_param2,
} }
registered_generators[id] = new_record table.insert(registered_generators, new_record)
registered_generators_count = registered_generators_count + 1
table.sort(registered_generators, function(a, b) table.sort(registered_generators, function(a, b)
return (a.i < b.i) or ((a.i == b.i) and a.vf and (b.vf == nil)) return (a.i < b.i) or ((a.i == b.i) and a.vf and (b.vf == nil))
end) end)
end end
function mcl_mapgen_core.unregister_generator(id) function mcl_mapgen_core.unregister_generator(id)
if not registered_generators[id] then return end local index
local rec = registered_generators[id] for i, gen in ipairs(registered_generators) do
registered_generators[id] = nil if gen.id == id then
registered_generators_count = registered_generators_count - 1 index = i
break
end
end
if not index then return end
local rec = registered_generators[index]
table.remove(registered_generators, index)
if rec.vf then lvm = lvm - 1 end if rec.vf then lvm = lvm - 1 end
if rec.nf then nodes = nodes - 1 end if rec.nf then nodes = nodes - 1 end
if rec.needs_param2 then param2 = param2 - 1 end if rec.needs_param2 then param2 = param2 - 1 end