MegiRicky

NGDWBVRGen5

Jul 25th, 2025 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 61.17 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local modem = peripheral.wrap("right")
  3. local speaker = peripheral.find("speaker")
  4. local holo = peripheral.find("hologram")
  5.  
  6. -- Ensure all required peripherals are connected
  7. if not radar or not ship or not holo then
  8. error("Required peripherals are not attached")
  9. end
  10.  
  11.  
  12. local friendlyID = {289, 232}
  13. local targetPositions = {}
  14. local waypoints = {}
  15. local displayState = "radar" -- Initial display state
  16. local selectedTarget = nil
  17. targetTypeIcon = "None"
  18.  
  19. local lockedTargetId = nil
  20. local lockedWaypoint = nil -- Variable to store the locked waypoint
  21. local projectileSpeed = 320 --m/s
  22.  
  23. local LaunchedMissiles = {}
  24. local missileControls = {
  25. fireMissile = {}
  26. }
  27. local immediateMissileInfo = {}
  28. local AIMInfoList,GBUInfoList,thunderBoltInfoList = {},{},{}
  29. local pendingMissileLaunches = {}
  30. local AIMCount,GBUCount,thunderboltCount = 0,0,0
  31.  
  32. -- Get channel data for cannons
  33. local function getChannelInput(prompt, default)
  34. print(prompt .. ", default: " .. default)
  35. local input = io.read()
  36. if input == "" then
  37. input = default
  38. end
  39. return tonumber(input)
  40. end
  41.  
  42. local controlChannel = getChannelInput("Input the control channel",500)
  43. local waypointChannel = getChannelInput("Input the control channel",1421)
  44. local port5inchChannel = getChannelInput("Input the port side cannon data channel", 900)
  45. local starboard5inchChannel = getChannelInput("Input the starboard side cannon data channel", 901)
  46. local bow15inchChannel = getChannelInput("Input the bow cannon data channel", 902)
  47. local stern15inchChannel = getChannelInput("Input the stern cannon data channel", 903)
  48. local missileControlChannel = getChannelInput("Input the missile control channel", 1400)
  49. local missileInfoChannel = missileControlChannel + 10
  50.  
  51. if modem then
  52. modem.open(waypointChannel)
  53. modem.open(controlChannel)
  54. modem.open(starboard5inchChannel)
  55. modem.open(port5inchChannel)
  56. modem.open(bow15inchChannel)
  57. modem.open(stern15inchChannel)
  58. modem.open(missileInfoChannel)
  59. modem.open(missileControlChannel)
  60. end
  61.  
  62. local cannonData = {
  63. port5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {} },
  64. starboard5inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  65. bow15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  66. stern15inch = {hitPosX = nil, hitPosY = nil, hitPosZ = nil, pitch = nil, yaw = nil, source = {}},
  67. }
  68.  
  69. local cannonTarget = {
  70. port5inch = { type = nil, side = "port", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  71. starboard5inch = { type = nil, side = "starboard", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0 },
  72. bow15inch = { type = nil, side = "bow", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  73. stern15inch = { type = nil, side = "stern", id = nil, x = nil, y = nil, z = nil, yawAdjust = 0, pitchAdjust = 0},
  74. }
  75.  
  76. --================--
  77. --Utility function--
  78. --================--
  79. local function checkFriendly(ID)
  80. local friendly = false
  81. for _, id in ipairs(friendlyID) do
  82. if tonumber(ID) == tonumber(id) then
  83. friendly = true
  84. break
  85. end
  86. end
  87. return friendly
  88. end
  89.  
  90. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  91. local radians = math.rad(angle)
  92. local u = projectileSpeed/20
  93. local part1 = u * math.cos(radians) / math.log(cd)
  94. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  95. local XR = part1 * part2
  96.  
  97. return XR
  98. end
  99.  
  100. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  101. local bestLowPitch = nil
  102. local bestHighPitch = nil
  103. local bestLowDistance = math.huge
  104. local bestHighDistance = math.huge
  105. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  106.  
  107. for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
  108. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  109. local distanceDifference = math.abs(calculatedRange - targetDistance)
  110.  
  111. -- Find the low-angle solution
  112. if pitch <= 30 then
  113. if distanceDifference < bestLowDistance then
  114. bestLowDistance = distanceDifference
  115. bestLowPitch = pitch
  116. end
  117. -- Find the high-angle solution
  118. elseif pitch > 30 then
  119. if distanceDifference < bestHighDistance then
  120. bestHighDistance = distanceDifference
  121. bestHighPitch = pitch
  122. end
  123. end
  124. end
  125.  
  126. -- If the target is within 400 blocks, prioritize the low-angle solution
  127. if targetDistance <= 830 then
  128. return bestLowPitch or bestLowPitch
  129. else
  130. return bestHighPitch or bestLowPitch
  131. end
  132. end
  133.  
  134. local function calculateSpeed(pos1, pos2, deltaTime)
  135. if pos1 and pos2 then
  136. local dx = pos2[1] - pos1[1]
  137. local dy = pos2[2] - pos1[2]
  138. local dz = pos2[3] - pos1[3]
  139. return math.sqrt(dx^2 + dy^2 + dz^2) / deltaTime
  140. end
  141. return 0
  142. end
  143.  
  144. local function modemMessage()
  145. while true do
  146. if modem then
  147. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  148. if channel == port5inchChannel then
  149. cannonData.port5inch.hitPosX = message.x
  150. cannonData.port5inch.hitPosY = message.y
  151. cannonData.port5inch.hitPosZ = message.z
  152. cannonData.port5inch.pitch = message.cannonPitch
  153. cannonData.port5inch.yaw = message.cannonYaw
  154. cannonData.port5inch.source = message.source
  155. elseif channel == starboard5inchChannel then
  156. cannonData.starboard5inch.hitPosX = message.x
  157. cannonData.starboard5inch.hitPosY = message.y
  158. cannonData.starboard5inch.hitPosZ = message.z
  159. cannonData.starboard5inch.pitch = message.cannonPitch
  160. cannonData.starboard5inch.yaw = message.cannonYaw
  161. cannonData.starboard5inch.source = message.source
  162. elseif channel == bow15inchChannel then
  163. cannonData.bow15inch.hitPosX = message.x
  164. cannonData.bow15inch.hitPosY = message.y
  165. cannonData.bow15inch.hitPosZ = message.z
  166. cannonData.bow15inch.pitch = message.cannonPitch
  167. cannonData.bow15inch.yaw = message.cannonYaw
  168. cannonData.bow15inch.source = message.source
  169. elseif channel == stern15inchChannel then
  170. cannonData.stern15inch.hitPosX = message.x
  171. cannonData.stern15inch.hitPosY = message.y
  172. cannonData.stern15inch.hitPosZ = message.z
  173. cannonData.stern15inch.pitch = message.cannonPitch
  174. cannonData.stern15inch.yaw = message.cannonYaw
  175. cannonData.stern15inch.source = message.source
  176. elseif channel == missileInfoChannel then
  177. immediateMissileInfo = message
  178. end
  179. else
  180. sleep()
  181. end
  182. end
  183. end
  184.  
  185. local function distance(a, b)
  186. return math.sqrt((a.x - b.x)^2 + (a.y - b.y)^2 + (a.z - b.z)^2)
  187. end
  188.  
  189. --================--
  190. --Waypoints function--
  191. --================--
  192. local function addWaypoint(x, y, z, color)
  193. local newWaypoint = {
  194. x = x,
  195. y = y,
  196. z = z,
  197. color = color or colors.purple
  198. }
  199. table.insert(waypoints, newWaypoint)
  200. end
  201.  
  202. local function deleteWaypointByPos(x, y, z)
  203. for i = #waypoints, 1, -1 do
  204. local wp = waypoints[i]
  205. if wp.x == tonumber(x) and wp.y == tonumber(y) and wp.z == tonumber(z) then
  206. table.remove(waypoints, i)
  207. return true
  208. end
  209. end
  210. return false
  211. end
  212.  
  213. --================--
  214. --Display function--
  215. --================--
  216. -- Configuration
  217. local w, h = 640,640
  218. local centerX, centerY = w / 2, h / 2
  219. local radarGreen = 0x00FF00FF
  220. local backgroundColor = 0x000000BB
  221. local labelBackground = 0x222222BB
  222. local inactiveTargetColor = 0x444444CC
  223. local aspectRatio = 1 -- adjust if needed for elliptical compensation
  224. local radarScale = 500
  225.  
  226. local colorTable = {
  227. red = 0xFF0000FF,
  228. green = 0x00FF00FF,
  229. blue = 0x0000FFFF,
  230. yellow = 0xFFFF00FF,
  231. purple = 0x8000FFFF,
  232. orange = 0xFFA500FF,
  233. cyan = 0x00FFFFFF,
  234. white = 0xFFFFFFFF,
  235. gray = 0x808080FF,
  236. pink = 0xFFC0CBFF,
  237. lime = 0xBFFF00FF
  238. }
  239.  
  240. local colorKeys = {}
  241. for name in pairs(colorTable) do
  242. table.insert(colorKeys, name)
  243. end
  244.  
  245.  
  246. -- Setup screen
  247. holo.Resize(w, h)
  248. holo.SetClearColor(backgroundColor)
  249. holo.Clear()
  250. holo.SetScale(0.025, 0.025)
  251. holo.SetRotation(0, 0, 0)
  252. holo.SetTranslation(0, 0.125, 0)
  253.  
  254. local function drawControls()
  255. local buttonPadding = 4 -- Padding around text
  256.  
  257. -- Clear bottom section for controls
  258. --holo.Fill(0, h - 50, w, 50, backgroundColor)
  259.  
  260. -- Helper to draw a "button"
  261. local function drawButton(x, y, label)
  262. local textWidth = #label * 8
  263. local textHeight = 10
  264. local bgX = x - buttonPadding
  265. local bgY = y + 2
  266. local bgW = textWidth + buttonPadding * 2
  267. local bgH = textHeight + 4
  268.  
  269. holo.Fill(bgX, bgY, bgW, bgH, labelBackground)
  270. holo.Text(x, y, label, radarGreen, 0)
  271. end
  272.  
  273. -- Draw scale adjustment
  274. drawButton(5, h - 45, "Scale:")
  275. drawButton(70, h - 45, "-")
  276. drawButton(95, h - 45, "+")
  277.  
  278. -- Stop locking and launch buttons
  279. drawButton(5, h - 70, "Stop Locking")
  280. drawButton(5, h - 95, "Launch Missile")
  281.  
  282. -- Segment length display
  283. local segmentLength = radarScale / 2
  284. local scaleText
  285. if segmentLength >= 1000 then
  286. scaleText = string.format("Segment Length: %.1f km", segmentLength / 1000)
  287. else
  288. scaleText = string.format("Segment Length: %d m", segmentLength)
  289. end
  290. holo.Text(5, h - 20, scaleText, radarGreen, 0)
  291.  
  292. -- Waypoints button (bottom-right)
  293. drawButton(w - 100, h - 45, "Waypoints")
  294.  
  295. -- Adjust Cannon button (top-right)
  296. drawButton(w - 130, 5, "Adjust Cannon")
  297. end
  298.  
  299. local function drawCannonAdjustmentPage()
  300. local w, h = monitor.getSize()
  301. monitor.clear()
  302. monitor.setCursorPos(1, 1)
  303. monitor.write("Cannon Pitch and Yaw Adjustment")
  304.  
  305. local cannonNames = {
  306. {name = "Port 5 inch", data = cannonData.port5inch, target = cannonTarget.port5inch},
  307. {name = "Starboard 5 inch", data = cannonData.starboard5inch, target = cannonTarget.starboard5inch},
  308. {name = "Bow 15 inch", data = cannonData.bow15inch, target = cannonTarget.bow15inch},
  309. {name = "Stern 15 inch", data = cannonData.stern15inch, target = cannonTarget.stern15inch}
  310. }
  311.  
  312. local yOffset = 3
  313.  
  314. for _, cannon in ipairs(cannonNames) do
  315. monitor.setCursorPos(1, yOffset)
  316. monitor.write(cannon.name .. ":")
  317.  
  318. monitor.setCursorPos(1, yOffset + 1)
  319. monitor.write("Current: pitch: " .. (cannon.data.pitch and string.format("%.2f", cannon.data.pitch) or "N/A"))
  320. monitor.setCursorPos(26, yOffset + 1)
  321. monitor.write("yaw: " .. (cannon.data.yaw and string.format("%.2f", cannon.data.yaw) or "N/A"))
  322.  
  323. monitor.setCursorPos(1, yOffset + 2)
  324. monitor.write("Adjustment: pitch: " .. (cannon.target.pitchAdjust and string.format("%.2f", cannon.target.pitchAdjust) or "N/A"))
  325. monitor.setCursorPos(26, yOffset + 2)
  326. monitor.write("yaw: " .. (cannon.target.yawAdjust and string.format("%.2f", cannon.target.yawAdjust) or "N/A"))
  327.  
  328. monitor.setCursorPos(1, yOffset + 3)
  329. monitor.write("[pitch +] [pitch -]")
  330. monitor.setCursorPos(w - 30, yOffset + 3)
  331. monitor.write("[yaw +] [yaw -]")
  332.  
  333. yOffset = yOffset + 6 -- Move down for the next cannon
  334. end
  335.  
  336. monitor.setCursorPos(1, h)
  337. monitor.write("Back")
  338. end
  339.  
  340. local function drawRadarBackground(radius, shipYawRadians, radarScale)
  341. local segmentLength = radarScale / 4
  342. local pixelPerSegmentX = w / 4
  343. local pixelPerSegmentY = h / 4
  344. -- Grid lines: vertical
  345. local x = 0
  346. while x < w do
  347. if x ~= centerX then
  348. for y = 0, h - 1, 5 do
  349. holo.Fill(x, y, 1, 1, radarGreen)
  350. end
  351. end
  352. x = x + pixelPerSegmentX
  353. end
  354.  
  355. -- Grid lines: horizontal
  356. local y = 0
  357. while y < h do
  358. for x2 = 0, w - 1, 5 do
  359. holo.Fill(x2, y, 1, 1, radarGreen)
  360. end
  361. y = y + pixelPerSegmentY
  362. end
  363.  
  364. -- Circle ring: precompute radian steps
  365. for i = 0, 360, 10 do
  366. local rad = math.rad(i)
  367. local sinVal = math.sin(rad)
  368. local cosVal = math.cos(rad)
  369. local px = math.floor(centerX + (radius * sinVal) * aspectRatio)
  370. local py = math.floor(centerY + radius * cosVal)
  371. holo.Fill(px, py, 1, 1, radarGreen)
  372. end
  373.  
  374. -- Crosshair vertical
  375. for y = centerY - radius, centerY + radius, 5 do
  376. holo.Fill(centerX, y, 1, 1, radarGreen)
  377. end
  378.  
  379. -- Bearing text
  380. local trueBearing = (math.deg(shipYawRadians) + 360) % 360
  381. local bearingText = string.format("%03d°", trueBearing)
  382. local bearingX = centerX - (#bearingText * 4)
  383. local bearingY = centerY - radius + 30
  384. holo.Text(bearingX, bearingY, bearingText, radarGreen, 0)
  385.  
  386. -- Cardinal directions (precomputed)
  387. local directions = { "N", "W", "S", "E" }
  388. local angles = { 0, 90, 180, 270 }
  389. for i = 1, 4 do
  390. local rad = math.rad(angles[i] + math.deg(shipYawRadians))
  391. local px = math.floor(centerX + (radius * math.sin(rad)) * aspectRatio)
  392. local py = math.floor(centerY + radius * math.cos(rad))
  393. holo.Text(px, py, directions[i], radarGreen, 0)
  394. end
  395.  
  396. -- Segment label
  397. --holo.Text(10, h - 40, "Segment: " .. math.floor(segmentLength) .. "m", radarGreen, 0)
  398. end
  399.  
  400. local function drawTargetOnCircle(cx, cy, r, x, y)
  401. local dx, dy = (x - cx) / aspectRatio, y - cy
  402. local dist = math.sqrt(dx * dx + dy * dy)
  403. if dist > r then
  404. local scale = r / dist
  405. return cx + dx * scale * aspectRatio, cy + dy * scale
  406. else
  407. return x, y
  408. end
  409. end
  410.  
  411. local function calculateRelativeAngle(objectAngle, shipYawRadians)
  412. local relativeAngle = objectAngle + shipYawRadians - math.pi
  413. if relativeAngle < -math.pi then
  414. relativeAngle = relativeAngle + 2 * math.pi
  415. elseif relativeAngle >= math.pi then
  416. relativeAngle = relativeAngle - 2 * math.pi
  417. end
  418. return relativeAngle
  419. end
  420.  
  421. -- Store last seen object positions and timestamps
  422. local objectHistory = {}
  423.  
  424. local function isActiveObject(id, pos)
  425. local now = os.epoch("utc")
  426.  
  427. -- Get previous data
  428. local record = objectHistory[id]
  429. if record then
  430. local dx = pos.x - record.x
  431. local dy = pos.y - record.y
  432. local dz = pos.z - record.z
  433. local moved = math.sqrt(dx^2 + dy^2 + dz^2) > 0.5 -- consider >0.5 blocks moved
  434. local age = now - record.time
  435. if age <= 30000 and moved then
  436. return true
  437. end
  438. end
  439.  
  440. -- Save current position and timestamp
  441. objectHistory[id] = {x = pos.x, y = pos.y, z = pos.z, time = now}
  442. return false
  443. end
  444.  
  445. local function bakeBitmap(bitmap, color)
  446. local data = {}
  447. for i = 0, #bitmap - 1 do
  448. data[i] = bitmap[i + 1] == 1 and color or 0x00000000
  449. end
  450. return data
  451. end
  452.  
  453. local ICONS = {
  454. ["missile"] = {
  455. width = 4, height = 14,
  456. bitmap = {
  457. 0,1,1,0,
  458. 0,1,1,0,
  459. 0,1,1,0,
  460. 1,1,1,1,
  461. 1,1,1,1,
  462. 0,1,1,0,
  463. 0,1,1,0,
  464. 0,1,1,0,
  465. 0,1,1,0,
  466. 0,1,1,0,
  467. 0,1,1,0,
  468. 1,1,1,1,
  469. 1,1,1,1,
  470. 0,1,1,0,
  471. }
  472. },
  473. ["plane"] = {
  474. width = 15, height = 15,
  475. bitmap = {
  476. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  477. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  478. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  479. 0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
  480. 0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
  481. 0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
  482. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  483. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
  484. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  485. 1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,
  486. 0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
  487. 0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,
  488. 0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,
  489. 0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,
  490. 0,0,0,0,1,1,0,0,0,1,1,0,0,0,0
  491. }
  492. },
  493. ["tank"] = {
  494. width = 11, height = 16,
  495. bitmap = {
  496. 0,0,0,0,0,1,0,0,0,0,0,
  497. 0,0,0,0,0,1,0,0,0,0,0,
  498. 1,1,0,0,0,1,0,0,0,1,1,
  499. 1,1,0,0,0,1,0,0,0,1,1,
  500. 1,1,0,0,0,1,0,0,0,1,1,
  501. 1,1,0,1,1,1,1,1,0,1,1,
  502. 1,1,0,1,1,1,1,1,0,1,1,
  503. 1,1,0,1,1,1,1,1,0,1,1,
  504. 1,1,0,1,1,1,1,1,0,1,1,
  505. 1,1,0,1,1,1,1,1,0,1,1,
  506. 1,1,0,1,1,1,1,1,0,1,1,
  507. 1,1,0,1,1,1,1,1,0,1,1,
  508. 1,1,0,1,1,1,1,1,0,1,1,
  509. 1,1,0,0,0,0,0,0,0,1,1,
  510. 1,1,1,1,1,1,1,1,1,1,1,
  511. 1,1,1,1,1,1,1,1,1,1,1,
  512. }
  513. },
  514. ["ship"] = {
  515. width = 15, height = 15,
  516. bitmap = {
  517. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  518. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  519. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  520. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  521. 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  522. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  523. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  524. 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  525. 0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,
  526. 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,
  527. 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,
  528. 0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,
  529. 0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,
  530. 0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,
  531. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  532. }
  533. },
  534. ["default"] = {
  535. width = 5, height = 5,
  536. bitmap = {
  537. 0,0,1,0,0,
  538. 0,1,1,1,0,
  539. 1,1,1,1,1,
  540. 0,1,1,1,0,
  541. }
  542. },
  543. ["waypoint"] ={
  544. width = 7, height = 7,
  545. bitmap = {
  546. 1,1,1,1,1,1,1,
  547. 1,1,0,0,0,1,1,
  548. 1,1,0,0,0,1,1,
  549. 1,1,0,0,0,1,1,
  550. 0,1,1,0,1,1,0,
  551. 0,0,1,1,1,0,0,
  552. 0,0,0,1,0,0,0,
  553. }
  554. }
  555. }
  556.  
  557. local function blitIcon(x, y, icon, color)
  558. local iconData = ICONS[icon] or ICONS["default"]
  559. local pixels = bakeBitmap(iconData.bitmap, color)
  560. holo.Blit(math.floor(x - iconData.width / 2), math.floor(y - iconData.height / 2),
  561. iconData.width, iconData.height, pixels, 1)
  562. end
  563.  
  564. local function getTargetIcon(object)
  565. if not object.size or not object.scale then return "default" end
  566.  
  567. -- Scale the dimensions
  568. local sx = (object.size.x or 0) --/ object.scale.x
  569. local sy = (object.size.y or 0) --/ object.scale.y
  570. local sz = (object.size.z or 0) --/ object.scale.z
  571.  
  572. -- Volume calculation
  573. if object.volume == 0 then return "default" end -- Avoid division by zero
  574.  
  575. -- Density calculation
  576. local density = object.mass / object.volume
  577. object._calculatedDensity = density -- Optional: store for debugging or later use
  578.  
  579. -- Icon classification based on size and mass (can now be extended to use density)
  580. local sizeSum = sx + sy + sz
  581. if object.scale.x < 0.6 and object.mass > 5000 and object.mass < 20000 then
  582. return "missile"
  583. elseif sizeSum < 50 and object.mass < 200000 then
  584. if density > 100 then
  585. return "tank"
  586. elseif density > 50 then
  587. return "default"
  588. else
  589. return "plane"
  590. end
  591. elseif sizeSum < 50 and object.mass >= 200000 then
  592. return "tank"
  593. elseif sizeSum > 100 and object.mass > 500000 then
  594. return "ship"
  595. end
  596.  
  597. return "default"
  598. end
  599.  
  600. local function clusterShips(ships)
  601. local clusters = {}
  602.  
  603. for _, ship in ipairs(ships) do
  604. local added = false
  605. for _, cluster in ipairs(clusters) do
  606. for _, other in ipairs(cluster.members) do
  607. if distance(ship.pos, other.pos) < 10 then
  608. table.insert(cluster.members, ship)
  609.  
  610. -- Update main if more massive
  611. if (ship.mass or 0) > (cluster.main.mass or 0) and ship.scale.x == 1 then
  612. cluster.main = ship
  613. end
  614.  
  615. -- Update total mass and volume
  616. cluster.massSum = cluster.massSum + (ship.mass or 0)
  617. local sx = (ship.size and ship.size.x or 0)
  618. local sy = (ship.size and ship.size.y or 0)
  619. local sz = (ship.size and ship.size.z or 0)
  620. cluster.volumeSum = cluster.volumeSum + (sx * sy * sz)
  621.  
  622. added = true
  623. break
  624. end
  625. end
  626. if added then break end
  627. end
  628.  
  629. if not added then
  630. local sx = (ship.size and ship.size.x or 0)
  631. local sy = (ship.size and ship.size.y or 0)
  632. local sz = (ship.size and ship.size.z or 0)
  633. local volume = sx * sy * sz
  634.  
  635. table.insert(clusters, {
  636. members = { ship },
  637. main = ship,
  638. massSum = ship.mass or 0,
  639. volumeSum = volume,
  640. })
  641. end
  642. end
  643.  
  644. -- Return only main ship data
  645. local result = {}
  646. for _, cluster in ipairs(clusters) do
  647. local main = cluster.main
  648.  
  649. table.insert(result, {
  650. pos = main.pos,
  651. id = main.id,
  652. mass = main.mass,
  653. volume = cluster.volumeSum,
  654. velocity = main.velocity,
  655. scale = main.scale,
  656. size = main.size,
  657. type = "ship",
  658. color = radarGreen,
  659. })
  660. end
  661.  
  662. return result
  663. end
  664.  
  665.  
  666. local function drawBox(x, y, size, color)
  667. holo.DrawLine(x - size, y - size, x + size, y - size, color,0)
  668. holo.DrawLine(x + size, y - size, x + size, y + size, color,0)
  669. holo.DrawLine(x + size, y + size, x - size, y + size, color,0)
  670. holo.DrawLine(x - size, y + size, x - size, y - size, color,0)
  671. end
  672.  
  673. local function drawRadarDisplay()
  674. local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  675. local radius = math.min(centerX, centerY) - 1
  676.  
  677. local matrix = ship.getTransformationMatrix()
  678. local shipYawRadians = math.atan2(-matrix[1][3], matrix[3][3])
  679. if shipYawRadians < 0 then
  680. shipYawRadians = shipYawRadians + 2 * math.pi
  681. end
  682.  
  683. local pos,rawShips,clusters,allEntities
  684. parallel.waitForAll(
  685. function()
  686. pos = ship.getWorldspacePosition()
  687. rawShips = radar.scanForShips(10000)
  688. clusters = clusterShips(rawShips)
  689. end,
  690. function()
  691. allEntities = radar.scanForEntities(2000)
  692. end,
  693. function()
  694. drawRadarBackground(radius, shipYawRadians, radarScale)
  695. end
  696. )
  697.  
  698. targetPositions = {}
  699.  
  700. parallel.waitForAll(
  701. function()
  702. for _, object in ipairs(clusters) do
  703. local x = object.pos.x - pos.x
  704. local z = object.pos.z - pos.z
  705. local distance = math.sqrt(x^2 + z^2)
  706. local objectAngle = math.atan2(x, z)
  707. local relativeAngle = calculateRelativeAngle(objectAngle, shipYawRadians)
  708.  
  709. local scaledDistance = (distance / radarScale) * radius
  710. local relX = centerX + (scaledDistance * math.sin(relativeAngle))
  711. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  712.  
  713. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY)
  714.  
  715. if object.mass > 2000 and distance > 5 then
  716. local isActive = isActiveObject(object.id, object.pos)
  717.  
  718. -- Check for nearby wheel entities to refine tank detection
  719. local wheelCount = 0
  720. for _, entity in ipairs(allEntities) do
  721. --print(entity.entity_type)
  722. --print(textutils.serialize(entity.pos))
  723. if entity.entity_type == "entity.trackwork.wheel_entity" then
  724. local dx = entity.pos[1] - object.pos.x
  725. local dy = entity.pos[2] - object.pos.y
  726. local dz = entity.pos[3] - object.pos.z
  727. if (dx * dx + dy * dy + dz * dz) <= 100 then -- within 10 blocks
  728. wheelCount = wheelCount + 1
  729. if wheelCount >= 4 then break end
  730. end
  731. end
  732. end
  733.  
  734. -- Override icon type if tank confirmed by wheels
  735.  
  736.  
  737. -- Set color
  738. local color = 0xFFFF0080
  739. if checkFriendly(object.id) then
  740. color = isActive and 0x4444FFFF or 0x4444FFAA
  741. elseif object.mass > 1000000 then
  742. color = isActive and 0xFFA500FF or inactiveTargetColor
  743. elseif math.sqrt(object.velocity.x^2 + object.velocity.y^2 + object.velocity.z^2) > 30 then
  744. color = isActive and 0xFF0000FF or inactiveTargetColor
  745. else
  746. color = isActive and 0xFFFF00FF or inactiveTargetColor
  747. end
  748.  
  749. -- Determine icon
  750. local icon
  751. if wheelCount >= 4 then
  752. icon = "tank"
  753. else
  754. icon = getTargetIcon(object)
  755. end
  756.  
  757. -- Draw icon
  758. blitIcon(relX, relY, icon, color)
  759.  
  760. -- Vertical arrow indicator
  761. local dy = object.pos.y - pos.y
  762. if dy >= 15 then
  763. holo.Text(math.floor(relX - 4), math.floor(relY) - 12, "^", color, 0)
  764. elseif dy <= -15 then
  765. holo.Text(math.floor(relX - 4), math.floor(relY) + 8, "v", color, 0)
  766. end
  767.  
  768. -- Draw velocity arrow
  769. local vx, vz = object.velocity.x or 0, object.velocity.z or 0
  770. local vMag = math.sqrt(vx^2 + vz^2)
  771. if vMag > 1 then
  772. local vAngle = math.atan2(vx, vz)
  773. local vRelAngle = calculateRelativeAngle(vAngle, shipYawRadians)
  774.  
  775. local arrowLength = math.min(100, vMag * 10)
  776. local arrowX = relX + arrowLength * math.sin(vRelAngle)
  777. local arrowY = relY + arrowLength * math.cos(vRelAngle)
  778.  
  779. holo.DrawLine(math.floor(relX), math.floor(relY), math.floor(arrowX), math.floor(arrowY), color)
  780. end
  781.  
  782. table.insert(targetPositions, {
  783. x = math.floor(relX),
  784. y = math.floor(relY),
  785. data = object,
  786. type = "ship",
  787. targetIcon = icon
  788. })
  789. end
  790. end
  791. end,
  792. function()
  793. -- Draw waypoints
  794. for index, waypoint in ipairs(waypoints) do
  795. local wpX = waypoint.x - pos.x
  796. local wpZ = waypoint.z - pos.z
  797. local distance = math.sqrt(wpX^2 + wpZ^2)
  798. local wpAngle = math.atan2(wpX, wpZ)
  799. local relativeAngle = calculateRelativeAngle(wpAngle, shipYawRadians)
  800.  
  801. local scaledDistance = (distance / radarScale) * radius
  802. local relX = centerX + (scaledDistance * math.sin(relativeAngle))
  803. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  804.  
  805. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY)
  806.  
  807. local wpColor = waypoint.color or 0xFFFF00FF
  808. blitIcon(math.floor(relX), math.floor(relY),"waypoint",wpColor)
  809.  
  810. table.insert(targetPositions, {
  811. x = math.floor(relX),
  812. y = math.floor(relY),
  813. data = {
  814. type = "waypoint",
  815. x = waypoint.x,
  816. y = waypoint.y,
  817. z = waypoint.z,
  818. index = index
  819. }
  820. })
  821. end
  822. end,
  823. function()
  824. -- Highlight cannon locked targets
  825. for _, lock in pairs(cannonTarget) do
  826. if lock and lock.id then
  827. for _, t in ipairs(targetPositions) do
  828. if t.data.id == lock.id then
  829. drawBox(t.x-1, t.y, 8, 0xFF0000FF) -- Red box
  830. holo.Text(t.x, t.y+5, "artillery", 0xFF0000FF, 0)
  831. end
  832. end
  833. elseif lock and lock.type == "waypoint" then
  834. -- Waypoint-based cannon lock
  835. for _, t in ipairs(targetPositions) do
  836. if t.data.type == "waypoint" and
  837. t.data.x == lock.x and t.data.y == lock.y and t.data.z == lock.z then
  838. drawBox(t.x-1, t.y, 8, 0xFF0000FF) -- Red box
  839. holo.Text(t.x, t.y+5, "artillery", 0xFF0000FF, 0)
  840. end
  841. end
  842. end
  843. end
  844.  
  845. -- Highlight pending missile targets
  846. for _, entry in ipairs(pendingMissileLaunches) do
  847. local tgt = entry.target
  848. for _, t in ipairs(targetPositions) do
  849. if tgt.id and t.data.id == tgt.id then
  850. drawBox(t.x-1, t.y, 10, 0xFFFF00FF) -- Yellow box
  851. holo.Text(t.x,t.y+5,entry.type,0xFFFF00FF,0)
  852. elseif tgt.type == "waypoint" and
  853. tgt.x == t.data.x and tgt.y == t.data.y and tgt.z == t.data.z then
  854. drawBox(t.x-1, t.y, 10, 0xFFFF00FF) -- Yellow box
  855. holo.Text(t.x,t.y+5,entry.type,0xFFFF00FF,0)
  856. end
  857. end
  858. end
  859. end
  860. )
  861. end
  862.  
  863. function drawWaypointPage()
  864. local white = 0xFFFFFFFF
  865. local background = 0x000000FF
  866. local gray = 0x404040FF
  867. local titleY = 50
  868. local instructionY = 100
  869. local inputY = 300
  870.  
  871. holo.Clear()
  872. holo.SetClearColor(background)
  873.  
  874. -- Instruction Text
  875. holo.Text(10, titleY, "Waypoint Page", white, 0)
  876. holo.Text(10, instructionY, "Enter coordinates (x, y, z, color):", white, 0)
  877. holo.Text(10, instructionY + 20, "Format: x,y,z,color", white, 0)
  878. holo.Text(10, instructionY + 40, "Colors: red, yellow, green, blue, purple", white, 0)
  879. holo.Text(10, instructionY + 60, "Press Enter to submit. Backspace to edit.", white, 0)
  880. holo.Flush()
  881. local inputStr = ""
  882.  
  883. local numPadMap = {
  884. [320] = "0", [321] = "1", [322] = "2", [323] = "3",
  885. [324] = "4", [325] = "5", [326] = "6",
  886. [327] = "7", [328] = "8", [329] = "9",
  887. [330] = ".", [331] = "-", [334] = "+"
  888. }
  889.  
  890. local function drawInput()
  891. holo.Fill(10, 300, 1000, 20, 0x000000FF)
  892. holo.Text(10, 300, inputStr .. "_", 0xFFFFFFFF, 0)
  893. holo.Flush()
  894. end
  895.  
  896. drawInput()
  897.  
  898. while true do
  899. local event, screenName, keyCode = os.pullEvent("vp_key_pressed")
  900. if screenName ~= holo.GetName() then goto continue end
  901.  
  902. local keyName = keys.getName and keys.getName(keyCode) or nil
  903.  
  904. if keyName == "enter" then
  905. local x, y, z, color = inputStr:match("^%s*([^,]+)%s*,%s*([^,]+)%s*,%s*([^,]+)%s*,?%s*([^,]*)%s*$")
  906.  
  907. if x and y and z then
  908. local colorKey = string.lower(color or "")
  909. local colorCode = colorTable[colorKey]
  910.  
  911. if not color or color == "" or not colorCode then
  912. local randKey = colorKeys[math.random(1, #colorKeys)]
  913. colorCode = colorTable[randKey]
  914. print("Assigning random color: " .. tostring(colorCode))
  915. end
  916.  
  917. addWaypoint(tonumber(x), tonumber(y), tonumber(z), colorCode)
  918. else
  919. print("Invalid format. Expected: x,y,z[,color]")
  920. end
  921.  
  922. displayState = "radar"
  923. drawControls()
  924. drawRadarDisplay()
  925. break
  926. elseif keyName == "backspace" then
  927. inputStr = inputStr:sub(1, -2)
  928. drawInput()
  929.  
  930. elseif keyCode >= 32 and keyCode <= 126 then
  931. inputStr = inputStr .. string.char(keyCode)
  932. drawInput()
  933.  
  934. elseif numPadMap[keyCode] then
  935. inputStr = inputStr .. numPadMap[keyCode]
  936. drawInput()
  937. end
  938. ::continue::
  939. end
  940. end
  941.  
  942. targetInfoButtons = {}
  943.  
  944. -- Utility function used in onClick for gun locking
  945. function lockCannon(key, target)
  946. if target.type == "waypoint" then
  947. cannonTarget[key] = {
  948. type = "waypoint",
  949. x = target.x, y = target.y, z = target.z, id = nil
  950. }
  951. else
  952. cannonTarget[key] = {
  953. type = "target",
  954. id = target.id, x = nil, y = nil, z = nil
  955. }
  956. end
  957. displayState = "radar"
  958. drawRadarDisplay()
  959. end
  960.  
  961. function displayTargetInfo(target, iconType)
  962. local padding = 4
  963. local offsetY = -150
  964. local pos = ship.getWorldspacePosition()
  965.  
  966. holo.Clear()
  967. targetInfoButtons = {}
  968.  
  969. local y = h - 250 + offsetY
  970. local x = 10
  971. local lines = {}
  972.  
  973. table.insert(lines, "Target Info")
  974.  
  975. local targetPosX, targetPosY, targetPosZ
  976. if target.type == "waypoint" then
  977. table.insert(lines, "Type: Waypoint")
  978. targetPosX, targetPosY, targetPosZ = target.x, target.y, target.z
  979. table.insert(lines, "Coordinate: " .. target.x .. ", " .. target.y .. ", " .. target.z)
  980. else
  981. table.insert(lines, "ID: " .. target.id)
  982. targetPosX, targetPosY, targetPosZ = target.pos.x, target.pos.y, target.pos.z
  983. table.insert(lines, "Mass: " .. target.mass)
  984.  
  985. -- Compute volume & density
  986. local sx = (target.size and target.size.x or 0) * (target.scale and target.scale.x or 1)
  987. local sy = (target.size and target.size.y or 0) * (target.scale and target.scale.y or 1)
  988. local sz = (target.size and target.size.z or 0) * (target.scale and target.scale.z or 1)
  989. local volume = sx * sy * sz
  990. local density = volume > 0 and (target.mass or 0) / volume or 0
  991.  
  992. table.insert(lines, string.format("Velocity: %.2f, %.2f, %.2f", target.velocity.x, target.velocity.y, target.velocity.z))
  993. table.insert(lines, string.format("Position: %.2f, %.2f, %.2f", target.pos.x, target.pos.y, target.pos.z))
  994. table.insert(lines, string.format("Size: %.2f × %.2f × %.2f", sx, sy, sz))
  995. table.insert(lines, string.format("Density: %.2f", density))
  996. end
  997.  
  998. local dx = targetPosX - pos.x
  999. local dz = targetPosZ - pos.z
  1000. local distance = math.sqrt(dx ^ 2 + dz ^ 2)
  1001. local bearing = (math.deg(math.atan2(dz, dx)) + 360) % 360 - 270
  1002. if bearing < 0 then bearing = bearing + 360 end
  1003.  
  1004. table.insert(lines, string.format("Distance: %.2f meters", distance))
  1005. table.insert(lines, string.format("Bearing: %.2f°", bearing))
  1006. table.insert(lines, "Target type: " .. tostring(iconType))
  1007.  
  1008. -- Draw info
  1009. for _, text in ipairs(lines) do
  1010. holo.Text(x, y, text, radarGreen, 0)
  1011. y = y + 20
  1012. end
  1013. sleep(0.1)
  1014. -- Button helper
  1015. local function makeButton(label, x, y, onClick)
  1016. local w = #label * 8 + padding * 2
  1017. local h = 16
  1018. table.insert(targetInfoButtons, {
  1019. label = label, x = x, y = y, w = w, h = h, onClick = onClick
  1020. })
  1021. holo.Fill(x - padding, y + 2, w, h, 0x222222BB)
  1022. holo.Text(x, y, label, radarGreen, 0)
  1023. end
  1024.  
  1025. -- Gun buttons
  1026. local gunX, gunY = 10, y + 10
  1027. makeButton("Port gun", gunX, gunY, function() lockCannon("port5inch", target) end)
  1028. makeButton("Starboard gun", gunX, gunY + 24, function() lockCannon("starboard5inch", target) end)
  1029. makeButton("Bow gun", gunX, gunY + 48, function() lockCannon("bow15inch", target) end)
  1030. makeButton("Stern gun", gunX, gunY + 72, function() lockCannon("stern15inch", target) end)
  1031.  
  1032. -- Missile buttons
  1033. local missileX, missileY = 300, gunY
  1034. makeButton("AIM-220 (Available: " .. AIMCount .. ")", missileX, missileY, function()
  1035. table.insert(pendingMissileLaunches, { type = "AIM-220", target = target })
  1036. displayState = "radar"
  1037. drawRadarDisplay()
  1038. end)
  1039. makeButton("GBU-42 (Available: " .. GBUCount .. ")", missileX, missileY + 24, function()
  1040. table.insert(pendingMissileLaunches, { type = "GBU-42", target = target })
  1041. displayState = "radar"
  1042. drawRadarDisplay()
  1043. end)
  1044. makeButton("Thunderbolt (Available: " .. thunderboltCount .. ")", missileX, missileY + 48, function()
  1045. table.insert(pendingMissileLaunches, { type = "thunderBolt", target = target })
  1046. displayState = "radar"
  1047. drawRadarDisplay()
  1048. end)
  1049.  
  1050. -- Friend Marking Buttons
  1051. local friendX, friendY = 10, missileY + 100
  1052.  
  1053. makeButton("Mark as Friendly", friendX, friendY, function()
  1054. if target.id then
  1055. local alreadyExists = false
  1056. for _, id in ipairs(friendlyID) do
  1057. if id == target.id then
  1058. alreadyExists = true
  1059. break
  1060. end
  1061. end
  1062. if not alreadyExists then
  1063. table.insert(friendlyID, target.id)
  1064. displayState = "radar"
  1065. drawRadarDisplay()
  1066. end
  1067. end
  1068. end)
  1069.  
  1070. makeButton("Unmark Friendly", friendX, friendY + 24, function()
  1071. if target.id then
  1072. for i = #friendlyID, 1, -1 do
  1073. if friendlyID[i] == target.id then
  1074. table.remove(friendlyID, i)
  1075. displayState = "radar"
  1076. drawRadarDisplay()
  1077. break
  1078. end
  1079. end
  1080. end
  1081. end)
  1082.  
  1083. -- Back button
  1084. makeButton("Back", 10, h - 30, function()
  1085. displayState = "radar"
  1086. drawRadarDisplay()
  1087. end)
  1088.  
  1089. holo.Flush()
  1090. end
  1091.  
  1092. --===================--
  1093. --Aim cannon function--
  1094. --===================--
  1095.  
  1096. local function aimCannon(targetPos, targetVel, side)
  1097. if targetPos and targetVel then
  1098. -- Determine which channel to use based on side
  1099. local channel
  1100. if side == "port" then
  1101. channel = port5inchChannel
  1102. elseif side == "starboard" then
  1103. channel = starboard5inchChannel
  1104. elseif side == "bow" then
  1105. channel = bow15inchChannel
  1106. elseif side == "stern" then
  1107. channel = stern15inchChannel
  1108. end
  1109. targetInfo = {targetPos = targetPos, targetVel = targetVel}
  1110. print("channel: "..channel)
  1111. print(textutils.serialize(targetInfo))
  1112. --yaw adjustment
  1113. if modem and targetInfo and targetInfo.targetPos and targetInfo.targetVel then
  1114. modem.transmit(channel,0,targetInfo)
  1115. end
  1116. end
  1117. end
  1118.  
  1119. local function aimPortCannonContinuous()
  1120. while true do
  1121. local cannon = cannonTarget.port5inch
  1122. if cannon.type then
  1123. local target
  1124. --monitor.setCursorPos(1,1)
  1125. if cannon.type=="waypoint" then
  1126. --monitor.write("Port waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  1127. else
  1128. --monitor.write("Port ship locked: "..cannon.id)
  1129. end
  1130. if cannon.type == "waypoint" then
  1131. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  1132. else -- handling target
  1133. local scanResults = radar.scanForShips(9999)
  1134. for _, object in ipairs(scanResults) do
  1135. if object.id == cannon.id then
  1136. target = object
  1137. break
  1138. end
  1139. end
  1140. end
  1141.  
  1142. if target and target.pos then
  1143. aimCannon(target.pos, target.velocity, "port")
  1144. end
  1145. else
  1146.  
  1147. end
  1148. sleep() -- Adjust sleep duration based on required reaction time
  1149. end
  1150. end
  1151.  
  1152. local function aimStarboardCannonContinuous()
  1153. while true do
  1154. local cannon = cannonTarget.starboard5inch
  1155. if cannon.type then
  1156. local target
  1157. --monitor.setCursorPos(1,2)
  1158. if cannon.type=="waypoint" then
  1159. --monitor.write("Starboard waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  1160. else
  1161. --monitor.write("Starboard ship locked: "..cannon.id)
  1162. end
  1163. if cannon.type == "waypoint" then
  1164. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  1165. else -- handling target
  1166. local scanResults = radar.scanForShips(9999)
  1167. for _, object in ipairs(scanResults) do
  1168. if object.id == cannon.id then
  1169. target = object
  1170. break
  1171. end
  1172. end
  1173. end
  1174.  
  1175. if target and target.pos then
  1176. aimCannon(target.pos, target.velocity, "starboard")
  1177. end
  1178. end
  1179. sleep()
  1180. end
  1181. end
  1182.  
  1183. local function aimBowCannonContinuous()
  1184. while true do
  1185. local cannon = cannonTarget.bow15inch
  1186. if cannon.type then
  1187. local target
  1188. --monitor.setCursorPos(1,3)
  1189. if cannon.type=="waypoint" then
  1190. --monitor.write("Bow waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  1191. else
  1192. --monitor.write("Bow ship locked: "..cannon.id)
  1193. end
  1194. if cannon.type == "waypoint" then
  1195. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  1196. else -- handling target
  1197. local scanResults = radar.scanForShips(9999)
  1198. for _, object in ipairs(scanResults) do
  1199. if object.id == cannon.id then
  1200. target = object
  1201. break
  1202. end
  1203. end
  1204. end
  1205.  
  1206. if target and target.pos then
  1207. aimCannon(target.pos, target.velocity, "bow")
  1208. end
  1209. end
  1210. sleep()
  1211. end
  1212. end
  1213.  
  1214. local function aimSternCannonContinuous()
  1215. while true do
  1216. local cannon = cannonTarget.stern15inch
  1217. if cannon.type then
  1218. local target
  1219. --monitor.setCursorPos(1,4)
  1220. if cannon.type=="waypoint" then
  1221. --monitor.write("Stern waypoint locked: "..cannon.x.." , "..cannon.y.." , "..cannon.z)
  1222. else
  1223. --monitor.write("Stern ship locked: "..cannon.id)
  1224. end
  1225. if cannon.type == "waypoint" then
  1226. target = {pos = {x = cannon.x, y = cannon.y, z = cannon.z}, velocity = {x = 0, y = 0, z = 0}}
  1227. else -- handling target
  1228. local scanResults = radar.scanForShips(9999)
  1229. for _, object in ipairs(scanResults) do
  1230. if object.id == cannon.id then
  1231. target = object
  1232. break
  1233. end
  1234. end
  1235. end
  1236.  
  1237. if target and target.pos then
  1238. aimCannon(target.pos, target.velocity,"stern")
  1239. end
  1240. end
  1241. sleep()
  1242. end
  1243. end
  1244.  
  1245. --=================--
  1246. --Missiles function--
  1247. --=================--
  1248. local function isMissileLaunchedById(id)
  1249. for _, launched in ipairs(LaunchedMissiles) do
  1250. if launched.id == id then
  1251. return true
  1252. end
  1253. end
  1254. return false
  1255. end
  1256.  
  1257.  
  1258. missileLaunchOffset = {x=0,y=30,z=0}
  1259. maxLaunchOffsetDistance = 5
  1260. local function launchMissile(targetType, targetId, targetPos, missileType)
  1261. local currentMissile = nil
  1262.  
  1263. -- Function to find and remove the first unlaunched missile from a list
  1264. local function findUnlaunchedMissile(missileList)
  1265. for i = #missileList, 1, -1 do -- Iterate in reverse to avoid index shifting issues
  1266. local missile = missileList[i]
  1267. if missile.launchState == false then
  1268. -- Check if this missile is already in LaunchedMissiles
  1269. print(textutils.serialize(LaunchedMissiles))
  1270. print(textutils.serialize(missile))
  1271.  
  1272. if not isMissileLaunchedById(missile.id) then
  1273. missile.launchState = true -- Mark missile as launched
  1274. table.insert(LaunchedMissiles, missile) -- Move to launched list
  1275. return table.remove(missileList, i) -- Remove from available list and return it
  1276. end
  1277. end
  1278. end
  1279. return nil
  1280. end
  1281.  
  1282. -- Get the first unlaunched missile of the specified type
  1283. if missileType == "AIM-220" then
  1284. currentMissile = findUnlaunchedMissile(AIMInfoList)
  1285. elseif missileType == "GBU-42" then
  1286. currentMissile = findUnlaunchedMissile(GBUInfoList)
  1287. elseif missileType == "thunderBolt" then
  1288. currentMissile = findUnlaunchedMissile(thunderBoltInfoList)
  1289. end
  1290.  
  1291. -- If no unlaunched missile is found, exit function
  1292. if not currentMissile then
  1293. print("No available unlaunched missile of type:", missileType)
  1294. return
  1295. end
  1296.  
  1297. local currentMissileId = currentMissile.id
  1298. print("Teleporting and launching missile " .. currentMissileId)
  1299.  
  1300. -- Assign the missile as launched and set target info
  1301. if targetType == "ship" then
  1302. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  1303. else
  1304. missileControls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  1305. end
  1306.  
  1307. -- Base launch position
  1308. local shipPos = ship.getWorldspacePosition()
  1309. local baseX = shipPos.x + missileLaunchOffset.x
  1310. local baseY = shipPos.y + missileLaunchOffset.y
  1311. local baseZ = shipPos.z + missileLaunchOffset.z
  1312.  
  1313. -- Randomize launch offset within a circle (XZ-plane)
  1314. local angle = math.random() * 2 * math.pi
  1315. local distance = math.random() * maxLaunchOffsetDistance
  1316. local offsetX = math.cos(angle) * distance
  1317. local offsetZ = math.sin(angle) * distance
  1318. local offsetY = math.random(-1, 1) -- Optional small vertical variation
  1319.  
  1320. local tx = baseX + offsetX
  1321. local ty = baseY + offsetY
  1322. local tz = baseZ + offsetZ
  1323.  
  1324. -- Aim calculation
  1325. local dx = targetPos.x - tx
  1326. local dy = targetPos.y - ty
  1327. local dz = targetPos.z - tz
  1328.  
  1329. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  1330. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  1331. local yaw = math.deg(math.atan2(dx, dz))
  1332.  
  1333. local teleportMessage = string.format(
  1334. "vs teleport %s %.2f %.2f %.2f (%.2f %.2f 0)",
  1335. currentMissile.slug, tx, ty, tz, -pitch, yaw
  1336. )
  1337.  
  1338. --print(teleportMessage)
  1339. print(commands.exec(teleportMessage))
  1340. commands.exec("/vs set-static "..currentMissile.slug.." false")
  1341. end
  1342.  
  1343. local function missileWarning()
  1344. local previousPositions = {}
  1345. while true do
  1346. local objectList = radar.scanForEntities(600)
  1347. -- Initial scan to capture positions
  1348. local initialPositions = {}
  1349. local shipPos = ship.getWorldspacePosition()
  1350. for _, object in ipairs(objectList) do
  1351. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  1352. if object.pos then
  1353. initialPositions[object.entity_type .. table.concat(object.pos, ":")] = object.pos
  1354. print("missile detected")
  1355. end
  1356. end
  1357. end
  1358.  
  1359. sleep(0.1) -- Short delay to measure displacement
  1360.  
  1361. objectList = radar.scanForEntities(600) -- Second scan to calculate speed
  1362. local currentPositions = {}
  1363. for _, object in ipairs(objectList) do
  1364. if object and (object.entity_type == "entity.tallyho.ir_missile" or object.entity_type == "entity.smallarm.at_rocket") then
  1365. if object.pos then
  1366. print("Missile detected for second time")
  1367. local key = object.entity_type .. table.concat(object.pos, ":")
  1368. currentPositions[key] = object.pos
  1369. local previousPos = initialPositions[key]
  1370. previousPos = nil
  1371. if previousPos then
  1372. local speed = calculateSpeed(previousPos, object.pos, 0.1)
  1373. print(textutils.serialize(previousPos))
  1374. print(textutils.serialize(object.pos))
  1375. print("speed: "..speed)
  1376. if speed > 5 then -- Check if speed exceeds threshold
  1377. local objX, objY, objZ = object.pos[1], object.pos[2], object.pos[3]
  1378. local missileVector = {x = objX - previousPos[1], y = objY - previousPos[2], z = objZ - previousPos[3]}
  1379. local toPlayerVector = {x = shipPos.x - objX, y = shipPos.y - objY, z = shipPos.z - objZ}
  1380. local dotProduct = missileVector.x * toPlayerVector.x + missileVector.y * toPlayerVector.y + missileVector.z * toPlayerVector.z
  1381. print("Missile with speed > 10 detected")
  1382. if dotProduct then -- Missile is moving towards the player
  1383. print("Missile coming for player")
  1384. local angle = math.deg(math.atan2(missileVector.z, missileVector.x) - math.atan2(toPlayerVector.z, toPlayerVector.x))
  1385. local direction = ((angle + 360) % 360) / 30
  1386. local clockDirection = math.floor(direction + 1)
  1387. if clockDirection == 0 then clockDirection = 12 end
  1388. monitor.setCursorPos(0,6)
  1389. monitor.write("Missile incoming from " .. clockDirection .. " o'clock")
  1390. if speaker then
  1391. speaker.playSound("entity.lightning_bolt.thunder", 1.0)
  1392. end
  1393. end
  1394. end
  1395. end
  1396. end
  1397. end
  1398. end
  1399.  
  1400. previousPositions = currentPositions
  1401. sleep() -- Delay before the next round of scanning
  1402. end
  1403. end
  1404.  
  1405. local function broadCastWaypoint()
  1406. while true do
  1407. modem.transmit(waypointChannel,0,waypoints)
  1408. sleep(1)
  1409. end
  1410. end
  1411.  
  1412. local function controlsSending()
  1413. while true do
  1414. --modem.transmit(missileControlChannel,missileControlChannel,missileControls)
  1415. sleep()
  1416. end
  1417. end
  1418.  
  1419. local function missileListBuilding()
  1420. while true do
  1421. if immediateMissileInfo and immediateMissileInfo.id and immediateMissileInfo.type then
  1422. local foundMissile = false
  1423.  
  1424. -- Update the existing missile state instead of adding duplicates
  1425. for _, missileInfo in ipairs(AIMInfoList) do
  1426. if missileInfo.id == immediateMissileInfo.id then
  1427. missileInfo.launchState = immediateMissileInfo.launchState
  1428. foundMissile = true
  1429. break
  1430. end
  1431. end
  1432. for _, missileInfo in ipairs(GBUInfoList) do
  1433. if missileInfo.id == immediateMissileInfo.id then
  1434. missileInfo.launchState = immediateMissileInfo.launchState
  1435. foundMissile = true
  1436. break
  1437. end
  1438. end
  1439. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1440. if missileInfo.id == immediateMissileInfo.id then
  1441. missileInfo.launchState = immediateMissileInfo.launchState
  1442. foundMissile = true
  1443. break
  1444. end
  1445. end
  1446.  
  1447. -- If it's a new missile, add it to the list
  1448. if not foundMissile then
  1449. if immediateMissileInfo.type == "AIM-220" then
  1450. table.insert(AIMInfoList, immediateMissileInfo)
  1451. elseif immediateMissileInfo.type == "GBU-42" then
  1452. table.insert(GBUInfoList, immediateMissileInfo)
  1453. elseif immediateMissileInfo.type == "thunderBolt" then
  1454. table.insert(thunderBoltInfoList, immediateMissileInfo)
  1455. end
  1456. --Add to friendly id list
  1457. local alreadyExists = false
  1458. for _, id in ipairs(friendlyID) do
  1459. if id == immediateMissileInfo.id then
  1460. alreadyExists = true
  1461. break
  1462. end
  1463. end
  1464. if not alreadyExists then
  1465. table.insert(friendlyID, immediateMissileInfo.id)
  1466. end
  1467. end
  1468. end
  1469.  
  1470. -- Remove missiles that have launched or are known by ID to have launched
  1471. for i = #AIMInfoList, 1, -1 do
  1472. local m = AIMInfoList[i]
  1473. if m.launchState == true or isMissileLaunchedById(m.id) then
  1474. table.remove(AIMInfoList, i)
  1475. end
  1476. end
  1477.  
  1478. for i = #GBUInfoList, 1, -1 do
  1479. local m = GBUInfoList[i]
  1480. if m.launchState == true or isMissileLaunchedById(m.id) then
  1481. table.remove(GBUInfoList, i)
  1482. end
  1483. end
  1484.  
  1485. for i = #thunderBoltInfoList, 1, -1 do
  1486. local m = thunderBoltInfoList[i]
  1487. if m.launchState == true or isMissileLaunchedById(m.id) then
  1488. table.remove(thunderBoltInfoList, i)
  1489. end
  1490. end
  1491.  
  1492.  
  1493. -- Count unlaunched missiles
  1494. AIMCount, GBUCount, thunderboltCount = 0, 0, 0
  1495. for _, missileInfo in ipairs(AIMInfoList) do
  1496. if not missileInfo.launchState then
  1497. AIMCount = AIMCount + 1
  1498. end
  1499. end
  1500. for _, missileInfo in ipairs(GBUInfoList) do
  1501. if not missileInfo.launchState then
  1502. GBUCount = GBUCount + 1
  1503. end
  1504. end
  1505. for _, missileInfo in ipairs(thunderBoltInfoList) do
  1506. if not missileInfo.launchState then
  1507. thunderboltCount = thunderboltCount + 1
  1508. end
  1509. end
  1510. sleep()
  1511. end
  1512. end
  1513.  
  1514. --==============--
  1515. --Touch handling--
  1516. --==============--
  1517. radarDisplayButtons = {
  1518. {
  1519. label = "Scale:",
  1520. x = 5, y = h - 45,
  1521. w = 50, h = 16,
  1522. onClick = function() end -- Label only, no action
  1523. },
  1524. {
  1525. label = "-",
  1526. x = 70, y = h - 45,
  1527. w = 20, h = 16,
  1528. onClick = function()
  1529. radarScale = math.max(100, radarScale - 100)
  1530. drawControls()
  1531. drawRadarDisplay()
  1532. end
  1533. },
  1534. {
  1535. label = "+",
  1536. x = 95, y = h - 45,
  1537. w = 20, h = 16,
  1538. onClick = function()
  1539. radarScale = math.min(10000, radarScale + 100)
  1540. drawControls()
  1541. drawRadarDisplay()
  1542. end
  1543. },
  1544. {
  1545. label = "Stop Locking",
  1546. x = 5, y = h - 70,
  1547. w = 110, h = 16,
  1548. onClick = function()
  1549. for _, key in pairs({"port5inch", "starboard5inch", "bow15inch", "stern15inch"}) do
  1550. cannonTarget[key] = { type = nil, side = key, id = nil, x = nil, y = nil, z = nil, pitchAdjust = 0, yawAdjust = 0 }
  1551. end
  1552. pendingMissileLaunches = {}
  1553. drawRadarDisplay()
  1554. end
  1555. },
  1556. {
  1557. label = "Launch Missile",
  1558. x = 5, y = h - 95,
  1559. w = 130, h = 16,
  1560. onClick = function()
  1561. for _, entry in ipairs(pendingMissileLaunches) do
  1562. local target = entry.target
  1563. if target.type == "waypoint" then
  1564. launchMissile("waypoint", nil, target, entry.type)
  1565. else
  1566. launchMissile("ship", target.id, target.pos, entry.type)
  1567. end
  1568. modem.transmit(missileControlChannel, missileControlChannel, missileControls)
  1569. sleep(0.5)
  1570. end
  1571. pendingMissileLaunches = {}
  1572. drawRadarDisplay()
  1573. end
  1574. },
  1575. {
  1576. label = "Waypoints",
  1577. x = w - 100, y = h - 45,
  1578. w = 90, h = 16,
  1579. onClick = function()
  1580. displayState = "waypoints"
  1581. drawWaypointPage()
  1582. end
  1583. },
  1584. --[[{
  1585. label = "Adjust Cannon",
  1586. x = w - 130, y = 5,
  1587. w = 120, h = 16,
  1588. onClick = function()
  1589. displayState = "adjustCannon"
  1590. drawCannonAdjustmentPage()
  1591. end
  1592. }]]
  1593. }
  1594.  
  1595. local function drawRadarDisplayControls()
  1596. local gray = 0x808080FF
  1597. local white = 0xFFFFFFFF
  1598. local buttonPadding = 4
  1599.  
  1600. holo.Fill(0, h - 100, w, 100, backgroundColor)
  1601.  
  1602. for _, btn in ipairs(radarDisplayButtons) do
  1603. local bgX = btn.x - buttonPadding
  1604. local bgY = btn.y + 2
  1605. local bgW = btn.w
  1606. local bgH = btn.h
  1607. holo.Fill(bgX, bgY, bgW, bgH, gray)
  1608. holo.Text(btn.x, btn.y, btn.label, white, 0)
  1609. end
  1610.  
  1611. local segmentLength = radarScale / 2
  1612. local scaleText
  1613. if segmentLength >= 1000 then
  1614. scaleText = string.format("Segment Length: %.1f km", segmentLength / 1000)
  1615. else
  1616. scaleText = string.format("Segment Length: %d m", segmentLength)
  1617. end
  1618. holo.Text(5, h - 20, scaleText, white, 0)
  1619. end
  1620.  
  1621. local function handleHologramClick()
  1622. while true do
  1623. local event, screenName, button, x, y = os.pullEvent("vp_mouse_clicked")
  1624.  
  1625. x, y = math.floor(x), math.floor(y)
  1626.  
  1627. if displayState == "radar" then
  1628. for _, btn in ipairs(radarDisplayButtons) do
  1629. local bx, by, bw, bh = btn.x, btn.y, btn.w, btn.h
  1630. if x >= bx and x <= bx + bw and y >= by and y <= by + bh then
  1631. if btn.onClick then btn.onClick() end
  1632. break
  1633. end
  1634. end
  1635.  
  1636. -- Check target clicks
  1637. for _, target in ipairs(targetPositions) do
  1638. if math.abs(x - target.x) <= 10 and math.abs(y - target.y) <= 10 then
  1639. --print(textutils.serialize(target))
  1640. --print(target.targetIcon)
  1641. selectedTarget = target.data
  1642. targetTypeIcon = target.targetIcon
  1643. displayState = "targetInfo"
  1644. displayTargetInfo(selectedTarget,targetTypeIcon)
  1645. break
  1646. end
  1647. end
  1648. elseif displayState == "targetInfo" then
  1649. for _, btn in ipairs(targetInfoButtons) do
  1650. if x >= btn.x and x <= btn.x + btn.w and y >= btn.y and y <= btn.y + btn.h then
  1651. if btn.onClick then btn.onClick() end
  1652. break
  1653. end
  1654. end
  1655. end
  1656.  
  1657. ::continue::
  1658. end
  1659. end
  1660.  
  1661. local function printOutput()
  1662. while true do
  1663. --print(textutils.serialize(missileControls))
  1664. --print(textutils.serialize(AIMInfoList))
  1665. --print(textutils.serialize(GBUInfoList))
  1666. --print(textutils.serialize(immediateMissileInfo))
  1667. --print(missileInfoChannel)
  1668. --print(textutils.serialize(thunderBoltInfoList))
  1669. sleep()
  1670. end
  1671. end
  1672.  
  1673. parallel.waitForAny(
  1674. function()
  1675. -- Main loop
  1676. while true do
  1677. if displayState == "radar" then
  1678. holo.SetClearColor(backgroundColor)
  1679. holo.Clear()
  1680. --holo.FreeAllFrameBuffer()
  1681. drawRadarDisplay()
  1682. drawControls()
  1683. holo.Flush()
  1684. elseif displayState == "targetInfo" and selectedTarget then
  1685. displayTargetInfo(selectedTarget, targetTypeIcon)
  1686. elseif displayState == "adjustCannon" then
  1687. --monitor.clear()
  1688. drawCannonAdjustmentPage()
  1689. end
  1690. sleep(0.1)
  1691. -- Adjust this duration if needed
  1692. end
  1693. end,
  1694. handleHologramClick,
  1695. modemMessage,
  1696. aimPortCannonContinuous,
  1697. aimStarboardCannonContinuous,
  1698. aimBowCannonContinuous,
  1699. aimSternCannonContinuous,
  1700. missileWarning,
  1701. broadCastWaypoint,
  1702. missileListBuilding,
  1703. controlsSending,
  1704. printOutput
  1705. )
Advertisement
Add Comment
Please, Sign In to add comment