From f33bb563e78a62cc8fb2e1483a6cb9a2436b708a Mon Sep 17 00:00:00 2001 From: orwell96 Date: Tue, 7 Jul 2020 14:25:47 +0200 Subject: Add "Fallback state" for freed routes, move p_mesecon_iface.lua to core --- advtrains/init.lua | 4 +++ advtrains/p_mesecon_iface.lua | 54 +++++++++++++++++++++++++++++ advtrains/passive.lua | 22 ++++++++++++ advtrains/signals.lua | 1 + advtrains_interlocking/routesetting.lua | 1 + advtrains_luaautomation/init.lua | 4 +-- advtrains_luaautomation/p_mesecon_iface.lua | 53 ---------------------------- 7 files changed, 83 insertions(+), 56 deletions(-) create mode 100644 advtrains/p_mesecon_iface.lua delete mode 100644 advtrains_luaautomation/p_mesecon_iface.lua diff --git a/advtrains/init.lua b/advtrains/init.lua index 0036037..28e104e 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -215,6 +215,10 @@ dofile(advtrains.modpath.."/craft_items.lua") dofile(advtrains.modpath.."/log.lua") dofile(advtrains.modpath.."/passive.lua") +if mesecon then + dofile(advtrains.modpath.."/p_mesecon_iface.lua") +end + dofile(advtrains.modpath.."/lzb.lua") diff --git a/advtrains/p_mesecon_iface.lua b/advtrains/p_mesecon_iface.lua new file mode 100644 index 0000000..87e1c6d --- /dev/null +++ b/advtrains/p_mesecon_iface.lua @@ -0,0 +1,54 @@ +-- p_mesecon_iface.lua +-- Mesecons interface by overriding the switch + +if not mesecon then return end + +minetest.override_item("mesecons_switch:mesecon_switch_off", { + groups = { + dig_immediate=2, + save_in_nodedb=1, + }, + on_rightclick = function (pos, node) + advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2}) + mesecon.receptor_on(pos) + minetest.sound_play("mesecons_switch", {pos=pos}) + end, + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_off(pos) + end, + advtrains = { + getstate = "off", + setstate = function(pos, node, newstate) + if newstate=="on" then + advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2}) + mesecon.receptor_on(pos) + end + end, + }, +}) + +minetest.override_item("mesecons_switch:mesecon_switch_on", { + groups = { + dig_immediate=2, + save_in_nodedb=1, + not_in_creative_inventory=1, + }, + on_rightclick = function (pos, node) + advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2}) + mesecon.receptor_off(pos) + minetest.sound_play("mesecons_switch", {pos=pos}) + end, + on_updated_from_nodedb = function(pos, node) + mesecon.receptor_on(pos) + end, + advtrains = { + getstate = "on", + setstate = function(pos, node, newstate) + if newstate=="off" then + advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2}) + mesecon.receptor_off(pos) + end + end, + fallback_state = "off", + }, +}) diff --git a/advtrains/passive.lua b/advtrains/passive.lua index 07cab42..fe4790c 100644 --- a/advtrains/passive.lua +++ b/advtrains/passive.lua @@ -97,3 +97,25 @@ function advtrains.is_passive(parpos, pnode) return false end end + +-- switches a node back to fallback state, if defined. Doesn't support pcnaming. +function advtrains.set_fallback_state(pos, pnode) + local node=pnode or advtrains.ndb.get_node(pos) + local ndef=minetest.registered_nodes[node.name] + local st + if ndef and ndef.advtrains and ndef.advtrains.setstate + and ndef.advtrains.fallback_state then + if advtrains.get_train_at_pos(pos) then + return false + end + + if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.pos_to_string(pos)) then + return false + end + + ndef.advtrains.setstate(pos, node, ndef.advtrains.fallback_state) + return true + end + + +end diff --git a/advtrains/signals.lua b/advtrains/signals.lua index b3dceca..e144aa6 100644 --- a/advtrains/signals.lua +++ b/advtrains/signals.lua @@ -302,6 +302,7 @@ minetest.register_node("advtrains:across_on", { advtrains.ndb.swap_node(pos, {name = "advtrains:across_off", param2 = node.param2}, true) end end, + fallback_state = "off", }, on_rightclick=function(pos, node, player) if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then diff --git a/advtrains_interlocking/routesetting.lua b/advtrains_interlocking/routesetting.lua index 1c9ee5a..575b053 100644 --- a/advtrains_interlocking/routesetting.lua +++ b/advtrains_interlocking/routesetting.lua @@ -206,6 +206,7 @@ function ilrs.free_route_locks_indiv(pts, ts, nocallbacks) -- TODO use luaautomation timers? if not nocallbacks then minetest.after(0, ilrs.update_waiting, "lck", pts) + minetest.after(0.5, advtrains.set_fallback_state, minetest.string_to_pos(pts)) end end -- frees all route locks, even manual ones set with the tool, at a specific position diff --git a/advtrains_luaautomation/init.lua b/advtrains_luaautomation/init.lua index 573a553..eca5633 100644 --- a/advtrains_luaautomation/init.lua +++ b/advtrains_luaautomation/init.lua @@ -33,9 +33,7 @@ dofile(mp.."/active_common.lua") dofile(mp.."/atc_rail.lua") dofile(mp.."/operation_panel.lua") dofile(mp.."/pcnaming.lua") -if mesecon then - dofile(mp.."/p_mesecon_iface.lua") -end + dofile(mp.."/chatcmds.lua") diff --git a/advtrains_luaautomation/p_mesecon_iface.lua b/advtrains_luaautomation/p_mesecon_iface.lua deleted file mode 100644 index 95e5d8a..0000000 --- a/advtrains_luaautomation/p_mesecon_iface.lua +++ /dev/null @@ -1,53 +0,0 @@ --- p_mesecon_iface.lua --- Mesecons interface by overriding the switch - -if not mesecon then return end - -minetest.override_item("mesecons_switch:mesecon_switch_off", { - groups = { - dig_immediate=2, - save_in_nodedb=1, - }, - on_rightclick = function (pos, node) - advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2}) - mesecon.receptor_on(pos) - minetest.sound_play("mesecons_switch", {pos=pos}) - end, - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_off(pos) - end, - luaautomation = { - getstate = "off", - setstate = function(pos, node, newstate) - if newstate=="on" then - advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2}) - mesecon.receptor_on(pos) - end - end, - }, -}) - -minetest.override_item("mesecons_switch:mesecon_switch_on", { - groups = { - dig_immediate=2, - save_in_nodedb=1, - not_in_creative_inventory=1, - }, - on_rightclick = function (pos, node) - advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2}) - mesecon.receptor_off(pos) - minetest.sound_play("mesecons_switch", {pos=pos}) - end, - on_updated_from_nodedb = function(pos, node) - mesecon.receptor_on(pos) - end, - luaautomation = { - getstate = "on", - setstate = function(pos, node, newstate) - if newstate=="off" then - advtrains.ndb.swap_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2}) - mesecon.receptor_off(pos) - end - end, - }, -}) -- cgit v1.2.3