-- Configurable constants local CHANNEL = 55555 local TOKEN = "LxdIjZPDLL2oZoayb7uR2Lbz7bnnbkxz" local PROMPT = "MineHost" -- Constants local HOST = "HOST" local LEADER = "LEAD" local FOLLOWER = "FOLLOW" local LOG = "LOG" local REQUEST = "REQ" local LUA_PREFIX = "?" -- Constant actions local MOVEBY = "moveby" local MOVETO = "moveto" local STATUS = "status" local POSITION = "position" local HEADING = "heading" local QUARRYCHUNK = "quarry" local TASK = "task" local FAILED = "failed" -- Constant messsage types local LOG = "LOG" local REQUEST = "REQ" -- Constant requests local BLOCKED = "blocked" local ITEM = "item" local FAILED = "failed" running = true local modem = peripheral.find("modem") print("Checking for modem.") if (modem == nil) then error("Modem not found.") end print("Modem found. Opening on channel: " .. CHANNEL) modem.open(CHANNEL) local function captureString(str, seq) if seq == nil then seq = "([^%s]+)" end res = {} for i in string.gmatch(str, seq) do table.insert(res, i) end return res end local function printPrompt() write(PROMPT .. "> ") end local function sendDirective(unit, directive, ...) print("Sending directive \"" .. directive .. "\" to " .. unit .. ".") dir = { token=TOKEN, recipient=unit, sender=HOST, type=REQUEST, request=directive, content=arg } modem.transmit(CHANNEL, CHANNEL, textutils.serialise(dir)) end -- User command loop print("Now listening for user input.") local function listenForCommands() while running do printPrompt() local input = read() if string.find(input, MOVEBY) == 1 then coordsRaw = string.sub(input, string.len(MOVEBY) + 1 + 1) local coords = captureString(coordsRaw, "([+-]?%d+)") sendDirective(LEADER, MOVEBY, coords[1], coords[2], coords[3]) elseif string.find(input, MOVETO) == 1 then coordsRaw = string.sub(input, string.len(MOVETO) + 1 + 1) local coords = captureString(coordsRaw, "([+-]?%d+)") sendDirective(LEADER, MOVETO, coords[1], coords[2], coords[3]) elseif string.find(input, STATUS) == 1 then sendDirective(LEADER, STATUS) elseif string.find(input, POSITION) == 1 then sendDirective(LEADER, POSITION) elseif string.find(input, HEADING) == 1 then sendDirective(LEADER, HEADING) elseif string.find(input, QUARRYCHUNK) == 1 then sendDirective(LEADER, input) end end end -- Status and requests from turtles local function handleRequest(unit, request, content) end local function handleMessage(sender, type, data) if type == LOG then print("[" .. sender .. "]: " .. data["log"] .. ": " .. (data["content"]["n"] >= 1 and tostring(data["content"][1]) or "")) elseif type == REQUEST then handleRequest(sender, data["request"], data["content"]) end end local function listenToMiners() print("Listening to miners.") while running do local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message") print("") local data = textutils.unserialise(serialized) if data["token"] == TOKEN and data["recipient"] == HOST then handleMessage(data["sender"], data["type"], data) else print("Invalid header information. Ignoring.") end printPrompt() end print("No longer listening to miners.") end parallel.waitForAll(listenToMiners, listenForCommands)