diff --git a/mine.lua b/mine.lua index 54b7d7a..e680822 100644 --- a/mine.lua +++ b/mine.lua @@ -6,13 +6,9 @@ local FUEL_RESERVE = 800 local FUEL_REFUEL_UNIT = 8 -- how many items to refuel with at a time local FULL_CHECK_SLOT = 14 -- slot to check for "fullness" - --- what size is good for a medium-size piece -local MED_DIM = 11 - -- what size of a dimension is considered big enough to split into medium-size -- pieces -local BIG_DIM = MED_DIM * 3 +local BIG_DIM = 7 local V_ZERO = vector.new(0, 0, 0) local UNIT_X = vector.new(1, 0, 0) @@ -36,6 +32,7 @@ end 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) @@ -63,6 +60,7 @@ local Block = { return self end, take = function (self, amt, dir) + -- TODO if dir ~= "x" and dir ~= "y" and dir ~= "z" then return nil end local block_offset = -1 @@ -214,6 +212,8 @@ 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 = getCurrentPos() -- Return to origin, facing BACK moveAbs(starting_point) moveAbs(V_ZERO) @@ -224,9 +224,10 @@ function dropOffItems (starting_point, go_back) turtle.drop() end turtle.select(1) - -- Return to the mining zone, facing forward + -- Return to current position, facing forward if go_back then moveAbs(starting_point) + moveAbs(pos_state) end end @@ -281,19 +282,33 @@ function canMine (block) end function splitHorizontal (block) - local take_amt = 1 - local bigBlock = function (block) - return (block.v2.x >= BIG_DIM) or (block.v2.z >= BIG_DIM) + -- 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) + local differentSigns = function (a, b) + return math.abs(a + b) < math.abs(a) + math.abs(b) end - if bigBlock(block) then - take_amt = MED_DIM + local bigBlock = function (block) + return (block.v2.x >= BIG_DIM) and (block.v2.z >= BIG_DIM) + end + local inMiddleOfBlock = function (block) + local middle_of_x = differentSigns(block.v1.x, block.v2.x) + local middle_of_z = differentSigns(block.v1.z, block.v2.z) + return middle_of_x or middle_of_z + end + local inMiddleOfBigBlock = function (block) + return bigBlock(block) and inMiddleOfBlock(block) + end + if inMiddleOfBigBlock(block) then + -- 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: bite a piece off the bigger dimension) - if math.abs(block.v2.z) >= math.abs(block.v2.x) then - return block:take(take_amt, "z") + -- (criteria: take the dimension that is not too big) + if math.abs(block.v2.z) < BIG_DIM then + return block:take(1, "z") else - return block:take(take_amt, "x") + return block:take(1, "x") end end @@ -309,16 +324,17 @@ function split (block) end function process (stack, starting_point) + -- fuel and go to starting point refuelUntil(FUEL_RESERVE) mode.mine = { forward = true } if #stack == 0 then return nil end - checkAndDropOff(starting_point) local working = table.remove(stack):orient(getCurrentPos()) print("Working:", working:tostring()) if canMine(working) then + checkAndDropOff(starting_point) mine(working) else - local new, remainder = split(working) + local new, remainder = split(working, mode.v) table.insert(stack, remainder) table.insert(stack, new) end @@ -333,7 +349,7 @@ function run (v1, v2) process({ block }, starting_point) -- return to base - dropOffItems(starting_point, false) + dropOffItems(starting_point) turnToFace(FORWARD) end