aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/inventory.lua
blob: 9cc8fca5e3885d43891b40db352a5a80e3489ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--[[
    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 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 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 = {
    "Head","Body","Legs","Boots","Gloves","Left","Right"
}
for _,v in pairs(equipedslots) do
    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
            for col = 1,v[2][1] do
                if self:CanFitInBackpack(v,row,col,item) then
                    return row,col,n
                end
            end
        end
    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)
    self:SynchronizeInventory()
end

function pmeta:CanFitInBackpack(backpack,x,y,item)
    return invfuncs.CanFitInBackpack(backpack,x,y,item)
end

if SERVER then
    util.AddNetworkString("synchinventory")
    util.AddNetworkString("moveitem")
    util.AddNetworkString("equipitem")
    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],
                            item
        ) then
            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)

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
        --Remove from the backpack
        for k = 1,#item.Shape 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
            end
        end
        ply.Inventory.Backpacks[backpacknum][1][frompos[1]][frompos[2]] = false
        ply.Inventory.Equiped[equippos] = item
        if item.onEquip ~= nil then
            item:onEquip(ply)
        end
        ply:SynchronizeInventory()
    end
end)

net.Receive("moveitem",function(len,ply)
    local froment = net.ReadEntity()
    local toent = net.ReadEntity()

    local frombackpack = net.ReadUInt(16)
    local tobackpack = net.ReadUInt(16)

    local frompos = {net.ReadUInt(16),net.ReadUInt(16)}
    local topos = {net.ReadUInt(16),net.ReadUInt(16)}

    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
    local item = froment.Inventory.Backpacks[frombackpack][1][frompos[1]][frompos[2]]

    --Set the shape it was at to false
    for k = 1,#item.Shape do
        for i = 1,#(item.Shape[k]) do
            if k == 1 and i == 1 then continue end
            froment.Inventory.Backpacks[frombackpack][1][frompos[1] + k - 1][frompos[2] + i - 1] = false
        end
    end
    froment.Inventory.Backpacks[frombackpack][1][frompos[1]][frompos[2]] = false
    --now check if it can fit in the backpack
    if  invfuncs.CanFitInBackpack(toent.Inventory.Backpacks[tobackpack],topos[1],topos[2],item) then
        invfuncs.PutItemInBackpack(toent.Inventory.Backpacks[tobackpack],topos[1],topos[2],item)
    else
        invfuncs.PutItemInBackpack(froment.Inventory.Backpacks[frombackpack],frompos[1],frompos[2],item)
    end
    froment:SynchronizeInventory()
    toent:SynchronizeInventory()
end)

function pmeta:LoadInventory(json)
    local reinv = util.JSONToTable(json)
    for k,v in pairs(reinv) do
        self.Inventory[k] = v
    end
    self:SynchronizeInventory()
end

function pmeta:SynchronizeInventory()
    net.Start("synchinventory")
        net.WriteEntity(self)
        net.WriteFloat(#self.Inventory.Backpacks)
        for k,v in pairs(self.Inventory.Backpacks) do
            invfuncs.SerializeBackpack(v)
        end
        for k,v in pairs(equipedslots) do
            if self.Inventory.Equiped and self.Inventory.Equiped[v] ~= false then
                net.WriteString(v)
                local data = self.Inventory.Equiped[v]:Serialize()
                net.WriteString(self.Inventory.Equiped[v].Name)
                net.WriteUInt(#data,32)
                net.WriteData(data,#data)
            end
        end
        net.WriteString("END_EQUIPED")
    net.Send(self)
end
if CLIENT then
    net.Receive("synchinventory",function(len,ply)
        if LocalPlayer().invdisplays == nil then
            LocalPlayer().invdisplays = {}
        end
        local what = net.ReadEntity()
        what.Inventory.Backpacks = {}
        local numbackpacks = net.ReadFloat()
        for k = 1,numbackpacks do
            local tbackpack = invfuncs.DeSerializeBackpack()
            table.insert(what.Inventory.Backpacks,tbackpack)
        end
        local neq = net.ReadString()
        local updated_slots = {}
        while neq ~= "END_EQUIPED" do
            local itemslot = neq
            local itemname = net.ReadString()
            local itemdata = net.ReadData(net.ReadUInt(32))
            local item = ART.GetItemByName(itemname):DeSerialize(itemdata)
            what.Inventory.Equiped[itemslot] = item
            updated_slots[itemslot] = true
            neq = net.ReadString()
        end
        if what.Inventory.Equiped ~= nil then
            for k,v in pairs(equipedslots) do
                if updated_slots[v] then continue end
                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
    end)
end

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 )