MegiRicky

thunderBoltVLScontrol

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