From 749c32325f35864e675a1d32aacc3c700835b3f9 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sun, 5 Oct 2025 23:30:31 +0200 Subject: [PATCH] Implement initial NetheriteController --- controller/netherite_controller.lua | 148 ++++++++++++++++++++ controller/turtle_controller.lua | 1 + controller/volume_excavation_controller.lua | 2 - main.lua | 3 + 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 controller/netherite_controller.lua diff --git a/controller/netherite_controller.lua b/controller/netherite_controller.lua new file mode 100644 index 0000000..b8d3a3b --- /dev/null +++ b/controller/netherite_controller.lua @@ -0,0 +1,148 @@ +local Direction = require("lib.direction") +local TurtleController = require("controller.turtle_controller") + +---@alias NetheriteControllerConfig {mine_forward: number, slices_left: number, slices_right: number, slices_padding: number} + +---@class NetheriteController +---@field controller TurtleController +---@field config NetheriteControllerConfig +local NetheriteController = {} +NetheriteController.__index = NetheriteController + + +-- TODO: This controller can't return to unstock/refuel if stuck in the middle of a slice + + +---@return NetheriteController +function NetheriteController:Create() + local t = {} + setmetatable(t, NetheriteController) + + + ----------------------------------------------------------------------------------------------- + -- Fields + ----------------------------------------------------------------------------------------------- + + + t.controller = TurtleController:Create() + t.config = { + mine_forward = 0, + slices_left = 0, + slices_right = 0, + slices_padding = 0, + } + + + return t +end + + +----------------------------------------------------------------------------------------------- +-- Behavior Methods +----------------------------------------------------------------------------------------------- + + +function NetheriteController:Excavate() + -- Enter the excavation area + self.controller:EnableMining() + self.controller:MoveRelative(1) + + local right_padded_width = self.config.slices_right * (self.config.slices_padding + 1) + local left_padded_width = self.config.slices_left * (self.config.slices_padding + 1) + + -- Move to the bottom right corner of the layer + self.controller:TurnToDirection(Direction.EAST) + self.controller:MoveRelative(right_padded_width) + self.controller:TurnToDirection(Direction.NORTH) + self.controller:MoveRelative(self.config.mine_forward) + self.controller:TurnToDirection(Direction.WEST) + self.controller:MoveRelative(left_padded_width + right_padded_width) + self.controller:TurnToDirection(Direction.SOUTH) + self.controller:MoveRelative(self.config.mine_forward) + self.controller:TurnToDirection(Direction.EAST) + self.controller:MoveRelative(left_padded_width + right_padded_width) + self.controller:TurnToDirection(Direction.NORTH) + + -- Zig zag mine the full width from back to front + local turn_dir = -1 + for i = 1,self.config.slices_right + self.config.slices_left do + self.controller:MoveRelative(self.config.mine_forward) + + -- Skip the last turn, as we won't mine that slice + if i == self.config.slices_right + self.config.slices_left then + break + end + + self.controller:TurnRelative(turn_dir) + self.controller:MoveRelative(self.config.slices_padding + 1) + self.controller:TurnRelative(turn_dir) + + turn_dir = -1 * turn_dir + end + + if self.controller.position.z > 1 then + self.controller:TurnToDirection(Direction.SOUTH) + self.controller:MoveRelative(self.controller.position.z - 1) + self.controller:MoveToPosition(0, 0, 0, Direction.NORTH) + end + + -- Unload before doing the next layer + self.controller:TurnToDirection(self.controller.config.storage_direction) + self.controller:DropInventory() + self.controller:TurnToDirection(Direction.NORTH) +end + + +----------------------------------------------------------------------------------------------- +-- Management Methods +----------------------------------------------------------------------------------------------- + + +function NetheriteController:Configure() + local config_complete = false + + while not config_complete do + print("How many blocks should the turtle mine forward?") + self.config.mine_forward = tonumber(io.read()) or 3 + + print("How many slices should the turtle to its right?") + self.config.slices_right = tonumber(io.read()) or 3 + + print("How many blocks should the turtle mine to its left?") + self.config.slices_left = tonumber(io.read()) or 3 + + print("How many blocks should the turtle leave between the slices?") + self.config.slices_padding = tonumber(io.read()) or 3 + + local width = self.config.slices_left + self.config.slices_right + 1 + local height = 3 + + print("Configuration complete!") + print(("Turtle will mine an area of %d x %d x %d (forward x width x height, excluding padding) totalling %d blocks."):format( + self.config.mine_forward, width, height, self.config.mine_forward * width * height + )) + print("Do you want to accept the configuration (enter 1, otherwise 0)?") + config_complete = tonumber(io.read()) == 1 + end +end + + +----------------------------------------------------------------------------------------------- +-- Main Method +----------------------------------------------------------------------------------------------- + + +function NetheriteController:Run() + self.controller:Configure() + self:Configure() + + -- Consume our starting fuel and refuel to the full amount + turtle.select(1) + turtle.refuel() + self.controller:RefuelIfEmpty() + + self:Excavate() +end + + +return NetheriteController \ No newline at end of file diff --git a/controller/turtle_controller.lua b/controller/turtle_controller.lua index 6ffc753..8956760 100644 --- a/controller/turtle_controller.lua +++ b/controller/turtle_controller.lua @@ -13,6 +13,7 @@ local TurtleController = {} TurtleController.__index = TurtleController +-- TODO: StackOverflow once the inventory is full... -- TODO: Test if there's a chest when dropping/sucking/mining (don't mine chests!) diff --git a/controller/volume_excavation_controller.lua b/controller/volume_excavation_controller.lua index d0fc449..ba3551e 100644 --- a/controller/volume_excavation_controller.lua +++ b/controller/volume_excavation_controller.lua @@ -39,8 +39,6 @@ end function VolumeExcavationController:Excavate() - print(("Turtle is mining layer at y=%d..."):format(self.controller.position.y)) - -- Enter the excavation area self.controller:EnableMining() self.controller:MoveRelative(1) diff --git a/main.lua b/main.lua index 936d308..62af19b 100644 --- a/main.lua +++ b/main.lua @@ -1,15 +1,18 @@ local TestingController = require("controller.testing_controller") local VolumeExcavationController = require("controller.volume_excavation_controller") +local NetheriteController = require("controller.netherite_controller") local controllers = { TestingController:Create(), VolumeExcavationController:Create(), + NetheriteController:Create(), } print("Multiple controllers are available:") print("1: Testing Mode") print("2: Volume Excavation") +print("3: Netherite Excavation") local choice = 0 while choice < 1 or choice > #controllers do