feat: implement first version of tunnels-miner

This commit is contained in:
Guillaume ARM 2024-05-26 02:29:51 +02:00
parent edcab83d4e
commit c2e63d427a
5 changed files with 231 additions and 3 deletions

5
config/tunnels.lua Normal file
View File

@ -0,0 +1,5 @@
return {
DIRECTION = 'right',
DISTANCE_Z = 256,
FUEL_MARGIN = 10
}

View File

@ -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

View File

@ -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'
}

View File

@ -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

160
tunnels-miner.lua Normal file
View File

@ -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)