turtle status
This commit is contained in:
@@ -11,12 +11,12 @@ local network = _G.network
|
||||
local os = _G.os
|
||||
|
||||
local swarm = Swarm()
|
||||
local gpt = GPS.getPoint()
|
||||
local gpt = GPS.getPoint() or error('GPS not found')
|
||||
local pts, blocks
|
||||
|
||||
local page = UI.Page {
|
||||
mode = UI.Chooser {
|
||||
x = 18,
|
||||
x = 13, y = -1,
|
||||
choices = {
|
||||
{ name = 'No breaking', value = 'digNone' },
|
||||
{ name = 'Destructive', value = 'turtleSafe' },
|
||||
@@ -24,7 +24,7 @@ local page = UI.Page {
|
||||
value = 'digNone',
|
||||
},
|
||||
grid = UI.ScrollingGrid {
|
||||
y = 2,
|
||||
y = 2, ey = -2,
|
||||
columns = {
|
||||
{ heading = 'Label', key = 'label' },
|
||||
{ heading = 'Dist', key = 'distance' },
|
||||
@@ -58,7 +58,7 @@ function page:enable()
|
||||
local function update()
|
||||
local t = { }
|
||||
for _,v in pairs(network) do
|
||||
if v.fuel and v.active then
|
||||
if v.fuel and v.active and v.fuel > 0 and v.distance then
|
||||
table.insert(t, v)
|
||||
end
|
||||
end
|
||||
@@ -82,6 +82,7 @@ local function follow(member)
|
||||
turtle.reset()
|
||||
turtle.set({
|
||||
digPolicy = page.mode.value,
|
||||
status = 'Following',
|
||||
})
|
||||
|
||||
if not turtle.enableGPS(nil, true) then
|
||||
@@ -107,10 +108,20 @@ local function follow(member)
|
||||
end
|
||||
end
|
||||
|
||||
function swarm:onRemove(member)
|
||||
if member.socket then
|
||||
member.turtle.set({ status = 'idle' })
|
||||
end
|
||||
end
|
||||
|
||||
function page:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
swarm:add(event.selected.id, { })
|
||||
swarm:run(follow)
|
||||
if not swarm.pool[event.selected.id] then
|
||||
swarm:add(event.selected.id)
|
||||
swarm:run(follow)
|
||||
else
|
||||
swarm:remove(event.selected.id)
|
||||
end
|
||||
self.grid:draw()
|
||||
|
||||
elseif event.type == 'choice_change' then
|
||||
@@ -128,7 +139,7 @@ end
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
local pt = GPS.getPoint()
|
||||
if pt then
|
||||
if pt and not Point.same(pt, gpt) then
|
||||
gpt = pt
|
||||
pts = {
|
||||
{ x = pt.x + 2, z = pt.z, y = pt.y },
|
||||
@@ -148,7 +159,7 @@ Event.addRoutine(function()
|
||||
|
||||
-- don't run into player
|
||||
addBlocks(pt)
|
||||
addBlocks({ x = pt.x, z = pt.z, y = pt.y + 1 })
|
||||
addBlocks(Point.above(pt))
|
||||
|
||||
for _, member in pairs(swarm.pool) do
|
||||
if member.snmp then
|
||||
@@ -167,4 +178,5 @@ UI:pullEvents()
|
||||
|
||||
for _, member in pairs(swarm.pool) do
|
||||
member.snmp:write({ type = 'scriptEx', args = 'turtle.abort(true)' })
|
||||
member.snmp:close()
|
||||
end
|
||||
|
||||
@@ -35,32 +35,43 @@ function Swarm:init(args)
|
||||
end
|
||||
|
||||
function Swarm:add(id, args)
|
||||
local member = Util.shallowCopy(args)
|
||||
local member = Util.shallowCopy(args or { })
|
||||
member.id = id
|
||||
self.pool[id] = member
|
||||
end
|
||||
|
||||
function Swarm:remove(id, s, m)
|
||||
local member = self.pool[id]
|
||||
if member then
|
||||
self.pool[id] = nil
|
||||
self:onRemove(member, s, m)
|
||||
if member.socket then
|
||||
member.socket:close()
|
||||
member.socket = nil
|
||||
end
|
||||
if member.handler then
|
||||
Event.terminate(member.handler)
|
||||
member.handler = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Swarm:run(fn)
|
||||
for id, member in pairs(self.pool) do
|
||||
if not member.socket then
|
||||
Event.addRoutine(function()
|
||||
member.handler = Event.addRoutine(function()
|
||||
local s, m = pcall(function()
|
||||
member.turtle, member.socket = hijackTurtle(id)
|
||||
|
||||
fn(member)
|
||||
end)
|
||||
if member.socket then
|
||||
member.socket:close()
|
||||
member.socket = nil
|
||||
end
|
||||
self.pool[id] = nil
|
||||
self:onRemove(member, s, m)
|
||||
self:remove(id, s, m)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Swarm:shutdown()
|
||||
function Swarm:stop()
|
||||
for _, member in pairs(self.pool) do
|
||||
if member.socket then
|
||||
member.socket:close()
|
||||
|
||||
@@ -101,13 +101,19 @@ local function run(member)
|
||||
repeat
|
||||
local pt = getNextPoint(member)
|
||||
if pt then
|
||||
turtle.set({ status = 'Relocating' })
|
||||
turtle.go({ y = pt.y })
|
||||
local c = os.clock()
|
||||
while not turtle.go(pt) do
|
||||
if abort then
|
||||
break
|
||||
end
|
||||
os.sleep(.5)
|
||||
if os.clock() - c > 3 then
|
||||
turtle.set({ status = 'Stuck' })
|
||||
end
|
||||
end
|
||||
turtle.set({ status = 'Boring' })
|
||||
|
||||
for _, v in ipairs(locations) do
|
||||
if abort then
|
||||
@@ -132,15 +138,18 @@ local function run(member)
|
||||
end
|
||||
until abort
|
||||
|
||||
turtle.set({ status = 'Aborting' })
|
||||
turtle.go({ y = gpt.y + member.index })
|
||||
turtle.go({ x = gpt.x, y = gpt.y + member.index, z = gpt.z })
|
||||
|
||||
repeat until turtle.go({ y = gpt.y })
|
||||
turtle.set({ status = 'idle' })
|
||||
end
|
||||
|
||||
function swarm:onRemove(member, success, message)
|
||||
if not success then
|
||||
Sound.play('entity.villager.no')
|
||||
print('Removed from swarm: ' .. member.id)
|
||||
_G.printError(message)
|
||||
end
|
||||
|
||||
|
||||
@@ -302,7 +302,7 @@ function page:scan()
|
||||
table.insert(candidates, b)
|
||||
end
|
||||
|
||||
if b.name == 'minecraft:chest' then
|
||||
if b.name == 'minecraft:chest' or b.name:find('shulker') then
|
||||
chestPoint = b
|
||||
end
|
||||
|
||||
|
||||
@@ -1,24 +1,9 @@
|
||||
{
|
||||
[ "shootingGallery" ] = {
|
||||
title = "Gallery",
|
||||
category = "Neural",
|
||||
run = "shootingGallery.lua",
|
||||
},
|
||||
[ "neuralFight" ] = {
|
||||
title = "Fight",
|
||||
category = "Neural",
|
||||
run = "neuralFight.lua",
|
||||
},
|
||||
[ "elytraFly" ] = {
|
||||
title = "ElytraFly",
|
||||
category = "Neural",
|
||||
run = "elytraFly.lua",
|
||||
},
|
||||
[ "neuralRemote" ] = {
|
||||
title = "Remote",
|
||||
category = "Neural",
|
||||
run = "neuralRemote.lua",
|
||||
},
|
||||
[ "Sensor" ] = {
|
||||
title = "Sensor",
|
||||
category = "Neural",
|
||||
|
||||
Reference in New Issue
Block a user