Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- targ = {
- nextColumnGenerator = function ()
- local curpos = {x=0, y=0, z=0, facing=3}
- local deph = 0
- while true do
- for i=0,deph do
- if deph%2 == 0 then
- curpos.z = curpos.z - 3
- curpos.x = curpos.x + 1
- else
- curpos.z = curpos.z + 3
- curpos.x = curpos.x - 1
- end
- coroutine.yield(curpos)
- end
- if deph%2 == 0 then
- curpos.z = curpos.z - 1
- curpos.x = curpos.x + 2
- else
- curpos.z = curpos.z + 2
- curpos.x = curpos.x + 1
- end
- deph = deph + 1
- coroutine.yield(curpos)
- end
- end
- }
- location = {
- lockd = false,
- x = 0,
- y = 0,
- z = 0,
- facing = 3,
- facingToAxis = {
- [0] = { axis = 'z', unit = 1 },
- [1] = { axis = 'x', unit = -1 },
- [2] = { axis = 'z', unit = -1 },
- [3] = { axis = 'x', unit = 1 }
- },
- lock = function()
- while location.lockd do
- sleep(1)
- end
- end,
- getAxisForFacing = function()
- return location.facingToAxis[location.facing]['axis'], location.facingToAxis[location.facing]['unit']
- end,
- turnLeft = function()
- location.lock()
- location.facing = (location.facing - 1) % 4
- turtle.turnLeft()
- end,
- turnRight = function()
- location.lock()
- location.facing = (location.facing + 1) % 4
- turtle.turnRight()
- end,
- moveForward = function()
- location.lock()
- if turtle.forward() then
- local axis, unit = location.getAxisForFacing()
- location[axis] = location[axis] + unit
- return true
- end
- return false
- end,
- moveBack = function()
- location.lock()
- if turtle.back() then
- local axis, unit = location.getAxisForFacing()
- location[axis] = location[axis] - unit
- return true
- end
- return false
- end,
- face = function(faceto)
- local dirdif = (faceto - location.facing) % 4
- if dirdif == 1 then
- location.turnRight()
- elseif dirdif == 2 then
- location.turnRight()
- location.turnRight()
- elseif dirdif == 3 then
- location.turnLeft()
- end
- end,
- movez = function(d)
- if d < location.z then location.face(2) elseif d > location.z then location.face(0) end
- return location.moveForward()
- end,
- movex = function(d)
- if d < location.x then location.face(1) elseif d > location.x then location.face(3) end
- return location.moveForward()
- end,
- movey = function(d)
- if d < location.y then
- return location.moveDown()
- end
- return location.moveUp()
- end,
- moveUp = function()
- location.lock()
- if turtle.up() then
- location.y = location.y + 1
- return true
- end
- return false
- end,
- moveDown = function()
- location.lock()
- if turtle.down() then
- location.y = location.y - 1
- return true
- end
- return false
- end,
- moveTo = function(x, y, z, facing)
- if y > location.y then
- while location.y < y do
- while not location.moveUp() do
- if turtle.detectUp() then
- turtle.select(1)
- turtle.digUp()
- else
- turtle.attackUp()
- end
- end
- end
- end
- while x ~= location.x do
- while not location.movex(x) do
- if turtle.detect() then
- turtle.select(1)
- turtle.dig()
- else
- turtle.attack()
- end
- end
- end
- while z ~= location.z do
- while not location.movez(z) do
- if turtle.detect() then
- turtle.select(1)
- turtle.dig()
- else
- turtle.attack()
- end
- end
- end
- if y < location.y then
- while location.y > y do
- while not location.moveDown() do
- if turtle.detectDown() then
- turtle.select(1)
- turtle.digDown()
- else
- turtle.attackDown()
- end
- end
- end
- end
- location.face(facing)
- return true
- end,
- moveToPos = function(pos)
- return location.moveTo(pos.x, pos.y, pos.z, pos.facing)
- end,
- distanceTo = function(x, y, z)
- return math.abs(x) - location.x + math.abs(y) - location.y + math.abs(z) - location.z
- end,
- distanceToPos = function(pos)
- return location.distanceTo(pos.x, pos.y, pos.z)
- end
- }
- mining = {
- -- returns false if bedrock
- down = function (h)
- while not location.movey(-1e10) do
- if turtle.detectDown() then
- turtle.digDown()
- if turtle.detectDown() then
- return false
- end
- else
- turtle.attackDown()
- end
- end
- return true
- end,
- checkForward = function()
- location.lock()
- local success, data = turtle.inspect()
- return success and not (data.name == "minecraft:stone" or data.name == "minecraft:dirt" or data.name == "minecraft:gravel")
- end,
- digHole = function ()
- while true do
- if not mining.down(0) then
- break
- end
- for j=1,4 do
- if mining.checkForward() then
- if not interface.space() then
- controls.returnToChest()
- end
- turtle.select(1)
- turtle.dig()
- end
- location.turnRight()
- end
- end
- end
- }
- interface = {
- refuel = function (ammount)
- if turtle.getFuelLevel() == "unlimited" then
- return true
- end
- local needed = ammount or location.distanceToPos({x=0, y=0, z=0, facing=1}) * 2 + 1
- if turtle.getFuelLevel() < needed then
- term.write("need more fuel. refueling")
- for n=1,16 do
- location.lock()
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end,
- unload = function ()
- for n=1,16 do
- location.lock()
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if n <= 0 then
- if (not false and n == 1) or n > 1 then
- turtle.drop(turtle.getItemCount(n) - 1)
- end
- else
- turtle.drop()
- end
- end
- end
- turtle.select(1)
- end,
- space = function ()
- for n=1,16 do
- if turtle.getItemCount(n) == 0 then
- return true
- end
- end
- return false
- end
- }
- controls = {
- returnToChest = function()
- local returnPos = {x = location.x, y = location.y, z = location.z, facing = location.facing}
- local fuelNeeded = location.distanceToPos({x=0,y=0,z=0,facing=1}) * 2 + 1
- location.moveToPos({x=0,y=0,z=0,facing=1})
- location.face((location.facing + 2) % 4)
- if not interface.refuel(fuelNeeded) then
- interface.unload()
- while not interface.refuel(fuelNeeded) do
- location.lock()
- sleep(1)
- end
- else
- interface.unload()
- end
- location.moveToPos(returnPos)
- end,
- forward = function ()
- if not space() then
- controls.returnToChest()
- end
- while not location.moveForward() do
- if turtle.detect() then
- turtle.select(1)
- turtle.dig()
- else
- turtle.attack()
- end
- end
- end
- }
- function oreThread()
- local target = coroutine.create(targ.nextColumnGenerator)
- while true do
- if not interface.refuel() or not interface.space() then
- controls.returnToChest()
- end
- local error, shaft = coroutine.resume(target)
- term.write("mining shaft at x=" .. shaft.x .. " and z=" .. shaft.z)
- location.moveToPos(shaft)
- mining.digHole()
- end
- end
- oreThread()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement