2021-05-02 00:39:10 +00:00
|
|
|
--configurables
|
|
|
|
local CHANNEL = 55555
|
|
|
|
local TOKEN = "LxdIjZPDLL2oZoayb7uR2Lbz7bnnbkxz"
|
|
|
|
|
|
|
|
-- Constant
|
|
|
|
local HOST = "HOST"
|
|
|
|
local UNIT = "LEAD"
|
|
|
|
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 messaging
|
2021-05-02 00:39:10 +00:00
|
|
|
local LOG = "LOG"
|
|
|
|
local REQUEST = "REQ"
|
|
|
|
-- Constant requests
|
|
|
|
local BLOCKED = "blocked"
|
|
|
|
local ITEM = "item"
|
|
|
|
|
|
|
|
-- INIT SEQUENCE
|
|
|
|
print("Initiating leader mining turtle.")
|
|
|
|
|
2021-05-02 21:02:35 +00:00
|
|
|
running = true
|
2021-05-02 00:39:10 +00:00
|
|
|
local task = nil
|
2021-05-02 21:02:35 +00:00
|
|
|
local taskArgs = {}
|
2021-05-02 00:39:10 +00:00
|
|
|
local modem = nil
|
|
|
|
local heading = nil
|
|
|
|
local position = nil
|
|
|
|
local rightAxis = nil
|
|
|
|
local relHeading = vector.new(1,0,0)
|
|
|
|
local relativePos = vector.new(0,0,0)
|
|
|
|
local relRightAxis = vector.new(0,0,1)
|
|
|
|
|
|
|
|
do
|
|
|
|
periphs = peripheral.find("modem")
|
|
|
|
if not periphs then
|
|
|
|
error("Modem peripheral not found.")
|
|
|
|
else
|
|
|
|
print("Modem found.")
|
|
|
|
modem = periphs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print("Opening channel: " .. CHANNEL)
|
|
|
|
modem.open(CHANNEL)
|
|
|
|
|
|
|
|
-- helpers
|
|
|
|
local function getMessage(data)
|
|
|
|
if string.find(data, TOKEN .. ":") == 1 then
|
|
|
|
print("Validated data token.")
|
|
|
|
local validated = string.sub(data, string.len(TOKEN) + 1 + 1)
|
|
|
|
print("Validated sequence: " .. validated)
|
|
|
|
if string.find(validated, UNIT .. ":") == 1 then
|
|
|
|
print("Checking unit...")
|
|
|
|
return string.sub(validated, string.len(UNIT) + 1 + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print("Data was unable to be parsed.")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2021-05-03 19:16:22 +00:00
|
|
|
local function SendRequest(request, ...)
|
2021-05-02 00:39:10 +00:00
|
|
|
print("Sending request: " .. request)
|
2021-05-03 19:16:22 +00:00
|
|
|
reqData = {
|
|
|
|
token=TOKEN,
|
|
|
|
recipient=HOST,
|
|
|
|
sender=UNIT,
|
|
|
|
type=REQUEST,
|
|
|
|
request=request,
|
|
|
|
content=arg
|
|
|
|
}
|
|
|
|
modem.transmit(CHANNEL, CHANNEL, textutils.serialise(reqData))
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
|
2021-05-03 19:16:22 +00:00
|
|
|
local function sendLog(log, ...)
|
2021-05-02 00:39:10 +00:00
|
|
|
print("Sending message: " .. log)
|
2021-05-03 19:16:22 +00:00
|
|
|
logData = {
|
|
|
|
token=TOKEN,
|
|
|
|
recipient=HOST,
|
|
|
|
sender=UNIT,
|
|
|
|
type=LOG,
|
|
|
|
log=log,
|
|
|
|
CONTENT=arg
|
|
|
|
}
|
|
|
|
modem.transmit(CHANNEL, CHANNEL, textutils.serialise(logData))
|
2021-05-02 00:39:10 +00:00
|
|
|
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 findItem(str)
|
|
|
|
for s=1,16 do
|
|
|
|
if string.sub(str, -string.len(str)) == str then
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local function refuel()
|
|
|
|
local slot = findItem("coal")
|
|
|
|
if slot == 0 then
|
|
|
|
--TODO: request more coal
|
|
|
|
end
|
|
|
|
turtle.select(slot)
|
|
|
|
turtle.refuel()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function checkFuel()
|
|
|
|
if turtle.getFuelLevel() > 0 then return false end
|
|
|
|
refuel()
|
|
|
|
return true;
|
|
|
|
end
|
|
|
|
|
|
|
|
local function vectorEqual(a, b)
|
|
|
|
return a.x == b.x and a.y == b.y and a.z == b.z
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMove(movef)
|
|
|
|
if not movef() then
|
|
|
|
refuel()
|
|
|
|
return movef();
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveUp()
|
|
|
|
if attemptMove(turtle.up) then
|
|
|
|
relativePos.y = relativePos.y + 1
|
|
|
|
if position ~= nil then position.y = position.y + 1 end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveDown()
|
|
|
|
if attemptMove(turtle.down) then
|
|
|
|
relativePos.y = relativePos.y - 1
|
|
|
|
if position ~= nil then position.y = position.y - 1 end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveForward()
|
|
|
|
if attemptMove(turtle.forward) then
|
|
|
|
relativePos = relativePos + relHeading
|
|
|
|
if (position ~= nil and heading ~= nil) then
|
|
|
|
position = position + heading
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveBackward()
|
|
|
|
if attemptMove(turtle.back) then
|
|
|
|
relativePos = relativePos - relHeading
|
|
|
|
if (position ~= nil and heading ~= nil) then
|
|
|
|
position = position - heading
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveLeft()
|
|
|
|
if attemptMove(turtle.turnLeft) then
|
|
|
|
local previousHeading = relHeading
|
|
|
|
relHeading = -relRightAxis
|
|
|
|
relRightAxis = previousHeading
|
|
|
|
|
|
|
|
if heading ~= nil then
|
|
|
|
local previousHeading = heading
|
|
|
|
heading = -rightAxis
|
|
|
|
rightAxis = previousHeading
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptMoveRight()
|
|
|
|
if attemptMove(turtle.turnRight) then
|
|
|
|
local previousHeading = relHeading
|
|
|
|
relHeading = relRightAxis
|
2021-05-02 21:02:35 +00:00
|
|
|
relRightAxis = -previousHeading
|
2021-05-02 00:39:10 +00:00
|
|
|
|
|
|
|
if heading ~= nil then
|
|
|
|
local previousHeading = heading
|
|
|
|
heading = rightAxis
|
|
|
|
rightAxis = -previousHeading
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function orient(v, forward, right)
|
|
|
|
print("Attempting to orient such that " .. tostring(v) .. " == " .. tostring(forward) .. ".")
|
|
|
|
if vectorEqual(v, forward) then
|
|
|
|
return
|
|
|
|
elseif vectorEqual(v, right) then
|
|
|
|
attemptMoveRight()
|
|
|
|
elseif vectorEqual(v, -right) then
|
|
|
|
attemptMoveLeft()
|
|
|
|
else
|
|
|
|
attemptMoveRight()
|
|
|
|
attemptMoveRight()
|
|
|
|
end
|
2021-05-02 21:02:35 +00:00
|
|
|
print("Orientation complete.")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function attemptStartTask(f, ...)
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(TASK, "Attempting to start task.")
|
2021-05-02 21:02:35 +00:00
|
|
|
if task ~= nil then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Unable to start task due to another task running.")
|
2021-05-02 21:02:35 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
task = f
|
|
|
|
taskArgs = arg
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- actions
|
|
|
|
|
|
|
|
local function locatePosition()
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(POSITION, "Using GPS to determine position.")
|
2021-05-02 00:39:10 +00:00
|
|
|
local x, y, z = gps.locate()
|
2021-05-03 19:16:22 +00:00
|
|
|
if x == nil then sendLog(POSITION, "could not locate.") end
|
2021-05-02 00:39:10 +00:00
|
|
|
position = vector.new(x, y, z)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function determineHeading()
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(HEADING, "Calculating heading.")
|
2021-05-02 00:39:10 +00:00
|
|
|
if position == nil then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "No current position")
|
2021-05-02 00:39:10 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local oldPos = vector.new(position.x, position.y, position.z)
|
|
|
|
if not attemptMoveForward() then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Could not move to get delta.")
|
2021-05-02 00:39:10 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if x == nil then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Could not locate.")
|
2021-05-02 00:39:10 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
heading = position - oldPos
|
|
|
|
|
|
|
|
|
|
|
|
if not attemptMoveRight() then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Unable to rotate right.")
|
2021-05-02 00:39:10 +00:00
|
|
|
heading = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local oldPos = vector.new(position.x, position.y, position.z)
|
|
|
|
if not attemptMoveForward() then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Unable to move forward after rotation.")
|
2021-05-02 00:39:10 +00:00
|
|
|
heading = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not attemptMoveLeft() then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(FAILED, "Unable to rotate left.")
|
2021-05-02 00:39:10 +00:00
|
|
|
heading = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
rightAxis = position - oldPos
|
|
|
|
end
|
|
|
|
|
|
|
|
local function moveBy(delta, forward, right)
|
2021-05-02 21:02:35 +00:00
|
|
|
sendLog("Attempting to move by (" .. delta.x .. "," .. delta.y .. "," .. delta.z .. ") (x,y,z).")
|
2021-05-02 00:39:10 +00:00
|
|
|
-- x first
|
2021-05-02 21:02:35 +00:00
|
|
|
if delta.x ~= 0 then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Moving on x axis by: " .. delta.x)
|
2021-05-02 21:02:35 +00:00
|
|
|
orient(vector.new(delta.x, 0, 0):normalize(), forward, right)
|
|
|
|
for x=1,math.abs(delta.x) do
|
|
|
|
attemptMoveForward()
|
|
|
|
end
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
2021-05-02 21:02:35 +00:00
|
|
|
if delta.z ~= 0 then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Moving on z axis by: " .. delta.z)
|
2021-05-02 21:02:35 +00:00
|
|
|
orient(vector.new(0, 0, delta.z):normalize(), forward, right)
|
|
|
|
for z=1,math.abs(delta.z) do
|
|
|
|
attemptMoveForward()
|
|
|
|
end
|
2021-05-02 00:39:10 +00:00
|
|
|
end
|
|
|
|
if delta.y > 0 then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Moving on y axis by: " .. delta.y)
|
2021-05-02 21:02:35 +00:00
|
|
|
for y=1,delta.y do
|
2021-05-02 00:39:10 +00:00
|
|
|
attemptMoveUp()
|
|
|
|
end
|
|
|
|
elseif delta.y < 0 then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Moving on y axis by: " .. delta.y)
|
2021-05-02 21:02:35 +00:00
|
|
|
for y=1,math.abs(delta.y) do
|
2021-05-02 00:39:10 +00:00
|
|
|
attemptMoveDown()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-03 18:20:03 +00:00
|
|
|
local function quarry(a, b)
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(QUARRY, "Starting quarry task from positions " .. tostring(a) .. " to " .. tostring(b) ".")
|
2021-05-03 18:16:52 +00:00
|
|
|
end
|
|
|
|
|
2021-05-02 21:02:35 +00:00
|
|
|
local function executeTasks()
|
|
|
|
while running do
|
|
|
|
if task ~= nil then
|
|
|
|
task(unpack(taskArgs))
|
|
|
|
task = nil
|
|
|
|
end
|
|
|
|
sleep(5)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function listen()
|
|
|
|
while running do
|
|
|
|
print("Listening for directives...")
|
2021-05-03 19:16:22 +00:00
|
|
|
local event, side, sendChannel, replyChannel, serialized, distance = os.pullEvent("modem_message")
|
|
|
|
print("Recieved data...")
|
|
|
|
local data = textutils.unserialise(serialized)
|
2021-05-03 19:21:07 +00:00
|
|
|
if data["token"] == TOKEN and data["recipient"] == UNIT and data["type"] == REQUEST then
|
|
|
|
print("Got request: " .. data["request"])
|
2021-05-03 19:16:22 +00:00
|
|
|
|
2021-05-03 19:21:07 +00:00
|
|
|
if data["request"] == LUA_PREFIX then
|
2021-05-03 19:16:22 +00:00
|
|
|
print("Executing: " .. data["content"][0])
|
2021-05-02 21:02:35 +00:00
|
|
|
local f = loadstring(luaScript)
|
|
|
|
if f ~= nil then
|
|
|
|
local freturn = f()
|
|
|
|
|
|
|
|
if freturn ~= nil then
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(LUA_PREFIX, textutils.serialise(freturn))
|
2021-05-02 21:02:35 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
printError("Unable to load lua string.")
|
|
|
|
end
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == MOVETO then
|
2021-05-03 19:16:22 +00:00
|
|
|
local x = data["content"][1]
|
|
|
|
local y = data["content"][2]
|
|
|
|
local z = data["content"][3]
|
2021-05-02 21:02:35 +00:00
|
|
|
if heading ~= nil then
|
2021-05-03 19:16:22 +00:00
|
|
|
print("Using actual coordinates.")
|
|
|
|
sendLog(MOVETO, "Using actual coordinates to move.")
|
2021-05-02 21:02:35 +00:00
|
|
|
attemptStartTask(moveTo, vector.new(x,y,z), heading, rightAxis)
|
|
|
|
else
|
2021-05-03 19:16:22 +00:00
|
|
|
print("Using relative coordinates.")
|
|
|
|
sendLog(MOVETO, "Using relative coordinates to move.")
|
2021-05-02 21:02:35 +00:00
|
|
|
attemptStartTask(moveTo, vector.new(x,y,z), relHeading, relRightAxis)
|
|
|
|
end
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == MOVEBY then
|
2021-05-03 19:16:22 +00:00
|
|
|
local x = data["content"][1]
|
|
|
|
local y = data["content"][2]
|
|
|
|
local z = data["content"][3]
|
2021-05-02 21:02:35 +00:00
|
|
|
if heading ~= nil then
|
|
|
|
print("Using actual coordinates.")
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Using actual coordinates to move.")
|
2021-05-02 21:02:35 +00:00
|
|
|
attemptStartTask(moveBy, vector.new(x,y,z), heading, rightAxis)
|
|
|
|
else
|
|
|
|
print("Using relative coordinates.")
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(MOVEBY, "Using relative coordinates to move.")
|
2021-05-02 21:02:35 +00:00
|
|
|
attemptStartTask(moveBy, vector.new(x,y,z), relHeading, relRightAxis)
|
|
|
|
end
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == STATUS then
|
2021-05-02 21:02:35 +00:00
|
|
|
status = {
|
|
|
|
relative_position = {
|
|
|
|
coordinates=relativePos,
|
|
|
|
forward=relHeading,
|
|
|
|
right=relRightAxis
|
|
|
|
},
|
|
|
|
position={
|
|
|
|
coordinates=position,
|
|
|
|
forward=heading,
|
|
|
|
right=rightAxis
|
|
|
|
},
|
|
|
|
fuel=turtle.getFuelLevel(),
|
|
|
|
}
|
2021-05-03 19:16:22 +00:00
|
|
|
sendLog(STATUS, status)
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == POSITION then
|
2021-05-02 21:02:35 +00:00
|
|
|
locatePosition()
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == HEADING then
|
2021-05-02 21:02:35 +00:00
|
|
|
determineHeading()
|
2021-05-03 19:21:07 +00:00
|
|
|
elseif data["request"] == QUARRY then
|
2021-05-02 21:02:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
parallel.waitForAll(listen, executeTasks)
|