infiniteblock

Untitled

Nov 14th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. if not term.isColor() then
  2. print("Advanced computer required")
  3. error()
  4. end
  5.  
  6. local function showError(message)
  7. term.setBackgroundColor(colors.black)
  8. term.setTextColor(colors.red)
  9. term.write(message)
  10. term.setBackgroundColor(colors.black)
  11. term.setTextColor(colors.white)
  12. print()
  13. end
  14.  
  15. local function showErrorAndExit(message)
  16. showError(message)
  17. error()
  18. end
  19.  
  20. local radar
  21. local monitor
  22. local sides = peripheral.getNames()
  23. for _, side in pairs(sides) do
  24. if peripheral.getType(side) == "warpdriveRadar" then
  25. print("Radar found on " .. side)
  26. radar = peripheral.wrap(side)
  27. elseif peripheral.getType(side) == "monitor" then
  28. print("Monitor found on " .. side)
  29. monitor = peripheral.wrap(side)
  30. term.redirect(monitor)
  31. end
  32. end
  33. if radar == nil or radar.isInterfaced() == nil then
  34. showErrorAndExit("No radar detected")
  35. end
  36.  
  37. local argv = { ... }
  38. if #argv ~= 1 then
  39. showErrorAndExit("Usage: scan <scanRadius>")
  40. end
  41.  
  42. local radius = tonumber(argv[1])
  43.  
  44. local w, h = term.getSize()
  45. local scale = math.min(w, h) / 2
  46. local _, _, radarX, radarY, radarZ = radar.getGlobalPosition()
  47.  
  48. term.clear()
  49.  
  50. local function colorScreen(color)
  51. for a = 2, w - 1 do
  52. for b = 1, h do
  53. paintutils.drawPixel(a, b, color)
  54. end
  55. end
  56. end
  57.  
  58. local function textOut(x, y, text, fg, bg)
  59. term.setCursorPos(x, y)
  60. term.setTextColor(fg)
  61. term.setBackgroundColor(bg)
  62. term.write(text)
  63. local _, yt = term.getCursorPos()
  64. term.setCursorPos(1, yt + 1)
  65. end
  66.  
  67. local function translateXZ(oldX, oldZ)
  68. local x = radarX - oldX
  69. local z = radarZ - oldZ
  70.  
  71. x = x / (radius / scale)
  72. z = z / (radius / scale)
  73.  
  74. x = x + (w / 2)
  75. z = z + (h / 2)
  76.  
  77. x = math.floor(x)
  78. z = math.floor(z)
  79.  
  80. return x, z
  81. end
  82.  
  83. local function drawContact(x, _, z, name, color)
  84. local newX, newZ = translateXZ(x, z)
  85.  
  86. paintutils.drawPixel(newX, newZ, color)
  87. textOut(newX - 3, newZ + 1, "[" .. name .. "]", colors.white, colors.black)
  88. end
  89.  
  90. local function scanAndDraw()
  91. local energyStored, energyMax, _ = radar.getEnergyStatus()
  92. if energyStored == nil then energyStored = 0 end
  93. if energyMax == nil or energyMax == 0 then energyMax = 1 end
  94.  
  95. radar.radius(radius)
  96. local success, result = radar.getEnergyRequired()
  97. if not success then
  98. showErrorAndExit(result)
  99. end
  100. local energyRequired = result
  101.  
  102. if energyRequired <= 0 or energyStored < energyRequired then
  103. textOut((w / 2) - 7, 1, " /!\\ LOW POWER ", colors.white, colors.red)
  104. os.sleep(1)
  105.  
  106. return 0
  107. end
  108.  
  109. radar.start()
  110. local scanDuration = radar.getScanDuration()
  111. textOut((w / 2) - 7, 1, " ping sent ", colors.gray, colors.black)
  112. os.sleep(scanDuration)
  113.  
  114. local delay = 0
  115. local numResults
  116. repeat
  117. numResults = radar.getResultsCount()
  118. os.sleep(0.05)
  119. delay = delay + 1
  120. until (numResults ~= nil and numResults ~= -1) or delay > 10
  121.  
  122. redraw()
  123.  
  124. drawContact(radarX, radarY, radarZ, "RAD", colors.yellow)
  125.  
  126. if numResults ~= nil and numResults > 0 then
  127. for i = 0, numResults-1 do
  128. local success, _, name, cx, cy, cz = radar.getResult(i)
  129. if success then
  130. drawContact(cx, cy, cz, name, colors.red)
  131. end
  132. end
  133. end
  134.  
  135. os.sleep(scanDuration)
  136. end
  137.  
  138. function redraw()
  139. colorScreen(colors.green)
  140.  
  141. paintutils.drawLine(1, 1, w, 1, colors.black)
  142.  
  143. textOut((w / 2) - 7, 1, "= Q-Radar v0.4 =", colors.white, colors.black)
  144.  
  145. paintutils.drawLine(1, h, w, h, colors.black)
  146. local energyStored, _, energyUnits = radar.getEnergyStatus()
  147. if energyStored == nil then energyStored = 0 end
  148. textOut(4, h, "Energy: " .. energyStored .. " " .. energyUnits .. " | Scan radius: " .. radius, colors.white, colors.black)
  149. end
  150.  
  151. local continue = true
  152. while continue do
  153. scanAndDraw()
  154. os.sleep(0)
  155. end
  156.  
  157. term.clear()
Add Comment
Please, Sign In to add comment