init.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. --Simple head-up display for current position, time and server lag.
  2. -- Origin:
  3. --ver 0.2.1 minetest_time
  4. --------------------------------------------------------------------------------
  5. --------------------------------------------------------------------------------
  6. -------Minetest Time--kazea's code tweaked by cg72 with help from crazyR--------
  7. ----------------Zeno` simplified some math and additional tweaks ---------------
  8. --------------------------------------------------------------------------------
  9. poshud = {
  10. -- Position of hud
  11. posx = tonumber(minetest.settings:get("poshud.hud.offsetx") or 0.8),
  12. posy = tonumber(minetest.settings:get("poshud.hud.offsety") or 0.95),
  13. enable_mapblock = minetest.settings:get_bool("poshud.mapblock.enable")
  14. }
  15. --settings
  16. colour = 0xFFFFFF --text colour in hex format default is white
  17. enable_star = true
  18. --------------------------------------------------------------------------------
  19. --------------------------------------------------------------------------------
  20. -- hud id map (playername -> hud-id)
  21. local player_hud = {}
  22. -- hud enabled map (playername -> bool)
  23. local player_hud_enabled = {}
  24. local function generatehud(player)
  25. local name = player:get_player_name()
  26. if player_hud[name] then
  27. -- already set up
  28. return
  29. end
  30. local hud = {}
  31. hud.id = player:hud_add({
  32. hud_elem_type = "text",
  33. name = "poshud",
  34. position = {x=poshud.posx, y=poshud.posy},
  35. offset = {x=8, y=-8},
  36. text = "Initializing...",
  37. scale = {x=100,y=100},
  38. alignment = {x=1,y=0},
  39. number = colour, --0xFFFFFF,
  40. })
  41. player_hud[name] = hud
  42. end
  43. local function updatehud(player, text)
  44. local name = player:get_player_name()
  45. if player_hud_enabled[name]==false then
  46. -- check if the player enabled the hud
  47. return
  48. end
  49. if not player_hud[name] then
  50. generatehud(player)
  51. end
  52. local hud = player_hud[name]
  53. if hud and text ~= hud.text then
  54. player:hud_change(hud.id, "text", text)
  55. hud.text = text
  56. end
  57. end
  58. local function removehud(player)
  59. local name = player:get_player_name()
  60. if player_hud[name] then
  61. player:hud_remove(player_hud[name].id)
  62. player_hud[name] = nil
  63. end
  64. end
  65. minetest.register_on_leaveplayer(function(player)
  66. minetest.after(1,removehud,player)
  67. end)
  68. --------------------------------------------------------------------------------
  69. --------------------------------------------------------------------------------
  70. -- hud enabled/disable
  71. minetest.register_chatcommand("poshud", {
  72. params = "on|off",
  73. description = "Turn poshud on or off",
  74. func = function(name, param)
  75. local player = minetest.get_player_by_name(name)
  76. if param == "on" then
  77. player_hud_enabled[name] = true
  78. generatehud(player)
  79. elseif param == "off" then
  80. player_hud_enabled[name] = false
  81. removehud(player)
  82. else
  83. return true, "Usage: poshud [on|off]"
  84. end
  85. end
  86. })
  87. --------------------------------------------------------------------------------
  88. --------------------------------------------------------------------------------
  89. -- time
  90. -- from https://gitlab.com/Rochambeau/mthudclock/blob/master/init.lua
  91. local function floormod ( x, y )
  92. return (math.floor(x) % y);
  93. end
  94. local function get_time()
  95. local secs = (60*60*24*minetest.get_timeofday());
  96. local s = floormod(secs, 60);
  97. local m = floormod(secs/60, 60);
  98. local h = floormod(secs/3600, 60);
  99. return ("%02d:%02d"):format(h, m);
  100. end
  101. -- rotating star
  102. local star={"\\", "|", "/", "-"}
  103. -- New lagometry algorithm:
  104. -- Make a list of N samples
  105. -- Every sample is time from last step, therefore "lag"
  106. -- Since time distance between samples is not equal, every sample needs
  107. -- to be weighted by... time, which is itself. This results in:
  108. -- avg_lag = sum(xi^2) / sum(xi)
  109. -- Results of sums are cached for performance (subtract old sample, add new sample)
  110. local l_time = 0
  111. local l_N = 2048
  112. local l_samples = {}
  113. local l_ctr = 0
  114. local l_sumsq = 0
  115. local l_sum = 0
  116. local l_max = 0.1
  117. local h_text = "Initializing..."
  118. local h_int = 2
  119. local h_tmr = 0
  120. local starc = 0
  121. minetest.register_globalstep(function (dtime)
  122. -- make a lag sample
  123. local news = os.clock() - l_time
  124. if l_time == 0 then
  125. news = 0.1
  126. end
  127. l_time = os.clock()
  128. local olds = l_samples[l_ctr+1] or 0
  129. l_sumsq = l_sumsq - olds*olds + news*news
  130. l_sum = l_sum - olds + news
  131. l_samples[l_ctr+1] = news
  132. l_max = math.max(l_max, news)
  133. l_ctr = (l_ctr + 1) % l_N
  134. if l_ctr == 0 then
  135. -- recalculate from scratch
  136. l_sumsq = 0
  137. l_sum = 0
  138. l_max = 0
  139. for i=1,l_N do
  140. local sample = l_samples[i]
  141. l_sumsq = l_sumsq + sample*sample
  142. l_sum = l_sum + sample
  143. l_max = math.max(l_max, sample)
  144. end
  145. end
  146. -- update hud text when necessary
  147. if h_tmr <= 0 then
  148. local l_avg = l_sumsq / l_sum
  149. -- Update hud text that is the same for all players
  150. local s_lag = string.format("Lag: %.2f avg: %.2f peak: %.2f", news, l_avg, l_max)
  151. local s_time = "Time: "..get_time()
  152. local s_rwt = ""
  153. if advtrains and advtrains.lines and advtrains.lines.rwt then
  154. s_rwt = "\nRailway Time: "..advtrains.lines.rwt.to_string(advtrains.lines.rwt.now(), true)
  155. end
  156. local s_star = ""
  157. if enable_star then
  158. s_star = star[starc+1]
  159. starc = (starc + 1) % 4
  160. end
  161. h_text = s_time .. " " .. s_star .. s_rwt .. "\n" .. s_lag
  162. h_tmr = h_int
  163. else
  164. h_tmr = h_tmr - news
  165. end
  166. for _,player in ipairs(minetest.get_connected_players()) do
  167. local posi = player:get_pos()
  168. local x = math.floor(posi.x+0.5)
  169. local y = math.floor(posi.y+0.5)
  170. local z = math.floor(posi.z+0.5)
  171. local posistr = x.." ".. y .." ".. z
  172. -- resulting hud string
  173. local hud_display = h_text .. "\nPos: " .. posistr
  174. if poshud.enable_mapblock then
  175. -- append if enabled
  176. local mapblockstr = math.floor(x / 16) .. " "
  177. .. math.floor(y / 16) .. " "
  178. .. math.floor(z / 16)
  179. hud_display = hud_display .. "\nMapblock: " .. mapblockstr
  180. end
  181. updatehud(player, hud_display)
  182. end
  183. end);