Improve turning logic in TurtleController and add edgecases in MoveToPosition when at the front/back of the mining area
This commit is contained in:
@ -55,16 +55,20 @@ function TurtleController:TurnRelative(number_of_turns)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
print(("Turtle is turning by %d..."):format(number_of_turns))
|
|
||||||
|
|
||||||
self:RefuelIfEmpty()
|
self:RefuelIfEmpty()
|
||||||
|
|
||||||
-- Turn turtle
|
-- Turn turtle
|
||||||
for _ = 1,math.abs(number_of_turns) do
|
local turns = number_of_turns % 4
|
||||||
if number_of_turns > 0 then
|
if turns == 3 then
|
||||||
|
print(("Turtle is turning by %d (shortened to %d)..."):format(number_of_turns, -1))
|
||||||
|
|
||||||
|
-- If we're rotating by 3, we could turn faster by rotating by 1 in the opposite direction
|
||||||
|
turtle.turnLeft()
|
||||||
|
else
|
||||||
|
print(("Turtle is turning by %d (shortened to %d)..."):format(number_of_turns, turns))
|
||||||
|
|
||||||
|
for _ = 1,turns do
|
||||||
turtle.turnRight()
|
turtle.turnRight()
|
||||||
else
|
|
||||||
turtle.turnLeft()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -79,17 +83,7 @@ function TurtleController:TurnToDirection(direction)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
print(("Turtle is turning to direction %d..."):format(direction))
|
self:TurnRelative(direction - self.position.dir)
|
||||||
|
|
||||||
self:RefuelIfEmpty()
|
|
||||||
|
|
||||||
local rotation = direction - self.position.dir
|
|
||||||
if math.abs(rotation) == 3 then
|
|
||||||
-- If we're rotating by 3, we could turn faster by rotating by 1 in the opposite direction
|
|
||||||
rotation = -1 * (rotation - 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
self:TurnRelative(rotation)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -133,9 +127,9 @@ function TurtleController:MoveRelative(number_of_blocks)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param x number
|
---@param x number The EAST/WEST axis (grows from WEST to EAST)
|
||||||
---@param y number
|
---@param y number The UP/DOWN axis (grows from DOWN to UP)
|
||||||
---@param z number
|
---@param z number The NORTH/SOUTH axis (grows from SOUTH to NORTH)
|
||||||
---@param dir Direction
|
---@param dir Direction
|
||||||
function TurtleController:MoveToPosition(x, y, z, dir)
|
function TurtleController:MoveToPosition(x, y, z, dir)
|
||||||
print(("Turtle is moving to (%d, %d, %d)..."):format(x, y, z))
|
print(("Turtle is moving to (%d, %d, %d)..."):format(x, y, z))
|
||||||
@ -143,9 +137,16 @@ function TurtleController:MoveToPosition(x, y, z, dir)
|
|||||||
self:DisableMining()
|
self:DisableMining()
|
||||||
self.last_positions:Push(Position:Copy(self.position))
|
self.last_positions:Push(Position:Copy(self.position))
|
||||||
|
|
||||||
-- Move south once (if possible) to not be blocked by the slice that is currently mined
|
if self.position.z > 1 then
|
||||||
self:TurnToDirection(Direction.SOUTH)
|
-- Move south once (if we're at the top) to not be blocked by the slice that is currently mined.
|
||||||
self:MoveRelative(1)
|
-- This assumes that we mine the full width back to front.
|
||||||
|
self:TurnToDirection(Direction.SOUTH)
|
||||||
|
self:MoveRelative(1)
|
||||||
|
elseif self.position.z == 0 then
|
||||||
|
-- Move north once (if we're at the bottom) to not be blocked by the chests
|
||||||
|
self:TurnToDirection(Direction.NORTH)
|
||||||
|
self:MoveRelative(1)
|
||||||
|
end
|
||||||
|
|
||||||
-- EAST/WEST axis (do first to not interfere with chests or unmined walls)
|
-- EAST/WEST axis (do first to not interfere with chests or unmined walls)
|
||||||
if self.position.x > x then
|
if self.position.x > x then
|
||||||
@ -176,8 +177,13 @@ end
|
|||||||
function TurtleController:MoveBack()
|
function TurtleController:MoveBack()
|
||||||
local target_position = self.last_positions:Pop()
|
local target_position = self.last_positions:Pop()
|
||||||
|
|
||||||
self:MoveToPosition(target_position.x, target_position.y, target_position.z, target_position.dir)
|
if target_position == nil then
|
||||||
self.last_positions:Pop()
|
shell.exit()
|
||||||
|
else
|
||||||
|
self:MoveToPosition(target_position.x, target_position.y, target_position.z, target_position.dir)
|
||||||
|
self.last_positions:Pop()
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -228,8 +234,10 @@ function TurtleController:DropInventory()
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:UnstockIfFull()
|
---@param skip_inventory_check boolean | nil
|
||||||
if self:HasInventorySpace() then
|
function TurtleController:UnstockIfFull(skip_inventory_check)
|
||||||
|
skip_inventory_check = skip_inventory_check or false
|
||||||
|
if not skip_inventory_check and self:HasInventorySpace() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -243,8 +251,10 @@ function TurtleController:UnstockIfFull()
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:RefuelIfEmpty()
|
---@param skip_fuel_check boolean | nil
|
||||||
if self:HasFuel() then
|
function TurtleController:RefuelIfEmpty(skip_fuel_check)
|
||||||
|
skip_fuel_check = skip_fuel_check or false
|
||||||
|
if not skip_fuel_check and self:HasFuel() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -260,8 +270,13 @@ function TurtleController:RefuelIfEmpty()
|
|||||||
|
|
||||||
-- Include the distance to the last position when refueling
|
-- Include the distance to the last position when refueling
|
||||||
-- to keep the amount of work done between refuelings constant
|
-- to keep the amount of work done between refuelings constant
|
||||||
|
local target_fuel_level = self.config.refuel_amount
|
||||||
local last_position = self.last_positions:Peek()
|
local last_position = self.last_positions:Peek()
|
||||||
local target_fuel_level = self.config.refuel_amount + last_position.x + last_position.y + last_position.z
|
if last_position == nil then
|
||||||
|
shell.exit()
|
||||||
|
else
|
||||||
|
target_fuel_level = target_fuel_level + last_position.x + last_position.y + last_position.z
|
||||||
|
end
|
||||||
|
|
||||||
-- Refuel until we hit the refuel_amount
|
-- Refuel until we hit the refuel_amount
|
||||||
local before_level = turtle.getFuelLevel()
|
local before_level = turtle.getFuelLevel()
|
||||||
|
|||||||
Reference in New Issue
Block a user