aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/inventory
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-10-09 16:20:46 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-10-09 16:20:46 -0400
commitda81a0a23a3704dd2de3ab2249496c1ad1912d1c (patch)
tree915edc671acbb292191adad2f25f87ba26e567cf /gamemode/core/inventory
parent497be6ff15989c7bf9de5beb138d2ef042dca6bd (diff)
downloadartery-da81a0a23a3704dd2de3ab2249496c1ad1912d1c.tar.gz
artery-da81a0a23a3704dd2de3ab2249496c1ad1912d1c.tar.bz2
artery-da81a0a23a3704dd2de3ab2249496c1ad1912d1c.zip
Updated internal representation of inventories
Diffstat (limited to 'gamemode/core/inventory')
-rw-r--r--gamemode/core/inventory/cl_invtracker.lua121
-rw-r--r--gamemode/core/inventory/inventory.lua1
-rw-r--r--gamemode/core/inventory/item.lua2
-rw-r--r--gamemode/core/inventory/sv_invtracker.lua22
4 files changed, 141 insertions, 5 deletions
diff --git a/gamemode/core/inventory/cl_invtracker.lua b/gamemode/core/inventory/cl_invtracker.lua
new file mode 100644
index 0000000..258a271
--- /dev/null
+++ b/gamemode/core/inventory/cl_invtracker.lua
@@ -0,0 +1,121 @@
+--[[
+ One of the tabs in the inventory
+]]
+local inv = nrequire("inventory/inventory.lua")
+local itm = nrequire("item.lua")
+--local state = nrequire("cl_state.lua")
+
+local q = {}
+
+local known_inventories = {}
+local inventory_frames = {}
+local invsheet
+q.known_inventories = known_inventories
+
+local drawfloatinginventory = function(id, inventory)
+ --print("Drawing a floating inventory!")
+ local frame = vgui.Create("DFrame")
+ frame:SetPos( ScrW() - (ScrW() / 4), 0 )
+ frame:SetSize( ScrW() / 4, ScrH() / 4 )
+ frame:SetTitle( inventory.Name )
+ frame:SetDraggable( true )
+ local panel = vgui.Create("DPanel",frame)
+ panel:Dock(FILL)
+ if inventory.DrawOnDPanel then
+ local prox = inventory:DrawOnDPanel(panel)
+ frame.id = known_inventories[id]:AddObserver(prox)
+ else
+ error("Inventory needs a DrawOnDPanel method!")
+ end
+ frame:MakePopup()
+ frame.OnClose = function(self)
+ --print("Closeing chest id", id)
+ --print("entity is", known_inventories[id].Owner)
+ known_inventories[id]:RemoveObserver(self.id)
+ net.Start("closechestinv")
+ net.WriteEntity(known_inventories[id].Owner)
+ net.SendToServer()
+ known_inventories[id] = nil
+ self:Remove()
+ end
+end
+
+local drawsheeton = function(id,inventory,higharchy)
+ --print("Drawing an inventory on a sheet!")
+ if invsheet == nil then return end
+ local tpanel = vgui.Create( "DPanel", invsheet )
+ --tpanel.Paint = function( self, w, h )
+ -- draw.RoundedBox( 4, 0, 0, w, h, Color( 0, 128, 255 ) )
+ --end
+ if inventory.DrawOnDPanel then
+ --print("Has drawondpanel")
+ local prox = inventory:DrawOnDPanel(tpanel)
+ --print("Prox returned was",prox)
+ --PrintTable(prox)
+ known_inventories[id]:AddObserver(prox)
+ --print("Oservers is now")
+ --PrintTable(known_inventories[id].observers)
+ end
+ invsheet:AddSheet( inventory.Name, tpanel, "icon16/tab.png" )
+end
+
+net.Receive("art_ObserveInventory",function()
+ print("Got call to observe inventory")
+ local id = net.ReadUInt(32)
+ local inv_type = net.ReadString()
+ local datalen = net.ReadUInt(32)
+ local inital_data = net.ReadData(datalen)
+ local ownent = net.ReadEntity()
+ --An array of hiarchy
+ local partof = net.ReadTable()
+ assert(known_inventories[id] == nil, "Trying to observe the same inventory twice!",id)
+ local tinv = inv.CreateInventoryFromData(inv_type,inital_data,ownent)
+ tinv.id = id
+ local cursor = known_inventories
+ for k,v in pairs(partof) do
+ cursor[v] = cursor[v] or {}
+ cursor = cursor[v]
+ end
+ cursor[id] = tinv
+ if id > 10 then
+ drawfloatinginventory(id,tinv)
+ hook.Call("OnSpawnMenuOpen")
+ else
+ drawsheeton(id,tinv)
+ end
+end)
+
+net.Receive("art_UpdateInventory",function()
+ local id = net.ReadUInt(32)
+ local isput = net.ReadBool()
+ local position = net.ReadTable()
+ if isput then
+ local item_name = net.ReadString()
+ --print("Putting ", item_name, "into inventory ",id, " at position")
+ --PrintTable(position)
+ local item_data = net.ReadData(net.ReadUInt(32))
+ local item = itm.GetItemFromData(item_name,item_data)
+ known_inventories[id]:Put(position,item)
+ --print("Inventorie's observers:")
+ --PrintTable(known_inventories[id].observers)
+ --print("Inventory is now")
+ --PrintTable(known_inventories[id])
+ else
+ known_inventories[id]:Remove(position)
+ end
+end)
+
+net.Receive("art_CloseInventory",function()
+ local id = net.ReadUInt(32)
+ known_inventories[id] = nil
+ if inventory_frames[id] then
+ inventory_frames[id]:Close()
+ inventory_frames[id] = nil
+ end
+end)
+
+concommand.Add("PrintKnownInventories",function(ply,cmd,args)
+ PrintTable(known_inventories)
+end)
+
+return q
diff --git a/gamemode/core/inventory/inventory.lua b/gamemode/core/inventory/inventory.lua
index b96d168..c9bf6da 100644
--- a/gamemode/core/inventory/inventory.lua
+++ b/gamemode/core/inventory/inventory.lua
@@ -154,6 +154,7 @@ function inv.CreateInventoryFromData(name,data,owner)
local ret = tinv:DeSerialize(data)
--print("is now",tinv)
--PrintTable(tinv)
+ assert(ret != nil, "Failed to create inventory " .. name .. ", returned nil")
return ret
end
diff --git a/gamemode/core/inventory/item.lua b/gamemode/core/inventory/item.lua
index 80e2a88..2eb2fb3 100644
--- a/gamemode/core/inventory/item.lua
+++ b/gamemode/core/inventory/item.lua
@@ -40,7 +40,7 @@ function itm.RegisterItem(tbl)
end
function itm.GetItemByName(name)
- assert(type(name) == "string",string.format("Attempted to get an item by name with a %s."))
+ assert(type(name) == "string",string.format("Attempted to get an item by name with a %s.",type(name)))
assert(items[name] ~= nil,string.format("Attempted to get item with invalid name %q Valid item names are:\n\t%s",name,table.concat(table.GetKeys(items),"\n\t")))
local item
if items[name].init then
diff --git a/gamemode/core/inventory/sv_invtracker.lua b/gamemode/core/inventory/sv_invtracker.lua
index 537d61d..7df4175 100644
--- a/gamemode/core/inventory/sv_invtracker.lua
+++ b/gamemode/core/inventory/sv_invtracker.lua
@@ -102,11 +102,11 @@ end)
function track.ClearInventories(ply)
ply.data = {}
- ply.data.inventories = {}
end
--Updates the client side inventory whenever the inventory is updated server side
function track.MakeInventoryObserver(ply,invid)
+ print("Making observer...")
local observer = {}
observer.Put = function(self,pos,item)
print("In observer, item was", item)
@@ -129,6 +129,7 @@ function track.MakeInventoryObserver(ply,invid)
net.WriteTable(pos)
net.Send(ply)
end
+ print("Returning observer...")
return observer
end
@@ -141,10 +142,12 @@ function track.NotifyPlayerOfInventory(ply,tinv)
net.WriteData(initaldat,#initaldat)
print("Before sending, inv owner is", tinv.Owner, "and type is",tinv.Name)
net.WriteEntity(tinv.Owner)
+ net.WriteTable({})
net.Send(ply)
end
-function track.GiveInventoryTo(ply,name)
+function track.GiveInventoryTo(ply,name,higharchy)
+ local hi = higharchy or {}
local i = inv.CreateInventory(name)
i.Owner = ply
local nid = #ply.data.inventories + 1
@@ -160,25 +163,36 @@ function track.GiveInventoryTo(ply,name)
net.WriteUInt(#dat,32)
net.WriteData(dat,#dat)
net.WriteEntity(i.Owner)
+ net.WriteTable(hi)
net.Send(ply)
end
-function track.GiveInventoryWithData(ply,name,data)
+function track.GiveInventoryWithData(ply,name,data,higharchy)
+ local hi = higharchy or {}
print("Giveing inventory with data")
local i = inv.CreateInventoryFromData(name,data,ply)
+ print("Created inventory track",ply,ply.data,ply.data.inventories)
local nid = #ply.data.inventories + 1
+ print("got nid")
local observer = track.MakeInventoryObserver(ply,nid)
+ print("Made observer for", i)
i.id = nid
i:AddObserver(observer)
+ print("Added observer")
ply.data.inventories[nid] = i
-
+ print("About to tell client to observe")
net.Start("art_ObserveInventory")
net.WriteUInt(nid,32)
net.WriteString(name)
net.WriteUInt(#data,32)
net.WriteData(data,#data)
net.WriteEntity(ply)
+ net.WriteTable(hi)
+ print("About to send...")
net.Send(ply)
+
+ print("Finished giving inventory with data, ply.data is now ", ply.data)
+ PrintTable(ply.data)
end