From 442f1c4d47a1fc16335d88fa588fd88b647cfb49 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 26 Feb 2019 09:47:44 -0500 Subject: [PATCH] util refactor + milo bulk crafting --- core/Follow.lua | 7 +++++-- core/apis/swarm.lua | 10 ++++++---- core/multiMiner.lua | 1 + farms/superTreefarm.lua | 2 ++ milo/apis/craft2.lua | 40 ++++++++++++++++++++++---------------- milo/plugins/statsView.lua | 15 ++++++++++++-- neural/Scanner.lua | 1 + 7 files changed, 51 insertions(+), 25 deletions(-) diff --git a/core/Follow.lua b/core/Follow.lua index d3f6d71..49b32b6 100644 --- a/core/Follow.lua +++ b/core/Follow.lua @@ -89,6 +89,7 @@ local function follow(member) end member.snmp = Socket.connect(member.id, 161) + member.snmp.co = coroutine.running() local pt @@ -109,8 +110,10 @@ end function swarm:onRemove(member, status, message) if member.socket then - member.turtle.set({ status = 'idle' }) - member.turtle.abort(true) + pcall(function() + member.turtle.set({ status = 'idle' }) + member.turtle.abort(true) + end) end if member.snmp then member.snmp:close() diff --git a/core/apis/swarm.lua b/core/apis/swarm.lua index 45ad47b..ee49e2a 100644 --- a/core/apis/swarm.lua +++ b/core/apis/swarm.lua @@ -1,7 +1,7 @@ local class = require('class') local Event = require('event') +local Map = require('map') local Socket = require('socket') -local Util = require('util') local function hijackTurtle(remoteId) local socket, msg = Socket.connect(remoteId, 188) @@ -10,8 +10,10 @@ local function hijackTurtle(remoteId) error(msg) end + socket.co = coroutine.running() + socket:write('turtle') - local methods = socket:read() + local methods = socket:read() or error('Timed out') local hijack = { } for _,method in pairs(methods) do @@ -31,11 +33,11 @@ end local Swarm = class() function Swarm:init(args) self.pool = { } - Util.merge(self, args) + Map.merge(self, args) end function Swarm:add(id, args) - local member = Util.shallowCopy(args or { }) + local member = Map.shallowCopy(args or { }) member.id = id self.pool[id] = member end diff --git a/core/multiMiner.lua b/core/multiMiner.lua index 7dfc2e9..6cd2ce7 100644 --- a/core/multiMiner.lua +++ b/core/multiMiner.lua @@ -320,6 +320,7 @@ function page:scan() else queue[b.pkey] = nil end + return acc end, { }) diff --git a/farms/superTreefarm.lua b/farms/superTreefarm.lua index c6d8862..a0a4f2a 100644 --- a/farms/superTreefarm.lua +++ b/farms/superTreefarm.lua @@ -481,6 +481,7 @@ local function findDroppedSaplings() b.sapling = true acc[makeKey(b)] = b end + return acc end, { }) return sensed @@ -502,6 +503,7 @@ local function scan(pt, filter, blocks) acc[makeKey(b)] = b end end + return acc end, blocks or { }) end diff --git a/milo/apis/craft2.lua b/milo/apis/craft2.lua index bba15d9..0f90b14 100644 --- a/milo/apis/craft2.lua +++ b/milo/apis/craft2.lua @@ -96,28 +96,32 @@ local function machineCraft(recipe, storage, machineName, request, count, item) end end - local xferred = { } - for k,v in pairs(recipe.ingredients) do - local provided = storage:export(machine, k, count, splitKey(v)) - xferred[k] = { - key = v, - count = provided, - } - if provided ~= count then - -- take back out whatever we put in - for k2,v2 in pairs(xferred) do - if v2.count > 0 then - storage:import(machine, k2, v2.count, splitKey(v2.key)) + local pending = item.pending[recipe.result] or 0 + + if count > 0 then + local xferred = { } + for k,v in pairs(recipe.ingredients) do + local provided = storage:export(machine, k, count, splitKey(v)) + xferred[k] = { + key = v, + count = provided, + } + if provided ~= count then + -- take back out whatever we put in + for k2,v2 in pairs(xferred) do + if v2.count > 0 then + storage:import(machine, k2, v2.count, splitKey(v2.key)) + end end + request.status = 'Invalid recipe' + request.statusCode = Craft.STATUS_ERROR + return end - request.status = 'Invalid recipe' - request.statusCode = Craft.STATUS_ERROR - return end end request.status = 'processing' request.statusCode = Craft.STATUS_INFO - item.pending[recipe.result] = count * recipe.count + item.pending[recipe.result] = pending + (count * recipe.count) end local function turtleCraft(recipe, storage, request, count) @@ -202,12 +206,14 @@ end function Craft.craftRecipeInternal(recipe, count, storage, origItem) local request = origItem.ingredients[recipe.result] + --[[ if origItem.pending[recipe.result] then request.status = 'processing' request.statusCode = Craft.STATUS_INFO return 0 end - + --]] + count = count - (origItem.pending[recipe.result] or 0) local canCraft = Craft.getCraftableAmount(recipe, count, origItem.ingredients) if not origItem.forceCrafting and canCraft == 0 then return 0 diff --git a/milo/plugins/statsView.lua b/milo/plugins/statsView.lua index 5035d35..2b25cc5 100644 --- a/milo/plugins/statsView.lua +++ b/milo/plugins/statsView.lua @@ -310,7 +310,18 @@ Unlocked Slots : %d of %d (%d%%) self.onlineLabel.value = string.format('Storage Status: (%s chests)', stats.totalChests) - self.crafting.progressColor = Milo:isCraftingPaused() and colors.yellow or colors.green + local total, crafted = 0, 0 + for _,v in pairs(context.craftingQueue) do + total = total + v.requested + crafted = crafted + v.crafted + end + if Milo:isCraftingPaused() then + self.crafting.progressColor = colors.yellow + self.crafting.value = 100 + else + self.crafting.progressColor = colors.green + self.crafting.value = total > 0 and math.ceil(crafted / total * 100) or 0 + end local percent = math.floor(stats.usedSlots / stats.totalSlots * 100) local color = colors.green @@ -338,7 +349,7 @@ Unlocked Slots : %d of %d (%d%%) self.unlockedLabel.value = string.format('Unlocked Usage: %s%% (%s of %s slots)', percent, stats.usedUnlockedSlots, stats.unlockedSlots) - UI.Tab.draw(self) + UI.Tab.draw(self) end function overviewTab:enable() diff --git a/neural/Scanner.lua b/neural/Scanner.lua index 5e9d9f2..3d6e332 100644 --- a/neural/Scanner.lua +++ b/neural/Scanner.lua @@ -81,6 +81,7 @@ function page:scan() acc[b.key] = entry end throttle() + return acc end, { }))