Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local SERVER_PORT = 568
- local CLIENT_PORT = 342
- local TURTLE_SLOT = 1
- local INVENTORY_SLOT = 2
- local FUEL_SLOT = 3
- local TURTLE_SUPPLY = 4
- local INVENTORY_SUPPLY = 5
- local FUEL_SUPPLY = 6
- --The number of slots in the turtle
- local SLOT_COUNT = 16
- --find the size of our segmentation
- --use given command line arugment or defualt to 8
- local segmentation = 8
- if (#arg == 1) then
- segmentation = tonumber(arg[1])
- elseif (#arg == 0) then
- print(string.format("Using default segmentation size of %d", segmentation))
- else
- print('Too many args given...')
- exit(1)
- end
- --connect and open the modem on the turtle
- local modem = peripheral.wrap("left")
- modem.open(SERVER_PORT)
- --these are the vectors for mining dimensions
- local target = vector.new()
- local size = vector.new()
- local finish = vector.new()
- --self-explanatory
- --This may need more work?
- --We can assume there is at leat one chest in the slot
- function deployInventoryChest()
- --give the turtle a Inventory chest
- turtle.drop(INVENTORY_SLOT)
- --make sure the slot for inventory chests
- --isn't empty or full
- if turtle.getItemCount(INVENTORY_SLOT) == 0 then
- turtle.select(INVENTORY_SUPPLY)
- turtle.placeUp()
- turtle.suckUp()
- turtle.digUp()
- elseif turtle.getItemCount(INVENTORY_SLOT) == 64 then
- turtle.select(INVENTORY_SLOT)
- turtle.placeUp()
- turtle.dropUp(32)
- turtle.digUp()
- end
- end
- --self-explanatory
- --This may need more work?
- --We can assume there is at leat one chest in the slot
- function deployFuelChest()
- turtle.drop(FUEL_SLOT)
- --make sure the slot for fuel chests
- --isn't empty or full
- if turtle.getItemCount(FUEL_SLOT) == 0 then
- turtle.select(FUEL_SUPPLY)
- turtle.placeUp()
- turtle.suckUp()
- turtle.digUp()
- elseif turtle.getItemCount(INVENTORY_SLOT) == 64 then
- turtle.select(INVENTORY_SLOT)
- turtle.placeUp()
- turtle.select(FUEL_SLOT)
- turtle.dropUp(32)
- turtle.digUp()
- end
- end
- --split a string message into a table of strings
- --inputstr is the sting to be separated
- --sep is the string that will be found between sections
- function split (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- --the data is the
- --put the string data into the coords array
- function parseParams(data)
- coords = {}
- params = split(data, " ")
- coords[1] = vector.new(params[1], params[2], params[3])
- coords[2] = vector.new(params[4], params[5], params[6])
- return (coords)
- end
- --deploy a turtle
- function deploy(startCoords, quarySize, endCoords)
- --Place turtle from inventory
- turtle.select(1)
- while(turtle.detect()) do
- os.sleep(1)
- end
- --Place and turn on turtle
- turtle.place()
- peripheral.call("front", "turnOn")
- --Wait for client to send ping
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- if(msg ~= "CLIENT_DEPLOYED") then
- print("No client deploy message, exitting...")
- os.exit()
- end
- deployInventoryChest()
- deployFuelChest()
- -- Client is deployed
- modem.transmit(CLIENT_PORT,
- SERVER_PORT,
- string.format("%d %d %d %d %d %d %d %d %d",
- startCoords.x, startCoords.y, startCoords.z,
- quarySize.x, quarySize.y, quarySize.z,
- endCoords.x, endCoords.y, endCoords.z
- ))
- end
- while (true) do
- -- Wait for phone
- print("Waiting for target signal...")
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- --get the target and size
- data = parseParams(msg)
- target = data[1]
- size = data[2]
- --finish right above the server
- finish = vector.new(gps.locate())
- finish.y = finish.y + 1
- --print the server quarry request
- print(string.format( "RECEIVED QUARY REQUEST AT: %d %d %d", target.x, target.y, target.z))
- --deploy turtles
- for i = 1, size.x, segmentation do
- --x dig length is the smaller of
- --segmentation and size.x - i
- --This will allow variable sizes of mining areas on the edges
- --This means segmentation won't have to evenly divide size.x
- local xLen = segmentation
- if ((size.x - i) < xLen) then
- xLen = (size.x - i)
- end
- for j = 1, size.z, segmentation do
- --z dig length is the smaller of
- --segmentation and size.z - j
- --This will allow variable sizes of mining areas on the edges
- --This means segmentation won't have to evenly divide size.z
- local zLen = segmentation
- if ((size.z - j) < zLen) then
- zLen = (size.z - j)
- end
- --start cords of the dig are i, j
- --size of dig is xLen by zLen
- local scaledSize = vector.new(xLen, size.y, zLen)
- local offsetTarget = vector.new(target.x + i, target.y, target.z + j)
- deploy(offsetTarget, scaledSize, finish)
- print(string.format( "Deploying to; %d %d %d %d %d", target.x + i, target.y, target.z + j, xLen, zLen))
- os.sleep(2)
- end
- end
- -- All bots deployed, wait for last bot finished signal
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- turtle.digUp()
- end
Add Comment
Please, Sign In to add comment