aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/skills/sh_skillcommon.lua
blob: 9c022eb1536a81bb2d457a70d3613f89dc891d56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--[[
	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

return lib