2018-11-05 23:02:39 +01:00
|
|
|
--Simple head-up display for current position, time and server lag.
|
|
|
|
|
|
|
|
-- Origin:
|
|
|
|
--ver 0.2.1 minetest_time
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-------Minetest Time--kazea's code tweaked by cg72 with help from crazyR--------
|
|
|
|
----------------Zeno` simplified some math and additional tweaks ---------------
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2018-12-07 09:48:38 +01:00
|
|
|
poshud = {
|
|
|
|
-- Position of hud
|
|
|
|
posx = tonumber(minetest.settings:get("poshud.hud.offsetx") or 0.8),
|
2018-12-09 19:50:08 +01:00
|
|
|
posy = tonumber(minetest.settings:get("poshud.hud.offsety") or 0.95),
|
|
|
|
enable_mapblock = minetest.settings:get_bool("poshud.mapblock.enable")
|
2018-12-07 09:48:38 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
--settings
|
|
|
|
|
|
|
|
colour = 0xFFFFFF --text colour in hex format default is white
|
2018-11-28 23:07:43 +01:00
|
|
|
enable_star = true
|
2018-11-05 23:02:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--------------------------------------------------------------------------------
|
2018-12-07 10:08:59 +01:00
|
|
|
|
|
|
|
-- hud id map (playername -> hud-id)
|
2018-11-05 23:02:39 +01:00
|
|
|
local player_hud = {}
|
|
|
|
|
2018-12-07 10:08:59 +01:00
|
|
|
-- hud enabled map (playername -> bool)
|
|
|
|
local player_hud_enabled = {}
|
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
local function generatehud(player)
|
|
|
|
local name = player:get_player_name()
|
2018-12-07 10:08:59 +01:00
|
|
|
|
|
|
|
if player_hud[name] then
|
|
|
|
-- already set up
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
local hud = {}
|
|
|
|
hud.id = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
name = "poshud",
|
2018-12-07 09:48:38 +01:00
|
|
|
position = {x=poshud.posx, y=poshud.posy},
|
2018-11-05 23:02:39 +01:00
|
|
|
offset = {x=8, y=-8},
|
|
|
|
text = "Initializing...",
|
|
|
|
scale = {x=100,y=100},
|
|
|
|
alignment = {x=1,y=0},
|
|
|
|
number = colour, --0xFFFFFF,
|
|
|
|
})
|
|
|
|
player_hud[name] = hud
|
|
|
|
end
|
2018-12-07 10:08:59 +01:00
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
local function updatehud(player, text)
|
|
|
|
local name = player:get_player_name()
|
2018-12-07 10:08:59 +01:00
|
|
|
|
|
|
|
if not player_hud_enabled[name] then
|
|
|
|
-- check if the player enabled the hud
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
if not player_hud[name] then
|
|
|
|
generatehud(player)
|
|
|
|
end
|
|
|
|
local hud = player_hud[name]
|
|
|
|
if hud and text ~= hud.text then
|
|
|
|
player:hud_change(hud.id, "text", text)
|
|
|
|
hud.text = text
|
|
|
|
end
|
|
|
|
end
|
2018-12-07 10:08:59 +01:00
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
local function removehud(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if player_hud[name] then
|
|
|
|
player:hud_remove(player_hud[name].id)
|
2018-12-07 10:08:59 +01:00
|
|
|
player_hud[name] = nil
|
2018-11-05 23:02:39 +01:00
|
|
|
end
|
|
|
|
end
|
2018-12-07 10:08:59 +01:00
|
|
|
|
2018-11-05 23:02:39 +01:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2018-12-07 10:08:59 +01:00
|
|
|
minetest.after(1,removehud,player)
|
2018-11-05 23:02:39 +01:00
|
|
|
end)
|
|
|
|
|
2018-12-07 10:08:59 +01:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- hud enabled/disable
|
|
|
|
|
|
|
|
|
|
|
|
minetest.register_chatcommand("poshud", {
|
|
|
|
params = "on|off",
|
|
|
|
description = "Turn poshud on or off",
|
|
|
|
func = function(name, param)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
|
|
|
|
if param == "on" then
|
|
|
|
player_hud_enabled[name] = true
|
|
|
|
generatehud(player)
|
|
|
|
|
|
|
|
elseif param == "off" then
|
|
|
|
player_hud_enabled[name] = false
|
|
|
|
removehud(player)
|
|
|
|
|
|
|
|
else
|
|
|
|
return true, "Usage: poshud [on|off]"
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--------------------------------------------------------------------------------
|
2018-11-05 23:02:39 +01:00
|
|
|
-- time
|
|
|
|
-- from https://gitlab.com/Rochambeau/mthudclock/blob/master/init.lua
|
|
|
|
|
|
|
|
local function floormod ( x, y )
|
|
|
|
return (math.floor(x) % y);
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_time()
|
|
|
|
local secs = (60*60*24*minetest.get_timeofday());
|
|
|
|
local s = floormod(secs, 60);
|
|
|
|
local m = floormod(secs/60, 60);
|
|
|
|
local h = floormod(secs/3600, 60);
|
|
|
|
return ("%02d:%02d"):format(h, m);
|
|
|
|
end
|
|
|
|
|
2018-11-28 23:07:43 +01:00
|
|
|
-- rotating star
|
|
|
|
local star={"\\", "|", "/", "-"}
|
|
|
|
|
2019-05-12 22:17:12 +02:00
|
|
|
-- New lagometry algorithm:
|
|
|
|
-- Make a list of N samples
|
|
|
|
-- Every sample is time from last step, therefore "lag"
|
|
|
|
-- Since time distance between samples is not equal, every sample needs
|
|
|
|
-- to be weighted by... time, which is itself. This results in:
|
|
|
|
-- avg_lag = sum(xi^2) / sum(xi)
|
|
|
|
-- Results of sums are cached for performance (subtract old sample, add new sample)
|
|
|
|
|
|
|
|
local l_time = 0
|
|
|
|
local l_N = 2048
|
|
|
|
local l_samples = {}
|
|
|
|
local l_ctr = 0
|
|
|
|
local l_sumsq = 0
|
|
|
|
local l_sum = 0
|
|
|
|
local l_max = 0.1
|
2018-11-05 23:02:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
local h_text = "Initializing..."
|
|
|
|
local h_int = 2
|
|
|
|
local h_tmr = 0
|
|
|
|
|
|
|
|
minetest.register_globalstep(function (dtime)
|
|
|
|
-- make a lag sample
|
2019-05-12 22:17:12 +02:00
|
|
|
|
|
|
|
local news = os.clock() - l_time
|
|
|
|
if l_time == 0 then
|
|
|
|
news = 0.1
|
|
|
|
end
|
|
|
|
l_time = os.clock()
|
|
|
|
|
|
|
|
local olds = l_samples[l_ctr+1] or 0
|
|
|
|
l_sumsq = l_sumsq - olds*olds + news*news
|
|
|
|
l_sum = l_sum - olds + news
|
|
|
|
|
|
|
|
l_samples[l_ctr+1] = news
|
|
|
|
l_max = math.max(l_max, news)
|
|
|
|
|
|
|
|
l_ctr = (l_ctr + 1) % l_N
|
|
|
|
|
|
|
|
if l_ctr == 0 then
|
|
|
|
-- recalculate from scratch
|
|
|
|
l_sumsq = 0
|
|
|
|
l_sum = 0
|
|
|
|
l_max = 0
|
|
|
|
for i=1,l_N do
|
|
|
|
local sample = l_samples[i]
|
|
|
|
l_sumsq = l_sumsq + sample*sample
|
|
|
|
l_sum = l_sum + sample
|
|
|
|
l_max = math.max(l_max, sample)
|
|
|
|
end
|
|
|
|
end
|
2018-11-05 23:02:39 +01:00
|
|
|
|
|
|
|
-- update hud text when necessary
|
|
|
|
if h_tmr <= 0 then
|
2019-05-12 22:17:12 +02:00
|
|
|
local l_avg = l_sumsq / l_sum
|
2018-11-05 23:02:39 +01:00
|
|
|
-- Update hud text that is the same for all players
|
2019-05-12 22:17:12 +02:00
|
|
|
local s_lag = string.format("Lag: avg: %.2f peak: %.2f", l_avg, l_max)
|
2018-11-05 23:02:39 +01:00
|
|
|
local s_time = "Time: "..get_time()
|
2018-11-28 23:07:43 +01:00
|
|
|
|
|
|
|
local s_star = ""
|
|
|
|
if enable_star then
|
2019-05-12 22:17:12 +02:00
|
|
|
s_star = star[(l_ctr % 4)+1]
|
2018-11-28 23:07:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
h_text = s_time .. " " .. s_star .. "\n" .. s_lag
|
2018-11-05 23:02:39 +01:00
|
|
|
|
|
|
|
h_tmr = h_int
|
|
|
|
else
|
2019-05-12 22:17:12 +02:00
|
|
|
h_tmr = h_tmr - news
|
2018-11-05 23:02:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local posi = player:get_pos()
|
2018-12-08 13:14:29 +01:00
|
|
|
local x = math.floor(posi.x+0.5)
|
|
|
|
local y = math.floor(posi.y+0.5)
|
|
|
|
local z = math.floor(posi.z+0.5)
|
|
|
|
local posistr = x.." ".. y .." ".. z
|
2018-12-07 10:21:00 +01:00
|
|
|
|
2018-12-09 19:50:08 +01:00
|
|
|
-- resulting hud string
|
|
|
|
local hud_display = h_text .. "\nPos: " .. posistr
|
|
|
|
|
|
|
|
if poshud.enable_mapblock then
|
|
|
|
-- append if enabled
|
|
|
|
local mapblockstr = math.floor(x / 16) .. " "
|
|
|
|
.. math.floor(y / 16) .. " "
|
|
|
|
.. math.floor(z / 16)
|
|
|
|
|
|
|
|
|
|
|
|
hud_display = hud_display .. "\nMapblock: " .. mapblockstr
|
|
|
|
end
|
|
|
|
|
|
|
|
updatehud(player, hud_display)
|
2018-11-05 23:02:39 +01:00
|
|
|
end
|
|
|
|
end);
|