2020-02-18 18:34:52 +01:00
|
|
|
local S = minetest.get_translator("pipeworks")
|
2013-12-15 08:53:10 +01:00
|
|
|
local filename=minetest.get_worldpath() .. "/teleport_tubes"
|
2013-01-19 13:14:41 +01:00
|
|
|
|
2015-01-20 22:58:46 +01:00
|
|
|
local tp_tube_db = nil -- nil forces a read
|
2015-01-25 23:40:23 +01:00
|
|
|
local tp_tube_db_version = 2.0
|
|
|
|
|
2021-03-18 07:51:10 +01:00
|
|
|
-- cached rceiver list: hash(pos) => {receivers}
|
|
|
|
local cache = {}
|
|
|
|
|
2015-01-25 23:40:23 +01:00
|
|
|
local function hash(pos)
|
2017-12-22 18:13:50 +01:00
|
|
|
return string.format("%.30g", minetest.hash_node_position(pos))
|
2015-01-25 23:40:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function save_tube_db()
|
|
|
|
local file, err = io.open(filename, "w")
|
|
|
|
if file then
|
|
|
|
tp_tube_db.version = tp_tube_db_version
|
|
|
|
file:write(minetest.serialize(tp_tube_db))
|
|
|
|
tp_tube_db.version = nil
|
|
|
|
io.close(file)
|
|
|
|
else
|
|
|
|
error(err)
|
|
|
|
end
|
2021-03-18 07:51:10 +01:00
|
|
|
-- reset tp-tube cache
|
|
|
|
cache = {}
|
2015-01-25 23:40:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function migrate_tube_db()
|
|
|
|
local tmp_db = {}
|
|
|
|
tp_tube_db.version = nil
|
2021-02-05 17:17:50 +01:00
|
|
|
for _, val in pairs(tp_tube_db) do
|
2015-01-25 23:40:23 +01:00
|
|
|
if(val.channel ~= "") then -- skip unconfigured tubes
|
|
|
|
tmp_db[hash(val)] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tp_tube_db = tmp_db
|
|
|
|
save_tube_db()
|
|
|
|
end
|
2013-01-19 13:14:41 +01:00
|
|
|
|
2015-01-20 22:58:46 +01: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 13:14:41 +01:00
|
|
|
|
2015-01-20 22:58:46 +01:00
|
|
|
if file_content and file_content ~= "" then
|
|
|
|
tp_tube_db = minetest.deserialize(file_content)
|
2015-01-25 23:40:23 +01:00
|
|
|
if(not tp_tube_db.version or tonumber(tp_tube_db.version) < tp_tube_db_version) then
|
|
|
|
migrate_tube_db()
|
|
|
|
end
|
|
|
|
tp_tube_db.version = nil -- we add it back when saving
|
|
|
|
return tp_tube_db -- we read sucessfully
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
end
|
2015-01-20 22:58:46 +01:00
|
|
|
tp_tube_db = {}
|
|
|
|
return tp_tube_db
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
|
2017-12-22 18:57:00 +01:00
|
|
|
-- debug formatter for coordinates used below
|
|
|
|
local fmt = function(pos)
|
|
|
|
return pos.x..", "..pos.y..", "..pos.z
|
|
|
|
end
|
|
|
|
|
2015-01-20 22:58:46 +01:00
|
|
|
-- updates or adds a tube
|
|
|
|
local function set_tube(pos, channel, can_receive)
|
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
2015-01-25 23:40:23 +01:00
|
|
|
local hash = hash(pos)
|
|
|
|
local tube = tubes[hash]
|
|
|
|
if tube then
|
|
|
|
tube.channel = channel
|
|
|
|
tube.cr = can_receive
|
|
|
|
save_tube_db()
|
|
|
|
return
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
2015-01-20 22:58:46 +01:00
|
|
|
|
|
|
|
-- we haven't found any tp tube to update, so lets add it
|
2017-12-22 18:57:00 +01:00
|
|
|
-- but sanity check that the hash has not already been inserted.
|
|
|
|
-- if so, complain very loudly and refuse the update so the player knows something is amiss.
|
|
|
|
-- to catch regressions of https://github.com/minetest-mods/pipeworks/issues/166
|
|
|
|
local existing = tp_tube_db[hash]
|
|
|
|
if existing ~= nil then
|
|
|
|
local e = "error"
|
|
|
|
minetest.log(e, "pipeworks teleport tube update refused due to position hash collision")
|
|
|
|
minetest.log(e, "collided hash: "..hash)
|
|
|
|
minetest.log(e, "tried-to-place tube: "..fmt(pos))
|
|
|
|
minetest.log(e, "existing tube: "..fmt(existing))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-01-25 23:40:23 +01:00
|
|
|
tp_tube_db[hash] = {x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=can_receive}
|
2015-01-20 22:58:46 +01:00
|
|
|
save_tube_db()
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
|
|
|
|
2015-01-20 22:58:46 +01:00
|
|
|
local function remove_tube(pos)
|
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
2015-01-25 23:40:23 +01:00
|
|
|
tubes[hash(pos)] = nil
|
2015-01-20 22:58:46 +01:00
|
|
|
save_tube_db()
|
2013-01-19 13:14:41 +01:00
|
|
|
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
|
|
|
|
|
2015-01-25 23:40:23 +01:00
|
|
|
local function get_receivers(pos, channel)
|
2021-03-18 07:51:10 +01:00
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
if cache[hash] then
|
|
|
|
-- re-use cached result
|
|
|
|
return cache[hash]
|
|
|
|
end
|
|
|
|
|
2015-01-20 22:58:46 +01:00
|
|
|
local tubes = tp_tube_db or read_tube_db()
|
|
|
|
local receivers = {}
|
2015-01-25 23:40:23 +01:00
|
|
|
local dirty = false
|
|
|
|
for key, val in pairs(tubes) do
|
2015-01-20 22:58:46 +01:00
|
|
|
-- 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
|
2015-01-25 23:40:23 +01:00
|
|
|
tp_tube_db[key] = nil
|
|
|
|
dirty = true
|
2014-06-29 12:36:22 +02:00
|
|
|
end
|
2013-02-03 16:11:15 +01:00
|
|
|
end
|
2014-06-29 12:36:22 +02:00
|
|
|
end
|
2015-01-25 23:40:23 +01:00
|
|
|
if dirty then
|
2015-01-20 22:58:46 +01:00
|
|
|
save_tube_db()
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
2021-03-18 07:51:10 +01:00
|
|
|
-- cache the result for next time
|
|
|
|
cache[hash] = receivers
|
2015-01-20 22:58:46 +01:00
|
|
|
return receivers
|
2013-01-19 13:14:41 +01:00
|
|
|
end
|
|
|
|
|
2015-01-29 18:57:34 +01:00
|
|
|
local function update_meta(meta, can_receive)
|
|
|
|
meta:set_int("can_receive", can_receive and 1 or 0)
|
|
|
|
local cr_state = can_receive and "on" or "off"
|
2021-04-02 21:47:48 +02:00
|
|
|
local itext = S("Channels are public by default").."\n"..
|
|
|
|
S("Use <player>:<channel> for fully private channels").."\n"..
|
|
|
|
S("Use <player>\\;<channel> for private receivers")
|
2022-04-16 20:51:59 +02:00
|
|
|
local size = "8.5,4"
|
2021-04-02 21:47:48 +02:00
|
|
|
meta:set_string("formspec",
|
2022-04-16 20:51:59 +02:00
|
|
|
"formspec_version[2]"..
|
|
|
|
"size["..size.."]"..
|
|
|
|
pipeworks.fs_helpers.get_prepends(size) ..
|
|
|
|
"image[0.5,o;1,1;pipeworks_teleport_tube_inv.png]"..
|
|
|
|
"label[1.5,0.5;"..S("Teleporting Tube").."]"..
|
|
|
|
"field[0.5,1.6;4.3,0.75;channel;"..S("Channel")..";${channel}]"..
|
|
|
|
"button[4.8,1.6;1.5,0.75;set_channel;"..S("Set").."]"..
|
|
|
|
"label[7.0,0.5;"..S("Receive").."]"..
|
|
|
|
"image_button[7.0,0.75;1,0.6;pipeworks_button_" .. cr_state .. ".png;cr" .. (can_receive and 0 or 1) .. ";;;false;pipeworks_button_interm.png]"..
|
|
|
|
"button_exit[6.3,1.6;2,0.75;close;"..S("Close").."]"..
|
|
|
|
"label[0.5,2.7;"..itext.."]")
|
2014-07-21 19:20:26 +02:00
|
|
|
end
|
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
pipeworks.register_tube("pipeworks:teleport_tube", {
|
2020-02-18 18:34:52 +01:00
|
|
|
description = S("Teleporting Pneumatic Tube Segment"),
|
2015-01-31 16:10:25 +01:00
|
|
|
inventory_image = "pipeworks_teleport_tube_inv.png",
|
|
|
|
noctr = { "pipeworks_teleport_tube_noctr.png" },
|
|
|
|
plain = { "pipeworks_teleport_tube_plain.png" },
|
|
|
|
ends = { "pipeworks_teleport_tube_end.png" },
|
|
|
|
short = "pipeworks_teleport_tube_short.png",
|
|
|
|
node_def = {
|
|
|
|
is_teleport_tube = true,
|
|
|
|
tube = {
|
|
|
|
can_go = function(pos,node,velocity,stack)
|
|
|
|
velocity.x = 0
|
|
|
|
velocity.y = 0
|
|
|
|
velocity.z = 0
|
|
|
|
|
|
|
|
local channel = minetest.get_meta(pos):get_string("channel")
|
|
|
|
if channel == "" then return {} end
|
|
|
|
|
|
|
|
local target = get_receivers(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
|
2022-06-23 18:06:01 +02:00
|
|
|
end,
|
|
|
|
on_repair = function(pos, node)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local channel = meta:get_string("channel")
|
|
|
|
minetest.swap_node(pos, { name = node.name, param2 = node.param2 })
|
|
|
|
pipeworks.scan_for_tube_objects(pos)
|
|
|
|
if channel ~= "" then
|
|
|
|
local can_receive = meta:get_int("can_receive")
|
|
|
|
set_tube(pos, channel, can_receive)
|
|
|
|
local cr_description = (can_receive == 1) and "sending and receiving" or "sending"
|
|
|
|
meta:set_string("infotext", S("Teleportation Tube @1 on '@2'", cr_description, channel))
|
|
|
|
end
|
2015-01-31 16:10:25 +01:00
|
|
|
end
|
|
|
|
},
|
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
update_meta(meta, true)
|
2021-04-02 21:47:48 +02:00
|
|
|
meta:set_string("infotext", S("Unconfigured Teleportation Tube"))
|
2015-01-31 16:10:25 +01:00
|
|
|
end,
|
|
|
|
on_receive_fields = function(pos,formname,fields,sender)
|
2015-03-05 11:14:30 +01:00
|
|
|
if not fields.channel -- ignore escaping or clientside manipulation of the form
|
2021-04-02 21:47:48 +02:00
|
|
|
or (fields.quit and not fields.key_enter_field)
|
2015-03-05 11:14:30 +01:00
|
|
|
or not pipeworks.may_configure(pos, sender) then
|
|
|
|
return
|
2015-01-31 16:10:25 +01:00
|
|
|
end
|
2015-02-04 15:37:20 +01:00
|
|
|
local new_channel = tostring(fields.channel):trim()
|
2015-01-25 20:05:10 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local can_receive = meta:get_int("can_receive")
|
|
|
|
|
|
|
|
-- check for private channels each time before actually changing anything
|
|
|
|
-- to not even allow switching between can_receive states of private channels
|
2015-02-02 19:33:41 +01:00
|
|
|
if new_channel ~= "" then
|
2015-01-31 16:10:25 +01:00
|
|
|
local sender_name = sender:get_player_name()
|
2015-02-02 19:33:41 +01:00
|
|
|
local name, mode = new_channel:match("^([^:;]+)([:;])")
|
2015-01-31 16:10:25 +01:00
|
|
|
if name and mode and name ~= sender_name then
|
|
|
|
--channels starting with '[name]:' can only be used by the named player
|
|
|
|
if mode == ":" then
|
2020-02-18 18:34:52 +01:00
|
|
|
minetest.chat_send_player(sender_name, S("Sorry, channel '@1' is reserved for exclusive use by @2",
|
|
|
|
new_channel, name))
|
2015-01-31 16:10:25 +01:00
|
|
|
return
|
2021-02-05 17:17:50 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
--channels starting with '[name];' can be used by other players, but cannot be received from
|
|
|
|
elseif mode == ";" and (fields.cr1 or (can_receive ~= 0 and not fields.cr0)) then
|
2020-02-18 18:34:52 +01:00
|
|
|
minetest.chat_send_player(sender_name, S("Sorry, receiving from channel '@1' is reserved for @2",
|
|
|
|
new_channel, name))
|
2015-01-31 16:10:25 +01:00
|
|
|
return
|
|
|
|
end
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
end
|
2015-01-20 22:58:46 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
local dirty = false
|
2015-01-25 20:05:10 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
-- was the channel changed?
|
|
|
|
local channel = meta:get_string("channel")
|
2021-04-02 21:47:48 +02:00
|
|
|
if new_channel ~= channel and (fields.key_enter_field == "channel" or fields.set_channel) then
|
2015-02-02 19:33:41 +01:00
|
|
|
channel = new_channel
|
2015-01-31 16:10:25 +01:00
|
|
|
meta:set_string("channel", channel)
|
|
|
|
dirty = true
|
|
|
|
end
|
2015-01-29 18:57:34 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
-- test if a can_receive button was pressed
|
|
|
|
if fields.cr0 and can_receive ~= 0 then
|
|
|
|
can_receive = 0
|
|
|
|
update_meta(meta, false)
|
|
|
|
dirty = true
|
|
|
|
elseif fields.cr1 and can_receive ~= 1 then
|
|
|
|
can_receive = 1
|
|
|
|
update_meta(meta, true)
|
|
|
|
dirty = true
|
|
|
|
end
|
2015-01-20 22:58:46 +01:00
|
|
|
|
2015-01-31 16:10:25 +01:00
|
|
|
-- save if we changed something, handle the empty channel while we're at it
|
|
|
|
if dirty then
|
|
|
|
if channel ~= "" then
|
|
|
|
set_tube(pos, channel, can_receive)
|
|
|
|
local cr_description = (can_receive == 1) and "sending and receiving" or "sending"
|
2020-02-20 15:17:58 +01:00
|
|
|
meta:set_string("infotext", S("Teleportation Tube @1 on '@2'", cr_description, channel))
|
2015-01-31 16:10:25 +01:00
|
|
|
else
|
|
|
|
-- remove empty channel tubes, to not have to search through them
|
|
|
|
remove_tube(pos)
|
2021-04-02 21:47:48 +02:00
|
|
|
meta:set_string("infotext", S("Unconfigured Teleportation Tube"))
|
2015-01-31 16:10:25 +01:00
|
|
|
end
|
2015-01-25 20:05:10 +01:00
|
|
|
end
|
2015-01-31 16:10:25 +01:00
|
|
|
end,
|
|
|
|
on_destruct = function(pos)
|
|
|
|
remove_tube(pos)
|
2015-01-25 20:05:10 +01:00
|
|
|
end
|
2015-01-31 16:10:25 +01:00
|
|
|
},
|
2015-01-20 22:58:46 +01:00
|
|
|
})
|
2015-01-29 20:10:45 +01:00
|
|
|
|
2014-01-03 13:29:38 +01:00
|
|
|
if minetest.get_modpath("mesecons_mvps") ~= nil then
|
2014-11-22 19:55:54 +01:00
|
|
|
mesecon.register_on_mvps_move(function(moved_nodes)
|
2014-01-03 13:29:38 +01:00
|
|
|
for _, n in ipairs(moved_nodes) do
|
|
|
|
if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
|
2015-01-20 22:58:46 +01:00
|
|
|
local meta = minetest.get_meta(n.pos)
|
|
|
|
set_tube(n.pos, meta:get_string("channel"), meta:get_int("can_receive"))
|
2013-06-05 15:54:23 +02:00
|
|
|
end
|
2014-01-03 13:29:38 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-08-09 22:09:31 +02:00
|
|
|
|
|
|
|
-- Expose teleport tube database API for other mods
|
|
|
|
pipeworks.tptube = {
|
|
|
|
hash = hash,
|
|
|
|
save_tube_db = save_tube_db,
|
|
|
|
get_db = function() return tp_tube_db or read_tube_db() end,
|
2022-01-02 15:14:14 +01:00
|
|
|
set_tube = set_tube,
|
|
|
|
update_meta = update_meta,
|
2020-08-09 22:09:31 +02:00
|
|
|
tp_tube_db_version = tp_tube_db_version
|
|
|
|
}
|