From 9adcb3c73a8d0e7ecfe66b30da630c6c2e67f03a Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 27 Aug 2016 17:08:46 -0400 Subject: Moved prayers to their own system --- gamemode/shared/inventory.lua | 168 ++++++++++++++++++++++++++++++------------ 1 file changed, 122 insertions(+), 46 deletions(-) (limited to 'gamemode/shared/inventory.lua') diff --git a/gamemode/shared/inventory.lua b/gamemode/shared/inventory.lua index 9cc8fca..382ebd5 100644 --- a/gamemode/shared/inventory.lua +++ b/gamemode/shared/inventory.lua @@ -10,8 +10,11 @@ local invfuncs = include("inventory_common.lua") --A 2d array of the inventory. pmeta.Inventory = pmeta.Inventory or {} +--Money +pmeta.Inventory.Credits = pmeta.Inventory.Credits or 0 --each backpack has 1:a tbl containing items or false, 2: a tbl {width,height} and 3:name 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 or {} local equipedslots = { @@ -21,6 +24,38 @@ for _,v in pairs(equipedslots) do pmeta.Inventory.Equiped[v] = pmeta.Inventory.Equiped[v] or false end +if SERVER then + --- Gets a player's credits. + -- Gets the number of credits a player has + -- @return The number of credits for this player + function pmeta:GetCredits() + return self.Inventory.Credits + end + + util.AddNetworkString( "art_synch_credits" ) + local function SynchCredits(ply) + net.Start("art_synch_credits") + net.WriteUInt(ply.Inventory.Credits,32) + net.Send(ply) + end + + --- Sets a player's credits. + -- Sets the number of credits this player has. Credits are synchronized after every set + -- @param num The number of credits to set on the player + function pmeta:SetCredits(num) + self.Inventory.Credits = num + SynchCredits(self) + end + +end + +if CLIENT then + net.Receive("art_synch_credits",function() + ART.Credits = net.ReadUInt(32) + print("I updated my credits! now:", ART.Credits) + end) +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 @@ -50,14 +85,28 @@ function pmeta:FindSpotForItem(item) end end ---- Checks if the player has an item by name --- -function pmeta:HasItem(itemname) +local function DefaultCompare(item, itemname) + return item.Name == itemname +end + +--- Checks if the player has an item. +-- Check to see if the player has an item. Supply the name of the item, or supply a function that takes 2 items, and returns true if they are equial. If any of the returns are nil, then all the the returns will be nil, and the item could not be found. +-- @param nameorcomparitor The item name as a string, or a function that returns true when given an item that you want. +-- @return row The row the items was found in +-- @return col The column the item was found in +-- @return n The backpack number the item was found in. +function pmeta:HasItem(nameorcomparitor) + local comparitor + if type(param) == "String" then + comparitor = function(t) return t.Name == nameorcomparitor end + else + comparitor = nameorcomparitor + end 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 + if itemin ~= false and comparitor(itemin) then return row,col,n end end @@ -65,6 +114,11 @@ function pmeta:HasItem(itemname) end end +--- Removes an item. +-- Remoes an item in the backpack of the player +-- @param backpack The backpack to remove the item from +-- @param row The row in the backpack the item is located at +-- @param col The column in the backpack the item is located at function pmeta:RemoveItemAt(backpack,row,col) local item = self.Inventory.Backpacks[backpack][1][row][col] for k = 1,#item.Shape do @@ -75,12 +129,21 @@ function pmeta:RemoveItemAt(backpack,row,col) self:SynchronizeInventory() end +--- Gives an item to a player. +-- Gives an item to the player in the next avaliable slot the item can fit in +-- @param item The item to give the player function pmeta:GiveItem(item) local x,y,b = self:FindSpotForItem(item) self:PutInvItem(b,x,y,item) self:SynchronizeInventory() end +--- Check if an item can fit in a position in a backpack +-- Check if an item can fit in a specific position in a specific backpack +-- @param backpack The backpack to try to fit the item in +-- @param x The row to try to fit the item in +-- @param y The column to try to fit the item in +-- @return bool If the item can fit in the backpack function pmeta:CanFitInBackpack(backpack,x,y,item) return invfuncs.CanFitInBackpack(backpack,x,y,item) end @@ -92,50 +155,71 @@ if SERVER then util.AddNetworkString("unequipitem") end -net.Receive("unequipitem",function(len,ply) - local itemslot = net.ReadString() - local tobackpack = net.ReadUInt(16) - local topos = {} - topos[1] = net.ReadUInt(16) - topos[2] = net.ReadUInt(16) - local item = ply.Inventory.Equiped[itemslot] - if ply:CanFitInBackpack( - ply.Inventory.Backpacks[tobackpack], - topos[1], - topos[2], +--- Unequips an item. +-- Unequips an item, and puts it in the specified backpack (makes sure that it's possible first) +-- @param equipslot The equipment slot the item is in right now +-- @param backpack The destination backpack to put the item into +-- @param row The row in the backpack to put the item +-- @param col The column in the backpack to put the item +function pmeta:UnEquip(equipslot, backpack, row, col) + local item = self.Inventory.Equiped[equipslot] + if self:CanFitInBackpack( + self.Inventory.Backpacks[backpack], + row, + col, item ) then - ply.Inventory.Equiped[itemslot] = false - ply:PutInvItem(tobackpack,topos[1],topos[2],item) + self.Inventory.Equiped[equipslot] = false + self:PutInvItem(tobackpack,row,col,item) if item.onUnEquip ~= nil then - item:onUnEquip(ply) + item:onUnEquip(self) end - ply:SynchronizeInventory() + self:SynchronizeInventory() + else + error("Could not fit item in backpack, client might be de-synchronized") end +end + +net.Receive("unequipitem",function(len,ply) + local itemslot = net.ReadString() + local tobackpack = net.ReadUInt(16) + local row = net.ReadUInt(16) + local col = net.ReadUInt(16) + + ply:UnEquip(itemslot,tobackpack,row,col) end) -net.Receive("equipitem",function(len,ply) - local backpacknum = net.ReadUInt(16) - local frompos = {} - frompos[1] = net.ReadUInt(16) - frompos[2] = net.ReadUInt(16) - local equippos = net.ReadString() - local item = ply.Inventory.Backpacks[backpacknum][1][frompos[1]][frompos[2]] - if item.Equipable ~= nil and item.Equipable == equippos then +--- Equips an item +-- Moves an item from a backpack to an equiped slot, makes sure the item can be equiped in that slot first +-- @param frombackpack The backpack to take the item from +-- @param fromrow The row to take the item from +-- @param fromcol The column to take the item from +-- @param toslo The equipment slot to put the item into +function pmeta:EquipItem(frombackpack, fromrow, fromcol, toslot) + local item = self.Inventory.Backpacks[frombackpack][1][fromrow[1]][fromcol[2]] + if item.Equipable ~= nil and item.Equipable == toslot then --Remove from the backpack for k = 1,#item.Shape do - for i = 1,#(item.Shape[k]) do + for i = 1,#item.Shape[k] do if k == 1 and i == 1 then continue end - ply.Inventory.Backpacks[backpacknum][1][frompos[1] + k - 1][frompos[2] + i - 1] = false + self.Inventory.Backpacks[frombackpack][1][fromrow + k - 1][fromcol + i - 1] = false end end - ply.Inventory.Backpacks[backpacknum][1][frompos[1]][frompos[2]] = false - ply.Inventory.Equiped[equippos] = item + self.Inventory.Backpacks[frombackpack][1][fromrow][fromcol] = false + self.Inventory.Equiped[toslot] = item if item.onEquip ~= nil then - item:onEquip(ply) + item:onEquip(self) end - ply:SynchronizeInventory() + self:SynchronizeInventory() end +end + +net.Receive("equipitem",function(len,ply) + local backpacknum = net.ReadUInt(16) + local fromrow = net.ReadUInt(16) + local fromcol = net.ReadUInt(16) + local equippos = net.ReadString() + ply:EquipItem(backpacknum,fromrow,fromcol,equippos) end) net.Receive("moveitem",function(len,ply) @@ -172,6 +256,8 @@ net.Receive("moveitem",function(len,ply) toent:SynchronizeInventory() end) +--- Loads a player's inventory +-- @param json The JSON string to create the player's inventory from function pmeta:LoadInventory(json) local reinv = util.JSONToTable(json) for k,v in pairs(reinv) do @@ -180,6 +266,8 @@ function pmeta:LoadInventory(json) self:SynchronizeInventory() end +--- Networks a player's inventory +-- Makes sure the client's version of the inventory matches up to the server's version, this should be called after manipulateing the client's inventory in any way. function pmeta:SynchronizeInventory() net.Start("synchinventory") net.WriteEntity(self) @@ -228,19 +316,7 @@ if CLIENT then what.Inventory.Equiped[v] = false end end - local discopy = LocalPlayer().invdisplays - LocalPlayer().invdisplays = {} - for k,ptbl in pairs(discopy) do - if not ptbl.panel:IsValid() then continue end - if ptbl.panel.Close ~= nil then - ptbl.panel:Close() - else - print(ptbl.panel) - error("panel has no close method") - ptbl.panel:Remove() - end - ptbl.redraw() - end + ART.RefreshDisplays() end) end -- cgit v1.2.3-70-g09d2