1

Compare commits

...

4 Commits

4 changed files with 223 additions and 46 deletions

View File

@ -23,19 +23,123 @@ end
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
function TestingController:TestSimpleMovement() function TestingController:TestRelativeMovementWithRelativeRotation()
print("Testing relative movement with relative rotation...")
-- N: BotCenter
self.controller:MoveRelative(3)
self.controller:TurnRelative(1) -- E: TopCenter
self.controller:MoveRelative(3)
self.controller:TurnRelative(2) -- W: TopRight
self.controller:MoveRelative(3)
self.controller:TurnRelative(10) -- E: TopCenter
self.controller:MoveRelative(3)
self.controller:TurnRelative(-3) -- S: TopRight
self.controller:MoveRelative(3)
self.controller:TurnRelative(1) -- W: BotRight
self.controller:MoveRelative(3)
self.controller:TurnRelative(1) -- N: BotCenter
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end end
function TestingController:TestComplexMovement() function TestingController:TestRelativeMovementWithAbsoluteRotation()
print("Testing relative movement with absolute rotation...")
-- N: BotCenter
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.EAST) -- E: TopCenter
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.WEST) -- W: TopRight
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.EAST) -- E: TopCenter
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.SOUTH) -- S: TopRight
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.WEST) -- W: BotRight
self.controller:MoveRelative(3)
self.controller:TurnToDirection(Direction.NORTH) -- N: BotCenter
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end end
function TestingController:TestUnloading() function TestingController:TestAbsoluteMovementWithoutStack()
print("Testing absolute movement without stack...")
-- N: BotCenter
self.controller:MoveToPosition(0, 0, 3, Direction.EAST) -- E: TopCenter
_ = io.read()
self.controller:MoveToPosition(3, 0, 3, Direction.WEST) -- W: TopRight
_ = io.read()
self.controller:MoveToPosition(0, 0, 3, Direction.EAST) -- E: TopCenter
_ = io.read()
self.controller:MoveToPosition(3, 0, 3, Direction.SOUTH) -- S: TopRight
_ = io.read()
self.controller:MoveToPosition(3, 0, 0, Direction.WEST) -- W: BotRight
_ = io.read()
self.controller:MoveToPosition(0, 0, 0, Direction.NORTH) -- N: BotCenter
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end
function TestingController:TestAbsoluteMovementWithStack()
print("Testing absolute movement with stack...")
-- N: BotCenter
self.controller:MoveToPosition(0, 0, 3, Direction.EAST) -- E: TopCenter
_ = io.read()
self.controller:MoveBack() -- N: BotCenter
_ = io.read()
self.controller:MoveToPosition(3, 0, 3, Direction.WEST) -- W: TopRight
_ = io.read()
self.controller:MoveBack() -- N: BotCenter
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end
function TestingController:TestUnstocking()
print("Testing inventory unloading...")
print("Please fill multiple inventory slots with items.")
_ = io.read()
self.controller:MoveToPosition(3, 0, 3, Direction.WEST)
_ = io.read()
self.controller:UnstockIfFull(true)
_ = io.read()
self.controller:MoveBack()
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end end
function TestingController:TestRefueling() function TestingController:TestRefueling()
print("Testing refueling...")
print("Please fill multiple inventory slots with items.")
_ = io.read()
self.controller:MoveToPosition(3, 0, 3, Direction.WEST)
_ = io.read()
self.controller:RefuelIfEmpty(true)
_ = io.read()
self.controller:MoveBack()
print("The turtle should be in its original location.")
print("Press Enter to continue")
_ = io.read()
end end
@ -46,11 +150,53 @@ end
function TestingController:Run() function TestingController:Run()
self.controller:Configure() self.controller:Configure()
self.controller:DisableMining()
self:TestSimpleMovement() print("There are multiple tests available:")
self:TestComplexMovement() print("1: Relative movement with relative rotation")
self:TestUnloading() print("2: Relative movement with absolute rotation")
self:TestRefueling() print("3: Absolute movement without stack")
print("4: Absolute movement with stack")
print("5: All movement tests")
print("6: Unloading")
print("7: Refueling")
print("8: All non-movement tests")
print("9: All tests")
local choice = 0
while choice < 1 or choice > 9 do
print("Choose a test by entering its number:")
choice = tonumber(io.read()) or 0
end
if choice == 1 then
self:TestRelativeMovementWithRelativeRotation()
elseif choice == 2 then
self:TestRelativeMovementWithAbsoluteRotation()
elseif choice == 3 then
self:TestAbsoluteMovementWithoutStack()
elseif choice == 4 then
self:TestAbsoluteMovementWithStack()
elseif choice == 5 then
self:TestRelativeMovementWithRelativeRotation()
self:TestRelativeMovementWithAbsoluteRotation()
self:TestAbsoluteMovementWithoutStack()
self:TestAbsoluteMovementWithStack()
elseif choice == 6 then
self:TestUnstocking()
elseif choice == 7 then
self:TestRefueling()
elseif choice == 8 then
self:TestUnstocking()
self:TestRefueling()
elseif choice == 9 then
self:TestRelativeMovementWithRelativeRotation()
self:TestRelativeMovementWithAbsoluteRotation()
self:TestAbsoluteMovementWithoutStack()
self:TestAbsoluteMovementWithStack()
self:TestUnstocking()
self:TestRefueling()
end
end end

View File

@ -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()

View File

@ -50,7 +50,7 @@ function VolumeExcavationController:Excavate()
self.controller:MoveRelative(self.config.mine_right) self.controller:MoveRelative(self.config.mine_right)
self.controller:TurnToDirection(Direction.WEST) self.controller:TurnToDirection(Direction.WEST)
-- Zig zag mine back to front -- Zig zag mine the full width from back to front
-- The direction is important so we can travel to start without colliding with unminded walls -- The direction is important so we can travel to start without colliding with unminded walls
local turn_dir = 1 local turn_dir = 1
for i = 1,self.config.mine_forward do for i = 1,self.config.mine_forward do
@ -62,9 +62,12 @@ function VolumeExcavationController:Excavate()
break break
end end
self.controller:TurnRelative(turn_dir) -- Skip the turning if we're mining in a straight line
self.controller:MoveRelative(1) if self.config.mine_left + self.config.mine_right > 0 then
self.controller:TurnRelative(turn_dir) self.controller:TurnRelative(turn_dir)
self.controller:MoveRelative(1)
self.controller:TurnRelative(turn_dir)
end
turn_dir = -1 * turn_dir turn_dir = -1 * turn_dir
end end

View File

@ -1,5 +1,7 @@
local Position = require("lib.position")
---@class Stack ---@class Stack
---@field elements table[] ---@field elements Position[]
local Stack = {} local Stack = {}
Stack.__index = Stack Stack.__index = Stack
@ -17,9 +19,10 @@ function Stack:Create()
end end
---@param element table ---@param element Position
function Stack:Push(element) function Stack:Push(element)
if element == nil or element == {} then if element == nil or element == {} then
printError("Failed to push empty element to the stack!")
return return
end end
@ -27,15 +30,25 @@ function Stack:Push(element)
end end
---@return table ---@return Position | nil
function Stack:Pop() function Stack:Pop()
if self:Count() == 0 then
printError("Failed to pop element from the empty stack!")
return nil
end
return table.remove(self.elements) return table.remove(self.elements)
end end
---@return table ---@return Position | nil
function Stack:Peek() function Stack:Peek()
return table[#self.elements] if self:Count() == 0 then
printError("Failed to peek element from the empty stack!")
return nil
end
return self.elements[#self.elements]
end end