Guest User

swarm.lua

a guest
Jul 27th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local version = 0.1
  3.  
  4. --Handle the arguments
  5. local tArgs = { ... }
  6.  
  7. local queen_broadcast = "This is the queen"
  8. local worker_response = "Worker reporting"
  9.  
  10. --This nice function for getting the modem peripheral taken from GPS
  11. local function open()
  12.     local bOpen, sFreeSide = false, nil
  13.     for n,sSide in pairs(rs.getSides()) do  
  14.         if peripheral.getType( sSide ) == "modem" then
  15.             sFreeSide = sSide
  16.             if rednet.isOpen( sSide ) then
  17.                 bOpen = true
  18.                 break
  19.             end
  20.         end
  21.     end
  22.    
  23.     if not bOpen then
  24.         if sFreeSide then
  25.             print( "No modem active. Opening "..sFreeSide.." modem" )
  26.             rednet.open( sFreeSide )
  27.             return true
  28.         else
  29.             print( "No modem attached" )
  30.             return false
  31.         end
  32.     end
  33.     return true
  34. end
  35.  
  36. local function collect()
  37.     collected = collected + 1
  38.     if math.fmod(collected, 25) == 0 then
  39.         print( "Mined "..collected.." blocks." )
  40.     end
  41. end
  42.  
  43.  
  44. local function tryDig()
  45.     while turtle.dig() do
  46.         collect()
  47.         sleep(0.5)
  48.         if not turtle.detect() then
  49.             return true
  50.         end
  51.     end
  52.     return not turtle.detect()
  53. end
  54.  
  55. local function tryDigUp()
  56.     while turtle.digUp() do
  57.         collect()
  58.         sleep(0.5)
  59.         if not turtle.detectUp() then
  60.             return true
  61.         end
  62.     end
  63.     return not turtle.detectUp()
  64. end
  65.  
  66. local function tunnel(length)
  67.     --Announce the tunneling function
  68.     print("Tunneling for "..tostring(length).." meters")
  69.     length = tonumber(length)
  70.     --Some local variables
  71.     local collected = 0
  72.     local depth = 0
  73.    
  74.     --This tunnels in a 2hx3w pattern
  75.     for n=1,length do
  76.         tryDigUp()
  77.         turtle.turnLeft()
  78.         tryDig()
  79.         turtle.up()
  80.         tryDig()
  81.         turtle.turnRight()
  82.         turtle.turnRight()
  83.         tryDig()
  84.         turtle.down()
  85.         tryDig()
  86.         turtle.turnLeft()
  87.         --This steps forward in depth now
  88.         if n<length then
  89.         depth = depth + 1
  90.             tryDig()
  91.             if not turtle.forward() then
  92.                 print( "Aborting Tunnel." )
  93.                 break
  94.             end
  95.         else
  96.             print( "Tunnel complete." )
  97.         end
  98.     end
  99.    
  100.     -- Return to where we started
  101.     print( "Returning to start..." )
  102.     turtle.turnLeft()
  103.     turtle.turnLeft()
  104.     while depth > 0 do
  105.         if turtle.forward() then
  106.             depth = depth - 1
  107.         else
  108.             turtle.dig()
  109.         end
  110.     end
  111.     worker()
  112. end
  113.  
  114.  
  115.  
  116. --The Queen acts as the issuer of orders. Most often a console.
  117. function queen( q_command, ... )
  118.     --Broadcast if we can open a modem
  119.     if open() then
  120.         print("Broadcasting to idle workers")
  121.         rednet.broadcast(queen_broadcast)
  122.     else
  123.         print("Unable to broadcast to workers, quitting")
  124.         return false
  125.     end
  126.    
  127.     --A local table of workers
  128.     local workers = {}
  129.     --Listen for responses from workers for about 5 seconds
  130.     t = os.clock()
  131.     repeat
  132.         sid, message, dist = rednet.receive(1)
  133.         if message == worker_response then
  134.             table.insert(workers, sid)
  135.         end
  136.     until (os.clock() - t) > 5
  137.    
  138.     --Tell how many workers contacted queen
  139.     print(tostring(#workers).." workers reported in")
  140.    
  141.     --Now with the list of workers, send them commands
  142.     for i=1,#workers do
  143.         --First the command
  144.         rednet.send(workers[i], q_command)
  145.         --Tell the worker to listen for the correct number of arguments
  146.         rednet.send(workers[i], tostring(#arg))
  147.         --Then send all the arguments
  148.         for j,v in ipairs(arg) do
  149.             rednet.send(workers[i], tostring(v))
  150.         end
  151.     end
  152. end
  153.  
  154. --The Worker accepts orders from the queen. Should be a turtle.
  155. function worker()
  156.     print("Turtle going online as a worker")
  157.     print("Running version: "..version)
  158.     rednet.open("right") --Modem is always "right" on turtle
  159.  
  160.     --Wait for the correct queen broadcast to identify the queen
  161.     repeat
  162.         sid, message, dist = rednet.receive()
  163.     until message == queen_broadcast
  164.     queen_id = sid
  165.     print("Received message identifying the queen as ID: "..queen_id)
  166.    
  167.     --Once we have the queen's ID, report back
  168.     rednet.send(queen_id, worker_response)
  169.     print("Reported back to the queen")
  170.    
  171.     --The first message is the "command"
  172.     repeat
  173.         sid, message, dist = rednet.receive()
  174.     until sid == queen_id
  175.     sid = nil
  176.     command = message
  177.     print("Command: "..command)
  178.    
  179.     --The second message is the number of arguments
  180.     repeat
  181.         sid, message, dist = rednet.receive()
  182.     until sid == queen_id
  183.     sid = nil
  184.     arg_num = tonumber(message)
  185.     print("Argument number: "..message)
  186.    
  187.     --Get all of the arguments
  188.     local qArgs = {}
  189.     while arg_num > 0 do
  190.         sid, arg_message, dist = rednet.receive()
  191.         if sid == queen_id then
  192.             table.insert(qArgs, arg_message)
  193.             arg_num = arg_num - 1
  194.         end
  195.     end
  196.    
  197.     commandlist = {["tunnel"] = tunnel}
  198.    
  199.     --Execute the proper command.
  200.     commandlist[command](unpack(qArgs))
  201.    
  202. end
Advertisement
Add Comment
Please, Sign In to add comment