From c2e63d427a4f755e72dab5d390966eb501b75b56 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Sun, 26 May 2024 02:29:51 +0200 Subject: [PATCH] feat: implement first version of tunnels-miner --- config/tunnels.lua | 5 ++ inferium-harvester.lua | 1 - install.lua | 2 + libs/turtle-utils.lua | 66 ++++++++++++++++- tunnels-miner.lua | 160 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 config/tunnels.lua create mode 100644 tunnels-miner.lua diff --git a/config/tunnels.lua b/config/tunnels.lua new file mode 100644 index 0000000..bc5e75c --- /dev/null +++ b/config/tunnels.lua @@ -0,0 +1,5 @@ +return { + DIRECTION = 'right', + DISTANCE_Z = 256, + FUEL_MARGIN = 10 +} \ No newline at end of file diff --git a/inferium-harvester.lua b/inferium-harvester.lua index b09f68e..9d2fe6f 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -4,7 +4,6 @@ local inferium = require('libs/inferium') local turtleUtils = require('libs/turtle-utils') local VERSION = "4.1.0" -local INFERIUM_SERVER = 'inferium.com' local IDLE_TIME = 2 local WAIT_ITEM_IDLE_TIME = 5 local NETWORK_TIMEOUT = 4 diff --git a/install.lua b/install.lua index a627a13..2053866 100644 --- a/install.lua +++ b/install.lua @@ -1,5 +1,6 @@ local LIST_TURTLE_FILES = { 'miner.lua', + 'tunnels-miner.lua', 'coal-crafter.lua', 'mystical-upgrader.lua', 'inferium-harvester.lua', @@ -7,6 +8,7 @@ local LIST_TURTLE_FILES = { }; local LIST_TURTLE_CONFIG_FILES = { + 'config/tunnels.lua', 'config/mining.lua' } diff --git a/libs/turtle-utils.lua b/libs/turtle-utils.lua index a1e93a9..15a33eb 100644 --- a/libs/turtle-utils.lua +++ b/libs/turtle-utils.lua @@ -188,6 +188,16 @@ turtleUtils.digAllUp = function() end end +turtleUtils.digAllDown = function() + while true do + local ok, err = turtle.digDown() + + if not ok then + return ok, err + end + end +end + turtleUtils.countFreeSlots = function() local freeSlots = 0 @@ -200,8 +210,9 @@ turtleUtils.countFreeSlots = function() return freeSlots end -turtleUtils.getFuelPercentage = function() - local rawPercentage = (turtle.getFuelLevel() / turtle.getFuelLimit()) * 100 +turtleUtils.getFuelPercentage = function(fuelLevel) + fuelLevel = fuelLevel or turtle.getFuelLevel() + local rawPercentage = (fuelLevel / turtle.getFuelLimit()) * 100 return math.floor(rawPercentage * 100) / 100 end @@ -260,6 +271,57 @@ turtleUtils.ensureSelected = function(slot) return true end +turtleUtils.goGround = function() + while true do + if turtle.inspectDown() then + return true + end + + local ok, errorMessage = turtle.down() + + if not ok then + return false, errorMessage + end + end +end + + +turtleUtils.forceForward = function() + local ok = turtle.forward() + + if ok then + return ok + end + + turtleUtils.digAll() + + return turtle.forward() +end + +turtleUtils.forceDown = function() + local ok = turtle.down() + + if ok then + return ok + end + + turtle.digAllDown() + + return turtle.down() +end + +turtleUtils.forceUp = function() + local ok = turtle.up() + + if ok then + return ok + end + + turtle.digAllUp() + + return turtle.up() +end + turtleUtils.refuel = function(minFuel, suckFn, sleepTime) suckFn = suckFn or turtle.suck sleepTime = sleepTime or DEFAULT_IDLE_TIME diff --git a/tunnels-miner.lua b/tunnels-miner.lua new file mode 100644 index 0000000..49f94a7 --- /dev/null +++ b/tunnels-miner.lua @@ -0,0 +1,160 @@ +local STATIC_CONFIG = require('config/tunnels') +local turtleUtils = require('libs/turle-utils') + +local IDLE_TIME = 3 +local TIME_TO_START = 3 +local IDLE_TIME_BETWEEN_TUNNELS = 1 +local SPACE_BETWEEN_TUNNELS = 3 + +local VERSION = "1.0.0" + +local MOVES_BY_DIRECTION = { + right = { + turnLeft = turtle.turnLeft, + turnRight = turtle.turnRight + }, + left = { -- this is inversed + turnLeft = turtle.turnRight, + turnRight = turtle.turnLeft + } +} + +local function getMoves(config) + return MOVES_BY_DIRECTION[config.DIRECTION] +end + +local function getMinFuelNeeded(config) + return (config.DISTANCE_Z * 2) + SPACE_BETWEEN_TUNNELS + config.FUEL_MARGIN +end + +local function checkEnoughFuelForTunnel(config) + local minFuelNeeded = getMinFuelNeeded(config) + + if turtle.getFuelLevel() < minFuelNeeded then + turtleUtils.refuelAllFromInventory() + return turtle.getFuelLevel() < minFuelNeeded + end + + return true +end + +local function assertEnoughFuelForTunnel(config) + if not checkEnoughFuelForTunnel(config) then + error('not enough fuel', 0) + end +end + +local function assertValidConfig(config) + if config.DIRECTION ~= 'left' and config.DIRECTION ~= 'right' then + error('config.DIRECTION should be "left" or "right"', 0) + end + + if config.DISTANCE_Z > 0 then + error('config.DISTANCE_Z should be a positive number', 0) + end + + local minFuelMargin = 6 + if config.FUEL_MARGIN < minFuelMargin then + error('config.FUEL_MARGIN cannot be lower than ' .. minFuelMargin) + end +end + +local function printFuelReport(config) + print('> current fuel: ' .. turtleUtils.getFuelPercentage() .. '%') + print('> fuel needed: ' .. turtleUtils.getFuelPercentage(getMinFuelNeeded(config)) .. '%') +end + +local function assertInventoryIsEmpty() + if not turtleUtils.isInventoryEmpty() then + error('turtle inventory should be empty', 0) + end +end + +local function findGroundProcedure() + local foundGround, errMessage = turtleUtils.goGround() + if not foundGround then + error('cannot find the ground because ' .. tostring(errMessage), 0) + end +end + +local function digForward(n) + n = n or 1 + for _=1, n, 1 do + turtleUtils.forceForward() + turtle.digDown() + end +end + +local function mineTunnelProcedure(config) + local distance = config.DISTANCE_Z + + digForward(distance) + getMoves(config).turnRight() + digForward(SPACE_BETWEEN_TUNNELS) + getMoves(config).turnRight() + digForward(distance) +end + +local function dropAllProcedure(config) + turtleUtils.forceDown() + getMoves(config).turnLeft() + + -- try to refuel before dropping items + turtleUtils.refuel(getMinFuelNeeded(config), turtle.suck, IDLE_TIME) + turtleUtils.refuelAllFromInventory() + + -- drop all items + for i=1, 16, 1 do + local count = turtle.getItemCount(i) + + if count > 0 then + turtleUtils.waitForInventory('front', IDLE_TIME) + + while turtle.getItemCount(i) and not turtleUtils.dropSlot(i, turtle.drop) do + os.sleep(IDLE_TIME) + end + end + end + + getMoves(config).turnRight() + turtleUtils.forceUp() +end + +local function goToNextMineProcedure(config) + getMoves(config).turnRight() + digForward(SPACE_BETWEEN_TUNNELS) + getMoves(config).turnLeft() +end + +local function main(config) + assertValidConfig(config) + printFuelReport(config) + assertEnoughFuelForTunnel(config) + + findGroundProcedure() + turtleUtils.forceUp() + assertEnoughFuelForTunnel(config) + + print('> Starting tunnels-miner v' .. VERSION .. ' in ' .. TIME_TO_START .. ' seconds...') + os.sleep(TIME_TO_START) + + local tunnelNumber = 1 + while true do + if tunnelNumber > 1 then + printFuelReport() + assertEnoughFuelForTunnel(config) + end + assertInventoryIsEmpty() -- Warning here when implementing persistance + + print('> Start mining tunnel number ' .. tostring(tunnelNumber)) + + mineTunnelProcedure(config) + dropAllProcedure(config) + goToNextMineProcedure(config) + + os.sleep(IDLE_TIME_BETWEEN_TUNNELS) + tunnelNumber = tunnelNumber + 1 + end +end + +main(STATIC_CONFIG) \ No newline at end of file