KaoSDlanor

Turtle squad member

Sep 25th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. rednet.open('right')
  2. local tQueue={}
  3. local intDone=true
  4.  
  5. local function register()
  6.     while true do
  7.         local input={os.pullEvent()}
  8.         if input[1]=='rednet_message' and input[3]=='gather' and (not leader) then
  9.             rednet.send(input[2],'join')
  10.             os.startTimer(1)
  11.             while true do
  12.                 local evt={os.pullEvent()}
  13.                 if evt[1]=='timer' then
  14.                     break
  15.                 elseif evt[1]=='rednet_message' and evt[2]==input[2] and evt[3]=='accepted' then
  16.                     leader=input[2]
  17.                     print('registered to '..leader)
  18.                     break
  19.                 end
  20.             end
  21.         end
  22.     end
  23. end
  24.  
  25. local function listen()
  26.     while true do
  27.         local input={rednet.receive()}
  28.         if type(textutils.unserialize(input[2]))=='table' then
  29.             input[2]=textutils.unserialize(input[2])
  30.             if leader and input[1]==leader and input[2][1]==os.computerID() then
  31.                 print(input[2][2])
  32.                 tQueue[#tQueue+1]=input[2][2]
  33.             end
  34.         end
  35.     end
  36. end
  37.  
  38. local function exec()
  39.     while true do
  40.         if tQueue[1] then
  41.             if intDone==true then
  42.                 intDone=false
  43.                 rednet.send(leader,'busy')
  44.             end
  45.             if fs.exists('running') then fs.delete('running') end
  46.             local oFile=io.open('running','w')
  47.             oFile:write(table.remove(tQueue,1))
  48.             oFile:close()
  49.             shell.run('running')
  50.         else
  51.             sleep(0.5)
  52.         end
  53.     end
  54. end
  55.  
  56. local tThreads={coroutine.create(register);
  57. coroutine.create(listen);
  58. coroutine.create(exec)}
  59. local tEvents={}
  60.  
  61. while true do
  62.     for k,v in pairs(tThreads) do
  63.         coroutine.resume(v,unpack(tEvents))
  64.     end
  65.     tEvents=nil
  66.     tEvents={os.pullEvent()}
  67. end
Advertisement
Add Comment
Please, Sign In to add comment