MegiRicky

NGDWEVRMouseAimGen3.lua

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