Autocrafting improvements
This commit is contained in:
@@ -130,21 +130,27 @@ function ChestAdapter:craftItems()
|
||||
end
|
||||
|
||||
function ChestAdapter:provide(item, qty, slot, direction)
|
||||
local stacks = self.list()
|
||||
for key,stack in Util.rpairs(stacks) do
|
||||
if stack.name == item.name and
|
||||
(not item.damage or stack.damage == item.damage) and
|
||||
stack.nbtHash == item.nbtHash then
|
||||
local amount = math.min(qty, stack.count)
|
||||
if amount > 0 then
|
||||
self.pushItems(direction or self.direction, key, amount, slot)
|
||||
end
|
||||
qty = qty - amount
|
||||
if qty <= 0 then
|
||||
break
|
||||
local s, m = pcall(function()
|
||||
local stacks = self.list()
|
||||
for key,stack in Util.rpairs(stacks) do
|
||||
if stack.name == item.name and
|
||||
(not item.damage or stack.damage == item.damage) and
|
||||
(not item.nbtHash or stack.nbtHash == item.nbtHash) then
|
||||
local amount = math.min(qty, stack.count)
|
||||
if amount > 0 then
|
||||
self.pushItems(direction or self.direction, key, amount, slot)
|
||||
end
|
||||
qty = qty - amount
|
||||
if qty <= 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not s then
|
||||
debug(m)
|
||||
end
|
||||
return s, m
|
||||
end
|
||||
|
||||
function ChestAdapter:extract(slot, qty, toSlot)
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
local ChestAdapter = require('chestAdapter')
|
||||
local ChestAdapter18 = require('chestAdapter18')
|
||||
local MEAdapter = require('meAdapter')
|
||||
|
||||
local Adapter = { }
|
||||
|
||||
function Adapter.wrap(args)
|
||||
local adapter = ChestAdapter18(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
local adapters = {
|
||||
'refinedAdapter',
|
||||
'meAdapter',
|
||||
'chestAdapter18',
|
||||
'chestAdapter',
|
||||
}
|
||||
|
||||
adapter = MEAdapter(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
for _,adapterType in ipairs(adapters) do
|
||||
local adapter = require(adapterType)(args)
|
||||
|
||||
adapter = ChestAdapter(args)
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
if adapter:isValid() then
|
||||
return adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -71,6 +71,18 @@ function itemDB:get(key)
|
||||
return item
|
||||
end
|
||||
end
|
||||
|
||||
if not key[3] then
|
||||
for _,item in pairs(self.data) do
|
||||
if item.name == key[1] and
|
||||
item.damage == key[2] and
|
||||
item.nbtHash then
|
||||
item = Util.shallowCopy(item)
|
||||
item.nbtHash = nil
|
||||
return item
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function itemDB:add(key, item)
|
||||
|
||||
@@ -13,6 +13,7 @@ local function clearGrid(inventoryAdapter)
|
||||
if count > 0 then
|
||||
inventoryAdapter:insert(i, count)
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
-- inventory is possibly full
|
||||
return false
|
||||
end
|
||||
end
|
||||
@@ -33,18 +34,24 @@ end
|
||||
|
||||
local function getItemCount(items, key)
|
||||
local item = splitKey(key)
|
||||
local count = 0
|
||||
for _,v in pairs(items) do
|
||||
if v.name == item.name and
|
||||
(not item.damage or v.damage == item.damage) and
|
||||
v.nbtHash == item.nbtHash then
|
||||
return v.count
|
||||
if item.damage then
|
||||
return v.count
|
||||
end
|
||||
count = count + v.count
|
||||
end
|
||||
end
|
||||
return 0
|
||||
return count
|
||||
end
|
||||
|
||||
local function turtleCraft(recipe, qty, inventoryAdapter)
|
||||
clearGrid(inventoryAdapter)
|
||||
if not clearGrid(inventoryAdapter) then
|
||||
return false
|
||||
end
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
@@ -88,6 +95,7 @@ function Craft.sumIngredients(recipe)
|
||||
for _,item in pairs(recipe.ingredients) do
|
||||
t[item] = (t[item] or 0) + 1
|
||||
end
|
||||
-- need a check for crafting tool
|
||||
return t
|
||||
end
|
||||
|
||||
@@ -102,11 +110,9 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
|
||||
local items = inventoryAdapter:listItems()
|
||||
|
||||
count = math.ceil(count / recipe.count)
|
||||
|
||||
local maxCount = recipe.maxCount or math.floor(64 / recipe.count)
|
||||
local summedItems = Craft.sumIngredients(recipe)
|
||||
|
||||
for key,icount in pairs(summedItems) do
|
||||
for key,icount in pairs(Craft.sumIngredients(recipe)) do
|
||||
local itemCount = getItemCount(items, key)
|
||||
if itemCount < icount * count then
|
||||
local irecipe = Craft.recipes[key]
|
||||
@@ -135,6 +141,38 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter)
|
||||
return crafted * recipe.count
|
||||
end
|
||||
|
||||
-- determine the full list of ingredients needed to craft
|
||||
-- a quantity of a recipe.
|
||||
-- negative quantities denote missing ingredients
|
||||
function Craft.getResourceList(inRecipe, items, inCount)
|
||||
local summed = { }
|
||||
local throttle = Util.throttle()
|
||||
|
||||
local function sumItems(recipe, count)
|
||||
for key,iqty in pairs(Craft.sumIngredients(recipe)) do
|
||||
throttle()
|
||||
local item = splitKey(key)
|
||||
local summedItem = summed[key]
|
||||
if not summedItem then
|
||||
summedItem = Util.shallowCopy(item)
|
||||
summedItem.recipe = Craft.recipes[key]
|
||||
summedItem.count = getItemCount(items, key)
|
||||
summed[key] = summedItem
|
||||
end
|
||||
summedItem.count = summedItem.count - (count * iqty)
|
||||
if summedItem.recipe and summedItem.count < 0 then
|
||||
local need = math.ceil(-summedItem.count / summedItem.recipe.count)
|
||||
summedItem.count = 0
|
||||
sumItems(summedItem.recipe, need)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
sumItems(inRecipe, math.ceil(inCount / inRecipe.count))
|
||||
|
||||
return summed
|
||||
end
|
||||
|
||||
-- given a certain quantity, return how many of those can be crafted
|
||||
function Craft.getCraftableAmount(recipe, count, items, missing)
|
||||
local function sumItems(recipe, summedItems, count)
|
||||
|
||||
@@ -89,11 +89,18 @@ local function splitKey(key)
|
||||
end
|
||||
|
||||
local function getItemQuantity(items, item)
|
||||
item = getItem(items, item)
|
||||
if item then
|
||||
return item.count
|
||||
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 0
|
||||
return count
|
||||
end
|
||||
|
||||
local function uniqueKey(item)
|
||||
@@ -152,39 +159,6 @@ local function filterItems(t, filter, displayMode)
|
||||
return t
|
||||
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()
|
||||
for i = 1, 16 do
|
||||
if turtle.getItemCount(i) ~= 0 then
|
||||
@@ -239,14 +213,14 @@ local function craftItem(recipe, items, originalItem, craftList, count)
|
||||
local need = count * qty
|
||||
local has = getItemQuantity(items, splitKey(key))
|
||||
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()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
debug('count: ' .. count)
|
||||
debug('toCraft: ' .. toCraft)
|
||||
|
||||
local crafted = 0
|
||||
|
||||
if toCraft > 0 then
|
||||
@@ -255,20 +229,17 @@ debug('toCraft: ' .. toCraft)
|
||||
items = inventoryAdapter:listItems()
|
||||
count = count - crafted
|
||||
end
|
||||
debug('count: ' .. count)
|
||||
|
||||
if count > 0 then
|
||||
local summedItems = { }
|
||||
sumItems3(recipe.ingredients, items, summedItems, math.ceil(count / recipe.count))
|
||||
|
||||
for _,ingredient in pairs(summedItems) do
|
||||
local ingredients = Craft.getResourceList(recipe, items, count)
|
||||
_G._p = ingredients
|
||||
for _,ingredient in pairs(ingredients) do
|
||||
--if not ingredient.recipe and ingredient.count < 0 then
|
||||
if ingredient.count < 0 then
|
||||
addCraftingRequest(ingredient, craftList, -ingredient.count)
|
||||
end
|
||||
end
|
||||
end
|
||||
debug('crafted: ' .. crafted)
|
||||
return crafted
|
||||
end
|
||||
|
||||
@@ -278,8 +249,9 @@ local function craftItems(craftList, allItems)
|
||||
local item = craftList[key]
|
||||
local recipe = Craft.recipes[key]
|
||||
if recipe then
|
||||
local crafted = craftItem(recipe, allItems, item, craftList, item.count)
|
||||
item.count = item.count - crafted
|
||||
item.status = nil
|
||||
item.statusCode = nil
|
||||
item.crafted = craftItem(recipe, allItems, item, craftList, item.count)
|
||||
allItems = inventoryAdapter:listItems() -- refresh counts
|
||||
elseif item.rsControl then
|
||||
item.status = 'Activated'
|
||||
@@ -287,7 +259,6 @@ local function craftItems(craftList, allItems)
|
||||
end
|
||||
|
||||
for key,item in pairs(craftList) do
|
||||
|
||||
if not Craft.recipes[key] and not item.rsControl then
|
||||
if not controller then
|
||||
item.status = '(no recipe)'
|
||||
@@ -317,7 +288,6 @@ local function craftItems(craftList, allItems)
|
||||
end
|
||||
|
||||
local function jobMonitor()
|
||||
|
||||
local mon = Peripheral.getByType('monitor')
|
||||
|
||||
if mon then
|
||||
@@ -1052,12 +1022,6 @@ Event.onInterval(5, function()
|
||||
if Util.size(demandCrafting) > 0 then
|
||||
local list = Util.shallowCopy(demandCrafting)
|
||||
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
|
||||
craftList[k] = v
|
||||
end
|
||||
@@ -1067,6 +1031,15 @@ Event.onInterval(5, function()
|
||||
jobListGrid:update()
|
||||
jobListGrid:draw()
|
||||
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
|
||||
craftItems(craftList, items)
|
||||
end
|
||||
|
||||
411
apps/crafter.lua
411
apps/crafter.lua
@@ -44,20 +44,32 @@ local function getItem(items, inItem, ignoreDamage)
|
||||
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)
|
||||
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||
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)
|
||||
for _,v in pairs(resources) do
|
||||
local item = getItem(t, v)
|
||||
@@ -130,7 +142,7 @@ local function canCraft(recipe, items, count)
|
||||
|
||||
local icount = Util.size(recipe.ingredients)
|
||||
local maxSlots = math.floor(16 / icount)
|
||||
debug(maxSlots)
|
||||
|
||||
for key,qty in pairs(recipe.ingredients) do
|
||||
local item = getItem(items, itemDB:splitKey(key))
|
||||
if not item then
|
||||
@@ -138,21 +150,20 @@ debug(maxSlots)
|
||||
end
|
||||
local x = math.min(math.floor(item.count / qty), item.maxCount * maxSlots)
|
||||
count = math.min(x, count)
|
||||
debug(count)
|
||||
end
|
||||
|
||||
return count, ''
|
||||
end
|
||||
|
||||
local function craftItem(recipe, items, cItem, count)
|
||||
local function craftItem(recipe, recipeKey, items, cItem, count)
|
||||
repeat until not turtle.forward()
|
||||
|
||||
-- local missing
|
||||
-- count, missing = canCraft(recipe, items, count)
|
||||
-- if count == 0 then
|
||||
-- cItem.status = 'missing ' .. missing
|
||||
-- return false
|
||||
-- end
|
||||
local resource = resources[recipeKey]
|
||||
if not resource or not resource.machine then
|
||||
cItem.status = 'machine not selected'
|
||||
return false
|
||||
end
|
||||
|
||||
if count == 0 then
|
||||
cItem.status = 'missing something'
|
||||
return false
|
||||
@@ -171,19 +182,20 @@ local function craftItem(recipe, items, cItem, count)
|
||||
inventoryAdapter:provide(item, maxCount, slot)
|
||||
if turtle.getItemCount(slot) == 0 then -- ~= maxCount then FIXXX !!!
|
||||
cItem.status = 'failed'
|
||||
debug(item)
|
||||
debug({ c, maxCount, count })
|
||||
--read()
|
||||
return false
|
||||
end
|
||||
c = c - maxCount
|
||||
slot = slot + 1
|
||||
end
|
||||
end
|
||||
if not gotoMachine(recipe.machine) then
|
||||
if not gotoMachine(resource.machine) then
|
||||
cItem.status = 'failed to find machine'
|
||||
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
|
||||
cItem.status = 'machine busy'
|
||||
else
|
||||
@@ -199,7 +211,8 @@ local function expandList(list)
|
||||
local maxSlots = math.floor(16 / Util.size(recipe.ingredients))
|
||||
|
||||
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
|
||||
item = itemDB:get(key)
|
||||
if not item then
|
||||
@@ -208,9 +221,10 @@ local function expandList(list)
|
||||
end
|
||||
item.count = 0
|
||||
end
|
||||
|
||||
local need = qty * count
|
||||
debug({ key, count, need })
|
||||
local irecipe = recipes[key]
|
||||
|
||||
if item.count < need and irecipe then
|
||||
need = math.ceil((need - item.count) / irecipe.count)
|
||||
if not list[key] then
|
||||
@@ -223,9 +237,8 @@ debug({ key, count, need })
|
||||
end
|
||||
list[key].ocount = list[key].ocount + need
|
||||
end
|
||||
debug('adding ' .. key .. ' ' .. need)
|
||||
local icount = getCraftable(irecipe, need)
|
||||
|
||||
local icount = getCraftable(irecipe, need)
|
||||
list[key].count = list[key].count + icount
|
||||
end
|
||||
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
|
||||
local recipe = recipes[key]
|
||||
|
||||
item.count = math.ceil(item.count / recipe.count)
|
||||
item.ocount = item.count
|
||||
if recipe then
|
||||
@@ -263,7 +277,7 @@ local function craftItems(craftList)
|
||||
for key, item in pairs(craftList) do
|
||||
local recipe = recipes[key]
|
||||
if recipe then
|
||||
craftItem(recipe, lastItems, item, item.count)
|
||||
craftItem(recipe, key, lastItems, item, item.count)
|
||||
repeat until not turtle.forward()
|
||||
jobListGrid:update()
|
||||
jobListGrid:draw()
|
||||
@@ -274,55 +288,26 @@ local function craftItems(craftList)
|
||||
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 craftList = { }
|
||||
|
||||
for _,res in pairs(resources) do
|
||||
local item = getItemWithQty(items, res, res.ignoreDamage)
|
||||
if not item then
|
||||
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.low then
|
||||
local item = Util.shallowCopy(res)
|
||||
item.nbtHash = res.nbtHash
|
||||
item.damage = res.damage
|
||||
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
|
||||
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
|
||||
|
||||
return craftList
|
||||
end
|
||||
|
||||
@@ -330,6 +315,11 @@ local function loadResources()
|
||||
resources = Util.readTable(RESOURCE_FILE) or { }
|
||||
for k,v in pairs(resources) do
|
||||
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
|
||||
|
||||
@@ -350,19 +340,32 @@ end
|
||||
local function findMachines()
|
||||
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 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
|
||||
local _
|
||||
_, machine = turtle.inspectDown()
|
||||
_, machine = turtle.getAction(dir).inspect()
|
||||
end
|
||||
if machine and type(machine) == 'table' then
|
||||
local name = machine.name
|
||||
local rawName = getName(side) or machine.name
|
||||
local name = rawName
|
||||
local i = 1
|
||||
while t[name] do
|
||||
name = machine.name .. '_' .. i
|
||||
name = rawName .. '_' .. i
|
||||
i = i + 1
|
||||
end
|
||||
t[name] = true
|
||||
@@ -370,8 +373,15 @@ local function findMachines()
|
||||
table.insert(machines, {
|
||||
name = name,
|
||||
index = index,
|
||||
dir = dir,
|
||||
order = #machines + 1
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
repeat
|
||||
getMachine('down')
|
||||
getMachine('up')
|
||||
index = index + 1
|
||||
until not turtle.back()
|
||||
end
|
||||
@@ -437,18 +447,43 @@ local itemPage = UI.Page {
|
||||
},
|
||||
help = 'Ignore damage of item'
|
||||
},
|
||||
[3] = UI.Button {
|
||||
text = 'Select', event= 'selectMachine',
|
||||
formLabel = 'Machine'
|
||||
},
|
||||
button = UI.Button {
|
||||
x = 2, y = 9,
|
||||
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 { }
|
||||
}
|
||||
|
||||
function itemPage:enable(item)
|
||||
if item then
|
||||
self.item = item
|
||||
|
||||
self.item = Util.shallowCopy(item)
|
||||
self.form:setValues(item)
|
||||
self.titleBar.title = item.displayName or item.name
|
||||
end
|
||||
@@ -456,6 +491,20 @@ function itemPage:enable(item)
|
||||
self:focusFirst()
|
||||
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)
|
||||
if event.type == 'form_cancel' then
|
||||
UI:setPreviousPage()
|
||||
@@ -463,30 +512,48 @@ function itemPage:eventHandler(event)
|
||||
elseif event.type == 'learn' then
|
||||
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
|
||||
self.statusBar:setStatus(event.focused.help)
|
||||
self.statusBar:draw()
|
||||
|
||||
elseif event.type == 'form_complete' then
|
||||
local values = self.form.values
|
||||
local keys = { 'name', 'low', 'damage', 'nbtHash', }
|
||||
local keys = { 'name', 'low', 'damage', 'nbtHash', 'machine' }
|
||||
|
||||
local filtered = { }
|
||||
for _,key in pairs(keys) do
|
||||
filtered[key] = values[key]
|
||||
end
|
||||
filtered.low = tonumber(filtered.low)
|
||||
filtered.machine = self.item.machine
|
||||
filtered.dir = self.item.dir
|
||||
|
||||
if values.ignoreDamage == true then
|
||||
filtered.damage = 0
|
||||
filtered.ignoreDamage = true
|
||||
end
|
||||
|
||||
if Util.empty(filtered) then
|
||||
filtered = nil
|
||||
end
|
||||
debug(filtered)
|
||||
resources[uniqueKey(filtered)] = filtered
|
||||
local key = uniqueKey(filtered)
|
||||
|
||||
resources[key] = filtered
|
||||
saveResources()
|
||||
|
||||
UI:setPreviousPage()
|
||||
@@ -498,55 +565,42 @@ debug(filtered)
|
||||
end
|
||||
|
||||
local learnPage = UI.Page {
|
||||
wizard = UI.Wizard {
|
||||
pages = {
|
||||
screen1 = UI.Window {
|
||||
index = 1,
|
||||
ingredients = UI.ScrollingGrid {
|
||||
y = 2, height = 3,
|
||||
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,
|
||||
},
|
||||
},
|
||||
ingredients = UI.ScrollingGrid {
|
||||
y = 2, height = 3,
|
||||
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,
|
||||
},
|
||||
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
|
||||
mergeResources(self.allItems)
|
||||
|
||||
local screen1 = self.wizard.screen1
|
||||
local screen2 = self.wizard.screen2
|
||||
|
||||
screen1.filter.value = ''
|
||||
screen1.grid.values = self.allItems
|
||||
screen1.grid:update()
|
||||
screen1.ingredients.values = { }
|
||||
screen2.count.value = 1
|
||||
screen2.machine:update()
|
||||
self.filter.value = ''
|
||||
self.grid.values = self.allItems
|
||||
self.grid:update()
|
||||
self.ingredients.values = { }
|
||||
self.count.value = 1
|
||||
|
||||
if target.has_recipe then
|
||||
local recipe = recipes[uniqueKey(target)]
|
||||
screen2.count.value = recipe.count
|
||||
local _, index = Util.find(machines, 'index', recipe.machine)
|
||||
if index then
|
||||
screen2.machine:setIndex(index)
|
||||
end
|
||||
self.count.value = recipe.count
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
screen1.ingredients.values[k] =
|
||||
self.ingredients.values[k] =
|
||||
{ name = k, count = v, displayName = itemDB:getName(k) }
|
||||
end
|
||||
end
|
||||
screen1.ingredients:update()
|
||||
self.ingredients:update()
|
||||
|
||||
self:setFocus(self.filter)
|
||||
UI.Page.enable(self)
|
||||
end
|
||||
|
||||
function learnPage.wizard.screen1:enable()
|
||||
UI.Window.enable(self)
|
||||
self:setFocus(self.filter)
|
||||
end
|
||||
|
||||
function learnPage.wizard.screen1:draw()
|
||||
function learnPage:draw()
|
||||
UI.Window.draw(self)
|
||||
self:write(2, 1, 'Ingredients', nil, colors.yellow)
|
||||
self:write(2, 5, 'Inventory', nil, colors.yellow)
|
||||
end
|
||||
|
||||
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')
|
||||
self:write(2, 12, 'Produces')
|
||||
end
|
||||
|
||||
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()
|
||||
|
||||
elseif event.type == 'accept' then
|
||||
|
||||
local screen1 = self.wizard.screen1
|
||||
local screen2 = self.wizard.screen2
|
||||
|
||||
local recipe = {
|
||||
count = tonumber(screen2.count.value) or 1,
|
||||
count = tonumber(self.count.value) or 1,
|
||||
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
|
||||
end
|
||||
recipes[uniqueKey(self.target)] = recipe
|
||||
@@ -638,24 +661,22 @@ function learnPage:eventHandler(event)
|
||||
UI:setPreviousPage()
|
||||
|
||||
elseif event.type == 'grid_select' then
|
||||
local screen1 = self.wizard.screen1
|
||||
|
||||
if event.element == screen1.grid then
|
||||
if event.element == self.grid then
|
||||
local key = uniqueKey(event.selected)
|
||||
if not screen1.ingredients.values[key] then
|
||||
screen1.ingredients.values[key] = Util.shallowCopy(event.selected)
|
||||
screen1.ingredients.values[key].count = 0
|
||||
if not self.ingredients.values[key] then
|
||||
self.ingredients.values[key] = Util.shallowCopy(event.selected)
|
||||
self.ingredients.values[key].count = 0
|
||||
end
|
||||
screen1.ingredients.values[key].count = screen1.ingredients.values[key].count + 1
|
||||
screen1.ingredients:update()
|
||||
screen1.ingredients:draw()
|
||||
elseif event.element == screen1.ingredients then
|
||||
self.ingredients.values[key].count = self.ingredients.values[key].count + 1
|
||||
self.ingredients:update()
|
||||
self.ingredients:draw()
|
||||
elseif event.element == self.ingredients then
|
||||
event.selected.count = event.selected.count - 1
|
||||
if event.selected.count == 0 then
|
||||
screen1.ingredients.values[uniqueKey(event.selected)] = nil
|
||||
screen1.ingredients:update()
|
||||
self.ingredients.values[uniqueKey(event.selected)] = nil
|
||||
self.ingredients:update()
|
||||
end
|
||||
screen1.ingredients:draw()
|
||||
self.ingredients:draw()
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
_G.requireInjector()
|
||||
|
||||
local ChestAdapter = require('chestAdapter18')
|
||||
local Event = require('event')
|
||||
local MEAdapter = require('meAdapter')
|
||||
local RefinedAdapter = require('refinedAdapter')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
local InventoryAdapter = require('inventoryAdapter')
|
||||
local Event = require('event')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local multishell = _ENV.multishell
|
||||
|
||||
local storage = RefinedAdapter()
|
||||
if not storage:isValid() then
|
||||
storage = MEAdapter({ autoDetect = true })
|
||||
end
|
||||
if not storage:isValid() then
|
||||
storage = ChestAdapter({ autoDetect = true })
|
||||
end
|
||||
if not storage:isValid() then
|
||||
error('Not connected to a storage device')
|
||||
local storage = InventoryAdapter.wrap({ autoDetect = true })
|
||||
if not storage then
|
||||
error('Not connected to a valid inventory')
|
||||
end
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Storage Activity')
|
||||
@@ -106,8 +98,9 @@ function changedPage:refresh()
|
||||
local t = storage:listItems()
|
||||
|
||||
if not t or Util.empty(t) then
|
||||
self:clear()
|
||||
self:centeredWrite(math.ceil(self.height/2), 'Communication failure')
|
||||
debug('clearing')
|
||||
self.grid:clear()
|
||||
self.grid:centeredWrite(math.ceil(self.height/2), 'Communication failure')
|
||||
return
|
||||
end
|
||||
|
||||
@@ -164,4 +157,5 @@ Event.onInterval(5, function()
|
||||
end)
|
||||
|
||||
UI:setPage(changedPage)
|
||||
changedPage:draw()
|
||||
UI:pullEvents()
|
||||
|
||||
Reference in New Issue
Block a user