Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------
- --Mining Program by Jharakn--
- -----------------------------
- --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
- max_x = 1
- max_y = 1
- max_z = 1
- skip_z_move = false
- function Crash()
- print ("The Turtle Stopped Unexpectedly i.e. ErrorErrorBzzzzd")
- exit()
- end
- function ForwardMine()
- while turtle.detect() == true do
- turtle.dig()
- sleep(1)
- end
- local movecheck
- movecheck = turtle.forward()
- if movecheck == false then
- sleep(5)
- turtle.dig()
- movecheck = turtle.forward()
- end
- if movecheck == false then
- sleep(5)
- turtle.dig()
- movecheck = turtle.forward()
- end
- if movecheck == false then
- sleep(5)
- turtle.dig()
- movecheck = turtle.forward()
- end
- return movecheck
- end
- function UpMine()
- while turtle.detectUp() == true do
- turtle.digUp()
- sleep(1)
- end
- local movecheck
- movecheck = turtle.up()
- if movecheck == false then
- sleep(5)
- turtle.digUp()
- movecheck = turtle.up()
- end
- if movecheck == false then
- sleep(5)
- turtle.digUp()
- movecheck = turtle.up()
- end
- if movecheck == false then
- sleep(5)
- turtle.digUp()
- movecheck = turtle.up()
- end
- return movecheck
- end
- function DownMine()
- while turtle.detectDown() == true do
- turtle.digDown()
- sleep(1)
- end
- local movecheck
- movecheck = turtle.down()
- if movecheck == false then
- sleep(5)
- turtle.digDown()
- movecheck = turtle.down()
- end
- if movecheck == false then
- sleep(5)
- turtle.digDown()
- movecheck = turtle.down()
- end
- if movecheck == false then
- sleep(5)
- turtle.digDown()
- movecheck = turtle.down()
- end
- return movecheck
- 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 empty_space = false
- local i = 1
- repeat
- if turtle.getItemCount(i) == 0 then
- empty_space = true
- end
- i = i + 1
- until i == 17
- if empty_space == false then
- print("Inventory check failed")
- end
- return empty_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 ReturnToStart(return_y)
- Turn(3)
- while current_z ~= 1 do
- ForwardMine()
- current_z = current_z - 1
- end
- Turn(4)
- while current_x ~= 1 do
- ForwardMine()
- current_x = current_x - 1
- end
- if return_y == true then
- while current_y ~= 1 do
- DownMine()
- current_y = current_y - 1
- end
- end
- print("remaining Fuel"..turtle.getFuelLevel())
- end
- function UnloadCycle()
- 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)
- ForwardMine()
- ForwardMine()
- ForwardMine()
- Turn(3)
- turtle.select(1)
- if turtle.suck() == false then
- Crash()
- end
- turtle.refuel()
- Turn(4)
- ForwardMine()
- ForwardMine()
- ForwardMine()
- Turn(3)
- if turtle.drop() == false then
- Crash()
- end
- end
- end
- function ReturnToMemory()
- while current_y ~= memory_y do
- UpMine()
- current_y = current_y + 1
- end
- while current_x ~= memory_x do
- Turn(2)
- ForwardMine()
- current_x = current_x + 1
- end
- while current_z ~= memory_z do
- Turn(1)
- ForwardMine()
- current_z = current_z + 1
- end
- Turn(memory_direction)
- end
- function DodgeBlockage(invert)
- Turn(1)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- current_y = current_y + 1
- if invert == false then
- Turn(2)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- current_x = current_x + 1
- Turn(3)
- if ForwardMine() == false then
- if current_x ~= max_x then
- Turn(2)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- current_x = current_x + 1
- Turn(3)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- else
- skip_z_move = true
- end
- end
- current_y = current_y - 1
- else
- Turn(4)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- current_x = current_x - 1
- Turn(3)
- if ForwardMine() == false then
- if current_x ~= 1 then
- Turn(4)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- current_x = current_x - 1
- Turn(3)
- if ForwardMine() == false then
- print("Turtle Blocked")
- while turtle.forward() == false do
- sleep(5)
- end
- end
- else
- skip_z_move = true
- end
- end
- current_y = current_y - 1
- end
- end
- ----------------------------
- --Main Program version 0.2--
- ----------------------------
- 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 startup = true
- local invert_mining = false
- repeat
- if startup == false then
- ReturnToStart(false)
- Turn(1)
- UpMine()
- current_y = current_y + 1
- startup = true
- invert_mining = false
- end
- repeat
- Turn(1)
- if startup == false or skip_z_move == false then
- ForwardMine()
- current_z = current_z + 1
- end
- skip_y_move = false
- startup = false
- if invert_mining == true then
- repeat
- Turn(4)
- if ForwardMine() == true then
- current_x = current_x - 1
- else
- DodgeBlockage(invert_mining)
- end
- if HasInventorySpace() == false or HasFuel() == false then
- print("Returning to base for fuel or to unload inventory")
- memory_x = current_x
- memory_y = current_y
- memory_z = current_z
- memory_direction = current_direction
- ReturnToStart(true)
- UnloadCycle()
- ReturnToMemory()
- end
- until current_x == 1
- else
- repeat
- Turn(2)
- if ForwardMine() == true then
- current_x = current_x + 1
- else
- DodgeBlockage(invert_mining)
- end
- if HasInventorySpace() == false or HasFuel() == false then
- print("Returning to base for fuel or to unload inventory")
- memory_x = current_x
- memory_y = current_y
- memory_z = current_z
- memory_direction = current_direction
- ReturnToStart(true)
- UnloadCycle()
- ReturnToMemory()
- end
- until current_x == max_x
- end
- if invert_mining == true then
- invert_mining = false
- else
- invert_mining = true
- end
- until current_z == max_z
- until current_y == max_y
- ReturnToStart(true)
- print("Finished")
Advertisement
Add Comment
Please, Sign In to add comment