MegiRicky

gunPL-01NGDW

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