forked from kemayo/wow-bankstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.lua
More file actions
117 lines (106 loc) · 4.22 KB
/
stack.lua
File metadata and controls
117 lines (106 loc) · 4.22 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
local core = BankStack
local L = core.L
local Debug = core.Debug
local encode_bagslot = core.encode_bagslot
local decode_bagslot = core.decode_bagslot
local encode_move = core.encode_move
local moves = core.moves
local bag_ids = core.bag_ids
local bag_stacks = core.bag_stacks
local bag_maxstacks = core.bag_maxstacks
do
-- This is a stack filterer. It's used to stop full stacks being shuffled around
-- while compressing bags.
local function is_partial(itemid, bag, slot)
-- (stacksize - count) > 0
local bagslot = encode_bagslot(bag, slot)
return ((bag_maxstacks[bagslot] or 0) - (bag_stacks[bagslot] or 0)) > 0
end
core.is_partial = is_partial
local bag_groups = {}
function core.Compress(...)
for i=1, select("#", ...) do
local bags = select(i, ...)
core.Stack(bags, bags, is_partial)
end
end
end
-- Stacking:
local target_items = {--[[link = available_slots--]]}
local source_used = {}
local target_slots = {}
local summary = {}
local function default_can_move() return true end
function core.Stack(source_bags, target_bags, can_move)
-- Fill incomplete stacks in target_bags with items from source_bags.
-- source_bags: table, e.g. {1,2,3,4}
-- target_bags: table, e.g. {1,2,3,4}
-- can_move: function or nil. Called as can_move(itemid, bag, slot)
-- for any slot in source that is not empty and contains an item that
-- could be moved to target. If it returns false then ignore the slot.
if not can_move then can_move = default_can_move end
wipe(summary)
-- Model the target bags.
for _, bag, slot in core.IterateBags(target_bags, nil, "deposit") do
local bagslot = encode_bagslot(bag, slot)
local itemid = bag_ids[bagslot]
if not core.IsIgnored(bag, slot) and itemid and (bag_stacks[bagslot] ~= bag_maxstacks[bagslot]) then
-- This is an item type that we'll want to bother moving.
target_items[itemid] = (target_items[itemid] or 0) + 1
table.insert(target_slots, bagslot)
end
end
-- Now go through the source bags... in reverse.
for _, bag, slot in core.IterateBags(source_bags, true, "withdraw") do
local source_slot = encode_bagslot(bag, slot)
local itemid = bag_ids[source_slot]
if not core.IsIgnored(bag, slot) and itemid and target_items[itemid] and can_move(itemid, bag, slot) then
--there's an item in this slot *and* we have room for more of it in the bank somewhere
for i=#target_slots, 1, -1 do
local target_slot = target_slots[i]
if
bag_ids[source_slot] -- slot hasn't been emptied
and bag_ids[target_slot] == itemid -- target is same as source
and target_slot ~= source_slot -- target *isn't* source
and bag_stacks[target_slot] ~= bag_maxstacks[target_slot] -- target isn't full
and not source_used[target_slot] -- already moved from slot
then
-- can't stack to itself, or to a full slot, or to a slot that has already been used as a source:
-- record a summary of the move (has to happen before AddMove, since that updates bag_stacks)
summary[itemid] = (summary[itemid] or 0) + bag_stacks[source_slot]
-- Schedule moving from this slot to the bank slot.
core.AddMove(source_slot, target_slot)
source_used[source_slot] = true
if bag_stacks[target_slot] == bag_maxstacks[target_slot] then
target_items[itemid] = (target_items[itemid] > 1) and (target_items[itemid] - 1) or nil
end
if bag_stacks[source_slot] == 0 then
-- This bag slot is emptied, move on.
target_items[itemid] = (target_items[itemid] > 1) and (target_items[itemid] - 1) or nil
break
end
if not target_items[itemid] then break end
end
end
end
end
-- clean up the various cache tables
wipe(target_items)
wipe(target_slots)
wipe(source_used)
end
function core.StackSummary(...)
core.Stack(...)
local summary_text = {}
for itemid, count in pairs(summary) do
table.insert(summary_text, select(2, GetItemInfo(itemid)) .. 'x' .. count)
end
if #summary_text > 0 then
core.announce(1, "Stacking items: " .. string.join(", ", unpack(summary_text)))
end
end
SlashCmdList["BANKSTACK"] = core.CommandDecorator(core.StackSummary, "bags bank", 2)
SLASH_BANKSTACK1 = "/stack"
SlashCmdList["COMPRESSBAGS"] = core.CommandDecorator(core.Compress, "bags")
SLASH_COMPRESSBAGS1 = "/compress"
SLASH_COMPRESSBAGS2 = "/compressbags"