storage manager improvements

This commit is contained in:
kepler155c
2017-12-27 23:56:14 -05:00
parent 85c5f542a8
commit aa50b55ab1
8 changed files with 280 additions and 218 deletions

View File

@@ -53,9 +53,10 @@ end
function ChestAdapter:getCachedItemDetails(item, k)
local detail = itemDB:get(item)
if not detail then
pcall(function() detail = self.getItemMeta(k) end)
local s, m = pcall(function() detail = self.getItemMeta(k) end)
if not detail then
debug(item)
debug(m)
debug('no details')
-- error('Inventory has changed')
return
@@ -89,16 +90,16 @@ end
-- provide a consolidated list of items
function ChestAdapter:listItems(throttle)
self.cache = { }
local cache = { }
local items = { }
debug('listing')
throttle = throttle or Util.throttle()
for k,v in pairs(self.list()) do
if v.count > 0 then
local key = table.concat({ v.name, v.damage, v.nbtHash }, ':')
local entry = self.cache[key]
local entry = cache[key]
if not entry then
entry = self:getCachedItemDetails(v, k)
if not entry then
@@ -107,7 +108,7 @@ function ChestAdapter:listItems(throttle)
return -- Inventory has changed
end
entry.count = 0
self.cache[key] = entry
cache[key] = entry
table.insert(items, entry)
end
@@ -120,7 +121,12 @@ function ChestAdapter:listItems(throttle)
--read()
itemDB:flush()
return items
debug('done listing')
if not Util.empty(items) then
self.cache = cache
return items
end
debug('its empty')
end
function ChestAdapter:getItemInfo(item)
@@ -137,6 +143,13 @@ end
function ChestAdapter:craftItems()
end
function ChestAdapter:getPercentUsed()
if self.cache then
return math.floor(Util.size(self.cache) / self.getDrawerCount() * 100)
end
return 0
end
function ChestAdapter:provide(item, qty, slot, direction)
local s, m = pcall(function()
local stacks = self.list()
@@ -161,6 +174,30 @@ function ChestAdapter:provide(item, qty, slot, direction)
return s, m
end
function ChestAdapter:eject(item, qty, direction)
local s, m = pcall(function()
local stacks = self.list()
for key,stack in Util.rpairs(stacks) do
if stack.name == item.name and
(not item.damage or stack.damage == item.damage) and
(not item.nbtHash or stack.nbtHash == item.nbtHash) then
local amount = math.min(qty, stack.count)
if amount > 0 then
self.drop(key, amount, direction)
end
qty = qty - amount
if qty <= 0 then
break
end
end
end
end)
if not s then
debug(m)
end
return s, m
end
function ChestAdapter:extract(slot, qty, toSlot)
self.pushItems(self.direction, slot, qty, toSlot)
end

View File

@@ -78,6 +78,7 @@ function itemDB:get(key)
if key.nbtHash then
item = self:get({ name = key.name, damage = key.damage })
if item and (item.maxDamage > 0 or item.damage == key.damage) then
item = Util.shallowCopy(item)
item.nbtHash = key.nbtHash
return item
end

View File

@@ -66,6 +66,7 @@ local function turtleCraft(recipe, qty, inventoryAdapter)
inventoryAdapter:provide(item, provideQty, k)
if turtle.getItemCount(k) == 0 then -- ~= qty then
-- FIX: ingredients cannot be stacked
debug('failed ' .. v .. ' - ' .. provideQty)
return false
end
end
@@ -176,7 +177,7 @@ function Craft.getResourceList(inRecipe, items, inCount)
return summed
end
function Craft.getResourceList3(inRecipe, items, inCount)
function Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter)
local summed = { }
local throttle = Util.throttle()
@@ -192,38 +193,35 @@ function Craft.getResourceList3(inRecipe, items, inCount)
summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.recipes[key]
summedItem.count = Craft.getItemCount(items, key)
summedItem.ocount = summedItem.count
summedItem.total = 0
summedItem.need = 0
summedItem.used = 0
summedItem.missing = 0
summedItem.craftable = 0
summed[key] = summedItem
end
debug(key)
local total = count * iqty -- 4 * 2
local used = math.min(summedItem.count, total) -- 5
local need = total - used -- 3
if recipe.craftingTools and recipe.craftingTools[key] then
summedItem.total = 1
if summedItem.count > 0 then
summedItem.used = 1
need = 0
else
summedItem.need = 1
need = 1
end
else
summedItem.total = summedItem.total + total
summedItem.count = summedItem.count - used
summedItem.used = summedItem.used + used
summedItem.need = summedItem.need + need
end
if need > 0 then
if not summedItem.recipe then
debug(summedItem)
debug({ total, used, need })
summedItem.missing = summedItem.missing + need
craftable = math.min(craftable, math.floor(used / iqty))
summedItem.need = summedItem.need + need
else
local c = sumItems(summedItem.recipe, need) -- 4
craftable = math.min(craftable, math.floor((used + c) / iqty))
@@ -232,12 +230,17 @@ debug({ total, used, need })
end
end
--[[
if inventoryAdapter and craftable > 0 then
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter)
clearGrid(inventoryAdapter)
end
]]
return craftable * recipe.count
end
sumItems(inRecipe, inCount)
return summed
return sumItems(inRecipe, inCount), summed
end
-- given a certain quantity, return how many of those can be crafted