Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to get valid number input from user
- function getInput(prompt)
- while true do
- print(prompt)
- local input = read()
- local num = tonumber(input)
- if num then
- return num
- else
- print("Invalid input. Please enter a number.")
- end
- end
- end
- -- Function to check for a tree on the given side (left/right)
- function checkForTree(side)
- if side == "left" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- if turtle.detect() then
- turtle.select(2) -- Select log for comparison
- if turtle.compare() then
- print("Tree detected on the " .. side .. ". Chopping and replanting.")
- -- Chopping and replanting logic goes here
- end
- end
- -- Return to original orientation
- if side == "left" then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- -- Move to the next row depending on which side we just checked
- function moveToNextRow(side)
- -- Step 1: Move 1 block forward (past the last sapling row)
- turtle.forward()
- -- Step 2: Turn and move 5 blocks up to next sapling line
- if side == "left" then
- turtle.turnRight()
- for i = 1, 5 do
- turtle.forward()
- end
- turtle.turnRight()
- elseif side == "right" then
- turtle.turnLeft()
- for i = 1, 5 do
- turtle.forward()
- end
- turtle.turnLeft()
- end
- -- Step 3: Move 1 forward to align with the next sapling row
- turtle.forward()
- end
- -- Return to original starting position
- function returnToStart(length, width, side)
- -- Turn around
- turtle.turnLeft()
- turtle.turnLeft()
- if side == "right" then
- turtle.forward()
- turtle.turnLeft()
- for i = 1, 5 * (width - 1) do
- turtle.forward()
- end
- elseif side == "left" then
- for i = 1, (5 * length) - 6 do
- turtle.forward()
- end
- turtle.turnRight()
- for i = 1, 5 * (width - 1) do
- turtle.forward()
- end
- end
- -- Final adjustments for original direction, but facing the chest
- turtle.turnLeft()
- turtle.forward()
- end
- -- Refuel until 1000 using all slots except 1 and 2, then deposit leftovers in chest behind
- function refuelAndDeposit()
- local fuelNeeded = 1000 - turtle.getFuelLevel()
- -- Try to refuel from slots 3-16
- for i = 3, 16 do
- if fuelNeeded <= 0 then break end
- turtle.select(i)
- while turtle.refuel(1) do
- fuelNeeded = fuelNeeded - 1
- if fuelNeeded <= 0 then break end
- end
- end
- -- Deposit remaining items in slots 3-16
- for i = 3, 16 do
- turtle.select(i)
- turtle.drop() -- Drops behind
- end
- -- Reselect slot 1 and reset to original facing to keep things clean
- turtle.select(1)
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Main traversal logic through the sapling grid
- function checkTrees(length, width)
- local finalSide = "left" -- We'll update this as we go
- for row = 1, width do
- local side = (row % 2 == 1) and "left" or "right"
- finalSide = side -- Remember the last side for return logic
- for x = 1, length do
- checkForTree(side)
- if x < length then
- for i = 1, 5 do
- turtle.forward()
- end
- end
- end
- if row < width then
- moveToNextRow(side)
- end
- end
- returnToStart(length, width, finalSide)
- end
- -- Prompt the user for the grid size
- local length = getInput("Enter the number of trees per row (length):")
- local width = getInput("Enter the number of rows (width):")
- -- Start repeating the process every minute
- while true do
- -- Begin traversal
- checkTrees(length, width)
- -- Refuel and deposit at the end
- refuelAndDeposit()
- -- Wait for 1 minute before repeating
- print("Waiting for 1 minute before the next round...")
- os.sleep(60) -- Wait 60 seconds (1 minute)
- end
Add Comment
Please, Sign In to add comment