milo storage filter + crafting fixes

This commit is contained in:
kepler155c
2018-10-28 15:24:12 -04:00
parent 9d45460a3b
commit 1c70085450
8 changed files with 177 additions and 168 deletions

View File

@@ -1,49 +1,44 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local device = _G.device
local ExportTask = {
name = 'exporter',
priority = 40,
}
local function filter(a)
return a.exports
end
function ExportTask:cycle(context)
for target, v in pairs(context.config.remoteDefaults) do
if v.exports then
local machine = device[target]
if machine and machine.getItemMeta then
for _, entry in pairs(v.exports) do
local slotNo = type(entry.slot) == 'number' and entry.slot or nil -- '*' indicates any slot
for machine in context.storage:filterActive('machine', filter) do
for _, entry in pairs(machine.exports) do
local slotNo = type(entry.slot) == 'number' and entry.slot or nil -- '*' indicates any slot
local slot = (slotNo and machine.getItemMeta(slotNo)) or { count = 0 }
for key in pairs(entry.filter) do
local item = itemDB:splitKey(key)
local slot = (slotNo and machine.adapter.getItemMeta(slotNo)) or { count = 0 }
for key in pairs(entry.filter or { }) do
local item = itemDB:splitKey(key)
-- is something else is in this slot
if not slot.name or slot.name == item.name then
local maxCount = slot.maxCount or itemDB:getMaxCount(item)
local count = maxCount - slot.count
if not slotNo then
-- TODO: should we just execute export -
-- or scan all slots for space ??
count = machine.size() * maxCount - slot.count
end
if count > 0 then
item = Milo:getItemWithQty(item)
if item and count > 0 then
context.storage:export(
target,
slotNo,
math.min(count, item.count),
item)
end
end
-- is something else is in this slot
if not slot.name or slot.name == item.name then
local maxCount = slot.maxCount or itemDB:getMaxCount(item)
local count = maxCount - slot.count
if not slotNo then
-- TODO: should we just execute export -
-- or scan all slots for space ??
count = machine.adapter.size() * maxCount - slot.count
end
if count > 0 then
item = Milo:getItemWithQty(item)
if item and count > 0 then
context.storage:export(
machine.name,
slotNo,
math.min(count, item.count),
item)
end
end
end
else
debug('Invalid export target: ' .. target)
end
end
end

View File

@@ -1,48 +1,43 @@
local Milo = require('milo')
local device = _G.device
local ImportTask = {
name = 'importer',
priority = 20,
}
local function filter(a)
return a.imports
end
function ImportTask:cycle(context)
for source, v in pairs(context.config.remoteDefaults) do
if v.imports then
local inventory = device[source]
if inventory then
for _, entry in pairs(v.imports) do
for inventory in context.storage:filterActive('machine', filter) do
for _, entry in pairs(inventory.imports) do
local function matchesFilter(item)
if not entry.filter then
return true
end
local key = Milo:uniqueKey(item)
if entry.blacklist then
return not entry.filter[key]
end
return entry.filter[key]
end
local function importSlot(slotNo)
local item = inventory.getItemMeta(slotNo)
if item and matchesFilter(item) then
context.storage:import(source, slotNo, item.count, item)
end
end
if type(entry.slot) == 'number' then
importSlot(entry.slot)
else
for i = 1, inventory.size() do
importSlot(i)
end
end
local function matchesFilter(item)
if not entry.filter then
return true
end
local key = Milo:uniqueKey(item)
if entry.blacklist then
return not entry.filter[key]
end
return entry.filter[key]
end
local function importSlot(slotNo)
local item = inventory.adapter.getItemMeta(slotNo)
if item and matchesFilter(item) then
context.storage:import(inventory.name, slotNo, item.count, item)
end
end
if type(entry.slot) == 'number' then
importSlot(entry.slot)
else
debug('Invalid import source: ' .. source)
for i = 1, inventory.adapter.size() do
importSlot(i)
end
end
end
end

View File

@@ -1,23 +1,14 @@
local Milo = require('milo')
local device = _G.device
local InputChest = {
name = 'input',
priority = 10,
}
function InputChest:cycle(context)
for source,v in pairs(context.config.remoteDefaults) do
if v.mtype == 'input' then
local inventory = device[source]
local list = inventory and inventory.list and inventory.list()
if list then
for slot, item in pairs(list) do
context.storage:import(source, slot, item.count, item)
end
end
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)
end
end
end

View File

@@ -6,28 +6,19 @@ local LimitTask = {
}
function LimitTask:cycle(context)
local trashcan
local trashcan = context.storage:filterActive('trashcan')()
for k,v in pairs(context.config.remoteDefaults) do
if v.mtype == 'trashcan' then
trashcan = k
break
end
end
if not trashcan then
return
end
for _,res in pairs(context.resources) do
if res.limit then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
if item and item.count > res.limit then
context.storage:export(
trashcan,
nil,
item.count - res.limit,
item)
if trashcan then
for _,res in pairs(context.resources) do
if res.limit then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
if item and item.count > res.limit then
context.storage:export(
trashcan.name,
nil,
item.count - res.limit,
item)
end
end
end
end

View File

@@ -1,62 +1,61 @@
local Craft = require('turtle.craft')
local Milo = require('milo')
local device = _G.device
local PotionImportTask = {
name = 'potions',
priority = 30,
brewQueue = { },
}
local function filter(a)
return a.adapter.type == 'minecraft:brewing_stand'
end
function PotionImportTask:cycle(context)
for _, v in pairs(device) do
if v.type == 'minecraft:brewing_stand' then
if v.getBrewTime() == 0 then
local list = v.list()
for bs in context.storage:filterActive('machine', filter) do
if bs.adapter.getBrewTime() == 0 then
local list = bs.adapter.list()
if list[1] and not list[4] then
-- brewing has completd
if list[1] and not list[4] then
-- brewing has completd
if self.brewQueue[v.name] and list[1] then
local key = Milo:uniqueKey(list[1])
if not Craft.findRecipe(key) then
debug('saving new recipe')
Milo:saveMachineRecipe(self.brewQueue[v.name], list[1], v.name)
end
end
for slot = 1, 3 do
if list[slot] then
context.storage:import(v.name, slot, 1, list[slot])
end
if self.brewQueue[bs.name] and list[1] then
local key = Milo:uniqueKey(list[1])
if not Craft.findRecipe(key) then
Milo:saveMachineRecipe(self.brewQueue[bs.name], list[1], bs.name)
end
end
self.brewQueue[v.name] = nil
elseif not self.brewQueue[v.name] then
local recipe = {
count = 3,
ingredients = { },
maxCount = 3,
}
local list = v.list()
local function valid()
for i = 1, 4 do
if not list[i] then
return false
end
for slot = 1, 3 do
if list[slot] then
context.storage:import(bs.name, slot, 1, list[slot])
end
return true
end
end
self.brewQueue[bs.name] = nil
elseif not self.brewQueue[bs.name] then
local recipe = {
count = 3,
ingredients = { },
maxCount = 3,
}
local list = bs.adapter.list()
local function valid()
for i = 1, 4 do
if not list[i] then
return false
end
end
return true
end
if valid() then
for i = 1, 4 do
recipe.ingredients[i] = Milo:uniqueKey(list[i])
end
if valid() then
for i = 1, 4 do
recipe.ingredients[i] = Milo:uniqueKey(list[i])
end
self.brewQueue[v.name] = recipe
end
self.brewQueue[bs.name] = recipe
end
end
end

View File

@@ -9,10 +9,10 @@ local RedstoneTask = {
}
function RedstoneTask:cycle(context)
for _,v in pairs(context.config.remoteDefaults) do
for v in context.storage:filterActive({ 'mtype', 'machine' }) do
if v.redstone then
local ri = device[v.redstone.integrator]
if not ri or not v.adapter or not v.adapter.online then
if not ri or not v.adapter then
debug(v.redstone)
else
local function conditionsSatisfied()