From 9e3a356ecad678f31a53124839ccc9e004fe5738 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Sat, 11 May 2024 15:31:51 +0200 Subject: [PATCH] feat: add confirmation by the user + persist started state --- miner.lua | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/miner.lua b/miner.lua index 8446e4f..22c14a8 100644 --- a/miner.lua +++ b/miner.lua @@ -18,6 +18,7 @@ local MIN_PERCENTAGE_NEEDED = 10 local miner = { robot = robotApi.create(), mission = 'unload', -- "unload" | "refuel" | "mine" | "return-home" | "return-mine" | nil + started = false, finished = false, lastPositionState = nil, -- { x, y, z, dir } targetY = INITIAL_TARGET_Y @@ -27,6 +28,7 @@ local function saveMinerState() local minerState = { robotState = miner.robot.getState(), mission = miner.mission, + started = miner.started, finished = miner.finished, lastPositionState = miner.lastPositionState, targetY = miner.targetY @@ -57,6 +59,7 @@ local function loadMinerState() miner = { robot = robotApi.create(minerState.robotState), mission = minerState.mission, + started = minerState.started, finished = minerState.finished, lastPositionState = minerState.lastPositionState, targetY = minerState.targetY @@ -130,9 +133,8 @@ local function checkConfig() end local function minerStarted() - checkConfig() turtle.select(1) - loadMinerState() + miner.started = true print("> Miner program started, minimum percentgae fuel needed: " .. MIN_PERCENTAGE_NEEDED) end @@ -218,8 +220,6 @@ local function returnMineProcedure() miner.mission = 'mine' print('> Starting mining procedure...') end - - saveMinerState() end local function mineProcedure() @@ -279,8 +279,6 @@ local function mineProcedure() miner.mission = 'return-home' miner.lastPositionState = miner.robot.getState() end - - saveMinerState() end local function returnHomeProcedure() @@ -315,11 +313,33 @@ local function returnHomeProcedure() miner.robot.turnLeft() miner.robot.turnLeft() end +end - saveMinerState() +local function waitForUserConfirmation() + print('> startY: ' .. config.startY) + print('> targetY: ' .. config.targetY) + print('> height: ' .. config.height) + print('> size: ' .. config.size) + print('> type "MINE" to start the mining process.') + + local val = read() + + if val == 'MINE' then + return true + end + + return false +end + +checkConfig() +loadMinerState() + +if not miner.started then + while not waitForUserConfirmation() do end end minerStarted() +saveMinerState() while not isProgramFinished() do if miner.mission == 'unload' then @@ -328,10 +348,13 @@ while not isProgramFinished() do refuelProcedure() elseif miner.mission == 'mine' then mineProcedure() + saveMinerState() elseif miner.mission == 'return-home' then returnHomeProcedure() + saveMinerState() elseif miner.mission == 'return-mine' then returnMineProcedure() + saveMinerState() end end