forked from minetest-mods/mesecons
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.
This commit is contained in:
parent
4816dee396
commit
912f17f335
@ -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",
|
||||
|
@ -1 +0,0 @@
|
||||
Mesecons are the wires, use them to connect effectors with receptors.
|
@ -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");
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
}}
|
||||
})
|
||||
|
@ -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)
|
||||
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
|
||||
|
1
mesecons_wires/depends.txt
Normal file
1
mesecons_wires/depends.txt
Normal file
@ -0,0 +1 @@
|
||||
mesecons
|
1
mesecons_wires/doc/mesecon/description.html
Normal file
1
mesecons_wires/doc/mesecon/description.html
Normal file
@ -0,0 +1 @@
|
||||
Mesecons are the wires, use them to connect effectors with receptors.
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
@ -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",
|
Loading…
Reference in New Issue
Block a user