132 lines
2.8 KiB
Lua
132 lines
2.8 KiB
Lua
|
|
||
|
local u = dofile(levels.path .. "/registry.lua").util
|
||
|
|
||
|
function levels.get_level(name, st)
|
||
|
local ref = u.get_reference(name)
|
||
|
if u.get_registered_stat(st) == nil then
|
||
|
-- TODO:
|
||
|
if ref[st] == nil then
|
||
|
return 0
|
||
|
end
|
||
|
end
|
||
|
return ref[st].level or 0
|
||
|
end
|
||
|
|
||
|
function levels.register_stat(name, def)
|
||
|
local stat_def = u.get_registered_stat(name)
|
||
|
if stat_def ~= nil then
|
||
|
-- already registered
|
||
|
-- TODO:
|
||
|
return
|
||
|
end
|
||
|
|
||
|
stat_def = {}
|
||
|
stat_def.on_xp_gain = def.on_xp_gain
|
||
|
stat_def.on_xp_loss = def.on_xp_loss
|
||
|
stat_def.on_level_up = def.on_level_up or function(player)
|
||
|
-- TODO:
|
||
|
end
|
||
|
stat_def.on_level_down = def.on_level_down or function(player)
|
||
|
-- TODO:
|
||
|
end
|
||
|
|
||
|
u.set_registered_stat(name, stat_def)
|
||
|
end
|
||
|
|
||
|
--~ local function init_stat(stats, st)
|
||
|
--~ if registered_stats[st] == nil then
|
||
|
--~ -- TODO:
|
||
|
--~ return stats
|
||
|
--~ end
|
||
|
--~ stats[st] = {level=0, xp=0}
|
||
|
--~ return stats
|
||
|
--~ end
|
||
|
|
||
|
--- Adds a stat object to player.
|
||
|
--
|
||
|
-- @param player Player object.
|
||
|
-- @param st Stat name.
|
||
|
-- @param def Stat definition.
|
||
|
--~ local function add_stat(player, st, def)
|
||
|
--~ if registered_stats[st] == nil then
|
||
|
--~ -- TODO:
|
||
|
--~ return
|
||
|
--~ end
|
||
|
|
||
|
--~ player.stats = player.stats or {}
|
||
|
--~ local stat = player.stats[st]
|
||
|
--~ if stat ~= nil then
|
||
|
--~ -- player already has stat
|
||
|
--~ -- TODO: show warning
|
||
|
--~ return
|
||
|
--~ end
|
||
|
--~ stat = {level=def.level or 0, xp=def.xp or 0, tnl=u.calculate_tnl(st, def.level)}
|
||
|
--~ player.stats[st] = stat
|
||
|
--~ end
|
||
|
|
||
|
function levels.get_xp(name, st)
|
||
|
local ref = u.get_reference(name)
|
||
|
if u.get_registered_stat(st) == nil then
|
||
|
-- TODO:
|
||
|
if ref[st] == nil then
|
||
|
return 0
|
||
|
end
|
||
|
end
|
||
|
return ref[st].xp or 0
|
||
|
end
|
||
|
|
||
|
function levels.add_xp(player, st, amount, notify)
|
||
|
notify = notify or notify == nil
|
||
|
|
||
|
local stat = registered_stats[st]
|
||
|
if stat == nil then
|
||
|
-- TODO: not registered stat
|
||
|
return
|
||
|
end
|
||
|
|
||
|
local player_stat = player.stats[st]
|
||
|
if player_stat == nil then
|
||
|
-- TODO: player doesn't have stat
|
||
|
return
|
||
|
end
|
||
|
|
||
|
local old_level = player_stat.level
|
||
|
local new_xp = player_stat.xp + amount
|
||
|
local rem = 0
|
||
|
if new_xp < 0 then
|
||
|
-- player lost a level
|
||
|
player_stat.level = old_level - 1
|
||
|
rem = new_xp
|
||
|
player_stat.tnl = u.calculate_tnl(st, player_stat.level)
|
||
|
player_stat.xp = player_stat.tnl
|
||
|
elseif new_xp >= player_stat.tnl then
|
||
|
-- player gained a level
|
||
|
player_stat.level = old_level + 1
|
||
|
rem = new_xp - player_stat.tnl
|
||
|
player_stat.tnl = u.calculate_tnl(st, player_stat.level)
|
||
|
player_stat.xp = 0
|
||
|
end
|
||
|
|
||
|
player_stat.xp = new_xp
|
||
|
player.stats[st] = player_stat
|
||
|
|
||
|
if player_stat.level < old_level then
|
||
|
stat.on_level_down(player)
|
||
|
elseif player_stat.level > old_level then
|
||
|
stat.on_level_up(player)
|
||
|
end
|
||
|
|
||
|
-- handle remaining xp
|
||
|
if rem ~= 0 then
|
||
|
levels.add_xp(player, st, rem, false)
|
||
|
end
|
||
|
|
||
|
if notify then
|
||
|
if amount < 0 then
|
||
|
stat.on_xp_loss(player, amount)
|
||
|
elseif amount > 0 then
|
||
|
stat.on_xp_gain(player, amount)
|
||
|
end
|
||
|
end
|
||
|
end
|