MegiRicky

AGM-134Control

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