Advertisement
JoshP95

Autotunnel 2x2 - Izzobot

Jun 13th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. --[[
  2. Autotunnel Program
  3. Created by Izzobot
  4.  
  5. Mines out a 2x2 tunnel and surrounds tunnel with material in slots 2-16.
  6. ]]--
  7.  
  8. FUEL_SLOT = 1
  9.  
  10. -- Functions
  11.  
  12. function refuel()
  13.     turtle.select(FUEL_SLOT)
  14.     items = turtle.getItemCount(FUEL_SLOT)
  15.     fuel = turtle.getFuelLevel(FUEL_SLOT)
  16.     if fuel == 0 then   -- When no fuel
  17.         if items ~= 0 then
  18.             print("Refuelling...")
  19.             turtle.refuel(FUEL_SLOT)
  20.             print("Refueled!")
  21.         else    -- If no fuel present
  22.             print("Add more fuel to slot " .. FUEL_SLOT)
  23.          error()
  24.         end
  25.     else
  26.         print("Fuel level sufficient")
  27.     end
  28. end
  29.  
  30. function digForward()
  31.     while turtle.detect() do
  32.         turtle.dig()
  33.     end
  34. end
  35.  
  36. function moveForward()
  37.     if turtle.getFuelLevel() == 0 then
  38.         refuel()
  39.     end
  40.  
  41.  while turtle.forward() == false do
  42.     digForward()
  43.     turtle.attack()
  44.  end  
  45.  
  46. end
  47.  
  48. function moveBack()
  49.     if turtle.getFuelLevel() == 0 then
  50.         refuel()
  51.     end
  52.  
  53.     while (not turtle.forward()) do
  54.         if (turtle.detect()) then
  55.             turtle.dig()
  56.         else
  57.             turtle.attack()
  58.         end
  59.     end  
  60.  
  61. end
  62.  
  63. function placeDown()
  64.     changeSlot()
  65.     turtle.placeDown()
  66. end
  67.  
  68. function placeUp()
  69.     changeSlot()
  70.     turtle.placeUp()
  71. end
  72.  
  73. function placeForward()
  74.     changeSlot()
  75.     turtle.place()
  76. end
  77.  
  78. function moveUp()
  79.     if turtle.getFuelLevel() == 0 then
  80.         refuel()
  81.     end
  82.  
  83.     while turtle.up() == false do
  84.         turtle.digUp()
  85.     end  
  86. end
  87.  
  88. function moveDown()
  89.     if turtle.getFuelLevel() == 0 then
  90.         refuel()
  91.     end
  92.  
  93.     while turtle.down() == false do
  94.         turtle.digDown()
  95.     end
  96. end
  97.  
  98. function enoughItems()
  99.     total = 0
  100.         for slot=2,16 do -- Count the number of blocks
  101.         total = total + turtle.getItemCount(slot)
  102.         end
  103.     if total < l*8 then
  104.         print("More blocks required.")
  105.         if total ~= 0 then
  106.             io.write("Continue? (y/n): ")
  107.             answer = io.read()
  108.             if answer == "y" then
  109.                 print("Continuing...")
  110.             else
  111.                 print("Cancelled.")
  112.                 error()
  113.             end
  114.         else
  115.             print("No blocks in inventory.")
  116.             error()
  117.         end
  118.     end
  119. end
  120.  
  121. function changeSlot()
  122.     if turtle.getItemCount() == 0 then
  123.         for i=2,16 do
  124.             turtle.select(i)
  125.             if turtle.getItemCount(i) ~= 0 then
  126.                 break
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. -- Start of Code
  133. turtle.select(2)
  134. print(" ")
  135. print("[Auto-tunnel]\n")
  136. io.write("Length: ")
  137. l = io.read()
  138. print(" ")
  139. enoughItems()
  140. print("Building...")
  141.  
  142. for i=1,l do
  143.     digForward()
  144.     moveForward()
  145.     placeDown()
  146.     turtle.turnLeft()
  147.     placeForward()
  148.     moveUp()
  149.     placeForward()
  150.     placeUp()
  151.     turtle.turnRight()
  152.     turtle.turnRight()
  153.     moveForward()
  154.     placeUp()
  155.     placeForward()
  156.     moveDown()
  157.     placeForward()
  158.     turtle.turnLeft()
  159.     placeDown()
  160.     turtle.turnLeft()
  161.     moveForward()
  162.     turtle.turnRight()
  163. end
  164.  
  165. print("Tunnel complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement