Advertisement
jcammyb

Computercraft - Basic farming turtle

Mar 20th, 2021 (edited)
4,861
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 1 0
  1. --[[
  2. --- Basic Farm ----
  3. 1. Refuel turtle with at least 8 coal only one time
  4. 2. Put any amount of seeds you want in the turtle inventory
  5. 3. Put 2 chest behind the turtle one on top of the other (Top chest has coal, bottom chest saves crops)
  6. 4. Place water base on the dimension size and clear the area from blocks ( farming turtles cant remove blocks, only flowers/grass/etc )
  7. 5. Run the program [ default args [1]FirstTime = true ]
  8. --]]
  9.  
  10. local DIMENSION = {x = 9, y = 9};
  11. local SEEDS = {"minecraft:carrot", "minecraft:potato", "minecraft:wheat_seeds"} -- Put any seed you want
  12.  
  13. -- DO NOT TOUCH
  14. local args = {...};
  15. local routes = 1;
  16. local firstTime = true;
  17.  
  18. function refuel()
  19.     local fueled = turtle.getFuelLevel() > 160;
  20.     if not fueled then
  21.         turtle.turnRight();
  22.         turtle.turnRight();
  23.         turtle.up();
  24.         if turtle.suck(32) then
  25.             print("Sarching for fuel...");
  26.             for i = 2, 16 do
  27.                 turtle.select(i)
  28.                 if turtle.refuel(0) then
  29.                     fueled = true;
  30.                     turtle.refuel(32);
  31.                     break
  32.                 end
  33.             end
  34.         end
  35.         turtle.down();
  36.         turtle.turnRight();
  37.         turtle.turnRight();
  38.     end
  39.     turtle.select(1);
  40.     print('Fuel Level -> ' .. turtle.getFuelLevel());
  41.     return fueled;
  42. end
  43.  
  44. function forceForward()
  45.     while turtle.detect() do turtle.dig(); end
  46.     turtle.forward();
  47. end
  48.  
  49. function traversePath(onTraverse)
  50.     for x = 1, DIMENSION.x do
  51.         for y = 1, DIMENSION.y do onTraverse(x, y) end
  52.         forceForward();
  53.         if (x % 2 == 0) then
  54.             turtle.turnRight();
  55.             forceForward();
  56.             turtle.turnRight();
  57.         else
  58.             turtle.turnLeft();
  59.             forceForward();
  60.             turtle.turnLeft();
  61.         end
  62.     end
  63. end
  64.  
  65. function goBackHome()
  66.     turtle.forward();
  67.     if DIMENSION.x % 2 == 0 then
  68.         turtle.turnRight();
  69.         forceForward();
  70.         turtle.turnRight();
  71.         forceForward();
  72.         turtle.turnLeft();
  73.         for x = 1, DIMENSION.x - 1 do forceForward(); end
  74.     else
  75.         for x = 1, DIMENSION.x do forceForward(); end
  76.         turtle.turnLeft();
  77.         for y = 1, DIMENSION.y do forceForward(); end
  78.     end
  79.     turtle.turnLeft();
  80.     turtle.down();
  81. end
  82.  
  83. function clearPath()
  84.     function clear(row, column)
  85.         forceForward();
  86.         if turtle.detectUp() then turtle.digUp(); end
  87.     end
  88.     traversePath(clear)
  89.     goBackHome();
  90. end
  91.  
  92. function plantCrops()
  93.     local planted = false;
  94.     for i = 1, 16 do
  95.         local item = turtle.getItemDetail(i);
  96.         if item ~= nil then
  97.             for x = 1, #SEEDS do
  98.                 if item.name == SEEDS[x] then
  99.                     turtle.select(i);
  100.                     planted = turtle.placeDown();
  101.                 end
  102.             end
  103.         end
  104.     end
  105.     return planted;
  106. end
  107.  
  108. function checkCrops()
  109.     print("Checking crops...");
  110.     turtle.up();
  111.     function check(row, column)
  112.         forceForward();
  113.         local success, item = turtle.inspectDown();
  114.         if (success and item.state.age == 7) then
  115.             turtle.digDown();
  116.             plantCrops();
  117.         elseif not success then
  118.             if not plantCrops() then
  119.                 turtle.digDown();
  120.                 plantCrops();
  121.             end
  122.         end
  123.         turtle.suckDown();
  124.     end
  125.     traversePath(check)
  126.     goBackHome();
  127. end
  128.  
  129. function saveCrops()
  130.     turtle.turnRight();
  131.     turtle.turnRight();
  132.     turtle.select(1);
  133.     local item = turtle.getItemDetail(1);
  134.     if item ~= nil then
  135.         local isSeed = false;
  136.         for x = 1, #SEEDS do
  137.             if item.name == SEEDS[x] then isSeed = true; end
  138.         end
  139.  
  140.         if not isSeed then turtle.drop(64) end
  141.     end
  142.     for i = 2, 16 do
  143.         turtle.select(i);
  144.         item = turtle.getItemDetail(i);
  145.         if item ~= nil then
  146.             local amount = turtle.getItemCount(i);
  147.             for x = 1, #SEEDS do
  148.                 if item.name == SEEDS[x] then
  149.                     turtle.transferTo(1, amount)
  150.                 end
  151.             end
  152.             if not turtle.drop(64) then
  153.                 print("Cant drop items.. trying again...")
  154.                 break
  155.             end
  156.         end
  157.     end
  158.     turtle.turnRight();
  159.     turtle.turnRight();
  160.     turtle.select(1);
  161. end
  162.  
  163. if args[1] ~= nil then firstTime = args[1] == "true"; end
  164. -- STARTS PROGRAM
  165. shell.run('clear')
  166. while true do
  167.     term.setCursorPos(1, 1);
  168.     print("--Starting farming process number: " .. routes)
  169.     if refuel() then
  170.         if firstTime then
  171.             clearPath();
  172.             firstTime = false;
  173.         end
  174.         checkCrops();
  175.         saveCrops();
  176.     end
  177.     textutils.slowPrint('Sleeping for 2 minutes....');
  178.     print("--")
  179.     os.sleep(120);
  180.     routes = routes + 1;
  181.     shell.run('clear')
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement