milo performance
This commit is contained in:
@@ -11,64 +11,71 @@ local function filter(a)
|
||||
end
|
||||
|
||||
function ExportTask:cycle(context)
|
||||
for machine in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(machine.exports) do
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(node.exports) do
|
||||
|
||||
local function exportSlot(list, slotNo, item, count)
|
||||
local slot = list[slotNo] or { count = 0 }
|
||||
if not entry.filter then
|
||||
-- exports must have a filter
|
||||
-- TODO: validate in exportView
|
||||
break
|
||||
end
|
||||
|
||||
if slot.count == 0 or
|
||||
(slot.name == item.name and
|
||||
slot.damage == item.damage and
|
||||
slot.nbtHash == item.nbtHash) then
|
||||
local function exportSingleSlot()
|
||||
local slot = node.adapter.getItemMeta(entry.slot)
|
||||
|
||||
local maxCount = itemDB:getMaxCount(item)
|
||||
count = math.min(maxCount - slot.count, count)
|
||||
if slot and slot.count == slot.maxCount then
|
||||
return
|
||||
end
|
||||
|
||||
if count > 0 then
|
||||
-- _debug('attempting to export %s %d into slot %d', item.name, count, slotNo)
|
||||
count = context.storage:export(machine.name, slotNo, count, item)
|
||||
if slot then
|
||||
-- something is in the slot, find what we can export
|
||||
for key in pairs(entry.filter) do
|
||||
local filterItem = Milo:splitKey(key)
|
||||
if (slot.name == filterItem.name and
|
||||
entry.ignoreDamage or slot.damage == filterItem.damage and
|
||||
entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash) then
|
||||
|
||||
if count > 0 then
|
||||
item.count = item.count - count
|
||||
list[slotNo] = {
|
||||
name = item.name,
|
||||
damage = item.damage,
|
||||
nbtHash = item.nbtHash,
|
||||
count = count + slot.count,
|
||||
}
|
||||
return true
|
||||
local items = Milo:getMatches(filterItem, entry)
|
||||
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)
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- slot is empty - export first matching item we have in storage
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(Milo:splitKey(key), entry)
|
||||
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)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local list
|
||||
local function getLazyList()
|
||||
if not list then
|
||||
list = machine.adapter.list()
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
for key in pairs(entry.filter or { }) do
|
||||
local items = Milo:getMatches(Milo:listItems(), itemDB:splitKey(key), entry.ignoreDamage, entry.ignoreNbtHash)
|
||||
for _,item in pairs(items) do
|
||||
if item and item.count > 0 then
|
||||
if type(entry.slot) == 'number' then
|
||||
if exportSlot(getLazyList(), entry.slot, item, item.count) then
|
||||
break
|
||||
end
|
||||
else
|
||||
-- _debug('attempting to export %s %d', item.name, item.count)
|
||||
-- TODO: always going to try and export even if the chest is full
|
||||
if context.storage:export(machine.name, nil, item.count, item) == 0 then
|
||||
break
|
||||
end
|
||||
local function exportItems()
|
||||
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
|
||||
-- TODO: really shouldn't break here as there may be room in other slots
|
||||
-- leaving for now for performance reasons
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(entry.slot) == 'number' then
|
||||
exportSingleSlot()
|
||||
else
|
||||
exportItems()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,8 +10,8 @@ local function filter(a)
|
||||
end
|
||||
|
||||
function ImportTask:cycle(context)
|
||||
for inventory in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(inventory.imports) do
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(node.imports) do
|
||||
|
||||
local function itemMatchesFilter(item)
|
||||
if not entry.ignoreDamage and not entry.ignoreNbtHash then
|
||||
@@ -41,16 +41,16 @@ function ImportTask:cycle(context)
|
||||
end
|
||||
|
||||
local function importSlot(slotNo)
|
||||
local item = inventory.adapter.getItemMeta(slotNo)
|
||||
local item = node.adapter.getItemMeta(slotNo)
|
||||
if item and matchesFilter(item) then
|
||||
context.storage:import(inventory.name, slotNo, item.count, item)
|
||||
context.storage:import(node.name, slotNo, item.count, item)
|
||||
end
|
||||
end
|
||||
|
||||
if type(entry.slot) == 'number' then
|
||||
importSlot(entry.slot)
|
||||
else
|
||||
for i = 1, inventory.adapter.size() do
|
||||
for i in pairs(node.adapter.list()) do
|
||||
importSlot(i)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,16 +9,21 @@ function LimitTask:cycle(context)
|
||||
local trashcan = context.storage:filterActive('trashcan')()
|
||||
|
||||
if trashcan then
|
||||
for k,res in pairs(context.resources) do
|
||||
for key,res in pairs(context.resources) do
|
||||
if res.limit then
|
||||
-- TODO: change to export method of finding items (maybe)
|
||||
local item, count = Milo:getItemWithQty(Milo:splitKey(k), res.ignoreDamage, res.ignoreNbtHash)
|
||||
if item and count > res.limit then
|
||||
context.storage:export(
|
||||
trashcan.name,
|
||||
nil,
|
||||
count - res.limit,
|
||||
item)
|
||||
local items, count = Milo:getMatches(Milo:splitKey(key), res)
|
||||
if count > res.limit then
|
||||
local amount = count - res.limit
|
||||
for _, item in pairs(items) do
|
||||
amount = amount - context.storage:export(
|
||||
trashcan.name,
|
||||
nil,
|
||||
math.min(amount, item.count),
|
||||
item)
|
||||
if amount <= 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
local itemDB = require('itemDB')
|
||||
local Milo = require('milo')
|
||||
|
||||
local ReplenishTask = {
|
||||
@@ -9,30 +8,22 @@ local ReplenishTask = {
|
||||
function ReplenishTask:cycle(context)
|
||||
for k,res in pairs(context.resources) do
|
||||
if res.low then
|
||||
local key = Milo:splitKey(k)
|
||||
local item, count = Milo:getItemWithQty(key, res.ignoreDamage, res.ignoreNbtHash)
|
||||
if not item then
|
||||
item = {
|
||||
damage = key.damage,
|
||||
nbtHash = key.nbtHash,
|
||||
name = key.name,
|
||||
displayName = itemDB:getName(key),
|
||||
count = 0
|
||||
}
|
||||
end
|
||||
local item = Milo:splitKey(k)
|
||||
item.key = k
|
||||
|
||||
local _, count = Milo:getMatches(item, res)
|
||||
|
||||
if count < res.low then
|
||||
local nbtHash = item.nbtHash
|
||||
if res.ignoreNbtHash then
|
||||
nbtHash = nil
|
||||
local nbtHash
|
||||
if not res.ignoreNbtHash then
|
||||
nbtHash = item.nbtHash
|
||||
end
|
||||
Milo:requestCrafting({
|
||||
name = item.name,
|
||||
damage = res.ignoreDamage and 0 or item.damage,
|
||||
nbtHash = nbtHash,
|
||||
requested = res.low - count,
|
||||
count = count,
|
||||
name = item.name,
|
||||
displayName = item.displayName,
|
||||
replenish = true,
|
||||
})
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user