Advertisement
jaklsfjlsak

自 雷

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