Advertisement
osmarks

PotatOSGPS

Jan 6th, 2020
1,616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.32 KB | None | 0 0
  1. local expect
  2. if _G["~expect"] then
  3.     expect = _G["~expect"]
  4. else
  5.     local native_select, native_type = select, type
  6.     expect = function(index, value, ...)
  7.         local t = native_type(value)
  8.         for i = 1, native_select("#", ...) do
  9.             if t == native_select(i, ...) then return true end
  10.         end
  11.         local types = table.pack(...)
  12.         for i = types.n, 1, -1 do
  13.             if types[i] == "nil" then table.remove(types, i) end
  14.         end
  15.         local type_names
  16.         if #types <= 1 then
  17.             type_names = tostring(...)
  18.         else
  19.             type_names = table.concat(types, ", ", 1, #types - 1) .. " or " .. types[#types]
  20.         end
  21.         -- If we can determine the function name with a high level of confidence, try to include it.
  22.         local name
  23.         if native_type(debug) == "table" and native_type(debug.getinfo) == "function" then
  24.             local ok, info = pcall(debug.getinfo, 3, "nS")
  25.             if ok and info.name and #info.name ~= "" and info.what ~= "C" then name = info.name end
  26.         end
  27.         if name then
  28.             error( ("bad argument #%d to '%s' (expected %s, got %s)"):format(index, name, type_names, t), 3 )
  29.         else
  30.             error( ("bad argument #%d (expected %s, got %s)"):format(index, type_names, t), 3 )
  31.         end
  32.     end
  33. end
  34.  
  35. CHANNEL_GPS = 65534
  36.  
  37. local function trilaterate( A, B, C )
  38.     local a2b = B.vPosition - A.vPosition
  39.     local a2c = C.vPosition - A.vPosition
  40.  
  41.     if math.abs( a2b:normalize():dot( a2c:normalize() ) ) > 0.999 then
  42.         return nil
  43.     end
  44.  
  45.     local d = a2b:length()
  46.     local ex = a2b:normalize( )
  47.     local i = ex:dot( a2c )
  48.     local ey = (a2c - ex * i):normalize()
  49.     local j = ey:dot( a2c )
  50.     local ez = ex:cross( ey )
  51.  
  52.     local r1 = A.nDistance
  53.     local r2 = B.nDistance
  54.     local r3 = C.nDistance
  55.  
  56.     local x = (r1 * r1 - r2 * r2 + d * d) / (2 * d)
  57.     local y = (r1 * r1 - r3 * r3 - x * x + (x - i) * (x - i) + j * j) / (2 * j)
  58.  
  59.     local result = A.vPosition + ex * x + ey * y
  60.  
  61.     local zSquared = r1 * r1 - x * x - y * y
  62.     if zSquared > 0 then
  63.         local z = math.sqrt( zSquared )
  64.         local result1 = result + ez * z
  65.         local result2 = result - ez * z
  66.  
  67.         local rounded1, rounded2 = result1:round( 0.01 ), result2:round( 0.01 )
  68.         if rounded1.x ~= rounded2.x or rounded1.y ~= rounded2.y or rounded1.z ~= rounded2.z then
  69.             return rounded1, rounded2
  70.         else
  71.             return rounded1
  72.         end
  73.     end
  74.     return result:round( 0.01 )
  75.  
  76. end
  77.  
  78. local function narrow( p1, p2, fix )
  79.     local dist1 = math.abs( (p1 - fix.vPosition):length() - fix.nDistance )
  80.     local dist2 = math.abs( (p2 - fix.vPosition):length() - fix.nDistance )
  81.  
  82.     if math.abs(dist1 - dist2) < 0.01 then
  83.         return p1, p2
  84.     elseif dist1 < dist2 then
  85.         return p1:round( 0.01 )
  86.     else
  87.         return p2:round( 0.01 )
  88.     end
  89. end
  90.  
  91. function locate( _nTimeout, _bDebug )
  92.     expect(1, _nTimeout, "number", "nil")
  93.     expect(2, _bDebug, "boolean", "nil")
  94.     -- Let command computers use their magic fourth-wall-breaking special abilities
  95.     if commands then
  96.         return commands.getBlockPosition()
  97.     end
  98.  
  99.     -- Find a modem
  100.     local sModemSide = nil
  101.     for _, sSide in ipairs( rs.getSides() ) do
  102.         if peripheral.getType( sSide ) == "modem" and peripheral.call( sSide, "isWireless" ) then
  103.             sModemSide = sSide
  104.             break
  105.         end
  106.     end
  107.  
  108.     if sModemSide == nil then
  109.         if _bDebug then
  110.             print( "No wireless modem attached" )
  111.         end
  112.         return nil
  113.     end
  114.  
  115.     if _bDebug then
  116.         print( "Finding position..." )
  117.     end
  118.    
  119.     -- Open it
  120.     local modem = peripheral.wrap( sModemSide )
  121.     local bCloseChannel = false
  122.     if not modem.isOpen( CHANNEL_GPS ) then
  123.         modem.open( CHANNEL_GPS )
  124.         bCloseChannel = true
  125.     end
  126.  
  127.     -- Send a ping to listening GPS hosts
  128.     modem.transmit( CHANNEL_GPS, CHANNEL_GPS, "PING" )
  129.  
  130.     -- Wait for the responses
  131.     local tFixes = {}
  132.     local pos1, pos2, dimension
  133.     local timeout = os.startTimer( _nTimeout or 2 )
  134.     while true do
  135.         local e, p1, p2, p3, p4, p5 = os.pullEvent()
  136.         if e == "modem_message" then
  137.             -- We received a reply from a modem
  138.             local sSide, sChannel, sReplyChannel, tMessage, nDistance = p1, p2, p3, p4, p5
  139.             if sSide == sModemSide and sChannel == CHANNEL_GPS and sReplyChannel == CHANNEL_GPS and nDistance then
  140.                 -- Received the correct message from the correct modem: use it to determine position
  141.                 if type(tMessage) == "table" and #tMessage == 3 and tonumber(tMessage[1]) and tonumber(tMessage[2]) and tonumber(tMessage[3]) then
  142.                     local tFix = { vPosition = vector.new( tMessage[1], tMessage[2], tMessage[3] ), nDistance = nDistance, dimension = tMessage.dimension }
  143.                     if _bDebug then
  144.                         local text = tFix.nDistance .. " metres from " .. tostring(tFix.vPosition)
  145.                         if type(tFix.dimension) == "number" or type(tFix.dimension) == "string" then text = text .. " " .. tFix.dimension end
  146.                         print(text)
  147.                     end
  148.                     if tFix.nDistance == 0 then
  149.                         pos1, pos2 = tFix.vPosition, nil
  150.                     else
  151.                         table.insert( tFixes, tFix )
  152.                         if #tFixes >= 3 then
  153.                             if not pos1 then
  154.                                 pos1, pos2 = trilaterate( tFixes[1], tFixes[2], tFixes[#tFixes] )
  155.                             else
  156.                                 pos1, pos2 = narrow( pos1, pos2, tFixes[#tFixes] )
  157.                             end
  158.                         end
  159.                     end
  160.                     if pos1 and not pos2 then
  161.                         local dimension_claims = {}
  162.                         for _, fix in pairs(tFixes) do
  163.                             if type(fix.dimension) == "string" or type(fix.dimension) == "number" then
  164.                                 dimension_claims[fix.dimension] = (dimension_claims[fix.dimension] or 0) + 1
  165.                             end
  166.                         end
  167.                         local highest_count = 0
  168.                         for dim, count in pairs(dimension_claims) do
  169.                             if count > highest_count then
  170.                                 highest_count = count
  171.                                 dimension = dim
  172.                             end
  173.                         end
  174.                         break
  175.                     end
  176.                 end
  177.             end
  178.  
  179.         elseif e == "timer" then
  180.             -- We received a timeout
  181.             local timer = p1
  182.             if timer == timeout then
  183.                 break
  184.             end
  185.  
  186.         end
  187.     end
  188.  
  189.     -- Close the channel, if we opened one
  190.     if bCloseChannel then
  191.         modem.close( CHANNEL_GPS )
  192.     end
  193.  
  194.     -- Return the response
  195.     if pos1 and pos2 then
  196.         if _bDebug then
  197.             print( "Ambiguous position" )
  198.             print( "Could be " .. pos1.x .. "," .. pos1.y .. "," .. pos1.z .. " or " .. pos2.x .. "," .. pos2.y .. "," .. pos2.z )
  199.         end
  200.         return nil
  201.     elseif pos1 then
  202.         if _bDebug then
  203.             print( "Position is " .. pos1.x .. "," .. pos1.y .. "," .. pos1.z )
  204.             if dimension then print("Dimension is " .. dimension) end
  205.         end
  206.         return pos1.x, pos1.y, pos1.z, dimension
  207.     else
  208.         if _bDebug then
  209.             print( "Could not determine position" )
  210.         end
  211.         return nil
  212.     end
  213. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement