2015-04-25 07:01:40 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>
|
|
|
|
|
|
|
|
"warps" 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.
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2021-02-16 22:07:09 +01:00
|
|
|
local warps = {}
|
|
|
|
local warps_queue = {}
|
|
|
|
local queue_state = 0
|
2015-04-28 09:47:58 +02:00
|
|
|
local warps_freeze = 5
|
|
|
|
-- t = time in usec
|
|
|
|
-- p = player obj
|
|
|
|
-- w = warp name
|
|
|
|
|
2019-02-03 06:11:53 +01:00
|
|
|
local S = minetest.get_mod_storage()
|
|
|
|
assert(S, "mod_storage is required")
|
|
|
|
|
|
|
|
-- import warps or load
|
2021-02-16 22:07:09 +01:00
|
|
|
local function firstload()
|
|
|
|
local store = S:get("warps")
|
|
|
|
local worldpath = minetest.get_worldpath()
|
|
|
|
if store then
|
|
|
|
warps = minetest.deserialize(store)
|
|
|
|
return
|
|
|
|
end
|
2019-02-03 06:11:53 +01:00
|
|
|
local fh,err = io.open(worldpath .. "/warps.txt", "r")
|
|
|
|
if err then
|
2021-02-16 22:07:09 +01:00
|
|
|
-- If it doesn't exist, we've never used this mod before.
|
|
|
|
if not err:find("No such file or directory") then
|
|
|
|
minetest.log("error", "[warps] Error trying to load warps.txt: " .. err)
|
|
|
|
end
|
2019-02-03 06:11:53 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
while true do
|
|
|
|
local line = fh:read()
|
|
|
|
if line == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local paramlist = string.split(line, " ")
|
|
|
|
local w = {
|
|
|
|
name = paramlist[1],
|
|
|
|
x = tonumber(paramlist[2]),
|
|
|
|
y = tonumber(paramlist[3]),
|
|
|
|
z = tonumber(paramlist[4]),
|
|
|
|
yaw = tonumber(paramlist[5]),
|
|
|
|
pitch = tonumber(paramlist[6])
|
|
|
|
}
|
|
|
|
table.insert(warps, w)
|
|
|
|
end
|
|
|
|
fh:close()
|
|
|
|
minetest.log("action", "[warps] converted warps to mod_storage. Please delete 'warps.txt'.")
|
|
|
|
S:set_string("warps", minetest.serialize(warps))
|
|
|
|
end
|
|
|
|
|
2016-04-06 20:43:27 +02:00
|
|
|
local function lookup_warp(name)
|
2015-04-28 09:47:58 +02:00
|
|
|
for i = 1,table.getn(warps) do
|
2016-04-06 20:43:27 +02:00
|
|
|
if warps[i].name == name then
|
|
|
|
return warps[i]
|
2015-04-28 09:47:58 +02:00
|
|
|
end
|
|
|
|
end
|
2016-04-06 20:43:27 +02:00
|
|
|
end
|
|
|
|
|
2016-06-27 22:09:27 +02:00
|
|
|
local function round_digits(n, digits)
|
|
|
|
digits = digits or 0
|
|
|
|
|
|
|
|
local multi = math.pow(10, digits)
|
|
|
|
n = n * multi
|
|
|
|
if n > 0 then
|
|
|
|
return math.floor(n + 0.5) / multi
|
|
|
|
else
|
|
|
|
return math.ceil(n - 0.5) / multi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-06 20:43:27 +02:00
|
|
|
local warp = function(player, dest)
|
|
|
|
local warp = lookup_warp(dest)
|
|
|
|
if not warp then
|
|
|
|
minetest.chat_send_player(player:get_player_name(), "Unknown warp \"" .. dest .. "\"")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-06-27 22:09:27 +02:00
|
|
|
local pos = vector.new(warp)
|
|
|
|
pos.y = pos.y + 0.5
|
2021-04-04 23:57:31 +02:00
|
|
|
player:set_pos(pos)
|
2017-10-03 18:05:12 +02:00
|
|
|
player:set_look_horizontal(warp.yaw)
|
|
|
|
player:set_look_vertical(warp.pitch)
|
2016-04-06 20:43:27 +02:00
|
|
|
minetest.chat_send_player(player:get_player_name(), "Warped to \"" .. dest .. "\"")
|
|
|
|
minetest.log("action", player:get_player_name() .. " warped to \"" .. dest .. "\"")
|
|
|
|
minetest.sound_play("warps_plop", {pos = pos})
|
2015-04-28 09:47:58 +02:00
|
|
|
end
|
|
|
|
|
2021-02-16 22:07:09 +01:00
|
|
|
local function do_warp_queue()
|
2015-04-28 09:47:58 +02:00
|
|
|
if table.getn(warps_queue) == 0 then
|
|
|
|
queue_state = 0
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local t = minetest.get_us_time()
|
|
|
|
for i = table.getn(warps_queue),1,-1 do
|
|
|
|
local e = warps_queue[i]
|
2021-04-04 23:57:31 +02:00
|
|
|
if e.p:get_pos() then
|
|
|
|
if vector.equals(e.p:get_pos(), e.pos) then
|
2015-09-09 13:41:11 +02:00
|
|
|
if t > e.t then
|
|
|
|
warp(e.p, e.w)
|
|
|
|
table.remove(warps_queue, i)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
minetest.sound_stop(e.sh)
|
2016-06-27 22:09:27 +02:00
|
|
|
minetest.chat_send_player(e.p:get_player_name(),
|
|
|
|
"You have to stand still for " .. warps_freeze .. " seconds!")
|
2015-04-28 09:47:58 +02:00
|
|
|
table.remove(warps_queue, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if table.getn(warps_queue) == 0 then
|
|
|
|
queue_state = 0
|
|
|
|
return
|
|
|
|
end
|
|
|
|
minetest.after(1, do_warp_queue)
|
|
|
|
end
|
|
|
|
|
|
|
|
local warp_queue_add = function(player, dest)
|
|
|
|
table.insert(warps_queue, {
|
2016-06-27 22:09:27 +02:00
|
|
|
t = minetest.get_us_time() + (warps_freeze * 1000000),
|
2021-04-04 23:57:31 +02:00
|
|
|
pos = player:get_pos(),
|
2015-04-28 09:47:58 +02:00
|
|
|
p = player,
|
2015-04-29 06:15:56 +02:00
|
|
|
w = dest,
|
2021-04-04 23:57:31 +02:00
|
|
|
sh = minetest.sound_play("warps_woosh", { pos = player:get_pos() })
|
2015-04-28 09:47:58 +02:00
|
|
|
})
|
|
|
|
minetest.chat_send_player(player:get_player_name(), "Don't move for " .. warps_freeze .. " seconds!")
|
|
|
|
if queue_state == 0 then
|
|
|
|
queue_state = 1
|
|
|
|
minetest.after(1, do_warp_queue)
|
|
|
|
end
|
2016-04-06 20:43:27 +02:00
|
|
|
-- attempt to emerge the target area before the player gets there
|
2016-06-27 22:09:27 +02:00
|
|
|
local pos = vector.new(lookup_warp(dest))
|
2016-04-06 20:43:27 +02:00
|
|
|
minetest.get_voxel_manip():read_from_map(pos, pos)
|
|
|
|
if not minetest.get_node_or_nil(pos) then
|
|
|
|
minetest.emerge_area(vector.subtract(pos, 80), vector.add(pos, 80))
|
|
|
|
end
|
2019-02-03 05:47:23 +01:00
|
|
|
-- force mapblock send to player, if supported
|
|
|
|
if player.send_mapblock then
|
2021-02-16 22:07:09 +01:00
|
|
|
player:send_mapblock(vector.divide(pos, 16))
|
2019-02-03 05:47:23 +01:00
|
|
|
end
|
2015-04-28 09:47:58 +02:00
|
|
|
end
|
2015-04-25 07:01:40 +02:00
|
|
|
|
|
|
|
minetest.register_privilege("warp_admin", {
|
|
|
|
description = "Allows modification of warp points",
|
|
|
|
give_to_singleplayer = true,
|
|
|
|
default = false
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_privilege("warp_user", {
|
|
|
|
description = "Allows use of warp points",
|
|
|
|
give_to_singleplayer = true,
|
|
|
|
default = true
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("setwarp", {
|
|
|
|
params = "name",
|
|
|
|
description = "Set a warp location to the players location",
|
|
|
|
privs = { warp_admin = true },
|
|
|
|
func = function(name, param)
|
2015-09-09 17:04:03 +02:00
|
|
|
param = param:gsub("%W", "")
|
2016-06-27 22:09:27 +02:00
|
|
|
if param == "" then
|
|
|
|
return false, "Cannot set warp: Name missing."
|
|
|
|
end
|
|
|
|
|
|
|
|
local h = "Created"
|
2015-04-25 07:01:40 +02:00
|
|
|
for i = 1,table.getn(warps) do
|
|
|
|
if warps[i].name == param then
|
|
|
|
table.remove(warps, i)
|
2016-06-27 22:09:27 +02:00
|
|
|
h = "Changed"
|
2015-04-25 07:01:40 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2016-06-27 22:09:27 +02:00
|
|
|
|
2015-04-25 07:01:40 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2021-04-04 23:57:31 +02:00
|
|
|
local pos = vector.round(player:get_pos())
|
2016-06-27 22:09:27 +02:00
|
|
|
table.insert(warps, {
|
|
|
|
name = param,
|
|
|
|
x = pos.x,
|
|
|
|
y = pos.y,
|
|
|
|
z = pos.z,
|
2017-10-03 18:05:12 +02:00
|
|
|
yaw = round_digits(player:get_look_horizontal(), 3),
|
|
|
|
pitch = round_digits(player:get_look_vertical(), 3)
|
2016-06-27 22:09:27 +02:00
|
|
|
})
|
2019-02-03 06:11:53 +01:00
|
|
|
S:set_string("warps", minetest.serialize(warps))
|
2016-06-27 22:09:27 +02:00
|
|
|
|
|
|
|
minetest.log("action", name .. " " .. h .. " warp \"" .. param .. "\": " ..
|
|
|
|
pos.x .. ", " .. pos.y .. ", " .. pos.z)
|
2015-04-28 09:47:58 +02:00
|
|
|
return true, h .. " warp \"" .. param .. "\""
|
2015-04-25 07:01:40 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("delwarp", {
|
|
|
|
params = "name",
|
|
|
|
description = "Set a warp location to the players location",
|
|
|
|
privs = { warp_admin = true },
|
|
|
|
func = function(name, param)
|
|
|
|
for i = 1,table.getn(warps) do
|
|
|
|
if warps[i].name == param then
|
|
|
|
table.remove(warps, i)
|
2015-04-28 09:47:58 +02:00
|
|
|
minetest.log("action", name .. " removed warp \"" .. param .. "\"")
|
2015-04-25 07:01:40 +02:00
|
|
|
return true, "Removed warp \"" .. param .. "\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false, "Unknown warp location \"" .. param .. "\""
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("listwarps", {
|
|
|
|
params = "name",
|
|
|
|
description = "List known warp locations",
|
|
|
|
privs = { warp_user = true },
|
|
|
|
func = function(name, param)
|
|
|
|
local s = "List of known warp locations:\n"
|
|
|
|
for i = 1,table.getn(warps) do
|
|
|
|
s = s .. "- " .. warps[i].name .. "\n"
|
|
|
|
end
|
|
|
|
return true, s
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("warp", {
|
|
|
|
params = "name",
|
|
|
|
description = "Warp to a warp location",
|
|
|
|
privs = { warp_user = true },
|
|
|
|
func = function(name, param)
|
2015-04-28 09:47:58 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
2016-01-04 08:18:23 +01:00
|
|
|
if not minetest.check_player_privs(player, {warp_admin = true}) then
|
|
|
|
warp_queue_add(player, param)
|
|
|
|
else
|
|
|
|
warp(player, param)
|
|
|
|
end
|
2015-04-25 07:01:40 +02:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2021-02-16 22:07:09 +01:00
|
|
|
local function prepare_dropdown(x,y,w,h,curr_dest)
|
|
|
|
local dd = string.format("dropdown[%f,%f;%f,%f;ddwarp;", x, y, w, h)
|
|
|
|
local sel = 0
|
|
|
|
for idx, warp in ipairs(warps) do
|
|
|
|
local warpname = warp.name
|
|
|
|
dd = dd .. minetest.formspec_escape(warpname) .. ","
|
|
|
|
if curr_dest == warpname then
|
|
|
|
sel = idx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dd = dd .. ";"..tostring(sel).."]"
|
|
|
|
return dd
|
|
|
|
end
|
|
|
|
|
|
|
|
local function prepare_formspec(dest)
|
|
|
|
local custdest = ""
|
|
|
|
if not lookup_warp(dest) then
|
|
|
|
custdest = dest
|
|
|
|
end
|
|
|
|
return "size[4.5,3]label[0.7,0;Warp destination]"
|
|
|
|
.."field[1,2.2;3,0.2;destination;Future destination;"
|
|
|
|
..minetest.formspec_escape(custdest).."]"
|
|
|
|
.."button_exit[0.7,2.7;3,0.5;proceed;Proceed]"
|
|
|
|
..prepare_dropdown(0.7,0.4,3,1, dest)
|
|
|
|
end
|
|
|
|
|
2015-04-28 07:08:26 +02:00
|
|
|
minetest.register_node("warps:warpstone", {
|
|
|
|
visual = "mesh",
|
|
|
|
mesh = "warps_warpstone.obj",
|
|
|
|
description = "A Warp Stone",
|
|
|
|
tiles = { "warps_warpstone.png" },
|
|
|
|
drawtype = "mesh",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
walkable = false,
|
|
|
|
paramtype = "light",
|
|
|
|
groups = { choppy=3 },
|
2015-04-29 06:15:56 +02:00
|
|
|
light_source = 8,
|
2019-02-03 06:13:19 +01:00
|
|
|
diggable = false,
|
2015-04-28 07:08:26 +02:00
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {-0.25, -0.5, -0.25, 0.25, 0.5, 0.25}
|
|
|
|
},
|
|
|
|
on_construct = function(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2021-02-16 22:07:09 +01:00
|
|
|
meta:set_string("formspec", prepare_formspec(""))
|
2015-04-28 09:47:58 +02:00
|
|
|
meta:set_string("infotext", "Uninitialized Warp Stone")
|
2015-04-28 07:08:26 +02:00
|
|
|
end,
|
|
|
|
on_receive_fields = function(pos, formname, fields, sender)
|
|
|
|
if not minetest.check_player_privs(sender:get_player_name(), {warp_admin = true}) then
|
|
|
|
minetest.chat_send_player(sender:get_player_name(), "You do not have permission to modify warp stones")
|
|
|
|
return false
|
|
|
|
end
|
2021-02-16 22:07:09 +01:00
|
|
|
if not (fields.destination and fields.quit) then
|
2015-04-28 08:45:18 +02:00
|
|
|
return
|
|
|
|
end
|
2021-02-16 22:07:09 +01:00
|
|
|
|
|
|
|
local dest
|
|
|
|
if fields.destination == "" and fields.ddwarp then
|
|
|
|
dest = fields.ddwarp
|
|
|
|
else
|
|
|
|
dest = fields.destination
|
|
|
|
end
|
|
|
|
|
2015-04-28 07:08:26 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2021-02-16 22:07:09 +01:00
|
|
|
|
|
|
|
meta:set_string("formspec", prepare_formspec(dest))
|
|
|
|
meta:set_string("infotext", "Warp stone to " .. dest)
|
|
|
|
meta:set_string("warps_destination", dest)
|
|
|
|
minetest.log("action", sender:get_player_name() .. " changed warp stone at "
|
|
|
|
.. minetest.pos_to_string(pos) .. " to \"" .. dest .. "\"")
|
2015-04-28 07:08:26 +02:00
|
|
|
end,
|
|
|
|
on_punch = function(pos, node, puncher, pointed_thingo)
|
2016-06-27 22:09:27 +02:00
|
|
|
if puncher:get_player_control().sneak and
|
|
|
|
minetest.check_player_privs(puncher:get_player_name(), {warp_admin = true}) then
|
2015-04-28 08:45:18 +02:00
|
|
|
minetest.remove_node(pos)
|
|
|
|
minetest.chat_send_player(puncher:get_player_name(), "Warp stone removed!")
|
|
|
|
return
|
|
|
|
end
|
2016-06-27 22:09:27 +02:00
|
|
|
|
2015-04-28 07:08:26 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local destination = meta:get_string("warps_destination")
|
2021-02-16 22:07:09 +01:00
|
|
|
if destination == "" or lookup_warp(destination) == nil then
|
2016-06-27 22:09:27 +02:00
|
|
|
minetest.chat_send_player(puncher:get_player_name(),
|
|
|
|
"Unknown warp location for this warp stone, cannot warp!")
|
2015-04-28 08:45:18 +02:00
|
|
|
return false
|
2015-04-28 07:08:26 +02:00
|
|
|
end
|
2021-02-16 22:07:09 +01:00
|
|
|
minetest.log("action", string.format("Going to warp player %s to waypoint %s",
|
|
|
|
puncher:get_player_name(), destination
|
|
|
|
))
|
2015-04-28 09:47:58 +02:00
|
|
|
warp_queue_add(puncher, destination)
|
2015-04-28 07:08:26 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2021-02-16 22:07:09 +01:00
|
|
|
firstload()
|