Advertisement
Guest User

startup

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