autocrafting improvements

This commit is contained in:
kepler155c
2018-01-03 02:41:56 -05:00
parent aa50b55ab1
commit 45bbc72fc3
13 changed files with 289 additions and 357 deletions

View File

@@ -24,15 +24,14 @@ function ChestAdapter:init(args)
Util.merge(self, args) Util.merge(self, args)
local chest local chest
if not self.autoDetect then if self.autoDetect then
chest = Peripheral.getByMethod('list')
else
chest = Peripheral.getBySide(self.wrapSide) chest = Peripheral.getBySide(self.wrapSide)
if chest and not chest.list then if chest and not chest.list then
chest = nil chest = nil
end end
end end
if not chest then
chest = Peripheral.getByMethod('list')
end
if chest then if chest then
Util.merge(self, chest) Util.merge(self, chest)
@@ -55,17 +54,11 @@ function ChestAdapter:getCachedItemDetails(item, k)
if not detail then if not detail then
local s, m = pcall(function() detail = self.getItemMeta(k) end) local s, m = pcall(function() detail = self.getItemMeta(k) end)
if not detail then if not detail then
debug(item)
debug(m)
debug('no details')
-- error('Inventory has changed') -- error('Inventory has changed')
return return
end end
-- NOT SUFFICIENT -- NOT SUFFICIENT
if detail.name ~= item.name then if detail.name ~= item.name then
debug('name change ?')
debug(item)
debug(detail)
-- error('Inventory has changed') -- error('Inventory has changed')
return return
end end
@@ -76,7 +69,6 @@ debug(detail)
end end
end end
debug('adding')
itemDB:add(detail) itemDB:add(detail)
end end
if detail then if detail then
@@ -92,7 +84,6 @@ end
function ChestAdapter:listItems(throttle) function ChestAdapter:listItems(throttle)
local cache = { } local cache = { }
local items = { } local items = { }
debug('listing')
throttle = throttle or Util.throttle() throttle = throttle or Util.throttle()
for k,v in pairs(self.list()) do for k,v in pairs(self.list()) do
@@ -103,8 +94,6 @@ debug('listing')
if not entry then if not entry then
entry = self:getCachedItemDetails(v, k) entry = self:getCachedItemDetails(v, k)
if not entry then if not entry then
debug(key)
debug('inv changed')
return -- Inventory has changed return -- Inventory has changed
end end
entry.count = 0 entry.count = 0
@@ -118,15 +107,12 @@ debug('listing')
throttle() throttle()
end end
end end
--read()
itemDB:flush() itemDB:flush()
debug('done listing')
if not Util.empty(items) then if not Util.empty(items) then
self.cache = cache self.cache = cache
return items return items
end end
debug('its empty')
end end
function ChestAdapter:getItemInfo(item) function ChestAdapter:getItemInfo(item)
@@ -177,11 +163,14 @@ end
function ChestAdapter:eject(item, qty, direction) function ChestAdapter:eject(item, qty, direction)
local s, m = pcall(function() local s, m = pcall(function()
local stacks = self.list() local stacks = self.list()
local maxStack = itemDB:getMaxCount(item)
for key,stack in Util.rpairs(stacks) do for key,stack in Util.rpairs(stacks) do
if stack.name == item.name and if stack.name == item.name and
(not item.damage or stack.damage == item.damage) and (not item.damage or stack.damage == item.damage) and
(not item.nbtHash or stack.nbtHash == item.nbtHash) then (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 if amount > 0 then
self.drop(key, amount, direction) self.drop(key, amount, direction)
end end

View 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

View File

@@ -2,8 +2,8 @@ local Adapter = { }
function Adapter.wrap(args) function Adapter.wrap(args)
local adapters = { local adapters = {
'refinedAdapter', --'refinedAdapter',
'meAdapter', --'meAdapter',
'chestAdapter18', 'chestAdapter18',
'chestAdapter', 'chestAdapter',
} }

View File

@@ -141,10 +141,6 @@ function itemDB:getName(item)
end end
function itemDB:getMaxCount(item) function itemDB:getMaxCount(item)
if type(item) == 'string' then
item = self:splitKey(item)
end
local detail = self:get(item) local detail = self:get(item)
if detail then if detail then
return detail.maxCount return detail.maxCount

View File

@@ -18,12 +18,22 @@ function RefinedAdapter:init(args)
local defaults = { local defaults = {
items = { }, items = { },
name = 'refinedStorage', name = 'refinedStorage',
direction = 'up',
wrapSide = 'bottom',
} }
Util.merge(self, defaults) Util.merge(self, defaults)
Util.merge(self, args) Util.merge(self, args)
local controller = Peripheral.getByType('refinedstorage:controller') or local controller
Peripheral.getByMethod('listAvailableItems') 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 if controller then
Util.merge(self, controller) Util.merge(self, controller)
end end

View File

@@ -1,3 +1,4 @@
local itemDB = require('itemDB')
local Util = require('util') local Util = require('util')
local fs = _G.fs local fs = _G.fs
@@ -32,8 +33,11 @@ local function splitKey(key)
return item return item
end end
function Craft.getItemCount(items, key) function Craft.getItemCount(items, item)
local item = splitKey(key) if type(item) == 'string' then
item = splitKey(item)
end
local count = 0 local count = 0
for _,v in pairs(items) do for _,v in pairs(items) do
if v.name == item.name and if v.name == item.name and
@@ -124,6 +128,7 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
local iqty = icount * count - itemCount local iqty = icount * count - itemCount
local crafted = Craft.craftRecipe(irecipe, iqty, inventoryAdapter) local crafted = Craft.craftRecipe(irecipe, iqty, inventoryAdapter)
if crafted ~= iqty then if crafted ~= iqty then
turtle.select(1) turtle.select(1)
return 0 return 0
end end
@@ -145,102 +150,80 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
return crafted * recipe.count return crafted * recipe.count
end end
-- determine the full list of ingredients needed to craft local function makeRecipeKey(item)
-- a quantity of a recipe. if type(item) == 'string' then
-- negative quantities denote missing ingredients item = splitKey(item)
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 = Craft.getItemCount(items, key)
summed[key] = summedItem
end end
summedItem.count = summedItem.count - (count * iqty) return table.concat({ item.name, item.damage or 0, item.nbtHash }, ':')
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 end
function Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter) function Craft.findRecipe(item)
return Craft.recipes[makeRecipeKey(item)]
end
-- determine the full list of ingredients needed to craft
-- a quantity of a recipe.
function Craft.getResourceList(recipe, items, inCount)
local summed = { } local summed = { }
local throttle = Util.throttle()
local function sumItems(recipe, count) local function sumItems(key, count)
count = math.ceil(count / recipe.count) local item = itemDB:splitKey(key)
local craftable = count
for key,iqty in pairs(Craft.sumIngredients(recipe)) do
throttle()
local item = splitKey(key)
local summedItem = summed[key] local summedItem = summed[key]
if not summedItem then if not summedItem then
summedItem = Util.shallowCopy(item) summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.recipes[key] summedItem.recipe = Craft.findRecipe(key)
summedItem.count = Craft.getItemCount(items, key) summedItem.count = Craft.getItemCount(items, item)
summedItem.displayName = itemDB:getName(item)
summedItem.total = 0 summedItem.total = 0
summedItem.need = 0 summedItem.need = 0
summedItem.used = 0 summedItem.used = 0
summedItem.craftable = 0
summed[key] = summedItem summed[key] = summedItem
end end
debug(key) local total = count
local total = count * iqty -- 4 * 2 local used = math.min(summedItem.count, total)
local used = math.min(summedItem.count, total) -- 5 local need = total - used
local need = total - used -- 3
if recipe.craftingTools and recipe.craftingTools[key] then if summedItem.recipe and summedItem.recipe.craftingTools and summedItem.recipe.craftingTools[key] then
summedItem.total = 1 summedItem.total = 1
if summedItem.count > 0 then if summedItem.count > 0 then
summedItem.used = 1 summedItem.used = 1
summedItem.need = 0
need = 0 need = 0
else else
if not summedItem.recipe then
summedItem.need = 1
need = 1 need = 1
end end
end
else else
summedItem.total = summedItem.total + total summedItem.total = summedItem.total + total
summedItem.count = summedItem.count - used summedItem.count = summedItem.count - used
summedItem.used = summedItem.used + used summedItem.used = summedItem.used + used
end
if need > 0 then
if not summedItem.recipe then if not summedItem.recipe then
craftable = math.min(craftable, math.floor(used / iqty))
summedItem.need = summedItem.need + need summedItem.need = summedItem.need + need
else end
local c = sumItems(summedItem.recipe, need) -- 4 end
craftable = math.min(craftable, math.floor((used + c) / iqty))
summedItem.craftable = summedItem.craftable + c 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 end
end end
--[[ inCount = math.ceil(inCount / recipe.count)
if inventoryAdapter and craftable > 0 then for ikey,iqty in pairs(Craft.sumIngredients(recipe)) do
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter) sumItems(ikey, math.ceil(inCount * iqty))
clearGrid(inventoryAdapter)
end
]]
return craftable * recipe.count
end end
return sumItems(inRecipe, inCount), summed return summed
end
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 end
-- given a certain quantity, return how many of those can be crafted -- given a certain quantity, return how many of those can be crafted

View File

@@ -6,11 +6,13 @@ local turtle = _G.turtle
local CRAFTING_TABLE = 'minecraft:crafting_table' local CRAFTING_TABLE = 'minecraft:crafting_table'
local function clearGrid(inventory) local function clearGrid(inventory)
print('clearing')
for i = 1, 16 do for i = 1, 16 do
local count = turtle.getItemCount(i) local count = turtle.getItemCount(i)
if count > 0 then if count > 0 then
inventory:insert(i, count) inventory:insert(i, count)
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
print('failed to insert')
return false return false
end end
end end
@@ -19,7 +21,7 @@ local function clearGrid(inventory)
end end
function turtle.craftItem(item, count, inventoryInfo) function turtle.craftItem(item, count, inventoryInfo)
local success local success, msg
local inventory = Adapter.wrap(inventoryInfo) local inventory = Adapter.wrap(inventoryInfo)
if not inventory then if not inventory then
@@ -46,7 +48,8 @@ function turtle.craftItem(item, count, inventoryInfo)
equipped = turtle.getItemDetail(slot.index) equipped = turtle.getItemDetail(slot.index)
end end
success = Craft.craftRecipe(item, count or 1, inventory) clearGrid(inventory)
success, msg = Craft.craftRecipe(item, count or 1, inventory)
if equipped then if equipped then
turtle.selectOpenSlot() turtle.selectOpenSlot()
@@ -54,7 +57,7 @@ function turtle.craftItem(item, count, inventoryInfo)
turtle.equip(side, equipped.name .. ':' .. equipped.damage) turtle.equip(side, equipped.name .. ':' .. equipped.damage)
end end
return success return success, msg
end end
function turtle.canCraft(item, count, items) function turtle.canCraft(item, count, items)

View File

@@ -1,18 +1,18 @@
_G.requireInjector() _G.requireInjector()
local Ansi = require('ansi') local Ansi = require('ansi')
local ChestAdapter = require('chestAdapter18')
local Config = require('config') local Config = require('config')
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local Event = require('event') local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local MEAdapater = require('meAdapter')
local Peripheral = require('peripheral') local Peripheral = require('peripheral')
local RefinedAdapter = require('refinedAdapter')
local Terminal = require('terminal') local Terminal = require('terminal')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
local ControllerAdapter = require('controllerAdapter')
local InventoryAdapter = require('inventoryAdapter')
local device = _G.device local device = _G.device
local multishell = _ENV.multishell local multishell = _ENV.multishell
local peripheral = _G.peripheral local peripheral = _G.peripheral
@@ -22,35 +22,23 @@ local turtle = _G.turtle
multishell.setTitle(multishell.getCurrent(), 'Resource Manager') multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
-- 3 wide monitor (any side of turtle)
-- Config location is /sys/config/chestManager -- Config location is /sys/config/chestManager
-- adjust directions in that file if needed -- adjust directions in that file if needed
local config = { local config = {
trashDirection = 'up', -- trash /chest in relation to chest trashDirection = 'up', -- trash /chest in relation to chest
inventoryDirection = { direction = 'north', wrapSide = 'back' }, inventoryDirection = { direction = 'north', wrapSide = 'back' },
chestDirection = { direction = 'down', wrapSide = 'top' }, chestDirection = { direction = 'down', wrapSide = 'top' },
controllerDirection = { direction = 'south', wrapSide = 'right' },
} }
Config.load('chestManager', config) Config.load('chestManager', config)
local inventoryAdapter = ChestAdapter(config.inventoryDirection) local inventoryAdapter = InventoryAdapter.wrap(config.inventoryDirection)
local turtleChestAdapter = ChestAdapter(config.chestDirection) local turtleChestAdapter = InventoryAdapter.wrap(config.chestDirection)
local controllerAdapter = ControllerAdapter.wrap(config.controllerDirection)
local duckAntenna local duckAntenna
local controller = RefinedAdapter()
if not controller:isValid() then
controller = MEAdapater(config.inventoryDirection)
if not controller:isValid() then
controller = nil
else
inventoryAdapter = controller -- ME functions as inventory and crafting
end
end
if device.workbench then if device.workbench then
local oppositeSide = { local oppositeSide = {
[ 'left' ] = 'right', [ 'left' ] = 'right',
[ 'right' ] = 'left', [ 'right' ] = 'left',
@@ -83,21 +71,6 @@ local function getItem(items, inItem, ignoreDamage, ignoreNbtHash)
end end
end end
local function getItemQuantity(items, item)
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
if item.damage then
return v.count
end
count = count + v.count
end
end
return count
end
local function uniqueKey(item) local function uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':') return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end end
@@ -145,6 +118,7 @@ local function listItems()
if not items then if not items then
-- error('could not check inventory') -- error('could not check inventory')
term.clear() term.clear()
print('Communication failure')
print('rebooting in 5 secs') print('rebooting in 5 secs')
os.sleep(5) os.sleep(5)
os.reboot() os.reboot()
@@ -190,7 +164,6 @@ local function clearGrid()
if count > 0 then if count > 0 then
inventoryAdapter:insert(i, count) inventoryAdapter:insert(i, count)
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
debug('insert failed')
return false return false
end end
end end
@@ -229,28 +202,6 @@ local function craftItem(recipe, items, originalItem, craftList, count)
if missing.name then if missing.name then
originalItem.status = string.format('%s missing', itemDB:getName(missing.name)) originalItem.status = string.format('%s missing', itemDB:getName(missing.name))
originalItem.statusCode = 'missing' originalItem.statusCode = 'missing'
-- debug(missing.name)
end
if originalItem.forceCrafting and toCraft == 0 then
for key,qty in pairs(Craft.sumIngredients(recipe)) do
local iRecipe = Craft.recipes[key]
if iRecipe then
local need = count * qty
local has = getItemQuantity(items, itemDB:splitKey(key))
--debug({ key, need, has })
if has < need then
--debug('crafting ' .. key .. ' - ' .. need - has)
--read()
craftItem(iRecipe, items, originalItem, { }, math.ceil((need - has) / iRecipe.count))
items = listItems()
if not items then
error('list failed')
--return 0
end
end
end
end
end end
local crafted = 0 local crafted = 0
@@ -263,11 +214,11 @@ local function craftItem(recipe, items, originalItem, craftList, count)
end end
if count > 0 and items then if count > 0 and items then
local ingredients = Craft.getResourceList(recipe, items, count) local ingredients = Craft.getResourceList4(recipe, items, count)
for _,ingredient in pairs(ingredients) do for _,ingredient in pairs(ingredients) do
--if not ingredient.recipe and ingredient.count < 0 then --if not ingredient.recipe and ingredient.count < 0 then
if ingredient.count < 0 then if ingredient.need > 0 then
addCraftingRequest(ingredient, craftList, -ingredient.count) addCraftingRequest(ingredient, craftList, ingredient.need)
end end
end end
end end
@@ -288,7 +239,7 @@ local function forceCraftItem(inRecipe, items, originalItem, craftList, inCount)
local summedItem = summed[key] local summedItem = summed[key]
if not summedItem then if not summedItem then
summedItem = Util.shallowCopy(item) summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.recipes[key] summedItem.recipe = Craft.findRecipe(item)
summedItem.count = Craft.getItemCount(items, key) summedItem.count = Craft.getItemCount(items, key)
summedItem.need = 0 summedItem.need = 0
summedItem.used = 0 summedItem.used = 0
@@ -321,17 +272,18 @@ local function forceCraftItem(inRecipe, items, originalItem, craftList, inCount)
local c = sumItems(summedItem.recipe, need) -- 4 local c = sumItems(summedItem.recipe, need) -- 4
craftable = math.min(craftable, math.floor((used + c) / iqty)) craftable = math.min(craftable, math.floor((used + c) / iqty))
summedItem.craftable = summedItem.craftable + c summedItem.craftable = summedItem.craftable + c
end
end
end
end
end
end
if craftable > 0 then if craftable > 0 then
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter) / recipe.count craftable = Craft.craftRecipe(recipe, craftable * recipe.count, inventoryAdapter) / recipe.count
clearGrid() clearGrid()
end end
return craftable * recipe.count return craftable * recipe.count
end end
local count = sumItems(inRecipe, inCount) local count = sumItems(inRecipe, inCount)
-- local count, summed = Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter) -- local count, summed = Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter)
@@ -348,7 +300,6 @@ local function forceCraftItem(inRecipe, items, originalItem, craftList, inCount)
end end
local function craftItems(craftList, allItems) local function craftItems(craftList, allItems)
for _,key in pairs(Util.keys(craftList)) do for _,key in pairs(Util.keys(craftList)) do
local item = craftList[key] local item = craftList[key]
local recipe = Craft.recipes[key] local recipe = Craft.recipes[key]
@@ -371,17 +322,17 @@ local function craftItems(craftList, allItems)
for key,item in pairs(craftList) do for key,item in pairs(craftList) do
if not Craft.recipes[key] and not item.rsControl then if not Craft.recipes[key] and not item.rsControl then
if not controller then if not controllerAdapter then
item.status = '(no recipe)' item.status = '(no recipe)'
else else
if controller:isCrafting(item) then if controllerAdapter:isCrafting(item) then
item.status = '(crafting)' item.status = '(crafting)'
else else
local count = item.count local count = item.count
while count >= 1 do -- try to request smaller quantities until successful while count >= 1 do -- try to request smaller quantities until successful
local s = pcall(function() local s = pcall(function()
item.status = '(no recipe)' item.status = '(no recipe)'
if not controller:craft(item, count) then if not controllerAdapter:craft(item, count) then
item.status = '(missing ingredients)' item.status = '(missing ingredients)'
error('failed') error('failed')
end end
@@ -442,6 +393,8 @@ local function jobMonitor()
return colors.red return colors.red
elseif row.statusCode == 'missing' then elseif row.statusCode == 'missing' then
return colors.yellow return colors.yellow
elseif row.statusCode == 'success' then
return colors.lime
end end
return UI.Grid:getRowTextColor(row, selected) return UI.Grid:getRowTextColor(row, selected)
end end
@@ -514,11 +467,6 @@ local function watchResources(items)
item.damage = 0 item.damage = 0
end end
local key = uniqueKey(res) local key = uniqueKey(res)
if res.name == 'harvestcraft:applesauceitem' then
_G._p = items
debug('applesause')
error('applesauce')
end
craftList[key] = { craftList[key] = {
damage = item.damage, damage = item.damage,
@@ -801,7 +749,6 @@ function itemPage:eventHandler(event)
else else
filtered.ignoreNbtHash = nil filtered.ignoreNbtHash = nil
end end
debug(filtered)
resources[originalKey] = nil resources[originalKey] = nil
resources[uniqueKey(filtered)] = filtered resources[uniqueKey(filtered)] = filtered
saveResources() saveResources()
@@ -834,15 +781,13 @@ local listingPage = UI.Page {
sortColumn = 'displayName', sortColumn = 'displayName',
}, },
statusBar = UI.StatusBar { statusBar = UI.StatusBar {
filterText = UI.Text {
x = 2,
value = 'Filter',
},
filter = UI.TextEntry { filter = UI.TextEntry {
x = 9, ex = -5, x = 1, ex = -4,
limit = 50, limit = 50,
backgroundColor = colors.gray, shadowText = 'filter',
backgroundFocusColor = colors.gray, shadowTextColor = colors.gray,
backgroundColor = colors.cyan,
backgroundFocusColor = colors.cyan,
}, },
display = UI.Button { display = UI.Button {
x = -3, x = -3,
@@ -858,6 +803,10 @@ local listingPage = UI.Page {
displayMode = 0, displayMode = 0,
} }
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage.grid:getRowTextColor(row, selected) function listingPage.grid:getRowTextColor(row, selected)
if row.is_craftable then if row.is_craftable then
return colors.yellow return colors.yellow
@@ -880,10 +829,6 @@ function listingPage.grid:getDisplayValues(row)
return row return row
end end
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage:eventHandler(event) function listingPage:eventHandler(event)
if event.type == 'quit' then if event.type == 'quit' then
UI:exitPullEvents() UI:exitPullEvents()
@@ -1062,7 +1007,7 @@ local function learnRecipe(page)
for k,ingredient in pairs(ingredients) do for k,ingredient in pairs(ingredients) do
if ingredient.maxDamage > 0 then if ingredient.maxDamage > 0 then
ingredient.damage = '*' ingredient.damage = '*' -- I don't think this is right
end end
ingredients[k] = uniqueKey(ingredient) ingredients[k] = uniqueKey(ingredient)
end end
@@ -1137,9 +1082,7 @@ function learnPage:eventHandler(event)
end end
local craftPage = UI.Page { local craftPage = UI.Page {
titleBar = UI.TitleBar { titleBar = UI.TitleBar { },
title = "Information",
},
wizard = UI.Wizard { wizard = UI.Wizard {
y = 2, ey = -2, y = 2, ey = -2,
pages = { pages = {
@@ -1170,14 +1113,13 @@ local craftPage = UI.Page {
}, },
resources = UI.Window { resources = UI.Window {
index = 2, index = 2,
grid = UI.Grid { grid = UI.ScrollingGrid {
y = 2, ey = -2, y = 2, ey = -2,
columns = { columns = {
{ heading = 'Name', key = 'displayName' }, { heading = 'Name', key = 'displayName' },
{ heading = 'Total', key = 'total' , width = 4 }, { heading = 'Total', key = 'total' , width = 5 },
{ heading = 'Used', key = 'used' , width = 4 }, { heading = 'Used', key = 'used' , width = 5 },
{ heading = 'Craf', key = 'craftable' , width = 4 }, { heading = 'Need', key = 'need' , width = 5 },
{ heading = 'Need', key = 'need' , width = 4 },
}, },
sortColumn = 'displayName', sortColumn = 'displayName',
}, },
@@ -1189,6 +1131,7 @@ local craftPage = UI.Page {
function craftPage:enable(item) function craftPage:enable(item)
self.item = item self.item = item
self:focusFirst() self:focusFirst()
self.titleBar.title = itemDB:getName(item)
-- self.wizard.pages.quantity.eject.value = true -- self.wizard.pages.quantity.eject.value = true
UI.Page.enable(self) UI.Page.enable(self)
end end
@@ -1203,27 +1146,39 @@ function craftPage.wizard.pages.resources.grid:getDisplayValues(row)
row = Util.shallowCopy(row) row = Util.shallowCopy(row)
row.total = Util.toBytes(row.total) row.total = Util.toBytes(row.total)
row.used = dv(row.used) row.used = dv(row.used)
row.craftable = dv(row.craftable)
row.need = dv(row.need) row.need = dv(row.need)
return row return row
end end
function craftPage.wizard.pages.resources.grid:getRowTextColor(row, selected) function craftPage.wizard.pages.resources.grid:getRowTextColor(row, selected)
if row.need - row.craftable > 0 then if row.need > 0 then
return colors.orange return colors.orange
end end
return UI.Grid:getRowTextColor(row, selected) return UI.Grid:getRowTextColor(row, selected)
end end
function craftPage.wizard:eventHandler(event)
if event.type == 'nextView' then
local count = tonumber(self.pages.quantity.count.value)
if not count or count <= 0 then
self.pages.quantity.count.backgroundColor = colors.red
self.pages.quantity.count:draw()
return false
end
self.pages.quantity.count.backgroundColor = colors.black
end
return UI.Wizard.eventHandler(self, event)
end
function craftPage:eventHandler(event) function craftPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'enable_view' and event.view == self.wizard.pages.resources then elseif event.type == 'enable_view' and event.view == self.wizard.pages.resources then
local items = listItems() local items = listItems()
local count = self.wizard.pages.quantity.count.value local count = tonumber(self.wizard.pages.quantity.count.value)
local recipe = Craft.recipes[uniqueKey(self.item)] local recipe = Craft.findRecipe(self.item)
local _, ingredients = Craft.getResourceList3(recipe, items, count) local ingredients = Craft.getResourceList4(recipe, items, count)
for _,v in pairs(ingredients) do for _,v in pairs(ingredients) do
v.displayName = itemDB:getName(v) v.displayName = itemDB:getName(v)
end end
@@ -1290,9 +1245,10 @@ Event.onInterval(5, function()
for _,key in pairs(Util.keys(demandCrafting)) do for _,key in pairs(Util.keys(demandCrafting)) do
local item = demandCrafting[key] local item = demandCrafting[key]
if item.crafted then if item.crafted then
item.count = item.count - item.crafted item.count = math.max(0, item.count - item.crafted)
if item.count <= 0 then if item.count <= 0 then
demandCrafting[key] = nil demandCrafting[key] = nil
item.statusCode = 'success'
if item.eject then if item.eject then
inventoryAdapter:eject(item, item.ocount, inventoryAdapter.getMetadata().state.facing) inventoryAdapter:eject(item, item.ocount, inventoryAdapter.getMetadata().state.facing)
end end

View File

@@ -183,64 +183,77 @@ local function getItems()
return items return items
end end
local function craftItem(recipe, recipeKey, items, cItem, count) local function craftItem(ikey, item, items)
dock() dock()
local resource = resources[recipeKey] local resource = resources[ikey]
if not resource or not resource.machine then if not resource or not resource.machine then
cItem.status = 'machine not selected' item.status = 'machine not selected'
return return
end end
local machine = Util.find(machines, 'order', resource.machine) local machine = Util.find(machines, 'order', resource.machine)
if not machine then if not machine then
cItem.status = 'invalid machine' item.status = 'invalid machine'
return
end
if count == 0 then
for key in pairs(recipe.ingredients) do
local item = getItemWithQty(items, itemDB:splitKey(key), recipe.ignoreNbtHash)
if item.count == 0 then
cItem.status = 'Missing: ' .. (item.displayName or itemDB:getName(item))
return false
end
end
return return
end end
local slot = 1 local slot = 1
for key,qty in pairs(recipe.ingredients) do local maxCount = math.ceil(item.need / item.recipe.count)
local item = getItemWithQty(items, itemDB:splitKey(key), recipe.ignoreNbtHash) for key,qty in pairs(item.recipe.ingredients) do
if item.count == 0 then local ingredient = itemDB:get(key)
debug(item) local c = math.min(maxCount * qty, getItemQuantity(items, ingredient))
cItem.status = 'Missing: ' .. (item.displayName or itemDB:getName(item)) c = math.min(c, ingredient.maxCount)
c = math.floor(c / qty)
if c < maxCount then
maxCount = c
end
if maxCount == 0 then
item.status = 'Missing ' .. ingredient.displayName
item.statusCode = 'missing'
return return
end end
local c = count * qty end
while c > 0 do
local maxCount = math.min(c, item.maxCount) for key,qty in pairs(item.recipe.ingredients) do
inventoryAdapter:provide(item, maxCount, slot) local ingredient = itemDB:get(key)
if turtle.getItemCount(slot) ~= maxCount then -- ~= maxCount then FIXXX !!! -- local c = item.craftable * qty
cItem.status = 'Extract failed: ' .. (item.displayName or itemDB:getName(item)) -- while c > 0 do
inventoryAdapter:provide(ingredient, maxCount * qty, slot)
if turtle.getItemCount(slot) ~= maxCount * qty then -- ~= maxCount then FIXXX !!!
item.status = 'Extract failed: ' .. (ingredient.displayName or itemDB:getName(ingredient))
debug({ key, maxCount })
return return
end end
c = c - maxCount -- c = c - maxCount
slot = slot + 1 slot = slot + 1
--end
end end
end
if not gotoMachine(machine) then if not gotoMachine(machine) then
cItem.status = 'failed to find machine' item.status = 'failed to find machine'
else else
if machine.empty then if machine.empty then
local s, l = pcall(Peripheral.call, local s, l = pcall(Peripheral.call,
turtle.getAction(machine.dir).side, 'list') turtle.getAction(machine.dir).side, 'list')
if not s and not l then
l = 'Unable to check empty status'
elseif not s then
s, l = pcall(Peripheral.call,
turtle.getAction(machine.dir).side, 'getProgress')
if s and l == 0 then
l = { }
elseif s then
l = { true }
end
end
debug { s, l }
if not s then if not s then
cItem.status = l item.status = l
return return
elseif not Util.empty(l) then elseif not Util.empty(l) then
cItem.status = 'machine busy' item.status = 'machine busy'
return return
end end
end end
@@ -251,78 +264,52 @@ debug(item)
turtle.emptyInventory(turtle.dropDown) turtle.emptyInventory(turtle.dropDown)
end end
if #turtle.getFilledSlots() ~= 0 then if #turtle.getFilledSlots() ~= 0 then
cItem.status = 'machine busy' item.status = 'machine busy'
else else
cItem.status = 'crafting' item.status = 'crafting'
end end
end end
end end
local function expandList(list, items) local function expandList(list, items)
local summed = { }
local function getCraftable(recipe, count) local function sumItems(key, count)
local maxSlots = math.floor(16 / Util.size(recipe.ingredients)) local item = itemDB:splitKey(key)
local summedItem = summed[key]
for key,qty in pairs(recipe.ingredients) do if not summedItem then
summedItem = Util.shallowCopy(item)
local item = getItemWithQty(items, itemDB:splitKey(key), recipe.ignoreNbtHash) summedItem.recipe = recipes[key]
summedItem.count = getItemQuantity(items, item)
local need = qty * count summedItem.displayName = itemDB:getName(item)
local irecipe = recipes[key] summedItem.total = 0
summedItem.need = 0
if item.count < need and irecipe then summedItem.used = 0
need = math.ceil((need - item.count) / irecipe.count) summedItem.craftable = 0
if not list[key] then summed[key] = summedItem
list[key] = Util.shallowCopy(item)
list[key].ocount = need
list[key].count = 0
else
if not list[key].ocount then
list[key].ocount = 0
end
list[key].ocount = list[key].ocount + need
end end
local total = count
local used = math.min(summedItem.count, total)
local need = total - used
local icount = getCraftable(irecipe, need) summedItem.total = summedItem.total + total
list[key].count = list[key].count + icount summedItem.count = summedItem.count - used
end summedItem.used = summedItem.used + used
local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots) summedItem.need = summedItem.need + need
count = math.min(x, count)
item.count = math.max(0, item.count - (count * qty))
end
return count if need > 0 and summedItem.recipe then
end need = math.ceil(need / summedItem.recipe.count)
for ikey,iqty in pairs(summedItem.recipe.ingredients) do
for key, item in pairs(Util.shallowCopy(list)) do sumItems(ikey, math.ceil(need * iqty))
local recipe = recipes[key] end
item.count = math.ceil(item.count / recipe.count)
item.ocount = item.count
if recipe then
item.count = getCraftable(recipe, item.count)
end end
end end
end
local function craftItems(craftList) for key, item in pairs(list) do
local items = getItems() sumItems(key, item.count)
expandList(craftList, items)
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
for key, item in pairs(craftList) do
local recipe = recipes[key]
if recipe then
craftItem(recipe, key, items, item, item.count)
dock()
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
clearGrid()
items = getItems()
end
end end
return Util.filter(summed, function(a) return a.need > 0 end)
end end
local function watchResources(items) local function watchResources(items)
@@ -339,7 +326,7 @@ local function watchResources(items)
item.count = getItemQuantity(items, item) item.count = getItemQuantity(items, item)
if item.count < res.low then if item.count < res.low then
item.displayName = itemDB:getName(res) item.displayName = itemDB:getName(res)
item.count = res.low - item.count item.count = res.low -- - item.count
craftList[uniqueKey(res)] = item craftList[uniqueKey(res)] = item
end end
end end
@@ -348,22 +335,31 @@ local function watchResources(items)
return craftList return craftList
end end
local function craftItems()
local items = getItems()
local craftList = watchResources(items)
local list = expandList(craftList, items)
jobListGrid:setValues(list)
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
for key, item in pairs(list) do
if item.need > 0 and item.recipe then
craftItem(key, item, items)
dock()
items = getItems() -- should decrement count instead ...
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
clearGrid()
end
end
end
local function loadResources() local function loadResources()
resources = Util.readTable(RESOURCE_FILE) or { } resources = Util.readTable(RESOURCE_FILE) or { }
for k,v in pairs(resources) do for k,v in pairs(resources) do
Util.merge(v, itemDB:splitKey(k)) Util.merge(v, itemDB:splitKey(k))
if v.dir then
for _,m in pairs(machines) do
if m.index == v.machine and m.dir == v.dir then
v.machine = m.order
v.dir = nil
break
end
end
if v.dir then
error('did not find')
end
end
end end
end end
@@ -460,8 +456,7 @@ local function jobMonitor()
parent = mon, parent = mon,
sortColumn = 'displayName', sortColumn = 'displayName',
columns = { columns = {
{ heading = 'Qty', key = 'ocount', width = 6 }, { heading = 'Qty', key = 'need', width = 6 },
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Crafting', key = 'displayName', width = (mon.width - 18) / 2 }, { heading = 'Crafting', key = 'displayName', width = (mon.width - 18) / 2 },
{ heading = 'Status', key = 'status', }, { heading = 'Status', key = 'status', },
}, },
@@ -760,7 +755,7 @@ local machinesPage = UI.Page {
previousPage = true, previousPage = true,
title = 'Machines', title = 'Machines',
}, },
grid = UI.Grid { grid = UI.ScrollingGrid {
y = 2, ey = -2, y = 2, ey = -2,
values = machines, values = machines,
columns = { columns = {
@@ -873,14 +868,12 @@ local listingPage = UI.Page {
}, },
sortColumn = 'displayName', sortColumn = 'displayName',
}, },
statusBar = UI.StatusBar { statusBar = UI.Window {
filterText = UI.Text { y = -1,
x = 2,
value = 'Filter',
},
filter = UI.TextEntry { filter = UI.TextEntry {
x = 9, ex = -2,
limit = 50, limit = 50,
shadowText = 'filter',
shadowTextColor = colors.lightGray,
backgroundColor = colors.gray, backgroundColor = colors.gray,
backgroundFocusColor = colors.gray, backgroundFocusColor = colors.gray,
}, },
@@ -910,10 +903,6 @@ function listingPage.grid:getDisplayValues(row)
return row return row
end end
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage.statusBar.filter:eventHandler(event) function listingPage.statusBar.filter:eventHandler(event)
if event.type == 'mouse_rightclick' then if event.type == 'mouse_rightclick' then
self.value = '' self.value = ''
@@ -1025,18 +1014,8 @@ Event.onInterval(30, function()
inventoryAdapter:provide({ name = 'minecraft:coal', damage = 1 }, 16, 1) inventoryAdapter:provide({ name = 'minecraft:coal', damage = 1 }, 16, 1)
turtle.refuel() turtle.refuel()
end end
local items = getItems() craftItems()
if items then
local craftList = watchResources(items)
jobListGrid:setValues(craftList)
jobListGrid:update()
jobListGrid:draw()
jobListGrid:sync()
craftItems(craftList)
end
end) end)
UI:pullEvents() UI:pullEvents()
jobListGrid.parent:reset() --jobListGrid.parent:reset()

View File

@@ -144,7 +144,7 @@ local itemPage = UI.Page {
} }
function itemPage:enable(item) function itemPage:enable(item)
self.item = item self.item = Util.shallowCopy(item)
self.form:setValues(item) self.form:setValues(item)
self.titleBar.title = item.displayName or item.name self.titleBar.title = item.displayName or item.name
@@ -179,7 +179,7 @@ function itemPage:eventHandler(event)
local originalKey = uniqueKey(self.item) local originalKey = uniqueKey(self.item)
resources[originalKey] = nil resources[originalKey] = nil
filtered.low = tonumber(filtered.limit) filtered.low = tonumber(filtered.low)
filtered.limit = tonumber(filtered.limit) filtered.limit = tonumber(filtered.limit)
if filtered.limit or filtered.low then if filtered.limit or filtered.low then
resources[uniqueKey(filtered)] = filtered resources[uniqueKey(filtered)] = filtered

View File

@@ -118,17 +118,21 @@ local function safePlaceBlock(item)
end end
local function craftItem(item, qty) local function craftItem(item, qty)
local success local success, msg
if safePlaceBlock(CHEST) then if safePlaceBlock(CHEST) then
Util.print('Crafting %d %s', (qty or 1), item) Util.print('Crafting %d %s', (qty or 1), item)
success = turtle.craftItem(item, qty or 1, { success, msg = turtle.craftItem(item, qty or 1, {
wrapSide = 'top', wrapSide = 'top',
direction = 'down', direction = 'down',
}) })
repeat until not turtle.suckUp() repeat until not turtle.suckUp()
if not success then
print(msg)
end
turtle.digUp() turtle.digUp()
end end

View File

@@ -56,13 +56,6 @@
}, },
count = 1, count = 1,
}, },
[ "appliedenergistics2:crystal_seed:0:5af0f90bcc279578cf09089f43278d57" ] = {
ingredients = {
"minecraft:sand:0",
"appliedenergistics2:material:2",
},
count = 2,
},
[ "appliedenergistics2:material:36" ] = { [ "appliedenergistics2:material:36" ] = {
ingredients = { ingredients = {
"minecraft:redstone:0", "minecraft:redstone:0",

View File

@@ -1,5 +1,6 @@
{ {
[ "exnihiloadscensio:hammerStone:0" ] = { [ "exnihiloadscensio:hammerStone:0" ] = {
maxCount = 1,
ingredients = { ingredients = {
[ 7 ] = "minecraft:cobblestone:0", [ 7 ] = "minecraft:cobblestone:0",
[ 9 ] = "minecraft:stick:0", [ 9 ] = "minecraft:stick:0",