This commit is contained in:
kepler155c
2018-10-25 05:51:46 -04:00
parent 6ce4039e8a
commit 9f9dcafc60
8 changed files with 275 additions and 303 deletions

141
milo/plugins/craftTask.lua Normal file
View File

@@ -0,0 +1,141 @@
local Craft = require('turtle.craft')
local itemDB = require('itemDB')
local Milo = require('milo')
local Util = require('util')
local context = Milo:getContext()
local craftTask = {
name = 'crafting',
priority = 70,
}
-- Craft
function craftTask:craftItem(recipe, originalItem, count)
local missing = { }
local toCraft = Craft.getCraftableAmount(recipe, count, Milo:listItems(), missing)
if missing.name then
originalItem.status = string.format('%s missing', itemDB:getName(missing.name))
originalItem.statusCode = Milo.STATUS_WARNING
end
local crafted = 0
if toCraft > 0 then
crafted = Craft.craftRecipe(recipe, toCraft, context.inventoryAdapter, originalItem)
Milo:clearGrid()
end
return crafted
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.inventoryAdapter, originalItem) / recipe.count
Milo:clearGrid()
end
return craftable * recipe.count
end
return sumItems(inRecipe, inCount)
end
function craftTask:craft(recipe, item)
item.status = nil
item.statusCode = nil
item.crafted = 0
if Milo:isCraftingPaused() then
return
end
-- todo: is this needed ?
if not Milo:clearGrid() then
item.status = 'Grid obstructed'
item.statusCode = Milo.STATUS_ERROR
return
end
if item.forceCrafting then
item.crafted = self:forceCraftItem(recipe, item, item.count)
else
item.crafted = self:craftItem(recipe, item, item.count)
end
end
function craftTask:cycle()
for _,key in pairs(Util.keys(context.craftingQueue)) do
local item = context.craftingQueue[key]
if item.count > 0 then
local recipe = Craft.recipes[key]
if recipe then
self:craft(recipe, item)
if item.eject and item.crafted >= item.requested then
Milo:eject(item, item.requested)
end
elseif not context.controllerAdapter then
item.status = '(no recipe)'
item.statusCode = Milo.STATUS_ERROR
item.crafted = 0
end
end
end
end
Milo:registerTask(craftTask)

View File

@@ -6,8 +6,6 @@ local Util = require('util')
local colors = _G.colors
local demandCrafting = { }
local craftPage = UI.Page {
titleBar = UI.TitleBar { },
wizard = UI.Wizard {
@@ -118,12 +116,11 @@ function craftPage:eventHandler(event)
UI:setPreviousPage()
elseif event.type == 'accept' then
local key = Milo: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
local item = Util.shallowCopy(self.item)
item.count = tonumber(self.wizard.pages.quantity.count.value)
item.forceCrafting = true
item.eject = self.wizard.pages.quantity.eject.value == true
Milo:requestCrafting(item)
UI:setPreviousPage()
else
return UI.Page.eventHandler(self, event)
@@ -131,48 +128,4 @@ function craftPage:eventHandler(event)
return true
end
local demandCraftingTask = {
name = 'demand crafting',
priority = 60,
}
function demandCraftingTask:cycle(context)
local demandCrafted = { }
-- look directly at the adapter import activity to determine
-- if the item was imported into storage from any source.
-- The item does NOT need to come from the machine that did
-- the crafting.
for _,key in pairs(Util.keys(demandCrafting)) do
local item = demandCrafting[key]
local imported = context.inventoryAdapter.activity[key]
if imported then
item.crafted = math.min(imported, item.count)
item.count = math.max(0, item.count - item.crafted)
context.inventoryAdapter.activity[key] = imported - item.crafted
end
demandCrafted[key] = item
end
if Util.size(demandCrafted) > 0 then
Milo:craftItems(demandCrafted)
end
for _,key in pairs(Util.keys(demandCrafting)) do
local item = demandCrafting[key]
if item.crafted then
item.count = math.max(0, item.count - item.crafted)
if item.count <= 0 then
item.statusCode = 'success'
demandCrafting[key] = nil
if item.eject then
Milo:eject(item, item.ocount)
end
end
end
end
end
UI:addPage('craft', craftPage)
Milo:registerTask(demandCraftingTask)

View File

@@ -15,12 +15,14 @@ local display = UI.Device {
local jobList = UI.Page {
parent = display,
grid = UI.Grid {
sortColumn = 'displayName',
sortColumn = 'index',
backgroundFocusColor = colors.black,
columns = {
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Crafting', key = 'displayName', width = display.width / 2 - 10 },
{ heading = 'Status', key = 'status', width = display.width - 10 },
{ heading = 'Crafting', key = 'displayName', }, -- width = display.width / 2 - 10 },
{ heading = 'Status', key = 'status', }, -- width = display.width - 10 },
{ heading = 'Req', key = 'requested', width = 6 },
{ heading = 'Cra', key = 'crafted', width = 6 },
},
},
}
@@ -33,7 +35,20 @@ end
function jobList:updateList(craftList)
if not Milo:isCraftingPaused() then
self.grid:setValues(craftList)
local t = { }
local index = 1
for k,v in pairs(craftList) do
t[k] = v
v.index = index
index = index + 1
for k2,v2 in pairs(v.processing) do
t[k2] = v2
v2.displayName = k2
v2.index = index
index = index + 1
end
end
self.grid:setValues(t)
self.grid:update()
self:draw()
self:sync()
@@ -61,7 +76,7 @@ local JobListTask = {
}
function JobListTask:cycle()
jobList:updateList(Milo:getCraftingStatus())
jobList:updateList(context.craftingQueue)
end
Milo:registerTask(JobListTask)

View File

@@ -3,12 +3,10 @@ local Milo = require('milo')
local ReplenishTask = {
name = 'replenish',
priority = 70,
priority = 60,
}
function ReplenishTask:cycle(context)
local craftList = { }
for _,res in pairs(context.resources) do
if res.low then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
@@ -26,22 +24,16 @@ function ReplenishTask:cycle(context)
if res.ignoreDamage then
item.damage = 0
end
local key = Milo:uniqueKey(res)
craftList[key] = {
Milo:requestCrafting({
damage = item.damage,
nbtHash = item.nbtHash,
count = res.low - item.count,
name = item.name,
displayName = item.displayName,
status = '',
rsControl = res.rsControl,
}
})
end
end
end
Milo:craftItems(craftList)
end
Milo:registerTask(ReplenishTask)