Advertisement
zodiak707

Phoenix LSI Radar

Jan 31st, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 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 sides = peripheral.getNames()
  22. for key,side in pairs(sides) do
  23.     if peripheral.getType(side) == "warpdriveRadar" then
  24.         radar = peripheral.wrap(side)
  25.     end
  26. end
  27. if radar == nil or radar.isInterfaced() == nil then
  28.     showErrorAndExit("No radar detected")
  29. end
  30.  
  31. term.clear()
  32. term.setCursorPos(1, 1)
  33. print("Phoenix radar client of Lupus Space Industries started.")
  34.  
  35. function split(inputstr, sep)
  36.     if sep == nil then
  37.         sep = "%s"
  38.     end
  39.     local t={}
  40.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  41.         table.insert(t, str)
  42.     end
  43.     return t
  44. end
  45.  
  46. while true do
  47.     local ws, err = http.websocket("ws://144.76.116.79:2021")
  48.     if err then
  49.         showErrorAndExit(err)
  50.         shell.run("startup")
  51.     end
  52.     local message = ws.receive()
  53.    
  54.     if string.match(message, "radScan") then
  55.         local commands = split(message, " ")
  56.         local radius = commands[2]
  57.         radar.radius(radius)
  58.         print("Received: "..message)
  59.  
  60.         local success, result = radar.getEnergyRequired()
  61.         if not success then
  62.             print(result)
  63.             ws.send(result)
  64.         end
  65.         local energyRequired = result
  66.  
  67.         local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  68.         if energyStored < energyRequired then
  69.             print("Low energy level... (" .. energyStored .. "/" .. energyRequired .. " " .. energyUnits .. ")")
  70.         end
  71.  
  72.         local scanDuration = radar.getScanDuration()
  73.         radar.start()
  74.         os.sleep(0.5)
  75.  
  76.         print("Scanning... (" .. scanDuration .. " s)")
  77.         os.sleep(scanDuration)
  78.  
  79.         local delay = 0
  80.         local count
  81.         repeat
  82.             count = radar.getResultsCount()
  83.             os.sleep(0.1)
  84.             delay = delay + 1
  85.         until (count ~= nil and count ~= -1) or delay > 10
  86.    
  87.         energyStored, energyMax, energyUnits = radar.getEnergyStatus()
  88.         if count ~= nil and count > 0 then
  89.             for i=0, count-1 do
  90.                 local success, shipType, name, x, y, z, mass = radar.getResult(i)
  91.                 if success then
  92.                     if name ~= "" then
  93.                         ws.send("radarResult " .. shipType .. " " .. name .. " " .. x .. " " .. y .. " " .. z .. " " .. mass)
  94.                         print("Send: radarResult " .. shipType .. " " .. name .. " " .. x .. " " .. y .. " " .. z .. " " .. mass)
  95.                     else
  96.                         print("Nothing was found =(")
  97.                         ws.send("radarResult none")
  98.                     end
  99.                 else
  100.                     showError("Error " .. type)
  101.                 end
  102.             end
  103.             ws.send("radarEnergy " .. energyStored .. " " .. energyMax .. " " .. energyUnits)
  104.             print("Send: radarEnergy " .. energyStored .. " " .. energyMax .. " " .. energyUnits)
  105.         else
  106.             print("Nothing was found =(")
  107.             ws.send("radarResult none")
  108.         end
  109.     elseif string.match(message, "radEnergy") then
  110.         local energy, maxEnergy, energyType = radar.getEnergyStatus()
  111.         print("Received: "..message)
  112.         ws.send("radarEnergy " .. energy .. " " .. maxEnergy .. " " .. energyType)
  113.         print("Send: radarEnergy " .. energy .. " " .. maxEnergy .. " " .. energyType)
  114.     end
  115.     ws.close()
  116. end
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement