Advertisement
Guest User

branch2

a guest
Jul 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 0 0
  1. local tArgs = {...}
  2. local length = tArgs[1]
  3. local numTunnels = tArgs[2]
  4. local inv = true
  5. local checkTorch = 0
  6. local height = 0
  7. local fullInvNum = 0
  8.  
  9. function checkOreId(name)
  10.     return name == 'minecraft:coal_ore'
  11.         or name == 'minecraft:iron_ore'
  12.         or name == 'minecraft:gold_ore'
  13.         or name == 'minecraft:diamond_ore'
  14.         or name == 'minecraft:lapis_ore'
  15.         or name == 'minecraft:redstone_ore'
  16.         or name == 'minecraft:lit_redstone_ore'
  17. end
  18.  
  19. function checkOre(func)
  20.     local success, data = func()
  21.  if not success then return false end
  22.  return checkOreId(data.name)
  23. end
  24.  
  25. function isOre()
  26.  return checkOre(turtle.inspect)
  27. end
  28.  
  29. function isOreUp()
  30.     return checkOre(turtle.inspectUp)
  31. end
  32.  
  33. function isOreDown()
  34.     return checkOre(turtle.inspectDown)
  35. end
  36.  
  37. function repeatSuccess(func)
  38.     while func() do end
  39. end
  40.  
  41. function dig()
  42.     repeatSuccess(turtle.dig)
  43. end
  44.  
  45. function digUp()
  46.     repeatSuccess(turtle.digUp)
  47. end
  48.  
  49. function digDown()
  50.     repeatSuccess(turtle.digDown)
  51. end
  52.  
  53. isUp = false
  54.  
  55. function searchAround()
  56.  print('searching for ores')
  57.  turtle.select(1)
  58.     if isOre() then
  59.         dig()
  60.         turtle.forward()
  61.         searchAround()
  62.         turtle.back()
  63.   turtle.place()
  64.     elseif isOreDown() then
  65.         digDown()
  66.         turtle.down()
  67.         searchAround()
  68.         turtle.up()
  69.         turtle.placeDown()
  70.     elseif isOreUp() then
  71.         digUp()
  72.         turtle.up()
  73.         searchAround()
  74.         turtle.down()
  75.         turtle.placeUp()
  76.     end
  77.    
  78.     turtle.turnLeft()
  79.     if isOre() then
  80.         dig()
  81.         turtle.forward()
  82.         searchAround()
  83.         turtle.back()
  84.         turtle.place()
  85.     end
  86.    
  87.     turtle.turnRight()
  88.     turtle.turnRight()
  89.     if isOre() then
  90.         dig()
  91.         turtle.forward()
  92.         searchAround()
  93.         turtle.back()
  94.         turtle.place()
  95.     end
  96.     turtle.turnLeft()
  97. end
  98.  
  99. -- The basic function to move forward and dig
  100. function tunnel()
  101.   -- These used throughout to erase screen
  102.   term.clear()
  103.   term.setCursorPos(1,1)
  104.   print("Mining...")
  105.   for i=1,length do
  106.     fuel()
  107.     dig()
  108.     turtle.forward()
  109.     digUp()
  110.     isUp = false
  111.     searchAround()
  112.     turtle.up()
  113.     isUp = true
  114.     searchAround()
  115.     turtle.down()
  116.     torch()
  117.   end
  118. end
  119.  
  120. -- The function to turn around and go the other direction down the tunnel, also clears side tunnels
  121. function turnAround()
  122.   term.clear()
  123.   term.setCursorPos(1,1)
  124.   print("Turning around...")
  125.   for k = 1,6 do
  126.     fuel()
  127.     dig()
  128.     turtle.forward()
  129.     digUp()
  130.     isUp = false
  131.     searchAround()
  132.     turtle.up()
  133.     isUp = true
  134.     searchAround()
  135.     turtle.down()
  136.     torch()
  137.   end
  138.  
  139.   checkInv()
  140.  
  141.   while turtle.back() == false do
  142.     turtle.turnRight()
  143.     turtle.turnRight()
  144.     dig()
  145.     for i = 1,3 do
  146.       turtle.forward()
  147.       dig()
  148.     end
  149.     turtle.turnRight()
  150.     turtle.turnRight()
  151.     for i = 1,3 do
  152.       turtle.back()  
  153.     end
  154.   end
  155.   turtle.back()
  156.   turtle.back()
  157. end
  158.  
  159. -- Clears the specified item from the inventory, most likely cobble, also stops on full inv
  160. function checkInv()
  161.   term.clear()
  162.   term.setCursorPos(1,1)
  163.   print("Trashing...")
  164.   for y = 2,16 do
  165.     turtle.select(y)
  166.     if turtle.compareTo(1) then
  167.       turtle.drop()
  168.     end
  169.   end
  170. end
  171.    
  172. function fullInv()
  173.   checkInv()
  174.   -- To not spill items everywhere, some credit to link0153 :)
  175.   for i = 1,16 do
  176.     if turtle.getItemCount(i) > 0 then
  177.       fullInvNum = fullInvNum + 1
  178.     end
  179.   end
  180.   while fullInvNum == 16 do
  181.     term.clear()
  182.     term.setCursorPos(1,1)
  183.     print("Inventory full, please clear.")
  184.     sleep(5)
  185.     for i = 1,16 do
  186.       if turtle.getItemCount(i) == 0 then
  187.         fullInvNum = 0
  188.       end
  189.     end
  190.   end
  191.   fullInvNum = 0
  192.   term.clear()
  193.   term.setCursorPos(1,1)
  194.   print("Mining...")
  195. end
  196.  
  197. -- Checks how long since the last torch, then decides to place one above or not
  198. function torch()
  199.   -- Checks if low on torches
  200.   if turtle.getItemCount(16) < 5 then
  201.     term.clear()
  202.     term.setCursorPos(1,1)
  203.     print("Waiting for torches...place in bottom right.")
  204.     while turtle.getItemCount(16) < 5 do
  205.       sleep(2)
  206.     end
  207.   end
  208.   term.clear()
  209.   term.setCursorPos(1,1)
  210.   print("Mining...")
  211.   if checkTorch == 8 then
  212.     fuel()
  213.     turtle.select(16)
  214.     turtle.up()
  215.     digUp()
  216.     -- This if and loop will bring the turtle back down from the placed torch
  217.     if turtle.placeUp() or turtle.getItemCount(16) == 0 then
  218.         for i = 0,height do
  219.           fuel()
  220.           turtle.down()
  221.         end
  222.         turtle.select(1)
  223.         checkTorch = 0
  224.         height = 0
  225.     else
  226.         height = height + 1
  227.         torch()
  228.     end
  229.   -- If it's too soon, dont place a torch
  230.   else
  231.     checkTorch = checkTorch + 1
  232.   end
  233. end
  234.  
  235. -- Checks the fuel level, asks for more if necessary
  236. function fuel()
  237.   if turtle.getFuelLevel() < 50 then
  238.     print ("Wating for fuel...place in bottom left.")    
  239.     while turtle.getFuelLevel() < 50 do
  240.       turtle.select(13)
  241.       turtle.refuel()
  242.       sleep(2)
  243.     end
  244.   end
  245.   term.clear()
  246.   term.setCursorPos(1,1)
  247.   print("Mining...")
  248.   turtle.select(1)
  249. end
  250.  
  251. -- This function is a one time startup function, makes sure the turtle has the necessary materials
  252. function begin()
  253.   turtle.select(1)
  254.   -- Checks for trash item
  255.   if turtle.getItemCount(1) == 0 then
  256.     print("Please place an item to trash in the top left.")
  257.     while turtle.getItemCount(1) == 0 do
  258.       sleep(1)
  259.     end
  260.     term.clear()
  261.     term.setCursorPos(1,1)
  262.     begin()
  263.   -- Confirms trashed item
  264.   else
  265.     print("The item in the top left will be trashed...confirm?")
  266.     os.pullEvent("key")
  267.   end
  268.   term.clear()
  269.   term.setCursorPos(1,1)
  270. end
  271.  
  272. -- From here is the main code  
  273.  
  274. -- Checks for correct args
  275. if not (tArgs[1] == nil or tArgs[2] == nil) then
  276.   begin()
  277.   -- Loops for the specified # of tunnels, one tunnel being up and back
  278.   for j = 1,numTunnels do
  279.     tunnel()
  280.     fullInv()
  281.     turtle.turnLeft()
  282.     turnAround()
  283.     fullInv()
  284.     turtle.turnLeft()
  285.     tunnel()
  286.     fullInv()
  287.     turtle.turnRight()
  288.     turnAround()
  289.     fullInv()
  290.     turtle.turnRight()
  291.   end
  292.   -- Resets the screen and displays a confirmation of finishing
  293.   term.clear()
  294.   term.setCursorPos(1,1)
  295.   print("Done!")
  296. else
  297.   term.clear()
  298.   term.setCursorPos(1,1)
  299.   print("Not enough arguments, should be used 'branch [length] [number of tunnels]'")
  300. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement