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