Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuración
- local ENV_SIDE = "left"
- local LIGHT_THRESHOLD = 5
- local FUEL_MINIMUM = 400
- local recorrido = {} -- Para registrar el camino recorrido
- -- Variables de visualización
- local posicion = {x = 0, y = 0, z = 0}
- local direccion = 0 -- 0: norte, 1: este, 2: sur, 3: oeste
- local TGiro = "right" -- Dirección inicial del zigzag
- -- Movimiento con registro para poder volver
- local function avanzarRegistrando()
- if turtle.forward() then
- table.insert(recorrido, "back")
- if direccion == 0 then posicion.z = posicion.z - 1
- elseif direccion == 1 then posicion.x = posicion.x + 1
- elseif direccion == 2 then posicion.z = posicion.z + 1
- elseif direccion == 3 then posicion.x = posicion.x - 1 end
- return true
- end
- return false
- end
- local function subirRegistrando()
- if turtle.up() then
- table.insert(recorrido, "down")
- posicion.y = posicion.y + 1
- return true
- end
- return false
- end
- local function bajarRegistrando()
- if turtle.down() then
- table.insert(recorrido, "up")
- posicion.y = posicion.y - 1
- return true
- end
- return false
- end
- local function girarDerecha()
- turtle.turnRight()
- direccion = (direccion + 1) % 4
- table.insert(recorrido, "turnLeft")
- end
- local function girarIzquierda()
- turtle.turnLeft()
- direccion = (direccion - 1) % 4
- if direccion < 0 then direccion = direccion + 4 end
- table.insert(recorrido, "turnRight")
- end
- -- Volver al inicio usando el recorrido almacenado
- local function volverAlInicio()
- print("Volviendo al inicio...")
- for i = #recorrido, 1, -1 do
- local accion = recorrido[i]
- if accion == "back" then
- turtle.back()
- elseif accion == "up" then
- turtle.up()
- elseif accion == "down" then
- turtle.down()
- elseif accion == "turnLeft" then
- turtle.turnLeft()
- elseif accion == "turnRight" then
- turtle.turnRight()
- end
- sleep(0.1)
- end
- print("Regresó al inicio.")
- end
- -- Repostaje si es necesario
- local function refuelIfNeeded()
- if turtle.getFuelLevel() == "unlimited" then return end
- if turtle.getFuelLevel() < FUEL_MINIMUM then
- print("Combustible bajo. Intentando repostar desde el slot 16...")
- turtle.select(16)
- if turtle.refuel() then
- print("Repostaje exitoso. Nivel actual:", turtle.getFuelLevel())
- else
- print("No se pudo repostar. Sin combustible.")
- end
- end
- end
- -- Obtener nivel de luz en el bloque actual
- local function isDarkHere()
- local light = peripheral.call(ENV_SIDE, "getBlockLightLevel")
- print("Nivel de luz:", light)
- return light < LIGHT_THRESHOLD
- end
- -- Colocar una antorcha en el bloque actual
- local function placeTorchHere()
- for i = 1, 15 do
- local item = turtle.getItemDetail(i)
- if item and item.name == "minecraft:torch" then
- turtle.select(i)
- if subirRegistrando() then
- if turtle.placeDown() then
- print("Antorcha colocada.")
- bajarRegistrando()
- return true
- else
- print("No se pudo colocar la antorcha.")
- bajarRegistrando()
- return false
- end
- else
- print("No se pudo subir para colocar la antorcha.")
- return false
- end
- end
- end
- print("No hay antorchas disponibles.")
- return false
- end
- -- Intentar subir un escalón si hay obstáculo
- local function subirescalon()
- if turtle.detect() then
- if not subirRegistrando() then
- return false
- end
- end
- return true
- end
- -- Cambio de carril con zigzag
- local function cambiocarril()
- print("Cambiando de carril...")
- while not turtle.detectDown() and bajarRegistrando() do end
- if TGiro == "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- local pasosExitosos = 0
- for paso = 1, 4 do
- if subirescalon() and avanzarRegistrando() then
- pasosExitosos = pasosExitosos + 1
- while not turtle.detectDown() and bajarRegistrando() do end
- else
- while not turtle.detectDown() and bajarRegistrando() do end
- local ret = true
- local retrocedi = false
- local retrocedo = false
- if TGiro == "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- while not retrocedo do
- if subirescalon() and avanzarRegistrando() and ret then
- if not retrocedi then
- if TGiro ~= "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- retrocedi = true
- retrocedo = true
- end
- else
- if TGiro ~= "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- turtle.back()
- table.insert(recorrido, "turnLeft")
- table.insert(recorrido, "turnLeft")
- table.insert(recorrido, "back")
- table.insert(recorrido, "turnLeft")
- table.insert(recorrido, "turnLeft")
- pasosExitosos = pasosExitosos - 1
- retrocedo = true
- ret = false
- end
- end
- end
- end
- -- Cambiar dirección del siguiente zigzag
- TGiro = (TGiro == "left") and "right" or "left"
- if TGiro == "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- if turtle.detect() then
- if TGiro == "left" then
- girarIzquierda()
- else
- girarDerecha()
- end
- print("Obstáculo en nuevo carril. Cambiando dirección.")
- end
- return true
- end
- -- Mostrar estado actual
- local function printEstado()
- local dirStr = ({[0] = "Norte", [1] = "Este", [2] = "Sur", [3] = "Oeste"})[direccion]
- print(string.format("[POS] X:%d Y:%d Z:%d | Dir: %s", posicion.x, posicion.y, posicion.z, dirStr))
- end
- -- Exploración principal
- local function explore()
- while true do
- refuelIfNeeded()
- if isDarkHere() and turtle.detectDown() then
- placeTorchHere()
- end
- if turtle.detect() then
- print("Obstáculo delante. Intentando subir...")
- local step = 0
- local toping = false
- while step < 3 do
- if subirRegistrando() then
- step = step + 1
- if avanzarRegistrando() then
- toping = true
- while not turtle.detectDown() and bajarRegistrando() do end
- break
- end
- else
- break
- end
- end
- if not toping then
- if not cambiocarril() then
- print("Fin del camino. No se puede avanzar.")
- volverAlInicio()
- return
- end
- end
- else
- if avanzarRegistrando() then
- while not turtle.detectDown() and bajarRegistrando() do end
- else
- print("No se pudo avanzar ni rodear.")
- if not cambiocarril() then
- volverAlInicio()
- return
- end
- end
- end
- printEstado()
- sleep(0.2)
- end
- end
- -- Verificar environmentDetector
- if not peripheral.isPresent(ENV_SIDE) or peripheral.getType(ENV_SIDE) ~= "environmentDetector" then
- error("No se encontró un environmentDetector en el lado: " .. ENV_SIDE)
- end
- -- Iniciar exploración
- explore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement