Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Wood Farming Program for CC:Tweaked
- -- Configuration
- local gridWidth = 3 -- number of tree positions per row
- local gridHeight = 3 -- number of rows
- local spacing = 3 -- distance between tree positions (in blocks)
- local saplingSlot = 16 -- slot containing saplings
- -- Helper function to check if a block is wood (e.g. oak_log, spruce_log, etc.)
- function isWood(blockData)
- return string.find(blockData.name, "log") ~= nil
- end
- -- Chops a tree by moving forward while wood is detected.
- -- It counts how many steps it moved so it can backtrack later.
- function chopTree()
- local steps = 0
- while turtle.detect() do
- local success, data = turtle.inspect()
- if success and isWood(data) then
- turtle.dig()
- if turtle.forward() then
- steps = steps + 1
- else
- break
- end
- else
- break
- end
- end
- -- Return to original position
- for i = 1, steps do
- turtle.back()
- end
- end
- -- Replants a sapling (from the designated sapling slot)
- function replantSapling()
- turtle.select(saplingSlot)
- turtle.place()
- end
- -- Harvests a tree at the current position (if one is present) and replants a sapling.
- function harvestAndReplant()
- local success, data = turtle.inspect()
- if success and isWood(data) then
- print("Tree detected. Harvesting...")
- chopTree()
- else
- print("No tree detected.")
- end
- print("Replanting sapling...")
- replantSapling()
- end
- -- Movement helper: move forward N steps
- function moveForwardSteps(n)
- for i = 1, n do
- while not turtle.forward() do
- turtle.dig()
- sleep(0.5)
- end
- end
- end
- -- Movement helper: move back N steps
- function moveBackSteps(n)
- for i = 1, n do
- while not turtle.back() do
- sleep(0.5)
- end
- end
- end
- -- Farms a grid of tree positions in a zigzag pattern.
- function farmGrid()
- for row = 1, gridHeight do
- for col = 1, gridWidth do
- print("Farming position: row " .. row .. ", col " .. col)
- harvestAndReplant()
- if col < gridWidth then
- moveForwardSteps(spacing)
- end
- end
- if row < gridHeight then
- -- Reposition to the next row using a zigzag pattern.
- if row % 2 == 1 then
- turtle.turnRight()
- moveForwardSteps(spacing)
- turtle.turnRight()
- else
- turtle.turnLeft()
- moveForwardSteps(spacing)
- turtle.turnLeft()
- end
- end
- end
- -- Return to the starting position:
- if gridHeight % 2 == 1 then
- turtle.turnRight()
- for i = 1, gridWidth - 1 do
- moveBackSteps(spacing)
- end
- turtle.turnRight()
- moveBackSteps((gridHeight - 1) * spacing)
- turtle.turnRight()
- turtle.turnRight()
- else
- turtle.turnLeft()
- for i = 1, gridWidth - 1 do
- moveBackSteps(spacing)
- end
- turtle.turnLeft()
- moveBackSteps((gridHeight - 1) * spacing)
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- -- Main loop: farm the grid, wait for trees to grow, then repeat.
- while true do
- farmGrid()
- print("Cycle complete. Waiting for trees to grow...")
- sleep(600) -- Wait 10 minutes (adjust as needed)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement