From d0ab016dcf6eaa6c4b7dc2c0c450782deb838f09 Mon Sep 17 00:00:00 2001 From: Emerson Rosen-Jones Date: Mon, 1 Dec 2025 19:58:46 -0500 Subject: [PATCH] mine.lua: rewrite checkpoint 1 --- mine.lua | 62 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/mine.lua b/mine.lua index c11955b..8aa1cea 100644 --- a/mine.lua +++ b/mine.lua @@ -32,8 +32,6 @@ local bmetatable local Block = { -- orient our corner and offset to a vector orient = function (self, pos) - -- TODO similar to "normalize" - error("not yet implemented") local dims = { x = { self.v1.x, self.v1.x + self.v2.x } y = { self.v1.y, self.v1.y + self.v2.y } @@ -42,7 +40,18 @@ local Block = { for dim, points in pairs(dims) do -- pick the closer point -- 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 + return self end, take = function (self, amt, dir) -- TODO @@ -69,9 +78,10 @@ bmetatable = { } local new_block = function (v1, v2) - return setmetatable( { v1 = v1, v2 = v2 } - , bmetatable - ) + return setmetatable( + { v1 = v1, v2 = v2 }, + bmetatable + ) end ---------------------- BASIC MOVEMENT --------------------- @@ -184,7 +194,6 @@ end function dropOffItems (starting_point, go_back) -- Turn off mining - local mine_state = mode.mine mode.mine = { forward = true } -- Note current position (assuming facing FORWARD) 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(pos_state) end - -- Turn mining back on, if it was on - mode.mine = mine_state end function inventoryFull () @@ -212,34 +219,54 @@ function inventoryFull () return turtle.getItemCount(FULL_CHECK_SLOT) > 0 end -function moveAndCheck (v, starting_point) - move(v) +function checkAndDropOff(starting_point) if inventoryFull() then dropOffItems(starting_point, true) end refuelUntil(FUEL_RESERVE) end function mine (block) - -- TODO adjust for new paradigm - -- assumptions: block is mineable in a single pass + -- assumptions: block is mineable in a single pass and oriented to the + -- currentPos -- rough algo draft: -- 1. go to "starting point" at one end of block -- 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 - -- 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(end_v) end 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 - 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) end function splitHorizontal (block) -- 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") end @@ -264,8 +291,9 @@ function process (stack, starting_point) -- END -- TODO if #stack == 0 then return nil end - local working = table.remove(stack) + local working = table.remove(stack):orient(getCurrentPos()) if canMine(working) then + checkAndDropOff(starting_point) mine(working) else local new, remainder = split(working, mode.v)