aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/inventory
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-02-18 21:55:55 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-02-18 21:55:55 -0500
commita22cbeddc5f8fb61e87a30aa14ba354de5cf4431 (patch)
tree297c1dbfb23185c5246e1dd7bdec52253a24ba60 /gamemode/core/inventory
parentf4ee62bb0725a3ae94477b2818071f506e4dfd9f (diff)
downloadartery-a22cbeddc5f8fb61e87a30aa14ba354de5cf4431.tar.gz
artery-a22cbeddc5f8fb61e87a30aa14ba354de5cf4431.tar.bz2
artery-a22cbeddc5f8fb61e87a30aa14ba354de5cf4431.zip
Updates
Diffstat (limited to 'gamemode/core/inventory')
-rw-r--r--gamemode/core/inventory/inventory.lua30
-rw-r--r--gamemode/core/inventory/item.lua5
-rw-r--r--gamemode/core/inventory/sv_invtracker.lua84
3 files changed, 91 insertions, 28 deletions
diff --git a/gamemode/core/inventory/inventory.lua b/gamemode/core/inventory/inventory.lua
index 47a7eb3..b4c025c 100644
--- a/gamemode/core/inventory/inventory.lua
+++ b/gamemode/core/inventory/inventory.lua
@@ -5,20 +5,21 @@
CreateInventoryFromData(string_name,string_data)::table_inventory)
DeriveInventory(string_name) ::table_inventory
Inventories have the following structure
- inv.Name ::string
- inv:FindPlaceFor(item) ::table_position or nil
- inv:CanFitIn(table_position,item) ::true or string_explanation
- inv:Put(table_position,item) ::nil
- inv:Has(string_or_compare_func) ::table_position or nil
- inv:Remove(position) ::table_item
- inv:Get(position) ::table_item
- inv:Serialize() ::string
- inv:DeSerialize(str) ::table_inventory
+ field returns description
+ inv.Name ::string The name!
+ inv:FindPlaceFor(item) ::table_position or nil Finds a place for the item
+ inv:CanFitIn(table_position,item) ::boolean Check if the item can fit in the position
+ inv:Put(table_position,item) ::nil Put an item in at the position
+ inv:Has(string_or_compare_func) ::table_position or nil find an item in the inventory
+ inv:Remove(position) ::table_item Remove an item from the position
+ inv:Get(position) ::table_item Get the item at a position
+ inv:Serialize() ::string Serialize the item to store it in a db
+ inv:DeSerialize(str) ::table_inventory recreate the item from data in serialize
The above fields must be defined for new inventories.
-----------------------------------------------------
The below are automatically made if they do not exist.
- inv:AddObserver(tbl_other) ::number_id
- inv:RemoveObserver(number_id) ::nil
+ inv:AddObserver(tbl_other) ::number_id Whenever put or remove is called on this inventory, tbl_other's put() and remove() is also called, for easy networking to whoever needs it
+ inv:RemoveObserver(number_id) ::nil Removes an observer from the inventory
------------------------------------------------------
These fields should be defined when an inventory is created, before it can be used
inv.Owner ::entity
@@ -31,9 +32,6 @@
Serialize() should take this inventories contents and return a string that it can recreate this inventory from. DeSerialize should create a self.Copy() with the appropriate fields set. Take advantage of the fact that items must also have Serialize() and DeSerialize() methods.
]]
-local thm = nrequire("colortheme.lua")
-local log = nrequire("log.lua")
-
local inv = {}
--Creates a partial copy of a table(tables are copied, functions are not)
@@ -64,7 +62,7 @@ local function DefaultAddObserver(self,tbl)
end
local function DefaultRemoveObserver(self,observer_id)
for i = observer_id, #self.observers do
- self.observers[i] = self.observers + 1
+ self.observers[i] = self.observers[i + 1]
end
end
local function SetDefaultObservers(tbl)
@@ -117,7 +115,7 @@ function inv.RegisterInventory(tbl)
SetDefaultObservers(tbl)
end
inventories[tbl.Name] = tbl
- log.debug("Registered inventory: " .. tbl.Name .. "\n")
+ print("Registered inventory: " .. tbl.Name)
end
--Create an inventory
diff --git a/gamemode/core/inventory/item.lua b/gamemode/core/inventory/item.lua
index 354472a..dd788d6 100644
--- a/gamemode/core/inventory/item.lua
+++ b/gamemode/core/inventory/item.lua
@@ -17,9 +17,6 @@
The above must be defined for every item
Items may also have methods from one or more interfaces registered with RegisterInterface
]]
-local log = nrequire("log.lua")
-print("in item.lua, log is:")
-PrintTable(log)
local itm = {}
local required_fields = {
@@ -33,7 +30,7 @@ function itm.RegisterItem(tbl)
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")
+ print("Registered item: " .. tbl.Name)
end
function itm.GetItemByName(name)
diff --git a/gamemode/core/inventory/sv_invtracker.lua b/gamemode/core/inventory/sv_invtracker.lua
index 98e0268..5331261 100644
--- a/gamemode/core/inventory/sv_invtracker.lua
+++ b/gamemode/core/inventory/sv_invtracker.lua
@@ -4,6 +4,7 @@
local inv = nrequire("inventory/inventory.lua")
local itm = nrequire("item.lua")
+local track = {}
for k,v in pairs({
"art_ObserveInventory",
@@ -41,8 +42,15 @@ net.Receive("art_RequestInvMove",function(len,ply)
print("froment",froment)
print("froment.data:",froment.data)
print("froment.data.inventories",froment.data.inventories)
+ PrintTable(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!")
+ print("toent",toent)
+ print("toent.data",toent.data)
+ print("toent.data.inventories",toent.data.inventories)
+ PrintTable(toent.data.inventories)
+ print("toinvid",toinvid)
+ print("toent.data.inventories[invid]",toent.data.inventories[toinvid])
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]
local toinv = toent.data.inventories[toinvid]
@@ -54,13 +62,13 @@ net.Receive("art_RequestInvMove",function(len,ply)
toinv:Put(topos,item)
end)
-local function ClearInventories(ply)
+function track.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)
+function track.MakeInventoryObserver(ply,invid)
local observer = {}
observer.Put = function(self,pos,item)
print("In observer, item was", item)
@@ -86,13 +94,25 @@ local function MakeInventoryObserver(ply,invid)
return observer
end
-local function GiveInventoryTo(ply,name)
+function track.NotifyPlayerOfInventory(ply,inv)
+ local initaldat = inv:Serialize()
+ net.Start("art_ObserveInventory")
+ net.WriteUInt(inv.id,32)
+ net.WriteString(inv.Name)
+ net.WriteUInt(#initaldat,32)
+ net.WriteData(initaldat,#initaldat)
+ print("Before sending, inv owner is", inv.Owner)
+ net.WriteEntity(inv.Owner)
+ net.Send(ply)
+end
+
+function track.GiveInventoryTo(ply,name)
local i = inv.CreateInventory(name)
- i.owner = ply
+ i.Owner = ply
local nid = #ply.data.inventories + 1
i.id = nid
local dat = i:Serialize()
- local observer = MakeInventoryObserver(ply,nid)
+ local observer = track.MakeInventoryObserver(ply,nid)
i:AddObserver(observer)
ply.data.inventories[nid] = i
@@ -101,13 +121,59 @@ local function GiveInventoryTo(ply,name)
net.WriteString(name)
net.WriteUInt(#dat,32)
net.WriteData(dat,#dat)
+ net.WriteEntity(i.Owner)
+ net.Send(ply)
+end
+
+function track.GiveInventoryWithData(ply,name,data)
+ print("Giveing inventory with data")
+ local i = inv.CreateInventoryFromData(name,data)
+ local nid = #ply.data.inventories + 1
+ local observer = track.MakeInventoryObserver(ply,nid)
+ i.Owner = ply
+ i.id = nid
+ i:AddObserver(observer)
+ ply.data.inventories[nid] = i
+
+ net.Start("art_ObserveInventory")
+ net.WriteUInt(nid,32)
+ net.WriteString(name)
+ net.WriteUInt(#data,32)
+ net.WriteData(data,#data)
net.WriteEntity(ply)
net.Send(ply)
end
+
+
+--A shortcut for finding if a player has an item
+local plymeta = FindMetaTable("Player")
+
+function plymeta:HasItem(str)
+ for k,v in pairs(self.inventories) do
+ local p = v:Has(str)
+ if type(p) == "table" then
+ return {k,p}
+ end
+ end
+ return false
+end
+
+function plymeta:RemoveItem(tbl)
+ local nid = tbl[1]
+ local pos = tbl[2]
+ self.inventories[nid]:Remove(pos)
+end
+
+function track.SendPlayerData(ply)
+ net.Start("art_load_player_data")
+ net.WriteTable({})
+ net.Send(ply)
+end
+
concommand.Add("SendMeData",function(ply,cmd,args)
- ClearInventories(ply)
- GiveInventoryTo(ply,"Equipment")
+ track.ClearInventories(ply)
+ track.GiveInventoryTo(ply,"Equipment")
net.Start("art_load_player_data")
net.WriteTable({})
net.Send(ply)
@@ -118,7 +184,7 @@ concommand.Add("ShowMyInventories",function(ply,cmd,args)
end)
concommand.Add("AddInventory",function(ply,cmd,args)
- GiveInventoryTo(ply,args[1])
+ track.GiveInventoryTo(ply,args[1])
end)
concommand.Add("GiveItem",function(ply,cmd,args)
@@ -139,3 +205,5 @@ concommand.Add("GiveItem",function(ply,cmd,args)
print("I couldn't find a place to put it!")
end
end)
+
+return track