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_luaautomation/active_common.lua | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 advtrains/advtrains_luaautomation/active_common.lua (limited to 'advtrains/advtrains_luaautomation/active_common.lua') diff --git a/advtrains/advtrains_luaautomation/active_common.lua b/advtrains/advtrains_luaautomation/active_common.lua new file mode 100644 index 0000000..b94a260 --- /dev/null +++ b/advtrains/advtrains_luaautomation/active_common.lua @@ -0,0 +1,118 @@ + + +local ac = {nodes={}} + +function ac.load(data) + ac.nodes=data and data.nodes or {} +end +function ac.save() + return {nodes = ac.nodes} +end + +function ac.after_place_node(pos, player) + advtrains.ndb.update(pos) + local meta=minetest.get_meta(pos) + meta:set_string("formspec", ac.getform(pos, meta)) + meta:set_string("infotext", "LuaAutomation component, unconfigured.") + local ph=minetest.hash_node_position(pos) + --just get first available key! + for en,_ in pairs(atlatc.envs) do + ac.nodes[ph]={env=en} + return + end +end +function ac.getform(pos, meta_p) + local meta = meta_p or minetest.get_meta(pos) + local envs_asvalues={} + + local ph=minetest.hash_node_position(pos) + local nodetbl = ac.nodes[ph] + local env, code, err = nil, "", "" + if nodetbl then + code=nodetbl.code or "" + err=nodetbl.err or "" + env=nodetbl.env or "" + end + local sel = 1 + for n,_ in pairs(atlatc.envs) do + envs_asvalues[#envs_asvalues+1]=n + if n==env then + sel=#envs_asvalues + end + end + local form = "size[10,10]dropdown[0,0;3;env;"..table.concat(envs_asvalues, ",")..";"..sel.."]" + .."button[4,0;2,1;save;Save]button[7,0;2,1;cle;Clear local env] textarea[0.2,1;10,10;code;Code;"..minetest.formspec_escape(code).."]" + .."label[0,9.8;"..err.."]" + return form +end + +function ac.after_dig_node(pos, node, player) + advtrains.invalidate_all_paths() + advtrains.ndb.clear(pos) + local ph=minetest.hash_node_position(pos) + ac.nodes[ph]=nil +end + +function ac.on_receive_fields(pos, formname, fields, player) + if not minetest.check_player_privs(player:get_player_name(), {atlatc=true}) then + minetest.chat_send_player(player:get_player_name(), "Missing privilege: atlatc - Operation cancelled!") + end + + local meta=minetest.get_meta(pos) + local ph=minetest.hash_node_position(pos) + local nodetbl = ac.nodes[ph] or {} + --if fields.quit then return end + if fields.env then + nodetbl.env=fields.env + end + if fields.code then + nodetbl.code=fields.code + end + if fields.save then + nodetbl.err=nil + end + if fields.cle then + nodetbl.data={} + end + meta:set_string("formspec", ac.getform(pos, meta)) + + ac.nodes[ph]=nodetbl + if nodetbl.env then + meta:set_string("infotext", "LuaAutomation component, assigned to environment '"..nodetbl.env.."'") + else + meta:set_string("infotext", "LuaAutomation component, invalid enviroment set!") + end +end + +function ac.run_in_env(pos, evtdata, customfct) + local ph=minetest.hash_node_position(pos) + local nodetbl = ac.nodes[ph] or {} + + local meta + if minetest.get_node(pos) then + meta=minetest.get_meta(pos) + end + + if not nodetbl.env or not atlatc.envs[nodetbl.env] then + return false, "Not an existing environment: "..(nodetbl.env or "") + end + if not nodetbl.code or nodetbl.code=="" then + return false, "No code to run!" + end + + local datain=nodetbl.data or {} + local succ, dataout = atlatc.envs[nodetbl.env]:execute_code(datain, nodetbl.code, evtdata, customfct) + if succ then + atlatc.active.nodes[ph].data=atlatc.remove_invalid_data(dataout) + else + atlatc.active.nodes[ph].err=dataout + if meta then + meta:set_string("infotext", "LuaAutomation ATC interface rail, ERROR:"..dataout) + end + end + if meta then + meta:set_string("formspec", ac.getform(pos, meta)) + end +end + +atlatc.active=ac -- 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/active_common.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'advtrains/advtrains_luaautomation/active_common.lua') diff --git a/advtrains/advtrains_luaautomation/active_common.lua b/advtrains/advtrains_luaautomation/active_common.lua index b94a260..474838e 100644 --- a/advtrains/advtrains_luaautomation/active_common.lua +++ b/advtrains/advtrains_luaautomation/active_common.lua @@ -84,7 +84,7 @@ function ac.on_receive_fields(pos, formname, fields, player) end end -function ac.run_in_env(pos, evtdata, customfct) +function ac.run_in_env(pos, evtdata, customfct_p) local ph=minetest.hash_node_position(pos) local nodetbl = ac.nodes[ph] or {} @@ -100,6 +100,11 @@ function ac.run_in_env(pos, evtdata, customfct) return false, "No code to run!" end + local customfct=customfct_p or {} + customfct.interrupt=function(t, imesg) + atlatc.interrupt.add(t, pos, {type="int", int=true, message=imesg}) + end + local datain=nodetbl.data or {} local succ, dataout = atlatc.envs[nodetbl.env]:execute_code(datain, nodetbl.code, evtdata, customfct) if succ then -- 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/active_common.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'advtrains/advtrains_luaautomation/active_common.lua') diff --git a/advtrains/advtrains_luaautomation/active_common.lua b/advtrains/advtrains_luaautomation/active_common.lua index 474838e..50a5051 100644 --- a/advtrains/advtrains_luaautomation/active_common.lua +++ b/advtrains/advtrains_luaautomation/active_common.lua @@ -3,7 +3,9 @@ local ac = {nodes={}} function ac.load(data) - ac.nodes=data and data.nodes or {} + if data then + ac.nodes=data.nodes + end end function ac.save() return {nodes = ac.nodes} @@ -14,7 +16,7 @@ function ac.after_place_node(pos, player) local meta=minetest.get_meta(pos) meta:set_string("formspec", ac.getform(pos, meta)) meta:set_string("infotext", "LuaAutomation component, unconfigured.") - local ph=minetest.hash_node_position(pos) + local ph=minetest.pos_to_string(pos) --just get first available key! for en,_ in pairs(atlatc.envs) do ac.nodes[ph]={env=en} @@ -25,7 +27,7 @@ function ac.getform(pos, meta_p) local meta = meta_p or minetest.get_meta(pos) local envs_asvalues={} - local ph=minetest.hash_node_position(pos) + local ph=minetest.pos_to_string(pos) local nodetbl = ac.nodes[ph] local env, code, err = nil, "", "" if nodetbl then @@ -49,7 +51,7 @@ end function ac.after_dig_node(pos, node, player) advtrains.invalidate_all_paths() advtrains.ndb.clear(pos) - local ph=minetest.hash_node_position(pos) + local ph=minetest.pos_to_string(pos) ac.nodes[ph]=nil end @@ -59,7 +61,7 @@ function ac.on_receive_fields(pos, formname, fields, player) end local meta=minetest.get_meta(pos) - local ph=minetest.hash_node_position(pos) + local ph=minetest.pos_to_string(pos) local nodetbl = ac.nodes[ph] or {} --if fields.quit then return end if fields.env then @@ -85,7 +87,7 @@ function ac.on_receive_fields(pos, formname, fields, player) end function ac.run_in_env(pos, evtdata, customfct_p) - local ph=minetest.hash_node_position(pos) + local ph=minetest.pos_to_string(pos) local nodetbl = ac.nodes[ph] or {} local meta -- 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/active_common.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'advtrains/advtrains_luaautomation/active_common.lua') 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 "") + atwarn("LuaAutomation component at",ph,": Not an existing environment: "..(nodetbl.env or "")) + 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 -- cgit v1.2.3