begin moving turtle specific code from opus into turtle package

This commit is contained in:
kepler155c@gmail.com
2019-02-18 02:57:01 -05:00
parent 20bb3d831f
commit 3564dff4fd
21 changed files with 331 additions and 134 deletions

View File

@@ -163,7 +163,7 @@ function turtle.level(startPt, endPt, firstPt, verbose)
local node = closestPoint(turtle.point, nodes)
node = getAdjacentPoint(node)
if not turtle._goto(node) then
if not turtle.go(node) then
break
end
until turtle.isAborted()

View File

@@ -1,108 +0,0 @@
_G.requireInjector(_ENV)
local Point = require('point')
local Util = require('util')
local os = _G.os
local turtle = _G.turtle
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.getItemCount(16) > 0 then
print('Inventory full')
print('Enter to continue...')
_G.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
node = Point.closest(turtle.point, nodes)
if not turtle._goto(node) then
break
end
until turtle.isAborted()
end
turtle.run(function()
turtle.reset()
turtle.set({ digPolicy = 'dig' })
local s, m = pcall(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({ x = 0, z = 0 })
turtle.placeDown()
os.sleep(2)
turtle.placeDown()
turtle.down()
turtle.select(1)
until turtle.isAborted()
end)
if not s and m then
error(m)
end
turtle._goto({ x = 0, y = 0, z = 0, heading = 0 })
end)

View File

@@ -32,7 +32,7 @@ local function summon(id)
local function doGPS()
tFixes = { }
for i = 1, 4 do
if not turtle._goto(pts[i]) then
if not turtle.go(pts[i]) then
error('turtle: Unable to perform GPS maneuver')
end
local distance = getDistance()

View File

@@ -1,66 +0,0 @@
local Point = require('point')
local device = _G.device
local turtle = _G.turtle
local MAX_FUEL = turtle.getFuelLimit()
if turtle.getFuelLevel() == 0 then
error('Need some fuel to begin')
end
if not turtle.has('minecraft:bucket') then
error('bucket required')
end
local scanner = device['plethora:scanner'] or
turtle.equip('right', 'plethora:module:2') and device['plethora:scanner'] or
error('Plethora scanner required')
if not turtle.select('minecraft:bucket') then
error('bucket required')
end
local s, m = turtle.run(function()
turtle.setMovementStrategy('goto')
local facing = scanner.getBlockMeta(0, 0, 0).state.facing
turtle.setPoint({ x = 0, y = 0, z = 0, heading = Point.facings[facing].heading })
local blocks = scanner.scan()
local first, last = blocks[#blocks].y, blocks[1].y
for y = first, last, -1 do
if turtle.getFuelLevel() >= (MAX_FUEL - 1000) then
print('I am full')
break
end
local t = { }
for _,v in pairs(blocks) do
if v.y == y then
if (v.name == 'minecraft:lava' and v.metadata == 0) or
(v.name == 'minecraft:flowing_lava' and v.metadata == 0) then
table.insert(t, v)
end
end
end
Point.eachClosest(turtle.point, t, function(b)
if turtle.getFuelLevel() >= (MAX_FUEL - 1000) then
return true
end
turtle.placeDownAt(b)
turtle.refuel()
print(turtle.getFuelLevel())
end)
end
end)
turtle.gotoY(0)
turtle._goto({ x = 0, y = 0, z = 0 })
turtle.unequip('right')
print('Fuel: ' .. turtle.getFuelLevel())
if not s and m then
error(m)
end

View File

@@ -1,93 +0,0 @@
local turtle = _G.turtle
local function doCommand(command, moves)
local function format(value)
if type(value) == 'boolean' then
if value then return 'true' end
return 'false'
end
if type(value) ~= 'table' then
return value
end
local str
for k,v in pairs(value) do
if not str then
str = '{ '
else
str = str .. ', '
end
str = str .. k .. '=' .. tostring(v)
end
if str then
str = str .. ' }'
else
str = '{ }'
end
return str
end
local function runCommand(fn, arg)
local r = { fn(arg) }
if r[2] then
print(format(r[1]) .. ': ' .. format(r[2]))
elseif r[1] then
print(format(r[1]))
end
return r[1]
end
local cmds = {
[ 's' ] = turtle.select,
[ 'rf' ] = turtle.refuel,
[ 'gh' ] = function() turtle.pathfind({ x = 0, y = 0, z = 0, heading = 0}) end,
}
local repCmds = {
[ 'u' ] = turtle.up,
[ 'd' ] = turtle.down,
[ 'f' ] = turtle.forward,
[ 'r' ] = turtle.turnRight,
[ 'l' ] = turtle.turnLeft,
[ 'ta' ] = turtle.turnAround,
[ 'el' ] = turtle.equipLeft,
[ 'er' ] = turtle.equipRight,
[ 'DD' ] = turtle.digDown,
[ 'DU' ] = turtle.digUp,
[ 'D' ] = turtle.dig,
[ 'p' ] = turtle.place,
[ 'pu' ] = turtle.placeUp,
[ 'pd' ] = turtle.placeDown,
[ 'b' ] = turtle.back,
[ 'gfl' ] = turtle.getFuelLevel,
[ 'gp' ] = turtle.getPoint,
[ 'R' ] = function() turtle.setPoint({x = 0, y = 0, z = 0, heading = 0}) return turtle.point end
}
if cmds[command] then
runCommand(cmds[command], moves)
elseif repCmds[command] then
for _ = 1, moves do
if not runCommand(repCmds[command]) then
break
end
end
end
end
local args = {...}
if #args > 0 then
doCommand(args[1], args[2] or 1)
else
print('Enter command (q to quit):')
while true do
local cmd = _G.read()
if cmd == 'q' then break
end
args = { }
cmd:gsub('%w+', function(w) table.insert(args, w) end)
doCommand(args[1], args[2] or 1)
end
end