Advertisement
Peni2000

Wood_Turtle

Mar 16th, 2025
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | Gaming | 0 0
  1. -- Wood Farming Program for CC:Tweaked
  2. -- Configuration
  3. local gridWidth = 3      -- number of tree positions per row
  4. local gridHeight = 3     -- number of rows
  5. local spacing = 3        -- distance between tree positions (in blocks)
  6. local saplingSlot = 16    -- slot containing saplings
  7.  
  8. -- Helper function to check if a block is wood (e.g. oak_log, spruce_log, etc.)
  9. function isWood(blockData)
  10.     return string.find(blockData.name, "log") ~= nil
  11. end
  12.  
  13. -- Chops a tree by moving forward while wood is detected.
  14. -- It counts how many steps it moved so it can backtrack later.
  15. function chopTree()
  16.     local steps = 0
  17.     while turtle.detect() do
  18.         local success, data = turtle.inspect()
  19.         if success and isWood(data) then
  20.             turtle.dig()
  21.             if turtle.forward() then
  22.                 steps = steps + 1
  23.             else
  24.                 break
  25.             end
  26.         else
  27.             break
  28.         end
  29.     end
  30.     -- Return to original position
  31.     for i = 1, steps do
  32.         turtle.back()
  33.     end
  34. end
  35.  
  36. -- Replants a sapling (from the designated sapling slot)
  37. function replantSapling()
  38.     turtle.select(saplingSlot)
  39.     turtle.place()
  40. end
  41.  
  42. -- Harvests a tree at the current position (if one is present) and replants a sapling.
  43. function harvestAndReplant()
  44.     local success, data = turtle.inspect()
  45.     if success and isWood(data) then
  46.         print("Tree detected. Harvesting...")
  47.         chopTree()
  48.     else
  49.         print("No tree detected.")
  50.     end
  51.     print("Replanting sapling...")
  52.     replantSapling()
  53. end
  54.  
  55. -- Movement helper: move forward N steps
  56. function moveForwardSteps(n)
  57.     for i = 1, n do
  58.         while not turtle.forward() do
  59.             turtle.dig()
  60.             sleep(0.5)
  61.         end
  62.     end
  63. end
  64.  
  65. -- Movement helper: move back N steps
  66. function moveBackSteps(n)
  67.     for i = 1, n do
  68.         while not turtle.back() do
  69.             sleep(0.5)
  70.         end
  71.     end
  72. end
  73.  
  74. -- Farms a grid of tree positions in a zigzag pattern.
  75. function farmGrid()
  76.     for row = 1, gridHeight do
  77.         for col = 1, gridWidth do
  78.             print("Farming position: row " .. row .. ", col " .. col)
  79.             harvestAndReplant()
  80.             if col < gridWidth then
  81.                 moveForwardSteps(spacing)
  82.             end
  83.         end
  84.         if row < gridHeight then
  85.             -- Reposition to the next row using a zigzag pattern.
  86.             if row % 2 == 1 then
  87.                 turtle.turnRight()
  88.                 moveForwardSteps(spacing)
  89.                 turtle.turnRight()
  90.             else
  91.                 turtle.turnLeft()
  92.                 moveForwardSteps(spacing)
  93.                 turtle.turnLeft()
  94.             end
  95.         end
  96.     end
  97.     -- Return to the starting position:
  98.     if gridHeight % 2 == 1 then
  99.         turtle.turnRight()
  100.         for i = 1, gridWidth - 1 do
  101.             moveBackSteps(spacing)
  102.         end
  103.         turtle.turnRight()
  104.         moveBackSteps((gridHeight - 1) * spacing)
  105.         turtle.turnRight()
  106.         turtle.turnRight()
  107.     else
  108.         turtle.turnLeft()
  109.         for i = 1, gridWidth - 1 do
  110.             moveBackSteps(spacing)
  111.         end
  112.         turtle.turnLeft()
  113.         moveBackSteps((gridHeight - 1) * spacing)
  114.         turtle.turnRight()
  115.         turtle.turnRight()
  116.     end
  117. end
  118.  
  119. -- Main loop: farm the grid, wait for trees to grow, then repeat.
  120. while true do
  121.     farmGrid()
  122.     print("Cycle complete. Waiting for trees to grow...")
  123.     sleep(600)  -- Wait 10 minutes (adjust as needed)
  124. end
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement