Advertisement
vacnoa

luz cueva v3

Apr 30th, 2025 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. -- Configuracion
  2. local ENV_SIDE = "left"
  3. local LIGHT_THRESHOLD = 8
  4. local FUEL_MINIMUM = 400
  5. local torchSlot = 1  -- Slot donde están las antorchas
  6. local initialPos = {x = 0, y = 0, z = 0} -- Posición inicial
  7.  
  8. -- Comprobar environment detector
  9. if not peripheral.isPresent(ENV_SIDE) or peripheral.getType(ENV_SIDE) ~= "environmentDetector" then
  10.   error("No se encontró un environmentDetector en el lado: " .. ENV_SIDE)
  11. end
  12.  
  13. -- Repostaje si es necesario
  14. local function refuelIfNeeded()
  15.   if turtle.getFuelLevel() == "unlimited" then return end
  16.   if turtle.getFuelLevel() < FUEL_MINIMUM then
  17.     print("Combustible bajo. Intentando repostar desde el slot 16...")
  18.     turtle.select(16)
  19.     if turtle.refuel() then
  20.       print("Repostaje exitoso. Nivel actual:", turtle.getFuelLevel())
  21.     else
  22.       print("No se pudo repostar. Sin combustible.")
  23.     end
  24.   end
  25. end
  26.  
  27. -- Obtener nivel de luz en el bloque actual
  28. local function isDarkHere()
  29.   local light = peripheral.call(ENV_SIDE, "getBlockLightLevel")
  30.   print("Nivel de luz:", light)
  31.   return light < LIGHT_THRESHOLD
  32. end
  33.  
  34. -- Colocar una antorcha en el bloque actual (subiendo y colocándola abajo)
  35. local function placeTorchHere()
  36.   for i = 1, 15 do
  37.     local item = turtle.getItemDetail(i)
  38.     if item and item.name == "minecraft:torch" then
  39.       turtle.select(i)
  40.       if turtle.up() then
  41.         if turtle.placeDown() then
  42.           print("Antorcha colocada.")
  43.           turtle.down()
  44.           return true
  45.         else
  46.           print("No se pudo colocar la antorcha.")
  47.           turtle.down()
  48.           return false
  49.         end
  50.       else
  51.         print("No se pudo subir para colocar la antorcha.")
  52.         return false
  53.       end
  54.     end
  55.   end
  56.   print("No hay antorchas disponibles.")
  57.   return false
  58. end
  59.  
  60. -- Función para verificar si hay bloques debajo
  61. local function detectBlockBelow()
  62.   if turtle.detectDown() then
  63.     local success, block = turtle.inspectDown()  -- Usar inspectDown() correctamente
  64.     if success then
  65.       local blockName = block.name  -- Obtener el nombre del bloque inspeccionado
  66.       if blockName == "minecraft:air" then
  67.         print("Bloque de aire debajo. Puedo bajar.")
  68.         return true -- Aire, el robot puede bajar
  69.       elseif blockName == "minecraft:water" or blockName == "minecraft:lava" then
  70.         print("Agua o lava debajo. No puedo bajar.")
  71.         return false -- Agua o lava, no puede bajar
  72.       else
  73.         print("Bloque solido debajo. No puedo bajar.")
  74.         return false -- Bloque sólido, no puede bajar
  75.       end
  76.     else
  77.       print("Error al inspeccionar el bloque debajo.")
  78.       return false
  79.     end
  80.   end
  81.   return false -- Si no hay ningún bloque debajo
  82. end
  83.  
  84.  
  85. -- Función para detectar y evitar obstáculos
  86. local function detectAndAvoidObstacle()
  87.   if turtle.detect() then
  88.     print("Obstáculo detectado. Girando...")
  89.     -- Si detecta un obstáculo, intentará girar y seguir avanzando
  90.     local success = false
  91.     for i = 1, 4 do
  92.       -- Intentamos girar y avanzar en 4 direcciones posibles
  93.       turtle.turnRight()
  94.       if turtle.forward() then
  95.         success = true
  96.         break
  97.       end
  98.     end
  99.     if not success then
  100.       print("No se pudo evitar el obstáculo. Intentando subir o bajar...")
  101.       moveWithSteps()  -- Si no puede girar, intenta subir o bajar
  102.     end
  103.   end
  104. end
  105.  
  106. -- Lógica principal de exploración
  107. local function explore()
  108.   while true do
  109.     refuelIfNeeded()
  110.  
  111.     -- Verificar si hay poca luz en el bloque actual
  112.     if isDarkHere() and turtle.detectDown() then
  113.         placeTorchHere()
  114.     end
  115.  
  116.     -- Intentar mover hacia adelante
  117.     if turtle.detect() then  --detectamos bloque delante
  118.         print("Obstaculo delante.Intentamos subir...")
  119.         local step = 0
  120.         while step < 3 do
  121.             if turtle.up() then
  122.                 print ("subo")
  123.                 if turtle.forward() then
  124.                     print ("avanzo")
  125.                     while not turtle.detectDown() and turtle.down() do end
  126.                     break
  127.                 else
  128.                     step = step + 1
  129.                 end
  130.             else
  131.                 detectAndAvoidObstacle ()
  132.             end
  133.         end
  134.     else
  135.         if turtle.forward() then
  136.             if not turtle.detectDown() then
  137.                 while not turtle.detectDown() and turtle.down() do end
  138.             end
  139.         else
  140.             detectAndAvoidObstacle ()
  141.         end
  142.     end
  143.     -- Esperar un momento antes de continuar
  144.     sleep(0.2)
  145.   end
  146. end
  147.  
  148. -- Iniciar la exploración
  149. explore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement