aboutsummaryrefslogtreecommitdiff
path: root/advtrains/advtrains_luaautomation/atc_rail.lua
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2017-02-05 16:32:53 +0100
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2017-02-05 16:32:53 +0100
commit746e328b7b77514d343bfe76922de0244b608278 (patch)
treee098b101c3ab96239f2827d44b8f8b97b3dc95d1 /advtrains/advtrains_luaautomation/atc_rail.lua
parentbfd24ee43a10ab79674636e9dad725cdba093f63 (diff)
parent61e48fff280075ec52bfaa31644c22b08814d680 (diff)
downloadadvtrains-746e328b7b77514d343bfe76922de0244b608278.tar.gz
advtrains-746e328b7b77514d343bfe76922de0244b608278.tar.bz2
advtrains-746e328b7b77514d343bfe76922de0244b608278.zip
Merge branch 'master' of https://github.com/orwell96/advtrains
Diffstat (limited to 'advtrains/advtrains_luaautomation/atc_rail.lua')
-rw-r--r--advtrains/advtrains_luaautomation/atc_rail.lua106
1 files changed, 106 insertions, 0 deletions
diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua
new file mode 100644
index 0000000..c2c8d6f
--- /dev/null
+++ b/advtrains/advtrains_luaautomation/atc_rail.lua
@@ -0,0 +1,106 @@
+-- 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
+
+ --prepare ingame API for ATC. Regenerate each time since pos needs to be known
+ --If no train, then return false.
+ local train_id=advtrains.detector.on_node[ph]
+ 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
+ for index, ppos in pairs(train.path) do
+ if vector.equals(advtrains.round_vector_floor_y(ppos), pos) then
+ atc_arrow =
+ vector.equals(
+ advtrains.dirCoordSet(pos, arrowconn),
+ advtrains.round_vector_floor_y(train.path[index+train.movedir])
+ )
+ end
+ end
+ if atc_arrow==nil then
+ atwarn("LuaAutomation ATC rail at", pos, ": Rail not on train's path! Can't determine arrow direction. Assuming +!")
+ atc_arrow=true
+ tvel=train.velocity
+ end
+ end
+ local customfct={
+ atc_send = function(cmd)
+ if not train_id then return false end
+ advtrains.atc.train_reset_command(train_id)
+ train.atc_command=cmd
+ train.atc_arrow=atc_arrow
+ return true
+ end,
+ atc_reset = function(cmd)
+ if not train_id then return false end
+ advtrains.atc.train_reset_command(train_id)
+ return true
+ end,
+ atc_arrow = atc_arrow,
+ atc_id = train_id,
+ atc_speed = tvel,
+ }
+
+ atlatc.active.run_in_env(pos, evtdata, customfct)
+
+end
+
+advtrains.register_tracks("default", {
+ nodename_prefix="advtrains_luaautomation:dtrack",
+ texture_prefix="advtrains_dtrack_atc",
+ models_prefix="advtrains_dtrack_detector",
+ models_suffix=".b3d",
+ shared_texture="advtrains_dtrack_rail_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 _, conn1=advtrains.get_rail_info_at(pos, advtrains.all_tracktypes)
+ atlatc.active.nodes[ph].arrowconn=conn1
+ end,
+
+ advtrains = {
+ 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,
+ },
+ luaautomation = {
+ fire_event=r.fire_event
+ }
+ }
+ end
+}, advtrains.trackpresets.t_30deg_straightonly)
+
+
+atlatc.rail = r