Octagonal_Square

farmTrees

Jan 29th, 2022 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. os.loadAPI("inv")
  2. os.loadAPI("t")
  3.  
  4. local LOG = "log"
  5. local SAP = "sapling"
  6. local COAL = "coal"
  7. local CHAR = "char"
  8.  
  9. local x = 0
  10. local y = 0
  11.  
  12. function chestyStuff()
  13.     -- We can't see what's on the chest
  14.     -- so we just grab everything
  15.     for i=1, 16 do
  16.         turtle.select(i)
  17.         data = turtle.getItemDetail()
  18.         if data ~= nil then
  19.             if string.find(data.name, "log") then
  20.                 turtle.drop()
  21.             end
  22.         end
  23.     end
  24.     turtle.turnRight()
  25.     if inv.getItemCount(COAL) < 64 then
  26.         while turtle.suck() do end
  27.         inv.stackItems()
  28.         -- and we drop back all the logs
  29.         while inv.selectItem("log") do
  30.             turtle.drop()
  31.         end
  32.         -- Drop all but one stack of coal
  33.         while inv.getItemCount(COAL) > 128 do
  34.             inv.selectItem(COAL)
  35.             turtle.drop()
  36.         end
  37.     end
  38.     inv.stackItems()
  39.     if inv.getItemCount(SAP) < 64 then
  40.         turtle.turnLeft()
  41.         turtle.turnLeft()
  42.         while turtle.suck() do end
  43.         while inv.getItemCount(SAP) > 128 do
  44.             inv.selectItem(SAP)
  45.             turtle.drop()
  46.         end
  47.         for i=1, 16 do
  48.             turtle.select(i)
  49.             data = turtle.getItemDetail()
  50.             if data ~= nil then
  51.                 if not string.find(data.name, COAL) and not string.find(data.name, CHAR) and not string.find(data.name, SAP) then
  52.                     turtle.drop()
  53.                 end
  54.             end
  55.         end
  56.         turtle.turnLeft()
  57.         turtle.turnLeft()
  58.     end
  59.     for i=1, 16 do
  60.         turtle.select(i)
  61.         data = turtle.getItemDetail()
  62.         if data ~= nil then
  63.             if not string.find(data.name, COAL) and not string.find(data.name, CHAR) and not string.find(data.name, SAP) then
  64.                 turtle.drop()
  65.             end
  66.         end
  67.     end
  68.     inv.stackItems()
  69. end
  70.  
  71. function refuelIfNeeded()
  72.     local fuelLevel = turtle.getFuelLevel()
  73.     if fuelLevel < 70 then
  74.         print("Fuel level is " .. fuelLevel .. ", refueling...")
  75.         if inv.selectItem(COAL) then
  76.             turtle.refuel(1)
  77.             print("Refueled! new level is " .. turtle.getFuelLevel())
  78.             return true
  79.         end
  80.         printError("No fuel in inventory!")
  81.     end
  82.     return false
  83. end
  84.  
  85. function placeSapling()
  86.     for i=1, 16 do
  87.         turtle.select(i)
  88.         local data = turtle.getItemDetail()
  89.         if (data and string.find(data.name, "sapling")) then
  90.             turtle.place()
  91.             break
  92.         end
  93.     end
  94.     while turtle.suck() do end
  95. end
  96.  
  97. function chop()
  98.     turtle.dig()
  99.     t.fw()
  100.     local success = true
  101.     local data
  102.     local count = 0
  103.     while success do
  104.         success, data = turtle.inspectUp()
  105.         if success then
  106.             if string.find(data.name, "log") then
  107.                 t.digUp()
  108.                 t.up()
  109.                 count = count + 1
  110.             else
  111.                 success = false
  112.             end
  113.         end
  114.     end
  115.     t.down(count)
  116.     turtle.turnLeft()
  117.     turtle.turnLeft()
  118.     t.fw()
  119.     turtle.turnLeft()
  120.     turtle.turnLeft()
  121.     placeSapling()
  122. end
  123.  
  124. function columns(colCount)
  125.     turtle.turnRight()
  126.     local success, data = turtle.inspect()
  127.     while turtle.suck() do end
  128.     if success then
  129.         if string.find(data.name, "log") then
  130.             chop()
  131.         end
  132.     else
  133.         placeSapling()
  134.     end
  135.     turtle.turnLeft()
  136.     for i=1, 4 do
  137.         t.fw()
  138.     end
  139.     turtle.turnRight()
  140.     t.fw()
  141.     turtle.turnLeft()
  142.     t.fw()
  143.     for corners=0, 2 do
  144.         refuelIfNeeded()
  145.         turtle.turnLeft()
  146.         success, data = turtle.inspect()
  147.         while turtle.suck() do end
  148.         if success then
  149.             if string.find(data.name, "log") then
  150.                 chop()
  151.             end
  152.         else
  153.             placeSapling()
  154.         end
  155.         turtle.turnRight()
  156.        
  157.         for i=1, 5 do
  158.             t.fw()
  159.         end
  160.         success, data = turtle.inspect()
  161.         while turtle.suck() do end
  162.         if success then
  163.             if string.find(data.name, "log") then
  164.                 chop()
  165.             end
  166.         else
  167.             placeSapling()
  168.         end
  169.         turtle.turnRight()
  170.         t.fw()
  171.         turtle.turnLeft()
  172.         t.fw()
  173.         turtle.turnRight()
  174.         for i=1, 4 do
  175.             t.fw()
  176.         end
  177.     end
  178.     refuelIfNeeded()
  179.     turtle.turnLeft()
  180.     success, data = turtle.inspect()
  181.     while turtle.suck() do end
  182.     if success then
  183.         if string.find(data.name, "log") then
  184.             chop()
  185.         end
  186.     else
  187.         placeSapling()
  188.     end
  189.     turtle.turnRight()
  190.     turtle.turnRight()
  191.    
  192.     for i=1, 3 do
  193.         t.fw()
  194.     end
  195.  
  196.     turtle.turnRight()
  197.     t.fw()
  198.     success, data = turtle.inspect()
  199.     while turtle.suck() do end
  200.     if success then
  201.         if string.find(data.name, LOG) then
  202.             chop()
  203.         end
  204.     else
  205.         placeSapling()
  206.     end
  207.     turtle.turnLeft()
  208.    
  209.     for i=1, 5 do
  210.         t.fw()
  211.     end
  212.  
  213.     for inner=0, 2 do
  214.         refuelIfNeeded()
  215.         turtle.turnRight()
  216.         success, data = turtle.inspect()
  217.         while turtle.suck() do end
  218.         if success then
  219.             if string.find(data.name, LOG) then
  220.                 chop()
  221.             end
  222.         else
  223.             placeSapling()
  224.         end
  225.         if inner < 2 then
  226.             turtle.turnRight()
  227.             t.fw()
  228.             turtle.turnRight()
  229.            
  230.             for i=1, 4 do
  231.                 t.fw()
  232.             end
  233.         end
  234.     end
  235.     turtle.turnLeft()
  236. end
  237.  
  238. function goToChest()
  239.     refuelIfNeeded()
  240.     for i=1, 5 do
  241.         t.fw()
  242.     end
  243.     turtle.turnRight()
  244.     for i=1, 5 do
  245.         t.fw()
  246.     end
  247.     turtle.turnLeft()
  248.    
  249.     for i=1, 3 do
  250.         t.fw()
  251.     end
  252.    
  253.     for i=1, 2 do
  254.         t.down()
  255.     end
  256.    
  257.     t.back()
  258.    
  259.     t.down()
  260. end
  261.  
  262. function returnToStart()
  263.     refuelIfNeeded()
  264.     turtle.turnRight()
  265.     t.up()
  266.     t.back()
  267.     t.up(2)
  268.     t.fw(5)
  269. end
  270.  
  271. while true do
  272.     refuelIfNeeded()
  273.    
  274.     columns(16)
  275.    
  276.     goToChest()
  277.    
  278.     chestyStuff()
  279.    
  280.     returnToStart()
  281.    
  282.     sleep(120)
  283. end
Advertisement
Add Comment
Please, Sign In to add comment