From 912f17f33590df4ad76775a3511881ad4bbf4853 Mon Sep 17 00:00:00 2001 From: Carter Kolwey Date: Tue, 23 Aug 2016 00:20:46 -0500 Subject: [PATCH] Split wires into their own mod, introduce autoconnect hooks The update_autoconnect function had to be abstracted away from the default wires, any kind of wire can now register autoconnect hooks, which should make having multiple different wire types much easier. mesecons_mvps, mesecons_receiver and mesecons_random made use of update_autoconnect, their code was also adapted. This also fixes a receiver bug: If a receiver was placed with a onstate receptor next to it (but not the wall lever / button that caused the receiver to appear) the receiver didn't turn on in the past. Also move documentation for mesecon wire into mesecons_wire. --- documentation.json | 2 +- mesecons/doc/mesecon/description.html | 1 - mesecons/init.lua | 3 -- mesecons/services.lua | 11 +++---- mesecons/util.lua | 31 ++++++++++++++++++ mesecons_mvps/init.lua | 1 - mesecons_random/init.lua | 2 +- mesecons_receiver/init.lua | 10 ++---- mesecons_wires/depends.txt | 1 + mesecons_wires/doc/mesecon/description.html | 1 + .../doc/mesecon/preview.png | Bin .../doc/mesecon/recipe.png | Bin mesecons/wires.lua => mesecons_wires/init.lua | 7 ++-- 13 files changed, 44 insertions(+), 26 deletions(-) delete mode 100644 mesecons/doc/mesecon/description.html create mode 100644 mesecons_wires/depends.txt create mode 100644 mesecons_wires/doc/mesecon/description.html rename {mesecons => mesecons_wires}/doc/mesecon/preview.png (100%) rename {mesecons => mesecons_wires}/doc/mesecon/recipe.png (100%) rename mesecons/wires.lua => mesecons_wires/init.lua (97%) diff --git a/documentation.json b/documentation.json index 8cc0254..711f7d2 100644 --- a/documentation.json +++ b/documentation.json @@ -1,6 +1,6 @@ { "Conductors" : { - "Mesecon" : "mesecons/doc/mesecon", + "Mesecon" : "mesecons_wires/doc/mesecon", "Insulated Wire" : "mesecons_insulated/doc/insulated", "T-Junction" : "mesecons_extrawires/doc/tjunction", "Crossing" : "mesecons_extrawires/doc/crossing", diff --git a/mesecons/doc/mesecon/description.html b/mesecons/doc/mesecon/description.html deleted file mode 100644 index a22ec65..0000000 --- a/mesecons/doc/mesecon/description.html +++ /dev/null @@ -1 +0,0 @@ - Mesecons are the wires, use them to connect effectors with receptors. diff --git a/mesecons/init.lua b/mesecons/init.lua index 3ab4f4a..6b36e69 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -132,8 +132,5 @@ print("[OK] Mesecons") -- To be removed in future releases dofile(minetest.get_modpath("mesecons").."/legacy.lua"); ---The actual wires -dofile(minetest.get_modpath("mesecons").."/wires.lua"); - --Services like turnoff receptor on dignode and so on dofile(minetest.get_modpath("mesecons").."/services.lua"); diff --git a/mesecons/services.lua b/mesecons/services.lua index 343410a..1abbc0c 100644 --- a/mesecons/services.lua +++ b/mesecons/services.lua @@ -1,7 +1,7 @@ -- Dig and place services -mesecon.on_placenode = function (pos, node) - mesecon.update_autoconnect(pos, node) +mesecon.on_placenode = function(pos, node) + mesecon.execute_autoconnect_hooks_now(pos, node) -- Receptors: Send on signal when active if mesecon.is_receptor_on(node.name) then @@ -52,16 +52,15 @@ mesecon.on_placenode = function (pos, node) end end -mesecon.on_dignode = function (pos, node) +mesecon.on_dignode = function(pos, node) if mesecon.is_conductor_on(node) then mesecon.receptor_off(pos, mesecon.conductor_get_rules(node)) elseif mesecon.is_receptor_on(node.name) then mesecon.receptor_off(pos, mesecon.receptor_get_rules(node)) end - mesecon.queue:add_action(pos, "update_autoconnect", {node}) -end -mesecon.queue:add_function("update_autoconnect", mesecon.update_autoconnect) + mesecon.execute_autoconnect_hooks_queue(pos, node) +end minetest.register_on_placenode(mesecon.on_placenode) minetest.register_on_dignode(mesecon.on_dignode) diff --git a/mesecons/util.lua b/mesecons/util.lua index 3827dce..d95f216 100644 --- a/mesecons/util.lua +++ b/mesecons/util.lua @@ -273,3 +273,34 @@ mesecon.forceloaded_blocks = mesecon.file2table("mesecon_forceloaded") minetest.register_on_shutdown(function() mesecon.table2file("mesecon_forceloaded", mesecon.forceloaded_blocks) end) + +-- Autoconnect Hooks +-- Nodes like conductors may change their appearance and their connection rules +-- right after being placed or after being dug, e.g. the default wires use this +-- to automatically connect to linking nodes after placement. +-- After placement, the update function will be executed immediately so that the +-- possibly changed rules can be taken into account when recalculating the circuit. +-- After digging, the update function will be queued and executed after +-- recalculating the circuit. The update function must take care of updating the +-- node at the given position itself, but also all of the other nodes the given +-- position may have (had) a linking connection to. +mesecon.autoconnect_hooks = {} + +-- name: A unique name for the hook, e.g. "foowire". Used to name the actionqueue function. +-- fct: The update function with parameters function(pos, node) +function mesecon.register_autoconnect_hook(name, fct) + mesecon.autoconnect_hooks[name] = fct + mesecon.queue:add_function("autoconnect_hook_"..name, fct) +end + +function mesecon.execute_autoconnect_hooks_now(pos, node) + for _, fct in pairs(mesecon.autoconnect_hooks) do + fct(pos, node) + end +end + +function mesecon.execute_autoconnect_hooks_queue(pos, node) + for name in pairs(mesecon.autoconnect_hooks) do + mesecon.queue:add_action(pos, "autoconnect_hook_"..name, {node}) + end +end diff --git a/mesecons_mvps/init.lua b/mesecons_mvps/init.lua index 1707408..2c6161b 100644 --- a/mesecons_mvps/init.lua +++ b/mesecons_mvps/init.lua @@ -200,7 +200,6 @@ end mesecon.register_on_mvps_move(function(moved_nodes) for _, n in ipairs(moved_nodes) do mesecon.on_placenode(n.pos, n.node) - mesecon.update_autoconnect(n.pos) end end) diff --git a/mesecons_random/init.lua b/mesecons_random/init.lua index e37886b..4c1d299 100644 --- a/mesecons_random/init.lua +++ b/mesecons_random/init.lua @@ -9,7 +9,7 @@ minetest.register_node("mesecons_random:removestone", { mesecons = {effector = { action_on = function (pos, node) minetest.remove_node(pos) - mesecon.update_autoconnect(pos) + mesecon.on_dignode(pos, node) end }} }) diff --git a/mesecons_receiver/init.lua b/mesecons_receiver/init.lua index 36f4659..9284e57 100644 --- a/mesecons_receiver/init.lua +++ b/mesecons_receiver/init.lua @@ -104,13 +104,8 @@ function mesecon.receiver_place(rcpt_pos) if string.find(nn.name, "mesecons:wire_") ~= nil then minetest.dig_node(pos) - if mesecon.is_power_on(rcpt_pos) then - minetest.set_node(pos, {name = "mesecons_receiver:receiver_on", param2 = node.param2}) - mesecon.receptor_on(pos, receiver_get_rules(node)) - else - minetest.set_node(pos, {name = "mesecons_receiver:receiver_off", param2 = node.param2}) - end - mesecon.update_autoconnect(pos) + minetest.set_node(pos, {name = "mesecons_receiver:receiver_off", param2 = node.param2}) + mesecon.on_placenode(pos, nn) end end @@ -121,7 +116,6 @@ function mesecon.receiver_remove(rcpt_pos, dugnode) minetest.dig_node(pos) local node = {name = "mesecons:wire_00000000_off"} minetest.set_node(pos, node) - mesecon.update_autoconnect(pos) mesecon.on_placenode(pos, node) end end diff --git a/mesecons_wires/depends.txt b/mesecons_wires/depends.txt new file mode 100644 index 0000000..acaa924 --- /dev/null +++ b/mesecons_wires/depends.txt @@ -0,0 +1 @@ +mesecons diff --git a/mesecons_wires/doc/mesecon/description.html b/mesecons_wires/doc/mesecon/description.html new file mode 100644 index 0000000..f217be9 --- /dev/null +++ b/mesecons_wires/doc/mesecon/description.html @@ -0,0 +1 @@ +Mesecons are the wires, use them to connect effectors with receptors. diff --git a/mesecons/doc/mesecon/preview.png b/mesecons_wires/doc/mesecon/preview.png similarity index 100% rename from mesecons/doc/mesecon/preview.png rename to mesecons_wires/doc/mesecon/preview.png diff --git a/mesecons/doc/mesecon/recipe.png b/mesecons_wires/doc/mesecon/recipe.png similarity index 100% rename from mesecons/doc/mesecon/recipe.png rename to mesecons_wires/doc/mesecon/recipe.png diff --git a/mesecons/wires.lua b/mesecons_wires/init.lua similarity index 97% rename from mesecons/wires.lua rename to mesecons_wires/init.lua index d77904e..879cfa1 100644 --- a/mesecons/wires.lua +++ b/mesecons_wires/init.lua @@ -91,10 +91,7 @@ local update_on_place_dig = function (pos, node) end end -function mesecon.update_autoconnect(pos, node) - if (not node) then node = minetest.get_node(pos) end - update_on_place_dig(pos, node) -end +mesecon.register_autoconnect_hook("wire", update_on_place_dig) -- ############################ -- ## Wire node registration ## @@ -204,7 +201,7 @@ register_wires = function() groups_off["not_in_creative_inventory"] = 1 end - mesecon.register_node("mesecons:wire_"..nodeid, { + mesecon.register_node(":mesecons:wire_"..nodeid, { description = "Mesecon", drawtype = "nodebox", inventory_image = "mesecons_wire_inv.png",