Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuracion
- local ENV_SIDE = "left"
- local LIGHT_THRESHOLD = 8
- local FUEL_MINIMUM = 400
- local torchSlot = 1 -- Slot donde están las antorchas
- local initialPos = {x = 0, y = 0, z = 0} -- Posición inicial
- -- 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
- -- 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 (subiendo y colocándola abajo)
- 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 turtle.up() then
- if turtle.placeDown() then
- print("Antorcha colocada.")
- turtle.down()
- return true
- else
- print("No se pudo colocar la antorcha.")
- turtle.down()
- 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
- -- Función para verificar si hay bloques debajo
- local function detectBlockBelow()
- if turtle.detectDown() then
- local success, block = turtle.inspectDown() -- Usar inspectDown() correctamente
- if success then
- local blockName = block.name -- Obtener el nombre del bloque inspeccionado
- if blockName == "minecraft:air" then
- print("Bloque de aire debajo. Puedo bajar.")
- return true -- Aire, el robot puede bajar
- elseif blockName == "minecraft:water" or blockName == "minecraft:lava" then
- print("Agua o lava debajo. No puedo bajar.")
- return false -- Agua o lava, no puede bajar
- else
- print("Bloque solido debajo. No puedo bajar.")
- return false -- Bloque sólido, no puede bajar
- end
- else
- print("Error al inspeccionar el bloque debajo.")
- return false
- end
- end
- return false -- Si no hay ningún bloque debajo
- end
- -- Función para detectar y evitar obstáculos
- local function detectAndAvoidObstacle()
- if turtle.detect() then
- print("Obstáculo detectado. Girando...")
- -- Si detecta un obstáculo, intentará girar y seguir avanzando
- local success = false
- for i = 1, 4 do
- -- Intentamos girar y avanzar en 4 direcciones posibles
- turtle.turnRight()
- if turtle.forward() then
- success = true
- break
- end
- end
- if not success then
- print("No se pudo evitar el obstáculo. Intentando subir o bajar...")
- moveWithSteps() -- Si no puede girar, intenta subir o bajar
- end
- end
- end
- -- Lógica principal de exploración
- local function explore()
- while true do
- refuelIfNeeded()
- -- Verificar si hay poca luz en el bloque actual
- if isDarkHere() and turtle.detectDown() then
- placeTorchHere()
- end
- -- Intentar mover hacia adelante
- if turtle.detect() then --detectamos bloque delante
- print("Obstaculo delante.Intentamos subir...")
- local step = 0
- while step < 3 do
- if turtle.up() then
- print ("subo")
- if turtle.forward() then
- print ("avanzo")
- while not turtle.detectDown() and turtle.down() do end
- break
- else
- step = step + 1
- end
- else
- detectAndAvoidObstacle ()
- end
- end
- else
- if turtle.forward() then
- if not turtle.detectDown() then
- while not turtle.detectDown() and turtle.down() do end
- end
- else
- detectAndAvoidObstacle ()
- end
- end
- -- Esperar un momento antes de continuar
- sleep(0.2)
- end
- end
- -- Iniciar la exploración
- explore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement