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.lua63
1 files changed, 51 insertions, 12 deletions
diff --git a/gamemode/shared/inventory.lua b/gamemode/shared/inventory.lua
index a8c1756..2165686 100644
--- a/gamemode/shared/inventory.lua
+++ b/gamemode/shared/inventory.lua
@@ -1,5 +1,7 @@
--[[
Various functions to work with inventories
+ player:GetCredits()
+ player:SetCredits(number credits)
]]
--- Various functions to deal with inventories.
-- @module Player
@@ -159,6 +161,7 @@ if SERVER then
util.AddNetworkString("moveitem")
util.AddNetworkString("equipitem")
util.AddNetworkString("unequipitem")
+ util.AddNetworkString("buyitem")
end
--- Unequips an item.
@@ -169,14 +172,16 @@ end
-- @param col The column in the backpack to put the item
function pmeta:UnEquip(equipslot, backpack, row, col)
local item = self.Inventory.Equiped[equipslot]
+ local tobackpack = self.Inventory.Backpacks[backpack]
+ print("Unequiping into backpack", tobackpack)
if self:CanFitInBackpack(
- self.Inventory.Backpacks[backpack],
+ tobackpack,
row,
col,
item
) then
self.Inventory.Equiped[equipslot] = false
- self:PutInvItem(tobackpack,row,col,item)
+ self:PutInvItem(backpack,row,col,item)
if item.onUnEquip ~= nil then
item:onUnEquip(self)
end
@@ -228,6 +233,50 @@ net.Receive("equipitem",function(len,ply)
ply:EquipItem(backpacknum,fromrow,fromcol,equippos)
end)
+net.Receive("buyitem",function(len,ply)
+ local itemname = net.ReadString()
+ local backpack = net.ReadUInt(8)
+ local j = net.ReadUInt(8)
+ local i = net.ReadUInt(8)
+ local item = ART.GetItemByName(itemname)
+ local nearshops = ents.FindInSphere( ply:GetPos(), 100 )
+ local cost
+ for k,v in pairs(nearshops) do
+ if v:GetClass() ~= "npc_shop" then goto nextent end
+ print("Checking if ", v, " has a ", itemname)
+ print("Shop items were")
+ PrintTable(v.shopitems)
+ for i,j in pairs(v.shopitems) do
+ if j[1] == itemname then
+ cost = j[2]
+ goto itemfound
+ end
+ end
+ ::nextent::
+ end
+ ::itemfound::
+ if cost == nil then
+ ply:ChatPrint("Could not find a shop selling that!")
+ return false
+ end
+ print("Found item, cost was ", cost)
+ local playercreds = ply:GetCredits()
+ local tbp = ply.Inventory.Backpacks[backpack]
+ local canfit = invfuncs.CanFitInBackpack(tbp,j,i,item)
+ print("Can it?",canfit)
+ if playercreds < cost then
+ ply:ChatPrint("You don't have enough credits to buy that!")
+ return false
+ end
+ if not canfit then
+ ply:ChatPrint("You can't put that into your packpack there!")
+ return false
+ end
+ playercreds:SetCredits(playercreds - cost)
+ invfuncs.PutItemInBackpack(tbp,j,i,item)
+ return true
+end)
+
net.Receive("moveitem",function(len,ply)
local froment = net.ReadEntity()
local toent = net.ReadEntity()
@@ -262,16 +311,6 @@ 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
- self.Inventory[k] = v
- end
- 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()