MegiRicky

55aAIMconsole.lua

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