Round input values

This commit is contained in:
Wuzzy 2015-02-20 15:19:57 +01:00
parent d400f20c55
commit 9921ba24cc

View File

@ -44,6 +44,7 @@ function mana.set(playername, value)
minetest.log("info", "[mana] Warning: mana.set was called with negative value!") minetest.log("info", "[mana] Warning: mana.set was called with negative value!")
value = 0 value = 0
end end
value = mana.round(value)
if value > mana.playerlist[playername].maxmana then if value > mana.playerlist[playername].maxmana then
value = mana.playerlist[playername].maxmana value = mana.playerlist[playername].maxmana
end end
@ -58,6 +59,7 @@ function mana.setmax(playername, value)
value = 0 value = 0
minetest.log("info", "[mana] Warning: mana.setmax was called with negative value!") minetest.log("info", "[mana] Warning: mana.setmax was called with negative value!")
end end
value = mana.round(value)
if mana.playerlist[playername].maxmana ~= value then if mana.playerlist[playername].maxmana ~= value then
mana.playerlist[playername].maxmana = value mana.playerlist[playername].maxmana = value
if(mana.playerlist[playername].mana > value) then if(mana.playerlist[playername].mana > value) then
@ -72,6 +74,7 @@ function mana.setregen(playername, value)
value = 0 value = 0
minetest.log("info", "[mana] Warning: mana.setregen was called with negative value!") minetest.log("info", "[mana] Warning: mana.setregen was called with negative value!")
end end
value = mana.round(value)
mana.playerlist[playername].regen = value mana.playerlist[playername].regen = value
end end
@ -89,6 +92,7 @@ end
function mana.add_up_to(playername, value) function mana.add_up_to(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value)
if(t ~= nil and value >= 0) then if(t ~= nil and value >= 0) then
local excess local excess
if((t.mana + value) > t.maxmana) then if((t.mana + value) > t.maxmana) then
@ -107,6 +111,7 @@ end
function mana.add(playername, value) function mana.add(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value)
if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then
t.mana = t.mana + value t.mana = t.mana + value
mana.hud_update(playername) mana.hud_update(playername)
@ -118,6 +123,7 @@ end
function mana.subtract(playername, value) function mana.subtract(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value)
if(t ~= nil and t.mana >= value and value >= 0) then if(t ~= nil and t.mana >= value and value >= 0) then
t.mana = t.mana -value t.mana = t.mana -value
mana.hud_update(playername) mana.hud_update(playername)
@ -129,6 +135,7 @@ end
function mana.subtract_up_to(playername, value) function mana.subtract_up_to(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value)
if(t ~= nil and value >= 0) then if(t ~= nil and value >= 0) then
local missing local missing
if((t.mana - value) < 0) then if((t.mana - value) < 0) then
@ -291,3 +298,10 @@ else
player:hud_remove(mana.playerlist[playername].hudid) player:hud_remove(mana.playerlist[playername].hudid)
end end
end end
--[===[
Helper functions
]===]
mana.round = function(x)
return math.ceil(math.floor(x+0.5))
end