Advertisement
johnnic431

mDistAPI

Jul 1st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1. --[[Modem Distance API
  2.  
  3. Sylvyrfysh - All Rights Reserved
  4. Unauthorized copying, and/or redistribution of this file, via any medium without express written permission of Sylvyrfysh (Nicholas Johnson) is strictly prohibited
  5. Proprietary and confidential
  6. Written by Nicholas Johnson <johnnic431@gmail.com>, June 30 2015
  7.  
  8. ]]
  9.  
  10. local modem
  11.  
  12. local values={}
  13.  
  14. wireless={
  15.     pingChannel=9150
  16. }
  17.  
  18. function init(mod)
  19.     modem=mod;
  20.     modem.open(wireless.pingChannel)
  21. end
  22.  
  23. local function stringSplit(st)
  24.     local str={}
  25.     for i in string.gmatch(st,"%S+") do
  26.         str[#str+1]=i;
  27.     end
  28.     return str
  29. end
  30.  
  31. function requestPing(range,wantedString)
  32.     modem.transmit(wireless.pingChannel,wireless.pingChannel,"Ping "..wantedString)
  33.     local respondedInRange={}
  34.     local tID=os.startTimer(1)
  35.     while true do
  36.         e={os.pullEvent()}
  37.         if e[1]=="modem_message" then
  38.             local str=stringSplit(e[5])
  39.             if str[1]=="Pong" and e[6]<=range then
  40.                 if #str>=2 then
  41.                     respondedInRange[#respondedInRange+1]=str[2]
  42.                 end
  43.             end
  44.         elseif e[1]=="timer" and e[2]==tID then
  45.             break
  46.         end
  47.     end
  48.     return respondedInRange;
  49. end
  50.  
  51. function addPongValue(key,value)
  52.     values[key]=value
  53. end
  54.  
  55. function respondPongCoroutine()
  56.     while true do
  57.         e={os.pullEvent("modem_message")}
  58.         local str=stringSplit(e[5])
  59.         if str[1]=="Ping" and e[6]<=range then
  60.             if #str>=2 then
  61.                 if values[str[2]] then
  62.                     modem.transmit(wireless.pingChannel,wireless.pingChannel,"Pong "..values[str[2]])
  63.                 end
  64.             end
  65.         end
  66.     end
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement