aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/inventory.lua
diff options
context:
space:
mode:
Diffstat (limited to 'gamemode/shared/inventory.lua')
-rw-r--r--gamemode/shared/inventory.lua73
1 files changed, 57 insertions, 16 deletions
diff --git a/gamemode/shared/inventory.lua b/gamemode/shared/inventory.lua
index 42f4516..9cc8fca 100644
--- a/gamemode/shared/inventory.lua
+++ b/gamemode/shared/inventory.lua
@@ -1,27 +1,43 @@
-
-print("Hello from inventory.lua!")
+--[[
+ Various functions to work with inventories
+]]
+--- Various functions to deal with inventories.
+-- @module Player
local pmeta = FindMetaTable("Player")
local emeta = FindMetaTable("Entity")
local invfuncs = include("inventory_common.lua")
--A 2d array of the inventory.
-pmeta.Inventory = {}
+pmeta.Inventory = pmeta.Inventory or {}
--each backpack has 1:a tbl containing items or false, 2: a tbl {width,height} and 3:name
-pmeta.Inventory.Backpacks = {}
+pmeta.Inventory.Backpacks = pmeta.Inventory.Backpacks or {}
--Eqiped stuff at base, player has 1=Head, 2=Body, 3=Legs, 4=Boots, 5=Gloves, 6=Left Hand, 7=Right Hand
-pmeta.Inventory.Equiped = {}
+pmeta.Inventory.Equiped = pmeta.Inventory.Equiped or {}
local equipedslots = {
"Head","Body","Legs","Boots","Gloves","Left","Right"
}
for _,v in pairs(equipedslots) do
- pmeta.Inventory.Equiped[v] = false
+ pmeta.Inventory.Equiped[v] = pmeta.Inventory.Equiped[v] or false
end
+--- Puts an item in the inventory.
+-- Puts an item in an inventory, overwriteing all other items it might be overlapping, you should check to make sure you're not over writeing something first.
+-- @param backpack the backpack number this item should be placed in
+-- @param x the column in the backpack this item should be placed in
+-- @param y the row in the backpack this item should be placed in
+-- @param item the item to place in the backpack.
+-- @see Player.FindSpotForItem
function pmeta:PutInvItem(backpack,x,y,item)
invfuncs.PutItemInBackpack(self.Inventory.Backpacks[backpack],x,y,item)
end
+--- Finds a spot for an item.
+-- Finds a backpack, row, and column for an item in a player's inventory.
+-- @param item The item to try and fit into the backpack.
+-- @return row The row where a spot was found
+-- @return col The column where a spot was found
+-- @return n The backpack number where a spot was found
function pmeta:FindSpotForItem(item)
for n,v in pairs(self.Inventory.Backpacks) do
for row = 1,v[2][2] do
@@ -34,6 +50,31 @@ function pmeta:FindSpotForItem(item)
end
end
+--- Checks if the player has an item by name
+--
+function pmeta:HasItem(itemname)
+ for n,v in pairs(self.Inventory.Backpacks) do
+ for row = 1,v[2][2] do
+ for col = 1,v[2][1] do
+ local itemin = v[1][row][col]
+ if itemin ~= false and itemin.Name == itemname then
+ return row,col,n
+ end
+ end
+ end
+ end
+end
+
+function pmeta:RemoveItemAt(backpack,row,col)
+ local item = self.Inventory.Backpacks[backpack][1][row][col]
+ for k = 1,#item.Shape do
+ for i = 1,#(item.Shape[k]) do
+ self.Inventory.Backpacks[backpack][1][row + k - 1][col + i - 1] = false
+ end
+ end
+ self:SynchronizeInventory()
+end
+
function pmeta:GiveItem(item)
local x,y,b = self:FindSpotForItem(item)
self:PutInvItem(b,x,y,item)
@@ -64,9 +105,12 @@ net.Receive("unequipitem",function(len,ply)
topos[2],
item
) then
- ply.Inventory.Equiped[itemslot] = false
- ply:PutInvItem(tobackpack,topos[1],topos[2],item)
- ply:SynchronizeInventory()
+ ply.Inventory.Equiped[itemslot] = false
+ ply:PutInvItem(tobackpack,topos[1],topos[2],item)
+ if item.onUnEquip ~= nil then
+ item:onUnEquip(ply)
+ end
+ ply:SynchronizeInventory()
end
end)
@@ -104,13 +148,10 @@ net.Receive("moveitem",function(len,ply)
local frompos = {net.ReadUInt(16),net.ReadUInt(16)}
local topos = {net.ReadUInt(16),net.ReadUInt(16)}
- print("Moveing from ",frompos[1],frompos[2],"to",topos[1],topos[2])
-
if froment:IsPlayer() and toent:IsPlayer() and (froment ~= toent) then--Just don't allow stealing between players, anything else is fine
ply:PrintMessage( HUD_PRINTCENTER, "You can't steal from this person!" )
return
end
- print("Passed the stealing check")
local item = froment.Inventory.Backpacks[frombackpack][1][frompos[1]][frompos[2]]
--Set the shape it was at to false
@@ -121,10 +162,8 @@ net.Receive("moveitem",function(len,ply)
end
end
froment.Inventory.Backpacks[frombackpack][1][frompos[1]][frompos[2]] = false
- print("Set shape to false")
--now check if it can fit in the backpack
if invfuncs.CanFitInBackpack(toent.Inventory.Backpacks[tobackpack],topos[1],topos[2],item) then
- print("It can fit there")
invfuncs.PutItemInBackpack(toent.Inventory.Backpacks[tobackpack],topos[1],topos[2],item)
else
invfuncs.PutItemInBackpack(froment.Inventory.Backpacks[frombackpack],frompos[1],frompos[2],item)
@@ -142,7 +181,6 @@ function pmeta:LoadInventory(json)
end
function pmeta:SynchronizeInventory()
- print("Player synchronize called")
net.Start("synchinventory")
net.WriteEntity(self)
net.WriteFloat(#self.Inventory.Backpacks)
@@ -192,7 +230,6 @@ if CLIENT then
end
local discopy = LocalPlayer().invdisplays
LocalPlayer().invdisplays = {}
- PrintTable(discopy)
for k,ptbl in pairs(discopy) do
if not ptbl.panel:IsValid() then continue end
if ptbl.panel.Close ~= nil then
@@ -211,3 +248,7 @@ concommand.Add("artery_showinventory",function(ply,cmd,args)
PrintTable(ply.Inventory)
PrintTable(ply.ClientInventory)
end)
+
+hook.Add( "PlayerSpawn", "artery_disable_sprint", function(ply)
+ ply:SetRunSpeed(ply:GetWalkSpeed())
+end )