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 end
function ChestAdapter:provide(item, qty, slot, direction) function ChestAdapter:provide(item, qty, slot, direction)
local stacks = self.list() local s, m = pcall(function()
for key,stack in Util.rpairs(stacks) do local stacks = self.list()
if stack.name == item.name and for key,stack in Util.rpairs(stacks) do
(not item.damage or stack.damage == item.damage) and if stack.name == item.name and
stack.nbtHash == item.nbtHash then (not item.damage or stack.damage == item.damage) and
local amount = math.min(qty, stack.count) (not item.nbtHash or stack.nbtHash == item.nbtHash) then
if amount > 0 then local amount = math.min(qty, stack.count)
self.pushItems(direction or self.direction, key, amount, slot) if amount > 0 then
end self.pushItems(direction or self.direction, key, amount, slot)
qty = qty - amount end
if qty <= 0 then qty = qty - amount
break if qty <= 0 then
break
end
end end
end end
end)
if not s then
debug(m)
end end
return s, m
end end
function ChestAdapter:extract(slot, qty, toSlot) 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 = { } local Adapter = { }
function Adapter.wrap(args) function Adapter.wrap(args)
local adapter = ChestAdapter18(args) local adapters = {
if adapter:isValid() then 'refinedAdapter',
return adapter 'meAdapter',
end 'chestAdapter18',
'chestAdapter',
}
adapter = MEAdapter(args) for _,adapterType in ipairs(adapters) do
if adapter:isValid() then local adapter = require(adapterType)(args)
return adapter
end
adapter = ChestAdapter(args) if adapter:isValid() then
if adapter:isValid() then return adapter
return adapter end
end end
end end

View File

@@ -71,6 +71,18 @@ function itemDB:get(key)
return item return item
end end
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 end
function itemDB:add(key, item) function itemDB:add(key, item)

View File

@@ -13,6 +13,7 @@ local function clearGrid(inventoryAdapter)
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
-- inventory is possibly full
return false return false
end end
end end
@@ -33,18 +34,24 @@ end
local function getItemCount(items, key) local function getItemCount(items, key)
local item = splitKey(key) local item = splitKey(key)
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
(not item.damage or v.damage == item.damage) and (not item.damage or v.damage == item.damage) and
v.nbtHash == item.nbtHash then v.nbtHash == item.nbtHash then
return v.count if item.damage then
return v.count
end
count = count + v.count
end end
end end
return 0 return count
end end
local function turtleCraft(recipe, qty, inventoryAdapter) local function turtleCraft(recipe, qty, inventoryAdapter)
clearGrid(inventoryAdapter) if not clearGrid(inventoryAdapter) then
return false
end
for k,v in pairs(recipe.ingredients) do for k,v in pairs(recipe.ingredients) do
local item = splitKey(v) local item = splitKey(v)
@@ -88,6 +95,7 @@ function Craft.sumIngredients(recipe)
for _,item in pairs(recipe.ingredients) do for _,item in pairs(recipe.ingredients) do
t[item] = (t[item] or 0) + 1 t[item] = (t[item] or 0) + 1
end end
-- need a check for crafting tool
return t return t
end end
@@ -102,11 +110,9 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
local items = inventoryAdapter:listItems() local items = inventoryAdapter:listItems()
count = math.ceil(count / recipe.count) count = math.ceil(count / recipe.count)
local maxCount = recipe.maxCount or math.floor(64 / 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) local itemCount = getItemCount(items, key)
if itemCount < icount * count then if itemCount < icount * count then
local irecipe = Craft.recipes[key] local irecipe = Craft.recipes[key]
@@ -135,6 +141,38 @@ 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
-- 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 -- given a certain quantity, return how many of those can be crafted
function Craft.getCraftableAmount(recipe, count, items, missing) function Craft.getCraftableAmount(recipe, count, items, missing)
local function sumItems(recipe, summedItems, count) local function sumItems(recipe, summedItems, count)

View File

@@ -89,11 +89,18 @@ local function splitKey(key)
end end
local function getItemQuantity(items, item) local function getItemQuantity(items, item)
item = getItem(items, item) local count = 0
if item then for _,v in pairs(items) do
return item.count 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 end
return 0 return count
end end
local function uniqueKey(item) local function uniqueKey(item)
@@ -152,39 +159,6 @@ local function filterItems(t, filter, displayMode)
return t return t
end end
local function sumItems3(ingredients, items, summedItems, count, throttle)
throttle = throttle or Util.throttle()
local function sumItems(items)
-- produces { ['minecraft:planks:0'] = 8 }
local t = {}
for _,item in pairs(items) do
t[item] = (t[item] or 0) + 1
end
return t
end
for key,iqty in pairs(sumItems(ingredients)) do
throttle()
local item = splitKey(key)
--print(key)
local summedItem = summedItems[key]
if not summedItem then
summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.recipes[key]
summedItem.count = getItemQuantity(items, summedItem)
summedItems[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
sumItems3(summedItem.recipe.ingredients, items, summedItems, need, throttle)
end
end
end
local function isGridClear() local function isGridClear()
for i = 1, 16 do for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then if turtle.getItemCount(i) ~= 0 then
@@ -239,14 +213,14 @@ local function craftItem(recipe, items, originalItem, craftList, count)
local need = count * qty local need = count * qty
local has = getItemQuantity(items, splitKey(key)) local has = getItemQuantity(items, splitKey(key))
if has < need then if has < need then
craftItem(iRecipe, items, originalItem, { }, need - has) debug('crafting ' .. key .. ' - ' .. need - has)
craftItem(iRecipe, items, originalItem, { }, math.ceil((need - has) / iRecipe.count))
items = inventoryAdapter:listItems() items = inventoryAdapter:listItems()
end end
end end
end end
end end
debug('count: ' .. count)
debug('toCraft: ' .. toCraft)
local crafted = 0 local crafted = 0
if toCraft > 0 then if toCraft > 0 then
@@ -255,20 +229,17 @@ debug('toCraft: ' .. toCraft)
items = inventoryAdapter:listItems() items = inventoryAdapter:listItems()
count = count - crafted count = count - crafted
end end
debug('count: ' .. count)
if count > 0 then if count > 0 then
local summedItems = { } local ingredients = Craft.getResourceList(recipe, items, count)
sumItems3(recipe.ingredients, items, summedItems, math.ceil(count / recipe.count)) _G._p = ingredients
for _,ingredient in pairs(ingredients) do
for _,ingredient in pairs(summedItems) 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.count < 0 then
addCraftingRequest(ingredient, craftList, -ingredient.count) addCraftingRequest(ingredient, craftList, -ingredient.count)
end end
end end
end end
debug('crafted: ' .. crafted)
return crafted return crafted
end end
@@ -278,8 +249,9 @@ local function craftItems(craftList, allItems)
local item = craftList[key] local item = craftList[key]
local recipe = Craft.recipes[key] local recipe = Craft.recipes[key]
if recipe then if recipe then
local crafted = craftItem(recipe, allItems, item, craftList, item.count) item.status = nil
item.count = item.count - crafted item.statusCode = nil
item.crafted = craftItem(recipe, allItems, item, craftList, item.count)
allItems = inventoryAdapter:listItems() -- refresh counts allItems = inventoryAdapter:listItems() -- refresh counts
elseif item.rsControl then elseif item.rsControl then
item.status = 'Activated' item.status = 'Activated'
@@ -287,7 +259,6 @@ local function craftItems(craftList, allItems)
end end
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 controller then
item.status = '(no recipe)' item.status = '(no recipe)'
@@ -317,7 +288,6 @@ local function craftItems(craftList, allItems)
end end
local function jobMonitor() local function jobMonitor()
local mon = Peripheral.getByType('monitor') local mon = Peripheral.getByType('monitor')
if mon then if mon then
@@ -1052,12 +1022,6 @@ Event.onInterval(5, function()
if Util.size(demandCrafting) > 0 then if Util.size(demandCrafting) > 0 then
local list = Util.shallowCopy(demandCrafting) local list = Util.shallowCopy(demandCrafting)
craftItems(list, inventoryAdapter:listItems()) craftItems(list, inventoryAdapter:listItems())
for _,key in pairs(Util.keys(demandCrafting)) do
debug(key .. ' - ' .. demandCrafting[key].count)
if demandCrafting[key].count <= 0 then -- should check statusCode
demandCrafting[key] = nil
end
end
for k,v in pairs(list) do for k,v in pairs(list) do
craftList[k] = v craftList[k] = v
end end
@@ -1067,6 +1031,15 @@ Event.onInterval(5, function()
jobListGrid:update() jobListGrid:update()
jobListGrid:draw() jobListGrid:draw()
jobListGrid:sync() jobListGrid:sync()
for _,key in pairs(Util.keys(demandCrafting)) do
local item = demandCrafting[key]
item.count = item.count - item.crafted
if item.count <= 0 then -- should check statusCode
demandCrafting[key] = nil
end
end
craftList = getAutocraftItems(items) -- autocrafted items don't show on job monitor craftList = getAutocraftItems(items) -- autocrafted items don't show on job monitor
craftItems(craftList, items) craftItems(craftList, items)
end end

View File

@@ -44,20 +44,32 @@ local function getItem(items, inItem, ignoreDamage)
end end
end end
local function getItemCount(items, inItem)
inItem.count = 0
for _,item in pairs(items) do
if item.name == inItem.name and item.damage == inItem.damage and item.nbtHash == inItem.nbtHash then
inItem.count = item.count
break
end
end
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
local function getItemQuantity(items, res)
local count = 0
for _,v in pairs(items) do
if res.name == v.name and
((not res.damage and v.maxDamage > 0) or res.damage == v.damage) and
((not res.nbtHash and v.nbtHash) or res.nbtHash == v.nbtHash) then
count = count + v.count
end
end
return count
end
local function getItemWithQty(items, res)
for _,v in pairs(items) do
if res.name == v.name and
((not res.damage and v.maxDamage > 0) or res.damage == v.damage) and
((not res.nbtHash and v.nbtHash) or res.nbtHash == v.nbtHash) then
return v
end
end
end
local function mergeResources(t) local function mergeResources(t)
for _,v in pairs(resources) do for _,v in pairs(resources) do
local item = getItem(t, v) local item = getItem(t, v)
@@ -130,7 +142,7 @@ local function canCraft(recipe, items, count)
local icount = Util.size(recipe.ingredients) local icount = Util.size(recipe.ingredients)
local maxSlots = math.floor(16 / icount) local maxSlots = math.floor(16 / icount)
debug(maxSlots)
for key,qty in pairs(recipe.ingredients) do for key,qty in pairs(recipe.ingredients) do
local item = getItem(items, itemDB:splitKey(key)) local item = getItem(items, itemDB:splitKey(key))
if not item then if not item then
@@ -138,21 +150,20 @@ debug(maxSlots)
end end
local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots) local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots)
count = math.min(x, count) count = math.min(x, count)
debug(count)
end end
return count, '' return count, ''
end end
local function craftItem(recipe, items, cItem, count) local function craftItem(recipe, recipeKey, items, cItem, count)
repeat until not turtle.forward() repeat until not turtle.forward()
-- local missing local resource = resources[recipeKey]
-- count, missing = canCraft(recipe, items, count) if not resource or not resource.machine then
-- if count == 0 then cItem.status = 'machine not selected'
-- cItem.status = 'missing ' .. missing return false
-- return false end
-- end
if count == 0 then if count == 0 then
cItem.status = 'missing something' cItem.status = 'missing something'
return false return false
@@ -171,19 +182,20 @@ local function craftItem(recipe, items, cItem, count)
inventoryAdapter:provide(item, maxCount, slot) inventoryAdapter:provide(item, maxCount, slot)
if turtle.getItemCount(slot) == 0 then -- ~= maxCount then FIXXX !!! if turtle.getItemCount(slot) == 0 then -- ~= maxCount then FIXXX !!!
cItem.status = 'failed' cItem.status = 'failed'
debug(item)
debug({ c, maxCount, count })
--read()
return false return false
end end
c = c - maxCount c = c - maxCount
slot = slot + 1 slot = slot + 1
end end
end end
if not gotoMachine(recipe.machine) then if not gotoMachine(resource.machine) then
cItem.status = 'failed to find machine' cItem.status = 'failed to find machine'
else else
turtle.emptyInventory(turtle.dropDown) if resource.dir == 'up' then
turtle.emptyInventory(turtle.dropUp)
else
turtle.emptyInventory(turtle.dropDown)
end
if #turtle.getFilledSlots() ~= 0 then if #turtle.getFilledSlots() ~= 0 then
cItem.status = 'machine busy' cItem.status = 'machine busy'
else else
@@ -199,7 +211,8 @@ local function expandList(list)
local maxSlots = math.floor(16 / Util.size(recipe.ingredients)) local maxSlots = math.floor(16 / Util.size(recipe.ingredients))
for key,qty in pairs(recipe.ingredients) do for key,qty in pairs(recipe.ingredients) do
local item = getItem(items, itemDB:splitKey(key))
local item = getItemWithQty(items, itemDB:splitKey(key))
if not item then if not item then
item = itemDB:get(key) item = itemDB:get(key)
if not item then if not item then
@@ -208,9 +221,10 @@ local function expandList(list)
end end
item.count = 0 item.count = 0
end end
local need = qty * count local need = qty * count
debug({ key, count, need })
local irecipe = recipes[key] local irecipe = recipes[key]
if item.count < need and irecipe then if item.count < need and irecipe then
need = math.ceil((need - item.count) / irecipe.count) need = math.ceil((need - item.count) / irecipe.count)
if not list[key] then if not list[key] then
@@ -223,9 +237,8 @@ debug({ key, count, need })
end end
list[key].ocount = list[key].ocount + need list[key].ocount = list[key].ocount + need
end end
debug('adding ' .. key .. ' ' .. need)
local icount = getCraftable(irecipe, need)
local icount = getCraftable(irecipe, need)
list[key].count = list[key].count + icount list[key].count = list[key].count + icount
end end
local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots) local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots)
@@ -247,6 +260,7 @@ read()
for key, item in pairs(Util.shallowCopy(list)) do for key, item in pairs(Util.shallowCopy(list)) do
local recipe = recipes[key] local recipe = recipes[key]
item.count = math.ceil(item.count / recipe.count) item.count = math.ceil(item.count / recipe.count)
item.ocount = item.count item.ocount = item.count
if recipe then if recipe then
@@ -263,7 +277,7 @@ local function craftItems(craftList)
for key, item in pairs(craftList) do for key, item in pairs(craftList) do
local recipe = recipes[key] local recipe = recipes[key]
if recipe then if recipe then
craftItem(recipe, lastItems, item, item.count) craftItem(recipe, key, lastItems, item, item.count)
repeat until not turtle.forward() repeat until not turtle.forward()
jobListGrid:update() jobListGrid:update()
jobListGrid:draw() jobListGrid:draw()
@@ -274,55 +288,26 @@ local function craftItems(craftList)
end end
end end
local function getItemWithQty(items, res, ignoreDamage)
local item = getItem(items, res, ignoreDamage)
if item and ignoreDamage then
local count = 0
for _,v in pairs(items) do
if item.name == v.name and item.nbtHash == v.nbtHash then
if item.maxDamage > 0 or item.damage == v.damage then
count = count + v.count
end
end
end
item.count = count
end
return item
end
local function watchResources(items) local function watchResources(items)
local craftList = { } local craftList = { }
for _,res in pairs(resources) do for _,res in pairs(resources) do
local item = getItemWithQty(items, res, res.ignoreDamage) if res.low then
if not item then local item = Util.shallowCopy(res)
item = { item.nbtHash = res.nbtHash
damage = res.damage, item.damage = res.damage
nbtHash = res.nbtHash,
name = res.name,
displayName = itemDB:getName(res),
count = 0
}
end
if res.low and item.count < res.low then
if res.ignoreDamage then if res.ignoreDamage then
item.damage = 0 item.damage = nil
end
item.count = getItemQuantity(items, item)
if item.count < res.low then
item.displayName = itemDB:getName(res)
item.count = res.low - item.count
craftList[uniqueKey(res)] = item
end end
local key = uniqueKey(res)
craftList[key] = {
damage = item.damage,
nbtHash = item.nbtHash,
count = res.low - item.count,
name = item.name,
displayName = item.displayName,
status = '',
}
end end
end end
return craftList return craftList
end end
@@ -330,6 +315,11 @@ 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 not v.machine then
local recipe = recipes[k]
v.machine = recipe.machine
v.dir = recipe.dir or 'down'
end
end end
end end
@@ -350,19 +340,32 @@ end
local function findMachines() local function findMachines()
repeat until not turtle.forward() repeat until not turtle.forward()
local function getName(side)
local p = Peripheral.getBySide(side)
if p and p.getMetadata then
local name = p.getMetadata().displayName
if name and not string.find(name, '.', 1, true) then
return name
end
end
end
local index = 0 local index = 0
local t = { } local t = { }
repeat
local machine = Peripheral.getBySide('bottom') local function getMachine(dir)
local side = turtle.getAction(dir).side
local machine = Peripheral.getBySide(side)
if not machine then if not machine then
local _ local _
_, machine = turtle.inspectDown() _, machine = turtle.getAction(dir).inspect()
end end
if machine and type(machine) == 'table' then if machine and type(machine) == 'table' then
local name = machine.name local rawName = getName(side) or machine.name
local name = rawName
local i = 1 local i = 1
while t[name] do while t[name] do
name = machine.name .. '_' .. i name = rawName .. '_' .. i
i = i + 1 i = i + 1
end end
t[name] = true t[name] = true
@@ -370,8 +373,15 @@ local function findMachines()
table.insert(machines, { table.insert(machines, {
name = name, name = name,
index = index, index = index,
dir = dir,
order = #machines + 1
}) })
end end
end
repeat
getMachine('down')
getMachine('up')
index = index + 1 index = index + 1
until not turtle.back() until not turtle.back()
end end
@@ -437,18 +447,43 @@ local itemPage = UI.Page {
}, },
help = 'Ignore damage of item' help = 'Ignore damage of item'
}, },
[3] = UI.Button {
text = 'Select', event= 'selectMachine',
formLabel = 'Machine'
},
button = UI.Button { button = UI.Button {
x = 2, y = 9, x = 2, y = 9,
text = 'Recipe', event = 'learn', text = 'Recipe', event = 'learn',
}, },
}, },
machines = UI.SlideOut {
backgroundColor = colors.cyan,
grid = UI.ScrollingGrid {
y = 2, ey = -4,
values = machines,
disableHeader = true,
columns = {
{ heading = '', key = 'index', width = 2 },
{ heading = 'Name', key = 'name'},
},
sortColumn = 'order',
},
button1 = UI.Button {
x = -14, y = -2,
text = 'Ok', event = 'setMachine',
},
button2 = UI.Button {
x = -9, y = -2,
text = 'Cancel', event = 'cancelMachine',
},
statusBar = UI.StatusBar(),
},
statusBar = UI.StatusBar { } statusBar = UI.StatusBar { }
} }
function itemPage:enable(item) function itemPage:enable(item)
if item then if item then
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
end end
@@ -456,6 +491,20 @@ function itemPage:enable(item)
self:focusFirst() self:focusFirst()
end end
function itemPage.machines:draw()
UI.Window.draw(self)
self:centeredWrite(1, 'Select machine', nil, colors.yellow)
end
function itemPage.machines:eventHandler(event)
if event.type == 'grid_focus_row' then
self.statusBar:setStatus(string.format('%d %s', event.selected.index, event.selected.dir))
else
return UI.SlideOut.eventHandler(self, event)
end
return true
end
function itemPage:eventHandler(event) function itemPage:eventHandler(event)
if event.type == 'form_cancel' then if event.type == 'form_cancel' then
UI:setPreviousPage() UI:setPreviousPage()
@@ -463,30 +512,48 @@ function itemPage:eventHandler(event)
elseif event.type == 'learn' then elseif event.type == 'learn' then
UI:setPage('learn', self.item) UI:setPage('learn', self.item)
elseif event.type == 'setMachine' then
self.item.machine = self.machines.grid:getSelected().index
self.item.dir = self.machines.grid:getSelected().dir
self.machines:hide()
elseif event.type == 'cancelMachine' then
self.machines:hide()
elseif event.type == 'selectMachine' then
self.machines.grid:update()
if self.item.machine then
local _, index = Util.find(machines, 'index', self.item.machine)
if index then
self.machines.grid:setIndex(index)
end
end
self.machines:show()
elseif event.type == 'focus_change' then elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help) self.statusBar:setStatus(event.focused.help)
self.statusBar:draw() self.statusBar:draw()
elseif event.type == 'form_complete' then elseif event.type == 'form_complete' then
local values = self.form.values local values = self.form.values
local keys = { 'name', 'low', 'damage', 'nbtHash', } local keys = { 'name', 'low', 'damage', 'nbtHash', 'machine' }
local filtered = { } local filtered = { }
for _,key in pairs(keys) do for _,key in pairs(keys) do
filtered[key] = values[key] filtered[key] = values[key]
end end
filtered.low = tonumber(filtered.low) filtered.low = tonumber(filtered.low)
filtered.machine = self.item.machine
filtered.dir = self.item.dir
if values.ignoreDamage == true then if values.ignoreDamage == true then
filtered.damage = 0 filtered.damage = 0
filtered.ignoreDamage = true filtered.ignoreDamage = true
end end
if Util.empty(filtered) then local key = uniqueKey(filtered)
filtered = nil
end resources[key] = filtered
debug(filtered)
resources[uniqueKey(filtered)] = filtered
saveResources() saveResources()
UI:setPreviousPage() UI:setPreviousPage()
@@ -498,55 +565,42 @@ debug(filtered)
end end
local learnPage = UI.Page { local learnPage = UI.Page {
wizard = UI.Wizard { ingredients = UI.ScrollingGrid {
pages = { y = 2, height = 3,
screen1 = UI.Window { disableHeader = true,
index = 1, columns = {
ingredients = UI.ScrollingGrid { { heading = 'Name', key = 'displayName', width = 31 },
y = 2, height = 3, { heading = 'Qty', key = 'count' , width = 5 },
values = machines,
disableHeader = true,
columns = {
{ heading = 'Name', key = 'displayName', width = 31 },
{ heading = 'Qty', key = 'count' , width = 5 },
},
sortColumn = 'displayName',
},
grid = UI.ScrollingGrid {
y = 6, height = 5,
disableHeader = true,
columns = {
{ heading = 'Name', key = 'displayName', width = 31 },
{ heading = 'Qty', key = 'count' , width = 5 },
},
sortColumn = 'displayName',
},
filter = UI.TextEntry {
x = 20, ex = -2, y = 5,
limit = 50,
shadowText = 'filter',
backgroundColor = colors.lightGray,
backgroundFocusColor = colors.lightGray,
},
},
screen2 = UI.Window {
index = 2,
machine = UI.ScrollingGrid {
y = 2, height = 7,
values = machines,
disableHeader = true,
columns = {
{ heading = '', key = 'index', width = 2 },
{ heading = 'Name', key = 'name'},
},
sortColumn = 'index',
},
count = UI.TextEntry {
x = 11, y = -2, width = 5,
limit = 50,
},
},
}, },
sortColumn = 'displayName',
},
grid = UI.ScrollingGrid {
y = 6, height = 5,
disableHeader = true,
columns = {
{ heading = 'Name', key = 'displayName', width = 31 },
{ heading = 'Qty', key = 'count' , width = 5 },
},
sortColumn = 'displayName',
},
filter = UI.TextEntry {
x = 20, ex = -2, y = 5,
limit = 50,
shadowText = 'filter',
backgroundColor = colors.lightGray,
backgroundFocusColor = colors.lightGray,
},
count = UI.TextEntry {
x = 11, y = -1, width = 5,
limit = 50,
},
button1 = UI.Button {
x = -14, y = -1,
text = 'Ok', event = 'accept',
},
button2 = UI.Button {
x = -9, y = -1,
text = 'Cancel', event = 'cancel',
}, },
} }
@@ -555,81 +609,50 @@ function learnPage:enable(target)
self.allItems = lastItems self.allItems = lastItems
mergeResources(self.allItems) mergeResources(self.allItems)
local screen1 = self.wizard.screen1 self.filter.value = ''
local screen2 = self.wizard.screen2 self.grid.values = self.allItems
self.grid:update()
screen1.filter.value = '' self.ingredients.values = { }
screen1.grid.values = self.allItems self.count.value = 1
screen1.grid:update()
screen1.ingredients.values = { }
screen2.count.value = 1
screen2.machine:update()
if target.has_recipe then if target.has_recipe then
local recipe = recipes[uniqueKey(target)] local recipe = recipes[uniqueKey(target)]
screen2.count.value = recipe.count self.count.value = recipe.count
local _, index = Util.find(machines, 'index', recipe.machine)
if index then
screen2.machine:setIndex(index)
end
for k,v in pairs(recipe.ingredients) do for k,v in pairs(recipe.ingredients) do
screen1.ingredients.values[k] = self.ingredients.values[k] =
{ name = k, count = v, displayName = itemDB:getName(k) } { name = k, count = v, displayName = itemDB:getName(k) }
end end
end end
screen1.ingredients:update() self.ingredients:update()
self:setFocus(self.filter)
UI.Page.enable(self) UI.Page.enable(self)
end end
function learnPage.wizard.screen1:enable() function learnPage:draw()
UI.Window.enable(self)
self:setFocus(self.filter)
end
function learnPage.wizard.screen1:draw()
UI.Window.draw(self) UI.Window.draw(self)
self:write(2, 1, 'Ingredients', nil, colors.yellow) self:write(2, 1, 'Ingredients', nil, colors.yellow)
self:write(2, 5, 'Inventory', nil, colors.yellow) self:write(2, 5, 'Inventory', nil, colors.yellow)
end self:write(2, 12, 'Produces')
function learnPage.wizard.screen1:eventHandler(event)
if event.type == 'text_change' then
local t = filterItems(learnPage.allItems, event.text)
self.grid:setValues(t)
self.grid:draw()
else
return false
end
return true
end
function learnPage.wizard.screen2:enable()
UI.Window.enable(self)
self:setFocus(self.count)
end
function learnPage.wizard.screen2:draw()
UI.Window.draw(self)
self:centeredWrite(1, 'Machine', nil, colors.yellow)
self:write(2, 10, 'Produces')
end end
function learnPage:eventHandler(event) function learnPage:eventHandler(event)
if event.type == 'cancel' then
if event.type == 'text_change' and event.element == self.filter then
local t = filterItems(learnPage.allItems, event.text)
self.grid:setValues(t)
self.grid:draw()
elseif event.type == 'cancel' then
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'accept' then elseif event.type == 'accept' then
local screen1 = self.wizard.screen1
local screen2 = self.wizard.screen2
local recipe = { local recipe = {
count = tonumber(screen2.count.value) or 1, count = tonumber(self.count.value) or 1,
ingredients = { }, ingredients = { },
machine = screen2.machine:getSelected().index,
} }
for key, item in pairs(screen1.ingredients.values) do for key, item in pairs(self.ingredients.values) do
recipe.ingredients[key] = item.count recipe.ingredients[key] = item.count
end end
recipes[uniqueKey(self.target)] = recipe recipes[uniqueKey(self.target)] = recipe
@@ -638,24 +661,22 @@ function learnPage:eventHandler(event)
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'grid_select' then elseif event.type == 'grid_select' then
local screen1 = self.wizard.screen1 if event.element == self.grid then
if event.element == screen1.grid then
local key = uniqueKey(event.selected) local key = uniqueKey(event.selected)
if not screen1.ingredients.values[key] then if not self.ingredients.values[key] then
screen1.ingredients.values[key] = Util.shallowCopy(event.selected) self.ingredients.values[key] = Util.shallowCopy(event.selected)
screen1.ingredients.values[key].count = 0 self.ingredients.values[key].count = 0
end end
screen1.ingredients.values[key].count = screen1.ingredients.values[key].count + 1 self.ingredients.values[key].count = self.ingredients.values[key].count + 1
screen1.ingredients:update() self.ingredients:update()
screen1.ingredients:draw() self.ingredients:draw()
elseif event.element == screen1.ingredients then elseif event.element == self.ingredients then
event.selected.count = event.selected.count - 1 event.selected.count = event.selected.count - 1
if event.selected.count == 0 then if event.selected.count == 0 then
screen1.ingredients.values[uniqueKey(event.selected)] = nil self.ingredients.values[uniqueKey(event.selected)] = nil
screen1.ingredients:update() self.ingredients:update()
end end
screen1.ingredients:draw() self.ingredients:draw()
end end
else else

View File

@@ -1,24 +1,16 @@
_G.requireInjector() _G.requireInjector()
local ChestAdapter = require('chestAdapter18') local InventoryAdapter = require('inventoryAdapter')
local Event = require('event') local Event = require('event')
local MEAdapter = require('meAdapter') local UI = require('ui')
local RefinedAdapter = require('refinedAdapter') local Util = require('util')
local UI = require('ui')
local Util = require('util')
local colors = _G.colors local colors = _G.colors
local multishell = _ENV.multishell local multishell = _ENV.multishell
local storage = RefinedAdapter() local storage = InventoryAdapter.wrap({ autoDetect = true })
if not storage:isValid() then if not storage then
storage = MEAdapter({ autoDetect = true }) error('Not connected to a valid inventory')
end
if not storage:isValid() then
storage = ChestAdapter({ autoDetect = true })
end
if not storage:isValid() then
error('Not connected to a storage device')
end end
multishell.setTitle(multishell.getCurrent(), 'Storage Activity') multishell.setTitle(multishell.getCurrent(), 'Storage Activity')
@@ -106,8 +98,9 @@ function changedPage:refresh()
local t = storage:listItems() local t = storage:listItems()
if not t or Util.empty(t) then if not t or Util.empty(t) then
self:clear() debug('clearing')
self:centeredWrite(math.ceil(self.height/2), 'Communication failure') self.grid:clear()
self.grid:centeredWrite(math.ceil(self.height/2), 'Communication failure')
return return
end end
@@ -164,4 +157,5 @@ Event.onInterval(5, function()
end) end)
UI:setPage(changedPage) UI:setPage(changedPage)
changedPage:draw()
UI:pullEvents() UI:pullEvents()