1

Play music when excavating

This commit is contained in:
2025-10-06 23:50:29 +02:00
parent b5cb13732e
commit b71267bf42
5 changed files with 165 additions and 56 deletions

View File

@ -1,10 +1,12 @@
local Direction = require("lib.direction")
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}
---@class ExcavationController
---@field controller TurtleController
---@field audio AudioController
---@field config ExcavationControllerConfig
local ExcavationController = {}
ExcavationController.__index = ExcavationController
@ -22,6 +24,7 @@ function ExcavationController:Create()
t.controller = TurtleController:Create()
t.audio = AudioController:Create()
t.config = {
slice_length = 0,
slice_height = 0,
@ -176,80 +179,85 @@ function ExcavationController:Excavate()
self:Excavate_WxHxL(true)
self.controller:MoveBack() -- (0, 0, 1)
-- Move to right slices starting location
local padded_width_right = self.config.slices_right * (self.config.slice_width + self.config.slice_padding)
self.controller:StorePosition()
self.controller:TurnToDirection(Direction.EAST)
self.controller:EnableMiningForward()
self.controller:EnableMiningAbove()
self.controller:MoveForward(center_width_right + padded_width_right)
self.controller:DisableMiningForward()
self.controller:DisableMiningAbove()
self.controller:TurnToDirection(Direction.NORTH)
if self.config.slices_right > 0 then
-- Move to right slices starting location
local padded_width_right = self.config.slices_right * (self.config.slice_width + self.config.slice_padding)
self.controller:StorePosition()
self.controller:TurnToDirection(Direction.EAST)
self.controller:EnableMiningForward()
self.controller:EnableMiningAbove()
self.controller:MoveForward(center_width_right + padded_width_right)
self.controller:DisableMiningForward()
self.controller:DisableMiningAbove()
self.controller:TurnToDirection(Direction.NORTH)
-- Excavate right slices
print("Excavating right slices")
for i = 1,self.config.slices_right do
self:Excavate_WxHxL()
-- Excavate right slices
print("Excavating right slices")
for i = 1,self.config.slices_right do
self:Excavate_WxHxL()
if i == self.config.slices_right then
break
if i == self.config.slices_right then
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
-- 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)
self.controller:MoveBack() -- (0, 0, 1)
end
self.controller:MoveBack() -- (0, 0, 1)
-- Move to left slices starting location
local center_width_left = self.config.center_slice_width - center_width_right
self.controller:StorePosition()
self.controller:TurnToDirection(Direction.WEST)
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
if self.config.slices_left > 0 then
-- Move to left slices starting location
local center_width_left = self.config.center_slice_width - center_width_right
self.controller:StorePosition()
self.controller:TurnToDirection(Direction.WEST)
self.controller:EnableMiningForward()
self.controller:EnableMiningAbove()
self.controller:MoveForward(self.config.slice_padding)
self.controller:MoveForward(center_width_left)
self.controller:DisableMiningForward()
self.controller:DisableMiningAbove()
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
break
self:Excavate_WxHxL()
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
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)
self.controller:MoveBack() -- (0, 0, 1)
end
self.controller:MoveBack() -- (0, 0, 1)
-- Finish up
self.controller:MoveToPosition(0, 0, 0, self.controller.config.storage_direction)
self.controller:DropInventory()
self.controller:TurnToDirection(Direction.NORTH)
self.audio:StopPlaying()
end
@ -317,7 +325,7 @@ function ExcavationController:Run()
turtle.refuel()
self.controller:RefuelIfEmpty()
self:Excavate()
parallel.waitForAll(function() self:Excavate() end, self.audio:PlayAudioFactory("bangarang"))
end