Advertisement
vacnoa

Excavación V2

Apr 30th, 2025
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.78 KB | None | 0 0
  1. -- Cargar funciones externas
  2. dofile("NearEDGE-Functions")
  3.  
  4. -- DIRECCIÓN Y POSICIÓN RELATIVA
  5. Dir = 0  -- Dirección actual (0:+Z, 1:+X, 2:-Z, 3:-X)
  6. LocX, LocY, LocZ = 0, 0, 0  -- Posición relativa desde el inicio
  7.  
  8. DirNames = { "in +Z", "in +X", "in -Z", "in -X" }
  9.  
  10. -- GIRO Y ORIENTACIÓN
  11. function TurnLeft()
  12.     turtle.turnLeft()
  13.     Dir = (Dir - 1) % 4
  14. end
  15.  
  16. function TurnRight()
  17.     turtle.turnRight()
  18.     Dir = (Dir + 1) % 4
  19. end
  20.  
  21. function TurnShortest(targetDir)
  22.     if ((Dir - 1) % 4 == targetDir) then
  23.         TurnLeft()
  24.     else
  25.         TurnRight()
  26.     end
  27. end
  28.  
  29. function TurnToDir(targetDir)
  30.     while Dir ~= targetDir do
  31.         TurnShortest(targetDir)
  32.     end
  33. end
  34.  
  35. -- ACTUALIZACIÓN DE POSICIÓN RELATIVA
  36. function UpdateLocZX()
  37.     local isX = Dir % 2 == 1
  38.     local val = (Dir > 1) and -1 or 1
  39.  
  40.     if isX then
  41.         LocX = LocX + val
  42.     else
  43.         LocZ = LocZ + val
  44.     end
  45. end
  46.  
  47. -- MOVIMIENTOS CON EXCAVACIÓN
  48. function DoDigMove()
  49.     assert(DigMove(), "No se pudo mover " .. DirNames[Dir + 1])
  50.     UpdateLocZX()
  51. end
  52.  
  53. function DoDigMoveUp()
  54.     assert(DigMoveUp(), "No se pudo subir")
  55.     LocY = LocY + 1
  56. end
  57.  
  58. function DoDigMoveDown()
  59.     assert(DigMoveDown(), "No se pudo bajar")
  60.     LocY = LocY - 1
  61. end
  62.  
  63. -- MOVIMIENTO A COORDENADAS
  64. function DigMoveToX(x)
  65.     local dir = (x - LocX > 0) and 1 or 3
  66.     TurnToDir(dir)
  67.     while LocX ~= x do DoDigMove() end
  68. end
  69.  
  70. function DigMoveToY(y)
  71.     while LocY ~= y do
  72.         if y - LocY > 0 then DoDigMoveUp()
  73.         else DoDigMoveDown() end
  74.     end
  75. end
  76.  
  77. function DigMoveToZ(z)
  78.     local dir = (z - LocZ > 0) and 0 or 2
  79.     TurnToDir(dir)
  80.     while LocZ ~= z do DoDigMove() end
  81. end
  82.  
  83. function DigMoveTo(x, y, z)
  84.     DigMoveToY(y)
  85.     DigMoveToX(x)
  86.     DigMoveToZ(z)
  87. end
  88.  
  89. -- INVENTARIO Y COMBUSTIBLE
  90. function deposit()
  91.     for i = 1, 16 do
  92.         turtle.select(i)
  93.         while turtle.getItemCount() > 0 and not turtle.drop() do
  94.             sleep(1)
  95.         end
  96.     end
  97.     turtle.select(1)
  98. end
  99.  
  100. function checkInventoryFull()
  101.     return turtle.getItemCount(15) == 0
  102. end
  103.  
  104. function CalculateFuelCost()
  105.     local total = (LenX * LenY * LenZ - 1) + (LenX - 1) + (LenY - 1) + (LenZ - 1)
  106.     if takeInitialStep then total = total + 2 end
  107.     return total
  108. end
  109.  
  110. -- INTERFAZ DE USUARIO
  111. function PromptUserForParameters()
  112.     LenZ = ReadNum("Bloques hacia delante a cavar:")
  113.     LenX = ReadNum("Filas:")
  114.     LenY = ReadNum("Alto:")
  115.     takeInitialStep = BooleanQuery("¿Empieza un bloque adelantado?")
  116.     __HaltOnInterruption = BooleanQuery("¿Parar al tener un obstáculo?")
  117.    
  118.     print(string.format("X: %i\nY: %i\nZ: %i\nTake Initial Step: %s\nHalt on Interruption: %s\n",
  119.         LenX, LenY, LenZ, tostring(takeInitialStep), tostring(__HaltOnInterruption)))
  120.        
  121.     return BooleanQuery("¿Es correcto?")
  122. end
  123.  
  124. function CheckAndRefuel()
  125.     local needed = CalculateFuelCost()
  126.     local current = turtle.getFuelLevel()
  127.  
  128.     if current == "unlimited" then return true end
  129.  
  130.     if needed > turtle.getFuelLimit() then
  131.         print("Requiere demasiado combustible. ¡Necesitas una turtle avanzada!")
  132.         return false
  133.     elseif current < needed then
  134.         local recommended = GetRecommendedFuel(needed)
  135.         print("Combustible insuficiente. Inserta al menos " .. needed ..
  136.             " unidades y pulsa Enter para continuar.\n(Approx. " .. recommended[2] .. " " .. recommended[1] .. " recomendados)")
  137.         io.read()
  138.         if not ConsumeFuel() or turtle.getFuelLevel() < needed then
  139.             print("No hay suficiente combustible.")
  140.             return false
  141.         end
  142.     end
  143.  
  144.     return true
  145. end
  146.  
  147. -- CICLO DE CAVADO
  148. function ExcavateCube()
  149.     local ModX, ModZ = 0, 0
  150.  
  151.     if takeInitialStep then
  152.         DoDigMove()
  153.         LocZ = 0
  154.     end
  155.  
  156.     while LocY ~= LenY do
  157.         while LocX ~= (LenX - 1 + ModX) do
  158.             while LocZ ~= (LenZ - 1 + ModZ) do
  159.                 DoDigMove()
  160.             end
  161.  
  162.             -- Verificar inventario y depositar si es necesario
  163.             if checkInventoryFull() then
  164.                 local x, y, z, d = LocX, LocY, LocZ, Dir
  165.                 DigMoveTo(0, 0, 0)
  166.                 TurnToDir(2)
  167.                 deposit()
  168.                 DigMoveTo(x, y, z)
  169.                 TurnToDir(d)
  170.             end
  171.  
  172.             -- Cambio de dirección en el zigzag
  173.             local nextDir = ((ModX * (1 / (LenX - 1))) * -2) + 1
  174.             TurnToDir(nextDir)
  175.             DoDigMove()
  176.             TurnToDir((Dir + 2) % 4)
  177.             ModZ = LocZ * -1
  178.         end
  179.  
  180.         while LocZ ~= (LenZ - 1 + ModZ) do
  181.             DoDigMove()
  182.         end
  183.  
  184.         -- Preparar siguiente capa
  185.         TurnToDir((Dir + 2) % 4)
  186.         ModX, ModZ = LocX * -1, LocZ * -1
  187.  
  188.         if LocY ~= LenY - 1 then
  189.             DoDigMoveUp()
  190.         else
  191.             break
  192.         end
  193.     end
  194.  
  195.     -- Regresar al punto de inicio
  196.     if takeInitialStep then
  197.         DigMoveTo(0, 0, -1)
  198.     else
  199.         DigMoveTo(0, 0, 0)
  200.     end
  201.  
  202.     print("¡Excavación terminada!")
  203. end
  204.  
  205. -- PROGRAMA PRINCIPAL
  206. function Main()
  207.     cls()
  208.     print("DigCubeArea v1.0\n---------------------------\n")
  209.    
  210.     local confirmed = PromptUserForParameters()
  211.     if not confirmed then
  212.         print("¡Inténtalo de nuevo!")
  213.         return
  214.     end
  215.  
  216.     if not CheckAndRefuel() then
  217.         return
  218.     end
  219.  
  220.     cls()
  221.     print("Iniciando excavación. ¡Suerte!")
  222.     ExcavateCube()
  223. end
  224.  
  225. -- EJECUTAR
  226. Main()
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement