2022-10-08 20:55:10 +02:00
local player_font_huds = { }
2021-07-27 19:11:46 +02:00
2022-10-08 20:55:10 +02:00
local font_states = {
2021-07-27 19:11:46 +02:00
{ 0 , " Normal font " } ,
{ 1 , " Bold font " } ,
{ 2 , " Italic font " } ,
{ 3 , " Bold and italic font " } ,
{ 4 , " Monospace font " } ,
{ 5 , " Bold and monospace font " } ,
{ 7 , " ZOMG all the font styles " } ,
2024-10-28 15:57:54 +01:00
{ 7 , " Colors test! " .. core.colorize ( " green " , " Green " ) ..
core.colorize ( " red " , " \n Red " ) .. " END " } ,
2021-07-27 19:11:46 +02:00
}
2022-10-08 20:55:10 +02:00
local font_default_def = {
2023-12-29 21:51:02 +01:00
type = " text " ,
2021-07-27 19:11:46 +02:00
position = { x = 0.5 , y = 0.5 } ,
scale = { x = 2 , y = 2 } ,
alignment = { x = 0 , y = 0 } ,
2022-10-08 20:55:10 +02:00
number = 0xFFFFFF ,
2021-07-27 19:11:46 +02:00
}
2022-10-08 20:55:10 +02:00
local function add_font_hud ( player , state )
local def = table.copy ( font_default_def )
local statetbl = font_states [ state ]
2021-07-27 19:11:46 +02:00
def.offset = { x = 0 , y = 32 * state }
def.style = statetbl [ 1 ]
def.text = statetbl [ 2 ]
return player : hud_add ( def )
end
2022-10-08 20:55:10 +02:00
local font_etime = 0
local font_state = 0
2021-07-27 19:11:46 +02:00
2024-10-28 15:57:54 +01:00
core.register_globalstep ( function ( dtime )
2022-10-08 20:55:10 +02:00
font_etime = font_etime + dtime
if font_etime < 1 then
2021-07-27 19:11:46 +02:00
return
end
2022-10-08 20:55:10 +02:00
font_etime = 0
2024-10-28 15:57:54 +01:00
for _ , player in ipairs ( core.get_connected_players ( ) ) do
2022-10-08 20:55:10 +02:00
local huds = player_font_huds [ player : get_player_name ( ) ]
2021-07-27 19:11:46 +02:00
if huds then
for i , hud_id in ipairs ( huds ) do
2022-10-08 20:55:10 +02:00
local statetbl = font_states [ ( font_state + i ) % # font_states + 1 ]
2021-07-27 19:11:46 +02:00
player : hud_change ( hud_id , " style " , statetbl [ 1 ] )
player : hud_change ( hud_id , " text " , statetbl [ 2 ] )
end
end
end
2022-10-08 20:55:10 +02:00
font_state = font_state + 1
2021-07-27 19:11:46 +02:00
end )
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudfonts " , {
2022-10-08 20:55:10 +02:00
params = " [<HUD elements>] " ,
2021-07-27 19:11:46 +02:00
description = " Show/Hide some text on the HUD with various font options " ,
func = function ( name , param )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2021-07-27 19:11:46 +02:00
local param = tonumber ( param ) or 0
2022-10-08 20:55:10 +02:00
param = math.min ( math.max ( param , 1 ) , # font_states )
if player_font_huds [ name ] == nil then
player_font_huds [ name ] = { }
2021-07-27 19:11:46 +02:00
for i = 1 , param do
2022-10-08 20:55:10 +02:00
table.insert ( player_font_huds [ name ] , add_font_hud ( player , i ) )
2021-07-27 19:11:46 +02:00
end
2024-10-28 15:57:54 +01:00
core.chat_send_player ( name , ( " %d text HUD element(s) added. " ) : format ( param ) )
2021-07-27 19:11:46 +02:00
else
2022-10-08 20:55:10 +02:00
local huds = player_font_huds [ name ]
2021-07-27 19:11:46 +02:00
if huds then
for _ , hud_id in ipairs ( huds ) do
player : hud_remove ( hud_id )
end
2024-10-28 15:57:54 +01:00
core.chat_send_player ( name , " All text HUD elements removed. " )
2021-07-27 19:11:46 +02:00
end
2022-10-08 20:55:10 +02:00
player_font_huds [ name ] = nil
2021-07-27 19:11:46 +02:00
end
return true
end ,
} )
2022-10-08 20:41:11 +02:00
2022-10-08 20:55:10 +02:00
-- Testing waypoint capabilities
2022-10-08 20:41:11 +02:00
2022-10-08 20:55:10 +02:00
local player_waypoints = { }
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudwaypoints " , {
2022-10-08 20:41:11 +02:00
params = " [ add | add_change | remove ] " ,
description = " Create HUD waypoints at your position for testing (add: Add waypoints and change them after 0.5s (default). add_change: Add waypoints and change immediately. remove: Remove all waypoints) " ,
func = function ( name , params )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2022-10-08 20:41:11 +02:00
if not player then
return false , " No player. "
end
if params == " remove " then
if player_waypoints [ name ] then
for i = 1 , # player_waypoints [ name ] do
player : hud_remove ( player_waypoints [ name ] [ i ] )
end
player_waypoints [ name ] = { }
end
2022-10-08 20:55:10 +02:00
return true , " All waypoint HUD elements removed. "
2022-10-08 20:41:11 +02:00
end
if not ( params == " add_change " or params == " add " or params == " " ) then
-- Incorrect syntax
return false
end
local regular = player : hud_add {
2023-12-29 21:51:02 +01:00
type = " waypoint " ,
2022-10-08 20:41:11 +02:00
name = " regular waypoint " ,
text = " m " ,
number = 0xFFFFFF ,
world_pos = vector.add ( player : get_pos ( ) , { x = 0 , y = 1.5 , z = 0 } )
}
local reduced_precision = player : hud_add {
2023-12-29 21:51:02 +01:00
type = " waypoint " ,
2022-10-08 20:41:11 +02:00
name = " imprecise waypoint " ,
text = " m (0.1 steps, precision = 10) " ,
precision = 10 ,
number = 0xFFFFFF ,
world_pos = vector.add ( player : get_pos ( ) , { x = 0 , y = 1 , z = 0 } )
}
local hidden_distance = player : hud_add {
2023-12-29 21:51:02 +01:00
type = " waypoint " ,
2022-10-08 20:41:11 +02:00
name = " waypoint with hidden distance " ,
text = " this text is hidden as well (precision = 0) " ,
precision = 0 ,
number = 0xFFFFFF ,
world_pos = vector.add ( player : get_pos ( ) , { x = 0 , y = 0.5 , z = 0 } )
}
local function change ( chplayer )
if not ( chplayer and chplayer : is_player ( ) ) then
return
end
if regular then
chplayer : hud_change ( regular , " world_pos " , vector.add ( player : get_pos ( ) , { x = 0 , y = 3 , z = 0 } ) )
chplayer : hud_change ( regular , " number " , 0xFF0000 )
end
if reduced_precision then
chplayer : hud_change ( reduced_precision , " precision " , 2 )
chplayer : hud_change ( reduced_precision , " text " , " m (0.5 steps, precision = 2) " )
chplayer : hud_change ( reduced_precision , " number " , 0xFFFF00 )
end
if hidden_distance then
chplayer : hud_change ( hidden_distance , " number " , 0x0000FF )
end
2024-10-28 15:57:54 +01:00
core.chat_send_player ( chplayer : get_player_name ( ) , " Waypoints changed. " )
2022-10-08 20:41:11 +02:00
end
if params == " add_change " then
-- change immediate
change ( player )
else
2024-10-28 15:57:54 +01:00
core.after ( 0.5 , change , player )
2022-10-08 20:41:11 +02:00
end
local image_waypoint = player : hud_add {
2023-12-29 21:51:02 +01:00
type = " image_waypoint " ,
2022-10-08 20:41:11 +02:00
text = " testhud_waypoint.png " ,
world_pos = player : get_pos ( ) ,
2024-01-10 19:32:49 +01:00
-- 20% of screen width, 3x image height
scale = { x = - 20 , y = 3 } ,
2022-10-08 20:41:11 +02:00
offset = { x = 0 , y = - 32 }
}
if not player_waypoints [ name ] then
player_waypoints [ name ] = { }
end
if regular then
table.insert ( player_waypoints [ name ] , regular )
end
if reduced_precision then
table.insert ( player_waypoints [ name ] , reduced_precision )
end
if hidden_distance then
table.insert ( player_waypoints [ name ] , hidden_distance )
end
if image_waypoint then
table.insert ( player_waypoints [ name ] , image_waypoint )
end
regular = regular or " error "
reduced_precision = reduced_precision or " error "
hidden_distance = hidden_distance or " error "
image_waypoint = image_waypoint or " error "
return true , " Waypoints added. IDs: regular: " .. regular .. " , reduced precision: " .. reduced_precision ..
" , hidden distance: " .. hidden_distance .. " , image waypoint: " .. image_waypoint
end
} )
2024-10-28 15:57:54 +01:00
core.register_on_joinplayer ( function ( player )
2022-10-08 20:41:11 +02:00
player : set_properties ( { zoom_fov = 15 } )
end )
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " zoomfov " , {
2022-10-08 20:41:11 +02:00
params = " [<FOV>] " ,
description = " Set or display your zoom_fov " ,
func = function ( name , param )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2022-10-08 20:41:11 +02:00
if not player then
return false , " No player. "
end
if param == " " then
local fov = player : get_properties ( ) . zoom_fov
return true , " zoom_fov = " .. tostring ( fov )
end
local fov = tonumber ( param )
if not fov then
return false , " Missing or incorrect zoom_fov parameter! "
end
player : set_properties ( { zoom_fov = fov } )
fov = player : get_properties ( ) . zoom_fov
return true , " zoom_fov = " .. tostring ( fov )
end ,
} )
2022-10-08 20:55:10 +02:00
2024-01-29 16:43:37 +01:00
-- Hotbars
local hud_hotbar_defs = {
{
type = " hotbar " ,
position = { x = 0.2 , y = 0.5 } ,
direction = 0 ,
alignment = { x = 1 , y =- 1 } ,
} ,
{
type = " hotbar " ,
position = { x = 0.2 , y = 0.5 } ,
direction = 2 ,
alignment = { x = 1 , y = 1 } ,
} ,
{
type = " hotbar " ,
position = { x = 0.7 , y = 0.5 } ,
direction = 0 ,
offset = { x = 140 , y = 20 } ,
alignment = { x =- 1 , y =- 1 } ,
} ,
{
type = " hotbar " ,
position = { x = 0.7 , y = 0.5 } ,
direction = 2 ,
offset = { x = 140 , y = 20 } ,
alignment = { x =- 1 , y = 1 } ,
} ,
}
local player_hud_hotbars = { }
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudhotbars " , {
2024-01-29 16:43:37 +01:00
description = " Shows some test Lua HUD elements of type hotbar. " ..
" (add: Adds elements (default). remove: Removes elements) " ,
params = " [ add | remove ] " ,
func = function ( name , params )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2024-01-29 16:43:37 +01:00
if not player then
return false , " No player. "
end
local id_table = player_hud_hotbars [ name ]
if not id_table then
id_table = { }
player_hud_hotbars [ name ] = id_table
end
if params == " remove " then
for _ , id in ipairs ( id_table ) do
player : hud_remove ( id )
end
return true , " Hotbars removed. "
end
-- params == "add" or default
for _ , def in ipairs ( hud_hotbar_defs ) do
table.insert ( id_table , player : hud_add ( def ) )
end
return true , # hud_hotbar_defs .. " Hotbars added. "
end
} )
2024-01-28 17:35:31 +01:00
-- Inventories
local hud_inventory_defs = {
{
type = " inventory " ,
position = { x = 0.2 , y = 0.5 } ,
direction = 0 ,
text = " main " ,
number = 4 ,
item = 2 ,
} ,
{
type = " inventory " ,
position = { x = 0.2 , y = 0.5 } ,
direction = 2 ,
text = " main " ,
number = 4 ,
item = 2 ,
} ,
{
type = " inventory " ,
position = { x = 0.7 , y = 0.5 } ,
direction = 1 ,
text = " main " ,
number = 4 ,
item = 2 ,
} ,
{
type = " inventory " ,
position = { x = 0.7 , y = 0.5 } ,
direction = 3 ,
text = " main " ,
number = 4 ,
item = 2 ,
} ,
}
local player_hud_inventories = { }
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudinventories " , {
2024-01-28 17:35:31 +01:00
description = " Shows some test Lua HUD elements of type inventory. (add: Adds elements (default). remove: Removes elements) " ,
params = " [ add | remove ] " ,
func = function ( name , params )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2024-01-28 17:35:31 +01:00
if not player then
return false , " No player. "
end
local id_table = player_hud_inventories [ name ]
if not id_table then
id_table = { }
player_hud_inventories [ name ] = id_table
end
if params == " remove " then
for _ , id in ipairs ( id_table ) do
player : hud_remove ( id )
end
return true , " HUD Inventories removed. "
end
-- params == "add" or default
for _ , def in ipairs ( hud_inventory_defs ) do
table.insert ( id_table , player : hud_add ( def ) )
end
return true , # hud_inventory_defs .. " HUD Inventories added. "
end
} )
2024-01-29 16:43:37 +01:00
2024-10-28 15:57:54 +01:00
core.register_on_leaveplayer ( function ( player )
2024-01-28 17:35:31 +01:00
local playername = player : get_player_name ( )
player_font_huds [ playername ] = nil
player_waypoints [ playername ] = nil
player_hud_hotbars [ playername ] = nil
player_hud_inventories [ playername ] = nil
2022-10-08 20:55:10 +02:00
end )
2024-01-14 17:46:29 +01:00
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudprint " , {
2024-01-14 17:46:29 +01:00
description = " Writes all used Lua HUD elements into chat. " ,
func = function ( name , params )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2024-01-14 17:46:29 +01:00
if not player then
return false , " No player. "
end
local s = " HUD elements: "
for k , elem in pairs ( player : hud_get_all ( ) ) do
local ename = dump ( elem.name )
local etype = dump ( elem.type )
local epos = " {x= " .. elem.position . x .. " , y= " .. elem.position . y .. " } "
s = s .. " \n [ " .. k .. " ] type = " .. etype .. " | name = " .. ename .. " | pos = " .. epos
end
return true , s
end
} )
2024-01-29 16:43:37 +01:00
local hud_flags = { " hotbar " , " healthbar " , " crosshair " , " wielditem " , " breathbar " ,
" minimap " , " minimap_radar " , " basic_debug " , " chat " }
2024-10-28 15:57:54 +01:00
core.register_chatcommand ( " hudtoggleflag " , {
2024-01-29 16:43:37 +01:00
description = " Toggles a hud flag. " ,
params = " [ " .. table.concat ( hud_flags , " | " ) .. " ] " ,
func = function ( name , params )
2024-10-28 15:57:54 +01:00
local player = core.get_player_by_name ( name )
2024-01-29 16:43:37 +01:00
if not player then
return false , " No player. "
end
local flags = player : hud_get_flags ( )
if flags [ params ] == nil then
return false , " Unknown hud flag. "
end
flags [ params ] = not flags [ params ]
player : hud_set_flags ( flags )
return true , " Flag \" " .. params .. " \" set to " .. tostring ( flags [ params ] ) .. " . "
end
} )