public
Description: WoW Addon - Fragger defrags your bags!
Homepage:
Clone URL: git://github.com/Zariel/Fragger.git
Fragger / core.lua
100644 316 lines (264 sloc) 6.113 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
local f = CreateFrame("Frame")
f:Hide()
local timer = 0
 
local jobList
local moveJob
local job
local queue = {}
local speed = 0
 
local itemWeights = {
["Weapon"] = 13,
["Armor"] = 12,
["Consumable"] = 11,
["Trade Goods"] = 10,
["Glyph"] = 9,
["Gem"] = 8,
["Recipe"] = 7,
["Goods"] = 6,
["Container"] = 5,
["Projectile"] = 4,
["Quiver"] = 3,
["Quest"] = 2,
["Miscellaneous"] = 1,
[0] = 0,
}
 
-- item class
local newItem = setmetatable({}, {
__call = function(self, ...)
local t = setmetatable({}, { __index = self })
t:__init(...)
return t
end
})
 
function newItem:__init(link)
local name, quality, iLevel, reqLevel, class, subClass, maxStack = false, 0, false, false, 0, false, false
 
if link then
name, _, quality, iLevel, reqLevel, class, subClass, maxStack = GetItemInfo(link)
end
 
self.name = name
self.quality = quality
self.ilevel = ilevel
self.level = level
self.class = class
self.subClass = subClass
self.type = link and GetItemFamily(link)
end
 
-- Sorting
local subclassSort = function(a, b)
end
 
local classSort = function(a, b)
if a and b then
return itemWeights[a.class] > itemWeights[b.class]
else
return true
end
end
 
local qualitySort = function(a, b)
if a and b then
if a.quality == b.quality then
--return classSort(a, b)
return false
else
return a.quality > b.quality
end
else
return true
end
-- return a.quality > b.quality
end
 
 
local sort = function(a, b)
return qualitySort(a, b)
end
 
-- Core
 
local itemInBag = function(item, bag)
local itemFamily = GetItemFamily(item)
local bagFamilty = GetItemFamily(bagFamily)
end
 
local getBags = function()
local t = {}
local i = 1
local bags = {}
local special = {}
local reverse = {}
 
for bag = 0, 4 do
local slots = GetContainerNumSlots(bag)
local type = select(2, GetContainerNumFreeSlots(bag))
if slots > 0 then
bags[bag] = slots
reverse[bag] = {}
end
 
if type and type > 0 then
special[bag] = { [0] = type }
end
 
for slot = 1, slots do
local link = GetContainerItemLink(bag, slot)
local item = newItem(link)
 
item.bag = bag
item.slot = slot
item.empty = not link
item.pos = i
 
t[i] = item
 
reverse[bag][slot] = i
 
if type and type > 0 then
special[bag][slot] = item
end
 
i = i + 1
end
end
 
return t, bags, special, reverse
end
 
local getFirstEmpty = function(bags)
for i = 1, #bags do
if bags[i].empty then
return i, bags[i]
end
end
end
 
local itemCount = function(bags)
local count = 0
for k, v in pairs(bags) do
if v.name then
count = count + 1
end
end
return count
end
 
local defragMap = function()
local bags, slot, special, reverse = getBags()
local map = {}
 
local specials = {}
for k, v in pairs(special) do
specials[v[0]] = k
end
 
-- Bottom up search to fill empty spaces
local item
for i = #bags, 1, -1 do
item = bags[i]
if item and item.name and not item.empty then
if item.type and item.type > 0 and specials[item.type] then
-- Madness :E
for b, t in pairs(special) do
-- Fits in the bag
if bit.band(item.type, t[0]) > 0 then
local k, v = getFirstEmpty(t)
if k and k < item.slot or v.bag ~= item.bag then
map[#map + 1] = { item.bag, item.slot, v.bag, v.slot }
bags[reverse[v.bag][v.slot]].empty = false
break
end
end
end
else
local k, v = getFirstEmpty(bags)
if k and k < i then
--Lets check if it can go in the bag
-- Is special!
 
--Move item -> v
map[#map + 1] = { item.bag, item.slot, v.bag, v.slot }
bags[k].empty = false
else
--break
end
end
end
end
 
return map
end
 
local sortMap = function()
local bags, slots, special = getBags()
local map = {}
 
for i = #bags, 1, -1 do
local item = bags[i]
if select(2, GetContainerNumFreeSlots(item.bag)) > 0 then
else
if item and item.name and not item.empty then
map[i] = item
end
end
end
 
-- Sorted table of items
table.sort(map, sort)
 
local reverse = {}
for k, v in pairs(map) do
reverse[v] = k
end
 
-- Second pass to put them into the correct location
local final = {}
for k, v in pairs(map) do
-- Move map[k] -> bags[k], need to update the location of
-- map[k]
final[#final + 1] = { v.bag, v.slot, bags[k].bag, bags[k].slot, v.quality }
 
local a = table.remove(bags, k)
table.insert(bags, v.pos, a)
v.pos = k
local bag, slot = a.bag, a.slot
a.bag = v.bag
a.slot = v.slot
 
a = map[revese[a]]
a.bag = bag
a.slot = slot
-- Is this the end?
end
 
-- Debug sanity pass
for k, v in ipairs(final) do
if final[k + 1] then
if final[k][5] < final[k + 1][5] then
print(string.format("wrong place at (%d, %d)", final[k][1], final[k][2]))
end
end
end
 
return final
end
 
local moveItem = function(fromBag, fromSlot, toBag, toSlot)
while true do
local _, _, locked1 = GetContainerItemInfo(fromBag, fromSlot)
local _, _, locked2 = GetContainerItemInfo(toBag, toSlot)
if locked1 or locked2 then
coroutine.yield()
else
break
end
end
 
if fromBag ~= toBag or fromSlot ~= toSLot then
print(string.format("Moving (%d, %d) -> (%d, %d)", fromBag, fromSlot, toBag, toSlot))
PickupContainerItem(fromBag, fromSlot)
PickupContainerItem(toBag, toSlot)
end
end
 
local walkJob = function(job)
if f:IsShown() then
queue[# queue + 1] = job
return
end
 
jobList = job()
 
f:Show()
end
 
-- walkJob(defragMap)
-- walkJob(sortMap)
 
f:SetScript("OnUpdate", function(self, elapsed)
timer = timer + elapsed
if timer < speed then return end
 
if jobList and next(jobList) then
if not moveJob or coroutine.status(moveJob) == "dead" then
moveJob = coroutine.create(moveItem)
job = table.remove(jobList, 1)
coroutine.resume(moveJob, unpack(job))
 
elseif coroutine.status(moveJob) == "suspended" then
coroutine.resume(moveJob, unpack(job))
end
else
jobList = nil
moveJob = nil
job = nil
 
self:Hide()
if #queue > 0 then
walkJob(table.remove(queue, 1))
end
end
 
timer = 0
end)
SlashCmdList["FRAGGER"] = function(str)
walkJob(defragMap)
walkJob(sortMap)
end
 
SLASH_FRAGGER1 = "/fragger"