Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuracion
- local ENV_SIDE = "left"
- local LIGHT_THRESHOLD = 5
- local FUEL_MINIMUM = 400
- local recorrido = {} -- Para registrar el camino recorrido
- -- Movimiento con registro para poder volver
- local function avanzarRegistrando()
- if turtle.forward() then
- table.insert(recorrido, "back")
- return true
- end
- return false
- end
- local function subirRegistrando()
- if turtle.up() then
- table.insert(recorrido, "down")
- return true
- end
- return false
- end
- local function bajarRegistrando()
- if turtle.down() then
- table.insert(recorrido, "up")
- return true
- end
- return false
- end
- local function girarDerecha()
- turtle.turnRight()
- table.insert(recorrido, "turnLeft")
- end
- local function girarIzquierda()
- turtle.turnLeft()
- 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
- -- Detectar obstáculos y desviarse lateralmente (con escalones) hasta avanzar 4 bloques
- local function cambiocarril()
- print("Intentando cambiar de carril...")
- -- Asegura que esté en el suelo
- while not turtle.detectDown() and bajarRegistrando() do end
- -- Girar hacia la derecha para empezar el cambio de carril
- girarDerecha()
- local pasosExitosos = 0
- for i = 1, 4 do
- -- Intentar subir hasta 3 veces si hay obstáculo
- for intento = 1, 3 do
- if turtle.detect() then
- if not subirRegistrando() then
- break -- No puede subir más
- end
- else
- break -- No hay obstáculo
- end
- end
- -- Intentar avanzar
- if avanzarRegistrando() then
- pasosExitosos = pasosExitosos + 1
- -- Asegura que esté en el suelo
- while not turtle.detectDown() and bajarRegistrando() do end
- else
- -- Si no puede avanzar, deshacer giro y volver
- print("No se pudo avanzar lateralmente, revirtiendo...")
- -- Deshacer desplazamiento parcial
- for j = 1, pasosExitosos do
- turtle.back()
- end
- girarIzquierda()
- return false
- end
- end
- print("Carril lateral alcanzado.")
- -- Intentar avanzar en la misma dirección original
- girarIzquierda()
- if turtle.detect() then
- -- Si hay obstáculo, girar completamente (sentido contrario)
- girarIzquierda()
- print("Obstáculo en nuevo carril. Cambiando dirección.")
- end
- return true
- end
- -- Exploración principal con registro
- local function explore()
- while true do
- refuelIfNeeded()
- if isDarkHere() and turtle.detectDown() then
- placeTorchHere()
- end
- -- Si hay obstáculo delante, intentar subirlo
- if turtle.detect() then
- print("Obstáculo delante. Intentando subir...")
- local step = 0
- local toping = false
- while step < 3 do
- if subirRegistrando() then
- print("Subo")
- step = step + 1
- if avanzarRegistrando() then
- print("Avanzo")
- toping = true
- while not turtle.detectDown() and bajarRegistrando() do end
- break
- end
- else
- if cambiocarril() then
- toping =true
- end
- end
- end
- if cambiocarril() then
- toping =true
- end
- if not toping then
- print("Fin del camino. No se puede avanzar.")
- volverAlInicio()
- return
- 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
- sleep(0.2)
- end
- end
- -- Comprobar environment detector
- 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 la exploración
- explore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement