teleport-request/init.lua

474 lines
16 KiB
Lua
Raw Normal View History

--[[
Copyright (C) 2015-2019 Michael Tomaino (PlatinumArts@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
----------------------------------------------------------------------------
Originally made by Traxie21 and released with the WTFPL license.
Forum link: https://forum.minetest.net/viewtopic.php?id=4457
Updates by Zeno, Panquesito7 and ChaosWormz.
2019-08-01 00:41:45 +02:00
License: LGPL-2.1 for everything.
Optional dependencies: areas, intllib
New release by RobbieF under new mod: tps_teleport - http://blog.minetest.tv/teleport-request/
--]]
2014-07-26 08:20:32 +02:00
-- Load support for intllib.
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")
-- Load configuration.
dofile(MP.."/config.lua")
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-07-25 21:53:20 +02: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
2019-07-26 02:50:28 +02:00
-- Teleport player to a player (used in "/tpr" and in "/tphr" command).
function tpr_teleport_player()
2019-07-25 21:16:48 +02:00
local target_coords = source:get_pos()
local target_sound = target:get_pos()
target:set_pos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
minetest.sound_play("whoosh", {pos = target_sound, gain = 0.5, max_hear_distance = 10})
--parti2(target_coords)
end
2019-07-26 06:51:04 +02:00
-- TPC & TPJ
2019-07-25 21:53:20 +02:00
function tpc_teleport_player(player)
local pname = minetest.get_player_by_name(player)
minetest.sound_play("whoosh", {pos = pname:get_pos(), gain = 0.5, max_hear_distance = 10})
pname:set_pos(find_free_position_near(target_coords))
minetest.sound_play("whoosh", {pos = target_coords, gain = 0.5, max_hear_distance = 10})
2019-07-25 21:57:22 +02:00
--parti2(target_coords)
2019-07-25 21:53:20 +02:00
end
2019-07-26 02:38:31 +02:00
-- TPP
function tpp_teleport_player(player)
local pname = minetest.get_player_by_name(player)
minetest.sound_play("whoosh", {pos = pname:get_pos(), gain = 0.5, max_hear_distance = 10})
pname:set_pos(find_free_position_near(pos))
2019-07-26 02:38:31 +02:00
minetest.sound_play("whoosh", {pos = pos, gain = 0.5, max_hear_distance = 10})
--parti2(target_coords)
end
2019-07-25 22:57:54 +02:00
function find_free_position_near(pos)
2016-05-13 19:41:44 +02:00
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
2019-07-25 22:59:21 +02:00
function parti(pos)
2016-05-13 20:59:32 +02:00
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
2019-07-25 22:59:21 +02:00
function parti2(pos)
2016-05-13 20:59:32 +02:00
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
-- Teleport Request System
function clear_tpr_list(name)
if tpr_list[name] then
tpr_list[name] = nil
return
end
end
function clear_tphr_list(name)
if tphr_list[name] then
tphr_list[name] = nil
return
end
end
2019-07-29 01:03:32 +02:00
-- Clear requests when the player leaves
minetest.register_on_leaveplayer(function(name)
if tpr_list[name] then
tpr_list[name] = nil
return
end
if tphr_list[name] then
tphr_list[name] = nil
return
end
end)
function tpr_send(sender, receiver)
2019-07-28 02:41:04 +02:00
if minetest.check_player_privs(sender, {tp_admin = true}) and enable_immediate_teleport then
2019-07-15 03:04:30 +02:00
if receiver == "" then
minetest.chat_send_player(sender, S("Usage: /tpr <Player name>"))
2019-07-15 03:04:30 +02:00
return
end
if not minetest.get_player_by_name(receiver) then
minetest.chat_send_player(sender, S("There is no player by that name. Keep in mind this is case-sensitive, and the player must be online"))
2019-07-15 03:04:30 +02:00
return
end
2019-07-25 20:39:40 +02:00
tpr_list[receiver] = sender
tpr_accept(receiver)
minetest.chat_send_player(sender, S("You are teleporting to @1.", receiver))
return
end
2014-07-30 04:15:08 +02:00
if receiver == "" then
minetest.chat_send_player(sender, S("Usage: /tpr <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
minetest.chat_send_player(sender, S("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, S("@1 is requesting to teleport to you. /tpy to accept", sender))
minetest.chat_send_player(sender, S("Teleport request sent! It will timeout in @1 seconds", timeout_delay))
-- Write name values to list and clear old values.
2019-07-25 23:59:37 +02:00
tpr_list[receiver] = sender
-- Teleport timeout delay
minetest.after(timeout_delay, function(name)
if tpr_list[name] then
tpr_list[name] = nil
minetest.chat_send_player(sender, S("Request timed-out."))
minetest.chat_send_player(receiver, S("Request timed-out."))
return
end
end, receiver)
end
2014-07-30 06:01:44 +02:00
function tphr_send(sender, receiver)
2019-07-28 02:41:04 +02:00
if minetest.check_player_privs(sender, {tp_admin = true}) and enable_immediate_teleport then
2019-07-15 03:04:30 +02:00
if receiver == "" then
minetest.chat_send_player(sender, S("Usage: /tphr <Player name>"))
2019-07-15 03:04:30 +02:00
return
end
if not minetest.get_player_by_name(receiver) then
minetest.chat_send_player(sender, S("There is no player by that name. Keep in mind this is case-sensitive, and the player must be online"))
2019-07-15 03:04:30 +02:00
return
end
2019-07-25 20:39:40 +02:00
tphr_list[receiver] = sender
tpr_accept(receiver)
minetest.chat_send_player(sender, S("@1 is teleporting to you.", receiver))
return
end
2014-07-30 06:05:03 +02:00
if receiver == "" then
minetest.chat_send_player(sender, S("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
minetest.chat_send_player(sender, S("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, S("@1 is requesting that you teleport to them. /tpy to accept; /tpn to deny", sender))
2019-07-26 00:06:13 +02:00
minetest.chat_send_player(sender, S("Teleport request sent! It will timeout in @1 seconds", timeout_delay))
-- Write name values to list and clear old values.
2019-07-25 20:39:40 +02:00
tphr_list[receiver] = sender
-- Teleport timeout delay
minetest.after(timeout_delay, function(name)
if tphr_list[name] then
tphr_list[name] = nil
minetest.chat_send_player(sender, S("Request timed-out."))
minetest.chat_send_player(receiver, S("Request timed-out."))
return
end
end, receiver)
2014-07-26 08:20:32 +02:00
end
2019-07-12 03:50:48 +02:00
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, S("Usage: /tpc <x, y, z>"))
2016-05-10 20:20:28 +02:00
return nil
end
2019-07-25 21:53:20 +02:00
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(player, S("You cannot teleport to a location outside the map!"))
2019-03-24 16:38:32 +01:00
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
2019-07-25 21:53:20 +02:00
tpc_teleport_player(player)
minetest.chat_send_player(player, S("Teleporting to: @1, @2, @3", posx, posy, posz))
else
2019-07-12 03:59:46 +02:00
if minetest.check_player_privs(pname, {tp_tpc = true}) then
local protected = minetest.is_protected(target_coords,pname)
2019-07-12 03:59:46 +02:00
if protected and minetest.get_modpath("areas") then
if not areas:canInteract(target_coords, player) then
local owners = areas:getNodeOwners(target_coords)
minetest.chat_send_player(player, S("Error: @1 is protected by @2.", minetest.pos_to_string(target_coords), table.concat(owners, ", ")))
2019-07-12 03:59:46 +02:00
return
end
end
2019-07-25 21:53:20 +02:00
tpc_teleport_player(player)
minetest.chat_send_player(player, S("Teleporting to: @1, @2, @3", posx, posy, posz))
else
minetest.chat_send_player(player, S("Error: You do not have permission to teleport to coordinates."))
return
end
end
end
function tpr_deny(name)
if tpr_list[name] then
name2 = tpr_list[name]
minetest.chat_send_player(name2, S("Teleport request denied."))
minetest.chat_send_player(name, S("You denied the request @1 sent you.", name2))
2014-07-30 08:56:20 +02:00
tpr_list[name] = nil
elseif tphr_list[name] then
name2 = tphr_list[name]
minetest.chat_send_player(name2, S("Teleport request denied."))
minetest.chat_send_player(name, S("You denied the request @1 sent you.", name2))
2014-07-30 08:56:20 +02:00
tphr_list[name] = nil
else
minetest.chat_send_player(name, S("Usage: /tpn allows you to deny teleport requests sent to you by other players."))
2019-07-29 04:31:24 +02:00
return
2014-07-26 08:20:32 +02:00
end
end
2014-07-30 04:30:37 +02:00
-- Teleport Accept Systems
function tpr_accept(name, param)
-- Check to prevent constant teleporting.
if not tpr_list[name]
and not tphr_list[name]
or param == "" then
minetest.chat_send_player(name, S("Usage: /tpy <player>"))
2014-07-26 08:20:32 +02:00
return
end
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(param)
chatmsg = S("@1 is teleporting to you.", param)
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]
source = minetest.get_player_by_name(param)
2014-12-09 04:03:30 +01:00
target = minetest.get_player_by_name(name)
chatmsg = S("You are teleporting to @1.", param)
2014-07-30 06:01:44 +02:00
tphr_list[name] = nil
else
2014-07-26 08:20:32 +02:00
return
end
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
minetest.chat_send_player(name, S("@1 doesn't exist, or just disconnected/left (by timeout).", param))
2014-07-26 08:20:32 +02:00
return
end
2019-07-25 21:16:48 +02:00
tpr_teleport_player()
minetest.chat_send_player(param, S("Request Accepted!"))
minetest.chat_send_player(name, chatmsg)
2014-07-26 08:20:32 +02:00
end
-- Teleport Jump - Relative Position Teleportation by number of nodes
2019-07-12 03:50:48 +02:00
function tpj(player, param)
local pname = minetest.get_player_by_name(player)
if param == "" then
minetest.chat_send_player(player, S("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, S("Usage: <x|y|z> <Number>"))
return false
end
if not tonumber(args[2]) then
2019-07-25 23:07:39 +02:00
return false, S("Not a number!")
end
-- Initially generate the target coords from the player's current position (since it's relative) and then perform the math.
2019-07-25 21:53:20 +02:00
target_coords = minetest.get_player_by_name(player):get_pos()
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, S("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, S("You cannot teleport to a location outside the map!"))
2019-03-24 16:38:32 +01:00
return
end
2019-07-26 06:51:04 +02:00
tpc_teleport_player(player)
end
2016-05-18 21:10:23 +02:00
-- Evade
function tpe(player)
minetest.chat_send_player(player, S("EVADE!"))
2016-05-19 00:59:28 +02:00
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
2019-07-25 21:53:20 +02:00
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
2019-07-12 03:59:46 +02:00
-- Register chatcommands
if enable_tpp_command then
minetest.register_chatcommand("tpp", {
description = S("Teleport to a place (i.e., spawn, shop)."),
params = S("<place> | leave empty to see available places"),
privs = {},
func = function(player, param)
local pname = minetest.get_player_by_name(player)
param = param:lower()
2019-07-19 03:56:06 +02:00
-- Show the available places to the player (taken from shivajiva101's POI mod, thanks!).
if param == "" then
local places = {}
2019-07-26 00:50:03 +02:00
if not available_places then available_places = {} end
for key, value in pairs(available_places) do
table.insert(places, key)
end
if #places == 0 then
return true, S("There are no places yet.")
end
table.insert(places, S("Usage: /tpp <place>"))
return true, table.concat(places, "\n")
2019-07-19 03:56:06 +02:00
-- Teleport player to the specified place (taken from shivajiva101's POI mod, thanks!).
elseif available_places[param] then
2019-07-26 02:38:31 +02:00
pos = {x = available_places[param].x, y = available_places[param].y, z = available_places[param].z}
tpp_teleport_player(player)
minetest.chat_send_player(player, S("Teleporting to @1.", param))
-- Check if the place exists.
elseif not available_places[param] then
minetest.chat_send_player(player, S("There is no place by that name. Keep in mind this is case-sensitive."))
return
end
end,
})
end
2014-07-26 08:20:32 +02:00
minetest.register_chatcommand("tpr", {
description = S("Request teleport to another player"),
2019-07-16 21:26:28 +02:00
params = S("<playername> | leave playername empty to see help message"),
privs = {interact = true, tp = true},
2014-07-30 04:15:08 +02:00
func = tpr_send
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tphr", {
description = S("Request player to teleport to you"),
2019-07-16 21:26:28 +02:00
params = S("<playername> | leave playername empty to see help message"),
privs = {interact = true, tp = true},
2014-07-30 04:15:08 +02:00
func = tphr_send
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tpc", {
description = S("Teleport to coordinates"),
2019-07-16 21:26:28 +02:00
params = S("<coordinates> | leave coordinates empty to see help message"),
2019-07-09 03:30:16 +02:00
privs = {interact = true, tp_tpc = true, tp = true},
func = tpc_send
})
minetest.register_chatcommand("tpj", {
description = S("Teleport to relative position"),
2019-07-16 21:26:28 +02:00
params = S("<axis> <distance> | leave empty to see help message"),
2019-07-09 03:30:16 +02:00
privs = {interact = true, tp_tpc = true, tp = true},
func = tpj
})
2016-05-18 21:10:23 +02:00
minetest.register_chatcommand("tpe", {
description = S("Evade Enemy"),
2019-07-09 03:30:16 +02:00
privs = {interact = true, tp_tpc = true, tp = true},
2016-05-18 21:10:23 +02:00
func = tpe
})
2014-07-26 08:20:32 +02:00
minetest.register_chatcommand("tpy", {
description = S("Accept teleport requests from another player"),
privs = {interact = true, tp = true},
2014-07-30 04:15:08 +02:00
func = tpr_accept
2014-07-26 08:20:32 +02:00
})
minetest.register_chatcommand("tpn", {
description = S("Deny teleport requests from another player"),
privs = {interact = true, tp = 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
-- Log
if minetest.settings:get_bool("log_mods") then
2019-07-16 21:48:29 +02:00
minetest.log("action", S("[Teleport Request] TPS Teleport v@1 Loaded!", version))
end