2013-01-19 16:14:41 +04:00
|
|
|
|
|
|
|
filename=minetest.get_worldpath() .. "/teleport_tubes"
|
|
|
|
|
|
|
|
function read_file()
|
|
|
|
local f = io.open(filename, "r")
|
|
|
|
if f==nil then return {} end
|
|
|
|
local t = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
if t=="" or t==nil then return {} end
|
|
|
|
return minetest.deserialize(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
function write_file(tbl)
|
|
|
|
local f = io.open(filename, "w")
|
|
|
|
f:write(minetest.serialize(tbl))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
2013-06-05 17:54:23 +04:00
|
|
|
function add_tube_in_file(pos,channel, cr)
|
2013-01-19 16:14:41 +04:00
|
|
|
tbl=read_file()
|
|
|
|
for _,val in ipairs(tbl) do
|
|
|
|
if val.x==pos.x and val.y==pos.y and val.z==pos.z then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2013-06-05 17:54:23 +04:00
|
|
|
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
|
2013-01-19 16:14:41 +04:00
|
|
|
write_file(tbl)
|
|
|
|
end
|
|
|
|
|
|
|
|
function remove_tube_in_file(pos)
|
|
|
|
tbl=read_file()
|
|
|
|
newtbl={}
|
|
|
|
for _,val in ipairs(tbl) do
|
|
|
|
if val.x~=pos.x or val.y~=pos.y or val.z~=pos.z then
|
|
|
|
table.insert(newtbl,val)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
write_file(newtbl)
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_tubes_in_file(pos,channel)
|
|
|
|
tbl=read_file()
|
|
|
|
newtbl={}
|
2013-02-03 19:11:15 +04:00
|
|
|
changed=false
|
2013-01-19 16:14:41 +04:00
|
|
|
for _,val in ipairs(tbl) do
|
2013-07-01 07:55:07 +04:00
|
|
|
local node = minetest.get_node(val)
|
|
|
|
local meta = minetest.get_meta(val)
|
2013-06-05 17:54:23 +04:00
|
|
|
if node.name~="ignore" and (val.channel~=meta:get_string("channel") or val.cr~=meta:get_int("can_receive")) then
|
2013-02-03 19:11:15 +04:00
|
|
|
val.channel=meta:get_string("channel")
|
2013-06-05 17:54:23 +04:00
|
|
|
val.cr=meta:get_int("can_receive")
|
2013-02-03 19:11:15 +04:00
|
|
|
changed=true
|
|
|
|
end
|
2013-06-05 17:54:23 +04:00
|
|
|
if val.cr==1 and val.channel==channel and (val.x~=pos.x or val.y~=pos.y or val.z~=pos.z) then
|
2013-01-19 16:14:41 +04:00
|
|
|
table.insert(newtbl,val)
|
|
|
|
end
|
|
|
|
end
|
2013-02-03 19:11:15 +04:00
|
|
|
if changed then write_file(tbl) end
|
2013-01-19 16:14:41 +04:00
|
|
|
return newtbl
|
|
|
|
end
|
|
|
|
|
2013-01-19 20:51:11 +04:00
|
|
|
teleport_noctr_textures={"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png",
|
|
|
|
"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png"}
|
|
|
|
teleport_plain_textures={"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png",
|
|
|
|
"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png"}
|
|
|
|
teleport_end_textures={"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png",
|
|
|
|
"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png"}
|
|
|
|
teleport_short_texture="pipeworks_teleport_tube_short.png"
|
|
|
|
teleport_inv_texture="pipeworks_teleport_tube_inv.png"
|
2013-01-19 16:14:41 +04:00
|
|
|
|
2013-01-19 20:51:11 +04:00
|
|
|
register_tube("pipeworks:teleport_tube","Teleporter pneumatic tube segment",teleport_plain_textures,
|
|
|
|
teleport_noctr_textures,teleport_end_textures,teleport_short_texture,teleport_inv_texture,
|
2013-01-19 16:14:41 +04:00
|
|
|
{tube={can_go=function(pos,node,velocity,stack)
|
|
|
|
velocity.x=0
|
|
|
|
velocity.y=0
|
|
|
|
velocity.z=0
|
2013-07-01 07:55:07 +04:00
|
|
|
local meta = minetest.get_meta(pos)
|
2013-01-19 16:40:19 +04:00
|
|
|
channel=meta:get_string("channel")
|
2013-05-22 01:44:05 +04:00
|
|
|
local target=get_tubes_in_file(pos,channel)
|
|
|
|
if target[1]==nil then return {} end
|
|
|
|
d=math.random(1,#target)
|
|
|
|
pos.x=target[d].x
|
|
|
|
pos.y=target[d].y
|
|
|
|
pos.z=target[d].z
|
2013-01-19 16:14:41 +04:00
|
|
|
return meseadjlist
|
|
|
|
end},
|
|
|
|
on_construct = function(pos)
|
2013-07-01 07:55:07 +04:00
|
|
|
local meta = minetest.get_meta(pos)
|
2013-06-05 17:54:23 +04:00
|
|
|
meta:set_string("channel","")
|
|
|
|
meta:set_int("can_receive",1)
|
|
|
|
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,"")
|
2013-01-19 16:40:19 +04:00
|
|
|
end,
|
|
|
|
on_receive_fields = function(pos,formname,fields,sender)
|
2013-07-01 07:55:07 +04:00
|
|
|
local meta = minetest.get_meta(pos)
|
2013-06-05 17:54:23 +04:00
|
|
|
if fields.channel==nil then fields.channel=meta:get_string("channel") end
|
2013-01-19 16:40:19 +04:00
|
|
|
meta:set_string("channel",fields.channel)
|
|
|
|
remove_tube_in_file(pos)
|
2013-06-05 17:54:23 +04:00
|
|
|
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)
|
2013-01-19 16:14:41 +04:00
|
|
|
end,
|
2013-02-03 19:11:15 +04:00
|
|
|
on_destruct = function(pos)
|
2013-01-19 16:14:41 +04:00
|
|
|
remove_tube_in_file(pos)
|
|
|
|
end})
|