aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/shapedinventory/sh_shaped.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-08-07 18:08:00 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-08-07 18:08:00 -0400
commit0ce40afcb04587bbccd39fc64a4928607c285c51 (patch)
tree8a82b316b7300864aca612c6b0c059e14c15bd37 /gamemode/inventorysystem/shapedinventory/sh_shaped.lua
parent076b4245a757dc0ca9d1c9d234c32df546b1239e (diff)
downloadartery-0ce40afcb04587bbccd39fc64a4928607c285c51.tar.gz
artery-0ce40afcb04587bbccd39fc64a4928607c285c51.tar.bz2
artery-0ce40afcb04587bbccd39fc64a4928607c285c51.zip
Various additions and removals for server
Diffstat (limited to 'gamemode/inventorysystem/shapedinventory/sh_shaped.lua')
-rw-r--r--gamemode/inventorysystem/shapedinventory/sh_shaped.lua50
1 files changed, 29 insertions, 21 deletions
diff --git a/gamemode/inventorysystem/shapedinventory/sh_shaped.lua b/gamemode/inventorysystem/shapedinventory/sh_shaped.lua
index b77f378..09668e2 100644
--- a/gamemode/inventorysystem/shapedinventory/sh_shaped.lua
+++ b/gamemode/inventorysystem/shapedinventory/sh_shaped.lua
@@ -1,5 +1,6 @@
--[[
An inventory, with a shape!
+ Items are stored in a 1-d array, array is 1-indexed
]]
local reg = nrequire("inventory/inventory.lua")
@@ -9,19 +10,20 @@ if CLIENT then inv = nrequire("cl_shaped.lua") end
inv.Name = "Shaped Inventory"
inv.tracker = {}
-inv.dimx = 5
-inv.dimy = 5
+inv.width = 5
+inv.height = 5
-local function calcposition(dimx,dimy,x,y)
- return ((x-1) * dimx) + y
+local function calcposition(width,height,row,col)
+ return ((row-1) * width) + col
end
-local function canfitin(self,x,y,shape)
+local function canfitin(self,arow,acol,shape)
for rn,row in ipairs(shape) do
for cn,col in ipairs(row) do
- local absx,absy = x + rn - 1, y + cn - 1
- local slot = calcposition(self.dimx,self.dimy,absx,absy)
- if col and ((self.tracker[slot] ~= nil) or (absx > self.dimx) or (absy > self.dimy)) then
+ local absrow,abscol = arow + rn - 1, acol + cn - 1
+ print("absrow was",absrow,"abscol was",abscol)
+ local slot = calcposition(self.width,self.height,absrow,abscol)
+ if col and ((self.tracker[slot] ~= nil) or (absrow > self.width) or (abscol > self.height)) then
return false
end
end
@@ -30,10 +32,10 @@ local function canfitin(self,x,y,shape)
end
function inv:FindPlaceFor(item)
- for x = 1, self.dimx do
- for y = 1, self.dimy do
- if canfitin(self,x,y,item.Shape) then
- return {x,y}
+ for row = 1, self.height do
+ for col = 1, self.width do
+ if canfitin(self,row,col,item.Shape) then
+ return {row,col}
end
end
end
@@ -54,14 +56,16 @@ function inv:Put(tbl,item)
for rn,row in ipairs(item.Shape) do
for cn,col in ipairs(row) do
if col then
- local slot = calcposition(self.dimx,self.dimy,tbl[1] + rn - 1,tbl[2] + cn - 1)
+ local slot = calcposition(self.width,self.height,tbl[1] + rn - 1,tbl[2] + cn - 1)
self.tracker[slot] = 1
end
end
end
--Now set the item in the correct slot
- local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
+ print("Put item at ",tbl[1],tbl[2])
+ local slot = calcposition(self.width,self.height,tbl[1],tbl[2])
+ print("Slot is",slot)
self.tracker[slot] = item
end
@@ -74,22 +78,24 @@ function inv:Has(ptr)
end
for k,v in pairs(self.tracker) do
if type(v) == "table" and compare_func(v) then
- local x = math.floor(k / self.dimx)
- local y = k % self.dimx
- return {x,y}
+ local row = math.ceil(k / self.width)
+ local col = k % self.width
+ print("Has is retuning",row,col)
+ return {row,col}
end
end
return nil
end
function inv:Remove(tbl)
- local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
+ local slot = calcposition(self.width,self.height,tbl[1],tbl[2])
+ print("Slot is",slot)
local item = self.tracker[slot]
self.tracker[slot] = nil
for rn,row in ipairs(item.Shape) do
for cn,col in ipairs(row) do
if col then
- slot = calcposition(self.dimx,self.dimy,tbl[1] + rn - 1,tbl[2] + cn - 1)
+ slot = calcposition(self.width,self.height,tbl[1] + rn - 1,tbl[2] + cn - 1)
self.tracker[slot] = nil
end
end
@@ -97,7 +103,7 @@ function inv:Remove(tbl)
end
function inv:Get(tbl)
- local slot = calcposition(self.dimx,self.dimy,tbl[1],tbl[2])
+ local slot = calcposition(self.width,self.height,tbl[1],tbl[2])
return self.tracker[slot]
end
@@ -114,13 +120,15 @@ end
function inv:DeSerialize(str)
--TODO:Implement
+ local ret = table.Copy(self)
local tbl = util.JSONToTable(str)
for k,v in pairs(tbl) do
local name = k
local pos = v[1]
local data = v[2]
- self.tracker[pos] = itm.GetItemFromData(name,data)
+ ret.tracker[pos] = itm.GetItemFromData(name,data)
end
+ return ret
end