reorganization

This commit is contained in:
kepler155c
2017-09-15 02:31:11 -04:00
parent c2491f53a8
commit d45b375678
17 changed files with 3350 additions and 0 deletions

1
etc/scripts/abort Normal file
View File

@@ -0,0 +1 @@
turtle.abortAction()

113
etc/scripts/follow Normal file
View File

@@ -0,0 +1,113 @@
local function follow(id)
requireInjector(getfenv(1))
local Event = require('event')
local Point = require('point')
local Socket = require('socket')
turtle.status = 'follow ' .. id
if not turtle.enableGPS() then
error('turtle: No GPS found')
end
local socket = Socket.connect(id, 161)
if not socket then
error('turtle: Unable to connect to ' .. id)
return
end
local lastPoint
local following = false
Event.on('turtle_follow', function(_, pt)
local pts = {
{ x = pt.x + 2, z = pt.z, y = pt.y },
{ x = pt.x - 2, z = pt.z, y = pt.y },
{ x = pt.x, z = pt.z + 2, y = pt.y },
{ x = pt.x, z = pt.z - 2, y = pt.y },
}
local cpt = Point.closest(turtle.point, pts)
local blocks = { }
local function addBlocks(tpt)
table.insert(blocks, tpt)
local apts = Point.adjacentPoints(tpt)
for _,apt in pairs(apts) do
table.insert(blocks, apt)
end
end
-- don't run into player
addBlocks(pt)
addBlocks({ x = pt.x, z = pt.z, y = pt.y + 1 })
if turtle.pathfind(cpt, { blocks = blocks }) then
turtle.headTowards(pt)
end
following = false
end)
Event.onInterval(.5, function()
local function getRemotePoint()
if not turtle.abort then
if socket:write({ type = 'gps' }) then
return socket:read(3)
end
end
end
-- sometimes gps will fail if moving
local pt, d
for i = 1, 3 do
pt, d = getRemotePoint()
if pt then
break
end
os.sleep(.5)
end
if not pt or turtle.abort then
error('Did not receive GPS location')
end
if not lastPoint or (lastPoint.x ~= pt.x or lastPoint.y ~= pt.y or lastPoint.z ~= pt.z) then
if following then
turtle.abort = true
while following do
os.sleep(.1)
end
turtle.abort = false
end
-- check if gps is inaccurate (player moving too fast)
if d < Point.pythagoreanDistance(turtle.point, pt) + 10 then
lastPoint = Point.copy(pt)
following = true
os.queueEvent('turtle_follow', pt)
end
end
end)
Event.on('turtle_abort', function()
Event.exitPullEvents()
end)
Event.pullEvents()
socket:close()
return true
end
local s, m = turtle.run(function() follow({COMPUTER_ID}) end)
if not s and m then
error(m)
end

1
etc/scripts/goHome Normal file
View File

@@ -0,0 +1 @@
turtle.run(turtle.gotoGPSHome)

85
etc/scripts/level Normal file
View File

@@ -0,0 +1,85 @@
requireInjector(getfenv(1))
local Point = require('point')
local Util = require('util')
local checkedNodes = { }
local nodes = { }
local box = { x = 65, ex = 69, y = 65, ey = 70, z = -23, ez = -19 }
local function inBox(pt, box)
return pt.x >= box.x and
pt.y >= box.y and
pt.z >= box.z and
pt.x <= box.ex and
pt.y <= box.ey and
pt.z <= box.ez
end
local function toKey(pt)
return table.concat({ pt.x, pt.y, pt.z }, ':')
end
local function addNode(node)
for i = 0, 5 do
local hi = turtle.getHeadingInfo(i)
local testNode = { x = node.x + hi.xd, y = node.y + hi.yd, z = node.z + hi.zd }
if inBox(testNode, box) then
local key = toKey(testNode)
if not checkedNodes[key] then
nodes[key] = testNode
end
end
end
end
local function dig(facing)
local direction = facing
if direction == 'forward' then
direction = turtle.getHeadingInfo(turtle.point.heading).direction
end
local hi = turtle.getHeadingInfo(direction)
local node = { x = turtle.point.x + hi.xd, y = turtle.point.y + hi.yd, z = turtle.point.z + hi.zd }
if inBox(node, box) then
if turtle.getAction(facing).dig() then
addNode(node)
end
end
end
local function level()
repeat
local node = { x = turtle.point.x, y = turtle.point.y, z = turtle.point.z }
local key = toKey(node)
checkedNodes[key] = true
nodes[key] = nil
dig('down')
dig('up')
dig('forward')
print(string.format('%d nodes remaining', Util.size(nodes)))
if Util.size(nodes) == 0 then
break
end
local node = Point.closest(turtle.point, nodes)
if not turtle.gotoPoint(node) then
break
end
until turtle.abort
end
local pt = Util.shallowCopy(turtle.point)
turtle.setPolicy(turtle.policies.none)
if turtle.pathfind({ x = 65, y = 70, z = -23 }) then
--turtle.reset()
turtle.setPolicy(turtle.policies.digOnly)
level()
end
turtle.pathfind(pt)
--local s, m = turtle.run(level)

29
etc/scripts/moveTo Normal file
View File

@@ -0,0 +1,29 @@
turtle.run(function()
requireInjector(getfenv(1))
local GPS = require('gps')
local Socket = require('socket')
local id = {COMPUTER_ID}
if not turtle.enableGPS() then
error('turtle: No GPS found')
end
local socket = Socket.connect(id, 161)
if not socket then
error('turtle: Unable to connect to ' .. id)
end
socket:write({ type = 'gps' })
local pt = socket:read(3)
if not pt then
error('turtle: No GPS response')
end
if not turtle.pathfind(pt) then
error('Unable to go to location')
end
end)

102
etc/scripts/obsidian Normal file
View File

@@ -0,0 +1,102 @@
requireInjector(getfenv(1))
local Point = require('point')
local Util = require('util')
local checkedNodes, nodes
local function addNode(node)
for i = 0, 3 do
local hi = turtle.getHeadingInfo(i)
local testNode = { x = node.x + hi.xd, z = node.z + hi.zd }
local key = table.concat({ testNode.x, testNode.z }, ':')
if not checkedNodes[key] then
nodes[key] = testNode
end
end
end
local function findObsidian()
repeat
local node = { x = turtle.point.x, z = turtle.point.z }
local key = table.concat({ node.x, node.z }, ':')
checkedNodes[key] = true
nodes[key] = nil
local _,b = turtle.inspectDown()
if b and (b.name == 'minecraft:lava' or b.name == 'minecraft:flowing_lava') then
if turtle.select('minecraft:water_bucket') then
while true do
if turtle.up() then
break
end
print('stuck')
end
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
_, b = turtle.inspectDown()
end
end
if turtle.getCount(16) > 0 then
print('Inventory full')
print('Enter to continue...')
read()
end
if b and b.name == 'minecraft:obsidian' then
turtle.digDown()
addNode(node)
else
turtle.digDown()
end
print(string.format('%d nodes remaining', Util.size(nodes)))
if Util.size(nodes) == 0 then
break
end
local node = Point.closest(turtle.point, nodes)
if not turtle.gotoPoint(node) then
break
end
until turtle.abort
end
turtle.reset()
turtle.setPolicy(turtle.policies.digOnly)
local s, m = turtle.run(function()
repeat
checkedNodes = { }
nodes = { }
local _,b = turtle.inspectDown()
if not b or b.name ~= 'minecraft:obsidian' then
break
end
findObsidian()
if not turtle.select('minecraft:water_bucket') then
break
end
turtle.goto(0, 0)
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
until turtle.abort
end)
turtle.goto(0, 0, 0, 0)
turtle.reset()
if not s and m then
error(m)
end

1
etc/scripts/reboot Normal file
View File

@@ -0,0 +1 @@
os.reboot()

1
etc/scripts/setHome Normal file
View File

@@ -0,0 +1 @@
turtle.run(turtle.setGPSHome)

1
etc/scripts/shutdown Normal file
View File

@@ -0,0 +1 @@
os.shutdown()

74
etc/scripts/summon Normal file
View File

@@ -0,0 +1,74 @@
local function summon(id)
requireInjector(getfenv(1))
local GPS = require('gps')
local Point = require('point')
local Socket = require('socket')
turtle.status = 'GPSing'
turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0 })
local pts = {
[ 1 ] = { x = 0, z = 0, y = 0 },
[ 2 ] = { x = 4, z = 0, y = 0 },
[ 3 ] = { x = 2, z = -2, y = 2 },
[ 4 ] = { x = 2, z = 2, y = 2 },
}
local tFixes = { }
local socket = Socket.connect(id, 161)
if not socket then
error('turtle: Unable to connect to ' .. id)
end
local function getDistance()
socket:write({ type = 'ping' })
local _, d = socket:read(5)
return d
end
local function doGPS()
tFixes = { }
for i = 1, 4 do
if not turtle.gotoPoint(pts[i]) then
error('turtle: Unable to perform GPS maneuver')
end
local distance = getDistance()
if not distance then
error('turtle: No response from ' .. id)
end
table.insert(tFixes, {
position = vector.new(turtle.point.x, turtle.point.y, turtle.point.z),
distance = distance
})
end
return true
end
if not doGPS() then
turtle.turnAround()
turtle.setPoint({ x = 0, y = 0, z = 0, heading = 0})
if not doGPS() then
socket:close()
return false
end
end
socket:close()
local pos = GPS.trilaterate(tFixes)
if pos then
local pt = { x = pos.x, y = pos.y, z = pos.z }
local _, h = Point.calculateMoves(turtle.getPoint(), pt)
local hi = turtle.getHeadingInfo(h)
turtle.status = 'recalling'
turtle.pathfind({ x = pt.x - hi.xd, z = pt.z - hi.zd, y = pt.y - hi.yd, heading = h })
else
error("turtle: Could not determine position")
end
end
turtle.run(function() summon({COMPUTER_ID}) end)

1
etc/scripts/update Normal file
View File

@@ -0,0 +1 @@
shell.run('/apps/update.lua')