Add an no-receive option to teleport tubes.

This commit is contained in:
Novatux 2013-06-05 15:54:23 +02:00
parent d3bc59b2ff
commit c9a8224bb5

View File

@ -16,14 +16,14 @@ function write_file(tbl)
f:close() f:close()
end end
function add_tube_in_file(pos,channel) function add_tube_in_file(pos,channel, cr)
tbl=read_file() tbl=read_file()
for _,val in ipairs(tbl) do for _,val in ipairs(tbl) do
if val.x==pos.x and val.y==pos.y and val.z==pos.z then if val.x==pos.x and val.y==pos.y and val.z==pos.z then
return return
end end
end end
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel}) table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
write_file(tbl) write_file(tbl)
end end
@ -45,11 +45,12 @@ function get_tubes_in_file(pos,channel)
for _,val in ipairs(tbl) do for _,val in ipairs(tbl) do
local node = minetest.env:get_node(val) local node = minetest.env:get_node(val)
local meta = minetest.env:get_meta(val) local meta = minetest.env:get_meta(val)
if node.name~="ignore" and val.channel~=meta:get_string("channel") then if node.name~="ignore" and (val.channel~=meta:get_string("channel") or val.cr~=meta:get_int("can_receive")) then
val.channel=meta:get_string("channel") val.channel=meta:get_string("channel")
val.cr=meta:get_int("can_receive")
changed=true changed=true
end end
if val.channel==channel and (val.x~=pos.x or val.y~=pos.y or val.z~=pos.z) then if val.cr==1 and val.channel==channel and (val.x~=pos.x or val.y~=pos.y or val.z~=pos.z) then
table.insert(newtbl,val) table.insert(newtbl,val)
end end
end end
@ -84,15 +85,33 @@ register_tube("pipeworks:teleport_tube","Teleporter pneumatic tube segment",tele
end}, end},
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
meta:set_string("channel","0") meta:set_string("channel","")
meta:set_string("formspec","size[9,1;]field[0,0.5;9,1;channel;Channel:;${channel}]") meta:set_int("can_receive",1)
add_tube_in_file(pos,"0") meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;On]")
add_tube_in_file(pos,"")
end, end,
on_receive_fields = function(pos,formname,fields,sender) on_receive_fields = function(pos,formname,fields,sender)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
if fields.channel==nil then fields.channel=meta:get_string("channel") end
meta:set_string("channel",fields.channel) meta:set_string("channel",fields.channel)
remove_tube_in_file(pos) remove_tube_in_file(pos)
add_tube_in_file(pos,fields.channel) local cr = meta:get_int("can_receive")
if fields["bt"] then
cr=1-cr
meta:set_int("can_receive",cr)
if cr==1 then
meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;On]")
else
meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;Off]")
end
end
add_tube_in_file(pos,fields.channel, cr)
end, end,
on_destruct = function(pos) on_destruct = function(pos)
remove_tube_in_file(pos) remove_tube_in_file(pos)