Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Advanced Bee Automation Turtle Script
- -- For All The Mods 10 - Productive Bees
- -- Configuration
- local START_FILE = "start_position.txt"
- local STATE_FILE = "turtle_state.txt"
- local CYCLE_DELAY = 60 -- seconds between cycles
- local MIN_FUEL_LEVEL = 1000 -- Minimum fuel before refueling
- local FUEL_SLOT = 16 -- Slot to keep fuel in
- -- Position tracking
- local pos = {x = 0, y = 0, z = 0}
- local facing = "west" -- Starting direction
- local startPos = {x = 0, y = 0, z = 0}
- -- Direction mappings
- local directions = {
- north = 0,
- east = 1,
- south = 2,
- west = 3
- }
- local directionVectors = {
- north = {x = 0, z = -1},
- east = {x = 1, z = 0},
- south = {x = 0, z = 1},
- west = {x = -1, z = 0}
- }
- -- Helper functions
- local function savePosition()
- local file = fs.open(START_FILE, "w")
- file.writeLine(pos.x)
- file.writeLine(pos.y)
- file.writeLine(pos.z)
- file.writeLine(facing)
- file.close()
- end
- local function loadPosition()
- if fs.exists(START_FILE) then
- local file = fs.open(START_FILE, "r")
- pos.x = tonumber(file.readLine()) or 0
- pos.y = tonumber(file.readLine()) or 0
- pos.z = tonumber(file.readLine()) or 0
- facing = file.readLine() or "west"
- file.close()
- return true
- end
- return false
- end
- local function saveState(state)
- local file = fs.open(STATE_FILE, "w")
- file.writeLine(state)
- file.close()
- end
- local function loadState()
- if fs.exists(STATE_FILE) then
- local file = fs.open(STATE_FILE, "r")
- local state = file.readLine()
- file.close()
- return state
- end
- return "idle"
- end
- local function turnRight()
- turtle.turnRight()
- local currentDir = directions[facing]
- currentDir = (currentDir + 1) % 4
- for dir, num in pairs(directions) do
- if num == currentDir then
- facing = dir
- break
- end
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- local currentDir = directions[facing]
- currentDir = (currentDir - 1) % 4
- if currentDir < 0 then currentDir = currentDir + 4 end
- for dir, num in pairs(directions) do
- if num == currentDir then
- facing = dir
- break
- end
- end
- end
- local function checkFuel()
- local fuelLevel = turtle.getFuelLevel()
- print("Current fuel level: " .. tostring(fuelLevel))
- if fuelLevel == "unlimited" then
- return true
- end
- if fuelLevel < MIN_FUEL_LEVEL then
- print("Low fuel! Attempting to refuel...")
- -- Try to refuel from any slot
- for slot = 1, 16 do
- turtle.select(slot)
- if turtle.refuel(0) then -- Check if item is fuel
- local fuelNeeded = MIN_FUEL_LEVEL - fuelLevel
- local itemCount = turtle.getItemCount(slot)
- turtle.refuel(math.min(itemCount, math.ceil(fuelNeeded / 80))) -- Coal gives ~80 fuel
- print("Refueled! New level: " .. turtle.getFuelLevel())
- turtle.select(1)
- return true
- end
- end
- turtle.select(1)
- print("ERROR: No fuel available! Please add fuel to the turtle.")
- print("Coal, charcoal, wood, or any furnace fuel will work.")
- return false
- end
- return true
- end
- local function turnTo(targetDirection)
- while facing ~= targetDirection do
- turnRight()
- end
- end
- local function forward(blocks)
- blocks = blocks or 1
- for i = 1, blocks do
- if not checkFuel() then
- return false
- end
- if turtle.forward() then
- local vec = directionVectors[facing]
- pos.x = pos.x + vec.x
- pos.z = pos.z + vec.z
- savePosition()
- else
- print("Cannot move forward - obstacle detected!")
- return false
- end
- end
- return true
- end
- local function back(blocks)
- blocks = blocks or 1
- for i = 1, blocks do
- if not checkFuel() then
- return false
- end
- if turtle.back() then
- local vec = directionVectors[facing]
- pos.x = pos.x - vec.x
- pos.z = pos.z - vec.z
- savePosition()
- else
- print("Cannot move backward - obstacle detected!")
- return false
- end
- end
- return true
- end
- local function up(blocks)
- blocks = blocks or 1
- for i = 1, blocks do
- if not checkFuel() then
- return false
- end
- if turtle.up() then
- pos.y = pos.y + 1
- savePosition()
- else
- print("Cannot move up - obstacle detected!")
- return false
- end
- end
- return true
- end
- local function down(blocks)
- blocks = blocks or 1
- for i = 1, blocks do
- if not checkFuel() then
- return false
- end
- if turtle.down() then
- pos.y = pos.y - 1
- savePosition()
- else
- print("Cannot move down - obstacle detected!")
- return false
- end
- end
- return true
- end
- local function goToPosition(targetX, targetY, targetZ)
- -- Move to correct Y level first
- local yDiff = targetY - pos.y
- if yDiff > 0 then
- up(yDiff)
- elseif yDiff < 0 then
- down(-yDiff)
- end
- -- Move along X axis
- local xDiff = targetX - pos.x
- if xDiff > 0 then
- turnTo("east")
- forward(xDiff)
- elseif xDiff < 0 then
- turnTo("west")
- forward(-xDiff)
- end
- -- Move along Z axis
- local zDiff = targetZ - pos.z
- if zDiff > 0 then
- turnTo("south")
- forward(zDiff)
- elseif zDiff < 0 then
- turnTo("north")
- forward(-zDiff)
- end
- end
- local function returnToStart()
- print("Returning to start position...")
- goToPosition(startPos.x, startPos.y, startPos.z)
- turnTo("west")
- end
- local function harvestBeehive()
- -- Wrap the beehive peripheral
- local hive = peripheral.wrap("front")
- if not hive then
- print("No beehive found!")
- return false
- end
- -- Try different methods based on what might be available
- -- Method 1: Try list/pushItems combo
- if hive.list and hive.pushItems then
- print("Using list/pushItems method...")
- local items = hive.list()
- local itemsFound = false
- for slot, item in pairs(items) do
- itemsFound = true
- print("Found: " .. item.name .. " x" .. item.count)
- -- Just use turtle.suck() instead of complex peripheral methods
- break
- end
- if itemsFound then
- -- Use turtle's built-in suck method
- local sucked = 0
- while turtle.suck() do
- sucked = sucked + 1
- end
- print("Sucked " .. sucked .. " stacks from beehive")
- else
- print("No items in beehive")
- end
- -- Method 2: Just try turtle.suck() directly
- else
- print("Using direct turtle.suck() method...")
- local sucked = 0
- while turtle.suck() do
- sucked = sucked + 1
- end
- if sucked > 0 then
- print("Sucked " .. sucked .. " stacks from beehive")
- else
- print("No items to collect")
- end
- end
- return true
- end
- local function depositItems()
- -- Deposit all items into the chest
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- local function harvestAllHives()
- print("Starting harvest cycle...")
- saveState("harvesting")
- -- Starting at deposit chest facing west
- turnRight() -- Face north
- turnRight() -- Face east
- if not forward(4) then return returnToStart() end
- turnLeft() -- Face north
- if not forward(1) then return returnToStart() end
- if not up(1) then return returnToStart() end
- turnRight() -- Face east
- if not forward(6) then return returnToStart() end
- turnRight() -- Face south
- if not forward(3) then return returnToStart() end
- turnRight() -- Face west
- -- Harvest 4 beehives
- for i = 1, 4 do
- print("\n=== Approaching beehive " .. i .. " ===")
- if not forward(1) then
- print("ERROR: Cannot reach beehive " .. i)
- return returnToStart()
- end
- print("Harvesting beehive " .. i .. "...")
- local success = harvestBeehive()
- if not success then
- print("Failed to harvest beehive " .. i)
- end
- print("Moving back from beehive...")
- if not back(1) then
- print("ERROR: Cannot move back from beehive " .. i)
- -- If we can't move back, we might be stuck against the hive
- -- Try turning around and moving forward instead
- turnRight()
- turnRight()
- if not forward(1) then
- print("ERROR: Stuck at beehive! Attempting recovery...")
- return returnToStart()
- end
- turnRight()
- turnRight()
- end
- if i < 4 then
- turnLeft() -- Face south
- if not forward(4) then return returnToStart() end
- turnRight() -- Face west
- end
- end
- -- Return to deposit chest
- print("\nReturning to deposit chest...")
- turnRight() -- Face north
- if not forward(15) then return returnToStart() end
- turnLeft() -- Face west
- if not forward(6) then return returnToStart() end
- if not down(1) then return returnToStart() end
- turnLeft() -- Face south
- if not forward(1) then return returnToStart() end
- turnRight() -- Face west
- if not forward(4) then return returnToStart() end
- -- Deposit items
- print("\nDepositing items...")
- depositItems()
- saveState("idle")
- print("Harvest cycle complete!")
- end
- -- Main program
- local function main()
- print("Advanced Bee Automation Turtle")
- print("==============================")
- -- Check fuel on startup
- if not checkFuel() then
- print("\nWAITING FOR FUEL...")
- print("Please add coal, charcoal, or other fuel to any slot.")
- while not checkFuel() do
- os.sleep(5)
- end
- end
- -- Check if we need to restore position
- if loadPosition() then
- print("Restored position: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z)
- local state = loadState()
- if state == "harvesting" then
- print("Resuming from interrupted harvest...")
- returnToStart()
- end
- else
- print("Setting current position as start...")
- savePosition()
- end
- print("\nStarting in 3 seconds...")
- os.sleep(3)
- -- Main loop
- while true do
- harvestAllHives()
- print("\nCycle complete. Waiting " .. CYCLE_DELAY .. " seconds...")
- print("Press Ctrl+T to stop")
- os.sleep(CYCLE_DELAY)
- end
- end
- -- Error handling wrapper
- local function safeMain()
- while true do
- local success, error = pcall(main)
- if not success then
- print("Error occurred: " .. error)
- print("Attempting to recover...")
- os.sleep(5)
- returnToStart()
- end
- end
- end
- -- Start the program
- safeMain()
Add Comment
Please, Sign In to add comment