Flaghacker

TurtletaskExecutor

Jul 23rd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.89 KB | None | 0 0
  1. local taskQueue = {}
  2.  
  3. local function taskToString (task)
  4.   return task.count .. "*" .. task.cmd
  5. end
  6.  
  7. local function addTask (task)
  8.   table.insert (taskQueue, task)
  9.   os.queueEvent ("taskAdd")
  10. end
  11.  
  12. local function pullTask ()
  13.   local task = table.remove (taskQueue, 1)
  14.  
  15.   while task == nil do
  16.     os.pullEvent ("taskAdd")
  17.     task = table.remove (taskQueue, 1)
  18.   end
  19.  
  20.   return task
  21. end
  22.  
  23. local function receiver ()
  24.   while true do
  25.     local id, msg, protocol = rednet.receive ()
  26.     local task = textutils.unserialise (msg) or msg
  27.    
  28.     addTask (task)
  29.     print ("received " .. taskToString (task)
  30.   end
  31. end
  32.  
  33. local function mover ()
  34.   while true do
  35.     local task = pullTask ()
  36.    
  37.     for i = 1, task.count do
  38.       turtle [task.cmd]()
  39.     end
  40.    
  41.     print ("moved" .. taskToString (task))
  42.   end
  43. end
  44.  
  45. rednet.open ("back")
  46. parallell.waitForAny (receiver, mover)
Advertisement
Add Comment
Please, Sign In to add comment