2018-12-14 20:38:35 +00:00
|
|
|
local C = Class:extend()
|
|
|
|
|
|
|
|
local ITEM_DATA = require 'database.items'
|
|
|
|
|
|
|
|
function C:new()
|
|
|
|
self._left = {}
|
|
|
|
for k, v in pairs(ITEM_DATA) do
|
|
|
|
local item = _.clone(v)
|
|
|
|
table.insert(self._left, item)
|
|
|
|
end
|
2019-03-11 12:12:11 +00:00
|
|
|
self._placed = {}
|
2018-12-14 20:38:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function _filterAny(item) return true end
|
|
|
|
|
|
|
|
function C:getAny()
|
|
|
|
return self:_getItem(_filterAny)
|
|
|
|
end
|
|
|
|
|
2019-03-11 12:12:11 +00:00
|
|
|
function C:getAnyByAttribute(attribute)
|
|
|
|
function _filterAnyByAttribute(item, attribute)
|
|
|
|
for k, v in pairs(item.attributes) do
|
|
|
|
if v == attribute then return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return self:_getItem(_filterAnyByAttribute)
|
2018-12-19 23:49:40 +00:00
|
|
|
end
|
|
|
|
|
2019-03-11 12:12:11 +00:00
|
|
|
function C:placeAny()
|
|
|
|
return self:place(self:getAny())
|
2018-12-20 04:24:00 +00:00
|
|
|
end
|
|
|
|
|
2019-03-11 12:12:11 +00:00
|
|
|
function C:placeAnyByAttribute(attributes)
|
|
|
|
return self:place(self:getAnyByAttribute(attributes))
|
2018-12-14 20:38:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function C:_getItem(filterFn)
|
|
|
|
-- Filter down to only applicable items.
|
|
|
|
local applicable = {}
|
2018-12-14 21:57:56 +00:00
|
|
|
local indexMap = {}
|
|
|
|
for index, item in ipairs(self._left) do
|
2018-12-14 20:38:35 +00:00
|
|
|
if filterFn(item) then
|
|
|
|
table.insert(applicable, item)
|
2018-12-14 21:57:56 +00:00
|
|
|
indexMap[item] = index
|
2018-12-14 20:38:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(#applicable >= 1, 'No applicable items!')
|
|
|
|
|
|
|
|
-- Select an item.
|
|
|
|
local selected = _.sample(applicable)
|
2018-12-14 21:57:56 +00:00
|
|
|
local index = indexMap[selected]
|
2019-03-11 12:12:11 +00:00
|
|
|
|
|
|
|
return selected, index
|
|
|
|
end
|
|
|
|
|
|
|
|
function C:place(item, index)
|
2018-12-14 20:38:35 +00:00
|
|
|
table.remove(self._left, index)
|
2019-03-11 12:12:11 +00:00
|
|
|
table.insert(self._placed, item)
|
|
|
|
return item
|
|
|
|
end
|
2018-12-14 20:38:35 +00:00
|
|
|
|
2019-03-11 12:12:11 +00:00
|
|
|
function C:getPlacedItems(item)
|
|
|
|
local placed = {table.unpack(self._placed)} -- clone the table
|
|
|
|
if(item) then table.insert(placed, item) end
|
|
|
|
return placed
|
2018-12-14 20:38:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return C
|