Advertisement
Vodka51200

minage A* test

Jun 18th, 2025
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | Gaming | 0 0
  1. -- === CONFIGURATION ===
  2. local blacklist = {
  3.   ["minecraft:lava"] = true,
  4.   ["minecraft:bedrock"] = true,
  5. }
  6.  
  7. -- === SAISIE UTILISATEUR ===
  8. print("Largeur (X) ?")
  9. local sizeX = tonumber(read())
  10. print("Hauteur (Y) ?")
  11. local sizeY = tonumber(read())
  12. print("Profondeur (Z) ?")
  13. local sizeZ = tonumber(read())
  14.  
  15. -- === ETAT DE LA TORTUE ===
  16. local position = {x = 0, y = 0, z = 0}
  17. local facing = 0 -- 0=nord, 1=est, 2=sud, 3=ouest
  18.  
  19. -- === UTILITAIRES DE DÉPLACEMENT ===
  20. function faceTo(dir)
  21.   while facing ~= dir do
  22.     turtle.turnRight()
  23.     facing = (facing + 1) % 4
  24.   end
  25. end
  26.  
  27. function moveForward()
  28.   if turtle.forward() then
  29.     if facing == 0 then position.z = position.z + 1
  30.     elseif facing == 1 then position.x = position.x + 1
  31.     elseif facing == 2 then position.z = position.z - 1
  32.     elseif facing == 3 then position.x = position.x - 1 end
  33.     return true
  34.   end
  35.   return false
  36. end
  37.  
  38. function moveUp()
  39.   if turtle.up() then
  40.     position.y = position.y + 1
  41.     return true
  42.   end
  43.   return false
  44. end
  45.  
  46. function moveDown()
  47.   if turtle.down() then
  48.     position.y = position.y - 1
  49.     return true
  50.   end
  51.   return false
  52. end
  53.  
  54. -- === MINAGE SÉCURISÉ ===
  55. function isBlacklisted(name)
  56.   return blacklist[name] == true
  57. end
  58.  
  59. function safeDig(inspectFunc, digFunc)
  60.   local success, data = inspectFunc()
  61.   if success and isBlacklisted(data.name) then return false end
  62.   if success then digFunc() end
  63.   return true
  64. end
  65.  
  66. function mineForward()
  67.   if not safeDig(turtle.inspect, turtle.dig) then return false end
  68.   return moveForward()
  69. end
  70.  
  71. function mineUp()
  72.   if not safeDig(turtle.inspectUp, turtle.digUp) then return false end
  73.   return moveUp()
  74. end
  75.  
  76. function mineDown()
  77.   if not safeDig(turtle.inspectDown, turtle.digDown) then return false end
  78.   return moveDown()
  79. end
  80.  
  81. -- === GÉNÉRATION DES COORDONNÉES À MINER ===
  82. function generateMiningTargets(sx, sy, sz)
  83.   local targets = {}
  84.   for y = 0, sy - 1 do
  85.     for z = 0, sz - 1 do
  86.       for x = 0, sx - 1 do
  87.         table.insert(targets, {x = x, y = y, z = z})
  88.       end
  89.     end
  90.   end
  91.   return targets
  92. end
  93.  
  94. -- === A* ===
  95. function makeNode(x, y, z)
  96.   return {x = x, y = y, z = z, g = 0, h = 0, f = 0, parent = nil}
  97. end
  98.  
  99. function heuristic(a, b)
  100.   return math.abs(a.x - b.x) + math.abs(a.y - b.y) + math.abs(a.z - b.z)
  101. end
  102.  
  103. function positionKey(pos)
  104.   return pos.x.."|"..pos.y.."|"..pos.z
  105. end
  106.  
  107. function isBlocked(x, y, z)
  108.   turtle.select(1)
  109.   local px, py, pz = position.x, position.y, position.z
  110.   local dx, dy, dz = x - px, y - py, z - pz
  111.  
  112.   if dx == 0 and dy == 0 and dz == 1 then
  113.     local ok, data = turtle.inspect()
  114.     return ok and isBlacklisted(data.name)
  115.   elseif dx == 0 and dy == 1 and dz == 0 then
  116.     local ok, data = turtle.inspectUp()
  117.     return ok and isBlacklisted(data.name)
  118.   elseif dx == 0 and dy == -1 and dz == 0 then
  119.     local ok, data = turtle.inspectDown()
  120.     return ok and isBlacklisted(data.name)
  121.   else
  122.     return false -- inconnu
  123.   end
  124. end
  125.  
  126. function astar(start, goal)
  127.   local openSet = {[positionKey(start)] = makeNode(start.x, start.y, start.z)}
  128.   local openList = {openSet[positionKey(start)]}
  129.   local closedSet = {}
  130.  
  131.   while #openList > 0 do
  132.     local currentIndex = 1
  133.     for i = 2, #openList do
  134.       if openList[i].f < openList[currentIndex].f then
  135.         currentIndex = i
  136.       end
  137.     end
  138.     local current = table.remove(openList, currentIndex)
  139.     openSet[positionKey(current)] = nil
  140.  
  141.     if current.x == goal.x and current.y == goal.y and current.z == goal.z then
  142.       local path = {}
  143.       while current do
  144.         table.insert(path, 1, {x = current.x, y = current.y, z = current.z})
  145.         current = current.parent
  146.       end
  147.       return path
  148.     end
  149.  
  150.     closedSet[positionKey(current)] = true
  151.  
  152.     for _, dir in ipairs({
  153.       {1, 0, 0}, {-1, 0, 0},
  154.       {0, 1, 0}, {0, -1, 0},
  155.       {0, 0, 1}, {0, 0, -1}
  156.     }) do
  157.       local nx, ny, nz = current.x + dir[1], current.y + dir[2], current.z + dir[3]
  158.       local key = nx.."|"..ny.."|"..nz
  159.  
  160.       if not closedSet[key] and not isBlocked(nx, ny, nz) then
  161.         local neighbor = makeNode(nx, ny, nz)
  162.         neighbor.g = current.g + 1
  163.         neighbor.h = heuristic(neighbor, goal)
  164.         neighbor.f = neighbor.g + neighbor.h
  165.         neighbor.parent = current
  166.  
  167.         if not openSet[key] then
  168.           openSet[key] = neighbor
  169.           table.insert(openList, neighbor)
  170.         end
  171.       end
  172.     end
  173.   end
  174.   return nil
  175. end
  176.  
  177. -- === ALLER VERS UNE COORDONNÉE ===
  178. function goTo(target)
  179.   local dx = target.x - position.x
  180.   local dy = target.y - position.y
  181.   local dz = target.z - position.z
  182.  
  183.   if dy > 0 then for i = 1, dy do mineUp() end
  184.   elseif dy < 0 then for i = 1, -dy do mineDown() end end
  185.  
  186.   if dx ~= 0 then
  187.     faceTo((dx > 0) and 1 or 3)
  188.     for i = 1, math.abs(dx) do mineForward() end
  189.   end
  190.  
  191.   if dz ~= 0 then
  192.     faceTo((dz > 0) and 0 or 2)
  193.     for i = 1, math.abs(dz) do mineForward() end
  194.   end
  195. end
  196.  
  197. -- === PROGRAMME PRINCIPAL ===
  198. local miningTargets = generateMiningTargets(sizeX, sizeY, sizeZ)
  199.  
  200. for i, target in ipairs(miningTargets) do
  201.   print(string.format("[%d/%d] Calcul vers (%d,%d,%d)", i, #miningTargets, target.x, target.y, target.z))
  202.   local path = astar(position, target)
  203.   if path then
  204.     for j = 2, #path do
  205.       goTo(path[j])
  206.     end
  207.   else
  208.     print("⚠️ Chemin bloqué vers ", target.x, target.y, target.z)
  209.   end
  210. end
  211.  
  212. -- === RETOUR AU DÉPART ===
  213. print("Retour au point de départ...")
  214. local returnPath = astar(position, {x = 0, y = 0, z = 0})
  215. if returnPath then
  216.   for i = 2, #returnPath do
  217.     goTo(returnPath[i])
  218.   end
  219.   print("✅ Retour terminé.")
  220. else
  221.   print("❌ Impossible de retourner au point de départ.")
  222. end
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement