diff --git a/milo/MiloRemote.lua b/milo/MiloRemote.lua index 8f63f19..d35fef3 100644 --- a/milo/MiloRemote.lua +++ b/milo/MiloRemote.lua @@ -83,6 +83,7 @@ local page = UI.Page { [ 'control-e' ] = 'eject', [ 'control-r' ] = 'refresh', [ 'control-s' ] = 'eject_stack', + [ 'control-a' ] = 'eject_all', [ 'control-1' ] = 'eject_1', [ 'control-2' ] = 'eject_1', [ 'control-3' ] = 'eject_1', @@ -123,6 +124,7 @@ end function page:sendRequest(data) local response +debug(data) sync(self, function() local msg for _ = 1, 2 do @@ -144,7 +146,7 @@ function page:sendRequest(data) end self.notification:error(msg or 'Failed to connect') end) - +debug('got response') return response end @@ -182,7 +184,7 @@ function page:eventHandler(event) local item = self.grid:getSelected() if item then local response = self:sendRequest({ request = 'transfer', item = item, count = 1 }) - item.count = item.count - response.count + item.count = response.count self.grid:draw() end @@ -190,7 +192,15 @@ function page:eventHandler(event) local item = self.grid:getSelected() if item then local response = self:sendRequest({ request = 'transfer', item = item, count = 64 }) - item.count = item.count - response.count + item.count = response.count + self.grid:draw() + end + + elseif event.type == 'eject_all' then + local item = self.grid:getSelected() + if item then + local response = self:sendRequest({ request = 'transfer', item = item, count = item.count }) + item.count = response.count self.grid:draw() end @@ -251,19 +261,22 @@ end debug(options.slot) if options.slot.value then debug('Transfer items initialized') - Event.onInterval(2, function() - local neural = device.neuralInterface - 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() + Event.addRoutine(function() + while true do + os.sleep(1.5) + local neural = device.neuralInterface + 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() + end + else + debug('missing Introspection module') end - else - debug('missing Introspection module') end end) end diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index 97eef95..a81d5f2 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -143,14 +143,45 @@ function Milo:getTurtleInventory() return list end -function Milo:eject(item, qty) - local s, m = pcall(function() - self.context.storage:provide(item, qty) - turtle.emptyInventory() +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) end) - if not s and m then - debug(m) +end + +function Milo:provideItem(item, count, callback) + 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 + toCraft = 0 + else + -- if you ask for 1 stick, getCraftableAmount will return 4 (obviously) + toCraft = math.min(toCraft, Craft.getCraftableAmount(recipe, toCraft, Milo:listItems(), { })) + 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)) + end + + item = Util.shallowCopy(item) + item.count = current.count + toCraft + item.eject = callback + self:requestCrafting(item) + item.crafted = current.count + + return current.count +end + +function Milo:eject(item, count) + count = self.context.storage:provide(item, count) + turtle.emptyInventory() + return count end function Milo:saveMachineRecipe(recipe, result, machine) @@ -175,6 +206,7 @@ function Milo:mergeResources(t) else item = Util.shallowCopy(v) item.count = 0 + item.key = self:uniqueKey(v) table.insert(t, item) end end @@ -185,6 +217,7 @@ function Milo:mergeResources(t) if not item then item = Util.shallowCopy(v) item.count = 0 + item.key = self:uniqueKey(v) table.insert(t, item) end item.has_recipe = true diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index f7c3422..048c86e 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -160,6 +160,7 @@ self.listCount = self.listCount + 1 if not entry then entry = Util.shallowCopy(v) entry.count = v.count + entry.key = key cache[key] = entry table.insert(items, entry) else diff --git a/milo/apis/turtle/craft.lua b/milo/apis/turtle/craft.lua index 6e891a0..c531c7d 100644 --- a/milo/apis/turtle/craft.lua +++ b/milo/apis/turtle/craft.lua @@ -175,6 +175,10 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter, origItem) --end for _, request in pairs(origItem.ingredients) do +if request.pending then + debug('??') + debug(request) +end if request.crafted >= request.count then request.status = nil request.statusCode = Craft.STATUS_SUCCESS diff --git a/milo/plugins/craftTask.lua b/milo/plugins/craftTask.lua index d360810..ca87d97 100644 --- a/milo/plugins/craftTask.lua +++ b/milo/plugins/craftTask.lua @@ -99,11 +99,15 @@ function craftTask:cycle() for _,key in pairs(Util.keys(context.craftingQueue)) do local item = context.craftingQueue[key] if item.count - item.crafted > 0 then - local recipe = Craft.recipes[key] + local recipe = Craft.findRecipe(key) if recipe then self:craft(recipe, item) if item.eject and item.crafted >= item.count then - Milo:eject(item, item.count) + if type(item.eject) == 'boolean' then + Milo:eject(item, item.count) + else + item.eject(item.count, 0) -- unknown amount in system + end end elseif not context.controllerAdapter then item.status = '(no recipe)' diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index 91c1e99..5a5699a 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -91,6 +91,8 @@ local listingPage = UI.Page { q = 'quit', [ 'control-e' ] = 'eject', [ 'control-s' ] = 'eject_stack', + [ 'control-a' ] = 'eject_all', + [ 'control-1' ] = 'eject_1', [ 'control-2' ] = 'eject_1', [ 'control-3' ] = 'eject_1', @@ -144,9 +146,7 @@ function listingPage:eventHandler(event) local item = self.grid:getSelected() if item then queue(function() - Milo:eject(item, 1) - local updated = Milo:getItem(Milo:listItems(), item) - item.count = updated and updated.count or 0 + item.count = Milo:xxx(item, 1) self.grid:draw() end) end @@ -154,7 +154,19 @@ function listingPage:eventHandler(event) elseif event.type == 'eject_stack' then local item = self.grid:getSelected() if item then - queue(function() Milo:eject(item, itemDB:getMaxCount(item)) end) + queue(function() + item.count = Milo:xxx(item, itemDB:getMaxCount(item)) + self.grid:draw() + end) + end + + elseif event.type == 'eject_all' then + local item = self.grid:getSelected() + if item then + local updated = Milo:getItem(Milo:listItems(), item) + if updated then + queue(function() Milo:eject(item, updated.count) end) + end end elseif event.type == 'machines' then @@ -235,9 +247,23 @@ end function listingPage:enable() self:refresh() self:setFocus(self.statusBar.filter) + self.timer = Event.onInterval(5, function() + for _,v in pairs(self.allItems) do + if not v.key then debug(v) error('') end + local c = context.storage.cache[v.key] + v.count = c and c.count or 0 + end + self.grid:draw() + self:sync() + end) UI.Page.enable(self) end +function listingPage:disable() + Event.off(self.timer) + UI.Page.disable(self) +end + function listingPage:refresh() self.allItems = Milo:refreshItems() Milo:mergeResources(self.allItems) diff --git a/milo/plugins/remote.lua b/milo/plugins/remote.lua index 7d2e3f5..e386a17 100644 --- a/milo/plugins/remote.lua +++ b/milo/plugins/remote.lua @@ -59,17 +59,20 @@ debug('remote: ' .. data.request) Milo:clearGrid() elseif data.request == 'transfer' then - local count = context.storage:export( - context.localName, - nil, - data.count, - data.item) - - turtle.eachFilledSlot(function(slot) - manipulator.getInventory().pullItems( + local count = Milo:provideItem(data.item, data.count, function(amount, currentCount) + amount = context.storage:export( context.localName, - slot.index, - slot.count) + nil, + amount, + data.item) + + turtle.eachFilledSlot(function(slot) + manipulator.getInventory().pullItems( + context.localName, + slot.index, + slot.count) + end) + return currentCount - amount end) socket:write({ count = count })