140 lines
5.0 KiB
Lua
140 lines
5.0 KiB
Lua
local player_waypoints = {}
|
|
local update_interval = 1.31415
|
|
|
|
-- lua metatables? what is that? :P
|
|
-- local function get_compass_meta_id(meta)
|
|
-- return meta:get_string("waypoint_compass:id") ~= "" or "default_id"
|
|
-- end
|
|
|
|
|
|
local function get_compass_meta_label(meta)
|
|
local label = meta:get_string("waypoint_compass:label")
|
|
return label ~= "" and label or "destination"
|
|
end
|
|
|
|
|
|
local function set_compass_meta_label(meta, label)
|
|
meta:set_string("waypoint_compass:label", label)
|
|
end
|
|
|
|
|
|
local function pos_serialize(pos)
|
|
-- strip "(" and ")"
|
|
return string.sub(minetest.pos_to_string(pos), 2, -2)
|
|
end
|
|
|
|
|
|
local function pos_deserialize(str)
|
|
-- numbers are separated by ","
|
|
local tmp = {}
|
|
for n in string.gmatch(str, "([^,]+)") do
|
|
table.insert(tmp, n)
|
|
end
|
|
tmp[1] = tonumber(tmp[1]) or 0
|
|
tmp[2] = tonumber(tmp[2]) or 0
|
|
tmp[3] = tonumber(tmp[3]) or 0
|
|
return {x = tmp[1], y = tmp[2], z = tmp[3]}
|
|
end
|
|
|
|
|
|
local function set_compass_meta_pos(meta, pos)
|
|
local pos_str = pos_serialize(pos)
|
|
meta:set_string("waypoint_compass:position", pos_str)
|
|
end
|
|
|
|
|
|
local function get_compass_meta_pos(meta)
|
|
return pos_deserialize(meta:get_string("waypoint_compass:position"))
|
|
end
|
|
|
|
-- local function set_compass_meta_pos(meta, pos)
|
|
-- local pos_hash = minetest.hash_node_position(pos)
|
|
-- meta:set_int("waypoint_compass:position", pos_hash)
|
|
-- end
|
|
|
|
|
|
-- local function get_compass_meta_pos(meta)
|
|
-- return minetest.get_position_from_hash(meta:get_int("waypoint_compass:position"))
|
|
-- end
|
|
|
|
minetest.register_tool("waypoint_compass:compass", {
|
|
description = "Waypoint compass",
|
|
short_description = "Waypoint compass",
|
|
inventory_image = "Waypoint_compass_inventory_image.png",
|
|
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
|
range = 160, -- TODO what's the good range?
|
|
on_place = function(itemstack, placer, pointed_thing)
|
|
if pointed_thing and pointed_thing.type == "node" then
|
|
local pointed_pos = pointed_thing.above
|
|
local meta = itemstack:get_meta()
|
|
meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos)))
|
|
set_compass_meta_pos(meta, pointed_pos)
|
|
--set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
|
|
--print("meta pos", minetest.pos_to_string(get_compass_meta_pos(meta)))
|
|
return itemstack
|
|
end
|
|
end,
|
|
})
|
|
|
|
|
|
local function remove_hud_waypoint(player, player_name)
|
|
---print("hud remove")
|
|
local hud_id = player_waypoints[player_name].hud_id
|
|
player_waypoints[player_name] = nil
|
|
player:hud_remove(hud_id)
|
|
end
|
|
|
|
|
|
local function show_hud_waypoint(player, compass_item_meta)
|
|
local player_name = player:get_player_name()
|
|
-- Show this waypoint
|
|
local waypoint_name = get_compass_meta_label(compass_item_meta)
|
|
local hud_id = player:hud_add({
|
|
hud_elem_type = "waypoint",
|
|
name = waypoint_name,
|
|
text = "m",
|
|
precision=1,
|
|
number = 0xFC7D0A,
|
|
world_pos = get_compass_meta_pos(compass_item_meta),
|
|
})
|
|
-- store HUD elemnt id to remove it later
|
|
if not player_waypoints[player_name] then
|
|
player_waypoints[player_name] = {}
|
|
end
|
|
--print("hud_id add", hud_id)
|
|
player_waypoints[player_name].pos = get_compass_meta_pos(compass_item_meta)
|
|
player_waypoints[player_name].hud_id = hud_id
|
|
end
|
|
|
|
|
|
local timer = 0
|
|
minetest.register_globalstep(function(dtime)
|
|
timer = timer + dtime
|
|
if timer > update_interval then
|
|
for _,player in pairs(minetest.get_connected_players()) do
|
|
local player_name = player:get_player_name()
|
|
local item = player:get_wielded_item()
|
|
-- player is holding compass
|
|
if (item and item:get_name() == "waypoint_compass:compass") then
|
|
local meta = item:get_meta()
|
|
local waypoint_pos = get_compass_meta_pos(meta)
|
|
-- remove different waypoint if it exists
|
|
if player_waypoints[player_name] and
|
|
(pos_serialize(player_waypoints[player_name].pos) ~= pos_serialize(waypoint_pos)) then
|
|
remove_hud_waypoint(player, player_name)
|
|
end
|
|
-- show new waypoint
|
|
if not player_waypoints[player_name] then
|
|
show_hud_waypoint(player, meta)
|
|
end
|
|
else
|
|
-- not holding it anymore
|
|
if player_waypoints[player_name] then
|
|
remove_hud_waypoint(player, player_name)
|
|
end
|
|
end
|
|
end
|
|
timer = 0
|
|
end
|
|
end)
|