Advertisement
KTRD

Tree choppa

Feb 19th, 2025 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.36 KB | None | 0 0
  1. -- Mocking the APIs for editor support
  2. -- Remove before running in minecraft
  3. --[[
  4. local turtle = {
  5.     select = function(_) end,
  6.     getFuelLevel = function()
  7.         return 1000
  8.     end,
  9.     refuel = function(_) end,
  10.     dropDown = function() end,
  11.     getItemCount = function(_)
  12.         return 0
  13.     end,
  14.     forward = function()
  15.         return true
  16.     end,
  17.     up = function()
  18.         return true
  19.     end,
  20.     down = function()
  21.         return true
  22.     end,
  23.     dig = function() end,
  24.     digUp = function() end,
  25.     digDown = function() end,
  26.     back = function() end,
  27. }
  28. local redstone = {
  29.     setOutput = function(_, _) end,
  30.     getInput = function(_)
  31.         return false
  32.     end,
  33. }
  34. local os = {
  35.     sleep = function(_) end,
  36. }
  37. local gps = {
  38.     locate = function(_)
  39.         return 0, 0, 0
  40.     end,
  41. }
  42. local fs = {
  43.     exists = function(_)
  44.         return false
  45.     end,
  46.     open = function(_, _)
  47.         return {
  48.             writeLine = function(_) end,
  49.             readLine = function()
  50.                 return "0"
  51.             end,
  52.             close = function() end,
  53.         }
  54.     end,
  55. }
  56. local rednet = {
  57.     open = function(_) end,
  58.     close = function(_) end,
  59. }
  60. --]]
  61.  
  62. -- Sapling goes to slot 1
  63. -- Wood goes to slot 2
  64. -- Fuel goes to slot 3
  65. -- Trees should be on the right side of the turtle
  66.  
  67. local HOME_FILE_NAME = "tree_choppa_home"
  68. local BLOCKS_BEFORE_TREES = 3
  69. local TREE_BLOCKS = 27
  70. local FUEL_MINIMUM = 1000
  71.  
  72. local function pulse(side)
  73.     redstone.setOutput(side, true)
  74.     os.sleep(0.05)
  75.     redstone.setOutput(side, false)
  76.     os.sleep(0.05)
  77. end
  78.  
  79. local function refuel()
  80.     turtle.select(3)
  81.     while turtle.getFuelLevel() < FUEL_MINIMUM do
  82.         print("Refueling...")
  83.         if not turtle.refuel(0) then
  84.             pulse("top")
  85.             print("Waiting for fuel...")
  86.             while not turtle.refuel(0) do
  87.                 os.sleep(10)
  88.                 pulse("top")
  89.             end
  90.         end
  91.         turtle.refuel()
  92.         print("Refueled!")
  93.     end
  94.     turtle.select(1)
  95. end
  96.  
  97. local function restock()
  98.     print("Restocking saplings...")
  99.     turtle.dropDown()
  100.     pulse("back")
  101.  
  102.     if turtle.getItemCount(1) == 0 then
  103.         print("Waiting for saplings...")
  104.         while turtle.getItemCount(1) == 0 do
  105.             os.sleep(10)
  106.             pulse("back")
  107.         end
  108.     end
  109.     print("Restocked!")
  110. end
  111.  
  112. local function dropAll()
  113.     print("Dropping all collected items...")
  114.     for i = 3, 16 do
  115.         if turtle.getItemCount(i) ~= 0 then
  116.             turtle.select(i)
  117.             turtle.dropDown()
  118.         end
  119.     end
  120.     turtle.select(1)
  121.     print("Dropped all collected items!")
  122. end
  123.  
  124. local function forward()
  125.     if not turtle.forward() then
  126.         print("Can't go forward, please clear the way manually") --Should happen rarely if ever
  127.         while not turtle.forward() do
  128.             os.sleep(10)
  129.         end
  130.         print("The way is cleared, continuing...")
  131.     end
  132. end
  133.  
  134. local function back()
  135.     if not turtle.back() then
  136.         print("Can't go back, please clear the way manually") --Should happen rarely if ever
  137.         while not turtle.back() do
  138.             os.sleep(10)
  139.         end
  140.         print("The way is cleared, continuing...")
  141.     end
  142. end
  143.  
  144. local function down()
  145.     while not turtle.down() do
  146.         turtle.digDown()
  147.     end
  148. end
  149.  
  150. local function up()
  151.     while not turtle.up() do
  152.         turtle.digUp()
  153.     end
  154. end
  155.  
  156. local function comeDown(dy)
  157.     print("Coming down for " .. dy .. " blocks")
  158.     for _ = dy, 1, -1 do
  159.         down()
  160.     end
  161.     print("Came down!")
  162. end
  163.  
  164. local function chopUp()
  165.     print("Chopping up the tree...")
  166.     local dy = 0
  167.     while turtle.detect() do
  168.         turtle.dig()
  169.         up()
  170.         dy = dy + 1
  171.     end
  172.     print("Chopped up the tree of height " .. dy)
  173.     return dy
  174. end
  175.  
  176. local function locate()
  177.     print("Getting the current location via GPS...")
  178.     rednet.open("right")
  179.     local x, y, z = gps.locate(1)
  180.     if x == nil then
  181.         print("Waiting for GPS signal, probably lost signal due to a storm")
  182.         while x == nil do
  183.             os.sleep(10)
  184.             x, y, z = gps.locate(1)
  185.         end
  186.     end
  187.     rednet.close("right")
  188.     print("Got the current location via GPS!")
  189.     return x, y, z
  190. end
  191.  
  192. local function comeBack(xz, xz0)
  193.     print("Coming back to the home point...")
  194.     for _ = 1, math.abs(xz0 - xz) do
  195.         back()
  196.     end
  197.     print("At the home point!")
  198. end
  199.  
  200. local function checkAndChop()
  201.     turtle.turnRight()
  202.     turtle.select(2)
  203.     if turtle.compare() then
  204.         local dy = chopUp()
  205.         comeDown(dy)
  206.         dropAll()
  207.     end
  208.     turtle.select(1)
  209.     turtle.place()
  210.     turtle.turnLeft()
  211. end
  212.  
  213. local function init()
  214.     local x, y, z = locate()
  215.     turtle.select(1)
  216.     local x0, y0, z0, s0 = nil, nil, nil, nil
  217.  
  218.     print("Checking if the home point is saved...")
  219.     if not fs.exists(HOME_FILE_NAME) then
  220.         print("Home point is not saved, I am at the very start, creating a file for the home point...")
  221.         x0 = x
  222.         y0 = y
  223.         z0 = z
  224.  
  225.         print("Home point is at x=" .. x0 .. ", y=" .. y0 .. ", z=" .. z0)
  226.  
  227.         restock()
  228.         refuel()
  229.  
  230.         print("Moving to determine the orientation...")
  231.         forward()
  232.         local x1, _, z1 = locate()
  233.         back()
  234.         if x1 > x0 then
  235.             s0 = "x+"
  236.         elseif x1 < x0 then
  237.             s0 = "x-"
  238.         elseif z1 > z0 then
  239.             s0 = "z+"
  240.         else
  241.             s0 = "z-"
  242.         end
  243.         print("Orientation is " .. s0)
  244.  
  245.         print("Writing the home point and orientation to the file...")
  246.         local f = fs.open(HOME_FILE_NAME, "w")
  247.         f.writeLine(tostring(x0))
  248.         f.writeLine(tostring(y0))
  249.         f.writeLine(tostring(z0))
  250.         f.writeLine(s0)
  251.         f.close()
  252.         print("A home point and an orientation are saved!")
  253.         return
  254.     end
  255.  
  256.     print("Not at the very start, loading the home point from the file...")
  257.     local f = fs.open(HOME_FILE_NAME, "r")
  258.     x0 = tonumber(f.readLine())
  259.     y0 = tonumber(f.readLine())
  260.     z0 = tonumber(f.readLine())
  261.     s0 = f.readLine()
  262.     f.close()
  263.     print("Home point is at x=" .. x0 .. ", y=" .. y0 .. ", z=" .. z0)
  264.     print("Orientation is " .. s0)
  265.  
  266.     print("Checking if turtle is in the air...")
  267.     if y > y0 then
  268.         print("Turtle is in the air")
  269.         turtle.dig()
  270.         up()
  271.         local dy = chopUp()
  272.         comeDown(y + 1 + dy - y0)
  273.         turtle.place()
  274.         turtle.turnLeft()
  275.         dropAll()
  276.         if (s0 == "x+") or (s0 == "x-") then
  277.             comeBack(x, x0)
  278.         else
  279.             comeBack(z, z0)
  280.         end
  281.         return
  282.     end
  283.  
  284.     print("Turtle is on the ground")
  285.     print("Checking if turtle is not in line with the home point...")
  286.     if ((s0 == "x+") or (s0 == "x-")) and (z ~= z0) then
  287.         print("Turtle is not in line with the home point, correcting")
  288.         for _ = 1, math.abs(z - z0) do
  289.             forward()
  290.         end
  291.         turtle.place()
  292.         turtle.turnLeft()
  293.         dropAll()
  294.         comeBack(x, x0)
  295.         return
  296.     end
  297.     if ((s0 == "z+") or (s0 == "z-")) and (x ~= x0) then
  298.         print("Turtle is not in line with the home point, correcting")
  299.         for _ = 1, math.abs(x - x0) do
  300.             forward()
  301.         end
  302.         turtle.place()
  303.         turtle.turnLeft()
  304.         dropAll()
  305.         comeBack(z, z0)
  306.         return
  307.     end
  308.  
  309.     print("Moving the turtle to tell orientation...")
  310.     if not turtle.back() then
  311.         print("Can't move back, must be at the start") -- Can be false positive, but I doubt this situation will ever happen
  312.         return
  313.     end
  314.     local x1, _, z1 = locate()
  315.     forward()
  316.  
  317.     if (s0 == "x+") or (s0 == "x-") then
  318.         if z1 ~= z0 then
  319.             turtle.place()
  320.             turtle.turnLeft()
  321.         end
  322.         dropAll()
  323.         comeBack(x, x0)
  324.         return
  325.     else
  326.         if x1 ~= x0 then
  327.             turtle.place()
  328.             turtle.turnLeft()
  329.         end
  330.         dropAll()
  331.         comeBack(z, z0)
  332.         return
  333.     end
  334. end
  335.  
  336. print("Initializing the turtle...")
  337. init()
  338. restock()
  339. refuel()
  340. print("Initialized!")
  341.  
  342. local loopCount = 0
  343.  
  344. -- Main loop
  345. while true do
  346.     loopCount = loopCount + 1
  347.     print("Starting loop " .. loopCount)
  348.  
  349.     for _ = 1, BLOCKS_BEFORE_TREES do
  350.         forward()
  351.     end
  352.     for _ = 1, TREE_BLOCKS do
  353.         forward()
  354.         checkAndChop()
  355.     end
  356.  
  357.     for _ = 1, TREE_BLOCKS - 1 do
  358.         back()
  359.         checkAndChop()
  360.     end
  361.     for _ = 1, BLOCKS_BEFORE_TREES + 1 do
  362.         back()
  363.     end
  364.  
  365.     restock()
  366.     refuel()
  367. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement