summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarter Kolwey <cheapiephp@gmail.com>2016-08-23 00:20:46 -0500
committerJeija <norrepli@gmail.com>2016-08-23 11:08:53 +0200
commit912f17f33590df4ad76775a3511881ad4bbf4853 (patch)
tree2d246683d4843b7829b026577c228038ea648c3c
parent4816dee396203dba1df9328c5565c145c4f2751f (diff)
downloadmesecons-912f17f33590df4ad76775a3511881ad4bbf4853.tar
mesecons-912f17f33590df4ad76775a3511881ad4bbf4853.tar.gz
mesecons-912f17f33590df4ad76775a3511881ad4bbf4853.tar.bz2
mesecons-912f17f33590df4ad76775a3511881ad4bbf4853.tar.xz
mesecons-912f17f33590df4ad76775a3511881ad4bbf4853.zip
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.
-rw-r--r--documentation.json2
-rw-r--r--mesecons/doc/mesecon/description.html1
-rw-r--r--mesecons/init.lua3
-rw-r--r--mesecons/services.lua11
-rw-r--r--mesecons/util.lua31
-rw-r--r--mesecons_mvps/init.lua1
-rw-r--r--mesecons_random/init.lua2
-rw-r--r--mesecons_receiver/init.lua10
-rw-r--r--mesecons_wires/depends.txt1
-rw-r--r--mesecons_wires/doc/mesecon/description.html1
-rwxr-xr-xmesecons_wires/doc/mesecon/preview.png (renamed from mesecons/doc/mesecon/preview.png)bin29946 -> 29946 bytes
-rw-r--r--mesecons_wires/doc/mesecon/recipe.png (renamed from mesecons/doc/mesecon/recipe.png)bin3932 -> 3932 bytes
-rw-r--r--mesecons_wires/init.lua (renamed from mesecons/wires.lua)7
13 files changed, 44 insertions, 26 deletions
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
index f81e5cb..f81e5cb 100755
--- a/mesecons/doc/mesecon/preview.png
+++ b/mesecons_wires/doc/mesecon/preview.png
Binary files differ
diff --git a/mesecons/doc/mesecon/recipe.png b/mesecons_wires/doc/mesecon/recipe.png
index 72f9210..72f9210 100644
--- a/mesecons/doc/mesecon/recipe.png
+++ b/mesecons_wires/doc/mesecon/recipe.png
Binary files differ
diff --git a/mesecons/wires.lua b/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",