Advertisement
vacnoa

Luz v6

May 1st, 2025 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.07 KB | None | 0 0
  1. -- Configuración
  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. -- Variables de visualización
  8. local posicion = {x = 0, y = 0, z = 0}
  9. local direccion = 0  -- 0: norte, 1: este, 2: sur, 3: oeste
  10. local TGiro = "right"  -- Dirección inicial del zigzag
  11.  
  12. -- Movimiento con registro para poder volver
  13. local function avanzarRegistrando()
  14.   if turtle.forward() then
  15.     table.insert(recorrido, "back")
  16.     if direccion == 0 then posicion.z = posicion.z - 1
  17.     elseif direccion == 1 then posicion.x = posicion.x + 1
  18.     elseif direccion == 2 then posicion.z = posicion.z + 1
  19.     elseif direccion == 3 then posicion.x = posicion.x - 1 end
  20.     return true
  21.   end
  22.   return false
  23. end
  24.  
  25. local function subirRegistrando()
  26.   if turtle.up() then
  27.     table.insert(recorrido, "down")
  28.     posicion.y = posicion.y + 1
  29.     return true
  30.   end
  31.   return false
  32. end
  33.  
  34. local function bajarRegistrando()
  35.   if turtle.down() then
  36.     table.insert(recorrido, "up")
  37.     posicion.y = posicion.y - 1
  38.     return true
  39.   end
  40.   return false
  41. end
  42.  
  43. local function girarDerecha()
  44.   turtle.turnRight()
  45.   direccion = (direccion + 1) % 4
  46.   table.insert(recorrido, "turnLeft")
  47. end
  48.  
  49. local function girarIzquierda()
  50.   turtle.turnLeft()
  51.   direccion = (direccion - 1) % 4
  52.   if direccion < 0 then direccion = direccion + 4 end
  53.   table.insert(recorrido, "turnRight")
  54. end
  55.  
  56. -- Volver al inicio usando el recorrido almacenado
  57. local function volverAlInicio()
  58.   print("Volviendo al inicio...")
  59.   for i = #recorrido, 1, -1 do
  60.     local accion = recorrido[i]
  61.     if accion == "back" then
  62.       turtle.back()
  63.     elseif accion == "up" then
  64.       turtle.up()
  65.     elseif accion == "down" then
  66.       turtle.down()
  67.     elseif accion == "turnLeft" then
  68.       turtle.turnLeft()
  69.     elseif accion == "turnRight" then
  70.       turtle.turnRight()
  71.     end
  72.     sleep(0.1)
  73.   end
  74.   print("Regresó al inicio.")
  75. end
  76.  
  77. -- Repostaje si es necesario
  78. local function refuelIfNeeded()
  79.   if turtle.getFuelLevel() == "unlimited" then return end
  80.   if turtle.getFuelLevel() < FUEL_MINIMUM then
  81.     print("Combustible bajo. Intentando repostar desde el slot 16...")
  82.     turtle.select(16)
  83.     if turtle.refuel() then
  84.       print("Repostaje exitoso. Nivel actual:", turtle.getFuelLevel())
  85.     else
  86.       print("No se pudo repostar. Sin combustible.")
  87.     end
  88.   end
  89. end
  90.  
  91. -- Obtener nivel de luz en el bloque actual
  92. local function isDarkHere()
  93.   local light = peripheral.call(ENV_SIDE, "getBlockLightLevel")
  94.   print("Nivel de luz:", light)
  95.   return light < LIGHT_THRESHOLD
  96. end
  97.  
  98. -- Colocar una antorcha en el bloque actual
  99. local function placeTorchHere()
  100.   for i = 1, 15 do
  101.     local item = turtle.getItemDetail(i)
  102.     if item and item.name == "minecraft:torch" then
  103.       turtle.select(i)
  104.       if subirRegistrando() then
  105.         if turtle.placeDown() then
  106.           print("Antorcha colocada.")
  107.           bajarRegistrando()
  108.           return true
  109.         else
  110.           print("No se pudo colocar la antorcha.")
  111.           bajarRegistrando()
  112.           return false
  113.         end
  114.       else
  115.         print("No se pudo subir para colocar la antorcha.")
  116.         return false
  117.       end
  118.     end
  119.   end
  120.   print("No hay antorchas disponibles.")
  121.   return false
  122. end
  123.  
  124. -- Intentar subir un escalón si hay obstáculo
  125. local function subirescalon()
  126.   if turtle.detect() then
  127.     if not subirRegistrando() then
  128.       return false
  129.     end
  130.   end
  131.   return true
  132. end
  133.  
  134. -- Cambio de carril con zigzag
  135. local function cambiocarril()
  136.   print("Cambiando de carril...")
  137.   while not turtle.detectDown() and bajarRegistrando() do end
  138.  
  139.   if TGiro == "left" then
  140.     girarIzquierda()
  141.   else
  142.     girarDerecha()
  143.   end
  144.  
  145.   local pasosExitosos = 0
  146.   for paso = 1, 4 do
  147.     if subirescalon() and avanzarRegistrando() then
  148.       pasosExitosos = pasosExitosos + 1
  149.       while not turtle.detectDown() and bajarRegistrando() do end
  150.     else
  151.       while not turtle.detectDown() and bajarRegistrando() do end
  152.       local ret = true
  153.       local retrocedi = false
  154.       local retrocedo = false
  155.  
  156.       if TGiro == "left" then
  157.         girarIzquierda()
  158.       else
  159.         girarDerecha()
  160.       end
  161.  
  162.       while not retrocedo do
  163.         if subirescalon() and avanzarRegistrando() and ret then
  164.           if not retrocedi then
  165.             if TGiro ~= "left" then
  166.               girarIzquierda()
  167.             else
  168.               girarDerecha()
  169.             end
  170.             retrocedi = true
  171.             retrocedo = true
  172.           end
  173.         else
  174.           if TGiro ~= "left" then
  175.             girarIzquierda()
  176.           else
  177.             girarDerecha()
  178.           end
  179.           turtle.back()
  180.           table.insert(recorrido, "turnLeft")
  181.           table.insert(recorrido, "turnLeft")
  182.           table.insert(recorrido, "back")
  183.           table.insert(recorrido, "turnLeft")
  184.           table.insert(recorrido, "turnLeft")
  185.           pasosExitosos = pasosExitosos - 1
  186.           retrocedo = true
  187.           ret = false
  188.         end
  189.       end
  190.     end
  191.   end
  192.  
  193.   -- Cambiar dirección del siguiente zigzag
  194.   TGiro = (TGiro == "left") and "right" or "left"
  195.  
  196.   if TGiro == "left" then
  197.     girarIzquierda()
  198.   else
  199.     girarDerecha()
  200.   end
  201.  
  202.   if turtle.detect() then
  203.     if TGiro == "left" then
  204.       girarIzquierda()
  205.     else
  206.       girarDerecha()
  207.     end
  208.     print("Obstáculo en nuevo carril. Cambiando dirección.")
  209.   end
  210.  
  211.   return true
  212. end
  213.  
  214. -- Mostrar estado actual
  215. local function printEstado()
  216.   local dirStr = ({[0] = "Norte", [1] = "Este", [2] = "Sur", [3] = "Oeste"})[direccion]
  217.   print(string.format("[POS] X:%d Y:%d Z:%d | Dir: %s", posicion.x, posicion.y, posicion.z, dirStr))
  218. end
  219.  
  220. -- Exploración principal
  221. local function explore()
  222.   while true do
  223.     refuelIfNeeded()
  224.  
  225.     if isDarkHere() and turtle.detectDown() then
  226.       placeTorchHere()
  227.     end
  228.  
  229.     if turtle.detect() then
  230.       print("Obstáculo delante. Intentando subir...")
  231.       local step = 0
  232.       local toping = false
  233.       while step < 3 do
  234.         if subirRegistrando() then
  235.           step = step + 1
  236.           if avanzarRegistrando() then
  237.             toping = true
  238.             while not turtle.detectDown() and bajarRegistrando() do end
  239.             break
  240.           end
  241.         else
  242.           break
  243.         end
  244.       end
  245.       if not toping then
  246.         if not cambiocarril() then
  247.           print("Fin del camino. No se puede avanzar.")
  248.           volverAlInicio()
  249.           return
  250.         end
  251.       end
  252.     else
  253.       if avanzarRegistrando() then
  254.         while not turtle.detectDown() and bajarRegistrando() do end
  255.       else
  256.         print("No se pudo avanzar ni rodear.")
  257.         if not cambiocarril() then
  258.           volverAlInicio()
  259.           return
  260.         end
  261.       end
  262.     end
  263.     printEstado()
  264.     sleep(0.2)
  265.   end
  266. end
  267.  
  268. -- Verificar environmentDetector
  269. if not peripheral.isPresent(ENV_SIDE) or peripheral.getType(ENV_SIDE) ~= "environmentDetector" then
  270.   error("No se encontró un environmentDetector en el lado: " .. ENV_SIDE)
  271. end
  272.  
  273. -- Iniciar exploración
  274. explore()
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement