Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mine_blocks = {
- "minecraft:coal_ore",
- "minecraft:iron_ore",
- "minecraft:gold_ore",
- "minecraft:redstone_ore",
- "minecraft:lapis_ore",
- "minecraft:emerald_ore",
- "minecraft:diamond_ore"
- }
- local Dir = {
- Front = 0,
- Back = 1,
- Up = 2,
- Down = 3,
- Left = 4,
- Right = 5
- }
- local turnTrack = {}
- local moveTrack = {}
- local depth = 0
- local function hasValue( tbl, str )
- local f = false
- for i = 1, #tbl do
- if type( tbl[i] ) == "table" then
- f = hasValue( tbl[i], str ) -- return value from recursion
- if f then break end -- if it returned true, break out of loop
- elseif tbl[i] == str then
- return true
- end
- end
- return f
- end
- function trackValue(depth, dir)
- return {depth=depth, dir=dir}
- end
- function moveAndDig(dir)
- if dir == Dir.Front then
- while turtle.forward() == false do
- turtle.dig()
- end
- table.insert(moveTrack, trackValue(depth, Dir.Front))
- elseif dir == Dir.Back then
- turtle.back()
- table.insert(moveTrack, trackValue(depth, Dir.Back))
- elseif dir == Dir.Up then
- while turtle.up() == false do
- turtle.digUp()
- end
- table.insert(moveTrack, trackValue(depth, Dir.Up))
- elseif dir == Dir.Down then
- while turtle.down() == false do
- turtle.digDown()
- end
- table.insert(moveTrack, trackValue(depth, Dir.Down))
- end
- end
- function alignDir(dir)
- if dir == Dir.Left then
- turtle.turnLeft()
- table.insert(turnTrack, trackValue(depth, Dir.Left))
- elseif dir == Dir.Right then
- turtle.turnRight()
- table.insert(turnTrack, trackValue(depth, Dir.Right))
- elseif dir == Dir.Back then
- turtle.turnRight()
- turtle.turnRight()
- table.insert(turnTrack, trackValue(depth, Dir.Right))
- table.insert(turnTrack, trackValue(depth, Dir.Right))
- end
- end
- function undoAlignDir(dir)
- if dir == Dir.Left then
- turtle.turnRight()
- print("Right")
- elseif dir == Dir.Right then
- turtle.turnLeft()
- print("Left")
- elseif dir == Dir.Back then
- turtle.turnLeft()
- turtle.turnLeft()
- print("Left Left")
- end
- end
- function backtrack(targetDepth)
- local depth = depth - 1
- while depth >= targetDepth do
- while #moveTrack > 0 and moveTrack[#moveTrack].depth == depth do
- local vec = table.remove(moveTrack)
- if vec.dir == Dir.Front then
- turtle.back()
- print("Back")
- elseif vec.dir == Dir.Up then
- turtle.down()
- print("Down")
- elseif vec.dir == Dir.Down then
- turtle.up()
- print("Up")
- end
- end
- while #turnTrack > 0 and turnTrack[#turnTrack].depth == depth do
- local vec = table.remove(turnTrack)
- undoAlignDir(vec.dir)
- end
- depth = depth - 1
- end
- end
- function testProgram()
- for i = 1, 10, 1 do
- local rand = math.random(1, 4)
- if rand == 1 then
- alignDir(Dir.Left)
- elseif rand == 2 then
- alignDir(Dir.Right)
- elseif rand == 3 then
- moveAndDig(Dir.Front)
- elseif rand == 4 then
- moveAndDig(Dir.Up)
- elseif rand == 5 then
- moveAndDig(Dir.Down)
- end
- depth = depth + 1
- os.sleep(1)
- end
- end
- function detectOres()
- local succes, blockInfo
- local returnValue = {}
- succes, blockInfo = turtle.inspect()
- if succes and hasValue(mine_blocks, blockInfo.name) then
- table.insert(returnValue, Dir.Front)
- return returnValue
- end
- turtle.turnLeft()
- succes, blockInfo = turtle.inspect()
- if succes and hasValue(mine_blocks, blockInfo.name) then
- table.insert(returnValue, Dir.Left)
- turtle.turnRight()
- return returnValue
- end
- turtle.turnRight()
- turtle.turnRight()
- succes, blockInfo = turtle.inspect()
- if succes and hasValue(mine_blocks, blockInfo.name) then
- table.insert(returnValue, Dir.Right)
- turtle.turnLeft()
- return returnValue
- end
- turtle.turnLeft()
- succes, blockInfo = turtle.inspectUp()
- if succes and hasValue(mine_blocks, blockInfo.name) then
- table.insert(returnValue, Dir.Up)
- return returnValue
- end
- succes, blockInfo = turtle.inspectDown()
- if succes and hasValue(mine_blocks, blockInfo.name) then
- table.insert(returnValue, Dir.Down)
- return returnValue
- end
- return returnValue
- end
- function dfs()
- local ores = detectOres()
- while #ores ~= 0 do
- ores = detectOres()
- alignDir(ores[1])
- print(ores[1])
- if ores[1] == Dir.Up then
- moveAndDig(Dir.Up)
- elseif ores[1] == Dir.Down then
- moveAndDig(Dir.Down)
- else
- moveAndDig(Dir.Front)
- end
- depth = depth + 1
- end
- while depth > 0 do
- backtrack(depth - 1)
- ores = detectOres()
- if #ores ~= 0 then
- dfs()
- break
- end
- depth = depth - 1
- end
- end
- dfs()
- -- testProgram()
- -- os.sleep(2)
Add Comment
Please, Sign In to add comment