2021-05-02 00:39:10 +00:00
|
|
|
-- 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"
|
2021-05-02 21:02:35 +00:00
|
|
|
local POSITION = "position"
|
|
|
|
local HEADING = "heading"
|
|
|
|
local QUARRY = "quarry"
|
2021-05-03 19:16:22 +00:00
|
|
|
local TASK = "task"
|
|
|
|
local FAILED = "failed"
|
|
|
|
-- Constant messsage types
|
2021-05-02 00:39:10 +00:00
|
|
|
local LOG = "LOG"
|
|
|
|
local REQUEST = "REQ"
|
|
|
|
-- Constant requests
|
|
|
|
local BLOCKED = "blocked"
|
|
|
|
local ITEM = "item"
|
|
|
|
local FAILED = "failed"
|
|
|
|
|
2021-05-02 21:02:35 +00:00
|
|
|
running = true
|
2021-05-02 00:39:10 +00:00
|
|
|
local modem = peripheral.find("modem")
|
2021-05-03 18:12:17 +00:00
|
|
|
|
2021-05-03 17:52:11 +00:00
|
|
|
print("Checking for modem.")
|
2021-05-02 00:39:10 +00:00
|
|
|
if (modem == nil) then
|
|
|
|
error("Modem not found.")
|
|
|
|
end
|
2021-05-03 18:34:25 +00:00
|
|
|
|
|
|
|
print("Modem found. Opening on channel: " .. CHANNEL)
|
|
|
|
modem.open(CHANNEL)
|
2021-05-03 17:52:11 +00:00
|
|
|
|
2021-05-02 00:39:10 +00:00
|
|
|
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
|
|
|
|
|
2021-05-03 19:16:22 +00:00
|
|
|
local function sendDirective(unit, directive, ...)
|
2021-05-02 00:39:10 +00:00
|
|
|
print("Sending directive \"" .. directive .. "\" to " .. unit .. ".")
|
2021-05-03 19:16:22 +00:00
|
|
|
dir = {
|
|
|
|
token=TOKEN,
|
|
|
|
recipient=unit,
|
|
|
|
sender=HOST,
|
|
|
|
type=REQUEST,
|
|
|
|
request=directive,
|
|
|
|
content=arg
|
|
|
|
}
|
|
|
|
modem.transmit(CHANNEL, CHANNEL, textutils.serialise(dir))
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- User command loop
|
2021-05-03 17:52:11 +00:00
|
|
|
print("Now listening for user input.")
|
2021-05-02 00:39:10 +00:00
|
|
|
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+)")
|
2021-05-03 19:16:22 +00:00
|
|
|
sendDirective(LEADER, MOVEBY, coords[1], coords[2], coords[3])
|
2021-05-02 00:39:10 +00:00
|
|
|
elseif string.find(input, MOVETO) == 1 then
|
|
|
|
coordsRaw = string.sub(input, string.len(MOVETO) + 1 + 1)
|
|
|
|
local coords = captureString(coordsRaw, "([+-]?%d+)")
|
2021-05-03 19:16:22 +00:00
|
|
|
sendDirective(LEADER, MOVETO, coords[1], coords[2], coords[3])
|
2021-05-02 00:39:10 +00:00
|
|
|
elseif string.find(input, STATUS) == 1 then
|
|
|
|
sendDirective(LEADER, STATUS)
|
2021-05-02 21:02:35 +00:00
|
|
|
elseif string.find(input, POSITION) == 1 then
|
|
|
|
sendDirective(LEADER, POSITION)
|
|
|
|
elseif string.find(input, HEADING) == 1 then
|
|
|
|
sendDirective(LEADER, HEADING)
|
|
|
|
elseif string.find(input, QUARRY) == 1 then
|
|
|
|
sendDirective(LEADER, input)
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Status and requests from turtles
|
|
|
|
|
2021-05-03 19:16:22 +00:00
|
|
|
local function handleRequest(unit, request, content)
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
|
2021-05-03 19:16:22 +00:00
|
|
|
local function handleMessage(sender, type, data)
|
2021-05-02 00:39:10 +00:00
|
|
|
|
|
|
|
if type == LOG then
|
2021-05-03 19:49:44 +00:00
|
|
|
print("[" .. sender .. "]: " .. data["log"] .. ": " .. (data["content"]["n"] >= 1 and tostring(data["content"][1]) or ""))
|
|
|
|
if data["log"] == STATUS then
|
2021-05-03 19:55:06 +00:00
|
|
|
print("Relative position: " .. data["content"][1]["relative_position"]["coordinates"])
|
|
|
|
print("Position: " .. (data["content"][1]["position"]["coordinates"] and data["content"][1]["position"]["coordinates"] or "not positioned."))
|
2021-05-03 19:49:44 +00:00
|
|
|
print("fuel: " .. tostring(data["content"][1]["fuel"]))
|
|
|
|
print("current task: " .. data["content"][1]["task"])
|
2021-05-03 19:50:11 +00:00
|
|
|
end
|
2021-05-02 00:39:10 +00:00
|
|
|
elseif type == REQUEST then
|
2021-05-03 19:23:52 +00:00
|
|
|
handleRequest(sender, data["request"], data["content"])
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function listenToMiners()
|
|
|
|
print("Listening to miners.")
|
|
|
|
while running do
|
2021-05-03 19:16:22 +00:00
|
|
|
local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message")
|
2021-05-02 00:39:10 +00:00
|
|
|
print("")
|
2021-05-03 19:22:05 +00:00
|
|
|
local data = textutils.unserialise(serialized)
|
2021-05-03 19:16:22 +00:00
|
|
|
if data["token"] == TOKEN and data["recipient"] == HOST then
|
|
|
|
handleMessage(data["sender"], data["type"], data)
|
2021-05-02 00:39:10 +00:00
|
|
|
else
|
|
|
|
print("Invalid header information. Ignoring.")
|
|
|
|
end
|
|
|
|
printPrompt()
|
|
|
|
end
|
2021-05-03 18:12:17 +00:00
|
|
|
print("No longer listening to miners.")
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
parallel.waitForAll(listenToMiners, listenForCommands)
|