Advertisement
TeoremaPi

Torre de refrigeración

Jul 1st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. -- basado en:
  2. -- https://themathkid.tumblr.com/post/31090705353/the-midpoint-circle-algorithm-is-used-to-draw
  3.  
  4. -- programa para construir torre de refrigeracion (figuras de revolucion)
  5.  
  6. -- construye hacia la izquierda
  7.  
  8. -- radio del circulo inicial
  9. r0 = 30
  10.  
  11. -- altura incial (por si se ha estropeado). z0 por defecto
  12. -- se mide colocando el metro en un bloque de la primera capa
  13. z0 = 0
  14.  
  15. -- altura de la torre
  16. hf = r0/0.4
  17.  
  18. -- construccion de la torre
  19. turtle.select(1)
  20.  
  21. -- comenzamos a medir el tiempo
  22. tiempo = os.clock()
  23.  
  24. -- funcion redondeo
  25. function round(x)
  26.  return math.floor(x+0.5)
  27. end
  28.  
  29. -- funcion para buscar el siguiente slot con recursos
  30. -- si no quedan recursos devuelve false, en caso contrario true
  31. function placeBlock()
  32.  while turtle.getItemCount() == 0 do
  33.   turtle.select(turtle.getSelectedSlot() + 1)
  34.  
  35.   if turtle.getSelectedSlot() == 16 and turtle.getItemCount() == 0 then
  36.    return false
  37.   end
  38.  
  39.  end
  40.   turtle.placeDown()
  41.  return true
  42. end
  43.  
  44.  
  45. -- funcion para recoger mas bloques para construir de un inventario
  46. -- lo bloque se situaran en el centro de algun circulo inferior
  47. function reBlocks(r)
  48.  turtle.turnLeft()
  49.  
  50.  for k = 1, r-1 do
  51.   turtle.forward()
  52.  end
  53.  
  54.  hi = 0
  55.  
  56.  turtle.select(1)
  57.  while not turtle.suck() do
  58.   turtle.down()
  59.   hi = hi + 1
  60.  end
  61.  
  62.  while turtle.suck() do
  63.  end
  64.  
  65.  for k = 1, hi do
  66.   turtle.up()
  67.  end
  68.  
  69.  turtle.turnLeft()
  70.  turtle.turnLeft()
  71.  
  72.  for k = 1, r-1 do
  73.   turtle.forward()
  74.  end
  75.  
  76.  turtle.turnLeft()
  77.  
  78. end
  79.  
  80. -- hiperbola x=x(y)
  81. function hiper(y)
  82.  return math.sqrt((0.86*y^2 - 1.26*y + 0.72)/4.75)
  83.  -- return math.sqrt((0.61*y^2 - 0.87*y + 0.41)/2.74)
  84.  -- return math.sqrt((0.73*y^2 - 1.04*y + 0.53)/3.51)
  85. end
  86.  
  87. -- funcion de la curva r=r(z)
  88. function rfun(z)
  89.  y0 = hiper(0)
  90.  return r0/y0 * hiper(z/hf)
  91. end
  92.  
  93.  
  94. for z = z0, hf do
  95.  
  96.  -- cargamos combustible antes de empezar cada altura
  97.  for k = 16, 1, -1 do
  98.   if turtle.getItemCount(k)>0 then
  99.    turtle.select(k)
  100.    turtle.refuel(all)
  101.   end
  102.  end
  103.  print(turtle.getFuelLevel())
  104.  
  105.  -- calculamos el radio de la altura actual y la siguiente
  106.  r = rfun(z)
  107.  rm1 = rfun(z+1)
  108.  
  109.  print("z=" .. z .. " r=" .. r)
  110.  
  111.  -- calculamos si tendremos suficientes recursos para la altura actual
  112.  -- contamos cuantos recursos quedan
  113.  items = 0
  114.  for k = 1, 16 do
  115.   items = items + turtle.getItemCount(k)
  116.  end
  117.  
  118.  -- si no quedan suficientes recargamos
  119.  if items < 8*r then
  120.   reBlocks(round(r))
  121.  end
  122.  
  123.  
  124.  -- realizamos la circunferencia en cuatro cuartos
  125.  for k = 1, 4 do
  126.  
  127.   -- reiniciamos la posicion a cada cuarto de vuelta
  128.   x = round(r)
  129.   y = 0
  130.  
  131.   -- creamos el primer octavo de vuelta
  132.   while x>=y do
  133.    -- para cada nivel de y miramos si la x actual es la mas cercana
  134.    -- a la circunferencia de radio dado
  135.    if round(math.sqrt(r^2 - y^2)) ~= x then
  136.     turtle.turnLeft()
  137.     turtle.forward()
  138.     x = x-1
  139.     turtle.turnRight()
  140.    end
  141.  
  142.    if not placeBlock() then
  143.     return
  144.    end
  145.    
  146.    if x>y then
  147.     turtle.forward()
  148.    end
  149.    y = y+1
  150.   end
  151.  
  152.   y = y-1
  153.   turtle.turnLeft()
  154.  
  155.   -- creamos el segundo octavo de vuelta
  156.   -- hacemos lo mismo que para el primero pero cambiando x e y
  157.   while x>=0 do
  158.    
  159.    if round(math.sqrt(r^2 - x^2)) ~= y then
  160.     turtle.turnRight()
  161.     turtle.forward()
  162.     y = y+1
  163.     turtle.turnLeft()
  164.    end
  165.  
  166.    if not placeBlock() then
  167.     return
  168.    end
  169.    
  170.    if x>0 then
  171.     turtle.forward()
  172.    end
  173.    x = x-1
  174.   end
  175.   x = x+1
  176.  end
  177.  
  178.  -- redondeamos los datos para poder comparar los puntos de incio
  179.  -- del circulo actual y el siguiente
  180.  r = round(r)
  181.  rm1 = round(rm1)
  182.  
  183.  -- al cambiar de altura debemos comprobar debemos movernos
  184.  -- hacia dentro, hacia fuera o solo subir
  185.  -- si el siguiente circulo es menor, nos movemos hacia dentro
  186.  if r>rm1 then
  187.   turtle.turnLeft()
  188.   for k=1, r-rm1 do
  189.    turtle.forward()
  190.   end
  191.   turtle.up()
  192.   turtle.turnRight()
  193.  
  194.   -- si el siguiente circulo es mayor nos movemos hacia fuera
  195.  elseif r<rm1 then
  196.   turtle.turnRight()
  197.   for k=1,rm1-r do
  198.    turtle.forward()
  199.   end
  200.   turtle.up()
  201.   turtle.turnLeft()
  202.  
  203.   -- si son iguales solo nos movemos hacia arriba
  204.  else
  205.   turtle.up()
  206.  end
  207.  
  208. end
  209.  
  210. -- calculamos el tiempo requerido para la construccion
  211. tiempo = os.clock() - tiempo
  212.  
  213. hor = math.floor(tiempo/3600)
  214. min = math.floor((tiempo - hor*3600)/60)
  215. seg = math.floor(tiempo - hor*3600 - min*60 + 0.5)
  216.  
  217. print(string.format("Elapsed time: %02d:%02d:%02d",hor,min,seg))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement