MarcPino

Claude's well maker

Apr 11th, 2025 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.62 KB | None | 0 0
  1. -- Configuration
  2. local width = 3  -- Largeur du puits (X)
  3. local length = 3  -- Longueur du puits (Y)
  4. local fuelThreshold = 100  -- Niveau de carburant minimum avant de refaire le plein
  5.  
  6. -- Variables
  7. local digDepth = 0  -- Profondeur actuelle
  8. local bedrockFound = false
  9. local homeX, homeY, homeZ = 0, 0, 0  -- Coordonnées du point de départ
  10. local currentX, currentY, currentZ = 0, 0, 0  -- Position actuelle de la tortue
  11.  
  12. -- Fonction pour vérifier et refaire le plein de carburant si nécessaire
  13. function checkFuel()
  14.   local currentFuel = turtle.getFuelLevel()
  15.  
  16.   -- Estimer la quantité de carburant nécessaire pour retourner à la surface
  17.   local fuelNeededToReturn = digDepth + 10  -- +10 pour la marge
  18.  
  19.   if currentFuel < fuelThreshold + fuelNeededToReturn then
  20.     print("Niveau de carburant bas (" .. currentFuel .. "). Retour à la surface pour refaire le plein.")
  21.     returnToSurface()
  22.     refuel()
  23.     returnToDiggingSite()
  24.   end
  25. end
  26.  
  27. -- Fonction pour vider l'inventaire dans le coffre à gauche
  28. function dumpInventory()
  29.   print("Vidage de l'inventaire...")
  30.   turtle.turnLeft()
  31.   turtle.select(1)
  32.  
  33.   for i = 1, 16 do
  34.     local item = turtle.getItemDetail(i)
  35.     if item and item.name ~= "minecraft:coal" and item.name ~= "minecraft:charcoal" then
  36.       turtle.select(i)
  37.       turtle.drop()
  38.     end
  39.   end
  40.  
  41.   turtle.turnRight()
  42. end
  43.  
  44. -- Fonction pour refaire le plein depuis le coffre à droite
  45. function refuel()
  46.   print("Ravitaillement en carburant...")
  47.   turtle.turnRight()
  48.  
  49.   -- Chercher l'emplacement d'un slot vide
  50.   local emptySlot = 0
  51.   for i = 1, 16 do
  52.     if turtle.getItemCount(i) == 0 then
  53.       emptySlot = i
  54.       break
  55.     end
  56.   end
  57.  
  58.   if emptySlot > 0 then
  59.     turtle.select(emptySlot)
  60.     turtle.suck(64)  -- Prendre du charbon
  61.    
  62.     -- Refaire le plein
  63.     local fuelNeeded = true
  64.     while fuelNeeded do
  65.       if turtle.getItemCount(emptySlot) > 0 then
  66.         turtle.refuel()
  67.       else
  68.         turtle.suck(64)
  69.         if turtle.getItemCount(emptySlot) == 0 then
  70.           fuelNeeded = false
  71.         end
  72.       end
  73.      
  74.       if turtle.getFuelLevel() > 5000 then
  75.         fuelNeeded = false
  76.       end
  77.     end
  78.   end
  79.  
  80.   turtle.turnLeft()
  81.   print("Niveau de carburant actuel: " .. turtle.getFuelLevel())
  82. end
  83.  
  84. -- Fonction pour creuser une seule couche du puits
  85. function digLayer()
  86.   local forward = true
  87.  
  88.   for w = 1, width do
  89.     for l = 1, length - 1 do
  90.       if not bedrockFound then
  91.         if forward then
  92.           turtle.dig()
  93.           if not turtle.forward() then
  94.             if turtle.detect() then
  95.               -- Vérifions si c'est du bedrock
  96.               success, block = turtle.inspect()
  97.               if success and block.name == "minecraft:bedrock" then
  98.                 bedrockFound = true
  99.                 print("Bedrock détecté à la profondeur " .. digDepth)
  100.                 break
  101.               end
  102.             end
  103.           else
  104.             -- Mise à jour des coordonnées
  105.             if turtle.getDirection() == 0 then  -- Face au sud
  106.               currentX = currentX + 1
  107.             elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  108.               currentY = currentY - 1
  109.             elseif turtle.getDirection() == 2 then  -- Face au nord
  110.               currentX = currentX - 1
  111.             elseif turtle.getDirection() == 3 then  -- Face à l'est
  112.               currentY = currentY + 1
  113.             end
  114.           end
  115.         else
  116.           turtle.dig()
  117.           if not turtle.forward() then
  118.             if turtle.detect() then
  119.               -- Vérifions si c'est du bedrock
  120.               success, block = turtle.inspect()
  121.               if success and block.name == "minecraft:bedrock" then
  122.                 bedrockFound = true
  123.                 print("Bedrock détecté à la profondeur " .. digDepth)
  124.                 break
  125.               end
  126.             end
  127.           else
  128.             -- Mise à jour des coordonnées
  129.             if turtle.getDirection() == 0 then  -- Face au sud
  130.               currentX = currentX + 1
  131.             elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  132.               currentY = currentY - 1
  133.             elseif turtle.getDirection() == 2 then  -- Face au nord
  134.               currentX = currentX - 1
  135.             elseif turtle.getDirection() == 3 then  -- Face à l'est
  136.               currentY = currentY + 1
  137.             end
  138.           end
  139.         end
  140.       end
  141.     end
  142.    
  143.     if bedrockFound then break end
  144.    
  145.     if w < width then
  146.       if forward then
  147.         turtle.turnRight()
  148.         turtle.dig()
  149.         if not turtle.forward() then
  150.           -- Vérifions si c'est du bedrock
  151.           success, block = turtle.inspect()
  152.           if success and block.name == "minecraft:bedrock" then
  153.             bedrockFound = true
  154.             print("Bedrock détecté à la profondeur " .. digDepth)
  155.             break
  156.           end
  157.         else
  158.           -- Mise à jour des coordonnées
  159.           if turtle.getDirection() == 0 then  -- Face au sud
  160.             currentX = currentX + 1
  161.           elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  162.             currentY = currentY - 1
  163.           elseif turtle.getDirection() == 2 then  -- Face au nord
  164.             currentX = currentX - 1
  165.           elseif turtle.getDirection() == 3 then  -- Face à l'est
  166.             currentY = currentY + 1
  167.           end
  168.         end
  169.         turtle.turnRight()
  170.       else
  171.         turtle.turnLeft()
  172.         turtle.dig()
  173.         if not turtle.forward() then
  174.           -- Vérifions si c'est du bedrock
  175.           success, block = turtle.inspect()
  176.           if success and block.name == "minecraft:bedrock" then
  177.             bedrockFound = true
  178.             print("Bedrock détecté à la profondeur " .. digDepth)
  179.             break
  180.           end
  181.         else
  182.           -- Mise à jour des coordonnées
  183.           if turtle.getDirection() == 0 then  -- Face au sud
  184.             currentX = currentX + 1
  185.           elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  186.             currentY = currentY - 1
  187.           elseif turtle.getDirection() == 2 then  -- Face au nord
  188.             currentX = currentX - 1
  189.           elseif turtle.getDirection() == 3 then  -- Face à l'est
  190.             currentY = currentY + 1
  191.           end
  192.         end
  193.         turtle.turnLeft()
  194.       end
  195.      
  196.       forward = not forward
  197.     end
  198.   end
  199.  
  200.   -- Retour à la position initiale de la couche
  201.   returnToLayerStart(forward)
  202. end
  203.  
  204. -- Fonction pour retourner à la position initiale d'une couche
  205. function returnToLayerStart(wasForward)
  206.   if bedrockFound then return end
  207.  
  208.   if wasForward then
  209.     -- Si on était en train d'avancer, on doit faire demi-tour
  210.     turtle.turnRight()
  211.     turtle.turnRight()
  212.    
  213.     -- Parcourir à l'envers
  214.     for l = 1, length - 1 do
  215.       turtle.forward()
  216.       -- Mise à jour des coordonnées
  217.       if turtle.getDirection() == 0 then  -- Face au sud
  218.         currentX = currentX + 1
  219.       elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  220.         currentY = currentY - 1
  221.       elseif turtle.getDirection() == 2 then  -- Face au nord
  222.         currentX = currentX - 1
  223.       elseif turtle.getDirection() == 3 then  -- Face à l'est
  224.         currentY = currentY + 1
  225.       end
  226.     end
  227.    
  228.     -- Maintenant se replacer pour la prochaine couche
  229.     for w = 1, width - 1 do
  230.       turtle.turnLeft()
  231.       turtle.forward()
  232.       -- Mise à jour des coordonnées
  233.       if turtle.getDirection() == 0 then  -- Face au sud
  234.         currentX = currentX + 1
  235.       elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  236.         currentY = currentY - 1
  237.       elseif turtle.getDirection() == 2 then  -- Face au nord
  238.         currentX = currentX - 1
  239.       elseif turtle.getDirection() == 3 then  -- Face à l'est
  240.         currentY = currentY + 1
  241.       end
  242.       turtle.turnRight()
  243.     end
  244.    
  245.     -- Retourner à l'orientation initiale
  246.     turtle.turnRight()
  247.     turtle.turnRight()
  248.   else
  249.     -- Si on reculait déjà, c'est plus simple
  250.     for w = 1, width - 1 do
  251.       turtle.turnRight()
  252.       turtle.forward()
  253.       -- Mise à jour des coordonnées
  254.       if turtle.getDirection() == 0 then  -- Face au sud
  255.         currentX = currentX + 1
  256.       elseif turtle.getDirection() == 1 then  -- Face à l'ouest
  257.         currentY = currentY - 1
  258.       elseif turtle.getDirection() == 2 then  -- Face au nord
  259.         currentX = currentX - 1
  260.       elseif turtle.getDirection() == 3 then  -- Face à l'est
  261.         currentY = currentY + 1
  262.       end
  263.       turtle.turnLeft()
  264.     end
  265.   end
  266. end
  267.  
  268. -- Fonction pour naviguer vers un point spécifique
  269. function navigateTo(targetX, targetY, targetZ)
  270.   -- Ajuster l'altitude d'abord
  271.   while currentZ < targetZ do
  272.     if turtle.up() then
  273.       currentZ = currentZ + 1
  274.     else
  275.       turtle.digUp()
  276.     end
  277.   end
  278.  
  279.   while currentZ > targetZ do
  280.     if turtle.down() then
  281.       currentZ = currentZ - 1
  282.     else
  283.       turtle.digDown()
  284.     end
  285.   end
  286.  
  287.   -- Orienter la tortue vers le sud (x+) comme référence
  288.   -- Supposons que la tortue commence face au sud (x+)
  289.  
  290.   -- Ajuster la position X
  291.   while currentX < targetX do
  292.     turtle.dig()
  293.     if turtle.forward() then
  294.       currentX = currentX + 1
  295.     end
  296.   end
  297.  
  298.   while currentX > targetX do
  299.     turtle.turnRight()
  300.     turtle.turnRight()
  301.     turtle.dig()
  302.     if turtle.forward() then
  303.       currentX = currentX - 1
  304.     end
  305.     turtle.turnRight()
  306.     turtle.turnRight()
  307.   end
  308.  
  309.   -- Ajuster la position Y
  310.   if currentY < targetY then
  311.     turtle.turnLeft()
  312.     while currentY < targetY do
  313.       turtle.dig()
  314.       if turtle.forward() then
  315.         currentY = currentY + 1
  316.       end
  317.     end
  318.     turtle.turnRight()
  319.   else
  320.     turtle.turnRight()
  321.     while currentY > targetY do
  322.       turtle.dig()
  323.       if turtle.forward() then
  324.         currentY = currentY - 1
  325.       end
  326.     end
  327.     turtle.turnLeft()
  328.   end
  329. end
  330.  
  331. -- Fonction pour retourner à la surface
  332. function returnToSurface()
  333.   print("Retour à la surface...")
  334.  
  335.   -- Se diriger vers la position au-dessus du point de départ
  336.   navigateTo(homeX, homeY, homeZ)
  337. end
  338.  
  339. -- Fonction pour aller au site de creusage
  340. function goToDiggingSite()
  341.   print("Déplacement vers le site de creusage...")
  342.  
  343.   -- Se déplacer vers la position de départ du puits [1,0,0]
  344.   navigateTo(1, 0, 0)
  345.  
  346.   -- Descendre d'un bloc pour commencer le puits à [1,0,-1]
  347.   if turtle.down() then
  348.     currentZ = currentZ - 1
  349.   else
  350.     turtle.digDown()
  351.     turtle.down()
  352.     currentZ = currentZ - 1
  353.   end
  354. }
  355.  
  356. -- Fonction pour retourner au site de creusage après ravitaillement
  357. function returnToDiggingSite()
  358.   print("Retour au site de creusage...")
  359.  
  360.   -- Retourner au coin du puits
  361.   navigateTo(1, 0, 0)
  362.  
  363.   -- Descendre jusqu'à la profondeur de creusage actuelle
  364.   for i = 1, digDepth do
  365.     if turtle.down() then
  366.       currentZ = currentZ - 1
  367.     else
  368.       turtle.digDown()
  369.       turtle.down()
  370.       currentZ = currentZ - 1
  371.     end
  372.   end
  373. }
  374.  
  375. -- Programme principal
  376. print("Début du creusage du puits " .. width .. "x" .. length)
  377.  
  378. -- Vérifier le carburant initial
  379. if turtle.getFuelLevel() < 100 then
  380.   print("Carburant insuffisant. Ravitaillement initial...")
  381.   refuel()
  382. end
  383.  
  384. -- Se déplacer vers le site de creusage initial
  385. goToDiggingSite()
  386.  
  387. -- Creuser jusqu'au bedrock
  388. while not bedrockFound do
  389.   -- Vérifier si l'inventaire est presque plein
  390.   local emptySlots = 0
  391.   for i = 1, 16 do
  392.     if turtle.getItemCount(i) == 0 then
  393.       emptySlots = emptySlots + 1
  394.     end
  395.   end
  396.  
  397.   if emptySlots < 3 then
  398.     returnToSurface()
  399.     dumpInventory()
  400.     returnToDiggingSite()
  401.   end
  402.  
  403.   -- Vérifier le carburant
  404.   checkFuel()
  405.  
  406.   -- Creuser vers le bas
  407.   if not turtle.detectDown() or turtle.digDown() then
  408.     local success = turtle.down()
  409.     if success then
  410.       currentZ = currentZ - 1
  411.       digDepth = digDepth + 1
  412.       print("Profondeur actuelle: " .. digDepth)
  413.       digLayer()
  414.     else
  415.       -- Vérifier si c'est du bedrock
  416.       success, block = turtle.inspectDown()
  417.       if success and block.name == "minecraft:bedrock" then
  418.         bedrockFound = true
  419.         print("Bedrock détecté à la profondeur " .. digDepth)
  420.       end
  421.     end
  422.   else
  423.     -- Vérifier si c'est du bedrock
  424.     success, block = turtle.inspectDown()
  425.     if success and block.name == "minecraft:bedrock" then
  426.       bedrockFound = true
  427.       print("Bedrock détecté à la profondeur " .. digDepth)
  428.     end
  429.   end
  430. end
  431.  
  432. -- Retour à la surface et vidage final
  433. returnToSurface()
  434. dumpInventory()
  435.  
  436. print("Creusage terminé! Puits " .. width .. "x" .. length .. " creusé jusqu'au bedrock à la profondeur " .. digDepth)
Tags: cc:tweaked
Add Comment
Please, Sign In to add comment