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

@@ -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