HeatedDZN

MFarm

May 18th, 2024 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.30 KB | None | 0 0
  1. -- MFarm: Start the farming process
  2.  
  3. -- Function to load the setup data
  4. function loadSetup()
  5.     if not fs.exists("farmSetup") then
  6.         print("Setup not found. Please run MFarmSetup first.")
  7.         return nil, nil, nil, nil, nil
  8.     end
  9.  
  10.     local file = fs.open("farmSetup", "r")
  11.     local seedItem = file.readLine()
  12.     local fuelItem = file.readLine()
  13.     local fullyGrownItem = file.readLine()
  14.     local farmSize = tonumber(file.readLine())
  15.     local sleepTime = tonumber(file.readLine())
  16.     file.close()
  17.  
  18.     return seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime
  19. end
  20.  
  21. -- Function to check and refuel the turtle
  22. function checkAndRefuel(fuelItem)
  23.     if turtle.getFuelLevel() < 500 then
  24.         for slot = 1, 16 do
  25.             turtle.select(slot)
  26.             local item = turtle.getItemDetail()
  27.             if item and item.name == fuelItem then
  28.                 turtle.refuel()
  29.                 if turtle.getFuelLevel() >= 500 then
  30.                     break
  31.                 end
  32.             end
  33.         end
  34.         if turtle.getFuelLevel() < 500 then
  35.             print("Turtle needs more fuel. Setup failed.")
  36.             return false
  37.         end
  38.     end
  39.     return true
  40. end
  41.  
  42. -- Function to sort items from turtle inventory and chest
  43. function sortItems(seedItem, fuelItem, fullyGrownItem)
  44.     local hasSeed = false
  45.     local hasFuel = false
  46.     local hasFullyGrown = false
  47.  
  48.     -- Check turtle inventory
  49.     for slot = 1, 16 do
  50.         turtle.select(slot)
  51.         local item = turtle.getItemDetail()
  52.         if item then
  53.             if item.name == seedItem then
  54.                 turtle.transferTo(1)
  55.                 hasSeed = true
  56.             elseif item.name == fuelItem then
  57.                 turtle.transferTo(2)
  58.                 hasFuel = true
  59.             elseif item.name == fullyGrownItem then
  60.                 turtle.transferTo(3)
  61.                 hasFullyGrown = true
  62.             end
  63.         end
  64.     end
  65.  
  66.     -- Check chest below
  67.     if not hasSeed or not hasFuel or not hasFullyGrown then
  68.         for slot = 1, 16 do
  69.             turtle.select(slot)
  70.             if not hasSeed then
  71.                 turtle.suckDown()
  72.                 local item = turtle.getItemDetail()
  73.                 if item and item.name == seedItem then
  74.                     turtle.transferTo(1)
  75.                     hasSeed = true
  76.                 else
  77.                     turtle.dropDown()
  78.                 end
  79.             end
  80.             if not hasFuel then
  81.                 turtle.suckDown()
  82.                 local item = turtle.getItemDetail()
  83.                 if item and item.name == fuelItem then
  84.                     turtle.transferTo(2)
  85.                     hasFuel = true
  86.                 else
  87.                     turtle.dropDown()
  88.                 end
  89.             end
  90.             if not hasFullyGrown then
  91.                 turtle.suckDown()
  92.                 local item = turtle.getItemDetail()
  93.                 if item and item.name == fullyGrownItem then
  94.                     turtle.transferTo(3)
  95.                     hasFullyGrown = true
  96.                 else
  97.                     turtle.dropDown()
  98.                 end
  99.             end
  100.         end
  101.     end
  102.  
  103.     return hasSeed, hasFuel, hasFullyGrown
  104. end
  105.  
  106. -- Function to return items to the chest
  107. function returnItems()
  108.     for slot = 4, 16 do
  109.         turtle.select(slot)
  110.         turtle.dropDown()
  111.     end
  112. end
  113.  
  114. -- Function to farm crops
  115. function farm(size)
  116.     turtle.select(1)  -- Ensure seeds are selected
  117.     turtle.forward()  -- Move forward to start farming
  118.  
  119.     for i = 1, size do
  120.         for j = 1, size do
  121.             if turtle.detectDown() then
  122.                 turtle.digDown()
  123.             end
  124.             turtle.placeDown()
  125.             if j < size then
  126.                 turtle.forward()
  127.             end
  128.         end
  129.         if i < size then
  130.             if i % 2 == 1 then
  131.                 turtle.turnRight()
  132.                 turtle.forward()
  133.                 turtle.turnRight()
  134.             else
  135.                 turtle.turnLeft()
  136.                 turtle.forward()
  137.                 turtle.turnLeft()
  138.             end
  139.         end
  140.     end
  141.  
  142.     -- Return to the starting position
  143.     turtle.forward()
  144.     turtle.turnLeft()
  145.     for k = 1, size do
  146.         turtle.forward()
  147.     end
  148.     turtle.turnLeft()
  149.     for k = 1, size + 1 do
  150.         turtle.forward()
  151.     end
  152.     turtle.turnLeft()
  153.     turtle.forward()
  154.     turtle.turnLeft()
  155. end
  156.  
  157. -- Function to check fuel level and refuel if necessary
  158. function manageFuel(fuelItem)
  159.     if turtle.getFuelLevel() < 500 then
  160.         print("Refueling...")
  161.         checkAndRefuel(fuelItem)
  162.     end
  163.  
  164.     if turtle.getFuelLevel() < 500 then
  165.         print("Turtle needs more fuel. Setup failed.")
  166.         return false
  167.     end
  168.  
  169.     return true
  170. end
  171.  
  172. -- Function to manage the farming cycle
  173. function farmingCycle(seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime)
  174.     while true do
  175.         farm(farmSize)
  176.  
  177.         -- Dump items from slots 4-16 into the chest below
  178.         returnItems()
  179.  
  180.         -- Check and manage fuel
  181.         if not manageFuel(fuelItem) then
  182.             print("Turtle needs more fuel. Pausing for " .. sleepTime .. " seconds.")
  183.             os.sleep(sleepTime)  -- Wait for the specified sleep time
  184.             print("Retrying farming process.")
  185.         else
  186.             print("Fuel level sufficient. Pausing for " .. sleepTime .. " seconds.")
  187.             os.sleep(sleepTime)  -- Wait for the specified sleep time
  188.         end
  189.     end
  190. end
  191.  
  192. -- Main function
  193. function main()
  194.     local seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime = loadSetup()
  195.     if not seedItem then
  196.         return
  197.     end
  198.  
  199.     -- Confirm setup information
  200.     print("Setup Information:")
  201.     print("Seed: " .. seedItem)
  202.     print("Fuel: " .. fuelItem)
  203.     print("Fully Grown Crop: " .. fullyGrownItem)
  204.     print("Farm Size: " .. farmSize .. "x" .. farmSize)
  205.     print("Sleep Time: " .. sleepTime .. " seconds")
  206.     print("")
  207.     print("Is this information correct? (yes/no)")
  208.     local confirmation = string.lower(read())
  209.  
  210.     if confirmation == "yes" then
  211.         if checkAndRefuel(fuelItem) then
  212.             print("Checking for required items in inventory and chest...")
  213.  
  214.             local hasSeed, hasFuel, hasFullyGrown = sortItems(seedItem, fuelItem, fullyGrownItem)
  215.  
  216.             if hasSeed and hasFuel and hasFullyGrown then
  217.                 print("All required items are present and sorted into the correct slots.")
  218.                 print("Ensuring seeds are in the first slot...")
  219.                 turtle.select(1)
  220.                 print("Setup confirmed.")
  221.                 print("Please manually place the seeds in the first slot of the turtle's inventory.")
  222.                 print("Press any key to continue once this is done.")
  223.                 os.pullEvent("key")
  224.                 print("Ready to start farming.")
  225.                 farmingCycle(seedItem, fuelItem, fullyGrownItem, farmSize, sleepTime)  -- Start the farming cycle
  226.             else
  227.                 print("Required items are missing. Setup failed.")
  228.                 returnItems()
  229.             end
  230.         else
  231.             print("Setup failed. Please provide more fuel.")
  232.         end
  233.     else
  234.         print("Setup information incorrect. Please run MFarmSetup again.")
  235.     end
  236. end
  237.  
  238. -- Start the farming process
  239. main()
  240.  
Add Comment
Please, Sign In to add comment