2013-12-15 11:53:10 +04:00
|
|
|
local filename=minetest.get_worldpath() .. "/teleport_tubes"
|
2013-01-19 16:14:41 +04:00
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local tp_tube_db = nil -- nil forces a read
|
2013-01-19 16:14:41 +04:00
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local function read_tube_db()
|
|
|
|
local file = io.open(filename, "r")
|
|
|
|
if file ~= nil then
|
|
|
|
local file_content = file:read("*all")
|
|
|
|
io.close(file)
|
2013-01-19 16:14:41 +04:00
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
if file_content and file_content ~= "" then
|
|
|
|
tp_tube_db = minetest.deserialize(file_content)
|
|
|
|
return tp_tube_db-- we read sucessfully
|
2014-01-03 16:29:38 +04:00
|
|
|
end
|
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
tp_tube_db = {}
|
|
|
|
return tp_tube_db
|
2014-01-03 16:29:38 +04:00
|
|
|
end
|
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local function save_tube_db()
|
|
|
|
local file, err = io.open(filename, "w")
|
|
|
|
if file then
|
|
|
|
file:write(minetest.serialize(tp_tube_db))
|
|
|
|
io.close(file)
|
|
|
|
else
|
|
|
|
error(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- updates or adds a tube
|
|
|
|
local function set_tube(pos, channel, can_receive)
|
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
|
|
|
for _, val in ipairs(tubes) do
|
|
|
|
if val.x == pos.x and val.y == pos.y and val.z == pos.z then
|
|
|
|
val.channel = channel
|
|
|
|
val.cr = can_receive
|
|
|
|
save_tube_db()
|
2013-01-19 16:14:41 +04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
|
|
|
|
-- we haven't found any tp tube to update, so lets add it
|
|
|
|
table.insert(tp_tube_db,{x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=cr})
|
|
|
|
save_tube_db()
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local function remove_tube(pos)
|
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
2014-06-29 14:36:22 +04:00
|
|
|
local newtbl = {}
|
2015-01-21 00:58:46 +03:00
|
|
|
for _, val in ipairs(tubes) do
|
2014-06-29 14:36:22 +04:00
|
|
|
if val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z then
|
|
|
|
table.insert(newtbl, val)
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
tp_tube_db = newtbl
|
|
|
|
save_tube_db()
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
|
|
|
|
2014-06-29 14:36:22 +04: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
|
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local function get_receivers(pos,channel)
|
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
|
|
|
local receivers = {}
|
2014-06-29 14:36:22 +04:00
|
|
|
local changed = false
|
2015-01-21 00:58:46 +03:00
|
|
|
for _, val in ipairs(tubes) do
|
|
|
|
-- skip all non-receivers and the tube that it came from as early as possible, as this is called often
|
|
|
|
if (val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z)) then
|
|
|
|
local is_loaded = (minetest.get_node_or_nil(val) ~= nil)
|
|
|
|
local node_name = is_loaded and minetest.get_node(pos).name or read_node_with_vm(val)
|
|
|
|
|
|
|
|
if minetest.registered_nodes[node_name] and minetest.registered_nodes[node_name].is_teleport_tube then
|
|
|
|
table.insert(receivers, val)
|
|
|
|
else
|
|
|
|
val.to_remove = true
|
2014-06-29 14:36:22 +04:00
|
|
|
changed = true
|
|
|
|
end
|
2013-02-03 19:11:15 +04:00
|
|
|
end
|
2014-06-29 14:36:22 +04:00
|
|
|
end
|
|
|
|
if changed then
|
|
|
|
local updated = {}
|
2015-01-21 00:58:46 +03:00
|
|
|
for _, val in ipairs(tubes) do
|
2014-06-29 14:36:22 +04:00
|
|
|
if not val.to_remove then
|
|
|
|
table.insert(updated, val)
|
|
|
|
end
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
tp_tube_db = updated
|
|
|
|
save_tube_db()
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
return receivers
|
2013-01-19 16:14:41 +04:00
|
|
|
end
|
|
|
|
|
2013-12-15 11:53:10 +04:00
|
|
|
local teleport_noctr_textures={"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png",
|
2013-01-19 20:51:11 +04:00
|
|
|
"pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png","pipeworks_teleport_tube_noctr.png"}
|
2013-12-15 11:53:10 +04:00
|
|
|
local teleport_plain_textures={"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png",
|
2013-01-19 20:51:11 +04:00
|
|
|
"pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png","pipeworks_teleport_tube_plain.png"}
|
2013-12-15 11:53:10 +04:00
|
|
|
local teleport_end_textures={"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png",
|
2013-01-19 20:51:11 +04:00
|
|
|
"pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png","pipeworks_teleport_tube_end.png"}
|
2013-12-15 11:53:10 +04:00
|
|
|
local teleport_short_texture="pipeworks_teleport_tube_short.png"
|
|
|
|
local teleport_inv_texture="pipeworks_teleport_tube_inv.png"
|
2013-01-19 16:14:41 +04:00
|
|
|
|
2015-01-21 00:58:46 +03:00
|
|
|
local function set_teleport_tube_formspec(meta, can_receive)
|
|
|
|
local cr = (can_receive ~= 0)
|
2014-07-21 21:20:26 +04:00
|
|
|
meta:set_string("formspec","size[10.5,1;]"..
|
2015-01-21 00:58:46 +03:00
|
|
|
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
|
|
|
|
"button[8,0;2.5,1;"..(cr and "cr0" or "cr1")..";"..
|
2014-07-21 21:20:26 +04:00
|
|
|
(cr and "Send and Receive" or "Send only").."]")
|
|
|
|
end
|
|
|
|
|
2014-07-05 19:27:09 +04:00
|
|
|
pipeworks.register_tube("pipeworks:teleport_tube","Teleporting Pneumatic Tube Segment",teleport_plain_textures,
|
2014-01-03 16:29:38 +04:00
|
|
|
teleport_noctr_textures,teleport_end_textures,teleport_short_texture,teleport_inv_texture, {
|
2014-06-29 14:36:22 +04:00
|
|
|
is_teleport_tube = true,
|
2014-01-03 16:29:38 +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)
|
2014-01-03 16:29:38 +04:00
|
|
|
local channel = meta:get_string("channel")
|
2015-01-21 00:58:46 +03:00
|
|
|
local target = get_receivers(pos, channel)
|
2014-01-03 16:29:38 +04:00
|
|
|
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)
|
2015-01-21 00:58:46 +03:00
|
|
|
set_tube(pos, "", 1)
|
|
|
|
set_teleport_tube_formspec(meta, 1)
|
2014-01-03 16:29:38 +04:00
|
|
|
end,
|
|
|
|
on_receive_fields = function(pos,formname,fields,sender)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2015-01-21 00:58:46 +03:00
|
|
|
local can_receive = meta:get_int("can_receive")
|
|
|
|
|
|
|
|
if fields.channel then
|
|
|
|
-- check for private channels
|
|
|
|
local sender_name = sender:get_player_name()
|
2014-01-03 16:29:38 +04:00
|
|
|
local name, mode = fields.channel:match("^([^:;]+)([:;])")
|
2015-01-21 00:58:46 +03:00
|
|
|
if name and mode and name ~= sender_name then
|
2014-01-03 16:29:38 +04:00
|
|
|
--channels starting with '[name]:' can only be used by the named player
|
|
|
|
if mode == ":" then
|
2015-01-21 00:58:46 +03:00
|
|
|
minetest.chat_send_player(sender_name, "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name)
|
2014-01-03 16:29:38 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
--channels starting with '[name];' can be used by other players, but cannot be received from
|
2015-01-21 00:58:46 +03:00
|
|
|
elseif mode == ";" and (fields.cr1 or (can_receive ~= 0 and not fields.cr0)) then
|
|
|
|
minetest.chat_send_player(sender_name, "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name)
|
2014-01-03 16:29:38 +04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
-- save channel if we set one
|
|
|
|
meta:set_string("channel", fields.channel)
|
2014-01-03 16:29:38 +04:00
|
|
|
end
|
2015-01-21 00:58:46 +03:00
|
|
|
-- make sure we have a channel, either the newly set one or the one from metadata
|
|
|
|
local channel = fields.channel or meta:get_string("channel")
|
|
|
|
|
|
|
|
if fields.cr0 and can_receive ~= 0 then
|
|
|
|
can_receive = 0
|
|
|
|
meta:set_int("can_receive", can_receive)
|
|
|
|
elseif fields.cr1 and can_receive ~= 1 then
|
|
|
|
can_receive = 1
|
|
|
|
meta:set_int("can_receive", can_receive)
|
|
|
|
end
|
|
|
|
|
|
|
|
set_tube(pos, fields.channel, can_receive)
|
|
|
|
set_teleport_tube_formspec(meta, can_receive)
|
2014-01-03 16:29:38 +04:00
|
|
|
end,
|
|
|
|
on_destruct = function(pos)
|
2015-01-21 00:58:46 +03:00
|
|
|
remove_tube(pos)
|
|
|
|
end
|
|
|
|
})
|
2014-01-03 16:29:38 +04:00
|
|
|
|
|
|
|
if minetest.get_modpath("mesecons_mvps") ~= nil then
|
2014-11-22 21:55:54 +03:00
|
|
|
mesecon.register_on_mvps_move(function(moved_nodes)
|
2014-01-03 16:29:38 +04:00
|
|
|
for _, n in ipairs(moved_nodes) do
|
|
|
|
if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
|
2015-01-21 00:58:46 +03:00
|
|
|
local meta = minetest.get_meta(n.pos)
|
|
|
|
set_tube(n.pos, meta:get_string("channel"), meta:get_int("can_receive"))
|
2013-06-05 17:54:23 +04:00
|
|
|
end
|
2014-01-03 16:29:38 +04:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|