MegiRicky

M154Autocannon

Oct 5th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.95 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local ar = peripheral.find("arController")
  3. local modem = peripheral.wrap("right")
  4. local bal = peripheral.find("ballistic_accelerator")
  5. local router = peripheral.find("redrouter")
  6.  
  7. local cannons = {}
  8. local k = 1
  9. for i = 0, 200 do
  10. local cannon = peripheral.wrap("cbc_cannon_mount_"..tostring(i))
  11. if cannon then
  12. cannons[k] = cannon
  13. k = k + 1
  14. if k == 5 then
  15. break
  16. end
  17. end
  18. end
  19.  
  20. if not cannons[2] then
  21. error("cannon2 not found")
  22. end
  23. local cannonSourceChannel = 701
  24. local controlChannel = 500
  25.  
  26. local function askUser(prompt, defaultValue)
  27. print(prompt .. " (default: " .. defaultValue .. ")")
  28. local input = io.read()
  29. if input == "" then
  30. return defaultValue
  31. else
  32. return input
  33. end
  34. end
  35.  
  36. local cannonControlMode = "mouseAim"
  37.  
  38. local screenWidth, screenHeight, fov, GUIscale
  39. local sourceX, sourceY, sourceZ = 0,0,0
  40. local source = {}
  41. local userID
  42. local friendlyIDs
  43. local friendlyIDFile = "friendly_ids.txt"
  44. local widthMagic = 1.125
  45. local projectionDistance = 30
  46. local currentPitch, currentYaw = 0,0
  47. local lockedTarget = nil
  48. local elements = {}
  49. local pInfo = {}
  50. local cannonHitPos = {}
  51. local pitchAdjustments = "1"
  52. local lastYawUpdateTime = 0
  53. local lastPitchUpdateTime = 0
  54. local updateInterval = 0.05
  55. local projectileSpeed = 240
  56.  
  57. local yawErrorSum = 0
  58. local lastYawError = 0
  59. local pitchErrorSum = 0
  60. local lastPitchError = 0
  61. local lastTime = os.clock()
  62.  
  63. local Kp = 0.3 -- Proportional gain
  64. local Ki = 0.1 -- Integral gain
  65. local Kd = 0.05 -- Derivative gain
  66.  
  67. local controls = {cannonControlMode = "manual"}
  68.  
  69.  
  70. -- Configuration file paths
  71. local function readConfig(filename)
  72. if not fs.exists(filename) then
  73. print("Configuration file '" .. filename .. "' does not exist. Creating it.")
  74. file = fs.open(filename, "w")
  75.  
  76. resolution = askUser("Input your resolution eg.(1920,1080)", "3440,1440")
  77. file.writeLine("resolution=" .. resolution)
  78.  
  79. fov = askUser("Input your FOV eg.(70)", "70")
  80. file.writeLine("fov=" .. fov)
  81.  
  82. guiScale = askUser("Input your GUI scale", "4")
  83. file.writeLine("guiScale=" .. guiScale)
  84.  
  85. mode = askUser("Input your mode 1:System in world space 2:System on vs ship", "1")
  86. file.writeLine("mode=" .. mode)
  87.  
  88. userID = askUser("Input your ID", "MegiRicky")
  89. file.writeLine("userID=" .. userID)
  90.  
  91. if mode == "1" then
  92. local source = askUser("Input the location of the computer (X,Y,Z)", "0,0,0")
  93. file.writeLine("source=" .. source)
  94. else
  95. file.writeLine("source=ship")
  96. end
  97.  
  98. -- Integrating channel number into the config
  99. cannonHitPosChannel = askUser("Input the cannon data channel number", "700")
  100. file.writeLine("cannonHitPosChannel=" .. cannonHitPosChannel)
  101.  
  102. controlChannel = askUser("Input the control channel", "500")
  103. file.writeLine("controlChannel=" .. controlChannel)
  104.  
  105. file.close()
  106. end
  107. sleep(1)
  108. local file = fs.open(filename, "r")
  109. local config = {}
  110. local line = file.readLine()
  111. while line do
  112. local key, value = line:match("^(%w+)=(.*)$")
  113. config[key] = value
  114. line = file.readLine()
  115. end
  116. file.close()
  117. return config
  118. end
  119.  
  120. -- Function to edit configuration
  121. local function editConfig(filename)
  122. print("Do you want to edit the config? (yes/no),press enter to skip")
  123. local editChoice = io.read()
  124. if editChoice == "" then
  125. editChoice = "no"
  126. end
  127. if editChoice:lower() == "yes" then
  128. if fs.exists(filename) then
  129. fs.delete(filename)
  130. end
  131. return readConfig(filename)
  132. else
  133. return nil
  134. end
  135. end
  136.  
  137. -- Function to select or create config
  138. local function loadOrCreateConfig()
  139. print("Which player's config do you want to load?")
  140. local selectedID = io.read()
  141.  
  142. local configFile = selectedID .. "config.txt"
  143.  
  144. if fs.exists(configFile) then
  145. print("Config file for " .. selectedID .. " exists. Loading it.")
  146. local config = readConfig(configFile)
  147.  
  148. -- Ask if user wants to edit config
  149. editConfig(configFile)
  150.  
  151. return config
  152. else
  153. print("No config file found for " .. selectedID .. ". Creating a new one.")
  154. return readConfig(configFile)
  155. end
  156. end
  157.  
  158. -- Function to read friendly IDs from a file
  159. local function readFriendlyIDs(filename)
  160. if not fs.exists(filename) then
  161. print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
  162. local file = fs.open(filename, "w")
  163. print("Input friendly IDs separated by commas:")
  164. local ids = io.read()
  165. file.write(ids)
  166. file.close()
  167. end
  168.  
  169. local file = fs.open(filename, "r")
  170. local friendlyIDs = {}
  171. if file then
  172. local line = file.readLine()
  173. while line do
  174. for id in string.gmatch(line, "%d+") do
  175. table.insert(friendlyIDs, tonumber(id))
  176. end
  177. line = file.readLine()
  178. end
  179. file.close()
  180. end
  181. return friendlyIDs
  182. end
  183.  
  184. local function sign(x)
  185. return x > 0 and 1 or x < 0 and -1 or 0
  186. end
  187.  
  188. -- Load configuration
  189.  
  190. local config = loadOrCreateConfig()
  191. sleep(1)
  192. print(textutils.serialize(config))
  193. screenWidth, screenHeight = config.resolution:match("(%d+),(%d+)")
  194. screenWidth = tonumber(screenWidth)
  195. screenHeight = tonumber(screenHeight)
  196. fov = tonumber(config.fov)
  197. GUIscale = tonumber(config.guiScale)
  198. local mode = tonumber(config.mode)
  199. userID = config.userID
  200. friendlyIDs = readFriendlyIDs(friendlyIDFile)
  201. local magicNumber = GUIscale/4
  202. local cannonHitPosChannel = tonumber(config.cannonHitPosChannel)
  203. local controlChannel = tonumber(config.controlChannel)
  204.  
  205. if modem then
  206. modem.open(cannonHitPosChannel)
  207. modem.open(controlChannel)
  208. end
  209.  
  210. if mode == 1 then
  211. sourceX, sourceY, sourceZ = string.match(config.source, "(%-?%d+),(%-?%d+),(%-?%d+)")
  212. sourceX = tonumber(sourceX)
  213. sourceY = tonumber(sourceY)
  214. sourceZ = tonumber(sourceZ)
  215. else
  216. local pos = ship.getWorldspacePosition()
  217. sourceX, sourceY, sourceZ = pos.x, pos.y, pos.z
  218. end
  219.  
  220. print("Source X: " .. sourceX)
  221. print("Source Y: " .. sourceY)
  222. print("Source Z: " .. sourceZ)
  223. print("Resolution: " .. screenWidth .. ", " .. screenHeight)
  224. if screenWidth == 3440 and screenHeight == 1440 then widthMagic = 1.125 else widthMagic = 0.92 end
  225. ar.setRelativeMode(true, screenWidth*4 , screenHeight*4 )
  226.  
  227. print("Press enter to assemble cannons")
  228. io.read()
  229.  
  230. cannons[1].assemble()
  231. cannons[2].assemble()
  232. cannons[3].assemble()
  233. cannons[4].assemble()
  234.  
  235. print("assemble complete")
  236.  
  237. local function isFriendly(id)
  238. for _, friendlyID in ipairs(friendlyIDs) do
  239. if id == friendlyID then
  240. return true
  241. end
  242. end
  243. return false
  244. end
  245.  
  246. -- Function to project 3D coordinates to 2D screen coordinates
  247. local function projectToScreen(x, y, z, userPos, lookVector)
  248. local dx = x - userPos[1]
  249. local dy = -(y - userPos[2])
  250. local dz = z - userPos[3]
  251.  
  252. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  253. local playerYaw = math.deg(math.atan2(lz, lx))
  254. local playerPitch = math.deg(math.asin(-ly))
  255. playerYaw = playerYaw + 90
  256. if playerYaw < 0 then
  257. playerYaw = playerYaw + 360
  258. end
  259.  
  260. -- Calculate yaw difference
  261. local targetYaw = math.deg(math.atan2(dz, dx)) + 90
  262. if targetYaw < 0 then
  263. targetYaw = targetYaw + 360
  264. end
  265.  
  266. local yawDiff = targetYaw - playerYaw
  267. if yawDiff > 180 then
  268. yawDiff = yawDiff - 360
  269. elseif yawDiff < -180 then
  270. yawDiff = yawDiff + 360
  271. end
  272.  
  273. if math.abs(yawDiff) > 90 then
  274. return nil, nil
  275. end
  276.  
  277. -- Calculate pitch difference
  278. local distXY = math.sqrt(dx * dx + dz * dz)
  279. local targetPitch = math.deg(math.atan2(dy, distXY))
  280. local pitchDiff = targetPitch - playerPitch
  281.  
  282. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  283. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  284.  
  285. local projectedX, projectedY
  286.  
  287. if yawDiff > 0 then
  288. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  289. else
  290. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  291. end
  292.  
  293. if pitchDiff > 0 then
  294. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  295. else
  296. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  297. end
  298.  
  299. projectedX = math.floor(projectedX / magicNumber)
  300. projectedY = math.floor(projectedY / magicNumber) + 20
  301.  
  302. return projectedX, projectedY
  303. end
  304.  
  305. -- Function to display pitch and yaw of a cannon
  306. local function angleToScreen(pitch, yaw, userPos, lookVector)
  307. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  308. local playerYaw = math.deg(math.atan2(lz, lx))
  309. local playerPitch = math.deg(math.asin(-ly))
  310. playerYaw = playerYaw + 90
  311. if playerYaw < 0 then
  312. playerYaw = playerYaw + 360
  313. end
  314.  
  315. -- Calculate yaw difference
  316. local yawDiff = yaw - playerYaw
  317. if yawDiff > 180 then
  318. yawDiff = yawDiff - 360
  319. elseif yawDiff < -180 then
  320. yawDiff = yawDiff + 360
  321. end
  322.  
  323. if math.abs(yawDiff) > 90 then
  324. return nil, nil
  325. end
  326.  
  327. -- Calculate pitch difference
  328. local pitchDiff = -pitch + playerPitch
  329.  
  330. local screenDepth = screenWidth / 2 * math.tan(math.rad(fov / 1.125 / 2))
  331. local verticalScreenDepth = screenHeight * math.tan(math.rad(fov / 2))
  332.  
  333. local projectedX, projectedY
  334.  
  335. if yawDiff > 0 then
  336. projectedX = screenWidth / 2 + screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  337. else
  338. projectedX = screenWidth / 2 - screenDepth * math.tan(math.rad(math.abs(yawDiff)))
  339. end
  340.  
  341. if pitchDiff > 0 then
  342. projectedY = screenHeight / 2 + screenHeight * math.tan(math.rad(fov / 0.95 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  343. else
  344. projectedY = screenHeight / 2 - screenHeight * math.tan(math.rad(fov / 1.05 / 2)) * math.tan(math.rad(math.abs(pitchDiff)))
  345. end
  346.  
  347. projectedX = math.floor(projectedX / magicNumber)
  348. projectedY = math.floor(projectedY / magicNumber) + 20
  349.  
  350. return projectedX, projectedY
  351. end
  352.  
  353. local function mouseAim(userPos, lookVector)
  354. lookTarget = {}
  355.  
  356. local lx, ly, lz = lookVector[1], lookVector[2], lookVector[3]
  357. local playerYaw = math.deg(math.atan2(lz, lx))
  358. local playerPitch = math.deg(math.asin(-ly)) - 20
  359. playerYaw = playerYaw + 90
  360. if playerYaw < 0 then
  361. playerYaw = playerYaw + 360
  362. end
  363.  
  364. local yawRad = math.rad(playerYaw)
  365. local pitchRad = math.rad(playerPitch)
  366.  
  367. local dx = projectionDistance * math.cos(pitchRad) * math.sin(yawRad)
  368. local dz = -projectionDistance * math.cos(pitchRad) * math.cos(yawRad)
  369. local dy = -projectionDistance * math.sin(pitchRad)
  370.  
  371. local targetX = userPos[1] + dx
  372. local targetY = userPos[2] + dy
  373. local targetZ = userPos[3] + dz
  374. lookTarget = { x = targetX, y = targetY, z = targetZ }
  375. return lookTarget
  376. end
  377.  
  378.  
  379. local function isWithinRenderDistance(projectedX, projectedY, centerX, centerY, maxdx, maxdy, maxd)
  380. local dx = projectedX - centerX / magicNumber
  381. local dy = projectedY - centerY / magicNumber
  382.  
  383. if dx < maxdx then
  384. if dy < maxdy then
  385. local distance = math.sqrt(dx * dx + dy * dy)
  386. if distance <= maxd then
  387. return true
  388. else
  389. return false
  390. end
  391. else
  392. return false
  393. end
  394. else
  395. return false
  396. end
  397. end
  398.  
  399. -- Function to check for overlap and adjust positions
  400. local function filterElements(elements)
  401. table.sort(elements, function(a, b) return a.mass > b.mass end) -- Sort by mass
  402. local filteredElements = {}
  403.  
  404. for i, element in ipairs(elements) do
  405. local overlap = false
  406. for _, filteredElement in ipairs(filteredElements) do
  407. local dx = math.abs(element.screenX - filteredElement.screenX)
  408. local dy = math.abs(element.screenY - filteredElement.screenY)
  409. if dx < 800 / GUIscale and dy < 800 / GUIscale then
  410. overlap = true
  411. element.renderInfo = false
  412. break
  413. end
  414. end
  415.  
  416. table.insert(filteredElements, element)
  417.  
  418. end
  419.  
  420. return filteredElements
  421. end
  422.  
  423. -- Function to render elements in parallel
  424. local function renderElements(elements, cannonHitPos, pInfo, lockedTarget)
  425. local tasks = {}
  426. for _, element in ipairs(elements) do
  427. table.insert(tasks, function()
  428. local color = element.color
  429. if lockedTarget and element.id == lockedTarget.id then
  430. color = 0xFF0000 -- Red color for locked target
  431. end
  432. ar.horizontalLineWithId("frame_top_" .. element.id, element.screenX - element.frameSize, element.screenX + element.frameSize, element.screenY - element.frameSize, color)
  433. ar.horizontalLineWithId("frame_bottom_" .. element.id, element.screenX - element.frameSize, element.screenX + element.frameSize, element.screenY + element.frameSize, color)
  434. ar.verticalLineWithId("frame_left_" .. element.id, element.screenX - element.frameSize, element.screenY - element.frameSize, element.screenY + element.frameSize, color)
  435. ar.verticalLineWithId("frame_right_" .. element.id, element.screenX + element.frameSize, element.screenY - element.frameSize, element.screenY + element.frameSize, color)
  436. if element.renderInfo then
  437. ar.drawString("dist: " .. element.distance, element.screenX - 15, element.screenY - 200 / GUIscale, color)
  438. ar.drawString("mass: " .. math.floor(element.mass / 1000) .. "k", element.screenX - 15, element.screenY - 340 / GUIscale, color)
  439. ar.drawString("speed: " .. element.speed, element.screenX - 15, element.screenY - 480 / GUIscale, color)
  440. ar.drawString("id: " .. element.id, element.screenX - 15, element.screenY - 620 / GUIscale, color)
  441. end
  442. end)
  443. end
  444. parallel.waitForAll(table.unpack(tasks))
  445. end
  446.  
  447. local function lockHighestThreatTarget(elements, centerX, centerY, radius)
  448. local closestTarget = nil
  449. local closestDistance = math.huge -- Initialize to a large number
  450.  
  451. for _, element in ipairs(elements) do
  452. if element.id then
  453. local dx = element.screenX - centerX / magicNumber
  454. local dy = element.screenY - centerY / magicNumber
  455. local distance = math.sqrt(dx * dx + dy * dy)
  456.  
  457. -- Check if the target is within the locking radius on screen
  458. if distance <= radius / GUIscale then
  459. -- If this target is closer to the center than the current closest target, update
  460. if distance < closestDistance then
  461. closestTarget = element
  462. closestDistance = distance
  463. end
  464. end
  465. end
  466. end
  467.  
  468. return closestTarget
  469. end
  470.  
  471. local function modemMessage()
  472. while true do
  473. if modem then
  474. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  475. if channel == cannonHitPosChannel then
  476. cannonHitPos = message
  477. elseif channel == controlChannel then
  478. controls = message
  479. end
  480. else
  481. sleep()
  482. end
  483. end
  484. end
  485.  
  486. local function getAngularVelocity(currentYaw, currentTime)
  487. if lastYaw and lastYawTime then
  488. local deltaTime = currentTime - lastYawTime
  489. local deltaYaw = (currentYaw - lastYaw + math.pi) % (2 * math.pi) - math.pi
  490. return deltaYaw / deltaTime -- Radians per second
  491. end
  492. return 0
  493. end
  494.  
  495. local function findRelativeAngle(targetYaw,targetPitch)
  496. rot = ship.getQuaternion()
  497. cacheYaw = math.pi - math.rad(targetYaw)
  498. cachePitch = - math.rad(targetPitch)
  499. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  500. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  501. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  502. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  503. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  504. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  505. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  506. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  507. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  508.  
  509. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  510. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  511. rotMatTGT31 = - math.sin(cachePitch)
  512.  
  513. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  514. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  515. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  516.  
  517. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  518. barrelPitch = math.asin(-rotMatRSLT31)
  519. return math.deg(-turretYaw),math.deg(-barrelPitch)
  520. end
  521.  
  522. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  523. local radians = math.rad(angle)
  524. local u = projectileSpeed/20
  525. local part1 = u * math.cos(radians) / math.log(cd)
  526. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  527. local XR = part1 * part2
  528.  
  529. return XR
  530. end
  531.  
  532. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  533. local bestLowPitch = nil
  534. local bestHighPitch = nil
  535. local bestLowDistance = math.huge
  536. local bestHighDistance = math.huge
  537. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  538.  
  539. for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
  540. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  541. local distanceDifference = math.abs(calculatedRange - targetDistance)
  542.  
  543. -- Find the low-angle solution
  544. if pitch <= 30 then
  545. if distanceDifference < bestLowDistance then
  546. bestLowDistance = distanceDifference
  547. bestLowPitch = pitch
  548. end
  549. -- Find the high-angle solution
  550. elseif pitch > 30 then
  551. if distanceDifference < bestHighDistance then
  552. bestHighDistance = distanceDifference
  553. bestHighPitch = pitch
  554. end
  555. end
  556. end
  557.  
  558. -- If the target is within 400 blocks, prioritize the low-angle solution
  559. if targetDistance <= 830 then
  560. return bestLowPitch or bestLowPitch
  561. else
  562. return bestHighPitch or bestLowPitch
  563. end
  564. end
  565.  
  566. local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ)
  567. if sourceX and sourceY and sourceZ then
  568. local dx = targetPos.x - sourceX
  569. local dy = targetPos.y - sourceY
  570. local dz = targetPos.z - sourceZ
  571. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  572.  
  573.  
  574. local estimateTime = distance / (projectileSpeed) -- assuming velocity units and time units need adjustment
  575. local estimateX = targetPos.x + targetVel.x * estimateTime
  576. local estimateY = targetPos.y + targetVel.y * estimateTime
  577. local estimateZ = targetPos.z + targetVel.z * estimateTime
  578.  
  579. local dx = estimateX - sourceX
  580. local dy = estimateY - sourceY
  581. local dz = estimateZ - sourceZ
  582.  
  583. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  584. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  585.  
  586. local yaw = math.deg(math.atan2(-dx, dz))
  587. yaw = (yaw + 180) % 360
  588.  
  589. pitch = pitch + findBestPitch(estimateX, estimateY, estimateZ, sourceX, sourceY, sourceZ, projectileSpeed, 0.025, 0.99, 0.0028, projectileSpeed)
  590.  
  591. local shipRoll = math.deg(ship.getRoll())
  592. local shipYaw = math.deg(ship.getYaw()) - 90
  593. if shipYaw < 0 then shipYaw = shipYaw + 360 end
  594. local shipPitch = math.deg(ship.getPitch())
  595.  
  596. local requiredRelativeYaw = yaw - shipYaw
  597. if requiredRelativeYaw > 180 then
  598. requiredRelativeYaw = requiredRelativeYaw - 360
  599. elseif requiredRelativeYaw < -180 then
  600. requiredRelativeYaw = requiredRelativeYaw + 360
  601. end
  602.  
  603. local requiredRelativeYaw,requiredRelativePitch = findRelativeAngle(yaw,pitch)
  604. if math.abs(math.abs(requiredRelativePitch) - math.abs(cannons[1].getPitch())) > 0 then
  605. cannons[1].setPitch(requiredRelativePitch)
  606. cannons[2].setPitch(requiredRelativePitch)
  607. cannons[3].setPitch(requiredRelativePitch)
  608. cannons[4].setPitch(requiredRelativePitch)
  609. end
  610. if math.abs(requiredRelativeYaw - cannons[1].getYaw()) > 0 then
  611. cannons[1].setYaw(requiredRelativeYaw)
  612. cannons[2].setYaw(requiredRelativeYaw)
  613. cannons[3].setYaw(requiredRelativeYaw)
  614. cannons[4].setYaw(requiredRelativeYaw)
  615. end
  616. end
  617. end
  618.  
  619. local function targetLockAndAim()
  620. while true do
  621. source = cannonHitPos[5]
  622. if controls.cannonControlMode == "mouseAim" then
  623. source = ship.getWorldspacePosition()
  624. source.y = source.y + 1
  625. if pInfo.nickname then
  626. lockedTarget = lockHighestThreatTarget(elements, screenWidth / 2, screenHeight / 2, 1000)
  627. if lockedTarget then
  628. print("Locked target ID: " .. lockedTarget.id)
  629. lockedTargetScan = radar.scanForShips(5000)
  630. for _, object in ipairs(lockedTargetScan) do
  631. if object.id == lockedTarget.id then
  632. -- Update the locked target information
  633. lockedTarget.pos = object.pos
  634. lockedTarget.velocity = object.velocity
  635. break
  636. end
  637. end
  638. if lockedTarget.pos then
  639. aimCannon(lockedTarget.pos, lockedTarget.velocity, source.x, source.y, source.z)
  640. end
  641. else
  642. lookTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  643. aimCannon(lookTarget, {x=0, y=0, z=0}, source.x, source.y, source.z)
  644. end
  645. end
  646. else
  647.  
  648. end
  649. sleep()
  650. end
  651. end
  652.  
  653. local function manualCannonControl()
  654. while true do
  655. if controls.cannonControlMode == "manual" then
  656. if controls then
  657. local pitch = cannons[1].getPitch()
  658. local yaw = cannons[1].getYaw()
  659. -- Control yaw and pitch manually with arrow keys
  660. if controls.cannonUp then
  661. -- Increase pitch
  662. cannons[1].setPitch(pitch + 2)
  663. cannons[2].setPitch(pitch + 2)
  664. cannons[3].setPitch(pitch + 2)
  665. cannons[4].setPitch(pitch + 2)
  666. elseif controls.cannonDown then
  667. -- Decrease pitch
  668. cannons[1].setPitch(pitch - 2)
  669. cannons[2].setPitch(pitch - 2)
  670. cannons[3].setPitch(pitch - 2)
  671. cannons[4].setPitch(pitch - 2)
  672. else
  673.  
  674. end
  675. if controls.cannonLeft then
  676. -- Turn left
  677. cannons[1].setYaw(yaw - 2)
  678. cannons[2].setYaw(yaw - 2)
  679. cannons[3].setYaw(yaw - 2)
  680. cannons[4].setYaw(yaw - 2)
  681. elseif controls.cannonRight then
  682. -- Turn right
  683. cannons[1].setYaw(yaw + 2)
  684. cannons[2].setYaw(yaw + 2)
  685. cannons[3].setYaw(yaw + 2)
  686. cannons[4].setYaw(yaw + 2)
  687. else
  688.  
  689. end
  690. else
  691.  
  692. end
  693. sleep()
  694. else
  695. sleep() -- Sleep for a short time if not in manual mode
  696. end
  697. end
  698. end
  699.  
  700. local function firing()
  701. while true do
  702. if controls then
  703. if controls.fireCannon then
  704. cannons[1].fire()
  705. cannons[2].fire()
  706. cannons[3].fire()
  707. cannons[4].fire()
  708. end
  709. end
  710. sleep()
  711. end
  712. end
  713.  
  714. -- Update the main function to remove target locking and aiming
  715. local function main()
  716. while true do
  717. local playerList = radar.scanForPlayers(1000)
  718.  
  719. for _, player in ipairs(playerList) do
  720. local playerName = player.nickname
  721. local playerPosition = player.pos
  722. local lookAngle = player.look_angle
  723.  
  724. if player.nickname == userID then
  725. pInfo = {
  726. nickname = player.nickname,
  727. pos = player.pos,
  728. lookVector = lookAngle
  729. }
  730. end
  731. end
  732.  
  733. if pInfo.nickname then
  734. local objectList = radar.scanForShips(10000)
  735. ar.clear()
  736.  
  737. elements = {}
  738. for _, object in ipairs(objectList) do
  739. local objX, objY, objZ = object.pos.x, object.pos.y, object.pos.z
  740. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  741. 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))
  742. local speed = math.floor(math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2))
  743. local color
  744. if isFriendly(object.id) then
  745. color = 0x009cff -- Friendly, blue color
  746. elseif object.mass < 10000 then
  747. color = 0x999999 -- type unknown/low threat
  748. elseif object.mass < 100000 then
  749. if speed > 40 then
  750. -- high speed low mass, potential missile
  751.  
  752. local missileVector = {object.velocity.x, object.velocity.y, object.velocity.z}
  753. local toPlayerVector = {pInfo.pos[1] - objX, pInfo.pos[2] - objY, pInfo.pos[3] - objZ}
  754. local dotProduct = missileVector[1] * toPlayerVector[1] + missileVector[2] * toPlayerVector[2] + missileVector[3] * toPlayerVector[3]
  755. if dotProduct > 0 then
  756. color = 0xff5600 -- missile coming, high threat
  757. else
  758. color = 0xFFFF00 -- potential threat
  759. end
  760. else
  761. color = 0x00FF00 -- low mass low speed, low threat
  762. end
  763. elseif object.mass < 1000000 then
  764. color = 0xfff700 -- Medium sized vehicle, fighter jet, tanks, helicopter
  765. else
  766. color = 0xffc300 -- Large sized vehicle, ships, strategic bomber
  767. end
  768.  
  769. if distance > 20 then
  770. if screenX and screenY and isWithinRenderDistance(screenX, screenY, screenWidth / 2, screenHeight / 2, 4000 / GUIscale, 4800 / GUIscale, 3200 / GUIscale) then
  771. table.insert(elements, {
  772. id = object.id,
  773. screenX = screenX,
  774. screenY = screenY,
  775. frameSize = 20,
  776. color = color,
  777. distance = distance,
  778. mass = object.mass,
  779. speed = speed,
  780. velocity = object.velocity,
  781. renderInfo = true
  782. })
  783. end
  784. end
  785. end
  786.  
  787. local filteredElements = filterElements(elements)
  788.  
  789. for _, element in ipairs(elements) do
  790. local found = false
  791. for _, filteredElement in ipairs(filteredElements) do
  792. if element.id == filteredElement.id then
  793. found = true
  794. break
  795. end
  796. end
  797. if not found then
  798. element.renderInfo = false
  799. end
  800. end
  801. renderElements(filteredElements, cannonHitPos, pInfo, lockedTarget)
  802. end
  803.  
  804. if pInfo then
  805. local AimTarget = mouseAim(pInfo.pos, pInfo.lookVector)
  806. local screenX, screenY = projectToScreen(AimTarget.x, AimTarget.y, AimTarget.z, pInfo.pos, pInfo.lookVector)
  807. if screenX and screenY then
  808. ar.drawStringWithId("AimCrossHair","X", screenX+20, screenY+80, 0xFFFFFF)
  809. end
  810. end
  811.  
  812. if ship.getVelocity() then
  813. local velocity = ship.getVelocity()
  814. local speed = math.floor(math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2))
  815.  
  816. ar.drawStringWithId("AimCrossHair","speed: "..speed, 100, 500, 0xFFFFFF)
  817. end
  818.  
  819.  
  820. sleep(0.05) -- Adjust the sleep time as needed to balance performance and responsiveness
  821. end
  822. end
  823.  
  824. local function hitMark()
  825. while true do
  826. local playerList = radar.scanForPlayers(1000)
  827. for _, player in ipairs(playerList) do
  828. local playerName = player.nickname
  829. local playerPosition = player.pos
  830. local lookAngle = player.look_angle
  831.  
  832. if player.nickname == userID then
  833. pInfo = {
  834. nickname = player.nickname,
  835. pos = player.pos,
  836. lookVector = lookAngle
  837. }
  838. end
  839. end
  840.  
  841. local shipRoll = math.deg(ship.getRoll())
  842. local shipYaw = math.deg(ship.getYaw()) - 90
  843. if shipYaw < 0 then shipYaw = shipYaw + 360 end
  844. local shipPitch = math.deg(ship.getPitch())
  845.  
  846. local currentPitch = shipPitch + cannons[1].getPitch()
  847. local currentYaw = shipYaw + cannons[1].getYaw() - 90
  848. if currentYaw < 0 then currentYaw = currentYaw + 360 end
  849. source = ship.getWorldspacePosition()
  850. if source and currentYaw and currentPitch then
  851. local dx = projectionDistance * math.cos(math.rad(currentPitch)) * math.sin(math.rad(currentYaw))
  852. local dz = -projectionDistance * math.cos(math.rad(currentPitch)) * math.cos(math.rad(currentYaw))
  853. local dy = projectionDistance * math.sin(math.rad(currentPitch))
  854. objX = source.x + dx
  855. objY = source.y+2 + dy
  856. objZ = source.z + dz
  857. end
  858.  
  859. if objX and objY and objZ then
  860. local screenX, screenY = projectToScreen(objX, objY, objZ, pInfo.pos, pInfo.lookVector)
  861. if screenX and screenY then
  862. ar.clearElement("CrossHair")
  863. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  864. end
  865. elseif cannonHitPos[3] == "degree" then
  866. print("pitch: "..cannonHitPos[1])
  867. print("yaw: "..cannonHitPos[2])
  868. local screenX, screenY = angleToScreen(cannonHitPos[1], cannonHitPos[2], pInfo.pos, pInfo.lookVector)
  869. if screenX and screenY then
  870. ar.clearElement("CrossHair")
  871. ar.drawStringWithId("CrossHair","X", screenX, screenY, 0xFF0000)
  872. end
  873. end
  874. sleep()
  875. end
  876. end
  877.  
  878. -- Main loop
  879. parallel.waitForAny(
  880. modemMessage,
  881. main,
  882. hitMark,
  883. targetLockAndAim,
  884. manualCannonControl,
  885. firing
  886. )
  887.  
Advertisement
Add Comment
Please, Sign In to add comment