multiMiner
This commit is contained in:
@@ -165,6 +165,10 @@ function itemDB:add(baseItem)
|
|||||||
nItem.displayName = nItem.displayName .. ': ' .. baseItem.media.recordTitle
|
nItem.displayName = nItem.displayName .. ': ' .. baseItem.media.recordTitle
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- turtles / computers / etc
|
||||||
|
elseif baseItem.computer then
|
||||||
|
nItem.displayName = baseItem.computer.label or baseItem.displayName
|
||||||
|
|
||||||
-- potions
|
-- potions
|
||||||
elseif nItem.name == 'minecraft:potion' or nItem.name == 'minecraft:lingering_potion' then
|
elseif nItem.name == 'minecraft:potion' or nItem.name == 'minecraft:lingering_potion' then
|
||||||
if baseItem.effects then
|
if baseItem.effects then
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ local GPS = require('gps')
|
|||||||
local itemDB = require('itemDB')
|
local itemDB = require('itemDB')
|
||||||
local Point = require('point')
|
local Point = require('point')
|
||||||
local Socket = require('socket')
|
local Socket = require('socket')
|
||||||
local sync = require('sync').sync
|
|
||||||
local Util = require('util')
|
local Util = require('util')
|
||||||
local UI = require('ui')
|
local UI = require('ui')
|
||||||
|
|
||||||
@@ -22,25 +21,27 @@ end
|
|||||||
local spt = GPS.getPoint() or error('GPS failure')
|
local spt = GPS.getPoint() or error('GPS failure')
|
||||||
local blockTypes = { } -- blocks types requested to mine
|
local blockTypes = { } -- blocks types requested to mine
|
||||||
local turtles = { }
|
local turtles = { }
|
||||||
local rawBlocks = { } -- scanner data
|
|
||||||
local queue = { } -- actual blocks to mine
|
local queue = { } -- actual blocks to mine
|
||||||
local abort
|
local abort
|
||||||
|
|
||||||
local page = UI.Page {
|
local page = UI.Page {
|
||||||
menuBar = UI.MenuBar {
|
menuBar = UI.MenuBar {
|
||||||
buttons = {
|
buttons = {
|
||||||
{ text = 'Scan', event = 'scan' },
|
{ text = 'Scan', event = 'scan' },
|
||||||
{ text = 'Totals', event = 'totals' },
|
{ text = 'Abort', event = 'abort' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
grid = UI.ScrollingGrid {
|
grid = UI.ScrollingGrid {
|
||||||
y = 2,
|
y = 2, ey = -4,
|
||||||
columns = {
|
columns = {
|
||||||
{ heading = 'Name', key = 'displayName' },
|
{ heading = 'Name', key = 'displayName' },
|
||||||
{ heading = 'Count', key = 'count', width = 5, justify = 'right' },
|
{ heading = 'Count', key = 'count', width = 5, justify = 'right' },
|
||||||
},
|
},
|
||||||
sortColumn = 'displayName',
|
sortColumn = 'displayName',
|
||||||
},
|
},
|
||||||
|
info = UI.Window {
|
||||||
|
y = -3,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local function hijackTurtle(remoteId)
|
local function hijackTurtle(remoteId)
|
||||||
@@ -69,20 +70,28 @@ local function hijackTurtle(remoteId)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function getNextPoint(turtle)
|
local function getNextPoint(turtle)
|
||||||
local pt
|
local pt = Point.closest(turtle.getPoint(), queue)
|
||||||
sync(turtles, function()
|
if pt then
|
||||||
if #queue == 0 then
|
turtle.pt = pt
|
||||||
queue = page:getBlocksToMine() or { }
|
queue[pt.pkey] = nil
|
||||||
end
|
return pt
|
||||||
pt = Point.closest(turtle.getPoint(), queue)
|
end
|
||||||
Util.removeByValue(queue, pt)
|
|
||||||
end)
|
|
||||||
return pt
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function run(id)
|
local function run(id)
|
||||||
Event.addRoutine(function()
|
Event.addRoutine(function()
|
||||||
local turtle = hijackTurtle(id)
|
local turtle = hijackTurtle(id)
|
||||||
|
|
||||||
|
local function emptySlots(retain)
|
||||||
|
local slots = turtle.getFilledSlots()
|
||||||
|
for _,slot in pairs(slots) do
|
||||||
|
if not retain[slot.key] then
|
||||||
|
turtle.select(slot.index)
|
||||||
|
turtle.dropUp(64)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if turtle then
|
if turtle then
|
||||||
turtles[id] = turtle
|
turtles[id] = turtle
|
||||||
|
|
||||||
@@ -97,6 +106,14 @@ local function run(id)
|
|||||||
else
|
else
|
||||||
os.sleep(1)
|
os.sleep(1)
|
||||||
end
|
end
|
||||||
|
if turtle.getItemCount(15) > 0 then
|
||||||
|
emptySlots(blockTypes)
|
||||||
|
turtle.condense()
|
||||||
|
end
|
||||||
|
if turtle.getItemCount(15) > 0 then
|
||||||
|
turtle._goto(spt)
|
||||||
|
emptySlots({ })
|
||||||
|
end
|
||||||
until abort
|
until abort
|
||||||
end
|
end
|
||||||
turtle._goto(spt)
|
turtle._goto(spt)
|
||||||
@@ -104,42 +121,42 @@ local function run(id)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function page:getBlocksToMine()
|
function page.info:draw()
|
||||||
if Util.size(blockTypes) > 0 then
|
self:clear()
|
||||||
self:scan()
|
self:write(2, 1, 'Turtles: ' .. Util.size(turtles))
|
||||||
return Util.reduce(rawBlocks,
|
self:write(2, 2, 'Queue: ' .. Util.size(queue))
|
||||||
function(acc, b)
|
|
||||||
local key = table.concat({ b.name, b.metadata }, ':')
|
|
||||||
if blockTypes[key] then
|
|
||||||
table.insert(acc, b)
|
|
||||||
end
|
|
||||||
end, { })
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function page:scan()
|
function page:scan()
|
||||||
rawBlocks = scanner:scan()
|
|
||||||
local gpt = GPS.getPoint() or error('GPS locate failed')
|
local gpt = GPS.getPoint() or error('GPS locate failed')
|
||||||
|
local rawBlocks = scanner:scan()
|
||||||
|
|
||||||
self.grid:setValues(Util.reduce(rawBlocks,
|
self.totals = Util.reduce(rawBlocks,
|
||||||
function(acc, b)
|
function(acc, b)
|
||||||
local key = table.concat({ b.name, b.metadata }, ':')
|
b.key = table.concat({ b.name, b.metadata }, ':')
|
||||||
local entry = acc[key]
|
local entry = acc[b.key]
|
||||||
if not entry then
|
if not entry then
|
||||||
b.displayName = itemDB:getName(key)
|
b.displayName = itemDB:getName(b.key)
|
||||||
b.count = 1
|
b.count = 1
|
||||||
b.key = key
|
acc[b.key] = b
|
||||||
acc[key] = b
|
|
||||||
else
|
else
|
||||||
entry.count = entry.count + 1
|
entry.count = entry.count + 1
|
||||||
end
|
end
|
||||||
b.x = gpt.x + b.x
|
|
||||||
b.y = gpt.y + b.y
|
|
||||||
b.z = gpt.z + b.z
|
|
||||||
end,
|
|
||||||
{ }))
|
|
||||||
|
|
||||||
self.grid:draw()
|
-- add relevant blocks to queue
|
||||||
|
if blockTypes[b.key] then
|
||||||
|
b.x = gpt.x + b.x
|
||||||
|
b.y = gpt.y + b.y
|
||||||
|
b.z = gpt.z + b.z
|
||||||
|
b.pkey = table.concat({ b.x, b.y, b.z }, ':')
|
||||||
|
if not Util.any(turtles, function(t)
|
||||||
|
return t.pt and t.pt.pkey == b.pkey
|
||||||
|
end) then
|
||||||
|
queue[b.pkey] = b
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
{ })
|
||||||
end
|
end
|
||||||
|
|
||||||
function page.grid:getDisplayValues(row)
|
function page.grid:getDisplayValues(row)
|
||||||
@@ -156,34 +173,58 @@ end
|
|||||||
|
|
||||||
function page:eventHandler(event)
|
function page:eventHandler(event)
|
||||||
if event.type == 'scan' then
|
if event.type == 'scan' then
|
||||||
self:scan()
|
self.grid:setValues(self.totals)
|
||||||
|
self.grid:draw()
|
||||||
|
|
||||||
|
elseif event.type == 'abort' then
|
||||||
|
abort = true
|
||||||
|
|
||||||
elseif event.type == 'grid_select' then
|
elseif event.type == 'grid_select' then
|
||||||
blockTypes[self.grid:getSelected().key] = true
|
local key = self.grid:getSelected().key
|
||||||
|
if blockTypes[key] then
|
||||||
|
for k,v in pairs(queue) do
|
||||||
|
if v.key == key then
|
||||||
|
queue[k] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
blockTypes[key] = nil
|
||||||
|
else
|
||||||
|
blockTypes[self.grid:getSelected().key] = true
|
||||||
|
end
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
UI.Page.eventHandler(self, event)
|
UI.Page.eventHandler(self, event)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Event.onInterval(3, function()
|
||||||
|
page:scan()
|
||||||
|
end)
|
||||||
|
|
||||||
Event.onInterval(1, function()
|
Event.onInterval(1, function()
|
||||||
if not abort then
|
if not abort then
|
||||||
for k,v in pairs(network) do
|
for k,v in pairs(network) do
|
||||||
if v.active and v.distance and v.distance < 16 and not turtles[k] then
|
if v.active and v.distance and v.distance < 16 and not turtles[k] then
|
||||||
turtles[k] = run(k)
|
turtles[k] = run(k)
|
||||||
|
elseif not v.active and turtles[k] then
|
||||||
|
turtles[k] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif Util.size(turtles) == 0 then
|
elseif Util.size(turtles) == 0 then
|
||||||
Event.exitPullEvents()
|
Event.exitPullEvents()
|
||||||
end
|
end
|
||||||
|
page.info:draw()
|
||||||
|
page.info:sync()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
page:scan()
|
page:scan()
|
||||||
|
page.grid:setValues(page.totals)
|
||||||
|
|
||||||
UI:setPage(page)
|
UI:setPage(page)
|
||||||
|
|
||||||
Event.onTerminate(function()
|
Event.onTerminate(function()
|
||||||
abort = true
|
abort = true
|
||||||
spt = GPS.getPoint()
|
spt = Point.above(GPS.getPoint())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
Event.pullEvents()
|
Event.pullEvents()
|
||||||
|
|||||||
Reference in New Issue
Block a user