Skip to content

Commit

Permalink
to_float32 and fix hud_manager checks for changed values which are st…
Browse files Browse the repository at this point in the history
…ored intermediately as float32
  • Loading branch information
fluxionary committed Feb 1, 2024
1 parent ab2bf87 commit 9858421
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion minetest/hud_manager.lua
Expand Up @@ -65,6 +65,13 @@ function ManagedHud:toggle_enabled(player)
return enabled
end

local function v2f_to_float_32(v)
return {
x = futil.math.to_float32(v.x),
y = futil.math.to_float32(v.y),
}
end

function ManagedHud:update(player, data)
local is_enabled = self:is_enabled(player)
local player_name = player:get_player_name()
Expand Down Expand Up @@ -99,7 +106,11 @@ function ManagedHud:update(player, data)

if old_hud_def then
for k, v in pairs(new_hud_def) do
if old_hud_def[k] ~= v and k ~= "type" and k ~= "hud_elem_type" then
if k == "position" or k == "scale" or k == "align" or k == "offset" then
v = v2f_to_float_32(v)
end

if not futil.equals(old_hud_def[k], v) and k ~= "type" and k ~= "hud_elem_type" then
player:hud_change(hud_id, k, v)
end
end
Expand Down
10 changes: 10 additions & 0 deletions util/math.lua
@@ -1,6 +1,7 @@
futil.math = {}

local floor = math.floor
local huge = math.huge
local max = math.max
local min = math.min

Expand Down Expand Up @@ -150,3 +151,12 @@ function futil.math.round(number, mult)
return round(number)
end
end

-- TODO this doesn't handle out-of-bounds exponents
function futil.math.to_float32(number)
if number == huge or number == -huge or number ~= number then
return number
end
local sign, significand, exponent = ("%a"):format(number):match("^(-?)0x([0-9a-f\\.]+)p([0-9+-]+)$")
return tonumber(("%s0x%sp%s"):format(sign, significand:sub(1, 8), exponent))
end

0 comments on commit 9858421

Please sign in to comment.