Reformat
This commit is contained in:
9
.luarc.json
Normal file
9
.luarc.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"diagnostics.globals": [
|
||||||
|
"printError",
|
||||||
|
"turtle",
|
||||||
|
"shell",
|
||||||
|
"peripheral",
|
||||||
|
"parallel"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
local dfpwm = require("cc.audio.dfpwm")
|
local dfpwm = require("cc.audio.dfpwm")
|
||||||
local Direction = require("lib.direction")
|
|
||||||
|
|
||||||
---@alias AudioControllerConfig {buffer_length_seconds: number}
|
---@alias AudioControllerConfig {buffer_length_seconds: number}
|
||||||
|
|
||||||
@ -9,31 +8,26 @@ local Direction = require("lib.direction")
|
|||||||
local AudioController = {}
|
local AudioController = {}
|
||||||
AudioController.__index = AudioController
|
AudioController.__index = AudioController
|
||||||
|
|
||||||
|
|
||||||
---@return AudioController
|
---@return AudioController
|
||||||
function AudioController:Create()
|
function AudioController:Create()
|
||||||
local t = {}
|
local t = {}
|
||||||
setmetatable(t, AudioController)
|
setmetatable(t, AudioController)
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Fields
|
-- Fields
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
t.config = {
|
t.config = {
|
||||||
buffer_length_seconds = 1
|
buffer_length_seconds = 1,
|
||||||
}
|
}
|
||||||
t.play = false
|
t.play = false
|
||||||
|
|
||||||
|
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Audio Methods
|
-- Audio Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function AudioController:PlayAudio(filename)
|
function AudioController:PlayAudio(filename)
|
||||||
self.play = true
|
self.play = true
|
||||||
|
|
||||||
@ -47,6 +41,10 @@ function AudioController:PlayAudio(filename)
|
|||||||
local buffer = decoder(chunk)
|
local buffer = decoder(chunk)
|
||||||
|
|
||||||
while not self:GetSpeaker().playAudio(buffer) do
|
while not self:GetSpeaker().playAudio(buffer) do
|
||||||
|
if not self.play then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
---@diagnostic disable-next-line: undefined-field
|
---@diagnostic disable-next-line: undefined-field
|
||||||
os.pullEvent("speaker_audio_empty")
|
os.pullEvent("speaker_audio_empty")
|
||||||
end
|
end
|
||||||
@ -54,7 +52,6 @@ function AudioController:PlayAudio(filename)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function AudioController:PlayAudioFactory(filename)
|
function AudioController:PlayAudioFactory(filename)
|
||||||
local function play()
|
local function play()
|
||||||
self:PlayAudio(filename)
|
self:PlayAudio(filename)
|
||||||
@ -63,34 +60,28 @@ function AudioController:PlayAudioFactory(filename)
|
|||||||
return play
|
return play
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Management Methods
|
-- Management Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
---@return table | nil
|
---@return table | nil
|
||||||
function AudioController:GetSpeaker()
|
function AudioController:GetSpeaker()
|
||||||
return peripheral.find("speaker")
|
return peripheral.find("speaker")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function AudioController:StopPlaying()
|
function AudioController:StopPlaying()
|
||||||
self.play = false
|
self.play = false
|
||||||
self:GetSpeaker().stop()
|
self:GetSpeaker().stop()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Main Method
|
-- Main Method
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function AudioController:Run()
|
function AudioController:Run()
|
||||||
-- self:Configure()
|
-- self:Configure()
|
||||||
|
|
||||||
self:PlayAudio("bangarang")
|
self:PlayAudio("bangarang")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return AudioController
|
return AudioController
|
||||||
@ -11,18 +11,15 @@ local AudioController = require("controller.audio_controller")
|
|||||||
local ExcavationController = {}
|
local ExcavationController = {}
|
||||||
ExcavationController.__index = ExcavationController
|
ExcavationController.__index = ExcavationController
|
||||||
|
|
||||||
|
|
||||||
---@return ExcavationController
|
---@return ExcavationController
|
||||||
function ExcavationController:Create()
|
function ExcavationController:Create()
|
||||||
local t = {}
|
local t = {}
|
||||||
setmetatable(t, ExcavationController)
|
setmetatable(t, ExcavationController)
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Fields
|
-- Fields
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
t.controller = TurtleController:Create()
|
t.controller = TurtleController:Create()
|
||||||
t.audio = AudioController:Create()
|
t.audio = AudioController:Create()
|
||||||
t.config = {
|
t.config = {
|
||||||
@ -35,16 +32,13 @@ function ExcavationController:Create()
|
|||||||
slices_right = 0,
|
slices_right = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Behavior Methods
|
-- Behavior Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
---Excavates a single 1x1/1x2 tunnel of configured length.
|
---Excavates a single 1x1/1x2 tunnel of configured length.
|
||||||
---Will leave the turtle wherever it ends up.
|
---Will leave the turtle wherever it ends up.
|
||||||
---Only unstocks/refuels if required.
|
---Only unstocks/refuels if required.
|
||||||
@ -66,7 +60,6 @@ function ExcavationController:Excavate_1x1or2or3xL(mine_above, mine_below)
|
|||||||
self.controller:DisableMiningBelow()
|
self.controller:DisableMiningBelow()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Excavates a single 1xH partial slice of configured length.
|
---Excavates a single 1xH partial slice of configured length.
|
||||||
---Will leave the turtle in its starting position.
|
---Will leave the turtle in its starting position.
|
||||||
---Only unstocks/refuels if required.
|
---Only unstocks/refuels if required.
|
||||||
@ -123,7 +116,6 @@ function ExcavationController:Excavate_1xHxL()
|
|||||||
self.controller:DisableMiningBelow()
|
self.controller:DisableMiningBelow()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Excavates a single WxH slice of configured length (EAST to WEST).
|
---Excavates a single WxH slice of configured length (EAST to WEST).
|
||||||
---Will leave the turtle in its starting position.
|
---Will leave the turtle in its starting position.
|
||||||
---Only unstocks/refuels if required.
|
---Only unstocks/refuels if required.
|
||||||
@ -153,7 +145,6 @@ function ExcavationController:Excavate_WxHxL(center_slice)
|
|||||||
self.controller:MoveBack()
|
self.controller:MoveBack()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Excavates all slices.
|
---Excavates all slices.
|
||||||
---Will leave the turtle refueled and unstocked in its 0x0x0 position.
|
---Will leave the turtle refueled and unstocked in its 0x0x0 position.
|
||||||
function ExcavationController:Excavate()
|
function ExcavationController:Excavate()
|
||||||
@ -260,12 +251,10 @@ function ExcavationController:Excavate()
|
|||||||
self.audio:StopPlaying()
|
self.audio:StopPlaying()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Management Methods
|
-- Management Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function ExcavationController:Configure()
|
function ExcavationController:Configure()
|
||||||
local config_complete = false
|
local config_complete = false
|
||||||
|
|
||||||
@ -299,23 +288,31 @@ function ExcavationController:Configure()
|
|||||||
local mined_width = mined_width_left + mined_width_right + self.config.center_slice_width
|
local mined_width = mined_width_left + mined_width_right + self.config.center_slice_width
|
||||||
|
|
||||||
print("Configuration complete!")
|
print("Configuration complete!")
|
||||||
print(("Mining area spans %d x %d x %d (width x height x forward), totalling %d blocks."):format(
|
print(
|
||||||
padded_width, self.config.slice_height, self.config.slice_length, padded_width * self.config.slice_height * self.config.slice_length
|
("Mining area spans %d x %d x %d (width x height x forward), totalling %d blocks."):format(
|
||||||
))
|
padded_width,
|
||||||
print(("Turtle will mine %d block wide slices (%d wide in the center) with %d blocks of padding, totalling %d mined blocks."):format(
|
self.config.slice_height,
|
||||||
self.config.slice_width, self.config.center_slice_width, self.config.slice_padding, mined_width * self.config.slice_height * self.config.slice_length
|
self.config.slice_length,
|
||||||
))
|
padded_width * self.config.slice_height * self.config.slice_length
|
||||||
|
)
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
("Turtle will mine %d block wide slices (%d wide in the center) with %d blocks of padding, totalling %d mined blocks."):format(
|
||||||
|
self.config.slice_width,
|
||||||
|
self.config.center_slice_width,
|
||||||
|
self.config.slice_padding,
|
||||||
|
mined_width * self.config.slice_height * self.config.slice_length
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Do you want to accept the configuration (enter 1, otherwise 0)?")
|
print("Do you want to accept the configuration (enter 1, otherwise 0)?")
|
||||||
config_complete = tonumber(io.read()) == 1
|
config_complete = tonumber(io.read()) == 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Main Method
|
-- Main Method
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function ExcavationController:Run()
|
function ExcavationController:Run()
|
||||||
self.controller:Configure()
|
self.controller:Configure()
|
||||||
self:Configure()
|
self:Configure()
|
||||||
@ -325,8 +322,9 @@ function ExcavationController:Run()
|
|||||||
turtle.refuel()
|
turtle.refuel()
|
||||||
self.controller:RefuelIfEmpty()
|
self.controller:RefuelIfEmpty()
|
||||||
|
|
||||||
parallel.waitForAll(function() self:Excavate() end, self.audio:PlayAudioFactory("bangarang"))
|
parallel.waitForAll(function()
|
||||||
|
self:Excavate()
|
||||||
|
end, self.audio:PlayAudioFactory("bangarang"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return ExcavationController
|
return ExcavationController
|
||||||
@ -6,7 +6,6 @@ local TurtleController = require("controller.turtle_controller")
|
|||||||
local TestingController = {}
|
local TestingController = {}
|
||||||
TestingController.__index = TestingController
|
TestingController.__index = TestingController
|
||||||
|
|
||||||
|
|
||||||
---@return TestingController
|
---@return TestingController
|
||||||
function TestingController:Create()
|
function TestingController:Create()
|
||||||
local t = {}
|
local t = {}
|
||||||
@ -17,12 +16,10 @@ function TestingController:Create()
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Behavior Methods
|
-- Behavior Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestRelativeMovementWithRelativeRotation()
|
function TestingController:TestRelativeMovementWithRelativeRotation()
|
||||||
print("Testing relative movement with relative rotation...")
|
print("Testing relative movement with relative rotation...")
|
||||||
|
|
||||||
@ -41,14 +38,18 @@ function TestingController:TestRelativeMovementWithRelativeRotation()
|
|||||||
self.controller:TurnRelative(1) -- N: BackCenter (Center)
|
self.controller:TurnRelative(1) -- N: BackCenter (Center)
|
||||||
|
|
||||||
print("The turtle should be in its original location.")
|
print("The turtle should be in its original location.")
|
||||||
print(("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
print(
|
||||||
self.controller.position.x, self.controller.position.y, self.controller.position.z, self.controller.position.dir
|
("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
||||||
))
|
self.controller.position.x,
|
||||||
|
self.controller.position.y,
|
||||||
|
self.controller.position.z,
|
||||||
|
self.controller.position.dir
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Press Enter to continue")
|
print("Press Enter to continue")
|
||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestRelativeMovementWithAbsoluteRotation()
|
function TestingController:TestRelativeMovementWithAbsoluteRotation()
|
||||||
print("Testing relative movement with absolute rotation...")
|
print("Testing relative movement with absolute rotation...")
|
||||||
|
|
||||||
@ -67,14 +68,18 @@ function TestingController:TestRelativeMovementWithAbsoluteRotation()
|
|||||||
self.controller:TurnToDirection(Direction.NORTH) -- N: BackCenter (Center)
|
self.controller:TurnToDirection(Direction.NORTH) -- N: BackCenter (Center)
|
||||||
|
|
||||||
print("The turtle should be in its original location.")
|
print("The turtle should be in its original location.")
|
||||||
print(("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
print(
|
||||||
self.controller.position.x, self.controller.position.y, self.controller.position.z, self.controller.position.dir
|
("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
||||||
))
|
self.controller.position.x,
|
||||||
|
self.controller.position.y,
|
||||||
|
self.controller.position.z,
|
||||||
|
self.controller.position.dir
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Press Enter to continue")
|
print("Press Enter to continue")
|
||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestAbsoluteMovementWithoutStack()
|
function TestingController:TestAbsoluteMovementWithoutStack()
|
||||||
print("Testing absolute movement without stack...")
|
print("Testing absolute movement without stack...")
|
||||||
|
|
||||||
@ -87,14 +92,18 @@ function TestingController:TestAbsoluteMovementWithoutStack()
|
|||||||
self.controller:MoveToPosition(0, 0, 0, Direction.NORTH) -- N: BackCenter (Center)
|
self.controller:MoveToPosition(0, 0, 0, Direction.NORTH) -- N: BackCenter (Center)
|
||||||
|
|
||||||
print("The turtle should be in its original location.")
|
print("The turtle should be in its original location.")
|
||||||
print(("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
print(
|
||||||
self.controller.position.x, self.controller.position.y, self.controller.position.z, self.controller.position.dir
|
("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
||||||
))
|
self.controller.position.x,
|
||||||
|
self.controller.position.y,
|
||||||
|
self.controller.position.z,
|
||||||
|
self.controller.position.dir
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Press Enter to continue")
|
print("Press Enter to continue")
|
||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestAbsoluteMovementWithStack()
|
function TestingController:TestAbsoluteMovementWithStack()
|
||||||
print("Testing absolute movement with stack...")
|
print("Testing absolute movement with stack...")
|
||||||
|
|
||||||
@ -105,14 +114,18 @@ function TestingController:TestAbsoluteMovementWithStack()
|
|||||||
self.controller:MoveBack() -- N: BotCenter (Center)
|
self.controller:MoveBack() -- N: BotCenter (Center)
|
||||||
|
|
||||||
print("The turtle should be in its original location.")
|
print("The turtle should be in its original location.")
|
||||||
print(("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
print(
|
||||||
self.controller.position.x, self.controller.position.y, self.controller.position.z, self.controller.position.dir
|
("The turtle has internal position (%d, %d, %d) and internal direction %d"):format(
|
||||||
))
|
self.controller.position.x,
|
||||||
|
self.controller.position.y,
|
||||||
|
self.controller.position.z,
|
||||||
|
self.controller.position.dir
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Press Enter to continue")
|
print("Press Enter to continue")
|
||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestUnstocking()
|
function TestingController:TestUnstocking()
|
||||||
print("Testing inventory unloading...")
|
print("Testing inventory unloading...")
|
||||||
|
|
||||||
@ -130,7 +143,6 @@ function TestingController:TestUnstocking()
|
|||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TestingController:TestRefueling()
|
function TestingController:TestRefueling()
|
||||||
print("Testing refueling...")
|
print("Testing refueling...")
|
||||||
|
|
||||||
@ -148,12 +160,10 @@ function TestingController:TestRefueling()
|
|||||||
_ = io.read()
|
_ = io.read()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Main Method
|
-- Main Method
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function TestingController:Run()
|
function TestingController:Run()
|
||||||
self.controller:Configure()
|
self.controller:Configure()
|
||||||
self.controller:DisableMiningForward()
|
self.controller:DisableMiningForward()
|
||||||
@ -207,5 +217,4 @@ function TestingController:Run()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return TestingController
|
return TestingController
|
||||||
@ -14,18 +14,15 @@ local Stack = require("lib.stack")
|
|||||||
local TurtleController = {}
|
local TurtleController = {}
|
||||||
TurtleController.__index = TurtleController
|
TurtleController.__index = TurtleController
|
||||||
|
|
||||||
|
|
||||||
---@return TurtleController
|
---@return TurtleController
|
||||||
function TurtleController:Create()
|
function TurtleController:Create()
|
||||||
local t = {}
|
local t = {}
|
||||||
setmetatable(t, TurtleController)
|
setmetatable(t, TurtleController)
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Fields
|
-- Fields
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
t.config = {
|
t.config = {
|
||||||
fuel_direction = Direction.EAST,
|
fuel_direction = Direction.EAST,
|
||||||
fuel_name = turtle.getItemDetail(1) == nil and "" or turtle.getItemDetail(1).name,
|
fuel_name = turtle.getItemDetail(1) == nil and "" or turtle.getItemDetail(1).name,
|
||||||
@ -39,16 +36,13 @@ function TurtleController:Create()
|
|||||||
t.mine_above = false
|
t.mine_above = false
|
||||||
t.mine_below = false
|
t.mine_below = false
|
||||||
|
|
||||||
|
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Movement Methods
|
-- Movement Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
---Positive numbers turn clockwise, negative numbers turn counterclockwise
|
---Positive numbers turn clockwise, negative numbers turn counterclockwise
|
||||||
---@param number_of_turns number
|
---@param number_of_turns number
|
||||||
function TurtleController:TurnRelative(number_of_turns)
|
function TurtleController:TurnRelative(number_of_turns)
|
||||||
@ -75,7 +69,6 @@ function TurtleController:TurnRelative(number_of_turns)
|
|||||||
self.position.dir = (self.position.dir + number_of_turns) % 4
|
self.position.dir = (self.position.dir + number_of_turns) % 4
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param direction Direction
|
---@param direction Direction
|
||||||
function TurtleController:TurnToDirection(direction)
|
function TurtleController:TurnToDirection(direction)
|
||||||
if self.position.dir == direction then
|
if self.position.dir == direction then
|
||||||
@ -85,7 +78,6 @@ function TurtleController:TurnToDirection(direction)
|
|||||||
self:TurnRelative(direction - self.position.dir)
|
self:TurnRelative(direction - self.position.dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Move forward by a number of blocks depending on the current rotation
|
---Move forward by a number of blocks depending on the current rotation
|
||||||
---@param number_of_blocks number
|
---@param number_of_blocks number
|
||||||
---@param skip_unstocking boolean | nil
|
---@param skip_unstocking boolean | nil
|
||||||
@ -179,13 +171,11 @@ function TurtleController:MoveVertical(number_of_blocks, skip_unstocking, skip_r
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
---Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
||||||
function TurtleController:StorePosition()
|
function TurtleController:StorePosition()
|
||||||
self.last_positions:Push(Position:Copy(self.position))
|
self.last_positions:Push(Position:Copy(self.position))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Move to an absolute position. Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
---Move to an absolute position. Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
||||||
---@param x number The EAST/WEST axis (grows from WEST to EAST)
|
---@param x number The EAST/WEST axis (grows from WEST to EAST)
|
||||||
---@param y number The UP/DOWN axis (grows from DOWN to UP)
|
---@param y number The UP/DOWN axis (grows from DOWN to UP)
|
||||||
@ -227,7 +217,6 @@ function TurtleController:MoveToPosition(x, y, z, dir, skip_unstocking, skip_ref
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Move by a vector. Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
---Move by a vector. Stores the current position on the stack so we can return using TurtleController:MoveBack()
|
||||||
---@param x number The EAST/WEST axis (grows from WEST to EAST)
|
---@param x number The EAST/WEST axis (grows from WEST to EAST)
|
||||||
---@param y number The UP/DOWN axis (grows from DOWN to UP)
|
---@param y number The UP/DOWN axis (grows from DOWN to UP)
|
||||||
@ -236,10 +225,16 @@ end
|
|||||||
---@param skip_unstocking boolean | nil
|
---@param skip_unstocking boolean | nil
|
||||||
---@param skip_refueling boolean | nil
|
---@param skip_refueling boolean | nil
|
||||||
function TurtleController:MoveByVector(x, y, z, dir, skip_unstocking, skip_refueling)
|
function TurtleController:MoveByVector(x, y, z, dir, skip_unstocking, skip_refueling)
|
||||||
self:MoveToPosition(self.position.x + x, self.position.y + y, self.position.z + z, dir, skip_unstocking, skip_refueling)
|
self:MoveToPosition(
|
||||||
|
self.position.x + x,
|
||||||
|
self.position.y + y,
|
||||||
|
self.position.z + z,
|
||||||
|
dir,
|
||||||
|
skip_unstocking,
|
||||||
|
skip_refueling
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Move to the previously stored position and pop this position from the stack.
|
---Move to the previously stored position and pop this position from the stack.
|
||||||
---@param skip_unstocking boolean | nil
|
---@param skip_unstocking boolean | nil
|
||||||
---@param skip_refueling boolean | nil
|
---@param skip_refueling boolean | nil
|
||||||
@ -249,19 +244,24 @@ function TurtleController:MoveBack(skip_unstocking, skip_refueling)
|
|||||||
if last_position == nil then
|
if last_position == nil then
|
||||||
error("Failed to obtain last_position to move back!")
|
error("Failed to obtain last_position to move back!")
|
||||||
else
|
else
|
||||||
self:MoveToPosition(last_position.x, last_position.y, last_position.z, last_position.dir, skip_unstocking, skip_refueling)
|
self:MoveToPosition(
|
||||||
|
last_position.x,
|
||||||
|
last_position.y,
|
||||||
|
last_position.z,
|
||||||
|
last_position.dir,
|
||||||
|
skip_unstocking,
|
||||||
|
skip_refueling
|
||||||
|
)
|
||||||
|
|
||||||
-- Pop the stack because MoveToPosition pushes our current position
|
-- Pop the stack because MoveToPosition pushes our current position
|
||||||
self.last_positions:Pop()
|
self.last_positions:Pop()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Inventory Methods
|
-- Inventory Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function TurtleController:HasFuel()
|
function TurtleController:HasFuel()
|
||||||
local level = turtle.getFuelLevel()
|
local level = turtle.getFuelLevel()
|
||||||
@ -270,7 +270,6 @@ function TurtleController:HasFuel()
|
|||||||
return level > distance_home + self.config.refuel_safety_margin
|
return level > distance_home + self.config.refuel_safety_margin
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function TurtleController:HasInventorySpace()
|
function TurtleController:HasInventorySpace()
|
||||||
for slot = 1, 16 do
|
for slot = 1, 16 do
|
||||||
@ -282,7 +281,6 @@ function TurtleController:HasInventorySpace()
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param slot number
|
---@param slot number
|
||||||
---@param count number
|
---@param count number
|
||||||
---@return boolean
|
---@return boolean
|
||||||
@ -297,7 +295,6 @@ function TurtleController:SuckItem(slot, count)
|
|||||||
return sucked
|
return sucked
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param slot number
|
---@param slot number
|
||||||
---@param count number
|
---@param count number
|
||||||
---@return boolean
|
---@return boolean
|
||||||
@ -312,7 +309,6 @@ function TurtleController:DropItem(slot, count)
|
|||||||
return dropped
|
return dropped
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:DropInventory()
|
function TurtleController:DropInventory()
|
||||||
print("Dropping inventory into chest...")
|
print("Dropping inventory into chest...")
|
||||||
|
|
||||||
@ -324,7 +320,6 @@ function TurtleController:DropInventory()
|
|||||||
turtle.select(1)
|
turtle.select(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param skip_inventory_check boolean | nil
|
---@param skip_inventory_check boolean | nil
|
||||||
function TurtleController:UnstockIfFull(skip_inventory_check)
|
function TurtleController:UnstockIfFull(skip_inventory_check)
|
||||||
skip_inventory_check = skip_inventory_check or false
|
skip_inventory_check = skip_inventory_check or false
|
||||||
@ -344,7 +339,6 @@ function TurtleController:UnstockIfFull(skip_inventory_check)
|
|||||||
self:MoveBack(true, true)
|
self:MoveBack(true, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param skip_fuel_check boolean | nil
|
---@param skip_fuel_check boolean | nil
|
||||||
function TurtleController:RefuelIfEmpty(skip_fuel_check)
|
function TurtleController:RefuelIfEmpty(skip_fuel_check)
|
||||||
skip_fuel_check = skip_fuel_check or false
|
skip_fuel_check = skip_fuel_check or false
|
||||||
@ -387,15 +381,19 @@ function TurtleController:RefuelIfEmpty(skip_fuel_check)
|
|||||||
|
|
||||||
self:MoveBack(true, true)
|
self:MoveBack(true, true)
|
||||||
|
|
||||||
print(("Refuelled %d units, current level is %d (old level was %d)"):format(after_level - before_level, after_level, before_level))
|
print(
|
||||||
|
("Refuelled %d units, current level is %d (old level was %d)"):format(
|
||||||
|
after_level - before_level,
|
||||||
|
after_level,
|
||||||
|
before_level
|
||||||
|
)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
-- Management Methods
|
-- Management Methods
|
||||||
-----------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:Configure()
|
function TurtleController:Configure()
|
||||||
local config_complete = false
|
local config_complete = false
|
||||||
|
|
||||||
@ -422,41 +420,38 @@ function TurtleController:Configure()
|
|||||||
self.config.storage_direction = tonumber(io.read()) or Direction.EAST
|
self.config.storage_direction = tonumber(io.read()) or Direction.EAST
|
||||||
|
|
||||||
print("Configuration complete!")
|
print("Configuration complete!")
|
||||||
print(("Turtle will consume \"%s\" for fuel. Put the desired fuel into the first slot to change."):format(self.config.fuel_name))
|
print(
|
||||||
|
('Turtle will consume "%s" for fuel. Put the desired fuel into the first slot to change.'):format(
|
||||||
|
self.config.fuel_name
|
||||||
|
)
|
||||||
|
)
|
||||||
print("Do you want to accept the configuration (enter 1, otherwise 0)?")
|
print("Do you want to accept the configuration (enter 1, otherwise 0)?")
|
||||||
config_complete = tonumber(io.read()) == 1
|
config_complete = tonumber(io.read()) == 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:EnableMiningForward()
|
function TurtleController:EnableMiningForward()
|
||||||
self.mine_forward = true
|
self.mine_forward = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:DisableMiningForward()
|
function TurtleController:DisableMiningForward()
|
||||||
self.mine_forward = false
|
self.mine_forward = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:EnableMiningAbove()
|
function TurtleController:EnableMiningAbove()
|
||||||
self.mine_above = true
|
self.mine_above = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:DisableMiningAbove()
|
function TurtleController:DisableMiningAbove()
|
||||||
self.mine_above = false
|
self.mine_above = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:EnableMiningBelow()
|
function TurtleController:EnableMiningBelow()
|
||||||
self.mine_below = true
|
self.mine_below = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function TurtleController:DisableMiningBelow()
|
function TurtleController:DisableMiningBelow()
|
||||||
self.mine_below = false
|
self.mine_below = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return TurtleController
|
return TurtleController
|
||||||
@ -3,7 +3,7 @@ local Direction = {
|
|||||||
NORTH = 0,
|
NORTH = 0,
|
||||||
EAST = 1,
|
EAST = 1,
|
||||||
SOUTH = 2,
|
SOUTH = 2,
|
||||||
WEST = 3
|
WEST = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
return Direction
|
return Direction
|
||||||
@ -8,7 +8,6 @@ local Direction = require("lib.direction")
|
|||||||
local Position = {}
|
local Position = {}
|
||||||
Position.__index = Position
|
Position.__index = Position
|
||||||
|
|
||||||
|
|
||||||
---@return Position
|
---@return Position
|
||||||
function Position:Empty()
|
function Position:Empty()
|
||||||
local t = {}
|
local t = {}
|
||||||
|
|||||||
@ -1,11 +1,8 @@
|
|||||||
local Position = require("lib.position")
|
|
||||||
|
|
||||||
---@class Stack
|
---@class Stack
|
||||||
---@field elements Position[]
|
---@field elements Position[]
|
||||||
local Stack = {}
|
local Stack = {}
|
||||||
Stack.__index = Stack
|
Stack.__index = Stack
|
||||||
|
|
||||||
|
|
||||||
---@return Stack
|
---@return Stack
|
||||||
function Stack:Create()
|
function Stack:Create()
|
||||||
-- stack table
|
-- stack table
|
||||||
@ -18,7 +15,6 @@ function Stack:Create()
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param element Position
|
---@param element Position
|
||||||
function Stack:Push(element)
|
function Stack:Push(element)
|
||||||
if element == nil or element == {} then
|
if element == nil or element == {} then
|
||||||
@ -29,7 +25,6 @@ function Stack:Push(element)
|
|||||||
table.insert(self.elements, element)
|
table.insert(self.elements, element)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@return Position | nil
|
---@return Position | nil
|
||||||
function Stack:Pop()
|
function Stack:Pop()
|
||||||
if self:Count() == 0 then
|
if self:Count() == 0 then
|
||||||
@ -40,7 +35,6 @@ function Stack:Pop()
|
|||||||
return table.remove(self.elements)
|
return table.remove(self.elements)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@return Position | nil
|
---@return Position | nil
|
||||||
function Stack:Peek()
|
function Stack:Peek()
|
||||||
if self:Count() == 0 then
|
if self:Count() == 0 then
|
||||||
@ -51,11 +45,9 @@ function Stack:Peek()
|
|||||||
return self.elements[#self.elements]
|
return self.elements[#self.elements]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@return number
|
---@return number
|
||||||
function Stack:Count()
|
function Stack:Count()
|
||||||
return #self.elements
|
return #self.elements
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return Stack
|
return Stack
|
||||||
2
main.lua
2
main.lua
@ -2,7 +2,6 @@ local TestingController = require("controller.testing_controller")
|
|||||||
local ExcavationController = require("controller.excavation_controller")
|
local ExcavationController = require("controller.excavation_controller")
|
||||||
local AudioController = require("controller.audio_controller")
|
local AudioController = require("controller.audio_controller")
|
||||||
|
|
||||||
|
|
||||||
local controllers = {
|
local controllers = {
|
||||||
TestingController:Create(),
|
TestingController:Create(),
|
||||||
ExcavationController:Create(),
|
ExcavationController:Create(),
|
||||||
@ -22,7 +21,6 @@ end
|
|||||||
|
|
||||||
controllers[choice]:Run()
|
controllers[choice]:Run()
|
||||||
|
|
||||||
|
|
||||||
-- TODO: StackOverflow once the inventory is full, unstocking doesn't work...
|
-- TODO: StackOverflow once the inventory is full, unstocking doesn't work...
|
||||||
-- Is the InventoryFull check wrong?
|
-- Is the InventoryFull check wrong?
|
||||||
-- TODO: Test if there's a chest when dropping/sucking/mining (don't mine chests!)
|
-- TODO: Test if there's a chest when dropping/sucking/mining (don't mine chests!)
|
||||||
|
|||||||
Reference in New Issue
Block a user