ccminingturtle/minehost.lua
Harrison Deng 441702556a Now opens modem.
Not sure why it wasn't being opened before.
2021-05-03 13:34:25 -05:00

128 lines
3.6 KiB
Lua

-- 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 QUARRY = "quarry"
-- Constant log
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 .. ".")
modem.transmit(CHANNEL, CHANNEL, TOKEN .. ":" .. unit .. ":" .. HOST .. ":" .. REQUEST .. ":" .. directive)
end
local function interpretData(data)
res = captureString(data, "([^%\][^%:]+)")
if res ~= nil then
res[5] = table.concat(res, ":", 5)
return res[1], res[2], res[3], res[4], res[5]
end
return nil
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, QUARRY) == 1 then
sendDirective(LEADER, input)
end
end
end
-- Status and requests from turtles
local function handleRequest(unit, request)
end
local function handleMessage(unit, str, type)
if type == LOG then
local log = string.sub(str, string.len(LOG) + 1 + 1)
print("[" .. unit .. "]: " .. str)
elseif type == REQUEST then
local request = string.sub(str, string.len(REQUEST) + 1 + 1)
handleRequest(unit, request)
end
end
local function listenToMiners()
print("Listening to miners.")
while running do
local event, side, sendChannel, replyChannel, data, distance = os.pullEvent("modem_message")
print("")
local token, recipient, sender, type, content = interpretData(data)
if token == TOKEN and recipient == HOST then
handleMessage(sender, content, type)
else
print("Invalid header information. Ignoring.")
end
printPrompt()
end
print("No longer listening to miners.")
end
parallel.waitForAll(listenToMiners, listenForCommands)