Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
364 lines (321 loc) · 11 KB
/
Copy pathCore.lua
File metadata and controls
364 lines (321 loc) · 11 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
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
local ADDON, ns = ...
ns.DEFAULT_DEADZONE = 8
ns.MIN_DEADZONE = 0
ns.MAX_DEADZONE = 600
ns.DEFAULT_DURATION = 3.0
function ns.ClampDeadzone(v)
v = tonumber(v)
if not v then return nil end
if v < ns.MIN_DEADZONE then v = ns.MIN_DEADZONE end
if v > ns.MAX_DEADZONE then v = ns.MAX_DEADZONE end
return math.floor(v * 10 + 0.5) / 10
end
local DEFAULTS = {
enabled = true,
queueMode = false, -- one lane > sequential
layeredMode = false, -- one lane per NPC, lanes overlap
queueMax = 2, -- lines allowed to wait per lane
channel = "Dialog",
stopPrevious = true,
entries = nil,
seeded = false,
}
local function CopyDefaults(src, dst)
if type(dst) ~= "table" then dst = {} end
for k, v in pairs(src) do
if type(v) == "table" then
dst[k] = CopyDefaults(v, dst[k])
elseif dst[k] == nil then
dst[k] = v
end
end
return dst
end
--------------------------------------------------------------------------------
-- Duration estimate
--------------------------------------------------------------------------------
function ns.EstimateDuration(line)
if type(line) ~= "string" then return ns.DEFAULT_DURATION end
local words = 0
for _ in line:gmatch("%S+") do words = words + 1 end
if words == 0 then return ns.DEFAULT_DURATION end
return 0.85 + 0.32 * words
end
--------------------------------------------------------------------------------
-- Entries
--------------------------------------------------------------------------------
function ns.BuildEntriesFromPresets()
local entries = {}
for i, p in ipairs(ns.presets) do
local spellID = ns.ResolveSpell(p.spells)
entries[i] = {
key = p.key,
npc = p.npc,
boss = p.boss,
line = p.line,
sound = p.sound,
hint = p.hint,
spec = p.spec,
spellID = spellID,
deadzone = p.dz or ns.DEFAULT_DEADZONE,
dur = p.dur or ns.EstimateDuration(p.line),
enabled = true,
}
end
return entries
end
function ns.RestoreDefaults()
ns.DB.entries = ns.BuildEntriesFromPresets()
ns.RebuildIndex()
end
ns.index = {}
function ns.RebuildIndex()
wipe(ns.index)
for _, e in ipairs(ns.DB.entries) do
if e.spellID then ns.index[e.spellID] = e end
end
end
function ns.AssignSpell(entry, spellID)
for _, e in ipairs(ns.DB.entries) do
if e ~= entry and e.spellID == spellID then
e.spellID = nil
end
end
entry.spellID = spellID
ns.RebuildIndex()
end
--------------------------------------------------------------------------------
-- Spell info helpers
--------------------------------------------------------------------------------
function ns.SpellName(spellID)
if not spellID then return nil end
if C_Spell and C_Spell.GetSpellInfo then
local info = C_Spell.GetSpellInfo(spellID)
if info and info.name then return info.name end
end
return nil
end
function ns.SpellIcon(spellID)
if not spellID then return nil end
if C_Spell and C_Spell.GetSpellTexture then
return C_Spell.GetSpellTexture(spellID)
end
end
function ns.IsKnown(spellID)
return spellID and IsPlayerSpell and IsPlayerSpell(spellID)
end
--------------------------------------------------------------------------------
-- Playback
--
-- Handles are tracked per lane so that "stop previous" only cuts a line from the same lane.
--------------------------------------------------------------------------------
local handles = {}
function ns.LaneKey(entry)
if ns.DB.layeredMode then return entry.npc or "misc" end
return "all"
end
function ns.Play(fileDataID, laneKey)
fileDataID = tonumber(fileDataID)
if not fileDataID or fileDataID == 0 then return false end
laneKey = laneKey or "all"
if ns.DB.stopPrevious and handles[laneKey] then
StopSound(handles[laneKey])
handles[laneKey] = nil
end
local ok, handle = PlaySoundFile(fileDataID, ns.DB.channel or "Dialog")
if ok then handles[laneKey] = handle end
return ok
end
function ns.StopAll()
for _, h in pairs(handles) do StopSound(h) end
wipe(handles)
end
-- used by the Play button in the UI
function ns.Preview(entry)
return ns.Play(entry.sound, ns.LaneKey(entry))
end
--------------------------------------------------------------------------------
-- Lanes
--
-- queueMode -> a single lane called "all", so nothing ever overlaps.
-- layeredMode -> one lane per NPC, so up to three lines run at once while each
-- NPC still finishes a sentence before starting the next.
--------------------------------------------------------------------------------
local lanes = {}
local StartLane
StartLane = function(laneKey, lane, entry)
ns.Play(entry.sound, laneKey)
local dur = math.max(tonumber(entry.dur) or ns.DEFAULT_DURATION, 0.3)
lane.timer = C_Timer.NewTimer(dur, function()
lane.timer = nil
local nxt = table.remove(lane.queue, 1)
if nxt then StartLane(laneKey, lane, nxt) end
end)
end
function ns.ClearQueue()
for _, lane in pairs(lanes) do
if lane.timer then lane.timer:Cancel() end
end
wipe(lanes)
end
function ns.QueueLength()
local n = 0
for _, lane in pairs(lanes) do n = n + #lane.queue end
return n
end
-- single entry point for the cast handler
function ns.Trigger(entry)
if not (ns.DB.queueMode or ns.DB.layeredMode) then
ns.Play(entry.sound, "all") -- unchanged legacy behaviour
return
end
local laneKey = ns.LaneKey(entry)
local lane = lanes[laneKey]
if not lane then
lane = { queue = {} }
lanes[laneKey] = lane
end
if lane.timer then
if #lane.queue >= (ns.DB.queueMax or 2) then return end
for _, q in ipairs(lane.queue) do
if q == entry then return end
end
lane.queue[#lane.queue + 1] = entry
else
StartLane(laneKey, lane, entry)
end
end
--------------------------------------------------------------------------------
-- Cast handling
--------------------------------------------------------------------------------
ns.captureCallback = nil
local lastPlay = setmetatable({}, { __mode = "k" })
function ns.GetRemaining(entry)
local t = lastPlay[entry]
if not t then return 0 end
local dz = entry.deadzone or ns.DEFAULT_DEADZONE
local left = dz - (GetTime() - t)
return left > 0 and left or 0
end
function ns.ResetDeadzones()
wipe(lastPlay)
end
local function OnCast(unit, castGUID, spellID)
if ns.captureCallback then
local cb = ns.captureCallback
ns.captureCallback = nil
cb(spellID)
return
end
if not ns.DB.enabled then return end
local e = ns.index[spellID]
if not e or not e.enabled then return end
local now = GetTime()
local last = lastPlay[e]
-- 0.25s hard floor also swallows the duplicate SUCCEEDED some spells fire
local gap = math.max(e.deadzone or ns.DEFAULT_DEADZONE, 0.25)
if last and (now - last) < gap then return end
lastPlay[e] = now
ns.Trigger(e)
end
--------------------------------------------------------------------------------
-- Migrations
--------------------------------------------------------------------------------
function ns.RunMigrations()
local from = ns.DB.dataVersion or 1
if from >= ns.DATA_VERSION then return end
local changed = 0
for v = from + 1, ns.DATA_VERSION do
for _, m in ipairs(ns.MIGRATIONS[v] or {}) do
for _, e in ipairs(ns.DB.entries) do
if e.key == m.key then
local isOldDefault = false
for _, id in ipairs(m.oldSpells) do
if e.spellID == id then isOldDefault = true break end
end
if isOldDefault or e.spellID == nil then
e.spellID = ns.ResolveSpell(m.newSpells)
local p
for _, pp in ipairs(ns.presets) do
if pp.key == m.key then p = pp break end
end
if p then e.hint = p.hint end
changed = changed + 1
end
end
end
end
end
if changed > 0 then
print("|cffffd28fVanguardMe|r: updated " .. changed ..
" default binding(s) for this version.")
end
end
--------------------------------------------------------------------------------
-- Events
--------------------------------------------------------------------------------
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player")
f:SetScript("OnEvent", function(self, event, ...)
if event == "UNIT_SPELLCAST_SUCCEEDED" then
OnCast(...)
elseif event == "ADDON_LOADED" and ... == ADDON then
VanguardMeDB = CopyDefaults(DEFAULTS, VanguardMeDB or {})
ns.DB = VanguardMeDB
elseif event == "PLAYER_LOGIN" then
if not ns.DB.seeded or not ns.DB.entries then
ns.DB.entries = ns.BuildEntriesFromPresets()
ns.DB.seeded = true
else
-- backfill fields added by later versions
local byKey = {}
for _, p in ipairs(ns.presets) do byKey[p.key] = p end
for _, e in ipairs(ns.DB.entries) do
local p = byKey[e.key]
if e.deadzone == nil then
e.deadzone = (p and p.dz) or ns.DEFAULT_DEADZONE
end
if e.dur == nil then
e.dur = (p and p.dur) or ns.EstimateDuration(e.line)
end
end
ns.RunMigrations()
end
ns.DB.dataVersion = ns.DATA_VERSION
-- queue and layered are mutually exclusive; layered wins if both stuck
if ns.DB.queueMode and ns.DB.layeredMode then
ns.DB.queueMode = false
end
ns.RebuildIndex()
if ns.BuildUI then ns.BuildUI() end
end
end)
--------------------------------------------------------------------------------
-- Slash
--------------------------------------------------------------------------------
SLASH_VANGUARDME1 = "/vgm"
SLASH_VANGUARDME2 = "/vanguardme"
SlashCmdList["VANGUARDME"] = function(msg)
msg = (msg or ""):lower():gsub("^%s+", ""):gsub("%s+$", "")
if msg == "off" then
ns.DB.enabled = false
ns.StopAll()
ns.ClearQueue()
print("|cffffd28fVanguardMe|r: off.")
elseif msg == "on" then
ns.DB.enabled = true
ns.ResetDeadzones()
ns.ClearQueue()
print("|cffffd28fVanguardMe|r: on.")
elseif msg == "reset" then
ns.RestoreDefaults()
ns.ResetDeadzones()
ns.ClearQueue()
if ns.Refresh then ns.Refresh() end
print("|cffffd28fVanguardMe|r: bindings restored to defaults.")
else
if ns.Toggle then ns.Toggle() end
end
end