-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorder.lua
More file actions
308 lines (277 loc) · 9.79 KB
/
Copy pathReorder.lua
File metadata and controls
308 lines (277 loc) · 9.79 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
---------------------------------------------------------------------------
-- PugzRaidTools - Reorder Engine
-- Ported from WeakAura: two-pass algorithm that computes minimal
-- SetRaidSubgroup / SwapRaidSubgroup calls to reach a target layout.
---------------------------------------------------------------------------
local _, PRT = ...
local RR_NAME = PRT.RR_NAME
local RR_SUBGROUP = PRT.RR_SUBGROUP
local RR_INDEX = PRT.RR_INDEX
local RR_LOCKED = PRT.RR_LOCKED
local RR_START = PRT.RR_START
function PRT:InitReorder()
self.pendingComp = nil
end
---------------------------------------------------------------------------
-- Queue / gate
---------------------------------------------------------------------------
function PRT:TryReorder()
if not self.pendingComp then return end
if not IsInRaid() then return end
if not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then
PRT.Print("You must be raid leader or assistant to reorder.")
self.pendingComp = nil
return
end
if InCombatLockdown and InCombatLockdown() then return end
if UnitAffectingCombat("player") then return end
local comp = self:GetComp(self.pendingComp)
if not comp then
self.pendingComp = nil
return
end
local target, targetError, targetReport =
self:CompileSortTarget(comp.roster, self.GetRaidRoster())
local runName = self.pendingComp
self.pendingComp = nil
self:ReportSortTargetSkips(targetReport)
if not target then
PRT.Print(targetError)
return
end
PRT.Print("Applying groups: " .. runName)
local autoMarkApplications
if PRT.BeginGroupSwapAutoMark then
autoMarkApplications = PRT:BeginGroupSwapAutoMark(runName)
end
C_Timer.After(0.2, function()
local actionCount = PRT:DoReorder(target)
if actionCount > 0 then
PRT:ShowNotification(runName .. " group sort commands sent.")
else
PRT:ShowNotification(runName .. " groups already matched.")
end
if PRT.FinishGroupSwapAutoMark then
PRT:FinishGroupSwapAutoMark(autoMarkApplications)
elseif PRT.OnGroupSwapForAutoMark then
PRT:OnGroupSwapForAutoMark(runName)
end
end)
end
function PRT:RequestReorder(compName, forcePos)
if not self:GetComp(compName) then
PRT.Print("No such composition: " .. tostring(compName))
return
end
if forcePos then
if self.RequestPositionReorder then
self:RequestPositionReorder(compName)
else
PRT.Print("The exact position sorter is not available.")
end
return
end
if self.CancelPositionSort then
self:CancelPositionSort("a fast group sort started")
end
self.pendingComp = compName
self:TryReorder()
end
---------------------------------------------------------------------------
-- Core reorder algorithm
---------------------------------------------------------------------------
function PRT:DoReorder(pTarget)
local pSubRaid = {}
local pCurrentRaid = {}
local pActionList = {}
local AL_TYPE = 1
local AL_ID1 = 2
local AL_ID2 = 3
local targetGroupByKey = {}
for group = 1, 8 do
for slot = 1, 5 do
local key = pTarget[group][slot][RR_NAME]
if key and key ~= "" then
targetGroupByKey[key] = group
end
end
end
-- helpers ---------------------------------------------------------------
local function InitRaid(pRaid)
for g = 1, 8 do
pRaid[g] = {}
for s = 1, 5 do
pRaid[g][s] = {
[RR_NAME] = "",
[RR_SUBGROUP] = g,
[RR_START] = 0,
[RR_LOCKED] = 0,
[RR_INDEX] = 0,
}
end
end
end
local function GetRaidInfo(pRaid)
local n = GetNumGroupMembers()
local counts = { 0, 0, 0, 0, 0, 0, 0, 0 }
for idx = 1, n do
local name, rank, grp = GetRaidRosterInfo(idx)
if name and grp and grp > 0 then
counts[grp] = counts[grp] + 1
local slot = counts[grp]
if slot <= 5 then
pRaid[grp][slot][RR_NAME] = PRT:GetRaidMemberIdentityKey(idx, name)
pRaid[grp][slot][RR_START] = grp
local key = pRaid[grp][slot][RR_NAME]
local leaderTargetGroup = targetGroupByKey[key]
-- A targeted leader may change subgroup. An omitted leader,
-- or one already in the requested subgroup, stays locked so
-- the planner cannot use them as unrelated collateral.
pRaid[grp][slot][RR_LOCKED] = rank == 2
and (not leaderTargetGroup
or leaderTargetGroup == grp)
and 1 or 0
pRaid[grp][slot][RR_INDEX] = idx
end
end
end
end
local function Match(a, b)
return a[RR_NAME] ~= "" and b[RR_NAME] ~= "" and a[RR_NAME] == b[RR_NAME]
end
local function LockPlayer(raidGroup, targetSlot)
for s = 1, 5 do
if raidGroup[s][RR_LOCKED] == 0 and Match(raidGroup[s], targetSlot) then
raidGroup[s][RR_LOCKED] = 1
return
end
end
end
local function LockTarget(targetGroup, raidSlot)
for s = 1, 5 do
if Match(targetGroup[s], raidSlot) then
raidSlot[RR_LOCKED] = 1
return
end
end
end
local function FindUnlockedInRaid(pRaid, targetSlot)
for g = 1, 8 do
for s = 1, 5 do
if pRaid[g][s][RR_LOCKED] == 0 and Match(pRaid[g][s], targetSlot) then
return pRaid[g][s]
end
end
end
end
local function FindUnlockedInGroup(raidGroup, destSlot, targetGroup)
-- prefer someone who belongs in the dest group
for s = 1, 5 do
if raidGroup[s][RR_LOCKED] == 0 then
for t = 1, 5 do
if Match(targetGroup[t], raidGroup[s]) then
return raidGroup[s]
end
end
end
end
-- then an empty slot
for s = 1, 5 do
if raidGroup[s][RR_LOCKED] == 0 and raidGroup[s][RR_NAME] == "" then
return raidGroup[s]
end
end
-- then anyone unlocked
for s = 1, 5 do
if raidGroup[s][RR_LOCKED] == 0 then
return raidGroup[s]
end
end
end
local function SwapSlots(src, dest)
src[RR_NAME], dest[RR_NAME] = dest[RR_NAME], src[RR_NAME]
src[RR_INDEX], dest[RR_INDEX] = dest[RR_INDEX], src[RR_INDEX]
src[RR_START], dest[RR_START] = dest[RR_START], src[RR_START]
src[RR_LOCKED] = 1
end
local function AddAction(aType, id1, id2)
pActionList[#pActionList + 1] = {
[AL_TYPE] = aType,
[AL_ID1] = id1,
[AL_ID2] = id2,
}
end
local function InvertActions(id1, id2)
for i = 1, #pActionList do
local a = pActionList[i]
if a[AL_TYPE] == "swap" then
if a[AL_ID1] == id1 then a[AL_ID1] = id2
elseif a[AL_ID1] == id2 then a[AL_ID1] = id1 end
if a[AL_ID2] == id1 then a[AL_ID2] = id2
elseif a[AL_ID2] == id2 then a[AL_ID2] = id1 end
end
end
end
local function ExecuteActions()
for i = 1, #pActionList do
local a = pActionList[i]
if a[AL_TYPE] == "swap" then
SwapRaidSubgroup(a[AL_ID1], a[AL_ID2])
elseif a[AL_TYPE] == "move" then
SetRaidSubgroup(a[AL_ID1], a[AL_ID2])
end
end
end
-- Pass 1: build intermediate raid by locking players already correct ---
InitRaid(pSubRaid)
GetRaidInfo(pSubRaid)
for g = 1, 8 do
for s = 1, 5 do
LockPlayer(pSubRaid[g], pTarget[g][s])
end
end
for g = 1, 8 do
for s = 1, 5 do
local dest = FindUnlockedInRaid(pSubRaid, pTarget[g][s])
if dest then
local src = FindUnlockedInGroup(pSubRaid[g], dest, pTarget[dest[RR_SUBGROUP]])
if src then
SwapSlots(src, dest)
if src[RR_NAME] ~= "" then
LockTarget(pTarget[dest[RR_SUBGROUP]], dest)
end
end
end
end
end
-- Pass 2: generate moves/swaps from current raid -> subRaid ------------
InitRaid(pCurrentRaid)
GetRaidInfo(pCurrentRaid)
for g = 1, 8 do
for s = 1, 5 do
LockPlayer(pCurrentRaid[g], pSubRaid[g][s])
end
end
for g = 1, 8 do
for s = 1, 5 do
local dest = FindUnlockedInRaid(pCurrentRaid, pSubRaid[g][s])
if dest then
local src = FindUnlockedInGroup(pCurrentRaid[g], dest, pSubRaid[dest[RR_SUBGROUP]])
if src then
if src[RR_START] == dest[RR_START] then
InvertActions(src[RR_INDEX], dest[RR_INDEX])
elseif src[RR_NAME] == "" then
AddAction("move", dest[RR_INDEX], g)
SwapSlots(src, dest)
else
AddAction("swap", src[RR_INDEX], dest[RR_INDEX])
SwapSlots(src, dest)
LockTarget(pSubRaid[dest[RR_SUBGROUP]], dest)
end
end
end
end
end
ExecuteActions()
return #pActionList
end