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