Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local facing = 0 -- everything will be done in local space just because it's simple and easy to add
- local startPos = vector.new(0,0,0)
- local currentPos = startPos
- local lastMinedBlockPos = startPos
- local lastMinedTurtlePos = startPos
- function getVeinmineBlockData() -- called at the start of the program
- local block, data = turtle.inspect()
- if not block then
- block, data = turtle.inspectDown()
- if not block then
- block, data = turtle.inspectUp()
- end
- end
- return data
- end
- local veinblock = getVeinmineBlockData()
- function inspect(dir)
- local block, data = turtle.inspect()
- if dir < 0 then
- block, data = turtle.inspectDown()
- elseif dir > 0 then
- block, data = turtle.inspectUp()
- end
- if block then
- if data.name == veinblock.name then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function dig(dir)
- if dir == 0 then
- turtle.dig()
- local change = vector.new(0,0,0)
- if facing == 0 then
- change = vector.new(0,0,-1)
- elseif facing == 1 then
- change = vector.new(1,0,0)
- elseif facing == 2 then
- change = vector.new(0,0,1)
- elseif facing == 3 then
- change = vector.new(-1,0,0)
- end
- lastMinedBlockPos = currentPos + change
- end
- if dir < 0 then
- turtle.digDown()
- lastMinedBlockPos = currentPos + vector.new(0,-1,0)
- end
- if dir > 0 then
- turtle.digUp()
- lastMinedBlockPos = currentPos + vector.new(0,1,0)
- end
- end
- function turn(dir)
- if dir < 0 then
- turtle.turnLeft()
- facing = facing - 1
- if facing < 0 then
- facing = 3
- end
- elseif dir > 0 then
- turtle.turnRight()
- facing = facing + 1
- if facing > 3 then
- facing = 0
- end
- end
- --print((facing == 0 and "North") or (facing == 1 and "East") or (facing == 2 and "South") or (facing == 3 and "West"))
- end
- function lookForVeinmineBlock(depth) -- call if we can't find any blocks
- local ahead, above, below = inspect(0) , inspect(1), inspect(-1)
- local canSeeVeinBlock = ahead or above or below
- print(depth, canSeeVeinBlock)
- if not canSeeVeinBlock then
- turn(1)
- if depth > 3 then
- repeat turtle.down() until turtle.detectDown()
- return true
- end
- lookForVeinmineBlock(depth + 1)
- return false
- end
- if above then
- dig(1)
- end
- if ahead then
- dig(0)
- end
- if below then
- dig(-1)
- end
- local difference = currentPos - lastMinedBlockPos
- if difference.z == 1 then -- north* of the turtle *(north in relative space)
- if facing == 0 then
- turtle.forward()
- elseif facing == 1 then
- turn(-1)
- turtle.forward()
- elseif facing == 2 then
- turn(-1)
- turn(-1)
- turtle.forward()
- elseif facing == 3 then
- turn(1)
- turtle.forward()
- end
- elseif difference.z == -1 then -- south of the turtle
- if facing == 0 then
- turn(-1)
- turn(-1)
- turtle.forward()
- elseif facing == 1 then
- turn(1)
- turtle.forward()
- elseif facing == 2 then
- turtle.forward()
- elseif facing == 3 then
- turn(-1)
- turtle.forward()
- end
- elseif difference.x == 1 then -- east of the turtle
- if facing == 0 then
- turn(1)
- turtle.forward()
- elseif facing == 1 then
- turn(-1)
- turn(-1)
- turtle.forward()
- elseif facing == 2 then
- turn(-1)
- turtle.forward()
- elseif facing == 3 then
- turtle.forward()
- end
- elseif difference.x == -1 then -- west of the turtle
- if facing == 0 then
- turn(-1)
- turtle.forward()
- elseif facing == 1 then
- turtle.forward()
- elseif facing == 2 then
- turn(1)
- turtle.forward()
- elseif facing == 3 then
- turn(1)
- turn(1)
- turtle.forward()
- end
- elseif difference.y == -1 then
- turtle.up()
- elseif difference.y == 1 then
- turtle.down()
- end
- return false
- end
- while true do
- if lookForVeinmineBlock(0) then
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement