Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- === CONFIGURATION ===
- local blacklist = {
- ["minecraft:lava"] = true,
- ["minecraft:bedrock"] = true,
- }
- -- === SAISIE UTILISATEUR ===
- print("Largeur (X) ?")
- local sizeX = tonumber(read())
- print("Hauteur (Y) ?")
- local sizeY = tonumber(read())
- print("Profondeur (Z) ?")
- local sizeZ = tonumber(read())
- -- === ETAT DE LA TORTUE ===
- local position = {x = 0, y = 0, z = 0}
- local facing = 0 -- 0=nord, 1=est, 2=sud, 3=ouest
- -- === UTILITAIRES DE DÉPLACEMENT ===
- function faceTo(dir)
- while facing ~= dir do
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- end
- function moveForward()
- if turtle.forward() then
- if facing == 0 then position.z = position.z + 1
- elseif facing == 1 then position.x = position.x + 1
- elseif facing == 2 then position.z = position.z - 1
- elseif facing == 3 then position.x = position.x - 1 end
- return true
- end
- return false
- end
- function moveUp()
- if turtle.up() then
- position.y = position.y + 1
- return true
- end
- return false
- end
- function moveDown()
- if turtle.down() then
- position.y = position.y - 1
- return true
- end
- return false
- end
- -- === MINAGE SÉCURISÉ ===
- function isBlacklisted(name)
- return blacklist[name] == true
- end
- function safeDig(inspectFunc, digFunc)
- local success, data = inspectFunc()
- if success and isBlacklisted(data.name) then return false end
- if success then digFunc() end
- return true
- end
- function mineForward()
- if not safeDig(turtle.inspect, turtle.dig) then return false end
- return moveForward()
- end
- function mineUp()
- if not safeDig(turtle.inspectUp, turtle.digUp) then return false end
- return moveUp()
- end
- function mineDown()
- if not safeDig(turtle.inspectDown, turtle.digDown) then return false end
- return moveDown()
- end
- -- === GÉNÉRATION DES COORDONNÉES À MINER ===
- function generateMiningTargets(sx, sy, sz)
- local targets = {}
- for y = 0, sy - 1 do
- for z = 0, sz - 1 do
- for x = 0, sx - 1 do
- table.insert(targets, {x = x, y = y, z = z})
- end
- end
- end
- return targets
- end
- -- === A* ===
- function makeNode(x, y, z)
- return {x = x, y = y, z = z, g = 0, h = 0, f = 0, parent = nil}
- end
- function heuristic(a, b)
- return math.abs(a.x - b.x) + math.abs(a.y - b.y) + math.abs(a.z - b.z)
- end
- function positionKey(pos)
- return pos.x.."|"..pos.y.."|"..pos.z
- end
- function isBlocked(x, y, z)
- turtle.select(1)
- local px, py, pz = position.x, position.y, position.z
- local dx, dy, dz = x - px, y - py, z - pz
- if dx == 0 and dy == 0 and dz == 1 then
- local ok, data = turtle.inspect()
- return ok and isBlacklisted(data.name)
- elseif dx == 0 and dy == 1 and dz == 0 then
- local ok, data = turtle.inspectUp()
- return ok and isBlacklisted(data.name)
- elseif dx == 0 and dy == -1 and dz == 0 then
- local ok, data = turtle.inspectDown()
- return ok and isBlacklisted(data.name)
- else
- return false -- inconnu
- end
- end
- function astar(start, goal)
- local openSet = {[positionKey(start)] = makeNode(start.x, start.y, start.z)}
- local openList = {openSet[positionKey(start)]}
- local closedSet = {}
- while #openList > 0 do
- local currentIndex = 1
- for i = 2, #openList do
- if openList[i].f < openList[currentIndex].f then
- currentIndex = i
- end
- end
- local current = table.remove(openList, currentIndex)
- openSet[positionKey(current)] = nil
- if current.x == goal.x and current.y == goal.y and current.z == goal.z then
- local path = {}
- while current do
- table.insert(path, 1, {x = current.x, y = current.y, z = current.z})
- current = current.parent
- end
- return path
- end
- closedSet[positionKey(current)] = true
- for _, dir in ipairs({
- {1, 0, 0}, {-1, 0, 0},
- {0, 1, 0}, {0, -1, 0},
- {0, 0, 1}, {0, 0, -1}
- }) do
- local nx, ny, nz = current.x + dir[1], current.y + dir[2], current.z + dir[3]
- local key = nx.."|"..ny.."|"..nz
- if not closedSet[key] and not isBlocked(nx, ny, nz) then
- local neighbor = makeNode(nx, ny, nz)
- neighbor.g = current.g + 1
- neighbor.h = heuristic(neighbor, goal)
- neighbor.f = neighbor.g + neighbor.h
- neighbor.parent = current
- if not openSet[key] then
- openSet[key] = neighbor
- table.insert(openList, neighbor)
- end
- end
- end
- end
- return nil
- end
- -- === ALLER VERS UNE COORDONNÉE ===
- function goTo(target)
- local dx = target.x - position.x
- local dy = target.y - position.y
- local dz = target.z - position.z
- if dy > 0 then for i = 1, dy do mineUp() end
- elseif dy < 0 then for i = 1, -dy do mineDown() end end
- if dx ~= 0 then
- faceTo((dx > 0) and 1 or 3)
- for i = 1, math.abs(dx) do mineForward() end
- end
- if dz ~= 0 then
- faceTo((dz > 0) and 0 or 2)
- for i = 1, math.abs(dz) do mineForward() end
- end
- end
- -- === PROGRAMME PRINCIPAL ===
- local miningTargets = generateMiningTargets(sizeX, sizeY, sizeZ)
- for i, target in ipairs(miningTargets) do
- print(string.format("[%d/%d] Calcul vers (%d,%d,%d)", i, #miningTargets, target.x, target.y, target.z))
- local path = astar(position, target)
- if path then
- for j = 2, #path do
- goTo(path[j])
- end
- else
- print("⚠️ Chemin bloqué vers ", target.x, target.y, target.z)
- end
- end
- -- === RETOUR AU DÉPART ===
- print("Retour au point de départ...")
- local returnPath = astar(position, {x = 0, y = 0, z = 0})
- if returnPath then
- for i = 2, #returnPath do
- goTo(returnPath[i])
- end
- print("✅ Retour terminé.")
- else
- print("❌ Impossible de retourner au point de départ.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement