Advertisement
ImTaken

minar

Mar 20th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.43 KB | None | 0 0
  1. logBakup = fs.open("minarLog.json", "r")
  2.  
  3. turtle.select(1)
  4. turtle.refuel(1)
  5.  
  6. -- Campo de mina
  7. local profundidad = 16
  8. local altura = 8
  9. local ancho = 256
  10. local totalItems = 832
  11. local soloMenas = true
  12. local menasDesechables = {"minecraft:gravel","minecraft:stone","minecraft:cobblestone"}
  13.  
  14. local totalMovimientosNecesarios = ancho * altura * profundidad
  15.  
  16. -- Movimientos totales
  17. local movimientos = turtle.getFuelLevel()
  18. local movimientosTotales = turtle.getFuelLevel()
  19.  
  20. -- variables reciclables
  21. local ciclo = {}
  22. if fs.exists("minarLog.json") then
  23.     local cicloBackup = logBakup.readAll()
  24.     cicloBackup = textutils.unserialise(cicloBackup)
  25.     ciclo = cicloBackup
  26. else
  27.     -- Salir de reposo
  28.     turtle.forward()
  29.  
  30.     ciclo = {altura = altura - 1, profundidad = profundidad - 1, ancho = ancho - 1, x = "right", y = "up", z = "front"}
  31. end
  32.  
  33. local contarMovimientos = 0
  34. local contarMovimientosTotales = 0
  35.  
  36. -- Proceso
  37. while movimientos > 0 do
  38.  
  39.     -- contar campos disponibles
  40.     local contarCamposLibres = 0
  41.     for i=3,16 do
  42.         local numElementos = turtle.getItemCount(i)
  43.         if numElementos == 0 then
  44.             contarCamposLibres = contarCamposLibres + 1
  45.         end
  46.     end
  47.  
  48.     -- Hay campos disponibles?
  49.     if contarCamposLibres == 0 then
  50.         local bool, bloque = turtle.inspect()
  51.         if bool then
  52.  
  53.             local hayEspacio = false
  54.             for i=3,16 do
  55.                 local bloqueCampo = turtle.getItemDetail(i)
  56.                 for m, mena in ipairs(menasDesechables) do
  57.                     if mena == bloqueCampo.name then
  58.                         turtle.select(i)
  59.                         turtle.drop()
  60.  
  61.                         hayEspacio = true
  62.                     end
  63.                 end
  64.  
  65.                 if bloqueCampo.name == bloque.name then
  66.                     if turtle.getItemSpace(i) > 5 then
  67.                         hayEspacio = true
  68.                     end
  69.                 end
  70.             end
  71.  
  72.             -- Hay que volver?
  73.             if not hayEspacio then
  74.                 local volverAncho = (ancho - 1) - ciclo.ancho
  75.                 local volverAltura = 0
  76.                 local volverProfundidad = 0
  77.  
  78.                 local combustibleNecesario
  79.  
  80.                 if ciclo.z == "front" then
  81.                     volverProfundidad = (profundidad - 1) - ciclo.profundidad
  82.                 else
  83.                     volverProfundidad = ciclo.profundidad
  84.                 end
  85.  
  86.                 if ciclo.z == "up" then
  87.                     volverAltura = (altura - 1) - ciclo.altura
  88.                 else
  89.                     volverAltura = ciclo.altura
  90.                 end
  91.  
  92.                 -- Calcular combustible necesario para volver
  93.                 combustibleNecesario = (volverAncho + volverAltura + volverProfundidad) * 2
  94.                 if combustibleNecesario > turtle.getFuelLevel() then
  95.                     local carbonNecesario = math.ceil(combustibleNecesario/80)
  96.  
  97.                     turtle.select(1)
  98.                     turtle.refuel(carbonNecesario)
  99.                 end
  100.  
  101.                 -- Volver en X
  102.                 if ciclo.z == "front" then
  103.                     turtle.turnLeft()
  104.                 else
  105.                     turtle.turnRight()
  106.                 end
  107.                 while volverAncho > 0 do
  108.                     turtle.forward()
  109.                     volverAncho = volverAncho - 1
  110.                 end
  111.  
  112.                 -- volver en Y
  113.                 while volverAltura > 0 do
  114.                     turtle.down()
  115.                     volverAltura = volverAltura - 1
  116.                 end
  117.  
  118.                 -- volver Z
  119.                 turtle.turnLeft()
  120.                 while volverProfundidad > 0 do
  121.                     turtle.forward()
  122.                     volverProfundidad = volverProfundidad - 1
  123.                 end
  124.  
  125.                 -- Dejando cofre y todo el inventario
  126.                 if not turtle.detect() then
  127.                     turtle.dig()
  128.                     turtle.select(2)
  129.                     turtle.place()
  130.                 end
  131.                
  132.                 for i=3,16 do
  133.                     turtle.select(i)
  134.                
  135.                     if turtle.compareTo(1) then
  136.                         turtle.transferTo(1)
  137.                     end
  138.                
  139.                     turtle.drop()
  140.                 end
  141.  
  142.                 turtle.select(1)
  143.  
  144.                 local regresarProfundidad = 0
  145.                 local regresarAltura = 0
  146.                 local regresarAncho = 0
  147.  
  148.                 -- calcular profunidad necesaria para regresar
  149.                 -- Es necesario saber la direccion hacia donde esta minando sobre la profundidad,
  150.                 -- esto se calcula, conociendo el dato, si la altura esta en un numero par o impar
  151.                 if ciclo.z == "front" then
  152.                     regresarProfundidad = (profundidad - 1) - ciclo.profundidad
  153.                 else
  154.                     regresarProfundidad = ciclo.profundidad
  155.                 end
  156.  
  157.                 -- calcular altura necesaria para regresar
  158.                 -- Es necesario saber hacia que direccion esta minando sobre la altura
  159.                 -- Para eso usaremos la variable ciclo.y
  160.                 if ciclo.y == "up" then
  161.                     regresarAltura = (altura - 1) - ciclo.altura
  162.                 else
  163.                     regresarAltura = ciclo.altura
  164.                 end
  165.  
  166.                 -- calcular ancho necesario para regresar
  167.                 -- Es necesaria la posicion respecto a la x, para eso usaremos la variable
  168.                 -- ciclo.ancho
  169.                 regresarAncho = (ancho - 1) - ciclo.ancho
  170.  
  171.                 -- mover hacia Z
  172.                 turtle.turnLeft()
  173.                 turtle.turnLeft()
  174.                 while regresarProfundidad > 0 do
  175.                     turtle.forward()
  176.                     regresarProfundidad = regresarProfundidad - 1
  177.                 end
  178.  
  179.                 -- mover hacia Y
  180.                 while regresarAltura > 0 do
  181.                     turtle.up()
  182.                     regresarAltura = regresarAltura - 1
  183.                 end
  184.  
  185.                 -- mover hacia X
  186.                 turtle.turnRight()
  187.                 while regresarAncho > 0 do
  188.                     turtle.forward()
  189.                     regresarAncho = regresarAncho - 1
  190.                 end
  191.  
  192.                 if ciclo.z == "front" then
  193.                     turtle.turnLeft()
  194.                 else
  195.                     turtle.turnRight()
  196.                 end
  197.             end
  198.         end
  199.     end
  200.  
  201.     -- Avanzar
  202.     if turtle.detect() then
  203.         turtle.dig()
  204.     end
  205.    
  206.     while turtle.forward() == false do
  207.         print("intentando avanzar...")
  208.         if turtle.detect() then
  209.             turtle.dig()
  210.         end
  211.         sleep(0.5)
  212.     end
  213.     ciclo.profundidad = ciclo.profundidad - 1
  214.     print("avanzo!")
  215.  
  216.     if ciclo.profundidad == 0 and ciclo.altura > 0 then
  217.         if ciclo.y == "down" then
  218.             turtle.digDown()
  219.             turtle.down()
  220.         elseif ciclo.y == "up" then
  221.             turtle.digUp()
  222.             turtle.up()
  223.         end
  224.        
  225.         turtle.turnLeft()
  226.         turtle.turnLeft()
  227.        
  228.         ciclo.altura = ciclo.altura - 1
  229.         ciclo.profundidad = profundidad - 1
  230.  
  231.         if ciclo.z == "front" then
  232.             ciclo.z = "back"
  233.         else
  234.             ciclo.z = "front"
  235.         end
  236.     end
  237.  
  238.     if ciclo.altura == 0 and ciclo.profundidad == 0 and ciclo.ancho > 0 then
  239.         if ciclo.y == "up" then
  240.             if altura%2 then
  241.                 turtle.turnLeft()
  242.             else
  243.                 turtle.turnRight()
  244.             end
  245.         else
  246.             if altura%2 then
  247.                 turtle.turnLeft()
  248.             else
  249.                 turtle.turnRight()
  250.             end
  251.         end
  252.        
  253.         turtle.dig()
  254.         turtle.forward()
  255.        
  256.         if ciclo.y == "up" then
  257.             if altura%2 then
  258.                 turtle.turnLeft()
  259.             else
  260.                 turtle.turnRight()
  261.             end
  262.         else
  263.             if altura%2 then
  264.                 turtle.turnLeft()
  265.             else
  266.                 turtle.turnRight()
  267.             end
  268.         end
  269.  
  270.         ciclo.ancho = ciclo.ancho - 1
  271.  
  272.         -- reanudando ciclo
  273.         ciclo.altura = altura - 1
  274.         ciclo.profundidad = profundidad - 1
  275.  
  276.         -- cambiando ciclo.y
  277.         if ciclo.y == "down" then
  278.             ciclo.y = "up"
  279.         elseif ciclo.y == "up" then
  280.             ciclo.y = "down"
  281.         end
  282.  
  283.         if ciclo.z == "front" then
  284.             ciclo.z = "back"
  285.         else
  286.             ciclo.z = "front"
  287.         end
  288.     end
  289.  
  290.     -- if ciclo.altura == 0 and ciclo.profundidad == 0 and ciclo.ancho == 0 then
  291.     --     break
  292.     -- end
  293.  
  294.     movimientos = turtle.getFuelLevel()
  295.  
  296.     if movimientos == 5 then
  297.         turtle.select(1)
  298.         turtle.refuel(1)
  299.     end
  300.  
  301.     log = fs.open("minarLog.json", "w")
  302.     log.write(textutils.serialise(ciclo))
  303.     log.close()
  304.  
  305.     contarMovimientos = contarMovimientos + 1
  306.     contarMovimientosTotales = contarMovimientosTotales + 1
  307.     term.clear()
  308.     term.setCursorPos(1,1)
  309.     print("Movimientos totales: " .. contarMovimientosTotales .. "\nCombustible: " .. movimientos .. "\n\nMovimiento en X: " .. ciclo.x .. "\nMovimiento en Y: " .. ciclo.y .. "\nMovimiento en Z: " .. ciclo.z .. "\n\nPosicion en X: " .. ciclo.ancho .. "\nPosicion en Y: " .. ciclo.altura .. "\nPosicion en Z: " .. ciclo.profundidad)
  310. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement