waypoint_compass/init.lua

650 lines
22 KiB
Lua

local DEFAULT_DESCRIPTION = "Waypoint compass\n(sneak+place to set point)"
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
local WAYPOINT_ICON = "waypoint_compass_waypoint.png"
local WAYPOINT_SCALE = {x=-1/16*9,y=-1}
local waypoint_lib = dofile(minetest.get_modpath("waypoint_compass") .. DIR_DELIM .. "waypoint_lib.lua")
-- internal mod state
local player_waypoints = {} -- store a current player waypoint to see if it needs to be updated
-- Store a context for compass item that player is editing via dialog.
-- {wield_index = player:get_wield_index()}
local compass_dialog_context = {}
local function get_compass_meta_is_set(meta)
local pos_str = meta:get_string("waypoint_compass:position")
return pos_str ~= ""
end
local function get_compass_meta_pos(meta)
return minetest.string_to_pos(meta:get_string("waypoint_compass:position"))
end
local function get_compass_meta_owner(meta)
return meta:get_string("waypoint_compass:owner")
end
local function get_compass_meta_label(meta)
return meta:get_string("waypoint_compass:label")
end
local function update_compass_meta_label(meta)
if get_compass_meta_is_set(meta) then
local label = get_compass_meta_label(meta)
local owner = get_compass_meta_owner(meta) -- or "no owner"
if label == "" then
local pos = get_compass_meta_pos(meta)
if pos then
meta:set_string("description", string.format("Waypoint compass to %s [%s]",
minetest.pos_to_string(pos),
owner))
return
end
else
meta:set_string("description", string.format("Waypoint compass to \"%s\" [%s]", label, owner))
return
end
end
meta:set_string("description", DEFAULT_DESCRIPTION)
end
local function set_compass_meta_label(meta, label)
meta:set_string("waypoint_compass:label", label)
update_compass_meta_label(meta)
end
local function set_compass_meta_pos(meta, pos)
local pos_str = minetest.pos_to_string(pos)
meta:set_string("waypoint_compass:position", pos_str)
update_compass_meta_label(meta)
end
local function set_compass_meta_color(meta, color)
meta:set_int("waypoint_compass:color", color)
-- thanks to Boot@YL for suggesting this
meta:set_string("color", ("#%06X"):format(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)
update_compass_meta_label(meta)
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()
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
local function hide_hud_waypoint(player)
local player_name = player:get_player_name()
local hud = player_waypoints[player_name]
player_waypoints[player_name] = nil
hud.waypoint:hide(player)
player:hud_remove(hud.background)
end
local function show_hud_waypoint(player, compass_item_meta)
if not get_compass_meta_is_set(compass_item_meta) then
-- do not show unset compass position
return
end
local player_name = player:get_player_name()
local waypoint_pos = get_compass_meta_pos(compass_item_meta)
local waypoint_name = get_compass_meta_label(compass_item_meta)
-- show coords instead of name
if waypoint_name == "" and waypoint_pos then
waypoint_name = minetest.pos_to_string(waypoint_pos)
end
local waypoint_color = get_compass_meta_color(compass_item_meta)
local hexcolor = ("#%06X"):format(waypoint_color)
local size = {x=-9, y=-16}
local alignment = {x=100/9*1.47,y=100/16*1.63} -- near compass in your hand
local background = player:hud_add({
hud_elem_type = "image",
scale = size,
text = "waypoint_compass_background.png",
alignment = alignment,
})
local waypoint = waypoint_lib.WaypointHUD:new(player, waypoint_pos,
waypoint_name, waypoint_color,
WAYPOINT_ICON, WAYPOINT_SCALE, hexcolor,
size, alignment, "waypoint_compass_arrow_%03d.png", hexcolor, 9,
true)
waypoint:show(player)
-- store HUD elemnt id to remove it later
if player_waypoints[player_name] then
minetest.log("error","[MOD] waypoint_compass: " ..
player_name ..
" got their HUD stuck on screen?")
end
player_waypoints[player_name] = {waypoint = waypoint, background = background}
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] and
(player_waypoints[player_name].waypoint.point_pos ~= waypoint_pos
or force
or not get_compass_meta_is_set(meta)) 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)
-- check if wielded item is a compass and update waypoints
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)
minetest.register_on_leaveplayer(function(player, timed_out)
local player_name = player:get_player_name()
if player_name ~= "" then
-- delete info about players that are left
player_waypoints[player_name] = nil
compass_dialog_context[player_name] = nil
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
function trim(s)
-- code from lua-users.org
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
end
local function clamp(x, min, max)
return math.max(math.min(x, max), min)
end
local function coords_from_string(str)
-- parse "1,2,3" or "1 2 3" or "(1,2,3)" or "{x=1,y=2,z=3}" or return nil on fail
local function parse(str)
str = trim(str)
local result = nil
local first = string.sub(str, 1, 1)
local last = string.sub(str, -1, -1)
if first == "{" and last == "}" then
local maybe_pos = minetest.deserialize("return " .. str)
result = {x = maybe_pos.x, y = maybe_pos.y, z = maybe_pos.z}
end
if not result and first == "(" and last == ")" then
str = string.sub(str, 2, -2)
end
if not result then
-- numbers are separated by "," or " "
local tmp = {}
for n in string.gmatch(str, "([^, ]+)") do
table.insert(tmp, n)
end
result = {x = tonumber(tmp[1]), y = tonumber(tmp[2]), z = tonumber(tmp[3])}
end
-- if any of xyz are not numbers, this will throw an error and pcall will fail
result.x = clamp(result.x, -32000, 32000)
result.y = clamp(result.y, -32000, 32000)
result.z = clamp(result.z, -32000, 32000)
return result
end
local success, result = pcall(parse, str)
if success then
return result
else
return nil
end
end
local _format_recent_dropbox -- defined later
local function show_basic_dialog(itemstack, player)
local player_name = player:get_player_name()
local meta = itemstack:get_meta()
-- set focus to "close" button to prevent accidental edting
local formspec_head = "formspec_version[4]size[14,4]set_focus[close;]"
local coords = coords_to_string(get_compass_meta_pos(meta))
local field_coords = ("field[0.5,0.6;4,0.8;coords;Coordinates;%s]"):format(minetest.formspec_escape(coords))
local label = get_compass_meta_label(meta) or "destination"
local field_destination = ("field[4.5,0.6;7,0.8;label;Label;%s]"):format(minetest.formspec_escape(label))
local color = ("%06X"):format(get_compass_meta_color(meta))
local field_color = ("field[11.5,0.6;2.0,0.8;color;Color;%s]"):format(minetest.formspec_escape(color))
local dropdown_recent = ("dropdown[2.0,1.7;11.5,0.5;recent;%s;1,false]"):format(_format_recent_dropbox(player_name))
local formspec = {
formspec_head,
field_coords,
field_destination,
field_color,
"button_exit[0.5,1.7;1.5,0.5;set_recent;copy:]",
dropdown_recent,
"button_exit[3,2.7;3,0.8;save;save]",
"button_exit[8,2.7;3,0.8;close;close]"}
compass_dialog_context[player_name] = {
-- save index to check if player switched the item before formspec is shown
wield_index = player:get_wield_index(),
}
minetest.show_formspec(player:get_player_name(),
"waypoint_compass:basic",
table.concat(formspec, ""))
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)
local player_name = user:get_player_name()
if owner == "" or not get_compass_meta_pos(meta) then
-- set first user as owner or change it if pos is unset
set_compass_meta_owner(meta, player_name)
elseif owner ~= player_name then
-- already has owner
-- TODO show message "You are not the owner" (or maybe limit editing to color change?)
minetest.chat_send_player(player_name, "You are not the owner of this compass. Owner is " .. owner .. ".")
return itemstack
end
if user:get_player_control()["sneak"] then
if pointed_thing.type == "nothing" then
pointed_thing = waypoint_lib.raycast_crosshair(user, COMPASS_RANGE, POINT_TO_OBJECTS, POINT_TO_LIQUIDS)
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.set_recent or (fields.key_enter_field and fields.quit) then
local dialog_context = compass_dialog_context[player:get_player_name()]
if not dialog_context then
-- should not happen normally
minetest.log("error","[MOD] waypoint_compass: " ..
player:get_player_name() ..
" Closed compass dialog without opening it?")
return
end
local wield_index = dialog_context.wield_index
local wield_list = player:get_wield_list()
local inventory = player:get_inventory()
local compass_item = inventory:get_stack(wield_list, wield_index)
-- Prevent overwriting random item if user switched before
-- formspec appeared.
if compass_item:get_name() ~= "waypoint_compass:compass" then
minetest.chat_send_player(player:get_player_name(),
"What happened to your compass?")
else
local meta = compass_item:get_meta()
local color = tonumber("0x"..fields.color, 16)
if color then
color = math.max(math.min(color, 0xFFFFFF), 0x0) -- for some reason seems to work fine without this
set_compass_meta_color(meta, color)
-- print(dump(meta:to_table()))
end
local coords = coords_from_string(fields.coords)
local label = fields.label
-- player pressed "copy" button, ignore the "coordinates field"
if fields.set_recent then
local coord_string = fields.recent:match("%S+") or ""
coords = coords_from_string(coord_string)
label = fields.recent:match("(%S+ |[^>]+)>") or ""
end
if coords then
set_compass_meta_pos(meta, coords)
elseif fields.coords == "" then
-- assume player wants to reset compass
meta:set_string("waypoint_compass:position", "") -- TODO make unset()?
-- TODO reset label too? or keep it to be safe?
end
set_compass_meta_label(meta, label)
inventory:set_stack(wield_list, wield_index, compass_item)
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 = DEFAULT_DESCRIPTION,
--short_description = "Waypoint compass",
inventory_image = "waypoint_compass_inventory_image.png",
inventory_overlay = "waypoint_compass_inventory_image_overlay.png",
color = "#f6d0a5",
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,
})
minetest.register_craft({
type = "shaped",
output = "waypoint_compass:compass",
recipe = {
{"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
{"default:copper_ingot", "default:mese_crystal_fragment", "default:copper_ingot"},
{"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
}
})
minetest.register_craft({
type = "shapeless",
output = "waypoint_compass:compass",
recipe = {"waypoint_compass:compass", "waypoint_compass:compass"}
})
local function waypoint_compass_copy_meta(itemstack, player, old_craft_grid, craft_inv)
if itemstack:get_name() ~= "waypoint_compass:compass" then
return
end
-- detect new compass recipie
if old_craft_grid[1]:get_name() == "default:copper_ingot" then
-- probably a new compass recipie
return itemstack
end
local original
local index
for i = 1, #old_craft_grid do
local meta = old_craft_grid[i]:get_meta()
if get_compass_meta_is_set(meta) then
if not original then
original = old_craft_grid[i]
index = i
else
return ""
end
end
end
if not original then
return ""
end
local copymeta = original:get_meta():to_table()
itemstack:get_meta():from_table(copymeta)
set_compass_meta_owner(itemstack:get_meta(),
(player and player:get_player_name()) or
original:get_meta():get_string("waypoint_compass:owner"))
-- put the compass with metadata back in the craft grid
craft_inv:set_stack("craft", index, original)
end
minetest.register_on_craft(waypoint_compass_copy_meta)
minetest.register_craft_predict(waypoint_compass_copy_meta)
-- pos is {x=0,y=0,z=0}
-- owner_name is str
-- label is str
-- color is int in range [0,0xFFFFFF]
local function make_compass(pos, owner_name, label, color)
local itemstack = ItemStack("waypoint_compass:compass")
local meta = itemstack:get_meta()
if not (pos and
tonumber(pos.x) and
tonumber(pos.y) and
tonumber(pos.z) and
type(owner_name) == 'string' and
type(label) == 'string' and
color and
type(color) == "number" and
color >= 0 and
color <= 0xFFFFFF ) then
return nil
end
set_compass_meta_pos(meta, pos)
set_compass_meta_owner(meta, owner_name)
set_compass_meta_label(meta, label)
set_compass_meta_color(meta, color)
return itemstack
end
--------------------------------------------------------------------------------
-- TODO move chat scanner into separate file, make enabling optional
-- TODO API to add/rm "sticky" coordinates (that won't be pushed out of the list)?
local last_coords_log = {}
local last_coords_log_limit = 5 -- how many to keep
-- TODO when should we clear this? Should keep this even if player is
-- left (for some time at least)
-- Stores personal points for players
local last_coords_log_personal = {}
local function add_coords_log(pos, message)
table.insert(last_coords_log, 1, {pos=pos, message=message, timestamp=os.clock()})
last_coords_log[last_coords_log_limit] = nil
end
local function add_coords_log_personal(playername, pos, message)
if not last_coords_log_personal[playername] then
last_coords_log_personal[playername] = {}
end
local personal = last_coords_log_personal[playername]
table.insert(personal, 1, {pos=pos, message=message, timestamp=os.clock()})
personal[last_coords_log_limit] = nil
end
-- Trying to mach any three integer numbers separated at least by
-- spaces or punctuation.
--local FUZZY_COORD_PATTERN = ".-(%d+)%D?[%s.,:;]%D?(%d+)%D?[%s.,:;]%D?(%d+)"
local FUZZY_COORD_PATTERN = "(%-?%d+)[^.%d]?[%s,:;][^%d-]?(%-?%d+)[^.%d]?[%s,:;][^%d-]?(%-?%d+)"
local function fuzzy_parse_coords(str)
-- find any 3 numbers in a string separated by punctuarion/spaces
local x,y,z = str:match(FUZZY_COORD_PATTERN)
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)
if x and y and z then
return vector.new(
clamp(tonumber(x), -30912, 30927),
clamp(tonumber(y), -30912, 30927),
clamp(tonumber(z), -30912, 30927)
)
end
end
_format_recent_dropbox = function(playername)
local logs = table.copy(last_coords_log)
table.insert_all(logs, last_coords_log_personal[playername] or {})
table.sort(logs, function(a, b) return a.timestamp > b.timestamp end)
local out = {}
for _, v in ipairs(logs) do
local item = string.format("%d,%d,%d | %s",
v.pos.x, v.pos.y, v.pos.z,
v.message:sub(1,50))
table.insert(out, minetest.formspec_escape(item))
end
return table.concat(out,",")
end
local function scan_chat_for_coords(message)
local coords = fuzzy_parse_coords(message)
if coords then
add_coords_log(coords, message)
end
end
local old_chat_send_all = minetest.chat_send_all
function minetest.chat_send_all(text)
scan_chat_for_coords(text)
return old_chat_send_all(text)
end
local function scan_player_chat_for_coords(name, message)
local coords = fuzzy_parse_coords(message)
if coords then
add_coords_log_personal(name, coords, message)
end
end
local old_chat_send_player = minetest.chat_send_player
function minetest.chat_send_player(name, text)
scan_player_chat_for_coords(name, text)
return old_chat_send_player(name, text)
end
--------------------------------------------------------------------------------
waypoint_compass={}
function waypoint_compass.make_compass(pos, owner_name, label, color)
return make_compass(pos,owner_name, label, color)
end
-- A hook for "fake" global chat messages, that don't trigger the
-- on_chat_message hooks.
function waypoint_compass.process_message(message)
scan_chat_for_coords(message)
end
-- Add a custom point to global list (name/message can be any strings)
-- `pos` is table like {x=1,y=2,z=2}
function waypoint_compass.add_point_to_recent_global(pos, message)
local success, ret = pcall(add_coords_log, pos, message)
if not success then
minetest.log(
string.format(
"error [MOD] waypoint_compass: Failed add_point_to_recent_global(%s %s %s)",
pos, message))
end
return success
end
-- Add a custom point to personal player's list (name/message can be any strings)
-- `pos` is table like {x=1,y=2,z=2}
function waypoint_compass.add_point_to_recent_personal(playername, pos, message)
local success, ret = pcall(add_coords_log_personal, playername, pos, message)
if not success then
minetest.log(
string.format(
"error [MOD] waypoint_compass: Failed add_point_to_recent_personal(%s %s %s %s)",
playername, pos, message))
end
return success
end
-- -- Usage example:
-- waypoint_compass.add_point_to_recent_personal('singleplayer', {x=0,y=1,z=2}, "death from lava")
return waypoint_compass