Advertisement
osmarks

TrilateratorCtrl

Jul 27th, 2018
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. shell.run "rm t"
  2. shell.run "pastebin get TPT3gpNF t"
  3. os.loadAPI "t"
  4.  
  5. local function trilaterate( A, B, C )
  6.     local a2b = B.position - A.position
  7.     local a2c = C.position - A.position
  8.        
  9.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  10.         return nil
  11.     end
  12.    
  13.     local d = a2b:length()
  14.     local ex = a2b:normalize( )
  15.     local i = ex:dot( a2c )
  16.     local ey = (a2c - (ex * i)):normalize()
  17.     local j = ey:dot( a2c )
  18.     local ez = ex:cross( ey )
  19.  
  20.     local r1 = A.distance
  21.     local r2 = B.distance
  22.     local r3 = C.distance
  23.        
  24.     local x = (r1*r1 - r2*r2 + d*d) / (2*d)
  25.     local y = (r1*r1 - r3*r3 - x*x + (x-i)*(x-i) + j*j) / (2*j)
  26.        
  27.     local result = A.position + (ex * x) + (ey * y)
  28.  
  29.     local zSquared = r1*r1 - x*x - y*y
  30.     if zSquared > 0 then
  31.         local z = math.sqrt( zSquared )
  32.         local result1 = result + (ez * z)
  33.         local result2 = result - (ez * z)
  34.        
  35.         local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
  36.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  37.             return rounded1, rounded2
  38.         else
  39.             return rounded1
  40.         end
  41.     end
  42.     return result:round( 0.01 )
  43. end
  44.  
  45. local function narrow( p1, p2, fix )
  46.     local dist1 = math.abs( (p1 - fix.position):length() - fix.distance )
  47.     local dist2 = math.abs( (p2 - fix.position):length() - fix.distance )
  48.    
  49.     if math.abs(dist1 - dist2) < 0.01 then
  50.         return p1, p2
  51.     elseif dist1 < dist2 then
  52.         return p1:round( 0.01 )
  53.     else
  54.         return p2:round( 0.01 )
  55.     end
  56. end
  57.  
  58. t.on_all_modems(t.wired, "open", t.message_channel)
  59. t.on_all_modems(t.wireless, "open", 31415)
  60.  
  61. local function send_command(c)
  62.     t.on_all_modems(t.wired, "transmit", t.command_channel, t.message_channel, c)
  63. end
  64.  
  65. local function message_matches(last_fix, message)
  66.     if not last_fix then return true end
  67.     return type(last_fix.message) == type(message.message) and last_fix.channel == message.channel and last_fix.reply_channel == message.reply_channel
  68. end
  69.  
  70. local function do_trilateration()
  71.     while true do
  72.         local fixes = {}
  73.  
  74.         local already_has_count = 0
  75.  
  76.         repeat
  77.             local _, _, chan, reply_chan, message = os.pullEvent "modem_message"
  78.            
  79.             local last_fix = fixes[#fixes]
  80.  
  81.             if chan == t.message_channel and type(message) == "table" and message.position and message.ID and message.distance and message_matches(last_fix, message) then
  82.                 print("fix from", message.ID, #fixes)
  83.  
  84.                 local already_has_fix = false
  85.                 for _, f in pairs(fixes) do
  86.                     if f.ID == message.ID then already_has_fix = true already_has_count = already_has_count + 1 end
  87.                 end
  88.  
  89.                 if already_has_count >= 2 then
  90.                     fixes = {}
  91.                     already_has_count = 0
  92.                     print "Switching to next message."
  93.                 end
  94.  
  95.                 message.position = vector.new(table.unpack(message.position))
  96.  
  97.                 if not already_has_fix then table.insert(fixes, message)
  98.                 else print("Already have fix from", message.ID) end
  99.             end
  100.         until #fixes == 4
  101.  
  102.         local first = fixes[1]
  103.         local p1, p2 = trilaterate(fixes[1], fixes[2], fixes[3])
  104.         if p1 and p2 then
  105.             local pos = narrow(p1, p2, fixes[4])
  106.             local first_line = tostring(first.channel) .. " > " .. tostring(first.reply_channel)
  107.             local second_line = textutils.serialise(first.message) .. " | " .. textutils.serialise(pos)
  108.             print(first_line)
  109.             print(second_line)
  110.             t.on_all_modems(t.wireless, "transmit", 31415, t.command_channel, { type = "position fix", origin = "GTech Trilaterator", position = pos, channel = first.channel, replyChannel = first.reply_channel })
  111.         end
  112.     end
  113. end
  114.  
  115. local blacklist = {
  116.     [666] = true,
  117.     [999] = true
  118. }
  119.  
  120. local function switch_channels()
  121.     local channels = {}
  122.     while true do
  123.         local _, _, chan, reply_chan, message = os.pullEvent "modem_message"
  124.         if chan == 31415 and type(message) == "table" and message.senderChannel then
  125.             local new_c = message.senderChannel
  126.             local already_exists = false
  127.             for _, c in pairs(channels) do
  128.                 if c == new_c then print("Already listening on", c) already_exists = true break end
  129.             end
  130.             if not already_exists and not blacklist[new_c] and not blacklist[message.replyChannel] then
  131.                 print("Listening on channel", new_c)
  132.                 table.insert(channels, new_c)
  133.                 if #channels == 127 then
  134.                     print("Dropping channel", table.remove(channels, 1))
  135.                 end
  136.                 send_command {
  137.                     type = "set_channels",
  138.                     channels = channels
  139.                 }
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. parallel.waitForAll(do_trilateration, switch_channels)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement