MrHG

SDChopper 1.0

Aug 6th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | None | 0 0
  1. -- uses Robust Turtle API: pastebin get 0TnEBf2P t
  2. os.loadAPI("t")
  3. -- local args = {...}
  4. local versionNumber = "1.0"
  5. local programName = "StarDustChopper"
  6. local programNameShort = "SDChopper"
  7. local startInventory = 3
  8. local treeDistance = 1
  9. local refuelAmount = 80
  10. local distanceFromContainer = 0
  11. local saplingName = nil
  12.  
  13. --if #args ~= 1 then
  14. --  print ("Usage: "..programNameShort.." (Amount of Simultaneous Trees)")
  15. --  return
  16. --else
  17. --  treeAmount = tonumber(args[1])
  18. --  if treeAmount < 1 then
  19. --      print("Amount Of trees must be positive")
  20. --      return
  21. --  end
  22.    
  23. function Refuel(amountOfRequiredFuel)
  24.     turtle.select(1)
  25.     if (turtle.getFuelLevel() == "unlimited") then
  26.         -- that's pretty neat dude.
  27.         return
  28.     end
  29.     if (turtle.getFuelLevel() < amountOfRequiredFuel) then
  30.         while (true) do
  31.             if turtle.refuel(1) then
  32.                 if (turtle.getFuelLevel() > amountOfRequiredFuel) then
  33.                     break
  34.                 end
  35.             else
  36.                 print("Turtle could not refuel. Please check why.")
  37.                 error()
  38.             end
  39.         end
  40.     end
  41. end
  42.  
  43. function CheckSaplings()
  44.     turtle.select(2)
  45.     if turtle.getItemDetail() then
  46.         if saplingName ~= nil then
  47.             if turtle.getItemDetail().name == saplingName then
  48.                 return true
  49.             else
  50.                 print("Foreign object in Sapling Area. Looking for others.")
  51.                     turtle.transferTo(16)
  52.                 for i=1,16 do
  53.                     turtle.select(i)
  54.                     if turtle.getItemDetail() ~= nil then
  55.                         if turtle.getItemDetail().name == saplingName then
  56.                             turtle.transferTo(2)
  57.                             print("Found others. Continuing.")
  58.                             return true
  59.                         end
  60.                     end
  61.                 end
  62.                 print("Could not find more saplings.")
  63.                 CheckSaplings()
  64.             end
  65.         else
  66.             saplingName = turtle.getItemDetail().name
  67.         end
  68.     else
  69.         print("Please insert saplings. Retrying in 5 Seconds.")
  70.         sleep(5)
  71.         CheckSaplings()
  72.     end
  73. end
  74.  
  75. function CheckIfGrown()
  76.     turtle.select(2)
  77.     if not turtle.detect() then
  78.         CheckSaplings()
  79.         t.place(2)
  80.         return false
  81.     elseif turtle.compare() then
  82.         return false
  83.     else
  84.         return true
  85.     end
  86. end
  87.  
  88. function CutTree()
  89.     turtle.select(2)
  90.     t.dig()
  91.     t.forward()
  92.     t.digUp()
  93.     t.up()
  94.     t.digUp()
  95.     t.up()
  96.     local upCount = 2
  97.     while turtle.detectUp() do
  98.         t.digUp()
  99.         t.up()
  100.         upCount = upCount + 1
  101.         t.dig()
  102.         t.right()
  103.         t.dig()
  104.         t.right()
  105.         t.dig()
  106.         t.right()
  107.         t.dig()
  108.         t.right()
  109.     end
  110.     t.down(upCount)
  111.     t.back()
  112. end
  113.  
  114. function EmptyInventory()
  115.     local itemsDepositedCount = 0
  116.     for i=startInventory,16 do
  117.         turtle.select(i)
  118.         itemsDepositedCount = itemsDepositedCount + turtle.getItemCount()
  119.         if turtle.getItemCount() == 0 then
  120.             --twiddle your thumbs
  121.         elseif not turtle.drop() then
  122.             print("Container full. Please empty it.")
  123.             error()
  124.         end
  125.     end
  126.     print("Deposited "..itemsDepositedCount.." Items.")
  127. end
  128.  
  129. function CheckInventory()
  130.     turtle.select(6)
  131.     if turtle.getItemCount() > 0 then
  132.         t.turnAround()
  133.         t.forward(distanceFromContainer)
  134.         EmptyInventory()
  135.         t.turnAround()
  136.         t.forward(distanceFromContainer)
  137.     end
  138. end
  139.  
  140. function Farm()
  141.     if CheckSaplings() then
  142.         if CheckIfGrown() then
  143.             CutTree()
  144.             if CheckSaplings() then
  145.                 CheckIfGrown()
  146.             end
  147.         end
  148.     end
  149. end
  150.  
  151.     -- INITIAL SETUP FASE
  152. term.clear()
  153. term.setCursorPos(1,1)
  154. print("Welcome to"..programName.."Version "..versionNumber.." !")
  155. print("(Coded by MrHG using RobustTurtleAPI)")
  156. print("")
  157. sleep(1)
  158. print("Insert Fuel in slot 1")
  159. print("Insert Sapling in slot 2")
  160. Refuel(80)
  161. CheckSaplings()
  162. t.turnAround()
  163. for i=1,5 do
  164.     if turtle.detect() then
  165.         print("Block found. Assuming it is Container.")
  166.         t.turnAround()
  167.         t.forward(distanceFromContainer)
  168.         break
  169.     else
  170.         t.forward()
  171.         distanceFromContainer = distanceFromContainer + 1
  172.     end
  173.     if i == 5 then
  174.         print("No container within reach.")
  175.         print("Would you like to proceed without one? (y/n)")
  176.         if read() == "y" then
  177.             print("Understood. Proceeding without container.")
  178.             t.turnAround()
  179.             t.forward(distanceFromContainer)
  180.             distanceFromContainer = 0
  181.         else
  182.             print("Please install container within reach before continuing.")
  183.             t.turnAround()
  184.             t.forward(distanceFromContainer)
  185.             error()
  186.         end
  187.     end
  188. end
  189.  
  190. function moveStraight()
  191.     t.strafeRight(treeDistance)
  192.     Refuel(refuelAmount)
  193.     Farm()
  194. end
  195.  
  196. function moveCorner()
  197.     t.strafeRight(treeDistance)
  198.     t.forward(2)
  199.     t.left()
  200.     Refuel(refuelAmount)
  201.     Farm()
  202. end
  203.     -- Main Block
  204. term.clear()
  205. term.setCursorPos(1,1)
  206. print("Commence Chopping!")
  207. print("")
  208. while true do
  209. -- AT START
  210.     CheckInventory()
  211.     Refuel(refuelAmount)
  212.     Farm()
  213.     -- OOO
  214.     -- O O
  215.     -- OXO
  216.     moveStraight()
  217.     -- OOO
  218.     -- O O
  219.     -- OOX
  220.     moveCorner()
  221.     -- OOO
  222.     -- O X
  223.     -- OOO
  224.     moveStraight()
  225.     -- OOX
  226.     -- O O
  227.     -- OOO
  228.     moveCorner()
  229.     -- OXO
  230.     -- O O
  231.     -- OOO
  232.     moveStraight()
  233.     -- XOO
  234.     -- O O
  235.     -- OOO
  236.     moveCorner()
  237.     -- OOO
  238.     -- X O
  239.     -- OOO
  240.     moveStraight()
  241.     -- OOO
  242.     -- O O
  243.     -- XOO
  244.     moveCorner()
  245.     -- OOO
  246.     -- O O
  247.     -- OXO
  248.     if CheckSaplings() then
  249.         local timesChecked = 0
  250.         while not CheckIfGrown() do
  251.             cursorPosx,cursorPosy = term.getCursorPos()
  252.             if cursorPosy > 1 then
  253.                 term.setCursorPos(cursorPosx, 1)
  254.             end
  255.             term.setCursorPos(cursorPosx,cursorPosy-1)
  256.             term.clearLine()
  257.             print("Turtle has spun around "..timesChecked.." Times Since last harvest.")
  258.             t.turnAround()
  259.             t.turnAround()
  260.             sleep(10)
  261.             timesChecked = timesChecked + 1
  262.             if timesChecked > 30 then
  263.                 print("This is taking a while.. Attempting Detection Override.")
  264.                 break
  265.             end
  266.         end
  267.         CutTree()
  268.     end
  269. end
Add Comment
Please, Sign In to add comment