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") .. "/waypoint_lib.lua") -- internal mod state local player_waypoints = {} -- store a current player waypoint to see if it needs to be updated local compass_dialog_context = {} -- store a compass item that player is editing via dialog local function get_compass_meta_pos(meta) return minetest.string_to_pos(meta:get_string("waypoint_compass:position")) end local function set_compass_meta_label(meta, label) meta:set_string("waypoint_compass:label", label) if label == "" then local pos = get_compass_meta_pos(meta) if pos then meta:set_string("description", string.format("Waypoint compass to %s", minetest.pos_to_string(pos))) else meta:set_string("description", DEFAULT_DESCRIPTION) end else meta:set_string("description", string.format("Waypoint compass to \"%s\"", label)) end end local function get_compass_meta_label(meta) return meta:get_string("waypoint_compass:label") 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) if get_compass_meta_label(meta) == "" then meta:set_string("description", string.format("Waypoint compass to %s", minetest.pos_to_string(pos))) end end local function set_compass_meta_color(meta, color) meta:set_int("waypoint_compass:color", color) end 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_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_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 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[14,3]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)) minetest.show_formspec(player:get_player_name(), "waypoint_compass:basic", formspec_head .. field_coords .. field_destination .. field_color .. "button_exit[3,1.7;3,0.8;save;save]" .. "button_exit[8,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) local player_name = user:get_player_name() if owner == "" then -- set first user as owner 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 if user and user:is_player() then pointed_thing = waypoint_lib.raycast_crosshair(user, COMPASS_RANGE, POINT_TO_OBJECTS, POINT_TO_LIQUIDS) 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 color = math.max(math.min(color, 0xFFFFFF), 0x0) -- for some reason seems to work fine without this 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 = DEFAULT_DESCRIPTION, --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, })