Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local slots = {}
- --# Fill a table with the sixteen slot IDs.
- for i = 1, 16 do
- slots[i] = i
- end
- local matches = {}
- local itemCount = 0
- local numItems = 0
- local leastItems = 64
- function clearOuterSlots()
- for i=4,16,4 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop(i)
- end
- end
- for i = 13,15 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop(i)
- end
- end
- end
- clearOuterSlots()
- while #slots > 0 do
- --# Get the next slot number from the slot ID table
- local current = table.remove(slots, 1)
- --# Create a new match set including it.
- if turtle.getItemCount(current) > 0 then
- local match = {current}
- turtle.select(current)
- --# Check the contents of that slot against the remaining unmatched slots.
- for i = #slots, 1, -1 do
- if turtle.compareTo(slots[i]) then
- table.insert(match, table.remove(slots, i))
- end
- end
- --# Sort the slot IDs in the current set of matches (optional)
- table.sort(match)
- --# Add the current set of matches to the final table.
- table.insert(matches, match)
- end
- end
- while true do
- clearOuterSlots()
- for i = 1,#matches do
- for k = 1,#matches[i] do
- itemCount = itemCount + turtle.getItemCount(matches[i][k])
- --print(string.format("Match %d: %d / %d", i, matches[i][k], itemCount))
- numItems = k
- end
- local perSlot = math.floor(itemCount / numItems)
- --print(string.format("Item %d per slot: %d", numItems, perSlot))
- for k = 1,#matches[i] do
- local slotItemCount = turtle.getItemCount(matches[i][k])
- if slotItemCount < perSlot then
- --print(string.format("Slot %d has less than %d items", matches[i][k], perSlot))
- for j = 1,#matches[i] do
- if turtle.getItemCount(matches[i][j]) > perSlot then
- local requiredItems = perSlot - turtle.getItemCount(matches[i][k])
- if (turtle.getItemCount(matches[i][j]) - requiredItems) >= perSlot then
- turtle.select(matches[i][j])
- turtle.transferTo(matches[i][k], requiredItems)
- else
- for w=1,requiredItems do
- while turtle.getItemCount(matches[i][j]) > perSlot do
- turtle.select(matches[i][j])
- turtle.transferTo(matches[i][k], 1)
- end
- end
- end
- print(requiredItems)
- --print(string.format("Transferring %d items from %d to %d", requiredItems, matches[i][j], matches[i][k]))
- end
- end
- end
- end
- itemCount = 0
- numItems = 0
- end
- for i=1,3 do
- if turtle.getItemCount(i) < leastItems then
- leastItems = turtle.getItemCount(i)
- end
- end
- for i=5,7 do
- if turtle.getItemCount(i) < leastItems then
- leastItems = turtle.getItemCount(i)
- end
- end
- for i=9,11 do
- if turtle.getItemCount(i) < leastItems then
- leastItems = turtle.getItemCount(i)
- end
- end
- --print(string.format("Crafting %d items ..", (leastItems-1)))
- turtle.select(16)
- turtle.craft(leastItems-1)
- turtle.drop()
- print("Done!")
- leastItems = 64
- os.sleep(2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement