autocrafting improvements
This commit is contained in:
@@ -24,15 +24,14 @@ function ChestAdapter:init(args)
|
||||
Util.merge(self, args)
|
||||
|
||||
local chest
|
||||
if not self.autoDetect then
|
||||
if self.autoDetect then
|
||||
chest = Peripheral.getByMethod('list')
|
||||
else
|
||||
chest = Peripheral.getBySide(self.wrapSide)
|
||||
if chest and not chest.list then
|
||||
chest = nil
|
||||
end
|
||||
end
|
||||
if not chest then
|
||||
chest = Peripheral.getByMethod('list')
|
||||
end
|
||||
|
||||
if chest then
|
||||
Util.merge(self, chest)
|
||||
@@ -55,17 +54,11 @@ function ChestAdapter:getCachedItemDetails(item, k)
|
||||
if not detail then
|
||||
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
|
||||
end
|
||||
-- NOT SUFFICIENT
|
||||
if detail.name ~= item.name then
|
||||
debug('name change ?')
|
||||
debug(item)
|
||||
debug(detail)
|
||||
-- error('Inventory has changed')
|
||||
return
|
||||
end
|
||||
@@ -76,7 +69,6 @@ debug(detail)
|
||||
end
|
||||
end
|
||||
|
||||
debug('adding')
|
||||
itemDB:add(detail)
|
||||
end
|
||||
if detail then
|
||||
@@ -92,7 +84,6 @@ end
|
||||
function ChestAdapter:listItems(throttle)
|
||||
local cache = { }
|
||||
local items = { }
|
||||
debug('listing')
|
||||
throttle = throttle or Util.throttle()
|
||||
|
||||
for k,v in pairs(self.list()) do
|
||||
@@ -103,8 +94,6 @@ debug('listing')
|
||||
if not entry then
|
||||
entry = self:getCachedItemDetails(v, k)
|
||||
if not entry then
|
||||
debug(key)
|
||||
debug('inv changed')
|
||||
return -- Inventory has changed
|
||||
end
|
||||
entry.count = 0
|
||||
@@ -118,15 +107,12 @@ debug('listing')
|
||||
throttle()
|
||||
end
|
||||
end
|
||||
--read()
|
||||
itemDB:flush()
|
||||
|
||||
debug('done listing')
|
||||
if not Util.empty(items) then
|
||||
self.cache = cache
|
||||
return items
|
||||
end
|
||||
debug('its empty')
|
||||
end
|
||||
|
||||
function ChestAdapter:getItemInfo(item)
|
||||
@@ -177,11 +163,14 @@ end
|
||||
function ChestAdapter:eject(item, qty, direction)
|
||||
local s, m = pcall(function()
|
||||
local stacks = self.list()
|
||||
local maxStack = itemDB:getMaxCount(item)
|
||||
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)
|
||||
local amount = math.min(maxStack, math.min(qty, stack.count))
|
||||
debug({ key, amount })
|
||||
debug(stack)
|
||||
if amount > 0 then
|
||||
self.drop(key, amount, direction)
|
||||
end
|
||||
|
||||
18
apis/controllerAdapter.lua
Normal file
18
apis/controllerAdapter.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local Adapter = { }
|
||||
|
||||
function Adapter.wrap(args)
|
||||
local adapters = {
|
||||
'refinedAdapter',
|
||||
'meAdapter',
|
||||
}
|
||||
|
||||
for _,adapterType in ipairs(adapters) do
|
||||
local adapter = require(adapterType)(args)
|
||||
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Adapter
|
||||
@@ -2,8 +2,8 @@ local Adapter = { }
|
||||
|
||||
function Adapter.wrap(args)
|
||||
local adapters = {
|
||||
'refinedAdapter',
|
||||
'meAdapter',
|
||||
--'refinedAdapter',
|
||||
--'meAdapter',
|
||||
'chestAdapter18',
|
||||
'chestAdapter',
|
||||
}
|
||||
|
||||
@@ -141,10 +141,6 @@ function itemDB:getName(item)
|
||||
end
|
||||
|
||||
function itemDB:getMaxCount(item)
|
||||
if type(item) == 'string' then
|
||||
item = self:splitKey(item)
|
||||
end
|
||||
|
||||
local detail = self:get(item)
|
||||
if detail then
|
||||
return detail.maxCount
|
||||
|
||||
@@ -18,12 +18,22 @@ function RefinedAdapter:init(args)
|
||||
local defaults = {
|
||||
items = { },
|
||||
name = 'refinedStorage',
|
||||
direction = 'up',
|
||||
wrapSide = 'bottom',
|
||||
}
|
||||
Util.merge(self, defaults)
|
||||
Util.merge(self, args)
|
||||
|
||||
local controller = Peripheral.getByType('refinedstorage:controller') or
|
||||
Peripheral.getByMethod('listAvailableItems')
|
||||
local controller
|
||||
if self.autoDetect then
|
||||
controller = Peripheral.getByType('refinedstorage:controller')
|
||||
else
|
||||
controller = Peripheral.getBySide(self.wrapSide)
|
||||
if controller and not controller.listAvailableItems then
|
||||
controller = nil
|
||||
end
|
||||
end
|
||||
|
||||
if controller then
|
||||
Util.merge(self, controller)
|
||||
end
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Util = require('util')
|
||||
|
||||
local fs = _G.fs
|
||||
@@ -32,8 +33,11 @@ local function splitKey(key)
|
||||
return item
|
||||
end
|
||||
|
||||
function Craft.getItemCount(items, key)
|
||||
local item = splitKey(key)
|
||||
function Craft.getItemCount(items, item)
|
||||
if type(item) == 'string' then
|
||||
item = splitKey(item)
|
||||
end
|
||||
|
||||
local count = 0
|
||||
for _,v in pairs(items) do
|
||||
if v.name == item.name and
|
||||
@@ -124,6 +128,7 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
|
||||
local iqty = icount * count - itemCount
|
||||
local crafted = Craft.craftRecipe(irecipe, iqty, inventoryAdapter)
|
||||
if crafted ~= iqty then
|
||||
|
||||
turtle.select(1)
|
||||
return 0
|
||||
end
|
||||
@@ -145,102 +150,80 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
|
||||
return crafted * recipe.count
|
||||
end
|
||||
|
||||
local function makeRecipeKey(item)
|
||||
if type(item) == 'string' then
|
||||
item = splitKey(item)
|
||||
end
|
||||
return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':')
|
||||
end
|
||||
|
||||
function Craft.findRecipe(item)
|
||||
return Craft.recipes[makeRecipeKey(item)]
|
||||
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)
|
||||
function Craft.getResourceList(recipe, 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 = Craft.getItemCount(items, key)
|
||||
summed[key] = summedItem
|
||||
local function sumItems(key, count)
|
||||
local item = itemDB:splitKey(key)
|
||||
local summedItem = summed[key]
|
||||
if not summedItem then
|
||||
summedItem = Util.shallowCopy(item)
|
||||
summedItem.recipe = Craft.findRecipe(key)
|
||||
summedItem.count = Craft.getItemCount(items, item)
|
||||
summedItem.displayName = itemDB:getName(item)
|
||||
summedItem.total = 0
|
||||
summedItem.need = 0
|
||||
summedItem.used = 0
|
||||
summed[key] = summedItem
|
||||
end
|
||||
local total = count
|
||||
local used = math.min(summedItem.count, total)
|
||||
local need = total - used
|
||||
|
||||
if summedItem.recipe and summedItem.recipe.craftingTools and summedItem.recipe.craftingTools[key] then
|
||||
summedItem.total = 1
|
||||
if summedItem.count > 0 then
|
||||
summedItem.used = 1
|
||||
summedItem.need = 0
|
||||
need = 0
|
||||
else
|
||||
if not summedItem.recipe then
|
||||
summedItem.need = 1
|
||||
need = 1
|
||||
end
|
||||
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)
|
||||
else
|
||||
summedItem.total = summedItem.total + total
|
||||
summedItem.count = summedItem.count - used
|
||||
summedItem.used = summedItem.used + used
|
||||
if not summedItem.recipe then
|
||||
summedItem.need = summedItem.need + need
|
||||
end
|
||||
end
|
||||
|
||||
if need > 0 and summedItem.recipe then
|
||||
need = math.ceil(need / summedItem.recipe.count)
|
||||
for ikey,iqty in pairs(Craft.sumIngredients(summedItem.recipe)) do
|
||||
sumItems(ikey, math.ceil(need * iqty))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
sumItems(inRecipe, math.ceil(inCount / inRecipe.count))
|
||||
inCount = math.ceil(inCount / recipe.count)
|
||||
for ikey,iqty in pairs(Craft.sumIngredients(recipe)) do
|
||||
sumItems(ikey, math.ceil(inCount * iqty))
|
||||
end
|
||||
|
||||
return summed
|
||||
end
|
||||
|
||||
function Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter)
|
||||
local summed = { }
|
||||
local throttle = Util.throttle()
|
||||
|
||||
local function sumItems(recipe, count)
|
||||
count = math.ceil(count / recipe.count)
|
||||
local craftable = 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 = Craft.getItemCount(items, key)
|
||||
summedItem.total = 0
|
||||
summedItem.need = 0
|
||||
summedItem.used = 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
|
||||
need = 1
|
||||
end
|
||||
else
|
||||
summedItem.total = summedItem.total + total
|
||||
summedItem.count = summedItem.count - used
|
||||
summedItem.used = summedItem.used + used
|
||||
end
|
||||
|
||||
if need > 0 then
|
||||
if not summedItem.recipe then
|
||||
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))
|
||||
summedItem.craftable = summedItem.craftable + c
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
if inventoryAdapter and craftable > 0 then
|
||||
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter)
|
||||
clearGrid(inventoryAdapter)
|
||||
end
|
||||
]]
|
||||
|
||||
return craftable * recipe.count
|
||||
end
|
||||
|
||||
return sumItems(inRecipe, inCount), summed
|
||||
function Craft.getResourceList4(inRecipe, items, count)
|
||||
local summed = Craft.getResourceList(inRecipe, items, count)
|
||||
-- filter down to just raw materials
|
||||
return Util.filter(summed, function(a) return a.used > 0 or a.need > 0 end)
|
||||
end
|
||||
|
||||
-- given a certain quantity, return how many of those can be crafted
|
||||
|
||||
@@ -6,11 +6,13 @@ local turtle = _G.turtle
|
||||
local CRAFTING_TABLE = 'minecraft:crafting_table'
|
||||
|
||||
local function clearGrid(inventory)
|
||||
print('clearing')
|
||||
for i = 1, 16 do
|
||||
local count = turtle.getItemCount(i)
|
||||
if count > 0 then
|
||||
inventory:insert(i, count)
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
print('failed to insert')
|
||||
return false
|
||||
end
|
||||
end
|
||||
@@ -19,7 +21,7 @@ local function clearGrid(inventory)
|
||||
end
|
||||
|
||||
function turtle.craftItem(item, count, inventoryInfo)
|
||||
local success
|
||||
local success, msg
|
||||
|
||||
local inventory = Adapter.wrap(inventoryInfo)
|
||||
if not inventory then
|
||||
@@ -46,7 +48,8 @@ function turtle.craftItem(item, count, inventoryInfo)
|
||||
equipped = turtle.getItemDetail(slot.index)
|
||||
end
|
||||
|
||||
success = Craft.craftRecipe(item, count or 1, inventory)
|
||||
clearGrid(inventory)
|
||||
success, msg = Craft.craftRecipe(item, count or 1, inventory)
|
||||
|
||||
if equipped then
|
||||
turtle.selectOpenSlot()
|
||||
@@ -54,7 +57,7 @@ function turtle.craftItem(item, count, inventoryInfo)
|
||||
turtle.equip(side, equipped.name .. ':' .. equipped.damage)
|
||||
end
|
||||
|
||||
return success
|
||||
return success, msg
|
||||
end
|
||||
|
||||
function turtle.canCraft(item, count, items)
|
||||
|
||||
Reference in New Issue
Block a user