--[[ Common functions since skills are a special inventory, adding skills need to be a special item ]] local itm = nrequire("item.lua") local log = nrequire("log.lua") --Common things --Make sure items have a "name" and "ammt" attribute local item = {} item.Name = "Skill" item.isskill = true item.Serialize = function(self) return util.TableToJSON({ name = self.name, ammt = self.ammt }) end item.DeSerialize = function(self,data) local tbl = util.JSONToTable(data) self.name = tbl.name self.ammt = tbl.ammt return self end itm.RegisterItem(item) local pmeta = FindMetaTable("Player") function pmeta:AddSkill(name, ammt) local item = itm.GetItemByName("Skill") item.name = name item.ammt = ammt self:GiveItem(item) end local lib = {} local skills = {} local skillicons = {} --Skillname is a table of {string_group,string_name,icon=nil} function lib.RegisterSkill(skillname) local group,name = skillname[1],skillname[2] local icon = skillname[3] if not skills[group] then skills[group] = {} end local alin = false for k,v in pairs(skills[group]) do if v == name then alin = true break end end if alin then log.warn("Re-registering skill: " .. skillname[1] .. skillname[2]) else local pos = #skills[group] + 1 skills[group][pos] = name end end function lib.SetGroupIcon(group,icon) skillicons[group] = icon end function lib.IconFor(group) return skillicons[group] end function lib.SkillList() return skills end local xpmult = 1.5 --the lower, the more lvl per xp function lib.levelfunc(val) local curlvl = math.log(val,xpmult) local prevlvlxp = xpmult ^ math.floor(curlvl) local nextlvlxp = xpmult ^ (math.floor(curlvl) + 1) local sp = nextlvlxp - prevlvlxp local frac = (val - prevlvlxp) / ((sp ~= 0) and sp or 1) --don't divide by 0 return math.floor(curlvl), frac end function pmeta:GetSkill(name) local loc = self:HasItem(name) if not loc then return 0 end local item = self:GetItem(loc) return lib.levelfunc(item.ammt) end return lib