storage manager improvements
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -17,6 +17,8 @@ local device = _G.device
|
||||
local multishell = _ENV.multishell
|
||||
local peripheral = _G.peripheral
|
||||
local term = _G.term
|
||||
local colors = _G.colors
|
||||
local turtle = _G.turtle
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Resource Manager')
|
||||
|
||||
@@ -64,13 +66,10 @@ end
|
||||
local RESOURCE_FILE = 'usr/config/resources.db'
|
||||
local RECIPES_FILE = 'usr/config/recipes.db'
|
||||
|
||||
local colors = _G.colors
|
||||
local turtle = _G.turtle
|
||||
|
||||
local craftingPaused = false
|
||||
local canCraft = not not duckAntenna or turtleChestAdapter:isValid()
|
||||
local userRecipes = Util.readTable(RECIPES_FILE) or { }
|
||||
local jobListGrid
|
||||
local jobList
|
||||
local resources
|
||||
local demandCrafting = { }
|
||||
|
||||
@@ -134,6 +133,26 @@ local function mergeResources(t)
|
||||
end
|
||||
end
|
||||
|
||||
local function listItems()
|
||||
local items
|
||||
for _ = 1, 5 do
|
||||
items = inventoryAdapter:listItems()
|
||||
if items then
|
||||
break
|
||||
end
|
||||
os.sleep(.25)
|
||||
end
|
||||
if not items then
|
||||
-- error('could not check inventory')
|
||||
term.clear()
|
||||
print('rebooting in 5 secs')
|
||||
os.sleep(5)
|
||||
os.reboot()
|
||||
end
|
||||
|
||||
return items
|
||||
end
|
||||
|
||||
local function filterItems(t, filter, displayMode)
|
||||
if filter or displayMode > 0 then
|
||||
local r = { }
|
||||
@@ -201,6 +220,7 @@ local function craftItem(recipe, items, originalItem, craftList, count)
|
||||
if not isGridClear() then
|
||||
if not clearGrid() then
|
||||
originalItem.status = 'Grid obstructed'
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -223,7 +243,7 @@ local function craftItem(recipe, items, originalItem, craftList, count)
|
||||
--debug('crafting ' .. key .. ' - ' .. need - has)
|
||||
--read()
|
||||
craftItem(iRecipe, items, originalItem, { }, math.ceil((need - has) / iRecipe.count))
|
||||
items = inventoryAdapter:listItems()
|
||||
items = listItems()
|
||||
if not items then
|
||||
error('list failed')
|
||||
--return 0
|
||||
@@ -238,11 +258,11 @@ local function craftItem(recipe, items, originalItem, craftList, count)
|
||||
if toCraft > 0 then
|
||||
crafted = Craft.craftRecipe(recipe, toCraft, inventoryAdapter)
|
||||
clearGrid()
|
||||
items = inventoryAdapter:listItems()
|
||||
items = listItems()
|
||||
count = count - crafted
|
||||
end
|
||||
|
||||
if count > 0 then
|
||||
if count > 0 and items then
|
||||
local ingredients = Craft.getResourceList(recipe, items, count)
|
||||
for _,ingredient in pairs(ingredients) do
|
||||
--if not ingredient.recipe and ingredient.count < 0 then
|
||||
@@ -269,15 +289,9 @@ local function forceCraftItem(inRecipe, items, originalItem, craftList, inCount)
|
||||
if not summedItem then
|
||||
summedItem = Util.shallowCopy(item)
|
||||
summedItem.recipe = Craft.recipes[key]
|
||||
|
||||
if summedItem.recipe then
|
||||
summedItem.recipe.key = key
|
||||
end
|
||||
summedItem.count = Craft.getItemCount(items, key)
|
||||
summedItem.ocount = summedItem.count
|
||||
summedItem.need = 0
|
||||
summedItem.used = 0
|
||||
summedItem.missing = 0
|
||||
summedItem.craftable = 0
|
||||
summed[key] = summedItem
|
||||
end
|
||||
@@ -297,43 +311,34 @@ end
|
||||
else
|
||||
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
|
||||
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
|
||||
debug({ 'summed', total, used, need, c })
|
||||
craftable = math.min(craftable, math.floor((used + c) / iqty))
|
||||
|
||||
summedItem.craftable = summedItem.craftable + c
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if craftable > 0 then
|
||||
debug('crafting ' .. recipe.key)
|
||||
debug({ count, craftable })
|
||||
_G._p = summed
|
||||
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter)
|
||||
craftable = Craft.craftRecipe(recipe, craftable, inventoryAdapter) / recipe.count
|
||||
clearGrid()
|
||||
debug('got ' .. craftable)
|
||||
|
||||
end
|
||||
|
||||
return craftable * recipe.count
|
||||
end
|
||||
|
||||
local count = sumItems(inRecipe, inCount)
|
||||
debug({ 'final', inCount, count })
|
||||
|
||||
-- local count, summed = Craft.getResourceList3(inRecipe, items, inCount, inventoryAdapter)
|
||||
if count < inCount then
|
||||
for _,ingredient in pairs(summed) do
|
||||
--if not ingredient.recipe and ingredient.count < 0 then
|
||||
if ingredient.missing > 0 then
|
||||
addCraftingRequest(ingredient, craftList, ingredient.missing)
|
||||
if ingredient.need > 0 then
|
||||
addCraftingRequest(ingredient, craftList, ingredient.need)
|
||||
originalItem.status = string.format('%s missing', itemDB:getName(ingredient))
|
||||
originalItem.statusCode = 'missing'
|
||||
end
|
||||
@@ -351,12 +356,11 @@ local function craftItems(craftList, allItems)
|
||||
item.status = nil
|
||||
item.statusCode = nil
|
||||
if item.forceCrafting then
|
||||
recipe.key = key
|
||||
item.crafted = forceCraftItem(recipe, allItems, item, craftList, item.count)
|
||||
else
|
||||
item.crafted = craftItem(recipe, allItems, item, craftList, item.count)
|
||||
end
|
||||
allItems = inventoryAdapter:listItems() -- refresh counts
|
||||
allItems = listItems() -- refresh counts
|
||||
if not allItems then
|
||||
break
|
||||
end
|
||||
@@ -408,29 +412,43 @@ local function jobMonitor()
|
||||
})
|
||||
end
|
||||
|
||||
jobListGrid = UI.Grid({
|
||||
jobList = UI.Page {
|
||||
parent = mon,
|
||||
sortColumn = 'displayName',
|
||||
columns = {
|
||||
{ heading = 'Qty', key = 'count', width = 6 },
|
||||
{ heading = 'Crafting', key = 'displayName', width = mon.width / 2 - 10 },
|
||||
{ heading = 'Status', key = 'status', width = mon.width - 10 },
|
||||
grid = UI.Grid {
|
||||
sortColumn = 'displayName',
|
||||
columns = {
|
||||
{ heading = 'Qty', key = 'count', width = 6 },
|
||||
{ heading = 'Crafting', key = 'displayName', width = mon.width / 2 - 10 },
|
||||
{ heading = 'Status', key = 'status', width = mon.width - 10 },
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function jobListGrid:getRowTextColor(row, selected)
|
||||
function jobList:showError(msg)
|
||||
self.grid:clear()
|
||||
self.grid:centeredWrite(math.ceil(self.grid.height / 2), msg)
|
||||
self:sync()
|
||||
end
|
||||
|
||||
if row.status == '(no recipe)'then
|
||||
function jobList:updateList(craftList)
|
||||
self.grid:setValues(craftList)
|
||||
self.grid:update()
|
||||
self:draw()
|
||||
self:sync()
|
||||
end
|
||||
|
||||
function jobList.grid:getRowTextColor(row, selected)
|
||||
if row.status == '(no recipe)' then
|
||||
return colors.red
|
||||
elseif row.statusCode == 'missing' then
|
||||
return colors.yellow
|
||||
end
|
||||
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
jobListGrid:draw()
|
||||
jobListGrid:sync()
|
||||
jobList:enable()
|
||||
jobList:draw()
|
||||
jobList:sync()
|
||||
end
|
||||
|
||||
local function getAutocraftItems()
|
||||
@@ -496,6 +514,12 @@ local function watchResources(items)
|
||||
item.damage = 0
|
||||
end
|
||||
local key = uniqueKey(res)
|
||||
if res.name == 'harvestcraft:applesauceitem' then
|
||||
_G._p = items
|
||||
debug('applesause')
|
||||
error('applesauce')
|
||||
end
|
||||
|
||||
craftList[key] = {
|
||||
damage = item.damage,
|
||||
nbtHash = item.nbtHash,
|
||||
@@ -532,12 +556,6 @@ local function loadResources()
|
||||
for _,k in pairs(Util.keys(resources)) do
|
||||
local v = resources[k]
|
||||
Util.merge(v, itemDB:splitKey(k))
|
||||
if not v.damage then
|
||||
v.damage = 0
|
||||
v.ignoreDamage = true
|
||||
resources[k] = nil
|
||||
resources[uniqueKey(v)] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -942,7 +960,7 @@ function listingPage:enable()
|
||||
end
|
||||
|
||||
function listingPage:refresh()
|
||||
self.allItems = inventoryAdapter:listItems()
|
||||
self.allItems = listItems()
|
||||
mergeResources(self.allItems)
|
||||
self:applyFilter()
|
||||
end
|
||||
@@ -1123,7 +1141,7 @@ local craftPage = UI.Page {
|
||||
title = "Information",
|
||||
},
|
||||
wizard = UI.Wizard {
|
||||
y = 2,
|
||||
y = 2, ey = -2,
|
||||
pages = {
|
||||
quantity = UI.Window {
|
||||
index = 1,
|
||||
@@ -1132,37 +1150,37 @@ local craftPage = UI.Page {
|
||||
value = 'Quantity',
|
||||
},
|
||||
count = UI.TextEntry {
|
||||
x = 15,
|
||||
y = 3,
|
||||
width = 10,
|
||||
x = 15, y = 3, width = 10,
|
||||
limit = 6,
|
||||
value = 1,
|
||||
},
|
||||
ejectText = UI.Text {
|
||||
x = 6, y = 4,
|
||||
value = 'Eject',
|
||||
},
|
||||
eject = UI.Chooser {
|
||||
x = 15, y = 4, width = 7,
|
||||
value = true,
|
||||
nochoice = 'No',
|
||||
choices = {
|
||||
{ name = 'Yes', value = true },
|
||||
{ name = 'No', value = false },
|
||||
},
|
||||
},
|
||||
},
|
||||
resources = UI.Window {
|
||||
index = 2,
|
||||
grid = UI.Grid {
|
||||
y = 2, ey = -4,
|
||||
y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
{ heading = 'Qty', key = 'ocount' , width = 5 },
|
||||
{ heading = 'Used', key = 'used' , width = 4 },
|
||||
{ heading = 'Name', key = 'displayName' },
|
||||
{ heading = 'Total', key = 'total' , width = 4 },
|
||||
{ heading = 'Used', key = 'used' , width = 4 },
|
||||
{ heading = 'Craf', key = 'craftable' , width = 4 },
|
||||
{ heading = 'Need', key = 'need' , width = 4 },
|
||||
{ heading = 'Miss', key = 'missing' , width = 4 },
|
||||
{ heading = 'cra', key = 'craftable' , width = 4 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
accept = UI.Button {
|
||||
x = -8, y = -2,
|
||||
backgroundColor = colors.green,
|
||||
text = '+', event = 'accept',
|
||||
},
|
||||
cancel = UI.Button {
|
||||
x = -4, y = -2,
|
||||
backgroundColor = colors.red,
|
||||
text = '\215', event = 'cancel'
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1171,28 +1189,53 @@ local craftPage = UI.Page {
|
||||
function craftPage:enable(item)
|
||||
self.item = item
|
||||
self:focusFirst()
|
||||
-- self.wizard.pages.quantity.eject.value = true
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function craftPage.wizard.pages.resources.grid:getDisplayValues(row)
|
||||
local function dv(v)
|
||||
if v == 0 then
|
||||
return ''
|
||||
end
|
||||
return Util.toBytes(v)
|
||||
end
|
||||
row = Util.shallowCopy(row)
|
||||
row.total = Util.toBytes(row.total)
|
||||
row.used = dv(row.used)
|
||||
row.craftable = dv(row.craftable)
|
||||
row.need = dv(row.need)
|
||||
return row
|
||||
end
|
||||
|
||||
function craftPage.wizard.pages.resources.grid:getRowTextColor(row, selected)
|
||||
if row.need - row.craftable > 0 then
|
||||
return colors.orange
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function craftPage:eventHandler(event)
|
||||
if event.type == 'cancel' then
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'enable_view' and event.view == self.wizard.pages.resources then
|
||||
local items = inventoryAdapter:listItems()
|
||||
local items = listItems()
|
||||
local count = self.wizard.pages.quantity.count.value
|
||||
local recipe = Craft.recipes[uniqueKey(self.item)]
|
||||
local ingredients = Craft.getResourceList3(recipe, items, count)
|
||||
self.wizard.pages.resources.grid:setValues(ingredients)
|
||||
local _, ingredients = Craft.getResourceList3(recipe, items, count)
|
||||
for _,v in pairs(ingredients) do
|
||||
v.displayName = itemDB:getName(v)
|
||||
end
|
||||
self.wizard.pages.resources.grid:setValues(ingredients)
|
||||
|
||||
elseif event.type == 'accept' then
|
||||
local key = uniqueKey(self.item)
|
||||
demandCrafting[key] = Util.shallowCopy(self.item)
|
||||
demandCrafting[key].count = tonumber(self.wizard.pages.quantity.count.value)
|
||||
demandCrafting[key].ocount = demandCrafting[key].count
|
||||
demandCrafting[key].forceCrafting = true
|
||||
demandCrafting[key].eject = self.wizard.pages.quantity.eject.value == true
|
||||
UI:setPreviousPage()
|
||||
else
|
||||
return UI.Page.eventHandler(self, event)
|
||||
@@ -1218,42 +1261,47 @@ jobMonitor()
|
||||
Event.onInterval(5, function()
|
||||
|
||||
if not craftingPaused then
|
||||
local items = inventoryAdapter:listItems()
|
||||
local items = listItems()
|
||||
if not items or Util.size(items) == 0 then
|
||||
jobListGrid.parent:clear()
|
||||
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')
|
||||
jobListGrid:sync()
|
||||
|
||||
jobList:showError('No items in system')
|
||||
else
|
||||
local craftList = watchResources(items)
|
||||
craftItems(craftList, items)
|
||||
|
||||
local demandCrafted
|
||||
if Util.size(demandCrafting) > 0 then
|
||||
items = inventoryAdapter:listItems()
|
||||
items = listItems()
|
||||
if items then
|
||||
local list = Util.shallowCopy(demandCrafting)
|
||||
craftItems(list, items)
|
||||
for k,v in pairs(list) do
|
||||
craftList[k] = v
|
||||
end
|
||||
demandCrafted = Util.shallowCopy(demandCrafting)
|
||||
craftItems(demandCrafted, items)
|
||||
end
|
||||
end
|
||||
|
||||
jobListGrid:setValues(craftList)
|
||||
jobListGrid:update()
|
||||
jobListGrid:draw()
|
||||
jobListGrid:sync()
|
||||
items = listItems()
|
||||
local craftList
|
||||
if items then
|
||||
craftList = watchResources(items)
|
||||
craftItems(craftList, items)
|
||||
end
|
||||
|
||||
if demandCrafted and craftList then
|
||||
for k,v in pairs(demandCrafted) do
|
||||
craftList[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
for _,key in pairs(Util.keys(demandCrafting)) do
|
||||
local item = demandCrafting[key]
|
||||
if item.crafted then
|
||||
item.count = item.count - item.crafted
|
||||
if item.count <= 0 then -- should check statusCode
|
||||
if item.count <= 0 then
|
||||
demandCrafting[key] = nil
|
||||
if item.eject then
|
||||
inventoryAdapter:eject(item, item.ocount, inventoryAdapter.getMetadata().state.facing)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
jobList:updateList(craftList)
|
||||
|
||||
craftList = getAutocraftItems(items) -- autocrafted items don't show on job monitor
|
||||
craftItems(craftList, items)
|
||||
end
|
||||
@@ -1261,4 +1309,4 @@ Event.onInterval(5, function()
|
||||
end)
|
||||
|
||||
UI:pullEvents()
|
||||
jobListGrid.parent:reset()
|
||||
jobList.parent:reset()
|
||||
|
||||
119
apps/crafter.lua
119
apps/crafter.lua
@@ -11,6 +11,7 @@ local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local multishell = _ENV.multishell
|
||||
local os = _G.os
|
||||
local term = _G.term
|
||||
local turtle = _G.turtle
|
||||
|
||||
@@ -32,7 +33,7 @@ local recipes = Util.readTable(RECIPES_FILE) or { }
|
||||
local resources
|
||||
local machines = { }
|
||||
local jobListGrid
|
||||
local lastItems
|
||||
local listing, docked = false, false
|
||||
|
||||
local function getItem(items, inItem, ignoreDamage)
|
||||
for _,item in pairs(items) do
|
||||
@@ -67,12 +68,9 @@ local function getItemWithQty(items, res, ignoreNbtHash)
|
||||
if res.name == v.name and
|
||||
((not res.damage and v.maxDamage > 0) or res.damage == v.damage) and
|
||||
((ignoreNbtHash and v.nbtHash) or res.nbtHash == v.nbtHash) then
|
||||
debug('found')
|
||||
debug(v)
|
||||
return v
|
||||
end
|
||||
end
|
||||
debug('nope:' .. uniqueKey(res))
|
||||
local item = Util.shallowCopy(res)
|
||||
item.count = 0
|
||||
item.maxCount = 1
|
||||
@@ -137,7 +135,15 @@ local function clearGrid()
|
||||
return true
|
||||
end
|
||||
|
||||
local function undock()
|
||||
while listing do
|
||||
os.sleep(.5)
|
||||
end
|
||||
docked = false
|
||||
end
|
||||
|
||||
local function gotoMachine(machine)
|
||||
undock()
|
||||
for _ = 1, machine.index do
|
||||
if not turtle.back() then
|
||||
return
|
||||
@@ -147,8 +153,38 @@ local function gotoMachine(machine)
|
||||
return true
|
||||
end
|
||||
|
||||
local function dock()
|
||||
if not docked then
|
||||
repeat until not turtle.forward()
|
||||
end
|
||||
docked = true
|
||||
end
|
||||
|
||||
local function getItems()
|
||||
while not docked do
|
||||
os.sleep(.5)
|
||||
end
|
||||
|
||||
listing = true
|
||||
|
||||
local items
|
||||
for _ = 1, 5 do
|
||||
items = inventoryAdapter:listItems()
|
||||
if items then
|
||||
break
|
||||
end
|
||||
end
|
||||
if not items then
|
||||
error('could not check inventory')
|
||||
end
|
||||
|
||||
listing = false
|
||||
|
||||
return items
|
||||
end
|
||||
|
||||
local function craftItem(recipe, recipeKey, items, cItem, count)
|
||||
repeat until not turtle.forward()
|
||||
dock()
|
||||
|
||||
local resource = resources[recipeKey]
|
||||
if not resource or not resource.machine then
|
||||
@@ -222,8 +258,7 @@ debug(item)
|
||||
end
|
||||
end
|
||||
|
||||
local function expandList(list)
|
||||
local items = lastItems
|
||||
local function expandList(list, items)
|
||||
|
||||
local function getCraftable(recipe, count)
|
||||
local maxSlots = math.floor(16 / Util.size(recipe.ingredients))
|
||||
@@ -259,15 +294,6 @@ local function expandList(list)
|
||||
return count
|
||||
end
|
||||
|
||||
--[[
|
||||
list = { }
|
||||
debug(getCraftable(recipes['minecraft:brick:0'], 512))
|
||||
for key, item in pairs(list) do
|
||||
debug(item.name .. ' : ' .. item.ocount .. ':' .. item.count)
|
||||
end
|
||||
read()
|
||||
]]
|
||||
|
||||
for key, item in pairs(Util.shallowCopy(list)) do
|
||||
local recipe = recipes[key]
|
||||
|
||||
@@ -280,21 +306,21 @@ read()
|
||||
end
|
||||
|
||||
local function craftItems(craftList)
|
||||
expandList(craftList)
|
||||
lastItems = inventoryAdapter:listItems() -- refresh counts
|
||||
local items = getItems()
|
||||
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, lastItems, item, item.count)
|
||||
repeat until not turtle.forward()
|
||||
craftItem(recipe, key, items, item, item.count)
|
||||
dock()
|
||||
jobListGrid:update()
|
||||
jobListGrid:draw()
|
||||
jobListGrid:sync()
|
||||
clearGrid()
|
||||
lastItems = inventoryAdapter:listItems() -- refresh counts
|
||||
items = getItems()
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -357,7 +383,7 @@ local function saveResources()
|
||||
end
|
||||
|
||||
local function findMachines()
|
||||
repeat until not turtle.forward()
|
||||
dock()
|
||||
|
||||
local function getName(side)
|
||||
local p = Peripheral.getBySide(side)
|
||||
@@ -370,7 +396,6 @@ local function findMachines()
|
||||
end
|
||||
|
||||
local index = 0
|
||||
local t = { }
|
||||
|
||||
local function getMachine(dir)
|
||||
local side = turtle.getAction(dir).side
|
||||
@@ -383,17 +408,10 @@ local function findMachines()
|
||||
end
|
||||
end
|
||||
if machine and type(machine) == 'table' then
|
||||
local rawName = getName(side) or machine.name
|
||||
local name = rawName
|
||||
local i = 1
|
||||
while t[name] do
|
||||
name = rawName .. '_' .. i
|
||||
i = i + 1
|
||||
end
|
||||
t[name] = true
|
||||
|
||||
local name = getName(side) or machine.name
|
||||
table.insert(machines, {
|
||||
name = name,
|
||||
rawName = name,
|
||||
index = index,
|
||||
dir = dir,
|
||||
order = #machines + 1
|
||||
@@ -405,13 +423,19 @@ local function findMachines()
|
||||
getMachine('down')
|
||||
getMachine('up')
|
||||
index = index + 1
|
||||
undock()
|
||||
until not turtle.back()
|
||||
|
||||
local mf = Util.readTable(MACHINES_FILE) or { }
|
||||
for _,m in pairs(machines) do
|
||||
local m2 = Util.find(mf, 'order', m.order)
|
||||
if m2 then
|
||||
m.name = m2.name or m.name
|
||||
if not m2.rawName then
|
||||
m2.rawName = m.rawName
|
||||
end
|
||||
if m.rawName == m2.rawName then
|
||||
m.name = m2.name or m.name
|
||||
end
|
||||
m.empty = m2.empty
|
||||
m.ignore = m2.ignore
|
||||
end
|
||||
@@ -483,6 +507,10 @@ local itemPage = UI.Page {
|
||||
text = 'Select', event= 'selectMachine',
|
||||
formLabel = 'Machine'
|
||||
},
|
||||
info = UI.TextArea {
|
||||
x = 2, ex = -2, y = 6, height = 3,
|
||||
textColor = colors.gray,
|
||||
},
|
||||
button = UI.Button {
|
||||
x = 2, y = 9,
|
||||
text = 'Recipe', event = 'learn',
|
||||
@@ -526,6 +554,16 @@ function itemPage:enable(item)
|
||||
self:focusFirst()
|
||||
end
|
||||
|
||||
function itemPage.form.info:draw()
|
||||
local recipe = recipes[uniqueKey(itemPage.item)]
|
||||
if recipe and itemPage.item.machine then
|
||||
self.value = string.format('Crafts %d using the %s machine',
|
||||
recipe.count,
|
||||
machines[itemPage.item.machine].name)
|
||||
end
|
||||
UI.TextArea.draw(self)
|
||||
end
|
||||
|
||||
--[[
|
||||
function itemPage.machines:eventHandler(event)
|
||||
if event.type == 'grid_focus_row' then
|
||||
@@ -638,7 +676,7 @@ local learnPage = UI.Page {
|
||||
|
||||
function learnPage:enable(target)
|
||||
self.target = target
|
||||
self.allItems = lastItems
|
||||
self.allItems = getItems()
|
||||
mergeResources(self.allItems)
|
||||
|
||||
self.filter.value = ''
|
||||
@@ -950,7 +988,7 @@ function listingPage:enable()
|
||||
end
|
||||
|
||||
function listingPage:refresh()
|
||||
self.allItems = lastItems
|
||||
self.allItems = getItems()
|
||||
mergeResources(self.allItems)
|
||||
self:applyFilter()
|
||||
end
|
||||
@@ -962,10 +1000,9 @@ end
|
||||
|
||||
findMachines()
|
||||
loadResources()
|
||||
repeat until not turtle.forward()
|
||||
dock()
|
||||
clearGrid()
|
||||
jobMonitor()
|
||||
lastItems = inventoryAdapter:listItems()
|
||||
|
||||
UI:setPages({
|
||||
listing = listingPage,
|
||||
@@ -982,15 +1019,15 @@ Event.on('turtle_abort', function()
|
||||
end)
|
||||
|
||||
Event.onInterval(30, function()
|
||||
repeat until not turtle.forward()
|
||||
dock()
|
||||
if turtle.getFuelLevel() < 100 then
|
||||
turtle.select(1)
|
||||
inventoryAdapter:provide({ name = 'minecraft:coal', damage = 1 }, 16, 1)
|
||||
turtle.refuel()
|
||||
end
|
||||
lastItems = inventoryAdapter:listItems()
|
||||
if lastItems then
|
||||
local craftList = watchResources(lastItems)
|
||||
local items = getItems()
|
||||
if items then
|
||||
local craftList = watchResources(items)
|
||||
|
||||
jobListGrid:setValues(craftList)
|
||||
jobListGrid:update()
|
||||
|
||||
@@ -209,7 +209,8 @@ local listingPage = UI.Page {
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName' , width = 22 },
|
||||
{ heading = 'Qty', key = 'count' , width = 5 },
|
||||
{ heading = 'Max', key = 'limit' , width = 4 },
|
||||
{ heading = 'Min', key = 'limit' , width = 4 },
|
||||
{ heading = 'Max', key = 'low' , width = 4 },
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
},
|
||||
|
||||
@@ -150,6 +150,7 @@ function changedPage:refresh()
|
||||
end
|
||||
|
||||
self.grid:setValues(changedItems)
|
||||
debug(storage:getPercentUsed())
|
||||
end
|
||||
self.grid:draw()
|
||||
end
|
||||
|
||||
@@ -19,20 +19,6 @@
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:supremium_essence:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 2 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 5 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 6 ] = "mysticalagriculture:infusion_crystal:*",
|
||||
[ 7 ] = "mysticalagriculture:superium_essence:0",
|
||||
},
|
||||
craftingTools = {
|
||||
[ "mysticalagriculture:infusion_crystal:*" ] = true,
|
||||
},
|
||||
maxCount = 1,
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:intermedium_ingot:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:intermedium_essence:0",
|
||||
@@ -57,20 +43,6 @@
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:intermedium_essence:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:prudentium_essence:0",
|
||||
[ 2 ] = "mysticalagriculture:prudentium_essence:0",
|
||||
[ 5 ] = "mysticalagriculture:prudentium_essence:0",
|
||||
[ 6 ] = "mysticalagriculture:infusion_crystal:*",
|
||||
[ 7 ] = "mysticalagriculture:prudentium_essence:0",
|
||||
},
|
||||
craftingTools = {
|
||||
[ "mysticalagriculture:infusion_crystal:*" ] = true,
|
||||
},
|
||||
maxCount = 1,
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:inferium_ingot:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:inferium_essence:0",
|
||||
@@ -109,20 +81,6 @@
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:superium_essence:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:intermedium_essence:0",
|
||||
[ 2 ] = "mysticalagriculture:intermedium_essence:0",
|
||||
[ 5 ] = "mysticalagriculture:intermedium_essence:0",
|
||||
[ 6 ] = "mysticalagriculture:infusion_crystal:*",
|
||||
[ 7 ] = "mysticalagriculture:intermedium_essence:0",
|
||||
},
|
||||
craftingTools = {
|
||||
[ "mysticalagriculture:infusion_crystal:*" ] = true,
|
||||
},
|
||||
maxCount = 1,
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:supremium_armor_core:0" ] = {
|
||||
ingredients = {
|
||||
"mysticalagriculture:supremium_essence:0",
|
||||
@@ -151,20 +109,6 @@
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:prudentium_essence:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:inferium_essence:0",
|
||||
[ 2 ] = "mysticalagriculture:inferium_essence:0",
|
||||
[ 5 ] = "mysticalagriculture:inferium_essence:0",
|
||||
[ 6 ] = "mysticalagriculture:infusion_crystal:*",
|
||||
[ 7 ] = "mysticalagriculture:inferium_essence:0",
|
||||
},
|
||||
craftingTools = {
|
||||
[ "mysticalagriculture:infusion_crystal:*" ] = true,
|
||||
},
|
||||
maxCount = 1,
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:base_essence_ingot:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:prosperity_shard:0",
|
||||
@@ -175,16 +119,6 @@
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:superium_ingot:0" ] = {
|
||||
ingredients = {
|
||||
[ 10 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 2 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 5 ] = "mysticalagriculture:superium_essence:0",
|
||||
[ 6 ] = "mysticalagriculture:intermedium_ingot:0",
|
||||
[ 7 ] = "mysticalagriculture:superium_essence:0",
|
||||
},
|
||||
count = 1,
|
||||
},
|
||||
[ "mysticalagriculture:tier2_inferium_seeds:0" ] = {
|
||||
count = 1,
|
||||
ingredients = {
|
||||
|
||||
Reference in New Issue
Block a user