From b19033b224f4f8ec33f10ba40327f1d811c04fbb Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 2 Feb 2017 16:40:51 +0100 Subject: LuaAutomation - Basic component implementation Implements the base code for LuaAutomation, an ATC rail and a punch-operated 'operation panel' as well as interface for passive components. Changes in advtrains code where neccessary. Supported passive components are light signals, switches and mesecon switches --- advtrains/advtrains_luaautomation/atc_rail.lua | 92 ++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 advtrains/advtrains_luaautomation/atc_rail.lua (limited to 'advtrains/advtrains_luaautomation/atc_rail.lua') diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua new file mode 100644 index 0000000..2af03cf --- /dev/null +++ b/advtrains/advtrains_luaautomation/atc_rail.lua @@ -0,0 +1,92 @@ +-- 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.hash_node_position(pos) + local railtbl = atlatc.active.nodes[ph] or {} + + local arrowconn = railtbl.arrowconn + + --prepare ingame API for ATC. Regenerate each time since pos needs to be known + local atc_valid, atc_arrow + local train_id=advtrains.detector.on_node[ph] + local train=advtrains.trains[train_id] + if not train then return false end + 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]) + ) + atc_valid = true + end + end + local customfct={ + atc_send = function(cmd) + advtrains.atc.train_reset_command(train_id) + if atc_valid then + train.atc_command=cmd + train.atc_arrow=atc_arrow + return atc_valid + end + end, + atc_reset = function(cmd) + advtrains.atc.train_reset_command(train_id) + return true + end, + atc_arrow = atc_arrow + } + + 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.hash_node_position(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", id=train_id}) + end, + }, + luaautomation = { + fire_event=r.fire_event + } + } + end +}, advtrains.trackpresets.t_30deg_straightonly) + + +atlatc.rail = r -- cgit v1.2.3 From 948482a99e4aae4d51ff97879432140645959f46 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Thu, 2 Feb 2017 21:14:20 +0100 Subject: LuaAutomation: Add interrupt to the ingame API and implement initialization code handling and env management --- advtrains/advtrains_luaautomation/atc_rail.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'advtrains/advtrains_luaautomation/atc_rail.lua') diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua index 2af03cf..b2a8aad 100644 --- a/advtrains/advtrains_luaautomation/atc_rail.lua +++ b/advtrains/advtrains_luaautomation/atc_rail.lua @@ -78,7 +78,7 @@ advtrains.register_tracks("default", { advtrains = { on_train_enter = function(pos, train_id) --do async. Event is fired in train steps - atlatc.interrupt.add(0, pos, {type="train", id=train_id}) + atlatc.interrupt.add(0, pos, {type="train", train=true, id=train_id}) end, }, luaautomation = { -- cgit v1.2.3 From 328d5054a105869c7e12df1941ceedb308ef1faa Mon Sep 17 00:00:00 2001 From: orwell96 Date: Fri, 3 Feb 2017 20:40:30 +0100 Subject: Revert change to node pos hashes, and rewrite trackdb to use individual coordinates The precision of integers was not sufficient for saving pos node hashes in most cases, leading to strange bugs. This fixes broken ATC rails, broken LuaAutomation stuff and trackdb on Windows. Probably also fixes trains randomly stopping. --- advtrains/advtrains_luaautomation/atc_rail.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'advtrains/advtrains_luaautomation/atc_rail.lua') diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua index b2a8aad..f52252c 100644 --- a/advtrains/advtrains_luaautomation/atc_rail.lua +++ b/advtrains/advtrains_luaautomation/atc_rail.lua @@ -7,8 +7,14 @@ local r={} function r.fire_event(pos, evtdata) - local ph=minetest.hash_node_position(pos) - local railtbl = atlatc.active.nodes[ph] or {} + local ph=minetest.pos_to_string(pos) + local railtbl = atlatc.active.nodes[ph] + + if not railtbl then + atprint("missing rail table entry!") + return + end + local arrowconn = railtbl.arrowconn @@ -70,7 +76,7 @@ advtrains.register_tracks("default", { atlatc.active.on_receive_fields(pos, ...) --set arrowconn (for ATC) - local ph=minetest.hash_node_position(pos) + 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, -- cgit v1.2.3 From 61e48fff280075ec52bfaa31644c22b08814d680 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Sat, 4 Feb 2017 18:35:34 +0100 Subject: Commit 1.6.2 - Add some more stuff to API for LuaATC rails - Warn on strange events even if debug info is disabled - save atlatc on shutdown too - fix detector rails in unloaded chunks - do not fail silently in simple ATC rails --- advtrains/advtrains_luaautomation/atc_rail.lua | 56 +++++++++++++++----------- 1 file changed, 32 insertions(+), 24 deletions(-) (limited to 'advtrains/advtrains_luaautomation/atc_rail.lua') diff --git a/advtrains/advtrains_luaautomation/atc_rail.lua b/advtrains/advtrains_luaautomation/atc_rail.lua index f52252c..c2c8d6f 100644 --- a/advtrains/advtrains_luaautomation/atc_rail.lua +++ b/advtrains/advtrains_luaautomation/atc_rail.lua @@ -11,7 +11,7 @@ function r.fire_event(pos, evtdata) local railtbl = atlatc.active.nodes[ph] if not railtbl then - atprint("missing rail table entry!") + atwarn("LuaAutomation ATC interface rail at",ph,": Data not in memory! Please visit position and click 'Save'!") return end @@ -19,40 +19,48 @@ function r.fire_event(pos, evtdata) local arrowconn = railtbl.arrowconn --prepare ingame API for ATC. Regenerate each time since pos needs to be known - local atc_valid, atc_arrow + --If no train, then return false. local train_id=advtrains.detector.on_node[ph] - local train=advtrains.trains[train_id] - if not train then return false end - 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]) - ) - atc_valid = true + 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) - if atc_valid then - train.atc_command=cmd - train.atc_arrow=atc_arrow - return atc_valid - end + 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_arrow = atc_arrow, + atc_id = train_id, + atc_speed = tvel, } atlatc.active.run_in_env(pos, evtdata, customfct) -- cgit v1.2.3