mine.lua: rewrite checkpoint 2

This commit is contained in:
Emerson Rosen-Jones 2025-12-01 20:32:46 -05:00
parent d0ab016dcf
commit f4444cd120

View file

@ -21,7 +21,7 @@ function copy_v(v)
return vector.new(
v.x,
v.y,
v.z,
v.z
)
end
@ -33,8 +33,8 @@ local Block = {
-- orient our corner and offset to a vector
orient = function (self, pos)
local dims = {
x = { self.v1.x, self.v1.x + self.v2.x }
y = { self.v1.y, self.v1.y + self.v2.y }
x = { self.v1.x, self.v1.x + self.v2.x },
y = { self.v1.y, self.v1.y + self.v2.y },
z = { self.v1.z, self.v1.z + self.v2.z }
}
for dim, points in pairs(dims) do
@ -67,7 +67,7 @@ local Block = {
copy = function (self)
return Block.new(
vector.new(self.v1.x, self.v1.y, self.v1.z),
vector.new(self.v2.x, self.v2.y, self.v2.z),
vector.new(self.v2.x, self.v2.y, self.v2.z)
)
end
}
@ -193,10 +193,11 @@ function refuelUntil (amt)
end
function dropOffItems (starting_point, go_back)
print("Time to drop off what I got")
-- Turn off mining
mode.mine = { forward = true }
-- Note current position (assuming facing FORWARD)
local pos_state = vector.new(mode.v.x, mode.v.y, mode.v.z)
local pos_state = getCurrentPos()
-- Return to origin, facing BACK
moveAbs(starting_point)
moveAbs(V_ZERO)
@ -232,7 +233,7 @@ function mine (block)
-- a. don't move vertically if you don't have to
-- b. set mining mode beforehand
-- 2. move to the ending point
local height = block.v2.y
local height = block.v2.y + 1
if height % 3 == 0 then
mode.mine = { up = true, forward = true, down = true }
elseif height == 2 then
@ -249,14 +250,14 @@ function mine (block)
start_v = start_v - UNIT_Y
end_v = end_v - UNIT_Y
end
move(start_v)
move(end_v)
moveAbs(start_v)
moveAbs(end_v)
end
function canMine (block)
local tooHigh = block.v2.y > 3
local tooHigh = block.v2.y > 2
-- we can only mine in a 1-wide strip at a time
local tooWide = block.v2.x > 1 and block.v2.z > 1
local tooWide = block.v2.x > 0 and block.v2.z > 0
return not (tooHigh or tooWide)
end
@ -272,7 +273,7 @@ end
function split (block)
local tooHigh = function (block)
return block.v2.y > 3
return block.v2.y > 2
end
block = block:orient(getCurrentPos())
if tooHigh(block) then
@ -283,12 +284,10 @@ function split (block)
end
function process (stack, starting_point)
-- START code taken from `mine`
-- fuel and go to starting point
refuelUntil(FUEL_RESERVE)
mode.mine = { forward = true }
moveAbs(starting_point)
-- END
-- TODO
if #stack == 0 then return nil end
local working = table.remove(stack):orient(getCurrentPos())
@ -323,7 +322,7 @@ function calculateBlock (v1, v2)
normalize("y", v1.y, v2.y)
normalize("z", v1.z, v2.z)
return new_block(
vector.new(block.x, block.y, block.z)
vector.new(block.x, block.y, block.z),
vector.new(block.xoff, block.yoff, block.zoff)
)
end
@ -332,14 +331,16 @@ function run (v1, v2)
-- TODO calculate starting_point, the place turtle will move to before
-- moving to the first corner of block
local block = new_block(v1, v2 - v1)
process({ block })
local starting_point = V_ZERO
process({ block }, starting_point)
-- return to base
dropOffItems(starting_point)
turnToFace(FORWARD)
end
function usage ()
print("Usage: mine x1 y1 z1 x2 y2 z2 OR mine x1 y1 z1 OR mine x1 y1 z1
print("Usage: mine x1 y1 z1 x2 y2 z2 OR mine x1 y1 z1 OR mine x1 y1 z1 \
x2 y2 z2 x_turtle y_turtle z_turtle.")
print("Mine from the first set of coordinates to the second in a \
rectangular prism.")
@ -347,9 +348,9 @@ function usage ()
start of the program.")
print("If invoked with only three arguments, act as if they are the last \
three arguments and substitute 0 0 0 for the first three.")
print("If invoked with nine arguments, interpret the first two sets as
world coordinates of the rectangular prism and the third set as
the coordinates of the turtle's starting position,
print("If invoked with nine arguments, interpret the first two sets as \
world coordinates of the rectangular prism and the third set as \
the coordinates of the turtle's starting position, \
ASSUMING FACING NORTH.")
print("Fuel goes in the top-left slot of the turtle's inventory.")
end