FaceInCake

Turtle Branch Miner

Sep 14th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. -- This program branch mines...
  2. -- It will also place torches. Make sure
  3. -- there's torches in the 1st slot.
  4. -- It will also deposit its items into
  5. -- a chest below where it started when
  6. -- it's inventory is full.
  7. --
  8. --================ ARGUMENT FLAGS ================================
  9. --> -t  <true/false>|<0/1>
  10. --      If true, turtle will place torches from slot 1.
  11. --      Default is true.
  12. --> -l  <int:[1, inf)>
  13. --      Specifies the length in blocks of each branch.
  14. --      Default is 12. Past that, mobs can spawn.
  15. --> -b  <int:[1, inf)>
  16. --      Specififes the number of segments, or branches*2, to mine.
  17. --      Default is 21. Past that, it runs out of torches.
  18. --> -s  <int:[0, inf)>
  19. --      Specifies the number of blocks inbetween each branch.
  20. --      Default is 3. The optimal space for diamond mining.
  21. --> -d  <true/false>|<0/1>
  22. --      If true, turtle will deposit it's items into the chest when it's full.
  23. --      Default is true.
  24. --================================================================
  25. --  Author : Matthew (FaceInCake) Eppel
  26. --    Date : Sept 14, 2019
  27. -- Version : 2.0
  28.  
  29. local args = {...}
  30. -- These are all the changable values
  31. local branches = 21
  32. local length = 12
  33. local space = 3
  34. local placeTorches = true
  35. local dropItems = true
  36.  
  37. function checkInteger(s)
  38.   local n = tonumber(s)
  39.   if n == nil then
  40.     print("[ERROR]: '"..s.."' is not a number")
  41.     return nil
  42.   end
  43.   n = math.floor(n)
  44.   if n < 0 then
  45.     print("[ERROR]: '"..s.."' is not an integer")
  46.     return nil
  47.   end
  48.   return n
  49. end
  50.  
  51. -- Parse all the arguments
  52. function parseArguments()
  53.   for i=1, #args do
  54.     local s = tostring(args[i])
  55.     if s:sub(1,1) == '-' then
  56.       i = i + 1
  57.       if i > #args then
  58.         break
  59.       end
  60.       local c = s:sub(2,2)
  61.       if c == 'b' then
  62.         local n = checkInteger(args[i])
  63.         if n == nil then
  64.           return 1
  65.         end
  66.         branches = n
  67.       elseif c == 'l' then
  68.         local n = checkInteger(args[i])
  69.         if n == nil then
  70.           return 1
  71.         end
  72.         length = n
  73.       elseif c == 's' then
  74.         local n = checkInteger(args[i])
  75.         if n == nil then
  76.           return 1
  77.         end
  78.         space = n
  79.       elseif c == 't' then
  80.         local s = tostring(args[i])
  81.         if s == "false"
  82.         or s == "0"
  83.         or s == "off" then
  84.           placeTorches = false
  85.         elseif s ~= "true"
  86.         and s ~= "1"
  87.         and s ~= "on" then
  88.           print("[ERROR]: '"..s.."' is not a valid boolean")
  89.           return 1
  90.         end
  91.       elseif c == 'd' then
  92.         local s = tostring(args[i])
  93.         if s == "false"
  94.         or s == "0"
  95.         or s == "off" then
  96.           dropItems = false
  97.         elseif s ~= "true"
  98.         and s ~= "1"
  99.         and s ~= "on" then
  100.           print("[ERROR]: '"..s.."' is not a valid boolean")
  101.           return 1
  102.         end
  103.       end
  104.     end
  105.   end
  106.   return 0
  107. end
  108.  
  109. -- Digs forward, but checks if a new block fell into place, and digs it
  110. function delete()
  111.   while turtle.detect() do
  112.     turtle.dig()
  113.     os.sleep(0.1)
  114.   end
  115. end
  116.  
  117. -- Goes forward one space until it is successful
  118. function forward()
  119.   while not turtle.forward() do
  120.     os.sleep(0.2)
  121.   end
  122. end
  123.  
  124. -- Goes backwards until it reaches `dist` blocks away
  125. function back(dist)
  126.   local count = 0
  127.   while count < dist do
  128.     if turtle.back() then
  129.       count = count + 1
  130.     else
  131.       os.sleep(0.2)
  132.     end
  133.   end
  134. end
  135.  
  136. -- Returns false if there is at least 1 space of inventory, true otherwise
  137. function inventoryFull()
  138.   for i=16, 1, -1 do
  139.     if turtle.getItemCount(i)==0 then
  140.       return false
  141.     end
  142.   end
  143.   return true
  144. end
  145.  
  146. -- Sideways offset from chest.
  147. -- Positive is to the right.
  148. local xOffset = 0
  149.  
  150. -- Forwards offset from chest
  151. local yOffset = 0
  152.  
  153. -- Direction. 0=forward, 1=right, etc.
  154. local face = 0
  155.  
  156. -- Turns left and increments 'face'
  157. function turnLeft()
  158.   turtle.turnLeft()
  159.   face = face - 1
  160.   if face < 0 then
  161.     face = 3
  162.   end
  163. end
  164.  
  165. -- Turns right and increments 'face'
  166. function turnRight()
  167.   turtle.turnRight()
  168.   face = face + 1
  169.   if face > 3 then
  170.     face = 0
  171.   end
  172. end
  173.  
  174. -- Does a 180deg turn and increments 'face'
  175. function oneEighty()
  176.   if face >= 2 then
  177.     face = face - 2
  178.     turtle.turnLeft()
  179.     turtle.turnLeft()
  180.   else
  181.     face = face + 2
  182.     turtle.turnRight()
  183.     turtle.turnRight()
  184.   end
  185. end
  186.  
  187. -- Increments xOffset/yOffset by n, depending on the current 'face'
  188. function increment(n)
  189.   if face == 0 then
  190.     yOffset = yOffset + n
  191.   elseif face == 1 then
  192.     xOffset = xOffset + n
  193.   elseif face == 2 then
  194.     yOffset = yOffset - n
  195.   elseif face == 3 then
  196.     xOffset = xOffset - n
  197.   else
  198.     return false
  199.   end
  200.   return true
  201. end
  202.  
  203. -- This function returns to where it was originally placed
  204. function recall()
  205.   if xOffset > 0 then
  206.     -- face needs to be 3
  207.     if face==0 then
  208.       turnLeft()
  209.     elseif face==1 then
  210.       oneEighty()
  211.     elseif face==2 then
  212.       turnRight()
  213.     end
  214.   elseif xOffset < 0 then
  215.     -- Face needs to be 1
  216.     if face==0 then
  217.       turnRight()
  218.     elseif face==2 then
  219.       turnLeft()
  220.     elseif face==3 then
  221.       oneEighty()
  222.     end
  223.   end
  224.   -- Move to middle
  225.   for i=1, math.abs(xOffset) do
  226.     forward()
  227.   end
  228.   -- Face needs to be 2
  229.   if face==0 then
  230.     oneEighty()
  231.   elseif face==1 then
  232.     turnRight()
  233.   elseif face==3 then
  234.     turnLeft()
  235.   end
  236.   -- Move to chest
  237.   for i=1, yOffset do
  238.     forward()
  239.   end
  240.   -- Move down
  241.   while not turtle.down() do
  242.     os.sleep(0.1)
  243.   end
  244.   -- Face back again
  245.   oneEighty()
  246. end
  247.  
  248. -- This long function takes control of the turtle, it
  249. -- will return to where it was placed when this program
  250. -- was run and drop all of it's items below itself, then
  251. -- return back to where it was and continue mining.
  252. function deposit()
  253.   -- Store value so they can be reset
  254.   local _xOffset = xOffset
  255.   local _yOffset = yOffset
  256.   local _face = face
  257.   -- Returns to the chest
  258.   recall()
  259.   -- Drop all items
  260.   for i=16, 2, -1 do
  261.     turtle.select(i)
  262.     turtle.dropDown(64)
  263.   end
  264.   -- Reselect the torches
  265.   turtle.select(1)
  266.   -- If we're not carrying torches, drop'em
  267.   if not placeTorches then
  268.     turtle.dropDown(64)
  269.   end
  270.   -- Move up
  271.   while not turtle.up() do
  272.     os.sleep(0.1)
  273.   end
  274.   -- Move back to the branch it was at
  275.   for i=1, _yOffset do
  276.     forward()
  277.   end
  278.   -- Turn to face '_face'
  279.   for i=1, _face do
  280.     turnRight()
  281.   end
  282.   -- Go back to original spot in branch
  283.   for i=1, math.abs(_xOffset) do
  284.     forward()
  285.   end
  286.   -- Reset our values just in case
  287.   xOffset = _xOffset
  288.   yOffset = _yOffset
  289.   face = _face
  290. end
  291.  
  292. function branch()
  293.   for i=1, length do
  294.     delete()
  295.     if dropItems then
  296.       if inventoryFull() then
  297.         deposit()
  298.       end
  299.     end
  300.     forward()
  301.     increment(1)
  302.     turtle.digDown()
  303.     if dropItems then
  304.       if inventoryFull() then
  305.         deposit()
  306.       end
  307.     end
  308.   end
  309.   back(1)
  310.   if placeTorches then
  311.     turtle.place()
  312.   end
  313.   back(length-1)
  314.   increment(-length)
  315. end
  316.  
  317. function main()
  318.   if parseArguments()~=0 then
  319.     return -1
  320.   end
  321.   while turtle.detectUp() do
  322.     turtle.digUp()
  323.     os.sleep(0.2)
  324.   end
  325.   while not turtle.up() do
  326.     os.sleep(0.1)
  327.   end
  328.   for i=1, branches do
  329.     for j=0, space do
  330.       delete()
  331.       if dropItems then
  332.         if inventoryFull() then
  333.           deposit()
  334.         end
  335.       end
  336.       forward()
  337.       increment(1)
  338.       turtle.digDown()
  339.       if dropItems then
  340.         if inventoryFull() then
  341.           deposit()
  342.         end
  343.       end
  344.     end
  345.     if placeTorches then
  346.       turtle.placeDown()
  347.     end
  348.     turnLeft()
  349.     branch()
  350.     oneEighty()
  351.     branch()
  352.     turnLeft()
  353.   end
  354.   recall()
  355.   return 0
  356. end
  357.  
  358. main()
Advertisement
Add Comment
Please, Sign In to add comment