use raycast to find pointed object, add some config variables
This commit is contained in:
parent
b1b233033b
commit
ef4adb9613
99
init.lua
99
init.lua
@ -1,5 +1,11 @@
|
|||||||
local player_waypoints = {}
|
local player_waypoints = {}
|
||||||
local update_interval = 1.31415
|
local update_interval = 1.31415
|
||||||
|
local compass_precision = 10 -- set to 1 to show whole number or 10 for 1 decimal
|
||||||
|
local waypoint_color = 0xFC7D0A
|
||||||
|
local target_above = false
|
||||||
|
local point_to_objects = false -- unimplemented
|
||||||
|
local point_to_liquids = true
|
||||||
|
local compass_range = 96
|
||||||
|
|
||||||
-- 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)
|
||||||
@ -18,33 +24,16 @@ local function set_compass_meta_label(meta, 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 function set_compass_meta_pos(meta, pos)
|
||||||
local pos_str = pos_serialize(pos)
|
local pos_str = minetest.serialize(pos)
|
||||||
meta:set_string("waypoint_compass:position", pos_str)
|
meta:set_string("waypoint_compass:position", pos_str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function get_compass_meta_pos(meta)
|
local function get_compass_meta_pos(meta)
|
||||||
return pos_deserialize(meta:get_string("waypoint_compass:position"))
|
return minetest.deserialize(meta:get_string("waypoint_compass:position"))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- local function set_compass_meta_pos(meta, pos)
|
-- local function set_compass_meta_pos(meta, pos)
|
||||||
@ -57,23 +46,55 @@ end
|
|||||||
-- return minetest.get_position_from_hash(meta:get_int("waypoint_compass:position"))
|
-- return minetest.get_position_from_hash(meta:get_int("waypoint_compass:position"))
|
||||||
-- end
|
-- end
|
||||||
|
|
||||||
minetest.register_tool("waypoint_compass:compass", {
|
local function set_waypoint_at_pointed_place(itemstack, user, pointed_thing)
|
||||||
description = "Waypoint compass",
|
if pointed_thing and pointed_thing.type == "node" then
|
||||||
short_description = "Waypoint compass",
|
local pointed_pos = target_above and pointed_thing.above or pointed_thing.under
|
||||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
local meta = itemstack:get_meta()
|
||||||
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos)))
|
||||||
range = 160, -- TODO what's the good range?
|
set_compass_meta_pos(meta, pointed_pos)
|
||||||
on_place = function(itemstack, placer, pointed_thing)
|
set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
|
||||||
if pointed_thing and pointed_thing.type == "node" then
|
return itemstack
|
||||||
local pointed_pos = pointed_thing.above
|
end
|
||||||
local meta = itemstack:get_meta()
|
-- TODO handle entities?
|
||||||
meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos)))
|
end
|
||||||
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 first thing player is pointing at
|
||||||
return itemstack
|
local function raycast_crosshair(player, range)
|
||||||
end
|
local p_pos = player:get_pos()
|
||||||
end,
|
local p_eye_height = player:get_properties().eye_height
|
||||||
|
local p_eye_pos = { x = p_pos.x, y = p_pos.y + p_eye_height, z = p_pos.z }
|
||||||
|
local to = vector.add(p_eye_pos, vector.multiply(player:get_look_dir(), range))
|
||||||
|
local ray = minetest.raycast(p_eye_pos, to, point_to_objects, point_to_liquids)
|
||||||
|
local pointed_thing = ray:next()
|
||||||
|
return pointed_thing
|
||||||
|
-- while pointed_thing do
|
||||||
|
-- print(pointed_thing.type, minetest.pos_to_string(pointed_thing.under))
|
||||||
|
-- pointed_thing = ray:next()
|
||||||
|
-- end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
minetest.register_tool(
|
||||||
|
"waypoint_compass:compass", {
|
||||||
|
description = "Waypoint compass",
|
||||||
|
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)
|
||||||
|
set_waypoint_at_pointed_place(itemstack, placer, pointed_thing)
|
||||||
|
return itemstack
|
||||||
|
end,
|
||||||
|
on_secondary_use = function(itemstack, user, pointed_thing)
|
||||||
|
if pointed_thing.type == "nothing" then
|
||||||
|
if user:is_player() then
|
||||||
|
pointed_thing = raycast_crosshair(user, compass_range)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
set_waypoint_at_pointed_place(itemstack, user, pointed_thing)
|
||||||
|
return itemstack
|
||||||
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -93,8 +114,8 @@ local function show_hud_waypoint(player, compass_item_meta)
|
|||||||
hud_elem_type = "waypoint",
|
hud_elem_type = "waypoint",
|
||||||
name = waypoint_name,
|
name = waypoint_name,
|
||||||
text = "m",
|
text = "m",
|
||||||
precision=1,
|
precision= compass_precision,
|
||||||
number = 0xFC7D0A,
|
number = waypoint_color,
|
||||||
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
|
||||||
@ -120,7 +141,7 @@ minetest.register_globalstep(function(dtime)
|
|||||||
local waypoint_pos = get_compass_meta_pos(meta)
|
local waypoint_pos = get_compass_meta_pos(meta)
|
||||||
-- remove different waypoint if it exists
|
-- remove different waypoint if it exists
|
||||||
if player_waypoints[player_name] and
|
if player_waypoints[player_name] and
|
||||||
(pos_serialize(player_waypoints[player_name].pos) ~= pos_serialize(waypoint_pos)) then
|
(player_waypoints[player_name].pos ~= waypoint_pos) then
|
||||||
remove_hud_waypoint(player, player_name)
|
remove_hud_waypoint(player, player_name)
|
||||||
end
|
end
|
||||||
-- show new waypoint
|
-- show new waypoint
|
||||||
|
Loading…
Reference in New Issue
Block a user