ccminingturtle/minehost.lua

122 lines
3.4 KiB
Lua
Raw Normal View History

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"
local POSITION = "position"
local HEADING = "heading"
local QUARRY = "quarry"
2021-05-02 00:39:10 +00:00
-- Constant log
local LOG = "LOG"
local REQUEST = "REQ"
-- Constant requests
local BLOCKED = "blocked"
local ITEM = "item"
local FAILED = "failed"
running = true
2021-05-02 00:39:10 +00:00
local modem = peripheral.find("modem")
if (modem == nil) then
error("Modem not found.")
end
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
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)
2021-05-02 00:39:10 +00:00
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, channel, reply, message, distance = os.pullEvent("modem_message")
print("")
print("Recieved data.")
local token, recipient, sender, type, content = interpretData(message)
if token == TOKEN and recipient == HOST then
handleMessage(sender, content, type)
else
print("Invalid header information. Ignoring.")
end
printPrompt()
end
end
parallel.waitForAll(listenToMiners, listenForCommands)