set new position on right click, store pos and label in item meta
This commit is contained in:
parent
dc151342fd
commit
1f92dac762
93
init.lua
93
init.lua
@ -2,69 +2,107 @@ local player_waypoints = {}
|
|||||||
local update_interval = 1.31415
|
local update_interval = 1.31415
|
||||||
|
|
||||||
-- lua metatables? what is that? :P
|
-- lua metatables? what is that? :P
|
||||||
local function get_compass_meta_id(meta)
|
-- local function get_compass_meta_id(meta)
|
||||||
return meta:get_string("waypoint_compass:id") ~= "" or "default_id"
|
-- 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
|
end
|
||||||
|
|
||||||
|
|
||||||
local function get_compass_meta_name(meta)
|
local function set_compass_meta_label(meta, label)
|
||||||
return meta:get_string("waypoint_compass:name") ~= "" or "destination"
|
meta:set_string("waypoint_compass:label", label)
|
||||||
end
|
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)
|
local function get_compass_meta_pos(meta)
|
||||||
return {x=0, y=0, z=0}
|
return pos_deserialize(meta:get_string("waypoint_compass:position"))
|
||||||
end
|
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", {
|
minetest.register_tool("waypoint_compass:compass", {
|
||||||
description = "Waypoint compass",
|
description = "Waypoint compass",
|
||||||
short_description = "Steel Axe",
|
short_description = "Waypoint compass",
|
||||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
inventory_image = "Waypoint_compass_inventory_image.png",
|
||||||
on_use = function(itemstack, user, pointed_thing)
|
range = 100,
|
||||||
-- FIXME test code for checking how setting the description works
|
on_place = function(itemstack, placer, pointed_thing)
|
||||||
if user and user:is_player() then
|
if pointed_thing and pointed_thing.type == "node" then
|
||||||
local player = user
|
local pointed_pos = pointed_thing.above
|
||||||
local meta = itemstack:get_meta()
|
local meta = itemstack:get_meta()
|
||||||
local pos = get_compass_meta_pos(meta)
|
meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos)))
|
||||||
local new_description = string.format("%f (%f,%f,%f)", math.random(), pos.x, pos.y, pos.z)
|
set_compass_meta_pos(meta, pointed_pos)
|
||||||
meta:set_string("description", new_description)
|
--set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
|
||||||
meta:set_string("short_description", new_description)
|
--print("meta pos", minetest.pos_to_string(get_compass_meta_pos(meta)))
|
||||||
print("on use", new_description)
|
|
||||||
--player:set_wielded_item(nil)
|
|
||||||
--player:set_wielded_item(itemstack)
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
local function remove_hud_waypoint(player, player_name)
|
local function remove_hud_waypoint(player, player_name)
|
||||||
print("hud remove")
|
---print("hud remove")
|
||||||
local hud_id = player_waypoints[player_name].hud_id
|
local hud_id = player_waypoints[player_name].hud_id
|
||||||
player_waypoints[player_name] = nil
|
player_waypoints[player_name] = nil
|
||||||
player:hud_remove(hud_id)
|
player:hud_remove(hud_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function show_hud_waypoint(player, compass_item_meta)
|
local function show_hud_waypoint(player, compass_item_meta)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
-- Show this waypoint
|
-- 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({
|
local hud_id = player:hud_add({
|
||||||
hud_elem_type = "waypoint",
|
hud_elem_type = "waypoint",
|
||||||
name = waypoint_name,
|
name = waypoint_name,
|
||||||
text = "m",
|
text = "m",
|
||||||
precision=1,
|
precision=1,
|
||||||
number = 0xFF, --0xFFFFFF,
|
number = 0xFC7D0A,
|
||||||
world_pos = get_compass_meta_pos(compass_item_meta),
|
world_pos = get_compass_meta_pos(compass_item_meta),
|
||||||
})
|
})
|
||||||
-- store HUD elemnt id to remove it later
|
-- store HUD elemnt id to remove it later
|
||||||
if not player_waypoints[player_name] then
|
if not player_waypoints[player_name] then
|
||||||
player_waypoints[player_name] = {}
|
player_waypoints[player_name] = {}
|
||||||
end
|
end
|
||||||
print("hud_id add", hud_id)
|
--print("hud_id add", hud_id)
|
||||||
player_waypoints[player_name].id = get_compass_meta_id(compass_item_meta)
|
player_waypoints[player_name].pos = get_compass_meta_pos(compass_item_meta)
|
||||||
player_waypoints[player_name].hud_id = hud_id
|
player_waypoints[player_name].hud_id = hud_id
|
||||||
print("id set", player_waypoints[player_name].id)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -78,10 +116,10 @@ minetest.register_globalstep(function(dtime)
|
|||||||
-- player is holding compass
|
-- player is holding compass
|
||||||
if (item and item:get_name() == "waypoint_compass:compass") then
|
if (item and item:get_name() == "waypoint_compass:compass") then
|
||||||
local meta = item:get_meta()
|
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
|
-- remove different waypoint if it exists
|
||||||
print(player_waypoints[player_name] and player_waypoints[player_name].id)
|
if player_waypoints[player_name] and
|
||||||
if player_waypoints[player_name] and (player_waypoints[player_name].id ~= waypoint_id) then
|
(pos_serialize(player_waypoints[player_name].pos) ~= pos_serialize(waypoint_pos)) then
|
||||||
remove_hud_waypoint(player, player_name)
|
remove_hud_waypoint(player, player_name)
|
||||||
end
|
end
|
||||||
-- show new waypoint
|
-- show new waypoint
|
||||||
@ -89,6 +127,7 @@ minetest.register_globalstep(function(dtime)
|
|||||||
show_hud_waypoint(player, meta)
|
show_hud_waypoint(player, meta)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
-- not holding it anymore
|
||||||
if player_waypoints[player_name] then
|
if player_waypoints[player_name] then
|
||||||
remove_hud_waypoint(player, player_name)
|
remove_hud_waypoint(player, player_name)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user