end
--------------------------------------
+local lib = {}
+
-- findItem : list of item names -> index | false message
-- returns the first inventory index containing an item from list t
-- if no item is found, returns nil
-local findItem = function(t)
+lib.findItem = function(t)
for i = 1, 16 do
local item = turtle.getItemDetail(i)
if item and oneOf(t, item.name) then return i end
-- findEmpty : index | false message
-- returns the first empty slot
-- if no empty slot is found, returns nil
-local findEmpty = function()
+lib.findEmpty = function()
for i = 1, 16 do if not turtle.getItemDetail(i) then return i end end
return false, "No empty slot"
end
-- count : list of item names -> count
-- counts how many slots contain an item from list t
-- (does not count individual items)
-local count = function(t)
+lib.count = function(t)
local c = 0
for i = 1, 16 do
local item = turtle.getItemDetail(i)
-- selectItem : list of item names -> true | false message
-- select first slot containing an item from list t if not already selected.
-- returns bool.
-local selectItem = function(t)
+lib.selectItem = function(t)
local item = turtle.getItemDetail()
if item and oneOf(t, item.name) then
return true
-- like selectItem, selects item from list, but tries to move it to the first slot.
-- requires an empty slot to move items around.
-- (this function exists because weak automata cannot digBlock with tools in slots >9)
-local transferAndSelect = function(i, t)
+lib.transferAndSelect = function(i, t)
if i < 1 or i > 16 then return false, msg .. "Index out of range" end
-- if a wanted item already is in the wanted slot, select it and return early
local item = turtle.getItemDetail(i)
-- "u1 5r" = invalid
-- commandMap : t f
-local commandMap =
+lib.commandMap =
{ ["u"] = turtle.up
, ["d"] = turtle.down
, ["f"] = turtle.forward
}
-- runStringCommand : string -> true | false message
-local runCommandString = function(str, map)
+lib.runStringCommand = function(str, map)
local c, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.*)")
if not c then return true end
if n == "" then n = 1 else n = tonumber(n) end
for i = 1, n do map[c]() end
-- print(cmd, tostring(i).."/"..tostring(n))
-- end
- return turtle.runCommandString(rest)
+ return lib.runStringCommand(rest)
end
-- rep : number of repeats -> string -> string | nil
-- repeats string n times
-local rep = function(n, str)
+lib.rep = function(n, str)
if not n or n < 0 then return nil end
local s = ""
for i = 1, n do
end
return s
end
+
+return lib