mine.lua: rewrite checkpoint 1

This commit is contained in:
Emerson Rosen-Jones 2025-12-01 19:58:46 -05:00
parent 4f0876fb64
commit d0ab016dcf

View file

@ -32,8 +32,6 @@ local bmetatable
local Block = { local Block = {
-- orient our corner and offset to a vector -- orient our corner and offset to a vector
orient = function (self, pos) orient = function (self, pos)
-- TODO similar to "normalize"
error("not yet implemented")
local dims = { local dims = {
x = { self.v1.x, self.v1.x + self.v2.x } x = { self.v1.x, self.v1.x + self.v2.x }
y = { self.v1.y, self.v1.y + self.v2.y } y = { self.v1.y, self.v1.y + self.v2.y }
@ -42,7 +40,18 @@ local Block = {
for dim, points in pairs(dims) do for dim, points in pairs(dims) do
-- pick the closer point -- pick the closer point
-- return to point-offset format -- return to point-offset format
local point, offset
if math.abs(points[1]) <= math.abs(points[2]) then
point = points[1]
offset = points[2] - points[1]
else
point = points[2]
offset = points[1] - points[2]
end
self.v1[dim] = point
self.v2[dim] = offset
end end
return self
end, end,
take = function (self, amt, dir) take = function (self, amt, dir)
-- TODO -- TODO
@ -69,9 +78,10 @@ bmetatable = {
} }
local new_block = function (v1, v2) local new_block = function (v1, v2)
return setmetatable( { v1 = v1, v2 = v2 } return setmetatable(
, bmetatable { v1 = v1, v2 = v2 },
) bmetatable
)
end end
---------------------- BASIC MOVEMENT --------------------- ---------------------- BASIC MOVEMENT ---------------------
@ -184,7 +194,6 @@ end
function dropOffItems (starting_point, go_back) function dropOffItems (starting_point, go_back)
-- Turn off mining -- Turn off mining
local mine_state = mode.mine
mode.mine = { forward = true } mode.mine = { forward = true }
-- Note current position (assuming facing FORWARD) -- Note current position (assuming facing FORWARD)
local pos_state = vector.new(mode.v.x, mode.v.y, mode.v.z) local pos_state = vector.new(mode.v.x, mode.v.y, mode.v.z)
@ -203,8 +212,6 @@ function dropOffItems (starting_point, go_back)
moveAbs(starting_point) moveAbs(starting_point)
moveAbs(pos_state) moveAbs(pos_state)
end end
-- Turn mining back on, if it was on
mode.mine = mine_state
end end
function inventoryFull () function inventoryFull ()
@ -212,34 +219,54 @@ function inventoryFull ()
return turtle.getItemCount(FULL_CHECK_SLOT) > 0 return turtle.getItemCount(FULL_CHECK_SLOT) > 0
end end
function moveAndCheck (v, starting_point) function checkAndDropOff(starting_point)
move(v)
if inventoryFull() then dropOffItems(starting_point, true) end if inventoryFull() then dropOffItems(starting_point, true) end
refuelUntil(FUEL_RESERVE) refuelUntil(FUEL_RESERVE)
end end
function mine (block) function mine (block)
-- TODO adjust for new paradigm -- assumptions: block is mineable in a single pass and oriented to the
-- assumptions: block is mineable in a single pass -- currentPos
-- rough algo draft: -- rough algo draft:
-- 1. go to "starting point" at one end of block -- 1. go to "starting point" at one end of block
-- a. don't move vertically if you don't have to -- a. don't move vertically if you don't have to
-- b. refuel beforehand -- b. set mining mode beforehand
-- 2. move to the ending point -- 2. move to the ending point
-- 3. drop off items if necessary local height = block.v2.y
if height % 3 == 0 then
mode.mine = { up = true, forward = true, down = true }
elseif height == 2 then
mode.mine = { forward = true, up = true }
elseif height == -2 then
mode.mine = { forward = true, down = true }
end
local start_v = copy_v(block.v1)
local end_v = block.v1 + block.v2
if height > 2 then
start_v = start_v + UNIT_Y
end_v = end_v + UNIT_Y
elseif height < -2 then
start_v = start_v - UNIT_Y
end_v = end_v - UNIT_Y
end
move(start_v) move(start_v)
move(end_v) move(end_v)
end end
function canMine (block) function canMine (block)
local notTooHigh = block.v2.y > 3 local tooHigh = block.v2.y > 3
-- we can only mine in a 1-wide strip at a time -- we can only mine in a 1-wide strip at a time
local notTooWide = block.v2.x > 1 and block.v2.z > 1 local tooWide = block.v2.x > 1 and block.v2.z > 1
return not (tooHigh or tooWide) return not (tooHigh or tooWide)
end end
function splitHorizontal (block) function splitHorizontal (block)
-- TODO make this less naive? -- TODO make this less naive?
-- Future work: potentially split a large block into smaller blocks
-- based on currentPos (think splitting down the middle instead of
-- splitting off of one end)
-- Future work: decide whether to take a slice off of the x or z direction
-- (criteria?)
return block:take(1, "x") return block:take(1, "x")
end end
@ -264,8 +291,9 @@ function process (stack, starting_point)
-- END -- END
-- TODO -- TODO
if #stack == 0 then return nil end if #stack == 0 then return nil end
local working = table.remove(stack) local working = table.remove(stack):orient(getCurrentPos())
if canMine(working) then if canMine(working) then
checkAndDropOff(starting_point)
mine(working) mine(working)
else else
local new, remainder = split(working, mode.v) local new, remainder = split(working, mode.v)