MegiRicky

strykerGunNGDW

Aug 11th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.18 KB | None | 0 0
  1. -- Variable initialization
  2. local radar = peripheral.find("sp_radar")
  3. local ar = peripheral.find("arController")
  4. local modem = peripheral.find("modem")
  5. local yawMotor = peripheral.wrap("back")
  6. local pitchMotor = peripheral.wrap("front")
  7.  
  8. local cannonHitPosChannel = 700
  9. local cannonSourceChannel = 701
  10. local cannonSource
  11. if modem then
  12. modem.open(cannonHitPosChannel)
  13. end
  14.  
  15. local screenWidth, screenHeight, fov, GUIscale
  16. local sourceX, sourceY, sourceZ = 0,0,0
  17. local source = {}
  18. local userID
  19. local friendlyIDs
  20. local widthMagic = 1.125
  21. local projectionDistance = 100
  22. local currentPitch, currentYaw = 0,0
  23. local lockedTarget = nil
  24. local elements = {}
  25. local pInfo = {}
  26. local cannonHitPos = {}
  27. local pitchAdjustments = "1"
  28. local lastYawUpdateTime = 0
  29. local lastPitchUpdateTime = 0
  30. local updateInterval = 0.05
  31.  
  32. local yawErrorSum = 0
  33. local lastYawError = 0
  34. local pitchErrorSum = 0
  35. local lastPitchError = 0
  36. local lastTime = os.clock()
  37.  
  38. local Kp = 0.3 -- Proportional gain
  39. local Ki = 0.1 -- Integral gain
  40. local Kd = 0.05 -- Derivative gain
  41.  
  42.  
  43. -- Configuration file paths
  44. local configFile = "ciwsNGDWconfig.txt"
  45. local friendlyIDFile = "friendly_ids.txt"
  46.  
  47. -- Function to read configuration from a file
  48. local function readConfig(filename)
  49. if not fs.exists(filename) then
  50. print("Configuration file '" .. filename .. "' does not exist. Creating it.")
  51. local file = fs.open(filename, "w")
  52.  
  53. print("Input your resolution eg.(1920,1080), default: 3440,1440")
  54. local resolution = io.read()
  55. if resolution == "" then
  56. resolution = "3440,1440"
  57. end
  58. file.writeLine("resolution=" .. resolution)
  59.  
  60. print("Input your FOV eg.(70), default: 70")
  61. local fov = io.read()
  62. if fov == "" then
  63. fov = "70"
  64. end
  65. file.writeLine("fov=" .. fov)
  66.  
  67. print("Input your GUI scale, default: 4")
  68. local guiScale = io.read()
  69. if guiScale == "" then
  70. guiScale = "4"
  71. end
  72. file.writeLine("guiScale=" .. guiScale)
  73.  
  74. print("Input your mode 1:System in world space 2:System on vs ship. Default: 1")
  75. local mode = io.read()
  76. if mode == "" then
  77. mode = "1"
  78. end
  79. file.writeLine("mode=" .. mode)
  80.  
  81. print("Input your ID, default: MegiRicky")
  82. local userID = io.read()
  83. if userID == "" then
  84. userID = "MegiRicky"
  85. end
  86. file.writeLine("userID=" .. userID)
  87.  
  88. if mode == "1" then
  89. print("Input the location of the computer (X,Y,Z)")
  90. local source = io.read()
  91. if source == "" then source = "0,0,0" end
  92. file.writeLine("source=" .. source)
  93. else
  94. file.writeLine("source=ship")
  95. end
  96.  
  97.  
  98. file.close()
  99. end
  100.  
  101. local file = fs.open(filename, "r")
  102. local config = {}
  103. local line = file.readLine()
  104. while line do
  105. local key, value = line:match("^(%w+)=(.*)$")
  106. config[key] = value
  107. line = file.readLine()
  108. end
  109. file.close()
  110. return config
  111. end
  112.  
  113. -- Function to read friendly IDs from a file
  114. local function readFriendlyIDs(filename)
  115. if not fs.exists(filename) then
  116. print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
  117. local file = fs.open(filename, "w")
  118. print("Input friendly IDs separated by commas:")
  119. local ids = io.read()
  120. file.write(ids)
  121. file.close()
  122. end
  123.  
  124. local file = fs.open(filename, "r")
  125. local friendlyIDs = {}
  126. if file then
  127. local line = file.readLine()
  128. while line do
  129. for id in string.gmatch(line, "%d+") do
  130. table.insert(friendlyIDs, tonumber(id))
  131. end
  132. line = file.readLine()
  133. end
  134. file.close()
  135. end
  136. return friendlyIDs
  137. end
  138.  
  139. -- Load configuration
  140. local config = readConfig(configFile)
  141. screenWidth, screenHeight = config.resolution:match("(%d+),(%d+)")
  142. screenWidth = tonumber(screenWidth)
  143. screenHeight = tonumber(screenHeight)
  144. fov = tonumber(config.fov)
  145. GUIscale = tonumber(config.guiScale)
  146. local mode = tonumber(config.mode)
  147. userID = config.userID
  148. friendlyIDs = readFriendlyIDs(friendlyIDFile)
  149. local magicNumber = GUIscale/4
  150.  
  151. if mode == 1 then
  152. sourceX, sourceY, sourceZ = string.match(config.source, "(%-?%d+),(%-?%d+),(%-?%d+)")
  153. sourceX = tonumber(sourceX)
  154. sourceY = tonumber(sourceY)
  155. sourceZ = tonumber(sourceZ)
  156. else
  157. local pos = ship.getWorldspacePosition()
  158. sourceX, sourceY, sourceZ = pos.x, pos.y, pos.z
  159. end
  160.  
  161. print("Source X: " .. sourceX)
  162. print("Source Y: " .. sourceY)
  163. print("Source Z: " .. sourceZ)
  164. print("Resolution: " .. screenWidth .. ", " .. screenHeight)
  165. if screenWidth == 3440 and screenHeight == 1440 then widthMagic = 1.125 else widthMagic = 0.92 end
  166. ar.setRelativeMode(true, screenWidth * 4, screenHeight * 4)
  167.  
  168. local function isFriendly(id)
  169. for _, friendlyID in ipairs(friendlyIDs) do
  170. if id == friendlyID then
  171. return true
  172. end
  173. end
  174. return false
  175. end
  176.  
  177. -- Function to project 3D coordinates to 2D screen coordinates
  178. local function projectToScreen(x, y, z, userPos, lookVector)
  179. local dx = x - userPos[1]
  180. local dy = -(y - userPos[2])
  181. local dz = z - userPos[3]
  182.  
  183. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  184. local playerYaw = math.deg(math.atan2(lz, lx))
  185. local playerPitch = math.deg(math.asin(-ly))
  186. playerYaw = playerYaw + 90
  187. if playerYaw < 0 then
  188. playerYaw = playerYaw + 360
  189. end
  190.  
  191. -- Calculate yaw difference
  192. local targetYaw = math.deg(math.atan2(dz, dx)) + 90
  193. if targetYaw < 0 then
  194. targetYaw = targetYaw + 360
  195. end
  196.  
  197. local yawDiff = targetYaw - playerYaw
  198. if yawDiff > 180 then
  199. yawDiff = yawDiff - 360
  200. elseif yawDiff < -180 then
  201. yawDiff = yawDiff + 360
  202. end
  203.  
  204. if math.abs(yawDiff) > 90 then
  205. return nil, nil
  206. end
  207.  
  208. -- Calculate pitch difference
  209. local distXY = math.sqrt(dx * dx + dz * dz)
  210. local targetPitch = math.deg(math.atan2(dy, distXY))
  211. local pitchDiff = targetPitch - playerPitch
  212.  
  213. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  214. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  215.  
  216. local projectedX, projectedY
  217.  
  218. if yawDiff > 0 then
  219. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  220. else
  221. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  222. end
  223.  
  224. if pitchDiff > 0 then
  225. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  226. else
  227. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  228. end
  229.  
  230. projectedX = math.floor(projectedX / magicNumber)
  231. projectedY = math.floor(projectedY / magicNumber) + 20
  232.  
  233. return projectedX, projectedY
  234. end
  235.  
  236. -- Function to display pitch and yaw of a cannon
  237. local function angleToScreen(pitch, yaw, userPos, lookVector)
  238. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  239. local playerYaw = math.deg(math.atan2(lz, lx))
  240. local playerPitch = math.deg(math.asin(-ly))
  241. playerYaw = playerYaw + 90
  242. if playerYaw < 0 then
  243. playerYaw = playerYaw + 360
  244. end
  245.  
  246. -- Calculate yaw difference
  247. local yawDiff = yaw - playerYaw
  248. if yawDiff > 180 then
  249. yawDiff = yawDiff - 360
  250. elseif yawDiff < -180 then
  251. yawDiff = yawDiff + 360
  252. end
  253.  
  254. if math.abs(yawDiff) > 90 then
  255. return nil, nil
  256. end
  257.  
  258. -- Calculate pitch difference
  259. local pitchDiff = -pitch + playerPitch
  260.  
  261. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  262. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  263.  
  264. local projectedX, projectedY
  265.  
  266. if yawDiff > 0 then
  267. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  268. else
  269. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  270. end
  271.  
  272. if pitchDiff > 0 then
  273. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  274. else
  275. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  276. end
  277.  
  278. projectedX = math.floor(projectedX / magicNumber)
  279. projectedY = math.floor(projectedY / magicNumber) + 20
  280.  
  281. return projectedX, projectedY
  282. end
  283.  
  284. local function mouseAim(userPos, lookVector)
  285. lookTarget = {}
  286.  
  287. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  288. local playerYaw = math.deg(math.atan2(lz, lx))
  289. local playerPitch = math.deg(math.asin(-ly)) - 15
  290. playerYaw = playerYaw + 90
  291. if playerYaw < 0 then
  292. playerYaw = playerYaw + 360
  293. end
  294.  
  295. local yawRad = math.rad(playerYaw)
  296. local pitchRad = math.rad(playerPitch)
  297.  
  298. local dx = projectionDistance * math.cos(pitchRad) * math.sin(yawRad)
  299. local dz = -projectionDistance * math.cos(pitchRad) * math.cos(yawRad)
  300. local dy = -projectionDistance * math.sin(pitchRad)
  301.  
  302. local targetX = userPos[1] + dx
  303. local targetY = userPos[2] + dy
  304. local targetZ = userPos[3] + dz
  305. lookTarget = { x = targetX, y = targetY, z = targetZ }
  306. return lookTarget
  307. end
  308.  
  309.  
  310. local function isWithinRenderDistance(projectedX, projectedY, centerX, centerY, maxdx, maxdy, maxd)
  311. local dx = projectedX - centerX / magicNumber
  312. local dy = projectedY - centerY / magicNumber
  313.  
  314. if dx < maxdx then
  315. if dy < maxdy then
  316. local distance = math.sqrt(dx * dx + dy * dy)
  317. if distance <= maxd then
  318. return true
  319. else
  320. return false
  321. end
  322. else
  323. return false
  324. end
  325. else
  326. return false
  327. end
  328. end
  329.  
  330. -- Function to check for overlap and adjust positions
  331. local function filterElements(elements)
  332. table.sort(elements, function(a, b) return a.mass > b.mass end) -- Sort by mass
  333. local filteredElements = {}
  334.  
  335. for i, element in ipairs(elements) do
  336. local overlap = false
  337. for _, filteredElement in ipairs(filteredElements) do
  338. local dx = math.abs(element.screenX - filteredElement.screenX)
  339. local dy = math.abs(element.screenY - filteredElement.screenY)
  340. if dx < 800 / GUIscale and dy < 800 / GUIscale then
  341. overlap = true
  342. element.renderInfo = false
  343. break
  344. end
  345. end
  346.  
  347. table.insert(filteredElements, element)
  348.  
  349. end
  350.  
  351. return filteredElements
  352. end
  353.  
  354. -- Function to render elements in parallel
  355. local function renderElements(elements, cannonHitPos, pInfo, lockedTarget)
  356. local tasks = {}
  357. for _, element in ipairs(elements) do
  358. table.insert(tasks, function()
  359. local color = element.color
  360. if lockedTarget and element.id == lockedTarget.id then
  361. color = 0xFF0000 -- Red color for locked target
  362. end
  363. ar.horizontalLineWithId("frame_top_" .. element.id, element.screenX - element.frameSize, element.screenX + element.frameSize, element.screenY - element.frameSize, color)
  364. ar.horizontalLineWithId("frame_bottom_" .. element.id, element.screenX - element.frameSize, element.screenX + element.frameSize, element.screenY + element.frameSize, color)
  365. ar.verticalLineWithId("frame_left_" .. element.id, element.screenX - element.frameSize, element.screenY - element.frameSize, element.screenY + element.frameSize, color)
  366. ar.verticalLineWithId("frame_right_" .. element.id, element.screenX + element.frameSize, element.screenY - element.frameSize, element.screenY + element.frameSize, color)
  367. if element.renderInfo then
  368. ar.drawString("dist: " .. element.distance, element.screenX - 15, element.screenY - 200 / GUIscale, color)
  369. ar.drawString("mass: " .. math.floor(element.mass / 1000) .. "k", element.screenX - 15, element.screenY - 340 / GUIscale, color)
  370. ar.drawString("speed: " .. element.speed, element.screenX - 15, element.screenY - 480 / GUIscale, color)
  371. ar.drawString("id: " .. element.id, element.screenX - 15, element.screenY - 620 / GUIscale, color)
  372. end
  373. end)
  374. end
  375. parallel.waitForAll(table.unpack(tasks))
  376. end
  377.  
  378. local function lockHighestThreatTarget(elements, centerX, centerY, radius)
  379. local highestThreat = nil
  380. local highestThreatLevel = -1
  381.  
  382. for _, element in ipairs(elements) do
  383. if element.id then
  384. local dx = element.screenX - centerX / magicNumber
  385. local dy = element.screenY - centerY / magicNumber
  386. local distance = math.sqrt(dx * dx + dy * dy)
  387.  
  388. if distance <= radius / GUIscale then
  389. local threatLevel = element.mass -- Use mass as a proxy for threat level
  390. if isFriendly(element.id) then threatLevel = -1 end
  391. if threatLevel > highestThreatLevel then
  392. highestThreat = element
  393. highestThreatLevel = threatLevel
  394. end
  395. end
  396. end
  397. end
  398.  
  399. return highestThreat
  400. end
  401.  
  402. local function modemMessage()
  403. while true do
  404. if modem then
  405. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  406. if channel == cannonHitPosChannel then
  407. cannonHitPos = message
  408. end
  409. else
  410. sleep()
  411. end
  412. end
  413. end
  414.  
  415. local function setMotorSpeed(motor, speed, motorType)
  416. local currentTime = os.clock()
  417. local lastUpdateTime = motorType == "yaw" and lastYawUpdateTime or lastPitchUpdateTime
  418. local updateInterval = 0.2
  419. local updateTolerance = 1
  420.  
  421. if currentTime - lastUpdateTime >= updateInterval then
  422. if math.abs(motor.getSpeed() - speed) > updateTolerance then
  423. motor.setSpeed(speed)
  424. end
  425. if motorType == "yaw" then
  426. lastYawUpdateTime = currentTime
  427. else
  428. lastPitchUpdateTime = currentTime
  429. end
  430. end
  431. end
  432.  
  433. local function getAngularVelocity(currentYaw, currentTime)
  434. if lastYaw and lastYawTime then
  435. local deltaTime = currentTime - lastYawTime
  436. local deltaYaw = (currentYaw - lastYaw + math.pi) % (2 * math.pi) - math.pi
  437. return deltaYaw / deltaTime -- Radians per second
  438. end
  439. return 0
  440. end
  441.  
  442. local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ, currentPitch, currentYaw)
  443. if sourceX and sourceY and sourceZ and currentPitch and currentYaw then
  444. local currentTime = os.clock()
  445. local dt = currentTime - lastTime
  446. if dt == 0 then return end -- Prevent division by zero
  447. lastTime = currentTime
  448.  
  449. local dx = targetPos.x - sourceX
  450. local dy = targetPos.y - sourceY
  451. local dz = targetPos.z - sourceZ
  452. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  453.  
  454. local estimateTime = distance / (6 * 20) -- assuming velocity units and time units need adjustment
  455. local estimateX = targetPos.x + targetVel.x * estimateTime
  456. local estimateY = targetPos.y + targetVel.y * estimateTime
  457. local estimateZ = targetPos.z + targetVel.z * estimateTime
  458.  
  459. local dx = estimateX - sourceX
  460. local dy = estimateY - sourceY
  461. local dz = estimateZ - sourceZ
  462.  
  463. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  464. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  465.  
  466. local yaw = math.deg(math.atan2(-dx, dz))
  467. yaw = (yaw + 180) % 360
  468.  
  469. -- Adjust pitch based on distance adjustments
  470. local pitchAdjustments = {
  471. {min = 50, max = 100, increment = 1},
  472. {min = 180, max = 250, increment = 2},
  473. {min = 250, max = 300, increment = 3},
  474. {min = 300, max = 350, increment = 4},
  475. {min = 350, max = 400, increment = 6},
  476. {min = 400, max = 450, increment = 4.3},
  477. {min = 450, max = 500, increment = 5},
  478. {min = 500, max = 550, increment = 5.9},
  479. {min = 550, max = 600, increment = 6.6},
  480. {min = 600, max = 650, increment = 7.5}
  481. }
  482.  
  483. for _, adjustment in ipairs(pitchAdjustments) do
  484. if distance > adjustment.min and distance <= adjustment.max then
  485. pitch = pitch + adjustment.increment
  486. break
  487. end
  488. end
  489.  
  490. local yawError = (yaw - currentYaw + 180) % 360 - 180
  491. local deltaPitch = pitch - currentPitch
  492.  
  493. local yawDerivative = (yawError - lastYawError) / dt
  494. lastYawError = yawError
  495. local pidYaw = Kp * yawError
  496. yawErrorSum = yawErrorSum + yawError * dt
  497. if yawError < 0 then pidYaw = -pidYaw end
  498. local yawRateMismatch = math.abs(yawDerivative) - math.abs(pidYaw * 6 *dt)
  499. print("pidYaw: "..pidYaw)
  500. print("pidYaw * 360/60*dt: "..pidYaw * 360/60*dt)
  501. print("yawDerivative: "..yawDerivative)
  502. print("yawRateMismatch: "..yawRateMismatch)
  503. local pidYaw = pidYaw
  504.  
  505. local minSpeed = 0.8
  506.  
  507. local velocity = ship.getVelocity()
  508. local speed = math.floor(math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2))
  509.  
  510. local pitchSpeedScale = -math.max(math.abs(deltaPitch)/4, minSpeed) * 8
  511.  
  512. -- Turning and pitch adjustment logic
  513. if math.abs(yawError) > 1 then
  514. setMotorSpeed(yawMotor, yawError < 0 and pidYaw or -pidYaw, "yaw")
  515. else
  516. setMotorSpeed(yawMotor, 0, "yaw")
  517. end
  518.  
  519. if math.abs(deltaPitch) > 1 then
  520. setMotorSpeed(pitchMotor, deltaPitch > 0 and -pitchSpeedScale or pitchSpeedScale, "pitch")
  521. else
  522. setMotorSpeed(pitchMotor, 0, "pitch")
  523. end
  524.  
  525. lastYaw = currentShipYaw
  526. lastYawTime = currentTime
  527. end
  528. end
  529.  
  530. local function targetLockAndAim()
  531. while true do
  532. currentPitch,currentYaw = cannonHitPos[6],cannonHitPos[7]
  533. source = cannonHitPos[5]
  534. if pInfo.nickname then
  535.  
  536. lockedTarget = lockHighestThreatTarget(elements, screenWidth / 2, screenHeight / 2, 1000)
  537. if lockedTarget then
  538. print("Locked target ID: " .. lockedTarget.id)
  539. lockedTargetScan = radar.scanForShips(5000)
  540. for _, object in ipairs(lockedTargetScan) do
  541. if object.id == lockedTarget.id then
  542. -- Update the locked target information
  543. lockedTarget.pos = object.pos
  544. lockedTarget.velocity = object.velocity
  545. break
  546. end
  547. end
  548. if lockedTarget.pos then
  549. aimCannon(lockedTarget.pos,lockedTarget.velocity,source.x, source.y, source.z,currentPitch, currentYaw)
  550. end
  551. else
  552. lookTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  553.  
  554. aimCannon(lookTarget, {x=0, y=0, z=0}, source.x, source.y, source.z, currentPitch, currentYaw)
  555. end
  556. end
  557. sleep()
  558. end
  559. end
  560.  
  561. -- Update the main function to remove target locking and aiming
  562. local function main()
  563. while true do
  564. local playerList = radar.scanForPlayers(1000)
  565. if cannonHitPos then
  566.  
  567. end
  568.  
  569. for _, player in ipairs(playerList) do
  570. local playerName = player.nickname
  571. local playerPosition = player.pos
  572. local lookAngle = player.look_angle
  573.  
  574. if player.nickname == userID then
  575. pInfo = {
  576. nickname = player.nickname,
  577. pos = player.pos,
  578. lookVector = lookAngle
  579. }
  580. end
  581. end
  582.  
  583. if pInfo.nickname then
  584. local objectList = radar.scanForShips(10000)
  585. ar.clear()
  586.  
  587. elements = {}
  588. for _, object in ipairs(objectList) do
  589. local objX, objY, objZ = object.pos.x, object.pos.y, object.pos.z
  590. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  591. 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))
  592. local speed = math.floor(math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2))
  593. local color
  594. if isFriendly(object.id) then
  595. color = 0x009cff -- Friendly, blue color
  596. elseif object.mass < 10000 then
  597. color = 0x999999 -- type unknown/low threat
  598. elseif object.mass < 100000 then
  599. if speed > 40 then
  600. -- high speed low mass, potential missile
  601.  
  602. local missileVector = {object.velocity.x, object.velocity.y, object.velocity.z}
  603. local toPlayerVector = {pInfo.pos[1] - objX, pInfo.pos[2] - objY, pInfo.pos[3] - objZ}
  604. local dotProduct = missileVector[1] * toPlayerVector[1] + missileVector[2] * toPlayerVector[2] + missileVector[3] * toPlayerVector[3]
  605. if dotProduct > 0 then
  606. color = 0xff5600 -- missile coming, high threat
  607. else
  608. color = 0xFFFF00 -- potential threat
  609. end
  610. else
  611. color = 0x00FF00 -- low mass low speed, low threat
  612. end
  613. elseif object.mass < 1000000 then
  614. color = 0xfff700 -- Medium sized vehicle, fighter jet, tanks, helicopter
  615. else
  616. color = 0xffc300 -- Large sized vehicle, ships, strategic bomber
  617. end
  618.  
  619. if distance > 10 then
  620. if screenX and screenY and isWithinRenderDistance(screenX, screenY, screenWidth / 2, screenHeight / 2, 4000 / GUIscale, 4800 / GUIscale, 3200 / GUIscale) then
  621. table.insert(elements, {
  622. id = object.id,
  623. screenX = screenX,
  624. screenY = screenY,
  625. frameSize = 20,
  626. color = color,
  627. distance = distance,
  628. mass = object.mass,
  629. speed = speed,
  630. velocity = object.velocity,
  631. renderInfo = true
  632. })
  633. end
  634. end
  635. end
  636.  
  637. local filteredElements = filterElements(elements)
  638.  
  639. for _, element in ipairs(elements) do
  640. local found = false
  641. for _, filteredElement in ipairs(filteredElements) do
  642. if element.id == filteredElement.id then
  643. found = true
  644. break
  645. end
  646. end
  647. if not found then
  648. element.renderInfo = false
  649. end
  650. end
  651.  
  652. renderElements(filteredElements, cannonHitPos, pInfo, lockedTarget)
  653. end
  654.  
  655. if pInfo then
  656. local AimTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  657. local screenX, screenY = projectToScreen(AimTarget.x, AimTarget.y, AimTarget.z, pInfo.pos, pInfo.lookVector)
  658. if screenX and screenY then
  659. ar.drawStringWithId("AimCrossHair","X", screenX, screenY, 0xFFFFFF)
  660. end
  661. end
  662.  
  663. if ship.getVelocity() then
  664. local velocity = ship.getVelocity()
  665. local speed = math.floor(math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2))
  666.  
  667. ar.drawStringWithId("AimCrossHair","speed: "..speed, 100, 500, 0xFFFFFF)
  668. end
  669.  
  670.  
  671. sleep(0.05) -- Adjust the sleep time as needed to balance performance and responsiveness
  672. end
  673. end
  674.  
  675. local function hitMark()
  676. while true do
  677. if cannonHitPos and modem then
  678. local playerList = radar.scanForPlayers(1000)
  679. for _, player in ipairs(playerList) do
  680. local playerName = player.nickname
  681. local playerPosition = player.pos
  682. local lookAngle = player.look_angle
  683.  
  684. if player.nickname == userID then
  685. pInfo = {
  686. nickname = player.nickname,
  687. pos = player.pos,
  688. lookVector = lookAngle
  689. }
  690. end
  691. end
  692.  
  693. if cannonHitPos[4] == "coordinate" then
  694. if cannonHitPos then
  695. objX, objY, objZ = cannonHitPos[1], cannonHitPos[2], cannonHitPos[3]
  696. end
  697. if objX and objY and objZ then
  698. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  699. if screenX and screenY then
  700. ar.clearElement("CrossHair")
  701. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  702. end
  703. end
  704. elseif cannonHitPos[3] == "degree" then
  705. print("pitch: "..cannonHitPos[1])
  706. print("yaw: "..cannonHitPos[2])
  707. local screenX, screenY = angleToScreen(cannonHitPos[1], cannonHitPos[2], pInfo.pos, pInfo.lookVector)
  708. if screenX and screenY then
  709. ar.clearElement("CrossHair")
  710. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  711. end
  712. end
  713. end
  714. sleep()
  715. end
  716. end
  717.  
  718. -- Main loop
  719. parallel.waitForAny(
  720. modemMessage,
  721. main,
  722. hitMark,
  723. targetLockAndAim
  724. )
  725.  
Advertisement
Add Comment
Please, Sign In to add comment