Advertisement
DanFrmSpace

bee_cent

Jun 4th, 2025 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.74 KB | None | 0 0
  1. -- Honeycomb Centrifuge Automation Turtle Script
  2. -- For All The Mods 10 - Productive Bees
  3.  
  4. -- Configuration
  5. local HONEYCOMB_ID = "productivebees:configurable_honeycomb"
  6. local CYCLE_DELAY = 60 -- seconds between cycles
  7. local MIN_FUEL_LEVEL = 500
  8.  
  9. -- Position tracking for recovery
  10. local pos = {x = 0, y = 0, z = 0}
  11. local facing = "west"
  12. local START_FILE = "honeycomb_start_pos.txt"
  13.  
  14. -- Direction mappings
  15. local directions = {
  16.     north = 0,
  17.     east = 1,
  18.     south = 2,
  19.     west = 3
  20. }
  21.  
  22. -- Helper functions
  23. local function savePosition()
  24.     local file = fs.open(START_FILE, "w")
  25.     file.writeLine(pos.x)
  26.     file.writeLine(pos.y)
  27.     file.writeLine(pos.z)
  28.     file.writeLine(facing)
  29.     file.close()
  30. end
  31.  
  32. local function loadPosition()
  33.     if fs.exists(START_FILE) then
  34.         local file = fs.open(START_FILE, "r")
  35.         pos.x = tonumber(file.readLine()) or 0
  36.         pos.y = tonumber(file.readLine()) or 0
  37.         pos.z = tonumber(file.readLine()) or 0
  38.         facing = file.readLine() or "west"
  39.         file.close()
  40.         return true
  41.     end
  42.     return false
  43. end
  44.  
  45. local function checkFuel()
  46.     local fuelLevel = turtle.getFuelLevel()
  47.     if fuelLevel == "unlimited" then
  48.         return true
  49.     end
  50.    
  51.     if fuelLevel < MIN_FUEL_LEVEL then
  52.         print("Low fuel! Attempting to refuel...")
  53.         for slot = 1, 16 do
  54.             turtle.select(slot)
  55.             if turtle.refuel(0) then
  56.                 turtle.refuel()
  57.                 print("Refueled! New level: " .. turtle.getFuelLevel())
  58.                 turtle.select(1)
  59.                 return true
  60.             end
  61.         end
  62.         turtle.select(1)
  63.         print("ERROR: No fuel available!")
  64.         return false
  65.     end
  66.     return true
  67. end
  68.  
  69. local function turnRight()
  70.     turtle.turnRight()
  71.     local currentDir = directions[facing]
  72.     currentDir = (currentDir + 1) % 4
  73.     for dir, num in pairs(directions) do
  74.         if num == currentDir then
  75.             facing = dir
  76.             break
  77.         end
  78.     end
  79. end
  80.  
  81. local function turnLeft()
  82.     turtle.turnLeft()
  83.     local currentDir = directions[facing]
  84.     currentDir = (currentDir - 1) % 4
  85.     if currentDir < 0 then currentDir = currentDir + 4 end
  86.     for dir, num in pairs(directions) do
  87.         if num == currentDir then
  88.             facing = dir
  89.             break
  90.         end
  91.     end
  92. end
  93.  
  94. local function forward(blocks)
  95.     blocks = blocks or 1
  96.     for i = 1, blocks do
  97.         if not checkFuel() then return false end
  98.         if turtle.forward() then
  99.             if facing == "north" then pos.z = pos.z - 1
  100.             elseif facing == "south" then pos.z = pos.z + 1
  101.             elseif facing == "east" then pos.x = pos.x + 1
  102.             elseif facing == "west" then pos.x = pos.x - 1
  103.             end
  104.             savePosition()
  105.         else
  106.             print("Cannot move forward - obstacle detected!")
  107.             return false
  108.         end
  109.     end
  110.     return true
  111. end
  112.  
  113. local function up()
  114.     if not checkFuel() then return false end
  115.     if turtle.up() then
  116.         pos.y = pos.y + 1
  117.         savePosition()
  118.         return true
  119.     end
  120.     return false
  121. end
  122.  
  123. local function down()
  124.     if not checkFuel() then return false end
  125.     if turtle.down() then
  126.         pos.y = pos.y - 1
  127.         savePosition()
  128.         return true
  129.     end
  130.     return false
  131. end
  132.  
  133. local function pullHoneycombs()
  134.     print("Pulling all items from chest below...")
  135.    
  136.     local pulledCount = 0
  137.    
  138.     -- Pull everything we can from the chest
  139.     local itemsPulled = 0
  140.     while turtle.suckDown() do
  141.         itemsPulled = itemsPulled + 1
  142.     end
  143.     print("Pulled " .. itemsPulled .. " stacks, now sorting...")
  144.    
  145.     -- Go through our inventory and return non-honeycombs
  146.     for slot = 1, 16 do
  147.         local item = turtle.getItemDetail(slot)
  148.         if item then
  149.             print("Slot " .. slot .. ": " .. item.name)
  150.             if item.name == HONEYCOMB_ID then
  151.                 pulledCount = pulledCount + 1
  152.                 print("Keeping configurable honeycomb in slot " .. slot)
  153.             else
  154.                 print("Returning " .. item.name .. " to chest")
  155.                 turtle.select(slot)
  156.                 turtle.dropDown()
  157.             end
  158.         end
  159.     end
  160.    
  161.     turtle.select(1)
  162.     print("Kept " .. pulledCount .. " stacks of honeycombs")
  163.     return true
  164. end
  165.  
  166. local function depositHoneycomb()
  167.     -- Try to deposit honeycomb into the centrifuge below
  168.     local centrifuge = peripheral.wrap("bottom")
  169.     if not centrifuge then
  170.         print("No centrifuge found below")
  171.         return false
  172.     end
  173.    
  174.     -- Find a honeycomb in our inventory
  175.     for slot = 1, 16 do
  176.         local item = turtle.getItemDetail(slot)
  177.         if item and item.name == HONEYCOMB_ID then
  178.             turtle.select(slot)
  179.            
  180.             -- Try to drop entire stack into the centrifuge
  181.             if turtle.dropDown() then
  182.                 print("Deposited " .. item.count .. " honeycombs into centrifuge")
  183.                 turtle.select(1)
  184.                 return true
  185.             else
  186.                 print("Centrifuge appears to be full")
  187.                 turtle.select(1)
  188.                 return false
  189.             end
  190.         end
  191.     end
  192.    
  193.     -- No honeycombs found
  194.     return false
  195. end
  196.  
  197. local function returnUnusedItems()
  198.     -- Return any non-honeycomb items to the chest
  199.     print("Returning non-honeycomb items...")
  200.    
  201.     -- Move back to chest position (we're currently at position 0,1,5 facing south)
  202.     turnRight()  -- Face west
  203.     turnRight()  -- Face north (180 turn from south)
  204.     forward(5)   -- Move back to 0,1,0
  205.    
  206.     -- Wait for clear path
  207.     local waitCount = 0
  208.     while turtle.detectDown() do
  209.         waitCount = waitCount + 1
  210.         print("Turtle detected below. Waiting... (" .. waitCount .. ")")
  211.         os.sleep(2)
  212.         if waitCount > 30 then
  213.             print("Waited too long. Proceeding anyway...")
  214.             break
  215.         end
  216.     end
  217.    
  218.     down()       -- Go to 0,0,0
  219.     turnRight()  -- Face east
  220.     turnRight()  -- Face south
  221.     turnRight()  -- Face west (back to original orientation)
  222.    
  223.     -- Deposit non-honeycomb items
  224.     for slot = 1, 16 do
  225.         local item = turtle.getItemDetail(slot)
  226.         if item and item.name ~= HONEYCOMB_ID then
  227.             turtle.select(slot)
  228.             turtle.dropDown()
  229.         end
  230.     end
  231.    
  232.     turtle.select(1)
  233. end
  234.  
  235. local function processCycle()
  236.     print("\n=== Starting honeycomb processing cycle ===")
  237.    
  238.     -- Pull honeycombs from chest
  239.     pullHoneycombs()
  240.    
  241.     -- Move to centrifuge area
  242.     up()
  243.     turnLeft()  -- Face south
  244.    
  245.     -- Try to deposit in 5 centrifuges
  246.     for i = 1, 5 do
  247.         forward(1)
  248.         print("Checking centrifuge " .. i .. "...")
  249.         depositHoneycomb()
  250.     end
  251.    
  252.     -- Return to start
  253.     returnUnusedItems()
  254.    
  255.     print("Cycle complete!")
  256. end
  257.  
  258. -- Main program
  259. local function main()
  260.     print("Honeycomb Centrifuge Automation")
  261.     print("================================")
  262.    
  263.     -- Check fuel
  264.     if not checkFuel() then
  265.         print("Please add fuel to any slot!")
  266.         while not checkFuel() do
  267.             os.sleep(5)
  268.         end
  269.     end
  270.    
  271.     -- Load or save position
  272.     if not loadPosition() then
  273.         print("Setting current position as start...")
  274.         savePosition()
  275.     end
  276.    
  277.     print("Starting in 3 seconds...")
  278.     print("Press Ctrl+T to stop")
  279.     os.sleep(3)
  280.    
  281.     -- Main loop
  282.     while true do
  283.         processCycle()
  284.         print("\nWaiting " .. CYCLE_DELAY .. " seconds...")
  285.         os.sleep(CYCLE_DELAY)
  286.     end
  287. end
  288.  
  289. -- Error handling
  290. local ok, err = pcall(main)
  291. if not ok then
  292.     print("Error: " .. err)
  293.     print("Turtle stopped.")
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement