This is an old revision of the document!
The standard Lua globals are available in the LuaATC environment:
string
math
table
os
:os.clock()
os.difftime()
os.time()
os.date()
1)The standard Lua functions are available in the LuaATC environment:
assert
error
ipairs
pairs
next
select
tonumber
tostring
type
unpack
In the following functions, all parameters named pos
designate a position. You can use either:
{x=34, y=2, z=-18}
)POS(34,2,-18)
shorthand
A shorthand function to designate a Minetest position vector like {x=?, y=?, z=?}
.
Switches (turnouts), simple signals and mesecon switches are so-called “Passive Components”. These functions are used to interact with them.
Gets the state of a passive component at position pos
. The returned states are component-specific, see Passive Components.
Sets the state of a passive component at position pos
to the value new_state
. The states are component-specific, see Passive Components.
Checks whether there is a passive component at position pos
. If pos
is a string, checks whether this passive component name exists.
These functions allow to schedule interrupts, a.k.a events to be executed at a later time. They are not available in init code.
The time counter and queue handling these interrupts is synchronized to minetest's internal step time. It is written in a very simple fashion, and is NOT secured against “interrupt bombs”. Be careful!
-- an example for an "interrupt bomb" -- NEVER DO THIS! if event.int then interrupt(1,"A") interrupt(1,"B") -- run 1: {A,B} -- run 2: {A,B,A,B} -- run 3: {A,B,A,B,A,B,A,B}...
Causes the LuaAutomation mod to trigger an int
event (the Advtrains equivalent of Mesecons' interrupt
) on this component after the given time
, in seconds, with the specified message
. message
can be of any Lua data type.
Use of this function is discouraged, as “fork bombs” can easily be built. Please use interrupt_safe()
or make sure to clear_interrupts()
in appropriate places.
since version 2.3.0
Like interrupt()
, but returns false when an interrupt (of any type) is already present for this component and does not add an interrupt. Returns true when interrupt was successfully added.
Triggers immediately an ext_int
event on the active component at pos
. message
can be of any Lua data type.
since version 2.3.0
Removes any pending interrupts (both of type 'int' and 'ext_int') set on this node.
since version 2.3.0
In addition to atc_send()
described below, LuaATC provides the possibility to send ATC commands to any train in the world knowing its train ID, by means of the following function.
Sends the specified ATC command to the train specified by its 6-digit train ID and returns true
. If there is no train with such an ID, returns false
and does nothing.
The ATC command is instantly applied to the train in question, regardless of where it is and what it is doing. It also cancels any active ATC command of the train.
Calling this function from an ATC rail is independent of any train that stands on the rail and does not affect it (except of course when train_id happens to be the ID of the train on the rail).
When advtrains_line_automation
is enabled, all Railway time functions are exposed as rwt.* and can safely be used in LuaATC code.
For the available functions, see Railway Time API.
--Example: print the time of the next full minute local now = rwt.now() local next_minute = rwt.next_rpt(now, "01;00", 0)
This is a separate schedule queue. In contrast to the interrupt system, which is the original and established way to schedule interrupts, it relies on the Railway Time system and therefore is only accessible when advtrains_line_automation
is enabled.
There are two important considerations to this:
1
.Clicking “Save” on the code edit form clears all events currently scheduled in the RWT scheduler. It does NOT clear the interrupt scheduler events.
Triggers a schedule
event AT the specified Railway Time.
The time value here is an absolute value.
msg can be any data type and is accessible in event.msg
.
-- Example: schedule a "depart" event on the next full 5 minutes local now = rwt.now() local next_5minutes = rwt.next_rpt(now, "05;00", 0) schedule(next_5minutes, "depart")
Like schedule(), but the passed time is relative.
-- Example: schedule a "depart" event in 1 minute schedule_in("01;00", "depart")
Sends a digiline message on the specified channel
.
This function is not available in init code.
Interlocking functions are available when the advtrains_interlocking
mod is enabled.
Checks whether it is possible to set the route designated by route_name
from the signal at position pos
.
It emits a warning and halts execution of Lua code in the following cases:
LUA Error: …: There's no signal at (0,0,0)
pos
is a string, and the named passive component does not exist:LUA Error: …: Invalid position supplied to ???: “…”
LUA Error: …: No route called B at (0,0,0)
Requests the route designated by route_name
from the signal at position pos
. Has the same effect as clicking the “Set Route” in the formspec from the designated signal.
Same warnings apply as for can_set_route
.
If the route can't be set, the signal remains red and waits for conflicting problems to be solved. Execution continues immediately.
There is currently no way to emit an event when the signal becomes green.
Cancels the route that is set from the signal at position pos
. Has the same effect as clicking the “Cancel Route” in the formspec from the designated signal.
Same warnings apply as for can_set_route
.
If the route has already been canceled, nothing happens.
Gets the aspect of the signal at pos
. The aspect format is described in the Signal page.
Same warnings apply as for can_set_route
.
The following functions can only be used from LuaATC rails. They are used to control trains.
To control a train, this train must be positioned on the ATC rail. It does not matter which portion of the train is on the ATC rail, and whether the train is moving or not. 2)
Sends the specified ATC command to the train and returns true
. If there is no train, returns false
and does nothing.
Resets the train's current ATC command and returns true
. If there is no train, returns false
and does nothing.
Sets the text shown on the outside of the train and returns true
. If there is no train, returns false
and does nothing.
Sets the text shown inside the train and returns true
. If there is no train, returns false
and does nothing.
Gets the outside/inside text currently set on the train.
Returns the line property of the train, as a string. This string can be used to distinguish trains of different lines and route them properly.
This property is also used by the interlocking system for Automatic Routesetting.
If there is no train, the Lua program stored in the rail will exit immediately:
2019-01-01 15:00:00: WARNING[Server]: [advtrains]LuaAutomation ATC interface rail at (0,0,0) : LUA Error: …/mods/advtrains/advtrains_luaautomation/atc_rail.lua:93: attempt to index upvalue 'train' (a nil value)
This is a bug.
Sets the line property of the train, as a string.
On subway trains, in the Basic Trains modpack, the line is automatically displayed on the outside of the trains, if the first character is a number between 0 and 9 (where 0 is displayed as “Line 10”).
If there is no train, same behavior as get_line() applies.
Returns the routing code of the train, as a string. This property is used by the interlocking system for Automatic Routesetting.
If there is no train, same behavior as get_line() applies.
Sets the routing code of the train, as a string. This property is used by the interlocking system for Automatic Routesetting.
If there is no train, same behavior as get_line() applies.
Returns the number of wagons in a train.
Enables shunting mode for the currently passing train and returns true
. This mode permanently restricts the train speed to 6 m/s (or 21.6 km/h). When in shunt mode, the train will couple to trains it collides with, and will obey shunt signals.
Further details on the “Shunt Mode” are explained in the Interlocking section. (Missing Reference)
If there is no train, returns false
and does nothing.
since version 2.3.0
Enables (value == false
) or disables (value == true
) interlocking for this train. The train will not trigger automatic route setting on signals based on ARS.
This function has essentially the same effect as the ATC command A<enable_ARS>
.
since version 2.3.0
Adds a Temporary Speed Restriction at the current rail, so that the train is passing the rail at the specified speed
, or at a lower speed.
Calling this function from an approach
event has essentially the same effect as a Point Speed Restriction Rail. Note that due to internal implementation details, you must call this function again on any subsequent approach
events received. (see Events)
approach
eventsEnables Autocouple mode. When the train hits another standing train, it couples to it and continues with its current ATC target speed in the same direction.
In contrast to the Cpl
ATC command, the autocouple property does not reset after coupling. It needs to be explicitly disabled using unset_autocouple()
. Note that the ATC Cpl
command and the LuaATC autocouple flag are independent and can be used at the same time.
since version 2.4.0:
The train that is initiating the coupling (that is, the train that is driving and whose mode is set to autocouple) is guaranteed to retain its Train ID.
Disables the Autocouple mode.
The ID of the train passing the rail. nil
if no there is no train.
The current speed of the train passing the rail, in metres per second. nil
if no there is no train.
Whether the train is driving in direction of the arrows on the ATC rail. nil
if no there is no train.
Note: this code does not indicate whether there is a train on the rail, as both false
and nil
evaluate to false:
-- BAD if not atc_arrow then -- ...do stuff end -- GOOD if atc_arrow == false then -- ...do stuff end