Replace mesecon.mergetable (#533)

This commit is contained in:
Vitaliy 2020-12-19 23:12:00 +03:00 committed by GitHub
parent 397d449f1e
commit 583d2c1441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -92,8 +92,8 @@ function mesecon.get_any_inputrules(node)
end end
function mesecon.get_any_rules(node) function mesecon.get_any_rules(node)
return mesecon.mergetable(mesecon.get_any_inputrules(node) or {}, return mesecon.merge_rule_sets(mesecon.get_any_inputrules(node),
mesecon.get_any_outputrules(node) or {}) mesecon.get_any_outputrules(node))
end end
-- Receptors -- Receptors

View File

@ -16,9 +16,9 @@ mesecon.rules.default = {
{x = 0, y = -1, z = -1}, {x = 0, y = -1, z = -1},
} }
mesecon.rules.floor = mesecon.mergetable(mesecon.rules.default, {{x = 0, y = -1, z = 0}}) mesecon.rules.floor = mesecon.merge_rule_sets(mesecon.rules.default, {{x = 0, y = -1, z = 0}})
mesecon.rules.pplate = mesecon.mergetable(mesecon.rules.floor, {{x = 0, y = -2, z = 0}}) mesecon.rules.pplate = mesecon.merge_rule_sets(mesecon.rules.floor, {{x = 0, y = -2, z = 0}})
mesecon.rules.buttonlike = { mesecon.rules.buttonlike = {
{x = 1, y = 0, z = 0}, {x = 1, y = 0, z = 0},

View File

@ -213,8 +213,9 @@ function mesecon.cmpAny(t1, t2)
return true return true
end end
-- does not overwrite values; number keys (ipairs) are appended, not overwritten -- Deprecated. Use `merge_tables` or `merge_rule_sets` as appropriate.
function mesecon.mergetable(source, dest) function mesecon.mergetable(source, dest)
minetest.log("warning", debug.traceback("Deprecated call to mesecon.mergetable"))
local rval = mesecon.tablecopy(dest) local rval = mesecon.tablecopy(dest)
for k, v in pairs(source) do for k, v in pairs(source) do
@ -227,6 +228,32 @@ function mesecon.mergetable(source, dest)
return rval return rval
end end
-- Merges several rule sets in one. Order may not be preserved. Nil arguments
-- are ignored.
-- The rule sets must be of the same kind (either all single-level or all two-level).
-- The function may be changed to normalize the resulting set in some way.
function mesecon.merge_rule_sets(...)
local rval = {}
for _, t in pairs({...}) do -- ignores nils automatically
table.insert_all(rval, mesecon.tablecopy(t))
end
return rval
end
-- Merges two tables, with entries from `replacements` taking precedence over
-- those from `base`. Returns the new table.
-- Values are deep-copied from either table, keys are referenced.
-- Numerical indices arent handled specially.
function mesecon.merge_tables(base, replacements)
local ret = mesecon.tablecopy(replacements) -- these are never overriden so have to be copied in any case
for k, v in pairs(base) do
if ret[k] == nil then -- it could be `false`
ret[k] = mesecon.tablecopy(v)
end
end
return ret
end
function mesecon.register_node(name, spec_common, spec_off, spec_on) function mesecon.register_node(name, spec_common, spec_off, spec_on)
spec_common.drop = spec_common.drop or name .. "_off" spec_common.drop = spec_common.drop or name .. "_off"
spec_common.on_blast = spec_common.on_blast or mesecon.on_blastnode spec_common.on_blast = spec_common.on_blast or mesecon.on_blastnode
@ -234,8 +261,8 @@ function mesecon.register_node(name, spec_common, spec_off, spec_on)
spec_on.__mesecon_state = "on" spec_on.__mesecon_state = "on"
spec_off.__mesecon_state = "off" spec_off.__mesecon_state = "off"
spec_on = mesecon.mergetable(spec_common, spec_on); spec_on = mesecon.merge_tables(spec_common, spec_on);
spec_off = mesecon.mergetable(spec_common, spec_off); spec_off = mesecon.merge_tables(spec_common, spec_off);
minetest.register_node(name .. "_on", spec_on) minetest.register_node(name .. "_on", spec_on)
minetest.register_node(name .. "_off", spec_off) minetest.register_node(name .. "_off", spec_off)

View File

@ -18,7 +18,7 @@ minetest.override_item("default:mese", {
-- Copy node definition of powered mese from normal mese -- Copy node definition of powered mese from normal mese
-- and brighten texture tiles to indicate mese is powered -- and brighten texture tiles to indicate mese is powered
local powered_def = mesecon.mergetable(minetest.registered_nodes["default:mese"], { local powered_def = mesecon.merge_tables(minetest.registered_nodes["default:mese"], {
drop = "default:mese", drop = "default:mese",
light_source = 5, light_source = 5,
mesecons = {conductor = { mesecons = {conductor = {