2017-07-30 16:04:31 -04:00
local player_to_id_text = { } -- Storage of players so the mod knows what huds to update
2017-07-30 17:20:44 -04:00
local player_to_id_image = { }
2017-07-30 11:15:43 -04:00
2017-07-30 16:04:31 -04:00
minetest.register_globalstep ( function ( dtime ) -- This will run every tick, so around 20 times/second
for _ , player in ipairs ( minetest : get_connected_players ( ) ) do -- Do everything below for each player in-game
local lookat = get_looking_node ( player ) -- Get the node they're looking at
2017-07-30 11:15:43 -04:00
if lookat then
2017-07-30 16:04:31 -04:00
player : hud_change ( player_to_id_text [ player ] , " text " , describe_node ( lookat ) ) -- If they are looking at something, display that
2017-07-30 17:20:44 -04:00
local node_object = minetest.registered_nodes [ lookat.name ]
player : hud_change ( player_to_id_image [ player ] , " text " , handle_tiles ( node_object.tiles ) )
2017-07-30 11:15:43 -04:00
else
2017-07-30 16:04:31 -04:00
player : hud_change ( player_to_id_text [ player ] , " text " , " " ) -- If they are not looking at anything, do not display the text
2017-07-30 17:20:44 -04:00
player : hud_change ( player_to_id_image [ player ] , " text " , " " )
2017-07-30 11:15:43 -04:00
end
end
end )
2017-07-30 16:04:31 -04:00
minetest.register_on_joinplayer ( function ( player ) -- Add the hud to all players
2017-07-30 11:15:43 -04:00
player_to_id_text [ player ] = player : hud_add ( {
hud_elem_type = " text " ,
text = " test " ,
number = 0xffffff ,
position = { x = 0.5 , y = 0.1 } ,
2017-07-30 17:20:44 -04:00
} )
player_to_id_image [ player ] = player : hud_add ( {
hud_elem_type = " image " ,
text = " " ,
scale = { x = 1 , y = 1 } ,
alignment = 0 ,
position = { x = 0.4 , y = 0.1 } ,
offset = { x = 0 , y = 0 }
2017-07-30 11:15:43 -04:00
} )
end )
2017-07-30 16:04:31 -04:00
function get_looking_node ( player ) -- Return the node the given player is looking at or nil
2017-07-30 11:15:43 -04:00
local lookat
for i = 0 , 10 do -- 10 is the maximum distance you can point to things in creative mode by default
local lookvector = -- This variable will store what node we might be looking at
vector.add ( -- This add function corrects for the players approximate height
vector.add ( -- This add function applies the camera's position to the look vector
vector.multiply ( -- This multiply function adjusts the distance from the camera by the iteration of the loop we're in
player : get_look_dir ( ) ,
i -- Goes from 0 to 10
) ,
player : get_pos ( )
) ,
vector.new ( 0 , 1.5 , 0 )
)
lookat = minetest.get_node_or_nil ( -- This actually gets the node we might be looking at
lookvector
) or lookat
2017-07-30 13:39:04 -04:00
if lookat ~= nil and lookat.name ~= " air " and lookat.name ~= " walking_light:light " then break else lookat = nil end -- If we *are* looking at something, stop the loop and continue
2017-07-30 11:15:43 -04:00
end
return lookat
end
2017-07-30 16:04:31 -04:00
function describe_node ( node ) -- Return a string that describes the node and mod
2017-07-30 15:55:38 -04:00
local mod , nodename = minetest.registered_nodes [ node.name ] . mod_origin , minetest.registered_nodes [ node.name ] . description
2017-07-30 11:15:43 -04:00
mod = remove_unneeded ( capitalize ( mod ) )
nodename = remove_unneeded ( capitalize ( nodename ) )
return
" Name: " .. nodename .. " \n " ..
2017-07-30 15:55:38 -04:00
" Mod: " .. mod .. " \n "
2017-07-30 11:15:43 -04:00
end
2017-07-30 16:04:31 -04:00
function remove_unneeded ( str ) -- Remove characters like '-' and '_' to make the string look better
2017-07-30 11:15:43 -04:00
return str : gsub ( " [_-] " , " " )
end
2017-07-30 16:04:31 -04:00
function capitalize ( str ) -- Capitalize every word in a string, looks good for node names
2017-07-30 15:55:38 -04:00
return string.gsub ( " " .. str , " %W%l " , string.upper ) : sub ( 2 )
2017-07-30 17:20:44 -04:00
end
function handle_tiles ( tiles )
-- TODO: Make better lol
local invalid = false
for i , v in pairs ( tiles ) do
if ( type ( v ) ~= " string " ) then
invalid = true
end
end
if not invalid then
return minetest.inventorycube ( tiles [ 1 ] , tiles [ 6 ] , tiles [ 5 ] )
else
return " "
end
2017-07-30 11:15:43 -04:00
end