originalceebs

CCTurtleFarming

Dec 30th, 2020 (edited)
4,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. --[[
  2.  
  3. Script Created by CEEBS
  4. YouTube: https://www.youtube.com/channel/UCQvB8QBknoS1jYgwpYGQrQg
  5. Twitter: https://twitter.com/OnlyCeebs
  6.  
  7. Really appreciate all the support you have given me, so thank you!
  8.  
  9. ]]--
  10.  
  11. local INVENTORY_SIZE = 16
  12. local MAIN_LOOP_INTERVAL = 420
  13. local FUEL_CROP_CHEST_INTERVAL = 5
  14. local length = 0
  15. local rows = 0
  16.  
  17. -- Receive arguments and validate user input
  18. if #arg == 2 then
  19.     length = tonumber(arg[1])
  20.     rows = tonumber(arg[2])
  21.  
  22.     if length <= 1 or rows <= 1 then
  23.         print("Please enter values bigger than 1")
  24.         return
  25.     end
  26. else
  27.     print("Please enter both the length and with of the farm area. (e.g. scriptName 10 10)")
  28.     return
  29. end
  30.  
  31. -- List of accepted fuels
  32. local ACCEPTED_FUELS = {
  33.     "minecraft:coal_block",
  34.     "minecraft:coal"
  35. }
  36.  
  37. -- List of accepted seeds
  38. local SEEDS = {
  39.     "minecraft:carrot",
  40.     "minecraft:potato",
  41.     "minecraft:wheat_seeds"
  42. }
  43.  
  44. -- List of mature crops
  45. local CROPS = {
  46.     "minecraft:carrots",
  47.     "minecraft:potatoes",
  48.     "minecraft:wheat"
  49. }
  50.  
  51. -- Refuel using the found fuel
  52. function refuel(slot_number)
  53.     turtle.select(slot_number)
  54.     turtle.refuel()
  55. end
  56.  
  57. -- Check the current fuel level
  58. function checkFuelLevel()
  59.    
  60.     local currentFuelLevel = turtle.getFuelLevel()
  61.  
  62.     if currentFuelLevel <= 0 then
  63.         print("Out of fuel, waiting for fuel to be added. Checking for fuel every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  64.         while currentFuelLevel <= 0 do
  65.             for i = 1, INVENTORY_SIZE do
  66.                 local currentItem = turtle.getItemDetail(i)
  67.                 if currentItem ~= nil then
  68.                     for x = 1, #ACCEPTED_FUELS do
  69.                         if currentItem.name == ACCEPTED_FUELS[x] then
  70.                             refuel(i)
  71.                         end
  72.                     end
  73.                 end
  74.             end
  75.             sleep(FUEL_CROP_CHEST_INTERVAL)
  76.             currentFuelLevel = turtle.getFuelLevel()
  77.         end
  78.         print("Fuel added successfully, continuing...")
  79.     end
  80. end
  81.  
  82. -- Get the amount of seeds
  83. function getSeedsCount()
  84.     local seedsCount = 0
  85.  
  86.     for i = 1, INVENTORY_SIZE do
  87.         local currentItem = turtle.getItemDetail(i)
  88.         if currentItem ~= nil then
  89.             for x = 1, #SEEDS do
  90.                 if currentItem.name == SEEDS[x] then
  91.                     seedsCount = seedsCount + turtle.getItemCount(i)
  92.                 end
  93.             end
  94.         end
  95.     end
  96.  
  97.     return seedsCount
  98. end
  99.  
  100. -- Checking for seeds
  101. function checkSeedsCount()
  102.    
  103.     local seedsCount = getSeedsCount()
  104.  
  105.     if seedsCount <= 0 then
  106.         print("Out of seeds, waiting for seeds to be added. Checking for seeds every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  107.         while seedsCount <= 0 do
  108.             seedsCount = getSeedsCount()
  109.             sleep(FUEL_CROP_CHEST_INTERVAL)
  110.         end
  111.         print("Seeds added successfully, continuing...")
  112.     end
  113.  
  114. end
  115.  
  116. -- Movement helper functions
  117. function moveForward(times)
  118.  
  119.     checkFuelLevel()
  120.     checkSeedsCount()
  121.    
  122.     if times then
  123.         for i = 1, times do
  124.             turtle.forward()
  125.             turtle.suckDown()
  126.         end
  127.     else
  128.         turtle.forward()
  129.         turtle.suckDown()
  130.     end
  131. end
  132.  
  133. function turnLeft(times)
  134.     if times then
  135.         for i = 1, times do
  136.             turtle.turnLeft()
  137.         end
  138.     else
  139.         turtle.turnLeft()
  140.     end
  141. end
  142.  
  143. function turnRight(times)
  144.     if times then
  145.         for i = 1, times do
  146.             turtle.turnRight()
  147.         end
  148.     else
  149.         turtle.turnRight()
  150.     end
  151. end
  152.  
  153. -- Plant our crops!
  154. function plantCrops()
  155.     for i = 1, INVENTORY_SIZE do
  156.         local currentItem = turtle.getItemDetail(i)
  157.         if currentItem ~= nil then
  158.             for x = 1, #SEEDS do
  159.                 if currentItem.name == SEEDS[x] then
  160.                     turtle.select(i)
  161.                     turtle.placeDown()
  162.                     break
  163.                 end
  164.             end
  165.         end
  166.     end
  167. end
  168.  
  169. -- Harvest our crops!
  170. function harvestCrops()
  171.     local isBlock, block = turtle.inspectDown()
  172.  
  173.     if isBlock ~= false then
  174.         local isCrop = false
  175.  
  176.         for x = 1, #CROPS do
  177.             if block.name == CROPS[x] then
  178.                 isCrop = true
  179.             end
  180.         end
  181.  
  182.         if isCrop then
  183.             if block.state.age == 7 then
  184.                 turtle.digDown()
  185.                 turtle.suckDown()
  186.                 plantCrops()
  187.             end
  188.         else
  189.             if block.name == "minecraft:grass" then
  190.                 turtle.digDown()
  191.                 turtle.digDown()
  192.                 turtle.suckDown()
  193.                 plantCrops()
  194.             end
  195.         end
  196.                
  197.     else
  198.         turtle.digDown()
  199.         turtle.suckDown()
  200.         plantCrops()
  201.     end
  202. end
  203.  
  204. -- Inventory sorting
  205. function inventorySort()
  206.     for j = 1, INVENTORY_SIZE do
  207.         local currentItem = turtle.getItemDetail(j)
  208.  
  209.         if currentItem ~= nil then
  210.             turtle.select(j)
  211.             for k = j, INVENTORY_SIZE do
  212.                 if turtle.compareTo(k) then
  213.                     turtle.select(k)
  214.                     turtle.transferTo(j)
  215.                     turtle.select(j)
  216.                 end
  217.             end
  218.         end
  219.     end
  220. end
  221.  
  222. -- Dump inventory
  223. function dumpInventory()
  224.  
  225.     local cropCount = 0
  226.     local requiredSeedsCount = math.ceil(length * rows)
  227.  
  228.     for i = 1, INVENTORY_SIZE do
  229.  
  230.         local currentItem = turtle.getItemDetail(i)
  231.  
  232.         if currentItem ~= nil then
  233.  
  234.             local isFuel = false
  235.             local isCrop = false
  236.  
  237.             for x = 1, #ACCEPTED_FUELS do
  238.                 if currentItem.name == ACCEPTED_FUELS[x] then
  239.                     isFuel = true
  240.                     break
  241.                 end
  242.             end
  243.  
  244.             if not isFuel then
  245.                 for x = 1, #SEEDS do
  246.                     if currentItem.name == SEEDS[x] then
  247.                         if cropCount < requiredSeedsCount then
  248.                             cropCount = cropCount + currentItem.count
  249.                             isCrop = true
  250.                         else
  251.                             isCrop = false
  252.                         end
  253.                     end
  254.                 end
  255.             end
  256.  
  257.             if not isCrop and not isFuel then
  258.                 turtle.select(i)
  259.                 while turtle.dropDown() == false do
  260.                     print("No room in target inventory, please clear some space. Checking target inventory every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  261.                     sleep(FUEL_CROP_CHEST_INTERVAL)
  262.                 end
  263.             end
  264.         end
  265.     end
  266.  
  267.     inventorySort()
  268. end
  269.  
  270. -- Farming loop
  271. function farm()
  272.  
  273.     print("Starting farm loop.")
  274.  
  275.     for i = 1, rows do
  276.  
  277.         if i ~= 1 then
  278.             for j = 1, length - 1 do
  279.                 moveForward()
  280.                 harvestCrops()
  281.             end  
  282.         else
  283.             for j = 1, length do
  284.                 moveForward()
  285.                 harvestCrops()
  286.             end  
  287.         end  
  288.  
  289.         if i == rows then
  290.             if i % 2 == 0 then
  291.                 moveForward()
  292.                 turnRight()
  293.                 moveForward(rows - 1)
  294.                 turnRight()
  295.             else
  296.                 turnRight()
  297.                 moveForward()
  298.                 turnRight()
  299.                 moveForward(length)
  300.                 turnRight()
  301.                 moveForward(rows)
  302.                 turnRight()
  303.             end
  304.         else
  305.             if i % 2 == 0 then
  306.                 turnLeft()
  307.                 moveForward()
  308.                 harvestCrops()
  309.                 turnLeft()
  310.             else
  311.                 turnRight()
  312.                 moveForward()
  313.                 harvestCrops()
  314.                 turnRight()
  315.             end
  316.         end
  317.        
  318.     end
  319.  
  320.     dumpInventory()
  321. end
  322.  
  323. while true do
  324.     farm()
  325.     print("Completed loop, waiting " .. MAIN_LOOP_INTERVAL .. " seconds to start next loop.")
  326.     sleep(MAIN_LOOP_INTERVAL)
  327. end
Advertisement
Add Comment
Please, Sign In to add comment