feat(inferium-harvester): improve printed messages + fuel management at start

This commit is contained in:
Guillaume ARM 2024-05-21 23:27:57 +02:00
parent 9dd106763e
commit a4a33d1eef

View File

@ -3,7 +3,7 @@ local utils = require('libs/utils')
local turtleUtils = require('libs/turtle-utils')
local config = require('config/harvesting')
local VERSION = "2.0.0"
local VERSION = "2.2.0"
local INFERIUM_SERVER = 'inferium.com'
local IDLE_TIME = 2
local WAIT_ITEM_IDLE_TIME = 5
@ -44,10 +44,8 @@ local function getIndexedCount(t)
return res
end
local function assertEnoughFuel()
if turtle.getFuelLevel() < MIN_FUEL_NEEDED then
error('Not enough fuel')
end
local function hasEnoughFuel()
return turtle.getFuelLevel() >= MIN_FUEL_NEEDED
end
local function retrieveChestFuelOrientation()
@ -100,6 +98,7 @@ end
-- Implementations
local function retrieveHomePositionProcedure()
print("> retrieving the home position")
if turtleUtils.getInventory('bottom') then
retrieveChestFuelOrientation()
return
@ -143,6 +142,7 @@ local function retrieveHomePositionProcedure()
end
local function dropAllProcedure()
print('> drop all')
for i=1, 16, 1 do
local count = turtle.getItemCount(i)
@ -157,6 +157,7 @@ local function dropAllProcedure()
end
local function refuelProcedure()
print('> refuel')
local refuelOk, refuelErr = turtleUtils.refuelWithBuffer('bottom', 'front', MIN_FUEL_NEEDED, IDLE_TIME)
if not refuelOk then
@ -221,6 +222,8 @@ local function forward()
end
local function harvestProcedure()
print('> harvest')
for i=1, config.length, 1 do
harvestDown(i)
@ -242,6 +245,7 @@ end
local function retrieveLocalPlan()
goToHarvestPoint()
print('> retrieve the local plan')
localPlan = {}
for i=1, config.length, 1 do
@ -267,6 +271,7 @@ local function retrieveLocalPlan()
end
local function fetchRemotePlan()
print('> fetch remote plan')
local message = { type = 'getplan', payload = { computerId = os.getComputerID() } }
while true do
@ -276,6 +281,7 @@ local function fetchRemotePlan()
previouslyFetchedRemotePlan = replyMessage.payload
return replyMessage.payload
elseif previouslyFetchedRemotePlan then
print('> failed to fetch remote plan, use the previous one instead')
return previouslyFetchedRemotePlan
end
@ -286,7 +292,8 @@ end
local function removeSeeds(seeds)
goToHarvestPoint()
local stateSeeds = seeds
print('> remove old seeds')
local stateSeeds = seeds -- warning: do not mutate the data (only the ref)
for i=1, config.length, 1 do
local ok, block = turtle.inspectDown()
@ -324,6 +331,7 @@ local function removeSeeds(seeds)
end
local function retrieveSeeds(seeds)
print('> retrieve seeds from inventory')
local seedRetrieved = false
local seedsCount = getIndexedCount(seeds)
@ -358,6 +366,8 @@ end
local function replantSeeds()
goToHarvestPoint()
print('> replant seeds')
for i=1, config.length, 1 do
local ok, block = turtle.inspectDown()
@ -419,21 +429,34 @@ local function replantProcedure()
localPlan = {}
end
local function resetTerminal()
term.clear()
term.setCursorPos(1, 1)
end
-- Main procedure
local function main()
net.openRednet()
print("Starting Trap's inferium harvester v" .. VERSION)
if not hasEnoughFuel() then
turtleUtils.refuelAllFromInventory()
assertEnoughFuel()
end
if not hasEnoughFuel() then
error('Not enough fuel', 0)
end
print("> retrieving home position")
retrieveHomePositionProcedure()
print("> start the harvesting process")
local cycleNumber = 1
while true do
resetTerminal()
print('> Starting cycle ' .. tostring(cycleNumber))
print()
dropAllProcedure()
refuelProcedure()
@ -442,6 +465,8 @@ local function main()
goToHarvestPoint()
harvestProcedure()
goBackToHome()
cycleNumber = cycleNumber + 1
end
net.closeRednet()