Files

430 lines
19 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo boss mods, built into the client. Every trigger is a native client event generated from the
-- authoritative server packets this client receives (spell cast start/go, aura, damage, death, boss
-- yells/emotes). There are no syncs between players: each client sees the same server events, so
-- every raider's timers agree. Encounter logic is ported from Octo's BigWigs fork (Hapto/Bigwigs).
local ADDON = ...
local BM = {modules = {}, byNpc = {}, active = nil, scheduled = {}, version = 1}
OctoBossMods = BM
local SOUND_DIR = "Interface\\AddOns\\" .. ADDON .. "\\Sounds\\"
BM.sounds = {
Long = "Long.mp3", Info = "Info.ogg", Alert = "Alert.mp3", Alarm = "Alarm.mp3", Victory = "Victory.mp3",
Beware = "Beware.wav", RunAway = "RunAway.wav",
One = "1.ogg", Two = "2.ogg", Three = "3.ogg", Four = "4.ogg", Five = "5.ogg",
Six = "6.ogg", Seven = "7.ogg", Eight = "8.ogg", Nine = "9.ogg", Ten = "10.ogg",
}
-- Octo BigWigs Colors.lua defaults.
BM.messageColors = {
Important = {1, 0, 0}, Personal = {1, 0, 0}, Urgent = {1, 0.5, 0}, Attention = {1, 1, 0},
Positive = {0, 1, 0}, Bosskill = {0, 1, 0}, Core = {0, 1, 1}, Purple = {0.4, 0, 0.6},
}
BM.barColors = {
Red = {1, 0, 0}, Orange = {1, 0.5, 0}, Yellow = {1, 1, 0}, Green = {0, 1, 0}, Cyan = {0, 1, 1},
Blue = {0.2, 0.4, 1}, Black = {0.15, 0.15, 0.15}, White = {1, 1, 1}, Purple = {0.6, 0.2, 0.8}, Magenta = {1, 0, 1},
}
local defaults = {enabled = true, sound = true, messages = true, bars = true, disabled = {}}
local db
-- 70334 hands addons "secret" unit values (e.g. UnitGUID of an instance target): they cannot be read as text.
local function secret(value)
return issecretvalue ~= nil and issecretvalue(value) == true
end
BM.IsSecret = secret
local function npcId(guid)
if type(guid) ~= "string" or secret(guid) then return nil end
local kind, _, _, _, _, id = strsplit("-", guid)
if kind == "Creature" or kind == "Vehicle" then return tonumber(id) end
return nil
end
BM.NpcId = npcId
function BM:Print(text)
DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99Boss Mods|r: " .. tostring(text))
end
-- Timers -----------------------------------------------------------------------------------------
local ticker = CreateFrame("Frame")
ticker:Hide()
ticker:SetScript("OnUpdate", function()
local now, due = GetTime(), {}
-- Collect first: a callback may schedule new keys, which must not happen during pairs().
for key, entry in pairs(BM.scheduled) do
if entry.at <= now then due[#due + 1] = key end
end
for _, key in ipairs(due) do
local entry = BM.scheduled[key]
if entry and entry.at <= now then
BM.scheduled[key] = nil
local ok, err = pcall(entry.fn)
if not ok then BM:Print(err) end
end
end
if not next(BM.scheduled) then ticker:Hide() end
end)
function BM:Schedule(key, delay, fn)
self.scheduled[key] = {at = GetTime() + delay, fn = fn}
ticker:Show()
end
function BM:Cancel(key) self.scheduled[key] = nil end
function BM:CancelAll()
-- Encounter timers only; the pending feed subscription survives a reset.
for key in pairs(self.scheduled) do if key ~= "\1subscribe" then self.scheduled[key] = nil end end
end
-- Presentation -----------------------------------------------------------------------------------
function BM:PlaySound(name)
if not db.sound or not name then return end
local file = self.sounds[name]
if file then PlaySoundFile(SOUND_DIR .. file, "Master") end
end
function BM:ShowMessage(text, color, sound)
if db.messages and text then
local c = self.messageColors[color or "Attention"] or self.messageColors.Attention
RaidNotice_AddMessage(RaidWarningFrame, text, {r = c[1], g = c[2], b = c[3]})
DEFAULT_CHAT_FRAME:AddMessage(text, c[1], c[2], c[3])
end
if sound then self:PlaySound(sound) end
end
-- Module API (names follow Octo BigWigs so ported modules read the same) ----------------------------
local Module = {}
Module.__index = Module
function BM:NewModule(name, spec)
local m = setmetatable(spec or {}, Module)
m.name = name
m.npcs = m.npcs or {}
m.events = m.events or {}
m.yells = m.yells or {}
m.emotes = m.emotes or {}
self.modules[name] = m
for _, id in ipairs(m.npcs) do self.byNpc[id] = m end
return m
end
local function key(m, kind, text) return m.name .. "\1" .. kind .. "\1" .. text end
function Module:Bar(text, seconds, icon, _, color)
if not db.bars then return end
BM.Bars:Start(text, seconds, icon, BM.barColors[color or "Orange"] or BM.barColors.Orange)
end
function Module:RespawnBar(text, seconds, icon, color)
if not db.bars then return end
BM.Bars:Start(text, seconds, icon, BM.barColors[color or "Magenta"] or BM.barColors.Orange, nil, true)
end
function Module:IntervalBar(text, low, high, icon, _, color)
if not db.bars then return end
BM.Bars:Start(text, high, icon, BM.barColors[color or "Orange"] or BM.barColors.Orange, low)
end
function Module:RemoveBar(text) BM.Bars:Stop(text) end
function Module:DelayedBar(delay, text, seconds, icon, flag, color)
BM:Schedule(key(self, "bar", text), delay, function() self:Bar(text, seconds, icon, flag, color) end)
end
function Module:CancelDelayedBar(text) BM:Cancel(key(self, "bar", text)) end
function Module:Message(text, color, _, sound) BM:ShowMessage(text, color, sound) end
function Module:DelayedMessage(delay, text, color, _, sound)
BM:Schedule(key(self, "msg", text), delay, function() BM:ShowMessage(text, color, sound) end)
end
function Module:CancelDelayedMessage(text) BM:Cancel(key(self, "msg", text)) end
function Module:Sound(name) BM:PlaySound(name) end
function Module:WarningSign(icon, seconds, _, text) BM.Bars:Sign(icon, seconds, text) end
function Module:RemoveWarningSign(icon) BM.Bars:Unsign(icon) end
function Module:ScheduleEvent(name, fn, delay) BM:Schedule(key(self, "event", name), delay, fn) end
function Module:CancelScheduledEvent(name) BM:Cancel(key(self, "event", name)) end
function Module:ScheduleRepeatingEvent(name, fn, interval)
local k = key(self, "event", name)
local function run() if BM.active == self then fn(self) BM:Schedule(k, interval, run) end end
BM:Schedule(k, interval, run)
end
function Module:Engaged() return BM.active == self end
function BM:UnitByName(name)
if not name then return nil end
if UnitName("player") == name then return "player" end
local prefix, count = "party", GetNumSubgroupMembers()
if IsInRaid() then prefix, count = "raid", GetNumGroupMembers() end
for i = 1, count do local n = UnitName(prefix .. i) if not secret(n) and n == name then return prefix .. i end end
end
local MARKS = {Star = 1, Circle = 2, Diamond = 3, Triangle = 4, Moon = 5, Square = 6, Cross = 7, Skull = 8}
-- Raid marks follow Octo BigWigs RaidIcon: only the leader or an assistant marks, and the previous mark returns.
function Module:SetRaidTargetForPlayer(name, mark)
local unit = BM:UnitByName(name)
if not unit or not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then return end
self.marks = self.marks or {}
if self.marks[name] == nil then self.marks[name] = GetRaidTargetIndex(unit) or 0 end
SetRaidTarget(unit, MARKS[mark] or mark)
end
function Module:RestorePreviousRaidTargetForPlayer(name)
local previous = self.marks and self.marks[name]
if previous == nil then return end
self.marks[name] = nil
local unit = BM:UnitByName(name)
if unit and (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then SetRaidTarget(unit, previous) end
end
-- One reaction per server event burst (e.g. a raid-wide hit lands on 40 players at once).
function Module:Throttle(name, seconds)
self.throttle = self.throttle or {}
local now = GetTime()
if (self.throttle[name] or 0) > now then return false end
self.throttle[name] = now + seconds
return true
end
-- Encounter state --------------------------------------------------------------------------------
local function anyGroupInCombat()
if UnitAffectingCombat("player") then return true end
local prefix, count = "party", GetNumSubgroupMembers()
if IsInRaid() then prefix, count = "raid", GetNumGroupMembers() end
for i = 1, count do
local unit = prefix .. i
if UnitAffectingCombat(unit) and not UnitIsDeadOrGhost(unit) then return true end
end
return false
end
function BM:Engage(m)
if self.active == m or not db.enabled or db.disabled[m.name] then return end
if self.active then self:Reset() end
self.active = m
m.dead = {}
m.throttle = {}
m.seen = {n = 0}
m.started = GetTime()
if m.OnEngage then m:OnEngage() end
end
function BM:Reset()
local m = self.active
if m and m.seen and db then
db.lastEncounters = db.lastEncounters or {}
local record = {name = m.name, seconds = floor(GetTime() - (m.started or GetTime())), events = m.seen}
tinsert(db.lastEncounters, 1, record)
while #db.lastEncounters > 5 do tremove(db.lastEncounters) end
end
self.active = nil
self:CancelAll()
self.Bars:StopAll()
if m and m.OnDisengage then m:OnDisengage() end
end
function BM:Victory(m)
self:ShowMessage(format("%s has been defeated", m.displayName or m.name), "Bosskill", "Victory")
self:Reset()
end
local function wipeCheck()
if not BM.active then return end
if anyGroupInCombat() then BM:Schedule("\1wipe", 2, wipeCheck) else BM:Reset() end
end
-- Event routing ------------------------------------------------------------------------------------
local events = CreateFrame("Frame")
events:RegisterEvent("ADDON_LOADED")
events:RegisterEvent("CHAT_MSG_ADDON")
events:RegisterEvent("PLAYER_ENTERING_WORLD")
events:RegisterEvent("CHAT_MSG_MONSTER_YELL")
events:RegisterEvent("CHAT_MSG_MONSTER_EMOTE")
events:RegisterEvent("CHAT_MSG_RAID_BOSS_EMOTE")
events:RegisterEvent("PLAYER_REGEN_DISABLED")
events:RegisterEvent("PLAYER_REGEN_ENABLED")
events:RegisterEvent("PLAYER_TARGET_CHANGED")
events:RegisterEvent("UNIT_HEALTH")
local function engageFrom(guid)
local m = BM.byNpc[npcId(guid) or 0]
if m and UnitAffectingCombat("player") then BM:Engage(m) end
return m
end
function BM:UnitDied(guid, name)
local m, id = self.active, npcId(guid)
if not m or not id or m.dead[id] then return end
m.dead[id] = true
if m.OnUnitDied then m:OnUnitDied(id, guid, name) end
local all = true
for _, need in ipairs(m.killNpcs or {m.npcs[1]}) do if not m.dead[need] then all = false end end
-- manualKill: the module calls Victory itself (bosses that fake-die or submit).
if all and self.active == m and not m.trash and not m.manualKill then self:Victory(m) end
end
-- Fallback for a death the combat log does not report: the unit itself reads dead.
local function unitHealth(unit)
if BM.active and UnitExists(unit) and UnitIsDead(unit) then BM:UnitDied(UnitGUID(unit), UnitName(unit)) end
end
-- Server combat facts arrive from the Backend as local "OctoBM" addon messages (70334 gives addons no
-- combat log): "sub;spell;src;dst;amount;extra", actor "-" | "P<name>" | "C<entry>:<low>". Every raider's
-- client hears the same server packets, so warnings never depend on another player's sync.
local FEED = "OctoBM"
local function actor(text)
local kind = strsub(text, 1, 1)
if kind == "P" then
local name = strsub(text, 2)
if name == UnitName("player") then return UnitGUID("player"), name end
return "Player-0-" .. name, name
elseif kind == "C" then
local entry, low = strmatch(text, "^C(%d+):(%d+)$")
if entry then return "Creature-0-0-0-0-" .. entry .. "-" .. low, nil end
end
return nil, nil
end
local function spellName(id)
if not id then return nil end
if C_Spell and C_Spell.GetSpellName then return C_Spell.GetSpellName(id) end
return GetSpellInfo and (GetSpellInfo(id)) or nil
end
function BM:CombatEvent(sub, spellId, srcGUID, srcName, dstGUID, dstName, amount, extra)
if not BM.active then
engageFrom(srcGUID)
if not BM.active then engageFrom(dstGUID) end
end
local m = BM.active
if not m then return end
local name = spellName(spellId)
-- Evidence for verification: which server events (subevent:spell) this encounter actually delivered.
local seen = m.seen
if seen and seen.n < 400 then
local k = sub .. ":" .. tostring(spellId or "")
if not seen[k] then seen[k] = 0 seen.n = seen.n + 1 end
seen[k] = seen[k] + 1
end
-- Handler arguments keep the old combat-log positions: (..., spellId, spellName, school, auraType, amount).
-- SWING events carry the miss type where spell events carry the spell, as the native combat log did.
local first = strsub(sub, 1, 6) == "SWING_" and extra or spellId
if m.OnCombatLog then m:OnCombatLog(sub, srcGUID, srcName, dstGUID, dstName, first, name, nil, nil, amount) end
if sub == "UNIT_DIED" then
BM:UnitDied(dstGUID, dstName)
return
end
-- Keys are spell IDs; Octo-only spells the local data lacks are keyed by their native name.
local byEvent = m.events[sub]
local handler = byEvent and ((spellId and byEvent[spellId]) or (name and byEvent[name]))
if handler then
local fn = type(handler) == "function" and handler or m[handler]
fn(m, srcGUID, srcName, dstGUID, dstName, spellId, name, nil, nil, amount)
end
end
local function feedMessage(text)
local sub, spell, src, dst, amount, extra = strsplit(";", text)
if not sub or not dst then return end
local srcGUID, srcName = actor(src)
local dstGUID, dstName = actor(dst)
spell = tonumber(spell)
BM:CombatEvent(sub, spell ~= 0 and spell or nil, srcGUID, srcName, dstGUID, dstName, tonumber(amount),
extra ~= "" and extra or nil)
end
-- Tell the Backend which spells the loaded encounters handle (stays inside this client).
function BM:Subscribe()
local ids, seen = {}, {}
for _, m in pairs(self.modules) do
for _, handlers in pairs(m.events or {}) do
for key in pairs(handlers) do
if type(key) == "number" and not seen[key] then seen[key] = true ids[#ids + 1] = key end
end
end
for _, id in ipairs(m.feedSpells or {}) do
if not seen[id] then seen[id] = true ids[#ids + 1] = id end
end
end
table.sort(ids)
local me = UnitName("player")
if not me or not C_ChatInfo then return end
C_ChatInfo.SendAddonMessage(FEED, "R", "WHISPER", me)
local line = "S"
for _, id in ipairs(ids) do
if #line + #tostring(id) + 1 > 240 then
C_ChatInfo.SendAddonMessage(FEED, line, "WHISPER", me)
line = "S"
end
line = line .. " " .. id
end
if line ~= "S" then C_ChatInfo.SendAddonMessage(FEED, line, "WHISPER", me) end
self.subscribed = #ids
end
local function chatTrigger(list, text, sender)
local m = BM.active
if not m then
-- Pre-pull event speech (e.g. Vaelastrasz's gossip run-up) is handled while no encounter runs.
for _, mod in pairs(BM.modules) do
for _, entry in ipairs(mod.idleYells or {}) do
if strfind(text, entry[1], 1, true) then
local fn = type(entry[2]) == "function" and entry[2] or mod[entry[2]]
fn(mod, text, sender)
end
end
end
for _, mod in pairs(BM.modules) do
for _, entry in ipairs(mod.engageYells or {}) do
if strfind(text, entry, 1, true) then BM:Engage(mod) m = mod break end
end
if m then break end
end
if not m then return end
end
for _, entry in ipairs(m[list]) do
if strfind(text, entry[1], 1, not entry.pattern) then
local fn = type(entry[2]) == "function" and entry[2] or m[entry[2]]
fn(m, text, sender)
end
end
end
events:SetScript("OnEvent", function(_, event, ...)
if event == "ADDON_LOADED" then
if ... ~= ADDON then return end
OctoBossModsDB = OctoBossModsDB or {}
db = OctoBossModsDB
for k, v in pairs(defaults) do if db[k] == nil then db[k] = type(v) == "table" and {} or v end end
BM.db = db
BM.Bars:Init(db)
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(FEED) end
elseif not db then
return
elseif event == "CHAT_MSG_ADDON" then
local prefix, text, _, sender = ...
if prefix == FEED and (sender == nil or Ambiguate(sender, "none") == UnitName("player")) then feedMessage(text) end
elseif event == "PLAYER_ENTERING_WORLD" then
-- A locally consumed request makes the Wire drain world packets. Right after login the spell-locator
-- barrier (bounded at 30 s) must not see such a drain, so the subscription waits until it is over.
local isLogin, isReload = ...
BM:Schedule("\1subscribe", (isLogin and not isReload) and 45 or 1, function() BM:Subscribe() end)
elseif event == "CHAT_MSG_MONSTER_YELL" then
chatTrigger("yells", ...)
elseif event == "CHAT_MSG_MONSTER_EMOTE" or event == "CHAT_MSG_RAID_BOSS_EMOTE" then
chatTrigger("emotes", ...)
elseif event == "PLAYER_REGEN_DISABLED" or event == "PLAYER_TARGET_CHANGED" then
if not BM.active then engageFrom(UnitGUID("target")) end
elseif event == "UNIT_HEALTH" then
unitHealth(...)
elseif event == "PLAYER_REGEN_ENABLED" then
if BM.active then BM:Schedule("\1wipe", 2, wipeCheck) end
end
end)
-- /pull uses the native countdown; the Backend carries it to BigWigs raiders as their pull timer.
SLASH_OCTOBOSSMODS1 = "/bossmods"
SLASH_OCTOBOSSMODS2 = "/obm"
SlashCmdList.OCTOBOSSMODS = function(msg)
msg = strlower(strtrim(msg or ""))
if msg == "unlock" or msg == "lock" then
BM.Bars:SetLocked(msg == "lock")
elseif msg == "sound" then
db.sound = not db.sound BM:Print("sound " .. (db.sound and "on" or "off"))
elseif msg == "reset" then
BM:Reset() BM:Print("encounter reset")
elseif msg == "test" then
BM.Bars:Start("Test bar", 10, 136116, BM.barColors.Orange)
BM:ShowMessage("Boss mods test warning", "Important", "Alarm")
else
db.enabled = not db.enabled
if not db.enabled then BM:Reset() end
BM:Print(db.enabled and "enabled" or "disabled")
end
end
SLASH_OCTOPULL1 = "/pull"
SlashCmdList.OCTOPULL = function(msg)
local seconds = tonumber(strtrim(msg or "")) or 6
if seconds < 0 or seconds > 3600 then return end
if C_PartyInfo and C_PartyInfo.DoCountdown then C_PartyInfo.DoCountdown(seconds) end
end