MegiRicky

gunM1AX.lua

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