LazyNub

LazyNubSugarCaneHarvester

May 31st, 2013
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.11 KB | None | 0 0
  1. --[[
  2.         Lazy Nub's Sugar Cane Harvester v0.01a
  3.        
  4.         This program will teach your turtle how to
  5.         harvest a micro farm of sugar cane crops.
  6.        
  7.         Things you will need are:
  8.        
  9.         1 Turtle (Felling,digging,mining does not matter)
  10.         1 chest (wood,iron,gold,diamond you pick)
  11.         1 bucket of water
  12.         4 pieces of sugar cane to plant
  13.         and a fuel source like coal. (fueling slot is slot 1)
  14.        
  15.         Link to you tube video: http://youtu.be/34B3dsVHU4s
  16.         Link to post of program: http://www.computercraft.info/forums2/index.php?/topic/13176-lazy-nub-sugar-cane-farmer-v001/
  17.         Pastebin link: http://pastebin.com/wG6EartB
  18.        
  19.         Type the following into your turtle to load program: (Must have http enabled)
  20.         pastebin get wG6EartB sugarfarm
  21.        
  22.         There are two modes you can run the harvester in:
  23.         Regular and Economy
  24.         In Regular mode you will need to supply fuel to use it.
  25.         In Economy mode you need no fuel in the turtle to use it.
  26.        
  27.         Just start the program as normal to use regular mode.
  28.         To enter economy mode start the program with /e after the program name.
  29.        
  30.         ie:  sugarfarm /e
  31.        
  32.        
  33. ]]--
  34.    
  35.  
  36.        
  37. --[[ Robust Turtle
  38.  Modified version of
  39.  Robust Turtle API by SpeedR
  40.  integrated
  41. ]]--
  42. --Digging with gravel/sand detection
  43.  
  44.         local maxTries = 5
  45.        
  46. function dig()
  47.   local tries = 0
  48.   while turtle.detect() do
  49.     turtle.dig()
  50.     sleep(0.5)
  51.     tries = tries + 1
  52.     if tries>maxTries then
  53.       print("Error: dug for too long.")
  54.       return false
  55.     end
  56.   end
  57.   return true
  58. end
  59.  
  60. function digUp()
  61.   local tries = 0
  62.   while turtle.detectUp() do
  63.     turtle.digUp()
  64.     sleep(0.5)
  65.     tries = tries + 1
  66.     if tries>maxTries then
  67.       print("Error: dug up for too long.")
  68.       return false
  69.     end
  70.   end
  71.   return true
  72. end
  73.  
  74. function digDown()
  75.   local tries = 0
  76.   while turtle.detectDown() do
  77.     turtle.digDown()
  78.     sleep(0.5)
  79.     tries = tries + 1
  80.     if tries>maxTries then
  81.       print("Error: dug down for too long.")
  82.       return false
  83.     end
  84.   end
  85.   return true
  86. end
  87.  
  88.  
  89. --Traveling: Goes in the direction no matter what (almost)
  90. --Will not be stopped by blocks or mobs
  91. function forward(l)
  92.   l=l or 1
  93.   for i=1,l do
  94.     local tries = 0
  95.     while turtle.forward() ~= true do
  96.       turtle.dig()
  97.       turtle.attack()
  98.       sleep(0.2)
  99.       tries = tries + 1
  100.       if tries>maxTries then
  101.         print("Error: can't move forward.")
  102.         return false
  103.       end
  104.     end
  105.   end
  106.   return true
  107. end
  108.  
  109. function up(l)
  110.   l=l or 1
  111.   for i=1,l do
  112.     local tries = 0
  113.     while turtle.up() ~= true do
  114.       turtle.digUp()
  115.       turtle.attackUp()
  116.       sleep(0.2)
  117.       tries = tries + 1
  118.       if tries>maxTries then
  119.         print("Error: can't move up.")
  120.         return false
  121.       end
  122.     end
  123.   end
  124.   return true
  125. end
  126.  
  127. function down(l)
  128.   l=l or 1
  129.   for i=1,l do
  130.     local tries = 0
  131.     while turtle.down() ~= true do
  132.       turtle.digDown()
  133.       turtle.attackDown()
  134.       sleep(0.2)
  135.       tries = tries + 1
  136.       if tries>maxTries then
  137.         print("Error: can't move down.")
  138.         return false
  139.       end
  140.     end
  141.   end
  142.   return true
  143. end
  144.  
  145. function back(l)
  146.   l=l or 1
  147.   for i=1,l do
  148.     if turtle.back() ~= true then
  149.       turnAround()
  150.       forward()
  151.       turnAround()
  152.     end
  153.   end
  154. end
  155.  
  156.  
  157. --Place blocks
  158. --Does not place when there's already the right block.
  159. function place(block)
  160.   turtle.select(block)
  161.   if turtle.compare()==false then
  162.     if turtle.getItemCount(block)==0 then
  163.       outOfResource(block)
  164.     end
  165.     dig()
  166.     turtle.place()
  167.   end
  168. end
  169.  
  170. function placeUp(block)
  171.   turtle.select(block)
  172.   if turtle.compareUp()==false then
  173.     if turtle.getItemCount(block)==0 then
  174.       outOfResource(block)
  175.     end
  176.     digUp()
  177.     turtle.placeUp()
  178.   end
  179. end
  180.  
  181. function placeDown(block)
  182.   turtle.select(block)
  183.   if turtle.compareDown()==false then
  184.     if turtle.getItemCount(block)==0 then
  185.       outOfResource(block)
  186.     end
  187.     digDown()
  188.     turtle.placeDown()
  189.   end
  190. end
  191.  
  192. local function outOfResource()
  193.   print("Ran out of a resource. Block: ",block , ".")
  194.   print("Refill, then say something to proceed.")
  195.   read()
  196. end
  197.  
  198. function placeRight(block)
  199.   turtle.turnRight()
  200.   place(block)
  201.   turtle.turnLeft()
  202. end
  203.  
  204. function placeLeft(block)
  205.   turtle.turnLeft()
  206.   place(block)
  207.   turtle.turnRight()
  208. end
  209.  
  210. function placeBack(block)
  211.   turnAround()
  212.   place(block)
  213.   turnAround()
  214. end
  215.  
  216. --place row     e.g. placeRow(up, marble, forward, 15)
  217. function placeRow(placeDir, block, travelDir, l)
  218.   l=l or 1
  219.   for i=1,l do
  220.     if placeDir == "forward" then
  221.       place(block)
  222.     elseif placeDir == "up" then
  223.       placeUp(block)
  224.     elseif placeDir == "down" then
  225.       placeDown(block)
  226.     elseif placeDir == "right" then
  227.       placeRight(block)
  228.     elseif placeDir == "left" then
  229.       placeLeft(block)
  230.     elseif placeDir == "back" then
  231.       placeBack(block)
  232.     else
  233.       print('"', placeDir, '" is not a valid direction!')
  234.       return false
  235.     end
  236.     if travelDir == "forward" then
  237.       forward()
  238.     elseif travelDir == "up" then
  239.       up()
  240.     elseif travelDir == "down" then
  241.       down()
  242.     elseif travelDir == "right" then
  243.       strafeRight()
  244.     elseif travelDir == "left" then
  245.       strafeLeft()
  246.     elseif travelDir == "back" then
  247.       back()
  248.     else
  249.       print('"', travelDir, '" is not a valid direction!')
  250.       return false
  251.     end
  252.   end
  253.   return true
  254. end
  255.  
  256.  
  257. --Turning
  258. function turnAround()
  259.   turtle.turnRight()
  260.   turtle.turnRight()
  261. end
  262.  
  263. function right()
  264.   turtle.turnRight()
  265. end
  266.  
  267. function left()
  268.   turtle.turnLeft()
  269. end
  270.  
  271. function goRight(l)
  272.   l=l or 1
  273.   turtle.turnRight()
  274.   forward(l)
  275. end
  276.  
  277. function goLeft(l)
  278.   l=l or 1
  279.   turtle.turnLeft()
  280.   forward(l)
  281. end
  282.  
  283. function strafeRight(l)
  284.   l=l or 1
  285.   goRight(l)
  286.   turtle.turnLeft()
  287. end
  288.  
  289. function strafeLeft(l)
  290.   l=l or 1
  291.   goLeft(l)
  292.   turtle.turnRight()
  293. end
  294. --[[ End of Robust Turtle
  295. ]]--
  296.  
  297. --[[
  298.     MAIN LOOP START HERE
  299. ]]--
  300.    
  301.     local args = { ... }
  302.     local running = true
  303.     local isEconoMode = false
  304.     local currFuelLevel = 0
  305.    
  306. if (#args == 0) then                -- start in Economy Mode or Regular
  307.     isEconoMode = false
  308.     print("Entering Regular Mode")
  309. elseif (args[1] == "/e") then
  310.     isEconoMode = true
  311.     print("Economy Mode Active")
  312. end
  313.    
  314. while running do                    -- start of main loop
  315.     while not turtle.dig() do       -- start loop looking for harvest-able crop
  316.         term.clear()
  317.         if isEconoMode then
  318.             print("Economy Mode Working...")
  319.         else
  320.             print("Regular Mode Working...")
  321.         end
  322.         left()
  323.         turtle.select(1)            -- make sure on fuel slot
  324.         if not isEconoMode then
  325.             currFuelLevel = turtle.getFuelLevel()
  326.             while (currFuelLevel ~= "unlimited")
  327.             and (currFuelLevel < 4) do  -- if out of fuel stop to refuel
  328.                 print("Need fuel, refuelling from slot 1")
  329.                 turtle.refuel()
  330.                 currFuelLevel = turtle.getFuelLevel()
  331.             end
  332.         end
  333.     end
  334.                                     -- got growth so time to harvest
  335.     dig()
  336.     if not isEconoMode then
  337.         down()
  338.         dig()
  339.     end
  340.     turtle.dropDown()
  341.     if not isEconoMode then
  342.         up()
  343.     end
  344. end -- end of program
Advertisement
Add Comment
Please, Sign In to add comment