MegiRicky

NGDWBVRGen3

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