Skip to content

Instantly share code, notes, and snippets.

@Warr1024
Last active March 9, 2022 00:53
Show Gist options
  • Save Warr1024/b7bb9e370311caff1ec91295576f471b to your computer and use it in GitHub Desktop.
Save Warr1024/b7bb9e370311caff1ec91295576f471b to your computer and use it in GitHub Desktop.
txthud_* mods from NodeCore public server
Copyright (C)2018-2019 Aaron Suen <warr1024@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- LUALOCALS < ---------------------------------------------------------
local ipairs, math, minetest, pairs, txthud
= ipairs, math, minetest, pairs, txthud
local math_floor
= math.floor
-- LUALOCALS > ---------------------------------------------------------
local minpos = {x = -512.5, y = -64.5, z = -1536.5}
local maxpos = {x = 511.5, y = 255.5, z = -512.5}
local function range(p, min, max)
if p <= min then
return false, min - p
elseif p > max then
return false, p - max
end
local a = p - min
local b = max - p
return true, (a < b) and a or b
end
local function rangechk(p, min, max, din, dout, safe)
return safe and ok, din, dout
end
local function format(player, zone, dist, num)
if dist >= 10 then
dist = math_floor(dist)
else
dist = math_floor(dist * 10) / 10
if dist == math_floor(dist) then dist = dist .. ".0" end
end
return txthud(player, {
id = "keepzone",
order = -1,
num = num,
lines = {zone, dist .. "m", ""}
})
end
local function update(player)
local pos = player:get_pos()
local safe = true
local din, dout
for k in pairs(pos) do
local ok, dist = range(pos[k], minpos[k], maxpos[k])
if ok and ((not din) or (din > dist)) then
din = dist
elseif (not ok) and ((not dout) or (dout > dist)) then
dout = dist
end
safe = safe and ok
end
if safe then
return format(player, "Old Mapgen", din, 0xC0C000)
else
return format(player, "New Mapgen", dout, 0x008000)
end
end
minetest.register_globalstep(function()
for _, p in ipairs(minetest.get_connected_players()) do
update(p)
end
end)
-- LUALOCALS < ---------------------------------------------------------
-- SKIP: txthud
local error, ipairs, math, minetest, pairs, rawset, string, table, type
= error, ipairs, math, minetest, pairs, rawset, string, table, type
local math_random, string_rep, table_sort
= math.random, string.rep, table.sort
-- LUALOCALS > ---------------------------------------------------------
minetest.register_chatcommand("txthud", {
description = "Toggle the text HUD",
privs = {},
func = function(name)
local player = minetest.get_player_by_name(name)
if not player then return end
local old = player:get_attribute("notxthud") or ""
local v = (old == "") and "1" or ""
player:set_attribute("notxthud", v)
minetest.chat_send_player(name, "Text HUD: "
.. (v ~= "" and "OFF" or "ON"))
end,
})
local huds = {}
local function txthud(player, data)
if (not player) or (not player.get_player_name) then
return error("invalid player parameter")
end
if type(data) ~= "table" then
return error("data must be a table")
end
if not data.id then
return error("missing required data.id")
end
local pname = player:get_player_name()
local hud = huds[pname]
if not hud then
hud = {
sections = {},
huds = {}
}
huds[pname] = hud
end
data.order = data.order or 0
hud.sections[data.id] = data
end
rawset(_G, "txthud", txthud)
minetest.register_on_leaveplayer(function(player)
huds[player:get_player_name()] = nil
end)
local function redraw(player)
local pname = player:get_player_name()
local hud = huds[pname]
if not hud then return end
local keys = {}
for k in pairs(hud.sections) do keys[#keys + 1] = k end
table_sort(keys, function(a, b)
return hud.sections[a].order < hud.sections[b].order
end)
local lines = {}
if (player:get_attribute("notxthud") or "") == "" then
for _, k in ipairs(keys) do
local v = hud.sections[k]
if v.lines then
for _, l in ipairs(v.lines) do
if type(l) == "string" then l = {text = l} end
l.text = l.text or ""
l.num = l.num or v.num or 0xC0C0C0
lines[#lines + 1] = l
end
end
end
end
local now = minetest.get_us_time() / 1000000
local max = #hud.huds
if #lines > max then max = #lines end
for i = 1, max do
local l = lines[i] or {text = "", num = 0xC0C0C0}
local h = hud.huds[i]
if h then
if h.text ~= l.text or now > h.texp then
player:hud_change(h.id, "text", string_rep("\n", i - 1) .. l.text)
h.text = l.text
h.texp = now + 1 + math_random()
end
if h.num ~= l.num or now > h.nexp then
player:hud_change(h.id, "number", l.num)
h.num = l.num
h.nexp = now + 1 + math_random()
end
else
hud.huds[i] = {
id = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
text = string_rep("\n", i - 1) .. l.text,
number = l.num,
alignment = {x = -1, y = 1},
offset = {x = -4, y = 4},
}),
text = l.text,
num = l.num,
texp = now + 1 + math_random(),
nexp = now + 1 + math_random()
}
end
end
end
minetest.register_globalstep(function()
for _, p in ipairs(minetest.get_connected_players()) do
redraw(p)
end
end)
-- LUALOCALS < ---------------------------------------------------------
local ipairs, math, minetest, table, txthud
= ipairs, math, minetest, table, txthud
local math_floor, table_sort
= math.floor, table.sort
-- LUALOCALS > ---------------------------------------------------------
local timer = 0
local huds = {}
local idle = {}
local function fmt(l, v, n)
if v < n then return "" end
return " " .. l .. " " .. math_floor(v * 100 + 0.5) / 100
end
local function updateall()
timer = 0
local now = minetest.get_us_time() / 1000000
local t = {}
local u = {}
for _, p in ipairs(minetest.get_connected_players()) do
local n = p:get_player_name()
t[#t + 1] = n
local pos = p:get_pos()
local look = p:get_look_dir()
local cur = pos.x .. "|" .. pos.y .. "|" .. pos.z
.. "|" .. look.x .. "|" .. look.y .. "|" .. look.z
local old = idle[n]
if not old then
old = {k = cur, n = now}
idle[n] = old
elseif old.k ~= cur then
old.k = cur
old.n = now
end
local i = now - old.n
if i >= 60 then
u[n] = {
text = n .. " (idle " .. math_floor(i / 60 + 0.5) .. ")",
num = 0x808080
}
else
u[n] = {text = n, num = 0xC0C0C0}
end
end
table_sort(t)
for i = 1, #t do t[i] = u[t[i]] end
for _, p in ipairs(minetest.get_connected_players()) do
txthud(p, {
id = "players",
lines = t
})
end
end
minetest.register_globalstep(function(dt)
timer = timer + dt
if timer > 2 then return updateall() end
end)
minetest.register_on_joinplayer(updateall)
minetest.register_on_leaveplayer(updateall)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment