View difference between Paste ID: 2uGtcMvA and fCU2qfjP
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")
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
      print("Waiting for energy.. ("..currPwr.."/"..reqPwr..")")
69
70
      os.sleep(1)
71
    end
72
  end
73
  term.setCursorPos(1,10)
74
  term.clearLine()
75
  print("Scanning in the radius of "..radarRadius.." blocks. This will take "..tonumber(dur).."s.")
76
77
  radar.start()
78
  os.sleep(dur + 0.1) -- sleep for duration plus buffer
79
  radar.getResultsCount()
80
  local cnt = radar.getResultsCount()
81
  local ret = {}
82
  local dsts = {}
83
  local alreadySeen = {}
84
  local rPosOK,dim,rPosX,rPosY,rPosZ = radar.getGlobalPosition()
85
  for i=1,cnt do
86
    local result={radar.getResult(i-1)}
87
    -- identifier:
88
    local tv = result[2]..":"..result[3]..":"..result[4].."-"..result[5].."-"..result[6]..":"..result[7]
89
    if result[1] == true then -- [1] = successful or not
90
      table.insert(ret,result)
91
92
      --            alreadySeen[#alreadySeen+1]=tv
93
    end
94
  end
95
  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)
96
  return ret
97
end -- func
98
99
local oldShips = {}
100
local goodChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"--,;:_-#'+~*?!§$%&/()={[]}^|<>"
101
radar.radius(radarRadius)
102
radar.enable()
103
while true do
104-
  shell.run("clear")
104+
105-
  print("==== Ship Scanner ====")
105+
106
 -- shell.run("clear")
107
 -- print("==== Ship Scanner ====")
108
109
  local str = ""
110
  local newShips = {}
111
112
  local firstMsgPart = true
113
  --    print("Currently tracked ships:")
114-
  print("Our pos: global xyz: ("..rPosX.." "..rPosY.." "..rPosZ..") local xyz: ("..rlPosX.." "..rlPosY.." "..rlPosZ..")")
114+
115
  local rlPosX,rlPosY,rlPosZ = radar.getLocalPosition()
116
 -- print("Our pos: global xyz: ("..rPosX.." "..rPosY.." "..rPosZ..") local xyz: ("..rlPosX.." "..rlPosY.." "..rlPosZ..")")
117
  --    rlPosX = 0
118
  --    rlPosY = 0
119
  --    rlPosZ = 0
120
121
  --print("ress:"..#res)
122
  os.sleep(1)
123
  for
124
  i=1,#res do
125
    local success, type, name, x, y, z, mass = table.unpack(res[i])
126
    if name ~= "" then
127
      local cdist = getDst(rPosX,x,rPosY,y,rPosZ,z)
128
      local so = ""
129
      if cdist < safetyDist then so = so .. " :warning: " end
130
131
      so = so.. "**"..name.."** ["..type..", "..mass.."t] at Hyper XYZ: "..x.." "..y.." "..z.." Local XYZ: "
132
133
      so = so..(x-rPosX+rlPosX).." "..(y-rPosY+rlPosY).." "..(z-rPosZ+rlPosZ).. " **Distance: "..cdist.."m**."
134
      table.insert(newShips,so)
135
      --print("added element "..#newShips)
136
      --os.sleep(1)
137
      if not tableHasValue(oldShips, so) then -- not already tracked
138
        if firstMsgPart then
139
          str = str.."**Newly tracked ships:**"
140
          firstMsgPart = false
141
        end
142
        str = str.."\n"..so
143
      end
144
      print(so)
145
      --os.sleep(.5)
146
      --
147
    end
148
  end
149
150
151
  firstMsgPart = true
152
  for i=1,#oldShips do
153
    if not tableHasValue(newShips, oldShips[i]) then -- if a ship was removed
154
      if firstMsgPart then
155
        str = str.."Lost contact with ships:\n"
156
        firstMsgPart = false
157
      end
158
      str = str..oldShips[i].."\n"
159
    end
160
  end
161
  print("\n\n")
162
  --print("l3:"..#newShips)
163
  if str ~= "" then
164
    str = "-------------------------------\n"..str
165
    --print("posting update to discord...")
166
    local sanitized = ""
167
    for j=1,str:len() do
168
      if string.find(str:sub(j,j), "\"") or str:sub(j,j):find("\\") then
169
        sanitized = sanitized .. "."
170
        --     print("removed "..str:sub(j,j))
171
      else
172
        sanitized = sanitized .. string.sub(str,j,j)
173
      end
174
    end
175
    print(sanitized)
176
    if sanitized:len() > 1950 then
177
      sanitized = sanitized:sub(1,1951).."[..]"
178
    end
179
    http.post(discordUri, "{\"content\":\""..sanitized:gsub("\n","\\n").."\"}",{['content-type']="application/json"})
180
  end
181
  oldShips = newShips
182
  sleep(intervalSec)
183
end