MegiRicky

thunderboltVLScontrolV3

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