Advertisement
vacnoa

Luz v5

May 1st, 2025 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | None | 0 0
  1. -- Configuracion
  2. local ENV_SIDE = "left"
  3. local LIGHT_THRESHOLD = 5
  4. local FUEL_MINIMUM = 400
  5. local recorrido = {}  -- Para registrar el camino recorrido
  6.  
  7. -- Movimiento con registro para poder volver
  8. local function avanzarRegistrando()
  9.   if turtle.forward() then
  10.     table.insert(recorrido, "back")
  11.     return true
  12.   end
  13.   return false
  14. end
  15.  
  16. local function subirRegistrando()
  17.   if turtle.up() then
  18.     table.insert(recorrido, "down")
  19.     return true
  20.   end
  21.   return false
  22. end
  23.  
  24. local function bajarRegistrando()
  25.   if turtle.down() then
  26.     table.insert(recorrido, "up")
  27.     return true
  28.   end
  29.   return false
  30. end
  31.  
  32. local function girarDerecha()
  33.   turtle.turnRight()
  34.   table.insert(recorrido, "turnLeft")
  35. end
  36.  
  37. local function girarIzquierda()
  38.   turtle.turnLeft()
  39.   table.insert(recorrido, "turnRight")
  40. end
  41.  
  42. -- Volver al inicio usando el recorrido almacenado
  43. local function volverAlInicio()
  44.   print("Volviendo al inicio...")
  45.   for i = #recorrido, 1, -1 do
  46.     local accion = recorrido[i]
  47.     if accion == "back" then
  48.       turtle.back()
  49.     elseif accion == "up" then
  50.       turtle.up()
  51.     elseif accion == "down" then
  52.       turtle.down()
  53.     elseif accion == "turnLeft" then
  54.       turtle.turnLeft()
  55.     elseif accion == "turnRight" then
  56.       turtle.turnRight()
  57.     end
  58.     sleep(0.1)
  59.   end
  60.   print("Regresó al inicio.")
  61. end
  62.  
  63. -- Repostaje si es necesario
  64. local function refuelIfNeeded()
  65.   if turtle.getFuelLevel() == "unlimited" then return end
  66.   if turtle.getFuelLevel() < FUEL_MINIMUM then
  67.     print("Combustible bajo. Intentando repostar desde el slot 16...")
  68.     turtle.select(16)
  69.     if turtle.refuel() then
  70.       print("Repostaje exitoso. Nivel actual:", turtle.getFuelLevel())
  71.     else
  72.       print("No se pudo repostar. Sin combustible.")
  73.     end
  74.   end
  75. end
  76.  
  77. -- Obtener nivel de luz en el bloque actual
  78. local function isDarkHere()
  79.   local light = peripheral.call(ENV_SIDE, "getBlockLightLevel")
  80.   print("Nivel de luz:", light)
  81.   return light < LIGHT_THRESHOLD
  82. end
  83.  
  84. -- Colocar una antorcha en el bloque actual
  85. local function placeTorchHere()
  86.   for i = 1, 15 do
  87.     local item = turtle.getItemDetail(i)
  88.     if item and item.name == "minecraft:torch" then
  89.       turtle.select(i)
  90.       if subirRegistrando() then
  91.         if turtle.placeDown() then
  92.           print("Antorcha colocada.")
  93.           bajarRegistrando()
  94.           return true
  95.         else
  96.           print("No se pudo colocar la antorcha.")
  97.           bajarRegistrando()
  98.           return false
  99.         end
  100.       else
  101.         print("No se pudo subir para colocar la antorcha.")
  102.         return false
  103.       end
  104.     end
  105.   end
  106.   print("No hay antorchas disponibles.")
  107.   return false
  108. end
  109.  
  110. -- Detectar obstáculos y desviarse lateralmente (con escalones) hasta avanzar 4 bloques
  111. local function cambiocarril()
  112.   print("Intentando cambiar de carril...")
  113.   -- Asegura que esté en el suelo
  114.   while not turtle.detectDown() and bajarRegistrando() do end
  115.  
  116.   -- Girar hacia la derecha para empezar el cambio de carril
  117.   girarDerecha()
  118.  
  119.   local pasosExitosos = 0
  120.  
  121.   for i = 1, 4 do
  122.     -- Intentar subir hasta 3 veces si hay obstáculo
  123.     for intento = 1, 3 do
  124.       if turtle.detect() then
  125.         if not subirRegistrando() then
  126.           break -- No puede subir más
  127.         end
  128.       else
  129.         break -- No hay obstáculo
  130.       end
  131.     end
  132.  
  133.     -- Intentar avanzar
  134.     if avanzarRegistrando() then
  135.       pasosExitosos = pasosExitosos + 1
  136.       -- Asegura que esté en el suelo
  137.       while not turtle.detectDown() and bajarRegistrando() do end
  138.     else
  139.       -- Si no puede avanzar, deshacer giro y volver
  140.       print("No se pudo avanzar lateralmente, revirtiendo...")
  141.       -- Deshacer desplazamiento parcial
  142.       for j = 1, pasosExitosos do
  143.         turtle.back()
  144.       end
  145.       girarIzquierda()
  146.       return false
  147.     end
  148.   end
  149.  
  150.   print("Carril lateral alcanzado.")
  151.  
  152.   -- Intentar avanzar en la misma dirección original
  153.   girarIzquierda()
  154.   if turtle.detect() then
  155.     -- Si hay obstáculo, girar completamente (sentido contrario)
  156.     girarIzquierda()
  157.     print("Obstáculo en nuevo carril. Cambiando dirección.")
  158.   end
  159.  
  160.   return true
  161. end
  162. -- Exploración principal con registro
  163. local function explore()
  164.   while true do
  165.         refuelIfNeeded()
  166.  
  167.         if isDarkHere() and turtle.detectDown() then
  168.             placeTorchHere()
  169.         end
  170.  
  171.         -- Si hay obstáculo delante, intentar subirlo
  172.         if turtle.detect() then
  173.             print("Obstáculo delante. Intentando subir...")
  174.             local step = 0
  175.             local toping = false
  176.             while step < 3 do
  177.                 if subirRegistrando() then
  178.                     print("Subo")
  179.                     step = step + 1
  180.                     if avanzarRegistrando() then
  181.                         print("Avanzo")
  182.                         toping = true
  183.                         while not turtle.detectDown() and bajarRegistrando() do end
  184.                         break
  185.                     end
  186.                 else
  187.                     if cambiocarril() then
  188.                         toping =true
  189.                     end
  190.                 end
  191.             end
  192.             if cambiocarril() then
  193.                 toping =true
  194.             end
  195.             if not toping then
  196.                 print("Fin del camino. No se puede avanzar.")
  197.                 volverAlInicio()
  198.                 return
  199.             end
  200.      
  201.         else
  202.             if avanzarRegistrando() then
  203.                 while not turtle.detectDown() and bajarRegistrando() do end
  204.             else
  205.                 print("No se pudo avanzar ni rodear.")
  206.                 if not cambiocarril() then
  207.                     volverAlInicio()
  208.                     return
  209.                 end
  210.             end
  211.         end
  212.         sleep(0.2)
  213.     end
  214.  end
  215.  
  216. -- Comprobar environment detector
  217. if not peripheral.isPresent(ENV_SIDE) or peripheral.getType(ENV_SIDE) ~= "environmentDetector" then
  218.   error("No se encontró un environmentDetector en el lado: " .. ENV_SIDE)
  219. end
  220.  
  221. -- Iniciar la exploración
  222. explore()
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement