ccminingturtle/minehost.lua

138 lines
4.0 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"
2021-05-07 22:10:41 +00:00
local QUARRY = "quarry"
local TASK = "task"
local FAILED = "failed"
2021-05-07 21:00:38 +00:00
local MOVEHERE = "movehere"
-- 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"
running = true
2021-05-02 00:39:10 +00:00
local modem = peripheral.find("modem")
2021-05-03 18:12:17 +00:00
print("Checking for modem.")
2021-05-02 00:39:10 +00:00
if (modem == nil) then
error("Modem not found.")
end
print("Modem found. Opening on channel: " .. CHANNEL)
modem.open(CHANNEL)
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
local function sendDirective(unit, directive, ...)
2021-05-02 00:39:10 +00:00
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))
2021-05-02 00:39:10 +00:00
end
-- User command loop
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+)")
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+)")
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)
elseif string.find(input, POSITION) == 1 then
sendDirective(LEADER, POSITION)
elseif string.find(input, HEADING) == 1 then
sendDirective(LEADER, HEADING)
2021-05-07 21:00:38 +00:00
elseif string.find(input, MOVEHERE) == 1 then
local x, y, z = gps.locate()
2021-05-07 21:00:38 +00:00
y = y + 1
sendDirective(LEADER, MOVEHERE, x, y, z)
2021-05-07 22:10:41 +00:00
elseif string.find(input, QUARRY) == 1 then
deltasRaw = string.sub(input, string.len(QUARRY) + 1 + 1)
2021-05-07 22:22:54 +00:00
local deltas = captureString(deltasRaw, "([+-]?%d+)")
2021-05-07 22:10:41 +00:00
sendDirective(LEADER, QUARRY, deltas[1], deltas[2], deltas[3])
2021-05-02 00:39:10 +00:00
end
end
end
-- Status and requests from turtles
local function handleRequest(unit, request, content)
2021-05-02 00:39:10 +00:00
end
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 ""))
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
local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message")
2021-05-02 00:39:10 +00:00
print("")
local data = nil
if not pcall(function() data = textutils.unserialise(serialized) end) then
print("Message already table, not deserializing.")
data = serialized
end
2021-05-07 21:20:52 +00:00
if (data == nil) then print("data nil.") end
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)