originalceebs

CCFellingTurtle

Sep 20th, 2021
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.11 KB | None | 0 0
  1. --[[
  2.  
  3. Script Created by CEEBS
  4. YouTube: https://www.youtube.com/channel/UCQvB8QBknoS1jYgwpYGQrQg
  5. Twitter: https://twitter.com/OnlyCeebs
  6.  
  7. Really appreciate all the support you have given me, so thank you!
  8.  
  9. ]]--
  10.  
  11. -- Apply constraints
  12. local FARM_HEIGHT = 7
  13. local TREE_SPACING = 4
  14. local TREES_PER_ROW = 4
  15. local ROWS = 4
  16.  
  17. local TIME_BEFORE_SWEEP = 120
  18. local TIME_BEFORE_NEXT_LOOP = 240
  19.  
  20. local INVENTORY_SIZE = 16
  21. local REFUEL_AMOUNT = 600
  22.  
  23. -- Items
  24. local CHEST = "minecraft:chest"
  25. local APPLE = "minecraft:apple"
  26. local STICK = "minecraft:stick"
  27. local ACCEPTED_FUELS = {
  28.     "minecraft:coal_block",
  29.     "minecraft:charcoal",
  30.     "minecraft:coal"
  31. }
  32. local LOGS = {
  33.     "minecraft:oak_log",
  34.     "minecraft:spruce_log",
  35.     "minecraft:birch_log",
  36.     "minecraft:jungle_log",
  37.     "minecraft:acacia_log",
  38.     "minecraft:dark_oak_log",
  39. }
  40. local SAPLINGS = {
  41.     "minecraft:oak_sapling",
  42.     "minecraft:spruce_sapling",
  43.     "minecraft:birch_sapling",
  44.     "minecraft:jungle_sapling",
  45.     "minecraft:acacia_sapling",
  46.     "minecraft:dark_oak_sapling",
  47. }
  48.  
  49. -- Movement helper functions
  50. local function moveForward(times)
  51.     for i = 1, times do
  52.         turtle.forward()
  53.         turtle.suck()
  54.     end
  55. end
  56.  
  57. local function turnLeft(times)
  58.     for i = 1, times do
  59.         turtle.turnLeft()
  60.     end
  61. end
  62.  
  63. local function turnRight(times)
  64.     for i = 1, times do
  65.         turtle.turnRight()
  66.     end
  67. end
  68.  
  69. -- Inventory Sorting
  70. local function inventorySort()
  71.     -- Group items together
  72.     for j = 1, INVENTORY_SIZE do
  73.         local currentItem = turtle.getItemDetail(j)
  74.  
  75.         if currentItem ~= nil then
  76.             turtle.select(j)
  77.             for k = j, INVENTORY_SIZE do
  78.                 if turtle.compareTo(k) then
  79.                     turtle.select(k)
  80.                     turtle.transferTo(j)
  81.                     turtle.select(j)
  82.                 end
  83.             end
  84.         end
  85.     end
  86. end
  87.  
  88. -- Find items in the turtles inventory and place
  89. local function findAndPlace(item)
  90.     local success, data, err = turtle.inspect()
  91.  
  92.     if success then
  93.         if data.name == CHEST then
  94.             -- Find and deposit
  95.             for i = 1, INVENTORY_SIZE do
  96.                 local currentItem = turtle.getItemDetail(i)
  97.  
  98.                 if currentItem ~= nil then
  99.                     if currentItem.name == item then
  100.                         turtle.select(i)
  101.                         turtle.drop()
  102.                     end
  103.                 end
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109. local function findAndPlaceAll(items)
  110.     local success, data, err = turtle.inspect()
  111.  
  112.     if success then
  113.         if data.name == CHEST then
  114.             for i = 1, INVENTORY_SIZE do
  115.                 local currentItem = turtle.getItemDetail(i)
  116.                 if currentItem ~= nil then
  117.                      for j = 1, #items do
  118.                         if currentItem.name == items[j] then
  119.                             turtle.select(i)
  120.                             turtle.drop()
  121.                         end
  122.                     end
  123.                 end
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. -- Refuel
  130. local function refuel()
  131.     local refuel_successful = false
  132.  
  133.     for i = 1, INVENTORY_SIZE do
  134.         local current_item = turtle.getItemDetail(i)
  135.         if current_item ~= nil then
  136.             for x = 1, #ACCEPTED_FUELS do
  137.                 if current_item.name == ACCEPTED_FUELS[x] then
  138.                     turtle.select(i)
  139.                     if turtle.refuel() then
  140.                         refuel_successful = true
  141.                     end
  142.                 end
  143.             end
  144.         end
  145.     end
  146.  
  147.     return refuel_successful
  148. end
  149.  
  150. -- Replant our trees
  151. local function replant()
  152.     for i = 1, INVENTORY_SIZE do
  153.         local current_item = turtle.getItemDetail(i)
  154.         if current_item ~= nil then
  155.             for x = 1, #SAPLINGS do
  156.                 if current_item.name == SAPLINGS[x] then
  157.                     turtle.select(i)
  158.                     turtle.place()
  159.                 end
  160.             end
  161.         end
  162.     end
  163. end
  164.  
  165. -- Chop chop
  166. local function chop()
  167.     -- Loop through the height of the tree
  168.     for i = 1, FARM_HEIGHT do
  169.         -- Check the block infront of the turtle
  170.         local success, data, err = turtle.inspect()
  171.        
  172.         -- Block found
  173.         if success then
  174.             local isLog = false
  175.  
  176.             -- Loop through our defined log table
  177.             for j = 1, #LOGS do
  178.                 if data.name == LOGS[j] then
  179.                     isLog = true
  180.                     break
  181.                 end
  182.             end
  183.  
  184.             -- Check to see if we did indeed find a log, if so - chop
  185.             if isLog then
  186.                 turtle.dig()
  187.             else
  188.                 -- If the turtle is on the ground level
  189.                 if i == 1 then
  190.                     return
  191.                 end
  192.             end
  193.         else
  194.             -- If the turtle is on the ground level
  195.             if i == 1 then
  196.                 replant()
  197.                 return
  198.             end
  199.         end
  200.  
  201.         -- Move the turtle
  202.         if i ~= FARM_HEIGHT then
  203.             while not turtle.up() do
  204.                 turtle.digUp()
  205.             end
  206.         end
  207.     end
  208.  
  209.     -- Decend back down to the ground
  210.     for i = 1, FARM_HEIGHT do
  211.         turtle.down()
  212.     end
  213.  
  214.     replant()
  215. end
  216.  
  217. -- Sweeping
  218. local function sweep()
  219.     local spaceBetween = TREE_SPACING - 1
  220.     local rowsWithBuffer = ROWS + 1
  221.  
  222.     turnLeft(1)
  223.     moveForward(TREE_SPACING)
  224.     turnRight(1)
  225.  
  226.     for i = 1, rowsWithBuffer do
  227.         for j = 1, spaceBetween do
  228.             moveForward(TREE_SPACING * 4)
  229.  
  230.             if i % 2 == 0 then
  231.                 if j % 2 == 0 then
  232.                     turnRight(1)
  233.                     moveForward(1)
  234.  
  235.                     if j ~= spaceBetween then
  236.                         turnRight(1)
  237.                     else
  238.                         moveForward(1)
  239.                     end
  240.  
  241.                 else
  242.                     turnLeft(1)
  243.                     moveForward(1)
  244.  
  245.                     if j ~= spaceBetween then
  246.                         turnLeft(1)
  247.                     else
  248.                         moveForward(1)
  249.                     end
  250.                 end
  251.             else
  252.                 if j % 2 == 0 then
  253.                     turnLeft(1)
  254.                     moveForward(1)
  255.  
  256.                     if j ~= spaceBetween then
  257.                         turnLeft(1)
  258.                     else
  259.                         moveForward(1)
  260.                     end
  261.                 else
  262.                     turnRight(1)
  263.                     moveForward(1)
  264.                    
  265.                     if j ~= spaceBetween then
  266.                         turnRight(1)
  267.                     else
  268.                         moveForward(1)
  269.                     end
  270.                 end
  271.             end
  272.         end
  273.  
  274.         if i ~= rowsWithBuffer then
  275.             if i % 2 == 0 then
  276.                 turnLeft(1)
  277.             else
  278.                 turnRight(1)
  279.             end
  280.         else
  281.             if i % 2 == 0 then
  282.                 -- Handle the case for the turtle finishing on an even amount of sweeps.
  283.             else
  284.                 turnRight(1)
  285.                 moveForward(TREE_SPACING * 4)
  286.                 turnRight(1)
  287.                 moveForward(TREE_SPACING * 4)
  288.                 turnRight(1)
  289.             end
  290.         end
  291.     end
  292. end
  293.  
  294. -- Deposit items in chests
  295. local function deposit()
  296.     -- Depoist apples
  297.     turnLeft(1)
  298.     moveForward(3)
  299.     turnLeft(1)
  300.  
  301.     findAndPlace(APPLE)
  302.  
  303.     -- Deposit sticks
  304.     turnLeft(1)
  305.     moveForward(1)
  306.     turnRight(1)
  307.  
  308.     findAndPlace(STICK)
  309.  
  310.     -- Deposit saplings
  311.     turnLeft(1)
  312.     moveForward(1)
  313.     turnRight(1)
  314.  
  315.     findAndPlaceAll(SAPLINGS)
  316.  
  317.     -- Get a stack off saplings for replanting
  318.     turtle.suck()
  319.  
  320.     -- Deposit logs
  321.     turnLeft(1)
  322.     moveForward(1)
  323.     turnRight(1)
  324.  
  325.     findAndPlaceAll(LOGS)
  326.  
  327.     turnLeft(2)
  328. end
  329.  
  330. -- Felling sequence
  331. local function fellingSequence()
  332.     moveForward(TREE_SPACING - 1)
  333.  
  334.     -- Loops through the number of rows
  335.     for i = 1, ROWS do
  336.         -- Loop through the number of trees per row
  337.         for j = 1, TREES_PER_ROW do
  338.             -- If the current row is even then - else
  339.             if i % 2 == 0 then
  340.                 turnRight(1)
  341.                 chop()
  342.                 turnLeft(1)
  343.             else
  344.                 turnLeft(1)
  345.                 chop()
  346.                 turnRight(1)
  347.             end
  348.  
  349.             -- Move between trees
  350.             if j ~= TREES_PER_ROW then
  351.                 moveForward(TREE_SPACING)
  352.             end
  353.         end
  354.  
  355.         -- Are we on the last row?
  356.         if i ~= ROWS then
  357.             -- Is the current row even?
  358.             if i % 2 == 0 then
  359.                 moveForward(1)
  360.                 turnLeft(1)
  361.                 moveForward(TREE_SPACING)
  362.                 turnLeft(1)
  363.                 moveForward(1)
  364.             else
  365.                 moveForward(1)
  366.                 turnRight(1)
  367.                 moveForward(TREE_SPACING)
  368.                 turnRight(1)
  369.                 moveForward(1)
  370.             end
  371.         else
  372.             -- Return to starting position
  373.             if i % 2 == 0 then
  374.                 moveForward(TREE_SPACING - 1)
  375.                 turnRight(1)
  376.                 moveForward(TREE_SPACING * (ROWS - 1))
  377.                 turnRight(1)
  378.             else
  379.                 -- Handle the case of an odd amount of rows
  380.                 moveForward(1)
  381.                 turnRight(1)
  382.                 moveForward(2)
  383.                 turnRight(1)
  384.                 moveForward(TREE_SPACING * 4)
  385.                 turnRight(1)
  386.                 moveForward(TREE_SPACING * (ROWS - 1))
  387.                 turnRight(1)
  388.             end
  389.         end
  390.     end
  391.  
  392.     inventorySort()
  393.  
  394.     sleep(TIME_BEFORE_SWEEP)
  395.  
  396.     sweep()
  397.  
  398.     deposit()
  399. end
  400.  
  401. -- Main Loop
  402. while true do
  403.     if turtle.getFuelLevel() < REFUEL_AMOUNT then
  404.         if not refuel() then
  405.             print("Not enough fuel, terminating...")
  406.             return
  407.         end
  408.     end
  409.  
  410.     fellingSequence()
  411.  
  412.     sleep(TIME_BEFORE_NEXT_LOOP)
  413. end
Advertisement
Add Comment
Please, Sign In to add comment