MegiRicky

gunM1AXGen2.lua

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