From ceaa5df331fbdd85090d0085a5a5be0028743349 Mon Sep 17 00:00:00 2001 From: whosit Date: Tue, 1 Jun 2021 15:45:33 +0300 Subject: [PATCH] add ability to add "measuring point" on sneak+punch --- init.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/init.lua b/init.lua index 8de7acd..75dbb4a 100644 --- a/init.lua +++ b/init.lua @@ -8,6 +8,7 @@ local POINT_TO_LIQUIDS = true local COMPASS_RANGE = 180 local WAYPOINT_ICON = "waypoint_compass_waypoint.png" local WAYPOINT_SCALE = {x=-1/16*9,y=-1} +local COMPASS_MEASURE_TIMEOUT = 8 -- time until "measuring point" disappears local waypoint_lib = dofile(minetest.get_modpath("waypoint_compass") .. "/waypoint_lib.lua") @@ -364,6 +365,49 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end) +local function compass_meausure_callback(itemstack, user, __pointed_thing) + if not user or not user:is_player() then + return + end + + if not user:get_player_control()["sneak"] then + return + end + + local _, pointed_thing = waypoint_lib.get_pointed_position(user, COMPASS_RANGE, POINT_TO_OBJECTS, POINT_TO_LIQUIDS) + if not pointed_thing or pointed_thing.type == "nothing" then + return + end + local point_pos = nil + if pointed_thing.type == "node" then + point_pos = TARGET_ABOVE and pointed_thing.above or pointed_thing.under + else + return + end + local meta = itemstack:get_meta() + local compass_pos = get_compass_meta_pos(meta) + if not compass_pos then + return + end + local distance = vector.length(vector.subtract(compass_pos, point_pos)) + local label = ("distance to waypoint: %.2fm"):format(distance) + local label_color = 0x00FF00 + local icon = WAYPOINT_ICON + local icon_scale = WAYPOINT_SCALE + local icon_color = "#FFFFFF" + + local waypoint = waypoint_lib.IconWaypointHUD:new(point_pos, label, label_color, icon, icon_scale, icon_color) + waypoint:show(user) + local player_name = user:get_player_name() + minetest.after(COMPASS_MEASURE_TIMEOUT, function() + local player = minetest.get_player_by_name(player_name) + if player then + waypoint:hide(player) + end + end) +end + + minetest.register_tool( "waypoint_compass:compass", { description = DEFAULT_DESCRIPTION, @@ -373,6 +417,11 @@ minetest.register_tool( color = "#f6d0a5", wield_scale = {x = 0.5, y = 0.5, z = 0.5}, range = 2.0, -- TODO what's the good range? + on_use = function(itemstack, user, pointed_thing) + compass_meausure_callback(itemstack, user, pointed_thing) + -- return nil since compass in unchanged + return nil + end, on_place = function(itemstack, placer, pointed_thing) return compass_use_callback(itemstack, placer, pointed_thing) end,