diff --git a/mine.lua b/mine.lua index 1265810..75fdafe 100644 --- a/mine.lua +++ b/mine.lua @@ -8,15 +8,11 @@ mode.mine = { forward = true } local block = {} function mineSides () - if mode.mine.left then - turtle.turnLeft() - turtle.dig() - turtle.turnRight() + if mode.mine.up then + turtle.digUp() end - if mode.mine.right then - turtle.turnRight() - turtle.dig() - turtle.turnLeft() + if mode.mine.down then + turtle.digDown() end end @@ -40,7 +36,6 @@ function moveUpDown (delta) end turtle.up() delta = delta - 1 - -- mineSides() end while delta < 0 do if mode.mine.forward then @@ -48,7 +43,6 @@ function moveUpDown (delta) end turtle.down() delta = delta + 1 - -- mineSides() end end @@ -154,55 +148,38 @@ function generateNextSteps (w, l) -- Algorithm: -- 1. mine l blocks in the y-direction next_step = { x = 0, y = DIRECTION.Y * column_len } - next_step.mine = { forward = true, left = true, right = true } - if remaining_width == 2 then - local delta = DIRECTION.Y + DIRECTION.X - if delta == 0 then - next_step.mine.right = false - else - next_step.mine.left = false - end - elseif remaining_width == 1 then - next_step.mine.left = false - next_step.mine.right = false - end table.insert(step_buffer, next_step) - -- setup position for next minePlane - if remaining_width == 1 and math.abs(w) > 1 then - next_step = { x = -DIRECTION.X, y = 0 } - next_step.mine = {} - table.insert(step_buffer, next_step) - end - DIRECTION.Y = -DIRECTION.Y - remaining_width = remaining_width - 3 + remaining_width = remaining_width - 1 next_step = { x = 0, y = 0 } - next_step.mine = { forward = true } - if remaining_width >= 3 then - -- 2. go in x-direction to mine a 3-wide column - next_step.x = DIRECTION.X * 3 - table.insert(step_buffer, next_step) - elseif remaining_width > 0 then - -- or move for a finishing pass - next_step.x = DIRECTION.X * 2 + if remaining_width > 0 then + -- 2. go in the x direction for the next y-sweep + next_step.x = DIRECTION.X table.insert(step_buffer, next_step) end end next_step = table.remove(step_buffer, 1) - return next_step.x, next_step.y, next_step.mine + return next_step.x, next_step.y end end -function minePlane (w, l) +function minePlane (w, l, h) -- mine a plane starting from the current location l blocks in the -- y-direction and w blocks in the x-direction - print(string.format("Mining a plane of %d by %d", w, l)) + -- print(string.format("Mining a plane of %d by %d by %d", w, l, h)) mode.mine = { forward = true } - for x, y, mine in generateNextSteps(w, l) do - mode.mine = mine + if h == 1 then + mode.mine.up = true + elseif h == -1 then + mode.mine.down = true + elseif h ~= 0 then + mode.mine.up = true + mode.mine.down = true + end + for x, y in generateNextSteps(w, l) do -- print(string.format("Moving %d, %d relative to current pos.", -- x, y)) moveAndCheck(x, y, 0) @@ -214,41 +191,39 @@ function mine (block) if turtle.getFuelLevel() < 100 then turtle.refuel(8) end mode.mine = { forward = true } moveAbs(block.x, block.y, block.z) - -- Set up for next plane - -- 1. move in if necessary - local width = math.abs(block.xoff) + 1 - local x_dir - if block.xoff < 0 then x_dir = -1 else x_dir = 1 end - if width > 2 then - move(x_dir, 0, 0) - end - -- Start mining planes - local h_remaining = function () - return math.abs(block.z + block.zoff - mode.z) - end - -- initial pass without moving down - minePlane(block.xoff, block.yoff) local z_dir if block.zoff > 1 then z_dir = 1 else z_dir = -1 end - while h_remaining() > 0 do + local remaining_h = math.abs(block.z + block.zoff - mode.z) + 1 + -- Initial pass; leave remaining_h at a nice multiple of 3 + local excess = remaining_h % 3 + if excess > 0 then + minePlane(block.xoff, block.yoff, (excess - 1) * z_dir) + remaining_h = remaining_h - excess + else + move(0, 0, z_dir) + minePlane(block.xoff, block.yoff, 3) + remaining_h = remaining_h - 3 + end + -- Start mining planes + while remaining_h > 0 do -- calculate orientation of next plane local w, l if mode.y == block.y then - turnToFace(FORWARD) l = block.yoff else - turnToFace(BACK) l = -block.yoff end local diff_from_start = math.abs(mode.x - block.x) -- print(string.format("Distance from start: %d", diff_from_start)) - if diff_from_start > 1 and width > 3 then - w = -block.xoff - else + if mode.x == block.x then w = block.xoff + else + w = -block.xoff end - move(0, 0, z_dir) - minePlane(w, l) + -- move down into the new plane and mine it + move(0, 0, z_dir * 3) + minePlane(w, l, 3) + remaining_h = remaining_h - 3 end end