From 3e708670308f76c105934d01ad96d7d4b9998e9b Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Sat, 13 Dec 2025 00:00:50 -0500 Subject: [PATCH] mine.lua: rewrite checkpoint 5 --- mine.lua | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/mine.lua b/mine.lua index 09a6224..3b9b7ea 100644 --- a/mine.lua +++ b/mine.lua @@ -29,17 +29,18 @@ function copy_v(v) ) end -local bmetatable +local bmetatable, new_block -- what is this? a type that holds a starting position and a dimention as -- two vectors -- TODO learn about metatables local Block = { -- orient our corner and offset to a vector orient = function (self, pos) + local rel_v1, rel_v2 = self.v1 - pos, self.v2 - pos local dims = { - 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 } + x = { rel_v1.x, rel_v1.x + rel_v2.x }, + y = { rel_v1.y, rel_v1.y + rel_v2.y }, + z = { rel_v1.z, rel_v1.z + rel_v2.z } } for dim, points in pairs(dims) do -- pick the closer point @@ -52,24 +53,30 @@ local Block = { point = points[2] offset = points[1] - points[2] end - self.v1[dim] = point - self.v2[dim] = offset + rel_v1[dim] = point + rel_v2[dim] = offset end - return self + return new_block(rel_v1, rel_v2) end, take = function (self, amt, dir) -- TODO 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 - new.v1[dir] = amt + new.v2[dir] = amt + block_offset remainder.v1[dir] = remainder.v1[dir] + amt remainder.v2[dir] = remainder.v2[dir] - amt return new, remainder end, copy = function (self) - return Block.new( + return new_block( vector.new(self.v1.x, self.v1.y, self.v1.z), vector.new(self.v2.x, self.v2.y, self.v2.z) ) @@ -85,7 +92,7 @@ bmetatable = { __index = Block, } -local new_block = function (v1, v2) +new_block = function (v1, v2) return setmetatable( { v1 = v1, v2 = v2 }, bmetatable @@ -265,7 +272,7 @@ end function canMine (block) local tooHigh = block.v2.y > 2 -- 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) end @@ -286,14 +293,14 @@ function splitHorizontal (block) return bigBlock(block) and inMiddleOfBlock(block) end if inMiddleOfBigBlock(block) then - -- TODO + -- TODO split the big block at where the turtle is near end -- decide whether to take a slice off of the x or z direction -- (criteria: take the dimension with the smaller length) - if block.v2.x <= block.v2.z then - return block:take(1, "x") - else + if block.v2.z <= block.v2.x then return block:take(1, "z") + else + return block:take(1, "x") end end @@ -301,7 +308,6 @@ function split (block) local tooHigh = function (block) return block.v2.y > 2 end - block = block:orient(getCurrentPos()) if tooHigh(block) then return block:take(3, "y") else @@ -313,7 +319,6 @@ function process (stack, starting_point) -- fuel and go to starting point refuelUntil(FUEL_RESERVE) mode.mine = { forward = true } - -- TODO if #stack == 0 then return nil end local working = table.remove(stack):orient(getCurrentPos()) print("Working:", working:tostring())