Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSide = 'left'
- local modem = peripheral.wrap(modemSide)
- function sync_master(channel, alarmTime)
- local timeout = os.startTimer(0.1)
- print("Doing three-way handshake")
- while true do
- modem.transmit(channel, channel, 'SYN')
- local e, p1, _, _, p4, _ = os.pullEvent()
- if e == 'modem_message' then
- if p4 == 'SYN-ACK' then
- modem.transmit(channel, channel, 'ACK')
- modem.transmit(channel, channel, tostring(alarmTime))
- elseif p4 == 'ACK' then
- break
- end
- elseif e == 'timer' and p1 == timeout then
- modem.transmit(channel, channel, 'SYN')
- timeout = os.startTimer(0.1)
- alarmTime = os.time() + 0.025
- end
- end
- return alarmTime
- end
- function sync_drone()
- local gotAck = false
- local syn = false
- print("Listening for three-way handshake")
- while true do
- local _, _, p3, _, p4, _ = os.pullEvent('modem_message')
- if p4 == 'SYN' then
- modem.transmit(p3, p3, 'SYN-ACK')
- syn = true
- elseif p4 == 'ACK' and syn and not gotAck then
- gotAck = true
- syn = false
- elseif tonumber(p4) and gotAck then
- modem.transmit(p3, p3, 'ACK')
- return tonumber(p4)
- end
- end
- end
- function do_timer(dur, alarm)
- os.setAlarm(alarm)
- os.pullEvent('alarm')
- local curr = 0
- local timer = os.startTimer(1)
- while true do
- print('Launch in '..(dur - curr)..' seconds...')
- local _, p = os.pullEvent('timer')
- if p == timer then
- timer = os.startTimer(1)
- curr = curr + 1
- if curr == dur then
- return false
- end
- end
- end
- end
- -- call this with the communication channel and how long you want the countdown to be.
- function startSyncedTimer(channel, dur)
- -- if the channel is not open, open it...
- if not modem.isOpen(channel) then
- modem.open(channel)
- end
- -- figure out a time that both computers should start the timer, based off the Minecraft time
- local alarmTime = os.time() + 0.1
- -- send this time out to the world
- alarmTime = sync_master(channel, alarmTime)
- -- call the timer function.
- -- Param 1: The duration of the timer, i.e. the countdown start time
- -- Param 2: when to start the timer
- local ok = do_timer(dur, alarmTime)
- -- if the timer finished with a boom, say boom (when you have the override make sure that you return true from the do_timer function so as to not make things go boom, then in this function make sure that the missile is launched)
- if not ok then
- print "BOOM!"
- end
- end
- -- call this with the communication channel and how long you want the countdown to be.
- -- the security in this function could be much better
- function listenForSync(channel, dur)
- -- if the channel is not open, open it...
- if not modem.isOpen(channel) then
- modem.open(channel)
- end
- -- create our alarm time variable and wait for its contents
- local alarmTime = sync_drone()
- -- call the timer function.
- -- Param 1: The duration of the timer, i.e. the countdown start time
- -- Param 2: when to start the timer
- local ok = do_timer(dur, alarmTime)
- -- if the timer finished with a boom, say boom (when you have the override make sure that you return true from the do_timer function so as to not make things go boom, then in this function make sure that the missile is launched)
- if not ok then
- print "LAUNCH!"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement