MegiRicky

NGDWBVRGen4

Mar 6th, 2025 (edited)
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.46 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local radar = peripheral.find("sp_radar")
  3. local modem = peripheral.wrap("right")
  4. local speaker = peripheral.find("speaker")
  5.  
  6. -- Ensure all required peripherals are connected
  7. if not monitor or not radar or not ship then
  8. error("Required peripherals are not attached")
  9. end
  10.  
  11. monitor.setTextScale(0.5)
  12. monitor.clear()
  13.  
  14. local radarScale = 500
  15. local friendlyID = {289, 232}
  16. local targetPositions = {}
  17. local waypoints = {}
  18. local displayState = "radar" -- Initial display state
  19. local selectedTarget = nil
  20. local lockedTargetId = nil
  21. local lockedWaypoint = nil -- Variable to store the locked waypoint
  22. local projectileSpeed = 320 --m/s
  23.  
  24. -- Get channel data for cannons
  25. local function getChannelInput(prompt, default)
  26. print(prompt .. ", default: " .. default)
  27. local input = io.read()
  28. if input == "" then
  29. input = default
  30. end
  31. return tonumber(input)
  32. end
  33.  
  34. local controlChannel = getChannelInput("Input the control channel",500)
  35. local waypointChannel = getChannelInput("Input the control channel",1421)
  36. local port5inchChannel = getChannelInput("Input the port side cannon data channel", 900)
  37. local starboard5inchChannel = getChannelInput("Input the starboard side cannon data channel", 901)
  38. local bow15inchChannel = getChannelInput("Input the bow cannon data channel", 902)
  39. local stern15inchChannel = getChannelInput("Input the stern cannon data channel", 903)
  40. local missileControlChannel = getChannelInput("Input the missile control channel", 1400)
  41. local missileInfoChannel = missileControlChannel + 10
  42.  
  43. if modem then
  44. modem.open(waypointChannel)
  45. modem.open(controlChannel)
  46. modem.open(starboard5inchChannel)
  47. modem.open(port5inchChannel)
  48. modem.open(bow15inchChannel)
  49. modem.open(stern15inchChannel)
  50. modem.open(missileInfoChannel)
  51. modem.open(missileControlChannel)
  52. end
  53.  
  54. local cannonData = {
  55. port5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {} },
  56. starboard5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  57. bow15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  58. stern15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  59. }
  60.  
  61. local cannonTarget = {
  62. port5inch = { type = nil, side = "port", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  63. starboard5inch = { type = nil, side = "starboard", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  64. bow15inch = { type = nil, side = "bow", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  65. stern15inch = { type = nil, side = "stern", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  66. }
  67.  
  68. local LaunchedMissiles = {}
  69. local missileControls = {
  70. fireMissile = {}
  71. }
  72. local immediateMissileInfo = {}
  73. local AIMInfoList,GBUInfoList,thunderBoltInfoList = {},{},{}
  74. local pendingMissileLaunches = {}
  75. local AIMCount,GBUCount,thunderboltCount = 0,0,0
  76.  
  77. local function checkFriendly(ID)
  78. local friendly = false
  79. for _, id in ipairs(friendlyID) do
  80. if tonumber(ID) == tonumber(id) then
  81. friendly = true
  82. break
  83. end
  84. end
  85. return friendly
  86. end
  87.  
  88. local w, h = monitor.getSize()
  89.  
  90. local function drawControls()
  91. local w, h = monitor.getSize()
  92. monitor.setCursorPos(1, h-1)
  93. monitor.write(" ")
  94. monitor.setCursorPos(1, h)
  95. monitor.write(" ")
  96. monitor.setCursorPos(1, h-1)
  97. monitor.write("Scale: ")
  98. monitor.setCursorPos(8, h-1)
  99. monitor.write("-")
  100. monitor.setCursorPos(10, h-1)
  101. monitor.write("+")
  102. monitor.setCursorPos(1, h-2)
  103. monitor.write("Stop locking")
  104. monitor.setCursorPos(1, h-3)
  105. monitor.write("Launch missile")
  106.  
  107. local segmentLength = radarScale / 2
  108. local scaleText
  109. if segmentLength >= 1000 then
  110. scaleText = string.format("%.1f km", segmentLength / 1000)
  111. else
  112. scaleText = string.format("%d m", segmentLength)
  113. end
  114.  
  115. monitor.setCursorPos(1, h)
  116. monitor.write("Segment Length: " .. scaleText)
  117.  
  118. -- Add button to navigate to waypoint page at the bottom right corner
  119. monitor.setCursorPos(w-10, h-1)
  120. monitor.write("Waypoints")
  121.  
  122. -- Add button to navigate to the cannon adjustment page at the top right corner
  123. monitor.setCursorPos(w-13, 1)
  124. monitor.write("Adjust Cannon")
  125. end
  126.  
  127. local function addWaypoint(x, y, z, color)
  128. local newWaypoint = {
  129. x = x,
  130. y = y,
  131. z = z,
  132. color = color or colors.purple
  133. }
  134. table.insert(waypoints, newWaypoint)
  135. end
  136.  
  137. local function drawWaypointPage()
  138. local w, h = monitor.getSize()
  139. monitor.clear()
  140. monitor.setCursorPos(1, 1)
  141. monitor.write("Waypoint Page")
  142. monitor.setCursorPos(1, 3)
  143. monitor.write("Enter coordinates (x, y, z) and color:")
  144. monitor.setCursorPos(1, 4)
  145. monitor.write("Format: x,y,z,color")
  146. monitor.setCursorPos(1, 5)
  147. monitor.write("Colors: red, yellow, green, blue, purple")
  148. monitor.setCursorPos(1, h-1)
  149. monitor.write("Enter to submit, Back to cancel.")
  150. end
  151.  
  152. local function drawCannonAdjustmentPage()
  153. local w, h = monitor.getSize()
  154. monitor.clear()
  155. monitor.setCursorPos(1, 1)
  156. monitor.write("Cannon Pitch and Yaw Adjustment")
  157.  
  158. local cannonNames = {
  159. {name = "Port 5 inch", data = cannonData.port5inch, target = cannonTarget.port5inch},
  160. {name = "Starboard 5 inch", data = cannonData.starboard5inch, target = cannonTarget.starboard5inch},
  161. {name = "Bow 15 inch", data = cannonData.bow15inch, target = cannonTarget.bow15inch},
  162. {name = "Stern 15 inch", data = cannonData.stern15inch, target = cannonTarget.stern15inch}
  163. }
  164.  
  165. local yOffset = 3
  166.  
  167. for _, cannon in ipairs(cannonNames) do
  168. monitor.setCursorPos(1, yOffset)
  169. monitor.write(cannon.name .. ":")
  170.  
  171. monitor.setCursorPos(1, yOffset + 1)
  172. monitor.write("Current: pitch: " .. (cannon.data.pitch and string.format("%.2f", cannon.data.pitch) or "N/A"))
  173. monitor.setCursorPos(26, yOffset + 1)
  174. monitor.write("yaw: " .. (cannon.data.yaw and string.format("%.2f", cannon.data.yaw) or "N/A"))
  175.  
  176. monitor.setCursorPos(1, yOffset + 2)
  177. monitor.write("Adjustment: pitch: " .. (cannon.target.pitchAdjust and string.format("%.2f", cannon.target.pitchAdjust) or "N/A"))
  178. monitor.setCursorPos(26, yOffset + 2)
  179. monitor.write("yaw: " .. (cannon.target.yawAdjust and string.format("%.2f", cannon.target.yawAdjust) or "N/A"))
  180.  
  181. monitor.setCursorPos(1, yOffset + 3)
  182. monitor.write("[pitch +] [pitch -]")
  183. monitor.setCursorPos(w - 30, yOffset + 3)
  184. monitor.write("[yaw +] [yaw -]")
  185.  
  186. yOffset = yOffset + 6 -- Move down for the next cannon
  187. end
  188.  
  189. monitor.setCursorPos(1, h)
  190. monitor.write("Back")
  191. end
  192.  
  193. local function drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  194. local aspectRatio = 1.5
  195. local segmentLength = radarScale / 4
  196. local pixelPerSegmentX = w / 4
  197. local pixelPerSegmentY = h / 4
  198.  
  199. -- Draw the grid
  200. for i = 0, w - pixelPerSegmentX, pixelPerSegmentX do
  201. -- Vertical lines
  202. for y = 1, h - 2 do -- Leave space for the scale display at the bottom
  203. monitor.setCursorPos(i, y)
  204. monitor.write("|")
  205. end
  206. end
  207.  
  208. for j = 0, h - pixelPerSegmentY, pixelPerSegmentY do
  209. -- Horizontal lines
  210. for x = 1, w do
  211. monitor.setCursorPos(x, j)
  212. monitor.write("-")
  213. end
  214. end
  215.  
  216. -- Draw the circle
  217. for i = 0, 360, 5 do
  218. local rad = i * (math.pi / 180)
  219. local x = centerX + (radius * math.sin(rad)) * aspectRatio
  220. local y = centerY + radius * math.cos(rad)
  221. monitor.setCursorPos(math.floor(x + 0.5), math.floor(y + 0.5))
  222. monitor.write(".")
  223. end
  224.  
  225. -- Draw the crosshair
  226. for y = centerY - radius, centerY + radius do
  227. monitor.setCursorPos(centerX, y)
  228. monitor.write("|")
  229. end
  230.  
  231. local trueBearing = (math.deg(shipYawRadians) + 360) % 360 - 180
  232. if trueBearing < 0 then
  233. trueBearing = trueBearing + 360
  234. end
  235. local textPos = centerY - radius + 10
  236. if textPos > 0 then
  237. monitor.setCursorPos(centerX - (#tostring(math.floor(trueBearing)) + 2) / 2, textPos)
  238. monitor.write(tostring(math.floor(trueBearing)) .. "°")
  239. end
  240.  
  241. local directions = {"N", "W", "S", "E"}
  242. local dirAngles = {0, 90, 180, 270}
  243.  
  244. for index, dir in ipairs(directions) do
  245. local dirRad = (dirAngles[index] + math.deg(shipYawRadians)) * (math.pi / 180)
  246. local dirX = centerX + (radius * math.sin(dirRad)) * aspectRatio
  247. local dirY = centerY + radius * math.cos(dirRad)
  248. monitor.setCursorPos(math.floor(dirX + 0.5), math.floor(dirY + 0.5))
  249. monitor.write(dir)
  250. end
  251.  
  252. -- Display segment length
  253. monitor.setCursorPos(1, centerY + radius + 2)
  254. monitor.write("Segment Length: " .. segmentLength .. " meters")
  255. end
  256.  
  257. local function drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  258. local dx = relX - centerX
  259. local dy = relY - centerY
  260. local mag = math.sqrt((dx / aspectRatio)^2 + dy^2)
  261.  
  262. if (mag > radius) then
  263. local intersectX = centerX + ((dx / mag) * radius) * aspectRatio
  264. local intersectY = centerY + (dy / mag) * radius
  265. return intersectX, intersectY
  266. else
  267. return relX, relY
  268. end
  269. end
  270.  
  271. local function drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  272. local pos = ship.getWorldspacePosition()
  273. local shipYawRadians = math.atan2(-ship.getTransformationMatrix()[1][3], ship.getTransformationMatrix()[3][3])
  274. if shipYawRadians < 0 then shipYawRadians = shipYawRadians + 2 * math.pi end
  275. -- Function to calculate relative yaw in degrees from ship's facing
  276. local function relativeYaw(dx, dz)
  277. local angle = math.atan2(dx, dz)
  278. local relativeAngle = angle + shipYawRadians - math.pi
  279. if relativeAngle < -math.pi then
  280. relativeAngle = relativeAngle + 2 * math.pi
  281. elseif relativeAngle >= math.pi then
  282. relativeAngle = relativeAngle - 2 * math.pi
  283. end
  284. return math.deg(relativeAngle)
  285. end
  286.  
  287. local function drawCrosshairForCannon(cannonData, color)
  288. if cannonData.hitPosX and cannonData.hitPosZ then
  289. local dx = cannonData.hitPosX - pos.x
  290. local dz = cannonData.hitPosZ - pos.z
  291. local cannonYaw = relativeYaw(dx, dz)
  292. local distance = math.sqrt(dx^2 + dz^2)
  293. local scaledDistance = (distance / radarScale) * radius
  294. local relX = centerX + (scaledDistance * math.sin(math.rad(cannonYaw))) * aspectRatio
  295. local relY = centerY + scaledDistance * math.cos(math.rad(cannonYaw))
  296. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  297. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  298. monitor.setBackgroundColor(color)
  299. monitor.write("X")
  300. monitor.setBackgroundColor(colors.black)
  301. return cannonYaw
  302. end
  303. return nil
  304. end
  305.  
  306. -- Draw crosshair and check range for each cannon
  307. local portYaw = drawCrosshairForCannon(cannonData.port5inch, colors.red)
  308. if portYaw and not(portYaw > 225 and portYaw < 330) then
  309. monitor.setCursorPos(1, 1)
  310. monitor.write("Warning: Port cannon may hit the ship!")
  311. end
  312.  
  313. local starboardYaw = drawCrosshairForCannon(cannonData.starboard5inch, colors.blue)
  314. if starboardYaw and not(starboardYaw > 30 and starboardYaw < 135) then
  315. monitor.setCursorPos(1, 2)
  316. monitor.write("Warning: Starboard cannon may hit the ship!")
  317. end
  318.  
  319. local bowYaw = drawCrosshairForCannon(cannonData.bow15inch, colors.green)
  320. if bowYaw and not(bowYaw > 120 and bowYaw < 240) then
  321. monitor.setCursorPos(1, 3)
  322. monitor.write("Warning: Bow cannon may hit the ship!")
  323. end
  324.  
  325. local sternYaw = drawCrosshairForCannon(cannonData.stern15inch, colors.yellow)
  326. if sternYaw and not(sternYaw > 300 or sternYaw < 60) then
  327. monitor.setCursorPos(1, 4)
  328. monitor.write("Warning: Stern cannon may hit the ship!")
  329. end
  330. end
  331.  
  332. local function calculateRelativeAngle(objectAngle, shipYawRadians)
  333. local relativeAngle = objectAngle + shipYawRadians - math.pi
  334. if relativeAngle < -math.pi then
  335. relativeAngle = relativeAngle + 2 * math.pi
  336. elseif relativeAngle >= math.pi then
  337. relativeAngle = relativeAngle - 2 * math.pi
  338. end
  339. return relativeAngle
  340. end
  341.  
  342. local function drawRadarDisplay()
  343. local w, h = monitor.getSize()
  344. local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  345. local radius = math.min(centerX, centerY) - 1
  346. local aspectRatio = 1.2
  347.  
  348. local matrix = ship.getTransformationMatrix()
  349. local shipYawRadians = math.atan2(-matrix[1][3], matrix[3][3])
  350. if (shipYawRadians < 0) then
  351. shipYawRadians = shipYawRadians + 2 * math.pi
  352. end
  353.  
  354. drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  355. monitor.setCursorPos(centerX, centerY)
  356. monitor.write("+")
  357.  
  358. drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  359.  
  360. local pos = ship.getWorldspacePosition()
  361. local results = radar.scanForShips(10000)
  362. targetPositions = {}
  363. for _, object in ipairs(results) do
  364. local x = object.pos.x - pos.x
  365. local z = object.pos.z - pos.z
  366. local distance = math.sqrt(x^2 + z^2)
  367. local objectAngle = math.atan2(x, z)
  368. local relativeAngle = calculateRelativeAngle(objectAngle, shipYawRadians)
  369.  
  370. local scaledDistance = (distance / radarScale) * radius
  371. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  372. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  373.  
  374. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  375.  
  376. if (object.mass > 2000 and distance > 5) then
  377. if (distance > radarScale) then
  378. monitor.setTextColor(colors.lightBlue)
  379. else
  380. monitor.setTextColor(colors.red)
  381. end
  382. if (checkFriendly(object.id)) then
  383. monitor.setBackgroundColor(colors.blue)
  384. elseif (object.mass > 1000000) then
  385. monitor.setBackgroundColor(colors.orange)
  386. elseif (math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2) > 30) then
  387. monitor.setBackgroundColor(colors.red)
  388. else
  389. monitor.setBackgroundColor(colors.yellow)
  390. end
  391. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  392. monitor.write("o")
  393. monitor.setTextColor(colors.green)
  394. monitor.setBackgroundColor(colors.black)
  395.  
  396. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = object})
  397. end
  398. end
  399.  
  400. -- Draw waypoints
  401. for index, waypoint in ipairs(waypoints) do
  402. local wpX = waypoint.x - pos.x
  403. local wpZ = waypoint.z - pos.z
  404. local distance = math.sqrt(wpX^2 + wpZ^2)
  405. local wpAngle = math.atan2(wpX, wpZ)
  406. local relativeAngle = calculateRelativeAngle(wpAngle, shipYawRadians)
  407.  
  408. local scaledDistance = (distance / radarScale) * radius
  409. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  410. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  411.  
  412. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  413.  
  414. monitor.setBackgroundColor(waypoint.color)
  415. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  416. monitor.write("W")
  417. monitor.setBackgroundColor(colors.black)
  418.  
  419. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = {type = "waypoint", x = waypoint.x,y = waypoint.y, z = waypoint.z, index = index}})
  420. end
  421. end
  422.  
  423. local function displayTargetInfo(target)
  424. local w, h = monitor.getSize()
  425. local pos = ship.getWorldspacePosition()
  426.  
  427. monitor.clear()
  428. monitor.setCursorPos(1, h-13)
  429. monitor.write("Target Info")
  430. monitor.setCursorPos(1, h-12)
  431.  
  432. local targetPosX, targetPosY, targetPosZ
  433. if target.type == "waypoint" then
  434. monitor.write("Type: Waypoint")
  435. targetPosX, targetPosY, targetPosZ = target.x, target.y, target.z
  436. monitor.setCursorPos(1, h-7)
  437. monitor.write("Coordinate: " .. target.x .. ", "..target.y..", ".. target.z)
  438. else
  439. monitor.write("ID: " .. target.id)
  440. targetPosX, targetPosY, targetPosZ = target.pos.x, target.pos.y, target.pos.z
  441. monitor.setCursorPos(1, h-11)
  442. monitor.write("Mass: " .. target.mass)
  443. monitor.setCursorPos(1, h-10)
  444. monitor.write("Velocity: " .. string.format("%.2f, %.2f, %.2f", target.velocity.x, target.velocity.y, target.velocity.z))
  445. monitor.setCursorPos(1, h-9)
  446. monitor.write("Position: " .. string.format("%.2f, %.2f, %.2f", target.pos.x, target.pos.y, target.pos.z))
  447. end
  448.  
  449. local distance = math.sqrt((targetPosX - pos.x)^2 + (targetPosZ - pos.z)^2)
  450. local dx = targetPosX - pos.x
  451. local dz = targetPosZ - pos.z
  452. local bearing = (math.deg(math.atan2(dz, dx)) + 360) % 360 - 270
  453. if bearing < 0 then
  454. bearing = bearing + 360
  455. end
  456.  
  457. monitor.setCursorPos(1, h-8)
  458. monitor.write("Distance: " .. string.format("%.2f", distance) .. " meters")
  459. monitor.setCursorPos(1, h-7)
  460. monitor.write("Bearing: " .. string.format("%.2f", bearing) .. "°")
  461.  
  462. if target.type ~= "waypoint" then
  463. local type = "unknown"
  464. local hostile = "hostile"
  465. if target.mass > 1000000 then
  466. type = "ship"
  467. elseif math.sqrt(target.velocity.x ^ 2 + target.velocity.y ^ 2 + target.velocity.z ^ 2) > 30 then
  468. type = "Flying missile / plane"
  469. else
  470. type = "unknown"
  471. end
  472. if checkFriendly(target.id) then
  473. hostile = "friendly"
  474. else
  475. hostile = "hostile"
  476. end
  477. monitor.setCursorPos(1, h-6)
  478. monitor.write("Type: " .. hostile .. " " .. type)
  479. end
  480.  
  481. monitor.setCursorPos(1, h-5)
  482. monitor.write("[Port gun]")
  483. monitor.setCursorPos(1, h-4)
  484. monitor.write("[Starboard gun]")
  485. monitor.setCursorPos(1, h-3)
  486. monitor.write("[Bow gun]")
  487. monitor.setCursorPos(1, h-2)
  488. monitor.write("[Stern gun]")
  489.  
  490. monitor.setCursorPos(20, h-5)
  491. monitor.write("[AIM-220, Avaialbe: "..AIMCount.."]")
  492. monitor.setCursorPos(20, h-4)
  493. monitor.write("[GBU-42, Avaialbe: "..GBUCount.."]")
  494. monitor.setCursorPos(20, h-3)
  495. monitor.write("[thunderbolt, Avaialbe: "..thunderboltCount.."]")
  496.  
  497.  
  498. monitor.setCursorPos(1, h)
  499. monitor.write("Back")
  500. end
  501.  
  502. local function launchMissile(targetType, targetId, targetPos, missileType)
  503. local currentMissile = nil
  504.  
  505. -- Function to find and remove the first unlaunched missile from a list
  506. local function findUnlaunchedMissile(missileList)
  507. for i = #missileList, 1, -1 do -- Iterate in reverse to avoid index shifting issues
  508. local missile = missileList[i]
  509. if missile.launchState == false then
  510. missile.launchState = true -- Mark missile as launched
  511. table.insert(LaunchedMissiles, missile) -- Move to launched list
  512. return table.remove(missileList, i) -- Remove from available list and return it
  513. end
  514. end
  515. return nil
  516. end
  517.  
  518.  
  519. -- Get the first unlaunched missile of the specified type
  520. if missileType == "AIM-220" then
  521. currentMissile = findUnlaunchedMissile(AIMInfoList)
  522. elseif missileType == "GBU-42" then
  523. currentMissile = findUnlaunchedMissile(GBUInfoList)
  524. elseif missileType == "thunderBolt" then
  525. currentMissile = findUnlaunchedMissile(thunderBoltInfoList)
  526. end
  527.  
  528. -- If no unlaunched missile is found, exit function
  529. if not currentMissile then
  530. print("No available unlaunched missile of type:", missileType)
  531. return
  532. end
  533.  
  534. local currentMissileId = currentMissile.id
  535. print("Launching missile " .. currentMissileId)
  536.  
  537. -- Assign the missile as launched and set target info
  538. if targetType == "ship" then
  539. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  540. else
  541. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  542. end
  543. end
  544.  
  545. local function handleTouch()
  546. local w, h = monitor.getSize()
  547. while true do
  548. local event, side, x, y = os.pullEvent("monitor_touch")
  549. if displayState == "radar" then
  550. if y == h-1 and x >= 8 and x <= 9 then
  551. radarScale = math.max(100, radarScale - 100)
  552. drawControls()
  553. drawRadarDisplay()
  554. elseif y == h-1 and x >= 10 and x <= 11 then
  555. radarScale = math.min(10000, radarScale + 100)
  556. drawControls()
  557. drawRadarDisplay()
  558. elseif y == h-1 and x >= w-10 and x <= w then
  559. displayState = "waypoints"
  560. monitor.clear()
  561. drawWaypointPage()
  562. print("Enter waypoint coordinates (x, y, z, color), color options: red, yellow, green, blue:")
  563. local input = read()
  564. local x, y, z, color = input:match("([^,]+),([^,]+),([^,]+),([^,]*)")
  565. local colorValue = colors.purple
  566. if color then
  567. local colorMap = {
  568. purple = colors.purple,
  569. red = colors.red,
  570. yellow = colors.yellow,
  571. green = colors.green,
  572. blue = colors.blue,
  573. white = colors.white,
  574. orange = colors.orange,
  575. lightBlue = colors.lightBlue,
  576. lime = colors.lime,
  577. pink = colors.pink,
  578. gray = colors.gray,
  579. lightGray = colors.lightGray,
  580. cyan = colors.cyan,
  581. purple = colors.purple,
  582. brown = colors.brown,
  583. black = colors.black
  584. }
  585. colorValue = colorMap[color] or colors.purple
  586. end
  587. if x and y and z then
  588. addWaypoint(tonumber(x), tonumber(y), tonumber(z), colorValue)
  589. end
  590. displayState = "radar"
  591. monitor.clear()
  592. drawControls()
  593. drawRadarDisplay()
  594. elseif y == 1 and x >= w-13 and x <= w then
  595. displayState = "adjustCannon"
  596. monitor.clear()
  597. drawCannonAdjustmentPage()
  598. elseif y == h-2 and x < 10 then
  599. cannonTarget.port5inch = { type = nil, side = "port", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  600. cannonTarget.starboard5inch = { type = nil, side = "starboard", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  601. cannonTarget.bow15inch = { type = nil, side = "bow", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  602. cannonTarget.stern15inch = { type = nil, side = "stern", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  603. monitor.setCursorPos(1, 1)
  604. monitor.clear()
  605. drawRadarDisplay()
  606. monitor.write("Locking stopped.")
  607. elseif y == h-3 and x < 15 then
  608. for _, entry in ipairs(pendingMissileLaunches) do
  609. local target = entry.target
  610. if target.type == "waypoint" then
  611. launchMissile("waypoint", nil, target, entry.type)
  612. else
  613. launchMissile("ship", target.id, nil, entry.type)
  614. end
  615. end
  616. pendingMissileLaunches = {} -- Clear the list
  617. modem.transmit(missileControlChannel, missileControlChannel, missileControls)
  618. monitor.setCursorPos(1, 1)
  619. monitor.write("All queued missiles launched!")
  620. else
  621. for _, target in ipairs(targetPositions) do
  622. if x == target.x and y == target.y then
  623. selectedTarget = target.data
  624. displayState = "targetInfo"
  625. displayTargetInfo(selectedTarget)
  626. break
  627. end
  628. end
  629. end
  630. elseif displayState == "targetInfo" then
  631. if y == h and x >= 1 and x <= 4 then
  632. -- Back button
  633. displayState = "radar"
  634. monitor.clear()
  635. drawRadarDisplay()
  636. elseif y == h-5 and x >= 1 and x <= 10 then
  637. -- Lock with port cannon
  638. if selectedTarget.type == "waypoint" then
  639. cannonTarget.port5inch.type = "waypoint"
  640. cannonTarget.port5inch.x = selectedTarget.x
  641. cannonTarget.port5inch.y = selectedTarget.y
  642. cannonTarget.port5inch.z = selectedTarget.z
  643. cannonTarget.port5inch.id = nil
  644. else
  645. cannonTarget.port5inch.id = selectedTarget.id
  646. cannonTarget.port5inch.type = "target"
  647. cannonTarget.port5inch.x = nil
  648. cannonTarget.port5inch.y = nil
  649. cannonTarget.port5inch.z = nil
  650. end
  651. displayState = "radar"
  652. monitor.clear()
  653. drawRadarDisplay()
  654. monitor.setCursorPos(1, 1)
  655. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  656. elseif y == h-4 and x >= 1 and x <= 15 then
  657. -- Lock with starboard cannon
  658. if selectedTarget.type == "waypoint" then
  659. cannonTarget.starboard5inch.type = "waypoint"
  660. cannonTarget.starboard5inch.x = selectedTarget.x
  661. cannonTarget.starboard5inch.y = selectedTarget.y
  662. cannonTarget.starboard5inch.z = selectedTarget.z
  663. cannonTarget.starboard5inch.id = nil
  664. else
  665. cannonTarget.starboard5inch.id = selectedTarget.id
  666. cannonTarget.starboard5inch.type = "target"
  667. cannonTarget.starboard5inch.x = nil
  668. cannonTarget.starboard5inch.y = nil
  669. cannonTarget.starboard5inch.z = nil
  670. end
  671. displayState = "radar"
  672. monitor.clear()
  673. drawRadarDisplay()
  674. monitor.setCursorPos(1, 1)
  675. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  676. elseif y == h-3 and x >= 1 and x <= 9 then
  677. -- Lock with bow cannon
  678. if selectedTarget.type == "waypoint" then
  679. cannonTarget.bow15inch.type = "waypoint"
  680. cannonTarget.bow15inch.x = selectedTarget.x
  681. cannonTarget.bow15inch.y = selectedTarget.y
  682. cannonTarget.bow15inch.z = selectedTarget.z
  683. cannonTarget.bow15inch.id = nil
  684. else
  685. cannonTarget.bow15inch.id = selectedTarget.id
  686. cannonTarget.bow15inch.type = "target"
  687. cannonTarget.bow15inch.x = nil
  688. cannonTarget.bow15inch.y = nil
  689. cannonTarget.bow15inch.z = nil
  690. end
  691. displayState = "radar"
  692. monitor.clear()
  693. drawRadarDisplay()
  694. monitor.setCursorPos(1, 1)
  695. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  696. elseif y == h-2 and x >= 1 and x <= 11 then
  697. -- Lock with stern cannon
  698. if selectedTarget.type == "waypoint" then
  699. cannonTarget.stern15inch.type = "waypoint"
  700. cannonTarget.stern15inch.x = selectedTarget.x
  701. cannonTarget.stern15inch.y = selectedTarget.y
  702. cannonTarget.stern15inch.z = selectedTarget.z
  703. cannonTarget.stern15inch.id = nil
  704. else
  705. cannonTarget.stern15inch.id = selectedTarget.id
  706. cannonTarget.stern15inch.type = "target"
  707. cannonTarget.stern15inch.x = nil
  708. cannonTarget.stern15inch.y = nil
  709. cannonTarget.stern15inch.z = nil
  710. end
  711. displayState = "radar"
  712. monitor.clear()
  713. drawRadarDisplay()
  714. monitor.setCursorPos(1, 1)
  715. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  716. -- AIM-220
  717. elseif y == h-5 and x >= 20 and x <=42 then
  718. table.insert(pendingMissileLaunches, {type = "AIM-220", target = selectedTarget})
  719. displayState = "radar"
  720. monitor.clear()
  721. drawRadarDisplay()
  722. monitor.setCursorPos(1, 1)
  723. monitor.write("Missile queued: AIM-220")
  724.  
  725. -- GBU-42
  726. elseif y == h-4 and x >= 20 and x <=41 then
  727. table.insert(pendingMissileLaunches, {type = "GBU-42", target = selectedTarget})
  728. displayState = "radar"
  729. monitor.clear()
  730. drawRadarDisplay()
  731. monitor.setCursorPos(1, 1)
  732. monitor.write("Missile queued: GBU-42")
  733.  
  734. -- thunderBolt
  735. elseif y == h-3 and x >= 20 and x <=46 then
  736. table.insert(pendingMissileLaunches, {type = "thunderBolt", target = selectedTarget})
  737. displayState = "radar"
  738. monitor.clear()
  739. drawRadarDisplay()
  740. monitor.setCursorPos(1, 1)
  741. monitor.write("Missile queued: thunderBolt")
  742. end
  743. elseif displayState == "adjustCannon" then
  744. -- Back button
  745. if y == h and x >= 1 and x <= 4 then
  746. displayState = "radar"
  747. monitor.clear()
  748. drawRadarDisplay()
  749. end
  750.  
  751. -- Detect pitch and yaw adjustment button presses
  752. local yOffset = 3
  753. local buttonWidth = 10
  754. for i, cannonName in ipairs({"port5inch", "starboard5inch", "bow15inch", "stern15inch"}) do
  755. if y == yOffset + 3 then
  756. if x >= 1 and x <= 10 then
  757. -- Increase pitch
  758. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust + 0.1
  759. elseif x >= 11 and x <= 20 then
  760. -- Decrease pitch
  761. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust - 0.1
  762. elseif x >= w - 30 and x <= w - 20 then
  763. -- Increase yaw
  764. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust + 0.1
  765. elseif x >= w - 19 and x <= w - 9 then
  766. -- Decrease yaw
  767. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust - 0.1
  768. end
  769. monitor.clear()
  770. drawCannonAdjustmentPage()
  771. end
  772. yOffset = yOffset + 6
  773. end
  774. end
  775. end
  776. end
  777.  
  778. local function modemMessage()
  779. while true do
  780. if modem then
  781. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  782. if channel == port5inchChannel then
  783. cannonData.port5inch.hitPosX = message.x
  784. cannonData.port5inch.hitPosY = message.y
  785. cannonData.port5inch.hitPosZ = message.z
  786. cannonData.port5inch.pitch = message.cannonPitch
  787. cannonData.port5inch.yaw = message.cannonYaw
  788. cannonData.port5inch.source = message.source
  789. elseif channel == starboard5inchChannel then
  790. cannonData.starboard5inch.hitPosX = message.x
  791. cannonData.starboard5inch.hitPosY = message.y
  792. cannonData.starboard5inch.hitPosZ = message.z
  793. cannonData.starboard5inch.pitch = message.cannonPitch
  794. cannonData.starboard5inch.yaw = message.cannonYaw
  795. cannonData.starboard5inch.source = message.source
  796. elseif channel == bow15inchChannel then
  797. cannonData.bow15inch.hitPosX = message.x
  798. cannonData.bow15inch.hitPosY = message.y
  799. cannonData.bow15inch.hitPosZ = message.z
  800. cannonData.bow15inch.pitch = message.cannonPitch
  801. cannonData.bow15inch.yaw = message.cannonYaw
  802. cannonData.bow15inch.source = message.source
  803. elseif channel == stern15inchChannel then
  804. cannonData.stern15inch.hitPosX = message.x
  805. cannonData.stern15inch.hitPosY = message.y
  806. cannonData.stern15inch.hitPosZ = message.z
  807. cannonData.stern15inch.pitch = message.cannonPitch
  808. cannonData.stern15inch.yaw = message.cannonYaw
  809. cannonData.stern15inch.source = message.source
  810. elseif channel == missileInfoChannel then
  811. immediateMissileInfo = message
  812. end
  813. else
  814. sleep()
  815. end
  816. end
  817. end
  818.  
  819. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  820. local radians = math.rad(angle)
  821. local u = projectileSpeed/20
  822. local part1 = u * math.cos(radians) / math.log(cd)
  823. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  824. local XR = part1 * part2
  825.  
  826. return XR
  827. end
  828.  
  829. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  830. local bestLowPitch = nil
  831. local bestHighPitch = nil
  832. local bestLowDistance = math.huge
  833. local bestHighDistance = math.huge
  834. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  835.  
  836. for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
  837. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  838. local distanceDifference = math.abs(calculatedRange - targetDistance)
  839.  
  840. -- Find the low-angle solution
  841. if pitch <= 30 then
  842. if distanceDifference < bestLowDistance then
  843. bestLowDistance = distanceDifference
  844. bestLowPitch = pitch
  845. end
  846. -- Find the high-angle solution
  847. elseif pitch > 30 then
  848. if distanceDifference < bestHighDistance then
  849. bestHighDistance = distanceDifference
  850. bestHighPitch = pitch
  851. end
  852. end
  853. end
  854.  
  855. -- If the target is within 400 blocks, prioritize the low-angle solution
  856. if targetDistance <= 830 then
  857. return bestLowPitch or bestLowPitch
  858. else
  859. return bestHighPitch or bestLowPitch
  860. end
  861. end
  862.  
  863. local function aimCannon(targetPos, targetVel, side)
  864. if targetPos and targetVel then
  865. -- Determine which channel to use based on side
  866. local channel
  867. if side == "port" then
  868. channel = port5inchChannel
  869. elseif side == "starboard" then
  870. channel = starboard5inchChannel
  871. elseif side == "bow" then
  872. channel = bow15inchChannel
  873. elseif side == "stern" then
  874. channel = stern15inchChannel
  875. end
  876. targetInfo = {targetPos = targetPos, targetVel = targetVel}
  877. print("channel: "..channel)
  878. print(textutils.serialize(targetInfo))
  879. --yaw adjustment
  880. if modem and targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  881. modem.transmit(channel,0,targetInfo)
  882. end
  883. end
  884. end
  885.  
  886. local function aimPortCannonContinuous()
  887. while true do
  888. local cannon = cannonTarget.port5inch
  889. if cannon.type then
  890. local target
  891. monitor.setCursorPos(1,1)
  892. if cannon.type=="waypoint" then
  893. monitor.write("Port waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  894. else
  895. monitor.write("Port ship locked: "..cannon.id)
  896. end
  897. if cannon.type == "waypoint" then
  898. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  899. else -- handling target
  900. local scanResults = radar.scanForShips(9999)
  901. for _, object in ipairs(scanResults) do
  902. if object.id == cannon.id then
  903. target = object
  904. break
  905. end
  906. end
  907. end
  908.  
  909. if target and target.pos then
  910. aimCannon(target.pos, target.velocity, "port")
  911. end
  912. else
  913.  
  914. end
  915. sleep() -- Adjust sleep duration based on required reaction time
  916. end
  917. end
  918.  
  919. local function aimStarboardCannonContinuous()
  920. while true do
  921. local cannon = cannonTarget.starboard5inch
  922. if cannon.type then
  923. local target
  924. monitor.setCursorPos(1,2)
  925. if cannon.type=="waypoint" then
  926. monitor.write("Starboard waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  927. else
  928. monitor.write("Starboard ship locked: "..cannon.id)
  929. end
  930. if cannon.type == "waypoint" then
  931. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  932. else -- handling target
  933. local scanResults = radar.scanForShips(9999)
  934. for _, object in ipairs(scanResults) do
  935. if object.id == cannon.id then
  936. target = object
  937. break
  938. end
  939. end
  940. end
  941.  
  942. if target and target.pos then
  943. aimCannon(target.pos, target.velocity, "starboard")
  944. end
  945. end
  946. sleep()
  947. end
  948. end
  949.  
  950. local function aimBowCannonContinuous()
  951. while true do
  952. local cannon = cannonTarget.bow15inch
  953. if cannon.type then
  954. local target
  955. monitor.setCursorPos(1,3)
  956. if cannon.type=="waypoint" then
  957. monitor.write("Bow waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  958. else
  959. monitor.write("Bow ship locked: "..cannon.id)
  960. end
  961. if cannon.type == "waypoint" then
  962. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  963. else -- handling target
  964. local scanResults = radar.scanForShips(9999)
  965. for _, object in ipairs(scanResults) do
  966. if object.id == cannon.id then
  967. target = object
  968. break
  969. end
  970. end
  971. end
  972.  
  973. if target and target.pos then
  974. aimCannon(target.pos, target.velocity, "bow")
  975. end
  976. end
  977. sleep()
  978. end
  979. end
  980.  
  981. local function aimSternCannonContinuous()
  982. while true do
  983. local cannon = cannonTarget.stern15inch
  984. if cannon.type then
  985. local target
  986. monitor.setCursorPos(1,4)
  987. if cannon.type=="waypoint" then
  988. monitor.write("Stern waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  989. else
  990. monitor.write("Stern ship locked: "..cannon.id)
  991. end
  992. if cannon.type == "waypoint" then
  993. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  994. else -- handling target
  995. local scanResults = radar.scanForShips(9999)
  996. for _, object in ipairs(scanResults) do
  997. if object.id == cannon.id then
  998. target = object
  999. break
  1000. end
  1001. end
  1002. end
  1003.  
  1004. if target and target.pos then
  1005. aimCannon(target.pos, target.velocity,"stern")
  1006. end
  1007. end
  1008. sleep()
  1009. end
  1010. end
  1011.  
  1012. local function calculateSpeed(pos1, pos2, deltaTime)
  1013. if pos1 and pos2 then
  1014. local dx = pos2[1] - pos1[1]
  1015. local dy = pos2[2] - pos1[2]
  1016. local dz = pos2[3] - pos1[3]
  1017. return math.sqrt(dx^2 + dy^2 + dz^2) / deltaTime
  1018. end
  1019. return 0
  1020. end
  1021.  
  1022. local function missileWarning()
  1023. local previousPositions = {}
  1024. while true do
  1025. local objectList = radar.scanForEntities(600)
  1026. -- Initial scan to capture positions
  1027. local initialPositions = {}
  1028. local shipPos = ship.getWorldspacePosition()
  1029. for _, object in ipairs(objectList) do
  1030. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  1031. if object.pos then
  1032. initialPositions[object.entity_type .. table.concat(object.pos, ":")] = object.pos
  1033. print("missile detected")
  1034. end
  1035. end
  1036. end
  1037.  
  1038. sleep(0.1) -- Short delay to measure displacement
  1039.  
  1040. objectList = radar.scanForEntities(600) -- Second scan to calculate speed
  1041. local currentPositions = {}
  1042. for _, object in ipairs(objectList) do
  1043. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  1044. if object.pos then
  1045. print("Missile detected for second time")
  1046. local key = object.entity_type .. table.concat(object.pos, ":")
  1047. currentPositions[key] = object.pos
  1048. local previousPos = initialPositions[key]
  1049. previousPos = nil
  1050. if previousPos then
  1051. local speed = calculateSpeed(previousPos, object.pos, 0.1)
  1052. print(textutils.serialize(previousPos))
  1053. print(textutils.serialize(object.pos))
  1054. print("speed: "..speed)
  1055. if speed > 5 then -- Check if speed exceeds threshold
  1056. local objX, objY, objZ = object.pos[1], object.pos[2], object.pos[3]
  1057. local missileVector = {x = objX - previousPos[1], y = objY - previousPos[2], z = objZ - previousPos[3]}
  1058. local toPlayerVector = {x = shipPos.x - objX, y = shipPos.y - objY, z = shipPos.z - objZ}
  1059. local dotProduct = missileVector.x * toPlayerVector.x + missileVector.y * toPlayerVector.y + missileVector.z * toPlayerVector.z
  1060. print("Missile with speed > 10 detected")
  1061. if dotProduct then -- Missile is moving towards the player
  1062. print("Missile coming for player")
  1063. local angle = math.deg(math.atan2(missileVector.z, missileVector.x) - math.atan2(toPlayerVector.z, toPlayerVector.x))
  1064. local direction = ((angle + 360) % 360) / 30
  1065. local clockDirection = math.floor(direction + 1)
  1066. if clockDirection == 0 then clockDirection = 12 end
  1067. monitor.setCursorPos(0,6)
  1068. monitor.write("Missile incoming from " .. clockDirection .. " o'clock")
  1069. if speaker then
  1070. speaker.playSound("entity.lightning_bolt.thunder", 1.0)
  1071. end
  1072. end
  1073. end
  1074. end
  1075. end
  1076. end
  1077. end
  1078.  
  1079. previousPositions = currentPositions
  1080. sleep() -- Delay before the next round of scanning
  1081. end
  1082. end
  1083.  
  1084. local function broadCastWaypoint()
  1085. while true do
  1086. modem.transmit(waypointChannel,0,waypoints)
  1087. sleep(1)
  1088. end
  1089. end
  1090.  
  1091. local function controlsSending()
  1092. while true do
  1093. --modem.transmit(missileControlChannel,missileControlChannel,missileControls)
  1094. sleep()
  1095. end
  1096. end
  1097.  
  1098. local function missileListBuilding()
  1099. while true do
  1100. if immediateMissileInfo and immediateMissileInfo.id and immediateMissileInfo.type then
  1101. local foundMissile = false
  1102.  
  1103. -- Update the existing missile state instead of adding duplicates
  1104. for _, missileInfo in ipairs(AIMInfoList) do
  1105. if missileInfo.id == immediateMissileInfo.id then
  1106. missileInfo.launchState = immediateMissileInfo.launchState
  1107. foundMissile = true
  1108. break
  1109. end
  1110. end
  1111. for _, missileInfo in ipairs(GBUInfoList) do
  1112. if missileInfo.id == immediateMissileInfo.id then
  1113. missileInfo.launchState = immediateMissileInfo.launchState
  1114. foundMissile = true
  1115. break
  1116. end
  1117. end
  1118. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1119. if missileInfo.id == immediateMissileInfo.id then
  1120. missileInfo.launchState = immediateMissileInfo.launchState
  1121. foundMissile = true
  1122. break
  1123. end
  1124. end
  1125.  
  1126. -- If it's a new missile, add it to the list
  1127. if not foundMissile then
  1128. if immediateMissileInfo.type == "AIM-220" then
  1129. table.insert(AIMInfoList, immediateMissileInfo)
  1130. elseif immediateMissileInfo.type == "GBU-42" then
  1131. table.insert(GBUInfoList, immediateMissileInfo)
  1132. elseif immediateMissileInfo.type == "thunderBolt" then
  1133. table.insert(thunderBoltInfoList, immediateMissileInfo)
  1134. end
  1135. end
  1136. end
  1137.  
  1138. -- Remove missiles that have launched
  1139. for i = #AIMInfoList, 1, -1 do
  1140. if AIMInfoList[i].launchState == true then
  1141. table.remove(AIMInfoList, i)
  1142. end
  1143. end
  1144. for i = #GBUInfoList, 1, -1 do
  1145. if GBUInfoList[i].launchState == true then
  1146. table.remove(GBUInfoList, i)
  1147. end
  1148. end
  1149. for i = #thunderBoltInfoList, 1, -1 do
  1150. if thunderBoltInfoList[i].launchState == true then
  1151. table.remove(thunderBoltInfoList, i)
  1152. end
  1153. end
  1154.  
  1155. -- Count unlaunched missiles
  1156. AIMCount, GBUCount, thunderboltCount = 0, 0, 0
  1157. for _, missileInfo in ipairs(AIMInfoList) do
  1158. if not missileInfo.launchState then
  1159. AIMCount = AIMCount + 1
  1160. end
  1161. end
  1162. for _, missileInfo in ipairs(GBUInfoList) do
  1163. if not missileInfo.launchState then
  1164. GBUCount = GBUCount + 1
  1165. end
  1166. end
  1167. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1168. if not missileInfo.launchState then
  1169. thunderboltCount = thunderboltCount + 1
  1170. end
  1171. end
  1172. sleep()
  1173. end
  1174. end
  1175.  
  1176. local function printOutput()
  1177. while true do
  1178. --print(textutils.serialize(missileControls))
  1179. --print(textutils.serialize(AIMInfoList))
  1180. --print(textutils.serialize(GBUInfoList))
  1181. --print(textutils.serialize(immediateMissileInfo))
  1182. --print(missileInfoChannel)
  1183. --print(textutils.serialize(thunderBoltInfoList))
  1184. sleep()
  1185. end
  1186. end
  1187.  
  1188. parallel.waitForAny(
  1189. function()
  1190. -- Main loop
  1191. while true do
  1192. if displayState == "radar" then
  1193. monitor.clear()
  1194. monitor.setTextScale(0.5)
  1195. drawRadarDisplay()
  1196. drawControls()
  1197. elseif displayState == "targetInfo" and selectedTarget then
  1198. displayTargetInfo(selectedTarget)
  1199. elseif displayState == "adjustCannon" then
  1200. monitor.clear()
  1201. drawCannonAdjustmentPage()
  1202. end
  1203. sleep(0.1)
  1204. -- Adjust this duration if needed
  1205. end
  1206. end,
  1207. handleTouch,
  1208. modemMessage,
  1209. aimPortCannonContinuous,
  1210. aimStarboardCannonContinuous,
  1211. aimBowCannonContinuous,
  1212. aimSternCannonContinuous,
  1213. missileWarning,
  1214. broadCastWaypoint,
  1215. missileListBuilding,
  1216. controlsSending,
  1217. printOutput
  1218. )
Advertisement
Add Comment
Please, Sign In to add comment