diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-01-13 20:33:59 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-01-13 20:33:59 -0500 |
| commit | f4ee62bb0725a3ae94477b2818071f506e4dfd9f (patch) | |
| tree | 5b185d1f93aea4e14a2d93e4addfde4dafda9bed /gamemode/core | |
| parent | 98e0462e4f6b13ff26af5211409352d45dd9453e (diff) | |
| download | artery-f4ee62bb0725a3ae94477b2818071f506e4dfd9f.tar.gz artery-f4ee62bb0725a3ae94477b2818071f506e4dfd9f.tar.bz2 artery-f4ee62bb0725a3ae94477b2818071f506e4dfd9f.zip | |
Finished up shaped inventory, and more work on shared functions.
Diffstat (limited to 'gamemode/core')
| -rw-r--r-- | gamemode/core/inventory/inventory.lua | 23 | ||||
| -rw-r--r-- | gamemode/core/inventory/item.lua | 5 | ||||
| -rw-r--r-- | gamemode/core/inventory/sv_invtracker.lua | 107 | ||||
| -rw-r--r-- | gamemode/core/npc/cl_npcmap.lua | 1 |
4 files changed, 109 insertions, 27 deletions
diff --git a/gamemode/core/inventory/inventory.lua b/gamemode/core/inventory/inventory.lua index f6e8f9c..47a7eb3 100644 --- a/gamemode/core/inventory/inventory.lua +++ b/gamemode/core/inventory/inventory.lua @@ -19,6 +19,10 @@ The below are automatically made if they do not exist. inv:AddObserver(tbl_other) ::number_id inv:RemoveObserver(number_id) ::nil + ------------------------------------------------------ + These fields should be defined when an inventory is created, before it can be used + inv.Owner ::entity + inv.id ::number The table used for "position" is not defined, and may vary from inventory to inventory, but a single inventory type should only use a single type of position. @@ -49,6 +53,11 @@ end local inventories = {} --Master list local function DefaultAddObserver(self,tbl) + assert(tbl ~= nil,"Cannot add a nil observer!") + for k,v in pairs({"Put","Remove"}) do + assert(tbl[v] ~= nil,"Cannot add an observer without a " .. v .. "! observer was:\n\t" .. table.concat(table.GetKeys(tbl),"\n\t")) + end + if self.observers == nil then self.observers = {} end self.observers[#self.observers + 1] = tbl return #self.observers @@ -65,6 +74,8 @@ local function SetDefaultObservers(tbl) local oldput,oldremove = tbl.Put,tbl.Remove tbl.Put = function(self,position,item) for k,v in pairs(self.observers) do + print("Calling put on observer:") + PrintTable(v) v:Put(position,item) end oldput(self,position,item) @@ -113,12 +124,20 @@ end function inv.CreateInventory(name) print("Createing inventory", name) assert(inventories[name] ~= nil, string.format("Tried to create a copy of inventory that does not exist:%s\nValid inventories are:\n\t%s",name,table.concat(table.GetKeys(inventories),"\n\t"))) - return TableCopy(inventories[name]) + local ret = TableCopy(inventories[name]) + ret.observers = {} + return ret end --Recreates an inventory from data function inv.CreateInventoryFromData(name,data) - return inv.CreateInventory(name):DeSerialize(data) + local tinv = inv.CreateInventory(name) + print("tinv was", tinv) + PrintTable(tinv) + tinv:DeSerialize(data) + print("is now",tinv) + PrintTable(tinv) + return tinv end --Must be called in a coroutine. diff --git a/gamemode/core/inventory/item.lua b/gamemode/core/inventory/item.lua index 86bd105..354472a 100644 --- a/gamemode/core/inventory/item.lua +++ b/gamemode/core/inventory/item.lua @@ -32,11 +32,12 @@ function itm.RegisterItem(tbl) assert(tbl[v] ~= nil, string.format("Attempted to register item without field %q",v)) end assert(items[tbl.Name] == nil, string.format("Attempted to register 2 items with the same name %q",tbl.Name)) + items[tbl.Name] = tbl log.debug("Registered item: " .. tbl.Name .. "\n") end function itm.GetItemByName(name) - assert(items[name] ~= nil,string.format("Attempted to get item with invalid name %q",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"))) return items[name] end @@ -53,7 +54,7 @@ function itm.DeriveItem(tbl,name) --Create a flywieght copy local ret = tbl local mt = { - __index = function(tbl,key) + __index = function(ntbl,key) return items[name][key] end } diff --git a/gamemode/core/inventory/sv_invtracker.lua b/gamemode/core/inventory/sv_invtracker.lua index 3bb2b37..98e0268 100644 --- a/gamemode/core/inventory/sv_invtracker.lua +++ b/gamemode/core/inventory/sv_invtracker.lua @@ -32,11 +32,16 @@ end) ]] net.Receive("art_RequestInvMove",function(len,ply) print("ply",ply,"requested inv move") + --Read the data from the net message local froment,toent = net.ReadEntity(),net.ReadEntity() local frominvid,toinvid = net.ReadUInt(32),net.ReadUInt(32) local frompos,topos = net.ReadTable(),net.ReadTable() --Make sure the player is not stealing! - assert(not (froment:IsPlayer() and toent:IsPlayer()), "Tried to move item between players!") + assert(not (froment:IsPlayer() and toent:IsPlayer() and froment ~= toent), "Tried to move item between players!") + print("froment",froment) + print("froment.data:",froment.data) + print("froment.data.inventories",froment.data.inventories) + print("invid:",froment.data.inventories[frominvid]) assert(froment.data ~= nil and froment.data.inventories ~= nil and froment.data.inventories[frominvid] ~= nil, "From entity did not have that inventory!") assert(toent.data ~= nil and toent.data.inventories ~= nil and toent.data.inventories[toinvid] ~= nil, "To entity did not have that inventory!") local frominv = froment.data.inventories[frominvid] @@ -46,35 +51,91 @@ net.Receive("art_RequestInvMove",function(len,ply) assert(toinv:CanFitIn(topos,item), "Could not fit the item in that position!") --If we've gotten here without error, we're all good! Move the item! frominv:Remove(frompos) - toinv:Put(topos) + toinv:Put(topos,item) end) -local slots = { - "inv_Head", - "inv_Shoulders", - "inv_Chest", - "inv_Arms", - "inv_Hands", - "inv_Legs", - "inv_Belt", - "inv_Feet", - "inv_Back", -} +local function ClearInventories(ply) + ply.data = {} + ply.data.inventories = {} +end + +--Updates the client side inventory whenever the inventory is updated server side +local function MakeInventoryObserver(ply,invid) + local observer = {} + observer.Put = function(self,pos,item) + print("In observer, item was", item) + PrintTable(item) + local name = item.Name + local data = item:Serialize() + net.Start("art_UpdateInventory") + net.WriteUInt(invid,32) + net.WriteBool(true) + net.WriteTable(pos) + net.WriteString(name) + net.WriteUInt(#data,32) + net.WriteData(data,#data) + net.Send(ply) + end + observer.Remove = function(self,pos) + net.Start("art_UpdateInventory") + net.WriteUInt(invid,32) + net.WriteBool(false) + net.WriteTable(pos) + net.Send(ply) + end + return observer +end + +local function GiveInventoryTo(ply,name) + local i = inv.CreateInventory(name) + i.owner = ply + local nid = #ply.data.inventories + 1 + i.id = nid + local dat = i:Serialize() + local observer = MakeInventoryObserver(ply,nid) + i:AddObserver(observer) + ply.data.inventories[nid] = i -concommand.Add("SendMeData",function(ply,cmd,args) - local i = inv.CreateInventory("Equipment") - if not ply.data then ply.data = {} end - if not ply.data.inventories then ply.data.inventories = {} end - ply.data.inventories[1] = i net.Start("art_ObserveInventory") - net.WriteUInt(1,32) - net.WriteString("Equipment") - local data = i:Serialize() - net.WriteUInt(#data,32) - net.WriteData(data,#data) + net.WriteUInt(nid,32) + net.WriteString(name) + net.WriteUInt(#dat,32) + net.WriteData(dat,#dat) + net.WriteEntity(ply) net.Send(ply) +end +concommand.Add("SendMeData",function(ply,cmd,args) + ClearInventories(ply) + GiveInventoryTo(ply,"Equipment") net.Start("art_load_player_data") net.WriteTable({}) net.Send(ply) end) + +concommand.Add("ShowMyInventories",function(ply,cmd,args) + PrintTable(ply.data.inventories) +end) + +concommand.Add("AddInventory",function(ply,cmd,args) + GiveInventoryTo(ply,args[1]) +end) + +concommand.Add("GiveItem",function(ply,cmd,args) + local itmname = args[1] + local item = itm.GetItemByName(itmname) + local foundplacefor = false + for k,v in pairs(ply.data.inventories) do + print("Trying to find a place in ", v.Name) + local pf = v:FindPlaceFor(item) + print("It returned ", pf) + if pf ~= nil then + v:Put(pf,item) + foundplacefor = true + break + end + end + if not foundplacefor then + print("I couldn't find a place to put it!") + end +end) diff --git a/gamemode/core/npc/cl_npcmap.lua b/gamemode/core/npc/cl_npcmap.lua index 5a9de7e..ea7293e 100644 --- a/gamemode/core/npc/cl_npcmap.lua +++ b/gamemode/core/npc/cl_npcmap.lua @@ -61,6 +61,7 @@ hook.Add("Initialize","loadmapicons",function() end local mapiconstxt = file.Read("artery/client/"..mapname.."/known.txt") + if not mapiconstxt then return end --File does not exist! for k,v in pairs(string.Explode("\r?\n",mapiconstxt,true)) do local isleaf = tobool(v[1]) local ttbl = string.Explode(",",v) |
