forked from mtcontrib/minetest_mana
160 lines
3.3 KiB
Lua
160 lines
3.3 KiB
Lua
|
--[[
|
||
|
Mana 0.1.0
|
||
|
This mod adds mana to players, a special attribute
|
||
|
|
||
|
License: WTFPL
|
||
|
]]
|
||
|
|
||
|
--[===[
|
||
|
Initialization
|
||
|
]===]
|
||
|
|
||
|
mana = {}
|
||
|
mana.playerlist = {}
|
||
|
|
||
|
|
||
|
|
||
|
--[===[
|
||
|
API functions
|
||
|
]===]
|
||
|
|
||
|
-- Sets the maximum mana of the specified player
|
||
|
function mana.set(playername, mana)
|
||
|
mana.playerlist[playerlist].mana = mana
|
||
|
end
|
||
|
|
||
|
-- Sets the maximum mana of the specified player
|
||
|
function mana.setmax(playername, maxmana)
|
||
|
mana.playerlist[playerlist].maxmana = maxmana
|
||
|
end
|
||
|
|
||
|
-- Returns the current mana of the specified player
|
||
|
function mana.get(playername)
|
||
|
return mana.playerlist[playerlist].mana
|
||
|
end
|
||
|
|
||
|
-- Returns the maximum mana of the specified player
|
||
|
function mana.getmax(playername)
|
||
|
return mana.playerlist[playerlist].maxmana
|
||
|
end
|
||
|
|
||
|
--[[
|
||
|
Adds the specified amount of mana to the player, but will
|
||
|
respect the player's maximum.
|
||
|
|
||
|
returns:
|
||
|
- true, excess on success, where excess is the amount of mana which
|
||
|
was no
|
||
|
- false on failure
|
||
|
]]
|
||
|
function mana.add(playername, mana)
|
||
|
local t = mana.playerlist[playername]
|
||
|
if(t ~= nil and mana >= 0) then
|
||
|
local excess
|
||
|
if((t.mana + mana) > t.maxmana) then
|
||
|
excess = (t.mana + mana) - t.maxmana
|
||
|
t.mana = t.maxmana
|
||
|
else
|
||
|
excess = 0
|
||
|
t.mana = t.mana + mana
|
||
|
end
|
||
|
return true, excess
|
||
|
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
|
||
|
- false on failure, no mana has been subtracted
|
||
|
]]
|
||
|
function mana.subtract(playername, mana)
|
||
|
local t = mana.playerlist[playername]
|
||
|
if(t ~= nil and t.mana >= mana and mana >= 0) then
|
||
|
t.mana = t.mana - mana
|
||
|
return true
|
||
|
else
|
||
|
return false
|
||
|
end
|
||
|
end
|
||
|
|
||
|
|
||
|
--[===[
|
||
|
File handling, loading data, saving data, setting up stuff for players.
|
||
|
]===]
|
||
|
|
||
|
|
||
|
-- Load the playerlist from a previous session, if available.
|
||
|
do
|
||
|
local filepath = minetest.get_worldpath().."/mana.mt"
|
||
|
local file = io.open(filepath, "r")
|
||
|
if file then
|
||
|
minetest.log("action", "[mana] mana.mt opened.")
|
||
|
local string = file:read()
|
||
|
io.close(file)
|
||
|
if(string ~= nil) then
|
||
|
local savetable = minetest.deserialize(string)
|
||
|
mana.playerlist = savetable.playerlist
|
||
|
minetest.debug("[mana] mana.mt successfully read.")
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function mana.save_to_file()
|
||
|
local savetable = {}
|
||
|
savetable.playerlist = mana.playerlist
|
||
|
|
||
|
local savestring = minetest.serialize(savetable)
|
||
|
|
||
|
local filepath = minetest.get_worldpath().."/mana.mt"
|
||
|
local file = io.open(filepath, "w")
|
||
|
if file then
|
||
|
file:write(savestring)
|
||
|
io.close(file)
|
||
|
minetest.log("action", "[mana] Wrote mana data into "..filepath..".")
|
||
|
else
|
||
|
minetest.log("error", "[mana] Failed to write mana data into "..filepath..".")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
|
||
|
minetest.register_on_dieplayer(function(player)
|
||
|
local playername = player:get_player_name()
|
||
|
mana.set(playername, 0)
|
||
|
end)
|
||
|
|
||
|
|
||
|
minetest.register_on_leaveplayer(function(player)
|
||
|
local playername = player:get_player_name()
|
||
|
mana.playerlist[playername] = nil
|
||
|
end)
|
||
|
|
||
|
minetest.register_on_shutdown(function()
|
||
|
minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt")
|
||
|
mana.save_to_file()
|
||
|
end)
|
||
|
|
||
|
minetest.register_on_joinplayer(function(player)
|
||
|
local playername = player:get_player_name()
|
||
|
|
||
|
if mana.playerlist[playername] == nil then
|
||
|
mana.playerlist[playername] = {}
|
||
|
mana.playerlist[playername].mana = 0
|
||
|
mana.playerlist[playername].maxmana = 20
|
||
|
end
|
||
|
|
||
|
-- TODO: Update HUD
|
||
|
end)
|
||
|
|
||
|
|
||
|
|
||
|
--[===[
|
||
|
TODO: HUD functions
|
||
|
]===]
|
||
|
|