Advertisement
Ubidibity

2x2_tree.lua

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