From e1bf4081dd5e1e9b96d2177e4cc6499c65f1a76a Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sun, 28 Aug 2022 22:11:02 -0600 Subject: [PATCH] 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. I discovered this when I attempted to register an lvm in a mod that was intended to run after "main", to modify clay that the main mapgen lvm adds to the world. It wasn't finding any clay because priority was being ignored and it was running before "main" was. --- mods/MAPGEN/mcl_mapgen_core/init.lua | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/mods/MAPGEN/mcl_mapgen_core/init.lua b/mods/MAPGEN/mcl_mapgen_core/init.lua index a691c2fb35..d3f0af6c56 100644 --- a/mods/MAPGEN/mcl_mapgen_core/init.lua +++ b/mods/MAPGEN/mcl_mapgen_core/init.lua @@ -1,6 +1,5 @@ mcl_mapgen_core = {} local registered_generators = {} -local registered_generators_count = 0 local lvm, nodes, param2 = 0, 0, 0 local lvm_buffer = {} @@ -1831,7 +1830,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed) end 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 local lvm_used0, shadow0 = rec.vf(vm, data, data2, e1, e2, area, p1, p2, blockseed) if lvm_used0 then @@ -1856,7 +1855,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed) end if nodes > 0 then - for _, rec in pairs(registered_generators) do + for _, rec in ipairs(registered_generators) do if rec.nf then rec.nf(p1, p2, blockseed) end @@ -1867,7 +1866,7 @@ minetest.register_on_generated(function(minp, maxp, blockseed) end) 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 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 local new_record = { + id = id, i = priority, vf = lvm_function, nf = node_function, needs_param2 = needs_param2, } - registered_generators[id] = new_record - registered_generators_count = registered_generators_count + 1 + table.insert(registered_generators, new_record) table.sort(registered_generators, function(a, b) return (a.i < b.i) or ((a.i == b.i) and a.vf and (b.vf == nil)) end) end function mcl_mapgen_core.unregister_generator(id) - if not registered_generators[id] then return end - local rec = registered_generators[id] - registered_generators[id] = nil - registered_generators_count = registered_generators_count - 1 + local index + for i, gen in ipairs(registered_generators) do + if gen.id == id then + 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.nf then nodes = nodes - 1 end if rec.needs_param2 then param2 = param2 - 1 end