Advertisement
infiniteblock

Untitled

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