Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- local functions:
- split(inputstr, sep)
- global functions:
- compareItems(itemStr1, itemStr2)
- select(slot)
- getItemDetail(slot)
- selectItem(item, wait)
- getItemCount(item)
- waitForItems(item, count, timeout)
- emptySlots()
- emptyItemFunc(item, count, wait, func)
- emptyItem(item, count, wait)
- emptyItemUp(item, count, wait)
- emptyItemDown(item, count, wait)
- stack()
- --]]
- local function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local table = {}
- local i = 1
- for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
- table [i] = str
- i = i + 1
- end
- return table
- end
- function compareItems(itemStr1, itemStr2)
- if itemStr1 and itemStr2 then
- local mod1, item1, damage1 = unpack(split(itemStr1, ":"))
- local mod2, item2, damage2 = unpack(split(itemStr2, ":"))
- if (mod1 == mod2) and (item1 == item2) then
- if damage1 and damage2 then
- return damage1 == damage2
- else
- return true
- end
- else
- return false
- end
- else
- return false
- end
- end
- function select(slot)
- assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
- turtle.select(slot)
- end
- function getItemDetail(slot)
- if slot == nil then
- slot = turtle.getSelectedSlot()
- end
- assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
- local data = turtle.getItemDetail(slot)
- if data then
- return data.name .. ":" .. data.damage
- else
- return nil
- end
- end
- function selectItem(item, wait)
- local trial = 0
- while true do
- for i = 1, 16, 1 do
- if compareItems(item, getItemDetail(i)) then
- turtle.select(i)
- return true
- end
- end
- if not wait then
- return false
- else
- print("Can't select " .. item .. " - " .. trial)
- os.sleep(1)
- trial = trial + 1
- end
- end
- end
- function getItemCount(item)
- local count = 0
- for i = 1, 16, 1 do
- if compareItems(item, getItemDetail(i)) then
- count = count + turtle.getItemCount(i)
- end
- end
- return count
- end
- function waitForItems(item, count, timeout)
- local trial = 0
- while inv.getItemCount(item) < count do
- print("Missing " .. item .. " - " .. count - inv.getItemCount(item) .. " (" .. inv.getItemCount(item) .. " of " .. count .. ") - " .. trial)
- if not timeout == nil and timeout == trial then
- return false
- end
- os.sleep(1)
- end
- return true
- end
- function emptySlots()
- local count = 0
- for i = 1, 16, 1 do
- if (turtle.getItemCount(i) == 0) then
- count = count + 1
- end
- end
- return count
- end
- function emptyItemFunc(item, count, wait, func)
- if count == nil then
- count = getItemCount(item)
- end
- local i = 0
- local trial = 0
- while i < count do
- if not selectItem(item) then
- return true
- else
- local pre_count = getItemCount(item)
- if not func(math.min(count - i, 64)) then
- if wait then
- print("Can't drop item - " .. trial)
- os.sleep(1)
- trial = trial + 1
- else
- return false
- end
- end
- local post_count = getItemCount(item)
- i = i + pre_count - post_count
- end
- end
- return true
- end
- function emptyItem(item, count, wait)
- emptyItemFunc(item, count, wait, turtle.drop)
- end
- function emptyItemUp(item, count, wait)
- emptyItemFunc(item, count, wait, turtle.dropUp)
- end
- function emptyItemDown(item, count, wait)
- emptyItemFunc(item, count, wait, turtle.dropDown)
- end
- function stack()
- for i = 1, 16, 1 do
- for j = 1, 16 - i do
- if compareItems(getItemDetail(i), getItemDetail(i + j)) then
- select(i + j)
- turtle.transferTo(i, 64 - turtle.getItemCount(i))
- end
- end
- end
- end
- --[[
- function export(slot, resourceChest, count)
- assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
- assert(tonumber(resourceChest) and resourceChest >= 1 and resourceChest <= 16, "Expected slot number in range 1-16, but got: " .. resourceChest)
- if (count == nil) then
- count = 64
- else
- assert(tonumber(count) and count <= turtle.getItemCount(slot) and count >= 0, "Expected count in range 0-" .. turtle.getItemCount() .. ", but got: " .. count)
- end
- select(resourceChest)
- while (not turtle.placeUp()) do
- end
- select(slot)
- local temp = turtle.getItemCount(slot)
- while (turtle.getItemCount(slot) > 0) do
- turtle.dropUp(turtle.getItemCount(slot) - (temp - count))
- end
- select(resourceChest)
- turtle.digUp()
- end
- function import(slot, resourceChest, count)
- assert(tonumber(slot) and slot >= 1 and slot <= 16, "Expected slot number in range 1-16, but got: " .. slot)
- assert(tonumber(resourceChest) and resourceChest >= 1 and resourceChest <= 16, "Expected slot number in range 1-16, but got: " .. resourceChest)
- if (count == nil) then
- count = 64
- else
- assert(tonumber(count) and count <= 64 and count >= 0, "Expected count in range 0-64, but got: " .. count)
- end
- select(resourceChest)
- while (not turtle.placeUp()) do
- end
- select(slot)
- while (turtle.getItemCount(slot) < count) do
- turtle.suckUp(count - turtle.getItemCount(slot))
- end
- select(resourceChest)
- turtle.digUp()
- end
- function emptySlot(slot, trashChest)
- assert(slot, "Expected slot number or array of slot numbers in range 1-16, but got: " .. textutils.serialize(slot))
- if (type(slot) == "table") then
- select(trashChest)
- while (not turtle.placeUp()) do
- end
- for i = 1, table.getn(slot), 1 do
- select(slot[i])
- while (turtle.getItemCount(slot[i]) > 0) do
- turtle.dropUp(turtle.getItemCount(slot[i]))
- end
- end
- select(trashChest)
- turtle.digUp()
- else
- select(trashChest)
- while (not turtle.placeUp()) do
- end
- select(slot)
- while (turtle.getItemCount(slot) > 0) do
- turtle.dropUp(turtle.getItemCount(slot))
- end
- select(trashChest)
- turtle.digUp()
- end
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement