Advertisement
Guest User

startup

a guest
Oct 23rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. shell.run("clear")
  2.  
  3. -- the webhook url for the requests
  4. local discordUri = "https://discordapp.com/api/webhooks/9897675164221440/bVNkDOVeMt0XruW3UBpLOys6ycH-E1ML9SoWwGrFXaTL_vFtpYxjj27quln6actJSEOr"
  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.  
  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.         if reqPwr <= currPwr then
  42.             break
  43.         else
  44.             if first then
  45.                 first = false
  46.             else
  47.                 local _,line=term.getCursorPos()
  48.                 term.setCursorPos(1,line-1)
  49.                 term.clearLine()
  50.             end
  51.             print("Waiting for energy.. ("..currPwr.."/"..reqPwr..")")                        
  52.            
  53.             os.sleep(1)
  54.         end
  55.     end
  56.     print("Scanning in the radius of "..radarRadius.." blocks. This will take "..tonumber(dur).."s.")
  57.    
  58.     radar.start()
  59.     os.sleep(dur + 0.1) -- sleep for duration plus buffer
  60.     radar.getResultsCount()
  61.     local cnt = radar.getResultsCount()    
  62.     local ret = {}
  63.     local dsts = {}
  64.     local alreadySeen = {}
  65.     local rPosOK,dim,rPosX,rPosY,rPosZ = radar.getGlobalPosition()
  66.     for i=1,cnt do
  67.         local result={radar.getResult(i-1)}
  68.         -- identifier:
  69.         local tv = result[2]..":"..result[3]..":"..result[4].."-"..result[5].."-"..result[6]..":"..result[7]
  70.         if result[1] == true then -- [1] = successful or not
  71.             table.insert(ret,result)            
  72.            
  73. --            alreadySeen[#alreadySeen+1]=tv
  74.         end
  75.     end
  76.     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)
  77.     return ret
  78. end -- func
  79.  
  80. local oldShips = {}
  81. local goodChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"--,;:_-#'+~*?!§$%&/()={[]}^|<>"
  82. radar.radius(radarRadius)
  83. radar.enable()
  84. while true do
  85.     local res = getRadarResults()
  86.    
  87.     shell.run("clear")    
  88.     print("==== Ship Scanner ====")
  89.      
  90.     local str = ""
  91.     local newShips = {}
  92.    
  93.     local firstMsgPart = true
  94. --    print("Currently tracked ships:")
  95.     local rPosOK,dim,rPosX,rPosY,rPosZ = radar.getGlobalPosition()
  96.     local rlPosX,rlPosY,rlPosZ = radar.getLocalPosition()
  97.     print("Our pos: global xyz: ("..rPosX.." "..rPosY.." "..rPosZ..") local xyz: ("..rlPosX.." "..rlPosY.." "..rlPosZ..")")
  98. --    rlPosX = 0
  99. --    rlPosY = 0
  100. --    rlPosZ = 0
  101.    
  102.     --print("ress:"..#res)
  103.     os.sleep(1)
  104.     for
  105.     i=1,#res do
  106.         local success, type, name, x, y, z, mass = table.unpack(res[i])
  107.         if name ~= "" then
  108.                 local cdist = getDst(rPosX,x,rPosY,y,rPosZ,z)
  109.                 local so = ""
  110.                 if cdist < safetyDist then so = so .. " :warning: " end
  111.                
  112.                 so = so.. "**"..name.."** ["..type..", "..mass.."t] at Hyper XYZ: "..x.." "..y.." "..z.." Local XYZ: "
  113.                
  114.                 so = so..(x-rPosX+rlPosX).." "..(y-rPosY+rlPosY).." "..(z-rPosZ+rlPosZ).. " **Distance: "..cdist.."m**."
  115.                 table.insert(newShips,so)
  116.                 --print("added element "..#newShips)
  117.                 --os.sleep(1)
  118.         if not tableHasValue(oldShips, so) then -- not already tracked
  119.             if firstMsgPart then
  120.                str = str.."**Newly tracked ships:**"
  121.                firstMsgPart = false
  122.             end
  123.             str = str.."\n"..so
  124.         end      
  125.         print(so)
  126.         --os.sleep(.5)
  127. --
  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