Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local modem = component.modem
- local event = require("event")
- local robot = require("robot")
- local sides = require("sides")
- local ms = require("movescript")
- local serialization = require("serialization")
- local ic = component.inventory_controller
- local PORT = 10002
- modem.open(PORT)
- local function selectItemByName(item_name, item_data)
- for i=1,16 do
- local stack = ic.getStackInInternalSlot(i)
- if (stack ~= nil and stack.name == item_name and (item_data == nil or stack.damage == item_data)) then
- robot.select(i)
- return true
- end
- end
- return false
- end
- local function selectSafely(item_name, item_data)
- local success = selectItemByName(item_name, item_data)
- while not success do
- print("Cannot find "..item_name.." in inventory. Please add some, and press enter.")
- io.read()
- success = selectItemByName(item_name, item_data)
- end
- end
- local function selectEmptySlot()
- for i = 1, 16 do
- local stack = ic.getStackInInternalSlot(i)
- if (stack == nil) then
- robot.select(i)
- return true
- end
- end
- return false
- end
- local function fetchItemFromChest(item_name, item_damage)
- local size = ic.getInventorySize(sides.front)
- for i = 1, size do
- local stack = ic.getStackInSlot(sides.front, i)
- if (stack ~= nil and stack.name == item_name and (item_damage == nil or stack.damage == item_damage)) then
- selectEmptySlot()
- ic.suckFromSlot(sides.front, i)
- return true
- end
- end
- return false
- end
- local function getFunctionalRobotCount()
- local size = ic.getInventorySize(sides.front)
- local robot_count = 0
- local pickaxe_count = 0
- for i = 1, size do
- local stack = ic.getStackInSlot(sides.front, i)
- if (stack ~= nil) then
- if (stack.name == "opencomputers:robot") then
- robot_count = robot_count + 1
- elseif (stack.name == "tconstruct:pickaxe") then
- pickaxe_count = pickaxe_count + 1
- end
- end
- end
- return math.min(robot_count, pickaxe_count)
- end
- local function fetchRobotAndTool()
- fetchItemFromChest("opencomputers:robot", nil)
- fetchItemFromChest("tconstruct:pickaxe", nil)
- end
- local function initializeRobot(index, length)
- print("Waiting for slave to send ready message...")
- local _, _, sender, port, dist, msg = event.pull("modem_message")
- print("Robot " .. sender .. " is ready. Sending movescript instructions.")
- if (msg == "READY") then
- local script = index .. "FR"
- local data = {
- length = length,
- index = index
- }
- modem.send(sender, port, script, serialization.serialize(data))
- end
- end
- local function deployRobot(index, length)
- fetchRobotAndTool()
- print("Placing robot.")
- selectSafely("opencomputers:robot", nil)
- ms.execute("2RP")
- print("Activating robot.")
- selectEmptySlot()
- ic.equip()
- local success = robot.use(sides.front, true)
- while not success do
- success = robot.use(sides.front, true)
- end
- ic.equip()
- print("Giving robot pickaxe.")
- selectSafely("tconstruct:pickaxe", nil)
- ic.dropIntoSlot(sides.front, 1)
- ms.execute("2R")
- initializeRobot(index, length)
- end
- local function depositRobot()
- ms.execute("2R")
- robot.swing()
- ms.execute("2R")
- for i = 1, 16 do
- robot.select(i)
- robot.drop()
- end
- robot.select(1)
- end
- local function doRow(index, robot_count, length)
- print("Doing row " .. index)
- for i = 1, robot_count do
- print("Deploying robot " .. i)
- deployRobot(((index - 1) * robot_count) + i, length)
- end
- print("All robots deployed, waiting for job completion.")
- for i = 1, robot_count do
- local _, _, sender, port, dist, msg = event.pull("modem_message")
- if (msg == "DONE") then
- print("Depositing robot.")
- depositRobot()
- else
- print("Error: Got msg from robot: " .. msg)
- end
- end
- end
- local robot_count = getFunctionalRobotCount()
- print("Deploying " .. robot_count .. " robots.")
- print("How far to dig?")
- local length = tonumber(io.read())
- print("How many rows to dig? Total width will be a multiple of " .. robot_count)
- local swaths = tonumber(io.read())
- for i = 1, swaths do
- doRow(i, robot_count, length)
- end
Add Comment
Please, Sign In to add comment