1

Update turtle absolute movement logic (mine only where needed)

This commit is contained in:
2025-10-07 16:51:38 +02:00
parent 6728841195
commit a1d4d65c42

View File

@ -152,13 +152,12 @@ function TurtleController:MoveVertical(number_of_blocks, skip_unstocking, skip_r
self:UnstockIfFull()
end
-- Mine
-- Mine/Move
if mine_enabled then
mine_function()
end
-- Move
if not move_function() then
while not move_function() do
mine_function()
end
elseif not move_function() then
error("Turtle failed to move vertically!")
end
@ -189,7 +188,15 @@ function TurtleController:MoveToPosition(x, y, z, dir, skip_unstocking, skip_ref
-- Store the current position on the stack, so we can return using TurtleController:MoveBack()
self.last_positions:Push(Position:Copy(self.position))
-- Store mining config so we can restore it later
local mine_forward = self.mine_forward
local mine_above = self.mine_above
local mine_below = self.mine_below
-- EAST/WEST axis (do first to not interfere with chests)
self:EnableMiningForward()
self:DisableMiningAbove()
self:DisableMiningBelow()
if self.position.x > x then
self:TurnToDirection(Direction.WEST)
elseif self.position.x < x then
@ -206,11 +213,34 @@ function TurtleController:MoveToPosition(x, y, z, dir, skip_unstocking, skip_ref
self:MoveForward(math.abs(z - self.position.z), skip_unstocking, skip_refueling)
-- UP/DOWN axis
self:DisableMiningForward()
if y < self.position.y then
self:EnableMiningBelow()
elseif y > self.position.y then
self:EnableMiningAbove()
end
self:MoveVertical(y - self.position.y, skip_unstocking, skip_refueling)
-- Direction
self:TurnToDirection(dir or Direction.NORTH)
-- Restore mining config
if mine_forward then
self:EnableMiningForward()
else
self:DisableMiningForward()
end
if mine_above then
self:EnableMiningAbove()
else
self:DisableMiningAbove()
end
if mine_below then
self:EnableMiningBelow()
else
self:DisableMiningBelow()
end
-- Sanity check
if not (self.position.x == x and self.position.y == y and self.position.z == z and self.position.dir == dir) then
error("TurtleController:MoveToPosition failed to move to target position!")
@ -258,6 +288,22 @@ function TurtleController:MoveBack(skip_unstocking, skip_refueling)
end
end
-----------------------------------------------------------------------------------------------
-- Testing Methods
-----------------------------------------------------------------------------------------------
---@param block_name string
---@return boolean
function TurtleController:TestForBlock(block_name)
local has_block, block_data = turtle.inspect()
if not has_block then
return false
end
return block_data.name == block_name
end
-----------------------------------------------------------------------------------------------
-- Inventory Methods
-----------------------------------------------------------------------------------------------
@ -336,6 +382,8 @@ function TurtleController:UnstockIfFull(skip_inventory_check)
self:DropInventory()
self:RefuelIfEmpty()
self:TurnToDirection(Direction.NORTH)
self:MoveForward(1)
self:MoveBack(true, true)
end
@ -379,6 +427,8 @@ function TurtleController:RefuelIfEmpty(skip_fuel_check)
until turtle.getFuelLevel() >= target_fuel_level
local after_level = turtle.getFuelLevel()
self:TurnToDirection(Direction.NORTH)
self:MoveForward(1)
self:MoveBack(true, true)
print(