2013-01-19 13:14:41 +01:00
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local filename=minetest.get_worldpath() .. "/teleport_tubes"
|
2013-01-19 13:14:41 +01:00
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local function read_file()
|
2013-01-19 13:14:41 +01:00
|
|
|
local f = io.open(filename, "r")
|
2014-06-29 12:36:22 +02:00
|
|
|
if f == nil then return {} end
|
2013-01-19 13:14:41 +01:00
|
|
|
local t = f:read("*all")
|
|
|
|
f:close()
|
2014-06-29 12:36:22 +02:00
|
|
|
if t == "" or t == nil then return {} end
|
2013-01-19 13:14:41 +01:00
|
|
|
return minetest.deserialize(t)
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local function write_file(tbl)
|
2013-01-19 13:14:41 +01:00
|
|
|
local f = io.open(filename, "w")
|
|
|
|
f:write(minetest.serialize(tbl))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
2014-01-03 13:29:38 +01:00
|
|
|
local function update_pos_in_file(pos)
|
|
|
|
local tbl=read_file()
|
2014-06-29 12:36:22 +02:00
|
|
|
for _, val in ipairs(tbl) do
|
|
|
|
if val.x == pos.x and val.y == pos.y and val.z == pos.z then
|
2014-01-03 13:29:38 +01:00
|
|
|
local meta = minetest.get_meta(val)
|
|
|
|
val.channel = meta:get_string("channel")
|
|
|
|
val.cr = meta:get_int("can_receive")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
write_file(tbl)
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local function add_tube_in_file(pos,channel, cr)
|
|
|
|
local tbl=read_file()
|
2013-01-19 13:14:41 +01:00
|
|
|
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 15:54:23 +02:00
|
|
|
table.insert(tbl,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
|
2013-01-19 13:14:41 +01:00
|
|
|
write_file(tbl)
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local function remove_tube_in_file(pos)
|
2014-06-29 12:36:22 +02:00
|
|
|
local tbl = read_file()
|
|
|
|
local 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)
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
write_file(newtbl)
|
|
|
|
end
|
|
|
|
|
2014-06-29 12:36:22 +02:00
|
|
|
local function read_node_with_vm(pos)
|
|
|
|
local vm = VoxelManip()
|
|
|
|
local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
|
|
|
|
local data = vm:get_data()
|
|
|
|
local area = VoxelArea:new({MinEdge = MinEdge, MaxEdge = MaxEdge})
|
|
|
|
return minetest.get_name_from_content_id(data[area:index(pos.x, pos.y, pos.z)])
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local function get_tubes_in_file(pos,channel)
|
2014-06-29 12:36:22 +02:00
|
|
|
local tbl = read_file()
|
|
|
|
local newtbl = {}
|
|
|
|
local changed = false
|
|
|
|
for _, val in ipairs(tbl) do
|
2013-07-01 05:55:07 +02:00
|
|
|
local meta = minetest.get_meta(val)
|
2014-06-29 12:36:22 +02:00
|
|
|
local name = read_node_with_vm(val)
|
|
|
|
local is_loaded = (minetest.get_node_or_nil(val) ~= nil)
|
|
|
|
local is_teleport_tube = minetest.registered_nodes[name] and minetest.registered_nodes[name].is_teleport_tube
|
|
|
|
if is_teleport_tube then
|
|
|
|
if is_loaded and (val.channel ~= meta:get_string("channel") or val.cr ~= meta:get_int("can_receive")) then
|
|
|
|
val.channel = meta:get_string("channel")
|
|
|
|
val.cr = meta:get_int("can_receive")
|
|
|
|
changed = true
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
val.to_remove = true
|
|
|
|
changed = true
|
2013-02-03 16:11:15 +01:00
|
|
|
end
|
2014-06-29 12:36:22 +02:00
|
|
|
end
|
|
|
|
if changed then
|
|
|
|
local updated = {}
|
|
|
|
for _, val in ipairs(tbl) do
|
|
|
|
if not val.to_remove then
|
|
|
|
table.insert(updated, val)
|
|
|
|
end
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
2014-06-29 12:36:22 +02:00
|
|
|
write_file(updated)
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
|
|
|
return newtbl
|
|
|
|
end
|
|
|
|
|
2013-12-15 08:53:10 +01:00
|
|
|
local teleport_noctr_textures={"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png",
|
2013-01-19 17:51:11 +01:00
|
|
|
"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png"}
|
2013-12-15 08:53:10 +01:00
|
|
|
local teleport_plain_textures={"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png",
|
2013-01-19 17:51:11 +01:00
|
|
|
"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png"}
|
2013-12-15 08:53:10 +01:00
|
|
|
local teleport_end_textures={"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png",
|
2013-01-19 17:51:11 +01:00
|
|
|
"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png"}
|
2013-12-15 08:53:10 +01:00
|
|
|
local teleport_short_texture="pipeworks_teleport_tube_short.png"
|
|
|
|
local teleport_inv_texture="pipeworks_teleport_tube_inv.png"
|
2013-01-19 13:14:41 +01:00
|
|
|
|
2014-07-05 17:27:09 +02:00
|
|
|
pipeworks.register_tube("pipeworks:teleport_tube","Teleporting Pneumatic Tube Segment",teleport_plain_textures,
|
2014-01-03 13:29:38 +01:00
|
|
|
teleport_noctr_textures,teleport_end_textures,teleport_short_texture,teleport_inv_texture, {
|
2014-06-29 12:36:22 +02:00
|
|
|
is_teleport_tube = true,
|
2014-01-03 13:29:38 +01:00
|
|
|
tube = {
|
|
|
|
can_go = function(pos,node,velocity,stack)
|
|
|
|
velocity.x = 0
|
|
|
|
velocity.y = 0
|
|
|
|
velocity.z = 0
|
2013-07-01 05:55:07 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2014-01-03 13:29:38 +01:00
|
|
|
local channel = meta:get_string("channel")
|
|
|
|
local target = get_tubes_in_file(pos,channel)
|
|
|
|
if target[1] == nil then return {} end
|
|
|
|
local d = math.random(1,#target)
|
|
|
|
pos.x = target[d].x
|
|
|
|
pos.y = target[d].y
|
|
|
|
pos.z = target[d].z
|
|
|
|
return pipeworks.meseadjlist
|
|
|
|
end
|
|
|
|
},
|
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
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,"")
|
|
|
|
end,
|
|
|
|
on_receive_fields = function(pos,formname,fields,sender)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
|
|
|
|
--check for private channels
|
|
|
|
if fields.channel ~= nil then
|
|
|
|
local name, mode = fields.channel:match("^([^:;]+)([:;])")
|
|
|
|
if name and mode and name ~= sender:get_player_name() then
|
|
|
|
|
|
|
|
--channels starting with '[name]:' can only be used by the named player
|
|
|
|
if mode == ":" then
|
|
|
|
minetest.chat_send_player(sender:get_player_name(), "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name)
|
|
|
|
return
|
|
|
|
|
|
|
|
--channels starting with '[name];' can be used by other players, but cannot be received from
|
|
|
|
elseif mode == ";" and (meta:get_int("can_receive") ~= 0) == (fields["bt"] == nil) then
|
|
|
|
minetest.chat_send_player(sender:get_player_name(), "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.channel==nil then fields.channel=meta:get_string("channel") end
|
|
|
|
meta:set_string("channel",fields.channel)
|
|
|
|
remove_tube_in_file(pos)
|
|
|
|
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;]"..
|
2013-06-05 15:54:23 +02:00
|
|
|
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
|
|
|
|
"button[8,0;1,1;bt;On]")
|
2014-01-03 13:29:38 +01:00
|
|
|
else
|
|
|
|
meta:set_string("formspec","size[9,1;]"..
|
|
|
|
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
|
|
|
|
"button[8,0;1,1;bt;Off]")
|
2013-08-06 19:26:05 +02:00
|
|
|
end
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
add_tube_in_file(pos,fields.channel, cr)
|
|
|
|
end,
|
|
|
|
on_destruct = function(pos)
|
|
|
|
remove_tube_in_file(pos)
|
|
|
|
end})
|
|
|
|
|
|
|
|
if minetest.get_modpath("mesecons_mvps") ~= nil then
|
|
|
|
mesecon:register_on_mvps_move(function(moved_nodes)
|
|
|
|
for _, n in ipairs(moved_nodes) do
|
|
|
|
if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
|
|
|
|
update_pos_in_file(n.pos)
|
2013-06-05 15:54:23 +02:00
|
|
|
end
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|