Initial attempt at changing to a completely serialization based com system.

This commit is contained in:
Harrison Deng 2021-05-03 14:16:22 -05:00
parent 441702556a
commit 40ff22f327
2 changed files with 92 additions and 85 deletions

View File

@ -17,7 +17,9 @@ local STATUS = "status"
local POSITION = "position" local POSITION = "position"
local HEADING = "heading" local HEADING = "heading"
local QUARRY = "quarry" local QUARRY = "quarry"
-- Constant log local TASK = "task"
local FAILED = "failed"
-- Constant messsage types
local LOG = "LOG" local LOG = "LOG"
local REQUEST = "REQ" local REQUEST = "REQ"
-- Constant requests -- Constant requests
@ -50,18 +52,17 @@ local function printPrompt()
write(PROMPT .. "> ") write(PROMPT .. "> ")
end end
local function sendDirective(unit, directive) local function sendDirective(unit, directive, ...)
print("Sending directive \"" .. directive .. "\" to " .. unit .. ".") print("Sending directive \"" .. directive .. "\" to " .. unit .. ".")
modem.transmit(CHANNEL, CHANNEL, TOKEN .. ":" .. unit .. ":" .. HOST .. ":" .. REQUEST .. ":" .. directive) dir = {
end token=TOKEN,
recipient=unit,
local function interpretData(data) sender=HOST,
res = captureString(data, "([^%\][^%:]+)") type=REQUEST,
if res ~= nil then request=directive,
res[5] = table.concat(res, ":", 5) content=arg
return res[1], res[2], res[3], res[4], res[5] }
end modem.transmit(CHANNEL, CHANNEL, textutils.serialise(dir))
return nil
end end
-- User command loop -- User command loop
@ -73,11 +74,11 @@ local function listenForCommands()
if string.find(input, MOVEBY) == 1 then if string.find(input, MOVEBY) == 1 then
coordsRaw = string.sub(input, string.len(MOVEBY) + 1 + 1) coordsRaw = string.sub(input, string.len(MOVEBY) + 1 + 1)
local coords = captureString(coordsRaw, "([+-]?%d+)") local coords = captureString(coordsRaw, "([+-]?%d+)")
sendDirective(LEADER, MOVEBY .. ":" .. coords[1] .. "," .. coords[2] .. "," .. coords[3]) sendDirective(LEADER, MOVEBY, coords[1], coords[2], coords[3])
elseif string.find(input, MOVETO) == 1 then elseif string.find(input, MOVETO) == 1 then
coordsRaw = string.sub(input, string.len(MOVETO) + 1 + 1) coordsRaw = string.sub(input, string.len(MOVETO) + 1 + 1)
local coords = captureString(coordsRaw, "([+-]?%d+)") local coords = captureString(coordsRaw, "([+-]?%d+)")
sendDirective(LEADER, MOVETO .. ":" .. coords[1] .. "," .. coords[2] .. "," .. coords[3]) sendDirective(LEADER, MOVETO, coords[1], coords[2], coords[3])
elseif string.find(input, STATUS) == 1 then elseif string.find(input, STATUS) == 1 then
sendDirective(LEADER, STATUS) sendDirective(LEADER, STATUS)
elseif string.find(input, POSITION) == 1 then elseif string.find(input, POSITION) == 1 then
@ -93,17 +94,15 @@ end
-- Status and requests from turtles -- Status and requests from turtles
local function handleRequest(unit, request) local function handleRequest(unit, request, content)
end end
local function handleMessage(unit, str, type) local function handleMessage(sender, type, data)
if type == LOG then if type == LOG then
local log = string.sub(str, string.len(LOG) + 1 + 1) print("[" .. unit .. "]: " .. data["log"] .. textutils.serialise(data["content"]))
print("[" .. unit .. "]: " .. str)
elseif type == REQUEST then elseif type == REQUEST then
local request = string.sub(str, string.len(REQUEST) + 1 + 1) handleRequest(unit, data["request"], data["content"])
handleRequest(unit, request)
end end
end end
@ -112,11 +111,11 @@ end
local function listenToMiners() local function listenToMiners()
print("Listening to miners.") print("Listening to miners.")
while running do while running do
local event, side, sendChannel, replyChannel, data, distance = os.pullEvent("modem_message") local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message")
print("") print("")
local token, recipient, sender, type, content = interpretData(data) local data = textutils.unserialise(serialised)
if token == TOKEN and recipient == HOST then if data["token"] == TOKEN and data["recipient"] == HOST then
handleMessage(sender, content, type) handleMessage(data["sender"], data["type"], data)
else else
print("Invalid header information. Ignoring.") print("Invalid header information. Ignoring.")
end end

View File

@ -13,7 +13,9 @@ local STATUS = "status"
local POSITION = "position" local POSITION = "position"
local HEADING = "heading" local HEADING = "heading"
local QUARRY = "quarry" local QUARRY = "quarry"
-- Constant log local TASK = "task"
local FAILED = "failed"
-- Constant messaging
local LOG = "LOG" local LOG = "LOG"
local REQUEST = "REQ" local REQUEST = "REQ"
-- Constant requests -- Constant requests
@ -61,14 +63,30 @@ local function getMessage(data)
return nil return nil
end end
local function SendRequest(request) local function SendRequest(request, ...)
print("Sending request: " .. request) print("Sending request: " .. request)
modem.transmit(CHANNEL, CHANNEL, TOKEN .. ":" .. HOST .. ":" .. UNIT .. ":" .. REQUEST .. ":" .. request) reqData = {
token=TOKEN,
recipient=HOST,
sender=UNIT,
type=REQUEST,
request=request,
content=arg
}
modem.transmit(CHANNEL, CHANNEL, textutils.serialise(reqData))
end end
local function sendLog(log) local function sendLog(log, ...)
print("Sending message: " .. log) print("Sending message: " .. log)
modem.transmit(CHANNEL, CHANNEL, TOKEN .. ":" .. HOST .. ":" .. UNIT .. ":" .. LOG .. ":" .. log) logData = {
token=TOKEN,
recipient=HOST,
sender=UNIT,
type=LOG,
log=log,
CONTENT=arg
}
modem.transmit(CHANNEL, CHANNEL, textutils.serialise(logData))
end end
local function captureString(str, seq) local function captureString(str, seq)
@ -80,15 +98,6 @@ local function captureString(str, seq)
return res return res
end 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
local function findItem(str) local function findItem(str)
for s=1,16 do for s=1,16 do
if string.sub(str, -string.len(str)) == str then if string.sub(str, -string.len(str)) == str then
@ -213,9 +222,9 @@ local function orient(v, forward, right)
end end
local function attemptStartTask(f, ...) local function attemptStartTask(f, ...)
sendLog("attempting to start a task.") sendLog(TASK, "Attempting to start task.")
if task ~= nil then if task ~= nil then
sendLog("failed: unable to start task due to another task running.") sendLog(FAILED, "Unable to start task due to another task running.")
return return
end end
task = f task = f
@ -225,43 +234,43 @@ end
-- actions -- actions
local function locatePosition() local function locatePosition()
sendLog("position: using GPS to determine position.") sendLog(POSITION, "Using GPS to determine position.")
local x, y, z = gps.locate() local x, y, z = gps.locate()
if x == nil then sendLog("failed: could not locate.") end if x == nil then sendLog(POSITION, "could not locate.") end
position = vector.new(x, y, z) position = vector.new(x, y, z)
end end
local function determineHeading() local function determineHeading()
sendLog("heading: calculating heading.") sendLog(HEADING, "Calculating heading.")
if position == nil then if position == nil then
sendLog("failed: no current position") sendLog(FAILED, "No current position")
return return
end end
local oldPos = vector.new(position.x, position.y, position.z) local oldPos = vector.new(position.x, position.y, position.z)
if not attemptMoveForward() then if not attemptMoveForward() then
sendLog("failed: could not move to get delta.") sendLog(FAILED, "Could not move to get delta.")
return return
end end
if x == nil then if x == nil then
sendLog("failed: could not locate.") sendLog(FAILED, "Could not locate.")
return return
end end
heading = position - oldPos heading = position - oldPos
if not attemptMoveRight() then if not attemptMoveRight() then
sendLog("failed: unable to rotate right.") sendLog(FAILED, "Unable to rotate right.")
heading = nil heading = nil
return return
end end
local oldPos = vector.new(position.x, position.y, position.z) local oldPos = vector.new(position.x, position.y, position.z)
if not attemptMoveForward() then if not attemptMoveForward() then
sendLog("failed: unable to move forward after rotation.") sendLog(FAILED, "Unable to move forward after rotation.")
heading = nil heading = nil
return return
end end
if not attemptMoveLeft() then if not attemptMoveLeft() then
sendLog("failed: unable to rotate left.") sendLog(FAILED, "Unable to rotate left.")
heading = nil heading = nil
return return
end end
@ -272,26 +281,26 @@ local function moveBy(delta, forward, right)
sendLog("Attempting to move by (" .. delta.x .. "," .. delta.y .. "," .. delta.z .. ") (x,y,z).") sendLog("Attempting to move by (" .. delta.x .. "," .. delta.y .. "," .. delta.z .. ") (x,y,z).")
-- x first -- x first
if delta.x ~= 0 then if delta.x ~= 0 then
sendLog("Moving on x axis by: " .. delta.x) sendLog(MOVEBY, "Moving on x axis by: " .. delta.x)
orient(vector.new(delta.x, 0, 0):normalize(), forward, right) orient(vector.new(delta.x, 0, 0):normalize(), forward, right)
for x=1,math.abs(delta.x) do for x=1,math.abs(delta.x) do
attemptMoveForward() attemptMoveForward()
end end
end end
if delta.z ~= 0 then if delta.z ~= 0 then
sendLog("Moving on z axis by: " .. delta.z) sendLog(MOVEBY, "Moving on z axis by: " .. delta.z)
orient(vector.new(0, 0, delta.z):normalize(), forward, right) orient(vector.new(0, 0, delta.z):normalize(), forward, right)
for z=1,math.abs(delta.z) do for z=1,math.abs(delta.z) do
attemptMoveForward() attemptMoveForward()
end end
end end
if delta.y > 0 then if delta.y > 0 then
sendLog("Moving on y axis by: " .. delta.y) sendLog(MOVEBY, "Moving on y axis by: " .. delta.y)
for y=1,delta.y do for y=1,delta.y do
attemptMoveUp() attemptMoveUp()
end end
elseif delta.y < 0 then elseif delta.y < 0 then
sendLog("Moving on y axis by: " .. delta.y) sendLog(MOVEBY, "Moving on y axis by: " .. delta.y)
for y=1,math.abs(delta.y) do for y=1,math.abs(delta.y) do
attemptMoveDown() attemptMoveDown()
end end
@ -299,7 +308,7 @@ local function moveBy(delta, forward, right)
end end
local function quarry(a, b) local function quarry(a, b)
sendLog("quarry: Starting quarry task from positions " .. tostring(a) .. " to " .. tostring(b) ".") sendLog(QUARRY, "Starting quarry task from positions " .. tostring(a) .. " to " .. tostring(b) ".")
end end
local function executeTasks() local function executeTasks()
@ -315,52 +324,51 @@ end
local function listen() local function listen()
while running do while running do
print("Listening for directives...") print("Listening for directives...")
local event, side, sendChannel, replyChannel, data, distance = os.pullEvent("modem_message") local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message")
print("Recieved data: " .. data) print("Recieved data...")
local token, recipient, sender, type, content = interpretData(data) local data = textutils.unserialise(serialized)
if token == TOKEN and recipient == UNIT then if data["token"] == TOKEN and data["recipient"] == UNIT then
print("Got content: " .. content) print("Got content: " .. data["type"])
if string.find(content, LUA_PREFIX) == 1 then
local luaScript = string.sub(content, string.len(LUA_PREFIX) + 1) if data["type"] == LUA_PREFIX then
print("Executing: " .. luaScript) print("Executing: " .. data["content"][0])
local f = loadstring(luaScript) local f = loadstring(luaScript)
if f ~= nil then if f ~= nil then
local freturn = f() local freturn = f()
if freturn ~= nil then if freturn ~= nil then
modem.transmit(CHANNEL, CHANNEL, TOKEN .. ":" .. UNIT .. ":" .. "obj" .. ":") sendLog(LUA_PREFIX, textutils.serialise(freturn))
modem.transmit(CHANNEL, CHANNEL, freturn)
end end
else else
printError("Unable to load lua string.") printError("Unable to load lua string.")
end end
elseif string.find(content, MOVETO .. ":") == 1 then elseif data["type"] == MOVETO then
local coordsRaw = string.sub(content, string.len(MOVETO .. ":") + 1) local x = data["content"][1]
print("Raw coordinate data: " .. coordsRaw) local y = data["content"][2]
local coords = captureString(coordsRaw, "([+-]?%d+)") local z = data["content"][3]
local x = coords[1]
local y = coords[2]
local z = coords[3]
if heading ~= nil then
attemptStartTask(moveTo, vector.new(x,y,z), heading, rightAxis)
else
attemptStartTask(moveTo, vector.new(x,y,z), relHeading, relRightAxis)
end
elseif string.find(content, MOVEBY .. ":") == 1 then
local valuesRaw = string.sub(content, string.len(MOVEBY .. ":") + 1)
print("Raw coordinate data: " .. valuesRaw)
local deltas = captureString(valuesRaw, "([+-]?%d+)")
local x = deltas[1]
local y = deltas[2]
local z = deltas[3]
if heading ~= nil then if heading ~= nil then
print("Using actual coordinates.") print("Using actual coordinates.")
sendLog(MOVETO, "Using actual coordinates to move.")
attemptStartTask(moveTo, vector.new(x,y,z), heading, rightAxis)
else
print("Using relative coordinates.")
sendLog(MOVETO, "Using relative coordinates to move.")
attemptStartTask(moveTo, vector.new(x,y,z), relHeading, relRightAxis)
end
elseif data["type"] == MOVEBY then
local x = data["content"][1]
local y = data["content"][2]
local z = data["content"][3]
if heading ~= nil then
print("Using actual coordinates.")
sendLog(MOVEBY, "Using actual coordinates to move.")
attemptStartTask(moveBy, vector.new(x,y,z), heading, rightAxis) attemptStartTask(moveBy, vector.new(x,y,z), heading, rightAxis)
else else
print("Using relative coordinates.") print("Using relative coordinates.")
sendLog(MOVEBY, "Using relative coordinates to move.")
attemptStartTask(moveBy, vector.new(x,y,z), relHeading, relRightAxis) attemptStartTask(moveBy, vector.new(x,y,z), relHeading, relRightAxis)
end end
elseif string.find(content, STATUS) == 1 then elseif data["type"] == STATUS then
status = { status = {
relative_position = { relative_position = {
coordinates=relativePos, coordinates=relativePos,
@ -374,7 +382,7 @@ local function listen()
}, },
fuel=turtle.getFuelLevel(), fuel=turtle.getFuelLevel(),
} }
sendLog("status:" .. textutils.serialise(status)) sendLog(STATUS, status)
elseif string.find(content, POSITION) == 1 then elseif string.find(content, POSITION) == 1 then
locatePosition() locatePosition()
elseif string.find(content, HEADING) == 1 then elseif string.find(content, HEADING) == 1 then