Alexr360

Radar

May 4th, 2025 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.53 KB | None | 0 0
  1. -- Wrap peripherals
  2. local scanner = peripheral.wrap("back")
  3. local monitor = peripheral.wrap("top")
  4. local modem = peripheral.find("modem") or error("No modem attached", 0)
  5. local comp_x, comp_y, comp_z = gps.locate()
  6. local direction = "south" -- Options: north, south, east, west
  7.  
  8. if not scanner then
  9.     print("Universal Scanner not found!")
  10.     return
  11. end
  12.  
  13. if not monitor then
  14.     print("Monitor not found!")
  15.     return
  16. end
  17.  
  18. -- Monitor setup
  19. monitor.setTextScale(0.5)
  20. monitor.setBackgroundColor(colors.black)
  21. monitor.setTextColor(colors.white)
  22.  
  23. -- Parameters
  24. local monWidth, monHeight = monitor.getSize()
  25. local centerX, centerY = math.floor(monWidth / 2), math.floor(monHeight / 2)
  26.  
  27. local iconMap = {}
  28. local infoEntityUUID = nil
  29. local currentEntity = nil
  30. local infoBoxLines = 6
  31. local infoBoxXBtn = { x = monWidth - 2, y = 1 }
  32. local fireButton = { label = "[FIRE]", x = 1, y = 6 }
  33. local radiusOptions = { 10, 16, 20 }
  34. local radiusIndex = 3
  35. local maxRange = radiusOptions[radiusIndex]
  36. local radiusButton = { label = "", x = 1, y = monHeight }
  37. local firingContinuously = false
  38. local flashState = false
  39.  
  40. -- Helpers
  41. local function getRelativeTargetCoords(entity)
  42.     local dy = entity.y + comp_y
  43.     local dx = comp_x - entity.x
  44.     local dz = comp_z - entity.z
  45.  
  46.     if direction == "north" then
  47.         local dx = comp_x + entity.x
  48.         local dz = comp_z + entity.z
  49.     elseif direction == "south" then
  50.         return dx, dy, dz
  51.     elseif direction == "east" then
  52.         local dz = comp_z + entity.z
  53.     elseif direction == "west" then
  54.         local dx = comp_x + entity.x
  55.     end
  56.  
  57.     return dx, dy, dz
  58. end
  59.  
  60. local function toScreen(x, z)
  61.     local scale = maxRange
  62.     local screenX = centerX + math.floor(x / scale * centerX)
  63.     local screenY = centerY + math.floor(z / scale * centerY)
  64.     return screenX, screenY
  65. end
  66.  
  67. local function drawInfo(entity)
  68.     monitor.setBackgroundColor(firingContinuously and (flashState and colors.red or colors.gray) or colors.gray)
  69.     monitor.setTextColor(colors.white)
  70.    
  71.     for i = 1, infoBoxLines do
  72.         monitor.setCursorPos(1, i)
  73.         monitor.write(string.rep(" ", monWidth))
  74.     end
  75.  
  76.     monitor.setCursorPos(infoBoxXBtn.x, infoBoxXBtn.y)
  77.     monitor.setTextColor(colors.red)
  78.     monitor.write("[X]")
  79.  
  80.     monitor.setCursorPos(1, 1)
  81.     monitor.setTextColor(colors.white)
  82.     monitor.write("Name: " .. (entity.displayName or "unknown"))
  83.  
  84.     monitor.setCursorPos(1, 2)
  85.     monitor.write("Type: " .. (entity.type or "unknown"))
  86.  
  87.     monitor.setCursorPos(1, 3)
  88.     monitor.write("Health: " .. (entity.health or "?"))
  89.  
  90.     monitor.setCursorPos(1, 4)
  91.     monitor.write(string.format("REL Pos: %.1f, %.1f, %.1f", entity.x, entity.y, entity.z))
  92.  
  93.     local tx, ty, tz = getRelativeTargetCoords(entity)
  94.     monitor.setCursorPos(1, 5)
  95.     monitor.write(string.format("ABS Pos: %.1f, %.1f, %.1f", tx, ty, tz))
  96.  
  97.     monitor.setCursorPos(fireButton.x, fireButton.y)
  98.     monitor.setTextColor(colors.orange)
  99.     if firingContinuously then
  100.         monitor.write("[STOP]")
  101.     else
  102.         monitor.write(fireButton.label)
  103.     end
  104.  
  105.     monitor.setBackgroundColor(colors.black)
  106. end
  107.  
  108. local function drawEntityCount(count)
  109.     monitor.setCursorPos(1, 1)
  110.     monitor.setTextColor(colors.yellow)
  111.     monitor.write("Entities: " .. tostring(count))
  112.     monitor.setTextColor(colors.white)
  113. end
  114.  
  115. local function drawRadiusButton()
  116.     radiusButton.label = "Radius: [" .. tostring(maxRange) .. "]"
  117.     radiusButton.x = monWidth - #radiusButton.label + 1
  118.     monitor.setCursorPos(radiusButton.x, radiusButton.y)
  119.     monitor.setTextColor(colors.cyan)
  120.     monitor.write(radiusButton.label)
  121.     monitor.setTextColor(colors.white)
  122. end
  123.  
  124. local function drawRadar(entities)
  125.     iconMap = {}
  126.     monitor.setBackgroundColor(colors.black)
  127.     monitor.clear()
  128.  
  129.     monitor.setCursorPos(centerX, centerY)
  130.     monitor.setTextColor(colors.white)
  131.     monitor.write("+")
  132.  
  133.     local displayedCount = 0
  134.     local found = false
  135.  
  136.     for _, entity in ipairs(entities) do
  137.         if entity.displayName ~= "Armor Stand" and entity.displayName ~= "Hat Stand" then
  138.             local screenX, screenY = toScreen(entity.x, entity.z)
  139.  
  140.             if screenX >= 1 and screenX <= monWidth and screenY >= 1 and screenY <= monHeight then
  141.                 local key = screenX .. "," .. screenY
  142.                 iconMap[key] = entity
  143.  
  144.                 monitor.setCursorPos(screenX, screenY)
  145.                 if entity.uuid == infoEntityUUID and firingContinuously then
  146.                     monitor.setTextColor(flashState and colors.orange or colors.red)
  147.                 elseif entity.displayName == "Player" then
  148.                     monitor.setTextColor(colors.lime)
  149.                 else
  150.                     monitor.setTextColor(colors.red)
  151.                 end
  152.  
  153.                 local displayChar = entity.displayName and entity.displayName:sub(1, 1) or "?"
  154.                 monitor.write(displayChar)
  155.  
  156.                 displayedCount = displayedCount + 1
  157.             end
  158.  
  159.             if infoEntityUUID and entity.uuid == infoEntityUUID then
  160.                 currentEntity = entity
  161.                 found = true
  162.             end
  163.         end
  164.     end
  165.  
  166.     if not found then
  167.         currentEntity = nil
  168.         infoEntityUUID = nil
  169.         firingContinuously = false
  170.     end
  171.  
  172.     if currentEntity then
  173.         drawInfo(currentEntity)
  174.     else
  175.         drawEntityCount(displayedCount)
  176.     end
  177.  
  178.     drawRadiusButton()
  179.     monitor.setTextColor(colors.white)
  180. end
  181.  
  182. local function sendFireCommand(entity)
  183.     local tx, ty, tz = getRelativeTargetCoords(entity)
  184.     local messages = {"Artillery", "Arrows", "Scatter", "1", tostring(tx), tostring(ty), tostring(tz)}
  185.     for _, msg in ipairs(messages) do
  186.         modem.transmit(15, 43, msg)
  187.         sleep(0.1)
  188.     end
  189. end
  190.  
  191. -- Main loop
  192. while true do
  193.     flashState = not flashState
  194.     local status, entities = pcall(function()
  195.         return scanner.scan("entity", maxRange)
  196.     end)
  197.  
  198.     if not status or not entities then
  199.         print("Scan failed or returned nil.")
  200.     end
  201.  
  202.     drawRadar(entities)
  203.     local timer = os.startTimer(2)
  204.  
  205.     if firingContinuously and currentEntity then
  206.         sendFireCommand(currentEntity)
  207.     end
  208.  
  209.     while true do
  210.         local event, side, x, y = os.pullEvent()
  211.  
  212.         if event == "monitor_touch" then
  213.             if x >= infoBoxXBtn.x and x <= infoBoxXBtn.x + 2 and y == infoBoxXBtn.y then
  214.                 infoEntityUUID, currentEntity = nil, nil
  215.                 firingContinuously = false
  216.                 drawRadar(entities)
  217.  
  218.             elseif y == radiusButton.y and x >= radiusButton.x and x <= radiusButton.x + #radiusButton.label - 1 then
  219.                 radiusIndex = radiusIndex % #radiusOptions + 1
  220.                 maxRange = radiusOptions[radiusIndex]
  221.                 infoEntityUUID, currentEntity = nil, nil
  222.                 firingContinuously = false
  223.                 break
  224.  
  225.             elseif currentEntity and y == fireButton.y and x >= fireButton.x and x <= fireButton.x + #fireButton.label - 1 then
  226.                 firingContinuously = not firingContinuously
  227.                 drawRadar(entities)
  228.  
  229.             else
  230.                 local key = x .. "," .. y
  231.                 if iconMap[key] then
  232.                     infoEntityUUID = iconMap[key].uuid
  233.                     currentEntity = iconMap[key]
  234.                     drawRadar(entities)
  235.                 end
  236.             end
  237.  
  238.         elseif event == "timer" and side == timer then
  239.             break
  240.         end
  241.     end
  242.  
  243.     ::continue::
  244. end
  245.  
Advertisement
Add Comment
Please, Sign In to add comment