aboutsummaryrefslogtreecommitdiff
path: root/advtrains/advtrains_luaautomation
diff options
context:
space:
mode:
authororwell96 <mono96.mml@gmail.com>2017-02-04 18:35:34 +0100
committerorwell96 <mono96.mml@gmail.com>2017-02-04 18:35:34 +0100
commit61e48fff280075ec52bfaa31644c22b08814d680 (patch)
treec7cac2a799bfe4bf6f180617f96c1035e04b26df /advtrains/advtrains_luaautomation
parent1e3bd3a5fd5d448fa123ba02012f935fbc29880a (diff)
downloadadvtrains-61e48fff280075ec52bfaa31644c22b08814d680.tar.gz
advtrains-61e48fff280075ec52bfaa31644c22b08814d680.tar.bz2
advtrains-61e48fff280075ec52bfaa31644c22b08814d680.zip
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
Diffstat (limited to 'advtrains/advtrains_luaautomation')
-rw-r--r--advtrains/advtrains_luaautomation/README.txt12
-rw-r--r--advtrains/advtrains_luaautomation/active_common.lua16
-rw-r--r--advtrains/advtrains_luaautomation/atc_rail.lua56
-rw-r--r--advtrains/advtrains_luaautomation/init.lua2
4 files changed, 53 insertions, 33 deletions
diff --git a/advtrains/advtrains_luaautomation/README.txt b/advtrains/advtrains_luaautomation/README.txt
index 41ffdb0..6a2114b 100644
--- a/advtrains/advtrains_luaautomation/README.txt
+++ b/advtrains/advtrains_luaautomation/README.txt
@@ -117,13 +117,15 @@ Fired when another node called 'interrupt_pos' on this position. 'message' is th
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.
-
+ 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
-
+ 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
+ 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.
# 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.
diff --git a/advtrains/advtrains_luaautomation/active_common.lua b/advtrains/advtrains_luaautomation/active_common.lua
index 50a5051..0351c85 100644
--- a/advtrains/advtrains_luaautomation/active_common.lua
+++ b/advtrains/advtrains_luaautomation/active_common.lua
@@ -76,9 +76,10 @@ function ac.on_receive_fields(pos, formname, fields, player)
if fields.cle then
nodetbl.data={}
end
- meta:set_string("formspec", ac.getform(pos, meta))
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
@@ -88,7 +89,11 @@ end
function ac.run_in_env(pos, evtdata, customfct_p)
local ph=minetest.pos_to_string(pos)
- local nodetbl = ac.nodes[ph] or {}
+ 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(pos) then
@@ -96,10 +101,12 @@ function ac.run_in_env(pos, evtdata, customfct_p)
end
if not nodetbl.env or not atlatc.envs[nodetbl.env] then
- return false, "Not an existing environment: "..(nodetbl.env or "<nil>")
+ atwarn("LuaAutomation component at",ph,": Not an existing environment: "..(nodetbl.env or "<nil>"))
+ return false
end
if not nodetbl.code or nodetbl.code=="" then
- return false, "No code to run!"
+ atwarn("LuaAutomation component at",ph,": No code to run! (insert -- to suppress warning)")
+ return false
end
local customfct=customfct_p or {}
@@ -113,6 +120,7 @@ function ac.run_in_env(pos, evtdata, customfct_p)
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
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)
diff --git a/advtrains/advtrains_luaautomation/init.lua b/advtrains/advtrains_luaautomation/init.lua
index d88944f..feea372 100644
--- a/advtrains/advtrains_luaautomation/init.lua
+++ b/advtrains/advtrains_luaautomation/init.lua
@@ -85,6 +85,8 @@ atlatc.save = function()
file:close()
end
+minetest.register_on_shutdown(atlatc.save)
+
-- globalstep for step code
local timer, step_int=0, 2
local stimer, sstep_int=0, 10