milo wip
This commit is contained in:
@@ -109,7 +109,7 @@ turtle.run(function()
|
|||||||
local facing = scanner.getBlockMeta(0, 0, 0).state.facing
|
local facing = scanner.getBlockMeta(0, 0, 0).state.facing
|
||||||
turtle.point.heading = Point.facings[facing].heading
|
turtle.point.heading = Point.facings[facing].heading
|
||||||
|
|
||||||
turtle.setPolicy('digOnly')
|
--turtle.setPolicy('digOnly')
|
||||||
turtle.setMovementStrategy('goto')
|
turtle.setMovementStrategy('goto')
|
||||||
repeat
|
repeat
|
||||||
local blocks, harvestCount = scan()
|
local blocks, harvestCount = scan()
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ local context = {
|
|||||||
|
|
||||||
learnTypes = { },
|
learnTypes = { },
|
||||||
tasks = { },
|
tasks = { },
|
||||||
|
queue = { },
|
||||||
|
|
||||||
localName = modem.getNameLocal(),
|
localName = modem.getNameLocal(),
|
||||||
storage = Storage(config),
|
storage = Storage(config),
|
||||||
@@ -101,10 +102,10 @@ Milo:clearGrid()
|
|||||||
local page = UI:getPage('listing')
|
local page = UI:getPage('listing')
|
||||||
UI:setPage(page)
|
UI:setPage(page)
|
||||||
|
|
||||||
Event.onInterval(5, function()
|
Event.on('milo_cycle', function()
|
||||||
if not Milo:isCraftingPaused() and context.storage:isOnline() then
|
if not context.turtleBusy then
|
||||||
|
context.turtleBusy = true
|
||||||
Milo:resetCraftingStatus()
|
Milo:resetCraftingStatus()
|
||||||
--Milo:refreshItems()
|
|
||||||
|
|
||||||
for _, task in ipairs(context.tasks) do
|
for _, task in ipairs(context.tasks) do
|
||||||
local s, m = pcall(function() task:cycle(context) end)
|
local s, m = pcall(function() task:cycle(context) end)
|
||||||
@@ -114,6 +115,30 @@ Event.onInterval(5, function()
|
|||||||
error(m)
|
error(m)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
context.turtleBusy = false
|
||||||
|
if not Util.empty(context.queue) then
|
||||||
|
os.queueEvent('milo_queue')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
Event.on('milo_queue', function()
|
||||||
|
if not context.turtleBusy then
|
||||||
|
context.turtleBusy = true
|
||||||
|
|
||||||
|
for _, key in pairs(Util.keys(context.queue)) do
|
||||||
|
local entry = context.queue[key]
|
||||||
|
entry.callback(entry.request)
|
||||||
|
context.queue[key] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context.turtleBusy = false
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
Event.onInterval(5, function()
|
||||||
|
if not Milo:isCraftingPaused() and context.storage:isOnline() then
|
||||||
|
os.queueEvent('milo_cycle')
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -195,12 +195,13 @@ end
|
|||||||
function page:transfer(item, count)
|
function page:transfer(item, count)
|
||||||
local response = self:sendRequest({ request = 'transfer', item = item, count = count })
|
local response = self:sendRequest({ request = 'transfer', item = item, count = count })
|
||||||
if response then
|
if response then
|
||||||
item.count = response.current - response.transferred
|
_debug(response)
|
||||||
|
item.count = response.current - response.count
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
if response.craft > 0 then
|
if response.craft > 0 then
|
||||||
self:setStatus(response.craft .. ' crafting ...')
|
self:setStatus(response.craft .. ' crafting ...')
|
||||||
elseif response.craft + response.available < response.requested then
|
elseif response.craft + response.count < response.requested then
|
||||||
self:setStatus((response.craft + response.available) .. ' available ...')
|
self:setStatus((response.craft + response.count) .. ' available ...')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -318,22 +319,22 @@ if options.slot.value or options.shield.value then
|
|||||||
os.sleep(1.5)
|
os.sleep(1.5)
|
||||||
local neural = device.neuralInterface
|
local neural = device.neuralInterface
|
||||||
if not neural or not neural[inv] then
|
if not neural or not neural[inv] then
|
||||||
_debug('missing Introspection module')
|
_G._debug('missing Introspection module')
|
||||||
end
|
end
|
||||||
|
|
||||||
local method = neural and neural[inv]
|
local method = neural and neural[inv]
|
||||||
local item = method and method().getItemMeta(slotNo)
|
local item = method and method().getItemMeta(slotNo)
|
||||||
if item then
|
if item then
|
||||||
_debug('depositing')
|
|
||||||
local response = page:sendRequest({
|
local response = page:sendRequest({
|
||||||
request = 'deposit',
|
request = 'deposit',
|
||||||
slot = slotValue,
|
slot = slotValue,
|
||||||
|
count = item.count,
|
||||||
key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
|
||||||
})
|
})
|
||||||
if response then
|
if response then
|
||||||
local ritem = page.items[response.key]
|
local ritem = page.items[response.key]
|
||||||
if ritem then
|
if ritem then
|
||||||
ritem.count = response.current
|
ritem.count = response.current + item.count
|
||||||
end
|
end
|
||||||
page.grid:draw()
|
page.grid:draw()
|
||||||
page:sync()
|
page:sync()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ local Craft = require('turtle.craft')
|
|||||||
local itemDB = require('itemDB')
|
local itemDB = require('itemDB')
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
|
|
||||||
|
local os = _G.os
|
||||||
local turtle = _G.turtle
|
local turtle = _G.turtle
|
||||||
|
|
||||||
local Milo = {
|
local Milo = {
|
||||||
@@ -178,25 +179,37 @@ function Milo:getTurtleInventory()
|
|||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function Milo:craftAndEject(item, count)
|
-- queue up an action that reliees on the crafting grid
|
||||||
local provided = self:provideItem(item, count, function(amount)
|
function Milo:queueRequest(request, callback)
|
||||||
-- eject rest when finished crafted
|
if Util.empty(self.context.queue) then
|
||||||
return self:eject(item, amount)
|
os.queueEvent('milo_queue')
|
||||||
end)
|
end
|
||||||
|
table.insert(self.context.queue, {
|
||||||
-- eject what we currently have
|
request = request,
|
||||||
return item.count - self:eject(item, provided.available)
|
callback = callback
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function Milo:provideItem(item, count, callback)
|
function Milo:craftAndEject(item, count)
|
||||||
|
local request = self:makeRequest(item, count, function(request)
|
||||||
|
-- eject rest when finished crafted
|
||||||
|
return self:eject(item, request.count)
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- predict that we will eject that amount
|
||||||
|
return request.current - request.count
|
||||||
|
end
|
||||||
|
|
||||||
|
function Milo:makeRequest(item, count, callback)
|
||||||
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
|
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
|
||||||
|
|
||||||
if count <= 0 then
|
if count <= 0 then
|
||||||
return {
|
return {
|
||||||
requested = 0,
|
requested = 0,
|
||||||
craft = 0,
|
craft = 0,
|
||||||
available = 0,
|
count = 0,
|
||||||
current = current.count,
|
current = current.count,
|
||||||
|
item = item,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -211,20 +224,26 @@ function Milo:provideItem(item, count, callback)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if toCraft > 0 then
|
local request = {
|
||||||
item = Util.shallowCopy(item)
|
|
||||||
item.count = toCraft
|
|
||||||
item.eject = callback
|
|
||||||
self:requestCrafting(item)
|
|
||||||
item.crafted = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
|
||||||
requested = count,
|
requested = count,
|
||||||
craft = toCraft,
|
craft = toCraft,
|
||||||
available = math.min(count, current.count),
|
count = math.min(count, current.count),
|
||||||
current = current.count,
|
current = current.count,
|
||||||
|
item = item,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if request.count > 0 then
|
||||||
|
Milo:queueRequest(request, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
if request.craft > 0 then
|
||||||
|
item = Util.shallowCopy(item)
|
||||||
|
item.count = request.craft
|
||||||
|
item.callback = callback
|
||||||
|
self:requestCrafting(item)
|
||||||
|
end
|
||||||
|
|
||||||
|
return request
|
||||||
end
|
end
|
||||||
|
|
||||||
function Milo:eject(item, count)
|
function Milo:eject(item, count)
|
||||||
|
|||||||
@@ -106,12 +106,8 @@ function craftTask:cycle()
|
|||||||
sync(turtle, function()
|
sync(turtle, function()
|
||||||
self:craft(recipe, item)
|
self:craft(recipe, item)
|
||||||
end)
|
end)
|
||||||
if item.eject and item.crafted >= item.count then
|
if item.callback and item.crafted >= item.count then
|
||||||
if type(item.eject) == 'boolean' then
|
item.callback(item) -- invoke callback
|
||||||
Milo:eject(item, item.count)
|
|
||||||
else
|
|
||||||
item.eject(item.count) -- invoke callback
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
elseif not context.controllerAdapter then
|
elseif not context.controllerAdapter then
|
||||||
item.status = '(no recipe)'
|
item.status = '(no recipe)'
|
||||||
|
|||||||
@@ -119,7 +119,11 @@ function craftPage:eventHandler(event)
|
|||||||
local item = Util.shallowCopy(self.item)
|
local item = Util.shallowCopy(self.item)
|
||||||
item.count = tonumber(self.wizard.pages.quantity.count.value)
|
item.count = tonumber(self.wizard.pages.quantity.count.value)
|
||||||
item.forceCrafting = true
|
item.forceCrafting = true
|
||||||
item.eject = self.wizard.pages.quantity.eject.value == true
|
if self.wizard.pages.quantity.eject.value then
|
||||||
|
item.callback = function(request)
|
||||||
|
Milo:eject(item, request.count)
|
||||||
|
end
|
||||||
|
end
|
||||||
Milo:requestCrafting(item)
|
Milo:requestCrafting(item)
|
||||||
UI:setPreviousPage()
|
UI:setPreviousPage()
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -43,36 +43,33 @@ local function client(socket)
|
|||||||
socket:write(items)
|
socket:write(items)
|
||||||
|
|
||||||
elseif data.request == 'deposit' then
|
elseif data.request == 'deposit' then
|
||||||
if Sync.isLocked(turtle) then
|
local function deposit()
|
||||||
socket:write({ msg = '' })
|
|
||||||
else
|
|
||||||
local count
|
|
||||||
|
|
||||||
Sync.sync(turtle, function()
|
Sync.sync(turtle, function()
|
||||||
if data.slot == 'shield' then
|
if data.slot == 'shield' then
|
||||||
count = manipulator.getEquipment().pushItems(
|
manipulator.getEquipment().pushItems(
|
||||||
context.localName,
|
context.localName,
|
||||||
SHIELD_SLOT,
|
SHIELD_SLOT,
|
||||||
64)
|
data.count)
|
||||||
else
|
else
|
||||||
count = manipulator.getInventory().pushItems(
|
manipulator.getInventory().pushItems(
|
||||||
context.localName,
|
context.localName,
|
||||||
data.slot,
|
data.slot,
|
||||||
64)
|
data.count)
|
||||||
end
|
end
|
||||||
Milo:clearGrid()
|
Milo:clearGrid()
|
||||||
end)
|
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
|
end
|
||||||
|
|
||||||
|
local list = Milo:listItems()
|
||||||
|
local current = list[data.key] and list[data.key].count or 0
|
||||||
|
|
||||||
|
socket:write({
|
||||||
|
key = data.key,
|
||||||
|
current = current,
|
||||||
|
})
|
||||||
|
|
||||||
|
Milo:queueRequest({ }, deposit)
|
||||||
|
|
||||||
elseif data.request == 'transfer' then
|
elseif data.request == 'transfer' then
|
||||||
local count = data.count
|
local count = data.count
|
||||||
|
|
||||||
@@ -83,33 +80,26 @@ local function client(socket)
|
|||||||
count = item and item.count or 0
|
count = item and item.count or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local function transfer(amount)
|
local function transfer(request)
|
||||||
Sync.sync(turtle, function()
|
Sync.sync(turtle, function()
|
||||||
amount = context.storage:export(
|
local transferred = context.storage:export(
|
||||||
context.localName,
|
context.localName,
|
||||||
nil,
|
nil,
|
||||||
amount,
|
request.count,
|
||||||
data.item)
|
data.item)
|
||||||
|
|
||||||
turtle.eachFilledSlot(function(slot)
|
turtle.eachFilledSlot(function(slot)
|
||||||
manipulator.getInventory().pullItems(
|
manipulator.getInventory().pullItems(
|
||||||
context.localName,
|
context.localName,
|
||||||
slot.index,
|
slot.index,
|
||||||
slot.count)
|
transferred)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return amount
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if Sync.isLocked(turtle) then
|
local request = Milo:makeRequest(data.item, count, transfer)
|
||||||
socket:write({ msg = 'Turtle in use. please wait...' })
|
|
||||||
end
|
|
||||||
|
|
||||||
local provided = Milo:provideItem(data.item, count, transfer)
|
socket:write(request)
|
||||||
provided.transferred = provided.available > 0 and transfer(provided.available) or 0
|
|
||||||
|
|
||||||
socket:write(provided)
|
|
||||||
end
|
end
|
||||||
until not socket.connected
|
until not socket.connected
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user