mine.lua: rewrite checkpoint 5

This commit is contained in:
Emerson Rosen-Jones 2025-12-13 00:00:50 -05:00
parent 91a27e292e
commit 3e70867030

View file

@ -29,17 +29,18 @@ function copy_v(v)
) )
end end
local bmetatable local bmetatable, new_block
-- what is this? a type that holds a starting position and a dimention as -- what is this? a type that holds a starting position and a dimention as
-- two vectors -- two vectors
-- TODO learn about metatables -- TODO learn about metatables
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)
local rel_v1, rel_v2 = self.v1 - pos, self.v2 - pos
local dims = { local dims = {
x = { self.v1.x, self.v1.x + self.v2.x }, x = { rel_v1.x, rel_v1.x + rel_v2.x },
y = { self.v1.y, self.v1.y + self.v2.y }, y = { rel_v1.y, rel_v1.y + rel_v2.y },
z = { self.v1.z, self.v1.z + self.v2.z } z = { rel_v1.z, rel_v1.z + rel_v2.z }
} }
for dim, points in pairs(dims) do for dim, points in pairs(dims) do
-- pick the closer point -- pick the closer point
@ -52,24 +53,30 @@ local Block = {
point = points[2] point = points[2]
offset = points[1] - points[2] offset = points[1] - points[2]
end end
self.v1[dim] = point rel_v1[dim] = point
self.v2[dim] = offset rel_v2[dim] = offset
end end
return self return new_block(rel_v1, rel_v2)
end, end,
take = function (self, amt, dir) take = function (self, amt, dir)
-- TODO -- TODO
if dir ~= "x" and dir ~= "y" and dir ~= "z" then return nil end if dir ~= "x" and dir ~= "y" and dir ~= "z" then return nil end
local block_offset = -1
if self.v2[dir] < 0 then
amt = amt * -1
block_offset = block_offset * -1
end
local new, remainder = self:copy(), self local new, remainder = self:copy(), self
new.v1[dir] = amt new.v2[dir] = amt + block_offset
remainder.v1[dir] = remainder.v1[dir] + amt remainder.v1[dir] = remainder.v1[dir] + amt
remainder.v2[dir] = remainder.v2[dir] - amt remainder.v2[dir] = remainder.v2[dir] - amt
return new, remainder return new, remainder
end, end,
copy = function (self) copy = function (self)
return Block.new( return new_block(
vector.new(self.v1.x, self.v1.y, self.v1.z), 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)
) )
@ -85,7 +92,7 @@ bmetatable = {
__index = Block, __index = Block,
} }
local new_block = function (v1, v2) new_block = function (v1, v2)
return setmetatable( return setmetatable(
{ v1 = v1, v2 = v2 }, { v1 = v1, v2 = v2 },
bmetatable bmetatable
@ -265,7 +272,7 @@ end
function canMine (block) function canMine (block)
local tooHigh = block.v2.y > 2 local tooHigh = block.v2.y > 2
-- 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 tooWide = block.v2.x > 0 and block.v2.z > 0 local tooWide = (math.abs(block.v2.x) > 0) and (math.abs(block.v2.z) > 0)
return not (tooHigh or tooWide) return not (tooHigh or tooWide)
end end
@ -286,14 +293,14 @@ function splitHorizontal (block)
return bigBlock(block) and inMiddleOfBlock(block) return bigBlock(block) and inMiddleOfBlock(block)
end end
if inMiddleOfBigBlock(block) then if inMiddleOfBigBlock(block) then
-- TODO -- TODO split the big block at where the turtle is near
end end
-- decide whether to take a slice off of the x or z direction -- decide whether to take a slice off of the x or z direction
-- (criteria: take the dimension with the smaller length) -- (criteria: take the dimension with the smaller length)
if block.v2.x <= block.v2.z then if block.v2.z <= block.v2.x then
return block:take(1, "x")
else
return block:take(1, "z") return block:take(1, "z")
else
return block:take(1, "x")
end end
end end
@ -301,7 +308,6 @@ function split (block)
local tooHigh = function (block) local tooHigh = function (block)
return block.v2.y > 2 return block.v2.y > 2
end end
block = block:orient(getCurrentPos())
if tooHigh(block) then if tooHigh(block) then
return block:take(3, "y") return block:take(3, "y")
else else
@ -313,7 +319,6 @@ function process (stack, starting_point)
-- fuel and go to starting point -- fuel and go to starting point
refuelUntil(FUEL_RESERVE) refuelUntil(FUEL_RESERVE)
mode.mine = { forward = true } mode.mine = { forward = true }
-- TODO
if #stack == 0 then return nil end if #stack == 0 then return nil end
local working = table.remove(stack):orient(getCurrentPos()) local working = table.remove(stack):orient(getCurrentPos())
print("Working:", working:tostring()) print("Working:", working:tostring())