diff options
-rw-r--r-- | advtrains/advtrains_luaautomation/README.txt | 16 | ||||
-rw-r--r-- | advtrains/advtrains_luaautomation/init.lua | 4 | ||||
-rw-r--r-- | advtrains/advtrains_luaautomation/passive.lua | 10 | ||||
-rw-r--r-- | advtrains/advtrains_luaautomation/pcnaming.lua | 72 | ||||
-rw-r--r-- | advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png | bin | 0 -> 329 bytes |
5 files changed, 90 insertions, 12 deletions
diff --git a/advtrains/advtrains_luaautomation/README.txt b/advtrains/advtrains_luaautomation/README.txt index 6a2114b..1d29786 100644 --- a/advtrains/advtrains_luaautomation/README.txt +++ b/advtrains/advtrains_luaautomation/README.txt @@ -57,9 +57,11 @@ Shorthand function to create a position vector {x=?, y=?, z=?} with less charact getstate(pos) Get the state of the passive component at position 'pos'. See section on passive components for more info. +pos can be either a position vector (created by POS()) or a string, the name of this passive component. setstate(pos, newstate) Set the state of the passive component at position 'pos'. +pos can be either a position vector (created by POS()) or a string, the name of this passive component. 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. @@ -86,18 +88,12 @@ if event.wanted then ...do stuff... end # Init code The initialization code is not a component as such, but rather a part of the whole environment. It can (and should) be used to make definitions that other components can refer to. Examples: -A table with the positions of signals mapped to memorizable names, like this: -F.signals={ - station_platform1_leave_north=POS(204,5,678), - station_platform1_leave_south=POS(202,5,643), - station_platform2_leave_north=POS(208,5,678), - station_platform2_leave_south=POS(210,5,643), -} A function to define behavior for trains in subway stations: function F.station() if event.train then atc_send("B0WOL") end if event.int and event.message="depart" then atc_send("OCD1SM") end end + The init code is run whenever the F table needs to be refilled with data. This is the case on server startup and whenever the init code is changed and you choose to run it. Functions are run in the environment of the currently active node, regardless of where they were defined. So, the 'event' table always reflects the state of the calling node. @@ -152,4 +148,10 @@ The Mesecon switch can be switched using LuaAutomation. Note that this is not po "on" - the switch is switched on "off" - the switch is switched off +### Passive component naming +You can assign names to passive components using the Passive Component Naming tool. +Once you set a name for any component, you can reference it by that name in the getstate() and setstate() functions, like this: +(Imagine a signal that you have named "Stn_P1_out" at position (1,2,3) ) +setstate("Stn_P1_out", "green") instead of setstate(POS(1,2,3), "green") +This way, you don't need to memorize positions. diff --git a/advtrains/advtrains_luaautomation/init.lua b/advtrains/advtrains_luaautomation/init.lua index feea372..87f5921 100644 --- a/advtrains/advtrains_luaautomation/init.lua +++ b/advtrains/advtrains_luaautomation/init.lua @@ -25,6 +25,7 @@ dofile(mp.."/interrupt.lua") dofile(mp.."/active_common.lua") dofile(mp.."/atc_rail.lua") dofile(mp.."/operation_panel.lua") +dofile(mp.."/pcnaming.lua") if mesecon then dofile(mp.."/p_mesecon_iface.lua") end @@ -39,13 +40,13 @@ else atprint("luaautomation reading file:",filename) local tbl = minetest.deserialize(file:read("*a")) if type(tbl) == "table" then - atprint(tbl) if tbl.version==1 then for envname, data in pairs(tbl.envs) do atlatc.envs[envname]=atlatc.env_load(envname, data) end atlatc.active.load(tbl.active) atlatc.interrupt.load(tbl.interrupt) + atlatc.pcnaming.load(tbl.pcnaming) end else minetest.log("error", " Failed to read advtrains_luaautomation save data from file "..filename..": Not a table!") @@ -69,6 +70,7 @@ atlatc.save = function() envs=envdata, active = atlatc.active.save(), interrupt = atlatc.interrupt.save(), + pcnaming = atlatc.pcnaming.save(), } local datastr = minetest.serialize(save_tbl) diff --git a/advtrains/advtrains_luaautomation/passive.lua b/advtrains/advtrains_luaautomation/passive.lua index e32bee9..774df8a 100644 --- a/advtrains/advtrains_luaautomation/passive.lua +++ b/advtrains/advtrains_luaautomation/passive.lua @@ -1,8 +1,9 @@ -- passive.lua -- API to passive components, as described in passive_api.txt -local function getstate(pos) - if not type(pos)=="table" or not pos.x or not pos.y or not pos.z then +local function getstate(parpos) + local pos=atlatc.pcnaming.resolve_pos(parpos) + if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then debug.sethook() error("Invalid position supplied to getstate") end @@ -19,8 +20,9 @@ local function getstate(pos) return nil end -local function setstate(pos, newstate) - if not type(pos)=="table" or not pos.x or not pos.y or not pos.z then +local function setstate(parpos, newstate) + local pos=atlatc.pcnaming.resolve_pos(parpos) + if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then debug.sethook() error("Invalid position supplied to setstate") end diff --git a/advtrains/advtrains_luaautomation/pcnaming.lua b/advtrains/advtrains_luaautomation/pcnaming.lua new file mode 100644 index 0000000..fbfea8a --- /dev/null +++ b/advtrains/advtrains_luaautomation/pcnaming.lua @@ -0,0 +1,72 @@ +--pcnaming.lua +--a.k.a Passive component naming +--Allows to assign names to passive components, so they can be called like: +--setstate("iamasignal", "green") +atlatc.pcnaming={name_map={}} +function atlatc.pcnaming.load(stuff) + if type(stuff)=="table" then + atlatc.pcnaming.name_map=stuff + end +end +function atlatc.pcnaming.save() + return atlatc.pcnaming.name_map +end + +function atlatc.pcnaming.resolve_pos(posorname) + if type(posorname)=="table" then return posorname end + return atlatc.pcnaming.name_map[posorname] +end + +minetest.register_craftitem("advtrains_luaautomation:pcnaming",{ + description = attrans("Passive Component Naming Tool\n\nRight-click to name a passive component."), + groups = {cracky=1}, -- key=name, value=rating; rating=1..3. + inventory_image = "atlatc_pcnaming.png", + wield_image = "atlatc_pcnaming.png", + stack_max = 1, + on_place = function(itemstack, placer, pointed_thing) + local pname = placer:get_player_name() + if not pname then + return + end + if not minetest.check_player_privs(pname, {atlatc=true}) then + minetest.chat_send_player(pname, "Missing privilege: atlatc") + end + if pointed_thing.type=="node" then + local pos=pointed_thing.under + if minetest.is_protected(pos, name) then + return + end + local node=minetest.get_node(pos) + local ndef=minetest.registered_nodes[node.name] + if ndef then + if ndef.luaautomation then + --look if this one already has a name + local pn="" + for name, npos in pairs(atlatc.pcnaming.name_map) do + if vector.equals(npos, pos) then + pn=name + end + end + minetest.show_formspec(pname, "atlatc_naming_"..minetest.pos_to_string(pos), "field[pn;Set name of component (empty to clear);"..pn.."]") + end + end + end + end, +}) +minetest.register_on_player_receive_fields(function(player, formname, fields) + local pts=string.match(formname, "^atlatc_naming_(.+)") + if pts then + local pos=minetest.string_to_pos(pts) + if fields.pn then + --first remove all occurences + for name, npos in pairs(atlatc.pcnaming.name_map) do + if vector.equals(npos, pos) then + atlatc.pcnaming.name_map[name]=nil + end + end + if fields.pn~="" then + atlatc.pcnaming.name_map[fields.pn]=pos + end + end + end +end) diff --git a/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png b/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png Binary files differnew file mode 100644 index 0000000..3fccdfc --- /dev/null +++ b/advtrains/advtrains_luaautomation/textures/atlatc_pcnaming.png |