specialties/xp.lua

49 lines
1.4 KiB
Lua
Raw Normal View History

2013-01-04 04:18:42 +01:00
--File Manipulating
specialties.writeXP = function(name)
local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "w")
2013-07-02 01:03:51 +02:00
for skill,num in pairs(specialties.players[name].skills) do
file:write(skill.." "..tostring(num).."\n")
end
2013-01-04 04:18:42 +01:00
file:close()
end
specialties.readXP = function(name, specialty)
local file = io.open(minetest.get_worldpath().."/"..name.."_XP", "r")
2013-01-04 04:18:42 +01:00
if file == nil then
specialties.writeXP(name)
local empty = {}
for skill,_ in pairs(specialties.skills) do
empty[skill] = 0
end
return empty
end
local xp = {}
local line = file:read("*l")
while line ~= nil do
local params = line:split(" ")
xp[params[1]] = tonumber(params[2])
line = file:read("*l")
2013-01-04 04:18:42 +01:00
end
file:close()
return xp
end
--Table Modification
specialties.changeXP = function(name, specialty, amount)
2013-07-02 04:04:06 +02:00
local current = specialties.players[name].skills[specialty]
if current == nil then current = 0 end
local newAmount = current+amount
2013-07-02 01:03:51 +02:00
if newAmount >= 0 then
specialties.players[name].skills[specialty] = newAmount
local player = minetest.get_player_by_name(name)
2013-07-02 04:04:06 +02:00
local id = specialties.players[name].hud[specialty]
2013-07-02 01:03:51 +02:00
local hudItem = player:hud_get(id)
hudItem.text = tostring(newAmount)
hudItem.offset = {x=100, y=0}
hudItem.alignment = {x=1, y=0}
player:hud_remove(id)
2013-07-02 04:04:06 +02:00
specialties.players[name].hud[specialty] = player:hud_add(hudItem)
2013-01-04 04:18:42 +01:00
return true
else
return false
end
end