This commit is contained in:
kepler155c
2018-10-31 19:38:54 -04:00
parent 4d3a896d6b
commit 865d642e5c
16 changed files with 276 additions and 234 deletions

View File

@@ -51,18 +51,9 @@ end
local introspectionModule = Peripheral.get('plethora:introspection') or
error('Introspection module not found')
local function loadResources()
local resources = Util.readTable(Milo.RESOURCE_FILE) or { }
for k,v in pairs(resources) do
Util.merge(v, itemDB:splitKey(k))
end
return resources
end
local context = {
config = config,
resources = loadResources(),
resources = Util.readTable(Milo.RESOURCE_FILE) or { },
craftingQueue = { },

View File

@@ -2,12 +2,13 @@ _G.requireInjector(_ENV)
local Event = require('event')
local Socket = require('socket')
local sync = require('sync')
local sync = require('sync').sync
local UI = require('ui')
local Util = require('util')
local colors = _G.colors
local device = _G.device
local os = _G.os
local socket
local SHIELD_SLOT = 2
@@ -139,7 +140,6 @@ function page:sendRequest(data)
local response
sync(self, function()
self:sync()
local msg
for _ = 1, 2 do
if not socket or not socket.connected then
@@ -154,6 +154,10 @@ function page:sendRequest(data)
if socket:write(data) then
response = socket:read(2)
if response then
if response.msg then
self:setStatus(response.msg)
response = nil
end
Event.onTimeout(2, function()
self:setStatus('')
end)
@@ -188,6 +192,20 @@ function page.grid:getDisplayValues(row)
return row
end
function page:transfer(item, count)
local response = self:sendRequest({ request = 'transfer', item = item, count = count })
debug(response)
if response then
item.count = response.current - response.transferred
self.grid:draw()
if response.craft > 0 then
self:setStatus(response.craft .. ' crafting ...')
elseif response.craft + response.available < response.requested then
self:setStatus((response.craft + response.available) .. ' available ...')
end
end
end
function page:eventHandler(event)
if event.type == 'quit' then
UI:exitPullEvents()
@@ -199,33 +217,21 @@ function page:eventHandler(event)
local item = self.grid:getSelected()
if item then
self:setStatus('requesting 1 ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 1 })
if response then
item.count = response.count
self.grid:draw()
end
self:transfer(item, 1)
end
elseif event.type == 'eject_stack' then
local item = self.grid:getSelected()
if item then
self:setStatus('requesting stack ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 'stack' })
if response then
item.count = response.count
self.grid:draw()
end
self:transfer(item, 'stack')
end
elseif event.type == 'eject_all' then
local item = self.grid:getSelected()
if item then
self:setStatus('requesting all ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 'all' })
if response then
item.count = response.count
self.grid:draw()
end
self:transfer(item, 'all')
end
elseif event.type == 'eject_specified' then
@@ -235,9 +241,7 @@ function page:eventHandler(event)
self.statusBar.amount:reset()
self:setFocus(self.statusBar.filter)
self:setStatus('requesting ' .. count .. ' ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = count })
item.count = response.count
self.grid:draw()
self:transfer(item, count)
else
self:setStatus('nope ...')
end
@@ -299,50 +303,43 @@ function page:applyFilter()
self.grid:setValues(t)
end
_G.p4 = Event
if options.slot.value or options.shield.value then
local inv = 'getInventory'
local slotNo = options.slot.value
local slotValue = options.slot.value
if options.shield.value then
slotNo = SHIELD_SLOT
slotValue = 'shield'
inv = 'getEquipment'
end
_debug(options.slot)
if options.slot.value then
_debug('Transfer items initialized')
Event.addRoutine(function()
while true do
_debug('sleeping')
os.sleep(1.5)
local neural = device.neuralInterface
_debug(neural)
if neural and neural.getInventory then
local item = neural.getInventory().getItem(options.slot.value)
if item then
_debug('depositing')
page:sendRequest({ request = 'deposit', slot = options.slot.value })
-- local item =
-- TODO: update count for this one item
-- page.grid:draw() page:sync()
else
_debug('empty')
end
else
if not neural or not neural[inv] then
_debug('missing Introspection module')
end
end
end)
end
if options.shield.value then
_debug('Transfer items initialized')
Event.onInterval(2, function()
local neural = device.neuralInterface
if neural and neural.getEquipment then
local item = neural.getEquipment().getItem(SHIELD_SLOT)
local method = neural and neural[inv]
local item = method and method().getItemMeta(slotNo)
if item then
_debug('depositing')
page:sendRequest({ request = 'deposit', slot = 'shield' })
-- local item =
-- TODO: update count for this one item
-- page.grid:draw() page:sync()
local response = page:sendRequest({
request = 'deposit',
slot = slotValue,
key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
})
if response then
local ritem = page.items[response.key]
if ritem then
ritem.count = response.current
end
page.grid:draw()
page:sync()
end
end
else
_debug('missing Introspection module')
end
end)
end

View File

@@ -64,6 +64,10 @@ function Milo:uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end
function Milo:splitKey(key)
return itemDB:splitKey(key)
end
function Milo:resetCraftingStatus()
self.context.storage.activity = { }
@@ -84,6 +88,10 @@ function Milo:showError(msg)
end
function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
if not ignoreDamage and not ignoreNbtHash then
return items[inItem.key or self:uniqueKey(inItem)]
end
for _,item in pairs(items) do
if item.name == inItem.name and
(ignoreDamage or item.damage == inItem.damage) and
@@ -96,10 +104,9 @@ end
function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
local items = self:listItems()
local item = self:getItem(items, res, ignoreDamage, ignoreNbtHash)
local count = 0
if item and (ignoreDamage or ignoreNbtHash) then
local count = 0
for _,v in pairs(items) do
if item.name == v.name and
(ignoreDamage or item.damage == v.damage) and
@@ -107,10 +114,11 @@ function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
count = count + v.count
end
end
item.count = count
elseif item then
count = item.count
end
return item
return item, count
end
function Milo:getMatches(items, item, ignoreDamage, ignoreNbtHash)
@@ -170,21 +178,29 @@ function Milo:getTurtleInventory()
return list
end
function Milo:xxx(item, count)
return self:provideItem(item, count, function(providable, currentCount)
-- return the current amount in the system
return currentCount - self:eject(item, providable)
function Milo:craftAndEject(item, count)
local provided = self:provideItem(item, count, function(amount)
-- eject rest when finished crafted
return self:eject(item, amount)
end)
-- eject what we currently have
return item.count - self:eject(item, provided.available)
end
function Milo:provideItem(item, count, callback)
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
if count <= 0 then
return 0
return {
requested = 0,
craft = 0,
available = 0,
current = current.count,
}
end
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
local toCraft = count - math.min(current.count, count)
if toCraft > 0 then
local recipe = Craft.findRecipe(self:uniqueKey(item))
if not recipe then
@@ -195,18 +211,20 @@ function Milo:provideItem(item, count, callback)
end
end
if toCraft == 0 then
return callback(math.min(count, current.count), current.count)
-- return current.count - self:eject(item, math.min(count, current.count))
if toCraft > 0 then
item = Util.shallowCopy(item)
item.count = toCraft
item.eject = callback
self:requestCrafting(item)
item.crafted = 0
end
item = Util.shallowCopy(item)
item.count = current.count + toCraft
item.eject = callback
self:requestCrafting(item)
item.crafted = current.count
return current.count
return {
requested = count,
craft = toCraft,
available = math.min(count, current.count),
current = current.count,
}
end
function Milo:eject(item, count)
@@ -230,36 +248,43 @@ function Milo:saveMachineRecipe(recipe, result, machine)
end
function Milo:mergeResources(t)
for _,v in pairs(self.context.resources) do
local item = self:getItem(t, v)
t = Util.shallowCopy(t)
for k,v in pairs(self.context.resources) do
local key = itemDB:splitKey(k)
local item = self:getItem(t, key)
if item then
Util.merge(item, v)
item = Util.shallowCopy(item)
else
item = Util.shallowCopy(v)
item = key
item.count = 0
item.key = self:uniqueKey(v)
-- table.insert(t, item)
t[item.key] = item
item.key = k
end
Util.merge(item, v)
item.resource = v
t[item.key] = item
end
for k in pairs(Craft.recipes) do
local v = itemDB:splitKey(k)
local item = self:getItem(t, v)
if not item then
item = Util.shallowCopy(v)
item = v
item.count = 0
item.key = self:uniqueKey(v)
t[item.key] = item
-- table.insert(t, item)
item.key = k
else
item = Util.shallowCopy(item)
end
t[item.key] = item
item.has_recipe = true
end
for key in pairs(Craft.machineLookup) do
local item = t[key]
if item then
item = Util.shallowCopy(item)
item.is_craftable = true
t[item.key] = item
end
end
@@ -269,38 +294,17 @@ function Milo:mergeResources(t)
end
v.lname = v.displayName:lower()
end
return t
end
function Milo:saveResources()
local t = { }
for k,v in pairs(self.context.resources) do
v = Util.shallowCopy(v)
local keys = Util.transpose({ 'auto', 'low', 'limit',
'ignoreDamage', 'ignoreNbtHash',
'rsControl', 'rsDevice', 'rsSide' })
for _,key in pairs(Util.keys(v)) do
if not keys[key] then
v[key] = nil
end
end
if not Util.empty(v) then
t[k] = v
end
end
Util.writeTable(self.RESOURCE_FILE, t)
Util.writeTable(self.RESOURCE_FILE, self.context.resources)
end
-- Return a list of everything in the system
function Milo:listItems()
return self.context.storage:listItems()
end
-- force a full rescan of chests
function Milo:refreshItems()
return self.context.storage:refresh()
function Milo:listItems(forceRefresh)
return forceRefresh and self.context.storage:refresh() or self.context.storage:listItems()
end
return Milo

View File

@@ -18,6 +18,7 @@ listCount = 0,
storageOnline = true,
hits = 0,
misses = 0,
lastRefresh = os.clock(),
}
Util.merge(self, defaults)
Util.merge(self, args)
@@ -136,6 +137,7 @@ end
function Storage:refresh(throttle)
self.dirty = true
self.lastRefresh = os.clock()
_debug('STORAGE: Forcing full refresh')
for _, adapter in self:onlineAdapters() do
adapter.dirty = true
@@ -266,7 +268,6 @@ function Storage:insert(slot, qty, toSlot, item, source)
local function insert(adapter)
local amount = adapter:insert(slot, qty, toSlot, source or self.localName)
if amount > 0 then
-- TODO: change debug to _debug
_debug('INS: %s(%d): %s[%d] -> %s',
item.name, amount,
source or self.localName, slot, adapter.name)

View File

@@ -2,6 +2,7 @@ local Config = require('config')
local Event = require('event')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local Util = require('util')
@@ -11,8 +12,6 @@ local turtle = _G.turtle
local context = Milo:getContext()
-- TODO: no blacklist for export
local function saveConfig()
local t = { }
for k,v in pairs(context.config.remoteDefaults) do
@@ -272,11 +271,13 @@ function machineWizard.filter:show(entry, callback, whitelistOnly)
self:setFocus(self.form.scan)
Milo:pauseCrafting()
sync.lock(turtle)
end
function machineWizard.filter:hide()
UI.SlideOut.hide(self)
Milo:resumeCrafting()
sync.release(turtle)
end
function machineWizard.filter:resetGrid()

View File

@@ -1,9 +1,11 @@
local Craft = require('turtle.craft')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync').sync
local Util = require('util')
local context = Milo:getContext()
local turtle = _G.turtle
local craftTask = {
name = 'crafting',
@@ -101,12 +103,14 @@ function craftTask:cycle()
if item.count - item.crafted > 0 then
local recipe = Craft.findRecipe(key)
if recipe then
self:craft(recipe, item)
sync(turtle, function()
self:craft(recipe, item)
end)
if item.eject and item.crafted >= item.count then
if type(item.eject) == 'boolean' then
Milo:eject(item, item.count)
else
item.eject(item.count, 0) -- unknown amount in system
item.eject(item.count) -- invoke callback
end
end
elseif not context.controllerAdapter then

View File

@@ -20,11 +20,13 @@ local itemPage = UI.Page {
x = 1, y = 2, height = 10, ex = -1,
[1] = UI.TextEntry {
width = 7,
formLabel = 'Min', formKey = 'low', help = 'Craft if below min'
formLabel = 'Min', formKey = 'low', help = 'Craft if below min',
validate = 'numeric',
},
[2] = UI.TextEntry {
width = 7,
formLabel = 'Max', formKey = 'limit', help = 'Eject if above max'
formLabel = 'Max', formKey = 'limit', help = 'Send to trash if above max',
validate = 'numeric',
},
--[[
[3] = UI.Chooser {
@@ -40,11 +42,11 @@ local itemPage = UI.Page {
]]
[4] = UI.Checkbox {
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
help = 'Ignore damage of item'
help = 'Ignore damage of item',
},
[5] = UI.Checkbox {
formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash',
help = 'Ignore NBT of item'
help = 'Ignore NBT of item',
},
[6] = UI.Button {
x = 2, y = -2, width = 10,
@@ -135,13 +137,15 @@ local itemPage = UI.Page {
event = 'hide_info',
},
},
statusBar = UI.StatusBar { }
statusBar = UI.StatusBar { },
notification = UI.Notification { },
}
function itemPage:enable(item)
self.item = Util.shallowCopy(item)
self.res = item.resource or { }
self.form:setValues(self.item)
self.form:setValues(self.res)
self.titleBar.title = item.displayName or item.name
UI.Page.enable(self)
@@ -232,47 +236,46 @@ function itemPage:eventHandler(event)
elseif event.type == 'hide_info' then
self.info:hide()
elseif event.type == 'form_invalid' then
self.notification:error(event.message)
elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help)
self.statusBar:draw()
elseif event.type == 'form_complete' then
local values = self.form.values
local originalKey = Milo:uniqueKey(self.item)
local item = self.item
local filtered = Util.shallowCopy(values)
filtered.low = tonumber(filtered.low)
filtered.limit = tonumber(filtered.limit)
if self.form:save() then
Util.prune(self.res, function(v)
if type(v) == 'boolean' then
return v
elseif type(v) == 'string' then
return #v > 0
end
return true
end)
if filtered.auto ~= true then
filtered.auto = nil
local newKey = {
name = item.name,
damage = self.res.ignoreDamage and 0 or item.damage,
nbtHash = not self.res.ignoreNbtHash and item.nbtHash or nil,
}
for k,v in pairs(context.resources) do
if v == self.res then
context.resources[k] = nil
break
end
end
if not Util.empty(self.res) then
context.resources[Milo:uniqueKey(newKey)] = self.res
end
Milo:saveResources()
UI:setPreviousPage()
end
if filtered.rsControl ~= true then
filtered.rsControl = nil
filtered.rsSide = nil
filtered.rsDevice = nil
end
if filtered.ignoreDamage == true then
filtered.damage = 0
else
filtered.ignoreDamage = nil
end
if filtered.ignoreNbtHash == true then
filtered.nbtHash = nil
else
filtered.ignoreNbtHash = nil
end
context.resources[originalKey] = nil
context.resources[Milo:uniqueKey(filtered)] = filtered
filtered.count = nil
Milo:saveResources()
-- TODO: min not updated upon save until refresh
UI:setPreviousPage()
else
return UI.Page.eventHandler(self, event)
end

View File

@@ -11,6 +11,8 @@ local context = Milo:getContext()
local mon = Peripheral.lookup(context.config.monitor) or
error('Monitor is not attached')
-- TODO: some way to cancel a job
local jobMonitor = UI.Page {
parent = UI.Device {
device = mon,

View File

@@ -1,7 +1,9 @@
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local context = Milo:getContext()
local turtle = _G.turtle
local learnPage = UI.Dialog {
height = 6, width = UI.term.width - 6,
@@ -34,6 +36,9 @@ function learnPage:enable()
Milo:getState('learnType') or
self.chooser.choices[1].value
Milo:pauseCrafting()
sync.lock(turtle)
self:focusFirst()
UI.Dialog.enable(self)
end
@@ -44,6 +49,8 @@ end
function learnPage:eventHandler(event)
if event.type == 'cancel' then
sync.release(turtle)
Milo:resumeCrafting()
UI:setPreviousPage()
elseif event.type == 'accept' then

View File

@@ -9,14 +9,15 @@ function LimitTask:cycle(context)
local trashcan = context.storage:filterActive('trashcan')()
if trashcan then
for _,res in pairs(context.resources) do
for k,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
-- 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,
item.count - res.limit,
count - res.limit,
item)
end
end

View File

@@ -118,7 +118,7 @@ end
function listingPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.count = row.count > 0 and Util.toBytes(row.count) or ''
row.count = row.count > 0 and Util.toBytes(row.count)
if row.low then
row.low = Util.toBytes(row.low)
end
@@ -139,7 +139,7 @@ function listingPage:eventHandler(event)
local item = self.grid:getSelected()
if item then
queue(function()
item.count = Milo:xxx(item, 1)
item.count = Milo:craftAndEject(item, 1)
self.grid:draw()
end)
end
@@ -148,7 +148,7 @@ function listingPage:eventHandler(event)
local item = self.grid:getSelected()
if item then
queue(function()
item.count = Milo:xxx(item, itemDB:getMaxCount(item))
item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item))
self.grid:draw()
end)
end
@@ -257,12 +257,7 @@ function listingPage:disable()
end
function listingPage:refresh(force)
if force then
self.allItems = Milo:refreshItems()
else
self.allItems = Milo:listItems()
end
Milo:mergeResources(self.allItems)
self.allItems = Milo:mergeResources(Milo:listItems(force))
self:applyFilter()
end

View File

@@ -1,5 +1,6 @@
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local Util = require('util')
@@ -133,13 +134,9 @@ function pages.confirmation:validate()
return true
end
function machineLearnWizard:enable()
Milo:pauseCrafting()
UI.Page.enable(self)
end
function machineLearnWizard:disable()
Milo:resumeCrafting()
sync.release(turtle)
UI.Page.disable(self)
end

View File

@@ -0,0 +1,16 @@
local Milo = require('milo')
-- Do a full scan of inventories every minute
local RefreshTask = {
name = 'refresher',
priority = 0,
}
function RefreshTask:cycle(context)
if os.clock() - context.storage.lastRefresh > 60 then
context.storage:refresh()
end
end
Milo:registerTask(RefreshTask)

View File

@@ -2,6 +2,7 @@ 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
@@ -19,7 +20,7 @@ local function getManipulatorForUser(user)
end
local function client(socket)
_debug('connection from ' .. socket.dhost)
_G._debug('connection from ' .. socket.dhost)
local user = socket:read(2)
if not user then
@@ -36,28 +37,41 @@ local function client(socket)
if not data then
break
end
_debug('remote: ' .. data.request)
if data.request == 'list' then
local items = Milo:listItems()
Milo:mergeResources(items)
local items = Milo:mergeResources(Milo:listItems())
socket:write(items)
elseif data.request == 'deposit' then
local count
if data.slot == 'shield' then
count = manipulator.getEquipment().pushItems(
context.localName,
SHIELD_SLOT,
64)
if Sync.isLocked(turtle) then
socket:write({ msg = '' })
else
count = manipulator.getInventory().pushItems(
context.localName,
data.slot,
64)
local count
Sync.sync(turtle, function()
if data.slot == 'shield' then
count = manipulator.getEquipment().pushItems(
context.localName,
SHIELD_SLOT,
64)
else
count = manipulator.getInventory().pushItems(
context.localName,
data.slot,
64)
end
Milo:clearGrid()
end)
local list = Milo:listItems()
local current = list[data.key] and list[data.key].count or 0
socket:write({
key = data.key,
count = count,
current = current + count,
})
end
socket:write({ count = count })
Milo:clearGrid()
elseif data.request == 'transfer' then
local count = data.count
@@ -69,32 +83,42 @@ _debug('remote: ' .. data.request)
count = item and item.count or 0
end
local provided = Milo:provideItem(data.item, count, function(amount, currentCount)
amount = context.storage:export(
context.localName,
nil,
amount,
data.item)
turtle.eachFilledSlot(function(slot)
manipulator.getInventory().pullItems(
local function transfer(amount)
Sync.sync(turtle, function()
amount = context.storage:export(
context.localName,
slot.index,
slot.count)
end)
return currentCount - amount
end)
nil,
amount,
data.item)
socket:write({ count = provided })
turtle.eachFilledSlot(function(slot)
manipulator.getInventory().pullItems(
context.localName,
slot.index,
slot.count)
end)
end)
return amount
end
if Sync.isLocked(turtle) then
socket:write({ msg = 'Turtle in use. please wait...' })
end
local provided = Milo:provideItem(data.item, count, transfer)
provided.transferred = provided.available > 0 and transfer(provided.available) or 0
socket:write(provided)
end
until not socket.connected
_debug('disconnected from ' .. socket.dhost)
_G._debug('disconnected from ' .. socket.dhost)
end
if device.wireless_modem then
Event.addRoutine(function()
_debug('Milo: listening on port 4242')
_G._debug('Milo: listening on port 4242')
while true do
local socket = Socket.server(4242)
Event.addRoutine(function()

View File

@@ -7,27 +7,29 @@ local ReplenishTask = {
}
function ReplenishTask:cycle(context)
for _,res in pairs(context.resources) do
for k,res in pairs(context.resources) do
if res.low then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash)
local key = Milo:splitKey(k)
local item, count = Milo:getItemWithQty(key, res.ignoreDamage, res.ignoreNbtHash)
if not item then
item = {
damage = res.damage,
nbtHash = res.nbtHash,
name = res.name,
displayName = itemDB:getName(res),
damage = key.damage,
nbtHash = key.nbtHash,
name = key.name,
displayName = itemDB:getName(key),
count = 0
}
end
if item.count < res.low then
if res.ignoreDamage then
item.damage = 0
if count < res.low then
local nbtHash = item.nbtHash
if res.ignoreNbtHash then
nbtHash = nil
end
Milo:requestCrafting({
damage = item.damage,
nbtHash = item.nbtHash,
count = res.low - item.count,
damage = res.ignoreDamage and 0 or item.damage,
nbtHash = nbtHash,
count = res.low - count,
name = item.name,
displayName = item.displayName,
replenish = true,

View File

@@ -1,6 +1,7 @@
local Craft = require('turtle.craft')
local itemDB = require('itemDB')
local Milo = require('milo')
local sync = require('sync')
local UI = require('ui')
local Util = require('util')
@@ -122,13 +123,9 @@ local turtleLearnWizard = UI.Page {
notification = UI.Notification { },
}
function turtleLearnWizard:enable()
Milo:pauseCrafting()
UI.Page.enable(self)
end
function turtleLearnWizard:disable()
Milo:resumeCrafting()
sync.release(turtle)
UI.Page.disable(self)
end