MegiRicky

thunderboltVLScontrolV4

Mar 23rd, 2025 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.93 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.  
  16. local controls = {}
  17. local target = {}
  18. local lockedId = nil
  19. local targetPos, targetVel, TargetYaw, TargetPitch
  20. local Kp_yaw, Ki_yaw, Kd_yaw = 0.22, 0.005, 0.0092
  21. local Kp_pitch, Ki_pitch, Kd_pitch = 0.27, 0.065, 0.0042
  22. local Kp_roll, Ki_roll,Kd_roll = 0.12,0,0.0057
  23. local Kp_throttle, Ki_throttle, Kd_throttle = 0.2, 0.1, 0.02
  24. local K_pitchDiff, k_heightDiff = 1,0
  25. local projectedMissileY,yLevelDiff,initialDistance,targetAltitude
  26. local closestDistance = math.huge
  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 speedError, speedIntegral, speedPrevError = 0,0,0
  40.  
  41. local throttle = 15
  42.  
  43. local lastMotorSpeed = nil
  44. local lastMotorUpdateTime = os.clock()
  45.  
  46. local dt = 0.1
  47.  
  48. local controlsChannel = 1400
  49. local missileInfoChannel = controlsChannel + 10
  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 = "thunderBolt", launchState = false}
  132. modem.transmit(missileInfoChannel,0,missileInfo)
  133. print("Broadcasting missile info at channel: "..missileInfoChannel)
  134. sleep()
  135. end
  136. end
  137.  
  138. parallel.waitForAny(
  139. function()
  140. while true do
  141. --print("controls: "..textutils.serialize(controls))
  142. missileId = ship.getId()
  143. if controls and controls.fireMissile and controls.fireMissile[missileId] and controls.fireMissile[missileId].launch == true then
  144. target = controls.fireMissile[missileId]
  145. print("launching")
  146. break
  147. end
  148. sleep()
  149. end
  150. end,
  151. modemMessage,
  152. broadCastid
  153. )
  154. print("Scaning for target")
  155.  
  156. local function broadCastid()
  157. while true do
  158. local id = ship.getId()
  159. local missileInfo = {id = id, type = "thunderBolt", launchState = true}
  160. print()
  161. modem.transmit(missileInfoChannel,0,missileInfo)
  162. sleep()
  163. end
  164. end
  165.  
  166. --lock on
  167. local targetPos = ship.getWorldspacePosition()
  168. sleep(0.01)
  169. local targetVel = ship.getVelocity()
  170. targetPos.x = targetPos.x + targetVel.x * 2
  171. targetPos.z = targetPos.z + targetVel.y * 2
  172.  
  173. local yaw = math.deg(getYaw())
  174. local pitch = math.deg(getPitch()) * (90/28.6478)
  175.  
  176. local results = radar.scanForShips(3000)
  177. local displayData = {}
  178. print(results)
  179.  
  180. if target.type == "ship" then
  181. if not results or #results == 0 then
  182. table.insert(displayData, "No objects detected.")
  183. else
  184. local smallestCombinedDiff = math.huge -- Initialize with a large value
  185. local finalTargetYaw = nil
  186. local finalTargetPitch = nil
  187. local finalDistance = nil
  188. local closestDistanceWithSmallDiff = math.huge -- New variable for closest distance with small combined diff
  189.  
  190. -- Assuming 'yaw' and 'pitch' are the current ship's yaw and pitch angles
  191. local pos = ship.getWorldspacePosition()
  192. local yaw = math.deg(getYaw()) + 180
  193. if yaw > 360 then yaw = yaw - 360 end
  194. local pitch = math.deg(getPitch())
  195.  
  196. for i, object in ipairs(results) do
  197. if object.mass > 1000 then
  198. -- Calculate dx, dy, dz from ship to object
  199. local dx = object.pos.x - pos.x
  200. local dy = object.pos.y - pos.y
  201. local dz = object.pos.z - pos.z
  202.  
  203. -- Calculate bearing (yaw) from ship to object
  204. local angleFromSouth = math.atan2(dx, dz)
  205. local bearingFromSouth = math.deg(angleFromSouth)
  206. local targetYaw = (180 - bearingFromSouth) % 360
  207.  
  208. -- Calculate yaw difference
  209. local yawDiff = (targetYaw - yaw + 180) % 360 - 180
  210.  
  211. -- Calculate pitch difference
  212. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  213. local targetPitch = math.deg(math.atan2(dy, horizontalDistance))
  214. local pitchDiff = targetPitch - (-pitch)
  215.  
  216. -- Calculate the Euclidean distance (for prioritization)
  217. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  218.  
  219. -- Combine yaw and pitch differences into a single score (weighted sum or simply adding)
  220. local combinedDiff = math.abs(yawDiff) + math.abs(pitchDiff)
  221.  
  222. -- Additional logic: If combinedDiff is less than 20, lock onto the closest target
  223. if object.id == target.id then
  224. closestDistanceWithSmallDiff = distance
  225. lockedId = object.id
  226. intialPosition = object.pos
  227. initialDistance = horizontalDistance
  228. print("Target with combinedDiff < 20, Distance: ", distance, " Locked target ID: ", object.id)
  229. end
  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("No large objects detected.")
  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("top",true)
  259. sleep(0.3)
  260. redstone.setOutput("top",false)
  261.  
  262.  
  263. local function initial()
  264. local results = radar.scanForShips(2000)
  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. print("pitch: "..pitch)
  275.  
  276. if target.type == "ship" then
  277. for i, object in ipairs(results) do
  278. if object.id == lockedId then
  279. targetPos = object.pos
  280. targetVel = object.velocity
  281. local dx = targetPos.x - pos.x
  282. local dy = targetPos.y - pos.y + 30
  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. -- Update pitch, yaw, and distance
  289. local estimateTime = immediateDistance/missileSpeed
  290. local estimateX = targetPos.x + targetVel.x * math.min(estimateTime,1)
  291. local estimateY = targetPos.y + 30
  292. local estimateZ = targetPos.z + targetVel.z * math.min(estimateTime,1)
  293.  
  294. local dx = estimateX - pos.x
  295. local dy = estimateY - pos.y
  296. local dz = estimateZ - pos.z
  297.  
  298. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  299.  
  300. TargetYaw = math.deg(math.atan2(-dx, dz))
  301. TargetYaw = (TargetYaw + 180) % 360
  302.  
  303. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  304. yLevelDiff = targetPos.y - projectedMissileY
  305. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  306.  
  307. targetAltitude = targetPos.y
  308.  
  309. yawError = TargetYaw - yaw
  310. pitchError = TargetPitch - (-pitch)
  311. rollError = math.deg(getRoll())
  312.  
  313. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  314. end
  315. end
  316. elseif target.type == "waypoint" then
  317. targetPos = target.pos
  318.  
  319. local dx = targetPos.x - pos.x
  320. local dy = targetPos.y - pos.y + 100
  321. local dz = targetPos.z - pos.z
  322. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  323. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  324. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  325.  
  326. local estimateTime = immediateDistance/missileSpeed
  327.  
  328. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  329.  
  330. TargetYaw = math.deg(math.atan2(-dx, dz))
  331. TargetYaw = (TargetYaw + 180) % 360
  332.  
  333. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  334. yLevelDiff = targetPos.y - projectedMissileY
  335. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  336.  
  337. targetAltitude = targetPos.y
  338.  
  339. yawError = TargetYaw - yaw
  340. pitchError = TargetPitch - (-pitch)
  341. rollError = math.deg(getRoll())
  342. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  343. print(pitch)
  344. end
  345.  
  346. -- Ensure yaw is between -180 and 180 for error
  347. if yawError > 180 then
  348. yawError = yawError - 360
  349. elseif yawError < -180 then
  350. yawError = yawError + 360
  351. end
  352.  
  353. local yawOutput, yawIntegral, yawPrevError = PIDController(Kp_yaw, Ki_yaw, Kd_yaw, yawError, yawIntegral, (yawError - yawPrevError), yawPrevError, dt)
  354. yawOutput = -math.min(math.max(yawOutput, -20), 20) / (1+math.abs(rollError)*0.1)
  355.  
  356. -- PID control for pitch
  357. local pitchOutput, pitchIntegral, pitchHeightPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchHeightDiff, pitchIntegral, (pitchHeightDiff - pitchHeightPrevError), pitchHeightPrevError, dt)
  358. pitchOutput = -math.min(math.max(pitchOutput, -20), 20) / (1+math.abs(rollError)*0.1)
  359.  
  360.  
  361. -- PIDcontrol for roll
  362. print("rollError: "..rollError)
  363. local rollOutput,rollIntegral,rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  364. rollOutput = -math.min(math.max(rollOutput, -10), 10)
  365. print("yawOutput: "..yawOutput.." pitchOutput: "..pitchOutput.." rollOutput: "..rollOutput)
  366.  
  367. --yaw and pitch flight control
  368. if yawOutput > 0 then
  369. topFlap.setAngle(yawOutput)
  370. bottomFlap.setAngle(yawOutput)
  371. elseif yawOutput < 0 then
  372. topFlap.setAngle(yawOutput)
  373. bottomFlap.setAngle(yawOutput)
  374. else
  375. topFlap.setAngle(0)
  376. bottomFlap.setAngle(0)
  377. end
  378.  
  379. if pitchOutput > 0.1 then
  380. leftFlap.setAngle(pitchOutput + rollOutput)
  381. rightFlap.setAngle(pitchOutput - rollOutput)
  382. elseif pitchOutput < -0.1 then
  383. leftFlap.setAngle(pitchOutput + rollOutput)
  384. rightFlap.setAngle(pitchOutput - rollOutput)
  385. else
  386. leftFlap.setAngle(0)
  387. rightFlap.setAngle(0)
  388. end
  389.  
  390. if missileSpeed < 60 then
  391. redstone.setAnalogOutput("back",throttle)
  392. else
  393. redstone.setAnalogOutput("back",0)
  394. end
  395. end
  396.  
  397. local function terminal()
  398. local results = radar.scanForShips(2000)
  399. pos = ship.getWorldspacePosition()
  400. local yaw = math.deg(getYaw())
  401. if yaw>360 then yaw = yaw - 360 end
  402. local missileVelocity = ship.getVelocity()
  403. local misisleSpeed = math.sqrt(missileVelocity.x ^ 2 + missileVelocity.y ^ 2 + missileVelocity.z ^ 2)
  404. local pitch = math.deg(getPitch())
  405. local flightControl = {
  406. pitchUp = nil, pitchDown, yawLeft = nil, yawRight = nil, throttle = nil
  407. }
  408. local targetSpeed = 0
  409.  
  410. if target.type == "ship" then
  411. for i, object in ipairs(results) do
  412. if object.id == lockedId then
  413. -- Update position and targetVel
  414. targetPos = object.pos
  415. targetVel = object.velocity
  416. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  417. local dx = targetPos.x - pos.x
  418. local dy = targetPos.y - pos.y
  419. local dz = targetPos.z - pos.z
  420. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  421. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  422. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  423.  
  424. -- Update pitch, yaw, and distance
  425. local estimateTime = immediateDistance/140
  426. local estimateX = targetPos.x + targetVel.x * math.min(estimateTime,8)
  427. local estimateY = targetPos.y + targetVel.y * math.min(estimateTime,8)
  428. local estimateZ = targetPos.z + targetVel.z * math.min(estimateTime,8)
  429.  
  430. local dx = estimateX - pos.x
  431. local dy = estimateY - pos.y
  432. local dz = estimateZ - pos.z
  433.  
  434. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  435.  
  436. TargetYaw = math.deg(math.atan2(-dx, dz))
  437. TargetYaw = (TargetYaw + 180) % 360
  438.  
  439. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  440. yLevelDiff = targetPos.y - projectedMissileY
  441. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  442.  
  443. yawError = TargetYaw - yaw
  444. pitchError = TargetPitch - (-pitch)
  445. rollError = math.deg(getRoll())
  446.  
  447. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  448. print(pitch)
  449. -- Output diagnostics
  450. end
  451. end
  452. elseif target.type == "waypoint" then
  453. -- Update position and targetVel
  454. targetPos = target.pos
  455.  
  456. local dx = targetPos.x - pos.x
  457. local dy = targetPos.y - pos.y
  458. local dz = targetPos.z - pos.z
  459. local immediateDistance = math.sqrt(dx * dx + dy * dy + dz * dz)
  460. missileSpeed = math.sqrt(missileVelocity.x^2 + missileVelocity.y^2 + missileVelocity.z^2)
  461. if missileSpeed < 0.1 then missileSpeed = 0.1 end
  462.  
  463. -- Update pitch, yaw, and distance
  464. local estimateTime = immediateDistance/missileSpeed
  465.  
  466. local targetHorizontalDistance = math.sqrt(dx * dx + dz * dz)
  467.  
  468. TargetYaw = math.deg(math.atan2(-dx, dz))
  469. TargetYaw = (TargetYaw + 180) % 360
  470.  
  471. projectedMissileY = pos.y + missileVelocity.y * estimateTime
  472. yLevelDiff = targetPos.y - projectedMissileY
  473. TargetPitch = math.deg(math.atan2(dy, targetHorizontalDistance))
  474.  
  475. yawError = TargetYaw - yaw
  476. pitchError = TargetPitch - (-pitch)
  477. rollError = math.deg(getRoll())
  478.  
  479. pitchHeightDiff = pitchError * K_pitchDiff + yLevelDiff * k_heightDiff
  480. end
  481.  
  482. -- Ensure yaw is between -180 and 180 for error
  483. if yawError > 180 then
  484. yawError = yawError - 360
  485. elseif yawError < -180 then
  486. yawError = yawError + 360
  487. end
  488.  
  489. if previousDistance then
  490. deltaDistance = previousDistance - immediateDistance
  491. else
  492. deltaDistance = 0 -- Initialize if this is the first frame
  493. end
  494. previousDistance = immediateDistance
  495.  
  496. -- PID control for yaw
  497. local yawOutput, yawIntegral, yawPrevError = PIDController(Kp_yaw, Ki_yaw, Kd_yaw, yawError, yawIntegral, (yawError - yawPrevError), yawPrevError, dt)
  498. yawOutput = -math.min(math.max(yawOutput, -20), 20) / (1+math.abs(rollError)*0.1)
  499.  
  500. -- PID control for pitch
  501. local pitchOutput, pitchIntegral, pitchHeightPrevError = PIDController(Kp_pitch, Ki_pitch, Kd_pitch, pitchHeightDiff, pitchIntegral, (pitchHeightDiff - pitchHeightPrevError), pitchHeightPrevError, dt)
  502. pitchOutput = -math.min(math.max(pitchOutput, -20), 20) / (1+math.abs(rollError)*0.1)
  503.  
  504.  
  505. -- PIDcontrol for roll
  506. print("rollError: "..rollError)
  507. local rollOutput,rollIntegral,rollPrevError = PIDController(Kp_roll, Ki_roll, Kd_roll, rollError, rollIntegral, (rollError - rollPrevError), rollPrevError, dt)
  508. rollOutput = -math.min(math.max(rollOutput, -10), 10)
  509. print("yawOutput: "..yawOutput.." pitchOutput: "..pitchOutput.." rollOutput: "..rollOutput)
  510.  
  511. --yaw and pitch flight control
  512. if yawOutput > 0 then
  513. topFlap.setAngle(yawOutput)
  514. bottomFlap.setAngle(yawOutput)
  515. elseif yawOutput < 0 then
  516. topFlap.setAngle(yawOutput)
  517. bottomFlap.setAngle(yawOutput)
  518. else
  519. topFlap.setAngle(0)
  520. bottomFlap.setAngle(0)
  521. end
  522.  
  523. if pitchOutput > 0.1 then
  524. leftFlap.setAngle(pitchOutput + rollOutput)
  525. rightFlap.setAngle(pitchOutput - rollOutput)
  526. elseif pitchOutput < -0.1 then
  527. leftFlap.setAngle(pitchOutput + rollOutput)
  528. rightFlap.setAngle(pitchOutput - rollOutput)
  529. else
  530. leftFlap.setAngle(0)
  531. rightFlap.setAngle(0)
  532. end
  533.  
  534. if missileSpeed < 40 then
  535. redstone.setAnalogOutput("back",throttle)
  536. elseif missileSpeed < 100 and yawOutput < 7 and pitchOutput < 7 then
  537. redstone.setAnalogOutput("back",throttle)
  538. else
  539. redstone.setAnalogOutput("back",0)
  540. end
  541. end
  542.  
  543. local function fuze()
  544. local results = radar.scanForShips(2000)
  545. pos = ship.getWorldspacePosition()
  546. local distance
  547. if target.type == "ship" then
  548. for i, object in ipairs(results) do
  549. if object.id == lockedId then
  550. targetPos = object.pos
  551. local dx = targetPos.x - pos.x
  552. local dy = targetPos.y - pos.y
  553. local dz = targetPos.z - pos.z
  554.  
  555. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  556. distance = math.sqrt(dx * dx + dy * dy +dz * dz)
  557. end
  558. end
  559. elseif target.type == "waypoint" then
  560. local dx = target.pos.x - pos.x
  561. local dy = target.pos.y - pos.y
  562. local dz = target.pos.z - pos.z
  563. distance = math.sqrt(dx * dx + dy * dy +dz * dz)
  564. targetSpeed = 0
  565. end
  566. --proximity fuze
  567. if distance < 25 and distance > closestDistance then
  568. print("impact fuze detonating")
  569. redstone.setOutput("front",true)
  570. sleep(0.1)
  571. redstone.setOutput("bottom",true)
  572. redstone.setOutput("front",false)
  573. redstone.setOutput("left",false)
  574. redstone.setOutput("bottom",false)
  575. redstone.setOutput("right",false)
  576. redstone.setOutput("back",false)
  577. redstone.setOutput("top",false)
  578. error("detonated")
  579. else
  580. closestDistance = distance
  581. end
  582. --impact fuze
  583. if redstone.getInput("right") and distance < 70 then
  584. print("impact fuze detonating")
  585. redstone.setOutput("front",true)
  586. sleep(0.1)
  587. redstone.setOutput("bottom",true)
  588. redstone.setOutput("front",false)
  589. redstone.setOutput("left",false)
  590. redstone.setOutput("bottom",false)
  591. redstone.setOutput("right",false)
  592. redstone.setOutput("back",false)
  593. redstone.setOutput("top",false)
  594. error("detonated")
  595. end
  596. end
  597. parallel.waitForAny(
  598. function()
  599. while true do
  600. local results = radar.scanForShips(2000)
  601. pos = ship.getWorldspacePosition()
  602. local targetSpeed = 0
  603.  
  604. if target.type == "ship" then
  605. for i, object in ipairs(results) do
  606. if object.id == lockedId then
  607. targetPos = object.pos
  608. targetVel = object.velocity
  609. targetSpeed = math.sqrt(targetVel.x^2 + targetVel.y^2 + targetVel.z^2)
  610.  
  611. local dx = targetPos.x - pos.x
  612. local dy = targetPos.y - pos.y
  613. local dz = targetPos.z - pos.z
  614. horizontalDistance = math.sqrt(dx * dx + dz * dz)
  615. end
  616. end
  617. elseif target.type == "waypoint" then
  618. local dx = target.pos.x - pos.x
  619. local dy = target.pos.y - pos.y
  620. local dz = target.pos.z - pos.z
  621. horizontalDistance = math.sqrt(dx * dx + dz * dz)
  622. end
  623.  
  624. --print("horizontalDistance: "..horizontalDistance)
  625. --print("targetSpeed: "..targetSpeed)
  626.  
  627. -- Check conditions for terminal phase
  628. if horizontalDistance < 220 or targetSpeed > 20 then
  629. --print("terminal phrase")
  630. parallel.waitForAll(
  631. terminal,
  632. fuze
  633. )
  634. else
  635. --print("initial phrase")
  636. initial()
  637. end
  638. end
  639. end,
  640. broadCastid
  641. )
  642.  
Advertisement
Add Comment
Please, Sign In to add comment