Advertisement
TeoremaPi

Crear cristal

Mar 22nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. -- distacina entre una capa y otra
  2. distCapas = 2
  3. -- distancia entre una linea y otra dentro de la misma capa
  4. distLineas = 2
  5. -- distancia entre un atomo y otro dentro de la misma linea
  6. distAtomos = 4
  7.  
  8. -- desfase de espacios entre el primer atomo de una fila y el de la siguiente en la misma capa
  9. -- 0 = si no hay desfase
  10. desfaseAtomos = 2
  11. -- desfase en X entre entre un atomo y otro de la fila equivalente en la capa inferior
  12. desfaseHX = 2
  13. -- desfase en Y entre entre una fila y otra equivalente en la capa inferior
  14. desfaseHY = 0
  15.  
  16. xMax = 20
  17. yMax = 20
  18. hMax = 20
  19.  
  20. -- x+ = hacia delante
  21. x = 1
  22. -- y+ = hacia la derecha
  23. y = 1
  24. -- h+ = hacia arriba
  25. h = 1
  26.  
  27. -- coordenada Y de la primera linea de la capa
  28. yInit = 1
  29.  
  30. turtle.select(1)
  31.  
  32. -- Funcion que coloca un atomo bajo la tortuga si es necesario
  33. function colocarAtomo(xf,yf,hf)
  34.  
  35.  fila = (yf - yInit)/distLineas
  36.  capa = (hf - 1)/distCapas
  37.  
  38.  desfase = desfaseAtomos*fila + desfaseHX*capa
  39.  
  40.  condicion = xf <= xMax and xf >= 1 and (xf - desfase)%distAtomos == 1
  41.  
  42.  if condicion then
  43.   while (turtle.getItemCount() == 0) and (turtle.getSelectedSlot() < 16) do
  44.    turtle.select(turtle.getSelectedSlot() + 1)
  45.   end
  46.  
  47.   if (turtle.getItemCount() == 0) and (turtle.getSelectedSlot() == 16) then
  48.    return false
  49.   end
  50.  
  51.   turtle.placeDown()
  52.   return true
  53.  
  54.  end
  55. end
  56.  
  57.  
  58. -- Funcion que calcula si la cooredada Y corresponde a una linea de atomos en esa capa
  59. function esLinea(xf,yf,hf)
  60.  fila = (yf - 1)/distLineas
  61.  capa = (hf - 1)/distCapas
  62.  
  63.  desfase = desfaseHY*capa
  64.  
  65.  return (yf - desfase)%distLineas == 1
  66. end
  67.  
  68.  
  69. -- Bucle principal
  70. while h <= hMax do
  71.  
  72.  -- Comprobar si a esta coordena Y le corresponde una linea de atomos
  73.  if not esLinea(x,y,h) then
  74.  
  75.   turtle.turnRight()
  76.  
  77.   while (not esLinea(x,y,h)) and y <= yMax do
  78.    turtle.forward()
  79.    y = y + 1
  80.   end
  81.  
  82.   yInit = y
  83.  
  84.   turtle.turnLeft()
  85.  
  86.  end
  87.  
  88.  -- Bucle de la capa
  89.  while y <= yMax do
  90.  
  91.   if x == xMax then
  92.    xDif = -1
  93.   else
  94.    xDif = 1
  95.   end
  96.  
  97.   -- Bucle de la fila
  98.   while x>0 and x<=xMax do
  99.    
  100.    print('normal',x,y)
  101.    
  102.    if colocarAtomo(x,y,h) == false then
  103.     return
  104.    end
  105.    
  106.    turtle.forward()
  107.    x = x + xDif
  108.    
  109.   end
  110.  
  111.   -- Dar la vuelta
  112.   if x > xMax then
  113.    -- al final del cristal girar a la derecha
  114.    turtle.turnRight()
  115.  
  116.    turtle.forward()
  117.    y = y+1
  118.  
  119.    while (not esLinea(x,y,h)) and y <= yMax do
  120.     turtle.forward()
  121.     y = y+1
  122.     print('der',x,y)
  123.    end
  124.    
  125.    turtle.turnRight()
  126.  
  127.   else
  128.    -- al principio del cristal girar a la izquierda
  129.    turtle.turnLeft()
  130.    
  131.    turtle.forward()
  132.    y = y+1
  133.  
  134.    while (not esLinea(x,y,h)) and y <= yMax do  
  135.     turtle.forward()
  136.     y = y+1
  137.     print('izq',x,y)
  138.    end
  139.    
  140.    turtle.turnLeft()
  141.  
  142.   end
  143.  
  144.   -- nada mas girar, comprobar si hay que colocar un atomo
  145.   if y <= yMax then
  146.    if colocarAtomo(x,y,h) == false then
  147.     return
  148.    end
  149.    
  150.    turtle.forward()
  151.    x = x - xDif
  152.   end
  153.  
  154.  end
  155.  
  156.  -- Vuelta a casa (1,1,h)
  157.  -- moverse a x=1
  158.  if x > xMax then
  159.   while x>1 do
  160.    turtle.forward()
  161.    x = x - 1
  162.   end
  163.  
  164.   turtle.turnRight()
  165.  
  166.  else
  167.   while x<1 do
  168.    turtle.forward()
  169.    x = x + 1
  170.   end
  171.  
  172.   turtle.turnLeft()
  173.  
  174.  end
  175.  
  176.  -- moverse a y=1
  177.  while y>1 do
  178.   turtle.forward()
  179.   y = y - 1
  180.  end
  181.  
  182.  -- Girar y subir hasta la siguiente altura
  183.  turtle.turnRight()
  184.  turtle.up()
  185.  h = h+1
  186.  
  187.  while (not (h%distCapas == 1)) and (h<=hMax) do
  188.   turtle.up()
  189.   h = h+1
  190.  end
  191.  
  192. end
  193.  
  194. print('Stop',x,y,h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement