MegiRicky

GBU-42Control

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