DanFrmSpace

bee_harvest

Jun 4th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.55 KB | None | 0 0
  1. -- Advanced Bee Automation Turtle Script
  2. -- For All The Mods 10 - Productive Bees
  3.  
  4. -- Configuration
  5. local START_FILE = "start_position.txt"
  6. local STATE_FILE = "turtle_state.txt"
  7. local CYCLE_DELAY = 60 -- seconds between cycles
  8. local MIN_FUEL_LEVEL = 1000 -- Minimum fuel before refueling
  9. local FUEL_SLOT = 16 -- Slot to keep fuel in
  10.  
  11. -- Position tracking
  12. local pos = {x = 0, y = 0, z = 0}
  13. local facing = "west" -- Starting direction
  14. local startPos = {x = 0, y = 0, z = 0}
  15.  
  16. -- Direction mappings
  17. local directions = {
  18.     north = 0,
  19.     east = 1,
  20.     south = 2,
  21.     west = 3
  22. }
  23.  
  24. local directionVectors = {
  25.     north = {x = 0, z = -1},
  26.     east = {x = 1, z = 0},
  27.     south = {x = 0, z = 1},
  28.     west = {x = -1, z = 0}
  29. }
  30.  
  31. -- Helper functions
  32. local function savePosition()
  33.     local file = fs.open(START_FILE, "w")
  34.     file.writeLine(pos.x)
  35.     file.writeLine(pos.y)
  36.     file.writeLine(pos.z)
  37.     file.writeLine(facing)
  38.     file.close()
  39. end
  40.  
  41. local function loadPosition()
  42.     if fs.exists(START_FILE) then
  43.         local file = fs.open(START_FILE, "r")
  44.         pos.x = tonumber(file.readLine()) or 0
  45.         pos.y = tonumber(file.readLine()) or 0
  46.         pos.z = tonumber(file.readLine()) or 0
  47.         facing = file.readLine() or "west"
  48.         file.close()
  49.         return true
  50.     end
  51.     return false
  52. end
  53.  
  54. local function saveState(state)
  55.     local file = fs.open(STATE_FILE, "w")
  56.     file.writeLine(state)
  57.     file.close()
  58. end
  59.  
  60. local function loadState()
  61.     if fs.exists(STATE_FILE) then
  62.         local file = fs.open(STATE_FILE, "r")
  63.         local state = file.readLine()
  64.         file.close()
  65.         return state
  66.     end
  67.     return "idle"
  68. end
  69.  
  70. local function turnRight()
  71.     turtle.turnRight()
  72.     local currentDir = directions[facing]
  73.     currentDir = (currentDir + 1) % 4
  74.     for dir, num in pairs(directions) do
  75.         if num == currentDir then
  76.             facing = dir
  77.             break
  78.         end
  79.     end
  80. end
  81.  
  82. local function turnLeft()
  83.     turtle.turnLeft()
  84.     local currentDir = directions[facing]
  85.     currentDir = (currentDir - 1) % 4
  86.     if currentDir < 0 then currentDir = currentDir + 4 end
  87.     for dir, num in pairs(directions) do
  88.         if num == currentDir then
  89.             facing = dir
  90.             break
  91.         end
  92.     end
  93. end
  94.  
  95. local function checkFuel()
  96.     local fuelLevel = turtle.getFuelLevel()
  97.     print("Current fuel level: " .. tostring(fuelLevel))
  98.    
  99.     if fuelLevel == "unlimited" then
  100.         return true
  101.     end
  102.    
  103.     if fuelLevel < MIN_FUEL_LEVEL then
  104.         print("Low fuel! Attempting to refuel...")
  105.        
  106.         -- Try to refuel from any slot
  107.         for slot = 1, 16 do
  108.             turtle.select(slot)
  109.             if turtle.refuel(0) then -- Check if item is fuel
  110.                 local fuelNeeded = MIN_FUEL_LEVEL - fuelLevel
  111.                 local itemCount = turtle.getItemCount(slot)
  112.                 turtle.refuel(math.min(itemCount, math.ceil(fuelNeeded / 80))) -- Coal gives ~80 fuel
  113.                 print("Refueled! New level: " .. turtle.getFuelLevel())
  114.                 turtle.select(1)
  115.                 return true
  116.             end
  117.         end
  118.        
  119.         turtle.select(1)
  120.         print("ERROR: No fuel available! Please add fuel to the turtle.")
  121.         print("Coal, charcoal, wood, or any furnace fuel will work.")
  122.         return false
  123.     end
  124.    
  125.     return true
  126. end
  127.  
  128. local function turnTo(targetDirection)
  129.     while facing ~= targetDirection do
  130.         turnRight()
  131.     end
  132. end
  133.  
  134. local function forward(blocks)
  135.     blocks = blocks or 1
  136.     for i = 1, blocks do
  137.         if not checkFuel() then
  138.             return false
  139.         end
  140.         if turtle.forward() then
  141.             local vec = directionVectors[facing]
  142.             pos.x = pos.x + vec.x
  143.             pos.z = pos.z + vec.z
  144.             savePosition()
  145.         else
  146.             print("Cannot move forward - obstacle detected!")
  147.             return false
  148.         end
  149.     end
  150.     return true
  151. end
  152.  
  153. local function back(blocks)
  154.     blocks = blocks or 1
  155.     for i = 1, blocks do
  156.         if not checkFuel() then
  157.             return false
  158.         end
  159.         if turtle.back() then
  160.             local vec = directionVectors[facing]
  161.             pos.x = pos.x - vec.x
  162.             pos.z = pos.z - vec.z
  163.             savePosition()
  164.         else
  165.             print("Cannot move backward - obstacle detected!")
  166.             return false
  167.         end
  168.     end
  169.     return true
  170. end
  171.  
  172. local function up(blocks)
  173.     blocks = blocks or 1
  174.     for i = 1, blocks do
  175.         if not checkFuel() then
  176.             return false
  177.         end
  178.         if turtle.up() then
  179.             pos.y = pos.y + 1
  180.             savePosition()
  181.         else
  182.             print("Cannot move up - obstacle detected!")
  183.             return false
  184.         end
  185.     end
  186.     return true
  187. end
  188.  
  189. local function down(blocks)
  190.     blocks = blocks or 1
  191.     for i = 1, blocks do
  192.         if not checkFuel() then
  193.             return false
  194.         end
  195.         if turtle.down() then
  196.             pos.y = pos.y - 1
  197.             savePosition()
  198.         else
  199.             print("Cannot move down - obstacle detected!")
  200.             return false
  201.         end
  202.     end
  203.     return true
  204. end
  205.  
  206. local function goToPosition(targetX, targetY, targetZ)
  207.     -- Move to correct Y level first
  208.     local yDiff = targetY - pos.y
  209.     if yDiff > 0 then
  210.         up(yDiff)
  211.     elseif yDiff < 0 then
  212.         down(-yDiff)
  213.     end
  214.    
  215.     -- Move along X axis
  216.     local xDiff = targetX - pos.x
  217.     if xDiff > 0 then
  218.         turnTo("east")
  219.         forward(xDiff)
  220.     elseif xDiff < 0 then
  221.         turnTo("west")
  222.         forward(-xDiff)
  223.     end
  224.    
  225.     -- Move along Z axis
  226.     local zDiff = targetZ - pos.z
  227.     if zDiff > 0 then
  228.         turnTo("south")
  229.         forward(zDiff)
  230.     elseif zDiff < 0 then
  231.         turnTo("north")
  232.         forward(-zDiff)
  233.     end
  234. end
  235.  
  236. local function returnToStart()
  237.     print("Returning to start position...")
  238.     goToPosition(startPos.x, startPos.y, startPos.z)
  239.     turnTo("west")
  240. end
  241.  
  242. local function harvestBeehive()
  243.     -- Wrap the beehive peripheral
  244.     local hive = peripheral.wrap("front")
  245.     if not hive then
  246.         print("No beehive found!")
  247.         return false
  248.     end
  249.    
  250.     -- Try different methods based on what might be available
  251.     -- Method 1: Try list/pushItems combo
  252.     if hive.list and hive.pushItems then
  253.         print("Using list/pushItems method...")
  254.         local items = hive.list()
  255.         local itemsFound = false
  256.         for slot, item in pairs(items) do
  257.             itemsFound = true
  258.             print("Found: " .. item.name .. " x" .. item.count)
  259.             -- Just use turtle.suck() instead of complex peripheral methods
  260.             break
  261.         end
  262.         if itemsFound then
  263.             -- Use turtle's built-in suck method
  264.             local sucked = 0
  265.             while turtle.suck() do
  266.                 sucked = sucked + 1
  267.             end
  268.             print("Sucked " .. sucked .. " stacks from beehive")
  269.         else
  270.             print("No items in beehive")
  271.         end
  272.     -- Method 2: Just try turtle.suck() directly
  273.     else
  274.         print("Using direct turtle.suck() method...")
  275.         local sucked = 0
  276.         while turtle.suck() do
  277.             sucked = sucked + 1
  278.         end
  279.         if sucked > 0 then
  280.             print("Sucked " .. sucked .. " stacks from beehive")
  281.         else
  282.             print("No items to collect")
  283.         end
  284.     end
  285.    
  286.     return true
  287. end
  288.  
  289. local function depositItems()
  290.     -- Deposit all items into the chest
  291.     for slot = 1, 16 do
  292.         if turtle.getItemCount(slot) > 0 then
  293.             turtle.select(slot)
  294.             turtle.drop()
  295.         end
  296.     end
  297.     turtle.select(1)
  298. end
  299.  
  300. local function harvestAllHives()
  301.     print("Starting harvest cycle...")
  302.     saveState("harvesting")
  303.    
  304.     -- Starting at deposit chest facing west
  305.     turnRight()  -- Face north
  306.     turnRight()  -- Face east
  307.     if not forward(4) then return returnToStart() end
  308.     turnLeft()   -- Face north
  309.     if not forward(1) then return returnToStart() end
  310.     if not up(1) then return returnToStart() end
  311.     turnRight()  -- Face east
  312.     if not forward(6) then return returnToStart() end
  313.     turnRight()  -- Face south
  314.     if not forward(3) then return returnToStart() end
  315.     turnRight()  -- Face west
  316.    
  317.     -- Harvest 4 beehives
  318.     for i = 1, 4 do
  319.         print("\n=== Approaching beehive " .. i .. " ===")
  320.         if not forward(1) then
  321.             print("ERROR: Cannot reach beehive " .. i)
  322.             return returnToStart()
  323.         end
  324.        
  325.         print("Harvesting beehive " .. i .. "...")
  326.         local success = harvestBeehive()
  327.         if not success then
  328.             print("Failed to harvest beehive " .. i)
  329.         end
  330.        
  331.         print("Moving back from beehive...")
  332.         if not back(1) then
  333.             print("ERROR: Cannot move back from beehive " .. i)
  334.             -- If we can't move back, we might be stuck against the hive
  335.             -- Try turning around and moving forward instead
  336.             turnRight()
  337.             turnRight()
  338.             if not forward(1) then
  339.                 print("ERROR: Stuck at beehive! Attempting recovery...")
  340.                 return returnToStart()
  341.             end
  342.             turnRight()
  343.             turnRight()
  344.         end
  345.        
  346.         if i < 4 then
  347.             turnLeft()  -- Face south
  348.             if not forward(4) then return returnToStart() end
  349.             turnRight() -- Face west
  350.         end
  351.     end
  352.    
  353.     -- Return to deposit chest
  354.     print("\nReturning to deposit chest...")
  355.     turnRight() -- Face north
  356.     if not forward(15) then return returnToStart() end
  357.     turnLeft()  -- Face west
  358.     if not forward(6) then return returnToStart() end
  359.     if not down(1) then return returnToStart() end
  360.     turnLeft()  -- Face south
  361.     if not forward(1) then return returnToStart() end
  362.     turnRight() -- Face west
  363.     if not forward(4) then return returnToStart() end
  364.    
  365.     -- Deposit items
  366.     print("\nDepositing items...")
  367.     depositItems()
  368.    
  369.     saveState("idle")
  370.     print("Harvest cycle complete!")
  371. end
  372.  
  373. -- Main program
  374. local function main()
  375.     print("Advanced Bee Automation Turtle")
  376.     print("==============================")
  377.    
  378.     -- Check fuel on startup
  379.     if not checkFuel() then
  380.         print("\nWAITING FOR FUEL...")
  381.         print("Please add coal, charcoal, or other fuel to any slot.")
  382.         while not checkFuel() do
  383.             os.sleep(5)
  384.         end
  385.     end
  386.    
  387.     -- Check if we need to restore position
  388.     if loadPosition() then
  389.         print("Restored position: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z)
  390.         local state = loadState()
  391.         if state == "harvesting" then
  392.             print("Resuming from interrupted harvest...")
  393.             returnToStart()
  394.         end
  395.     else
  396.         print("Setting current position as start...")
  397.         savePosition()
  398.     end
  399.    
  400.     print("\nStarting in 3 seconds...")
  401.     os.sleep(3)
  402.    
  403.     -- Main loop
  404.     while true do
  405.         harvestAllHives()
  406.        
  407.         print("\nCycle complete. Waiting " .. CYCLE_DELAY .. " seconds...")
  408.         print("Press Ctrl+T to stop")
  409.         os.sleep(CYCLE_DELAY)
  410.     end
  411. end
  412.  
  413. -- Error handling wrapper
  414. local function safeMain()
  415.     while true do
  416.         local success, error = pcall(main)
  417.         if not success then
  418.             print("Error occurred: " .. error)
  419.             print("Attempting to recover...")
  420.             os.sleep(5)
  421.             returnToStart()
  422.         end
  423.     end
  424. end
  425.  
  426. -- Start the program
  427. safeMain()
Add Comment
Please, Sign In to add comment