SHOW:
|
|
- or go back to the newest paste.
| 1 | -- the webhook url for the requests | |
| 2 | local discordUri = "https://discordapp.com/api/webhooks/9897675164221440/bVNkDOVeMt0XruW3UBpLOys6ycH-E1ML9SoWwGrFXaTL_vFtpYxjj27quln6actJSEOr" | |
| 3 | -- Node ID | |
| 4 | nodeid = 1 | |
| 5 | -- waittime between radar scans | |
| 6 | local intervalSec = 15 | |
| 7 | -- range in blocks | |
| 8 | local radarRadius = 1000 | |
| 9 | -- if something is closer than that then a warning sign will be prepended in discord | |
| 10 | local safetyDist = 400 | |
| 11 | ||
| 12 | term.clear() | |
| 13 | term.setBackgroundColor(colors.gray) | |
| 14 | term.setTextColor(colors.white) | |
| 15 | term.clear() | |
| 16 | term.setCursorPos(1,2) | |
| 17 | print(" _/ _/ _/_/_/_/ _/_/_/ _/_/_/ ")
| |
| 18 | print(" _/ _/ _/ _/ _/ _/ ")
| |
| 19 | print(" _/_/_/_/ _/_/_/ _/ _/_/ _/_/_/ ")
| |
| 20 | print(" _/ _/ _/ _/ _/ _/ _/ ")
| |
| 21 | print(" _/ _/ _/ _/_/_/ _/ _/ ")
| |
| 22 | print(" ")
| |
| 23 | term.setTextColor( colors.red ) | |
| 24 | - | term.setTextColor( colors.greeb ) |
| 24 | + | |
| 25 | - | print(" Node ID: " & nodeid & " ")
|
| 25 | + | term.setTextColor( colors.green ) |
| 26 | print(" Node ID: " & nodeid & " Online")
| |
| 27 | term.setTextColor( colors.white ) | |
| 28 | sleep(4) | |
| 29 | ||
| 30 | local radar = peripheral.find("warpdriveRadar")
| |
| 31 | if radar == nil then | |
| 32 | error("No radar could be found!")
| |
| 33 | end | |
| 34 | ||
| 35 | function tableHasValue(table, v) | |
| 36 | for i=1,#table do | |
| 37 | if table[i] == v then | |
| 38 | return true | |
| 39 | end | |
| 40 | end | |
| 41 | return false | |
| 42 | end | |
| 43 | ||
| 44 | function getDst(x1,x2,y1,y2,z1,z2) | |
| 45 | local dx = x1-x2 | |
| 46 | local dy = y1-y2 | |
| 47 | local dz = z1-z2 | |
| 48 | return math.floor(math.sqrt(dx*dx+dy*dy+dz*dz)) | |
| 49 | end | |
| 50 | ||
| 51 | function getRadarResults() | |
| 52 | ||
| 53 | local dur = radar.getScanDuration(radarRadius) | |
| 54 | local first = true | |
| 55 | while true do | |
| 56 | local currPwr, maxPwr, unit = radar.getEnergyStatus() | |
| 57 | local _,reqPwr = radar.getEnergyRequired(radarRadius) | |
| 58 | if reqPwr <= currPwr then | |
| 59 | break | |
| 60 | else | |
| 61 | if first then | |
| 62 | first = false | |
| 63 | else | |
| 64 | local _,line=term.getCursorPos() | |
| 65 | term.setCursorPos(1,line-1) | |
| 66 | term.clearLine() | |
| 67 | end | |
| 68 | print("Waiting for energy.. ("..currPwr.."/"..reqPwr..")")
| |
| 69 | ||
| 70 | os.sleep(1) | |
| 71 | end | |
| 72 | end | |
| 73 | print("Scanning in the radius of "..radarRadius.." blocks. This will take "..tonumber(dur).."s.")
| |
| 74 | ||
| 75 | radar.start() | |
| 76 | os.sleep(dur + 0.1) -- sleep for duration plus buffer | |
| 77 | radar.getResultsCount() | |
| 78 | local cnt = radar.getResultsCount() | |
| 79 | local ret = {}
| |
| 80 | local dsts = {}
| |
| 81 | local alreadySeen = {}
| |
| 82 | local rPosOK,dim,rPosX,rPosY,rPosZ = radar.getGlobalPosition() | |
| 83 | for i=1,cnt do | |
| 84 | local result={radar.getResult(i-1)}
| |
| 85 | -- identifier: | |
| 86 | local tv = result[2]..":"..result[3]..":"..result[4].."-"..result[5].."-"..result[6]..":"..result[7] | |
| 87 | if result[1] == true then -- [1] = successful or not | |
| 88 | table.insert(ret,result) | |
| 89 | ||
| 90 | -- alreadySeen[#alreadySeen+1]=tv | |
| 91 | end | |
| 92 | end | |
| 93 | table.sort(ret, function(a,b) return getDst(rPosX,a[4],rPosY,a[5],rPosZ,a[6]) < getDst(rPosX,b[4],rPosY,b[5],rPosZ,b[6]) end) | |
| 94 | return ret | |
| 95 | end -- func | |
| 96 | ||
| 97 | local oldShips = {}
| |
| 98 | local goodChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"--,;:_-#'+~*?!ยง$%&/()={[]}^|<>"
| |
| 99 | radar.radius(radarRadius) | |
| 100 | radar.enable() | |
| 101 | while true do | |
| 102 | local res = getRadarResults() | |
| 103 | ||
| 104 | shell.run("clear")
| |
| 105 | print("==== Ship Scanner ====")
| |
| 106 | ||
| 107 | local str = "" | |
| 108 | local newShips = {}
| |
| 109 | ||
| 110 | local firstMsgPart = true | |
| 111 | -- print("Currently tracked ships:")
| |
| 112 | local rPosOK,dim,rPosX,rPosY,rPosZ = radar.getGlobalPosition() | |
| 113 | local rlPosX,rlPosY,rlPosZ = radar.getLocalPosition() | |
| 114 | print("Our pos: global xyz: ("..rPosX.." "..rPosY.." "..rPosZ..") local xyz: ("..rlPosX.." "..rlPosY.." "..rlPosZ..")")
| |
| 115 | -- rlPosX = 0 | |
| 116 | -- rlPosY = 0 | |
| 117 | -- rlPosZ = 0 | |
| 118 | ||
| 119 | --print("ress:"..#res)
| |
| 120 | os.sleep(1) | |
| 121 | for | |
| 122 | i=1,#res do | |
| 123 | local success, type, name, x, y, z, mass = table.unpack(res[i]) | |
| 124 | if name ~= "" then | |
| 125 | local cdist = getDst(rPosX,x,rPosY,y,rPosZ,z) | |
| 126 | local so = "" | |
| 127 | if cdist < safetyDist then so = so .. " :warning: " end | |
| 128 | ||
| 129 | so = so.. "**"..name.."** ["..type..", "..mass.."t] at Hyper XYZ: "..x.." "..y.." "..z.." Local XYZ: " | |
| 130 | ||
| 131 | so = so..(x-rPosX+rlPosX).." "..(y-rPosY+rlPosY).." "..(z-rPosZ+rlPosZ).. " **Distance: "..cdist.."m**." | |
| 132 | table.insert(newShips,so) | |
| 133 | --print("added element "..#newShips)
| |
| 134 | --os.sleep(1) | |
| 135 | if not tableHasValue(oldShips, so) then -- not already tracked | |
| 136 | if firstMsgPart then | |
| 137 | str = str.."**Newly tracked ships:**" | |
| 138 | firstMsgPart = false | |
| 139 | end | |
| 140 | str = str.."\n"..so | |
| 141 | end | |
| 142 | print(so) | |
| 143 | --os.sleep(.5) | |
| 144 | -- | |
| 145 | end | |
| 146 | end | |
| 147 | ||
| 148 | ||
| 149 | firstMsgPart = true | |
| 150 | for i=1,#oldShips do | |
| 151 | if not tableHasValue(newShips, oldShips[i]) then -- if a ship was removed | |
| 152 | if firstMsgPart then | |
| 153 | str = str.."Lost contact with ships:\n" | |
| 154 | firstMsgPart = false | |
| 155 | end | |
| 156 | str = str..oldShips[i].."\n" | |
| 157 | end | |
| 158 | end | |
| 159 | print("\n\n")
| |
| 160 | --print("l3:"..#newShips)
| |
| 161 | if str ~= "" then | |
| 162 | str = "-------------------------------\n"..str | |
| 163 | --print("posting update to discord...")
| |
| 164 | local sanitized = "" | |
| 165 | for j=1,str:len() do | |
| 166 | if string.find(str:sub(j,j), "\"") or str:sub(j,j):find("\\") then
| |
| 167 | sanitized = sanitized .. "." | |
| 168 | -- print("removed "..str:sub(j,j))
| |
| 169 | else | |
| 170 | sanitized = sanitized .. string.sub(str,j,j) | |
| 171 | end | |
| 172 | end | |
| 173 | print(sanitized) | |
| 174 | if sanitized:len() > 1950 then | |
| 175 | sanitized = sanitized:sub(1,1951).."[..]" | |
| 176 | end | |
| 177 | http.post(discordUri, "{\"content\":\""..sanitized:gsub("\n","\\n").."\"}",{['content-type']="application/json"})
| |
| 178 | end | |
| 179 | oldShips = newShips | |
| 180 | sleep(intervalSec) | |
| 181 | end |