tmdart

Computercraft Turtle Sugar Farm

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