-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprescience.lua
368 lines (280 loc) · 11.3 KB
/
prescience.lua
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
--[[
TODO:
- [FEATURE] add range detection
]]--
-------------------------------------------------------------------------------
-- init
local tryUpdateStateDuration = function(state, expirationTime, duration)
-- Set the specified 'state' to have the specified 'expirationTime' and
-- 'duration'. Set 'state["changed"]' to 'true' and return 'true' if
-- 'state' is changed. Otherwise, return 'false'.
if state["expirationTime"] ~= expirationTime
or state["duration"] ~= duration then
state["changed"] = true
state["expirationTime"] = expirationTime
state["duration"] = duration
return true
end
return false
end
local black = CreateColor(0, 0, 0)
local addAutohideEnabledState = function(allstates, unitName)
-- Insert into 'allstates' an autohide-enabled state for the specified
-- 'unitName'. The inserted state will not include any progress
-- information. Return the inserted state.
local className, classFile = UnitClass(unitName)
local classColor = classFile and C_ClassColor.GetClassColor(classFile) or black
local state = { ["show"] = true,
["changed"] = true,
["index"] = 99, -- Order after the static set.
["progressType"] = "timed",
["autoHide"] = true,
["color"] = classColor,
["unitName"] = unitName,
["unitClass"] = className,
["dead"] = false }
allstates[unitName] = state
return state
end
aura_env.handleAuraChange = function(allstates, triggerStates)
-- Modify 'allstates' based on the auras described by the specified
-- 'triggerStates'. Return 'true' if any states within 'allstates' is
-- changed. Otherwise, return 'false'.
local changed = false
local activeUnitNames = {}
-- Iterate through all instances of prescience applied by the player.
for _, triggerState in pairs(triggerStates) do
local unit = triggerState["unit"]
local expirationTime = triggerState["expirationTime"]
local duration = triggerState["duration"]
local unitName = GetUnitName(unit, true)
activeUnitNames[unitName] = true
local matchingStateFound = false
-- Update any matches (possibly multiple) within the static set.
for index = 1, 3 do
local state = allstates[index]
if state["unitName"] == unitName then
matchingStateFound = true
changed = tryUpdateStateDuration(state,
expirationTime,
duration) or changed
end
end
-- If this prescience is not on a unit in the static set, then add an
-- 'autoHide'-enabled state for this unit, using the unit's name as the
-- clone ID.
if not matchingStateFound then
local state = allstates[unitName]
if state == nil then
state = addAutohideEnabledState(allstates, unitName)
changed = true
end
changed = tryUpdateStateDuration(state,
expirationTime,
duration) or changed
end
end
-- Update any states from 'allstates' that no longer have a corresponding
-- state in 'triggerStates'
for cloneId, state in pairs(allstates) do
if activeUnitNames[state["unitName"]] == nil then
if cloneId == 1 or cloneId == 2 or cloneId == 3 then
state["expirationTime"] = 1
else
state["show"] = false
end
state["changed"] = true
changed = true
end
end
return changed
end
aura_env.handleHealthChange = function(allstates, triggerStates)
-- Modify 'allstates' based on the unit healths described by the specified
-- 'triggerStates'. Return 'true' if any states within 'allstates' is
-- changed. Otherwise, return 'false'.
local deadUnitNames = {}
for unit, triggerState in pairs(triggerStates) do
local unitName = GetUnitName(unit, true)
if unitName ~= nil then
deadUnitNames[unitName] = true
end
end
local changed = false
for _, state in pairs(allstates) do
local newDead = (deadUnitNames[state["unitName"]] ~= nil)
if state["dead"] ~= newDead then
state["dead"] = newDead
state["changed"] = true
changed = true
end
end
return changed
end
local isAddressablePlayer = function(unitName)
-- Return 'true' if the specified 'unitName' is an addressable player unit.
-- Otherwise, return 'false'.
local unitGuid = UnitGUID(unitName)
return unitGuid ~= nil and string.sub(unitGuid, 1, 6) == "Player"
end
local assignStaticState = function(state, unitName)
-- Set the specified 'state' for the specified 'unitName'. Return 'nil'.
state["changed"] = true
state["unitName"] = unitName
if not isAddressablePlayer(unitName) then
state["color"] = black
state["unitClass"] = nil
state["expirationTime"] = 1
state["duration"] = 1
state["dead"] = false
else
local className, classFile = UnitClass(unitName)
state["color"] = C_ClassColor.GetClassColor(classFile)
state["unitClass"] = className
local _, _, _, _, duration, expirationTime = WA_GetUnitBuff(unitName,
410089,
"PLAYER")
state["expirationTime"] = expirationTime or 1
state["duration"] = duration or 1
state["dead"] = UnitIsDeadOrGhost(unitName)
end
end
aura_env.handleInit = function(allstates)
-- Modify the static states in 'allstates' to reflect the macroed
-- prescience targets. Return 'true' if any state is changed. Otherwise,
-- return 'false'.
local changed = false
local newStaticTargetNames = {}
local removedStaticTargetStates = {}
for index = 1, 3 do
-- Read the macro body for its target.
local macroName = 'Pres' .. index
local macroBody = GetMacroBody(macroName)
local targetBegin, targetEnd = string.find(macroBody, "@[^@]+,nodead%]")
targetBegin = targetBegin + 1
targetEnd = targetEnd - 8
local targetName = string.sub(macroBody, targetBegin, targetEnd)
newStaticTargetNames[targetName] = true
local state = allstates[index]
if state == nil then
state = { ["show"] = true,
["changed"] = true,
["index"] = index,
["progressType"] = "timed",
["autoHide"] = false }
allstates[index] = state
changed = true
end
local oldTargetName = state["unitName"]
if oldTargetName ~= targetName then
if oldTargetName ~= nil
and removedStaticTargetStates[oldTargetName] == nil then
-- Cache the preexisting state's duration info in case we need
-- to add an autohide-enabled state for its target.
removedStaticTargetStates[oldTargetName] = {
["expirationTime"] = state["expirationTime"],
["duration"] = state["duration"] }
end
-- Reassign this state.
assignStaticState(state, targetName)
changed = true
-- Set the color for the region if it already exists. Otherwise,
-- rely on the "condition" custom code block to change the color.
local region = WeakAuras.GetRegion(aura_env.id, index)
if region ~= nil then
region:Color(state["color"]:GetRGBA())
end
-- Also delete any autohide-enabled state maintained for this unit
-- (where the clone ID is the unit name).
local namedState = allstates[targetName]
if namedState ~= nil then
namedState["show"] = false
namedState["changed"] = true
changed = true
end
end
end
-- Check if any of the removed static targets have to be inserted as
-- autohide-enabled states.
for targetName, semiState in pairs(removedStaticTargetStates) do
if newStaticTargetNames[targetName] == nil then
-- 'targetName' no longer matches any of the static targets. Add
-- an autohide-enabled state for it and merge the duration info in.
local state = addAutohideEnabledState(allstates, targetName)
for k, v in pairs(semiState) do
state[k] = v
end
changed = true
end
end
return changed
end
aura_env.hasEmptyStaticTargets = function(allstates)
-- Return 'true' if any of the static targets in the specified 'allstates'
-- is not an addressable player. Otherwise, return 'false'.
for i = 1, 3 do
if not isAddressablePlayer(allstates[i]["unitName"]) then
return true
end
end
return false
end
-------------------------------------------------------------------------------
-- condition
local color = aura_env.state["color"]
local setColor = function() aura_env.region:Color(color:GetRGBA()) end
setColor()
-- Also set the color on the next frame as a way to circumvent WeakAuras
-- reverting the foreground color to its "Display" setting.
C_Timer.After(0, setColor)
-------------------------------------------------------------------------------
-- TSU: TRIGGER:2:3, INK_PRESCIENCE_TARGET_CHANGED, READY_CHECK
function(allstates, event, ...)
if event == 'TRIGGER' then
local triggerNumber, triggerStates = ...
if triggerNumber == 2 then
return aura_env.handleAuraChange(allstates, triggerStates)
else
return aura_env.handleHealthChange(allstates, triggerStates)
end
elseif event == "READY_CHECK" then
if aura_env.hasEmptyStaticTargets(allstates) then
WeakAuras.ScanEvents("INK_PRESCIENCE_TARGET_EMPTY")
end
return false
else -- 'INK_PRESCIENCE_TARGET_CHANGED' or 'STATUS'
local changed = aura_env.handleInit(allstates)
if not aura_env.hasEmptyStaticTargets(allstates) then
WeakAuras.ScanEvents("INK_PRESCIENCE_ALL_TARGETS_VALID")
end
return changed
end
end
-------------------------------------------------------------------------------
-- custom variables
{
["expirationTime"] = true,
["duration"] = true,
["unitName"] = "string",
["unitClass"] = "string",
["dead"] = "bool",
}
-------------------------------------------------------------------------------
--[[
Macro to set Prescience target:
```
local t=GetUnitName("target", true)
if t ~= nil then
local n="Pres1"
local m=GetMacroBody(n)
m=string.gsub(m, "@[^@]+,nodead%]", "@"..t..",nodead]")
EditMacro(n,n,nil,m)
WeakAuras.ScanEvents("INK_PRESCIENCE_TARGET_CHANGED")
end
```
Sample "Pres1" macro:
```
#showtooltip Prescience
/cast [@mouseover,nodead,help][@Kamelock-Zul'jin,nodead]Prescience
```
]]