teleport-request/init.lua

334 lines
11 KiB
Lua
Raw Normal View History

-- Originally Teleport Request by Traxie21 and released with the WTFPL license
2014-07-31 06:41:44 +02:00
-- https://forum.minetest.net/viewtopic.php?id=4457
-- Updates by Zeno and ChaosWormz
-- New release by RobbieF under new mod: tps_teleport - http://blog.minetest.tv/teleport-request/
2014-07-26 08:20:32 +02:00
local timeout_delay = 60
2014-07-26 08:20:32 +02:00
local version = "1.5"
2014-07-26 08:20:32 +02:00
2014-07-30 04:30:37 +02:00
local tpr_list = {}
local tphr_list = {}
2014-07-26 08:20:32 +02:00
2019-03-24 16:38:32 +01:00
local map_size = 30912
local function can_teleport(to)
return to.x < map_size and to.x > -map_size and to.y < map_size and to.y > -map_size and to.z < map_size and to.z > -map_size
end
minetest.register_privilege("tp_admin", {
description = "Admin overrides for tps_teleport.",
give_to_singleplayer=false
})
minetest.register_privilege("tp_tpc", {
description = "Allow player to teleport to coordinates (if permitted by area protection).",
give_to_singleplayer=true
})
2016-05-13 19:41:44 +02:00
local function find_free_position_near(pos)
local tries = {
{x=1,y=0,z=0},
{x=-1,y=0,z=0},
{x=0,y=0,z=1},
{x=0,y=0,z=-1},
}
for _,d in pairs(tries) do
local p = vector.add(pos, d)
if not minetest.registered_nodes[minetest.get_node(p).name].walkable then
return p, true
end
end
return pos, false
end
2016-05-13 20:59:32 +02:00
local function parti(pos)
minetest.add_particlespawner(50, 0.4,
{x=pos.x + 0.5, y=pos.y, z=pos.z + 0.5}, {x=pos.x - 0.5, y=pos.y, z=pos.z - 0.5},
{x=0, y=5, z=0}, {x=0, y=0, z=0},
{x=0, y=5, z=0}, {x=0, y=0, z=0},
3, 5,
3, 5,
false,
"tps_portal_parti.png")
end
local function parti2(pos)
minetest.add_particlespawner(50, 0.4,
{x=pos.x + 0.5, y=pos.y + 10, z=pos.z + 0.5}, {x=pos.x - 0.5, y=pos.y, z=pos.z - 0.5},
{x=0, y=-5, z=0}, {x=0, y=0, z=0},
{x=0, y=-5, z=0}, {x=0, y=0, z=0},
3, 5,
3, 5,
false,
"tps_portal_parti.png")
end
2014-07-30 06:01:44 +02:00
--Teleport Request System
local function tpr_send(sender, receiver)
2014-07-30 04:15:08 +02:00
if receiver == "" then
minetest.chat_send_player(sender, "Usage: /tpr <Player name>")
return
end
2014-07-26 08:20:32 +02:00
if not minetest.get_player_by_name(receiver) then
2016-05-18 13:49:07 +02:00
minetest.chat_send_player(sender, "There is no player by that name. Keep in mind this is case sensitive, and the player must be online.")
return
2014-07-30 04:15:08 +02:00
end
2014-07-26 08:20:32 +02:00
minetest.chat_send_player(receiver, sender ..' is requesting to teleport to you. /tpy to accept.')
minetest.chat_send_player(sender, 'Teleport request sent! It will time out in '.. timeout_delay ..' seconds.')
2014-07-26 08:20:32 +02:00
--Write name values to list and clear old values.
tpr_list[receiver] = sender
--Teleport timeout delay
minetest.after(timeout_delay, function(name)
if tpr_list[name] then
tpr_list[name] = nil
end
end, sender)
end
2014-07-30 06:01:44 +02:00
local function tphr_send(sender, receiver)
2014-07-30 06:05:03 +02:00
if receiver == "" then
minetest.chat_send_player(sender, "Usage: /tphr <Player name>")
2014-07-30 04:15:08 +02:00
return
end
2014-07-26 08:20:32 +02:00
if not minetest.get_player_by_name(receiver) then
2016-05-18 13:49:07 +02:00
minetest.chat_send_player(sender, "There is no player by that name. Keep in mind this is case sensitive, and the player must be online.")
return
2014-07-30 04:15:08 +02:00
end
minetest.chat_send_player(receiver, sender ..' is requesting that you teleport to them. /tpy to accept; /tpn to deny')
minetest.chat_send_player(sender, 'Teleport request sent! It will time out in '.. timeout_delay ..' seconds.')
--Write name values to list and clear old values.
tphr_list[receiver] = sender
--Teleport timeout delay
minetest.after(timeout_delay, function(name)
if tphr_list[name] then
tphr_list[name] = nil
end
end, sender)
2014-07-26 08:20:32 +02:00
end
local function tpc_send(player,coordinates)
local posx,posy,posz = string.match(coordinates, "^(-?%d+),(-?%d+),(-?%d+)$")
local pname = minetest.get_player_by_name(player)
2016-05-10 19:58:24 +02:00
if posx ~= nil or posy ~= nil or posz ~= nil then
posx = tonumber(posx) + 0.0
posy = tonumber(posy) + 0.0
posz = tonumber(posz) + 0.0
end
2016-05-10 20:20:28 +02:00
if posx==nil or posy==nil or posz==nil or string.len(posx) > 6 or string.len(posy) > 6 or string.len(posz) > 6 then
minetest.chat_send_player(player, "Usage: /tpc <x,y,z>")
return nil
end
local target_coords={x=posx, y=posy, z=posz}
2019-03-24 16:38:32 +01:00
if can_teleport(target_coords) == false then
minetest.chat_send_player("You cannot teleport to a location outside the map!")
return nil
end
-- If the area is protected, reject the user's request to teleport to these coordinates
-- In future release we'll actually query the player who owns the area, if they're online, and ask for their permission.
-- Admin user (priv "tp_admin") overrides all protection
if minetest.check_player_privs(pname, {tp_admin=true}) then
2016-05-13 20:09:57 +02:00
minetest.chat_send_player(player, 'Teleporting to '..posx..','..posy..','..posz)
pname:setpos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
2017-01-24 09:12:42 +01:00
--parti2(target_coords)
else
if minetest.check_player_privs(pname, {tp_tpc=true}) then
local protected = minetest.is_protected(target_coords,pname)
if protected then
if not areas:canInteract(target_coords, player) then
local owners = areas:getNodeOwners(target_coords)
minetest.chat_send_player(player,("Error: %s is protected by %s."):format(minetest.pos_to_string(target_coords),table.concat(owners, ", ")))
return
end
end
minetest.chat_send_player(player, 'Teleporting to '..posx..','..posy..','..posz)
pname:setpos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
2017-01-24 09:12:42 +01:00
--parti2(target_coords)
else
minetest.chat_send_player(player, "Error: You do not have permission to teleport to coordinates.")
return
end
end
end
local function tpr_deny(name)
if tpr_list[name] then
2014-07-30 08:56:20 +02:00
minetest.chat_send_player(tpr_list[name], 'Teleport request denied.')
tpr_list[name] = nil
2014-07-26 08:20:32 +02:00
end
if tphr_list[name] then
2014-07-30 08:56:20 +02:00
minetest.chat_send_player(tphr_list[name], 'Teleport request denied.')
tphr_list[name] = nil
2014-07-26 08:20:32 +02:00
end
end
2014-07-30 04:30:37 +02:00
2014-07-30 06:01:44 +02:00
--Teleport Accept Systems
local function tpr_accept(name, param)
2014-07-26 08:20:32 +02:00
2014-07-30 04:15:08 +02:00
--Check to prevent constant teleporting.
if not tpr_list[name]
and not tphr_list[name] then
minetest.chat_send_player(name, "Usage: /tpy allows you to accept teleport requests sent to you by other players.")
2014-07-26 08:20:32 +02:00
return
end
2014-07-30 04:15:08 +02:00
local chatmsg, source, target, name2
2014-07-30 04:15:08 +02:00
2014-07-26 08:20:32 +02:00
if tpr_list[name] then
2014-07-30 06:01:44 +02:00
name2 = tpr_list[name]
2014-12-09 04:03:30 +01:00
source = minetest.get_player_by_name(name)
target = minetest.get_player_by_name(name2)
2014-07-30 06:01:44 +02:00
chatmsg = name2 .. " is teleporting to you."
2014-07-26 08:20:32 +02:00
tpr_list[name] = nil
2014-07-30 06:01:44 +02:00
elseif tphr_list[name] then
name2 = tphr_list[name]
2014-12-09 04:03:30 +01:00
source = minetest.get_player_by_name(name2)
target = minetest.get_player_by_name(name)
2014-07-30 06:01:44 +02:00
chatmsg = "You are teleporting to " .. name2 .. "."
tphr_list[name] = nil
else
2014-07-26 08:20:32 +02:00
return
end
2014-07-30 04:15:08 +02:00
2014-07-30 06:01:44 +02:00
-- Could happen if either player disconnects (or timeout); if so just abort
if not source
or not target then
2014-07-26 08:20:32 +02:00
return
end
2014-07-30 06:01:44 +02:00
minetest.chat_send_player(name2, "Request Accepted!")
minetest.chat_send_player(name, chatmsg)
2016-05-14 00:08:55 +02:00
local target_coords=source:getpos()
target:setpos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
2017-01-24 09:12:42 +01:00
--parti2(target_coords)
2014-07-26 08:20:32 +02:00
end
-- Teleport Jump - Relative Position Teleportation by number of nodes
2016-05-18 21:35:25 +02:00
local function tpj(player,param)
local pname = minetest.get_player_by_name(player)
if param == "" then
minetest.chat_send_player(player, "Usage. <X|Y|Z> <Number>")
return false
end
2016-05-18 23:17:05 +02:00
local args = param:split(" ") -- look into this. Can it crash if the player does not have two parameters?
if #args < 2 then
minetest.chat_send_player(player, "Usage. <X|Y|Z> <Number>")
return false
end
if not tonumber(args[2]) then
return false, "Not a Number!"
end
-- Initially generate the target coords from the player's current position (since it's relative) and then perform the math.
2016-05-18 21:55:38 +02:00
local target_coords = minetest.get_player_by_name(player):getpos()
if args[1] == "x" then
target_coords["x"] = target_coords["x"] + tonumber(args[2])
elseif args[1] == "y" then
target_coords["y"] = target_coords["y"] + tonumber(args[2])
elseif args[1] == "z" then
target_coords["z"] = target_coords["z"] + tonumber(args[2])
else
minetest.chat_send_player(player,"Not a valid axis. Valid options are X, Y or Z.")
2019-03-24 16:38:32 +01:00
return
end
if can_teleport(target_coords) == false then
minetest.chat_send_player(player, "You cannot teleport to a location outside the map!")
return
end
2019-03-24 16:38:32 +01:00
pname:setpos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
--parti2(target_coords)
end
2016-05-18 21:10:23 +02:00
-- Evade
2016-05-18 21:35:25 +02:00
local function tpe(player)
2016-05-19 00:59:28 +02:00
minetest.chat_send_player(player, "EVADE!")
local mindistance = 15
local maxdistance = 50
local times = math.random(6,20) -- how many times to jump - minimum,maximum
2016-05-18 23:10:58 +02:00
local negatives = { '-','' } -- either it's this way or that way: the difference between -10 and 10
2016-05-18 22:48:43 +02:00
local options = { 'x', 'y', 'z' }
2016-05-18 22:31:08 +02:00
local isnegative = ''
local distance = 0
local axis = ''
local iteration = 0
2016-05-18 21:10:23 +02:00
for i = 1,times do
2016-05-18 22:31:08 +02:00
-- do this every 1 second
minetest.after(iteration,
2016-05-18 22:31:08 +02:00
function()
2016-05-18 22:48:43 +02:00
isnegative = negatives[math.random(2)] -- choose randomly whether this is this way or that
2016-05-18 22:31:08 +02:00
distance = isnegative .. math.random(mindistance,maxdistance) -- the distance to jump
axis = options[math.random(3)]
local command = axis .. " " .. distance
tpj(player,command)
2016-05-18 22:31:08 +02:00
end
)
iteration = iteration + 0.5
2016-05-18 21:10:23 +02:00
end
end
2014-07-26 08:20:32 +02:00
minetest.register_chatcommand("tpr", {
2014-07-30 04:15:08 +02:00
description = "Request teleport to another player",
params = "<playername> | leave playername empty to see help message",
privs = {interact=true},
func = tpr_send
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tphr", {
description = "Request player to teleport to you",
2014-07-30 04:15:08 +02:00
params = "<playername> | leave playername empty to see help message",
privs = {interact=true},
func = tphr_send
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tpc", {
description = "Teleport to coordinates",
params = "<coordinates> | leave coordinates empty to see help message",
privs = {interact=true,tp_tpc=true},
func = tpc_send
})
minetest.register_chatcommand("tpj", {
description = "Teleport to relative position",
params = "<axis> <distance> | leave empty to see help message",
2017-01-24 09:22:31 +01:00
privs = {interact=true,tp_tpc=true},
func = tpj
})
2016-05-18 21:10:23 +02:00
minetest.register_chatcommand("tpe", {
description = "Evade Enemy",
2017-01-24 09:22:31 +01:00
privs = {interact=true,tp_tpc=true},
2016-05-18 21:10:23 +02:00
func = tpe
})
2014-07-26 08:20:32 +02:00
minetest.register_chatcommand("tpy", {
2014-07-30 04:15:08 +02:00
description = "Accept teleport requests from another player",
privs = {interact=true},
2014-07-30 04:15:08 +02:00
func = tpr_accept
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tpn", {
2014-07-30 04:15:08 +02:00
description = "Deny teleport requests from another player",
privs = {interact=true},
2014-07-30 04:15:08 +02:00
func = tpr_deny
2014-07-26 08:20:32 +02:00
})
2014-07-30 04:30:37 +02:00
minetest.log("info", "[Teleport Request] TPS Teleport v" .. version .. " Loaded.")