MegiRicky

NGDWEVRGen5_Air

Jul 26th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.44 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local modem = peripheral.wrap("right")
  3. local holo = peripheral.find("hologram")
  4.  
  5. local cannonSourceChannel = 701
  6. local controlChannel = 500
  7.  
  8. local function askUser(prompt, defaultValue)
  9. print(prompt .. " (default: " .. defaultValue .. ")")
  10. local input = io.read()
  11. if input == "" then
  12. return defaultValue
  13. else
  14. return input
  15. end
  16. end
  17.  
  18. local screenWidth, screenHeight, fov, GUIscale
  19. local sourceX, sourceY, sourceZ = 0,0,0
  20. local source = {}
  21. local userID
  22. local friendlyIDs
  23. local friendlyIDFile = "friendly_ids.txt"
  24. local widthMagic = 1.125
  25. local projectionDistance = 100
  26. local currentPitch, currentYaw = 0,0
  27. local lockedTarget = nil
  28. local elements = {}
  29. local pInfo = {}
  30. local cannonHitPos = {}
  31. local pitchAdjustments = "1"
  32. local lastYawUpdateTime = 0
  33. local lastPitchUpdateTime = 0
  34. local updateInterval = 0.05
  35. local projectileSpeed = 240
  36.  
  37. local maxRayDistance = 500
  38.  
  39. local yawErrorSum = 0
  40. local lastYawError = 0
  41. local pitchErrorSum = 0
  42. local lastPitchError = 0
  43. local lastTime = os.clock()
  44.  
  45. local missileId = 1
  46. local missileLaunched = {}
  47. local controls = {
  48. {cannonControlMode = "manual"},
  49. fireMissile = {}
  50. }
  51. local LaunchedMissiles = {}
  52. local missileControls = {
  53. fireMissile = {}
  54. }
  55. local immediateMissileInfo = {}
  56. local AIMInfoList,GBUInfoList,thunderBoltInfoList = {},{},{}
  57. local AIMCount,GBUCount,thunderboltCount = 0,0,0
  58. local weaponChoosen = "AIM-220"
  59. local missileCoolDown = 20
  60.  
  61. local waypoints = {}
  62.  
  63. -- Configuration file paths
  64. local function readConfig(filename)
  65. if not fs.exists(filename) then
  66. print("Configuration file '" .. filename .. "' does not exist. Creating it.")
  67. file = fs.open(filename, "w")
  68.  
  69. resolution = askUser("Input your IRL monitor's resolution eg.(1920,1080)", "3440,1440")
  70. file.writeLine("resolution=" .. resolution)
  71.  
  72. fov = askUser("Input your FOV eg.(70) Actually it only works with 70 FOV", "70")
  73. file.writeLine("fov=" .. fov)
  74.  
  75. userID = askUser("Input your in game ID(name)", "MegiRicky")
  76. file.writeLine("userID=" .. userID)
  77.  
  78. cannonHitPosChannel = askUser("Input the cannon data channel number(For guiding cannon, NGDWEVROnboard required)", "700")
  79. file.writeLine("cannonHitPosChannel=" .. cannonHitPosChannel)
  80.  
  81. missileControlChannel = askUser("Input the missile launch channel","1400")
  82. file.writeLine("missileControlChannel="..missileControlChannel)
  83.  
  84. controlChannel = askUser("Input the control channel", "1420")
  85. file.writeLine("controlChannel=" .. controlChannel)
  86.  
  87. pitchCompensation = askUser("Input the pitchCompensation.(for when the plane and the computer is not on the same ship)","48.24")
  88. file.writeLine("pitchCompensation="..pitchCompensation)
  89.  
  90. file.close()
  91. end
  92. sleep(0.2)
  93. local file = fs.open(filename, "r")
  94. local config = {}
  95. local line = file.readLine()
  96. while line do
  97. local key, value = line:match("^(%w+)=(.*)$")
  98. config[key] = value
  99. line = file.readLine()
  100. end
  101. file.close()
  102. return config
  103. end
  104.  
  105. -- Function to edit configuration
  106. local function editConfig(filename)
  107. print("Do you want to edit the config? (yes/no),press enter to skip")
  108. local editChoice = io.read()
  109. if editChoice == "" then
  110. editChoice = "no"
  111. end
  112. if editChoice:lower() == "yes" then
  113. if fs.exists(filename) then
  114. fs.delete(filename)
  115. end
  116. return readConfig(filename)
  117. else
  118. return nil
  119. end
  120. end
  121.  
  122. -- Function to select or create config
  123. local function loadOrCreateConfig()
  124. print("Which player's config do you want to load?")
  125. local selectedID = io.read()
  126.  
  127. local configFile = selectedID .. "config.txt"
  128.  
  129. if fs.exists(configFile) then
  130. print("Config file for " .. selectedID .. " exists. Loading it.")
  131. local config = readConfig(configFile)
  132.  
  133. -- Ask if user wants to edit config
  134. editConfig(configFile)
  135.  
  136. return config
  137. else
  138. print("No config file found for " .. selectedID .. ". Creating a new one.")
  139. return readConfig(configFile)
  140. end
  141. end
  142.  
  143. -- Function to read friendly IDs from a file
  144. local function readFriendlyIDs(filename)
  145. if not fs.exists(filename) then
  146. print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
  147. local file = fs.open(filename, "w")
  148. print("Input friendly IDs separated by commas:")
  149. local ids = io.read()
  150. file.write(ids)
  151. file.close()
  152. end
  153.  
  154. local file = fs.open(filename, "r")
  155. local friendlyIDs = {}
  156. if file then
  157. local line = file.readLine()
  158. while line do
  159. for id in string.gmatch(line, "%d+") do
  160. table.insert(friendlyIDs, tonumber(id))
  161. end
  162. line = file.readLine()
  163. end
  164. file.close()
  165. end
  166. return friendlyIDs
  167. end
  168.  
  169. local function sign(x)
  170. return x > 0 and 1 or x < 0 and -1 or 0
  171. end
  172.  
  173. -- Load configuration
  174.  
  175. local config = loadOrCreateConfig()
  176. sleep(0.5)
  177. print(textutils.serialize(config))
  178. screenWidth, screenHeight = config.resolution:match("(%d+),(%d+)")
  179. screenWidth = tonumber(screenWidth)
  180. screenHeight = tonumber(screenHeight)
  181. fov = tonumber(config.fov)
  182. GUIscale = 4
  183. local mode = tonumber(config.mode)
  184. userID = config.userID
  185. friendlyIDs = readFriendlyIDs(friendlyIDFile)
  186. local magicNumber = GUIscale/4
  187. local cannonHitPosChannel = tonumber(config.cannonHitPosChannel)
  188. local controlChannel = tonumber(config.controlChannel)
  189. local missileControlChannel = tonumber(config.missileControlChannel)
  190. local missileInfoChannel = missileControlChannel + 10
  191. local waypointChannel = controlChannel + 1
  192.  
  193. if modem then
  194. modem.open(cannonHitPosChannel)
  195. modem.open(controlChannel)
  196. modem.open(waypointChannel)
  197. modem.open(missileControlChannel)
  198. modem.open(missileInfoChannel)
  199. end
  200.  
  201. local pos = ship.getWorldspacePosition()
  202. sourceX, sourceY, sourceZ = pos.x, pos.y, pos.z
  203.  
  204. print("Source X: " .. sourceX)
  205. print("Source Y: " .. sourceY)
  206. print("Source Z: " .. sourceZ)
  207. print("Resolution: " .. screenWidth .. ", " .. screenHeight)
  208. if screenWidth == 3440 and screenHeight == 1440 then widthMagic = 1.125 else widthMagic = 0.92 end
  209. holo.Resize(screenWidth/4,screenHeight/4)
  210. local screenSizeScale = 4
  211. holo.SetScale(0,0)
  212.  
  213.  
  214. local function isFriendly(id)
  215. for _, friendlyID in ipairs(friendlyIDs) do
  216. if id == friendlyID then
  217. return true
  218. end
  219. end
  220. return false
  221. end
  222.  
  223. local function normalizeVector(v)
  224. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  225. if length == 0 then
  226. return {0, 0, 0}
  227. end
  228. return {v[1] / length, v[2] / length, v[3] / length}
  229. end
  230.  
  231. -- Normalize the rotation matrix
  232. local function normalizeRotationMatrix(rotMatrix)
  233. local normalizedMatrix = {}
  234. for i = 1, #rotMatrix do
  235. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  236. end
  237. return normalizedMatrix
  238. end
  239.  
  240. -- Get the pitch of the ship
  241. local function getPitch()
  242. local rotMatrix = ship.getTransformationMatrix()
  243. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  244. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  245. end
  246.  
  247. -- Get the yaw of the ship
  248. local function getYaw()
  249. local rotMatrix = ship.getTransformationMatrix()
  250. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  251. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  252. end
  253.  
  254. -- Get the roll of the ship
  255. local function getRoll()
  256. local rotMatrix = ship.getTransformationMatrix()
  257. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  258. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  259. end
  260.  
  261. -- Function to project 3D coordinates to 2D screen coordinates
  262. local function projectToScreen(x, y, z, userPos, lookVector)
  263. local dx = x - userPos[1]
  264. local dy = -(y - userPos[2])
  265. local dz = z - userPos[3]
  266.  
  267. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  268. local playerYaw = math.deg(math.atan2(lz, lx))
  269. local playerPitch = math.deg(math.asin(-ly))
  270. playerYaw = playerYaw + 90
  271. if playerYaw < 0 then
  272. playerYaw = playerYaw + 360
  273. end
  274.  
  275. -- Calculate yaw difference
  276. local targetYaw = math.deg(math.atan2(dz, dx)) + 90
  277. if targetYaw < 0 then
  278. targetYaw = targetYaw + 360
  279. end
  280.  
  281. local yawDiff = targetYaw - playerYaw
  282. if yawDiff > 180 then
  283. yawDiff = yawDiff - 360
  284. elseif yawDiff < -180 then
  285. yawDiff = yawDiff + 360
  286. end
  287.  
  288. if math.abs(yawDiff) > 90 then
  289. return nil, nil
  290. end
  291.  
  292. -- Calculate pitch difference
  293. local distXY = math.sqrt(dx * dx + dz * dz)
  294. local targetPitch = math.deg(math.atan2(dy, distXY))
  295. local pitchDiff = targetPitch - playerPitch
  296.  
  297. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  298. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  299.  
  300. local projectedX, projectedY
  301.  
  302. if yawDiff > 0 then
  303. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  304. else
  305. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  306. end
  307.  
  308. if pitchDiff > 0 then
  309. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  310. else
  311. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  312. end
  313.  
  314. projectedX = math.floor(projectedX / magicNumber)
  315. projectedY = math.floor(projectedY / magicNumber) + 20
  316.  
  317. return projectedX, projectedY
  318. end
  319.  
  320. --[[local graphics_3d = require("graphics_3d")
  321. local camera = graphics_3d.camera().create(70, screenWidth/screenSizeScale, screenHeight/screenSizeScale, 0.1, 1000)
  322.  
  323. -- Replace the original projection logic
  324. local function projectToScreen(x, y, z, userPos, lookVector)
  325. -- Update the camera position and orientation
  326. ccVectorLook = vector.new(lookVector[1],lookVector[2],lookVector[3])
  327. print(userPos[1],userPos[2],userPos[3])
  328. ccVectorPos = vector.new(userPos[1],userPos[2],userPos[3])
  329.  
  330. graphics_3d.camera().update(ccVectorPos, ccVectorLook)
  331. -- Project the 3D point onto the screen
  332. ccVectorTargetPos = vector.new(x,y,z)
  333.  
  334. local screenX, screenY = graphics_3d.project(ccVectorTargetPos, camera)
  335. screenX = math.abs(screenX)
  336.  
  337. return screenX, screenY
  338. end]]
  339.  
  340. -- Function to display pitch and yaw of a cannon
  341. local function angleToScreen(pitch, yaw, userPos, lookVector)
  342. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  343. local playerYaw = math.deg(math.atan2(lz, lx))
  344. local playerPitch = math.deg(math.asin(-ly))
  345. playerYaw = playerYaw + 90
  346. if playerYaw < 0 then
  347. playerYaw = playerYaw + 360
  348. end
  349.  
  350. -- Calculate yaw difference
  351. local yawDiff = yaw - playerYaw
  352. if yawDiff > 180 then
  353. yawDiff = yawDiff - 360
  354. elseif yawDiff < -180 then
  355. yawDiff = yawDiff + 360
  356. end
  357.  
  358. if math.abs(yawDiff) > 90 then
  359. return nil, nil
  360. end
  361.  
  362. -- Calculate pitch difference
  363. local pitchDiff = -pitch + playerPitch
  364.  
  365. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  366. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  367.  
  368. local projectedX, projectedY
  369.  
  370. if yawDiff > 0 then
  371. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  372. else
  373. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  374. end
  375.  
  376. if pitchDiff > 0 then
  377. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  378. else
  379. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  380. end
  381.  
  382. projectedX = math.floor(projectedX / magicNumber)
  383. projectedY = math.floor(projectedY / magicNumber) + 20
  384.  
  385. return projectedX, projectedY
  386. end
  387.  
  388. local function mouseAim(userPos, lookVector)
  389. lookTarget = {}
  390.  
  391. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  392. local playerYaw = math.deg(math.atan2(lz, lx))
  393. local playerPitch = math.deg(math.asin(-ly))
  394. playerYaw = playerYaw + 90
  395. if playerYaw < 0 then
  396. playerYaw = playerYaw + 360
  397. end
  398.  
  399. local yawRad = math.rad(playerYaw)
  400. local pitchRad = math.rad(playerPitch)
  401.  
  402. local dx = projectionDistance * math.cos(pitchRad) * math.sin(yawRad)
  403. local dz = -projectionDistance * math.cos(pitchRad) * math.cos(yawRad)
  404. local dy = -projectionDistance * math.sin(pitchRad)
  405.  
  406. local targetX = userPos[1] + dx
  407. local targetY = userPos[2] + dy
  408. local targetZ = userPos[3] + dz
  409. lookTarget = { x = targetX, y = targetY, z = targetZ }
  410. return lookTarget
  411. end
  412.  
  413. local function isWithinRenderDistance(projectedX, projectedY, centerX, centerY, maxdx, maxdy, maxd)
  414. local dx = projectedX - centerX / magicNumber
  415. local dy = projectedY - centerY / magicNumber
  416.  
  417. if dx < maxdx then
  418. if dy < maxdy then
  419. local distance = math.sqrt(dx * dx + dy * dy)
  420. if distance <= maxd then
  421. return true
  422. else
  423. return false
  424. end
  425. else
  426. return false
  427. end
  428. else
  429. return false
  430. end
  431. end
  432.  
  433. -- Function to check for overlap and adjust positions
  434. local function filterElements(elements)
  435. table.sort(elements, function(a, b) return a.mass > b.mass end) -- Sort by mass
  436. local filteredElements = {}
  437.  
  438. for i, element in ipairs(elements) do
  439. local overlap = false
  440. for _, filteredElement in ipairs(filteredElements) do
  441. local dx = math.abs(element.screenX - filteredElement.screenX)
  442. local dy = math.abs(element.screenY - filteredElement.screenY)
  443. if dx < 800 / GUIscale and dy < 800 / GUIscale then
  444. overlap = true
  445. element.renderInfo = false
  446. break
  447. end
  448. end
  449.  
  450. table.insert(filteredElements, element)
  451. end
  452.  
  453. return filteredElements
  454. end
  455.  
  456. local function drawEdgeIndicator(holo, screenX, screenY, screenWidth, screenHeight, radius)
  457. local centerX = screenWidth / 2
  458. local centerY = screenHeight / 2
  459.  
  460. -- Calculate angle and differences
  461. local dx = screenX - centerX
  462. local dy = screenY - centerY
  463. local distance = math.sqrt(dx * dx + dy * dy)
  464. local yawDiff = math.deg(math.atan2(dx, distance)) -- Yaw difference
  465. local pitchDiff = math.deg(math.atan2(dy, distance)) -- Pitch difference
  466.  
  467. -- Apply compensation for sensitivity differences
  468. if yawDiff > 0 then
  469. dy = dy * 2 -- Adjust sensitivity
  470. dx = dx * 2
  471. else
  472. dy = dy * 0.5 -- Adjust sensitivity
  473. dx = dx * 0.5
  474. end
  475.  
  476. if pitchDiff > 0 then
  477. dy = dy * 0.5 -- Adjust sensitivity
  478. dx = dx * 0.5
  479. else
  480. dy = dy * 2 -- Adjust sensitivity
  481. dx = dx * 2
  482. end
  483.  
  484. -- Normalize direction vector to get unit vector
  485. local normFactor = math.sqrt(dx * dx + dy * dy)
  486. local unitX = dx / normFactor
  487. local unitY = dy / normFactor
  488.  
  489. -- Calculate arrow position at the edge of the circle
  490. local edgeX = centerX + unitX * radius / 2
  491. local edgeY = centerY + unitY * radius / 2
  492.  
  493. -- Draw the arrow on the hologram
  494. holo.DrawLine(
  495. math.floor(edgeX) - 1, math.floor(edgeY) - 1,
  496. math.floor(edgeX) + 1, math.floor(edgeY) + 1,
  497. 0xFF00FFFF, 1 -- Cyan arrow for visibility
  498. )
  499. holo.DrawLine(
  500. math.floor(edgeX) + 1, math.floor(edgeY) - 1,
  501. math.floor(edgeX) - 1, math.floor(edgeY) + 1,
  502. 0xFF00FFFF, 1 -- Cyan arrow for visibility
  503. )
  504. end
  505.  
  506. -- Function to render elements
  507. local function renderElements(elements, cannonHitPos, pInfo, lockedTarget)
  508. if not holo then return end -- Ensure hologram peripheral is available
  509.  
  510. -- Set the background to transparent
  511. holo.SetClearColor(0x00000000) -- Fully transparent (RGBA: 0,0,0,0)
  512.  
  513. local circleRadius = math.min(screenWidth, screenHeight) / 4
  514.  
  515. -- Iterate through detected objects and render them on the hologram
  516. for _, element in ipairs(elements) do
  517. local color = element.color
  518. local color = element.color
  519. if lockedTarget and element.id == lockedTarget.id then
  520. color = 0xFF0000FF -- Highlight locked target in red
  521. local screenWidth = screenWidth / screenSizeScale
  522. local centerX = screenWidth / 2
  523. local altitudeBoxX = centerX + screenWidth / 6
  524. -- Define the target information to display
  525. local targetInfo = {
  526. "TARGET LOCKED",
  527. "ID: " .. tostring(element.id),
  528. "Speed: " .. tostring(element.speed) .. " m/s",
  529. "Pos: (" ..
  530. string.format("%.0f", element.pos.x) .. "," ..
  531. string.format("%.0f", element.pos.y) .. "," ..
  532. string.format("%.0f", element.pos.z) .. ")",
  533. "Mass: " .. tostring(element.mass) .. " kg",
  534. "Dist: " .. tostring(element.distance) .. " m"
  535. }
  536.  
  537. local missileLaunchedAtTarget = false
  538. if type(controls.fireMissile) == "table" then
  539. for _, missileData in pairs(controls.fireMissile) do
  540. if missileData.type == "ship" and missileData.id == lockedTarget.id then
  541. missileLaunchedAtTarget = true
  542. break
  543. end
  544. end
  545. end
  546.  
  547. -- Add missile launch status to the target info
  548. if missileLaunchedAtTarget then
  549. table.insert(targetInfo, "Missile Launched")
  550. end
  551.  
  552. -- Display each line of the target information
  553. for i, line in ipairs(targetInfo) do
  554. holo.Text(altitudeBoxX, 20 + (i * 15), line, 0xFF000088, 1) -- White text
  555. end
  556. end
  557.  
  558. -- Map screen coordinates to hologram coordinates
  559. local holoX = math.floor(element.screenX / 4) -- Scale down for better fit
  560. local holoY = math.floor(element.screenY / 4) -- Adjust scaling as needed
  561.  
  562. -- Define the size of the box for the target
  563. local boxSize = 6 -- Increase the box size for better visibility
  564.  
  565. -- Ensure the coordinates fit within the hologram display bounds
  566. if holoX >= 0 and holoX < screenWidth / 4 and holoY >= 0 and holoY < screenHeight / 4 and element.renderBox then
  567. -- Draw a bounding box around the detected object
  568. holo.DrawLine(holoX - boxSize, holoY - boxSize, holoX + boxSize, holoY - boxSize, color, 1) -- Top
  569. holo.DrawLine(holoX - boxSize, holoY + boxSize, holoX + boxSize, holoY + boxSize, color, 1) -- Bottom
  570. holo.DrawLine(holoX - boxSize, holoY - boxSize, holoX - boxSize, holoY + boxSize, color, 1) -- Left
  571. holo.DrawLine(holoX + boxSize, holoY - boxSize, holoX + boxSize, holoY + boxSize, color, 1) -- Right
  572.  
  573. if element.renderInfo then
  574. -- Display object ID & distance on the hologram (smaller text)
  575. holo.Text(holoX + boxSize + 2, holoY - boxSize, tostring(element.id), color, 1) -- Target ID
  576. holo.Text(holoX + boxSize + 2, holoY + boxSize, tostring(element.distance) .. "m", color, 1) -- Distance
  577. holo.Text(holoX + boxSize + 2, holoY + boxSize * 3, tostring(element.speed) .. "m/s", color, 1) -- Speed
  578. end
  579. else
  580. -- Draw an edge indicator for objects outside the display bounds
  581. --drawEdgeIndicator(holo, element.screenX, element.screenY, screenWidth / 4, screenHeight / 4, circleRadius)
  582. end
  583. end
  584. end
  585.  
  586. local function lockHighestThreatTarget(elements, centerX, centerY, radius)
  587. local closestTarget = nil
  588. local closestDistance = math.huge -- Initialize to a large number
  589.  
  590. for _, element in ipairs(elements) do
  591. if element.id then
  592. local dx = element.screenX - centerX / magicNumber
  593. local dy = element.screenY - centerY / magicNumber
  594. local distance = math.sqrt(dx * dx + dy * dy)
  595.  
  596. -- Check if the target is within the locking radius on screen
  597. if distance <= radius / GUIscale then
  598. -- If this target is closer to the center than the current closest target, update
  599. if distance < closestDistance then
  600. closestTarget = element
  601. closestDistance = distance
  602. end
  603. end
  604. end
  605. end
  606.  
  607. return closestTarget
  608. end
  609.  
  610. local function modemMessage()
  611. while true do
  612. if modem then
  613. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  614. if channel == cannonHitPosChannel then
  615. cannonHitPos = message
  616. elseif channel == controlChannel then
  617. controls = message
  618. elseif channel == waypointChannel then
  619. waypoints = message
  620. elseif channel == missileInfoChannel then
  621. immediateMissileInfo = message
  622. end
  623. else
  624. sleep()
  625. end
  626. end
  627. end
  628.  
  629. local function getAngularVelocity(currentYaw, currentTime)
  630. if lastYaw and lastYawTime then
  631. local deltaTime = currentTime - lastYawTime
  632. local deltaYaw = (currentYaw - lastYaw + math.pi) % (2 * math.pi) - math.pi
  633. return deltaYaw / deltaTime -- Radians per second
  634. end
  635. return 0
  636. end
  637.  
  638. local function launchMissile(targetType,targetId,targetPos)
  639. if type(controls.fireMissile) ~= "table" then
  640. controls.fireMissile = {}
  641. end
  642.  
  643. local currentMissileId = missileId
  644. -- Assign a table to this missile's ID in fireMissile, marking it as launched
  645. if targetType == "ship" then
  646. controls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  647. else
  648. controls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  649. end
  650. missileLaunched[currentMissileId] = true -- Mark this missile as launched
  651. print("Launching missile:", currentMissileId)
  652.  
  653. missileId = missileId + 1 -- Increment missile ID for next launch
  654. end
  655.  
  656. local function targetLockAndAim()
  657. while true do
  658. source = cannonHitPos[5]
  659. cannonChannel = 0
  660. source = ship.getWorldspacePosition()
  661. source.y = source.y + 1
  662. if pInfo.nickname then
  663. lockedTarget = lockHighestThreatTarget(elements, screenWidth / 2, screenHeight / 2, 1000)
  664. if lockedTarget then
  665. lockedTargetScan = radar.scanForShips(10000)
  666. for _, object in ipairs(lockedTargetScan) do
  667. if object.id == lockedTarget.id then
  668. -- Update the locked target information
  669. lockedTarget.pos = object.pos
  670. lockedTarget.velocity = object.velocity
  671. break
  672. end
  673. end
  674. if lockedTarget.pos then
  675. local targetInfo = {targetPos = lockedTarget.pos, targetVel = lockedTarget.velocity}
  676. modem.transmit(cannonHitPosChannel,0,targetInfo)
  677. end
  678. else
  679. lookTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  680. local targetInfo = {targetPos = lookTarget, targetVel = {x = 0,y = 0,z = 0}}
  681. modem.transmit(cannonHitPosChannel,0,targetInfo)
  682. end
  683. local launch = redstone.getInput("front") or redstone.getInput("back") or redstone.getInput("left") or redstone.getInput("right")
  684.  
  685. if lockedTarget and lockedTarget.id and launch then
  686. if type(lockedTarget.id) == "number" then
  687. launchMissile("ship",lockedTarget.id,lockedTarget.pos) --target is a ship
  688. else
  689. launchMissile("waypoint",lockedTarget.id,lockedTarget.pos)
  690. modem.transmit(missileControlChannel, missileControlChannel, controls)
  691. end
  692. end
  693. end
  694. sleep()
  695. end
  696. end
  697.  
  698. local function drawPitchLadder()
  699. local pitch = getPitch()
  700. local roll = -getRoll()
  701. local screenWidth,screenHeight = screenWidth/4, screenHeight/4
  702.  
  703. local centerX = screenWidth / 2
  704. local centerY = screenHeight / 2
  705.  
  706. local pitchStep = 10 -- Degrees per pitch line
  707. local maxPitch = 90 -- Maximum pitch levels to render (both positive and negative)
  708. local lineLength = screenWidth / 10 -- Length of the angled pitch lines
  709.  
  710. -- Loop through each pitch level to render the ladder
  711. for i = -maxPitch, maxPitch, pitchStep do
  712. local pitchOffset = math.tan(math.rad(i - (math.deg(pitch) + config.pitchCompensation))) * (screenHeight / 2)
  713. local ladderY = centerY - pitchOffset
  714.  
  715. -- Rotate the pitch lines based on roll
  716. local rollRadians = roll * 0.9
  717. local halfLineLength = lineLength / 2
  718. local startX = centerX - halfLineLength * math.cos(rollRadians)
  719. local startY = ladderY - halfLineLength * math.sin(rollRadians)
  720. local endX = centerX + halfLineLength * math.cos(rollRadians)
  721. local endY = ladderY + halfLineLength * math.sin(rollRadians)
  722.  
  723. -- Draw the pitch line
  724. if ladderY >= 1 and ladderY <= screenHeight then
  725. holo.DrawLine(
  726. math.floor(startX), math.floor(startY),
  727. math.floor(endX), math.floor(endY),
  728. 0x00FF0055, -- Green color (RGBA format)
  729. 1 -- Mode: Blending
  730. )
  731.  
  732. -- Draw pitch labels at both ends of the line
  733. local labelColor = 0x00FF0088 -- Green color for labels
  734. if i == 0 then
  735. holo.Text(math.floor(startX - 10), math.floor(startY - 8), "00", labelColor, 0) -- Start label
  736. holo.Text(math.floor(endX), math.floor(endY - 8), "00", labelColor,0) -- End label
  737. else
  738. holo.Text(math.floor(startX - 10), math.floor(startY - 8), tostring(math.abs(i)), labelColor, 0) -- Start label
  739. holo.Text(math.floor(endX), math.floor(endY - 8), tostring(math.abs(i)), labelColor, 0) -- End label
  740. end
  741. end
  742. end
  743. end
  744.  
  745. local function drawFlightInfo()
  746. local pitch = getPitch()
  747. local roll = -getRoll()
  748. local adjustedPitch = math.deg(pitch + math.rad(config.pitchCompensation))
  749.  
  750. local screenWidth = screenWidth / 4
  751. local screenHeight = screenHeight / 4
  752.  
  753. local centerX = screenWidth / 2
  754. local centerY = screenHeight / 2
  755.  
  756. -- Get the ship's position and velocity
  757. local shipPos = ship.getWorldspacePosition()
  758. local velocity = ship.getVelocity()
  759.  
  760. -- Calculate the speed in m/s
  761. local speed = math.floor(math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2))
  762.  
  763. -- Calculate altitude (assuming altitude is based on the Y position)
  764. local altitude = math.floor(shipPos.y)
  765.  
  766. -- Define positions for speed and altitude boxes
  767. local boxWidth = 40 -- Increased box width for better fit
  768. local boxHeight = 14 -- Increased box height
  769. local boxPadding = 4
  770. local speedBoxX = centerX - screenWidth / 6 + boxPadding
  771. local altitudeBoxX = centerX + screenWidth / 6 - boxWidth - boxPadding
  772. local boxY = centerY - boxHeight - boxPadding - 30 -- Slightly higher than before
  773.  
  774. local labelColor = 0x00FF0088 -- Green color for labels
  775. -- Draw the speed box
  776. holo.DrawLine(speedBoxX, boxY, speedBoxX + boxWidth, boxY, labelColor, 1) -- Top
  777. holo.DrawLine(speedBoxX, boxY + boxHeight, speedBoxX + boxWidth, boxY + boxHeight, labelColor, 1) -- Bottom
  778. holo.DrawLine(speedBoxX, boxY, speedBoxX, boxY + boxHeight, labelColor, 1) -- Left
  779. holo.DrawLine(speedBoxX + boxWidth, boxY, speedBoxX + boxWidth, boxY + boxHeight, labelColor, 1) -- Right
  780. holo.Text(speedBoxX + 2, boxY, tostring(speed) .. "m/s", labelColor, 0) -- Adjusted text position
  781.  
  782. -- Draw the altitude box
  783. holo.DrawLine(altitudeBoxX, boxY, altitudeBoxX + boxWidth, boxY, labelColor, 1) -- Top
  784. holo.DrawLine(altitudeBoxX, boxY + boxHeight, altitudeBoxX + boxWidth, boxY + boxHeight, labelColor, 1) -- Bottom
  785. holo.DrawLine(altitudeBoxX, boxY, altitudeBoxX, boxY + boxHeight, labelColor, 1) -- Left
  786. holo.DrawLine(altitudeBoxX + boxWidth, boxY, altitudeBoxX + boxWidth, boxY + boxHeight, labelColor, 1) -- Right
  787. holo.Text(altitudeBoxX + 2, boxY, tostring(altitude) .. "m", labelColor, 0) -- Adjusted text position
  788.  
  789. -- Display weapon count
  790. local AIMcolor,GBUcolor,TBColor = labelColor,labelColor,labelColor
  791. if weaponChoosen == "AIM-220" then
  792. if missileCoolDown == 0 then
  793. AIMcolor = 0xFF9900FF
  794. else
  795. AIMcolor = 0xFF000088
  796. end
  797. elseif weaponChoosen == "GBU-42" then
  798. if missileCoolDown == 0 then
  799. GBUcolor = 0xFF9900FF
  800. else
  801. GBUcolor = 0xFF000088
  802. end
  803. elseif weaponChoosen == "thunderBolt" then
  804. if missileCoolDown == 0 then
  805. TBColor = 0xFF9900FF
  806. else
  807. TBColor = 0xFF000088
  808. end
  809. end
  810. holo.Text(speedBoxX + 2, 35, "AIM-220:" .. AIMCount, AIMcolor, 0)
  811. holo.Text(speedBoxX + 2, 50, "GBU-42:" .. GBUCount, GBUcolor, 0)
  812. holo.Text(speedBoxX + 2, 65, "TB:" .. thunderboltCount, TBColor, 0)
  813. end
  814.  
  815. local function drawCompass()
  816. local yaw = math.deg(getYaw())
  817. if yaw < 0 then
  818. yaw = yaw + 360 -- Ensure yaw is within 0-360
  819. end
  820.  
  821. local screenHeight = screenHeight / screenSizeScale
  822. local screenWidth = screenWidth / screenSizeScale
  823.  
  824. -- Define compass settings
  825. local compassWidth = screenWidth / 2 -- Width of the compass bar
  826. local compassHeight = 8 -- Height for tick marks
  827. local compassY = 10 -- Y position of the compass (top of the screen)
  828. local centerX = screenWidth / 2 -- Center of the screen
  829. local tickSpacing = compassWidth / 36 -- Spacing between ticks (10 degrees per tick)
  830. local labelColor = 0x00FF00FF -- Green color for labels
  831.  
  832. -- Render major directions (N, E, S, W) and tick marks
  833. for angle = -180, 180, 10 do
  834. local relativeAngle = (angle - yaw + 360) % 360 -- Adjust for yaw
  835. local normalizedAngle = relativeAngle > 180 and relativeAngle - 360 or relativeAngle -- Normalize to -180 to 180
  836.  
  837. local posX = math.floor(centerX + (normalizedAngle / 180) * (compassWidth / 2)) -- Map angle to screen
  838.  
  839. if posX >= 0 and posX <= screenWidth then
  840. if angle % 90 == 0 then
  841. -- Major directions (N, E, S, W)
  842. local dir = ({ [0] = "N", [90] = "E", [180] = "S", [-90] = "W", [-180] = "S" })[angle]
  843. holo.Text(posX - 3, compassY + 5, dir, labelColor, 0) -- Label the major directions
  844. holo.DrawLine(posX, compassY, posX, compassY + compassHeight, labelColor, 1) -- Long tick
  845. else
  846. -- Minor tick marks (every 10 degrees)
  847. local tickHeight = compassHeight / 2
  848. holo.DrawLine(posX, compassY, posX, compassY + tickHeight, labelColor, 1)
  849. end
  850. end
  851. end
  852.  
  853. local triangleSize = 3
  854. holo.DrawLine(centerX - triangleSize, compassY, centerX, compassY - compassHeight - 5, labelColor, 1) -- Left edge
  855. holo.DrawLine(centerX, compassY - compassHeight - 5, centerX + triangleSize, compassY, labelColor, 1) -- Right edge
  856. holo.DrawLine(centerX - triangleSize, compassY, centerX + triangleSize, compassY, labelColor, 1) -- Base
  857. end
  858.  
  859. local function renderLockOnArea()
  860. -- Define the center and dimensions
  861. local lockOnRadius = 65 -- Radius of the lock-on circle
  862. local centerX = screenWidth/screenSizeScale / 2
  863. local centerY = screenHeight/screenSizeScale / 2
  864. local color = 0x00FF0088 -- Green color (RGBA format)
  865.  
  866. -- Draw the circle for the lock-on area
  867. for angle = 0, 360, 5 do
  868. local rad = math.rad(angle)
  869. local x1 = math.floor(centerX + lockOnRadius * math.cos(rad))
  870. local y1 = math.floor(centerY + lockOnRadius * math.sin(rad))
  871. local x2 = math.floor(centerX + lockOnRadius * math.cos(math.rad(angle + 5)))
  872. local y2 = math.floor(centerY + lockOnRadius * math.sin(math.rad(angle + 5)))
  873. holo.DrawLine(x1, y1, x2, y2, color, 1)
  874. end
  875. end
  876.  
  877. local colorMap = {
  878. [colors.white] = 0xF0F0F0FF, -- White
  879. [colors.orange] = 0xF2B233FF, -- Orange
  880. [colors.magenta] = 0xE57FD8FF, -- Magenta
  881. [colors.lightBlue] = 0x99B2F2FF, -- Light Blue
  882. [colors.yellow] = 0xDEDE6CFF, -- Yellow
  883. [colors.lime] = 0x7FCC19FF, -- Lime
  884. [colors.pink] = 0xF2B2CCFF, -- Pink
  885. [colors.gray] = 0x4C4C4CFF, -- Gray
  886. [colors.lightGray] = 0x999999FF, -- Light Gray
  887. [colors.cyan] = 0x4C99B2FF, -- Cyan
  888. [colors.purple] = 0xB266E5FF, -- Purple
  889. [colors.blue] = 0x3366CCFF, -- Blue
  890. [colors.brown] = 0x7F664CFF, -- Brown
  891. [colors.green] = 0x57A64EFF, -- Green
  892. [colors.red] = 0xCC4C4CFF, -- Red
  893. [colors.black] = 0x111111FF -- Black
  894. }
  895.  
  896. -- Update the main function to remove target locking and aiming
  897. local function main()
  898. while true do
  899. holo.Clear()
  900. local playerList = radar.scanForPlayers(1000)
  901. parallel.waitForAll(
  902. function()
  903. for _, player in ipairs(playerList) do
  904. local playerName = player.nickname
  905. local playerPosition = player.pos
  906. local lookAngle = player.look_angle
  907.  
  908. if player.nickname == userID then
  909. pInfo = {
  910. nickname = player.nickname,
  911. pos = player.pos,
  912. lookVector = lookAngle
  913. }
  914. end
  915. end
  916. if pInfo.nickname then
  917. local objectList = radar.scanForShips(10000)
  918. elements = {}
  919. for _, object in ipairs(objectList) do
  920. local objX, objY, objZ = object.pos.x, object.pos.y, object.pos.z
  921. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  922. local distance = math.floor(math.sqrt((object.pos.x - pInfo.pos[1]) ^ 2 + (object.pos.y - pInfo.pos[2]) ^ 2 + (object.pos.z - pInfo.pos[3]) ^ 2))
  923. local speed = math.floor(math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2))
  924. local color
  925. if isFriendly(object.name) then
  926. color = 0x009cff88 -- Friendly, blue color
  927. elseif object.mass < 8000 then
  928. color = 0x99999988 -- type unknown/low threat
  929. elseif object.mass < 30000 then
  930. if speed > 40 then
  931. -- high speed low mass, potential missile
  932.  
  933. local missileVector = {object.velocity.x, object.velocity.y, object.velocity.z}
  934. local toPlayerVector = {pInfo.pos[1] - objX, pInfo.pos[2] - objY, pInfo.pos[3] - objZ}
  935. local dotProduct = missileVector[1] * toPlayerVector[1] + missileVector[2] * toPlayerVector[2] + missileVector[3] * toPlayerVector[3]
  936. if dotProduct > 0 then
  937. color = 0xff5600ff -- missile coming, high threat
  938. else
  939. color = 0xFFFF00ff -- potential threat
  940. end
  941. else
  942. color = 0x00FF0088 -- low mass low speed, low threat
  943. end
  944. elseif object.mass < 200000 then
  945. if speed > 20 then
  946. color = 0xFFFF00ff -- jet
  947. else
  948. color = 0xfff70088 -- Medium sized vehicle, tanks, helicopter
  949. end
  950. else
  951. color = 0xffc300ff -- Large sized vehicle, ships, strategic bomber
  952. end
  953.  
  954. if distance > 20 then
  955. if screenX and screenY then
  956. table.insert(elements, {
  957. id = object.id,
  958. pos = object.pos,
  959. screenX = screenX,
  960. screenY = screenY,
  961. frameSize = 20,
  962. color = color,
  963. distance = distance,
  964. mass = object.mass,
  965. speed = speed,
  966. velocity = object.velocity,
  967. renderInfo = true,
  968. renderBox = isWithinRenderDistance(screenX, screenY, screenWidth / 2, screenHeight / 2, 4000 / GUIscale, 4800 / GUIscale, 3200 / GUIscale)
  969. })
  970. end
  971. end
  972. end
  973.  
  974. --waypoint render
  975. for i,waypoint in ipairs(waypoints) do
  976. local screenX, screenY = projectToScreen(waypoint.x, waypoint.y, waypoint.z, pInfo.pos, pInfo.lookVector)
  977. local distance = math.floor(math.sqrt((waypoint.x - pInfo.pos[1]) ^ 2 + (waypoint.y - pInfo.pos[2]) ^ 2 + (waypoint.z - pInfo.pos[3]) ^ 2))
  978. local color = tonumber(string.format("0x%X",colorMap[waypoint.color]))
  979. if screenX and screenY then
  980. table.insert(elements, {
  981. id = "W"..i,
  982. pos = waypoint,
  983. screenX = screenX,
  984. screenY = screenY,
  985. frameSize = 20,
  986. color = color,
  987. distance = distance,
  988. mass = 0,
  989. speed = 0,
  990. velocity = {0,0,0},
  991. renderInfo = true,
  992. renderBox = isWithinRenderDistance(screenX, screenY, screenWidth / 2, screenHeight / 2, 4000 / GUIscale, 4800 / GUIscale, 3200 / GUIscale)
  993. })
  994. end
  995. end
  996.  
  997. local filteredElements = filterElements(elements)
  998.  
  999. for _, element in ipairs(elements) do
  1000. local found = false
  1001. for _, filteredElement in ipairs(filteredElements) do
  1002. if element.id == filteredElement.id then
  1003. found = true
  1004. break
  1005. end
  1006. end
  1007. if not found then
  1008. element.renderInfo = false
  1009. end
  1010. end
  1011. renderElements(filteredElements, cannonHitPos, pInfo, lockedTarget)
  1012. end
  1013.  
  1014. if pInfo then
  1015. local AimTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  1016. local screenX, screenY = projectToScreen(AimTarget.x, AimTarget.y, AimTarget.z, pInfo.pos, pInfo.lookVector)
  1017. if screenX and screenY then
  1018. --ar.drawStringWithId("AimCrossHair","X", screenX+20, screenY+80, 0xFFFFFF)
  1019. end
  1020. end
  1021.  
  1022. if ship.getVelocity() then
  1023. local velocity = ship.getVelocity()
  1024. local speed = math.floor(math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2))
  1025. end
  1026. end,
  1027. drawPitchLadder,
  1028. drawFlightInfo,
  1029. drawCompass,
  1030. renderLockOnArea
  1031. )
  1032. holo.Flush()
  1033. sleep() -- Adjust the sleep time as needed to balance performance and responsiveness
  1034. end
  1035. end
  1036.  
  1037. local function hitMark()
  1038. while true do
  1039. local playerList = radar.scanForPlayers(1000)
  1040. for _, player in ipairs(playerList) do
  1041. local playerName = player.nickname
  1042. local playerPosition = player.pos
  1043. local lookAngle = player.look_angle
  1044.  
  1045. if player.nickname == userID then
  1046. pInfo = {
  1047. nickname = player.nickname,
  1048. pos = player.pos,
  1049. lookVector = lookAngle
  1050. }
  1051. end
  1052. end
  1053.  
  1054. local shipRoll = math.deg(ship.getRoll())
  1055. local shipYaw = math.deg(ship.getYaw()) - 90
  1056. if shipYaw < 0 then shipYaw = shipYaw + 360 end
  1057. local shipPitch = math.deg(ship.getPitch())
  1058.  
  1059. local currentPitch = shipPitch + cannons[1].getPitch()
  1060. local currentYaw = shipYaw + cannons[1].getYaw() - 90
  1061. if currentYaw < 0 then currentYaw = currentYaw + 360 end
  1062. source = ship.getWorldspacePosition()
  1063. if source and currentYaw and currentPitch then
  1064. local dx = projectionDistance * math.cos(math.rad(currentPitch)) * math.sin(math.rad(currentYaw))
  1065. local dz = -projectionDistance * math.cos(math.rad(currentPitch)) * math.cos(math.rad(currentYaw))
  1066. local dy = projectionDistance * math.sin(math.rad(currentPitch))
  1067. objX = source.x + dx
  1068. objY = source.y+2 + dy
  1069. objZ = source.z + dz
  1070. end
  1071.  
  1072. if objX and objY and objZ then
  1073. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  1074. if screenX and screenY then
  1075. ar.clearElement("CrossHair")
  1076. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  1077. end
  1078. elseif cannonHitPos[3] == "degree" then
  1079. local screenX, screenY = angleToScreen(cannonHitPos[1], cannonHitPos[2], pInfo.pos, pInfo.lookVector)
  1080. if screenX and screenY then
  1081. ar.clearElement("CrossHair")
  1082. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  1083. end
  1084. end
  1085. sleep()
  1086. end
  1087. end
  1088.  
  1089. local function missileListBuilding()
  1090. while true do
  1091. if immediateMissileInfo and immediateMissileInfo.id and immediateMissileInfo.type then
  1092. local foundMissile = false
  1093.  
  1094. -- Update the existing missile state instead of adding duplicates
  1095. for _, missileInfo in ipairs(AIMInfoList) do
  1096. if missileInfo.id == immediateMissileInfo.id then
  1097. missileInfo.launchState = immediateMissileInfo.launchState
  1098. foundMissile = true
  1099. break
  1100. end
  1101. end
  1102. for _, missileInfo in ipairs(GBUInfoList) do
  1103. if missileInfo.id == immediateMissileInfo.id then
  1104. missileInfo.launchState = immediateMissileInfo.launchState
  1105. foundMissile = true
  1106. break
  1107. end
  1108. end
  1109. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1110. if missileInfo.id == immediateMissileInfo.id then
  1111. missileInfo.launchState = immediateMissileInfo.launchState
  1112. foundMissile = true
  1113. break
  1114. end
  1115. end
  1116.  
  1117. -- If it's a new missile, add it to the list
  1118. if not foundMissile then
  1119. if immediateMissileInfo.type == "AIM-220" then
  1120. table.insert(AIMInfoList, immediateMissileInfo)
  1121. elseif immediateMissileInfo.type == "GBU-42" then
  1122. table.insert(GBUInfoList, immediateMissileInfo)
  1123. elseif immediateMissileInfo.type == "thunderBolt" then
  1124. table.insert(thunderBoltInfoList, immediateMissileInfo)
  1125. end
  1126. end
  1127. end
  1128.  
  1129. -- Remove missiles that have launched
  1130. for i = #AIMInfoList, 1, -1 do
  1131. if AIMInfoList[i].launchState == true then
  1132. table.remove(AIMInfoList, i)
  1133. end
  1134. end
  1135. for i = #GBUInfoList, 1, -1 do
  1136. if GBUInfoList[i].launchState == true then
  1137. table.remove(GBUInfoList, i)
  1138. end
  1139. end
  1140. for i = #thunderBoltInfoList, 1, -1 do
  1141. if thunderBoltInfoList[i].launchState == true then
  1142. table.remove(thunderBoltInfoList, i)
  1143. end
  1144. end
  1145.  
  1146. -- Count unlaunched missiles
  1147. AIMCount, GBUCount, thunderboltCount = 0, 0, 0
  1148. for _, missileInfo in ipairs(AIMInfoList) do
  1149. if not missileInfo.launchState then
  1150. AIMCount = AIMCount + 1
  1151. end
  1152. end
  1153. for _, missileInfo in ipairs(GBUInfoList) do
  1154. if not missileInfo.launchState then
  1155. GBUCount = GBUCount + 1
  1156. end
  1157. end
  1158. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1159. if not missileInfo.launchState then
  1160. thunderboltCount = thunderboltCount + 1
  1161. end
  1162. end
  1163. sleep()
  1164. end
  1165. end
  1166.  
  1167. local function isMissileLaunchedById(id)
  1168. for _, launched in ipairs(LaunchedMissiles) do
  1169. if launched.id == id then
  1170. return true
  1171. end
  1172. end
  1173. return false
  1174. end
  1175.  
  1176. missileLaunchOffset = {x=0,y=-5,z=0}
  1177. maxLaunchOffsetDistance = 5
  1178. local function launchMissile(targetType, targetId, targetPos, missileType)
  1179. local currentMissile = nil
  1180.  
  1181. -- Function to find and remove the first unlaunched missile from a list
  1182. local function findUnlaunchedMissile(missileList)
  1183. for i = #missileList, 1, -1 do -- Iterate in reverse to avoid index shifting issues
  1184. local missile = missileList[i]
  1185. if missile.launchState == false then
  1186. -- Check if this missile is already in LaunchedMissiles
  1187. print(textutils.serialize(LaunchedMissiles))
  1188. print(textutils.serialize(missile))
  1189.  
  1190. if not isMissileLaunchedById(missile.id) then
  1191. missile.launchState = true -- Mark missile as launched
  1192. table.insert(LaunchedMissiles, missile) -- Move to launched list
  1193. return table.remove(missileList, i) -- Remove from available list and return it
  1194. end
  1195. end
  1196. end
  1197. return nil
  1198. end
  1199.  
  1200. -- Get the first unlaunched missile of the specified type
  1201. if missileType == "AIM-220" then
  1202. currentMissile = findUnlaunchedMissile(AIMInfoList)
  1203. elseif missileType == "GBU-42" then
  1204. currentMissile = findUnlaunchedMissile(GBUInfoList)
  1205. elseif missileType == "thunderBolt" then
  1206. currentMissile = findUnlaunchedMissile(thunderBoltInfoList)
  1207. end
  1208.  
  1209. -- If no unlaunched missile is found, exit function
  1210. if not currentMissile then
  1211. print("No available unlaunched missile of type:", missileType)
  1212. return
  1213. end
  1214.  
  1215. local currentMissileId = currentMissile.id
  1216. print("Teleporting and launching missile " .. currentMissileId)
  1217.  
  1218. -- Assign the missile as launched and set target info
  1219. if targetType == "ship" then
  1220. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  1221. else
  1222. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  1223. end
  1224.  
  1225. -- Base launch position
  1226. local shipPos = ship.getWorldspacePosition()
  1227. local baseX = shipPos.x + missileLaunchOffset.x
  1228. local baseY = shipPos.y + missileLaunchOffset.y
  1229. local baseZ = shipPos.z + missileLaunchOffset.z
  1230.  
  1231. -- Randomize launch offset within a circle (XZ-plane)
  1232. local angle = math.random() * 2 * math.pi
  1233. local distance = math.random() * maxLaunchOffsetDistance
  1234. local offsetX = math.cos(angle) * distance
  1235. local offsetZ = math.sin(angle) * distance
  1236. local offsetY = math.random(-1, 1) -- Optional small vertical variation
  1237.  
  1238. local tx = baseX + offsetX
  1239. local ty = baseY + offsetY
  1240. local tz = baseZ + offsetZ
  1241.  
  1242. -- Aim calculation
  1243. local dx = targetPos.x - tx
  1244. local dy = targetPos.y - ty
  1245. local dz = targetPos.z - tz
  1246.  
  1247. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  1248. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  1249. local yaw = math.deg(math.atan2(dx, dz))
  1250.  
  1251. local teleportMessage = string.format(
  1252. "vmod teleport %s %.2f %.2f %.2f (%.2f %.2f 0)",
  1253. currentMissile.slug, tx, ty, tz, -pitch, yaw
  1254. )
  1255.  
  1256. --print(teleportMessage)
  1257. print(commands.exec(teleportMessage))
  1258. commands.exec("/vs set-static "..currentMissile.slug.." false")
  1259. end
  1260.  
  1261.  
  1262. local function missileHandler()
  1263. while true do
  1264. if controls.switchToAIM220 then
  1265. weaponChoosen = "AIM-220"
  1266. elseif controls.switchToGBU then
  1267. weaponChoosen = "GBU-42"
  1268. elseif controls.switchToThunderbolt then
  1269. weaponChoosen = "thunderBolt"
  1270. elseif controls.switchToAIM9 then
  1271. weaponChoosen = "AIM-9"
  1272. elseif controls.switchToGun then
  1273. weaponChoosen = "Gun"
  1274. end
  1275. if controls.fire then
  1276. if weaponChoosen == "AIM-220" or weaponChoosen == "GBU-42" or weaponChoosen == "thunderBolt" then
  1277. if lockedTarget and lockedTarget.id and missileCoolDown == 0 then
  1278. if type(lockedTarget.id) == "number" then
  1279. launchMissile("ship",lockedTarget.id,lockedTarget.pos,weaponChoosen) --target is a ship
  1280. else
  1281. launchMissile("waypoint",lockedTarget.id,lockedTarget.pos,weaponChoosen)
  1282. end
  1283. missileCoolDown = 20
  1284. modem.transmit(missileControlChannel,missileControlChannel,missileControls)
  1285. end
  1286. elseif weaponChoosen == "AIM-9" then
  1287.  
  1288. elseif weaponChoosen == "Gun" then
  1289.  
  1290. end
  1291. end
  1292. missileCoolDown = math.max(missileCoolDown - 1,0)
  1293. sleep()
  1294. end
  1295. end
  1296.  
  1297. local function printOutput()
  1298. while true do
  1299. --print(textutils.serialize(AIMInfoList))
  1300. --print(textutils.serialize(GBUInfoList))
  1301. --print(textutils.serialize(thunderBoltInfoList))
  1302. --print(textutils.serialize(controls))
  1303. --print(textutils.serialize(missileControls))
  1304. print(textutils.serialize(missileControls))
  1305. print(waypointChannel)
  1306. --print("controlsChannel: "..controlChannel)
  1307. --print("weaponChoosen: "..weaponChoosen)
  1308. sleep(0.5)
  1309. end
  1310. end
  1311.  
  1312. -- Main loop
  1313. parallel.waitForAny(
  1314. modemMessage,
  1315. main,
  1316. targetLockAndAim,
  1317. missileListBuilding,
  1318. printOutput,
  1319. missileHandler
  1320. )
Advertisement
Add Comment
Please, Sign In to add comment