2017-05-16 19:47:12 +02:00
|
|
|
|
|
|
|
--
|
2015-09-01 17:15:24 +02:00
|
|
|
-- Map handling
|
2017-05-16 19:47:12 +02:00
|
|
|
--
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2015-10-05 20:25:11 +02:00
|
|
|
nav.map_radius = 256
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
nav.waypoints = {}
|
|
|
|
|
|
|
|
local open_formspecs = {}
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Timer
|
|
|
|
|
|
|
|
local timer_interval = 0.1
|
2015-09-01 17:15:24 +02:00
|
|
|
local timer = 10
|
|
|
|
|
|
|
|
function nav.add_waypoint(pos, name, label, isinfo, type)
|
2017-05-16 19:47:12 +02:00
|
|
|
nav.waypoints[name] = {
|
|
|
|
pos = pos,
|
|
|
|
label = label,
|
|
|
|
isinfo = isinfo or false,
|
|
|
|
type = type
|
|
|
|
}
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function nav.remove_waypoint(name)
|
|
|
|
nav.waypoints[name] = nil
|
|
|
|
end
|
|
|
|
|
2015-11-27 16:34:59 +01:00
|
|
|
function nav.relocate_waypoint(name, pos)
|
|
|
|
nav.waypoints[name].pos = pos
|
|
|
|
end
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
function nav.get_waypoints_in_square(pos, radius)
|
|
|
|
local wpts = {}
|
|
|
|
|
|
|
|
for name, data in pairs(nav.waypoints) do
|
|
|
|
local wp = data.pos
|
2017-05-14 00:15:43 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
if wp.x > pos.x-radius and wp.x < pos.x+radius
|
|
|
|
and wp.z > pos.z-radius and wp.z < pos.z+radius then
|
2015-09-01 17:15:24 +02:00
|
|
|
table.insert(wpts, name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return wpts
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_formspec_waypoint(x, y, name, label, isinfo)
|
|
|
|
local img = "nav_waypoint.png"
|
|
|
|
if isinfo then
|
|
|
|
img = "nav_info.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
local form = ""
|
2017-05-14 00:15:43 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
form = form .. "image_button[" .. (x-0.72) .. "," .. (y-0.53) .. ";0.5,0.5;"
|
|
|
|
.. img .. ";" .. name .. ";;false;false;" .. img .. "]"
|
|
|
|
form = form .. "tooltip[" .. name .. ";" .. minetest.formspec_escape(label) .. "]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
return form
|
|
|
|
end
|
|
|
|
|
|
|
|
function nav.show_map(player)
|
2015-12-02 21:26:23 +01:00
|
|
|
if player == nil then return end
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
local name = player:get_player_name()
|
2015-12-02 21:26:23 +01:00
|
|
|
|
2015-11-05 00:47:28 +01:00
|
|
|
if not open_formspecs[name] then return end
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
local pos = player:getpos()
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
local form = default.ui.get_page("default:default")
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
form = form .. "field[-1,-1;0,0;nav_map_tracker;;]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
form = form .. "label[0.25,0.25;" .. minetest.formspec_escape(name)
|
|
|
|
.. " (x: " .. math.floor(pos.x+0.5)
|
|
|
|
.. ", y: " .. math.floor(pos.y)
|
|
|
|
.. ", z: " .. math.floor(pos.z+0.5) .. ")]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
form = form .. "image[0.5,3;6,6;nav_background.png]"
|
|
|
|
|
|
|
|
local wpts = nav.get_waypoints_in_square(pos, nav.map_radius)
|
|
|
|
for _, wptname in pairs(wpts) do
|
|
|
|
local wpt = nav.waypoints[wptname]
|
|
|
|
|
|
|
|
form = form .. get_formspec_waypoint(
|
|
|
|
3.5+(((wpt.pos.x-pos.x)/nav.map_radius)*3),
|
|
|
|
6+(((pos.z-wpt.pos.z)/nav.map_radius)*3),
|
|
|
|
wptname,
|
|
|
|
wpt.label,
|
2015-10-02 22:58:37 +02:00
|
|
|
wpt.isinfo)
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
form = form .. "image[5.5,3;1,1;nav_map_compass.png]"
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
form = form .. "label[6.25,6.6;" .. nav.map_radius .. "m]"
|
|
|
|
form = form .. "image[5.5,5.5;3,3;"
|
|
|
|
.. minetest.formspec_escape("nav_legend.png^[transformFX") .. "]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.show_formspec(name, "nav:map", form)
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Formspec interaction
|
|
|
|
|
|
|
|
local function on_player_recieve_fields(player, form_name, fields)
|
2016-02-10 22:18:54 +01:00
|
|
|
if form_name == "nav:map" then
|
2015-09-01 17:15:24 +02:00
|
|
|
if fields.quit or fields.nav_map_tracker then
|
|
|
|
open_formspecs[player:get_player_name()] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
if minetest.setting_get_pos("static_spawnpoint")
|
|
|
|
and (not minetest.is_singleplayer()) then
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.after(
|
2015-12-03 19:59:45 +01:00
|
|
|
1.0,
|
|
|
|
function()
|
2017-05-12 04:29:55 +02:00
|
|
|
nav.add_waypoint(minetest.setting_get_pos("static_spawnpoint"),
|
2015-12-03 19:59:45 +01:00
|
|
|
"spawn", "Spawn", true, "spawn")
|
2017-05-16 19:47:12 +02:00
|
|
|
end)
|
2015-12-03 19:59:45 +01:00
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Joining player
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
local function on_joinplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.after(
|
2015-09-01 17:15:24 +02:00
|
|
|
1.0,
|
|
|
|
function()
|
2017-05-16 19:47:12 +02:00
|
|
|
nav.add_waypoint(
|
|
|
|
player:getpos(),
|
|
|
|
"player_" .. name,
|
|
|
|
name,
|
|
|
|
true,
|
|
|
|
"player"
|
|
|
|
)
|
|
|
|
end)
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Disconnecting player
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
local function on_leaveplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
nav.remove_waypoint("player_" .. name)
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Global update function
|
|
|
|
|
|
|
|
local function on_globalstep(dtime)
|
2015-09-01 17:15:24 +02:00
|
|
|
timer = timer + dtime
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
if timer < timer_interval then
|
|
|
|
return
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
timer = 0
|
2017-05-14 00:15:43 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
local players = {}
|
2017-05-14 00:15:43 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
if player ~= nil then
|
|
|
|
local name = player:get_player_name()
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
players[name] = player
|
|
|
|
|
|
|
|
nav.show_map(player)
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
2017-05-16 19:47:12 +02:00
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
for wptname, wpt in pairs(nav.waypoints) do
|
|
|
|
if wpt.type == "player" then
|
|
|
|
if players[wpt.label] ~= nil and minetest.get_player_by_name(wpt.label) ~= nil then
|
|
|
|
nav.relocate_waypoint(wptname, players[wpt.label]:getpos())
|
|
|
|
end
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
|
|
|
minetest.register_on_leaveplayer(on_leaveplayer)
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(on_player_recieve_fields)
|
|
|
|
|
|
|
|
minetest.register_globalstep(on_globalstep)
|
|
|
|
|
|
|
|
-- Items
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_craftitem(
|
2015-10-02 22:58:37 +02:00
|
|
|
"nav:map",
|
|
|
|
{
|
|
|
|
description = "Map",
|
|
|
|
inventory_image = "nav_inventory.png",
|
|
|
|
wield_image = "nav_inventory.png",
|
|
|
|
stack_max = 1,
|
|
|
|
on_use = function(itemstack, player, pointed_thing)
|
2017-05-16 19:47:12 +02:00
|
|
|
open_formspecs[player:get_player_name()] = true
|
|
|
|
nav.show_map(player)
|
|
|
|
end,
|
|
|
|
})
|
2015-10-02 22:58:37 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Crafting
|
|
|
|
|
|
|
|
crafting.register_craft(
|
2015-09-01 17:15:24 +02:00
|
|
|
{
|
|
|
|
output = "nav:map",
|
2017-05-16 19:47:12 +02:00
|
|
|
items = {
|
|
|
|
"default:stick 6",
|
|
|
|
"default:paper 3",
|
2015-09-01 17:15:24 +02:00
|
|
|
}
|
2017-05-16 19:47:12 +02:00
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
|
2015-10-25 22:18:54 +01:00
|
|
|
-- Achievements
|
|
|
|
|
|
|
|
achievements.register_achievement(
|
|
|
|
"navigator",
|
|
|
|
{
|
|
|
|
title = "Navigator",
|
|
|
|
description = "Craft a map",
|
|
|
|
times = 1,
|
|
|
|
craftitem = "nav:map",
|
2017-05-16 19:47:12 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
default.log("map", "loaded")
|