fix(inferium-harvester): crash with waitFor util + better refuel procedure
This commit is contained in:
parent
f811031db5
commit
1e9536b089
@ -1,21 +1,16 @@
|
||||
local config = {
|
||||
length = 8
|
||||
length = 8,
|
||||
firstCropZ = 2
|
||||
}
|
||||
|
||||
local MIN_FUEL_NEEDED = (10 + config.firstCropZ + config.length) * 2
|
||||
|
||||
local VERSION = "0.1.0"
|
||||
local VERSION = "0.1.1"
|
||||
local IDLE_TIME = 2
|
||||
|
||||
local turtleUtils = require('libs/turtle-utils')
|
||||
|
||||
-- UTILS
|
||||
local function waitForMatureCrop()
|
||||
return turtleUtils.waitFor(function()
|
||||
return turtleUtils.isMatureCrop(turtle.inspectDown)
|
||||
end, IDLE_TIME)
|
||||
end
|
||||
|
||||
local function assertEnoughFuel()
|
||||
if turtle.getFuelLevel() < MIN_FUEL_NEEDED then
|
||||
@ -95,8 +90,14 @@ end
|
||||
|
||||
local function refuelProcedure()
|
||||
turtle.turnLeft()
|
||||
turtleUtils.waitForInventory('front')
|
||||
turtleUtils.refuel(MIN_FUEL_NEEDED, turtle.suck)
|
||||
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
|
||||
|
||||
@ -113,7 +114,7 @@ local function goBackToHome()
|
||||
end
|
||||
|
||||
local function harvestDown()
|
||||
local cropName = waitForMatureCrop()
|
||||
local cropName = turtleUtils.waitForMatureCrop(turtle.inspectDown, IDLE_TIME)
|
||||
|
||||
if not turtle.digDown() then
|
||||
error('turtle cannot harvest crop')
|
||||
|
||||
@ -6,14 +6,14 @@ turtleUtils.getInventory = function(side)
|
||||
side = side or 'front'
|
||||
local inv = peripheral.wrap(side)
|
||||
|
||||
if peripheral.hasType(inv, 'inventory') then
|
||||
if inv and peripheral.hasType(inv, 'inventory') then
|
||||
return inv
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
function waitFor(predicate, sleepTime)
|
||||
local function waitFor(predicate, sleepTime)
|
||||
sleepTime = sleepTime or DEFAULT_IDLE_TIME
|
||||
|
||||
while true do
|
||||
@ -33,6 +33,14 @@ turtleUtils.waitForInventory = function(side, sleepTime)
|
||||
end, sleepTime)
|
||||
end
|
||||
|
||||
turtleUtils.waitForMatureCrop = function(inspectFn, sleepTime)
|
||||
inspectFn = inspectFn or turtle.inspect
|
||||
|
||||
return waitFor(function()
|
||||
return turtleUtils.isMatureCrop(inspectFn)
|
||||
end, sleepTime)
|
||||
end
|
||||
|
||||
turtleUtils.trySuck = function(suckFn, sleepTime)
|
||||
suckFn = suckFn or turtle.suck
|
||||
sleepTime = sleepTime or DEFAULT_IDLE_TIME
|
||||
@ -151,20 +159,25 @@ turtleUtils.selectItemByName = function(itemName)
|
||||
return false
|
||||
end
|
||||
|
||||
turtleUtils.refuel = function(minFuel, suckFn)
|
||||
turtleUtils.refuel = function(minFuel, suckFn, sleepTime)
|
||||
suckFn = suckFn or turtle.suck
|
||||
sleepTime = sleepTime or DEFAULT_IDLE_TIME
|
||||
|
||||
if not turtleUtils.selectFirstEmptySlot() then
|
||||
return false
|
||||
return false, 'turtle inventory is full'
|
||||
end
|
||||
|
||||
while turtle.getFuelLevel() < minFuel do
|
||||
suckFn()
|
||||
local suckRes = suckFn()
|
||||
|
||||
local ok = turtle.refuel()
|
||||
if suckRes then
|
||||
local ok = turtle.refuel()
|
||||
|
||||
if not ok then
|
||||
return false
|
||||
if not ok then
|
||||
return false, 'selected item is not considered as fuel'
|
||||
end
|
||||
else
|
||||
os.sleep(sleepTime)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user