th3w0lf3

WheatHarvest

Jun 8th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. --[[
  2.     This program will prompt the user for the length and width of the field to be harvested.
  3.     It will then go through and dig up all GROWN WHEAT in that area, return home,
  4.     drop off crops, refuel, and wait the specified time before running again.
  5. ]]
  6.  
  7. --Sniff will inspect the block below the Turtle and break it only if it is a Melon or Pumpkin
  8. local function Sniff()
  9.  
  10.     --Detect the block. If it detects a block, it will decide what to do with it. Otherwise, it will see nothing there and continue on
  11.     local success, data = turtle.inspectDown()
  12.     if success then
  13.         blockDetected = (data.name)
  14.          
  15.             --If the detected block is a Wheat, harvest it with right-click. Will not harvest non-fully grown wheat.
  16.             if string.find(blockDetected, "wheat") then
  17.                 print("\nFound a crop! Leaving it if it isn't fully grown.")
  18.                 --Harvest the crop
  19.                 turtle.select(16)
  20.                 turtle.placeDown()
  21.                 --Pickup item 1
  22.                 turtle.select(1)
  23.                 turtle.suckDown()
  24.                 --Pickup item 2
  25.                 turtle.select(2)
  26.                 turtle.suckDown()
  27.             else
  28.                 print("\nFound", blockDetected, "I'll leave that.")
  29.             end
  30.     else
  31.         print("Nothing here!")
  32.          --Pickup item 1 (in case an item popped too far)
  33.          turtle.select(1)
  34.          turtle.suckDown()
  35.          --Pickup item 2
  36.          turtle.select(2)
  37.          turtle.suckDown()
  38.     end
  39. end
  40.  
  41. --This function moves the Turtle forward "width", sniffing at every block.
  42. local function Patrol()
  43.     i = 0
  44.     repeat
  45.         turtle.forward()
  46.         Sniff()
  47.         i = i + 1
  48.     until(i>=width)
  49. end
  50.  
  51. --This function turns the Turtle RIGHT in preparation for the next Patrol function and sniffs the first block.
  52. local function turnRight()
  53.     turtle.turnRight()
  54.     turtle.forward()
  55.     turtle.turnRight()
  56.     Sniff()
  57. end
  58.  
  59. --This function turns the Turtle LEFT in preparation for the next Patrol function and sniffs the first block.
  60. local function turnLeft()
  61.     turtle.turnLeft()
  62.     turtle.forward()
  63.     turtle.turnLeft()
  64.     Sniff()
  65. end
  66.  
  67. --This function refuels the Turtle and prints out fuel level.
  68. local function Refuel()
  69.     turtle.select(1)
  70.     print("Refueling!")
  71.     turtle.refuel()
  72.     fuel = turtle.getFuelLevel()
  73.     print("I can move", fuel, "more blocks!")
  74. end
  75.  
  76. --The main function. Runs Patrol and the Turn functions until the inputted length is met
  77. local function Harvest()
  78.     laps = 1
  79.     Refuel()
  80.     repeat
  81.         print("Starting Lap #", laps)
  82.         Patrol()
  83.         turnRight()
  84.         laps = laps + 1
  85.         print("Starting Lap #", laps)
  86.         Patrol()
  87.         turnLeft()
  88.         laps = laps + 1
  89.     until (laps>=length)
  90. end
  91.  
  92. --This function returns the Turtle to the home position one the Harvest function ends. On the way, it drops off items and picks up fuel.
  93. local function Home()
  94.     print("Area completed! Returning home.")
  95.     turtle.turnLeft()
  96.  
  97.     j = 1
  98.     repeat
  99.         turtle.forward()
  100.         j = j + 1
  101.     until(j==length)
  102.  
  103.     print("Dropping off items!")
  104.     turtle.select(1)
  105.     turtle.dropDown()
  106.     turtle.select(2)
  107.     turtle.dropDown()
  108.     turtle.select(3)
  109.     turtle.dropDown()
  110.     turtle.select(4)
  111.     turtle.dropDown()
  112.     turtle.select(5)
  113.     turtle.dropDown()
  114.     turtle.select(6)
  115.     turtle.dropDown()
  116.     turtle.select(7)
  117.     turtle.dropDown()
  118.     turtle.select(8)
  119.     turtle.dropDown()
  120.     turtle.select(9)
  121.     turtle.dropDown()
  122.     turtle.select(10)
  123.     turtle.dropDown()
  124.     turtle.select(11)
  125.     turtle.dropDown()
  126.     turtle.select(12)
  127.     turtle.dropDown()
  128.     turtle.select(13)
  129.     turtle.dropDown()
  130.     turtle.select(14)
  131.     turtle.dropDown()
  132.     turtle.select(15)
  133.     turtle.dropDown()
  134.    
  135.     turtle.forward()
  136.  
  137.     print("Picking up some fuel!")
  138.     turtle.select(1)
  139.     turtle.suckDown(10)
  140.     turtle.turnRight()  
  141. end
  142.  
  143. --START OF PROGRAM
  144. io.write("\nHi there! \nI should begin placed above a chest full of fuel. That is my home position. \nDirectly to the right of the fuel chest, place a seperate (NOT CONNECTED!) chest for me to dump your delicious crops into!\nI'll go through and harvest everything in the defined area, drop if off, refuel, and patiently wait at the home position to start again after the defined sleep time.")
  145. io.write("\nWhat is the  width of the field?")
  146.     width = tonumber(io.read())
  147.     print("\nOkay!", width, "Wide!")
  148. io.write("\nWhat is the length of the field?")
  149.     length = tonumber(io.read())
  150.     print("\nOkay!", length, "Long!")
  151. io.write("\nHow many minutes should I sleep between each Harvest?")
  152.     sleep = tonumber(io.read()) * 60
  153.     print("Got it. I'll chill out for", sleep, "seconds between Harvests!")
  154.     print("Starting the Harvest!")
  155.  
  156. loop = 1
  157. repeat
  158. Harvest()
  159. Home()
  160. print("Sleeping for", sleep/60, "minutes. Zzz...")
  161. os.sleep(sleep)
  162. until(loop~=1)
Add Comment
Please, Sign In to add comment