Advertisement
Guest User

Mine

a guest
Jan 19th, 2016
3,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.85 KB | None | 0 0
  1. --Foogles Mining Software
  2. --Foogle Proof Mine 1.3 (1/19/2016)
  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.     Continue()
  35.     dist = dist + 1
  36.     Search()
  37.     CheckInv()
  38.   end
  39.   Return(dist)
  40. end
  41.  
  42. --this function is used to find a building block (cobblestone)
  43. --in the inventory and select it before placing a block
  44. --it starts at the beginning and checks every inventory slot until
  45. --it finds cobblestone, or reaches the end. If it finds cobble,
  46. --it selects it
  47. function GetBuild()
  48.   for i=1,16 do
  49.     if (turtle.getItemDetail(i) ~= nil) then
  50.       if (turtle.getItemDetail(i).name == 'minecraft:cobblestone' or turtle.getItemDetail(i).name == 'minecraft:dirt') then
  51.         turtle.select(i)
  52.         return true
  53.       end
  54.     end
  55.   end
  56.   return false
  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.   TurtleUp() --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 (TurtleDown() == false) then --face lower right block
  99.     turtle.digDown()
  100.     TurtleDown()
  101.   end
  102.   Check(reg)
  103.   Check(down) --check block below it
  104.   turtle.turnLeft() --face forward again
  105.   if (dist % 15 == 0) then
  106.     PlaceTorch()
  107.   end
  108. end
  109.  
  110. --This function analyzes a block in front, above, or below
  111. --and determines what to do about it.
  112. --1. If it detects air, it will place cobblestone
  113. --2. If it detects a non dirt, cobblestone, gravel, or stone block,
  114. --   it will mine it and place cobblestone
  115. --3. If it detects lava, it will attempt to pick it up with a bucket
  116. --4. It will move into a water/lava block above or beneath it to remove
  117. --   the source and then fill it with cobblestone to seal the passage
  118.  
  119. function Check(dir)
  120.   local throw, success, block = 0;
  121.   if (dir == up) then
  122.     throw, block = turtle.inspectUp() --save what the block above it is
  123.   elseif (dir == down) then
  124.     throw, block = turtle.inspectDown() --save what the block below it is
  125.   else
  126.     throw, block = turtle.inspect() --save what the block in front of it is
  127.   end
  128.   success = 1
  129.   --if it finds lava, it will select an empty bucket and collect the lava
  130.   if (block.name == 'minecraft:lava' or block.name == 'minecraft:flowing_lava') then
  131.     GetBucket()
  132.     if (dir == up) then
  133.       turtle.placeUp()
  134.     elseif (dir == down) then
  135.       turtle.placeDown()
  136.     else
  137.       turtle.place()
  138.     end
  139.     GetBuild() --switches back to the building block (cobble)
  140.   end
  141.   --below, it scans a block in front, above, or beneath it
  142.   --and replaces if with cobblestone if it is not on the white list below
  143.   --if it runs out of cobblestone, it may place a random block
  144.   if (block.name ~= 'minecraft:stone' and block.name ~= 'minecraft:dirt' and block.name ~= 'minecraft:gravel' and block.name ~= 'minecraft:cobblestone') then
  145.     if (dir == up) then
  146.       turtle.digUp()
  147.       if (GetBuild() == true) then
  148.         success = turtle.placeUp()
  149.       end
  150.     elseif (dir == down) then
  151.       turtle.digDown()
  152.       if (GetBuild() == true) then
  153.         success = turtle.placeDown()
  154.       end
  155.     else
  156.       turtle.dig()
  157.       if (GetBuild() == true) then
  158.         turtle.place()
  159.       end
  160.     end
  161.     --sometimes when it tries to place a block in a liquid space it fails
  162.     --to combat this, it will "fill" the space by moving into it and then out
  163.     --and placing cobblestone before the liquid flows back in
  164.     if (success == false) then
  165.       Fill(dir)
  166.     end
  167.   end
  168. end
  169.  
  170. --this function is short but powerful. It is solely responsible
  171. --for making sure that the turtle moves the correct number of spaces
  172. --First it digs in front of it, then it moves into the space if possible
  173. --Then it digs up. If it failed to move into the space,
  174. --It will call itself again. This usually only happens with gravel
  175. --or sand, but it also triggers when an entity blocks its path
  176. --It doesn't have path finding for alternate routes, but it will keep
  177. --trying to move and dig until the path is clear
  178. function Continue()
  179.   turtle.dig()
  180.   local x = turtle.forward()
  181.   if (x == false) then
  182.     Continue()
  183.   end
  184. end
  185.  
  186. --This short function utilizes the recursion of continue() to (hopefully)
  187. --guarantee that it will return to the place where it was left, including
  188. --the same orientation. The turtle will not repair walls and seal the
  189. --shaft on the way back, but instead it will quickly mine any
  190. --gravel or obstacles that have fallen into the shaft on its
  191. --return trip
  192. function Return(dist)
  193.   turtle.turnLeft()
  194.   turtle.turnLeft()
  195.   for i=1,dist do
  196.     Continue()
  197.   end
  198.   turtle.turnLeft()
  199.   turtle.turnLeft()
  200.   DumpInv()
  201. end
  202.  
  203. --This function is used to fill in liquids in the floor and ceiling that
  204. --the turtle would otherwise fail to seal, so that a player
  205. --can walk through the shaft without hazards
  206. function  Fill(d)
  207.   if (d == 1) then
  208.     TurtleUp()
  209.     TurtleDown()
  210.     turtle.placeUp()
  211.   elseif (d == 2) then
  212.     TurtleDown()
  213.     TurtleUp()
  214.     turtle.placeDown()
  215.   end
  216. end
  217.  
  218. --This function will refuel the turtle with a lava bucket if it is below 99%
  219. function CheckFuel()
  220.   if (turtle.getFuelLevel() < 99000) then
  221.     GetFuel()
  222.     turtle.refuel()
  223.   end
  224. end
  225.  
  226. --this method call starts the program, and goes
  227. --at the end so it can find all the methods
  228.  
  229. function CheckInv()
  230.   local count = 0
  231.   for i=1,16 do
  232.     if (turtle.getItemDetail(i) ~= nil) then
  233.       count = count + 1
  234.     end
  235.   end
  236.   if (count >= 16) then
  237.     Return(dist)
  238.     DumpInv()
  239.     for i=1,dist do Continue() end
  240.   end
  241. end
  242.  
  243. function DumpInv()
  244.   for i=1,16 do
  245.     if (turtle.getItemDetail(i) ~= nil) then
  246.       if (turtle.getItemDetail(i).name ~= 'minecraft:bucket' and turtle.getItemDetail(i).name ~= 'minecraft:lava_bucket' and turtle.getItemDetail(i).name ~= 'minecraft:torch') then
  247.         turtle.select(i)
  248.         turtle.dropDown()
  249.       end
  250.     else
  251.       break
  252.     end
  253.   end
  254. end
  255.  
  256. function GetTorch()
  257.   for i=1,16 do
  258.     if (turtle.getItemDetail(i) ~= nil) then
  259.       if (turtle.getItemDetail(i).name == 'minecraft:torch') then
  260.         turtle.select(i)
  261.         return true
  262.       end
  263.     end
  264.   end
  265.   return false
  266. end
  267.  
  268. function PlaceTorch()
  269.   if (GetTorch() == true) then
  270.     turtle.back()
  271.     turtle.placeUp()
  272.     turtle.forward()
  273.     GetBuild()
  274.   end
  275. end
  276.  
  277. function TurtleUp()
  278.   if (turtle.up() == false) then
  279.     turtle.digUp()
  280.     TurtleUp()
  281.   end
  282. end
  283.  
  284. function TurtleDown()
  285.   if (turtle.down() == false) then
  286.     turtle.digDown()
  287.     TurtleDown()
  288.   end
  289. end
  290.  
  291. Mine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement