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