Implement initial NetheriteController
This commit is contained in:
148
controller/netherite_controller.lua
Normal file
148
controller/netherite_controller.lua
Normal file
@ -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
|
||||||
@ -13,6 +13,7 @@ local TurtleController = {}
|
|||||||
TurtleController.__index = 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!)
|
-- TODO: Test if there's a chest when dropping/sucking/mining (don't mine chests!)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -39,8 +39,6 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function VolumeExcavationController:Excavate()
|
function VolumeExcavationController:Excavate()
|
||||||
print(("Turtle is mining layer at y=%d..."):format(self.controller.position.y))
|
|
||||||
|
|
||||||
-- Enter the excavation area
|
-- Enter the excavation area
|
||||||
self.controller:EnableMining()
|
self.controller:EnableMining()
|
||||||
self.controller:MoveRelative(1)
|
self.controller:MoveRelative(1)
|
||||||
|
|||||||
3
main.lua
3
main.lua
@ -1,15 +1,18 @@
|
|||||||
local TestingController = require("controller.testing_controller")
|
local TestingController = require("controller.testing_controller")
|
||||||
local VolumeExcavationController = require("controller.volume_excavation_controller")
|
local VolumeExcavationController = require("controller.volume_excavation_controller")
|
||||||
|
local NetheriteController = require("controller.netherite_controller")
|
||||||
|
|
||||||
|
|
||||||
local controllers = {
|
local controllers = {
|
||||||
TestingController:Create(),
|
TestingController:Create(),
|
||||||
VolumeExcavationController:Create(),
|
VolumeExcavationController:Create(),
|
||||||
|
NetheriteController: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: Netherite Excavation")
|
||||||
|
|
||||||
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