1
Files
lua-computercraft/controller/netherite_controller.lua

148 lines
5.1 KiB
Lua

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