Advertisement
LazyNub

LazyNubChopChopV01

May 17th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.29 KB | None | 0 0
  1. -- Lazy Nub Chop Chop v0.01
  2. -- BY: LazyNub
  3. --
  4. -- These trees have issues with Lazy Nub chop chop
  5. -- Double size Fir trees will be recognized as single size
  6. -- Robust Twilight Oak isn't recognized correctly
  7. -- Redwood trees are not recognized correctly
  8. --  
  9. -- Most all trees are at least detected as single trees and can be
  10. -- chopped down with multiple chops
  11. --
  12. --[[  Log Entries:
  13.  
  14.  Project Started        May 17 '13
  15.  First Release          May 17 '13  LazyNub Chop Chop v0.01
  16.    
  17. ]]--
  18.  
  19.     local treeSize = 1
  20.  
  21. --[[ Robust Turtle
  22.  Modified version of
  23.  Robust Turtle API by SpeedR
  24.  integrated
  25. ]]--
  26. --Digging with gravel/sand detection
  27. function dig()
  28.   local tries = 0
  29.   while turtle.detect() do
  30.     turtle.dig()
  31.     sleep(0.4)
  32.     tries = tries + 1
  33.     if tries>500 then
  34.       print("Error: dug for too long.")
  35.       return false
  36.     end
  37.   end
  38.   return true
  39. end
  40.  
  41. function digUp()
  42.   local tries = 0
  43.   while turtle.detectUp() do
  44.     turtle.digUp()
  45.     sleep(0.4)
  46.     tries = tries + 1
  47.     if tries>500 then
  48.       print("Error: dug up for too long.")
  49.       return false
  50.     end
  51.   end
  52.   return true
  53. end
  54.  
  55. function digDown()
  56.   local tries = 0
  57.   while turtle.detectDown() do
  58.     turtle.digDown()
  59.     sleep(0.4)
  60.     tries = tries + 1
  61.     if tries>500 then
  62.       print("Error: dug down for too long.")
  63.       return false
  64.     end
  65.   end
  66.   return true
  67. end
  68.  
  69. --Traveling: Goes in the direction no matter what (almost)
  70. --Will not be stopped by blocks or mobs
  71. function forward(l)
  72.   l=l or 1
  73.   for i=1,l do
  74.     local tries = 0
  75.     while turtle.forward() ~= true do
  76.       turtle.dig()
  77.       turtle.attack()
  78.       sleep(0.2)
  79.       tries = tries + 1
  80.       if tries>500 then
  81.         print("Error: can't move forward.")
  82.         return false
  83.       end
  84.     end
  85.   end
  86.   return true
  87. end
  88.  
  89. function up(l)
  90.   l=l or 1
  91.   for i=1,l do
  92.     local tries = 0
  93.     while turtle.up() ~= true do
  94.       turtle.digUp()
  95.       turtle.attackUp()
  96.       sleep(0.2)
  97.       tries = tries + 1
  98.       if tries>500 then
  99.         print("Error: can't move up.")
  100.         return false
  101.       end
  102.     end
  103.   end
  104.   return true
  105. end
  106.  
  107. function down(l)
  108.   l=l or 1
  109.   for i=1,l do
  110.     local tries = 0
  111.     while turtle.down() ~= true do
  112.       turtle.digDown()
  113.       turtle.attackDown()
  114.       sleep(0.2)
  115.       tries = tries + 1
  116.       if tries>500 then
  117.         print("Error: can't move down.")
  118.         return false
  119.       end
  120.     end
  121.   end
  122.   return true
  123. end
  124.  
  125. function back(l)
  126.   l=l or 1
  127.   for i=1,l do
  128.     if turtle.back() ~= true then
  129.       turnAround()
  130.       forward()
  131.       turnAround()
  132.     end
  133.   end
  134. end
  135.  
  136. --Place blocks
  137. --Does not place when there's already the right block.
  138. function place(block)
  139.   turtle.select(block)
  140.   if turtle.compare()==false then
  141.     if turtle.getItemCount(block)==0 then
  142.       outOfResource(block)
  143.     end
  144.     dig()
  145.     turtle.place()
  146.   end
  147. end
  148.  
  149. function placeUp(block)
  150.   turtle.select(block)
  151.   if turtle.compareUp()==false then
  152.     if turtle.getItemCount(block)==0 then
  153.       outOfResource(block)
  154.     end
  155.     digUp()
  156.     turtle.placeUp()
  157.   end
  158. end
  159.  
  160. function placeDown(block)
  161.   turtle.select(block)
  162.   if turtle.compareDown()==false then
  163.     if turtle.getItemCount(block)==0 then
  164.       outOfResource(block)
  165.     end
  166.     digDown()
  167.     turtle.placeDown()
  168.   end
  169. end
  170.  
  171. local function outOfResource()
  172.   print("Ran out of a resource. Block: ",block , ".")
  173.   print("Refill, then say something to proceed.")
  174.   read()
  175. end
  176.  
  177. function placeRight(block)
  178.   turtle.turnRight()
  179.   place(block)
  180.   turtle.turnLeft()
  181. end
  182.  
  183. function placeLeft(block)
  184.   turtle.turnLeft()
  185.   place(block)
  186.   turtle.turnRight()
  187. end
  188.  
  189. function placeBack(block)
  190.   turnAround()
  191.   place(block)
  192.   turnAround()
  193. end
  194.  
  195. --place row     e.g. placeRow(up, marble, forward, 15)
  196. function placeRow(placeDir, block, travelDir, l)
  197.   l=l or 1
  198.   for i=1,l do
  199.     if placeDir == "forward" then
  200.       place(block)
  201.     elseif placeDir == "up" then
  202.       placeUp(block)
  203.     elseif placeDir == "down" then
  204.       placeDown(block)
  205.     elseif placeDir == "right" then
  206.       placeRight(block)
  207.     elseif placeDir == "left" then
  208.       placeLeft(block)
  209.     elseif placeDir == "back" then
  210.       placeBack(block)
  211.     else
  212.       print('"', placeDir, '" is not a valid direction!')
  213.       return false
  214.     end
  215.     if travelDir == "forward" then
  216.       forward()
  217.     elseif travelDir == "up" then
  218.       up()
  219.     elseif travelDir == "down" then
  220.       down()
  221.     elseif travelDir == "right" then
  222.       strafeRight()
  223.     elseif travelDir == "left" then
  224.       strafeLeft()
  225.     elseif travelDir == "back" then
  226.       back()
  227.     else
  228.       print('"', travelDir, '" is not a valid direction!')
  229.       return false
  230.     end
  231.   end
  232.   return true
  233. end
  234.  
  235. --Turning
  236. function turnAround()
  237.   turtle.turnRight()
  238.   turtle.turnRight()
  239. end
  240.  
  241. function right()
  242.   turtle.turnRight()
  243. end
  244.  
  245. function left()
  246.   turtle.turnLeft()
  247. end
  248.  
  249. function goRight(l)
  250.   l=l or 1
  251.   turtle.turnRight()
  252.   forward(l)
  253. end
  254.  
  255. function goLeft(l)
  256.   l=l or 1
  257.   turtle.turnLeft()
  258.   forward(l)
  259. end
  260.  
  261. function strafeRight(l)
  262.   l=l or 1
  263.   goRight(l)
  264.   turtle.turnLeft()
  265. end
  266.  
  267. function strafeLeft(l)
  268.   l=l or 1
  269.   goLeft(l)
  270.   turtle.turnRight()
  271. end
  272.  
  273. --[[ End of Robust Turtle
  274. ]]--
  275. function plant()                            -- Plants a sapling to save the Planet
  276.     if turtle.getItemCount(16) > 0 then
  277.         print("Planting Sapling")
  278.         place(16)
  279.     end
  280. end
  281.  
  282. function cutDouble() -- Cuts down a double wide tree
  283.     local ifleft = false
  284.    
  285.     print("Cutting Double")
  286.     turtle.select(1)
  287.     while turtle.compareUp() do -- going up trunk
  288.         digUp()
  289.         up()
  290.     end
  291.     digUp()
  292.     right()
  293.     turtle.select(1)
  294.     if turtle.compare() then
  295.         print("Cutting from Left")
  296.         isleft = false
  297.         dig()
  298.         forward()
  299.         right()
  300.         dig()
  301.         forward()
  302.         right()
  303.     else
  304.         print("Cutting from Right")
  305.         isleft = true
  306.         turnAround()
  307.         dig()
  308.         forward()
  309.         left()
  310.         dig()
  311.         forward()
  312.         left()
  313.     end
  314.     getfuel()
  315.     print("Going Down")
  316.     turtle.select(1)
  317.     while turtle.compareDown() do -- going down beating leaves and last quarter of double tree
  318.         for i = 1,4 do
  319.             dig()
  320.             left()
  321.         end
  322.         down()
  323.     end
  324.     for i = 1,4 do
  325.         turtle.select(1)
  326.         if turtle.compare() then
  327.             dig()
  328.         else
  329.             turtle.select(16)
  330.             turtle.suck()
  331.         end
  332.         right()
  333.     end
  334.     turtle.select(1)
  335.     i = 0
  336.     while turtle.compareDown() do -- see if stump continues down
  337.         print("Getting Stump now")
  338.         digDown()
  339.         down()
  340.         i = i+1
  341.     end
  342.     if i > 0 then
  343.         for j = 1,i do
  344.             up()
  345.         end
  346.     end
  347.     forward()
  348.     if isleft == true then
  349.         left()
  350.     else
  351.         right()
  352.     end
  353. end
  354. function cutTriple() -- Cuts down a triple wide tree
  355.  
  356.     local islayer = 1
  357.    
  358.     print("Cutting Triple")
  359.     turtle.select(1)
  360.     left()
  361.     if turtle.compare() then                        -- Checking for location on trunk start
  362.         turnAround()
  363.         if turtle.compare() then
  364.             islayer = 2
  365.             print("Cutting from middle")
  366.             back()
  367.         else
  368.             islayer = 3
  369.             print("Cutting from Right side")
  370.             turnAround()
  371.         end
  372.     else
  373.         islayer = 1
  374.         print("Cutting from Left side")
  375.         turnAround()
  376.     end
  377.     while turtle.compareUp() or turtle.compare() do
  378.         for i = 1,4 do
  379.             dig()
  380.             right()
  381.         end
  382.         digUp()
  383.         up()
  384.     end
  385.     getfuel()
  386.     forward()
  387.     print("Going Down Middle")
  388.     while not turtle.detectDown() do
  389.         for i = 1,4 do
  390.             dig()
  391.             right()
  392.         end
  393.         down()
  394.     end
  395.     for i = 1,4 do
  396.         dig()
  397.         left()
  398.     end
  399.     forward()
  400.     left()
  401.     print("Going Back Up Last Time")
  402.     while turtle.compareUp() or turtle.compare() do
  403.         for i = 1,4 do
  404.             dig()
  405.             right()
  406.         end
  407.         digUp()
  408.         up()
  409.     end
  410.     getfuel()
  411.     print("Going Down Last Time")
  412.     while not turtle.detectDown() do
  413.         down()
  414.     end
  415.     if islayer == 3 then
  416.         forward(2)
  417.         left()
  418.         forward(2)
  419.         left()
  420.     end
  421.     if islayer == 2 then
  422.         back(2)
  423.         strafeLeft()
  424.     end
  425.     if islayer == 1 then
  426.         back(2)
  427.         strafeLeft(2)
  428.     end
  429.     print("Whew Done.")
  430. end
  431. function fellTree() -- Cuts down a tree
  432.     treeSize = 1
  433.     getfuel()
  434.     turtle.select(1) -- Making sure initial chop goes into slot 1
  435.     dig()
  436.     forward()
  437.     if turtle.detect() then                 -- Checks to see if there is another block ahead
  438.         turtle.select(1)                    -- Makes Sure chopped log is selected for compare
  439.         if turtle.compare() then
  440.             print("Found double Maybe")     -- If there is another matching block is double wide
  441.             treeSize = 2
  442.             turtle.select(1)                -- Chop 2nd log and move forward to check for triple
  443.             dig()
  444.             forward()
  445.             if turtle.detect() then         -- Checks to see if there is another block ahead
  446.                 turtle.select(1)            -- Makes sure chopped log is selected for compare
  447.                 if turtle.compare() then
  448.                     print("Found triple Maybe") -- If there is another matching block is triple wide
  449.                     treeSize = 3
  450.                     turtle.select(1)
  451.                     dig()
  452.                 end
  453.             end
  454.         end
  455.     end                                     -- Now we know if it is a single wide,
  456.                                             -- double wide or triple wide tree.
  457.                                             -- Divert to routine for type of tree we
  458.                                             -- found and chop it down.
  459.     if treeSize == 3 then
  460.         cutTriple()
  461.     end
  462.     if treeSize == 2 then
  463.         cutDouble()
  464.     end
  465.                                             -- must be single so get to cutting
  466.     getfuel()
  467.     turtle.select(1)                        -- Place logs in slot 1
  468.     if treeSize == 1 then
  469.         print ("Chop Chop Going up Single Tree")
  470.         while turtle.detectUp() do          -- going up trunk
  471.             digUp()
  472.             up()
  473.         end
  474.         turtle.select(16)                   -- Place saplings in slot 16   
  475.         print("At the top, Going Down")
  476.         while not turtle.detectDown() do    -- going down beating leaves
  477.             for i = 1,4 do                  -- Spins and chops leaves on way down
  478.                 dig()
  479.                 left()
  480.                 turtle.suck()               -- Tries to pick-up any saplings
  481.             end
  482.             down()
  483.         end
  484.        
  485.         for i = 1,4 do                      -- One spin to catch saplings on ground
  486.             right()                         -- Noted unique opposite spin technique
  487.             turtle.suck()
  488.         end
  489.         turtle.select(1)                    -- Change back to log storage slot
  490.         i = 0
  491.         while turtle.compareDown() do       -- Check if stump continues down
  492.             print("Getting Stump")
  493.             digDown()
  494.             down()
  495.             i = i+1
  496.         end
  497.         if i then
  498.             turtle.select(16)               -- Change to saplings slot
  499.             print("Picking Up Saplings")
  500.             for j = 1,i do
  501.                 for k = 1,4 do
  502.                     turtle.suck()           -- pick-up saplings
  503.                     left()
  504.                 end
  505.                 up()
  506.             end
  507.         end
  508.     end
  509.     back()
  510.     plant()                                 -- Plant sapling when done to save planet
  511. end
  512.  
  513. function getfuel()                          -- Checks if enough fuel and refuels
  514.     print("Checking fuel levels")
  515.     if turtle.getFuelLevel() < 160 then
  516.         turtle.select(13)
  517.         turtle.refuel(2)
  518.     end
  519.     if turtle.getFuelLevel() < 160 and turtle.getItemCount(13) == 0 then
  520.         print("Emergency Fuel Needed!")
  521.         print("Fuel Level: "..turtle.getFuelLevel())
  522.         print("Using logs for fuel")
  523.         i = 2
  524.         while turtle.getFuelLevel() < 160 and i < 14 do
  525.             turtle.select(i)
  526.             turtle.refuel()
  527.             i=i+1
  528.         end
  529.     end
  530. end
  531.  
  532. --[[
  533.  
  534.      Main Program
  535.      
  536. ]]--
  537.  
  538. local running = true
  539.  
  540. print("Lazy Nub Chop Chop v0.01")
  541. print("===============================")
  542. print("Slot 1-12 for harvested logs")
  543. print("Slot 13 for fueling")
  544. print("Slot 16 for harvested saplings")
  545. print("===============================")
  546. print(" Chop Chop Is On The Job")
  547.  
  548. if turtle.getFuelLevel() < 160 then
  549.         turtle.select(13)
  550.         turtle.refuel()
  551. end
  552. if turtle.getFuelLevel() < 160 then
  553.     print("Need more fuel in slot 13 please.")
  554.     print("Place fuel in slot and restart me.")
  555.     print("I want atleast 160 I have only: "..turtle.getFuelLevel())
  556.     running = false
  557. end
  558.  
  559. if running then
  560.     fellTree()
  561. end
  562.  
  563. --[[ THE END
  564.  
  565. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement