louwding

Tree cutter Turtle

Apr 10th, 2023 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | Gaming | 0 0
  1. gps = require("turtle_gps")
  2.  
  3.  
  4. Drop_in_world = false
  5.  
  6. function check_inventory()
  7.  
  8.     -- check logs slot
  9.     current_slot = 1
  10.     over = turtle.getItemCount(current_slot)
  11.     if over > 1 then
  12.         turtle.select(current_slot)
  13.         -- refuel
  14.         while turtle.getFuelLevel() < 200 and over > 1 do
  15.             turtle.refuel(1)
  16.             over = over - 1
  17.         end
  18.  
  19.         if over > 1 then
  20.             if Drop_in_world then
  21.                 -- keeps 1 log to compare
  22.                 turtle.drop(over - 1)
  23.             else
  24.                 while turtle.dropDown(over - 1) == false do
  25.                     print("Inventory full. Press ENTER to continue")
  26.                     read()
  27.                 end
  28.             end
  29.         end
  30.     end
  31.  
  32.     -- check saplings slot
  33.     current_slot = 2
  34.     over = turtle.getItemCount(current_slot)
  35.     if over < 5 then
  36.         turtle.select(current_slot)
  37.         turtle.suckDown(5 - over)
  38.     end
  39.  
  40.     -- drop other slots' content
  41.     for current_slot = 3, 16, 1 do
  42.         over  = turtle.getItemCount(current_slot)
  43.         if over > 0 then
  44.             turtle.select(current_slot)
  45.  
  46.             -- refuel
  47.             while turtle.getFuelLevel() < 200 and over > 0 do
  48.                 turtle.refuel(1)
  49.                 over = over - 1
  50.             end
  51.            
  52.             if Drop_in_world then
  53.                 turtle.drop()
  54.             else
  55.                 while turtle.dropDown() == false do
  56.                     print("Inventory full. Press ENTER to continue")
  57.                     read()
  58.                 end
  59.             end
  60.         end
  61.     end
  62.    
  63. end
  64.  
  65.  
  66. function check_layer()
  67.     did_cut = false
  68.     turtle.select(1)
  69.  
  70.     turnToDirection(Directions.Forward)
  71.     if turtle.compare() then
  72.         turtle.dig()
  73.         did_cut = true
  74.     end
  75.  
  76.     turnToDirection(Directions.Right)
  77.     if turtle.compare() then
  78.         turtle.dig()
  79.         did_cut = true
  80.     end
  81.  
  82.     turnToDirection(Directions.Backwards)
  83.     if turtle.compare() then
  84.         turtle.dig()
  85.         did_cut = true
  86.     end
  87.    
  88.     turnToDirection(Directions.Left)
  89.     if turtle.compare() then
  90.         turtle.dig()
  91.         did_cut = true
  92.     end
  93.  
  94.  
  95.     turnToDirection(Directions.Forward)
  96.     return did_cut
  97. end
  98.  
  99. function plant()
  100.     -- select the sapling slot
  101.     turtle.select(2)
  102.     planted = 0
  103.  
  104.     if turtle.getItemCount(2) >= 5 then
  105.         turnToDirection(Directions.Forward)
  106.         if turtle.detect() == false then
  107.             turtle.place()
  108.             planted = planted + 1
  109.         end
  110.         turnToDirection(Directions.Right)
  111.         if turtle.detect() == false then
  112.             turtle.place()
  113.             planted = planted + 1
  114.         end
  115.         turnToDirection(Directions.Backwards)
  116.         if turtle.detect() == false then
  117.             turtle.place()
  118.             planted = planted + 1
  119.         end
  120.         turnToDirection(Directions.Left)
  121.         if turtle.detect() == false then
  122.             turtle.place()
  123.             planted = planted + 1
  124.         end
  125.     else
  126.         print("Out of saplings")
  127.     end
  128.  
  129.     return planted
  130. end
  131.  
  132.  
  133. -------------------------------
  134. function main()
  135.     Auto_refuel = false
  136.  
  137.     print("Please place 1 log in slot 1 and 5 saplings in slot 2 (less saplings can work)")
  138.     print("_________________________________")
  139.     print("Place a chest underneath the turtle to replenish saplings")
  140.     print("_________________________________")
  141.     print("Drop items in world [Y/n]?  (when using [vacuum] hoppers)")
  142.     if string.lower(read()) == 'y' then
  143.         Drop_in_world = true
  144.         term.clear()
  145.         term.setCursorPos(1, 1)
  146.         print("Turtle will drop items infront of it")
  147.     else
  148.         term.clear()
  149.         term.setCursorPos(1, 1)
  150.         print("Turtle will drop items in the inventory below it")
  151.     end
  152.  
  153.     -- refuel
  154.     turtle.select(16)
  155.     while turtle.getFuelLevel() < 200 do
  156.         print("Not enough fuel: "..turtle.getFuelLevel().." / 200")
  157.         print("place fuel in slot 16 and Press ENTER to continue...")
  158.         read()
  159.         turtle.refuel()
  160.     end
  161.  
  162.     print("_________________________________")
  163.     print("The turtle  will use logs to refuel during operation")
  164.     print("Press ENTER to start logging")
  165.     read()
  166.  
  167.     term.clear()
  168.     term.setCursorPos(1, 1)
  169.  
  170.    
  171.     read_position()
  172.  
  173.     print("Turtle is now logging trees...")
  174.     if plant() > 0 then
  175.         sleep(10)
  176.     end
  177.  
  178.  
  179.     while true do
  180.         if check_layer() then
  181.             if turtle.detectUp() then
  182.                 turtle.digUp()
  183.             end
  184.             moveTo(0,0,Local_z + 1, 'xyz')
  185.         else  
  186.             if Local_z > 0 then
  187.                 moveTo(0,0,0, 'xyz')
  188.                 check_inventory()
  189.                 if plant() > 0 then
  190.                     sleep(10)
  191.                 end
  192.             else
  193.                 sleep(10)
  194.             end
  195.         end
  196.     end
  197.  
  198. end
  199.  
  200.  
  201. main()
Add Comment
Please, Sign In to add comment