Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables--
- current_x = 1
- current_y = 1
- current_z = 1
- current_direction = 1
- memory_x = 1
- memory_y = 1
- memory_z = 1
- memory_direction = 1
- memory_y_offset = 0
- max_x = 1
- max_y = 1
- max_z = 1
- flag_offset = false
- y_offset = 0
- function waitForReset()
- local counter = 1
- while turtle.forward() == false do
- term.clear()
- term.setCursorPos (1,1)
- print("Turtle is stuck, count = "..tostring(counter))
- counter = counter + 1
- if counter == 200 then
- print("turtle is fed up of being ignored and has shut down")
- exit()
- end
- sleep(1)
- end
- updateCoords(current_direction)
- end
- function mineForward()
- local counter = 0
- local flag_success = true
- while turtle.forward() == false do
- turtle.dig()
- turtle.attack()
- sleep(1)
- counter = counter + 1
- if counter == 4 then
- flag_success = false
- break
- end
- end
- if flag_success == true then
- return true
- else
- return false
- end
- end
- function mineUp()
- local counter = 0
- local flag_success = true
- while turtle.up() == false do
- turtle.digUp()
- turtle.attackUp()
- sleep(1)
- counter = counter + 1
- if counter == 4 then
- flag_success = false
- break
- end
- end
- if flag_success == true then
- return true
- else
- return false
- end
- end
- function mineDown()
- local counter = 0
- local flag_success = true
- while turtle.down() == false do
- turtle.digDown()
- turtle.attackDown()
- sleep(1)
- counter = counter + 1
- if counter == 4 then
- flag_success = false
- break
- end
- end
- if flag_success == true then
- return true
- else
- return false
- end
- end
- function updateCoords(direction_moved)
- if direction_moved == 1 then
- current_z = current_z + 1
- end
- if direction_moved == 2 then
- current_x = current_x + 1
- end
- if direction_moved == 3 then
- current_z = current_z - 1
- end
- if direction_moved == 4 then
- current_x = current_x - 1
- end
- end
- function turn(direction) --Note facing is clockwise so 1 is Forwards, 2 is right, 3 is backwards, 4 is left
- while current_direction ~= direction do
- if current_direction < direction then
- if current_direction == 1 and direction == 4 then
- turtle.turnLeft()
- current_direction = 4
- else
- turtle.turnRight()
- current_direction = current_direction + 1
- end
- else
- if current_direction == 4 and direction == 1 then
- turtle.turnRight()
- current_direction = 1
- else
- turtle.turnLeft()
- current_direction = current_direction - 1
- end
- end
- end
- end
- function hasInventorySpace()
- local flag_space = false
- local i = 1
- repeat
- if turtle.getItemCount(i) == 0 then
- flag_space = true
- end
- i = i + 1
- until i == 17
- if flag_space == false then
- print("Inventory check failed")
- end
- return flag_space
- end
- function hasFuel()
- local cost_to_return = current_x + current_y + current_z + 20
- if turtle.getFuelLevel() < cost_to_return then
- print("Fuel check failed")
- return false
- else
- return true
- end
- end
- function unloadAndRefuel()
- --return section
- local y_offset_correct_fail = false --might cause an error if a local doesn't work in the main programme and a function
- memory_x = current_x
- memory_y = current_y
- memory_z = current_z
- memory_direction = current_direction
- memory_y_offset = y_offset
- current_y = current_y + y_offset
- if current_y ~= 1 then
- if mineDown() == true then
- current_y = current_y - 1
- end
- end
- if current_y ~= 1 then
- if mineDown() == true then
- current_y = current_y - 1
- end
- end
- turn(3)
- while current_z ~= 1 do
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_z = current_z - 1
- end
- turn(4)
- while current_x ~= 1 do
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x - 1
- end
- while current_y ~= 1 do
- if mineDown() == false then
- waitForReset()
- end
- current_y = current_y - 1
- end
- --Unload Section
- turn(3)
- local i = 1
- repeat
- turtle.select(i)
- if turtle.drop() == false then
- turtle.dropDown()
- end
- i = i + 1
- until i == 17
- if turtle.getFuelLevel() < 1000 then
- turn(2)
- if mineForward() == false then
- waitForReset()
- end
- if mineForward() == false then
- waitForReset()
- end
- if mineForward() == false then
- waitForReset()
- end
- turn(3)
- turtle.select(1)
- if turtle.suck() == false then
- waitForReset()
- end
- turtle.refuel()
- turn(4)
- if mineForward() == false then
- waitForReset()
- end
- if mineForward() == false then
- waitForReset()
- end
- if mineForward() == false then
- waitForReset()
- end
- turn(3)
- if turtle.drop() == false then
- waitForReset()
- end
- end
- --return to memory section
- local y_return_level
- if memory_y == 1 then
- y_return_level = memory_y
- end
- if memory_y == 2 then
- y_return_level = (memory_y - 1)
- end
- if memory_y > 2 then
- y_return_level = (memory_y - 2)
- end
- while current_y ~= y_return_level do
- if mineUp() == false then
- waitForReset()
- end
- current_y = current_y + 1
- end
- turn(2)
- while current_x ~= memory_x do
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x + 1
- end
- turn(1)
- while current_z ~= memory_z do
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_z = current_z + 1
- end
- while current_y ~= memory_y do
- if mineUp() == false then
- waitForReset()
- end
- current_y = current_y + 1
- end
- turn(memory_direction)
- end
- --Main Program--
- term.clear()
- term.setCursorPos(1, 1)
- print ("Please enter how WIDE you want me to dig.")
- max_x = tonumber(read())
- term.clear()
- term.setCursorPos(1, 1)
- print ("Please enter how HIGH you want me to dig.")
- max_y = tonumber(read())
- term.clear()
- term.setCursorPos(1, 1)
- print ("Please enter how FAR BACK you want me to dig.")
- max_z = tonumber(read())
- local y_offset_correct_fail = false
- local x_direction_reverse = false
- local z_direction_reverse = false
- local startup = true
- repeat
- if startup == false then
- if y_offset == 0 then
- if mineUp() == true then
- current_y = current_y + 1
- else
- waitForReset()
- end
- else
- y_offset = y_offset - 1
- current_y = current_y + 1
- end
- end
- startup = true
- if z_direction_reverse == false then
- repeat
- if startup == false then
- turn(1)
- local y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_z = current_z + 1
- end
- if x_direction_reverse == false then
- turn(2)
- repeat
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x + 1
- if hasInventorySpace() == false or hasFuel() == false then
- unloadAndRefuel()
- end
- until current_x == max_x
- x_direction_reverse = true
- startup = false
- else
- turn(4)
- repeat
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x - 1
- if hasInventorySpace() == false or hasFuel() == false then
- unloadAndRefuel()
- end
- until current_x == 1
- x_direction_reverse = false
- startup = false
- end
- until current_z == max_z
- z_direction_reverse = true
- else
- repeat
- if startup == false then
- turn(3)
- local y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_z = current_z - 1
- end
- if x_direction_reverse == true then
- turn(4)
- repeat
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x - 1
- if hasInventorySpace() == false or hasFuel() == false then
- unloadAndRefuel()
- end
- until current_x == 1
- x_direction_reverse = false
- startup = false
- else
- turn(2)
- repeat
- y_offset_correct_fail = false
- while y_offset ~= 0 and y_offset_correct_fail == false do
- if mineDown() == true then
- y_offset = y_offset - 1
- else
- y_offset_correct_fail = true
- end
- end
- while mineForward() == false do
- if mineUp() == true then
- y_offset = y_offset + 1
- else
- waitForReset()
- end
- if y_offset == 10 then
- print("turtle is climbing without end, something is very very odd. Shutting down")
- exit()
- end
- end
- current_x = current_x + 1
- if hasInventorySpace() == false or hasFuel() == false then
- unloadAndRefuel()
- end
- until current_x == max_x
- x_direction_reverse = true
- startup = false
- end
- until current_z == 1
- z_direction_reverse = false
- end
- until current_y == max_y
Advertisement
Add Comment
Please, Sign In to add comment