Advertisement
th3w0lf3

MelonPumpkinHarvester

Jun 7th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.20 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 PUMPKINS and MELONS 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, "stem") then
  16.                 print("Don't break the stems!")
  17.             else
  18.                 --If the detected block is a melon, break it.
  19.                 if string.match(blockDetected, "melon") then
  20.                     print("\nFound a Melon!")
  21.                     turtle.digDown()
  22.                 --If the detected block is a melon, break it.
  23.                 elseif string.match(blockDetected, "pumpkin") then
  24.                     print("\nFound a Pumpkin!")
  25.                     turtle.digDown()
  26.                 --If the detected block is neither a melon nor a pumpkin, leave it be.
  27.                 else
  28.                     print("\nFound", blockDetected, "I'll leave that.")
  29.                 end
  30.             end
  31.     else
  32.         print("Nothing here!")
  33.     end
  34. end
  35.  
  36. --This function moves the Turtle forward "width", sniffing at every block.
  37. local function Patrol()
  38.     i = 0
  39.     repeat
  40.         turtle.forward()
  41.         Sniff()
  42.         i = i + 1
  43.     until(i>=width)
  44. end
  45.  
  46. --This function turns the Turtle RIGHT in preparation for the next Patrol function and sniffs the first block.
  47. local function turnRight()
  48.     turtle.turnRight()
  49.     turtle.forward()
  50.     turtle.turnRight()
  51.     Sniff()
  52. end
  53.  
  54. --This function turns the Turtle LEFT in preparation for the next Patrol function and sniffs the first block.
  55. local function turnLeft()
  56.     turtle.turnLeft()
  57.     turtle.forward()
  58.     turtle.turnLeft()
  59.     Sniff()
  60. end
  61.  
  62. --This function refuels the Turtle and prints out fuel level.
  63. local function Refuel()
  64.     turtle.select(1)
  65.     print("Refueling!")
  66.     turtle.refuel()
  67.     fuel = turtle.getFuelLevel()
  68.     print("I can move", fuel, "more blocks!")
  69. end
  70.  
  71. --The main function. Runs Patrol and the Turn functions until the inputted length is met
  72. local function Harvest()
  73.     laps = 1
  74.     Refuel()
  75.     repeat
  76.         print("Starting Lap #", laps)
  77.         Patrol()
  78.         turnRight()
  79.         laps = laps + 1
  80.         print("Starting Lap #", laps)
  81.         Patrol()
  82.         turnLeft()
  83.         laps = laps + 1
  84.     until (laps>=length)
  85. end
  86.  
  87. --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.
  88. local function Home()
  89.     print("Area completed! Returning home.")
  90.     turtle.turnLeft()
  91.  
  92.     j = 1
  93.     repeat
  94.         turtle.forward()
  95.         j = j + 1
  96.     until(j==length)
  97.  
  98.     print("Dropping off items!")
  99.     turtle.select(1)
  100.     turtle.dropDown()
  101.     turtle.select(2)
  102.     turtle.dropDown()
  103.     turtle.forward()
  104.  
  105.     print("Picking up some fuel!")
  106.     turtle.select(1)
  107.     turtle.suckDown(10)
  108.     turtle.turnRight()  
  109. end
  110.  
  111. --START OF PROGRAM
  112. 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.")
  113. io.write("\nWhat is the  width of the field?")
  114.     width = tonumber(io.read())
  115.     print("\nOkay!", width, "Wide!")
  116. io.write("\nWhat is the length of the field?")
  117.     length = tonumber(io.read())
  118.     print("\nOkay!", length, "Long!")
  119. io.write("\nHow many minutes should I sleep between each Harvest?")
  120.     sleep = tonumber(io.read()) * 60
  121.     print("Got it. I'll chill out for", sleep, "seconds between Harvests!")
  122.     print("Starting the Harvest!")
  123.  
  124. loop = 1
  125. repeat
  126. Harvest()
  127. Home()
  128. print("Sleeping for", sleep/60, "minutes. Zzz...")
  129. os.sleep(sleep)
  130. until(loop~=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement