summaryrefslogtreecommitdiff
path: root/advtrains/advtrains_luaautomation
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/advtrains_luaautomation')
-rw-r--r--advtrains/advtrains_luaautomation/README.txt288
-rw-r--r--advtrains/advtrains_luaautomation/active_common.lua175
-rw-r--r--advtrains/advtrains_luaautomation/atc_rail.lua194
-rw-r--r--advtrains/advtrains_luaautomation/chatcmds.lua84
-rw-r--r--advtrains/advtrains_luaautomation/depends.txt4
-rw-r--r--advtrains/advtrains_luaautomation/environment.lua362
-rw-r--r--advtrains/advtrains_luaautomation/init.lua109
-rw-r--r--advtrains/advtrains_luaautomation/interrupt.lua48
-rw-r--r--advtrains/advtrains_luaautomation/operation_panel.lua28
-rw-r--r--advtrains/advtrains_luaautomation/p_display.lua0
-rw-r--r--advtrains/advtrains_luaautomation/passive_api.txt24
-rw-r--r--advtrains/advtrains_luaautomation/pcnaming.lua76
-rw-r--r--advtrains/advtrains_luaautomation/textures/atlatc_oppanel.pngbin0 -> 631 bytes
-rw-r--r--advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.pngbin0 -> 329 bytes
14 files changed, 1392 insertions, 0 deletions
diff --git a/advtrains/advtrains_luaautomation/README.txt b/advtrains/advtrains_luaautomation/README.txt
new file mode 100644
index 0000000..287a0bd
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/README.txt
@@ -0,0 +1,288 @@
+
+#### Advtrains - Lua Automation features
+
+This mod offers components that run LUA code and interface with each other through a global environment. It makes complex automated railway systems possible.
+
+### atlatc
+The mod is sometimes abbreviated as 'atlatc'. This stands for AdvTrainsLuaATC. This short name has been chosen for user convenience, since the name of this mod ('advtrains_luaautomation') is very long.
+
+### Privilege
+To perform any operations using this mod (except executing operation panels), players need the "atlatc" privilege.
+This privilege should never be granted to anyone except trusted administrators. Even though the LUA environment is sandboxed, it is still possible to DoS the server by coding infinite loops or requesting expotentially growing interrupts.
+
+### Active and passive
+Active components are these who have LUA code running in them. They are triggered on specific events. Passive components are dumb, they only have a state and can be set to another state, they can't perform actions themselves.
+
+### Environments
+
+Each active component is assigned to an environment. This is where all data are held. Components in different environments can't inferface with each other.
+This system allows multiple independent automation systems to run simultaneously without polluting each other's environment.
+
+/env_create <env_name>
+Create environment with the given name. To be able to do anything, you first need to create an environment. Choose the name wisely, you can't change it afterwards.
+
+/env_setup <env_name>
+Invoke the form to edit the environment's initialization code. For more information, see the section on active components. You can also delete an environment from here.
+
+### Active components
+
+The code of every active component is run on specific events which are explained soon. When run, every variable written that is not local and is no function or userdata is saved over code re-runs and over server restarts. Additionally, the following global variables are defined:
+
+# event
+The variable 'event' contains a table with information on the current event. How this table can look is explained below.
+
+# S
+The variable 'S' contains a table which is shared between all components of the environment. Its contents are persistent over server restarts. May not contain functions, every other value is allowed.
+Example:
+Component 1: S.stuff="foo"
+Component 2: print(S.stuff)
+-> foo
+
+# F
+The variable 'F' also contains a table which is shared between all components of the environment. Its contents are discarded on server shutdown or when the init code gets re-run. Every data type is allowed, even functions.
+The purpose of this table is not to save data, but to provide static value and function definitions. The table should be populated by the init code.
+
+# Standard Lua functions
+The following standard Lua libraries are available:
+string, math, table, os
+The following standard Lua functions are available:
+assert, error, ipairs, pairs, next, select, tonumber, tostring, type, unpack
+
+Every attempt to overwrite any of the predefined values results in an error.
+
+# LuaAutomation-specific global functions
+
+POS(x,y,z)
+Shorthand function to create a position vector {x=?, y=?, z=?} with less characters
+
+In the following functions, all parameters named 'pos' designate a position. You can use either:
+- a default Minetest position vector (like {x=34, y=2, z=-18})
+- the POS(34,2,-18) shorthand
+- A string, the passive component name. See 'passive component naming'.
+
+getstate(pos)
+Get the state of the passive component at position 'pos'. See section on passive components for more info.
+pos can be either a position vector (created by POS()) or a string, the name of this passive component.
+
+setstate(pos, newstate)
+Set the state of the passive component at position 'pos'.
+
+is_passive(pos)
+Checks whether there is a passive component at the position pos (and/or whether a passive component with this name exists)
+
+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!
+
+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!
+
+-- The next 4 functions are available when advtrains_interlocking is enabled: --
+
+can_set_route(pos, route_name)
+Returns whether it is possible to set the route designated by route_name from the signal at pos.
+
+set_route(pos, route_name)
+Requests the given route from the signal at pos. Has the same effect as clicking "Set Route" in the signalling dialog.
+
+cancel_route(pos)
+Cancels the route that is set from the signal at pos. Has the same effect as clicking "Cancel Route" in the signalling dialog.
+
+get_aspect(pos)
+Returns the signal aspect of the signal at pos. A signal aspect has the following format:
+aspect = {
+ main = { -- the next track section in line. Shows blocked for shunt routes
+ free = <boolean>,
+ speed = <int km/h>,
+ },
+ shunt = { -- whether a "shunting allowed" aspect should be shown
+ free = <boolean>,
+ }
+ dst = { -- the aspect of the next main signal on (at end of) route
+ free = <boolean>,
+ speed = <int km/h>,
+ }
+ info = {
+ call_on = <boolean>, -- Call-on route, expect train in track ahead
+ dead_end = <boolean>, -- Route ends on a dead end (e.g. bumper)
+ }
+}
+As of August 2018, only the aspect.main.free field is ever used by the interlocking system.
+
+# Lines
+
+The advtrains_line_automation component adds a few contraptions that should make creating timeable systems easier.
+Part of its functionality is also available in LuaATC:
+
+- rwt.* - all Railway Time functions are included as documented in https://advtrains.de/wiki/doku.php?id=dev:lines:rwt
+
+- schedule(rw_time, msg)
+- schedule_in(rw_dtime, msg)
+Schedules an event of type {type="schedule", schedule=true, msg=msg} at (resp. after) the specified railway time.
+(which can be in any format). You can only schedule one event this way. (uses the new lines-internal scheduler)
+
+## Components and events
+
+The event table is a table of the following format:
+{
+ type = "<type>",
+ <type> = true,
+ ... additional content ...
+}
+You can check for the event type by either using
+if event.type == "wanted" then ...do stuff... end
+or
+if event.wanted then ...do stuff... end
+(if 'wanted' is the event type to check for)
+
+# Init code
+The initialization code is not a component as such, but rather a part of the whole environment. It can (and should) be used to make definitions that other components can refer to.
+Examples:
+A function to define behavior for trains in subway stations:
+function F.station()
+ if event.train then atc_send("B0WOL") end
+ if event.int and event.message="depart" then atc_send("OCD1SM") end
+end
+
+The init code is run whenever the F table needs to be refilled with data. This is the case on server startup and whenever the init code is changed and you choose to run it.
+Functions are run in the environment of the currently active node, regardless of where they were defined. So, the 'event' table always reflects the state of the calling node.
+
+The 'event' table of the init code is always {type="init", init=true}.
+
+# ATC rails
+The Lua-controlled ATC rails are the only components that can actually interface with trains. The following event types are generated:
+
+{type="train", train=true, id="<train_id>"}
+This event is fired when a train enters the rail. The field 'id' is the unique train ID, which is 6-digit random numerical string.
+If the world contains trains from an older advtrains version, this string may be longer and contain a dot (.)
+
+{type="int", int=true, msg=<message>}
+Fired when an interrupt set by the 'interrupt' function runs out. 'message' is the message passed to the interrupt function.
+For backwards compatiblity reasons, <message> is also contained in an event.message field.
+{type="ext_int", ext_int=true, message=<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=<channel>, msg=<message>}
+Fired when the controller receives a digiline message.
+
+In addition to the default environment functions, the following functions are available:
+
+atc_send(<atc_command>)
+ Sends the specified ATC command to the train and returns true. If there is no train, returns false and does nothing.
+atc_reset()
+ Resets the train's current ATC command. If there is no train, returns false and does nothing.
+atc_arrow
+ Boolean, true when the train is driving in the direction of the arrows of the ATC rail. Nil if there is no train.
+atc_id
+ Train ID of the train currently passing the controller. Nil if there's no train.
+atc_speed
+ Speed of the train, or nil if there is no train.
+atc_set_text_outside(text)
+ Set text shown on the outside of the train. Pass nil to show no text.
+atc_set_text_inside(text)
+ Set text shown to train passengers. Pass nil to show no text.
+get_line()
+ Returns the "Line" property of the train (a string).
+ This can be used to distinguish between trains of different lines and route them appropriately.
+ The interlocking system also uses this property for Automatic Routesetting.
+set_line(line)
+ Sets the "Line" property of the train (a string).
+ If the first digit of this string is a number (0-9), any subway wagons on the train will have this one displayed as line number
+ (where "0" is actually shown as Line 10 on the train)
+get_rc()
+ Returns the "Routingcode" property of the train (a string).
+ The interlocking system uses this property for Automatic Routesetting.
+set_rc(routingcode)
+ Sets the "Routingcode" property of the train (a string).
+ The interlocking system uses this property for Automatic Routesetting.
+split_at_index(index, command)
+ Splits the train at the specified index, into a train with index-1 wagons and a second train starting with the index-th wagon.
+ command specifies an atc command to be sent to the second train after decoupling.
+split_at_fc(command, len)
+ Splits the train in such a way that all cars with non-empty
+ current FC of the first part of the train have the same FC. The
+ command specified is sent to the rear part, as with
+ split_at_index. It returns the fc of the cars of the first part.
+
+ The optional argument len specifies the maximum length for the
+ first part of the train. Say, we have len=3, and the train has ""
+ "" "foo" "foo" "foo" "bar", then the first train part will be ""
+ "" "foo".
+
+ Example : Train has current FCs "" "" "foo" "bar" "foo"
+ Result: first train: "" "" "foo"; second train: "bar" "foo"
+ The command returns "foo" in this case
+split_off_locomotive(command, len)
+ Splits off the locomotives at the front of the train, which are
+ identified by an empty FC. command specifies the command to be
+ executed by the rear half of the train.
+
+ The optional argument len specifies the maximum length for the
+ first part of the train. Say, we have len=3, and the train has ""
+ "" "foo" "foo" "foo" "bar", then the first train part will be ""
+ "" "foo".
+step_fc()
+ Steps the FCs of all train cars forward. FCs are composed of codes
+ separated by exclamation marks (!), for instance
+ "foo!bar!baz". Each wagon has a current FC, indicating its next
+ destination. Stepping the freight code forward, selects the next
+ code after the !. If the end of the string is reached, then the
+ first code is selected, except if the string ends with a question
+ mark, then the order is reversed.
+
+train_length()
+ returns the number of cars the train is composed of
+set_autocouple()
+ Sets the train into autocouple mode
+unset_autocouple()
+ Unsets autocouple mode
+
+set_shunt(), unset_shunt()
+ deprecated aliases for set_autocouple() and unset_autocouple(), will be removed from a later release.
+
+
+# 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 or a digiline message, the events are similar to the ones of the ATC rail.
+
+### Passive components
+
+All passive components can be interfaced with the setstate and getstate functions(see above).
+Below, each apperance is mapped to the "state" of that node.
+
+## Signals
+The light signals are interfaceable, the analog signals are not.
+"green" - Signal shows green light
+"red" - Signal shows red light
+
+## Switches
+All default rail switches are interfaceable, independent of orientation.
+"cr" - The switch is set in the direction that is not straight.
+"st" - The switch is set in the direction that is straight.
+
+## Mesecon Switch
+The Mesecon switch can be switched using LuaAutomation. Note that this is not possible on levers, only the full-node 'Switch' block.
+"on" - the switch is switched on
+"off" - the switch is switched off
+
+##Andrew's Cross
+"on" - it blinks
+"off" - it does not blink
+
+### Passive component naming
+You can assign names to passive components using the Passive Component Naming tool.
+Once you set a name for any component, you can reference it by that name in the getstate() and setstate() functions, like this:
+(Imagine a signal that you have named "Stn_P1_out" at position (1,2,3) )
+setstate("Stn_P1_out", "green") instead of setstate(POS(1,2,3), "green")
+This way, you don't need to memorize positions.
+
+PC-Naming can also be used to name interlocking signals for route setting via the set_route() functions. IMPORTANT: The "Signal Name" set in the
+signalling formspec is completely independent and can NOT be used to look up the position, you need to explicitly use the PCNaming tool.
+
+--TODO: Ein paar mehr Codebeispiele wären schön, insbesondere mit os.date und so...
diff --git a/advtrains/advtrains_luaautomation/active_common.lua b/advtrains/advtrains_luaautomation/active_common.lua
new file mode 100644
index 0000000..dac81b3
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/active_common.lua
@@ -0,0 +1,175 @@
+
+
+local ac = {nodes={}}
+
+function ac.load(data)
+ if data then
+ ac.nodes=data.nodes
+ end
+end
+function ac.save()
+ return {nodes = ac.nodes}
+end
+
+function ac.after_place_node(pos, player)
+ local meta=minetest.get_meta(pos)
+ meta:set_string("formspec", ac.getform(pos, meta))
+ meta:set_string("infotext", "LuaAutomation component, unconfigured.")
+ local ph=minetest.pos_to_string(pos)
+ --just get first available key!
+ for en,_ in pairs(atlatc.envs) do
+ ac.nodes[ph]={env=en}
+ return
+ end
+end
+function ac.getform(pos, meta_p)
+ local meta = meta_p or minetest.get_meta(pos)
+ local envs_asvalues={}
+
+ local ph=minetest.pos_to_string(pos)
+ local nodetbl = ac.nodes[ph]
+ local env, code, err = nil, "", ""
+ if nodetbl then
+ code=nodetbl.code or ""
+ err=nodetbl.err or ""
+ env=nodetbl.env or ""
+ end
+ local sel = 1
+ for n,_ in pairs(atlatc.envs) do
+ envs_asvalues[#envs_asvalues+1]=minetest.formspec_escape(n)
+ if n==env then
+ sel=#envs_asvalues
+ end
+ end
+ local form = "size[10,10]dropdown[0,0;3;env;"..table.concat(envs_asvalues, ",")..";"..sel.."]"
+ .."button[4,0;2,1;save;Save]button[7,0;2,1;cle;Clear local env] textarea[0.2,1;10,10;code;Code;"..minetest.formspec_escape(code).."]"
+ .."label[0,9.8;"..err.."]"
+ return form
+end
+
+function ac.after_dig_node(pos, node, player)
+ advtrains.invalidate_all_paths(pos)
+ advtrains.ndb.clear(pos)
+ local ph=minetest.pos_to_string(pos)
+ ac.nodes[ph]=nil
+end
+
+function ac.on_receive_fields(pos, formname, fields, player)
+ if not minetest.check_player_privs(player:get_player_name(), {atlatc=true}) then
+ minetest.chat_send_player(player:get_player_name(), "Missing privilege: atlatc - Operation cancelled!")
+ return
+ end
+
+ local meta=minetest.get_meta(pos)
+ local ph=minetest.pos_to_string(pos)
+ local nodetbl = ac.nodes[ph] or {}
+ --if fields.quit then return end
+ if fields.env then
+ nodetbl.env=fields.env
+ end
+ if fields.code then
+ nodetbl.code=fields.code
+ end
+ if fields.save then
+ -- reset certain things
+ nodetbl.err=nil
+ if advtrains.lines and advtrains.lines.sched then
+ -- discard all schedules for this node
+ advtrains.lines.sched.discard_all(advtrains.encode_pos(pos))
+ end
+ end
+ if fields.cle then
+ nodetbl.data={}
+ end
+
+ ac.nodes[ph]=nodetbl
+
+ meta:set_string("formspec", ac.getform(pos, meta))
+ if nodetbl.env then
+ meta:set_string("infotext", "LuaAutomation component, assigned to environment '"..nodetbl.env.."'")
+ else
+ meta:set_string("infotext", "LuaAutomation component, invalid enviroment set!")
+ end
+end
+
+function ac.run_in_env(pos, evtdata, customfct_p)
+ local ph=minetest.pos_to_string(pos)
+ local nodetbl = ac.nodes[ph]
+ if not nodetbl then
+ atwarn("LuaAutomation component at",ph,": Data not in memory! Please visit component and click 'Save'!")
+ return
+ end
+
+ local meta
+ if minetest.get_node_or_nil(pos) then
+ meta=minetest.get_meta(pos)
+ end
+
+ if not nodetbl.env or not atlatc.envs[nodetbl.env] then
+ atwarn("LuaAutomation component at",ph,": Not an existing environment: "..(nodetbl.env or "<nil>"))
+ return false
+ end
+ if not nodetbl.code or nodetbl.code=="" then
+ atwarn("LuaAutomation component at",ph,": No code to run! (insert -- to suppress warning)")
+ return false
+ 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, msg=imesg}) --Compatiblity "message" field.
+ end
+ -- add digiline_send function, if digiline is loaded
+ if minetest.global_exists("digiline") then
+ customfct.digiline_send=function(channel, msg)
+ assertt(channel, "string")
+ digiline:receptor_send(pos, digiline.rules.default, channel, msg)
+ end
+ end
+ -- add lines scheduler if enabled
+ if advtrains.lines and advtrains.lines.sched then
+ customfct.schedule = function(rwtime, msg)
+ return advtrains.lines.sched.enqueue(rwtime, "atlatc_env", {pos=pos, msg=msg}, advtrains.encode_pos(pos), 1)
+ end
+ customfct.schedule_in = function(rwtime, msg)
+ return advtrains.lines.sched.enqueue_in(rwtime, "atlatc_env", {pos=pos, msg=msg}, advtrains.encode_pos(pos), 1)
+ end
+ end
+
+ local datain=nodetbl.data or {}
+ local succ, dataout = atlatc.envs[nodetbl.env]:execute_code(datain, nodetbl.code, evtdata, customfct)
+ if succ then
+ atlatc.active.nodes[ph].data=atlatc.remove_invalid_data(dataout)
+ else
+ atlatc.active.nodes[ph].err=dataout
+ atwarn("LuaAutomation ATC interface rail at",ph,": LUA Error:",dataout)
+ if meta then
+ meta:set_string("infotext", "LuaAutomation ATC interface rail, ERROR:"..dataout)
+ end
+ end
+ if meta then
+ meta:set_string("formspec", ac.getform(pos, meta))
+ 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
+
+if advtrains.lines and advtrains.lines.sched then
+ advtrains.lines.sched.register_callback("atlatc_env", function(data)
+ -- This adds another interrupt to the atlatc queue... there might be a better way
+ atlatc.interrupt.add(0, data.pos, {type="schedule",schedule=true, msg=data.msg})
+ end)
+end
+
+ac.trackdef_advtrains_defs = {
+ on_train_enter = function(pos, train_id)
+ --do async. Event is fired in train steps
+ atlatc.interrupt.add(0, pos, {type="train", train=true, id=train_id})
+ end,
+}
+
+atlatc.active=ac
diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua
new file mode 100644
index 0000000..6083dfc
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/atc_rail.lua
@@ -0,0 +1,194 @@
+-- atc_rail.lua
+-- registers and handles the ATC rail. Active component.
+-- This is the only component that can interface with trains, so train interface goes here too.
+
+--Using subtable
+local r={}
+
+function r.fire_event(pos, evtdata)
+
+ local ph=minetest.pos_to_string(pos)
+ local railtbl = atlatc.active.nodes[ph]
+
+ if not railtbl then
+ atwarn("LuaAutomation ATC interface rail at",ph,": Data not in memory! Please visit position and click 'Save'!")
+ return
+ end
+
+
+ local arrowconn = railtbl.arrowconn
+ if not arrowconn then
+ atwarn("LuaAutomation ATC interface rail at",ph,": Incomplete Data! Please visit position and click 'Save'!")
+ return
+ end
+
+ --prepare ingame API for ATC. Regenerate each time since pos needs to be known
+ --If no train, then return false.
+ local train_id=advtrains.get_train_at_pos(pos)
+ local train, atc_arrow, tvel
+ if train_id then train=advtrains.trains[train_id] end
+ if train then
+ if not train.path then
+ --we happened to get in between an invalidation step
+ --delay
+ atlatc.interrupt.add(0,pos,evtdata)
+ return
+ end
+ local index = advtrains.path_lookup(train, pos)
+
+ local iconnid = 1
+ if index then
+ iconnid = train.path_cn[index]
+ else
+ atwarn("ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!")
+ end
+ atc_arrow = iconnid == 1
+
+ tvel=train.velocity
+ end
+ local customfct={
+ atc_send = function(cmd)
+ if not train_id then return false end
+ assertt(cmd, "string")
+ advtrains.atc.train_set_command(train, cmd, atc_arrow)
+ return true
+ end,
+ split_at_index = function(index, cmd)
+ if not train_id then return false end
+ assertt(cmd, "string")
+ if type(index) ~= "number" then
+ return false
+ end
+ local new_id = advtrains.split_train_at_index(train, index)
+ if new_id then
+ minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow)
+ return true
+ end
+ return false
+ end,
+ split_at_fc = function(cmd, len)
+ assertt(cmd, "string")
+ if not train_id then return false end
+ local new_id, fc = advtrains.split_train_at_fc(train, false, len)
+ if new_id then
+ minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow)
+ end
+ return fc or ""
+ end,
+ split_off_locomotive = function(cmd, len)
+ assertt(cmd, "string")
+ if not train_id then return false end
+ local new_id, fc = advtrains.split_train_at_fc(train, true, len)
+ if new_id then
+ minetest.after(1,advtrains.atc.train_set_command,advtrains.trains[new_id], cmd, atc_arrow)
+ end
+ end,
+ train_length = function ()
+ if not train_id then return false end
+ return #train.trainparts
+ end,
+ step_fc = function()
+ if not train_id then return false end
+ advtrains.train_step_fc(train)
+ end,
+ set_shunt = function()
+ if not train_id then return false end
+ train.autocouple = true
+ end,
+ unset_shunt = function()
+ if not train_id then return false end
+ train.autocouple = nil
+ end,
+ set_autocouple = function ()
+ if not train_id then return false end
+ train.autocouple = true
+ end,
+ unset_autocouple = function ()
+ if not train_id then return false end
+ train.autocouple = nil
+ end,
+ set_line = function(line)
+ if type(line)~="string" and type(line)~="number" then
+ return false
+ end
+ train.line = line .. ""
+ minetest.after(0, advtrains.invalidate_path, train_id)
+ return true
+ end,
+ get_line = function()
+ return train.line
+ end,
+ set_rc = function(rc)
+ if type(rc)~="string"then
+ return false
+ end
+ train.routingcode = rc
+ minetest.after(0, advtrains.invalidate_path, train_id)
+ return true
+ end,
+ get_rc = function()
+ return train.routingcode
+ end,
+ atc_reset = function(cmd)
+ if not train_id then return false end
+ assertt(cmd, "string")
+ advtrains.atc.train_reset_command(train)
+ return true
+ end,
+ atc_arrow = atc_arrow,
+ atc_id = train_id,
+ atc_speed = tvel,
+ atc_set_text_outside = function(text)
+ if not train_id then return false end
+ if text then assertt(text, "string") end
+ advtrains.trains[train_id].text_outside=text
+ return true
+ end,
+ atc_set_text_inside = function(text)
+ if not train_id then return false end
+ if text then assertt(text, "string") end
+ advtrains.trains[train_id].text_inside=text
+ return true
+ end,
+ }
+
+ atlatc.active.run_in_env(pos, evtdata, customfct)
+
+end
+
+if minetest.get_modpath("advtrains_train_track") ~= nil then
+ advtrains.register_tracks("default", {
+ nodename_prefix="advtrains_luaautomation:dtrack",
+ texture_prefix="advtrains_dtrack_atc",
+ models_prefix="advtrains_dtrack",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_shared_atc.png",
+ description=atltrans("LuaAutomation ATC Rail"),
+ formats={},
+ get_additional_definiton = function(def, preset, suffix, rotation)
+ return {
+ after_place_node = atlatc.active.after_place_node,
+ after_dig_node = atlatc.active.after_dig_node,
+ on_receive_fields = function(pos, ...)
+ atlatc.active.on_receive_fields(pos, ...)
+ --set arrowconn (for ATC)
+ local ph=minetest.pos_to_string(pos)
+ local _, conns=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
+ atlatc.active.nodes[ph].arrowconn=conns[1].c
+ end,
+ advtrains = atlatc.active.trackdef_advtrains_defs,
+ luaautomation = {
+ fire_event=r.fire_event
+ },
+ digiline = {
+ receptor = {},
+ effector = {
+ action = atlatc.active.on_digiline_receive
+ },
+ },
+ }
+ end,
+ }, advtrains.trackpresets.t_30deg_straightonly)
+end
+
+atlatc.rail = r
diff --git a/advtrains/advtrains_luaautomation/chatcmds.lua b/advtrains/advtrains_luaautomation/chatcmds.lua
new file mode 100644
index 0000000..2d0c69d
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/chatcmds.lua
@@ -0,0 +1,84 @@
+--chatcmds.lua
+--Registers commands to modify the init and step code for LuaAutomation
+
+--position helper.
+--punching a node will result in that position being saved and inserted into a text field on the top of init form.
+local punchpos={}
+
+minetest.register_on_punchnode(function(pos, node, player, pointed_thing)
+ local pname=player:get_player_name()
+ punchpos[pname]=pos
+end)
+
+local function get_init_form(env, pname)
+ local err = env.init_err or ""
+ local code = env.init_code or ""
+ local ppos=punchpos[pname]
+ local pp=""
+ if ppos then
+ pp="POS"..minetest.pos_to_string(ppos)
+ end
+ local form = "size[10,10]button[0,0;2,1;run;Run InitCode]button[2,0;2,1;cls;Clear S]"
+ .."button[4,0;2,1;save;Save] button[6,0;2,1;del;Delete Env.] field[8.1,0.5;2,1;punchpos;Last punched position;"..pp.."]"
+ .."textarea[0.2,1;10,10;code;Environment initialization code;"..minetest.formspec_escape(code).."]"
+ .."label[0,9.8;"..err.."]"
+ return form
+end
+
+core.register_chatcommand("env_setup", {
+ params = "<environment name>",
+ description = "Set up and modify AdvTrains LuaAutomation environment",
+ privs = {atlatc=true},
+ func = function(name, param)
+ local env=atlatc.envs[param]
+ if not env then return false,"Invalid environment name!" end
+ minetest.show_formspec(name, "atlatc_envsetup_"..param, get_init_form(env, name))
+ return true
+ end,
+})
+
+core.register_chatcommand("env_create", {
+ params = "<environment name>",
+ description = "Create an AdvTrains LuaAutomation environment",
+ privs = {atlatc=true},
+ func = function(name, param)
+ if not param or param=="" then return false, "Name required!" end
+ if atlatc.envs[param] then return false, "Environment already exists!" end
+ atlatc.envs[param] = atlatc.env_new(param)
+ return true, "Created environment '"..param.."'. Use '/env_setup "..param.."' to define global initialization code, or start building LuaATC components!"
+ end,
+})
+
+
+minetest.register_on_player_receive_fields(function(player, formname, fields)
+
+ local pname=player:get_player_name()
+ if not minetest.check_player_privs(pname, {atlatc=true}) then return end
+
+ local envname=string.match(formname, "^atlatc_delconfirm_(.+)$")
+ if envname and fields.sure=="YES" then
+ atlatc.envs[envname]=nil
+ minetest.chat_send_player(pname, "Environment deleted!")
+ return
+ end
+
+ envname=string.match(formname, "^atlatc_envsetup_(.+)$")
+ if not envname then return end
+
+ local env=atlatc.envs[envname]
+ if not env then return end
+
+ if fields.del then
+ minetest.show_formspec(pname, "atlatc_delconfirm_"..envname, "field[sure;"..minetest.formspec_escape("SURE TO DELETE ENVIRONMENT "..envname.."? Type YES (all uppercase) to continue or just quit form to cancel.")..";]")
+ return
+ end
+
+ env.init_err=nil
+ if fields.code then
+ env.init_code=fields.code
+ end
+ if fields.run then
+ env:run_initcode()
+ minetest.show_formspec(pname, formname, get_init_form(env, pname))
+ end
+end)
diff --git a/advtrains/advtrains_luaautomation/depends.txt b/advtrains/advtrains_luaautomation/depends.txt
new file mode 100644
index 0000000..d5523e1
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/depends.txt
@@ -0,0 +1,4 @@
+advtrains
+advtrains_interlocking?
+advtrains_line_automation?
+mesecons_switch? \ No newline at end of file
diff --git a/advtrains/advtrains_luaautomation/environment.lua b/advtrains/advtrains_luaautomation/environment.lua
new file mode 100644
index 0000000..3e7787b
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/environment.lua
@@ -0,0 +1,362 @@
+-------------
+-- lua sandboxed environment
+
+-- function to cross out functions and userdata.
+-- modified from dump()
+function atlatc.remove_invalid_data(o, nested)
+ if o==nil then return nil end
+ local valid_dt={["nil"]=true, boolean=true, number=true, string=true}
+ if type(o) ~= "table" then
+ --check valid data type
+ if not valid_dt[type(o)] then
+ return nil
+ end
+ return o
+ end
+ -- Contains table -> true/nil of currently nested tables
+ nested = nested or {}
+ if nested[o] then
+ return nil
+ end
+ nested[o] = true
+ for k, v in pairs(o) do
+ v = atlatc.remove_invalid_data(v, nested)
+ end
+ nested[o] = nil
+ return o
+end
+
+
+local env_proto={
+ load = function(self, envname, data)
+ self.name=envname
+ self.sdata=data.sdata and atlatc.remove_invalid_data(data.sdata) or {}
+ self.fdata={}
+ self.init_code=data.init_code or ""
+ self.step_code=data.step_code or ""
+ end,
+ save = function(self)
+ -- throw any function values out of the sdata table
+ self.sdata = atlatc.remove_invalid_data(self.sdata)
+ return {sdata = self.sdata, init_code=self.init_code, step_code=self.step_code}
+ end,
+}
+
+--Environment
+--Code modified from mesecons_luacontroller (credit goes to Jeija and mesecons contributors)
+
+local safe_globals = {
+ "assert", "error", "ipairs", "next", "pairs", "select",
+ "tonumber", "tostring", "type", "unpack", "_VERSION"
+}
+
+--print is actually minetest.chat_send_all()
+--using advtrains.print_concat_table because it's cool
+local function safe_print(t, ...)
+ local str=advtrains.print_concat_table({t, ...})
+ minetest.log("action", "[atlatc] "..str)
+ minetest.chat_send_all(str)
+end
+
+local function safe_date(f, t)
+ if not f then
+ -- fall back to old behavior
+ return(os.date("*t",os.time()))
+ else
+ --pass parameters
+ return os.date(f,t)
+ end
+end
+
+-- string.rep(str, n) with a high value for n can be used to DoS
+-- the server. Therefore, limit max. length of generated string.
+local function safe_string_rep(str, n)
+ if #str * n > 2000 then
+ debug.sethook() -- Clear hook
+ error("string.rep: string length overflow", 2)
+ end
+
+ return string.rep(str, n)
+end
+
+-- string.find with a pattern can be used to DoS the server.
+-- Therefore, limit string.find to patternless matching.
+-- Note: Disabled security since there are enough security leaks and this would be unneccessary anyway to DoS the server
+local function safe_string_find(...)
+ --if (select(4, ...)) ~= true then
+ -- debug.sethook() -- Clear hook
+ -- error("string.find: 'plain' (fourth parameter) must always be true for security reasons.")
+ --end
+
+ return string.find(...)
+end
+
+local mp=minetest.get_modpath("advtrains_luaautomation")
+
+local static_env = {
+ --core LUA functions
+ print = safe_print,
+ string = {
+ byte = string.byte,
+ char = string.char,
+ format = string.format,
+ len = string.len,
+ lower = string.lower,
+ upper = string.upper,
+ rep = safe_string_rep,
+ reverse = string.reverse,
+ sub = string.sub,
+ find = safe_string_find,
+ },
+ math = {
+ abs = math.abs,
+ acos = math.acos,
+ asin = math.asin,
+ atan = math.atan,
+ atan2 = math.atan2,
+ ceil = math.ceil,
+ cos = math.cos,
+ cosh = math.cosh,
+ deg = math.deg,
+ exp = math.exp,
+ floor = math.floor,
+ fmod = math.fmod,
+ frexp = math.frexp,
+ huge = math.huge,
+ ldexp = math.ldexp,
+ log = math.log,
+ log10 = math.log10,
+ max = math.max,
+ min = math.min,
+ modf = math.modf,
+ pi = math.pi,
+ pow = math.pow,
+ rad = math.rad,
+ random = math.random,
+ sin = math.sin,
+ sinh = math.sinh,
+ sqrt = math.sqrt,
+ tan = math.tan,
+ tanh = math.tanh,
+ },
+ table = {
+ concat = table.concat,
+ insert = table.insert,
+ maxn = table.maxn,
+ remove = table.remove,
+ sort = table.sort,
+ },
+ os = {
+ clock = os.clock,
+ difftime = os.difftime,
+ time = os.time,
+ date = safe_date,
+ },
+ POS = function(x,y,z) return {x=x, y=y, z=z} end,
+ getstate = advtrains.getstate,
+ setstate = advtrains.setstate,
+ is_passive = advtrains.is_passive,
+ --interrupts are handled per node, position unknown. (same goes for digilines)
+ --however external interrupts can be set here.
+ interrupt_pos = function(parpos, imesg)
+ local pos=atlatc.pcnaming.resolve_pos(parpos)
+ atlatc.interrupt.add(0, pos, {type="ext_int", ext_int=true, message=imesg})
+ end,
+}
+
+-- If interlocking is present, enable route setting functions
+if advtrains.interlocking then
+ local function gen_checks(signal, route_name, noroutesearch)
+ assertt(route_name, "string")
+ local pos = atlatc.pcnaming.resolve_pos(signal)
+ local sigd = advtrains.interlocking.db.get_sigd_for_signal(pos)
+ if not sigd then
+ error("There's no signal at "..minetest.pos_to_string(pos))
+ end
+ local tcbs = advtrains.interlocking.db.get_tcbs(sigd)
+ if not tcbs then
+ error("Inconsistent configuration, no tcbs for signal at "..minetest.pos_to_string(pos))
+ end
+
+ local routeid, route
+ if not noroutesearch then
+ for routeidt, routet in ipairs(tcbs.routes) do
+ if routet.name == route_name then
+ routeid = routeidt
+ route = routet
+ break
+ end
+ end
+ if not route then
+ error("No route called "..route_name.." at "..minetest.pos_to_string(pos))
+ end
+ end
+ return pos, sigd, tcbs, routeid, route
+ end
+
+
+ static_env.can_set_route = function(signal, route_name)
+ local pos, sigd, tcbs, routeid, route = gen_checks(signal, route_name)
+ -- if route is already set on signal, return whether it's committed
+ if tcbs.routeset == routeid then
+ return tcbs.route_committed
+ end
+ -- actually try setting route (parameter 'true' designates try-run
+ local ok = advtrains.interlocking.route.set_route(sigd, route, true)
+ return ok
+ end
+ static_env.set_route = function(signal, route_name)
+ local pos, sigd, tcbs, routeid, route = gen_checks(signal, route_name)
+ return advtrains.interlocking.route.update_route(sigd, tcbs, routeid)
+ end
+ static_env.cancel_route = function(signal)
+ local pos, sigd, tcbs, routeid, route = gen_checks(signal, "", true)
+ return advtrains.interlocking.route.update_route(sigd, tcbs, nil, true)
+ end
+ static_env.get_aspect = function(signal)
+ local pos = atlatc.pcnaming.resolve_pos(signal)
+ return advtrains.interlocking.signal_get_aspect(pos)
+ end
+ static_env.set_aspect = function(signal, asp)
+ local pos = atlatc.pcnaming.resolve_pos(signal)
+ return advtrains.interlocking.signal_set_aspect(pos)
+ end
+end
+
+-- Lines-specific:
+if advtrains.lines then
+ local atlrwt = advtrains.lines.rwt
+ static_env.rwt = {
+ now = atlrwt.now,
+ new = atlrwt.new,
+ copy = atlrwt.copy,
+ to_table = atlrwt.to_table,
+ to_secs = atlrwt.to_secs,
+ to_string = atlrwt.to_string,
+ add = atlrwt.add,
+ diff = atlrwt.diff,
+ sub = atlrwt.sub,
+ adj_diff = atlrwt.adj_diff,
+ adjust_cycle = atlrwt.adjust_cycle,
+ adjust = atlrwt.adjust,
+ to_string = atlrwt.to_string,
+ get_time_until = atlrwt.get_time_until,
+ next_rpt = atlrwt.next_rpt,
+ last_rpt = atlrwt.last_rpt,
+ time_from_last_rpt = atlrwt.time_from_last_rpt,
+ time_to_next_rpt = atlrwt.time_to_next_rpt,
+ }
+end
+
+for _, name in pairs(safe_globals) do
+ static_env[name] = _G[name]
+end
+
+
+--The environment all code calls get is a table that has set static_env as metatable.
+--In general, every variable is local to a single code chunk, but kept persistent over code re-runs. Data is also saved, but functions and userdata and circular references are removed
+--Init code and step code's environments are not saved
+-- S - Table that can contain any save data global to the environment. Will be saved statically. Can't contain functions or userdata or circular references.
+-- F - Table global to the environment, can contain volatile data that is deleted when server quits.
+-- The init code should populate this table with functions and other definitions.
+
+local proxy_env={}
+--proxy_env gets a new metatable in every run, but is the shared environment of all functions ever defined.
+
+-- returns: true, fenv if successful; nil, error if error
+function env_proto:execute_code(localenv, code, evtdata, customfct)
+ local metatbl ={
+ __index = function(t, i)
+ if i=="S" then
+ return self.sdata
+ elseif i=="F" then
+ return self.fdata
+ elseif i=="event" then
+ return evtdata
+ elseif customfct and customfct[i] then
+ return customfct[i]
+ elseif localenv and localenv[i] then
+ return localenv[i]
+ end
+ return static_env[i]
+ end,
+ __newindex = function(t, i, v)
+ if i=="S" or i=="F" or i=="event" or (customfct and customfct[i]) or static_env[i] then
+ debug.sethook()
+ error("Trying to overwrite environment contents")
+ end
+ localenv[i]=v
+ end,
+ }
+ setmetatable(proxy_env, metatbl)
+ local fun, err=loadstring(code)
+ if not fun then
+ return false, err
+ end
+
+ setfenv(fun, proxy_env)
+ local succ, data = pcall(fun)
+ if succ then
+ data=localenv
+ end
+ return succ, data
+end
+
+function env_proto:run_initcode()
+ if self.init_code and self.init_code~="" then
+ local old_fdata=self.fdata
+ self.fdata = {}
+ atprint("[atlatc]Running initialization code for environment '"..self.name.."'")
+ local succ, err = self:execute_code({}, self.init_code, {type="init", init=true})
+ if not succ then
+ atwarn("[atlatc]Executing InitCode for '"..self.name.."' failed:"..err)
+ self.init_err=err
+ if old_fdata then
+ self.fdata=old_fdata
+ atwarn("[atlatc]The 'F' table has been restored to the previous state.")
+ end
+ end
+ end
+end
+function env_proto:run_stepcode()
+ if self.step_code and self.step_code~="" then
+ local succ, err = self:execute_code({}, self.step_code, nil, {})
+ if not succ then
+ --TODO
+ end
+ end
+end
+
+--- class interface
+
+function atlatc.env_new(name)
+ local newenv={
+ name=name,
+ init_code="",
+ step_code="",
+ sdata={}
+ }
+ setmetatable(newenv, {__index=env_proto})
+ return newenv
+end
+function atlatc.env_load(name, data)
+ local newenv={}
+ setmetatable(newenv, {__index=env_proto})
+ newenv:load(name, data)
+ return newenv
+end
+
+function atlatc.run_initcode()
+ for envname, env in pairs(atlatc.envs) do
+ env:run_initcode()
+ end
+end
+function atlatc.run_stepcode()
+ for envname, env in pairs(atlatc.envs) do
+ env:run_stepcode()
+ end
+end
+
+
+
+
diff --git a/advtrains/advtrains_luaautomation/init.lua b/advtrains/advtrains_luaautomation/init.lua
new file mode 100644
index 0000000..eca5633
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/init.lua
@@ -0,0 +1,109 @@
+-- advtrains_luaautomation/init.lua
+-- Lua automation features for advtrains
+-- Uses global table 'atlatc' (AdvTrains_LuaATC)
+
+-- Boilerplate to support localized strings if intllib mod is installed.
+if intllib then
+ atltrans = intllib.Getter()
+else
+ atltrans = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
+end
+
+--Privilege
+--Only trusted players should be enabled to build stuff which can break the server.
+
+atlatc = { envs = {}}
+
+minetest.register_privilege("atlatc", { description = "Player can place and modify LUA ATC components. Grant with care! Allows to execute bad LUA code.", give_to_singleplayer = false, default= false })
+
+--assertt helper. error if a variable is not of a type
+function assertt(var, typ)
+ if type(var)~=typ then
+ error("Assertion failed, variable has to be of type "..typ)
+ end
+end
+
+local mp=minetest.get_modpath("advtrains_luaautomation")
+if not mp then
+ error("Mod name error: Mod folder is not named 'advtrains_luaautomation'!")
+end
+dofile(mp.."/environment.lua")
+dofile(mp.."/interrupt.lua")
+dofile(mp.."/active_common.lua")
+dofile(mp.."/atc_rail.lua")
+dofile(mp.."/operation_panel.lua")
+dofile(mp.."/pcnaming.lua")
+
+dofile(mp.."/chatcmds.lua")
+
+
+local filename=minetest.get_worldpath().."/advtrains_luaautomation"
+
+function atlatc.load()
+ local file, err = io.open(filename, "r")
+ if not file then
+ minetest.log("warning", " Failed to read advtrains_luaautomation save data from file "..filename..": "..(err or "Unknown Error"))
+ minetest.log("warning", " (this is normal when first enabling advtrains on this world)")
+ else
+ atprint("luaautomation reading file:",filename)
+ local tbl = minetest.deserialize(file:read("*a"))
+ if type(tbl) == "table" then
+ if tbl.version==1 then
+ for envname, data in pairs(tbl.envs) do
+ atlatc.envs[envname]=atlatc.env_load(envname, data)
+ end
+ atlatc.active.load(tbl.active)
+ atlatc.interrupt.load(tbl.interrupt)
+ atlatc.pcnaming.load(tbl.pcnaming)
+ end
+ else
+ minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!")
+ end
+ file:close()
+ end
+ -- run init code of all environments
+ atlatc.run_initcode()
+end
+
+
+atlatc.save = function()
+ --versions:
+ -- 1 - Initial save format.
+
+ local envdata={}
+ for envname, env in pairs(atlatc.envs) do
+ envdata[envname]=env:save()
+ end
+ local save_tbl={
+ version = 1,
+ envs=envdata,
+ active = atlatc.active.save(),
+ interrupt = atlatc.interrupt.save(),
+ pcnaming = atlatc.pcnaming.save(),
+ }
+
+ local datastr = minetest.serialize(save_tbl)
+ if not datastr then
+ minetest.log("error", " Failed to save advtrains_luaautomation save data to file "..filename..": Can't serialize!")
+ return
+ end
+ local file, err = io.open(filename, "w")
+ if err then
+ minetest.log("error", " Failed to save advtrains_luaautomation save data to file "..filename..": "..(err or "Unknown Error"))
+ return
+ end
+ file:write(datastr)
+ file:close()
+end
+
+
+-- globalstep for step code
+local timer, step_int=0, 2
+
+function atlatc.mainloop_stepcode(dtime)
+ timer=timer+dtime
+ if timer>step_int then
+ timer=0
+ atlatc.run_stepcode()
+ end
+end
diff --git a/advtrains/advtrains_luaautomation/interrupt.lua b/advtrains/advtrains_luaautomation/interrupt.lua
new file mode 100644
index 0000000..525c3b4
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/interrupt.lua
@@ -0,0 +1,48 @@
+-- interrupt.lua
+-- implements interrupt queue
+
+--to be saved: pos and evtdata
+local iq={}
+local queue={}
+local timer=0
+local run=false
+
+function iq.load(data)
+ local d=data or {}
+ queue = d.queue or {}
+ timer = d.timer or 0
+end
+function iq.save()
+ return {queue = queue, timer=timer}
+end
+
+function iq.add(t, pos, evtdata)
+ queue[#queue+1]={t=t+timer, p=pos, e=evtdata}
+ run=true
+end
+
+function iq.mainloop(dtime)
+ timer=timer + math.min(dtime, 0.2)
+ for i=1,#queue do
+ local qe=queue[i]
+ if not qe then
+ table.remove(queue, i)
+ i=i-1
+ elseif timer>qe.t then
+ local pos, evtdata=queue[i].p, queue[i].e
+ local node=advtrains.ndb.get_node(pos)
+ local ndef=minetest.registered_nodes[node.name]
+ if ndef and ndef.luaautomation and ndef.luaautomation.fire_event then
+ ndef.luaautomation.fire_event(pos, evtdata)
+ else
+ atwarn("[atlatc][interrupt] Couldn't run event",evtdata.type,"on",pos,", something wrong with the node",node)
+ end
+ table.remove(queue, i)
+ i=i-1
+ end
+ end
+end
+
+
+
+atlatc.interrupt=iq
diff --git a/advtrains/advtrains_luaautomation/operation_panel.lua b/advtrains/advtrains_luaautomation/operation_panel.lua
new file mode 100644
index 0000000..f8b93b5
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/operation_panel.lua
@@ -0,0 +1,28 @@
+
+local function on_punch(pos, player)
+ atlatc.interrupt.add(0, pos, {type="punch", punch=true})
+end
+
+
+minetest.register_node("advtrains_luaautomation:oppanel", {
+ drawtype = "normal",
+ tiles={"atlatc_oppanel.png"},
+ description = "LuaAutomation operation panel",
+ groups = {
+ cracky = 1,
+ save_in_at_nodedb=1,
+ },
+ after_place_node = atlatc.active.after_place_node,
+ after_dig_node = atlatc.active.after_dig_node,
+ on_receive_fields = atlatc.active.on_receive_fields,
+ 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/advtrains_luaautomation/p_display.lua b/advtrains/advtrains_luaautomation/p_display.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/p_display.lua
diff --git a/advtrains/advtrains_luaautomation/passive_api.txt b/advtrains/advtrains_luaautomation/passive_api.txt
new file mode 100644
index 0000000..9852e94
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/passive_api.txt
@@ -0,0 +1,24 @@
+Lua Automation - Passive Component API
+
+Passive components are nodes that do not have code running in them. However, active components can query these and request actions from them. Examples:
+Switches
+Signals
+Displays
+Mesecon Transmitter
+Those passive components can also be used inside interlocking systems.
+
+All passive components have a table called 'advtrains' in their node definition and have the group 'save_in_nodedb' set, so they work in unloaded chunks.
+Example for a switch:
+advtrains = {
+ getstate = function(pos, node)
+ return "st"
+ end,
+ -- OR
+ getstate = "st",
+
+ setstate = function(pos, node, newstate)
+ if newstate=="cr" then
+ advtrains.ndb.swap_node(pos, <corresponding switch alt>)
+ end
+ end
+} \ No newline at end of file
diff --git a/advtrains/advtrains_luaautomation/pcnaming.lua b/advtrains/advtrains_luaautomation/pcnaming.lua
new file mode 100644
index 0000000..ebb769f
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/pcnaming.lua
@@ -0,0 +1,76 @@
+--pcnaming.lua
+--a.k.a Passive component naming
+--Allows to assign names to passive components, so they can be called like:
+--setstate("iamasignal", "green")
+atlatc.pcnaming={name_map={}}
+function atlatc.pcnaming.load(stuff)
+ if type(stuff)=="table" then
+ atlatc.pcnaming.name_map=stuff
+ end
+end
+function atlatc.pcnaming.save()
+ return atlatc.pcnaming.name_map
+end
+
+function atlatc.pcnaming.resolve_pos(pos, func_name)
+ if type(pos)=="string" then
+ local e = atlatc.pcnaming.name_map[pos]
+ if e then return e end
+ elseif type(pos)=="table" and pos.x and pos.y and pos.z then
+ return pos
+ end
+ error("Invalid position supplied to " .. (func_name or "???")..": " .. dump(pos))
+end
+
+minetest.register_craftitem("advtrains_luaautomation:pcnaming",{
+ description = attrans("Passive Component Naming Tool\n\nRight-click to name a passive component."),
+ groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
+ inventory_image = "atlatc_pcnaming.png",
+ wield_image = "atlatc_pcnaming.png",
+ stack_max = 1,
+ on_place = function(itemstack, placer, pointed_thing)
+ local pname = placer:get_player_name()
+ if not pname then
+ return
+ end
+ if not minetest.check_player_privs(pname, {atlatc=true}) then
+ minetest.chat_send_player(pname, "Missing privilege: atlatc")
+ return
+ end
+ if pointed_thing.type=="node" then
+ local pos=pointed_thing.under
+ if advtrains.is_protected(pos, pname) then
+ minetest.record_protection_violation(pos, pname)
+ return
+ end
+ local node = advtrains.ndb.get_node(pos)
+ if node.name and (minetest.get_item_group(node.name, "advtrains_signal")>0 or advtrains.is_passive(pos)) then
+ --look if this one already has a name
+ local pn=""
+ for name, npos in pairs(atlatc.pcnaming.name_map) do
+ if vector.equals(npos, pos) then
+ pn=name
+ end
+ end
+ minetest.show_formspec(pname, "atlatc_naming_"..minetest.pos_to_string(pos), "field[pn;Set name of component (empty to clear);"..minetest.formspec_escape(pn).."]")
+ end
+ end
+ end,
+})
+minetest.register_on_player_receive_fields(function(player, formname, fields)
+ local pts=string.match(formname, "^atlatc_naming_(.+)")
+ if pts then
+ local pos=minetest.string_to_pos(pts)
+ if fields.pn then
+ --first remove all occurences
+ for name, npos in pairs(atlatc.pcnaming.name_map) do
+ if vector.equals(npos, pos) then
+ atlatc.pcnaming.name_map[name]=nil
+ end
+ end
+ if fields.pn~="" then
+ atlatc.pcnaming.name_map[fields.pn]=pos
+ end
+ end
+ end
+end)
diff --git a/advtrains/advtrains_luaautomation/textures/atlatc_oppanel.png b/advtrains/advtrains_luaautomation/textures/atlatc_oppanel.png
new file mode 100644
index 0000000..96eb30e
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/textures/atlatc_oppanel.png
Binary files differ
diff --git a/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png b/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png
new file mode 100644
index 0000000..3fccdfc
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png
Binary files differ