Files

345 lines
15 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- OctoReplay grave panel, adapted from patch-5.mpq OctoReplay.lua lines 2477–2839 (1e08296a).
-- Server capability bit 8 enables the 1.12 grave inspection response.
local _, L = ...
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
local replyPrefix = "OWLDTH"
local requestPrefix = "GWGRAVE"
local graveFrame, graveFacts, graveWords, watchState, watchTimer, fragments
local function sendRequest(payload)
local player = UnitName("player")
if (issecretvalue and issecretvalue(player)) or not player or player == "" then return false end
local result = C_ChatInfo.SendAddonMessage(requestPrefix, payload, "WHISPER", player)
return result == 0 or result == true
end
local function cancelWatch()
watchState = nil
if watchTimer then watchTimer:Cancel(); watchTimer = nil end
end
local CLASS_NAME = { [1]=L.CLASS_WARRIOR, [2]=L.CLASS_PALADIN, [3]=L.CLASS_HUNTER, [4]=L.CLASS_ROGUE, [5]=L.CLASS_PRIEST,
[7]=L.CLASS_SHAMAN, [8]=L.CLASS_MAGE, [9]=L.CLASS_WARLOCK, [11]=L.CLASS_DRUID }
local RACE_NAME = { [1] = L.RACE_HUMAN, [2] = L.RACE_ORC, [3] = L.RACE_DWARF,
[4] = L.RACE_NIGHTELF, [5] = L.RACE_UNDEAD, [6] = L.RACE_TAUREN,
[7] = L.RACE_GNOME, [8] = L.RACE_TROLL }
local function RaceName(r) return RACE_NAME[r] or "" end
local function ClassNameOf(c) return CLASS_NAME[c] or "" end
local function GraveCauseLine(kind, attacker, attackerLevel)
kind = kind or 0
if kind == 1 or kind == 2 then
local who = (attacker and attacker ~= "") and attacker or L.HC_A_FOE
local lvl = tonumber(attackerLevel) or 0
if lvl > 0 then return string.format(L.HC_FELL_TO_LVL, who, lvl) end
return string.format(L.HC_FELL_TO, who)
end
if kind == 3 then return L.HC_DROWNED end
if kind == 4 then return L.HC_BURNED end
if kind == 5 then return L.HC_EXHAUSTION end
if kind == 6 then return L.HC_NATURAL end
return ""
end
local function GraveWatchReason(code)
if code == "ok" then return nil end
if code == "private" then return L.HC_PRIVATE end
if code == "notyet" then return L.HC_NOTYET end
if code == "notrec" then return L.HC_NOTREC end
if code == "instance" then return L.HC_INSTANCE end
if code == "off" then return L.HC_OFF end
return L.HC_CANT_NOW
end
local function GhostEndText(code)
code = code or ""
if code == "done" or code == "teleport" or code == "death" or code == "grave" then
return L.HC_FADES
end
if code == "stop" then return L.HC_LOOK_AWAY end
if code == "attacked" then return L.HC_ATTACKED end
if code == "off" then return L.HC_VISION_OFF end
if code == "far" then return L.HC_STAND_BY end
if code == "busy" then return L.HC_BUSY end
if code == "unsafe" then return L.HC_UNSAFE end
if code == "instance" then return L.HC_INSTANCE end
if code == "private" then return L.HC_PRIVATE end
if code == "notyet" then return L.HC_NOTYET end
if code == "notrec" then return L.HC_NOTREC end
if code == "combat" then return L.HC_COMBAT end
if code == "dead" then return L.HC_DEAD end
if code == "bad" then return L.HC_BAD end
return L.HC_VISION_OFF
end
-- A pre-start ERR uses the keeper's shared refusal vocabulary, except for a
-- grave's distance check, which concerns the stone itself.
local refusal = {
notyours = L.HC_ERR_NOTYOURS, private = L.HC_ERR_PRIVATE, gone = L.HC_ERR_GONE,
moved = L.HC_ERR_MOVED, far = L.HC_STAND_BY, instance = L.HC_ERR_INSTANCE,
toosoon = L.HC_ERR_TOOSOON, notnow = L.HC_ERR_NOTNOW,
}
local function updateWatch()
local reason = GraveWatchReason(graveFacts.watchCode)
if not reason and UnitAffectingCombat("player") then reason = L.HC_COMBAT end
graveFrame.watch:SetEnabled(reason == nil and not watchState)
if reason then
graveFrame.status:Hide()
graveFrame.reason:SetText(reason)
graveFrame.reason:Show()
else
graveFrame.reason:Hide()
end
end
local function status(text)
graveFrame.reason:Hide()
graveFrame.status:SetText(text)
graveFrame.status:Show()
end
local function finishWatch(text)
cancelWatch()
if graveFrame:IsShown() then status(text) end
graveFrame.watch:Show()
updateWatch()
DEFAULT_CHAT_FRAME:AddMessage("|cff66ccff" .. L.CHAT_TAG .. "|r " .. text)
end
local STONE_TEX = "Interface\\ItemTextFrame\\ItemText-Stone-"
local STONE_WIDE = 256
local STONE_NARROW = 64
local SLAB_TEX_ROWS = 256
local BORDER_INSETS = { left = 11, right = 12, top = 12, bottom = 11 }
local STONE_TITLE = { 0.93, 0.82, 0.00 }
local STONE_BODY = { 1.00, 1.00, 1.00 }
local STONE_SUB = { 0.80, 0.80, 0.80 }
local WOOD_TEX = "Interface\\AddOns\\OctoReplayGrave\\GraveWood"
local WOOD_TEX_W = 512
local HORDE_RACE = { [2] = true, [5] = true, [6] = true, [8] = true, [9] = true }
local function GraveIsWood(facts)
return (tonumber(facts.level) or 0) < 20 and not HORDE_RACE[tonumber(facts.race) or 0]
end
local PAD_TOP, GAP_TITLE, GAP_LINE, GAP_MSG, MSG_H, BTN_H, PAD_BOTTOM = 20, 10, 6, 10, 24, 22, 18
local function SlabPiece(g, file, w, relativeTo, relativePoint, x, y)
local t = g:CreateTexture(nil, "BACKGROUND")
t:SetTexture(file)
t:SetWidth(w)
t:SetPoint("TOPLEFT", relativeTo, relativePoint, x, y)
return t
end
local function SlabRows(t, rows, uRight)
t:SetHeight(rows)
if rows <= SLAB_TEX_ROWS then t:SetTexCoord(0, uRight, 0, rows / SLAB_TEX_ROWS)
else t:SetTexCoord(0, uRight, 0, 1) end
end
local function OctoReplayGrave_Layout()
local g = graveFrame
local y = -PAD_TOP
local function put(fs, gap)
fs:ClearAllPoints(); fs:SetPoint("TOP", g, "TOP", 0, y)
local h = fs:GetHeight()
if not h or h < 1 then h = 12 end
y = y - h - gap
end
put(g.title, GAP_TITLE)
put(g.ident, GAP_LINE)
if g.cause:IsShown() then put(g.cause, GAP_LINE) end
if g.words:IsShown() then put(g.words, GAP_LINE) end
y = y + GAP_LINE - GAP_MSG
g.reason:ClearAllPoints(); g.reason:SetPoint("CENTER", g, "TOP", 0, y - MSG_H / 2)
g.status:ClearAllPoints(); g.status:SetPoint("CENTER", g, "TOP", 0, y - MSG_H / 2)
y = y - MSG_H - GAP_MSG
g.watch:ClearAllPoints(); g.watch:SetPoint("TOP", g, "TOP", 0, y)
local height = BTN_H + PAD_BOTTOM - y
g:SetHeight(height)
local rows = height - BORDER_INSETS.top - BORDER_INSETS.bottom
SlabRows(g.stoneL, rows, 1)
SlabRows(g.stoneR, rows, 1)
SlabRows(g.wood, rows, (STONE_WIDE + STONE_NARROW) / WOOD_TEX_W)
end
local function buildPanel()
if graveFrame then return end
local g = CreateFrame("Frame", "OctoReplayGraveFrame", UIParent)
g:SetWidth(BORDER_INSETS.left + STONE_WIDE + STONE_NARROW + BORDER_INSETS.right)
g:SetPoint("TOP", UIParent, "TOP", 0, -160)
g:SetFrameStrata("DIALOG")
g:EnableMouse(true)
g.stoneL = SlabPiece(g, STONE_TEX .. "TopLeft", STONE_WIDE, g, "TOPLEFT", BORDER_INSETS.left, -BORDER_INSETS.top)
g.stoneR = SlabPiece(g, STONE_TEX .. "TopRight", STONE_NARROW, g.stoneL, "TOPRIGHT", 0, 0)
g.wood = g:CreateTexture(nil, "BACKGROUND")
g.wood:SetTexture(WOOD_TEX)
g.wood:SetWidth(STONE_WIDE + STONE_NARROW)
g.wood:SetPoint("TOPLEFT", g, "TOPLEFT", BORDER_INSETS.left, -BORDER_INSETS.top)
g.wood:Hide()
local border = CreateFrame("Frame", nil, g, "BackdropTemplate")
border:SetAllPoints(g)
border:SetFrameLevel(g:GetFrameLevel() + 1)
border:SetBackdrop({ edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", edgeSize = 32, insets = BORDER_INSETS })
local x = CreateFrame("Button", nil, g, "UIPanelCloseButton")
x:SetPoint("TOPRIGHT", g, "TOPRIGHT", -3, -3)
x:SetFrameLevel(border:GetFrameLevel() + 1)
x:SetScript("OnClick", function() g:Hide() end)
g.close = x
table.insert(UISpecialFrames, "OctoReplayGraveFrame")
local function line(objfont, color, w)
local fs = g:CreateFontString(nil, "OVERLAY", objfont)
fs:SetPoint("TOP", g, "TOP", 0, -PAD_TOP); fs:SetWidth(w or 300); fs:SetJustifyH("CENTER")
fs:SetShadowOffset(1, -1)
local c = color or STONE_BODY
fs:SetTextColor(c[1], c[2], c[3])
return fs
end
g.title = line("GameFontNormalLarge", STONE_TITLE)
g.ident = line("GameFontNormal", STONE_SUB)
g.cause = line("GameFontNormal", STONE_BODY)
g.words = line("GameFontNormalSmall", STONE_BODY)
g.reason = line("GameFontNormalSmall", STONE_SUB); g.reason:Hide()
g.status = line("GameFontNormalSmall", STONE_SUB); g.status:Hide()
local watch = CreateFrame("Button", nil, g, "UIPanelButtonTemplate")
watch:SetWidth(120); watch:SetHeight(BTN_H)
watch:SetPoint("TOP", g, "TOP", 0, -PAD_TOP)
watch:SetText(L.HC_WATCH_DEATH)
watch:SetScript("OnClick", function()
if watchState or not graveFacts or graveFacts.watchCode ~= "ok" or UnitAffectingCombat("player") then return end
if not sendRequest("WATCH " .. graveFacts.graveGuid) then
status(L.HC_ERR_UNAVAIL)
return
end
watchState = "requested"
watchTimer = C_Timer.NewTimer(25, function()
if watchState == "requested" then finishWatch(L.HC_NO_START) end
end)
status(L.HC_REQUESTING)
watch:Hide()
end)
g.watch = watch
graveFrame = g
end
local function OctoReplayGrave_Render(facts)
local wood = GraveIsWood(facts)
if wood then
graveFrame.wood:Show(); graveFrame.stoneL:Hide(); graveFrame.stoneR:Hide()
else
graveFrame.wood:Hide(); graveFrame.stoneL:Show(); graveFrame.stoneR:Show()
end
graveFrame.title:SetText(string.format(L.HC_GRAVE_OF, facts.name or ""))
graveFrame.ident:SetText(string.format(L.HC_IDENT, facts.level or "?",
RaceName(facts.race), ClassNameOf(facts.class)))
local cause = GraveCauseLine(facts.causeKind, facts.attacker, facts.attackerLevel)
if cause ~= "" then graveFrame.cause:SetText(cause); graveFrame.cause:Show()
else graveFrame.cause:SetText(""); graveFrame.cause:Hide() end
if facts.words and facts.words ~= "" then
graveFrame.words:SetText(string.format(L.HC_WORDS, facts.words)); graveFrame.words:Show()
else
graveFrame.words:SetText(""); graveFrame.words:Hide()
end
OctoReplayGrave_Layout()
end
local function onMessage(body)
if body:sub(1, 2) == "W|" then
graveWords = body:sub(3)
elseif body:sub(1, 2) == "G|" then
-- The wire carries 64-bit IDs as text. Consume optional last words once per G record.
local words = graveWords
graveWords = nil
local raw, death, name, level, race, class, cause, attackerLevel, code, attacker =
body:match("^G|(%d+)|(%d+)|([^|]+)|(%d+)|(%d+)|(%d+)|(%d+)|(%d+)|([^|]*)|([^|]*)$")
if not raw or #raw > 20 or #death > 20 then return end
graveFacts = {graveGuid = raw, name = name, level = tonumber(level), race = tonumber(race),
class = tonumber(class), causeKind = tonumber(cause), attackerLevel = tonumber(attackerLevel),
watchCode = code, attacker = attacker, words = words}
buildPanel()
OctoReplayGrave_Render(graveFacts)
if not watchState then
graveFrame.status:Hide()
graveFrame.watch:Show()
updateWatch()
end
graveFrame:Show()
elseif body == "GW|start" and watchState == "requested" then
cancelWatch()
watchState = "playing"
graveFrame:Hide()
watchTimer = C_Timer.NewTimer(60, function()
if watchState == "playing" then finishWatch(GhostEndText("done")) end
end)
elseif watchState then
local code = body:match("^GW|end|([%a]+)$")
if code then
finishWatch(GhostEndText(code))
elseif watchState == "requested" then
local why = body:match("^ERR|([%a]+)$")
if why then finishWatch(refusal[why] or L.HC_ERR_UNAVAIL) end
end
end
end
local function selfSender(sender)
local name, realm = UnitFullName("player")
if issecretvalue and issecretvalue(name) then return false end
if not name or not sender then return false end
if sender == name then return true end
if issecretvalue and issecretvalue(realm) then return false end
if not realm or realm == "" then realm = GetNormalizedRealmName() end
return (not issecretvalue or not issecretvalue(realm)) and type(realm) == "string"
and realm ~= "" and sender == name .. "-" .. realm
end
local events = CreateFrame("Frame")
for _, event in ipairs({"PLAYER_LOGIN", "PLAYER_ENTERING_WORLD", "PLAYER_LEAVING_WORLD", "PLAYER_REGEN_DISABLED",
"PLAYER_REGEN_ENABLED", "CHAT_MSG_ADDON", "GOSSIP_SHOW"}) do
events:RegisterEvent(event)
end
local registered, announceOnEntry = false, true
events:SetScript("OnEvent", function(_, event, tag, payload, channel, sender)
if event == "PLAYER_LOGIN" then
local result = C_ChatInfo.RegisterAddonMessagePrefix(replyPrefix)
registered = result == true or result == 0 or (C_ChatInfo.IsAddonMessagePrefixRegistered and C_ChatInfo.IsAddonMessagePrefixRegistered(replyPrefix))
elseif event == "PLAYER_ENTERING_WORLD" then
-- The gateway handles grave requests on a self-whisper after world entry.
if registered and announceOnEntry and sendRequest("CAPS") then
announceOnEntry = false
end
elseif event == "PLAYER_LEAVING_WORLD" then
cancelWatch()
graveFacts, graveWords, fragments = nil, nil, nil
if graveFrame then graveFrame:Hide() end
elseif event == "GOSSIP_SHOW" then
local name = UnitName("npc")
if registered and (not issecretvalue or not issecretvalue(name)) and name
and (name == L.KEEPER_VIMES or name == L.KEEPER_ORTAK) and sendRequest("CAPS") then
announceOnEntry = false
end
elseif event ~= "CHAT_MSG_ADDON" then
if graveFrame and graveFacts and not watchState then updateWatch() end
elseif registered and (not issecretvalue or (not issecretvalue(tag) and not issecretvalue(payload)
and not issecretvalue(channel) and not issecretvalue(sender)))
and tag == replyPrefix and channel == "GUILD" and selfSender(sender) and type(payload) == "string" then
-- 70334 carries long service replies in 254-byte pieces with a leading continuation or end marker.
local marker = payload:byte(1)
if marker == 1 or marker == 2 then
local joined = (fragments or "") .. payload:sub(2)
if #joined > 4096 then fragments, graveWords = nil, nil; return end
if marker == 1 then fragments = joined; return end
if not fragments then return end
payload = joined
end
fragments = nil
onMessage(payload)
end
end)