aboutsummaryrefslogtreecommitdiff
path: root/advtrains_interlocking/init.lua
blob: a2f5882c3ac353423856e8069bc0201bb40dfb8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- Advtrains interlocking system
-- See database.lua for a detailed explanation

advtrains.interlocking = {}

advtrains.SHUNT_SPEED_MAX = 6

function advtrains.interlocking.sigd_equal(sigd, cmp)
	return vector.equals(sigd.p, cmp.p) and sigd.s==cmp.s
end


local modpath = minetest.get_modpath(minetest.get_current_modname()) .. DIR_DELIM

dofile(modpath.."database.lua")
dofile(modpath.."signal_api.lua")
dofile(modpath.."demosignals.lua")
dofile(modpath.."train_sections.lua")
dofile(modpath.."route_prog.lua")
dofile(modpath.."routesetting.lua")
dofile(modpath.."tcb_ts_ui.lua")
dofile(modpath.."route_ui.lua")
dofile(modpath.."tool.lua")

dofile(modpath.."approach.lua")
dofile(modpath.."ars.lua")
dofile(modpath.."tsr_rail.lua")


minetest.register_privilege("interlocking", {description = "Can set up track sections, routes and signals.", give_to_singleplayer = true})
> 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

# 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. The mod is sometimes abbreviated as 'LuaATC' or '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.

A probably more complete documentation of LuaATC is found on the [Advtrains Wiki](http://advtrains.de/wiki/doku.php?id=usage:atlatc:start)

## Privileges
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. 

## Environments

Each active component is assigned to an environment where all atlac data is 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 without deleting the environment and starting again.

 - `/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.

 - `/env_subscribe <env_name>`, `/env_unsubscribe <env_name>`:
Subscribe or unsubscribe from log/error messages originating from this environment

 - `/env_subscriptions [env_name]`:
List your subscriptions or players subscribed to an environment.


## Functions and variables
### General Functions and Variables
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`

Any attempt to overwrite the predefined values results in an error.

### LuaAutomation Global Variables
 - `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.

 - `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.

### LuaAutomation Global Functions
> Note: in the following functions, all parameters named `pos` designate a position. You can use the following:  
> - a default Minetest position vector (eg. {x=34, y=2, z=-18})  
> - the POS(34,2,-18) shorthand below.  
> - A string, the passive component name. See 'passive component naming'. 
  


 - `POS(x,y,z)`
Shorthand function to create a position vector {x=?, y=?, z=?} with less characters.

 - `getstate(pos)`
Get the state of the passive component at position `pos`.

 - `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. Returns true. *Not available in init code.*

 - `interrupt_safe(time, message)`
Like `interrupt()`, but does not add an interrupt and returns false when an interrupt (of any type) is already present for this component. Returns true when interrupt was successfully added.

 - `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_**.

 - `clear_interrupts()`
Removes any pending interrupts of this node.

 - `digiline_send(channel, message)`
Make this active component send a digiline message on the specified channel.
Not available in init code.

 - `atc_send_to_train(<train_id>, <atc_command>)`
	Sends the specified ATC command to the train specified by its train id. This happens regardless of where the train is in the world, and can be used to remote-control trains. Returns true on success. If the train ID does not exist, returns false and does nothing. See [atc_command.txt](../atc_command.txt) for the ATC command syntax.

#### Interlocking Route Management Functions
If `advtrains_interlocking` is enabled, the following aditional functions can be used:

 - `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:
```lua
asp = {
	main = <int speed>,
		-- Main signal aspect, tells state and permitted speed of next section
		-- 0 = section is blocked
		-- >0 = section is free, speed limit is this value
		-- -1 = section is free, maximum speed permitted
		-- false = Signal doesn't provide main signal information, retain current speed limit.
	shunt = <boolean>,
		-- Whether train may proceed as shunt move, on sight
		-- main aspect takes precedence over this
		-- When main==0, train switches to shunt move and is restricted to speed 8
	proceed_as_main = <boolean>,
		-- If an approaching train is a shunt move and 'shunt' is false,
		-- the train may proceed as a train move under the "main" aspect
		-- if the main aspect permits it (i.e. main!=0)
		-- If this is not set, shunt moves are NOT allowed to switch to
		-- a train move, and must stop even if "main" would permit passing.
		-- This is intended to be used for "Halt for shunt moves" signs.
	
	dst = <int speed>,
		-- Distant signal aspect, tells state and permitted speed of the section after next section
		-- The character of these information is purely informational
		-- At this time, this field is not actively used
		-- 0 = section is blocked
		-- >0 = section is free, speed limit is this value
		-- -1 = section is free, maximum speed permitted
		-- false = Signal doesn't provide distant signal information.
	
	-- the character of call_on and dead_end is purely informative
	call_on = <boolean>, -- Call-on route, expect train in track ahead (not implemented yet)
	dead_end = <boolean>, -- Route ends on a dead end (e.g. bumper) (not implemented yet)

	w_speed = <integer>,
	-- "Warning speed restriction". Supposed for short-term speed
	-- restrictions which always override any other restrictions
	-- imposed by "speed" fields, until lifted by a value of -1
	-- (Example: german Langsamfahrstellen-Signale)
}
```
As of January 2020, the 'dst', 'call_on' and 'dead_end' fields are not used.

#### 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 [the wiki](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)

Note: Using the lines scheduler is preferred over using `interrupt()`, as it's more performant and safer to use.

## Events
The event table is a variable created locally by the component being triggered. It is a table with the following format:
```lua
event = {
	type = "<event type>",
	<event type> = true,
	--additional event-specific content
}
```
You can check the event type by using the following:
```lua 
if event.type == "wanted" then
	--do stuff
end
```
or
```lua
if event.wanted then
	--do stuff
end
```
where `wanted` is the event type to check for.  
See the "Active Components" section below for details on the various event types as not all of them are applicable to all components.

## Components
Atlac components introduce automation-capable components that fall within two categories:
 - Active Components are components that are able to run Lua code, triggered by specific events.
 - Passive Components can't perform actions themselves. Their state can be read and set by active components or manually by the player.

### Lua ATC Rails
Lua ATC rails are the only components that can actually interface with trains. The following event types are available to the Lua ATC rails:
 - `{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` variable.

 - `{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.

#### Basic Lua Rail Functions and Variables
In addition to the above environment functions, the following functions are available to whilst the train is in contact with the LuaATC rail:

 - `atc_send(<atc_command>)`
	Sends the specified ATC command to the train (a string) and returns true. If there is no train, returns false and does nothing. See [atc_command.txt](../atc_command.txt) for the ATC command syntax.

 - `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. `text` must be a string.

 - `atc_set_text_inside(text)`