Began work on moving to destination.

This commit is contained in:
Harrison Deng 2021-05-03 18:15:22 -05:00
parent 57b36d0941
commit bcf30e259d
2 changed files with 71 additions and 7 deletions

View File

@ -16,7 +16,7 @@ local MOVETO = "moveto"
local STATUS = "status" local STATUS = "status"
local POSITION = "position" local POSITION = "position"
local HEADING = "heading" local HEADING = "heading"
local QUARRY = "quarry" local QUARRYCHUNK = "quarry"
local TASK = "task" local TASK = "task"
local FAILED = "failed" local FAILED = "failed"
-- Constant messsage types -- Constant messsage types
@ -84,7 +84,7 @@ local function listenForCommands()
sendDirective(LEADER, POSITION) sendDirective(LEADER, POSITION)
elseif string.find(input, HEADING) == 1 then elseif string.find(input, HEADING) == 1 then
sendDirective(LEADER, HEADING) sendDirective(LEADER, HEADING)
elseif string.find(input, QUARRY) == 1 then elseif string.find(input, QUARRYCHUNK) == 1 then
sendDirective(LEADER, input) sendDirective(LEADER, input)
end end
end end

View File

@ -12,7 +12,7 @@ local MOVETO = "moveto"
local STATUS = "status" local STATUS = "status"
local POSITION = "position" local POSITION = "position"
local HEADING = "heading" local HEADING = "heading"
local QUARRY = "quarry" local QUARRYCHUNK = "quarry"
local TASK = "task" local TASK = "task"
local FAILED = "failed" local FAILED = "failed"
-- Constant messaging -- Constant messaging
@ -21,6 +21,9 @@ local REQUEST = "REQ"
-- Constant requests -- Constant requests
local BLOCKED = "blocked" local BLOCKED = "blocked"
local ITEM = "item" local ITEM = "item"
-- avoid breaking pattern
local notPattern = "^allthemodium"
-- INIT SEQUENCE -- INIT SEQUENCE
print("Initiating leader mining turtle.") print("Initiating leader mining turtle.")
@ -134,6 +137,17 @@ local function attemptMove(movef)
return true return true
end end
local function attemptBreak(breakf, inspectf, notPattern)
local exists, data = inspectf()
if not exists then
return false
end
if string.match(data.name, pattern) then
return false
end
breakf()
end
local function locatePosition() local function locatePosition()
local x, y, z = gps.locate() local x, y, z = gps.locate()
if x == nil then return false end if x == nil then return false end
@ -163,7 +177,7 @@ local function attemptMoveForward()
if attemptMove(turtle.forward) then if attemptMove(turtle.forward) then
relativePos = relativePos + relHeading relativePos = relativePos + relHeading
if (position ~= nil) then if (position ~= nil) then
if ( heading ~= nil and heading:length() ~= 0) then if ( heading ~= nil ) then
position = position + heading position = position + heading
else else
locatePosition() locatePosition()
@ -178,7 +192,7 @@ local function attemptMoveBackward()
if attemptMove(turtle.back) then if attemptMove(turtle.back) then
relativePos = relativePos - relHeading relativePos = relativePos - relHeading
if (position ~= nil) then if (position ~= nil) then
if (heading ~= nil and heading:length() ~= 0) then if (heading ~= nil) then
position = position - heading position = position - heading
else else
locatePosition() locatePosition()
@ -195,7 +209,7 @@ local function attemptMoveLeft()
relHeading = -relRightAxis relHeading = -relRightAxis
relRightAxis = previousHeading relRightAxis = previousHeading
if heading ~= nil and heading:length() ~= 0 then if heading ~= nil then
local previousHeading = heading local previousHeading = heading
heading = -rightAxis heading = -rightAxis
rightAxis = previousHeading rightAxis = previousHeading
@ -211,7 +225,7 @@ local function attemptMoveRight()
relHeading = relRightAxis relHeading = relRightAxis
relRightAxis = -previousHeading relRightAxis = -previousHeading
if heading ~= nil and heading:length() ~= 0 then if heading ~= nil then
local previousHeading = heading local previousHeading = heading
heading = rightAxis heading = rightAxis
rightAxis = -previousHeading rightAxis = -previousHeading
@ -221,6 +235,18 @@ local function attemptMoveRight()
return true return true
end end
local function attemptBreakUp(notPattern)
return attemptBreak(turtle.digUp, turtle.inspectUp, notPattern);
end
local function attemptBreakFront(notPattern)
return attemptBreak(turtle.dig, turtle.inspect, notPattern)
end
local function attemptBreakDown(notPattern)
return attemptBreak(turtle.digDown, turtle.inspectDown, notPattern)
end
local function orient(v, forward, right) local function orient(v, forward, right)
print("Attempting to orient such that " .. tostring(v) .. " == " .. tostring(forward) .. ".") print("Attempting to orient such that " .. tostring(v) .. " == " .. tostring(forward) .. ".")
if vectorEqual(v, forward) then if vectorEqual(v, forward) then
@ -329,6 +355,44 @@ local function moveBy(delta, forward, right)
end end
end end
local function moveTo(dest, range)
sendLog(MOVETO, "Attempting to move by (" .. dest.x .. "," .. dest.y .. "," .. dest.z .. ") (x,y,z).")
if position == nil or heading == nil then
sendLog(FAILED, "Position or heading is invalid.")
end
local xBlocked, yBlocked, zBlocked = false, false, false
local oldPos = nil
while not vectorEqual(dest, position) do
if vectorEqual(oldPos, position) then
sendLog(FAILED, "Cannot proceed to due obstacle.")
return
end
oldPos = vector.new(position.x, position.y, position.z);
if position.x ~= dest.x then
if not orient(vector.new(dest.x - position.x, 0, 0), heading, rightAxis) then
sendLog(FAILED, "Unable to orient for X axis.")
return
end
attemptMoveForward()
end
if position.y ~= dest.y then
if dest.y - position.y > 0 then
attemptMoveUp()
else
attemptMoveDown()
end
end
if position.z ~= dest.z then
if not orient(vector.new(0, 0, dest.z - position.z), heading, rightAxis) then
sendLog(FAILED, "Unable to orient for Z axis.")
return
end
attemptMoveForward()
end
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