HandieAndy

master_init.lua

May 12th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. local component = require("component")
  2. local modem = component.modem
  3. local event = require("event")
  4. local robot = require("robot")
  5. local sides = require("sides")
  6. local ms = require("movescript")
  7. local serialization = require("serialization")
  8. local ic = component.inventory_controller
  9.  
  10. local PORT = 10002
  11.  
  12. modem.open(PORT)
  13.  
  14. local function selectItemByName(item_name, item_data)
  15.     for i=1,16 do
  16.         local stack = ic.getStackInInternalSlot(i)
  17.         if (stack ~= nil and stack.name == item_name and (item_data == nil or stack.damage == item_data)) then
  18.             robot.select(i)
  19.             return true
  20.         end
  21.     end
  22.     return false
  23. end
  24.  
  25. local function selectSafely(item_name, item_data)
  26.     local success = selectItemByName(item_name, item_data)
  27.     while not success do
  28.         print("Cannot find "..item_name.." in inventory. Please add some, and press enter.")
  29.         io.read()
  30.         success = selectItemByName(item_name, item_data)
  31.     end
  32. end
  33.  
  34. local function selectEmptySlot()
  35.     for i = 1, 16 do
  36.         local stack = ic.getStackInInternalSlot(i)
  37.         if (stack == nil) then
  38.             robot.select(i)
  39.             return true
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. local function fetchItemFromChest(item_name, item_damage)
  46.     local size = ic.getInventorySize(sides.front)
  47.     for i = 1, size do
  48.         local stack = ic.getStackInSlot(sides.front, i)
  49.         if (stack ~= nil and stack.name == item_name and (item_damage == nil or stack.damage == item_damage)) then
  50.             selectEmptySlot()
  51.             ic.suckFromSlot(sides.front, i)
  52.             return true
  53.         end
  54.     end
  55.     return false
  56. end
  57.  
  58. local function getFunctionalRobotCount()
  59.     local size = ic.getInventorySize(sides.front)
  60.     local robot_count = 0
  61.     local pickaxe_count = 0
  62.     for i = 1, size do
  63.         local stack = ic.getStackInSlot(sides.front, i)
  64.         if (stack ~= nil) then
  65.             if (stack.name == "opencomputers:robot") then
  66.                 robot_count = robot_count + 1
  67.             elseif (stack.name == "tconstruct:pickaxe") then
  68.                 pickaxe_count = pickaxe_count + 1
  69.             end
  70.         end
  71.     end
  72.     return math.min(robot_count, pickaxe_count)
  73. end
  74.  
  75. local function fetchRobotAndTool()
  76.     fetchItemFromChest("opencomputers:robot", nil)
  77.     fetchItemFromChest("tconstruct:pickaxe", nil)
  78. end
  79.  
  80. local function initializeRobot(index, length)
  81.     print("Waiting for slave to send ready message...")
  82.     local _, _, sender, port, dist, msg = event.pull("modem_message")
  83.     print("Robot " .. sender .. " is ready. Sending movescript instructions.")
  84.     if (msg == "READY") then
  85.         local script = index .. "FR"
  86.         local data = {
  87.             length = length,
  88.             index = index
  89.         }
  90.         modem.send(sender, port, script, serialization.serialize(data))
  91.     end
  92. end
  93.  
  94. local function deployRobot(index, length)
  95.     fetchRobotAndTool()
  96.  
  97.     print("Placing robot.")
  98.     selectSafely("opencomputers:robot", nil)
  99.     ms.execute("2RP")
  100.  
  101.     print("Activating robot.")
  102.     selectEmptySlot()
  103.     ic.equip()
  104.     local success = robot.use(sides.front, true)
  105.     while not success do
  106.         success = robot.use(sides.front, true)
  107.     end
  108.     ic.equip()
  109.  
  110.     print("Giving robot pickaxe.")
  111.     selectSafely("tconstruct:pickaxe", nil)
  112.     ic.dropIntoSlot(sides.front, 1)
  113.     ms.execute("2R")
  114.  
  115.     initializeRobot(index, length)
  116. end
  117.  
  118. local function depositRobot()
  119.     ms.execute("2R")
  120.     robot.swing()
  121.     ms.execute("2R")
  122.     for i = 1, 16 do
  123.         robot.select(i)
  124.         robot.drop()
  125.     end
  126.     robot.select(1)
  127. end
  128.  
  129. local function doRow(index, robot_count, length)
  130.     print("Doing row " .. index)
  131.     for i = 1, robot_count do
  132.         print("Deploying robot " .. i)
  133.         deployRobot(((index - 1) * robot_count) + i, length)
  134.     end
  135.  
  136.     print("All robots deployed, waiting for job completion.")
  137.  
  138.     for i = 1, robot_count do
  139.         local _, _, sender, port, dist, msg = event.pull("modem_message")
  140.         if (msg == "DONE") then
  141.             print("Depositing robot.")
  142.             depositRobot()
  143.         else
  144.             print("Error: Got msg from robot: " .. msg)
  145.         end
  146.     end
  147. end
  148.  
  149. local robot_count = getFunctionalRobotCount()
  150. print("Deploying " .. robot_count .. " robots.")
  151.  
  152. print("How far to dig?")
  153. local length = tonumber(io.read())
  154.  
  155. print("How many rows to dig? Total width will be a multiple of " .. robot_count)
  156. local swaths = tonumber(io.read())
  157.  
  158. for i = 1, swaths do
  159.     doRow(i, robot_count, length)
  160. end
Add Comment
Please, Sign In to add comment