feat: add confirmation by the user + persist started state
This commit is contained in:
parent
bedd5adbcb
commit
9e3a356eca
37
miner.lua
37
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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user