From 4502db1ba1bce363e53339cde7a9d5623f3f3f68 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Fri, 24 May 2024 16:30:49 +0200 Subject: [PATCH] perf(inferium-harvester): do not continue replant when inventory is empty --- inferium-harvester.lua | 23 ++++++++++++++--------- libs/turtle-utils.lua | 10 ++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/inferium-harvester.lua b/inferium-harvester.lua index c884978..414a1b7 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -2,7 +2,7 @@ local net = require('libs/net') local utils = require('libs/utils') local turtleUtils = require('libs/turtle-utils') -local VERSION = "4.0.0" +local VERSION = "4.0.1" local INFERIUM_SERVER = 'inferium.com' local IDLE_TIME = 2 local WAIT_ITEM_IDLE_TIME = 5 @@ -364,6 +364,7 @@ local function removeSeeds(seeds, config) print('> remove ' .. utils.sizeof(seeds) .. ' seed(s)') local stateSeeds = seeds -- warning: do not mutate the data (only the ref) + local nbBlockTraveled = 0 for i=1, config.length, 1 do local ok, block = turtle.inspectDown() local blockName = block and block.name @@ -372,6 +373,7 @@ local function removeSeeds(seeds, config) if found then local digOk = turtle.digDown() + compactIfNeeded() if not digOk then @@ -383,16 +385,15 @@ local function removeSeeds(seeds, config) if i ~= config.length then forward() + nbBlockTraveled = nbBlockTraveled + 1 end end turtle.turnLeft() turtle.turnLeft() - for i=1, config.length, 1 do - if i ~= config.length then - forward() - end + for i=1, nbBlockTraveled, 1 do + forward() end goBackToHome(config) @@ -467,6 +468,7 @@ local function replantSeeds(config) print('> replant seeds') + local nbBlockTraveled = 0 for i=1, config.length, 1 do local ok, block = turtle.inspectDown() @@ -477,20 +479,23 @@ local function replantSeeds(config) end) turtle.placeDown() + + if turtleUtils.isInventoryEmpty() then + break + end end if i ~= config.length then forward() + nbBlockTraveled = nbBlockTraveled + 1 end end turtle.turnLeft() turtle.turnLeft() - for i=1, config.length, 1 do - if i ~= config.length then - forward() - end + for i=1, nbBlockTraveled, 1 do + forward() end goBackToHome(config) diff --git a/libs/turtle-utils.lua b/libs/turtle-utils.lua index e8580ac..25484ed 100644 --- a/libs/turtle-utils.lua +++ b/libs/turtle-utils.lua @@ -25,6 +25,16 @@ turtleUtils.getInventory = function(side) return nil end +turtleUtils.isInventoryEmpty = function() + for i=1, 16, 1 do + if turtle.getItemCount(i) > 0 then + return false + end + end + + return true +end + turtleUtils.getItemName = function(slotIndex) local item = turtle.getItemDetail(slotIndex) return item and item.name