michael9411

TFarm

Feb 12th, 2021 (edited)
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- pastebin get 4SHjtFAU tfarm.lua
  2. -- This turtle program harvests rows of crops with rows of water between them
  3. -- Seeds go in the first slot and fuel goes in the last (16th) slot. Everything else gets dumped.
  4. -- The turtle needs to be outside the end of the last crop row facing into the row and 1 block above crop height
  5. -- To automate storage of crops and extra seeds place a chest under this position
  6.  
  7. -- Farm parameters
  8. local rowLength = 20
  9. local numRows = 16
  10. local matureCropStage = 7 -- 7 will work for most crops
  11. local turnRightFirst = true -- true if the first turn needed to face the next row is right
  12. --Network parameters
  13. local networkMessageKeyword = "harvest" -- if received over the network, this will trigger a harvest
  14. local modemSide = "left" -- side the modem is on
  15.  
  16.  
  17. -- Initialize
  18. local turnRightNext = turnRightFirst
  19. local currentSlot = 1
  20. local onFirstRow = true;
  21.  
  22. function setActiveSlot(slotNumber)
  23.     slotNumber = tonumber(slotNumber)
  24.     if slotNumber ~= nil then
  25.         currentSlot = slotNumber
  26.         turtle.select(slotNumber)
  27.     end
  28. end
  29.  
  30. function harvest()
  31.     local success, data = turtle.inspectDown()
  32.  
  33.     if success then
  34.         if data.state.age == matureCropStage then -- 7 = fully grown
  35.             setActiveSlot(1)
  36.             turtle.digDown()
  37.             turtle.placeDown()
  38.         end
  39.     end
  40. end
  41.  
  42. function refuelFromLastSlot()
  43.     setActiveSlot(16)
  44.     turtle.refuel()
  45. end
  46.  
  47. function forceMoveForward()
  48.     while not turtle.forward() do
  49.         print("Failed to move forward!")
  50.         sleep(1)
  51.     end
  52. end
  53.  
  54. function forceTurn(goRight)
  55.     if goRight == true then
  56.         while not turtle.turnRight() do
  57.             print("Failed to turn right!")
  58.             sleep(1)
  59.         end
  60.     else
  61.         while not turtle.turnLeft() do
  62.             print("Failed to turn left!")
  63.             sleep(1)
  64.         end
  65.     end
  66. end
  67.  
  68. function forceDropDown()
  69.     while not turtle.dropDown() do
  70.         print("Failed to transfer items!")
  71.         sleep(1)
  72.     end
  73. end
  74.  
  75. function returnHome()
  76.  
  77.     local distanceToHome = (numRows * 2) - 2
  78.  
  79.     forceMoveForward()
  80.     forceTurn(true) -- TODO handle more cases
  81.  
  82.     for i=1, distanceToHome do
  83.         forceMoveForward()
  84.     end
  85.  
  86.     forceTurn(true)
  87. end
  88.  
  89. function transferItems()
  90.     for i=2, 15 do
  91.         turtle.select(i)
  92.         turtle.dropDown()
  93.     end
  94. end
  95.  
  96. function harvestField()
  97.     print("Harvesting crops...")
  98.     refuelFromLastSlot()
  99.     forceMoveForward()
  100.  
  101.     for i=1, numRows do
  102.  
  103.         if onFirstRow == true then
  104.             onFirstRow = false
  105.         else
  106.             -- Get to next row
  107.             forceTurn(turnRightNext)
  108.             forceMoveForward()
  109.             forceMoveForward()
  110.             forceTurn(turnRightNext)
  111.  
  112.             -- Zig zag efficiently through the crop rows
  113.             turnRightNext = not turnRightNext
  114.         end
  115.        
  116.         for j=2, rowLength do
  117.             harvest()
  118.             forceMoveForward()
  119.         end
  120.        
  121.         harvest()
  122.  
  123.     end
  124.  
  125.     returnHome()
  126.     transferItems()
  127.  
  128.     turnRightNext = turnRightFirst
  129.     onFirstRow = true
  130.  
  131. end
  132.  
  133. -- Execution starts here --
  134. print("Listening for messages...")
  135. rednet.open(modemSide)
  136. while true do
  137.     id, msg = rednet.receive()
  138.     if msg == networkMessageKeyword then
  139.         rednet.close(modemSide)
  140.         harvestField()
  141.         print("Harvest finished. Listening...")
  142.         rednet.open(modemSide)
  143.     end
  144. end    
Advertisement
Add Comment
Please, Sign In to add comment