Play music when excavating
This commit is contained in:
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -2,6 +2,8 @@
|
|||||||
"Lua.diagnostics.globals": [
|
"Lua.diagnostics.globals": [
|
||||||
"printError",
|
"printError",
|
||||||
"turtle",
|
"turtle",
|
||||||
"shell"
|
"shell",
|
||||||
|
"peripheral",
|
||||||
|
"parallel"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
BIN
audio/bangarang.dfpwm
Normal file
BIN
audio/bangarang.dfpwm
Normal file
Binary file not shown.
96
controller/audio_controller.lua
Normal file
96
controller/audio_controller.lua
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
local dfpwm = require("cc.audio.dfpwm")
|
||||||
|
local Direction = require("lib.direction")
|
||||||
|
|
||||||
|
---@alias AudioControllerConfig {buffer_length_seconds: number}
|
||||||
|
|
||||||
|
---@class AudioController
|
||||||
|
---@field config AudioControllerConfig
|
||||||
|
---@field play boolean
|
||||||
|
local AudioController = {}
|
||||||
|
AudioController.__index = AudioController
|
||||||
|
|
||||||
|
|
||||||
|
---@return AudioController
|
||||||
|
function AudioController:Create()
|
||||||
|
local t = {}
|
||||||
|
setmetatable(t, AudioController)
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Fields
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
t.config = {
|
||||||
|
buffer_length_seconds = 1
|
||||||
|
}
|
||||||
|
t.play = false
|
||||||
|
|
||||||
|
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Audio Methods
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
function AudioController:PlayAudio(filename)
|
||||||
|
self.play = true
|
||||||
|
|
||||||
|
local decoder = dfpwm.make_decoder()
|
||||||
|
while self.play do
|
||||||
|
for chunk in io.lines(("audio/%s.dfpwm"):format(filename), self.config.buffer_length_seconds * 1024) do
|
||||||
|
if not self.play then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
local buffer = decoder(chunk)
|
||||||
|
|
||||||
|
while not self:GetSpeaker().playAudio(buffer) do
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
|
os.pullEvent("speaker_audio_empty")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function AudioController:PlayAudioFactory(filename)
|
||||||
|
local function play()
|
||||||
|
self:PlayAudio(filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
return play
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Management Methods
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
---@return table | nil
|
||||||
|
function AudioController:GetSpeaker()
|
||||||
|
return peripheral.find("speaker")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function AudioController:StopPlaying()
|
||||||
|
self.play = false
|
||||||
|
self:GetSpeaker().stop()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
-- Main Method
|
||||||
|
-----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
function AudioController:Run()
|
||||||
|
-- self:Configure()
|
||||||
|
|
||||||
|
self:PlayAudio("bangarang")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return AudioController
|
||||||
@ -1,10 +1,12 @@
|
|||||||
local Direction = require("lib.direction")
|
local Direction = require("lib.direction")
|
||||||
local TurtleController = require("controller.turtle_controller")
|
local TurtleController = require("controller.turtle_controller")
|
||||||
|
local AudioController = require("controller.audio_controller")
|
||||||
|
|
||||||
---@alias ExcavationControllerConfig {slice_length: number, slice_height: number, center_slice_width: number, slice_width: number, slice_padding: number, slices_left: number, slices_right: number}
|
---@alias ExcavationControllerConfig {slice_length: number, slice_height: number, center_slice_width: number, slice_width: number, slice_padding: number, slices_left: number, slices_right: number}
|
||||||
|
|
||||||
---@class ExcavationController
|
---@class ExcavationController
|
||||||
---@field controller TurtleController
|
---@field controller TurtleController
|
||||||
|
---@field audio AudioController
|
||||||
---@field config ExcavationControllerConfig
|
---@field config ExcavationControllerConfig
|
||||||
local ExcavationController = {}
|
local ExcavationController = {}
|
||||||
ExcavationController.__index = ExcavationController
|
ExcavationController.__index = ExcavationController
|
||||||
@ -22,6 +24,7 @@ function ExcavationController:Create()
|
|||||||
|
|
||||||
|
|
||||||
t.controller = TurtleController:Create()
|
t.controller = TurtleController:Create()
|
||||||
|
t.audio = AudioController:Create()
|
||||||
t.config = {
|
t.config = {
|
||||||
slice_length = 0,
|
slice_length = 0,
|
||||||
slice_height = 0,
|
slice_height = 0,
|
||||||
@ -176,80 +179,85 @@ function ExcavationController:Excavate()
|
|||||||
self:Excavate_WxHxL(true)
|
self:Excavate_WxHxL(true)
|
||||||
self.controller:MoveBack() -- (0, 0, 1)
|
self.controller:MoveBack() -- (0, 0, 1)
|
||||||
|
|
||||||
-- Move to right slices starting location
|
if self.config.slices_right > 0 then
|
||||||
local padded_width_right = self.config.slices_right * (self.config.slice_width + self.config.slice_padding)
|
-- Move to right slices starting location
|
||||||
self.controller:StorePosition()
|
local padded_width_right = self.config.slices_right * (self.config.slice_width + self.config.slice_padding)
|
||||||
self.controller:TurnToDirection(Direction.EAST)
|
self.controller:StorePosition()
|
||||||
self.controller:EnableMiningForward()
|
self.controller:TurnToDirection(Direction.EAST)
|
||||||
self.controller:EnableMiningAbove()
|
self.controller:EnableMiningForward()
|
||||||
self.controller:MoveForward(center_width_right + padded_width_right)
|
self.controller:EnableMiningAbove()
|
||||||
self.controller:DisableMiningForward()
|
self.controller:MoveForward(center_width_right + padded_width_right)
|
||||||
self.controller:DisableMiningAbove()
|
self.controller:DisableMiningForward()
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
self.controller:DisableMiningAbove()
|
||||||
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
|
|
||||||
-- Excavate right slices
|
-- Excavate right slices
|
||||||
print("Excavating right slices")
|
print("Excavating right slices")
|
||||||
for i = 1,self.config.slices_right do
|
for i = 1,self.config.slices_right do
|
||||||
self:Excavate_WxHxL()
|
self:Excavate_WxHxL()
|
||||||
|
|
||||||
if i == self.config.slices_right then
|
if i == self.config.slices_right then
|
||||||
break
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Move to the next slice
|
||||||
|
self.controller:TurnToDirection(Direction.WEST)
|
||||||
|
self.controller:EnableMiningForward()
|
||||||
|
self.controller:EnableMiningAbove()
|
||||||
|
self.controller:MoveForward(self.config.slice_width + self.config.slice_padding)
|
||||||
|
self.controller:DisableMiningForward()
|
||||||
|
self.controller:DisableMiningAbove()
|
||||||
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
end
|
end
|
||||||
|
self.controller:MoveBack() -- (0, 0, 1)
|
||||||
-- Move to the next slice
|
|
||||||
self.controller:TurnToDirection(Direction.WEST)
|
|
||||||
self.controller:EnableMiningForward()
|
|
||||||
self.controller:EnableMiningAbove()
|
|
||||||
self.controller:MoveForward(self.config.slice_width + self.config.slice_padding)
|
|
||||||
self.controller:DisableMiningForward()
|
|
||||||
self.controller:DisableMiningAbove()
|
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
|
||||||
end
|
end
|
||||||
self.controller:MoveBack() -- (0, 0, 1)
|
|
||||||
|
|
||||||
-- Move to left slices starting location
|
if self.config.slices_left > 0 then
|
||||||
local center_width_left = self.config.center_slice_width - center_width_right
|
-- Move to left slices starting location
|
||||||
self.controller:StorePosition()
|
local center_width_left = self.config.center_slice_width - center_width_right
|
||||||
self.controller:TurnToDirection(Direction.WEST)
|
self.controller:StorePosition()
|
||||||
self.controller:EnableMiningForward()
|
|
||||||
self.controller:EnableMiningAbove()
|
|
||||||
self.controller:MoveForward(center_width_left)
|
|
||||||
self.controller:DisableMiningForward()
|
|
||||||
self.controller:DisableMiningAbove()
|
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
|
||||||
|
|
||||||
-- Excavate left slices
|
|
||||||
print("Excavate left slices")
|
|
||||||
for i = 1,self.config.slices_left do
|
|
||||||
-- Move to the next slice
|
|
||||||
self.controller:TurnToDirection(Direction.WEST)
|
self.controller:TurnToDirection(Direction.WEST)
|
||||||
self.controller:EnableMiningForward()
|
self.controller:EnableMiningForward()
|
||||||
self.controller:EnableMiningAbove()
|
self.controller:EnableMiningAbove()
|
||||||
self.controller:MoveForward(self.config.slice_padding)
|
self.controller:MoveForward(center_width_left)
|
||||||
self.controller:DisableMiningForward()
|
self.controller:DisableMiningForward()
|
||||||
self.controller:DisableMiningAbove()
|
self.controller:DisableMiningAbove()
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
|
|
||||||
self:Excavate_WxHxL()
|
-- Excavate left slices
|
||||||
|
print("Excavate left slices")
|
||||||
|
for i = 1,self.config.slices_left do
|
||||||
|
-- Move to the next slice
|
||||||
|
self.controller:TurnToDirection(Direction.WEST)
|
||||||
|
self.controller:EnableMiningForward()
|
||||||
|
self.controller:EnableMiningAbove()
|
||||||
|
self.controller:MoveForward(self.config.slice_padding)
|
||||||
|
self.controller:DisableMiningForward()
|
||||||
|
self.controller:DisableMiningAbove()
|
||||||
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
|
|
||||||
if i == self.config.slices_left then
|
self:Excavate_WxHxL()
|
||||||
break
|
|
||||||
|
if i == self.config.slices_left then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
self.controller:TurnToDirection(Direction.WEST)
|
||||||
|
self.controller:EnableMiningForward()
|
||||||
|
self.controller:EnableMiningAbove()
|
||||||
|
self.controller:MoveForward(self.config.slice_width)
|
||||||
|
self.controller:DisableMiningForward()
|
||||||
|
self.controller:DisableMiningAbove()
|
||||||
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
end
|
end
|
||||||
|
self.controller:MoveBack() -- (0, 0, 1)
|
||||||
self.controller:TurnToDirection(Direction.WEST)
|
|
||||||
self.controller:EnableMiningForward()
|
|
||||||
self.controller:EnableMiningAbove()
|
|
||||||
self.controller:MoveForward(self.config.slice_width)
|
|
||||||
self.controller:DisableMiningForward()
|
|
||||||
self.controller:DisableMiningAbove()
|
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
|
||||||
end
|
end
|
||||||
self.controller:MoveBack() -- (0, 0, 1)
|
|
||||||
|
|
||||||
-- Finish up
|
-- Finish up
|
||||||
self.controller:MoveToPosition(0, 0, 0, self.controller.config.storage_direction)
|
self.controller:MoveToPosition(0, 0, 0, self.controller.config.storage_direction)
|
||||||
self.controller:DropInventory()
|
self.controller:DropInventory()
|
||||||
self.controller:TurnToDirection(Direction.NORTH)
|
self.controller:TurnToDirection(Direction.NORTH)
|
||||||
|
self.audio:StopPlaying()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -317,7 +325,7 @@ function ExcavationController:Run()
|
|||||||
turtle.refuel()
|
turtle.refuel()
|
||||||
self.controller:RefuelIfEmpty()
|
self.controller:RefuelIfEmpty()
|
||||||
|
|
||||||
self:Excavate()
|
parallel.waitForAll(function() self:Excavate() end, self.audio:PlayAudioFactory("bangarang"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
3
main.lua
3
main.lua
@ -1,15 +1,18 @@
|
|||||||
local TestingController = require("controller.testing_controller")
|
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 controllers = {
|
local controllers = {
|
||||||
TestingController:Create(),
|
TestingController:Create(),
|
||||||
ExcavationController:Create(),
|
ExcavationController:Create(),
|
||||||
|
AudioController:Create(),
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Multiple controllers are available:")
|
print("Multiple controllers are available:")
|
||||||
print("1: Testing Mode")
|
print("1: Testing Mode")
|
||||||
print("2: Volume Excavation")
|
print("2: Volume Excavation")
|
||||||
|
print("3: Play Bangarang")
|
||||||
|
|
||||||
local choice = 0
|
local choice = 0
|
||||||
while choice < 1 or choice > #controllers do
|
while choice < 1 or choice > #controllers do
|
||||||
|
|||||||
Reference in New Issue
Block a user