MegiRicky

Z-10Camera

Jul 4th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.79 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. local camera = peripheral.find("camera")
  3. local cannon = peripheral.find("cbc_cannon_mount")
  4. local radar = peripheral.find("sp_radar")
  5. if not(cannon) then
  6. cannon = peripheral.find("cbcmf_compact_cannon_mount")
  7. end
  8. cannon.assemble()
  9.  
  10. print("Input the controlChannel, default = 1100")
  11.  
  12. local controlChannel = io.read()
  13. if controlChannel == "" then
  14. controlChannel = 1100
  15. end
  16. controlChannel = tonumber(controlChannel)
  17. modem.open(controlChannel) -- Open a channel to communicate
  18.  
  19. local missileControlChannel = io.read()
  20. if missileControlChannel == "" then
  21. missileControlChannel = 1400
  22. end
  23. missileControlChannel = tonumber(missileControlChannel)
  24. modem.open(missileControlChannel) -- Open a channel to communicate
  25.  
  26. local missileInfoChannel = missileControlChannel + 10
  27. modem.open(missileInfoChannel)
  28.  
  29. local shipPos = {x=0,y=0,z=0}
  30. local shipVel = {x=0,y=0,z=0}
  31. local hitPos, cameraPos = {x=0,y=0,z=0}, {x=0,y=0,z=0}
  32. local g,cd,projectileSpeed = 0.005, 0.99999999999999, 80
  33. local spreadYawRange = 1 -- Max spread angle for yaw (in degrees)
  34. local spreadPitchRange = 1 -- Max spread angle for pitch (in degrees)
  35.  
  36. local lockedTarget = {}
  37.  
  38. local missileId = 1
  39. local missileLaunched = {}
  40. local controls = {
  41. {cannonControlMode = "manual"},
  42. fireMissile = {}
  43. }
  44. local LaunchedMissiles = {}
  45. local missileControls = {
  46. fireMissile = {}
  47. }
  48. local immediateMissileInfo = {}
  49. local AIMInfoList,GBUInfoList,thunderBoltInfoList,AGMInfoList = {},{},{},{}
  50. local AIMCount,GBUCount,thunderboltCount,AGMCount = 0,0,0,0
  51. local missileCoolDown = 20
  52.  
  53. local function distance(a, b)
  54. return math.sqrt((a.x - b.x)^2 + (a.y - b.y)^2 + (a.z - b.z)^2)
  55. end
  56.  
  57. -- Normalize a vector
  58. local function normalizeVector(v)
  59. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  60. if length == 0 then
  61. return {0, 0, 0}
  62. end
  63. return {v[1] / length, v[2] / length, v[3] / length}
  64. end
  65.  
  66. -- Normalize the rotation matrix
  67. local function normalizeRotationMatrix(rotMatrix)
  68. local normalizedMatrix = {}
  69. for i = 1, #rotMatrix do
  70. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  71. end
  72. return normalizedMatrix
  73. end
  74.  
  75. -- Get the pitch of the ship
  76. local function getPitch()
  77. local rotMatrix = ship.getTransformationMatrix()
  78. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  79. return math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  80. end
  81. --pitch = math.deg(math.asin(ship.getTransformationMatrix()[2][3]))
  82. -- Get the yaw of the ship
  83. local function getYaw()
  84. local rotMatrix = ship.getTransformationMatrix()
  85. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  86. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  87. end
  88.  
  89. -- Get the roll of the ship
  90. local function getRoll()
  91. local rotMatrix = ship.getTransformationMatrix()
  92. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  93. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  94. end
  95.  
  96. local function designation()
  97. -- Check if camera exists
  98. if not camera then
  99. print("Error: Camera peripheral not found")
  100. return
  101. end
  102. camera.setClipRange(200)
  103. local result = camera.clip()
  104. --local result = {hit={x=0,y=0,z=0}}
  105.  
  106. if result and result.hit then
  107. local hitPos = {
  108. x = result.hit.x,
  109. y = result.hit.y,
  110. z = result.hit.z,
  111. }
  112.  
  113. return hitPos
  114. end
  115. return
  116. end
  117.  
  118. local function updateInfo()
  119. while true do
  120. shipPos = ship.getWorldspacePosition()
  121. shipVel = ship.getVelocity()
  122. cameraPos = camera.getCameraPosition()
  123. allShips = radar.scanForShips(9999)
  124. hitPos = designation()
  125. currentTime = os.clock()
  126. sleep()
  127. end
  128. end
  129.  
  130. local function modemMessage()
  131. while true do
  132. if modem then
  133. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  134. if channel == cannonHitPosChannel then
  135. cannonHitPos = message
  136. elseif channel == controlChannel then
  137. controls = message
  138. elseif channel == missileInfoChannel then
  139. immediateMissileInfo = message
  140. end
  141. else
  142. sleep()
  143. end
  144. end
  145. end
  146.  
  147. local function findRelativeAngle(targetYaw,targetPitch)
  148. rot = ship.getQuaternion()
  149. cacheYaw = math.pi - math.rad(targetYaw)
  150. cachePitch = - math.rad(targetPitch)
  151. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  152. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  153. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  154. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  155. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  156. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  157. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  158. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  159. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  160.  
  161. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  162. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  163. rotMatTGT31 = - math.sin(cachePitch)
  164.  
  165. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  166. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  167. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  168.  
  169. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  170. barrelPitch = math.asin(-rotMatRSLT31)
  171. return math.deg(-turretYaw),math.deg(-barrelPitch)
  172. end
  173.  
  174. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  175. local radians = math.rad(angle)
  176. local u = projectileSpeed/20
  177. local part1 = u * math.cos(radians) / math.log(cd)
  178. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  179. local XR = part1 * part2
  180.  
  181. return XR
  182. end
  183.  
  184. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  185. local bestLowPitch = nil
  186. local bestHighPitch = nil
  187. local bestLowDistance = math.huge
  188. local bestHighDistance = math.huge
  189. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  190.  
  191. for pitch = 0, 90, 0.1 do -- Iterate over pitch angles
  192. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  193. local distanceDifference = math.abs(calculatedRange - targetDistance)
  194.  
  195. -- Find the low-angle solution
  196. if pitch <= 30 then
  197. if distanceDifference < bestLowDistance then
  198. bestLowDistance = distanceDifference
  199. bestLowPitch = pitch
  200. end
  201. -- Find the high-angle solution
  202. elseif pitch > 30 then
  203. if distanceDifference < bestHighDistance then
  204. bestHighDistance = distanceDifference
  205. bestHighPitch = pitch
  206. end
  207. end
  208. end
  209.  
  210. -- If the target is within 400 blocks, prioritize the low-angle solution
  211. return bestLowPitch
  212. end
  213.  
  214. local function applySpread(targetYaw, targetPitch, distance)
  215. -- Avoid division by zero
  216. if distance < 1 then distance = 1 end
  217.  
  218. -- Adjust angle spread inversely with distance (constant spatial spread)
  219. -- Example: spreadAngle = atan(spreadSize / distance)
  220. -- Let’s assume a constant spread size of 1 block/metre
  221. local spatialSpreadSize = 1 -- change as needed
  222. local effectiveYawSpread = math.deg(math.atan(spatialSpreadSize / distance)) * spreadYawRange
  223. local effectivePitchSpread = math.deg(math.atan(spatialSpreadSize / distance)) * spreadPitchRange
  224.  
  225. -- Apply the adjusted spread
  226. local yawSpread = (math.random() * 2 - 1) * effectiveYawSpread
  227. local pitchSpread = (math.random() * 2 - 1) * effectivePitchSpread
  228.  
  229. local newYaw = targetYaw + yawSpread
  230. local newPitch = targetPitch + pitchSpread
  231.  
  232. return newYaw, newPitch
  233. end
  234.  
  235. local lastTargetPos, lastTargetVelocity, predictedTargetVelocity, predictedTargetAcceleration
  236. local lastTargetAqquireTime = os.clock()
  237.  
  238. local function predictFuturePosition(targetPos, targetVel, source)
  239. local currentTime = os.clock()
  240. local dt = currentTime - lastTargetAqquireTime
  241. --print(textutils.serialize(targetVel))
  242. local isStaticTarget = targetVel.x == 0 and targetVel.y == 0 and targetVel.z == 0
  243.  
  244. if isStaticTarget then
  245. -- Predict only using ship's velocity (target assumed static)
  246. local dx = targetPos.x - source.x
  247. local dy = targetPos.y - source.y
  248. local dz = targetPos.z - source.z
  249. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  250. local estimateTime = distance / projectileSpeed + 0.5
  251.  
  252. return {
  253. x = targetPos.x - shipVel.x * estimateTime,
  254. y = targetPos.y - shipVel.y * estimateTime,
  255. z = targetPos.z - shipVel.z * estimateTime
  256. }
  257. end
  258.  
  259. -- Dynamic target logic (same as before)
  260. if not lastTargetPos then
  261. lastTargetPos = targetPos
  262. lastTargetVelocity = targetVel
  263. predictedTargetVelocity = targetVel
  264. predictedTargetAcceleration = {x = 0, y = 0, z = 0}
  265. lastTargetAqquireTime = currentTime
  266. elseif dt >= 0.5 then
  267. predictedTargetVelocity = {
  268. x = (targetPos.x - lastTargetPos.x) / dt,
  269. y = (targetPos.y - lastTargetPos.y) / dt,
  270. z = (targetPos.z - lastTargetPos.z) / dt
  271. }
  272.  
  273. predictedTargetAcceleration = {
  274. x = (predictedTargetVelocity.x - lastTargetVelocity.x) / dt,
  275. y = (predictedTargetVelocity.y - lastTargetVelocity.y) / dt,
  276. z = (predictedTargetVelocity.z - lastTargetVelocity.z) / dt
  277. }
  278.  
  279. lastTargetPos = targetPos
  280. lastTargetVelocity = predictedTargetVelocity
  281. lastTargetAqquireTime = currentTime
  282. end
  283.  
  284. -- Estimate future position (first pass)
  285. local dx = targetPos.x - source.x
  286. local dy = targetPos.y - source.y
  287. local dz = targetPos.z - source.z
  288. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  289. local estimateTime = distance / projectileSpeed + 0.5
  290.  
  291. local ex = targetPos.x
  292. + (predictedTargetVelocity.x - shipVel.x) * estimateTime
  293. + 0.5 * predictedTargetAcceleration.x * estimateTime * estimateTime
  294.  
  295. local ey = targetPos.y
  296. + (predictedTargetVelocity.y - shipVel.y) * estimateTime
  297. + 0.5 * predictedTargetAcceleration.y * estimateTime * estimateTime
  298.  
  299. local ez = targetPos.z
  300. + (predictedTargetVelocity.z - shipVel.z) * estimateTime
  301. + 0.5 * predictedTargetAcceleration.z * estimateTime * estimateTime
  302.  
  303. -- Refine
  304. dx = ex - source.x; dy = ey - source.y; dz = ez - source.z
  305. distance = math.sqrt(dx*dx + dy*dy + dz*dz)
  306. estimateTime = distance / projectileSpeed + 0.2
  307.  
  308. ex = targetPos.x
  309. + (predictedTargetVelocity.x - shipVel.x) * estimateTime
  310. + 0.5 * predictedTargetAcceleration.x * estimateTime * estimateTime
  311.  
  312. ey = targetPos.y
  313. + (predictedTargetVelocity.y - shipVel.y) * estimateTime
  314. + 0.5 * predictedTargetAcceleration.y * estimateTime * estimateTime
  315.  
  316. ez = targetPos.z
  317. + (predictedTargetVelocity.z - shipVel.z) * estimateTime
  318. + 0.5 * predictedTargetAcceleration.z * estimateTime * estimateTime
  319.  
  320. return {x = ex, y = ey, z = ez}
  321. end
  322.  
  323. local requiredRelativePitch,requiredRelativeYaw = 0,180
  324. local function aimCannon(targetPos, targetVel, source)
  325. if source and source.x then
  326. futurePos = predictFuturePosition(targetPos, targetVel, source)
  327.  
  328. --print(textutils.serialize(futurePos))
  329. -- Calculate the new dx, dy, dz for the predicted future position
  330. dx = futurePos.x - source.x
  331. dy = futurePos.y - source.y
  332. dz = futurePos.z - source.z
  333.  
  334. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  335. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  336. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  337.  
  338. local yaw = math.deg(math.atan2(-dx, dz))
  339. yaw = (yaw + 180) % 360
  340.  
  341. --Adjust pitch based on distance adjustments
  342. local ballisticPitch = findBestPitch(futurePos.x,futurePos.y,futurePos.z, source.x,source.y,source.z, projectileSpeed, g, cd, 0.0028, projectileSpeed)
  343. print(ballisticPitch)
  344. if ballisticPitch then
  345. pitch = pitch + ballisticPitch
  346. end
  347.  
  348. yaw, pitch = applySpread(yaw, pitch, distance)
  349.  
  350. requiredRelativeYaw,requiredRelativePitch = findRelativeAngle(yaw,pitch)
  351. locked = true
  352. end
  353. end
  354.  
  355. local lengthCorrection = -8.5
  356. local widthCorrection = 0
  357. local heightCorrection = -1.5
  358. local lastRenderTime = os.clock()
  359. local function aimGun()
  360. while true do
  361. if controls and controls.cannonControlMode == "mouseAim" then
  362. adjustedPos = {x=0,y=0,z=0}
  363. adjustedPos.x = shipPos.x + lengthCorrection * math.cos(getYaw() - math.pi/2) + widthCorrection * math.cos(getYaw())
  364. adjustedPos.y = shipPos.y + heightCorrection
  365. adjustedPos.z = shipPos.z + lengthCorrection * math.sin(getYaw() - math.pi/2) + widthCorrection * math.sin(getYaw())
  366.  
  367. if lockedTarget and lockedTarget.pos and lockedTarget.pos.x and controls.lockShip then
  368. --print(textutils.serialize(lockedTarget.velocity))
  369. aimCannon(lockedTarget.pos,lockedTarget.velocity,cameraPos)
  370. else
  371. if currentTime - 0.2 > lastRenderTime then
  372. if hitPos and hitPos.x then
  373. camera.outlineToUser(
  374. hitPos.x, hitPos.y, hitPos.z,
  375. "UP", -- Default direction
  376. 0xFFFFFF, -- White
  377. "designation" -- Slot ID
  378. )
  379. end
  380. lastRenderTime = currentTime
  381. end
  382. aimCannon(hitPos,{x=0,y=0,z=0},cameraPos)
  383. end
  384. end
  385. if controls and controls.weaponChoosen == "gun" and controls.fire then
  386. cannon.fire()
  387. end
  388. sleep()
  389. end
  390. end
  391.  
  392. local rocketSpeed = 60 -- blocks per second (assumed, you can tweak)
  393. -- Convert pitch and yaw to directional unit vector
  394. local function getLaunchVector(pitchDeg, yawDeg)
  395. local pitchRad = math.rad(pitchDeg)
  396. local yawRad = math.rad(yawDeg)
  397.  
  398. return {
  399. x = -math.sin(yawRad) * math.cos(pitchRad),
  400. y = math.sin(pitchRad),
  401. z = math.cos(yawRad) * math.cos(pitchRad),
  402. }
  403. end
  404.  
  405. local function solveQuadratic(a, b, c)
  406. local discriminant = b*b - 4*a*c
  407. if discriminant < 0 then return nil end
  408.  
  409. local sqrtDisc = math.sqrt(discriminant)
  410. local t1 = (-b + sqrtDisc) / (2 * a)
  411. local t2 = (-b - sqrtDisc) / (2 * a)
  412.  
  413. -- Return the positive time
  414. if t1 > 0 and t2 > 0 then
  415. return math.min(t1, t2)
  416. elseif t1 > 0 then
  417. return t1
  418. elseif t2 > 0 then
  419. return t2
  420. else
  421. return nil
  422. end
  423. end
  424.  
  425. local function estimateRocketImpact(launchPos, landPos, pitchDeg, yawDeg)
  426. local dir = getLaunchVector(pitchDeg, yawDeg)
  427. local vy = rocketSpeed * dir.y
  428. local dy = landPos.y - launchPos.y
  429.  
  430. -- Kinematic: dy = vy * t - 0.5 * g * t^2
  431. local a = -0.5 * 10
  432. local b = vy
  433. local c = -dy
  434.  
  435. local t = solveQuadratic(a, b, c)
  436. if not t then return nil end
  437.  
  438. return {
  439. x = launchPos.x + rocketSpeed * dir.x * t,
  440. y = landPos.y,
  441. z = launchPos.z + rocketSpeed * dir.z * t
  442. }
  443. end
  444.  
  445. local function aimRocket()
  446. while true do
  447. if controls and controls.weaponChoosen == "rocket" then
  448. camera.forcePitchYaw(20, 180)
  449. requiredRelativePitch,requiredRelativeYaw = -20,180
  450. if currentTime - 0.2 > lastRenderTime then
  451. if hitPos and hitPos.x then
  452. print(math.deg(-getPitch()))
  453. estimateLandPos = estimateRocketImpact(shipPos, hitPos, math.deg(-getPitch())-20, math.deg(getYaw()))
  454. print(textutils.serialize(estimateLandPos))
  455. if estimateLandPos and estimateLandPos.x then
  456. camera.outlineToUser(
  457. estimateLandPos.x, estimateLandPos.y-1, estimateLandPos.z,
  458. "UP", -- Default direction
  459. 0xFF0000, -- Red
  460. "designation" -- Slot ID
  461. )
  462. end
  463. end
  464. lastRenderTime = currentTime
  465. end
  466.  
  467. end
  468. sleep()
  469. end
  470. end
  471.  
  472. local function setGunAngle()
  473. while true do
  474. if requiredRelativePitch and requiredRelativeYaw then
  475. cannon.setPitch(requiredRelativePitch)
  476. cannon.setYaw(requiredRelativeYaw)
  477. end
  478. sleep()
  479. end
  480. end
  481.  
  482. local HOLD_THRESH = 1
  483.  
  484. -- ░░ VECTOR HELPERS ░░
  485. local function angleBetween(a,b) -- deg
  486. local function vecLen(v) return math.sqrt(v.x*v.x+v.y*v.y+v.z*v.z) end
  487. local function vecDot(a,b) return a.x*b.x + a.y*b.y + a.z*b.z end
  488. local function vecNorm(v) local l=vecLen(v); return {x=v.x/l,y=v.y/l,z=v.z/l} end
  489. return math.deg(math.acos(math.min(1, math.max(-1, vecDot(vecNorm(a),vecNorm(b))))))
  490. end
  491.  
  492. -- ░░ WORLD → PITCH/YAW (ship local) ░░
  493. local function vectorToPitchYaw(v)
  494. local pitch = math.deg(math.asin(v.y))
  495. local yaw = -math.deg(math.atan2(-v.x, -v.z))
  496. return pitch, yaw
  497. end
  498. local lastLocalFwd = camera.getLocViewForward() -- initial reference
  499. local lockedWorldFwd = camera.getAbsViewForward()
  500. local lockPos = {x=0,y=0,z=0}
  501. local lockedCoordinate
  502. -- ░░ Lock camera ░░
  503. local function lockCoordinate()
  504. while true do
  505. if controls and controls.lockCoordinate then
  506. local dx = lockPos.x - cameraPos.x
  507. local dy = lockPos.y - cameraPos.y
  508. local dz = lockPos.z - cameraPos.z
  509.  
  510. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  511. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  512. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  513.  
  514. local yaw = math.deg(math.atan2(-dx, dz))
  515. yaw = (yaw + 180) % 360
  516.  
  517. local localYaw, localPitch = findRelativeAngle(yaw,pitch)
  518.  
  519. camera.forcePitchYaw(-localPitch, localYaw)
  520.  
  521. lockedCoordinate = {
  522. type = "waypoint",
  523. pos = lockPos,
  524. id = 0
  525. }
  526. --lastLocalFwd = curlocalFwd
  527. else
  528. lockPos = hitPos
  529. lockedCoordinate = nil
  530. end
  531. sleep(0.001)
  532. end
  533. end
  534.  
  535. local function lockShip()
  536. while true do
  537. if controls and controls.lockShip and lockedTarget and lockedTarget.pos then
  538. --print("locking ship")
  539. local dx = lockedTarget.pos.x - cameraPos.x
  540. local dy = lockedTarget.pos.y - cameraPos.y
  541. local dz = lockedTarget.pos.z - cameraPos.z
  542.  
  543. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  544. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  545. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  546.  
  547. local yaw = math.deg(math.atan2(-dx, dz))
  548. yaw = (yaw + 180) % 360
  549.  
  550. local localYaw, localPitch = findRelativeAngle(yaw,pitch)
  551.  
  552. camera.forcePitchYaw(-localPitch, localYaw)
  553. end
  554. sleep()
  555. end
  556. end
  557.  
  558. local function hslToRGB(h, s, l)
  559. local function hue2rgb(p, q, t)
  560. if t < 0 then t = t + 1 end
  561. if t > 1 then t = t - 1 end
  562. if t < 1/6 then return p + (q - p) * 6 * t end
  563. if t < 1/2 then return q end
  564. if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
  565. return p
  566. end
  567.  
  568. local r, g, b
  569. if s == 0 then
  570. r, g, b = l, l, l -- achromatic
  571. else
  572. local q = l < 0.5 and l * (1 + s) or l + s - l * s
  573. local p = 2 * l - q
  574. r = hue2rgb(p, q, h + 1/3)
  575. g = hue2rgb(p, q, h)
  576. b = hue2rgb(p, q, h - 1/3)
  577. end
  578. return bit32.lshift(math.floor(r * 255), 16) +
  579. bit32.lshift(math.floor(g * 255), 8) +
  580. math.floor(b * 255)
  581. end
  582.  
  583. local function clusterShips(ships)
  584. local clusters = {}
  585.  
  586. for _, ship in ipairs(ships) do
  587. local added = false
  588. for _, cluster in ipairs(clusters) do
  589. for _, other in ipairs(cluster.members) do
  590. if distance(ship.pos, other.pos) < 20 then
  591. table.insert(cluster.members, ship)
  592. cluster.massSum = cluster.massSum + (ship.mass or 0)
  593. if (ship.mass or 0) > (cluster.main.mass or 0) then
  594. cluster.main = ship
  595. end
  596. added = true
  597. break
  598. end
  599. end
  600. if added then break end
  601. end
  602.  
  603. if not added then
  604. table.insert(clusters, {
  605. members = { ship },
  606. main = ship,
  607. massSum = ship.mass or 0
  608. })
  609. end
  610. end
  611. --calculate color
  612. local result = {}
  613. local MIN_MASS, MAX_MASS = 10000, 200000
  614. for _, cluster in ipairs(clusters) do
  615. local m = math.max(MIN_MASS, math.min(cluster.massSum, MAX_MASS))
  616. local lightness = 0.85 - ((m - MIN_MASS) / (MAX_MASS - MIN_MASS)) * 0.5 -- 从 0.85 到 0.35
  617. local color = hslToRGB(0.13, 1.0, lightness) -- hue 0.13 ≈ 黄橙色
  618.  
  619. table.insert(result, {
  620. pos = cluster.main.pos,
  621. id = cluster.main.id,
  622. mass = cluster.massSum,
  623. velocity = cluster.main.velocity,
  624. type = "ship",
  625. color = color
  626. })
  627. end
  628.  
  629. return result
  630. end
  631.  
  632. local function renderShips()
  633. while true do
  634. lockedTarget = nil
  635. local validShips = {}
  636.  
  637. -- 筛选质量与距离合格的目标
  638. for _, ship in ipairs(allShips or {}) do
  639. if ship.mass and ship.mass >= 10000 and distance(ship.pos, cameraPos) >= 20 then
  640. table.insert(validShips, ship)
  641. end
  642. end
  643.  
  644. local shipsToRender = clusterShips(validShips)
  645. local camForward = camera.getAbsViewForward()
  646.  
  647. local minAngle = 10
  648. local bestTarget = nil
  649.  
  650. -- 第一遍扫描,寻找最小角度目标
  651. for _, ship in ipairs(shipsToRender) do
  652. local dirToShip = {
  653. x = ship.pos.x - cameraPos.x,
  654. y = ship.pos.y - cameraPos.y,
  655. z = ship.pos.z - cameraPos.z,
  656. }
  657.  
  658. local angle = angleBetween(dirToShip, camForward)
  659.  
  660. if angle < minAngle then
  661. minAngle = angle
  662. bestTarget = ship
  663. end
  664. end
  665.  
  666. -- 第二遍绘制所有目标,并标注最佳目标
  667. for _, ship in ipairs(shipsToRender) do
  668. local outlineColor = ship.color
  669.  
  670. if bestTarget and ship.id == bestTarget.id then
  671. outlineColor = controls and controls.lockShip and 0x882200 or 0xCC5500
  672. lockedTarget = ship
  673. end
  674.  
  675. camera.outlineToUser(
  676. ship.pos.x, ship.pos.y - 2, ship.pos.z,
  677. "UP",
  678. outlineColor,
  679. tostring(ship.id)
  680. )
  681. end
  682.  
  683. sleep(0.5)
  684. end
  685. end
  686.  
  687. local function missileListBuilding()
  688. while true do
  689. if immediateMissileInfo and immediateMissileInfo.id and immediateMissileInfo.type then
  690. local foundMissile = false
  691.  
  692. -- Update the existing missile state instead of adding duplicates
  693. for _, missileInfo in ipairs(AIMInfoList) do
  694. if missileInfo.id == immediateMissileInfo.id then
  695. missileInfo.launchState = immediateMissileInfo.launchState
  696. foundMissile = true
  697. break
  698. end
  699. end
  700. for _, missileInfo in ipairs(GBUInfoList) do
  701. if missileInfo.id == immediateMissileInfo.id then
  702. missileInfo.launchState = immediateMissileInfo.launchState
  703. foundMissile = true
  704. break
  705. end
  706. end
  707. for _, missileInfo in ipairs(thunderBoltInfoList) do
  708. if missileInfo.id == immediateMissileInfo.id then
  709. missileInfo.launchState = immediateMissileInfo.launchState
  710. foundMissile = true
  711. break
  712. end
  713. end
  714. for _, missileInfo in ipairs(AGMInfoList) do
  715. if missileInfo.id == immediateMissileInfo.id then
  716. missileInfo.launchState = immediateMissileInfo.launchState
  717. foundMissile = true
  718. break
  719. end
  720. end
  721.  
  722. -- If it's a new missile, add it to the list
  723. if not foundMissile then
  724. if immediateMissileInfo.type == "AIM-220" then
  725. table.insert(AIMInfoList, immediateMissileInfo)
  726. elseif immediateMissileInfo.type == "GBU-42" then
  727. table.insert(GBUInfoList, immediateMissileInfo)
  728. elseif immediateMissileInfo.type == "thunderBolt" then
  729. table.insert(thunderBoltInfoList, immediateMissileInfo)
  730. elseif immediateMissileInfo.type == "AGM-134" then
  731. table.insert(AGMInfoList, immediateMissileInfo)
  732. end
  733. end
  734. end
  735.  
  736. -- Remove missiles that have launched
  737. for i = #AIMInfoList, 1, -1 do
  738. if AIMInfoList[i].launchState == true then
  739. table.remove(AIMInfoList, i)
  740. end
  741. end
  742. for i = #GBUInfoList, 1, -1 do
  743. if GBUInfoList[i].launchState == true then
  744. table.remove(GBUInfoList, i)
  745. end
  746. end
  747. for i = #thunderBoltInfoList, 1, -1 do
  748. if thunderBoltInfoList[i].launchState == true then
  749. table.remove(thunderBoltInfoList, i)
  750. end
  751. end
  752. for i = #AGMInfoList, 1, -1 do
  753. if AGMInfoList[i].launchState == true then
  754. table.remove(AGMInfoList, i)
  755. end
  756. end
  757.  
  758. -- Count unlaunched missiles
  759. AIMCount, GBUCount, thunderboltCount = 0, 0, 0
  760. for _, missileInfo in ipairs(AIMInfoList) do
  761. if not missileInfo.launchState then
  762. AIMCount = AIMCount + 1
  763. end
  764. end
  765. for _, missileInfo in ipairs(GBUInfoList) do
  766. if not missileInfo.launchState then
  767. GBUCount = GBUCount + 1
  768. end
  769. end
  770. for _, missileInfo in ipairs(thunderBoltInfoList) do
  771. if not missileInfo.launchState then
  772. thunderboltCount = thunderboltCount + 1
  773. end
  774. end
  775. for _, missileInfo in ipairs(AGMInfoList) do
  776. if not missileInfo.launchState then
  777. AGMCount = AGMCount + 1
  778. end
  779. end
  780. sleep()
  781. end
  782. end
  783.  
  784. local function launchMissile(targetType, targetId, targetPos, missileType)
  785. local currentMissile = nil
  786.  
  787. -- Function to find and remove the first unlaunched missile from a list
  788. local function findUnlaunchedMissile(missileList)
  789. for i = #missileList, 1, -1 do -- Iterate in reverse to avoid index shifting issues
  790. local missile = missileList[i]
  791. if missile.launchState == false then
  792. missile.launchState = true -- Mark missile as launched
  793. table.insert(LaunchedMissiles, missile) -- Move to launched list
  794. return table.remove(missileList, i) -- Remove from available list and return it
  795. end
  796. end
  797. return nil
  798. end
  799.  
  800.  
  801. -- Get the first unlaunched missile of the specified type
  802. if missileType == "AIM-220" then
  803. currentMissile = findUnlaunchedMissile(AIMInfoList)
  804. elseif missileType == "GBU-42" then
  805. currentMissile = findUnlaunchedMissile(GBUInfoList)
  806. elseif missileType == "thunderBolt" then
  807. currentMissile = findUnlaunchedMissile(thunderBoltInfoList)
  808. elseif missileType == "AGM-134" then
  809. currentMissile = findUnlaunchedMissile(AGMInfoList)
  810. end
  811.  
  812. -- If no unlaunched missile is found, exit function
  813. if not currentMissile then
  814. print("No available unlaunched missile of type:", missileType)
  815. return
  816. end
  817.  
  818. local currentMissileId = currentMissile.id
  819. print("Launching missile " .. currentMissileId)
  820.  
  821. -- Assign the missile as launched and set target info
  822. if targetType == "ship" then
  823. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  824. else
  825. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  826. end
  827. end
  828.  
  829. local function missileHandler()
  830. while true do
  831. --print(textutils.serialize(AGMInfoList))
  832. if controls and controls.fire then
  833. if controls.weaponChoosen == "AIM-220" or controls.weaponChoosen == "GBU-42" or controls.weaponChoosen == "thunderBolt" or controls.weaponChoosen == "AGM-134" then
  834. print("Firing missile")
  835. if controls.lockCoordinate then
  836. if lockedCoordinate and lockedCoordinate.pos and missileCoolDown == 0 then
  837. launchMissile("waypoint",lockedCoordinate.id,lockedCoordinate.pos,controls.weaponChoosen)
  838. missileCoolDown = 20
  839. modem.transmit(missileControlChannel,missileControlChannel,missileControls)
  840. end
  841. elseif controls.lockShip then
  842. if lockedTarget and lockedTarget.id and missileCoolDown == 0 then
  843. print("Launching at ship")
  844. launchMissile("ship",lockedTarget.id,lockedTarget.pos,controls.weaponChoosen) --target is a ship
  845. missileCoolDown = 20
  846. modem.transmit(missileControlChannel,missileControlChannel,missileControls)
  847. end
  848. end
  849. end
  850. end
  851. missileCoolDown = math.max(missileCoolDown - 1,0)
  852. sleep()
  853. end
  854. end
  855.  
  856. parallel.waitForAny(
  857. modemMessage,
  858. updateInfo,
  859. aimGun,
  860. aimRocket,
  861. setGunAngle,
  862. lockCoordinate,
  863. lockShip,
  864. renderShips,
  865. missileHandler,
  866. missileListBuilding
  867. )
Advertisement
Add Comment
Please, Sign In to add comment