Transforming mine.lua to do vertical slices instead of horizontal ones

This commit is contained in:
Emerson Rosen-Jones 2025-07-17 23:43:41 -04:00
parent 4e25323753
commit 2cf0dc58d9

105
mine.lua
View file

@ -8,15 +8,11 @@ mode.mine = { forward = true }
local block = {} local block = {}
function mineSides () function mineSides ()
if mode.mine.left then if mode.mine.up then
turtle.turnLeft() turtle.digUp()
turtle.dig()
turtle.turnRight()
end end
if mode.mine.right then if mode.mine.down then
turtle.turnRight() turtle.digDown()
turtle.dig()
turtle.turnLeft()
end end
end end
@ -40,7 +36,6 @@ function moveUpDown (delta)
end end
turtle.up() turtle.up()
delta = delta - 1 delta = delta - 1
-- mineSides()
end end
while delta < 0 do while delta < 0 do
if mode.mine.forward then if mode.mine.forward then
@ -48,7 +43,6 @@ function moveUpDown (delta)
end end
turtle.down() turtle.down()
delta = delta + 1 delta = delta + 1
-- mineSides()
end end
end end
@ -154,55 +148,38 @@ function generateNextSteps (w, l)
-- Algorithm: -- Algorithm:
-- 1. mine l blocks in the y-direction -- 1. mine l blocks in the y-direction
next_step = { x = 0, y = DIRECTION.Y * column_len } 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) 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 DIRECTION.Y = -DIRECTION.Y
remaining_width = remaining_width - 3 remaining_width = remaining_width - 1
next_step = { x = 0, y = 0 } next_step = { x = 0, y = 0 }
next_step.mine = { forward = true } if remaining_width > 0 then
if remaining_width >= 3 then -- 2. go in the x direction for the next y-sweep
-- 2. go in x-direction to mine a 3-wide column next_step.x = DIRECTION.X
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
table.insert(step_buffer, next_step) table.insert(step_buffer, next_step)
end end
end end
next_step = table.remove(step_buffer, 1) 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
end end
function minePlane (w, l) function minePlane (w, l, h)
-- mine a plane starting from the current location l blocks in the -- mine a plane starting from the current location l blocks in the
-- y-direction and w blocks in the x-direction -- 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 } mode.mine = { forward = true }
for x, y, mine in generateNextSteps(w, l) do if h == 1 then
mode.mine = mine 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.", -- print(string.format("Moving %d, %d relative to current pos.",
-- x, y)) -- x, y))
moveAndCheck(x, y, 0) moveAndCheck(x, y, 0)
@ -214,41 +191,39 @@ function mine (block)
if turtle.getFuelLevel() < 100 then turtle.refuel(8) end if turtle.getFuelLevel() < 100 then turtle.refuel(8) end
mode.mine = { forward = true } mode.mine = { forward = true }
moveAbs(block.x, block.y, block.z) 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 local z_dir
if block.zoff > 1 then z_dir = 1 else z_dir = -1 end 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 -- calculate orientation of next plane
local w, l local w, l
if mode.y == block.y then if mode.y == block.y then
turnToFace(FORWARD)
l = block.yoff l = block.yoff
else else
turnToFace(BACK)
l = -block.yoff l = -block.yoff
end end
local diff_from_start = math.abs(mode.x - block.x) local diff_from_start = math.abs(mode.x - block.x)
-- print(string.format("Distance from start: %d", diff_from_start)) -- print(string.format("Distance from start: %d", diff_from_start))
if diff_from_start > 1 and width > 3 then if mode.x == block.x then
w = -block.xoff
else
w = block.xoff w = block.xoff
else
w = -block.xoff
end end
move(0, 0, z_dir) -- move down into the new plane and mine it
minePlane(w, l) move(0, 0, z_dir * 3)
minePlane(w, l, 3)
remaining_h = remaining_h - 3
end end
end end