aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/cl_common.lua
blob: 559c8f05293b5e7d625b6c6c46a0e757bc7cf2e2 (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
--[[
	A bunch of functions that a lot of inventories have in common, dragged out here so bugfixing is easier

]]
local com = {}

--Displays a dropdown of options under the players mouse, if the option is clicked, does the function
--Requires a table of strings to functions, or strings to tables of strings to functions.
--Be careful not to make this a recursive table.
function com.CreateMenuFor(menu, tbl)
	for k,v in pairs(tbl) do
		if isfunction(v) then --This is a dead-end, add the menu
			menu:AddOption(k,v)
		elseif istable(v) then --Otherwise it should be a table, recursively call to create
			local submenu = menu:AddSubMenu(k)
			CreateMenuFor(submenu,v)
		else
			error("I got wanted to make a menu for something not a function or a table:" + type(v))
		end
	end
end

function com.generatereceiver()
	return function(self,panels,dropped,index,cx,cy)
		if dropped then
			local froment,toent = panels[1].info.owner,self.info.owner
			local fromid,toid = panels[1].info.id,self.info.id
			local frompos,topos = panels[1].info.pos,self.info.pos
			local frominv,toinv = panels[1].info.inv,self.info.inv

			--Do nothing if we don't actually want to move anything anywhere
			local posequal = true
			if (froment ~= toent) or (fromid ~= toid) or (frominv ~= toinv) then posequal = false end
			if posequal then
				for k,v in pairs(frompos) do
					if topos[k] ~= v then
						posequal = false
						break
					end
				end
			end
			if posequal then return end

			local item = frominv:Get(frompos)
			--Fake remove the item, in case the position we want to move it to overlaps with where it is now.
			frominv:Remove(frompos)
			local cf = toinv:CanFitIn(topos,item)
			frominv:Put(frompos,item)
			if cf == true then
				--Send the request
				net.Start("art_RequestInvMove")
				net.WriteEntity(froment)
				net.WriteEntity(toent)
				net.WriteUInt(fromid,32)
				net.WriteUInt(toid,32)
				net.WriteTable(frompos)
				net.WriteTable(topos)
				net.SendToServer()
			end
		end
	end
end

return com