From dee2b05cfbe05b53587ef9c2a057b10f150154df Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sun, 13 Jan 2019 13:26:19 -0500 Subject: [PATCH] multiMiner + cloud catcher --- core/cedit.lua | 13 +++ milo/Milo.lua | 16 +-- milo/plugins/shopConfig.lua | 12 +-- miners/multiMiner.lua | 189 ++++++++++++++++++++++++++++++++++++ miners/scanningMiner.lua | 10 +- swshop/swshop.lua | 13 ++- swshop/w.lua | 4 +- 7 files changed, 236 insertions(+), 21 deletions(-) create mode 100644 core/cedit.lua create mode 100644 miners/multiMiner.lua diff --git a/core/cedit.lua b/core/cedit.lua new file mode 100644 index 0000000..1098897 --- /dev/null +++ b/core/cedit.lua @@ -0,0 +1,13 @@ +local read = _G.read +local shell = _ENV.shell + +if not _G.cloud_catcher then + print('Paste key: ') + local key = read() + if #key == 0 then + return + end + shell.openHiddenTab('cloud ' .. key) +end + +shell.run('cloud edit ' .. table.unpack({ ... })) diff --git a/milo/Milo.lua b/milo/Milo.lua index 8751596..3d28cb4 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -169,15 +169,17 @@ Event.on({ 'storage_offline', 'storage_online' }, function() end end) +Event.on('terminate', function() + for _, node in pairs(context.storage.nodes) do + if node.category == 'display' and node.adapter and node.adapter.clear then + node.adapter.setBackgroundColor(colors.black) + node.adapter.clear() + end + end +end) + os.queueEvent( context.storage:isOnline() and 'storage_online' or 'storage_offline', context.storage:isOnline()) UI:pullEvents() - -for _, node in pairs(context.storage.nodes) do - if node.category == 'display' and node.adapter and node.adapter.clear then - node.adapter.setBackgroundColor(colors.black) - node.adapter.clear() - end -end diff --git a/milo/plugins/shopConfig.lua b/milo/plugins/shopConfig.lua index 06b23f9..2420e19 100644 --- a/milo/plugins/shopConfig.lua +++ b/milo/plugins/shopConfig.lua @@ -10,7 +10,7 @@ local wizardPage = UI.Window { index = 2, backgroundColor = colors.cyan, form = UI.Form { - x = 2, ex = -2, y = 2, ey = -2, + x = 2, ex = -2, y = 1, ey = -2, manualControls = true, [1] = UI.TextEntry { formLabel = 'Domain', formKey = 'domain', @@ -33,11 +33,6 @@ local wizardPage = UI.Window { shadowText = "xxxx's shop", required = false, }, - warning = UI.Text { - x = 2, y = -1, - textColor = colors.yellow, - value = 'swshop Package must be installed', - }, [4] = UI.Chooser { width = 9, formLabel = 'Font Size', formKey = 'textScale', @@ -48,6 +43,11 @@ local wizardPage = UI.Window { }, help = 'Adjust text scaling', }, + warning = UI.Text { + x = 2, y = -1, + textColor = colors.orange, + value = 'Package swshop must be installed', + }, }, } diff --git a/miners/multiMiner.lua b/miners/multiMiner.lua new file mode 100644 index 0000000..3b3ca74 --- /dev/null +++ b/miners/multiMiner.lua @@ -0,0 +1,189 @@ +local Event = require('event') +local GPS = require('gps') +local itemDB = require('itemDB') +local Point = require('point') +local Socket = require('socket') +local sync = require('sync').sync +local Util = require('util') +local UI = require('ui') + +local colors = _G.colors +local device = _G.device +local network = _G.network +local os = _G.os + +UI:configure('multiMiner', ...) + +local scanner = device.neuralInterface +if not scanner or not scanner.scan then + error('Plethora scanner must be equipped') +end + +local spt = GPS.getPoint() or error('GPS failure') +local blockTypes = { } -- blocks types requested to mine +local turtles = { } +local rawBlocks = { } -- scanner data +local queue = { } -- actual blocks to mine +local abort + +local page = UI.Page { + menuBar = UI.MenuBar { + buttons = { + { text = 'Scan', event = 'scan' }, + { text = 'Totals', event = 'totals' }, + }, + }, + grid = UI.ScrollingGrid { + y = 2, + columns = { + { heading = 'Name', key = 'displayName' }, + { heading = 'Count', key = 'count', width = 5, justify = 'right' }, + }, + sortColumn = 'displayName', + }, +} + +local function hijackTurtle(remoteId) + local socket, msg = Socket.connect(remoteId, 188) + + if not socket then + error(msg) + end + + socket:write('turtle') + local methods = socket:read() + + local hijack = { } + for _,method in pairs(methods) do + hijack[method] = function(...) + socket:write({ fn = method, args = { ... } }) + local resp = socket:read() + if not resp then + error('timed out') + end + return table.unpack(resp) + end + end + + return hijack +end + +local function getNextPoint(turtle) + local pt + sync(turtles, function() + if #queue == 0 then + queue = page:getBlocksToMine() or { } + end + pt = Point.closest(turtle.getPoint(), queue) + Util.removeByValue(queue, pt) + end) + return pt +end + +local function run(id) + Event.addRoutine(function() + local turtle = hijackTurtle(id) + if turtle then + turtles[id] = turtle + + turtle.resetState() + turtle.enableGPS() + turtle.setPolicy('turtleSafe') + + repeat + local pt = getNextPoint(turtle) + if pt then + turtle.digAt(pt, pt.name) + else + os.sleep(1) + end + until abort + end + turtle._goto(spt) + turtles[id] = nil + end) +end + +function page:getBlocksToMine() + if Util.size(blockTypes) > 0 then + self:scan() + return Util.reduce(rawBlocks, + function(acc, b) + local key = table.concat({ b.name, b.metadata }, ':') + if blockTypes[key] then + table.insert(acc, b) + end + end, { }) + end +end + +function page:scan() + rawBlocks = scanner:scan() + local gpt = GPS.getPoint() or error('GPS locate failed') + + self.grid:setValues(Util.reduce(rawBlocks, + function(acc, b) + local key = table.concat({ b.name, b.metadata }, ':') + local entry = acc[key] + if not entry then + b.displayName = itemDB:getName(key) + b.count = 1 + b.key = key + acc[key] = b + else + entry.count = entry.count + 1 + end + b.x = gpt.x + b.x + b.y = gpt.y + b.y + b.z = gpt.z + b.z + end, + { })) + + self.grid:draw() +end + +function page.grid:getDisplayValues(row) + row = Util.shallowCopy(row) + row.count = Util.toBytes(row.count) + return row +end + +function page.grid:getRowTextColor(row, selected) + return blockTypes[row.key] and + colors.yellow or + UI.Grid.getRowTextColor(self, row, selected) +end + +function page:eventHandler(event) + if event.type == 'scan' then + self:scan() + + elseif event.type == 'grid_select' then + blockTypes[self.grid:getSelected().key] = true + self.grid:draw() + end + + UI.Page.eventHandler(self, event) +end + +Event.onInterval(1, function() + if not abort then + for k,v in pairs(network) do + if v.active and v.distance and v.distance < 16 and not turtles[k] then + turtles[k] = run(k) + end + end + elseif Util.size(turtles) == 0 then + Event.exitPullEvents() + end +end) + +page:scan() +UI:setPage(page) + +Event.onTerminate(function() + abort = true + spt = GPS.getPoint() +end) + +Event.pullEvents() diff --git a/miners/scanningMiner.lua b/miners/scanningMiner.lua index bdc732f..fce08b9 100644 --- a/miners/scanningMiner.lua +++ b/miners/scanningMiner.lua @@ -38,6 +38,8 @@ local MIN_FUEL = 7500 local LOW_FUEL = 1500 local MAX_FUEL = turtle.getFuelLimit() +local HOME_PT = { x = 0, y = 0, z = 0 } + local DICTIONARY_FILE = 'usr/config/mining.dictionary' local PROGRESS_FILE = 'usr/config/scanning_mining.progress' local STARTUP_FILE = 'usr/autorun/scanningMiner.lua' @@ -104,6 +106,7 @@ local page = UI.Page { statusBar = UI.StatusBar { columns = { { key = 'status' }, + { key = 'distance', width = 4 }, { key = 'fuel', width = 6 }, }, }, @@ -475,7 +478,7 @@ local function mineChunk() if turtle.getFuelLevel() < LOW_FUEL then refuel() - local veryMinFuel = Point.turtleDistance(turtle.point, { x = 0, y = 0, z = 0 }) + 512 + local veryMinFuel = Point.turtleDistance(turtle.point, HOME_PT) + 512 if turtle.getFuelLevel() < veryMinFuel then error('Not enough fuel to continue') end @@ -571,6 +574,7 @@ Event.addRoutine(function() turtle.setMoveCallback(function() page.statusBar:setValue('fuel', Util.toBytes(turtle.getFuelLevel())) + page.statusBar:setValue('distance', math.floor(Point.distance(turtle.point, HOME_PT))) page.statusBar:draw() page:sync() end) @@ -600,7 +604,7 @@ Event.addRoutine(function() { x = mining.x, y = 0, z = mining.z } ) local maxDistance = Point.distance( - { x = 0, y = 0, z = 0 }, + HOME_PT, { x = mining.x + 16, y = 0, z = mining.z + 16 } ) @@ -640,7 +644,7 @@ Event.addRoutine(function() status(success and 'finished' or turtle.isAborted() and 'aborting' or 'error') turtle.gotoY(0) - if turtle._goto({ x = 0, y = 0, z = 0 }) then + if turtle._goto(HOME_PT) then unload() end turtle.reset() diff --git a/swshop/swshop.lua b/swshop/swshop.lua index 495e463..5fa2c17 100644 --- a/swshop/swshop.lua +++ b/swshop/swshop.lua @@ -121,7 +121,14 @@ local function connect() assert(success, "Failed to subscribe to event") end -jua.go(function() - print("Ready") - connect() +local s, m = pcall(function() + jua.go(function() + print("Ready") + connect() + end) end) + +rs.setOutput('top', false) +if not s then + error(m) +end diff --git a/swshop/w.lua b/swshop/w.lua index 084133d..1b33f4a 100644 --- a/swshop/w.lua +++ b/swshop/w.lua @@ -32,8 +32,8 @@ end local function findID(url) local found = gfind(url, idPatt) local id = tonumber(found[#found]:sub(found[#found]:find("%d+"))) - _debug('id: ') - _debug(id) + --_debug('id: ') + --_debug(id) return id end