Autocrafting improvements

This commit is contained in:
kepler155c
2017-12-16 00:09:28 -05:00
parent 0ce537d4ce
commit 863b7ff600
7 changed files with 341 additions and 301 deletions

View File

@@ -130,21 +130,27 @@ function ChestAdapter:craftItems()
end
function ChestAdapter:provide(item, qty, slot, direction)
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
stack.nbtHash == item.nbtHash then
local amount = math.min(qty, stack.count)
if amount > 0 then
self.pushItems(direction or self.direction, key, amount, slot)
end
qty = qty - amount
if qty <= 0 then
break
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.pushItems(direction or self.direction, key, amount, slot)
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)

View File

@@ -1,23 +1,19 @@
local ChestAdapter = require('chestAdapter')
local ChestAdapter18 = require('chestAdapter18')
local MEAdapter = require('meAdapter')
local Adapter = { }
function Adapter.wrap(args)
local adapter = ChestAdapter18(args)
if adapter:isValid() then
return adapter
end
local adapters = {
'refinedAdapter',
'meAdapter',
'chestAdapter18',
'chestAdapter',
}
adapter = MEAdapter(args)
if adapter:isValid() then
return adapter
end
for _,adapterType in ipairs(adapters) do
local adapter = require(adapterType)(args)
adapter = ChestAdapter(args)
if adapter:isValid() then
return adapter
if adapter:isValid() then
return adapter
end
end
end

View File

@@ -71,6 +71,18 @@ function itemDB:get(key)
return item
end
end
if not key[3] then
for _,item in pairs(self.data) do
if item.name == key[1] and
item.damage == key[2] and
item.nbtHash then
item = Util.shallowCopy(item)
item.nbtHash = nil
return item
end
end
end
end
function itemDB:add(key, item)

View File

@@ -13,6 +13,7 @@ local function clearGrid(inventoryAdapter)
if count > 0 then
inventoryAdapter:insert(i, count)
if turtle.getItemCount(i) ~= 0 then
-- inventory is possibly full
return false
end
end
@@ -33,18 +34,24 @@ end
local function getItemCount(items, key)
local item = splitKey(key)
local count = 0
for _,v in pairs(items) do
if v.name == item.name and
(not item.damage or v.damage == item.damage) and
v.nbtHash == item.nbtHash then
return v.count
if item.damage then
return v.count
end
count = count + v.count
end
end
return 0
return count
end
local function turtleCraft(recipe, qty, inventoryAdapter)
clearGrid(inventoryAdapter)
if not clearGrid(inventoryAdapter) then
return false
end
for k,v in pairs(recipe.ingredients) do
local item = splitKey(v)
@@ -88,6 +95,7 @@ function Craft.sumIngredients(recipe)
for _,item in pairs(recipe.ingredients) do
t[item] = (t[item] or 0) + 1
end
-- need a check for crafting tool
return t
end
@@ -102,11 +110,9 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
local items = inventoryAdapter:listItems()
count = math.ceil(count / recipe.count)
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
local summedItems = Craft.sumIngredients(recipe)
for key,icount in pairs(summedItems) do
for key,icount in pairs(Craft.sumIngredients(recipe)) do
local itemCount = getItemCount(items, key)
if itemCount < icount * count then
local irecipe = Craft.recipes[key]
@@ -135,6 +141,38 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
return crafted * recipe.count
end
-- determine the full list of ingredients needed to craft
-- a quantity of a recipe.
-- negative quantities denote missing ingredients
function Craft.getResourceList(inRecipe, items, inCount)
local summed = { }
local throttle = Util.throttle()
local function sumItems(recipe, count)
for key,iqty in pairs(Craft.sumIngredients(recipe)) do
throttle()
local item = splitKey(key)
local summedItem = summed[key]
if not summedItem then
summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.recipes[key]
summedItem.count = getItemCount(items, key)
summed[key] = summedItem
end
summedItem.count = summedItem.count - (count * iqty)
if summedItem.recipe and summedItem.count < 0 then
local need = math.ceil(-summedItem.count / summedItem.recipe.count)
summedItem.count = 0
sumItems(summedItem.recipe, need)
end
end
end
sumItems(inRecipe, math.ceil(inCount / inRecipe.count))
return summed
end
-- given a certain quantity, return how many of those can be crafted
function Craft.getCraftableAmount(recipe, count, items, missing)
local function sumItems(recipe, summedItems, count)