From d50749a7539bc3d6d142e3c10affd46ce88453d7 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sat, 16 Feb 2019 04:35:36 -0500 Subject: [PATCH] finder --- milo/plugins/statsView.lua | 3 +- miners/findSwarm.lua | 197 +++++++++++++++++++++++++++++++++++ neural/autorun/interface.lua | 26 +++++ 3 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 miners/findSwarm.lua diff --git a/milo/plugins/statsView.lua b/milo/plugins/statsView.lua index 8ce680c..c9df94e 100644 --- a/milo/plugins/statsView.lua +++ b/milo/plugins/statsView.lua @@ -123,7 +123,7 @@ local function createPage(node) }, [3] = UI.Tab { tabTitle = 'Storage', - grid = UI.Grid { + grid = UI.ScrollingGrid { y = 2, columns = { { heading = 'Name', key = 'name' }, @@ -151,7 +151,6 @@ local function createPage(node) }, }, }, - timestamp = os.clock(), } local overviewTab = page.tabs[1] diff --git a/miners/findSwarm.lua b/miners/findSwarm.lua new file mode 100644 index 0000000..876049d --- /dev/null +++ b/miners/findSwarm.lua @@ -0,0 +1,197 @@ + +local Event = require('event') +local GPS = require('gps') +local Point = require('point') +local Socket = require('socket') +local Util = require('util') + +local device = _G.device +local os = _G.os + +local gpt = GPS.getPoint() or error('GPS not found') +local scanner = device.neuralInterface +if not scanner or not scanner.scan then + error('Plethora scanner must be equipped') +end + +local paused, abort +local chunkIndex = 0 +local pool = { } +local blocks = Util.transpose({ + 'minecraft:chest', 'minecraft:mob_spawner', 'minecraft:mossy_cobblestone' +}) +local locations = { } + +local function getLocations() + local y = gpt.y - 8 + while y > 5 do + table.insert(locations, y) + y = y - 16 + end + if y > 0 then + table.insert(locations, 5) + end +end + +getLocations() +for _, v in pairs(locations) do + print(v) +end + +for _, b in pairs(scanner.scan()) do + if b.name == 'computercraft:turtle_advanced' or + b.name == 'computercraft:turtle' then + + local pt = { + x = gpt.x + b.x, + y = gpt.y + b.y, + z = gpt.z + b.z, + } + local v = scanner.getBlockMeta(b.x, b.y, b.z) + if v and v.computer then + if not v.computer.isOn then + print('Powered off: ' .. v.computer.id) + elseif v.turtle.fuel < 100 then + print('not enough fuel: ' .. v.computer.id) + else + pt.heading = Point.facings[v.state.facing].heading + + pool[v.computer.id] = { + id = v.computer.id, + label = v.computer.label, + fuel = v.turtle.fuel, + distance = 0, + point = pt, + } + end + end + end +end + +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: ' .. method) + end + return table.unpack(resp) + end + end + + return hijack, socket +end + +local function getNextPoint() + local z = math.floor(chunkIndex / 3) + local x = chunkIndex % 3 + + chunkIndex = chunkIndex + 1 + + while paused do + os.sleep(3) + end + + return { x = gpt.x + (x * 16), y = gpt.y, z = gpt.z + (z * 16) } +end + +local function run(member) + Event.addRoutine(function() + local turtle, socket + local _, m = pcall(function() + member.active = true + turtle, socket = hijackTurtle(member.id) + + if turtle then + if not turtle.has('plethora:module:2') then + error('missing scanner') + end + turtle.reset() + turtle.set({ + attackPolicy = 'attack', + digPolicy = 'turtleSafe', + movementStrategy = 'goto', + point = member.point, + }) + turtle.select(1) + + repeat + local pt = getNextPoint(turtle) + if pt then + member.status = 'digging' + + repeat until turtle._goto(pt) + + for _, v in ipairs(locations) do + if abort then + break + end + turtle.gotoY(v) + turtle.equip('left', 'plethora:module:2') + local found = turtle.scan(blocks) + turtle.equip('left', 'minecraft:diamond_pickaxe') + if Util.size(found) > 0 then + paused = true + print('found spawner') + local b = next(found) + print(string.format('%s:%s:%s'), b.x, b.y, b.z) + print('press r to continue') + end + end + turtle.gotoY(gpt.y) + end + + if member.fuel < 100 then + member.status = 'out of fuel' + break + end + until abort + end + + repeat until turtle.gotoY(gpt.y) + end) + + if m then + _G.printError(m) + end + + pool[member.id] = nil + print('Turtles: ' .. Util.size(pool)) + if Util.size(pool) == 0 then + Event.exitPullEvents() + end + + if socket then + socket:close() + end + end) +end + +print('press a to abort, r to resume') +Event.on('char', function(_, k) + if k == 'r' then + print('Resuming') + paused = false + elseif k == 'a' then + print('Aborting') + abort = true + end +end) + +Util.print('Found %s turtles', Util.size(pool)) +Util.each(pool, function(member) + run(member) +end) + +Event.pullEvents() diff --git a/neural/autorun/interface.lua b/neural/autorun/interface.lua index 942996e..2b3a090 100644 --- a/neural/autorun/interface.lua +++ b/neural/autorun/interface.lua @@ -1,6 +1,8 @@ local GPS = require('gps') +local Util = require('util') local device = _G.device +local turtle = _G.turtle if device.neuralInterface and device.wireless_modem then local ni = require('neural.interface') @@ -27,3 +29,27 @@ if device.neuralInterface and device.wireless_modem then return false, 'No GPS' end end + +if turtle then + function turtle.scan(blocks) + local pt = turtle.point + local scanner = device['plethora:scanner'] or error('Scanner not equipped') + + if not blocks then + return Util.each(scanner:scan(), function(b) + b.x = pt.x + b.x + b.y = pt.y + b.y + b.z = pt.z + b.z + end) + end + + return Util.filter(scanner:scan(), function(b) + if blocks[b.name] then + b.x = pt.x + b.x + b.y = pt.y + b.y + b.z = pt.z + b.z + return true + end + end) + end +end