From 0e3af4e90003538d41ef7d43cf0a9ce312477f98 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 4 Feb 2015 21:21:04 +0100 Subject: [PATCH] Keep mana in bounds when using set and setmax --- init.lua | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index dc0a216..a9e203f 100644 --- a/init.lua +++ b/init.lua @@ -18,16 +18,38 @@ mana.playerlist = {} 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) - mana.playerlist[playername].mana = value - mana.hud_update(playername) + if value < 0 then + 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 --- 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) - mana.playerlist[playername].maxmana = value - mana.hud_update(playername) + if value < 0 then + 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 -- Returns the current mana of the specified player