Advertisement
AlexMastang

OC-HBM-Missile_it

Nov 21st, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | Source Code | 0 0
  1. local component = require("component")
  2. local side = require("sides")
  3. local term = require("term")
  4.  
  5. local red = {}
  6. local C = 1
  7. for add, n in component.list("redstone") do
  8.   red[C] = component.proxy(add)
  9.   C = C + 1
  10. end
  11. if (#red >= 1) then
  12.   red = red[1]
  13. else
  14.   red = nil
  15. end
  16.  
  17. local X, Y, W, H, Xt, Yt, D, x0, y0, r0
  18. local matrix = {}
  19. local siti = {}
  20. local basi = {}
  21. local b_missile, can_missile, gud_missile, des_missile, ful_missile
  22. local b_launch
  23. local lati = {}
  24.  
  25. lati.check = side.top
  26. lati.wait = side.north
  27. lati.silo = side.east
  28. lati.launch = side.west
  29.  
  30. local n1 = {"Alpha", "Bravo", "Charlie", "Delta", "Echo"}
  31. local n2 = {"Romeo", "Tango", "Golf", "Oscar", "Foxtrot"}
  32.  
  33. W, H = term.getViewport()
  34.  
  35.  
  36. -- Disegno del Cerchio
  37. local function Cerchio(mat, r, x, y, v)
  38.   mat[r+x][r+y] = v
  39.   mat[r-x][r+y] = v
  40.   mat[r+x][r-y] = v
  41.   mat[r-x][r-y] = v
  42.   mat[r+y][r+x] = v
  43.   mat[r-y][r+x] = v
  44.   mat[r+y][r-x] = v
  45.   mat[r-y][r-x] = v
  46. end
  47.  
  48. -- Genera un punto casuale nella circonferenza
  49. local function Bomba(mat, r, v)
  50.   local x, y
  51.  
  52.   repeat
  53.     x = math.random(2*r+1)
  54.     y = math.random(2*r+1)
  55.   until ((x-r-1)^2 + (y-r-1)^2 < r^2-1 and mat[y][x] ~= v)
  56.  
  57.   mat[y][x] = v
  58.  
  59.   return x, y
  60. end
  61.  
  62.  
  63. -- Reset della redstone
  64. if (red ~= nil) then
  65.   for i=0,#side-1,1 do
  66.     red.setOutput(side[side[i]], 0)
  67.   end
  68. end
  69.  
  70. -- Componenti
  71. C = 1
  72. for add, n in component.list("ntm_launch_pad") do
  73.   basi[C] = component.proxy(add)
  74.   C = C + 1
  75. end
  76. for add, n in component.list("ntm_custom_launch_pad") do
  77.   basi[C] = component.proxy(add)
  78.   C = C + 1
  79. end
  80.  
  81. local boom = #basi
  82. local lunghezza = math.floor(W / 7)
  83. local raggio = math.floor(lunghezza / 2)
  84.  
  85. if (boom <= 0) then
  86.   error("cacati in culo")
  87. end
  88.  
  89. -- Inserimento coordinate e raggio bersaglio
  90. repeat
  91.   term.clear()
  92.   term.setCursor(1, 1)
  93.   term.write("> Inserire la coordinata X(X): ")
  94.   x0 = tonumber(io.read())
  95.   term.write("> Inserire la coordinata Y(Z): ")
  96.   y0 = tonumber(io.read())
  97.   term.write("> Inserire il raggio: ")
  98.   r0 = tonumber(io.read())
  99. until (x0 ~= nil and y0 ~= nil and r0 ~= nil)
  100. r0 = math.abs(r0)
  101. print()
  102. Xt, Yt = term.getCursor()
  103.  
  104. -- Creazione delle righe
  105. for i=1,lunghezza+1,1 do
  106.   matrix[i] = {}
  107. end
  108.  
  109. -- Impostazione e reset della matrix
  110. for y=1,lunghezza+1,1 do
  111.   for x=1,lunghezza+1,1 do
  112.     matrix[y][x] = 0
  113.   end
  114. end
  115.  
  116. -- Calcolo della circonferenza
  117. X = 0
  118. Y = raggio
  119. D = 3 - 2 * raggio
  120. Cerchio(matrix, raggio+1, X, Y, -1)
  121. while (Y >= X) do
  122.   if (D > 0) then
  123.     Y = Y - 1
  124.     D = D + 4 * (X - Y) + 10
  125.   else
  126.     D = D + 4 * X + 6
  127.   end
  128.   X = X + 1
  129.   Cerchio(matrix, raggio+1, X, Y, -1)
  130. end
  131.  
  132. math.randomseed(os.time())
  133. -- Bombardé!
  134. for i=1,boom,1 do
  135.   siti[i] = {Bomba(matrix, raggio+1, 1)}
  136.   siti[i][1] = math.floor(x0 + (siti[i][1] - raggio - 1) / raggio * r0)
  137.   siti[i][2] = math.floor(y0 + (siti[i][2] - raggio - 1) / raggio * r0)
  138. end
  139.  
  140. -- Stampa la matrix
  141. for y=1,lunghezza+1,1 do
  142.   for x=1,lunghezza+1,1 do
  143.     if (matrix[y][x] == -1) then
  144.       C = "@"
  145.     elseif (matrix[y][x] == 1) then
  146.       C = "X"
  147.     else
  148.       C = " "
  149.     end
  150.     term.write(C .. " ")
  151.   end
  152.   print()
  153. end
  154. X, Y = term.getCursor()
  155.  
  156. -- Stampa i siti
  157. print()
  158. for i=1,#siti,1 do
  159.   term.setCursor((lunghezza+1)*2+1, i+Yt)
  160.   print(("> Sito n. %2d [%15s] -> (X: %5d, Y: %5d)"):format(i, n1[math.random(#n1)].." "..n2[math.random(#n2)], siti[i][1], siti[i][2]))
  161. end
  162.  
  163. -- Settaggio delle coordinate (basi custom)
  164. for i=1,#basi,1 do
  165.   if (basi[i].type == "ntm_custom_launch_pad") then basi[i].setCoords(siti[i][1], siti[i][2]) end
  166. end
  167.  
  168. -- Stampa le basi ed attende che siano tutte disponibili
  169. if (red ~= nil) then
  170.   red.setOutput(lati.check, 15)
  171. end
  172. repeat
  173.   b_launch = true
  174.   for i=1,#basi,1 do
  175.     if (basi[i].type == "ntm_custom_launch_pad") then
  176.       can_missile, gud_missile, des_missile, ful_missile = basi[i].getLaunchInfo()
  177.       b_missile = can_missile and gud_missile and des_missile and ful_missile
  178.       b_launch = b_launch and b_missile
  179.     else
  180.       b_missile = basi[i].canLaunch()
  181.       b_launch = b_launch and b_missile
  182.     end
  183.    
  184.     term.setCursor((lunghezza+1)*2+1, i+#siti+Yt+1)
  185.     print(("> Base n. %2d -> %s     "):format(i, (b_missile) and "Pronto" or "Attesa"))
  186.   end
  187.   os.sleep(1.0)
  188. until (b_launch)
  189.  
  190. -- Fase finale
  191. if (red ~= nil) then
  192.   red.setOutput(lati.check, 0)
  193.   red.setOutput(lati.wait, 15)
  194. end
  195. term.setCursor(X, Y)
  196. term.write("\n> Apertura silos... ")
  197. if (red ~= nil) then
  198.   red.setOutput(lati.silo, 15)
  199. end
  200. Xt, Yt = term.getCursor()
  201. for i=5.0,0.0,-0.1 do
  202.   term.setCursor(Xt, Yt)
  203.   print(("%.1fs  "):format(i))
  204.   os.sleep(0.1)
  205. end
  206. print("> Tutto pronto!")
  207. term.write("> Decollo? (y/n): ")
  208.  
  209. if (io.read() ~= "y")  then
  210.   if (red ~= nil) then
  211.     red.setOutput(lati.silo, 0)
  212.     red.setOutput(lati.wait, 0)
  213.   end
  214.   error("uscita")
  215. end
  216.  
  217. if (red ~= nil) then
  218.   red.setOutput(lati.wait, 0)
  219.   red.setOutput(lati.launch, 15)
  220. end
  221. print("\n> SEQUENZA DI LANCIO INIZIATA")
  222. term.write("> TESTATE LANCIATE IN T-")
  223. Xt, Yt = term.getCursor()
  224. for i=10,0,-1 do
  225.   term.setCursor(Xt, Yt)
  226.   print(i .. "   ")
  227.   os.sleep(1.0)
  228. end
  229.  
  230. for i=1,#basi,1 do
  231.   b_launch = (basi[i].type == "ntm_custom_launch_pad") and basi[i].launch() or basi[i].launch(siti[i][1], siti[i][2])
  232.   print(("> Base n. %d -> %s"):format(i, (b_launch) and "Successo" or "Errore"))
  233.   os.sleep(1.0)
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement