Advertisement
AlexMastang

OC-HBM-Missile_en

Nov 21st, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 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 sites = {}
  20. local pads = {}
  21. local b_missile, can_missile, gud_missile, des_missile, ful_missile
  22. local b_launch
  23. local sides = {}
  24.  
  25. sides.check = side.top
  26. sides.wait = side.north
  27. sides.silo = side.east
  28. sides.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. -- Draws the circle
  37. local function Circle(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. -- Generates a random point on the circumference
  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. -- Resets 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. -- Components
  71. C = 1
  72. for add, n in component.list("ntm_launch_pad") do
  73.   pads[C] = component.proxy(add)
  74.   C = C + 1
  75. end
  76. for add, n in component.list("ntm_custom_launch_pad") do
  77.   pads[C] = component.proxy(add)
  78.   C = C + 1
  79. end
  80.  
  81. local boom = #pads
  82. local length = math.floor(W / 7)
  83. local radius = math.floor(length / 2)
  84.  
  85. if (boom <= 0) then
  86.   error("cacati in culo")
  87. end
  88.  
  89. -- Coordinates and target radius aquisition
  90. repeat
  91.   term.clear()
  92.   term.setCursor(1, 1)
  93.   term.write("> Insert coordinate X(X): ")
  94.   x0 = tonumber(io.read())
  95.   term.write("> Insert coordinate Y(Z): ")
  96.   y0 = tonumber(io.read())
  97.   term.write("> Insert radius: ")
  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. -- Creates lines
  105. for i=1,length+1,1 do
  106.   matrix[i] = {}
  107. end
  108.  
  109. -- Matrix initialization and reset
  110. for y=1,length+1,1 do
  111.   for x=1,length+1,1 do
  112.     matrix[y][x] = 0
  113.   end
  114. end
  115.  
  116. -- Circumference calculations
  117. X = 0
  118. Y = radius
  119. D = 3 - 2 * radius
  120. Circle(matrix, radius+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.   Circle(matrix, radius+1, X, Y, -1)
  130. end
  131.  
  132. math.randomseed(os.time())
  133. -- Bombardé!
  134. for i=1,boom,1 do
  135.   sites[i] = {Bomba(matrix, radius+1, 1)}
  136.   sites[i][1] = math.floor(x0 + (sites[i][1] - radius - 1) / radius * r0)
  137.   sites[i][2] = math.floor(y0 + (sites[i][2] - radius - 1) / radius * r0)
  138. end
  139.  
  140. -- Prints the matrix
  141. for y=1,length+1,1 do
  142.   for x=1,length+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. -- Prints the sites
  157. print()
  158. for i=1,#sites,1 do
  159.   term.setCursor((length+1)*2+1, i+Yt)
  160.   print(("> Site n. %2d [%15s] -> (X: %5d, Y: %5d)"):format(i, n1[math.random(#n1)].." "..n2[math.random(#n2)], sites[i][1], sites[i][2]))
  161. end
  162.  
  163. -- Coordinates setting (custom pads)
  164. for i=1,#pads,1 do
  165.   if (pads[i].type == "ntm_custom_launch_pad") then pads[i].setCoords(sites[i][1], sites[i][2]) end
  166. end
  167.  
  168. -- Prints the pads and waits for the all clear
  169. if (red ~= nil) then
  170.   red.setOutput(sides.check, 15)
  171. end
  172. repeat
  173.   b_launch = true
  174.   for i=1,#pads,1 do
  175.     if (pads[i].type == "ntm_custom_launch_pad") then
  176.       can_missile, gud_missile, des_missile, ful_missile = pads[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 = pads[i].canLaunch()
  181.       b_launch = b_launch and b_missile
  182.     end
  183.    
  184.     term.setCursor((length+1)*2+1, i+#sites+Yt+1)
  185.     print(("> Pad n. %2d -> %s     "):format(i, (b_missile) and "Ready" or "Pending"))
  186.   end
  187.   os.sleep(1.0)
  188. until (b_launch)
  189.  
  190. -- Final phase
  191. if (red ~= nil) then
  192.   red.setOutput(sides.check, 0)
  193.   red.setOutput(sides.wait, 15)
  194. end
  195. term.setCursor(X, Y)
  196. term.write("\n> Opening silos... ")
  197. if (red ~= nil) then
  198.   red.setOutput(sides.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("> All clear!")
  207. term.write("> Launch? (y/n): ")
  208.  
  209. if (io.read() ~= "y")  then
  210.   if (red ~= nil) then
  211.     red.setOutput(sides.silo, 0)
  212.     red.setOutput(sides.wait, 0)
  213.   end
  214.   error("exit")
  215. end
  216.  
  217. if (red ~= nil) then
  218.   red.setOutput(sides.wait, 0)
  219.   red.setOutput(sides.launch, 15)
  220. end
  221. print("\n> LAUNCH SEQUENCE INITIATED")
  222. term.write("> MISSILES LAUNCHED 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,#pads,1 do
  231.   b_launch = (pads[i].type == "ntm_custom_launch_pad") and pads[i].launch() or pads[i].launch(sites[i][1], sites[i][2])
  232.   print(("> Pad n. %d -> %s"):format(i, (b_launch) and "Success" or "Error"))
  233.   os.sleep(1.0)
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement