local turtleUtils = require('libs/turtle-utils') local config = require('config/harvesting') local VERSION = "0.3.0" local IDLE_TIME = 2 local MIN_FUEL_NEEDED = (10 + config.firstCropZ + config.length) * 2 -- UTILS local function assertEnoughFuel() if turtle.getFuelLevel() < MIN_FUEL_NEEDED then error('Not enough fuel') end end local function retrieveChestFuelOrientation() local inventoryFound = false for i=1, 4, 1 do if turtleUtils.getInventory('front') then inventoryFound = true break end turtle.turnRight() end if not inventoryFound then error('chest fuel not found') end end local function getSeedNameFromCropName(cropName) return string.gsub(cropName, 'crop', 'seeds') end -- Implementations local function retrieveHomePositionProcedure() if turtleUtils.getInventory('bottom') then retrieveChestFuelOrientation() turtle.turnLeft() end turtle.turnRight() if turtle.inspect() then turtle.turnRight() end while true do if turtleUtils.getInventory('bottom') then return end if turtle.inspect() then break else turtle.forward() end end turtle.turnLeft() turtle.turnLeft() while true do if turtleUtils.getInventory('bottom') then return end if turtle.inspect() then error('Cannot retrieve home position') else turtle.forward() end end end local function dropAllProcedure() for i=1, 16, 1 do local count = turtle.getItemCount(i) if count > 0 then while not turtleUtils.dropSlot(i, turtle.dropDown) do os.sleep(IDLE_TIME) end end end end local function refuelProcedure() turtle.turnLeft() turtleUtils.waitForInventory('front', IDLE_TIME) local refuelOk, refuelErr = turtleUtils.refuel(MIN_FUEL_NEEDED, turtle.suck, IDLE_TIME) if not refuelOk then error('Cannot refuel the turtle: "' .. refuelErr .. '"') end turtle.turnLeft() end local function goToHarvestPoint() for i=1, config.firstCropZ, 1 do turtle.forward() end end local function goBackToHome() for i=1, config.firstCropZ, 1 do turtle.forward() end end local function harvestDown() local cropName = turtleUtils.waitForMatureCrop(turtle.inspectDown, IDLE_TIME) local seedName = getSeedNameFromCropName(cropName) if not turtle.digDown() then error('turtle cannot harvest crop') end if not turtleUtils.selectItemByName(seedName) then error('turtle cannot find any crop to place') end if not turtle.placeDown() then error('turtle cannot place crop') end turtleUtils.compactInventory() end local function forward() if not turtle.forward() then error('turtle is blocked') end end local function harvestProcedure() for i=1, config.length, 1 do harvestDown() if i ~= config.length then forward() end end turtle.turnLeft() turtle.turnLeft() for i=1, config.length, 1 do if config.energySaving then harvestDown() end if i ~= config.length then forward() end end end -- Main procedure local function main() print("Starting Trap's inferium harvester v" .. VERSION) turtleUtils.refuelAllFromInventory() assertEnoughFuel() print("> retrieving home position") retrieveHomePositionProcedure() print("> start the harvesting process") while true do dropAllProcedure() refuelProcedure() goToHarvestPoint() harvestProcedure() goBackToHome() end end main()