milo: smart transfer

This commit is contained in:
kepler155c@gmail.com
2018-12-01 19:08:57 -05:00
parent 0853a16b59
commit 4a844eebfe
19 changed files with 177 additions and 121 deletions

View File

@@ -30,7 +30,7 @@ function brewingStandView:isValidType(node)
return m and m.type == 'minecraft:brewing_stand'and {
name = 'Brewing Stand',
value = 'brewingStand',
category = 'custom',
category = 'machine',
help = 'Auto-learning brewing stand',
}
end

View File

@@ -1,6 +1,5 @@
local Craft = require('craft2')
local Milo = require('milo')
local sync = require('sync').sync
local Util = require('util')
local context = Milo:getContext()
@@ -63,9 +62,7 @@ function craftTask:cycle()
if item.requested - item.crafted > 0 then
local recipe = Craft.findRecipe(key)
if recipe then
sync(turtle, function()
self:craft(recipe, item)
end)
self:craft(recipe, item)
if item.callback and item.crafted >= item.requested then
item.callback(item) -- invoke callback
end

View File

@@ -39,7 +39,7 @@ function ExportTask:cycle(context)
local _, item = next(items)
if item then
local count = math.min(item.count, slot.maxCount - slot.count)
context.storage:export(node.name, entry.slot, count, item)
context.storage:export(node, entry.slot, count, item)
end
break
end
@@ -53,7 +53,7 @@ function ExportTask:cycle(context)
local _, item = next(items)
if item then
local count = math.min(item.count, itemDB:getMaxCount(item))
context.storage:export(node.name, entry.slot, count, item)
context.storage:export(node, entry.slot, count, item)
break
end
end
@@ -63,7 +63,7 @@ function ExportTask:cycle(context)
for key in pairs(entry.filter) do
local items = Milo:getMatches(itemDB:splitKey(key), entry)
for _,item in pairs(items) do
if context.storage:export(node.name, nil, item.count, item) == 0 then
if context.storage:export(node, nil, item.count, item) == 0 then
-- TODO: really shouldn't break here as there may be room in other slots
-- leaving for now for performance reasons
break

View File

@@ -6,7 +6,7 @@ local colors = _G.colors
local device = _G.device
local exportView = UI.Window {
title = 'Export item into machine',
title = 'Export item into inventory',
index = 3,
grid = UI.ScrollingGrid {
x = 2, ex = -6, y = 2, ey = -4,

View File

@@ -44,7 +44,7 @@ function ImportTask:cycle(context)
local function importSlot(slotNo)
local item = node.adapter.getItemMeta(slotNo)
if item and matchesFilter(item) then
context.storage:import(node.name, slotNo, item.count, item)
context.storage:import(node, slotNo, item.count, item)
end
end

View File

@@ -6,7 +6,7 @@ local colors = _G.colors
local device = _G.device
local importView = UI.Window {
title = 'Import item from machine',
title = 'Import item from inventory',
index = 4,
grid = UI.ScrollingGrid {
x = 2, ex = -6, y = 2, ey = -4,

View File

@@ -6,9 +6,9 @@ local InputChest = {
}
function InputChest:cycle(context)
for inventory in context.storage:filterActive('input') do
for slot, item in pairs(inventory.adapter.list()) do
context.storage:import(inventory.name, slot, item.count, item)
for node in context.storage:filterActive('input') do
for slot, item in pairs(node.adapter.list()) do
context.storage:import(node, slot, item.count, item)
end
end
end

View File

@@ -1,9 +1,8 @@
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local context = Milo:getContext()
local turtle = _G.turtle
local turtle = _G.turtle
local learnPage = UI.Dialog {
height = 9, width = UI.term.width - 6,
@@ -39,7 +38,6 @@ function learnPage:enable()
self.grid:setSelected('name', Milo:getState('learnType') or '')
Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' })
sync.lock(turtle)
self:focusFirst()
UI.Dialog.enable(self)
@@ -51,8 +49,8 @@ end
function learnPage:eventHandler(event)
if event.type == 'cancel' then
sync.release(turtle)
Milo:resumeCrafting({ key = 'gridInUse' })
turtle.emptyInventory()
UI:setPreviousPage()
elseif event.type == 'accept' or event.type == 'grid_select' then

View File

@@ -16,7 +16,7 @@ function LimitTask:cycle(context)
local amount = count - res.limit
for _, item in pairs(items) do
amount = amount - context.storage:export(
trashcan.name,
trashcan,
nil,
math.min(amount, item.count),
item)

View File

@@ -1,6 +1,5 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local Util = require('util')
@@ -133,7 +132,6 @@ end
function machineLearnWizard:disable()
Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle)
UI.Page.disable(self)
end

View File

@@ -1,11 +1,9 @@
local Ansi = require('ansi')
local Milo = require('milo')
local Sync = require('sync')
local UI = require('ui')
local colors = _G.colors
local device = _G.device
local turtle = _G.turtle
--[[ Configuration Screen ]]--
local wizardPage = UI.Window {
@@ -57,7 +55,7 @@ function wizardPage:validate()
return self.form:save()
end
UI:getPage('nodeWizard').wizard:add({ manipulator = wizardPage })
--UI:getPage('nodeWizard').wizard:add({ manipulator = wizardPage })
--[[ Task ]]--
local task = {
@@ -72,15 +70,9 @@ function task:cycle(context)
for manipulator in context.storage:filterActive('manipulator', filter) do
for slot, item in pairs(manipulator.adapter.getEnder().list()) do
Sync.sync(turtle, function()
manipulator.adapter.getEnder().pushItems(
context.localName,
slot,
item.count)
Milo:clearGrid()
end)
context.storage:import('joebodo:enderChest', slot, item.count, item)
end
end
end
Milo:registerTask(task)
--Milo:registerTask(task)

View File

@@ -23,7 +23,7 @@ function PotionImportTask:cycle(context)
if not list[5] then
local blazePowder = context.storage.cache[BLAZE_POWDER]
if blazePowder then
context.storage:export(bs.name, 5, 1, blazePowder)
context.storage:export(bs, 5, 1, blazePowder)
else
local item = itemDB:get(BLAZE_POWDER)
if item then
@@ -45,7 +45,7 @@ function PotionImportTask:cycle(context)
for slot = 1, 3 do
if list[slot] then
context.storage:import(bs.name, slot, 1, list[slot])
context.storage:import(bs, slot, 1, list[slot])
end
end
end

View File

@@ -2,10 +2,8 @@ local Event = require('event')
local itemDB = require('itemDB')
local Milo = require('milo')
local Socket = require('socket')
local Sync = require('sync')
local device = _G.device
local turtle = _G.turtle
local SHIELD_SLOT = 2
@@ -61,6 +59,17 @@ local function client(socket)
data = 'ok',
})
local function makeNode(devType)
local devName = user .. ':' .. devType
local adapter = device[devName]
if adapter then
return {
adapter = adapter,
name = devName,
}
end
end
repeat
local data = socket:read()
if not data then
@@ -77,20 +86,20 @@ local function client(socket)
elseif data.request == 'deposit' then
local function deposit()
Sync.sync(turtle, function()
if data.slot == 'shield' then
manipulator.getEquipment().pushItems(
context.localName,
SHIELD_SLOT,
data.count)
else
manipulator.getInventory().pushItems(
context.localName,
data.slot,
data.count)
local devType = 'inventory'
local slotNo = data.slot
if data.slot == 'shield' then
slotNo = SHIELD_SLOT
devType = 'equipment'
end
local node = makeNode(devType)
if node then
local slot = node.adapter.getItemMeta(slotNo)
if slot then
context.storage:import(node, slotNo, slot.count, slot)
end
Milo:clearGrid()
end)
end
end
local list = Milo:listItems()
@@ -114,23 +123,14 @@ local function client(socket)
end
local function transfer(request)
Sync.sync(turtle, function()
local transferred = context.storage:export(
context.localName,
local target = makeNode('inventory')
if target then
context.storage:export(
target,
nil,
request.requested,
data.item)
if transferred > 0 then
turtle.eachFilledSlot(function(slot)
manipulator.getInventory().pullItems(
context.localName,
slot.index,
slot.count)
end)
end
Milo:clearGrid() -- in case all items do not fit in user's inventory
end)
end
end
local request = Milo:makeRequest(data.item, count, transfer)

View File

@@ -1,7 +1,6 @@
local Craft = require('craft2')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local Util = require('util')
@@ -125,7 +124,6 @@ local turtleLearnWizard = UI.Page {
function turtleLearnWizard:disable()
Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle)
UI.Page.disable(self)
end