View difference between Paste ID: jCzV6rr1 and LtwSsdpm
SHOW: | | - or go back to the newest paste.
1
local slots = {}
2
--# Fill a table with the sixteen slot IDs.
3
for i = 1, 16 do
4
    slots[i] = i
5
end
6
local matches = {}
7
local itemCount = 0
8
local numItems = 0
9
local leastItems = 64
10
11
function clearOuterSlots()
12
	for i=4,16,4 do
13
		if turtle.getItemCount(i) > 0 then
14
			turtle.select(i)
15
			turtle.drop(i)
16
		end
17
	end
18
	for i = 13,15 do
19
		if turtle.getItemCount(i) > 0 then
20
			turtle.select(i)
21
			turtle.drop(i)
22
		end
23
	end
24
end
25
26
clearOuterSlots()
27
while #slots > 0 do
28
    --# Get the next slot number from the slot ID table
29
    local current = table.remove(slots, 1)
30
    --# Create a new match set including it.
31
	if turtle.getItemCount(current) > 0 then
32
    local match = {current}
33
    turtle.select(current)
34
    --# Check the contents of that slot against the remaining unmatched slots.
35
    for i = #slots, 1, -1 do
36
        if turtle.compareTo(slots[i]) then
37
            table.insert(match, table.remove(slots, i))
38
        end
39
    end
40
    --# Sort the slot IDs in the current set of matches (optional)
41
    table.sort(match)
42
    --# Add the current set of matches to the final table.
43
    table.insert(matches, match)
44
	end
45
end
46
while true do
47
clearOuterSlots()
48
for i = 1,#matches do
49
	for k = 1,#matches[i] do
50
	itemCount = itemCount + turtle.getItemCount(matches[i][k])
51
	--print(string.format("Match %d: %d / %d", i, matches[i][k], itemCount))
52
	numItems = k
53
	end
54
local perSlot = math.floor(itemCount / numItems)
55
--print(string.format("Item %d per slot: %d", numItems, perSlot))
56
for k = 1,#matches[i] do
57
	local slotItemCount = turtle.getItemCount(matches[i][k])
58
		if slotItemCount < perSlot then
59
		--print(string.format("Slot %d has less than %d items", matches[i][k], perSlot))
60
			for j = 1,#matches[i] do
61
			if turtle.getItemCount(matches[i][j]) > perSlot then
62
				local requiredItems = perSlot - turtle.getItemCount(matches[i][k])
63
					if (turtle.getItemCount(matches[i][j]) - requiredItems) >= perSlot then
64
						turtle.select(matches[i][j])
65
						turtle.transferTo(matches[i][k], requiredItems)
66
					else
67
						for w=1,requiredItems do
68
							while turtle.getItemCount(matches[i][j]) > perSlot do
69
							turtle.select(matches[i][j])
70
							turtle.transferTo(matches[i][k], 1)
71
							end
72
						end
73
					end
74
				print(requiredItems)
75
				--print(string.format("Transferring %d items from %d to %d", requiredItems, matches[i][j], matches[i][k]))
76
			end
77
		end
78
	end
79
end
80
itemCount = 0
81
numItems = 0
82
end
83
for i=1,3 do
84
	if turtle.getItemCount(i) < leastItems then
85
	leastItems = turtle.getItemCount(i)
86
	end
87
end
88
for i=5,7 do
89
	if turtle.getItemCount(i) < leastItems then
90
	leastItems = turtle.getItemCount(i)
91
	end
92
end
93
for i=9,11 do
94
	if turtle.getItemCount(i) < leastItems then
95
	leastItems = turtle.getItemCount(i)
96
	end
97
end
98
--print(string.format("Crafting %d items ..", (leastItems-1)))
99
turtle.select(16)
100
turtle.craft(leastItems-1)
101
turtle.drop()
102
print("Done!")
103
leastItems = 64
104
os.sleep(2)
105
end