Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration variables
- local CHOP_LEAVES = true -- Set to false to disable chopping leaves
- local WAIT_TIME = 30 -- Time to wait between sapling checks (in seconds)
- -- Analytics variables
- local startTime = os.clock()
- local treesChopped = 0
- local totalLogsCollected = 0
- local totalSaplingsCollected = 0
- local totalFuelUsed = 0
- -- Function to check if the block in front is a sapling
- local function isSapling()
- local success, data = turtle.inspect()
- if success then
- return string.find(data.name, "sapling") ~= nil
- end
- return false
- end
- -- Function to count items in inventory
- local function countItems()
- local count = 0
- for i = 1, 16 do
- count = count + turtle.getItemCount(i)
- end
- return count
- end
- -- Function to cut down the tree and gather leaves
- local function cutTree()
- print("Cutting down tree and gathering leaves...")
- local initialFuel = turtle.getFuelLevel()
- local initialSaplings = turtle.getItemCount(1)
- local initialItems = countItems()
- local height = 0
- while turtle.detect() do
- turtle.dig()
- turtle.digUp()
- if CHOP_LEAVES and height > 0 then -- Don't dig around at ground level
- for _ = 1, 3 do
- turtle.turnRight()
- turtle.dig()
- end
- turtle.turnRight() -- Return to original orientation
- end
- turtle.up()
- height = height + 1
- end
- -- Dig any remaining leaves above
- turtle.digUp()
- print("Moving back down...")
- for i = 1, height do
- turtle.down()
- end
- -- Update analytics
- treesChopped = treesChopped + 1
- totalLogsCollected = totalLogsCollected + (countItems() - initialItems)
- totalSaplingsCollected = totalSaplingsCollected + (turtle.getItemCount(1) - initialSaplings)
- totalFuelUsed = totalFuelUsed + (initialFuel - turtle.getFuelLevel())
- end
- -- Function to refuel the turtle
- local function refuel()
- print("Refueling...")
- turtle.select(2) -- Select the second slot for logs
- while turtle.getFuelLevel() < 100 do
- if turtle.getItemCount(2) > 0 and turtle.refuel(1) then
- -- Refueled successfully
- else
- -- If we're out of fuel items, break the loop
- break
- end
- end
- turtle.select(1) -- Select sapling slot
- print("Refueling complete. Current fuel level: " .. turtle.getFuelLevel())
- end
- -- Function to wait for fuel
- local function waitForFuel()
- print("Fuel low. Please add fuel to the inventory.")
- while turtle.getFuelLevel() < 100 do
- local event, side, slot = os.pullEvent("turtle_inventory")
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then -- Check if item is valid fuel
- while turtle.getFuelLevel() < 100 and turtle.getItemCount(i) > 0 do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= 100 then
- break
- end
- end
- end
- end
- print("Fuel level sufficient. Resuming operation.")
- turtle.select(1) -- Select sapling slot
- end
- -- Function to get saplings from the chest on the left
- local function getSaplingsFromChest()
- print("Attempting to get saplings from the chest...")
- turtle.turnLeft()
- local success = turtle.suck(64) -- Try to get a stack of saplings
- turtle.turnRight()
- return success
- end
- -- Function to wait for saplings
- local function waitForSaplings()
- if getSaplingsFromChest() then
- print("Retrieved saplings from chest. Resuming operation.")
- return
- end
- print("Out of saplings. Please add saplings to the first inventory slot or to the chest on the left.")
- while turtle.getItemCount(1) == 0 do
- os.pullEvent("turtle_inventory")
- if turtle.getItemCount(1) > 0 then
- break
- end
- if getSaplingsFromChest() then
- break
- end
- end
- print("Saplings received. Resuming operation.")
- end
- -- Function to deposit items in the chest behind
- local function depositItems()
- print("Attempting to deposit items...")
- turtle.turnRight()
- turtle.turnRight()
- if not turtle.detect() then
- print("No chest detected behind the turtle.")
- print("Please place a chest behind the turtle to deposit items.")
- print("Waiting 5 seconds before continuing...")
- os.sleep(5)
- turtle.turnRight()
- turtle.turnRight()
- return
- end
- for i = 2, 16 do -- Start from slot 2 to preserve saplings in slot 1
- turtle.select(i)
- turtle.drop()
- end
- turtle.turnRight()
- turtle.turnRight()
- turtle.select(1) -- Select sapling slot
- print("Items deposited.")
- end
- -- Function to calculate and display statistics
- local function displayStatistics()
- local elapsedHours = (os.clock() - startTime) / 3600
- local treesPerHour = treesChopped / elapsedHours
- local logsPerHour = totalLogsCollected / elapsedHours
- local saplingsPerHour = totalSaplingsCollected / elapsedHours
- local fuelPerHour = totalFuelUsed / elapsedHours
- term.clear()
- term.setCursorPos(1,1)
- print("The time is " .. textutils.formatTime(os.time(), false))
- print("\nTree Farm Statistics (per hour):")
- print(string.format("Trees chopped: %.2f", treesPerHour))
- print(string.format("Logs collected: %.2f", logsPerHour))
- print(string.format("Saplings collected: %.2f", saplingsPerHour))
- print(string.format("Fuel used: %.2f", fuelPerHour))
- print("\nTree farm is running. Waiting for tree to grow...")
- -- Broadcast statistics if rednet modem is available
- if peripheral.find("modem") then
- local stats = {
- treesPerHour = treesPerHour,
- logsPerHour = logsPerHour,
- saplingsPerHour = saplingsPerHour,
- fuelPerHour = fuelPerHour
- }
- rednet.broadcast(textutils.serialize(stats))
- end
- end
- -- Main function
- local function main()
- print("Tree farm program starting...")
- if turtle.getFuelLevel() < 100 then
- waitForFuel()
- end
- while true do
- print("Checking for sapling...")
- if not isSapling() then
- cutTree()
- refuel()
- depositItems()
- print("Placing sapling...")
- if not turtle.place() then
- waitForSaplings()
- turtle.place()
- end
- end
- displayStatistics()
- os.sleep(WAIT_TIME)
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement