feat(turtle-utils): add refuelWithBuffer
This commit is contained in:
parent
2ee725388a
commit
e7a95dcb44
@ -1,6 +1,7 @@
|
|||||||
local turtleUtils = {}
|
local turtleUtils = {}
|
||||||
|
|
||||||
local DEFAULT_IDLE_TIME = 2
|
local DEFAULT_IDLE_TIME = 2
|
||||||
|
local DEFAULT_MIN_FUEL_NEEDED = 1000
|
||||||
|
|
||||||
turtleUtils.isFuelItem = function(item)
|
turtleUtils.isFuelItem = function(item)
|
||||||
item = item or {}
|
item = item or {}
|
||||||
@ -221,7 +222,7 @@ turtleUtils.refuelAllFromInventory = function()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- utils function used by "compactInventory"
|
-- utils functions used by "compactInventory"
|
||||||
local function getItemsMap()
|
local function getItemsMap()
|
||||||
local result = {}
|
local result = {}
|
||||||
|
|
||||||
@ -283,4 +284,47 @@ turtleUtils.compactInventory = function()
|
|||||||
return turtleUtils.countFreeSlots() - initialNbFreeSlots
|
return turtleUtils.countFreeSlots() - initialNbFreeSlots
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- utils functions used by "refuelWithBuffer"
|
||||||
|
local function getSuckFn(side)
|
||||||
|
if side == 'front' then
|
||||||
|
return turtle.suck
|
||||||
|
elseif side == 'top'
|
||||||
|
return turtle.suckUp
|
||||||
|
elseif side == 'bottom'
|
||||||
|
return turtle.suckDown
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function findFuelSlot(inventory)
|
||||||
|
for slot in pairs(inventory.list()) do
|
||||||
|
local item = inventory.getItemDetail(slot)
|
||||||
|
|
||||||
|
if item and turtleUtils.isFuelItem(item) then
|
||||||
|
return slot
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Refuel using a buffer chest
|
||||||
|
turtleUtils.refuelWithBuffer = function(sideFuel, sideBuffer, minFuelNeeded, sleepTime)
|
||||||
|
sleepTime = sleepTime or DEFAULT_IDLE_TIME
|
||||||
|
minFuelNeeded = minFuelNeeded or DEFAULT_MIN_FUEL_NEEDED
|
||||||
|
local suckFn = getSuckFn(sideBuffer)
|
||||||
|
|
||||||
|
if not suckFn then
|
||||||
|
return false, 'refuelWithBuffer: sideBuffer should be "front", "top" or "bottom"'
|
||||||
|
end
|
||||||
|
|
||||||
|
local fuelInventory = waitForInventory(sideFuel, sleepTime)
|
||||||
|
local bufferInventory = waitForInventory(sideBuffer, sleepTime)
|
||||||
|
|
||||||
|
local ok, errMessage = turtleUtils.refuel(minFuelNeeded, function()
|
||||||
|
local fuelSlot = findFuelSlot(fuelInventory)
|
||||||
|
fuelInventory.pushItems(peripheral.getName(bufferInventory), fuelSlot)
|
||||||
|
return suckFn()
|
||||||
|
end, sleepTime)
|
||||||
|
|
||||||
|
return ok, errMessage
|
||||||
|
end
|
||||||
|
|
||||||
return turtleUtils
|
return turtleUtils
|
||||||
Loading…
Reference in New Issue
Block a user