th3w0lf3

CropHarvest

Jun 8th, 2020
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.71 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.             --If the detected block is a stem, don't break it.
  15.             if string.match(blockDetected, "chest") then
  16.                 print("Leave stuff in the chest!")
  17.             elseif success then
  18.                 print("\nFound something! Leaving it if it isn't fully grown.")
  19.                 --Harvest the crop
  20.                 turtle.select(16)
  21.                 turtle.placeDown()
  22.                 --Pickup item 1
  23.                 turtle.select(1)
  24.                 turtle.suckDown()
  25.                 --Pickup item 2
  26.                 turtle.select(2)
  27.                 turtle.suckDown()
  28.            
  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.            
  42.        
  43.    
  44.  
  45. --This function moves the Turtle forward "width", sniffing at every block.
  46. local function Patrol()
  47.     i = 0
  48.     repeat
  49.         turtle.forward()
  50.         Sniff()
  51.         i = i + 1
  52.     until(i>=width)
  53. end
  54.  
  55. --This function turns the Turtle RIGHT in preparation for the next Patrol function and sniffs the first block.
  56. local function turnRight()
  57.     turtle.turnRight()
  58.     turtle.forward()
  59.     turtle.turnRight()
  60.     Sniff()
  61. end
  62.  
  63. --This function turns the Turtle LEFT in preparation for the next Patrol function and sniffs the first block.
  64. local function turnLeft()
  65.     if laps>=length then
  66.         return
  67.         else
  68.             turtle.turnLeft()
  69.             turtle.forward()
  70.             turtle.turnLeft()
  71.             Sniff()
  72.         end
  73. end
  74.  
  75. --This function refuels the Turtle and prints out fuel level.
  76. local function Refuel()
  77.     turtle.select(1)
  78.     print("Refueling!")
  79.     turtle.refuel()
  80.     fuel = turtle.getFuelLevel()
  81.     print("I can move", fuel, "more blocks!")
  82. end
  83.  
  84. --The main function. Runs Patrol and the Turn functions until the inputted length is met
  85. local function Harvest()
  86.     laps = 1
  87.     Refuel()
  88.     repeat
  89.         print("Starting Lap #", laps)
  90.         Patrol()
  91.         turnRight()
  92.         laps = laps + 1
  93.         print("Starting Lap #", laps)
  94.         Patrol()
  95.         turnLeft()
  96.         laps = laps + 1
  97.     until (laps>=length)
  98. end
  99.  
  100. --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.
  101. local function Home()
  102.     print("Area completed! Returning home.")
  103.     turtle.turnRight()
  104.  
  105.     j = 1
  106.     repeat
  107.         turtle.forward()
  108.         j = j + 1
  109.     until(j==length - 1)
  110.  
  111.     print("Dropping off items!")
  112.     turtle.select(1)
  113.     turtle.dropDown()
  114.     turtle.select(2)
  115.     turtle.dropDown()
  116.     turtle.select(3)
  117.     turtle.dropDown()
  118.     turtle.select(4)
  119.     turtle.dropDown()
  120.     turtle.select(5)
  121.     turtle.dropDown()
  122.     turtle.select(6)
  123.     turtle.dropDown()
  124.     turtle.select(7)
  125.     turtle.dropDown()
  126.     turtle.select(8)
  127.     turtle.dropDown()
  128.     turtle.select(9)
  129.     turtle.dropDown()
  130.     turtle.select(10)
  131.     turtle.dropDown()
  132.     turtle.select(11)
  133.     turtle.dropDown()
  134.     turtle.select(12)
  135.     turtle.dropDown()
  136.     turtle.select(13)
  137.     turtle.dropDown()
  138.     turtle.select(14)
  139.     turtle.dropDown()
  140.     turtle.select(15)
  141.     turtle.dropDown()
  142.    
  143.     turtle.forward()
  144.  
  145.     print("Picking up some fuel!")
  146.     turtle.select(1)
  147.     turtle.suckDown(10)
  148.     turtle.turnRight()  
  149. end
  150.  
  151. --START OF PROGRAM
  152. 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.")
  153. io.write("\nWhat is the  width of the field?")
  154.     width = tonumber(io.read())
  155.     print("\nOkay!", width, "Wide!")
  156. io.write("\nWhat is the length of the field?")
  157.     length = tonumber(io.read())
  158.     print("\nOkay!", length, "Long!")
  159. io.write("\nHow many minutes should I sleep between each Harvest?")
  160.     sleep = tonumber(io.read()) * 60
  161.     print("Got it. I'll chill out for", sleep, "seconds between Harvests!")
  162.     print("Starting the Harvest!")
  163.  
  164. loop = 1
  165. repeat
  166. Harvest()
  167. Home()
  168. print("I can move", fuel, "more blocks!")
  169. print("Sleeping for", sleep/60, "minutes. Zzz...")
  170. os.sleep(sleep)
  171. until(loop~=1)
Add Comment
Please, Sign In to add comment