local st=minetest.get_mod_storage()
local playerwp={}

local function update_wp(name, pos)
	if not name then return end
	local player=minetest.get_player_by_name(name)
	if not player then return end
	if playerwp[name] then
		player:hud_remove(playerwp[name])
		playerwp[name]=nil
	end
	if pos then
		playerwp[name]=player:hud_add {
			hud_elem_type="waypoint",
			name="+",
			number=0x808080,
			precision=1,
			world_pos=pos }
	end
end

minetest.register_on_joinplayer(function(player)
	local name=player:get_player_name()
	local p=minetest.deserialize(st:get_string(name))
	if p then update_wp(name, p) end
	end)

minetest.register_chatcommand("wp",
{
	param="<name> or *",
	description="Sets the custom waypoint of a player to yours (if they don't already have one set); '*' clears your own waypoint.",
	privs={interact=true},
	func = function(name, param)
		if not playerwp[name] then return false, "Your waypoint is not set" end
		if param=="*" then
			st:set_string(name, "")
			update_wp(name, nil)
			return true, "Waypoint cleared"
		end
		if not minetest.player_exists(param) then return false, "Who?" end
		if playerwp[param] then return false, "Player's waypoint already in use." end
		local pos=minetest.deserialize(st:get_string(name))
		st:set_string(param, minetest.serialize(pos))
		update_wp(param, pos)
		return true, "Waypoint set"
	end

})

minetest.register_craftitem("compass:compass", {
	description = "Compass.\nDetects nearby ores when activated\nWhen used on a beacon (same clan): sets secondary waypoint.\nGives heading, time and date.\nSingle-use.\n",
	inventory_image = "bronze_compass.png",
	sound = {breaks = "default_tool_breaks"},
	groups={not_repaired_by_anvil=1},
	on_use=function(itemstack, player)
		local name=player:get_player_name()
		local t=minetest.get_timeofday()*1440
		local bearing=math.floor(player:get_look_horizontal()*180/math.pi)
		--[[
		if minetest.find_node_near(player:get_pos(), 3, {name="group:ore"}) then
			bearing="???"
		end
		--]]
		minetest.chat_send_player(name,"Bearing: "..bearing.." Altitude: "..math.floor(player:get_pos().y)..string.format(" Time: Moon %d, %02d:%02d",minetest.get_day_count(), t/60,t%60).." Light level: "..minetest.get_node_light(player:get_pos()))

		local zero={x=0, y=0, z=0}
		local pos=player:get_pos()
		local vol=5
		local p1={x=pos.x-vol, y=pos.y-vol, z=pos.z-vol}
		local p2={x=pos.x+vol, y=pos.y+vol, z=pos.z+vol}
		for _,ore in ipairs(minetest.find_nodes_in_area(p1, p2, {"group:ore"})) do
			minetest.add_particlespawner
			{
				amount=200,
				time=20,
				minpos=ore,
				maxpos=ore,
				minvel={x=-5,y=-5,z=-5},
				maxvel={x=5,y=5,z=5},
				minacc=zero,
				maxacc=zero,
				minsize=4,
				maxsize=4,
				glow=7,
				texture="default_mineral_mese.png",
				playername=player:get_player_name()
			}
		end
		--itemstack:add_wear(65536/64)
		itemstack:take_item()
		return itemstack
	end,

	on_place=function(itemstack, clicker, pointed_thing)
		if clicker and pointed_thing then
			local pos=pointed_thing.under
			local node=minetest.get_node(pos).name
			if node ~="beacon:beacon" then return itemstack end
			local name=clicker:get_player_name()
			if name and not mf.clan_ok(name, pos) then
				return itemstack
			end
			st:set_string(name, minetest.serialize(pos))
			update_wp(name, pos)
			itemstack:take_item()
			return itemstack
		end
	end
})

--[[ Not craftable, loot crates only.
minetest.register_craft{
	output = "compass:compass",
	recipe = {
		{ "", "default:gold_ingot", "" },
		{ "default:steel_ingot", "default:mese_crystal", "default:tin_ingot" },
		{ "", "default:copper_ingot", "" },
	}
}
]]
