Keep mana in bounds when using set and setmax

This commit is contained in:
Wuzzy 2015-02-04 21:21:04 +01:00
parent 9563fcffbc
commit 0e3af4e900
1 changed files with 28 additions and 6 deletions

View File

@ -18,16 +18,38 @@ mana.playerlist = {}
API functions API functions
]===] ]===]
-- Sets the maximum mana of the specified player --[[ Sets the mana reserves of the specified player
It is ensured that the resulting value will always be within the bounds of [0, maxmana]
]]
function mana.set(playername, value) function mana.set(playername, value)
mana.playerlist[playername].mana = value if value < 0 then
mana.hud_update(playername) minetest.log("info", "[mana] Warning: mana.set was called with negative value!")
value = 0
end
if value > mana.playerlist[playername].maxmana then
value = mana.playerlist[playername].maxmana
end
if mana.playerlist[playername].mana ~= value then
mana.playerlist[playername].mana = value
mana.hud_update(playername)
end
end end
-- Sets the maximum mana of the specified player --[[ Sets the maximum mana of the specified player. The value must be positive or 0.
The player's mana reserves will be capped at the new maximum, if neccessary.
]]
function mana.setmax(playername, value) function mana.setmax(playername, value)
mana.playerlist[playername].maxmana = value if value < 0 then
mana.hud_update(playername) value = 0
minetest.log("info", "[mana] Warning: mana.setmax was called with negative value!")
end
if mana.playerlist[playername].maxmana ~= value then
mana.playerlist[playername].maxmana = value
if(mana.playerlist[playername].mana > value) then
mana.playerlist[playername].mana = value
end
mana.hud_update(playername)
end
end end
-- Returns the current mana of the specified player -- Returns the current mana of the specified player