Advertisement
Guest User

ping

a guest
Mar 26th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. if not term.isColor() then
  2.   print("Advanced computer required")
  3.   exit()
  4. end
  5.  
  6. function error(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 radar
  16. local sides = peripheral.getNames()
  17. for key,side in pairs(sides) do
  18.   if peripheral.getType(side) == "warpdriveRadar" then
  19.     print("Radar found on " .. side)
  20.     radar = peripheral.wrap(side)
  21.   end
  22. end
  23. if radar == nil then
  24.   error("No radar detected")
  25.   exit()
  26. end
  27.  
  28. local argv = { ... }
  29. if #argv ~= 1 then
  30.   error("Usage: ping <scanRadius>")
  31.   exit()
  32. end
  33.  
  34. local radius = tonumber(argv[1])
  35.  
  36. if radius < 1 or radius > 9999 then
  37.   error("Radius must be between 1 and 9999")
  38.   exit()
  39. end
  40.  
  41. local energy, energyMax = radar.energy()
  42. local energyRequired = radar.getEnergyRequired(radius)
  43. local scanDuration = radar.getScanDuration(radius)
  44. if energy < energyRequired then
  45.   error("Low energy level... (" .. energy .. "/" .. energyRequired .. ")")
  46.   exit()
  47. end
  48. radar.radius(radius)
  49. radar.start()
  50. os.sleep(0.5)
  51.  
  52. print("Scanning... (" .. scanDuration .. " s)")
  53. os.sleep(scanDuration)
  54.  
  55. local delay = 0
  56. local count
  57. repeat
  58.   count = radar.getResultsCount()
  59.   os.sleep(0.1)
  60.   delay = delay + 1
  61. until (count ~= nil and count ~= -1) or delay > 10
  62.  
  63. if count ~= nil and count > 0 then
  64.   for i=0, count-1 do
  65.     success, type, name, x, y, z = radar.getResult(i)
  66.     if success then
  67.       print(type .. " " .. name .. " @ (" .. x .. " " .. y .. " " .. z .. ")")
  68.     else
  69.       error("Error " .. type)
  70.     end
  71.   end
  72. else
  73.   print("Nothing was found =(")
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement