Advertisement
LayZee

Turtle Strip Mining

May 20th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | None | 0 0
  1. local args = {...}
  2. local defaultBranchLength = 60
  3. local branchLength = tonumber(args[1]) or defaultBranchLength
  4. local enderChestMode = true
  5.  
  6. local cobblestone =  1
  7. local torch       = 15
  8. local enderChest  = 16
  9.  
  10. function dig ()
  11.   turtle.select(1)
  12.   local tries = 0
  13.  
  14.   turtle.dig()
  15.  
  16.   while turtle.detect() and tries < 120 do
  17.     tries = tries + 1
  18.     turtle.dig()
  19.     sleep(0.8)
  20.   end
  21.  
  22.   slotsFilled()
  23. end
  24.  
  25. function forward (n, digUp)
  26.   local steps = n or 1
  27.  
  28.   for i = 1, steps do
  29.     dig()
  30.  
  31.     while not turtle.forward() do
  32.       dig()
  33.       sleep(0.8)
  34.     end
  35.    
  36.     if digUp then
  37.       local tries = 0
  38.       turtle.digUp()
  39.      
  40.       while turtle.detectUp and tries < 120 do
  41.         tries = tries + 1
  42.         turtle.digUp()
  43.       end
  44.     end
  45.   end
  46. end
  47.  
  48. function up ()
  49.   turtle.digUp()
  50.  
  51.   while not turtle.up() do
  52.     turtle.digUp()
  53.     sleep(0.8)
  54.   end
  55. end
  56.  
  57. function placeTorch ()
  58.   if turtle.getItemCount(torch) > 1 then
  59.     turtle.select(torch)
  60.    
  61.     if not turtle.place() and turtle.getItemCount(cobblestone) > 1 then
  62.       forward()
  63.       turtle.select(cobblestone)
  64.       turtle.place()
  65.       turtle.back()
  66.       turtle.select(torch)
  67.       turtle.place()
  68.     end
  69.   end
  70. end
  71.  
  72. function emptyInventory()
  73.   turtle.turnLeft()
  74.   turtle.turnLeft()
  75.   turtle.dig()
  76.   turtle.select(enderChest)
  77.  
  78.   while turtle.getItemCount(enderChest) == 0 do
  79.     print("No chest found, press Enter to continue")
  80.    
  81.     repeat
  82.       event, key = os.pullEvent("key")
  83.     until key == keys.enter
  84.   end
  85.  
  86.   turtle.place()
  87.  
  88.   for slot = 2, 14 do
  89.     if turtle.getItemCount(slot) > 0 then
  90.       turtle.select(slot)
  91.       turtle.drop()
  92.     end
  93.   end
  94.  
  95.   turtle.select(enderChest)
  96.  
  97.   if enderChestMode then
  98.     turtle.dig()
  99.   end
  100.  
  101.   turtle.turnLeft()
  102.   turtle.turnLeft()
  103. end
  104.  
  105. function floor ()
  106.   if not turtle.detectDown() and turtle.getItemCount(cobblestone) > 1 then
  107.     turtle.select(cobblestone)
  108.     turtle.placeDown()
  109.   end
  110. end
  111.  
  112. function ceiling ()
  113.   if not turtle.detectUp() and turtle.getItemCount(cobblestone) > 1 then
  114.     turtle.select(cobblestone)
  115.     turtle.placeUp()
  116.   end
  117. end
  118.  
  119. function dig1x3 (i)
  120.   turtle.turnLeft()
  121.   dig()
  122.   turtle.turnRight()
  123.   turtle.turnRight()
  124.   dig()
  125.  
  126.   if i and i % 7 == 1 then
  127.     placeTorch()
  128.   end
  129.  
  130.   turtle.turnLeft()
  131. end
  132.  
  133. function dig2x3 (i)
  134.   forward()
  135.   floor()
  136.   up()
  137.   dig1x3(i)
  138.   ceiling()
  139.   turtle.down()
  140.   dig1x3()
  141. end
  142.  
  143. function slotsFilled ()
  144.   local filled = true
  145.  
  146.   for slot = 2, 14 do
  147.     if turtle.getItemCount(slot) == 0 then
  148.       filled = false
  149.      
  150.       break
  151.     end
  152.   end
  153.  
  154.   if filled then
  155.     emptyInventory()
  156.   end
  157. end
  158.  
  159. function nextBranch (n)
  160.   local odd = n % 2 == 1
  161.   local turn = nil
  162.  
  163.   if odd then
  164.     turn = turtle.turnRight
  165.   else
  166.     turn = turtle.turnLeft
  167.   end
  168.  
  169.   forward(1, true)
  170.   floor()
  171.   turn()
  172.  
  173.   for i = 1, 5 do
  174.     forward(1, true)
  175.     floor()
  176.   end
  177.  
  178.   turn()
  179. end
  180.  
  181. --Main program
  182. print("lzStrip v0.2")
  183. print("(C) LayZee 2013")
  184. print("")
  185. print("Place Cobblestones in slot 1")
  186. print("Place Torches in slot 15")
  187. print("Place an Ender Chest in slot 16")
  188. print("Press Enter to start strip mining")
  189.  
  190. repeat
  191.   local event, key = os.pullEvent("key")
  192. until key == keys.enter
  193.  
  194. while true do
  195.   local branch = 1
  196.  
  197.   while turtle.getFuelLevel() >= branchLength * 2 + 12 + branchLength / 7 do
  198.     for i = 1, branchLength do
  199.       dig2x3(i)
  200.     end
  201.    
  202.     branch = branch + 1    
  203.     nextBranch(branch)
  204.   end
  205.  
  206.   print("Out of fuel")
  207.   print("")
  208.   print("1. Remove Cobblestones from slot 1")
  209.   print("2. Place fuel source in slot 1")
  210.   print("3. Press any key to refuel")
  211.   os.pullEvent("key")
  212.  
  213.   while turtle.getFuelLevel() < branchLength * 2 + 12 do
  214.     turtle.select(1)
  215.     turtle.refuel()
  216.  
  217.     local timerId = os.startTimer(2)
  218.     local event = ""
  219.     local id = 0
  220.    
  221.     while timerId ~= id do
  222.       event, id = os.pullEvent("timer")
  223.     end
  224.   end
  225.  
  226.   print("4. Place Cobblestone in slot 1")
  227.   print("5. Press Enter to resume strip mining")
  228.  
  229.   repeat
  230.     event, key = os.pullEvent("key")
  231.   until key == keys.enter
  232. end
  233.  
  234. print("ERROR: Broke out of main loop")
  235. print("Report to layzee.dk@gmail.com")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement