multiMiner + cloud catcher

This commit is contained in:
kepler155c@gmail.com
2019-01-13 13:26:19 -05:00
parent 91d434f93d
commit dee2b05cfb
7 changed files with 236 additions and 21 deletions

13
core/cedit.lua Normal file
View File

@@ -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({ ... }))

View File

@@ -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

View File

@@ -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',
},
},
}

189
miners/multiMiner.lua Normal file
View File

@@ -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()

View File

@@ -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()

View File

@@ -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

View File

@@ -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