MegiRicky

Z-10gun.lua

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