Organized slot functions.

This commit is contained in:
Andrew Lalis 2023-05-05 09:36:29 +02:00
parent e477ba3b42
commit d27320c135
1 changed files with 28 additions and 32 deletions

View File

@ -248,44 +248,52 @@ function itemscript.filterize(value)
end end
end end
-- Finds the first matching slot for the given filter expression.
-- Gets the total number of items in the turtle's inventory that match the given expression. function itemscript.findSlot(filterExpr)
function itemscript.totalCount(filterExpr)
local filter = itemscript.filterize(filterExpr) local filter = itemscript.filterize(filterExpr)
local count = 0 for i = 1, 16 do
local item = turtle.getItemDetail(i)
if filter(i) then return i end
end
return nil
end
-- Gets a list of all inventory slots that match the given filter expression.
function itemscript.findSlots(filterExpr)
local filter = itemscript.filterize(filterExpr)
local slots = {}
for i = 1, 16 do for i = 1, 16 do
local item = turtle.getItemDetail(i) local item = turtle.getItemDetail(i)
if filter(item) then if filter(item) then
count = count + item.count table.insert(slots, i)
end end
end end
end
-- Gets the total number of items in the turtle's inventory that match the given expression.
function itemscript.totalCount(filterExpr)
local count = 0
for _, slot in pairs(itemscript.findSlots(filterExpr)) do
local item = turtle.getItemDetail(slot)
count = count + item.count
end
return count return count
end end
-- Select the first slot containing a matching item stack for a filter. -- Select the first slot containing a matching item stack for a filter.
-- Returns a boolean indicating whether we could find and select the item. -- Returns a boolean indicating whether we could find and select the item.
function itemscript.select(filterExpr) function itemscript.select(filterExpr)
local filter = itemscript.filterize(filterExpr) local slot = itemscript.findSlot(filterExpr)
for i = 1, 16 do if slot ~= nil then
local item = turtle.getItemDetail(i) turtle.select(slot)
if filter(item) then
turtle.select(i)
return true return true
end end
end
return false return false
end end
-- Selects a random slot containing a matching item stack. -- Selects a random slot containing a matching item stack.
function itemscript.selectRandom(filterExpr) function itemscript.selectRandom(filterExpr)
local filter = itemscript.filterize(filterExpr) local eligibleSlots = itemscript.findSlots(filterExpr)
local eligibleSlots = {}
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if filter(item) then
table.insert(eligibleSlots, i)
end
end
if #eligibleSlots == 0 then return false end if #eligibleSlots == 0 then return false end
local slot = eligibleSlots[math.random(1, #eligibleSlots)] local slot = eligibleSlots[math.random(1, #eligibleSlots)]
turtle.select(slot) turtle.select(slot)
@ -330,18 +338,6 @@ function itemscript.selectEmptyOrWait()
end end
end end
-- Gets a list of all inventory slots that match the given filter expression.
function itemscript.findSlots(filterExpr)
local filter = itemscript.filterize(filterExpr)
local slots = {}
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if filter(item) then
table.insert(slots, i)
end
end
end
-- Helper function to drop items in a flexible way, using a drop function and filtering function. -- Helper function to drop items in a flexible way, using a drop function and filtering function.
local function dropFiltered(dropFunction, filterExpr) local function dropFiltered(dropFunction, filterExpr)
local filter = itemscript.filterize(filterExpr) local filter = itemscript.filterize(filterExpr)