MegiRicky

thunderboltVLScontrolV4P2

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