From 6d3541a1c65500c3ba4440ed27dd3586a53a5a75 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sun, 9 May 2021 13:15:50 -0500 Subject: [PATCH] Potentially fixed heading issue. --- mineleader.lua | 65 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/mineleader.lua b/mineleader.lua index 2ba2840..64fc092 100644 --- a/mineleader.lua +++ b/mineleader.lua @@ -325,9 +325,17 @@ local function determineHeading() sendLog(HEADING, "Heading obtained. \nForward: " .. textutils.serialiseJSON(heading) .. "\nRight direction: " .. textutils.serialiseJSON(rightAxis)) end -local function moveBy(delta, forward, right) +local function moveBy(delta, relative) sendLog(MOVEBY, "Attempting to move by (" .. delta.x .. "," .. delta.y .. "," .. delta.z .. ") (x,y,z).") - -- x first + local forward + local right + if relative then + forward = relHeading + right = relRightAxis + else + forward = heading + right = rightAxis + end if delta.x ~= 0 then sendLog(MOVEBY, "Moving on x axis by: " .. delta.x) orient(vector.new(delta.x, 0, 0):normalize(), forward, right) @@ -367,7 +375,16 @@ local function moveBy(delta, forward, right) end end -local function moveTo(dest) +local function moveTo(dest, relative) + local forward, right + if relative then + forward = relHeading + right = relRightAxis + else + forward = heading + right = rightAxis + end + dest = dest:round() sendLog(MOVETO, "Attempting to move by (" .. dest.x .. "," .. dest.y .. "," .. dest.z .. ") (x,y,z).") if position == nil or heading == nil then @@ -377,27 +394,33 @@ local function moveTo(dest) local xBlocked, yBlocked, zBlocked = false, false, false local oldPos = nil while not vectorEqual(dest, position) do - if oldPos ~= nil and vectorEqual(oldPos, position) then + local pos + if relative then + pos = position + else + pos = relativePos + end + if oldPos ~= nil and vectorEqual(oldPos, pos) then sendLog(FAILED, "Cannot proceed to due obstacle. XYZ being blocked: " .. tostring(xBlocked) .. "," .. tostring(yBlocked) .. "," .. tostring(zBlocked)) break end - oldPos = vector.new(position.x, position.y, position.z); + oldPos = vector.new(pos.x, pos.y, pos.z); if position.x ~= dest.x then - if not orient(vector.new(dest.x - position.x, 0, 0):normalize(), heading, rightAxis) then + if not orient(vector.new(dest.x - pos.x, 0, 0):normalize(), forward, right) then sendLog(FAILED, "Unable to orient for X axis.") return end xblocked = attemptMoveForward() end if position.y ~= dest.y then - if dest.y - position.y > 0 then + if dest.y - pos.y > 0 then yBlocked = attemptMoveUp() else yBlocked = attemptMoveDown() end end - if position.z ~= dest.z then - if not orient(vector.new(0, 0, dest.z - position.z):normalize(), heading, rightAxis) then + if pos.z ~= dest.z then + if not orient(vector.new(0, 0, dest.z - pos.z):normalize(), forward, right) then sendLog(FAILED, "Unable to orient for Z axis.") return end @@ -407,10 +430,19 @@ local function moveTo(dest) end -local function quarry(x, y, z, forward, right, pattern) +local function quarry(x, y, z, relative, pattern) x = tonumber(x) y = tonumber(y) z = tonumber(z) + local forward; + local right; + if relative then + forward = relHeading + right = relRightAxis + else + forward = heading + right = rightAxis + end sendLog(QUARRY, "Starting quarry of (x length,depth,z length): " .. x .. "," .. y .. "," .. z .. " with a not pattern of: " .. pattern) for yDelta=1,math.abs(y) do if (y > 0) then @@ -436,6 +468,7 @@ local function quarry(x, y, z, forward, right, pattern) for xDelta=1,math.abs(x) do for zDelta=1,math.abs(z) do print("X,Z: " .. xDelta .. "," .. zDelta) + print("heading, right heading: " .. forward .. ", " .. right) local success = true; if reverseTrip then orient(vector.new(0,0, -z), forward, right) @@ -506,11 +539,11 @@ local function listen() if heading ~= nil then print("Using actual coordinates.") sendLog(MOVETO, "Using actual coordinates to move.") - attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), heading, rightAxis) + attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), false) else print("Using relative coordinates.") sendLog(MOVETO, "Using relative coordinates to move.") - attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), relHeading, relRightAxis) + attemptStartTask(MOVETO, moveTo, vector.new(x,y,z), true) end elseif data["request"] == MOVEBY then local x = data["content"][1] @@ -519,11 +552,11 @@ local function listen() if heading ~= nil then print("Using actual coordinates.") sendLog(MOVEBY, "Using actual coordinates to move.") - attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), heading, rightAxis) + attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), false) else print("Using relative coordinates.") sendLog(MOVEBY, "Using relative coordinates to move.") - attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), relHeading, relRightAxis) + attemptStartTask(MOVEBY, moveBy, vector.new(x,y,z), true) end elseif data["request"] == STATUS then status = { @@ -550,11 +583,11 @@ local function listen() determineHeading() elseif data["request"] == QUARRY then if heading ~= nil then - quarry(data["content"][1], data["content"][2], data["content"][3], heading, rightAxis, notPattern) + quarry(data["content"][1], data["content"][2], data["content"][3], false, notPattern) sendLog(QUARRY, "Using actual position for quarrying.") else sendLog(QUARRY, "Using relative position for quarrying.") - quarry(data["content"][1], data["content"][2], data["content"][3], relHeading, relRightAxis, notPattern) + quarry(data["content"][1], data["content"][2], data["content"][3], true, notPattern) end end end