Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = 0.1
- --Handle the arguments
- local tArgs = { ... }
- local queen_broadcast = "This is the queen"
- local worker_response = "Worker reporting"
- --This nice function for getting the modem peripheral taken from GPS
- local function open()
- local bOpen, sFreeSide = false, nil
- for n,sSide in pairs(rs.getSides()) do
- if peripheral.getType( sSide ) == "modem" then
- sFreeSide = sSide
- if rednet.isOpen( sSide ) then
- bOpen = true
- break
- end
- end
- end
- if not bOpen then
- if sFreeSide then
- print( "No modem active. Opening "..sFreeSide.." modem" )
- rednet.open( sFreeSide )
- return true
- else
- print( "No modem attached" )
- return false
- end
- end
- return true
- end
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." blocks." )
- end
- end
- local function tryDig()
- while turtle.dig() do
- collect()
- sleep(0.5)
- if not turtle.detect() then
- return true
- end
- end
- return not turtle.detect()
- end
- local function tryDigUp()
- while turtle.digUp() do
- collect()
- sleep(0.5)
- if not turtle.detectUp() then
- return true
- end
- end
- return not turtle.detectUp()
- end
- local function tunnel(length)
- --Announce the tunneling function
- print("Tunneling for "..tostring(length).." meters")
- length = tonumber(length)
- --Some local variables
- local collected = 0
- local depth = 0
- --This tunnels in a 2hx3w pattern
- for n=1,length do
- tryDigUp()
- turtle.turnLeft()
- tryDig()
- turtle.up()
- tryDig()
- turtle.turnRight()
- turtle.turnRight()
- tryDig()
- turtle.down()
- tryDig()
- turtle.turnLeft()
- --This steps forward in depth now
- if n<length then
- depth = depth + 1
- tryDig()
- if not turtle.forward() then
- print( "Aborting Tunnel." )
- break
- end
- else
- print( "Tunnel complete." )
- end
- end
- -- Return to where we started
- print( "Returning to start..." )
- turtle.turnLeft()
- turtle.turnLeft()
- while depth > 0 do
- if turtle.forward() then
- depth = depth - 1
- else
- turtle.dig()
- end
- end
- worker()
- end
- --The Queen acts as the issuer of orders. Most often a console.
- function queen( q_command, ... )
- --Broadcast if we can open a modem
- if open() then
- print("Broadcasting to idle workers")
- rednet.broadcast(queen_broadcast)
- else
- print("Unable to broadcast to workers, quitting")
- return false
- end
- --A local table of workers
- local workers = {}
- --Listen for responses from workers for about 5 seconds
- t = os.clock()
- repeat
- sid, message, dist = rednet.receive(1)
- if message == worker_response then
- table.insert(workers, sid)
- end
- until (os.clock() - t) > 5
- --Tell how many workers contacted queen
- print(tostring(#workers).." workers reported in")
- --Now with the list of workers, send them commands
- for i=1,#workers do
- --First the command
- rednet.send(workers[i], q_command)
- --Tell the worker to listen for the correct number of arguments
- rednet.send(workers[i], tostring(#arg))
- --Then send all the arguments
- for j,v in ipairs(arg) do
- rednet.send(workers[i], tostring(v))
- end
- end
- end
- --The Worker accepts orders from the queen. Should be a turtle.
- function worker()
- print("Turtle going online as a worker")
- print("Running version: "..version)
- rednet.open("right") --Modem is always "right" on turtle
- --Wait for the correct queen broadcast to identify the queen
- repeat
- sid, message, dist = rednet.receive()
- until message == queen_broadcast
- queen_id = sid
- print("Received message identifying the queen as ID: "..queen_id)
- --Once we have the queen's ID, report back
- rednet.send(queen_id, worker_response)
- print("Reported back to the queen")
- --The first message is the "command"
- repeat
- sid, message, dist = rednet.receive()
- until sid == queen_id
- sid = nil
- command = message
- print("Command: "..command)
- --The second message is the number of arguments
- repeat
- sid, message, dist = rednet.receive()
- until sid == queen_id
- sid = nil
- arg_num = tonumber(message)
- print("Argument number: "..message)
- --Get all of the arguments
- local qArgs = {}
- while arg_num > 0 do
- sid, arg_message, dist = rednet.receive()
- if sid == queen_id then
- table.insert(qArgs, arg_message)
- arg_num = arg_num - 1
- end
- end
- commandlist = {["tunnel"] = tunnel}
- --Execute the proper command.
- commandlist[command](unpack(qArgs))
- end
Advertisement
Add Comment
Please, Sign In to add comment