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