Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[Modem Distance API
- Sylvyrfysh - All Rights Reserved
- Unauthorized copying, and/or redistribution of this file, via any medium without express written permission of Sylvyrfysh (Nicholas Johnson) is strictly prohibited
- Proprietary and confidential
- Written by Nicholas Johnson <johnnic431@gmail.com>, June 30 2015
- ]]
- local modem
- local values={}
- wireless={
- pingChannel=9150
- }
- function init(mod)
- modem=mod;
- modem.open(wireless.pingChannel)
- end
- local function stringSplit(st)
- local str={}
- for i in string.gmatch(st,"%S+") do
- str[#str+1]=i;
- end
- return str
- end
- function requestPing(range,wantedString)
- modem.transmit(wireless.pingChannel,wireless.pingChannel,"Ping "..wantedString)
- local respondedInRange={}
- local tID=os.startTimer(1)
- while true do
- e={os.pullEvent()}
- if e[1]=="modem_message" then
- local str=stringSplit(e[5])
- if str[1]=="Pong" and e[6]<=range then
- if #str>=2 then
- respondedInRange[#respondedInRange+1]=str[2]
- end
- end
- elseif e[1]=="timer" and e[2]==tID then
- break
- end
- end
- return respondedInRange;
- end
- function addPongValue(key,value)
- values[key]=value
- end
- function respondPongCoroutine()
- while true do
- e={os.pullEvent("modem_message")}
- local str=stringSplit(e[5])
- if str[1]=="Ping" and e[6]<=range then
- if #str>=2 then
- if values[str[2]] then
- modem.transmit(wireless.pingChannel,wireless.pingChannel,"Pong "..values[str[2]])
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement