Advertisement
civilwargeeky

Claude's Tree Farm

Sep 13th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.65 KB | Software | 0 0
  1. -- Configuration variables
  2. local CHOP_LEAVES = true  -- Set to false to disable chopping leaves
  3. local WAIT_TIME = 30  -- Time to wait between sapling checks (in seconds)
  4.  
  5. -- Analytics variables
  6. local startTime = os.clock()
  7. local treesChopped = 0
  8. local totalLogsCollected = 0
  9. local totalSaplingsCollected = 0
  10. local totalFuelUsed = 0
  11.  
  12. -- Function to check if the block in front is a sapling
  13. local function isSapling()
  14.     local success, data = turtle.inspect()
  15.     if success then
  16.         return string.find(data.name, "sapling") ~= nil
  17.     end
  18.     return false
  19. end
  20.  
  21. -- Function to count items in inventory
  22. local function countItems()
  23.     local count = 0
  24.     for i = 1, 16 do
  25.         count = count + turtle.getItemCount(i)
  26.     end
  27.     return count
  28. end
  29.  
  30. -- Function to cut down the tree and gather leaves
  31. local function cutTree()
  32.     print("Cutting down tree and gathering leaves...")
  33.     local initialFuel = turtle.getFuelLevel()
  34.     local initialSaplings = turtle.getItemCount(1)
  35.     local initialItems = countItems()
  36.    
  37.     local height = 0
  38.     while turtle.detect() do
  39.         turtle.dig()
  40.         turtle.digUp()
  41.         if CHOP_LEAVES and height > 0 then  -- Don't dig around at ground level
  42.             for _ = 1, 3 do
  43.                 turtle.turnRight()
  44.                 turtle.dig()
  45.             end
  46.             turtle.turnRight()  -- Return to original orientation
  47.         end
  48.         turtle.up()
  49.         height = height + 1
  50.     end
  51.    
  52.     -- Dig any remaining leaves above
  53.     turtle.digUp()
  54.    
  55.     print("Moving back down...")
  56.     for i = 1, height do
  57.         turtle.down()
  58.     end
  59.    
  60.     -- Update analytics
  61.     treesChopped = treesChopped + 1
  62.     totalLogsCollected = totalLogsCollected + (countItems() - initialItems)
  63.     totalSaplingsCollected = totalSaplingsCollected + (turtle.getItemCount(1) - initialSaplings)
  64.     totalFuelUsed = totalFuelUsed + (initialFuel - turtle.getFuelLevel())
  65. end
  66.  
  67. -- Function to refuel the turtle
  68. local function refuel()
  69.     print("Refueling...")
  70.     turtle.select(2)  -- Select the second slot for logs
  71.     while turtle.getFuelLevel() < 100 do
  72.         if turtle.getItemCount(2) > 0 and turtle.refuel(1) then
  73.             -- Refueled successfully
  74.         else
  75.             -- If we're out of fuel items, break the loop
  76.             break
  77.         end
  78.     end
  79.     turtle.select(1)  -- Select sapling slot
  80.     print("Refueling complete. Current fuel level: " .. turtle.getFuelLevel())
  81. end
  82.  
  83. -- Function to wait for fuel
  84. local function waitForFuel()
  85.     print("Fuel low. Please add fuel to the inventory.")
  86.     while turtle.getFuelLevel() < 100 do
  87.         local event, side, slot = os.pullEvent("turtle_inventory")
  88.         for i = 1, 16 do
  89.             turtle.select(i)
  90.             if turtle.refuel(0) then  -- Check if item is valid fuel
  91.                 while turtle.getFuelLevel() < 100 and turtle.getItemCount(i) > 0 do
  92.                     turtle.refuel(1)
  93.                 end
  94.                 if turtle.getFuelLevel() >= 100 then
  95.                     break
  96.                 end
  97.             end
  98.         end
  99.     end
  100.     print("Fuel level sufficient. Resuming operation.")
  101.     turtle.select(1)  -- Select sapling slot
  102. end
  103.  
  104. -- Function to get saplings from the chest on the left
  105. local function getSaplingsFromChest()
  106.     print("Attempting to get saplings from the chest...")
  107.     turtle.turnLeft()
  108.     local success = turtle.suck(64)  -- Try to get a stack of saplings
  109.     turtle.turnRight()
  110.     return success
  111. end
  112.  
  113. -- Function to wait for saplings
  114. local function waitForSaplings()
  115.     if getSaplingsFromChest() then
  116.         print("Retrieved saplings from chest. Resuming operation.")
  117.         return
  118.     end
  119.     print("Out of saplings. Please add saplings to the first inventory slot or to the chest on the left.")
  120.     while turtle.getItemCount(1) == 0 do
  121.         os.pullEvent("turtle_inventory")
  122.         if turtle.getItemCount(1) > 0 then
  123.             break
  124.         end
  125.         if getSaplingsFromChest() then
  126.             break
  127.         end
  128.     end
  129.     print("Saplings received. Resuming operation.")
  130. end
  131.  
  132. -- Function to deposit items in the chest behind
  133. local function depositItems()
  134.     print("Attempting to deposit items...")
  135.     turtle.turnRight()
  136.     turtle.turnRight()
  137.    
  138.     if not turtle.detect() then
  139.         print("No chest detected behind the turtle.")
  140.         print("Please place a chest behind the turtle to deposit items.")
  141.         print("Waiting 5 seconds before continuing...")
  142.         os.sleep(5)
  143.         turtle.turnRight()
  144.         turtle.turnRight()
  145.         return
  146.     end
  147.    
  148.     for i = 2, 16 do  -- Start from slot 2 to preserve saplings in slot 1
  149.         turtle.select(i)
  150.         turtle.drop()
  151.     end
  152.     turtle.turnRight()
  153.     turtle.turnRight()
  154.     turtle.select(1)  -- Select sapling slot
  155.     print("Items deposited.")
  156. end
  157.  
  158. -- Function to calculate and display statistics
  159. local function displayStatistics()
  160.     local elapsedHours = (os.clock() - startTime) / 3600
  161.     local treesPerHour = treesChopped / elapsedHours
  162.     local logsPerHour = totalLogsCollected / elapsedHours
  163.     local saplingsPerHour = totalSaplingsCollected / elapsedHours
  164.     local fuelPerHour = totalFuelUsed / elapsedHours
  165.    
  166.     term.clear()
  167.     term.setCursorPos(1,1)
  168.     print("The time is " .. textutils.formatTime(os.time(), false))
  169.     print("\nTree Farm Statistics (per hour):")
  170.     print(string.format("Trees chopped: %.2f", treesPerHour))
  171.     print(string.format("Logs collected: %.2f", logsPerHour))
  172.     print(string.format("Saplings collected: %.2f", saplingsPerHour))
  173.     print(string.format("Fuel used: %.2f", fuelPerHour))
  174.     print("\nTree farm is running. Waiting for tree to grow...")
  175.    
  176.     -- Broadcast statistics if rednet modem is available
  177.     if peripheral.find("modem") then
  178.         local stats = {
  179.             treesPerHour = treesPerHour,
  180.             logsPerHour = logsPerHour,
  181.             saplingsPerHour = saplingsPerHour,
  182.             fuelPerHour = fuelPerHour
  183.         }
  184.         rednet.broadcast(textutils.serialize(stats))
  185.     end
  186. end
  187.  
  188. -- Main function
  189. local function main()
  190.     print("Tree farm program starting...")
  191.     if turtle.getFuelLevel() < 100 then
  192.         waitForFuel()
  193.     end
  194.  
  195.     while true do
  196.         print("Checking for sapling...")
  197.         if not isSapling() then
  198.             cutTree()
  199.             refuel()
  200.             depositItems()
  201.             print("Placing sapling...")
  202.             if not turtle.place() then
  203.                 waitForSaplings()
  204.                 turtle.place()
  205.             end
  206.         end
  207.        
  208.         displayStatistics()
  209.         os.sleep(WAIT_TIME)
  210.     end
  211. end
  212.  
  213. -- Run the main function
  214. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement