MegiRicky

vectorVLSGen2Control.lua

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