Guest User

mine

a guest
Aug 1st, 2015
1,603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.99 KB | None | 0 0
  1. --Foogles Mining Software
  2. --Foogle Proof Mine 1.1 (7/30/2015)
  3. --Install:    in your turtle console, type pastebin get <code> Mine
  4. --            the code will be in the forum contents
  5. --Pre-Launch: put a bucket (empty or with lava) in any inventory slot
  6. --            place 32 or more cobblestone in any inventory slot(s)
  7. --Launch:     type the file name (in this case Mine) followed by a number
  8. --            for how long you want the mine shaft to be
  9. --            because this turtle program doesn't support chest use, you
  10. --            shouldn't make it longer than 100 to avoid overfilling your turtle
  11. --            For example, type: Mine 30
  12. --            To make the turtle mine a 30 block shaft and then come back
  13.  
  14.  
  15. --global variables (their scope applies to all functions in this file)
  16. args = {...}
  17. local reg = 0
  18. local up = 1
  19. local down = 2
  20. local dist = 0
  21.  
  22. --this is the only function that is automatically called, and all
  23. --it really does is coordinates the order that the methods
  24. --which actually accomplish things will be executed in
  25. --1. GetBuild() Find a block of cobblestone for building walls
  26. --2. Continue() Move forward, clearing a 1x2 path (even if gravel is in the way)
  27. --3. Search() Analyze the nearby blocks, searching for ores, gaps, lava, and water
  28. --            This function will also call methods to obtain lava for automatic fueling
  29. --4. CheckFuel() Attempt to refuel with a lava bucket if the fuel tank is less than 99% full
  30. --5. Return() This function will utilize Continue() to return to the starting location
  31. function Mine()
  32.   for i=1,args[1],1 do --do this the amount of times you specified (ex. Mine 10, does it 10 times)
  33.     CheckFuel()
  34.     GetBuild()
  35.     Continue()
  36.     dist = dist + 1
  37.     Search()
  38.     CheckInv()
  39.   end
  40.   Return(dist)
  41. end
  42.  
  43. --this function is used to find a building block (cobblestone)
  44. --in the inventory and select it before placing a block
  45. --it starts at the beginning and checks every inventory slot until
  46. --it finds cobblestone, or reaches the end. If it finds cobble,
  47. --it selects it
  48. function GetBuild()
  49.   for i=1,16 do
  50.     if (turtle.getItemDetail(i) ~= nil) then
  51.       if (turtle.getItemDetail(i).name == 'minecraft:cobblestone' or turtle.getItemDetail(i).name == 'minecraft:dirt') then
  52.         turtle.select(i)
  53.         break
  54.       end
  55.     end
  56.   end
  57. end
  58.  
  59. --this behaves similarly to GetBuild() but instead tries to find a lava
  60. -- bucket which this turtle uses for fuel
  61. function GetFuel()
  62.   for i=1,16 do
  63.     if (turtle.getItemDetail(i) ~= nil) then
  64.       if (turtle.getItemDetail(i).name == 'minecraft:lava_bucket') then
  65.         turtle.select(i)
  66.         break
  67.       end
  68.     end
  69.   end
  70. end
  71.  
  72. --this behaves similarly to GetFuel() but it tries to find an
  73. --empty bucket to refill
  74. function GetBucket()
  75.   for i=1,16 do
  76.     if (turtle.getItemDetail(i) ~= nil) then
  77.       if (turtle.getItemDetail(i).name == 'minecraft:bucket') then
  78.         turtle.select(i)
  79.         return true
  80.       end
  81.     end
  82.   end
  83.   return false
  84. end
  85.  
  86. --This search function makes the turtle move and turn towards
  87. --all visible blocks in one vertical sliver of the mine shaft
  88.  
  89. function Search()
  90.   turtle.turnLeft() --face lower left block
  91.   Check(reg) --check block in front of it
  92.   turtle.up() --face upper left block
  93.   Check(reg)
  94.   turtle.turnRight()
  95.   turtle.turnRight() --face upper right block
  96.   Check(reg)
  97.   Check(up) --check block above it
  98.   if (turtle.down() == false) then --face lower right block
  99.     turtle.digDown()
  100.     turtle.down()
  101.   end
  102.   Check(reg)
  103.   Check(down) --check block below it
  104.   turtle.turnLeft() --face forward again
  105. end
  106.  
  107. --This function analyzes a block in front, above, or below
  108. --and determines what to do about it.
  109. --1. If it detects air, it will place cobblestone
  110. --2. If it detects a non dirt, cobblestone, gravel, or stone block,
  111. --   it will mine it and place cobblestone
  112. --3. If it detects lava, it will attempt to pick it up with a bucket
  113. --4. It will move into a water/lava block above or beneath it to remove
  114. --   the source and then fill it with cobblestone to seal the passage
  115.  
  116. function Check(dir)
  117.   local throw, success, block = 0;
  118.   if (dir == up) then
  119.     throw, block = turtle.inspectUp() --save what the block above it is
  120.   elseif (dir == down) then
  121.     throw, block = turtle.inspectDown() --save what the block below it is
  122.   else
  123.     throw, block = turtle.inspect() --save what the block in front of it is
  124.   end
  125.   success = 1
  126.   --if it finds lava, it will select an empty bucket and collect the lava
  127.   if (block.name == 'minecraft:lava' or block.name == 'minecraft:flowing_lava') then
  128.     GetBucket()
  129.     if (dir == up) then
  130.       turtle.placeUp()
  131.     elseif (dir == down) then
  132.       turtle.placeDown()
  133.     else
  134.       turtle.place()
  135.     end
  136.     GetBuild() --switches back to the building block (cobble)
  137.   end
  138.   --below, it scans a block in front, above, or beneath it
  139.   --and replaces if with cobblestone if it is not on the white list below
  140.   --if it runs out of cobblestone, it may place a random block
  141.   if (block.name ~= 'minecraft:stone' and block.name ~= 'minecraft:dirt' and block.name ~= 'minecraft:gravel' and block.name ~= 'minecraft:cobblestone') then
  142.     if (dir == up) then
  143.       turtle.digUp()
  144.       success = turtle.placeUp()
  145.     elseif (dir == down) then
  146.       turtle.digDown()
  147.       success = turtle.placeDown()
  148.     else
  149.       turtle.dig()
  150.       turtle.place()
  151.     end
  152.     --sometimes when it tries to place a block in a liquid space it fails
  153.     --to combat this, it will "fill" the space by moving into it and then out
  154.     --and placing cobblestone before the liquid flows back in
  155.     if (success == false) then
  156.       Fill(dir)
  157.     end
  158.   end
  159. end
  160.  
  161. --this function is short but powerful. It is solely responsible
  162. --for making sure that the turtle moves the correct number of spaces
  163. --First it digs in front of it, then it moves into the space if possible
  164. --Then it digs up. If it failed to move into the space,
  165. --It will call itself again. This usually only happens with gravel
  166. --or sand, but it also triggers when an entity blocks its path
  167. --It doesn't have path finding for alternate routes, but it will keep
  168. --trying to move and dig until the path is clear
  169. function Continue()
  170.   turtle.dig()
  171.   local x = turtle.forward()
  172.   turtle.digUp()
  173.   if (x == false) then
  174.     Continue()
  175.   end
  176. end
  177.  
  178. --This short function utilizes the recursion of continue() to (hopefully)
  179. --guarantee that it will return to the place where it was left, including
  180. --the same orientation. The turtle will not repair walls and seal the
  181. --shaft on the way back, but instead it will quickly mine any
  182. --gravel or obstacles that have fallen into the shaft on its
  183. --return trip
  184. function Return(dist)
  185.   turtle.turnLeft()
  186.   turtle.turnLeft()
  187.   for i=1,dist do
  188.     Continue()
  189.   end
  190.   turtle.turnLeft()
  191.   turtle.turnLeft()
  192. end
  193.  
  194. --This function is used to fill in liquids in the floor and ceiling that
  195. --the turtle would otherwise fail to seal, so that a player
  196. --can walk through the shaft without hazards
  197. function  Fill(d)
  198.   if (d == 1) then
  199.     turtle.up()
  200.     turtle.down()
  201.     turtle.placeUp()
  202.   elseif (d == 2) then
  203.     turtle.down()
  204.     turtle.up()
  205.     turtle.placeDown()
  206.   end
  207. end
  208.  
  209. --This function will refuel the turtle with a lava bucket if it is below 99%
  210. function CheckFuel()
  211.   if (turtle.getFuelLevel() < 99000) then
  212.     GetFuel()
  213.     turtle.refuel()
  214.   end
  215. end
  216.  
  217. --this method call starts the program, and goes
  218. --at the end so it can find all the methods
  219.  
  220. function CheckInv()
  221.   local count = 0
  222.   for i=1,16 do
  223.     if (turtle.getItemDetail(i) ~= nil) then
  224.       count = count + 1
  225.     end
  226.   end
  227.   if (count >= 16) then
  228.     Return(dist)
  229.     DumpInv()
  230.     for i=1,dist do Continue() end
  231.   end
  232. end
  233.  
  234. function DumpInv()
  235.   for i=1,16 do
  236.     if (turtle.getItemDetail ~= nil) then
  237.       if (turtle.getItemDetail(i).name ~= 'minecraft:bucket' and turtle.getItemDetail(i).name ~= 'minecraft:lava_bucket') then
  238.         turtle.select(i)
  239.         turtle.dropDown()
  240.       end
  241.     end
  242.   end
  243. end
  244.  
  245. Mine()
Advertisement
Add Comment
Please, Sign In to add comment