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.png" local WAYPOINT_SCALE = {x=-1/16*9,y=-1} local waypoint_lib = dofile(minetest.get_modpath("waypoint_announce") .. "/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 set_compass_meta_label(meta, label) meta:set_string("description", string.format("Waypoint compass to \"%s\"", 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_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() 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 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:hide(player) end local function show_hud_waypoint(player, compass_item_meta) local player_name = player:get_player_name() local waypoint_pos = get_compass_meta_pos(compass_item_meta) if not get_compass_meta_is_set(compass_item_meta) then -- do not show unset compass position return end -- Show this waypoint local waypoint_name = get_compass_meta_label(compass_item_meta) 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 waypoint = waypoint_lib.WaypointHUD:new(player, waypoint_pos, waypoint_name, waypoint_color, WAYPOINT_ICON, WAYPOINT_SCALE, hexcolor, size, alignment, "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:get_player_name() .. " got their HUD stuck on screen?") end player_waypoints[player_name] = waypoint 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].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) 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 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 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) 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 = "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, })