MegiRicky

gunM144.lua

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