Advertisement
Ubidibity

2x2_tree.lua

Apr 22nd, 2025 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | Gaming | 0 0
  1. -- simp_tree.lua: Enhanced tree felling with branch handling for 1x1 and 2x2 trees
  2. -- Array of valid wood block IDs (expand as needed for ATM 10 trees)
  3. -- Credit: derivative from Grok's original work, second iteration of second version (first version being 1x1 only as 'simp_tree.lua'
  4. -- Usage: place at base of tree, in bottom-left corner of 2x2 trunk (or at 1x1 trunk), facing east (into trunk).
  5.  
  6. local woodBlocks = {
  7.     "minecraft:oak_log",
  8.     "minecraft:spruce_log",
  9.     "minecraft:birch_log",
  10.     "minecraft:jungle_log",
  11.     "minecraft:acacia_log",
  12.     "minecraft:dark_oak_log",
  13.     "regions_unexplored:blackwood_log"
  14.     -- Add modded wood types here, e.g., "biomesoplenty:mahogany_log"
  15. }
  16.  
  17. -- Function to check if a block is a wood block
  18. local function isWoodBlock(blockName)
  19.     for _, wood in ipairs(woodBlocks) do
  20.         if blockName == wood then
  21.             return true
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. -- Function to handle a single branch in the current direction (1x1 branches)
  28. local function handleBranch()
  29.     local counter = 0
  30.     local success, data = turtle.inspect()
  31.    
  32.     -- Check if the block in front is wood
  33.     if success and isWoodBlock(data.name) then
  34.         while true do
  35.             -- Dig and move forward
  36.             turtle.dig()
  37.             if turtle.forward() then
  38.                 counter = counter + 1
  39.             else
  40.                 break -- Can't move forward (obstruction)
  41.             end
  42.            
  43.             -- Check the next block
  44.             success, data = turtle.inspect()
  45.             if not success or not isWoodBlock(data.name) then
  46.                 break -- Hit non-wood block (leaves, air, etc.)
  47.             end
  48.         end
  49.        
  50.         -- Turn around (180 degrees)
  51.         turtle.turnRight()
  52.         turtle.turnRight()
  53.        
  54.         -- Move back to starting position
  55.         for i = 1, counter do
  56.             turtle.forward()
  57.         end
  58.        
  59.         -- Face original direction
  60.         turtle.turnRight()
  61.         turtle.turnRight()
  62.     end
  63. end
  64.  
  65. -- Function to detect if the tree is 2x2 by checking adjacent blocks
  66. local function is2x2Tree()
  67.     local success, data = turtle.inspect() -- Block in front (east)
  68.     if not (success and isWoodBlock(data.name)) then
  69.         return false -- Not even a trunk in front
  70.     end
  71.     local woodType = data.name -- Store the wood type for comparison
  72.    
  73.     -- Turn right to check south block
  74.     turtle.turnRight()
  75.     success, data = turtle.inspect() -- Block to south
  76.     if not (success and data.name == woodType) then
  77.         turtle.turnLeft() -- Restore orientation
  78.         return false
  79.     end
  80.    
  81.     -- Move forward and check southeast block
  82.     turtle.dig()
  83.     if not turtle.forward() then
  84.         turtle.turnLeft()
  85.         return false
  86.     end
  87.     success, data = turtle.inspect() -- Block to south (from new position)
  88.     if not (success and data.name == woodType) then
  89.         turtle.back()
  90.         turtle.turnLeft()
  91.         return false
  92.     end
  93.    
  94.     -- Return to starting position
  95.     turtle.back()
  96.     turtle.turnLeft()
  97.     return true
  98. end
  99.  
  100. -- Function to mine 2x2 trunk at current height and check branches
  101. local function mine2x2Trunk()
  102.     -- Assume turtle is in bottom-left (southwest) corner, facing east
  103.     -- Path: mine bottom-left, move to bottom-right, top-right, top-left, return to bottom-left
  104.     turtle.dig() -- Mine bottom-left
  105.     turtle.turnLeft() -- Face north to check branch
  106.     handleBranch() -- Check south (left relative to east)
  107.     turtle.turnRight() -- Face east
  108.     turtle.forward() -- Move to bottom-right
  109.    
  110.     turtle.dig() -- Mine bottom-right
  111.     turtle.turnRight() -- Face south to check branch
  112.     handleBranch() -- Check east (left relative to north)
  113.     turtle.turnLeft() -- Face north
  114.     turtle.forward() -- Move to top-right
  115.    
  116.     turtle.dig() -- Mine top-right
  117.     turtle.turnRight() -- Face west to check branch
  118.     handleBranch() -- Check north (left relative to west)
  119.     turtle.turnLeft() -- Face west
  120.     turtle.forward() -- Move to top-left
  121.    
  122.     turtle.dig() -- Mine top-left
  123.     turtle.turnRight() -- Face north to check branch
  124.     handleBranch() -- Check west (left relative to south)
  125.     turtle.turnLeft() -- Face south
  126.     turtle.forward() -- Move to bottom-left
  127.    
  128.     turtle.turnRight() -- Face east (restore starting orientation)
  129. end
  130.  
  131. -- Main tree felling function
  132. local function fellTree()
  133.     -- Detect tree type
  134.     local is2x2 = is2x2Tree()
  135.    
  136.     if is2x2 then
  137.         -- 2x2 tree logic
  138.         while true do
  139.             mine2x2Trunk()
  140.             -- Dig up in bottom-left position and try to move up
  141.             turtle.digUp()
  142.             if not turtle.up() then
  143.                 break -- Can't move up anymore
  144.             end
  145.             -- Dig up in other three positions to clear trunk
  146.             turtle.forward()
  147.             turtle.digUp()
  148.             turtle.turnLeft()
  149.             turtle.forward()
  150.             turtle.digUp()
  151.             turtle.turnLeft()
  152.             turtle.forward()
  153.             turtle.digUp()
  154.             turtle.turnLeft()
  155.             turtle.forward()
  156.             turtle.turnLeft() -- Back to bottom-left, facing east
  157.         end
  158.     else
  159.         -- 1x1 tree logic (your corrected version)
  160.         turtle.dig()
  161.         turtle.forward()
  162.         while true do
  163.             turtle.digUp()
  164.             -- Check for branches in all four directions
  165.             for i = 1, 4 do
  166.                 handleBranch()
  167.                 turtle.turnRight() -- Check next direction
  168.             end
  169.             if not turtle.up() then
  170.                 break -- Can't move up
  171.             end
  172.         end
  173.     end
  174.    
  175.     -- Return to ground
  176.     while turtle.down() do
  177.         -- Do nothing, just go down
  178.     end
  179. end
  180.  
  181. -- Main program
  182. print("Starting simp_tree with 1x1 and 2x2 tree handling...")
  183. fellTree()
  184. print("Tree felling complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement