refactor(inferium-harvester): dynamic config (but still local)
This commit is contained in:
parent
f988356750
commit
38b9c24bd6
@ -1,14 +1,15 @@
|
||||
local net = require('libs/net')
|
||||
local utils = require('libs/utils')
|
||||
local turtleUtils = require('libs/turtle-utils')
|
||||
local config = require('config/harvesting')
|
||||
local LOCAL_CONFIG = require('config/harvesting')
|
||||
|
||||
local VERSION = "2.6.0"
|
||||
local VERSION = "2.6.1"
|
||||
local INFERIUM_SERVER = 'inferium.com'
|
||||
local IDLE_TIME = 2
|
||||
local WAIT_ITEM_IDLE_TIME = 5
|
||||
local NETWORK_TIMEOUT = 4
|
||||
local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2
|
||||
local MIN_INITIAL_FUEL_NEEDED = 16
|
||||
local ADDITIONAL_FUEL_MARGIN = 100
|
||||
local MAX_FERTILIZED_ESSENCE = 32
|
||||
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
||||
|
||||
@ -47,8 +48,12 @@ local function getIndexedCount(t)
|
||||
return res
|
||||
end
|
||||
|
||||
local function hasEnoughFuel()
|
||||
return turtle.getFuelLevel() >= MIN_FUEL_NEEDED
|
||||
local function getMinFuelNeeded(config)
|
||||
return (ADDITIONAL_FUEL_MARGIN + config.firstCropZ + config.length) * 2
|
||||
end
|
||||
|
||||
local function hasEnoughInitialFuel()
|
||||
return turtle.getFuelLevel() >= MIN_INITIAL_FUEL_NEEDED
|
||||
end
|
||||
|
||||
local function retrieveChestFuelOrientation()
|
||||
@ -144,7 +149,8 @@ local function retrieveHomePositionProcedure()
|
||||
turtle.turnLeft()
|
||||
end
|
||||
|
||||
local function dropAllProcedure()
|
||||
-- first parameter is config
|
||||
local function dropAllProcedure(_)
|
||||
print('> drop all')
|
||||
for i=1, 16, 1 do
|
||||
local count = turtle.getItemCount(i)
|
||||
@ -159,14 +165,16 @@ local function dropAllProcedure()
|
||||
end
|
||||
end
|
||||
|
||||
local function refuelProcedure()
|
||||
local function refuelProcedure(config)
|
||||
local fuelLevel = turtle.getFuelLevel()
|
||||
print('> fuel: ' .. tostring(fuelLevel))
|
||||
|
||||
if fuelLevel < MIN_FUEL_NEEDED then
|
||||
print('> refuel needed (minimum is ' .. MIN_FUEL_NEEDED .. ')')
|
||||
local minFuelNeeded = getMinFuelNeeded(config)
|
||||
|
||||
local refuelOk, refuelErr = turtleUtils.refuelWithBuffer('bottom', 'front', MIN_FUEL_NEEDED, IDLE_TIME)
|
||||
if fuelLevel < minFuelNeeded then
|
||||
print('> refuel needed (minimum is ' .. minFuelNeeded .. ')')
|
||||
|
||||
local refuelOk, refuelErr = turtleUtils.refuelWithBuffer('bottom', 'front', minFuelNeeded, IDLE_TIME)
|
||||
|
||||
if not refuelOk then
|
||||
error('Cannot refuel the turtle: "' .. refuelErr .. '"')
|
||||
@ -176,7 +184,7 @@ local function refuelProcedure()
|
||||
end
|
||||
end
|
||||
|
||||
local function goToHarvestPoint()
|
||||
local function goToHarvestPoint(config)
|
||||
turtle.turnLeft()
|
||||
|
||||
for i=1, config.firstCropZ, 1 do
|
||||
@ -184,7 +192,7 @@ local function goToHarvestPoint()
|
||||
end
|
||||
end
|
||||
|
||||
local function goBackToHome()
|
||||
local function goBackToHome(config)
|
||||
for i=1, config.firstCropZ, 1 do
|
||||
turtle.forward()
|
||||
end
|
||||
@ -204,7 +212,7 @@ local function applyFertilizedEssenceIfAny()
|
||||
end
|
||||
end
|
||||
|
||||
local function harvestDown(index)
|
||||
local function harvestDown(index, config)
|
||||
if config.fertilizedBoost then
|
||||
applyFertilizedEssenceIfAny()
|
||||
end
|
||||
@ -242,13 +250,13 @@ local function forward()
|
||||
end
|
||||
end
|
||||
|
||||
local function harvestProcedure()
|
||||
goToHarvestPoint()
|
||||
local function harvestProcedure(config)
|
||||
goToHarvestPoint(config)
|
||||
|
||||
term.write('> harvest on ' .. tostring(config.length) .. ' blocks ')
|
||||
local nbHarvested = 0
|
||||
for i=1, config.length, 1 do
|
||||
if harvestDown(i) then
|
||||
if harvestDown(i, config) then
|
||||
nbHarvested = nbHarvested + 1
|
||||
term.write('.')
|
||||
else
|
||||
@ -272,11 +280,11 @@ local function harvestProcedure()
|
||||
end
|
||||
end
|
||||
|
||||
goBackToHome()
|
||||
goBackToHome(config)
|
||||
end
|
||||
|
||||
local function retrieveLocalPlan()
|
||||
goToHarvestPoint()
|
||||
local function retrieveLocalPlan(config)
|
||||
goToHarvestPoint(config)
|
||||
|
||||
print('> retrieve the local plan')
|
||||
localPlan = {}
|
||||
@ -300,7 +308,7 @@ local function retrieveLocalPlan()
|
||||
end
|
||||
end
|
||||
|
||||
goBackToHome()
|
||||
goBackToHome(config)
|
||||
end
|
||||
|
||||
local function fetchRemotePlan()
|
||||
@ -314,10 +322,16 @@ local function fetchRemotePlan()
|
||||
if replyMessage and replyMessage.payload then
|
||||
previouslyFetchedRemotePlan = replyMessage.payload
|
||||
print('> plan fetched')
|
||||
return replyMessage.payload
|
||||
local payload = replyMessage.payload
|
||||
if not payload then
|
||||
error('cannot fetch the remote plan')
|
||||
end
|
||||
|
||||
return payload
|
||||
elseif previouslyFetchedRemotePlan then
|
||||
print('> failed to fetch: ' .. tostring(errMessage))
|
||||
print('> use the previous recorded remote plan instead')
|
||||
|
||||
return previouslyFetchedRemotePlan
|
||||
elseif not replyMessage and errMessage then
|
||||
if lastErrMessage ~= errMessage then
|
||||
@ -332,8 +346,8 @@ local function fetchRemotePlan()
|
||||
end
|
||||
end
|
||||
|
||||
local function removeSeeds(seeds)
|
||||
goToHarvestPoint()
|
||||
local function removeSeeds(seeds, config)
|
||||
goToHarvestPoint(config)
|
||||
|
||||
print('> remove ' .. utils.sizeof(seeds) .. ' seed(s)')
|
||||
local stateSeeds = seeds -- warning: do not mutate the data (only the ref)
|
||||
@ -369,8 +383,8 @@ local function removeSeeds(seeds)
|
||||
end
|
||||
end
|
||||
|
||||
goBackToHome()
|
||||
dropAllProcedure()
|
||||
goBackToHome(config)
|
||||
dropAllProcedure(config)
|
||||
end
|
||||
|
||||
local function retrieveSeeds(seeds)
|
||||
@ -406,7 +420,8 @@ local function retrieveSeeds(seeds)
|
||||
return seedRetrieved
|
||||
end
|
||||
|
||||
local function retrieveFertilizedEssences()
|
||||
-- first parameter is config
|
||||
local function retrieveFertilizedEssences(_)
|
||||
local storageInventory = turtleUtils.waitForInventory('bottom', WAIT_ITEM_IDLE_TIME)
|
||||
local bufferInventory = turtleUtils.waitForInventory('front', WAIT_ITEM_IDLE_TIME)
|
||||
|
||||
@ -435,8 +450,8 @@ local function retrieveFertilizedEssences()
|
||||
return false
|
||||
end
|
||||
|
||||
local function replantSeeds()
|
||||
goToHarvestPoint()
|
||||
local function replantSeeds(config)
|
||||
goToHarvestPoint(config)
|
||||
|
||||
print('> replant seeds')
|
||||
|
||||
@ -466,18 +481,12 @@ local function replantSeeds()
|
||||
end
|
||||
end
|
||||
|
||||
goBackToHome()
|
||||
goBackToHome(config)
|
||||
end
|
||||
|
||||
local function replantProcedure()
|
||||
local remotePlan = fetchRemotePlan()
|
||||
|
||||
if remotePlan == nil then
|
||||
error('cannot fetch the remote plan')
|
||||
end
|
||||
|
||||
local function replantProcedure(remotePlan, config)
|
||||
if localPlan == nil then
|
||||
retrieveLocalPlan()
|
||||
retrieveLocalPlan(config)
|
||||
end
|
||||
|
||||
if localPlan == nil then
|
||||
@ -488,12 +497,12 @@ local function replantProcedure()
|
||||
local seedsToPlant = utils.shallowDiff(remotePlan, localPlan)
|
||||
|
||||
if utils.sizeof(seedsToRemove) > 0 then
|
||||
removeSeeds(seedsToRemove)
|
||||
removeSeeds(seedsToRemove, config)
|
||||
end
|
||||
|
||||
if utils.sizeof(seedsToPlant) > 0 then
|
||||
if retrieveSeeds(seedsToPlant) then
|
||||
replantSeeds()
|
||||
replantSeeds(config)
|
||||
end
|
||||
end
|
||||
|
||||
@ -512,11 +521,11 @@ local function main()
|
||||
|
||||
print("Starting Trap's inferium harvester v" .. VERSION)
|
||||
|
||||
if not hasEnoughFuel() then
|
||||
if not hasEnoughInitialFuel() then
|
||||
turtleUtils.refuelAllFromInventory()
|
||||
end
|
||||
|
||||
if not hasEnoughFuel() then
|
||||
if not hasEnoughInitialFuel() then
|
||||
error('Not enough fuel', 0)
|
||||
end
|
||||
|
||||
@ -529,17 +538,21 @@ local function main()
|
||||
print('> Starting cycle ' .. tostring(cycleNumber))
|
||||
print()
|
||||
|
||||
dropAllProcedure()
|
||||
local remotePlan = fetchRemotePlan()
|
||||
local config = LOCAL_CONFIG
|
||||
|
||||
dropAllProcedure(config)
|
||||
refuelProcedure()
|
||||
|
||||
replantProcedure()
|
||||
-- TODO: change parameters to be just config
|
||||
replantProcedure(remotePlan, config)
|
||||
|
||||
if config.fertilizedBoost then
|
||||
print('> fertilized boost enabled')
|
||||
retrieveFertilizedEssences()
|
||||
retrieveFertilizedEssences(config)
|
||||
end
|
||||
|
||||
harvestProcedure()
|
||||
harvestProcedure(config)
|
||||
|
||||
cycleNumber = cycleNumber + 1
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user