MegiRicky

NGDWBVRGen2

Oct 11th, 2024 (edited)
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.56 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local radar = peripheral.find("sp_radar")
  3. local modem = peripheral.wrap("right")
  4. local speaker = peripheral.find("speaker")
  5.  
  6. -- Ensure all required peripherals are connected
  7. if not monitor or not radar or not ship then
  8. error("Required peripherals are not attached")
  9. end
  10.  
  11. monitor.setTextScale(0.5)
  12. monitor.clear()
  13.  
  14. local radarScale = 500
  15. local friendlyID = {289, 232}
  16. local targetPositions = {}
  17. local waypoints = {}
  18. local displayState = "radar" -- Initial display state
  19. local selectedTarget = nil
  20. local lockedTargetId = nil
  21. local lockedWaypoint = nil -- Variable to store the locked waypoint
  22. local projectileSpeed = 320 --m/s
  23.  
  24. -- Get channel data for cannons
  25. local function getChannelInput(prompt, default)
  26. print(prompt .. ", default: " .. default)
  27. local input = io.read()
  28. if input == "" then
  29. input = default
  30. end
  31. return tonumber(input)
  32. end
  33.  
  34. local port5inchChannel = getChannelInput("Input the port side cannon data channel", 900)
  35. local starboard5inchChannel = getChannelInput("Input the starboard side cannon data channel", 901)
  36. local bow15inchChannel = getChannelInput("Input the bow cannon data channel", 902)
  37. local stern15inchChannel = getChannelInput("Input the stern cannon data channel", 903)
  38.  
  39. if modem then
  40. modem.open(starboard5inchChannel)
  41. modem.open(port5inchChannel)
  42. modem.open(bow15inchChannel)
  43. modem.open(stern15inchChannel)
  44. end
  45.  
  46. local cannonData = {
  47. port5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {} },
  48. starboard5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  49. bow15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  50. stern15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  51. }
  52.  
  53. local cannonTarget = {
  54. port5inch = { type = nil, side = "port", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  55. starboard5inch = { type = nil, side = "starboard", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  56. bow15inch = { type = nil, side = "bow", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  57. stern15inch = { type = nil, side = "stern", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  58. }
  59.  
  60. local function checkFriendly(ID)
  61. local friendly = false
  62. for _, id in ipairs(friendlyID) do
  63. if tonumber(ID) == tonumber(id) then
  64. friendly = true
  65. break
  66. end
  67. end
  68. return friendly
  69. end
  70.  
  71. local w, h = monitor.getSize()
  72.  
  73. local function drawControls()
  74. local w, h = monitor.getSize()
  75. monitor.setCursorPos(1, h-1)
  76. monitor.write(" ")
  77. monitor.setCursorPos(1, h)
  78. monitor.write(" ")
  79. monitor.setCursorPos(1, h-1)
  80. monitor.write("Scale: ")
  81. monitor.setCursorPos(8, h-1)
  82. monitor.write("-")
  83. monitor.setCursorPos(10, h-1)
  84. monitor.write("+")
  85. monitor.setCursorPos(1, h-2)
  86. monitor.write("Stop locking")
  87.  
  88. local segmentLength = radarScale / 2
  89. local scaleText
  90. if segmentLength >= 1000 then
  91. scaleText = string.format("%.1f km", segmentLength / 1000)
  92. else
  93. scaleText = string.format("%d m", segmentLength)
  94. end
  95.  
  96. monitor.setCursorPos(1, h)
  97. monitor.write("Segment Length: " .. scaleText)
  98.  
  99. -- Add button to navigate to waypoint page
  100. monitor.setCursorPos(w-10, h-1)
  101. monitor.write("Waypoints")
  102.  
  103. -- Add button to navigate to the cannon adjustment page
  104. monitor.setCursorPos(w-18, h-1)
  105. monitor.write("Adjust Cannon")
  106. end
  107.  
  108. local function drawControls()
  109. local w, h = monitor.getSize()
  110. monitor.setCursorPos(1, h-1)
  111. monitor.write(" ")
  112. monitor.setCursorPos(1, h)
  113. monitor.write(" ")
  114. monitor.setCursorPos(1, h-1)
  115. monitor.write("Scale: ")
  116. monitor.setCursorPos(8, h-1)
  117. monitor.write("-")
  118. monitor.setCursorPos(10, h-1)
  119. monitor.write("+")
  120. monitor.setCursorPos(1, h-2)
  121. monitor.write("Stop locking")
  122.  
  123. local segmentLength = radarScale / 2
  124. local scaleText
  125. if segmentLength >= 1000 then
  126. scaleText = string.format("%.1f km", segmentLength / 1000)
  127. else
  128. scaleText = string.format("%d m", segmentLength)
  129. end
  130.  
  131. monitor.setCursorPos(1, h)
  132. monitor.write("Segment Length: " .. scaleText)
  133.  
  134. -- Add button to navigate to waypoint page at the bottom right corner
  135. monitor.setCursorPos(w-10, h-1)
  136. monitor.write("Waypoints")
  137.  
  138. -- Add button to navigate to the cannon adjustment page at the top right corner
  139. monitor.setCursorPos(w-13, 1)
  140. monitor.write("Adjust Cannon")
  141. end
  142.  
  143. local function addWaypoint(x, y, z, color)
  144. local newWaypoint = {
  145. x = x,
  146. y = y,
  147. z = z,
  148. color = color or colors.purple
  149. }
  150. table.insert(waypoints, newWaypoint)
  151. end
  152.  
  153. local function drawWaypointPage()
  154. local w, h = monitor.getSize()
  155. monitor.clear()
  156. monitor.setCursorPos(1, 1)
  157. monitor.write("Waypoint Page")
  158. monitor.setCursorPos(1, 3)
  159. monitor.write("Enter coordinates (x, y, z) and color:")
  160. monitor.setCursorPos(1, 4)
  161. monitor.write("Format: x,y,z,color")
  162. monitor.setCursorPos(1, 5)
  163. monitor.write("Colors: red, yellow, green, blue, purple")
  164. monitor.setCursorPos(1, h-1)
  165. monitor.write("Enter to submit, Back to cancel.")
  166. end
  167.  
  168. local function drawCannonAdjustmentPage()
  169. local w, h = monitor.getSize()
  170. monitor.clear()
  171. monitor.setCursorPos(1, 1)
  172. monitor.write("Cannon Pitch and Yaw Adjustment")
  173.  
  174. local cannonNames = {
  175. {name = "Port 5 inch", data = cannonData.port5inch, target = cannonTarget.port5inch},
  176. {name = "Starboard 5 inch", data = cannonData.starboard5inch, target = cannonTarget.starboard5inch},
  177. {name = "Bow 15 inch", data = cannonData.bow15inch, target = cannonTarget.bow15inch},
  178. {name = "Stern 15 inch", data = cannonData.stern15inch, target = cannonTarget.stern15inch}
  179. }
  180.  
  181. local yOffset = 3
  182.  
  183. for _, cannon in ipairs(cannonNames) do
  184. monitor.setCursorPos(1, yOffset)
  185. monitor.write(cannon.name .. ":")
  186.  
  187. monitor.setCursorPos(1, yOffset + 1)
  188. monitor.write("Current: pitch: " .. (cannon.data.pitch and string.format("%.2f", cannon.data.pitch) or "N/A"))
  189. monitor.setCursorPos(26, yOffset + 1)
  190. monitor.write("yaw: " .. (cannon.data.yaw and string.format("%.2f", cannon.data.yaw) or "N/A"))
  191.  
  192. monitor.setCursorPos(1, yOffset + 2)
  193. monitor.write("Adjustment: pitch: " .. (cannon.target.pitchAdjust and string.format("%.2f", cannon.target.pitchAdjust) or "N/A"))
  194. monitor.setCursorPos(26, yOffset + 2)
  195. monitor.write("yaw: " .. (cannon.target.yawAdjust and string.format("%.2f", cannon.target.yawAdjust) or "N/A"))
  196.  
  197. monitor.setCursorPos(1, yOffset + 3)
  198. monitor.write("[pitch +] [pitch -]")
  199. monitor.setCursorPos(w - 30, yOffset + 3)
  200. monitor.write("[yaw +] [yaw -]")
  201.  
  202. yOffset = yOffset + 6 -- Move down for the next cannon
  203. end
  204.  
  205. monitor.setCursorPos(1, h)
  206. monitor.write("Back")
  207. end
  208.  
  209. local function drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  210. local aspectRatio = 1.5
  211. local segmentLength = radarScale / 4
  212. local pixelPerSegmentX = w / 4
  213. local pixelPerSegmentY = h / 4
  214.  
  215. -- Draw the grid
  216. for i = 0, w - pixelPerSegmentX, pixelPerSegmentX do
  217. -- Vertical lines
  218. for y = 1, h - 2 do -- Leave space for the scale display at the bottom
  219. monitor.setCursorPos(i, y)
  220. monitor.write("|")
  221. end
  222. end
  223.  
  224. for j = 0, h - pixelPerSegmentY, pixelPerSegmentY do
  225. -- Horizontal lines
  226. for x = 1, w do
  227. monitor.setCursorPos(x, j)
  228. monitor.write("-")
  229. end
  230. end
  231.  
  232. -- Draw the circle
  233. for i = 0, 360, 5 do
  234. local rad = i * (math.pi / 180)
  235. local x = centerX + (radius * math.sin(rad)) * aspectRatio
  236. local y = centerY + radius * math.cos(rad)
  237. monitor.setCursorPos(math.floor(x + 0.5), math.floor(y + 0.5))
  238. monitor.write(".")
  239. end
  240.  
  241. -- Draw the crosshair
  242. for y = centerY - radius, centerY + radius do
  243. monitor.setCursorPos(centerX, y)
  244. monitor.write("|")
  245. end
  246.  
  247. local trueBearing = (math.deg(shipYawRadians) + 360) % 360 - 180
  248. if trueBearing < 0 then
  249. trueBearing = trueBearing + 360
  250. end
  251. local textPos = centerY - radius + 10
  252. if textPos > 0 then
  253. monitor.setCursorPos(centerX - (#tostring(math.floor(trueBearing)) + 2) / 2, textPos)
  254. monitor.write(tostring(math.floor(trueBearing)) .. "°")
  255. end
  256.  
  257. local directions = {"N", "W", "S", "E"}
  258. local dirAngles = {0, 90, 180, 270}
  259.  
  260. for index, dir in ipairs(directions) do
  261. local dirRad = (dirAngles[index] + math.deg(shipYawRadians)) * (math.pi / 180)
  262. local dirX = centerX + (radius * math.sin(dirRad)) * aspectRatio
  263. local dirY = centerY + radius * math.cos(dirRad)
  264. monitor.setCursorPos(math.floor(dirX + 0.5), math.floor(dirY + 0.5))
  265. monitor.write(dir)
  266. end
  267.  
  268. -- Display segment length
  269. monitor.setCursorPos(1, centerY + radius + 2)
  270. monitor.write("Segment Length: " .. segmentLength .. " meters")
  271. end
  272.  
  273. local function drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  274. local dx = relX - centerX
  275. local dy = relY - centerY
  276. local mag = math.sqrt((dx / aspectRatio)^2 + dy^2)
  277.  
  278. if (mag > radius) then
  279. local intersectX = centerX + ((dx / mag) * radius) * aspectRatio
  280. local intersectY = centerY + (dy / mag) * radius
  281. return intersectX, intersectY
  282. else
  283. return relX, relY
  284. end
  285. end
  286.  
  287. local function drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  288. local pos = ship.getWorldspacePosition()
  289. local shipYawRadians = math.atan2(-ship.getRotationMatrix()[1][3], ship.getRotationMatrix()[3][3])
  290. if shipYawRadians < 0 then shipYawRadians = shipYawRadians + 2 * math.pi end
  291. -- Function to calculate relative yaw in degrees from ship's facing
  292. local function relativeYaw(dx, dz)
  293. local angle = math.atan2(dx, dz)
  294. local relativeAngle = angle + shipYawRadians - math.pi
  295. if relativeAngle < -math.pi then
  296. relativeAngle = relativeAngle + 2 * math.pi
  297. elseif relativeAngle >= math.pi then
  298. relativeAngle = relativeAngle - 2 * math.pi
  299. end
  300. return math.deg(relativeAngle)
  301. end
  302.  
  303. local function drawCrosshairForCannon(cannonData, color)
  304. if cannonData.hitPosX and cannonData.hitPosZ then
  305. local dx = cannonData.hitPosX - pos.x
  306. local dz = cannonData.hitPosZ - pos.z
  307. local cannonYaw = relativeYaw(dx, dz)
  308. local distance = math.sqrt(dx^2 + dz^2)
  309. local scaledDistance = (distance / radarScale) * radius
  310. local relX = centerX + (scaledDistance * math.sin(math.rad(cannonYaw))) * aspectRatio
  311. local relY = centerY + scaledDistance * math.cos(math.rad(cannonYaw))
  312. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  313. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  314. monitor.setBackgroundColor(color)
  315. monitor.write("X")
  316. monitor.setBackgroundColor(colors.black)
  317. return cannonYaw
  318. end
  319. return nil
  320. end
  321.  
  322. -- Draw crosshair and check range for each cannon
  323. local portYaw = drawCrosshairForCannon(cannonData.port5inch, colors.red)
  324. if portYaw and not(portYaw > 225 and portYaw < 330) then
  325. monitor.setCursorPos(1, 1)
  326. monitor.write("Warning: Port cannon may hit the ship!")
  327. end
  328.  
  329. local starboardYaw = drawCrosshairForCannon(cannonData.starboard5inch, colors.blue)
  330. if starboardYaw and not(starboardYaw > 30 and starboardYaw < 135) then
  331. monitor.setCursorPos(1, 2)
  332. monitor.write("Warning: Starboard cannon may hit the ship!")
  333. end
  334.  
  335. local bowYaw = drawCrosshairForCannon(cannonData.bow15inch, colors.green)
  336. if bowYaw and not(bowYaw > 120 and bowYaw < 240) then
  337. monitor.setCursorPos(1, 3)
  338. monitor.write("Warning: Bow cannon may hit the ship!")
  339. end
  340.  
  341. local sternYaw = drawCrosshairForCannon(cannonData.stern15inch, colors.yellow)
  342. if sternYaw and not(sternYaw > 300 or sternYaw < 60) then
  343. monitor.setCursorPos(1, 4)
  344. monitor.write("Warning: Stern cannon may hit the ship!")
  345. end
  346. end
  347.  
  348. local function calculateRelativeAngle(objectAngle, shipYawRadians)
  349. local relativeAngle = objectAngle + shipYawRadians - math.pi
  350. if relativeAngle < -math.pi then
  351. relativeAngle = relativeAngle + 2 * math.pi
  352. elseif relativeAngle >= math.pi then
  353. relativeAngle = relativeAngle - 2 * math.pi
  354. end
  355. return relativeAngle
  356. end
  357.  
  358. local function drawRadarDisplay()
  359. local w, h = monitor.getSize()
  360. local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  361. local radius = math.min(centerX, centerY) - 1
  362. local aspectRatio = 1.2
  363.  
  364. local matrix = ship.getRotationMatrix()
  365. local shipYawRadians = math.atan2(-matrix[1][3], matrix[3][3])
  366. if (shipYawRadians < 0) then
  367. shipYawRadians = shipYawRadians + 2 * math.pi
  368. end
  369.  
  370. drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  371. monitor.setCursorPos(centerX, centerY)
  372. monitor.write("+")
  373.  
  374. drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  375.  
  376. local pos = ship.getWorldspacePosition()
  377. local results = radar.scanForShips(10000)
  378. targetPositions = {}
  379. for _, object in ipairs(results) do
  380. local x = object.pos.x - pos.x
  381. local z = object.pos.z - pos.z
  382. local distance = math.sqrt(x^2 + z^2)
  383. local objectAngle = math.atan2(x, z)
  384. local relativeAngle = calculateRelativeAngle(objectAngle, shipYawRadians)
  385.  
  386. local scaledDistance = (distance / radarScale) * radius
  387. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  388. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  389.  
  390. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  391.  
  392. if (object.mass > 2000 and distance > 5) then
  393. if (distance > radarScale) then
  394. monitor.setTextColor(colors.lightBlue)
  395. else
  396. monitor.setTextColor(colors.red)
  397. end
  398. if (checkFriendly(object.id)) then
  399. monitor.setBackgroundColor(colors.blue)
  400. elseif (object.mass > 1000000) then
  401. monitor.setBackgroundColor(colors.orange)
  402. elseif (math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2) > 30) then
  403. monitor.setBackgroundColor(colors.red)
  404. else
  405. monitor.setBackgroundColor(colors.yellow)
  406. end
  407. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  408. monitor.write("o")
  409. monitor.setTextColor(colors.green)
  410. monitor.setBackgroundColor(colors.black)
  411.  
  412. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = object})
  413. end
  414. end
  415.  
  416. -- Draw waypoints
  417. for index, waypoint in ipairs(waypoints) do
  418. local wpX = waypoint.x - pos.x
  419. local wpZ = waypoint.z - pos.z
  420. local distance = math.sqrt(wpX^2 + wpZ^2)
  421. local wpAngle = math.atan2(wpX, wpZ)
  422. local relativeAngle = calculateRelativeAngle(wpAngle, shipYawRadians)
  423.  
  424. local scaledDistance = (distance / radarScale) * radius
  425. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  426. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  427.  
  428. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  429.  
  430. monitor.setBackgroundColor(waypoint.color)
  431. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  432. monitor.write("W")
  433. monitor.setBackgroundColor(colors.black)
  434.  
  435. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = {type = "waypoint", x = waypoint.x,y = waypoint.y, z = waypoint.z, index = index}})
  436. end
  437. end
  438.  
  439. local function displayTargetInfo(target)
  440. local w, h = monitor.getSize()
  441. local pos = ship.getWorldspacePosition()
  442.  
  443. monitor.clear()
  444. monitor.setCursorPos(1, h-12)
  445. monitor.write("Target Info")
  446. monitor.setCursorPos(1, h-11)
  447.  
  448. local targetPosX, targetPosY, targetPosZ
  449. if target.type == "waypoint" then
  450. monitor.write("Type: Waypoint")
  451. targetPosX, targetPosY, targetPosZ = target.x, target.y, target.z
  452. monitor.setCursorPos(1, h-7)
  453. monitor.write("Coordinate: " .. target.x .. ", "..target.y..", ".. target.z)
  454. else
  455. monitor.write("ID: " .. target.id)
  456. targetPosX, targetPosY, targetPosZ = target.pos.x, target.pos.y, target.pos.z
  457. monitor.setCursorPos(1, h-10)
  458. monitor.write("Mass: " .. target.mass)
  459. monitor.setCursorPos(1, h-9)
  460. monitor.write("Velocity: " .. string.format("%.2f, %.2f, %.2f", target.velocity.x, target.velocity.y, target.velocity.z))
  461. monitor.setCursorPos(1, h-8)
  462. monitor.write("Position: " .. string.format("%.2f, %.2f, %.2f", target.pos.x, target.pos.y, target.pos.z))
  463. end
  464.  
  465. local distance = math.sqrt((targetPosX - pos.x)^2 + (targetPosZ - pos.z)^2)
  466. local dx = targetPosX - pos.x
  467. local dz = targetPosZ - pos.z
  468. local bearing = (math.deg(math.atan2(dz, dx)) + 360) % 360 - 270
  469. if bearing < 0 then
  470. bearing = bearing + 360
  471. end
  472.  
  473. monitor.setCursorPos(1, h-7)
  474. monitor.write("Distance: " .. string.format("%.2f", distance) .. " meters")
  475. monitor.setCursorPos(1, h-6)
  476. monitor.write("Bearing: " .. string.format("%.2f", bearing) .. "°")
  477.  
  478. if target.type ~= "waypoint" then
  479. local type = "unknown"
  480. local hostile = "hostile"
  481. if target.mass > 1000000 then
  482. type = "ship"
  483. elseif math.sqrt(target.velocity.x ^ 2 + target.velocity.y ^ 2 + target.velocity.z ^ 2) > 30 then
  484. type = "Flying missile / plane"
  485. else
  486. type = "unknown"
  487. end
  488. if checkFriendly(target.id) then
  489. hostile = "friendly"
  490. else
  491. hostile = "hostile"
  492. end
  493. monitor.setCursorPos(1, h-5)
  494. monitor.write("Type: " .. hostile .. " " .. type)
  495. end
  496.  
  497. if target.type == "waypoint" then
  498. monitor.setCursorPos(1, h-5)
  499. monitor.write("[Lock Waypoint with port gun]")
  500. monitor.setCursorPos(1, h-4)
  501. monitor.write("[Lock Waypoint with starboard gun]")
  502. monitor.setCursorPos(1, h-3)
  503. monitor.write("[Lock Waypoint with bow gun]")
  504. monitor.setCursorPos(1, h-2)
  505. monitor.write("[Lock Waypoint with stern gun]")
  506. else
  507. monitor.setCursorPos(1, h-5)
  508. monitor.write("[Lock Target with port gun]")
  509. monitor.setCursorPos(1, h-4)
  510. monitor.write("[Lock Target with starboard gun]")
  511. monitor.setCursorPos(1, h-3)
  512. monitor.write("[Lock Target with bow gun]")
  513. monitor.setCursorPos(1, h-2)
  514. monitor.write("[Lock Target with stern gun]")
  515. end
  516. monitor.setCursorPos(1, h)
  517. monitor.write("Back")
  518. end
  519.  
  520. local function handleTouch()
  521. local w, h = monitor.getSize()
  522. while true do
  523. local event, side, x, y = os.pullEvent("monitor_touch")
  524. if displayState == "radar" then
  525. if y == h-1 and x >= 8 and x <= 9 then
  526. radarScale = math.max(100, radarScale - 100)
  527. drawControls()
  528. drawRadarDisplay()
  529. elseif y == h-1 and x >= 10 and x <= 11 then
  530. radarScale = math.min(10000, radarScale + 100)
  531. drawControls()
  532. drawRadarDisplay()
  533. elseif y == h-1 and x >= w-10 and x <= w then
  534. displayState = "waypoints"
  535. monitor.clear()
  536. drawWaypointPage()
  537. print("Enter waypoint coordinates (x, y, z, color), color options: red, yellow, green, blue:")
  538. local input = read()
  539. local x, y, z, color = input:match("([^,]+),([^,]+),([^,]+),([^,]*)")
  540. local colorValue = colors.purple
  541. if color then
  542. local colorMap = {
  543. purple = colors.purple,
  544. red = colors.red,
  545. yellow = colors.yellow,
  546. green = colors.green,
  547. blue = colors.blue,
  548. white = colors.white,
  549. orange = colors.orange,
  550. lightBlue = colors.lightBlue,
  551. lime = colors.lime,
  552. pink = colors.pink,
  553. gray = colors.gray,
  554. lightGray = colors.lightGray,
  555. cyan = colors.cyan,
  556. purple = colors.purple,
  557. brown = colors.brown,
  558. black = colors.black
  559. }
  560. colorValue = colorMap[color] or colors.purple
  561. end
  562. if x and y and z then
  563. addWaypoint(tonumber(x), tonumber(y), tonumber(z), colorValue)
  564. end
  565. displayState = "radar"
  566. monitor.clear()
  567. drawControls()
  568. drawRadarDisplay()
  569. elseif y == 1 and x >= w-13 and x <= w then
  570. displayState = "adjustCannon"
  571. monitor.clear()
  572. drawCannonAdjustmentPage()
  573. elseif y == h-2 and x < 10 then
  574. cannonTarget.port5inch = { type = nil, side = "port", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  575. cannonTarget.starboard5inch = { type = nil, side = "starboard", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  576. cannonTarget.bow15inch = { type = nil, side = "bow", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  577. cannonTarget.stern15inch = { type = nil, side = "stern", id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  578. monitor.setCursorPos(1, 1)
  579. monitor.clear()
  580. drawRadarDisplay()
  581. monitor.write("Locking stopped.")
  582. else
  583. for _, target in ipairs(targetPositions) do
  584. if x == target.x and y == target.y then
  585. selectedTarget = target.data
  586. displayState = "targetInfo"
  587. displayTargetInfo(selectedTarget)
  588. break
  589. end
  590. end
  591. end
  592. elseif displayState == "targetInfo" then
  593. if y == h and x >= 1 and x <= 4 then
  594. -- Back button
  595. displayState = "radar"
  596. monitor.clear()
  597. drawRadarDisplay()
  598. elseif y == h-5 and x >= 1 and x <= 28 then
  599. -- Lock with port cannon
  600. if selectedTarget.type == "waypoint" then
  601. cannonTarget.port5inch.type = "waypoint"
  602. cannonTarget.port5inch.x = selectedTarget.x
  603. cannonTarget.port5inch.y = selectedTarget.y
  604. cannonTarget.port5inch.z = selectedTarget.z
  605. cannonTarget.port5inch.id = nil
  606. else
  607. cannonTarget.port5inch.id = selectedTarget.id
  608. cannonTarget.port5inch.type = "target"
  609. cannonTarget.port5inch.x = nil
  610. cannonTarget.port5inch.y = nil
  611. cannonTarget.port5inch.z = nil
  612. end
  613. displayState = "radar"
  614. monitor.clear()
  615. drawRadarDisplay()
  616. monitor.setCursorPos(1, 1)
  617. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  618. elseif y == h-4 and x >= 1 and x <= 32 then
  619. -- Lock with starboard cannon
  620. if selectedTarget.type == "waypoint" then
  621. cannonTarget.starboard5inch.type = "waypoint"
  622. cannonTarget.starboard5inch.x = selectedTarget.x
  623. cannonTarget.starboard5inch.y = selectedTarget.y
  624. cannonTarget.starboard5inch.z = selectedTarget.z
  625. cannonTarget.starboard5inch.id = nil
  626. else
  627. cannonTarget.starboard5inch.id = selectedTarget.id
  628. cannonTarget.starboard5inch.type = "target"
  629. cannonTarget.starboard5inch.x = nil
  630. cannonTarget.starboard5inch.y = nil
  631. cannonTarget.starboard5inch.z = nil
  632. end
  633. displayState = "radar"
  634. monitor.clear()
  635. drawRadarDisplay()
  636. monitor.setCursorPos(1, 1)
  637. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  638. elseif y == h-3 and x >= 1 and x <= 22 then
  639. -- Lock with bow cannon
  640. if selectedTarget.type == "waypoint" then
  641. cannonTarget.bow15inch.type = "waypoint"
  642. cannonTarget.bow15inch.x = selectedTarget.x
  643. cannonTarget.bow15inch.y = selectedTarget.y
  644. cannonTarget.bow15inch.z = selectedTarget.z
  645. cannonTarget.bow15inch.id = nil
  646. else
  647. cannonTarget.bow15inch.id = selectedTarget.id
  648. cannonTarget.bow15inch.type = "target"
  649. cannonTarget.bow15inch.x = nil
  650. cannonTarget.bow15inch.y = nil
  651. cannonTarget.bow15inch.z = nil
  652. end
  653. displayState = "radar"
  654. monitor.clear()
  655. drawRadarDisplay()
  656. monitor.setCursorPos(1, 1)
  657. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  658. elseif y == h-2 and x >= 1 and x <= 22 then
  659. -- Lock with stern cannon
  660. if selectedTarget.type == "waypoint" then
  661. cannonTarget.stern15inch.type = "waypoint"
  662. cannonTarget.stern15inch.x = selectedTarget.x
  663. cannonTarget.stern15inch.y = selectedTarget.y
  664. cannonTarget.stern15inch.z = selectedTarget.z
  665. cannonTarget.stern15inch.id = nil
  666. else
  667. cannonTarget.stern15inch.id = selectedTarget.id
  668. cannonTarget.stern15inch.type = "target"
  669. cannonTarget.stern15inch.x = nil
  670. cannonTarget.stern15inch.y = nil
  671. cannonTarget.stern15inch.z = nil
  672. end
  673. displayState = "radar"
  674. monitor.clear()
  675. drawRadarDisplay()
  676. monitor.setCursorPos(1, 1)
  677. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  678. end
  679. elseif displayState == "adjustCannon" then
  680. -- Back button
  681. if y == h and x >= 1 and x <= 4 then
  682. displayState = "radar"
  683. monitor.clear()
  684. drawRadarDisplay()
  685. end
  686.  
  687. -- Detect pitch and yaw adjustment button presses
  688. local yOffset = 3
  689. local buttonWidth = 10
  690. for i, cannonName in ipairs({"port5inch", "starboard5inch", "bow15inch", "stern15inch"}) do
  691. if y == yOffset + 3 then
  692. if x >= 1 and x <= 10 then
  693. -- Increase pitch
  694. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust + 0.1
  695. elseif x >= 11 and x <= 20 then
  696. -- Decrease pitch
  697. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust - 0.1
  698. elseif x >= w - 30 and x <= w - 20 then
  699. -- Increase yaw
  700. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust + 0.1
  701. elseif x >= w - 19 and x <= w - 9 then
  702. -- Decrease yaw
  703. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust - 0.1
  704. end
  705. monitor.clear()
  706. drawCannonAdjustmentPage()
  707. end
  708. yOffset = yOffset + 6
  709. end
  710. end
  711. end
  712. end
  713.  
  714. local function modemMessage()
  715. while true do
  716. if modem then
  717. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  718. if channel == port5inchChannel then
  719. cannonData.port5inch.hitPosX = message.x
  720. cannonData.port5inch.hitPosY = message.y
  721. cannonData.port5inch.hitPosZ = message.z
  722. cannonData.port5inch.pitch = message.cannonPitch
  723. cannonData.port5inch.yaw = message.cannonYaw
  724. cannonData.port5inch.source = message.source
  725. elseif channel == starboard5inchChannel then
  726. cannonData.starboard5inch.hitPosX = message.x
  727. cannonData.starboard5inch.hitPosY = message.y
  728. cannonData.starboard5inch.hitPosZ = message.z
  729. cannonData.starboard5inch.pitch = message.cannonPitch
  730. cannonData.starboard5inch.yaw = message.cannonYaw
  731. cannonData.starboard5inch.source = message.source
  732. elseif channel == bow15inchChannel then
  733. cannonData.bow15inch.hitPosX = message.x
  734. cannonData.bow15inch.hitPosY = message.y
  735. cannonData.bow15inch.hitPosZ = message.z
  736. cannonData.bow15inch.pitch = message.cannonPitch
  737. cannonData.bow15inch.yaw = message.cannonYaw
  738. cannonData.bow15inch.source = message.source
  739. elseif channel == stern15inchChannel then
  740. cannonData.stern15inch.hitPosX = message.x
  741. cannonData.stern15inch.hitPosY = message.y
  742. cannonData.stern15inch.hitPosZ = message.z
  743. cannonData.stern15inch.pitch = message.cannonPitch
  744. cannonData.stern15inch.yaw = message.cannonYaw
  745. cannonData.stern15inch.source = message.source
  746. end
  747. else
  748. sleep()
  749. end
  750. end
  751. end
  752.  
  753. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  754. local radians = math.rad(angle)
  755. local u = projectileSpeed/20
  756. local part1 = u * math.cos(radians) / math.log(cd)
  757. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  758. local XR = part1 * part2
  759.  
  760. return XR
  761. end
  762.  
  763. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  764. local bestLowPitch = nil
  765. local bestHighPitch = nil
  766. local bestLowDistance = math.huge
  767. local bestHighDistance = math.huge
  768. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  769.  
  770. for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
  771. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  772. local distanceDifference = math.abs(calculatedRange - targetDistance)
  773.  
  774. -- Find the low-angle solution
  775. if pitch <= 30 then
  776. if distanceDifference < bestLowDistance then
  777. bestLowDistance = distanceDifference
  778. bestLowPitch = pitch
  779. end
  780. -- Find the high-angle solution
  781. elseif pitch > 30 then
  782. if distanceDifference < bestHighDistance then
  783. bestHighDistance = distanceDifference
  784. bestHighPitch = pitch
  785. end
  786. end
  787. end
  788.  
  789. -- If the target is within 400 blocks, prioritize the low-angle solution
  790. if targetDistance <= 830 then
  791. return bestLowPitch or bestLowPitch
  792. else
  793. return bestHighPitch or bestLowPitch
  794. end
  795. end
  796.  
  797. local function aimCannon(targetPos, targetVel, side)
  798. if targetPos and targetVel then
  799. -- Determine which channel to use based on side
  800. local channel
  801. if side == "port" then
  802. channel = port5inchChannel
  803. elseif side == "starboard" then
  804. channel = starboard5inchChannel
  805. elseif side == "bow" then
  806. channel = bow15inchChannel
  807. elseif side == "stern" then
  808. channel = stern15inchChannel
  809. end
  810. targetInfo = {targetPos = targetPos, targetVel = targetVel}
  811. print("channel: "..channel)
  812. print(textutils.serialize(targetInfo))
  813. --yaw adjustment
  814. if modem and targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  815. modem.transmit(channel,0,targetInfo)
  816. end
  817. end
  818. end
  819.  
  820. local function aimPortCannonContinuous()
  821. while true do
  822. local cannon = cannonTarget.port5inch
  823. if cannon.type then
  824. local target
  825. monitor.setCursorPos(1,1)
  826. if cannon.type=="waypoint" then
  827. monitor.write("Port waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  828. else
  829. monitor.write("Port ship locked: "..cannon.id)
  830. end
  831. if cannon.type == "waypoint" then
  832. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  833. else -- handling target
  834. local scanResults = radar.scanForShips(9999)
  835. for _, object in ipairs(scanResults) do
  836. if object.id == cannon.id then
  837. target = object
  838. break
  839. end
  840. end
  841. end
  842.  
  843. if target and target.pos then
  844. aimCannon(target.pos, target.velocity, "port")
  845. end
  846. else
  847.  
  848. end
  849. sleep() -- Adjust sleep duration based on required reaction time
  850. end
  851. end
  852.  
  853. local function aimStarboardCannonContinuous()
  854. while true do
  855. local cannon = cannonTarget.starboard5inch
  856. if cannon.type then
  857. local target
  858. monitor.setCursorPos(1,2)
  859. if cannon.type=="waypoint" then
  860. monitor.write("Starboard waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  861. else
  862. monitor.write("Starboard ship locked: "..cannon.id)
  863. end
  864. if cannon.type == "waypoint" then
  865. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  866. else -- handling target
  867. local scanResults = radar.scanForShips(9999)
  868. for _, object in ipairs(scanResults) do
  869. if object.id == cannon.id then
  870. target = object
  871. break
  872. end
  873. end
  874. end
  875.  
  876. if target and target.pos then
  877. aimCannon(target.pos, target.velocity, "starboard")
  878. end
  879. end
  880. sleep()
  881. end
  882. end
  883.  
  884. local function aimBowCannonContinuous()
  885. while true do
  886. local cannon = cannonTarget.bow15inch
  887. if cannon.type then
  888. local target
  889. monitor.setCursorPos(1,3)
  890. if cannon.type=="waypoint" then
  891. monitor.write("Bow waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  892. else
  893. monitor.write("Bow ship locked: "..cannon.id)
  894. end
  895. if cannon.type == "waypoint" then
  896. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  897. else -- handling target
  898. local scanResults = radar.scanForShips(9999)
  899. for _, object in ipairs(scanResults) do
  900. if object.id == cannon.id then
  901. target = object
  902. break
  903. end
  904. end
  905. end
  906.  
  907. if target and target.pos then
  908. aimCannon(target.pos, target.velocity, "bow")
  909. end
  910. end
  911. sleep()
  912. end
  913. end
  914.  
  915. local function aimSternCannonContinuous()
  916. while true do
  917. local cannon = cannonTarget.stern15inch
  918. if cannon.type then
  919. local target
  920. monitor.setCursorPos(1,4)
  921. if cannon.type=="waypoint" then
  922. monitor.write("Stern waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  923. else
  924. monitor.write("Stern ship locked: "..cannon.id)
  925. end
  926. if cannon.type == "waypoint" then
  927. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  928. else -- handling target
  929. local scanResults = radar.scanForShips(9999)
  930. for _, object in ipairs(scanResults) do
  931. if object.id == cannon.id then
  932. target = object
  933. break
  934. end
  935. end
  936. end
  937.  
  938. if target and target.pos then
  939. aimCannon(target.pos, target.velocity,"stern")
  940. end
  941. end
  942. sleep()
  943. end
  944. end
  945.  
  946. local function calculateSpeed(pos1, pos2, deltaTime)
  947. if pos1 and pos2 then
  948. local dx = pos2[1] - pos1[1]
  949. local dy = pos2[2] - pos1[2]
  950. local dz = pos2[3] - pos1[3]
  951. return math.sqrt(dx^2 + dy^2 + dz^2) / deltaTime
  952. end
  953. return 0
  954. end
  955.  
  956. local function missileWarning()
  957. local previousPositions = {}
  958. while true do
  959. local objectList = radar.scanForEntities(600)
  960. -- Initial scan to capture positions
  961. local initialPositions = {}
  962. local shipPos = ship.getWorldspacePosition()
  963. for _, object in ipairs(objectList) do
  964. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  965. if object.pos then
  966. initialPositions[object.entity_type .. table.concat(object.pos, ":")] = object.pos
  967. print("missile detected")
  968. end
  969. end
  970. end
  971.  
  972. sleep(0.1) -- Short delay to measure displacement
  973.  
  974. objectList = radar.scanForEntities(600) -- Second scan to calculate speed
  975. local currentPositions = {}
  976. for _, object in ipairs(objectList) do
  977. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  978. if object.pos then
  979. print("Missile detected for second time")
  980. local key = object.entity_type .. table.concat(object.pos, ":")
  981. currentPositions[key] = object.pos
  982. local previousPos = initialPositions[key]
  983. previousPos = nil
  984. if previousPos then
  985. local speed = calculateSpeed(previousPos, object.pos, 0.1)
  986. print(textutils.serialize(previousPos))
  987. print(textutils.serialize(object.pos))
  988. print("speed: "..speed)
  989. if speed > 5 then -- Check if speed exceeds threshold
  990. local objX, objY, objZ = object.pos[1], object.pos[2], object.pos[3]
  991. local missileVector = {x = objX - previousPos[1], y = objY - previousPos[2], z = objZ - previousPos[3]}
  992. local toPlayerVector = {x = shipPos.x - objX, y = shipPos.y - objY, z = shipPos.z - objZ}
  993. local dotProduct = missileVector.x * toPlayerVector.x + missileVector.y * toPlayerVector.y + missileVector.z * toPlayerVector.z
  994. print("Missile with speed > 10 detected")
  995. if dotProduct then -- Missile is moving towards the player
  996. print("Missile coming for player")
  997. local angle = math.deg(math.atan2(missileVector.z, missileVector.x) - math.atan2(toPlayerVector.z, toPlayerVector.x))
  998. local direction = ((angle + 360) % 360) / 30
  999. local clockDirection = math.floor(direction + 1)
  1000. if clockDirection == 0 then clockDirection = 12 end
  1001. monitor.setCursorPos(0,6)
  1002. monitor.write("Missile incoming from " .. clockDirection .. " o'clock")
  1003. if speaker then
  1004. speaker.playSound("entity.lightning_bolt.thunder", 1.0)
  1005. end
  1006. end
  1007. end
  1008. end
  1009. end
  1010. end
  1011. end
  1012.  
  1013. previousPositions = currentPositions
  1014. sleep() -- Delay before the next round of scanning
  1015. end
  1016. end
  1017.  
  1018. parallel.waitForAny(
  1019. function()
  1020. -- Main loop
  1021. while true do
  1022. if displayState == "radar" then
  1023. monitor.clear()
  1024. monitor.setTextScale(0.5)
  1025. drawRadarDisplay()
  1026. drawControls()
  1027. elseif displayState == "targetInfo" and selectedTarget then
  1028. displayTargetInfo(selectedTarget)
  1029. elseif displayState == "adjustCannon" then
  1030. monitor.clear()
  1031. drawCannonAdjustmentPage()
  1032. end
  1033. sleep(0.1)
  1034. -- Adjust this duration if needed
  1035. end
  1036. end,
  1037. handleTouch,
  1038. modemMessage,
  1039. aimPortCannonContinuous,
  1040. aimStarboardCannonContinuous,
  1041. aimBowCannonContinuous,
  1042. aimSternCannonContinuous,
  1043. missileWarning
  1044. )
  1045.  
  1046.  
  1047.  
Advertisement
Add Comment
Please, Sign In to add comment