summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitaliy <numzer0@yandex.ru>2020-12-19 23:12:00 +0300
committerGitHub <noreply@github.com>2020-12-19 23:12:00 +0300
commit583d2c144165545f10a04075f1f215d829da1ab2 (patch)
treefdb65818e2854677e793b9fe9b9f34f3f588f044
parent397d449f1e668ceb8939626e018fc98ef72abbd3 (diff)
downloadmesecons-583d2c144165545f10a04075f1f215d829da1ab2.tar
mesecons-583d2c144165545f10a04075f1f215d829da1ab2.tar.gz
mesecons-583d2c144165545f10a04075f1f215d829da1ab2.tar.bz2
mesecons-583d2c144165545f10a04075f1f215d829da1ab2.tar.xz
mesecons-583d2c144165545f10a04075f1f215d829da1ab2.zip
Replace mesecon.mergetable (#533)
-rw-r--r--mesecons/internal.lua4
-rw-r--r--mesecons/presets.lua4
-rw-r--r--mesecons/util.lua33
-rw-r--r--mesecons_extrawires/mesewire.lua2
4 files changed, 35 insertions, 8 deletions
diff --git a/mesecons/internal.lua b/mesecons/internal.lua
index 6fdc3f9..044e3bf 100644
--- a/mesecons/internal.lua
+++ b/mesecons/internal.lua
@@ -92,8 +92,8 @@ function mesecon.get_any_inputrules(node)
end
function mesecon.get_any_rules(node)
- return mesecon.mergetable(mesecon.get_any_inputrules(node) or {},
- mesecon.get_any_outputrules(node) or {})
+ return mesecon.merge_rule_sets(mesecon.get_any_inputrules(node),
+ mesecon.get_any_outputrules(node))
end
-- Receptors
diff --git a/mesecons/presets.lua b/mesecons/presets.lua
index a2062d9..e10dd36 100644
--- a/mesecons/presets.lua
+++ b/mesecons/presets.lua
@@ -16,9 +16,9 @@ mesecon.rules.default = {
{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 = {
{x = 1, y = 0, z = 0},
diff --git a/mesecons/util.lua b/mesecons/util.lua
index f1f88d6..3cdeb64 100644
--- a/mesecons/util.lua
+++ b/mesecons/util.lua
@@ -213,8 +213,9 @@ function mesecon.cmpAny(t1, t2)
return true
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)
+ minetest.log("warning", debug.traceback("Deprecated call to mesecon.mergetable"))
local rval = mesecon.tablecopy(dest)
for k, v in pairs(source) do
@@ -227,6 +228,32 @@ function mesecon.mergetable(source, dest)
return rval
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 aren’t 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)
spec_common.drop = spec_common.drop or name .. "_off"
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_off.__mesecon_state = "off"
- spec_on = mesecon.mergetable(spec_common, spec_on);
- spec_off = mesecon.mergetable(spec_common, spec_off);
+ spec_on = mesecon.merge_tables(spec_common, spec_on);
+ spec_off = mesecon.merge_tables(spec_common, spec_off);
minetest.register_node(name .. "_on", spec_on)
minetest.register_node(name .. "_off", spec_off)
diff --git a/mesecons_extrawires/mesewire.lua b/mesecons_extrawires/mesewire.lua
index 455f75f..519129f 100644
--- a/mesecons_extrawires/mesewire.lua
+++ b/mesecons_extrawires/mesewire.lua
@@ -18,7 +18,7 @@ minetest.override_item("default:mese", {
-- Copy node definition of powered mese from normal mese
-- 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",
light_source = 5,
mesecons = {conductor = {