netripper

Computercraft - snake pattern mining

Nov 11th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. -- snake
  2.  
  3. os.loadAPI("path")
  4. os.loadAPI("dig")
  5.  
  6. local foundBlockToMine = 0
  7.  
  8. function digIt()
  9.   path.forward()
  10.  
  11.   -- Algorithm continuation: while nothing detected 'up', simply move on. First time we find something, start being throrough again.
  12.   if turtle.detectUp() and foundBlockToMine == 0 then
  13.     foundBlockToMine = 1
  14.     print("* Found first block to mine, being thorough now")
  15.   end
  16.   if foundBlockToMine == 0 then return end
  17.  
  18.   dig.leftWithoutMoving()
  19.   dig.rightWithoutMoving()
  20.  
  21.   dig.up()
  22.   dig.leftWithoutMoving()
  23.   dig.rightWithoutMoving()
  24.  
  25.   dig.up()
  26.   dig.leftWithoutMoving()
  27.   dig.rightWithoutMoving()
  28.  
  29.   dig.up()
  30.   dig.leftWithoutMoving()
  31.   dig.rightWithoutMoving()
  32.  
  33.   dig.up()
  34.   dig.leftWithoutMoving()
  35.   dig.rightWithoutMoving()
  36.  
  37.   dig.up()
  38.   dig.leftWithoutMoving()
  39.   dig.rightWithoutMoving()
  40.  
  41.   dig.down()
  42.   dig.down()
  43.   dig.down()
  44.   dig.down()
  45.   dig.down()
  46. end
  47.  
  48. function placeTorch()
  49.   turtle.turnLeft()
  50.   if turtle.getItemCount(16) > 0 then
  51.     turtle.select(16)
  52.     turtle.place()
  53.     turtle.select(1)
  54.   else
  55.     print("* No torches in inventory to place")
  56.   end
  57.   turtle.turnRight()
  58. end
  59.  
  60. function fuelCheck()
  61.   if path.length() + 50 > turtle.getFuelLevel() then
  62.     print("* I'm about to go out of fuel, going back")
  63.     path.navigateBack()
  64.     print("* I'm back. Emptying and shutting down.")
  65.     emptyInventory()
  66.     quit()
  67.   end
  68. end
  69.  
  70. function fullCheck()
  71.   if turtle.getItemCount(15) > 0 then
  72.     print("* My 15th slot is used. Heading back to empty.")
  73.     path.navigateBack()
  74.     print("* I'm back, emptying!")
  75.     emptyInventory()
  76.     if (path.length() * 2) + 50 > turtle.getFuelLevel() then
  77.       print("* Fuel is kind of low, not heading out!")
  78.       quit()
  79.     else
  80.       print("* Heading out again!")
  81.       path.navigateForward()
  82.     end
  83.   end
  84. end
  85.  
  86. function emptyInventory()
  87.   print("* Finding something to drop items in!")
  88.   if turtle.detect() then
  89.     emptyInventoryFront()
  90.   end
  91.   turtle.turnLeft()
  92.   if turtle.detect() then
  93.     emptyInventoryFront()
  94.   end
  95.   turtle.turnLeft()
  96.   if turtle.detect() then
  97.     emptyInventoryFront()
  98.   end
  99.   turtle.turnLeft()
  100.   if turtle.detect() then
  101.     emptyInventoryFront()
  102.   end
  103.   turtle.turnLeft()
  104. end
  105.  
  106. function emptyInventoryFront()
  107.   print("* Found something, dropping items!")
  108.   for l = 1, 15 do
  109.     turtle.select(l)
  110.     turtle.drop()
  111.   end
  112.   turtle.select(1)
  113. end
  114.  
  115. -- Q: Length of the lane?
  116. local lanelength = 0
  117. term.write("Length of the lane (#)? ")
  118. lanelength = tonumber(read())
  119.  
  120. -- Q: Do we go LEFT or RIGHT at end of lane?
  121. local leftright = ""
  122. term.write("Left or Right at end (L/R)? ")
  123. leftright = read()
  124. local leftrightmod = 1
  125. if leftright == "l" or leftright == "L" then
  126.   leftrightmod = 0
  127. end
  128.  
  129. -- Q: How many lanes?
  130. local lanecount = 0
  131. term.write("How many lanes (#)? ")
  132. lanecount = tonumber(read())
  133.  
  134. -- Q: Start at lane?
  135. local offset = 0
  136. term.write("Lane offset (#)? ")
  137. offset = tonumber(read()) * 5
  138.  
  139. -- Simple fuel check before we start
  140. if turtle.getFuelLevel() < 100 then
  141.   print("* Fuel me first, mister!")
  142.   quit()
  143. end
  144.  
  145. -- Offsets?
  146. if offset > 0 and leftrightmod == 1 then
  147.   path.right()
  148.   for i = 1, offset do
  149.     path.forward()
  150.   end
  151.   path.left()
  152. end
  153. if offset > 0 and leftrightmod == 0 then
  154.   path.left()
  155.   for i = 1, offset do
  156.     path.forward()
  157.   end
  158.   path.right()
  159. end
  160.  
  161. -- Go through lanes
  162. for i = 1, lanecount do
  163.   print("* Proceeding with lane "..i)
  164.  
  165.   -- Go through the current lane
  166.   for j = 1, lanelength do
  167.     fuelCheck()
  168.     fullCheck()
  169.     digIt()
  170.     if j % 7 == 0 then placeTorch() end
  171.   end
  172.  
  173.   -- If this was the last lane, break out
  174.   if i < lanecount then
  175.     -- When we finished a lane and there's more to do, move a bit for the next lane
  176.     print("* Going around the corner!")
  177.     digIt()
  178.     path.back()
  179.     if i % 2 == leftrightmod then
  180.       -- Turn right
  181.       path.right()
  182.       digIt()
  183.       digIt()
  184.       digIt()
  185.       digIt()
  186.       digIt()
  187.       digIt()
  188.       path.back()
  189.       path.right()
  190.     else
  191.       -- Turn left
  192.       path.left()
  193.       digIt()
  194.       digIt()
  195.       digIt()
  196.       digIt()
  197.       digIt()
  198.       digIt()
  199.       path.back()
  200.       path.left()
  201.     end
  202.   end
  203. end
  204.  
  205. -- All done
  206. print("* All done! Heading back.")
  207. path.navigateBack()
  208. emptyInventory()
  209. print("* Shutting down!")
Advertisement
Add Comment
Please, Sign In to add comment