diff --git a/init.lua b/init.lua index 5547ec5..d1a6bed 100644 --- a/init.lua +++ b/init.lua @@ -2,69 +2,107 @@ local player_waypoints = {} local update_interval = 1.31415 -- lua metatables? what is that? :P -local function get_compass_meta_id(meta) - return meta:get_string("waypoint_compass:id") ~= "" or "default_id" +-- local function get_compass_meta_id(meta) +-- return meta:get_string("waypoint_compass:id") ~= "" or "default_id" +-- 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 get_compass_meta_name(meta) - return meta:get_string("waypoint_compass:name") ~= "" or "destination" +local function set_compass_meta_label(meta, label) + meta:set_string("waypoint_compass:label", label) end + +local function pos_serialize(pos) + -- strip "(" and ")" + return string.sub(minetest.pos_to_string(pos), 2, -2) +end + + +local function pos_deserialize(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 set_compass_meta_pos(meta, pos) + local pos_str = pos_serialize(pos) + meta:set_string("waypoint_compass:position", pos_str) +end + + local function get_compass_meta_pos(meta) - return {x=0, y=0, z=0} + return pos_deserialize(meta:get_string("waypoint_compass:position")) 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 minetest.register_tool("waypoint_compass:compass", { description = "Waypoint compass", - short_description = "Steel Axe", + short_description = "Waypoint compass", inventory_image = "Waypoint_compass_inventory_image.png", - on_use = function(itemstack, user, pointed_thing) - -- FIXME test code for checking how setting the description works - if user and user:is_player() then - local player = user + range = 100, + on_place = function(itemstack, placer, pointed_thing) + if pointed_thing and pointed_thing.type == "node" then + local pointed_pos = pointed_thing.above local meta = itemstack:get_meta() - local pos = get_compass_meta_pos(meta) - local new_description = string.format("%f (%f,%f,%f)", math.random(), pos.x, pos.y, pos.z) - meta:set_string("description", new_description) - meta:set_string("short_description", new_description) - print("on use", new_description) - --player:set_wielded_item(nil) - --player:set_wielded_item(itemstack) + meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos))) + set_compass_meta_pos(meta, pointed_pos) + --set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos)) + --print("meta pos", minetest.pos_to_string(get_compass_meta_pos(meta))) return itemstack end - end + end, }) local function remove_hud_waypoint(player, player_name) - print("hud remove") + ---print("hud remove") 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_name(compass_item_meta) + local waypoint_name = get_compass_meta_label(compass_item_meta) local hud_id = player:hud_add({ hud_elem_type = "waypoint", name = waypoint_name, text = "m", precision=1, - number = 0xFF, --0xFFFFFF, + number = 0xFC7D0A, 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].id = get_compass_meta_id(compass_item_meta) + --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 - print("id set", player_waypoints[player_name].id) end @@ -78,10 +116,10 @@ minetest.register_globalstep(function(dtime) -- player is holding compass if (item and item:get_name() == "waypoint_compass:compass") then local meta = item:get_meta() - local waypoint_id = get_compass_meta_id(meta) + local waypoint_pos = get_compass_meta_pos(meta) -- remove different waypoint if it exists - print(player_waypoints[player_name] and player_waypoints[player_name].id) - if player_waypoints[player_name] and (player_waypoints[player_name].id ~= waypoint_id) then + if player_waypoints[player_name] and + (pos_serialize(player_waypoints[player_name].pos) ~= pos_serialize(waypoint_pos)) then remove_hud_waypoint(player, player_name) end -- show new waypoint @@ -89,6 +127,7 @@ minetest.register_globalstep(function(dtime) show_hud_waypoint(player, meta) end else + -- not holding it anymore if player_waypoints[player_name] then remove_hud_waypoint(player, player_name) end