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
|
---An example inventory.
-- An inventory that can hold any kind of item. Inventories in Artery are pretty flexible, when you get started, decide on how you want to represent items in your inventory. Inventories use a "position table" to talk about where items are in the inventory. What the position table's structure is can be different for each type of inventory, but each inventory should not use more than one kind of position table. If you find yourself needing more than 1 position table, consider making 2 different inventories.
--@classmod invtbl
local inv = {}
local reg = nrequire("inventory.lua")
---Inventory name.
-- All inventories must have a name, names must be unique per TYPE of inventory.
inv.Name = "Example Inventory"
inv.items = {}
---Finds a spot for an item.
-- Finds a place in this inventory to place an item. Returns nil if the item cannot fit.
--@tparam invtbl self The instace we want to put the item into
--@tparam itemtbl itemtbl The item we want to put into the inventory
--@treturn table|nil Returns a position table if the item can fit, or nil if it cannot.
function inv.FindPlaceFor(self,itemtbl)
return {#self.items + 1}
end
---Check if the item can fit in the given position.
-- Finds if an item can be placed at a given position in this inventory
--@tparam invtbl self The instance to put the item into
--@tparam table position The position to put the item into
--@tparam itemtbl item The item to try to put in
--@treturn boolean True if the item can fit, false if it cannot
function inv.CanFitIn(self,position,item)
return self.items[position[1]] == nil
end
---Puts an item into the inventory.
-- Puts an item into the inventory at a given position
--@tparam invtbl self The inventory instance
--@tparam table position The position to put the item into
--@tparam itemtbl item The item to put into the inventory
function inv.Put(self,position,item)
self.items[position[1]] = item
end
---Checks if this inventory has an item.
-- This method checks if an inventory has an item, this can be passed EITHER a string, item name, OR a function that takes a itemtbl as it's single parameter and returns True if the item is what is being looked for or false if it is not.
--@tparam invtbl self The instance to look at
--@tparam string|function ptr The string or compare function to look for
--@treturn table|nil Returns the position of the item if the item was found, or nil if it was not
function inv.Has(self,ptr)
for k,v in pairs(self.items) do
if type(ptr) == "function" then
if ptr(v) then
return {k}
end
elseif type(ptr) == "string" then
if v.Name == ptr then
return {k}
end
else
error("Passed a " .. type(ptr) .. " to example inventory's Has() function")
end
end
end
---Removes an item from the inventory.
-- Removes an item by position, returns the item that was removed
--@tparam invtbl self The inventory instance to remove from
--@tparam table position The position to remove the item from
--@treturn itemtbl The item that was removed
--@raises error May error if the given position does not contain an item.
function inv.Remove(self,position)
local item = self.items[position[1]]
self.items[position[1]] = nil
return item
end
---Gets the item at a given position.
-- Returns the item at a given position in the inventory. May error if the given position does not have an item.
--@tparam invtbl self The instance to get from
--@tparam table position The position to look in
--@treturn itemtbl The item at the given position
--@raises error May error if the given position does not contain an item
function inv.Get(self,position)
return self.items[position[1]]
end
---Serailizes the inventory.
-- Returns a string that the inventory can be rebuilt from. This should include all items currently in the inventory. Take advantage of the fact that items must also have a Serailize() method.
--@tparam invtbl self The instance of the inventory to serialize
--@treturn string The string representation of this inventory
function inv.Serialize(self)
local items = {}
for k,v in pairs(self.items) do
items[k] = {v.Name,v:Serialize()}
end
return util.TableToJSON(items)
end
---Deserializes an inventory.
-- Takes the string returned by the Serialize() function and returns the inventory re-made
--@tparam invtbl self The prototype table, you probably want to make a table.Copy() of this
--@tparam string data The string returned by Serialize()
--@treturn invtbl The table after it has been deserialized
function inv.DeSerialize(self,str)
local cpy = table.Copy(self)
local json = util.JSONToTable(str)
for k,v in pairs(json) do
local item = itm.GetItemFromData(v[1],v[2])
cpy.items[k] = item
end
return cpy
end
reg.RegisterInventory(inv)
|