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

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

View File

@@ -134,8 +134,8 @@ end
function Milo:getTurtleInventory()
local list = { }
for i = 1,16 do
local item = self.context.turtleInventory.getItemMeta(i)
for i in pairs(self.context.turtleInventory.adapter.list()) do
local item = self.context.turtleInventory.adapter.getItemMeta(i)
if item and not itemDB:get(item) then
itemDB:add(item)
end
@@ -225,7 +225,7 @@ function Milo:makeRequest(item, count, callback)
end
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()
return count
end

View File

@@ -1,9 +1,8 @@
local class = require('class')
local Event = require('event')
local InventoryAdapter = require('inventoryAdapter')
local itemDB = require('itemDB')
local Peripheral = require('peripheral')
local Util = require('util')
local class = require('class')
local Event = require('event')
local Adapter = require('inventoryAdapter')
local itemDB = require('itemDB')
local Util = require('util')
local device = _G.device
local os = _G.os
@@ -20,9 +19,6 @@ function Storage:init(nodes)
}
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)
_G._debug('%s: %s', e, tostring(dev))
self:initStorage()
@@ -34,9 +30,13 @@ end
function Storage:showStorage()
local t = { }
local ignores = {
ignore = true,
hidden = true,
}
for k,v in pairs(self.nodes) do
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)
end
end
@@ -58,18 +58,20 @@ function Storage:initStorage()
_G._debug('Initializing storage')
for k,v in pairs(self.nodes) do
if v.adapter then
v.adapter.online = not not device[k]
elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
v.adapter = InventoryAdapter.wrap({ side = k })
v.adapter.online = true
v.adapter.dirty = true
elseif device[k] then
v.adapter = device[k]
v.adapter.online = true
end
if v.mtype == 'storage' then
online = online and not not (v.adapter and v.adapter.online)
if v.mtype ~= 'hidden' then
if v.adapter then
v.adapter.online = not not device[k]
elseif device[k] and device[k].list and device[k].size and device[k].pullItems then
v.adapter = Adapter.wrap({ side = k })
v.adapter.online = true
v.adapter.dirty = true
elseif device[k] then
v.adapter = device[k]
v.adapter.online = true
end
if v.mtype == 'storage' then
online = online and not not (v.adapter and v.adapter.online)
end
end
end
@@ -247,6 +249,10 @@ function Storage:updateCache(adapter, item, count)
end
function Storage:_sn(name)
if not name then
error('Invalid target', 3)
end
local node = self.nodes[name]
if node and node.displayName then
return node.displayName
@@ -258,16 +264,58 @@ function Storage:_sn(name)
return table.concat(t, '_')
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)
local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
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
_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 '[*]')
self:updateCache(adapter, item, -amount)
@@ -287,7 +335,7 @@ function Storage:export(target, slot, count, item)
end
_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)
-- 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
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)
if not source then error('Storage:import: source 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)
else
-- 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
itemDB:flush()
end
item = entry
local function insert(adapter)
local amount = adapter:insert(slot, count, nil, source)
local amount = rawInsert(adapter, source.adapter, slot, count)
if amount > 0 then
_G._debug('INS: %s(%d): %s[%d] -> %s',
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)
@@ -384,9 +449,9 @@ function Storage:trash(source, slot, count)
local trashcan = Util.find(self.nodes, 'mtype', 'trashcan')
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
return 0
end