Mr_Rockers

TunnelExcavator

Aug 2nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. local length = 0
  2.  
  3. local currentFwd = 0
  4. local stopReason = ""
  5.  
  6. function operationFinishMessage(reason)
  7.     print("Operation has stopped.")
  8.     os.sleep(1)
  9.     print("Reason: ")
  10.     os.sleep(2)
  11.     print(reason)
  12. end
  13.  
  14. function cls()
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17. end
  18.  
  19. function craftDropOff()
  20.     turtle.select(5)
  21.     turtle.craft(turtle.getItemCount(5))
  22.     --print("Crafted " .. tostring(turtle.getItemCount(5)) .. " " .. turtle.getItemDetail(5).name .. "(s).")
  23. end
  24.  
  25. function move(direction)
  26.  
  27.     if direction == "forward" then
  28.         return turtle.forward()
  29.     elseif direction == "back" then
  30.         return turtle.back()
  31.     elseif direction == "up" then
  32.         return turtle.up()
  33.     elseif direction == "down" then
  34.         return turtle.down()
  35.     elseif direction == "left" then
  36.         turtle.turnLeft()
  37.         local success = turtle.forward()
  38.         turtle.turnRight()
  39.         return success
  40.     elseif direction == "right" then
  41.         turtle.turnRight()
  42.         local success = turtle.forward()
  43.         turtle.turnLeft()
  44.         return success
  45.     else
  46.         print (direction .. " is not a valid direction!")
  47.         return false
  48.     end
  49.    
  50. end
  51.  
  52. function dig(direction)
  53.     if direction == "forward" then
  54.         return turtle.dig()
  55.     elseif direction == "back" then
  56.         turtle.turnRight()
  57.         turtle.turnRight()
  58.         local success = turtle.dig()
  59.         turtle.turnRight()
  60.         turtle.turnRight()
  61.         return success
  62.     elseif direction == "up" then
  63.         return turtle.digUp()
  64.     elseif direction == "down" then
  65.         return turtle.digDown()
  66.     elseif direction == "left" then
  67.         turtle.turnLeft()
  68.         local success = turtle.dig()
  69.         turtle.turnRight()
  70.         return success
  71.     elseif direction == "right" then
  72.         turtle.turnRight()
  73.         local success = turtle.dig()
  74.         turtle.turnLeft()
  75.         return success
  76.     else
  77.         print (direction .. " is not a valid direction!")
  78.         return false
  79.     end
  80. end
  81.  
  82. function excavate()
  83.     cls()
  84.     print("Starting excavation...")
  85.     turtle.select(6)
  86.  
  87.     stopReason = "Movement blocked."
  88.     while currentFwd <= length do
  89.  
  90.         dig("forward")
  91.         if move("forward") == false then break end
  92.         dig("right")
  93.         dig("up")
  94.         if move("up") == false then break end
  95.         dig("right")
  96.         dig("up")
  97.         if move("up") == false then break end
  98.         dig("right")
  99.  
  100.         if currentFwd % 2 == 0 then
  101.             if move("down") == false then break end
  102.             if move("down") == false then break end
  103.         else
  104.             if move("right") == false then break end
  105.             if move("down") == false then break end
  106.             turtle.turnLeft()
  107.             turtle.select(1)
  108.             turtle.place()
  109.             turtle.select(6)
  110.             turtle.turnRight()
  111.             if move("down") == false then break end
  112.             if move("left") == false then break end
  113.         end
  114.  
  115.         dig("down")
  116.         if move("down") == false then break end
  117.         dig("down")
  118.  
  119.         turtle.select(5)
  120.         if turtle.placeDown() == false then
  121.             stopReason = "Can't place drop off point."
  122.             if move("up") == false then break end
  123.             break
  124.         end
  125.         turtle.select(6)
  126.         if move("up") == false then break end
  127.         --Places dropOffPoint
  128.        
  129.         currentFwd = currentFwd + 1
  130.     end
  131.    
  132.     if currentFwd >= length then stopReason = "Finished operation." end
  133.     if turtle.getFuelLevel() <= 0 then stopReason = "Ran out of fuel." end
  134.    
  135.     cls()
  136.     operationFinishMessage(stopReason)
  137. end
  138.  
  139. function startExcavation()
  140.     print("Tunnel Excavation")
  141.     print("")
  142.     print("Please read instructions.")
  143.     print("Press enter...")
  144.     read()
  145.     cls()
  146.  
  147.     print("How far would you like to")
  148.     print("excavate?")
  149.     length = tonumber(read())
  150.     cls()
  151.  
  152.     turtle.select(2)
  153.     turtle.refuel()
  154.     turtle.select(1)
  155.     --Refuel.
  156.  
  157.     print("Turtle now has: ")
  158.     print(tostring(turtle.getFuelLevel()) .. " fuel.")
  159.     print(tostring(turtle.getItemCount(1)) .. " torches.")
  160.     os.sleep(2)
  161.     cls()
  162.  
  163.     local craftInputSuccessful = false
  164.     while craftInputSuccessful == false do
  165.         print("Would you like to craft")
  166.         print("drop off points?")
  167.         print("(yes/no)")
  168.         local craftInputReturn = read()
  169.  
  170.         if craftInputReturn == "yes" then
  171.             craftInputSuccessful = true
  172.             cls()
  173.             print("Crafting drop off points...")
  174.             craftDropOff()
  175.         elseif craftInputReturn == "no" then
  176.             craftInputSuccessful = true
  177.         else
  178.             print("Please input either yes or no.")
  179.             os.sleep(1)
  180.             cls()
  181.         end
  182.     end
  183.    
  184.     os.sleep(2)
  185.     excavate()
  186. end
  187.  
  188. startExcavation()
Add Comment
Please, Sign In to add comment