tommy2805

chopping tree v3

Jun 28th, 2024 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 KB | None | 0 0
  1. -- Soglia di carburante sotto la quale deve rifornirsi
  2. local LOW_FUEL_THRESHOLD = 500
  3.  
  4. -- Funzione per controllare se il blocco davanti è lo stesso tipo di quello nello slot dato
  5. function isBlock(slot)
  6.     local success, data = turtle.inspect()
  7.     local Detail = turtle.getItemDetail(slot)
  8.     if success and Detail and data.name == Detail.name then
  9.         return true
  10.     end
  11.     return false
  12. end
  13.  
  14. function isRightItem(slot,item)
  15.     --test
  16. end
  17.  
  18. function isSapling()
  19.     return isBlock(2)
  20. end
  21.  
  22. function isLog()
  23.     return isBlock(4)
  24. end
  25.  
  26. -- Funzione per controllare se il blocco davanti è acqua
  27. function isWater()
  28.     local success, data = turtle.inspect()
  29.     if success then
  30.         return data.name == "minecraft:water" or data.name == "minecraft:flowing_water"
  31.     end
  32.     return false
  33. end
  34.  
  35. function fillResource(direction, steps, slot, resource)
  36.     print("Filling Up " .. resource .. "...")
  37.     for i=1,3 do
  38.         while not turtle.back() do
  39.             print("turtle is stuck")
  40.             forceBackMove()
  41.             sleep(0.5)
  42.         end
  43.     end
  44.     for i=1,steps do
  45.         if direction == "left" then
  46.             turtle.turnLeft()
  47.         else
  48.             turtle.turnRight()
  49.         end
  50.     end
  51.     turtle.select(slot)
  52.     turtle.suck(63)
  53.     if resource == "Sapling" or resource == "BoneMeal" then
  54.         while turtle.getItemCount(slot) <= 5 do
  55.             print(resource .. "...")
  56.             sleep(15)
  57.             local neededItems = (64-turtle.getItemCount(slot))
  58.             turtle.suck(neededItems)
  59.         end
  60.     elseif resource == "fuel" then
  61.         local maxfuel = turtle.getFuelLimit()
  62.         while turtle.getFuelLevel() < maxfuel do
  63.             print("Fuel: " .. turtle.getFuelLevel() .. "/" .. turtle.getFueleLimit())
  64.             while turtle.getItemCount(slot) == 0 do
  65.                 turtle.suck(64)
  66.                 if turtle.getItemCount(slot) == 0 then
  67.                     sleep(15)
  68.                 end
  69.             end
  70.             turtle.refuel(1)
  71.         end
  72.         turtle.suck(64)
  73.     end
  74.     for i=1,steps do
  75.         if direction == "left" then
  76.             turtle.turnRight()
  77.         else
  78.             turtle.turnLeft()
  79.         end
  80.     end
  81.     for i=1,3 do
  82.         while not turtle.forward() do
  83.             print("turtle is stuck")
  84.             forceForwardMove()
  85.             sleep(0.5)
  86.         end
  87.     end
  88.     print("Filled")
  89. end
  90.  
  91. function useBoneMeal()
  92.     print("Fertilizing...")
  93.     while not isLog() do
  94.         checkFuel()
  95.         if turtle.getItemCount(3) <= 5 then
  96.             fillResource("left",1,3,"BoneMeal")
  97.         else
  98.             turtle.select(3)
  99.             turtle.place()
  100.             sleep(0.5)
  101.         end
  102.     end
  103.     print("Tree has grown!!")
  104. end
  105.  
  106. function dispenserToggler()
  107.     rs.setBundledOutput("bottom", colors.gray)
  108.     sleep(0.5)
  109.     rs.setBundledOutput("bottom", 0)
  110. end
  111.  
  112. function digAndPlant()
  113.     while turtle.detect() do
  114.         turtle.dig()
  115.     end
  116.     turtle.select(2)
  117.     turtle.place()
  118. end
  119.  
  120. function forceBackMove()
  121.     turtle.turnLeft()
  122.     turtle.turnLeft()
  123.     while turtle.detect() do
  124.         turtle.dig()
  125.     end
  126.     turtle.turnRight()
  127.     turtle.turnRight()
  128. end
  129.  
  130. function forceForwardMove()
  131.     while turtle.detect() do
  132.         turtle.dig()
  133.     end
  134. end
  135.  
  136. function plantSapling()
  137.     if turtle.getItemCount(2) <= 5 then
  138.         fillResource("right", 1, 2, "sapling")
  139.     end
  140.    
  141.     --Primo sapling
  142.     while not turtle.forward() do
  143.         forceForwardMove()
  144.     end
  145.     turtle.turnLeft()
  146.     digAndPlant()
  147.     turtle.turnRight()
  148.  
  149.     --Secondo sapling
  150.     while not turtle.forward() do
  151.         forceForwardMove()
  152.     end
  153.     turtle.turnLeft()
  154.     digAndPlant()
  155.     turtle.turnRight()
  156.  
  157.     --Terzo sapling
  158.     while not turtle.back() do
  159.         forceBackMove()
  160.     end
  161.     digAndPlant()
  162.  
  163.     --Quarto sapling
  164.     while not turtle.back() do
  165.         forceBackMove()
  166.     end
  167.     digAndPlant()
  168. end
  169.  
  170. --abbatto l'albero e ripianto
  171. function chopTreeAndReplant()
  172.     dispenserToggler()
  173.     print("Chopping tree.") -- Taglia l'albero
  174.     turtle.dig()
  175.     sleep(7) -- aspetto che l'acqua rimuova le foglie
  176.     dispenserToggler()
  177.     sleep(3) --aspetto che l'acqua vada via
  178.     print("Planting sapling.") -- Ripianto il sapling
  179.     plantSapling()
  180. end
  181.  
  182. -- controlla il livello di carburante
  183. function checkFuel()
  184.     if turtle.getFuelLevel() < LOW_FUEL_THRESHOLD  then
  185.         if turtle.getItemCount(1) <= 5 then
  186.             fillResource("left", 2, 1, "fuel")
  187.         else
  188.             turtle.select(1)
  189.             turtle.refuel()
  190.         end
  191.         print("Refueled")
  192.     end
  193. end
  194.  
  195. while true do
  196.     checkFuel()
  197.     if colors.test(rs.getBundledInput("bottom"), colors.green) then
  198.         print("Logs storage full.")
  199.         sleep(50)
  200.     else
  201.         if isWater() then
  202.             print("Water detected...")
  203.             dispenserToggler()
  204.             sleep(5)
  205.         end
  206.         if isLog() then
  207.             print("Chopping tree.")
  208.             chopTreeAndReplant()
  209.         elseif not isSapling() then
  210.             print("Planting sapling...")
  211.             plantSapling()
  212.         elseif isSapling() then
  213.             useBoneMeal()
  214.             print("Waiting....")
  215.         end
  216.     end
  217.     sleep(3)
  218. end
Advertisement
Add Comment
Please, Sign In to add comment