mirror of
git://repo.or.cz/minetest_mana.git
synced 2025-06-30 14:10:21 +02:00
Change the add and subtract functions, add new
This commit is contained in:
57
init.lua
57
init.lua
@ -63,15 +63,16 @@ function mana.getmax(playername)
|
||||
end
|
||||
|
||||
--[[
|
||||
Adds the specified amount of mana to the player, but will
|
||||
respect the player's maximum.
|
||||
Adds up to the specified amount of mana to the player.
|
||||
If the sum would be greater than the maximum, the new
|
||||
mana amount will be capped at the maximum.
|
||||
|
||||
returns:
|
||||
- true, excess on success, where excess is the amount of mana which
|
||||
was no
|
||||
- false on failure
|
||||
]]
|
||||
function mana.add(playername, value)
|
||||
function mana.add_up_to(playername, value)
|
||||
local t = mana.playerlist[playername]
|
||||
if(t ~= nil and value >= 0) then
|
||||
local excess
|
||||
@ -90,12 +91,31 @@ function mana.add(playername, value)
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
Adds the specified amount of mana to the player,
|
||||
iff it would not exceed the maximum.
|
||||
|
||||
returns:
|
||||
- true on success, all mana has been added
|
||||
- false on failure, no mana has been added
|
||||
]]
|
||||
function mana.add(playername, value)
|
||||
local t = mana.playerlist[playername]
|
||||
if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then
|
||||
t.mana = t.mana + value
|
||||
mana.hud_update(playername)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
Subtracts the specified amount of mana from the player,
|
||||
iff the player has enough mana reserves.
|
||||
|
||||
returns:
|
||||
- true on success, mana has been subtracted
|
||||
- true on success, all mana has been subtracted
|
||||
- false on failure, no mana has been subtracted
|
||||
]]
|
||||
function mana.subtract(playername, value)
|
||||
@ -110,6 +130,35 @@ function mana.subtract(playername, value)
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
Subtracts up to the specified amount of mana from the player.
|
||||
|
||||
returns:
|
||||
- true, missing on success, where missing is the amount of mana which could not been subtracted
|
||||
- false on failure, no mana has been subtracted
|
||||
]]
|
||||
function mana.subtract_up_to(playername, value)
|
||||
local t = mana.playerlist[playername]
|
||||
if(t ~= nil and value >= 0) then
|
||||
local missing
|
||||
if((t.mana - value) < 0) then
|
||||
missing = math.abs(t.mana - value)
|
||||
t.mana = 0
|
||||
else
|
||||
missing = 0
|
||||
t.mana = t.mana - value
|
||||
end
|
||||
mana.hud_update(playername)
|
||||
return true, missing
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
--[===[
|
||||
File handling, loading data, saving data, setting up stuff for players.
|
||||
]===]
|
||||
|
Reference in New Issue
Block a user