MegiRicky

computer craft radar

Jul 19th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.75 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local radar = peripheral.find("sp_radar")
  3.  
  4. if not monitor or not radar or not ship then
  5. error("Required peripherals are not attached")
  6. end
  7.  
  8. monitor.setTextScale(0.5)
  9. monitor.clear()
  10. local radarScale = 100
  11. local friendlyID = {289, 232}
  12. local targetPositions = {}
  13. local waypoints = {}
  14. local displayState = "radar" -- Initial display state
  15. local selectedTarget = nil
  16.  
  17. local function checkFriendly(ID)
  18. local friendly = false
  19. for _, id in ipairs(friendlyID) do
  20. if tonumber(ID) == tonumber(id) then
  21. friendly = true
  22. break
  23. end
  24. end
  25. return friendly
  26. end
  27.  
  28. local w, h = monitor.getSize()
  29.  
  30. local function drawControls()
  31. local w, h = monitor.getSize()
  32. monitor.setCursorPos(1, h-1)
  33. monitor.write(" ")
  34. monitor.setCursorPos(1, h)
  35. monitor.write(" ")
  36. monitor.setCursorPos(1, h-1)
  37. monitor.write("Scale: ")
  38. monitor.setCursorPos(8, h-1)
  39. monitor.write("-")
  40. monitor.setCursorPos(10, h-1)
  41. monitor.write("+")
  42.  
  43. local segmentLength = radarScale / 2
  44. local scaleText
  45. if segmentLength >= 1000 then
  46. scaleText = string.format("%.1f km", segmentLength / 1000)
  47. else
  48. scaleText = string.format("%d m", segmentLength)
  49. end
  50.  
  51. monitor.setCursorPos(1, h)
  52. monitor.write("Segment Length: " .. scaleText)
  53.  
  54. -- Add button to navigate to waypoint page
  55. monitor.setCursorPos(w-10, h-1)
  56. monitor.write("Waypoints")
  57. end
  58.  
  59. local function addWaypoint(x, z, color)
  60. local newWaypoint = {
  61. x = x,
  62. z = z,
  63. color = color or colors.purple
  64. }
  65. table.insert(waypoints, newWaypoint)
  66. end
  67.  
  68. local function drawWaypointPage()
  69. local w, h = monitor.getSize()
  70. monitor.setCursorPos(1, 1)
  71. monitor.write("Waypoint Page")
  72. monitor.setCursorPos(1, 3)
  73. monitor.write("Colors: purple (default),")
  74. monitor.setCursorPos(1, 4)
  75. monitor.write("red, yellow, green, blue")
  76. monitor.setCursorPos(1, h-1)
  77. monitor.write("Open the computer terminal")
  78. monitor.setCursorPos(1, h)
  79. monitor.write("to enter waypoint coordinates.")
  80. monitor.setCursorPos(1, h+1)
  81. monitor.write("Back")
  82. end
  83.  
  84. local function drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  85. local aspectRatio = 1.5
  86. local segmentLength = radarScale / 4
  87. local pixelPerSegmentX = w / 4
  88. local pixelPerSegmentY = h / 4
  89.  
  90. -- Draw the grid
  91. for i = 0, w - pixelPerSegmentX, pixelPerSegmentX do
  92. -- Vertical lines
  93. for y = 1, h - 2 do -- Leave space for the scale display at the bottom
  94. monitor.setCursorPos(i, y)
  95. monitor.write("|")
  96. end
  97. end
  98.  
  99. for j = 0, h - pixelPerSegmentY, pixelPerSegmentY do
  100. -- Horizontal lines
  101. for x = 1, w do
  102. monitor.setCursorPos(x, j)
  103. monitor.write("-")
  104. end
  105. end
  106.  
  107. -- Draw the circle
  108. for i = 0, 360, 5 do
  109. local rad = i * (math.pi / 180)
  110. local x = centerX + (radius * math.sin(rad)) * aspectRatio
  111. local y = centerY + radius * math.cos(rad)
  112. monitor.setCursorPos(math.floor(x + 0.5), math.floor(y + 0.5))
  113. monitor.write(".")
  114. end
  115.  
  116. -- Draw the crosshair
  117. for y = centerY - radius, centerY + radius do
  118. monitor.setCursorPos(centerX, y)
  119. monitor.write("|")
  120. end
  121.  
  122. local trueBearing = (math.deg(shipYawRadians) + 360) % 360 - 180
  123. if trueBearing < 0 then
  124. trueBearing = trueBearing + 360
  125. end
  126. local textPos = centerY - radius + 10
  127. if textPos > 0 then
  128. monitor.setCursorPos(centerX - (#tostring(math.floor(trueBearing)) + 2) / 2, textPos)
  129. monitor.write(tostring(math.floor(trueBearing)) .. "°")
  130. end
  131.  
  132. local directions = {"N", "W", "S", "E"}
  133. local dirAngles = {0, 90, 180, 270}
  134.  
  135. for index, dir in ipairs(directions) do
  136. local dirRad = (dirAngles[index] + math.deg(shipYawRadians)) * (math.pi / 180)
  137. local dirX = centerX + (radius * math.sin(dirRad)) * aspectRatio
  138. local dirY = centerY + radius * math.cos(dirRad)
  139. monitor.setCursorPos(math.floor(dirX + 0.5), math.floor(dirY + 0.5))
  140. monitor.write(dir)
  141. end
  142.  
  143. -- Display segment length
  144. monitor.setCursorPos(1, centerY + radius + 2)
  145. monitor.write("Segment Length: " .. segmentLength .. " meters")
  146. end
  147.  
  148. local function drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  149. local dx = relX - centerX
  150. local dy = relY - centerY
  151. local mag = math.sqrt((dx / aspectRatio)^2 + dy^2)
  152.  
  153. if (mag > radius) then
  154. local intersectX = centerX + ((dx / mag) * radius) * aspectRatio
  155. local intersectY = centerY + (dy / mag) * radius
  156. return intersectX, intersectY
  157. else
  158. return relX, relY
  159. end
  160. end
  161.  
  162. local function drawRadarDisplay()
  163. local w, h = monitor.getSize()
  164. local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  165. local radius = math.min(centerX, centerY) - 1
  166. local aspectRatio = 1.2
  167.  
  168. local matrix = ship.getRotationMatrix()
  169. local shipYawRadians = math.atan2(-matrix[1][3], matrix[3][3])
  170. if (shipYawRadians < 0) then
  171. shipYawRadians = shipYawRadians + 2 * math.pi
  172. end
  173.  
  174. drawRadarBackground(centerX, centerY, radius, shipYawRadians)
  175. monitor.setCursorPos(centerX, centerY)
  176. monitor.write("+")
  177.  
  178. local pos = ship.getWorldspacePosition()
  179. local results = radar.scanForShips(10000)
  180. targetPositions = {}
  181. for _, object in ipairs(results) do
  182. local x = object.pos.x - pos.x
  183. local z = object.pos.z - pos.z
  184. local distance = math.sqrt(x^2 + z^2)
  185. local objectAngle = math.atan2(x, z)
  186. local relativeAngle = objectAngle + shipYawRadians - math.pi
  187. if (relativeAngle < -math.pi) then
  188. relativeAngle = relativeAngle + 2 * math.pi
  189. elseif (relativeAngle >= math.pi) then
  190. relativeAngle = relativeAngle - 2 * math.pi
  191. end
  192.  
  193. local scaledDistance = (distance / radarScale) * radius
  194. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  195. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  196.  
  197. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  198.  
  199. if (object.mass > 20000 and distance > 5) then
  200. if (distance > radarScale) then
  201. monitor.setTextColor(colors.lightBlue)
  202. else
  203. monitor.setTextColor(colors.red)
  204. end
  205. if (checkFriendly(object.id)) then
  206. monitor.setBackgroundColor(colors.blue)
  207. elseif (object.mass > 1000000) then
  208. monitor.setBackgroundColor(colors.orange)
  209. elseif (math.sqrt(object.velocity.x ^ 2 + object.velocity.y ^ 2 + object.velocity.z ^ 2) > 30) then
  210. monitor.setBackgroundColor(colors.red)
  211. else
  212. monitor.setBackgroundColor(colors.yellow)
  213. end
  214. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  215. monitor.write("o")
  216. monitor.setTextColor(colors.green)
  217. monitor.setBackgroundColor(colors.black)
  218.  
  219. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = object})
  220. end
  221. end
  222.  
  223. -- Draw waypoints
  224. for index, waypoint in ipairs(waypoints) do
  225. local wpX = waypoint.x - pos.x
  226. local wpZ = waypoint.z - pos.z
  227. local distance = math.sqrt(wpX^2 + wpZ^2)
  228. local wpAngle = math.atan2(wpX, wpZ)
  229. local relativeAngle = wpAngle + shipYawRadians - math.pi
  230. if (relativeAngle < -math.pi) then
  231. relativeAngle = relativeAngle + 2 * math.pi
  232. elseif (relativeAngle >= math.pi) then
  233. relativeAngle = relativeAngle - 2 * math.pi
  234. end
  235.  
  236. local scaledDistance = (distance / radarScale) * radius
  237. local relX = centerX + (scaledDistance * math.sin(relativeAngle)) * aspectRatio
  238. local relY = centerY + scaledDistance * math.cos(relativeAngle)
  239.  
  240. relX, relY = drawTargetOnCircle(centerX, centerY, radius, relX, relY, aspectRatio)
  241.  
  242. monitor.setBackgroundColor(waypoint.color)
  243. monitor.setCursorPos(math.floor(relX + 0.5), math.floor(relY + 0.5))
  244. monitor.write("W")
  245. monitor.setBackgroundColor(colors.black)
  246.  
  247. table.insert(targetPositions, {x = math.floor(relX + 0.5), y = math.floor(relY + 0.5), data = {type = "waypoint", x = waypoint.x, z = waypoint.z, index = index}})
  248. end
  249. end
  250.  
  251. local function displayTargetInfo(target)
  252. local w, h = monitor.getSize()
  253. local pos = ship.getWorldspacePosition()
  254.  
  255. monitor.clear()
  256. monitor.setCursorPos(1, h-9)
  257. monitor.write("Target Info")
  258. monitor.setCursorPos(1, h-8)
  259.  
  260. local targetPosX, targetPosY, targetPosZ
  261. if target.type == "waypoint" then
  262. monitor.write("Type: Waypoint")
  263. targetPosX, targetPosY, targetPosZ = target.x, 0, target.z
  264. monitor.setCursorPos(1, h-7)
  265. monitor.write("Coordinate: " .. target.x .. ", " .. target.z)
  266. else
  267. monitor.write("ID: " .. target.id)
  268. targetPosX, targetPosY, targetPosZ = target.pos.x, target.pos.y, target.pos.z
  269. monitor.setCursorPos(1, h-7)
  270. monitor.write("Mass: " .. target.mass)
  271. monitor.setCursorPos(1, h-6)
  272. monitor.write("Velocity: " .. string.format("%.2f, %.2f, %.2f", target.velocity.x, target.velocity.y, target.velocity.z))
  273. monitor.setCursorPos(1, h-5)
  274. monitor.write("Position: " .. string.format("%.2f, %.2f, %.2f", target.pos.x, target.pos.y, target.pos.z))
  275. end
  276.  
  277. local distance = math.sqrt((targetPosX - pos.x)^2 + (targetPosZ - pos.z)^2)
  278. local dx = targetPosX - pos.x
  279. local dz = targetPosZ - pos.z
  280. local bearing = (math.deg(math.atan2(dz, dx)) + 360) % 360 - 270
  281. if bearing < 0 then
  282. bearing = bearing + 360
  283. end
  284.  
  285. monitor.setCursorPos(1, h-4)
  286. monitor.write("Distance: " .. string.format("%.2f", distance) .. " meters")
  287. monitor.setCursorPos(1, h-3)
  288. monitor.write("Bearing: " .. string.format("%.2f", bearing) .. "°")
  289.  
  290. if target.type ~= "waypoint" then
  291. local type = "unknown"
  292. local hostile = "hostile"
  293. if target.mass > 1000000 then
  294. type = "ship"
  295. elseif math.sqrt(target.velocity.x ^ 2 + target.velocity.y ^ 2 + target.velocity.z ^ 2) > 30 then
  296. type = "Flying missile / plane"
  297. else
  298. type = "unknown"
  299. end
  300. if checkFriendly(target.id) then
  301. hostile = "friendly"
  302. else
  303. hostile = "hostile"
  304. end
  305. monitor.setCursorPos(1, h-2)
  306. monitor.write("Type: " .. hostile .. " " .. type)
  307. end
  308.  
  309. if target.type == "waypoint" then
  310. monitor.setCursorPos(1, h-1)
  311. monitor.write("Delete Waypoint")
  312. end
  313.  
  314. monitor.setCursorPos(1, h)
  315. monitor.write("Back")
  316. end
  317.  
  318.  
  319. local function handleTouch()
  320. local w, h = monitor.getSize()
  321. while true do
  322. local event, side, x, y = os.pullEvent("monitor_touch")
  323. if displayState == "radar" then
  324. if y == h-1 and x == 8 then
  325. radarScale = math.max(100, radarScale - 100)
  326. drawControls()
  327. elseif y == h-1 and x == 10 then
  328. radarScale = math.min(10000, radarScale + 100)
  329. drawControls()
  330. elseif y == h-1 and x >= w-10 and x <= w then
  331. displayState = "waypoints"
  332. monitor.clear()
  333. drawWaypointPage()
  334. -- Prompt for waypoint coordinates
  335. print("Enter waypoint coordinates (x, z, color), color, red,yellow,green,blue:")
  336. local input = read()
  337. local x, z, color = input:match("([^,]+),([^,]+),([^,]*)")
  338. local colorValue = colors.purple
  339. if color then
  340. local colorMap = {
  341. purple = colors.purple,
  342. red = colors.red,
  343. yellow = colors.yellow,
  344. green = colors.green,
  345. blue = colors.blue,
  346. white = colors.white,
  347. orange = colors.orange,
  348. lightBlue = colors.lightBlue,
  349. lime = colors.lime,
  350. pink = colors.pink,
  351. gray = colors.gray,
  352. lightGray = colors.lightGray,
  353. cyan = colors.cyan,
  354. purple = colors.purple,
  355. brown = colors.brown,
  356. black = colors.black
  357. }
  358. colorValue = colorMap[color] or colors.purple
  359. end
  360. if x and z then
  361. addWaypoint(tonumber(x), tonumber(z), colorValue)
  362. end
  363. displayState = "radar"
  364. monitor.clear()
  365. drawControls()
  366. drawRadarDisplay()
  367. else
  368. for _, target in ipairs(targetPositions) do
  369. if x == target.x and y == target.y then
  370. selectedTarget = target.data
  371. displayState = "targetInfo"
  372. break
  373. end
  374. end
  375. end
  376. elseif displayState == "targetInfo" then
  377. if y == h and x >= 1 and x <= 4 then
  378. displayState = "radar"
  379. monitor.clear()
  380. drawControls()
  381. drawRadarDisplay()
  382. elseif y == h-5 and x >= 1 and x <= 15 then
  383. -- Ensure the correct waypoint is identified for deletion
  384. for i, target in ipairs(targetPositions) do
  385. if target.data.type == "waypoint" then
  386. table.remove(waypoints, target.data.index)
  387. break
  388. end
  389. end
  390. displayState = "radar"
  391. monitor.clear()
  392. drawControls()
  393. drawRadarDisplay()
  394. end
  395. end
  396. end
  397. end
  398.  
  399. parallel.waitForAny(
  400. function()
  401. -- Main loop
  402. while true do
  403. if displayState == "radar" then
  404. monitor.clear()
  405. monitor.setTextScale(0.5)
  406. drawRadarDisplay()
  407. drawControls()
  408. elseif displayState == "targetInfo" and selectedTarget then
  409. displayTargetInfo(selectedTarget)
  410. end
  411. sleep(0.1)
  412. end
  413. end,
  414. handleTouch
  415. )
  416.  
Add Comment
Please, Sign In to add comment