Advertisement
Alexr360

Radar

May 4th, 2025 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. -- Wrap peripherals
  2. local detector = peripheral.wrap("back") -- Change if needed
  3. local monitor = peripheral.wrap("top")
  4. -- Monitor setup
  5. monitor.setTextScale(0.5)
  6. monitor.setBackgroundColor(colors.black)
  7. monitor.setTextColor(colors.white)
  8.  
  9. -- Radar and monitor parameters
  10. local monWidth, monHeight = monitor.getSize()
  11. local centerX, centerY = math.floor(monWidth / 2), math.floor(monHeight / 2)
  12. local maxRange = 10
  13.  
  14. -- Click detection and display state
  15. local iconMap = {}          -- screenX,screenY => entity
  16. local infoEntityUUID = nil  -- UUID of selected entity
  17. local currentEntity = nil   -- Cached entity data
  18. local infoBoxLines = 5
  19. local infoBoxXBtn = {x = monWidth - 2, y = 1} -- Position of [X] button
  20.  
  21. -- Convert relative world position to screen
  22. local function toScreen(x, z)
  23.     local scale = maxRange
  24.     local screenX = centerX + math.floor(x / scale * centerX)
  25.     local screenY = centerY + math.floor(z / scale * centerY)
  26.     return screenX, screenY
  27. end
  28.  
  29. -- Draw the info panel
  30. local function drawInfo(entity)
  31.     monitor.setBackgroundColor(colors.gray)
  32.     monitor.setTextColor(colors.white)
  33.     for i = 1, infoBoxLines do
  34.         monitor.setCursorPos(1, i)
  35.         monitor.write(string.rep(" ", monWidth))
  36.     end
  37.  
  38.     -- Draw [X] close button
  39.     monitor.setCursorPos(infoBoxXBtn.x, infoBoxXBtn.y)
  40.     monitor.setTextColor(colors.red)
  41.     monitor.write("[X]")
  42.  
  43.     monitor.setCursorPos(1, 1)
  44.     monitor.setTextColor(colors.white)
  45.     monitor.write("Name: " .. (entity.name or "unknown"))
  46.  
  47.     monitor.setCursorPos(1, 2)
  48.     monitor.write("Type: " .. (entity.type or "unknown"))
  49.  
  50.     monitor.setCursorPos(1, 3)
  51.     monitor.write("Player: " .. tostring(entity.isPlayer))
  52.  
  53.     monitor.setCursorPos(1, 4)
  54.     monitor.write(string.format("Pos: %.1f, %.1f, %.1f", entity.x, entity.y, entity.z))
  55.  
  56.     monitor.setBackgroundColor(colors.black)
  57. end
  58.  
  59. -- Redraw radar and info panel
  60. local function drawRadar(entities)
  61.     iconMap = {}
  62.     monitor.setBackgroundColor(colors.black)
  63.     monitor.clear()
  64.  
  65.     -- Radar center
  66.     monitor.setCursorPos(centerX, centerY)
  67.     monitor.setTextColor(colors.white)
  68.     monitor.write("+")
  69.  
  70.     -- Plot entities
  71.     for _, entity in ipairs(entities) do
  72.         -- Skip armor stands
  73.         if entity.type ~= "Armor Stand" then
  74.             local screenX, screenY = toScreen(entity.x, entity.z)
  75.             if screenX >= 1 and screenX <= monWidth and screenY >= 1 and screenY <= monHeight then
  76.                 local key = screenX .. "," .. screenY
  77.                 iconMap[key] = entity
  78.  
  79.                 monitor.setCursorPos(screenX, screenY)
  80.                 if entity.isPlayer then
  81.                     monitor.setTextColor(colors.lime)
  82.                     monitor.write("P")
  83.                 else
  84.                     monitor.setTextColor(colors.red)
  85.                     monitor.write("E")
  86.                 end
  87.             end
  88.  
  89.             -- Update selected entity if matched
  90.             if infoEntityUUID and entity.uuid == infoEntityUUID then
  91.                 currentEntity = entity
  92.             end
  93.         end
  94.     end
  95.  
  96.     if currentEntity then
  97.         drawInfo(currentEntity)
  98.     end
  99.  
  100.     monitor.setTextColor(colors.white)
  101. end
  102.  
  103. -- Main loop
  104. while true do
  105.     local entities = detector.nearbyEntities()
  106.     drawRadar(entities)
  107.  
  108.     local timer = os.startTimer(1)
  109.     while true do
  110.         local event, side, x, y = os.pullEvent()
  111.  
  112.         if event == "monitor_touch" then
  113.             if x >= infoBoxXBtn.x and x <= infoBoxXBtn.x + 2 and y == infoBoxXBtn.y then
  114.                 infoEntityUUID = nil
  115.                 currentEntity = nil
  116.                 drawRadar(entities)
  117.             else
  118.                 local key = x .. "," .. y
  119.                 if iconMap[key] then
  120.                     infoEntityUUID = iconMap[key].uuid
  121.                     currentEntity = iconMap[key]
  122.                     drawRadar(entities)
  123.                 end
  124.             end
  125.         elseif event == "timer" and side == timer then
  126.             break
  127.         end
  128.     end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement