MegiRicky

thunderboltVLSconsoleV2

Dec 30th, 2024 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.34 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local radar = peripheral.find("sp_radar")
  3. local modem = peripheral.wrap("right")
  4.  
  5. -- Ensure all required peripherals are connected
  6. if not monitor or not radar or not ship then
  7. error("Required peripherals are not attached")
  8. end
  9.  
  10. monitor.setTextScale(0.5)
  11. monitor.clear()
  12.  
  13. local radarScale = 500
  14. local friendlyID = {289, 232}
  15. local targetPositions = {}
  16. local waypoints = {}
  17. local displayState = "radar" -- Initial display state
  18. local selectedTarget = nil
  19. local lockedTargetId = nil
  20. local lockedWaypoint = nil -- Variable to store the locked waypoint
  21. local projectileSpeed = 320 --m/s
  22.  
  23. local missileId = 1
  24. local missileLaunched = {}
  25. local controls = {
  26. fireMissile = {}
  27. }
  28.  
  29. -- Get channel data for cannons
  30. local function getChannelInput(prompt, default)
  31. print(prompt .. ", default: " .. default)
  32. local input = io.read()
  33. if input == "" then
  34. input = default
  35. end
  36. return tonumber(input)
  37. end
  38.  
  39. local controlChannel = getChannelInput("Input the missile control channel", 1400)
  40. modem.open(controlChannel)
  41.  
  42. local function checkFriendly(ID)
  43. local friendly = false
  44. for _, id in ipairs(friendlyID) do
  45. if tonumber(ID) == tonumber(id) then
  46. friendly = true
  47. break
  48. end
  49. end
  50. return friendly
  51. end
  52.  
  53. local w, h = monitor.getSize()
  54.  
  55. local function drawControls()
  56. local w, h = monitor.getSize()
  57. monitor.setCursorPos(1, h-1)
  58. monitor.write(" ")
  59. monitor.setCursorPos(1, h)
  60. monitor.write(" ")
  61. monitor.setCursorPos(1, h-1)
  62. monitor.write("Scale: ")
  63. monitor.setCursorPos(8, h-1)
  64. monitor.write("-")
  65. monitor.setCursorPos(10, h-1)
  66. monitor.write("+")
  67. monitor.setCursorPos(1, h-2)
  68. monitor.write("Stop locking")
  69.  
  70. local segmentLength = radarScale / 2
  71. local scaleText
  72. if segmentLength >= 1000 then
  73. scaleText = string.format("%.1f km", segmentLength / 1000)
  74. else
  75. scaleText = string.format("%d m", segmentLength)
  76. end
  77.  
  78. monitor.setCursorPos(1, h)
  79. monitor.write("Segment Length: " .. scaleText)
  80.  
  81. -- Add button to navigate to waypoint page
  82. monitor.setCursorPos(w-10, h-1)
  83. monitor.write("Waypoints")
  84.  
  85. -- Add button to navigate to the cannon adjustment page
  86. monitor.setCursorPos(w-18, h-1)
  87. monitor.write("Adjust Cannon")
  88. end
  89.  
  90. local function drawControls()
  91. local w, h = monitor.getSize()
  92. monitor.setCursorPos(1, h-1)
  93. monitor.write(" ")
  94. monitor.setCursorPos(1, h)
  95. monitor.write(" ")
  96. monitor.setCursorPos(1, h-1)
  97. monitor.write("Scale: ")
  98. monitor.setCursorPos(8, h-1)
  99. monitor.write("-")
  100. monitor.setCursorPos(10, h-1)
  101. monitor.write("+")
  102. monitor.setCursorPos(1, h-2)
  103. monitor.write("Stop locking")
  104.  
  105. local segmentLength = radarScale / 2
  106. local scaleText
  107. if segmentLength >= 1000 then
  108. scaleText = string.format("%.1f km", segmentLength / 1000)
  109. else
  110. scaleText = string.format("%d m", segmentLength)
  111. end
  112.  
  113. monitor.setCursorPos(1, h)
  114. monitor.write("Segment Length: " .. scaleText)
  115.  
  116. -- Add button to navigate to waypoint page at the bottom right corner
  117. monitor.setCursorPos(w-10, h-1)
  118. monitor.write("Waypoints")
  119.  
  120. -- Add button to navigate to the cannon adjustment page at the top right corner
  121. monitor.setCursorPos(w-13, 1)
  122. monitor.write("Adjust Cannon")
  123. end
  124.  
  125. local function addWaypoint(x, y, z, color)
  126. local newWaypoint = {
  127. x = x,
  128. y = y,
  129. z = z,
  130. color = color or colors.purple
  131. }
  132. table.insert(waypoints, newWaypoint)
  133. end
  134.  
  135. local function drawWaypointPage()
  136. local w, h = monitor.getSize()
  137. monitor.clear()
  138. monitor.setCursorPos(1, 1)
  139. monitor.write("Waypoint Page")
  140. monitor.setCursorPos(1, 3)
  141. monitor.write("Enter coordinates (x, y, z) and color:")
  142. monitor.setCursorPos(1, 4)
  143. monitor.write("Format: x,y,z,color")
  144. monitor.setCursorPos(1, 5)
  145. monitor.write("Colors: red, yellow, green, blue, purple")
  146. monitor.setCursorPos(1, h-1)
  147. monitor.write("Enter to submit, Back to cancel.")
  148. end
  149.  
  150. local function drawCannonAdjustmentPage()
  151. local w, h = monitor.getSize()
  152. monitor.clear()
  153. monitor.setCursorPos(1, 1)
  154. monitor.write("Cannon Pitch and Yaw Adjustment")
  155.  
  156. local cannonNames = {
  157. {name = "Port 5 inch", data = cannonData.port5inch, target = cannonTarget.port5inch},
  158. {name = "Starboard 5 inch", data = cannonData.starboard5inch, target = cannonTarget.starboard5inch},
  159. {name = "Bow 15 inch", data = cannonData.bow15inch, target = cannonTarget.bow15inch},
  160. {name = "Stern 15 inch", data = cannonData.stern15inch, target = cannonTarget.stern15inch}
  161. }
  162.  
  163. local yOffset = 3
  164.  
  165. for _, cannon in ipairs(cannonNames) do
  166. monitor.setCursorPos(1, yOffset)
  167. monitor.write(cannon.name .. ":")
  168.  
  169. monitor.setCursorPos(1, yOffset + 1)
  170. monitor.write("Current: pitch: " .. (cannon.data.pitch and string.format("%.2f", cannon.data.pitch) or "N/A"))
  171. monitor.setCursorPos(26, yOffset + 1)
  172. monitor.write("yaw: " .. (cannon.data.yaw and string.format("%.2f", cannon.data.yaw) or "N/A"))
  173.  
  174. monitor.setCursorPos(1, yOffset + 2)
  175. monitor.write("Adjustment: pitch: " .. (cannon.target.pitchAdjust and string.format("%.2f", cannon.target.pitchAdjust) or "N/A"))
  176. monitor.setCursorPos(26, yOffset + 2)
  177. monitor.write("yaw: " .. (cannon.target.yawAdjust and string.format("%.2f", cannon.target.yawAdjust) or "N/A"))
  178.  
  179. monitor.setCursorPos(1, yOffset + 3)
  180. monitor.write("[pitch +] [pitch -]")
  181. monitor.setCursorPos(w - 30, yOffset + 3)
  182. monitor.write("[yaw +] [yaw -]")
  183.  
  184. yOffset = yOffset + 6 -- Move down for the next cannon
  185. end
  186.  
  187. monitor.setCursorPos(1, h)
  188. monitor.write("Back")
  189. end
  190.  
  191. local function drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  192. local aspectRatio = 1.5
  193. local segmentLength = radarScale / 4
  194. local pixelPerSegmentX = w / 4
  195. local pixelPerSegmentY = h / 4
  196.  
  197. -- Draw the grid
  198. for i = 0, w - pixelPerSegmentX, pixelPerSegmentX do
  199. -- Vertical lines
  200. for y = 1, h - 2 do -- Leave space for the scale display at the bottom
  201. monitor.setCursorPos(i, y)
  202. monitor.write("|")
  203. end
  204. end
  205.  
  206. for j = 0, h - pixelPerSegmentY, pixelPerSegmentY do
  207. -- Horizontal lines
  208. for x = 1, w do
  209. monitor.setCursorPos(x, j)
  210. monitor.write("-")
  211. end
  212. end
  213.  
  214. -- Draw the circle
  215. for i = 0, 360, 5 do
  216. local rad = i * (math.pi / 180)
  217. local x = centerX + (radius * math.sin(rad)) * aspectRatio
  218. local y = centerY + radius * math.cos(rad)
  219. monitor.setCursorPos(math.floor(x + 0.5), math.floor(y + 0.5))
  220. monitor.write(".")
  221. end
  222.  
  223. -- Draw the crosshair
  224. for y = centerY - radius, centerY + radius do
  225. monitor.setCursorPos(centerX, y)
  226. monitor.write("|")
  227. end
  228.  
  229. local trueBearing = (math.deg(shipYawRadians) + 360) % 360 - 180
  230. if trueBearing < 0 then
  231. trueBearing = trueBearing + 360
  232. end
  233. local textPos = centerY - radius + 10
  234. if textPos > 0 then
  235. monitor.setCursorPos(centerX - (#tostring(math.floor(trueBearing)) + 2) / 2, textPos)
  236. monitor.write(tostring(math.floor(trueBearing)) .. "°")
  237. end
  238.  
  239. local directions = {"N", "W", "S", "E"}
  240. local dirAngles = {0, 90, 180, 270}
  241.  
  242. for index, dir in ipairs(directions) do
  243. local dirRad = (dirAngles[index] + math.deg(shipYawRadians)) * (math.pi / 180)
  244. local dirX = centerX + (radius * math.sin(dirRad)) * aspectRatio
  245. local dirY = centerY + radius * math.cos(dirRad)
  246. monitor.setCursorPos(math.floor(dirX + 0.5), math.floor(dirY + 0.5))
  247. monitor.write(dir)
  248. end
  249.  
  250. -- Display segment length
  251. monitor.setCursorPos(1, centerY + radius + 2)
  252. monitor.write("Segment Length: " .. segmentLength .. " meters")
  253. end
  254.  
  255. local function drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  256. local dx = relX - centerX
  257. local dy = relY - centerY
  258. local mag = math.sqrt((dx / aspectRatio)^2 + dy^2)
  259.  
  260. if (mag > radius) then
  261. local intersectX = centerX + ((dx / mag) * radius) * aspectRatio
  262. local intersectY = centerY + (dy / mag) * radius
  263. return intersectX, intersectY
  264. else
  265. return relX, relY
  266. end
  267. end
  268.  
  269. local function drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  270. local pos = ship.getWorldspacePosition()
  271. local shipYawRadians = math.atan2(-ship.getTransformationMatrix()[1][3], ship.getTransformationMatrix()[3][3])
  272. if shipYawRadians < 0 then shipYawRadians = shipYawRadians + 2 * math.pi end
  273. -- Function to calculate relative yaw in degrees from ship's facing
  274. local function relativeYaw(dx, dz)
  275. local angle = math.atan2(dx, dz)
  276. local relativeAngle = angle + shipYawRadians - math.pi
  277. if relativeAngle < -math.pi then
  278. relativeAngle = relativeAngle + 2 * math.pi
  279. elseif relativeAngle >= math.pi then
  280. relativeAngle = relativeAngle - 2 * math.pi
  281. end
  282. return math.deg(relativeAngle)
  283. end
  284. end
  285.  
  286. local function calculateRelativeAngle(objectAngle, shipYawRadians)
  287. local relativeAngle = objectAngle + shipYawRadians - math.pi
  288. if relativeAngle < -math.pi then
  289. relativeAngle = relativeAngle + 2 * math.pi
  290. elseif relativeAngle >= math.pi then
  291. relativeAngle = relativeAngle - 2 * math.pi
  292. end
  293. return relativeAngle
  294. end
  295.  
  296. local function drawRadarDisplay()
  297. local w, h = monitor.getSize()
  298. local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  299. local radius = math.min(centerX, centerY) - 1
  300. local aspectRatio = 1.2
  301.  
  302. local matrix = ship.getTransformationMatrix()
  303. local shipYawRadians = math.atan2(-matrix[1][3], matrix[3][3])
  304. if (shipYawRadians < 0) then
  305. shipYawRadians = shipYawRadians + 2 * math.pi
  306. end
  307.  
  308. drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  309. monitor.setCursorPos(centerX, centerY)
  310. monitor.write("+")
  311.  
  312. drawCannonCrosshair(centerX, centerY, radius, aspectRatio)
  313.  
  314. local pos = ship.getWorldspacePosition()
  315. local results = radar.scanForShips(10000)
  316. targetPositions = {}
  317. for _, object in ipairs(results) do
  318. local x = object.pos.x - pos.x
  319. local z = object.pos.z - pos.z
  320. local distance = math.sqrt(x^2 + z^2)
  321. local objectAngle = math.atan2(x, z)
  322. local relativeAngle = calculateRelativeAngle(objectAngle, shipYawRadians)
  323.  
  324. local scaledDistance = (distance / radarScale) * radius
  325. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  326. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  327.  
  328. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  329.  
  330. if (object.mass > 2000 and distance > 5) then
  331. if (distance > radarScale) then
  332. monitor.setTextColor(colors.lightBlue)
  333. else
  334. monitor.setTextColor(colors.red)
  335. end
  336. if (checkFriendly(object.id)) then
  337. monitor.setBackgroundColor(colors.blue)
  338. elseif (object.mass > 1000000) then
  339. monitor.setBackgroundColor(colors.orange)
  340. elseif (math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2) > 30) then
  341. monitor.setBackgroundColor(colors.red)
  342. else
  343. monitor.setBackgroundColor(colors.yellow)
  344. end
  345. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  346. monitor.write("o")
  347. monitor.setTextColor(colors.green)
  348. monitor.setBackgroundColor(colors.black)
  349.  
  350. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = object})
  351. end
  352. end
  353.  
  354. -- Draw waypoints
  355. for index, waypoint in ipairs(waypoints) do
  356. local wpX = waypoint.x - pos.x
  357. local wpZ = waypoint.z - pos.z
  358. local distance = math.sqrt(wpX^2 + wpZ^2)
  359. local wpAngle = math.atan2(wpX, wpZ)
  360. local relativeAngle = calculateRelativeAngle(wpAngle, shipYawRadians)
  361.  
  362. local scaledDistance = (distance / radarScale) * radius
  363. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  364. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  365.  
  366. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  367.  
  368. monitor.setBackgroundColor(waypoint.color)
  369. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  370. monitor.write("W")
  371. monitor.setBackgroundColor(colors.black)
  372.  
  373. 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}})
  374. end
  375. end
  376.  
  377. local function displayTargetInfo(target)
  378. local w, h = monitor.getSize()
  379. local pos = ship.getWorldspacePosition()
  380.  
  381. monitor.clear()
  382. monitor.setCursorPos(1, h-12)
  383. monitor.write("Target Info")
  384. monitor.setCursorPos(1, h-11)
  385.  
  386. local targetPosX, targetPosY, targetPosZ
  387. if target.type == "waypoint" then
  388. monitor.write("Type: Waypoint")
  389. targetPosX, targetPosY, targetPosZ = target.x, target.y, target.z
  390. monitor.setCursorPos(1, h-7)
  391. monitor.write("Coordinate: " .. target.x .. ", "..target.y..", ".. target.z)
  392. else
  393. monitor.write("ID: " .. target.id)
  394. targetPosX, targetPosY, targetPosZ = target.pos.x, target.pos.y, target.pos.z
  395. monitor.setCursorPos(1, h-10)
  396. monitor.write("Mass: " .. target.mass)
  397. monitor.setCursorPos(1, h-9)
  398. monitor.write("Velocity: " .. string.format("%.2f, %.2f, %.2f", target.velocity.x, target.velocity.y, target.velocity.z))
  399. monitor.setCursorPos(1, h-8)
  400. monitor.write("Position: " .. string.format("%.2f, %.2f, %.2f", target.pos.x, target.pos.y, target.pos.z))
  401. end
  402.  
  403. local distance = math.sqrt((targetPosX - pos.x)^2 + (targetPosZ - pos.z)^2)
  404. local dx = targetPosX - pos.x
  405. local dz = targetPosZ - pos.z
  406. local bearing = (math.deg(math.atan2(dz, dx)) + 360) % 360 - 270
  407. if bearing < 0 then
  408. bearing = bearing + 360
  409. end
  410.  
  411. monitor.setCursorPos(1, h-7)
  412. monitor.write("Distance: " .. string.format("%.2f", distance) .. " meters")
  413. monitor.setCursorPos(1, h-6)
  414. monitor.write("Bearing: " .. string.format("%.2f", bearing) .. "°")
  415.  
  416. if target.type ~= "waypoint" then
  417. local type = "unknown"
  418. local hostile = "hostile"
  419. if target.mass > 1000000 then
  420. type = "ship"
  421. elseif math.sqrt(target.velocity.x ^ 2 + target.velocity.y ^ 2 + target.velocity.z ^ 2) > 30 then
  422. type = "Flying missile / plane"
  423. else
  424. type = "unknown"
  425. end
  426. if checkFriendly(target.id) then
  427. hostile = "friendly"
  428. else
  429. hostile = "hostile"
  430. end
  431. monitor.setCursorPos(1, h-5)
  432. monitor.write("Type: " .. hostile .. " " .. type)
  433. end
  434.  
  435. if target.type == "waypoint" then
  436. monitor.setCursorPos(1, h-3)
  437. monitor.write("[LAUNCH MISSILE]")
  438. monitor.setCursorPos(1, h-2)
  439. monitor.write("[LAUNCH 3 MISSILE]")
  440. else
  441. monitor.setCursorPos(1, h-3)
  442. monitor.write("[LAUNCH MISSILE]")
  443. monitor.setCursorPos(1, h-2)
  444. monitor.write("[LAUNCH 3 MISSILE]")
  445. end
  446. monitor.setCursorPos(1, h)
  447. monitor.write("Back")
  448. end
  449.  
  450. local function launchMissile(targetType,targetId,targetPos)
  451. if type(controls.fireMissile) ~= "table" then
  452. controls.fireMissile = {}
  453. end
  454.  
  455. local currentMissileId = missileId
  456. -- Assign a table to this missile's ID in fireMissile, marking it as launched
  457. if targetType == "ship" then
  458. controls.fireMissile[currentMissileId] = {launch = true, type = targetType, id = targetId}
  459. else
  460. controls.fireMissile[currentMissileId] = {launch = true, type = targetType, pos = targetPos}
  461. end
  462. missileLaunched[currentMissileId] = true -- Mark this missile as launched
  463. print("Launching missile:", currentMissileId)
  464. print(textutils.serialize(controls))
  465.  
  466. missileId = missileId + 1 -- Increment missile ID for next launch
  467. end
  468.  
  469. local function handleTouch()
  470. local w, h = monitor.getSize()
  471. while true do
  472. local event, side, x, y = os.pullEvent("monitor_touch")
  473. if displayState == "radar" then
  474. if y == h-1 and x >= 8 and x <= 9 then
  475. radarScale = math.max(100, radarScale - 100)
  476. drawControls()
  477. drawRadarDisplay()
  478. elseif y == h-1 and x >= 10 and x <= 11 then
  479. radarScale = math.min(10000, radarScale + 100)
  480. drawControls()
  481. drawRadarDisplay()
  482. elseif y == h-1 and x >= w-10 and x <= w then
  483. displayState = "waypoints"
  484. monitor.clear()
  485. drawWaypointPage()
  486. print("Enter waypoint coordinates (x, y, z, color), color options: red, yellow, green, blue:")
  487. local input = read()
  488. local x, y, z, color = input:match("([^,]+),([^,]+),([^,]+),([^,]*)")
  489. local colorValue = colors.purple
  490. if color then
  491. local colorMap = {
  492. purple = colors.purple,
  493. red = colors.red,
  494. yellow = colors.yellow,
  495. green = colors.green,
  496. blue = colors.blue,
  497. white = colors.white,
  498. orange = colors.orange,
  499. lightBlue = colors.lightBlue,
  500. lime = colors.lime,
  501. pink = colors.pink,
  502. gray = colors.gray,
  503. lightGray = colors.lightGray,
  504. cyan = colors.cyan,
  505. purple = colors.purple,
  506. brown = colors.brown,
  507. black = colors.black
  508. }
  509. colorValue = colorMap[color] or colors.purple
  510. end
  511. if x and y and z then
  512. addWaypoint(tonumber(x), tonumber(y), tonumber(z), colorValue)
  513. end
  514. displayState = "radar"
  515. monitor.clear()
  516. drawControls()
  517. drawRadarDisplay()
  518. elseif y == 1 and x >= w-13 and x <= w then
  519. displayState = "adjustCannon"
  520. monitor.clear()
  521. drawCannonAdjustmentPage()
  522. elseif y == h-2 and x < 10 then
  523.  
  524. else
  525. for _, target in ipairs(targetPositions) do
  526. if x == target.x and y == target.y then
  527. selectedTarget = target.data
  528. displayState = "targetInfo"
  529. displayTargetInfo(selectedTarget)
  530. break
  531. end
  532. end
  533. end
  534. elseif displayState == "targetInfo" then
  535. if y == h and x >= 1 and x <= 4 then
  536. -- Back button
  537. displayState = "radar"
  538. monitor.clear()
  539. drawRadarDisplay()
  540. elseif y == h-3 and x >= 1 and x <= 32 then
  541. -- Launch missile
  542. local missileTarget = {}
  543. if selectedTarget.type == "waypoint" then
  544. launchMissile("waypoint",nil,selectedTarget)
  545. modem.transmit(controlChannel, controlChannel, controls)
  546. else
  547. launchMissile("ship",selectedTarget.id)
  548. modem.transmit(controlChannel, controlChannel, controls)
  549. end
  550. displayState = "radar"
  551. monitor.clear()
  552. drawRadarDisplay()
  553. monitor.setCursorPos(1, 1)
  554. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  555. elseif y == h-2 and x>=1 and x<=35 then
  556. -- Launch 3 missile
  557. if selectedTarget.type == "waypoint" then
  558. monitor.setCursorPos(1, 1)
  559. monitor.write("Launching salvo of missiles")
  560.  
  561. for i = 1, 3 do
  562. -- Create a new unique table for each missile
  563. local uniqueTarget = {
  564. x = selectedTarget.x,
  565. y = selectedTarget.y,
  566. z = selectedTarget.z
  567. }
  568. launchMissile("waypoint", nil, uniqueTarget)
  569. modem.transmit(controlChannel, controlChannel, controls)
  570. sleep(0.2)
  571. end
  572. else
  573. monitor.setCursorPos(1, 1)
  574. monitor.write("Launching salvo of missiles")
  575. launchMissile("ship",selectedTarget.id)
  576. sleep(0.2)
  577. launchMissile("ship",selectedTarget.id)
  578. sleep(0.2)
  579. launchMissile("ship",selectedTarget.id)
  580. modem.transmit(controlChannel, controlChannel, controls)
  581. sleep(0.2)
  582. end
  583. displayState = "radar"
  584. monitor.clear()
  585. drawRadarDisplay()
  586. monitor.setCursorPos(1, 1)
  587. monitor.write("Target locked: " .. (selectedTarget.type == "waypoint" and "Waypoint" or selectedTarget.id))
  588. end
  589. elseif displayState == "adjustCannon" then
  590. -- Back button
  591. if y == h and x >= 1 and x <= 4 then
  592. displayState = "radar"
  593. monitor.clear()
  594. drawRadarDisplay()
  595. end
  596.  
  597. -- Detect pitch and yaw adjustment button presses
  598. local yOffset = 3
  599. local buttonWidth = 10
  600. for i, cannonName in ipairs({"port5inch", "starboard5inch", "bow15inch", "stern15inch"}) do
  601. if y == yOffset + 3 then
  602. if x >= 1 and x <= 10 then
  603. -- Increase pitch
  604. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust + 0.1
  605. elseif x >= 11 and x <= 20 then
  606. -- Decrease pitch
  607. cannonTarget[cannonName].pitchAdjust = cannonTarget[cannonName].pitchAdjust - 0.1
  608. elseif x >= w - 30 and x <= w - 20 then
  609. -- Increase yaw
  610. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust + 0.1
  611. elseif x >= w - 19 and x <= w - 9 then
  612. -- Decrease yaw
  613. cannonTarget[cannonName].yawAdjust = cannonTarget[cannonName].yawAdjust - 0.1
  614. end
  615. monitor.clear()
  616. drawCannonAdjustmentPage()
  617. end
  618. yOffset = yOffset + 6
  619. end
  620. end
  621. end
  622. end
  623.  
  624. local function modemMessage()
  625. while true do
  626. if modem then
  627. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  628. if channel == port5inchChannel then
  629. cannonData.port5inch.hitPosX = message.x
  630. cannonData.port5inch.hitPosY = message.y
  631. cannonData.port5inch.hitPosZ = message.z
  632. cannonData.port5inch.pitch = message.cannonPitch
  633. cannonData.port5inch.yaw = message.cannonYaw
  634. cannonData.port5inch.source = message.source
  635. elseif channel == starboard5inchChannel then
  636. cannonData.starboard5inch.hitPosX = message.x
  637. cannonData.starboard5inch.hitPosY = message.y
  638. cannonData.starboard5inch.hitPosZ = message.z
  639. cannonData.starboard5inch.pitch = message.cannonPitch
  640. cannonData.starboard5inch.yaw = message.cannonYaw
  641. cannonData.starboard5inch.source = message.source
  642. elseif channel == bow15inchChannel then
  643. cannonData.bow15inch.hitPosX = message.x
  644. cannonData.bow15inch.hitPosY = message.y
  645. cannonData.bow15inch.hitPosZ = message.z
  646. cannonData.bow15inch.pitch = message.cannonPitch
  647. cannonData.bow15inch.yaw = message.cannonYaw
  648. cannonData.bow15inch.source = message.source
  649. elseif channel == stern15inchChannel then
  650. cannonData.stern15inch.hitPosX = message.x
  651. cannonData.stern15inch.hitPosY = message.y
  652. cannonData.stern15inch.hitPosZ = message.z
  653. cannonData.stern15inch.pitch = message.cannonPitch
  654. cannonData.stern15inch.yaw = message.cannonYaw
  655. cannonData.stern15inch.source = message.source
  656. end
  657. else
  658. sleep()
  659. end
  660. end
  661. end
  662.  
  663. local function controlsSending()
  664. while true do
  665. modem.transmit(controlChannel, controlChannel, controls)
  666. sleep()
  667. end
  668. end
  669.  
  670. parallel.waitForAny(
  671. function()
  672. -- Main loop
  673. while true do
  674. if displayState == "radar" then
  675. monitor.clear()
  676. monitor.setTextScale(0.5)
  677. drawRadarDisplay()
  678. drawControls()
  679. elseif displayState == "targetInfo" and selectedTarget then
  680. displayTargetInfo(selectedTarget)
  681. elseif displayState == "adjustCannon" then
  682. monitor.clear()
  683. drawCannonAdjustmentPage()
  684. end
  685. sleep(0.1)
  686. -- Adjust this duration if needed
  687. end
  688. end,
  689. handleTouch,
  690. modemMessage,
  691. controlsSending
  692. )
  693.  
  694.  
  695.  
Advertisement
Add Comment
Please, Sign In to add comment