rework milo crafting

This commit is contained in:
kepler155c
2018-11-10 13:04:23 -05:00
parent 7d09073703
commit aff05f8587
8 changed files with 103 additions and 234 deletions

View File

@@ -19,7 +19,6 @@ local page = UI.Window {
textScale = .5,
},
grid = UI.Grid {
ey = -6,
columns = {
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Change', key = 'change', width = 6 },
@@ -28,31 +27,6 @@ local page = UI.Window {
},
sortColumn = 'displayName',
},
buttons = UI.Window {
y = -5, height = 5,
backgroundColor = colors.gray,
prevButton = UI.Button {
x = 2, y = 2, height = 3, width = 5,
event = 'previous',
backgroundColor = colors.lightGray,
text = ' < '
},
resetButton = UI.Button {
x = 8, y = 2, height = 3, ex = -8,
event = 'reset',
backgroundColor = colors.lightGray,
text = 'Reset'
},
nextButton = UI.Button {
x = -6, y = 2, height = 3, width = 5,
event = 'next',
backgroundColor = colors.lightGray,
text = ' > '
},
},
accelerators = {
q = 'quit',
}
}
function page.grid:getRowTextColor(row, selected)
@@ -77,27 +51,11 @@ function page.grid:getDisplayValues(row)
return row
end
function page:eventHandler(event)
if event.type == 'reset' then
self.lastItems = nil
self.grid:setValues({ })
self.grid:clear()
self.grid:draw()
elseif event.type == 'next' then
self.grid:nextPage()
elseif event.type == 'previous' then
self.grid:previousPage()
elseif event.type == 'quit' then
Event.exitPullEvents()
else
return UI.Window.eventHandler(self, event)
end
return true
function page:reset()
self.lastItems = nil
self.grid:setValues({ })
self.grid:clear()
self.grid:draw()
end
function page:refresh()
@@ -165,7 +123,7 @@ end)
Event.on('monitor_touch', function(_, side)
if side == mon.side then
page:emit({ type = 'reset' })
page:reset()
page:sync()
end
end)

View File

@@ -1,5 +1,4 @@
local Craft = require('turtle.craft')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync').sync
local Util = require('util')
@@ -12,89 +11,30 @@ local craftTask = {
priority = 70,
}
function craftTask:craftItem(recipe, item, count)
Craft.craftRecipe(recipe, count, context.storage, item)
Milo:clearGrid()
end
-- Craft as much as possible regardless if all ingredients are available
function craftTask:forceCraftItem(inRecipe, originalItem, inCount)
local summed = { }
local items = Milo:listItems()
local throttle = Util.throttle()
local function sumItems(recipe, count)
count = math.ceil(count / recipe.count)
local craftable = count
for key,iqty in pairs(Craft.sumIngredients(recipe)) do
throttle()
local item = itemDB:splitKey(key)
local summedItem = summed[key]
if not summedItem then
summedItem = Util.shallowCopy(item)
summedItem.recipe = Craft.findRecipe(item)
summedItem.count = Craft.getItemCount(items, key)
summedItem.need = 0
summedItem.used = 0
summedItem.craftable = 0
summed[key] = summedItem
end
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
if summedItem.count > 0 then
summedItem.used = 1
summedItem.need = 0
need = 0
elseif not summedItem.recipe then
summedItem.need = 1
need = 1
else
need = 1
end
else
summedItem.count = summedItem.count - used
summedItem.used = summedItem.used + used
end
if need > 0 then
if not summedItem.recipe then
craftable = math.min(craftable, math.floor(used / iqty))
summedItem.need = summedItem.need + need
else
local c = sumItems(summedItem.recipe, need) -- 4
craftable = math.min(craftable, math.floor((used + c) / iqty))
summedItem.craftable = summedItem.craftable + c
end
end
end
if craftable > 0 then
craftable = Craft.craftRecipe(recipe, craftable * recipe.count,
context.storage, originalItem) / recipe.count
Milo:clearGrid()
end
return craftable * recipe.count
end
return sumItems(inRecipe, inCount)
end
function craftTask:craft(recipe, item)
if Milo:isCraftingPaused() then
return
end
if item.forceCrafting then
self:forceCraftItem(recipe, item, item.count - item.crafted)
else
self:craftItem(recipe, item, item.count - item.crafted)
Craft.processPending(item, context.storage)
item.ingredients = Craft.getResourceList(recipe, Milo:listItems(), item.count - item.crafted)
for k, v in pairs(item.ingredients) do
v.crafted = v.used
v.count = v.used
v.key = k
if v.need > 0 then
v.status = 'No recipe'
v.statusCode = Craft.STATUS_ERROR
end
end
item.ingredients[recipe.result] = Util.shallowCopy(item)
item.ingredients[recipe.result].total = item.count
item.ingredients[recipe.result].crafted = item.crafted
Craft.craftRecipe(recipe, item.count - item.crafted, context.storage, item)
Milo:clearGrid()
end
function craftTask:cycle()

View File

@@ -100,7 +100,7 @@ function craftPage.wizard.pages.resources:enable()
local count = tonumber(self.parent.quantity.count.value)
local recipe = Craft.findRecipe(craftPage.item)
if recipe then
local ingredients = Craft.getResourceList4(recipe, items, count)
local ingredients = Craft.getResourceList(recipe, items, count)
for _,v in pairs(ingredients) do
v.displayName = itemDB:getName(v)
end

View File

@@ -25,7 +25,12 @@ local jobMonitor = UI.Page {
{ heading = 'Qty', key = 'remaining', width = 4 },
{ heading = 'Crafting', key = 'displayName', },
{ heading = 'Status', key = 'status', },
{ heading = 'Progress', key = 'progress', width = 8 },
{ heading = 'need', key = 'need', width = 4 },
{ heading = 'total', key = 'total', width = 4 },
{ heading = 'used', key = 'used', width = 4 },
{ heading = 'count', key = 'count', width = 4 },
{ heading = 'crafted', key = 'crafted', width = 4 },
-- { heading = 'Progress', key = 'progress', width = 8 },
},
},
}
@@ -44,7 +49,7 @@ function jobMonitor:updateList(craftList)
v.index = #t
v.showRemaining = true
for k2,v2 in pairs(v.ingredients) do
if v2 ~= v then
if v2.key ~= v.key and v2.statusCode then
table.insert(t, v2)
if not v2.displayName then
v2.displayName = itemDB:getName(k2)