From 1feae7a1ea35fd8f4f3495d1bf65f8180bb0f720 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 25 Apr 2018 17:14:03 +0200 Subject: Add digiline interface and is_passive function to LuaATC --- advtrains_luaautomation/README.txt | 13 ++++++++++++- advtrains_luaautomation/active_common.lua | 14 ++++++++++++++ advtrains_luaautomation/atc_rail.lua | 10 ++++++++-- advtrains_luaautomation/environment.lua | 5 +++-- advtrains_luaautomation/operation_panel.lua | 9 +++++++-- advtrains_luaautomation/passive.lua | 15 ++++++++++++++- 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/advtrains_luaautomation/README.txt b/advtrains_luaautomation/README.txt index ce4d45d..faf3cbb 100644 --- a/advtrains_luaautomation/README.txt +++ b/advtrains_luaautomation/README.txt @@ -63,6 +63,10 @@ setstate(pos, newstate) Set the state of the passive component at position 'pos'. pos can be either a position vector (created by POS()) or a string, the name of this passive component. +is_passive(pos) +Checks whether there is a passive component at the position pos (and/or whether a passive component with this name exists) +pos can be either a position vector (created by POS()) or a string, the name of this passive component. + interrupt(time, message) Cause LuaAutomation to trigger an 'int' event on this component after the given time in seconds with the specified 'message' field. 'message' can be of any Lua data type. Not available in init code! @@ -71,6 +75,10 @@ interrupt_pos(pos, message) Immediately trigger an 'ext_int' event on the active component at position pos. 'message' is like in interrupt(). USE WITH CARE, or better don't use! Incorrect use can result in expotential growth of interrupts. +digiline_send(channel, message) +Make this active component send a digiline message on the specified channel. +Not available in init code! + ## Components and events The event table is a table of the following format: @@ -110,6 +118,9 @@ Fired when an interrupt set by the 'interrupt' function runs out. 'message' is t {type="ext_int", ext_int=true, message=} Fired when another node called 'interrupt_pos' on this position. 'message' is the message passed to the interrupt_pos function. +{type="digiline", digiline=true, channel=, msg=} +Fired when the controller receives a digiline message. + In addition to the default environment functions, the following functions are available: atc_send() @@ -132,7 +143,7 @@ set_line(number) # Operator panel This simple node executes its actions when punched. It can be used to change a switch and update the corresponding signals or similar applications. -The event fired is {type="punch", punch=true} by default. In case of an interrupt, the events are similar to the ones of the ATC rail. +The event fired is {type="punch", punch=true} by default. In case of an interrupt or a digiline message, the events are similar to the ones of the ATC rail. ### Passive components diff --git a/advtrains_luaautomation/active_common.lua b/advtrains_luaautomation/active_common.lua index 8c910c6..62dc83c 100644 --- a/advtrains_luaautomation/active_common.lua +++ b/advtrains_luaautomation/active_common.lua @@ -111,9 +111,19 @@ function ac.run_in_env(pos, evtdata, customfct_p) end local customfct=customfct_p or {} + -- add interrupt function customfct.interrupt=function(t, imesg) + assertt(t, "number") + assert(t >= 0) atlatc.interrupt.add(t, pos, {type="int", int=true, message=imesg}) end + -- add digiline_send function, if digiline is loaded + if digiline then + customfct.digiline_send=function(channel, msg) + assertt(channel, "string") + digiline:receptor_send(pos, digiline.rules.default, channel, msg) + end + end local datain=nodetbl.data or {} local succ, dataout = atlatc.envs[nodetbl.env]:execute_code(datain, nodetbl.code, evtdata, customfct) @@ -131,4 +141,8 @@ function ac.run_in_env(pos, evtdata, customfct_p) end end +function ac.on_digiline_receive(pos, node, channel, msg) + atlatc.interrupt.add(0, pos, {type="digiline", digiline=true, channel = channel, msg = msg}) +end + atlatc.active=ac diff --git a/advtrains_luaautomation/atc_rail.lua b/advtrains_luaautomation/atc_rail.lua index 3313401..88c9ee9 100644 --- a/advtrains_luaautomation/atc_rail.lua +++ b/advtrains_luaautomation/atc_rail.lua @@ -119,9 +119,15 @@ advtrains.register_tracks("default", { }, luaautomation = { fire_event=r.fire_event - } + }, + digiline = { + receptor = {}, + effector = { + action = atlatc.active.on_digiline_receive + }, + }, } - end + end, }, advtrains.trackpresets.t_30deg_straightonly) diff --git a/advtrains_luaautomation/environment.lua b/advtrains_luaautomation/environment.lua index 0b25e87..52d36a4 100644 --- a/advtrains_luaautomation/environment.lua +++ b/advtrains_luaautomation/environment.lua @@ -86,7 +86,7 @@ local function safe_string_find(...) end local mp=minetest.get_modpath("advtrains_luaautomation") -local p_api_getstate, p_api_setstate = dofile(mp.."/passive.lua") +local p_api_getstate, p_api_setstate, p_api_is_passive = dofile(mp.."/passive.lua") local static_env = { --core LUA functions @@ -150,7 +150,8 @@ local static_env = { POS = function(x,y,z) return {x=x, y=y, z=z} end, getstate = p_api_getstate, setstate = p_api_setstate, - --interrupts are handled per node, position unknown. + is_passive = p_api_is_passive, + --interrupts are handled per node, position unknown. (same goes for digilines) --however external interrupts can be set here. interrupt_pos = function(pos, imesg) if not type(pos)=="table" or not pos.x or not pos.y or not pos.z then diff --git a/advtrains_luaautomation/operation_panel.lua b/advtrains_luaautomation/operation_panel.lua index 1d585f7..eb7201c 100644 --- a/advtrains_luaautomation/operation_panel.lua +++ b/advtrains_luaautomation/operation_panel.lua @@ -18,6 +18,11 @@ minetest.register_node("advtrains_luaautomation:oppanel", { on_punch = on_punch, luaautomation = { fire_event=atlatc.active.run_in_env - } - + }, + digiline = { + receptor = {}, + effector = { + action = atlatc.active.on_digiline_receive + }, + }, }) diff --git a/advtrains_luaautomation/passive.lua b/advtrains_luaautomation/passive.lua index 774df8a..e0902f5 100644 --- a/advtrains_luaautomation/passive.lua +++ b/advtrains_luaautomation/passive.lua @@ -34,6 +34,19 @@ local function setstate(parpos, newstate) end end +local function is_passive(parpos) + local pos=atlatc.pcnaming.resolve_pos(parpos) + if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then + return false + end + local node=advtrains.ndb.get_node(pos) + local ndef=minetest.registered_nodes[node.name] + if ndef and ndef.luaautomation and ndef.luaautomation.getstate then + return true + end + return false +end + -- gets called from environment.lua -- return the values here to keep them local -return getstate, setstate +return getstate, setstate, is_passive -- cgit v1.2.3