aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/equipment/sh_equipment.lua
blob: 3c30df1552fc6b226a6a913a1a7d5ecb2952e882 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
--[[
	A simple inventory that holds 1 item
]]
local itm = nrequire("item.lua")
local ste = nrequire("utility/stream.lua")
local inventory = nrequire("inventory/inventory.lua")
print("Got invnetory table, it is:")
PrintTable(inventory)
local slots = {
	"Head",
	"Shoulders",
	"Chest",
	"Arms",
	"Left Hand",
	"Right Hand",
	"Dual",
	"Legs",
	"Belt",
	"Gloves",
	"Feet",
	"Back",
	"Ring 1",
	"Ring 2",
	"Ring 3",
	"Ring 4",
	"Ring 5",
	"Ring 6",
}

local inv = {}
if SERVER then inv = nrequire("sv_equipment.lua") end
if CLIENT then inv = nrequire("cl_equipment.lua") end

inv.Name = "Equipment"
inv.equiped = {}
inv.FindPlaceFor = function(self, item)
	--Make sure it's equipable
	if not item.Equipable then return nil end

	--If this is a dual weielding weapon
	if item.Equipable == "Dual" then
		if self.equiped["Left Hand"] == nil and self.equiped["Right Hand"] == nil then
			return {"Dual"}
		else
			return nil
		end
	end

	--If this item is a left or right handed, make sure we don't have a dual equiped
	if item.Equipable == "Left Hand" or item.Equipable == "Right Hand" then
		if self.equiped["Dual"] ~= nil then return nil
		elseif self.equiped[item.Equipable] ~= nil then return nil
		else return {item.Equipable} end
	end

	--If this item is a ring
	if item.Equipable == "Ring" then
		for i = 1,6 do
			if self.equiped["Ring " .. i] == nil then
				return {"Ring " .. i}
			end
		end
		return nil
	end

	--Otherwise, just check if the slot is empty
	if self.equiped[item.Equipable] == nil and slots[self.equiped] ~= nil then
		return {item.Equipable}
	else
		return nil
	end
end

inv.CanFitIn = function(self,position,item)
	return (position[1] == item.Equipable) and (self.equiped[position[1]] == nil)
end

inv.Put = function(self,position,item)
	self.equiped[position[1]] = item
	if item.onEquip then item:onEquip(self.owner) end
end

inv.Has = function(self,string_or_compare_func)
	if type(string_or_compare_func) == "string" then
		for k,v in pairs(self.equiped) do
			if v.Name == string_or_compare_func then return k end
		end
		return nil
	elseif type(string_or_compare_func) == "function" then
		for k,v in pairs(self.equiped) do
			if string_or_compare_func(v.Name) then return k end
		end
		return nil
	end
	error(string.format("equipment:Has() called with a %s, expected string or function.",type(string_or_compare_func)))
end

inv.Remove = function(self,position)
	local item = self.equiped[position[1]]
	if item.onUnEquip then item:onUnEquip(self.owner) end
	self.equiped[position[1]] = nil
end

inv.Get = function(self,position)
	return self.equiped[position[1]]
end

inv.Serialize = function(self)
	local tbl = {}
	for k,v in pairs(self.equiped) do
		tbl[k] = v:Serialize()
	end
	return util.TableToJSON(tbl)
end

inv.DeSerialize = function(self,data)
	print("deserializeing, data was",data)
	if data ~= nil and data ~= "" then
		local tbl = util.JSONToTable(data)
		local cpy = table.Copy(self)
		for k,v in pairs(tbl) do
			cpy.equiped[k] = itm.GetItemFromData(v)
		end
	else
		return table.Copy(self)
	end
end

inventory.RegisterInventory(inv)