waypoint_compass/init.lua

308 lines
10 KiB
Lua

local player_waypoints = {}
local update_interval = 1.31415
local compass_precision = 10 -- set to 1 to show whole number or 10 for 1 decimal
local default_waypoint_color = 0xFFFFFF
local target_above = false -- place waypoint inside the block or above it
local point_to_objects = false -- unimplemented
local point_to_liquids = true
local compass_range = 180
-- 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 set_compass_meta_label(meta, label)
meta:set_string("waypoint_compass:label", label)
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_pos(meta, pos)
local pos_str = minetest.serialize(pos)
meta:set_string("waypoint_compass:position", pos_str)
end
local function get_compass_meta_pos(meta)
return minetest.deserialize(meta:get_string("waypoint_compass:position"))
end
local function set_compass_meta_color(meta, color)
meta:set_int("waypoint_compass:color", color)
end
local function get_compass_meta_color(meta)
local color = meta:get_int("waypoint_compass:color")
if color > 0 then
return color
else
return default_waypoint_color
end
end
local function set_compass_meta_owner(meta, player_name)
meta:set_string("waypoint_compass:owner", player_name)
end
local function get_compass_meta_owner(meta)
return meta:get_string("waypoint_compass:owner")
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
local function set_waypoint_at_pointed_place(itemstack, pointed_thing)
if pointed_thing and pointed_thing.type == "node" then
local pointed_pos = target_above and pointed_thing.above or pointed_thing.under
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)
-- TODO change only if default?
--set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
return itemstack
end
-- TODO handle entities?
end
-- return first thing player is pointing at
local function raycast_crosshair(player, range)
local p_pos = player:get_pos()
local p_eye_height = player:get_properties().eye_height
local p_eye_pos = { x = p_pos.x, y = p_pos.y + p_eye_height, z = p_pos.z }
local to = vector.add(p_eye_pos, vector.multiply(player:get_look_dir(), range))
local ray = minetest.raycast(p_eye_pos, to, point_to_objects, point_to_liquids)
local pointed_thing = ray:next()
return pointed_thing
-- while pointed_thing do
-- print(pointed_thing.type, minetest.pos_to_string(pointed_thing.under))
-- pointed_thing = ray:next()
-- end
end
local function hide_hud_waypoint(player)
local player_name = player:get_player_name()
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 waypoint_color = get_compass_meta_color(compass_item_meta)
local hud_id = player:hud_add({
hud_elem_type = "waypoint",
name = waypoint_name,
text = "m",
precision= compass_precision,
number = waypoint_color,
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
-- if item is a compass, then show stored waypoint
-- force - force hud update even if position is unchanged
local function update_hud_waypoint(player, itemstack, force)
if not player:is_player() then
return
end
local player_name = player:get_player_name()
-- player is holding compass
if (itemstack and itemstack:get_name() == "waypoint_compass:compass") then
local meta = itemstack:get_meta()
local waypoint_pos = get_compass_meta_pos(meta)
-- remove different waypoint if it exists
if player_waypoints[player_name] or force and
(player_waypoints[player_name].pos ~= waypoint_pos) then
hide_hud_waypoint(player)
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
hide_hud_waypoint(player)
end
end
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 itemstack = player:get_wielded_item()
update_hud_waypoint(player, itemstack)
end
timer = 0
end
end)
local function coords_to_string(pos)
-- strip "()"
if not pos then
pos = {x = 0, y = 0, z = 0}
end
return string.sub(minetest.pos_to_string(pos), 2, -2)
end
local function coords_from_string(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 compass_dialog_context = {}
local function show_basic_dialog(itemstack, player)
local meta = itemstack:get_meta()
-- set focus to "close" button to preven accidental edting
local formspec_head = "formspec_version[4]size[10,3]set_focus[close;]"
local coords = coords_to_string(get_compass_meta_pos(meta))
local field_coords = ("field[0.5,0.6;2.5,0.8;coords;Coords;%s]"):format(minetest.formspec_escape(coords))
local label = get_compass_meta_label(meta)
local field_destination = ("field[3,0.6;5,0.8;label;Label;%s]"):format(minetest.formspec_escape(label))
local color = ("%06X"):format(get_compass_meta_color(meta))
local field_color = ("field[8,0.6;1.5,0.8;color;Color;%s]"):format(minetest.formspec_escape(color))
minetest.show_formspec(player:get_player_name(), "waypoint_compass:basic",
formspec_head ..
field_coords ..
field_destination ..
field_color ..
"button_exit[1,1.7;3,0.8;save;save]" ..
"button_exit[6,1.7;3,0.8;close;close]")
local player_name = player:get_player_name()
compass_dialog_context[player_name] = itemstack
end
local function compass_use_callback(itemstack, user, pointed_thing)
if not (user and user:is_player()) then
return itemstack
end
local meta = itemstack:get_meta()
local owner = get_compass_meta_owner(meta)
print("owner", owner)
if owner == "" then
-- set first user as owner
print("set owner", user:get_player_name())
set_compass_meta_owner(meta, user:get_player_name())
elseif owner ~= user:get_player_name() then
-- already has owner
-- TODO show message "You are not the owner" (or maybe limit editing to color change?)
print("You are not the owner. Owner is " .. owner)
return itemstack
end
if user:get_player_control()["sneak"] then
if pointed_thing.type == "nothing" then
if user and user:is_player() then
pointed_thing = raycast_crosshair(user, compass_range)
end
end
set_waypoint_at_pointed_place(itemstack, pointed_thing)
update_hud_waypoint(user, itemstack)
else
if user:is_player() then
show_basic_dialog(itemstack, user)
end
end
return itemstack
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "waypoint_compass:basic" then
return
end
if fields.save or (fields.key_enter_field and fields.quit) then
local compass_item = compass_dialog_context[player:get_player_name()]
if not compass_item then
-- should not happen normally
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Closed compass dialog without opening it?")
return
end
local meta = compass_item:get_meta()
local color = tonumber("0x"..fields.color, 16)
if color then
set_compass_meta_color(meta, color)
end
local coords = coords_from_string(fields.coords)
if coords then
set_compass_meta_pos(meta, coords)
end
local label = fields.label
set_compass_meta_label(meta, label)
local success = player:set_wielded_item(compass_item)
if not success then
-- no idea why this can happen
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Failed to swap compass after editing it")
end
-- assume dialog is closed and reset context
compass_dialog_context[player:get_player_name()] = nil
update_hud_waypoint(player, compass_item, 'force')
end
end)
minetest.register_tool(
"waypoint_compass:compass", {
description = "Waypoint compass\n(sneak+place to set point)",
short_description = "Waypoint compass",
inventory_image = "Waypoint_compass_inventory_image.png",
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
range = 2.0, -- TODO what's the good range?
on_place = function(itemstack, placer, pointed_thing)
return compass_use_callback(itemstack, placer, pointed_thing)
end,
on_secondary_use = function(itemstack, user, pointed_thing)
return compass_use_callback(itemstack, user, pointed_thing)
end,
})