Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not term.isColor() then
- print("Advanced computer required")
- error()
- end
- local function showError(message)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.write(message)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- print()
- end
- local function showErrorAndExit(message)
- showError(message)
- error()
- end
- local radar
- local sides = peripheral.getNames()
- for key,side in pairs(sides) do
- if peripheral.getType(side) == "warpdriveRadar" then
- radar = peripheral.wrap(side)
- end
- end
- if radar == nil or radar.isInterfaced() == nil then
- showErrorAndExit("No radar detected")
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("Phoenix radar client of Lupus Space Industries started.")
- function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- while true do
- local ws, err = http.websocket("ws://144.76.116.79:2021")
- if err then
- showErrorAndExit(err)
- shell.run("startup")
- end
- local message = ws.receive()
- if string.match(message, "radScan") then
- local commands = split(message, " ")
- local radius = commands[2]
- radar.radius(radius)
- print("Received: "..message)
- local success, result = radar.getEnergyRequired()
- if not success then
- print(result)
- ws.send(result)
- end
- local energyRequired = result
- local energyStored, energyMax, energyUnits = radar.getEnergyStatus()
- if energyStored < energyRequired then
- print("Low energy level... (" .. energyStored .. "/" .. energyRequired .. " " .. energyUnits .. ")")
- end
- local scanDuration = radar.getScanDuration()
- radar.start()
- os.sleep(0.5)
- print("Scanning... (" .. scanDuration .. " s)")
- os.sleep(scanDuration)
- local delay = 0
- local count
- repeat
- count = radar.getResultsCount()
- os.sleep(0.1)
- delay = delay + 1
- until (count ~= nil and count ~= -1) or delay > 10
- energyStored, energyMax, energyUnits = radar.getEnergyStatus()
- if count ~= nil and count > 0 then
- for i=0, count-1 do
- local success, shipType, name, x, y, z, mass = radar.getResult(i)
- if success then
- if name ~= "" then
- ws.send("radarResult " .. shipType .. " " .. name .. " " .. x .. " " .. y .. " " .. z .. " " .. mass)
- print("Send: radarResult " .. shipType .. " " .. name .. " " .. x .. " " .. y .. " " .. z .. " " .. mass)
- else
- print("Nothing was found =(")
- ws.send("radarResult none")
- end
- else
- showError("Error " .. type)
- end
- end
- ws.send("radarEnergy " .. energyStored .. " " .. energyMax .. " " .. energyUnits)
- print("Send: radarEnergy " .. energyStored .. " " .. energyMax .. " " .. energyUnits)
- else
- print("Nothing was found =(")
- ws.send("radarResult none")
- end
- elseif string.match(message, "radEnergy") then
- local energy, maxEnergy, energyType = radar.getEnergyStatus()
- print("Received: "..message)
- ws.send("radarEnergy " .. energy .. " " .. maxEnergy .. " " .. energyType)
- print("Send: radarEnergy " .. energy .. " " .. maxEnergy .. " " .. energyType)
- end
- ws.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement