Files
pfUI_CombatLedgerExt/pfUI_CombatLedgerExt_Ring.lua

345 lines
11 KiB
Lua

--[[----------------------------------------------------------------------
pfUI_CombatLedgerExt - Aggro ring on hostile nameplates
Every hostile (red) nameplate gets a ring of dots around it as you approach its estimated aggro range:
- the ring starts filling clockwise from the top `ringwarn` yards (default 15) before the edge;
- each new dot is redder than the last (amber -> orange -> red) and the ring glows brighter as it fills;
- when it closes (you are inside the radius, or the mob is targeting you) it becomes a solid, slowly
pulsing red glow.
The plate's name is tinted along with it (amber -> red). If OctoPad is loaded, its soft-selected unit
keeps OctoPad's own name colour.
Aggro radius estimate (vanilla): about 20 yd at equal level, 1 yd more / less per level the mob is above /
below you, between 5 and 45 yd. Neutral (yellow) mobs, players and dead units are ignored.
Needs SuperWoW (each nameplate's GUID). Distance comes from UnitXP SP3 (UnitXP("distanceBetween", ...)).
Without UnitXP SP3 the game's coarse interact ranges (about 10 and 28 yd) are used instead.
Does not need CombatLedger data. Lua 5.0.
------------------------------------------------------------------------]]
CLpfUIThreatRing = {}
local M = CLpfUIThreatRing
local function Cfg()
local db = CLpfUIThreatDB
if not db then return nil end
if db.ring == nil then db.ring = true end
if db.ringwarn == nil then db.ringwarn = 15 end
return db
end
-- the visible name text of a plate: pfUI keeps it on p.nameplate.name (the Blizzard regions are hidden)
local function NameFS(p)
local np = p.nameplate
if np and np.name and np.name.GetText then return np.name end
local regs = { p:GetRegions() }
local i
for i = 1, table.getn(regs) do
if regs[i]:GetObjectType() == "FontString" then return regs[i] end
end
return nil
end
local function SoftNameFS()
if OctoPad and OctoPad.SoftNameFS then return OctoPad.SoftNameFS() end
return nil
end
local SEG = 36 -- dots around the plate
local DOT = "Interface\\AddOns\\pfUI_CombatLedgerExt\\ringdot"
local PAD = 6 -- ring distance from the plate edge
local rings = {} -- plate frame -> ring frame
local hasXP
local function HasUnitXP()
if hasXP == nil then
hasXP = (UnitXP ~= nil) and pcall(UnitXP, "nop", "nop") and true or false
end
return hasXP
end
M.HasUnitXP = HasUnitXP
-- yards between you and the unit; second value = exact (UnitXP) or coarse (interact ranges)
local function Distance(u)
if HasUnitXP() then
local ok, d = pcall(UnitXP, "distanceBetween", "player", u)
if ok and type(d) == "number" and d >= 0 then return d, true end
end
if CheckInteractDistance then
local ok3, near = pcall(CheckInteractDistance, u, 3) -- about 10 yd
if ok3 and near then return 9, false end
local ok4, mid = pcall(CheckInteractDistance, u, 4) -- about 28 yd
if ok4 and mid then return 27, false end
return 40, false
end
return nil
end
local function AggroRadius(u)
local ml = UnitLevel(u) or 0
local pl = UnitLevel("player") or 1
if ml <= 0 then return 45 end -- "??" level
local r = 20 + (ml - pl)
if r < 5 then r = 5 elseif r > 45 then r = 45 end
return r
end
local function TargetsMe(u)
local ok, yes = pcall(function()
return UnitExists(u .. "target") and UnitIsUnit(u .. "target", "player")
end)
return ok and yes
end
local function Eligible(u)
if not UnitExists(u) or UnitIsPlayer(u) then return false end
if UnitIsDead and UnitIsDead(u) then return false end
if not UnitCanAttack("player", u) then return false end
local re = UnitReaction and UnitReaction(u, "player")
if re and re > 2 then return false end -- neutral / friendly
return true
end
----------------------------------------------------------------------
-- ring frames
----------------------------------------------------------------------
-- points around a rounded rectangle (w x h, corner radius rad), clockwise from the top centre;
-- coordinates from the top-left corner, y pointing down
local function Perimeter(w, h, rad, n)
local pts = {}
local straightW, straightH = w - 2 * rad, h - 2 * rad
local arc = math.pi * rad / 2
local segs = {
{ "l", w / 2, 0, w - rad, 0 },
{ "a", w - rad, rad, -90, 0 },
{ "l", w, rad, w, h - rad },
{ "a", w - rad, h - rad, 0, 90 },
{ "l", w - rad, h, rad, h },
{ "a", rad, h - rad, 90, 180 },
{ "l", 0, h - rad, 0, rad },
{ "a", rad, rad, 180, 270 },
{ "l", rad, 0, w / 2, 0 },
}
local per = 2 * straightW + 2 * straightH + 4 * arc
local k
for k = 0, n - 1 do
local s = (k / n) * per
local x, y = 0, 0
local j
for j = 1, table.getn(segs) do
local g = segs[j]
local len
if g[1] == "l" then local dx, dy = g[4] - g[2], g[5] - g[3]
len = math.sqrt(dx * dx + dy * dy) else len = arc end
if s <= len or j == table.getn(segs) then
local t = 0
if len > 0 then t = s / len end
if g[1] == "l" then
x, y = g[2] + (g[4] - g[2]) * t, g[3] + (g[5] - g[3]) * t
else
local a = math.rad(g[4] + (g[5] - g[4]) * t)
x, y = g[2] + rad * math.cos(a), g[3] + rad * math.sin(a)
end
break
end
s = s - len
end
pts[k + 1] = { x, y }
end
return pts
end
local function VisualOf(p)
return p.nameplate or p -- pfUI draws its plate on p.nameplate
end
local function GetRing(p)
local r = rings[p]
if r then return r end
local vis = VisualOf(p)
r = CreateFrame("Frame", nil, vis)
r:SetAllPoints(vis)
r:SetFrameLevel(vis:GetFrameLevel() + 6)
r.dots = {}
local i
for i = 1, SEG do
local t = r:CreateTexture(nil, "OVERLAY")
t:SetTexture(DOT)
t:SetBlendMode("ADD")
r.dots[i] = t
end
r.vis = vis
rings[p] = r
return r
end
local function Layout(r)
local vis = r.vis
local w, h = vis:GetWidth() or 0, vis:GetHeight() or 0
if w < 20 or h < 6 then return false end
if r.w == w and r.h == h then return true end
r.w, r.h = w, h
local W, H = w + 2 * PAD, h + 2 * PAD
local rad = math.min(9, H / 2)
local pts = Perimeter(W, H, rad, SEG)
local i
for i = 1, SEG do
local d = r.dots[i]
d:ClearAllPoints()
d:SetPoint("CENTER", vis, "TOPLEFT", pts[i][1] - PAD, -(pts[i][2] - PAD))
end
return true
end
local pulsing = {}
local function Paint(r, frac, full)
local i
if full then
for i = 1, SEG do
local d = r.dots[i]
d:SetWidth(13)
d:SetHeight(13)
d:SetVertexColor(1, .24, .18)
d:Show()
end
pulsing[r] = true
return
end
pulsing[r] = nil
r:SetAlpha(1)
local lit = math.floor(frac * SEG + .5)
local size = 5 + 4 * frac
for i = 1, SEG do
local d = r.dots[i]
if i <= lit then
local t = (i - 1) / (SEG - 1) -- amber -> red along the ring
d:SetVertexColor(1, .71 - .47 * t, .23 - .05 * t, .55 + .45 * frac)
d:SetWidth(size)
d:SetHeight(size)
else
d:SetVertexColor(1, 1, 1, .12)
d:SetWidth(4)
d:SetHeight(4)
end
d:Show()
end
end
----------------------------------------------------------------------
-- name tint (restored when the ring goes away)
----------------------------------------------------------------------
local function TintName(r, fs, cr, cg, cb)
if not fs then return end
if SoftNameFS() == fs then return end -- OctoPad soft select owns it
if not r.tintFS or r.tintFS ~= fs then
local or_, og, ob = fs:GetTextColor()
r.tintFS, r.oR, r.oG, r.oB = fs, or_, og, ob
end
fs:SetTextColor(cr, cg, cb)
end
local function UntintName(r)
if r.tintFS then
if SoftNameFS() ~= r.tintFS then
pcall(r.tintFS.SetTextColor, r.tintFS, r.oR or 1, r.oG or 1, r.oB or 1)
end
r.tintFS = nil
end
end
local function HideRing(r)
pulsing[r] = nil
UntintName(r)
r:Hide()
end
----------------------------------------------------------------------
-- driver
----------------------------------------------------------------------
local function Tick()
local cfg = Cfg()
local seen = {}
if cfg and cfg.ring and SUPERWOW_VERSION then
local warn = cfg.ringwarn or 15
local kids = { WorldFrame:GetChildren() }
local i
for i = 1, table.getn(kids) do
local p = kids[i]
if p ~= UIParent and p:IsVisible() and not p:GetName() then
local okg, guid = pcall(p.GetName, p, 1)
if okg and type(guid) == "string" and guid ~= "" and Eligible(guid) then
local d = Distance(guid)
local rad = AggroRadius(guid)
local full = TargetsMe(guid) or (d and d <= rad)
local frac = 0
if d and not full then
frac = (rad + warn - d) / warn
if frac < 0 then frac = 0 elseif frac > 1 then frac = 1 end
end
if full or frac > 0 then
local r = GetRing(p)
if Layout(r) then
seen[r] = true
r:Show()
Paint(r, frac, full)
local fs = NameFS(p)
if fs then
if full then TintName(r, fs, 1, .29, .23)
else TintName(r, fs, 1, .71 - .35 * frac, .23) end
end
end
end
end
end
end
end
local p, r
for p, r in pairs(rings) do
if not seen[r] and r:IsShown() then HideRing(r) end
end
end
local drv = CreateFrame("Frame")
local acc = 0
drv:SetScript("OnUpdate", function()
if not CLpfUIThreatDB then return end
acc = acc + arg1
if acc >= 0.1 then
acc = 0
Tick()
end
-- slow pulse on closed rings
local a = .6 + .4 * math.sin(GetTime() * 3)
local r
for r in pairs(pulsing) do r:SetAlpha(a) end
end)
----------------------------------------------------------------------
-- /clpf ring ... (called from the main file's slash handler)
----------------------------------------------------------------------
function M.Slash(arg, Print)
local cfg = Cfg()
if not cfg then return end
local n = tonumber(arg or "")
if n then
if n < 3 then n = 3 elseif n > 40 then n = 40 end
cfg.ringwarn = math.floor(n)
Print("Aggro ring starts " .. cfg.ringwarn .. " yd before the edge")
return
end
if arg == "on" then cfg.ring = true elseif arg == "off" then cfg.ring = false else cfg.ring = not cfg.ring end
Print("Aggro ring " .. (cfg.ring and "on" or "off") ..
(HasUnitXP() and " (UnitXP SP3 distance)" or " (no UnitXP SP3: coarse 10 / 28 yd steps)") ..
(SUPERWOW_VERSION and "" or " |cffff6655needs SuperWoW|r"))
end
function M.Debug(Print)
local cfg = Cfg()
local n = 0
local p, r
for p, r in pairs(rings) do if r:IsShown() then n = n + 1 end end
Print("ring=" .. tostring(cfg and cfg.ring) .. " ringwarn=" .. tostring(cfg and cfg.ringwarn) ..
" SuperWoW=" .. tostring(SUPERWOW_VERSION ~= nil) .. " UnitXP=" .. tostring(HasUnitXP()) .. " ringsShown=" .. n)
end