aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/shapedinventory/sh_shaped.lua
blob: b77f37823769fe4ae352a859baac80e8d30b1b33 (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
--[[
	An inventory, with a shape!
]]

local reg = nrequire("inventory/inventory.lua")
local itm = nrequire("item.lua")
local inv = {}
if CLIENT then inv = nrequire("cl_shaped.lua") end

inv.Name = "Shaped Inventory"
inv.tracker = {}
inv.dimx = 5
inv.dimy = 5

local function calcposition(dimx,dimy,x,y)
	return ((x-1) * dimx) + y
end

local function canfitin(self,x,y,shape)
	for rn,row in ipairs(shape) do
		for cn,col in ipairs(row) do
			local absx,absy = x + rn - 1, y + cn - 1
			local slot = calcposition(self.dimx,self.dimy,absx,absy)
			if col and ((self.tracker[slot] ~= nil) or (absx > self.dimx) or (absy > self.dimy)) then
				return false
			end
		end
	end
	return true
end

function inv:FindPlaceFor(item)
	for x = 1, self.dimx do
		for y = 1, self.dimy do
			if canfitin(self,x,y,item.Shape) then
				return {x,y}
			end
		end
	end
	return nil
end

function inv:CanFitIn(tbl,item)
	if canfitin(self,tbl[1],tbl[2],item.Shape) then
		return true
	else
		return "Could not fit :("
	end
end

function inv:Put(tbl,item)

	--Set the item's shape to true
	for rn,row in ipairs(item.Shape) do
		for cn,col in ipairs(row) do
			if col then
				local slot = calcposition(self.dimx,self.dimy,tbl[1] + rn - 1,tbl[2] + cn - 1)
				self.tracker[slot] = 1
			end
		end
	end

	--Now set the item in the correct slot
	local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
	self.tracker[slot] = item
end

function inv:Has(ptr)
	local compare_func = ptr
	if type(ptr) == "string" then
		compare_func = function(t)
			return t.Name == ptr
		end
	end
	for k,v in pairs(self.tracker) do
		if type(v) == "table" and compare_func(v) then
			local x = math.floor(k / self.dimx)
			local y = k % self.dimx
			return {x,y}
		end
	end
	return nil
end

function inv:Remove(tbl)
	local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
	local item = self.tracker[slot]
	self.tracker[slot] = nil
	for rn,row in ipairs(item.Shape) do
		for cn,col in ipairs(row) do
			if col then
				slot = calcposition(self.dimx,self.dimy,tbl[1] + rn - 1,tbl[2] + cn - 1)
				self.tracker[slot] = nil
			end
		end
	end
end

function inv:Get(tbl)
	local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
	return self.tracker[slot]
end

function inv:Serialize()
	--Turn it into a sparse table
	local ret = {}
	for k,v in pairs(self.tracker) do
		if type(v) == "table" then
			ret[v.Name] = {k,v:Serialize()}
		end
	end
	return util.TableToJSON(ret)
end

function inv:DeSerialize(str)
	--TODO:Implement
	local tbl = util.JSONToTable(str)
	for k,v in pairs(tbl) do
		local name = k
		local pos = v[1]
		local data = v[2]
		self.tracker[pos] = itm.GetItemFromData(name,data)
	end
end


reg.RegisterInventory(inv)