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

@@ -47,7 +47,7 @@ if nodes.nodes then
input = 'custom', input = 'custom',
trashcan = 'custom', trashcan = 'custom',
machine = 'machine', machine = 'machine',
brewingStand = 'custom', brewingStand = 'machine',
activity = 'display', activity = 'display',
jobs = 'display', jobs = 'display',
ignore = 'ignore', ignore = 'ignore',
@@ -105,6 +105,8 @@ if not device.workbench then
end end
end end
local localName = modem.getNameLocal()
local context = { local context = {
nodes = nodes, nodes = nodes,
resources = Util.readTable(Milo.RESOURCE_FILE) or { }, resources = Util.readTable(Milo.RESOURCE_FILE) or { },
@@ -115,20 +117,28 @@ local context = {
tasks = { }, tasks = { },
queue = { }, queue = { },
localName = modem.getNameLocal(),
storage = Storage(nodes), storage = Storage(nodes),
turtleInventory = introspection.getInventory(), turtleInventory = {
name = localName,
mtype = 'hidden',
adapter = introspection.getInventory(),
}
} }
device[context.localName] = introspection.getInventory() nodes[localName] = context.turtleInventory
nodes[localName].adapter.name = localName
_G._p = context --debug _G._p = context --debug
Milo:init(context) Milo:init(context)
context.storage:initStorage() context.storage:initStorage()
-- TODO: fix
context.storage.turtleInventory = context.turtleInventory
local function loadDirectory(dir) local function loadDirectory(dir)
for _, file in pairs(fs.list(dir)) do for _, file in pairs(fs.list(dir)) do
_debug('loading: ' .. file)
local s, m = Util.run(_ENV, fs.combine(dir, file)) local s, m = Util.run(_ENV, fs.combine(dir, file))
if not s and m then if not s and m then
error(m or 'Unknown error') error(m or 'Unknown error')
@@ -190,6 +200,12 @@ Event.on('milo_queue', function()
end end
end) end)
Event.on('turtle_inventory', function()
if not processing and not Milo:isCraftingPaused() then
Milo:clearGrid()
end
end)
Event.onInterval(5, function() Event.onInterval(5, function()
if not Milo:isCraftingPaused() then if not Milo:isCraftingPaused() then
os.queueEvent('milo_cycle') os.queueEvent('milo_cycle')

View File

@@ -35,17 +35,14 @@ local function makeRecipeKey(item)
end end
function Craft.clearGrid(storage) function Craft.clearGrid(storage)
turtle.eachFilledSlot(function(slot) local success = true
storage:import(storage.localName, slot.index, slot.count, slot)
end)
for i = 1, 16 do for index, slot in pairs(storage.turtleInventory.adapter.list()) do
if turtle.getItemCount(i) ~= 0 then if storage:import(storage.turtleInventory, index, slot.count, slot) ~= slot.count then
return false success = false
end end
end end
return success
return true
end end
function Craft.getItemCount(items, item) function Craft.getItemCount(items, item)
@@ -77,7 +74,7 @@ function Craft.sumIngredients(recipe)
end end
local function machineCraft(recipe, storage, machineName, request, count, item) local function machineCraft(recipe, storage, machineName, request, count, item)
local machine = device[machineName] local machine = storage.nodes[machineName]
if not machine then if not machine then
request.status = 'machine not found' request.status = 'machine not found'
request.statusCode = Craft.STATUS_ERROR request.statusCode = Craft.STATUS_ERROR
@@ -85,7 +82,7 @@ local function machineCraft(recipe, storage, machineName, request, count, item)
end end
for k in pairs(recipe.ingredients) do for k in pairs(recipe.ingredients) do
if machine.getItemMeta(k) then if machine.adapter.getItemMeta(k) then
request.status = 'machine in use' request.status = 'machine in use'
request.statusCode = Craft.STATUS_WARNING request.statusCode = Craft.STATUS_WARNING
return return
@@ -94,7 +91,7 @@ local function machineCraft(recipe, storage, machineName, request, count, item)
local xferred = { } local xferred = { }
for k,v in pairs(recipe.ingredients) do for k,v in pairs(recipe.ingredients) do
local provided = storage:export(machineName, k, count, splitKey(v)) local provided = storage:export(machine, k, count, splitKey(v))
xferred[k] = { xferred[k] = {
key = v, key = v,
count = provided, count = provided,
@@ -103,7 +100,7 @@ local function machineCraft(recipe, storage, machineName, request, count, item)
-- take back out whatever we put in -- take back out whatever we put in
for k2,v2 in pairs(xferred) do for k2,v2 in pairs(xferred) do
if v2.count > 0 then if v2.count > 0 then
storage:import(machineName, k2, v2.count, splitKey(v2.key)) storage:import(machine, k2, v2.count, splitKey(v2.key))
end end
end end
request.status = 'Invalid recipe' request.status = 'Invalid recipe'
@@ -125,7 +122,7 @@ local function turtleCraft(recipe, storage, request, count)
for k,v in pairs(recipe.ingredients) do for k,v in pairs(recipe.ingredients) do
local item = splitKey(v) local item = splitKey(v)
if storage:export(storage.localName, k, count, item) ~= count then if storage:export(storage.turtleInventory, k, count, item) ~= count then
request.status = 'unknown error' request.status = 'unknown error'
request.statusCode = Craft.STATUS_ERROR request.statusCode = Craft.STATUS_ERROR
return return

View File

@@ -134,8 +134,8 @@ end
function Milo:getTurtleInventory() function Milo:getTurtleInventory()
local list = { } local list = { }
for i = 1,16 do for i in pairs(self.context.turtleInventory.adapter.list()) do
local item = self.context.turtleInventory.getItemMeta(i) local item = self.context.turtleInventory.adapter.getItemMeta(i)
if item and not itemDB:get(item) then if item and not itemDB:get(item) then
itemDB:add(item) itemDB:add(item)
end end
@@ -225,7 +225,7 @@ function Milo:makeRequest(item, count, callback)
end end
function Milo:eject(item, count) function Milo:eject(item, count)
count = self.context.storage:export(self.context.storage.localName, nil, count, item) count = self.context.storage:export(self.context.turtleInventory, nil, count, item)
turtle.emptyInventory() turtle.emptyInventory()
return count return count
end end

View File

@@ -1,9 +1,8 @@
local class = require('class') local class = require('class')
local Event = require('event') local Event = require('event')
local InventoryAdapter = require('inventoryAdapter') local Adapter = require('inventoryAdapter')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Peripheral = require('peripheral') local Util = require('util')
local Util = require('util')
local device = _G.device local device = _G.device
local os = _G.os local os = _G.os
@@ -20,9 +19,6 @@ function Storage:init(nodes)
} }
Util.merge(self, defaults) Util.merge(self, defaults)
local modem = Peripheral.get('wired_modem') or error('Wired modem not attached')
self.localName = modem.getNameLocal()
Event.on({ 'device_attach', 'device_detach' }, function(e, dev) Event.on({ 'device_attach', 'device_detach' }, function(e, dev)
_G._debug('%s: %s', e, tostring(dev)) _G._debug('%s: %s', e, tostring(dev))
self:initStorage() self:initStorage()
@@ -34,9 +30,13 @@ end
function Storage:showStorage() function Storage:showStorage()
local t = { } local t = { }
local ignores = {
ignore = true,
hidden = true,
}
for k,v in pairs(self.nodes) do for k,v in pairs(self.nodes) do
local online = v.adapter and v.adapter.online local online = v.adapter and v.adapter.online
if not online and v.mtype ~= 'ignore' then if not online and not ignores[v.mtype] then
table.insert(t, k) table.insert(t, k)
end end
end end
@@ -58,18 +58,20 @@ function Storage:initStorage()
_G._debug('Initializing storage') _G._debug('Initializing storage')
for k,v in pairs(self.nodes) do for k,v in pairs(self.nodes) do
if v.adapter then if v.mtype ~= 'hidden' then
v.adapter.online = not not device[k] if v.adapter then
elseif device[k] and device[k].list and device[k].size and device[k].pullItems then v.adapter.online = not not device[k]
v.adapter = InventoryAdapter.wrap({ side = k }) elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
v.adapter.online = true v.adapter = Adapter.wrap({ side = k })
v.adapter.dirty = true v.adapter.online = true
elseif device[k] then v.adapter.dirty = true
v.adapter = device[k] elseif device[k] then
v.adapter.online = true v.adapter = device[k]
end v.adapter.online = true
if v.mtype == 'storage' then end
online = online and not not (v.adapter and v.adapter.online) if v.mtype == 'storage' then
online = online and not not (v.adapter and v.adapter.online)
end
end end
end end
@@ -247,6 +249,10 @@ function Storage:updateCache(adapter, item, count)
end end
function Storage:_sn(name) function Storage:_sn(name)
if not name then
error('Invalid target', 3)
end
local node = self.nodes[name] local node = self.nodes[name]
if node and node.displayName then if node and node.displayName then
return node.displayName return node.displayName
@@ -258,16 +264,58 @@ function Storage:_sn(name)
return table.concat(t, '_') return table.concat(t, '_')
end end
local function isValidTransfer(adapter, target)
for _,v in pairs(adapter.getTransferLocations()) do
if v == target then
return true
end
end
end
local function rawExport(source, target, item, qty, slot)
local total = 0
local push = isValidTransfer(source, target.name)
local s, m = pcall(function()
local stacks = source.list()
for key,stack in Util.rpairs(stacks) do
if stack.name == item.name and
stack.damage == item.damage and
stack.nbtHash == item.nbtHash then
local amount = math.min(qty, stack.count)
if amount > 0 then
if push then
amount = source.pushItems(target.name, key, amount, slot)
else
amount = target.pullItems(source.name, key, amount, slot)
end
end
qty = qty - amount
total = total + amount
if qty <= 0 then
break
end
end
end
end)
if not s and m then
_debug(m)
end
return total, m
end
function Storage:export(target, slot, count, item) function Storage:export(target, slot, count, item)
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
local function provide(adapter) local function provide(adapter)
local amount = adapter:provide(item, count, slot, target) local amount = rawExport(adapter, target.adapter, item, count, slot)
if amount > 0 then if amount > 0 then
_G._debug('EXT: %s(%d): %s -> %s%s', _G._debug('EXT: %s(%d): %s -> %s%s',
item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target), item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target.name),
slot and string.format('[%d]', slot) or '[*]') slot and string.format('[%d]', slot) or '[*]')
self:updateCache(adapter, item, -amount) self:updateCache(adapter, item, -amount)
@@ -287,7 +335,7 @@ function Storage:export(target, slot, count, item)
end end
_G._debug('MISS: %s(%d): %s%s %s', _G._debug('MISS: %s(%d): %s%s %s',
item.displayName or item.name, count, self:_sn(target), item.displayName or item.name, count, self:_sn(target.name),
slot and string.format('[%d]', slot) or '[*]', key) slot and string.format('[%d]', slot) or '[*]', key)
-- TODO: If there are misses when a slot is specified than something is wrong... -- TODO: If there are misses when a slot is specified than something is wrong...
@@ -298,6 +346,23 @@ function Storage:export(target, slot, count, item)
return total return total
end end
local function rawInsert(source, target, slot, qty)
local count = 0
local s, m = pcall(function()
if isValidTransfer(source, target.name) then
count = source.pullItems(target.name, slot, qty)
else
count = target.pushItems(source.name, slot, qty)
end
end)
if not s and m then
_debug(m)
end
return count
end
function Storage:import(source, slot, count, item) function Storage:import(source, slot, count, item)
if not source then error('Storage:import: source is required') end if not source then error('Storage:import: source is required') end
if not slot then error('Storage:import: slot is required') end if not slot then error('Storage:import: slot is required') end
@@ -316,19 +381,19 @@ function Storage:import(source, slot, count, item)
entry = itemDB:add(item) entry = itemDB:add(item)
else else
-- get the metadata from the device and add to db -- get the metadata from the device and add to db
entry = itemDB:add(device[source].getItemMeta(slot)) entry = itemDB:add(source.adapter.getItemMeta(slot))
end end
itemDB:flush() itemDB:flush()
end end
item = entry item = entry
local function insert(adapter) local function insert(adapter)
local amount = adapter:insert(slot, count, nil, source) local amount = rawInsert(adapter, source.adapter, slot, count)
if amount > 0 then if amount > 0 then
_G._debug('INS: %s(%d): %s[%d] -> %s', _G._debug('INS: %s(%d): %s[%d] -> %s',
item.displayName or item.name, amount, item.displayName or item.name, amount,
self:_sn(source), slot, self:_sn(adapter.name)) self:_sn(source.name), slot, self:_sn(adapter.name))
self:updateCache(adapter, item, amount) self:updateCache(adapter, item, amount)
@@ -384,9 +449,9 @@ function Storage:trash(source, slot, count)
local trashcan = Util.find(self.nodes, 'mtype', 'trashcan') local trashcan = Util.find(self.nodes, 'mtype', 'trashcan')
if trashcan and trashcan.adapter and trashcan.adapter.online then if trashcan and trashcan.adapter and trashcan.adapter.online then
_G._debug('TRA: %s[%d] (%d)', self:_sn(source), slot, count or 64) _G._debug('TRA: %s[%d] (%d)', self:_sn(source.name), slot, count or 64)
return trashcan.adapter.pullItems(source, slot, count) return trashcan.adapter.pullItems(source.name, slot, count)
end end
return 0 return 0
end end

View File

@@ -2,7 +2,6 @@ local Config = require('config')
local Event = require('event') local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -48,8 +47,7 @@ local networkPage = UI.Page {
{ key = 'suffix', width = 4, justify = 'right' }, { key = 'suffix', width = 4, justify = 'right' },
{ heading = 'Name', key = 'displayName' }, { heading = 'Name', key = 'displayName' },
{ heading = 'Type', key = 'mtype', width = 4 }, { heading = 'Type', key = 'mtype', width = 4 },
{ heading = 'Cat', key = 'category', width = 3 }, { heading = 'Pri', key = 'priority', width = 3 },
--{ heading = 'Pri', key = 'priority', width = 3 },
}, },
sortColumn = 'displayName', sortColumn = 'displayName',
help = 'Select Node', help = 'Select Node',
@@ -136,7 +134,6 @@ function networkPage:enable()
self:getList() self:getList()
self:applyFilter() self:applyFilter()
self.grid:draw() self.grid:draw()
self.grid:sync()
updateStatus() updateStatus()
self:sync() self:sync()
end) end)
@@ -331,13 +328,11 @@ function nodeWizard.filter:show(entry, callback, whitelistOnly)
self:setFocus(self.form.scan) self:setFocus(self.form.scan)
Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' }) Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' })
sync.lock(turtle)
end end
function nodeWizard.filter:hide() function nodeWizard.filter:hide()
UI.SlideOut.hide(self) UI.SlideOut.hide(self)
Milo:resumeCrafting({ key = 'gridInUse' }) Milo:resumeCrafting({ key = 'gridInUse' })
sync.release(turtle)
end end
function nodeWizard.filter:resetGrid() function nodeWizard.filter:resetGrid()

View File

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

View File

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

View File

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

View File

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

View File

@@ -44,7 +44,7 @@ function ImportTask:cycle(context)
local function importSlot(slotNo) local function importSlot(slotNo)
local item = node.adapter.getItemMeta(slotNo) local item = node.adapter.getItemMeta(slotNo)
if item and matchesFilter(item) then 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
end end

View File

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

View File

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

View File

@@ -1,9 +1,8 @@
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local context = Milo:getContext() local context = Milo:getContext()
local turtle = _G.turtle local turtle = _G.turtle
local learnPage = UI.Dialog { local learnPage = UI.Dialog {
height = 9, width = UI.term.width - 6, height = 9, width = UI.term.width - 6,
@@ -39,7 +38,6 @@ function learnPage:enable()
self.grid:setSelected('name', Milo:getState('learnType') or '') self.grid:setSelected('name', Milo:getState('learnType') or '')
Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' }) Milo:pauseCrafting({ key = 'gridInUse', msg = 'Crafting paused' })
sync.lock(turtle)
self:focusFirst() self:focusFirst()
UI.Dialog.enable(self) UI.Dialog.enable(self)
@@ -51,8 +49,8 @@ end
function learnPage:eventHandler(event) function learnPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
sync.release(turtle)
Milo:resumeCrafting({ key = 'gridInUse' }) Milo:resumeCrafting({ key = 'gridInUse' })
turtle.emptyInventory()
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'accept' or event.type == 'grid_select' then 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 local amount = count - res.limit
for _, item in pairs(items) do for _, item in pairs(items) do
amount = amount - context.storage:export( amount = amount - context.storage:export(
trashcan.name, trashcan,
nil, nil,
math.min(amount, item.count), math.min(amount, item.count),
item) item)

View File

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

View File

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

View File

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

View File

@@ -2,10 +2,8 @@ local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local Socket = require('socket') local Socket = require('socket')
local Sync = require('sync')
local device = _G.device local device = _G.device
local turtle = _G.turtle
local SHIELD_SLOT = 2 local SHIELD_SLOT = 2
@@ -61,6 +59,17 @@ local function client(socket)
data = 'ok', 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 repeat
local data = socket:read() local data = socket:read()
if not data then if not data then
@@ -77,20 +86,20 @@ local function client(socket)
elseif data.request == 'deposit' then elseif data.request == 'deposit' then
local function deposit() local function deposit()
Sync.sync(turtle, function() local devType = 'inventory'
if data.slot == 'shield' then local slotNo = data.slot
manipulator.getEquipment().pushItems( if data.slot == 'shield' then
context.localName, slotNo = SHIELD_SLOT
SHIELD_SLOT, devType = 'equipment'
data.count) end
else
manipulator.getInventory().pushItems( local node = makeNode(devType)
context.localName, if node then
data.slot, local slot = node.adapter.getItemMeta(slotNo)
data.count) if slot then
context.storage:import(node, slotNo, slot.count, slot)
end end
Milo:clearGrid() end
end)
end end
local list = Milo:listItems() local list = Milo:listItems()
@@ -114,23 +123,14 @@ local function client(socket)
end end
local function transfer(request) local function transfer(request)
Sync.sync(turtle, function() local target = makeNode('inventory')
local transferred = context.storage:export( if target then
context.localName, context.storage:export(
target,
nil, nil,
request.requested, request.requested,
data.item) data.item)
end
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) local request = Milo:makeRequest(data.item, count, transfer)

View File

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