MegiRicky

AIM-220Control.lua

Feb 16th, 2025 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.11 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local modem = peripheral.find("modem")
  3. local topFlap = peripheral.wrap("top")
  4. local bottomFlap = peripheral.wrap("bottom")
  5. local leftFlap = peripheral.wrap("back")
  6. local rightFlap = peripheral.wrap("front")
  7. local redrouter = peripheral.find("redrouter")
  8. local jet = peripheral.find("jet")
  9.  
  10. redstone.setOutput("front",false)
  11. redstone.setOutput("left",false)
  12. redstone.setOutput("bottom",false)
  13. redstone.setOutput("right",false)
  14. redstone.setOutput("back",false)
  15. redstone.setOutput("top",false)
  16. redrouter.setOutput("front",false)
  17. redrouter.setOutput("left",false)
  18. redrouter.setOutput("bottom",false)
  19. redrouter.setOutput("right",false)
  20. redrouter.setOutput("back",false)
  21. redrouter.setOutput("top",false)
  22. jet.setOutputThrust(0)
  23.  
  24. local controls = {}
  25. local target = {}
  26. local lockedId = nil
  27. local targetPos, targetVel, TargetYaw, TargetPitch
  28. local Kp_yaw, Ki_yaw, Kd_yaw = 0.18, 0.005, 0.008
  29. local Kp_pitch, Ki_pitch, Kd_pitch = 0.24, 0.065, 0.0052
  30. local Kp_roll, Ki_roll,Kd_roll = 0.052,0,0.0035
  31. local Kp_throttle, Ki_throttle, Kd_throttle = 0.2, 0.1, 0.02
  32. local K_pitchDiff, k_heightDiff = 1,0
  33. local projectedMissileY,yLevelDiff,initialDistance,targetAltitude
  34. local closestDistance = math.huge
  35.  
  36. local highThrust = 20000
  37.  
  38. -- Initialize error, integral, and derivative terms for yaw and pitch
  39. local yawError = 0
  40. local yawIntegral = 0
  41. local yawPrevError = 0
  42.  
  43. local pitchError = 0
  44. local pitchIntegral = 0
  45. local pitchHeightPrevError = 0
  46.  
  47. local rollError,rollIntegral,rollPrevError = 0,0,0
  48.  
  49. local dt = 0.1
  50.  
  51. local deltaDistance = 0
  52. local lastDistance = 0
  53. local lastTime = os.clock()
  54.  
  55. local controlsChannel = 1400
  56. local missileInfoChannel = controlsChannel + 10
  57. if modem then
  58. modem.open(controlsChannel)
  59. modem.open(missileInfoChannel)
  60. end
  61.  
  62. -- Normalize a vector
  63. local function normalizeVector(v)
  64. local length = math.sqrt(v[1]^2 + v[2]^2 + v[3]^2)
  65. if length == 0 then
  66. return {0, 0, 0}
  67. end
  68. return {v[1] / length, v[2] / length, v[3] / length}
  69. end
  70.  
  71. -- Normalize the rotation matrix
  72. local function normalizeRotationMatrix(rotMatrix)
  73. local normalizedMatrix = {}
  74. for i = 1, #rotMatrix do
  75. normalizedMatrix[i] = normalizeVector(rotMatrix[i])
  76. end
  77. return normalizedMatrix
  78. end
  79.  
  80. -- Get the pitch of the ship
  81. local function getPitch()
  82. local rotMatrix = ship.getTransformationMatrix()
  83. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  84. return -math.asin(normalizedMatrix[2][3]) -- Extract pitch from the matrix
  85. end
  86.  
  87. -- Get the yaw of the ship
  88. local function getYaw()
  89. local rotMatrix = ship.getTransformationMatrix()
  90. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  91. return math.atan2(-normalizedMatrix[3][1], -normalizedMatrix[3][3]) -- Extract yaw from the matrix
  92. end
  93.  
  94. -- Get the roll of the ship
  95. local function getRoll()
  96. local rotMatrix = ship.getTransformationMatrix()
  97. local normalizedMatrix = normalizeRotationMatrix(rotMatrix)
  98. return math.atan2(normalizedMatrix[2][1], normalizedMatrix[2][2]) -- Extract roll from the matrix
  99. end
  100.  
  101. local function modemMessage()
  102. while true do
  103. if modem then
  104. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  105. if channel == controlsChannel then
  106. controls = message
  107. end
  108. else
  109. sleep()
  110. end
  111. end
  112. end
  113.  
  114. local function PIDController(Kp, Ki, Kd, error, integral, derivative, prevError, dt)
  115. -- Calculate the proportional, integral, and derivative components
  116. local proportional = Kp * error
  117. integral = integral + error * dt
  118. derivative = (error - prevError) / dt
  119.  
  120. -- Calculate output
  121. local output = proportional + (Ki * integral) + (Kd * derivative)
  122.  
  123. -- Return the PID output and updated integral and previous error
  124. return output, integral, error
  125. end
  126.  
  127. local function calculateProjectedY(currentY, currentPitch, currentVelocity, distance)
  128. -- Convert pitch to radians for calculation
  129. local pitchRad = math.rad(currentPitch)
  130. -- Estimate the change in Y based on the pitch, velocity, and distance
  131. local projectedY = currentY + math.tan(pitchRad) * distance
  132. return projectedY
  133. end
  134.  
  135. local function broadCastid()
  136. while true do
  137. local id = ship.getId()
  138. local missileInfo = {id = id, type = "AIM-220", launchState = false}
  139. modem.transmit(missileInfoChannel,0,missileInfo)
  140. print("Broadcasting missile info at channel: "..missileInfoChannel)
  141. sleep()
  142. end
  143. end
  144.  
  145. parallel.waitForAny(
  146. function()
  147. while true do
  148. --print("controls: "..textutils.serialize(controls))
  149. missileId = ship.getId()
  150. if controls and controls.fireMissile and controls.fireMissile[missileId] and controls.fireMissile[missileId].launch == true then
  151. target = controls.fireMissile[missileId]
  152. print("launching")
  153. break
  154. end
  155. sleep()
  156. end
  157. end,
  158. modemMessage,
  159. broadCastid
  160. )
  161. print("Scaning for target")
  162.  
  163. local function broadCastid()
  164. while true do
  165. local id = ship.getId()
  166. local missileInfo = {id = id, type = "AIM-220", launchState = true}
  167. print()
  168. modem.transmit(missileInfoChannel,0,missileInfo)
  169. sleep()
  170. end
  171. end
  172.  
  173. --lock on
  174. local results = radar.scanForShips(4000)
  175. local displayData = {}
  176. print(textutils.serialize(target))
  177.  
  178. local fastestSpeed = 0
  179.  
  180. if target.type == "ship" then
  181. if not results or #results == 0 then
  182. table.insert(displayData, "No objects detected.")
  183. else
  184. local smallestCombinedDiff = math.huge -- Initialize with a large value
  185. local finalTargetYaw = nil
  186. local finalTargetPitch = nil
  187. local finalDistance = nil
  188. local closestDistanceWithSmallDiff = math.huge -- New variable for closest distance with small combined diff
  189.  
  190. -- Assuming 'yaw' and 'pitch' are the current ship's yaw and pitch angles
  191. local pos = ship.getWorldspacePosition()
  192. local yaw = math.deg(getYaw()) + 180
  193. if yaw > 360 then yaw = yaw - 360 end
  194. local pitch = math.deg(getPitch())
  195.  
  196. for i, object in ipairs(results) do
  197. -- Calculate dx, dy, dz from ship to object
  198. local dx = object.pos.x - pos.x
  199. local dy = object.pos.y - pos.y
  200. local dz = object.pos.z - pos.z
  201.  
  202. -- Calculate bearing (yaw) from ship to object
  203. local angleFromSouth = math.atan2(dx, dz)
  204. local bearingFromSouth = math.deg(angleFromSouth)
  205. local targetYaw = (180 - bearingFromSouth) % 360
  206.  
  207. -- Calculate yaw difference
  208. local yawDiff = (targetYaw - yaw + 180) % 360 - 180
  209.  
  210. -- Calculate pitch difference
  211. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  212. local targetPitch = math.deg(math.atan2(dy, horizontalDistance))
  213. local pitchDiff = targetPitch - (-pitch)
  214.  
  215. -- Calculate the Euclidean distance (for prioritization)
  216. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  217.  
  218. -- Combine yaw and pitch differences into a single score (weighted sum or simply adding)
  219. local combinedDiff = math.abs(yawDiff) + math.abs(pitchDiff)
  220.  
  221. -- Additional logic: If combinedDiff is less than 20, lock onto the closest target
  222. if object.id == target.id then
  223. closestDistanceWithSmallDiff = distance
  224. lockedId = object.id
  225. intialPosition = object.pos
  226. initialDistance = horizontalDistance
  227. print("Target with combinedDiff < 20, Distance: ", distance, " Locked target ID: ", object.id)
  228. end
  229. end
  230.  
  231. if lockedId and intialPosition then
  232. print("Closest Object ID:", lockedId)
  233. print("X: "..intialPosition.x.." Y: "..intialPosition.y.." Z: "..intialPosition.z)
  234. print("initialDistance: "..initialDistance)
  235. else
  236. print("Target not dectected")
  237. end
  238. end
  239. else
  240. local pos = ship.getWorldspacePosition()
  241. local dx = target.pos.x - pos.x
  242. local dy = target.pos.y - pos.y
  243. local dz = target.pos.z - pos.z
  244. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  245. intialPosition = target.pos
  246. initialDistance = math.sqrt(dx * dx + dz * dz)
  247. end
  248.  
  249. print("Locked Target:", lockedId)
  250. print("X: "..intialPosition.x.." Y: "..intialPosition.y.." Z: "..intialPosition.z)
  251. topFlap.assemble()
  252. bottomFlap.assemble()
  253. leftFlap.assemble()
  254. rightFlap.assemble()
  255.  
  256. jet.setOutputThrust(10000)
  257. redrouter.setOutput("front",true)
  258. sleep(0.3)
  259. jet.setOutputThrust(0)
  260. redrouter.setOutput("front",false)
  261.  
  262. local function initial()
  263. local results = radar.scanForShips(4000)
  264. pos = ship.getWorldspacePosition()
  265. local yaw = math.deg(getYaw())
  266. if yaw>360 then yaw = yaw - 360 end
  267. local missileVelocity = ship.getVelocity()
  268. local misisleSpeed = math.sqrt(missileVelocity.x ^ 2 + missileVelocity.y ^ 2 + missileVelocity.z ^ 2)
  269. local pitch = math.deg(getPitch())
  270. local flightControl = {
  271. pitchUp = nil, pitchDown, yawLeft = nil, yawRight = nil, throttle = nil
  272. }
  273.  
  274. if target.type == "ship" then
  275. for i, object in ipairs(results) do
  276. if object.id == lockedId then
  277. targetPos = object.pos
  278. targetVel = object.velocity
  279. local dx = targetPos.x - pos.x
  280. local dy = targetPos.y - pos.y + 50
  281. local dz = targetPos.z - pos.z
  282. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  283. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  284. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  285.  
  286. -- Update pitch, yaw, and distance
  287. local estimateTime = immediateDistance/missileSpeed
  288. local estimateX = targetPos.x + targetVel.x * math.min(estimateTime,1)
  289. local estimateY = targetPos.y + 50
  290. local estimateZ = targetPos.z + targetVel.z * math.min(estimateTime,1)
  291.  
  292. local dx = estimateX - pos.x
  293. local dy = estimateY - pos.y
  294. local dz = estimateZ - pos.z
  295.  
  296. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  297.  
  298. TargetYaw = math.deg(math.atan2(-dx, dz))
  299. TargetYaw = (TargetYaw + 180) % 360
  300.  
  301. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  302. yLevelDiff = targetPos.y - projectedMissileY
  303. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  304.  
  305. targetAltitude = targetPos.y
  306.  
  307. yawError = TargetYaw - yaw
  308. pitchError = TargetPitch - (-pitch)
  309. rollError = math.deg(getRoll())
  310.  
  311. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  312. end
  313. end
  314. elseif target.type == "waypoint" then
  315. targetPos = target.pos
  316.  
  317. local dx = targetPos.x - pos.x
  318. local dy = targetPos.y - pos.y + 100
  319. local dz = targetPos.z - pos.z
  320. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  321. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  322. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  323.  
  324. local estimateTime = immediateDistance/missileSpeed
  325.  
  326. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  327.  
  328. TargetYaw = math.deg(math.atan2(-dx, dz))
  329. TargetYaw = (TargetYaw + 180) % 360
  330.  
  331. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  332. yLevelDiff = targetPos.y - projectedMissileY
  333. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  334.  
  335. targetAltitude = targetPos.y
  336.  
  337. yawError = TargetYaw - yaw
  338. pitchError = TargetPitch - (-pitch)
  339. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  340. print(pitch)
  341. end
  342.  
  343. -- Ensure yaw is between -180 and 180 for error
  344. if yawError > 180 then
  345. yawError = yawError - 360
  346. elseif yawError < -180 then
  347. yawError = yawError + 360
  348. end
  349.  
  350. -- PID control for yaw
  351. local yawOutput, yawIntegral, yawPrevError = PIDController(Kp_yaw, Ki_yaw, Kd_yaw, yawError, yawIntegral, (yawError - yawPrevError), yawPrevError, dt)
  352. yawOutput = -math.min(math.max(yawOutput, -45), 45) / (1+math.abs(rollError)*0.1)
  353.  
  354. local pitchOutput, pitchIntegral, pitchHeightPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchHeightDiff, pitchIntegral, (pitchHeightDiff - pitchHeightPrevError), pitchHeightPrevError, dt)
  355. pitchOutput = -math.min(math.max(pitchOutput, -30), 30) / (1+math.abs(rollError)*0.1)
  356.  
  357. local rollOutput,rollIntegral,rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  358. rollOutput = -math.min(math.max(rollOutput, -10), 10)
  359.  
  360. --yaw and pitch flight control
  361. if missileSpeed < 40 then --adjustment for yaw propeller bug
  362. yawOutput = yawOutput
  363. pitchOutput = pitchOutput * 2.3
  364. rollOutput = rollOutput
  365. end
  366.  
  367.  
  368. if yawOutput > 0 then
  369. topFlap.setAngle(yawOutput)
  370. bottomFlap.setAngle(yawOutput)
  371. elseif yawOutput < 0 then
  372. topFlap.setAngle(yawOutput)
  373. bottomFlap.setAngle(yawOutput)
  374. else
  375. topFlap.setAngle(0)
  376. bottomFlap.setAngle(0)
  377. end
  378.  
  379. if pitchOutput > 0.1 then
  380. leftFlap.setAngle(pitchOutput + rollOutput)
  381. rightFlap.setAngle(pitchOutput - rollOutput)
  382. elseif pitchOutput < -0.1 then
  383. leftFlap.setAngle(pitchOutput + rollOutput)
  384. rightFlap.setAngle(pitchOutput - rollOutput)
  385. else
  386. leftFlap.setAngle(0)
  387. rightFlap.setAngle(0)
  388. end
  389.  
  390. if missileSpeed < 340 then
  391. jet.setOutputThrust(highThrust) -- Increase throttle
  392. else
  393. jet.setOutputThrust(0)
  394. end
  395. end
  396.  
  397. local function terminal()
  398. local results = radar.scanForShips(4000)
  399. pos = ship.getWorldspacePosition()
  400. local yaw = math.deg(getYaw())
  401. if yaw>360 then yaw = yaw - 360 end
  402. local missileVelocity = ship.getVelocity()
  403. local misisleSpeed = math.sqrt(missileVelocity.x ^ 2 + missileVelocity.y ^ 2 + missileVelocity.z ^ 2)
  404. local pitch = math.deg(getPitch())
  405.  
  406. local targetSpeed = 0
  407.  
  408. if target.type == "ship" then
  409. for i, object in ipairs(results) do
  410. if object.id == lockedId then
  411. -- Update position and targetVel
  412. targetPos = object.pos
  413. targetVel = object.velocity
  414. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  415. local dx = targetPos.x - pos.x
  416. local dy = targetPos.y - pos.y
  417. local dz = targetPos.z - pos.z
  418. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  419. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  420. if missileSpeed < 150 then missileSpeed = 150 end
  421.  
  422. -- Update pitch, yaw, and distance
  423. local estimateTime = immediateDistance/missileSpeed
  424. local estimateX = targetPos.x + targetVel.x * math.min(estimateTime)
  425. local estimateY = targetPos.y + targetVel.y * math.min(estimateTime)
  426. local estimateZ = targetPos.z + targetVel.z * math.min(estimateTime)
  427.  
  428. local dx = estimateX - pos.x
  429. local dy = estimateY - pos.y
  430. local dz = estimateZ - pos.z
  431. local immediateDistance2nd = math.sqrt(dx * dx + dy * dy + dz * dz)
  432.  
  433. local estimateTime2nd = math.min(immediateDistance/missileSpeed,8)
  434. local estimateX = targetPos.x + targetVel.x * math.min(estimateTime2nd)
  435. local estimateY = targetPos.y + targetVel.y * math.min(estimateTime2nd)
  436. local estimateZ = targetPos.z + targetVel.z * math.min(estimateTime2nd)
  437.  
  438. local dx = estimateX - pos.x
  439. local dy = estimateY - pos.y
  440. local dz = estimateZ - pos.z
  441.  
  442. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  443.  
  444. TargetYaw = math.deg(math.atan2(-dx, dz))
  445. TargetYaw = (TargetYaw + 180) % 360
  446.  
  447. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  448. yLevelDiff = targetPos.y - projectedMissileY
  449. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  450.  
  451. yawError = TargetYaw - yaw
  452. pitchError = TargetPitch - (-pitch)
  453. rollError = math.deg(getRoll())
  454.  
  455. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  456. -- Output diagnostics
  457. end
  458. end
  459. elseif target.type == "waypoint" then
  460. -- Update position and targetVel
  461. targetPos = target.pos
  462.  
  463. local dx = targetPos.x - pos.x
  464. local dy = targetPos.y - pos.y
  465. local dz = targetPos.z - pos.z
  466. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  467. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  468. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  469.  
  470. -- Update pitch, yaw, and distance
  471. local estimateTime = immediateDistance/missileSpeed
  472.  
  473. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  474.  
  475. TargetYaw = math.deg(math.atan2(-dx, dz))
  476. TargetYaw = (TargetYaw + 180) % 360
  477.  
  478. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  479. yLevelDiff = targetPos.y - projectedMissileY
  480. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  481.  
  482. yawError = TargetYaw - yaw
  483. pitchError = TargetPitch - (-pitch)
  484. rollError = math.deg(getRoll())
  485.  
  486. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  487. end
  488.  
  489. -- Ensure yaw is between -180 and 180 for error
  490. if yawError > 180 then
  491. yawError = yawError - 360
  492. elseif yawError < -180 then
  493. yawError = yawError + 360
  494. end
  495.  
  496. if previousDistance then
  497. deltaDistance = previousDistance - immediateDistance
  498. else
  499. deltaDistance = 0 -- Initialize if this is the first frame
  500. end
  501. previousDistance = immediateDistance
  502.  
  503. -- PID control for yaw
  504. local yawOutput, yawIntegral, yawPrevError = PIDController(Kp_yaw, Ki_yaw, Kd_yaw, yawError, yawIntegral, (yawError - yawPrevError), yawPrevError, dt)
  505. yawOutput = -math.min(math.max(yawOutput, -10), 10) / (1+math.abs(rollError)*0.1)
  506.  
  507. -- PID control for pitch
  508. local pitchOutput, pitchIntegral, pitchHeightPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchHeightDiff, pitchIntegral, (pitchHeightDiff - pitchHeightPrevError), pitchHeightPrevError, dt)
  509. pitchOutput = -math.min(math.max(pitchOutput, -10), 10) / (1+math.abs(rollError)*0.1)
  510.  
  511.  
  512. -- PIDcontrol for roll
  513. local rollOutput,rollIntegral,rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  514. rollOutput = -math.min(math.max(rollOutput, -10), 10)
  515.  
  516. --yaw and pitch flight control
  517. if yawOutput > 0 then
  518. topFlap.setAngle(yawOutput)
  519. bottomFlap.setAngle(yawOutput)
  520. elseif yawOutput < 0 then
  521. topFlap.setAngle(yawOutput)
  522. bottomFlap.setAngle(yawOutput)
  523. else
  524. topFlap.setAngle(0)
  525. bottomFlap.setAngle(0)
  526. end
  527.  
  528. if pitchOutput > 0.1 then
  529. leftFlap.setAngle(pitchOutput + rollOutput)
  530. rightFlap.setAngle(pitchOutput - rollOutput)
  531. elseif pitchOutput < -0.1 then
  532. leftFlap.setAngle(pitchOutput + rollOutput)
  533. rightFlap.setAngle(pitchOutput - rollOutput)
  534. else
  535. leftFlap.setAngle(0)
  536. rightFlap.setAngle(0)
  537. end
  538.  
  539. if (deltaDistance < 5 or missileSpeed < 60) and missileSpeed < 270 then
  540. if (yawOutput < 7 or pitchOutput < 7) and missileSpeed > 80 then
  541. jet.setOutputThrust(highThrust) --target aimmed, missile speed sufficient, full thrust
  542. else
  543. jet.setOutputThrust(0) --target not aimmed, missile speed sufficient, reduce thrust
  544. end
  545. else
  546. jet.setOutputThrust(0)
  547. --redrouter.setOutput("front",false)
  548. print("Maintaining throttle: Missile speed is sufficient")
  549. end
  550. end
  551.  
  552. local function fuze()
  553. local results = radar.scanForShips(2000)
  554. pos = ship.getWorldspacePosition()
  555. local distance
  556. local missileVelocity = ship.getVelocity()
  557. local missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  558.  
  559. if target.type == "ship" then
  560. for i, object in ipairs(results) do
  561. if object.id == lockedId then
  562. targetPos = object.pos
  563. local dx = targetPos.x - pos.x
  564. local dy = targetPos.y - pos.y
  565. local dz = targetPos.z - pos.z
  566.  
  567. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  568. distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  569. end
  570. end
  571. elseif target.type == "waypoint" then
  572. local dx = target.pos.x - pos.x
  573. local dy = target.pos.y - pos.y
  574. local dz = target.pos.z - pos.z
  575. distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  576. targetSpeed = 0
  577. end
  578.  
  579. -- **Calculate Rate of Change of Distance Using Delta Time**
  580. local deltaDistance = lastDistance - distance
  581. local rateOfClosure = math.min(deltaDistance / (dt + 0.0001),200)
  582.  
  583. -- **Proximity Fuze Logic**
  584. local timeToHit = distance / (rateOfClosure + 0.0001)
  585.  
  586. if timeToHit < 0.4 and deltaDistance > 0 and distance < 50 then
  587. sleep(math.max(timeToHit - 0.15, 0)) -- Prevent negative sleep time
  588. print("Proximity fuze detonating!")
  589. redrouter.setOutput("back", true)
  590. sleep(0.1)
  591.  
  592. -- Reset all outputs
  593. local outputs = {"front", "left", "bottom", "right", "back", "top"}
  594. for _, output in ipairs(outputs) do
  595. redstone.setOutput(output, false)
  596. redrouter.setOutput(output, false)
  597. end
  598.  
  599. jet.setOutputThrust(0)
  600. print("Fastest speed: " .. missileSpeed)
  601. error("Proximity fuze detonated")
  602. end
  603.  
  604. -- **Impact Fuze Logic**
  605. if redstone.getInput("right") and distance < 70 then
  606. print("Impact fuze detonating!")
  607. redrouter.setOutput("back", true)
  608. sleep(0.1)
  609.  
  610. -- Reset all outputs
  611. for _, output in ipairs(outputs) do
  612. redstone.setOutput(output, false)
  613. redrouter.setOutput(output, false)
  614. end
  615.  
  616. jet.setOutputThrust(0)
  617. print("Fastest speed: " .. missileSpeed)
  618. error("Impact fuze detonated")
  619. end
  620.  
  621. lastDistance = distance
  622. end
  623.  
  624. parallel.waitForAny(
  625. function()
  626. while true do
  627. local results = radar.scanForShips(4000)
  628. pos = ship.getWorldspacePosition()
  629. local targetSpeed = 0
  630.  
  631. if target.type == "ship" then
  632. for i, object in ipairs(results) do
  633. if object.id == lockedId then
  634. targetPos = object.pos
  635. targetVel = object.velocity
  636. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  637.  
  638. local dx = targetPos.x - pos.x
  639. local dy = targetPos.y - pos.y
  640. local dz = targetPos.z - pos.z
  641. horizontalDistance = math.sqrt(dx * dx + dz * dz)
  642. end
  643. end
  644. elseif target.type == "waypoint" then
  645. local dx = target.pos.x - pos.x
  646. local dy = target.pos.y - pos.y
  647. local dz = target.pos.z - pos.z
  648. horizontalDistance = math.sqrt(dx * dx + dz * dz)
  649. end
  650.  
  651. --print("horizontalDistance: "..horizontalDistance)
  652. --print("targetSpeed: "..targetSpeed)
  653.  
  654. -- Check conditions for terminal phase
  655.  
  656. --record speed
  657. local missileVelocity = ship.getVelocity()
  658. local missileSpeed = math.sqrt(missileVelocity.x ^ 2 + missileVelocity.y ^ 2 + missileVelocity.z ^ 2)
  659. if missileSpeed > fastestSpeed then fastestSpeed = missileSpeed end
  660. print("fastestSpeed: ",fastestSpeed)
  661. if horizontalDistance < 200 then
  662. --print("terminal phrase")
  663. parallel.waitForAll(
  664. terminal,
  665. fuze
  666. )
  667. else
  668. --print("initial phrase")
  669. terminal()
  670. end
  671. end
  672. end,
  673. broadCastid
  674. )
  675.  
Advertisement
Add Comment
Please, Sign In to add comment