Files

361 lines
18 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Equipped weapon statistics and skill comparisons for the Camelot paper doll.
-- The original damage tooltips retain their same-level and higher-level PvE breakdown.
WoWHybridUI = WoWHybridUI or {};
local state = WoWHybridUI;
local info = state.weaponSkill or {};
state.weaponSkill = info;
local function PublicNumber(value)
if issecretvalue and issecretvalue(value) then return false; end
return type(value) == "number" and value == value and math.abs(value) < math.huge;
end
local function Clamp(value, minimum, maximum)
return math.max(minimum, math.min(maximum, value));
end
local function SafeNumber(fn, ...)
if type(fn) ~= "function" then return nil; end
local ok, value = pcall(fn, ...);
if not ok or not PublicNumber(value) then return nil; end
return value;
end
-- Tortoise server, player attacker against a creature (PvE):
-- Unit::MeleeMissChanceCalc / WorldObject::MeleeSpellMissChance (miss)
-- Unit::RollMeleeOutcomeAgainst (dodge, parry, block, glancing)
-- Unit::GetUnitCriticalChance (crit change)
-- Unit::CalculateMeleeDamage, MELEE_HIT_GLANCING (glancing damage range)
-- Creature defense is its level * 5; base creature dodge/parry/block is 5%.
-- options: ranged, dualWield, wandUser (booleans), hit (percent or nil).
function info.Comparison(level, skill, enemyLevel, options)
options = options or {};
if not (PublicNumber(level) and PublicNumber(skill) and PublicNumber(enemyLevel)) then return nil; end
if level < 1 or level > 60 or enemyLevel < 1 or skill < 0 then return nil; end
local defense, maximum = enemyLevel * 5, level * 5;
local difference = skill - defense;
local capped = math.min(maximum, skill) - defense;
local lowLevel = enemyLevel < 10 and enemyLevel / 10 or 1;
local hitKnown = PublicNumber(options.hit);
local hit = hitKnown and options.hit or 0;
-- The first 1% of hit is ignored when defense exceeds weapon skill by more than 10.
if difference < -10 and hit > 0 then hit = hit - 1; end
local base = 5 - difference * (difference < -10 and 0.2 or 0.1);
local result = {
hitKnown = hitKnown,
miss = Clamp(base * lowLevel - hit, 0, 60),
critChange = difference > 0 and difference * 0.04 or capped * 0.2,
};
if options.ranged then return result; end -- ranged cannot be dodged, parried or glance
if options.dualWield then result.dualMiss = Clamp((base + 19) * lowLevel - hit, 0, 60); end
result.dodge = math.max(0, 5 - difference * 0.1) * lowLevel;
result.parry = math.max(0, 5 - capped * (capped < -10 and 0.6 or 0.2)) * lowLevel;
result.block = Clamp(5 - difference * 0.1, 0, 5) * lowLevel;
-- Skill above level * 5 does not lower the glancing frequency, only its damage loss.
result.glancing = Clamp(10 + (defense - math.min(skill, maximum)) * 2, 0, 40);
local gap = defense - skill;
local low, high, lowCap = 1.3 - 0.05 * gap, 1.2 - 0.03 * gap, 0.91;
if options.wandUser then low, high, lowCap = low - 0.7, high - 0.3, 0.6; end
result.glancingLow = Clamp(low, 0.01, lowCap) * 100;
result.glancingHigh = Clamp(high, 0.2, 0.99) * 100;
return result;
end
-- Item weapon subclass -> skill line (Tortoise Item.cpp item_weapon_skills;
-- same table as the original paperdollframestats.lua). Fist weapons use Unarmed.
local SUBCLASS_SKILL = {[0] = 44, [1] = 172, [2] = 45, [3] = 46, [4] = 54, [5] = 160,
[6] = 229, [7] = 43, [8] = 55, [10] = 136, [13] = 162, [15] = 173, [16] = 176,
[18] = 226, [19] = 228};
local UNARMED_SKILL, FERAL_SKILL, WEAPON_CLASS = 162, 3014, 2;
local FERAL_FORMS = {[1] = true, [5] = true, [8] = true}; -- cat, bear, dire bear
local WAND_CLASSES = {PRIEST = true, MAGE = true, WARLOCK = true};
local MAINHAND, OFFHAND, RANGED = 16, 17, 18;
local function SkillLine(id)
if not C_SkillInfo or type(C_SkillInfo.GetSkillLineInfoByID) ~= "function" then return nil; end
local ok, line = pcall(C_SkillInfo.GetSkillLineInfoByID, id);
if not ok or (issecretvalue and issecretvalue(line)) or type(line) ~= "table" then return nil; end
if not PublicNumber(line.rank) then return nil; end
if issecretvalue and issecretvalue(line.modifier) then return nil; end
if line.modifier ~= nil and not PublicNumber(line.modifier) then return nil; end
local modifier = line.modifier or 0;
local name = line.name;
if issecretvalue and issecretvalue(name) then name = nil; end
return math.max(0, line.rank + modifier), type(name) == "string" and name or nil;
end
local function PlayerClass()
if type(UnitClass) ~= "function" then return nil; end
local _, class = UnitClass("player");
if issecretvalue and issecretvalue(class) then return nil; end
return class;
end
local function InFeralForm()
if PlayerClass() ~= "DRUID" or type(GetShapeshiftFormID) ~= "function" then return false; end
local ok, form = pcall(GetShapeshiftFormID);
return ok and PublicNumber(form) and FERAL_FORMS[form] == true;
end
-- Returns skill value, skill name; nil when the slot has no usable weapon skill.
local function SlotSkill(slot, level)
if slot == MAINHAND and InFeralForm() then
-- The server always uses level * 5 in cat and bear forms.
local _, name = SkillLine(FERAL_SKILL);
return level * 5, name or "Feral Combat";
end
if type(GetInventoryItemID) ~= "function" then return nil; end
local id = GetInventoryItemID("player", slot);
if issecretvalue and issecretvalue(id) then return nil; end
local broken = false;
if id then
if type(GetInventoryItemDurability) ~= "function" then return nil; end
local durability, maximum = GetInventoryItemDurability(slot);
if issecretvalue and (issecretvalue(durability) or issecretvalue(maximum)) then return nil; end
broken = PublicNumber(durability) and PublicNumber(maximum) and maximum > 0 and durability == 0;
end
local skillID;
if not id or broken then
if slot ~= MAINHAND then return nil; end -- unarmed applies to the main attack only
skillID = UNARMED_SKILL;
else
if not C_Item or type(C_Item.GetItemInfoInstant) ~= "function" then return nil; end
local _, _, _, _, _, classID, subclassID = C_Item.GetItemInfoInstant(id);
if issecretvalue and (issecretvalue(classID) or issecretvalue(subclassID)) then return nil; end
if classID ~= WEAPON_CLASS then return nil; end
skillID = SUBCLASS_SKILL[subclassID];
end
if not skillID then return nil; end
return SkillLine(skillID);
end
local function HitBonus(ranged)
-- The original Camelot calculation has a separate ranged modifier.
local index, readModifier = CR_HIT_MELEE, GetHitModifier;
if ranged then index, readModifier = CR_HIT_RANGED, GetRangedHitModifier; end
if not PublicNumber(index) then return nil; end
local rating, modifier = SafeNumber(GetCombatRatingBonus,index), SafeNumber(readModifier);
if rating and modifier then return rating + modifier; end
end
local function Percent(value) return string.format("%.2f%%", value); end
local function ReadCrit(ranged)
if ranged then return SafeNumber(GetRangedCritChance); end
return SafeNumber(GetCritChance);
end
local function AddBlock(label, values, crit)
GameTooltip:AddLine(" ");
GameTooltip:AddLine(label, 1, 0.82, 0);
if values.dualMiss then
GameTooltip:AddDoubleLine("Miss (special attacks)", Percent(values.miss));
GameTooltip:AddDoubleLine("Miss (dual-wield auto-attack)", Percent(values.dualMiss));
else
GameTooltip:AddDoubleLine("Miss", Percent(values.miss));
end
if values.dodge then
GameTooltip:AddDoubleLine("Enemy dodge", Percent(values.dodge));
GameTooltip:AddDoubleLine("Enemy parry (from the front)", Percent(values.parry));
GameTooltip:AddDoubleLine("Enemy block (from the front)", Percent(values.block));
GameTooltip:AddDoubleLine("Glancing blow (auto-attack)", Percent(values.glancing));
if values.glancing > 0 then
GameTooltip:AddDoubleLine("Glancing blow damage",
string.format("%.0f%% - %.0f%%", values.glancingLow, values.glancingHigh));
end
end
if crit then
GameTooltip:AddDoubleLine("Critical strike",
string.format("%.2f%% (%+.2f%%)", math.max(0, crit + values.critChange), values.critChange));
else
GameTooltip:AddDoubleLine("Critical strike change", string.format("%+.2f%%", values.critChange));
end
end
local function WeaponTooltip(frame)
if not frame or frame.unit ~= "player" then return; end
local level = UnitLevel("player");
if not PublicNumber(level) then return; end
local slot = MAINHAND;
local label = frame.tooltipLabel;
if label ~= nil and label == INVTYPE_WEAPONOFFHAND then slot = OFFHAND;
elseif label ~= nil and label == INVTYPE_RANGED then slot = RANGED; end
local skill, name = SlotSkill(slot, level);
if not skill then return; end
local ranged = slot == RANGED;
local options = {ranged = ranged, hit = HitBonus(ranged), wandUser = WAND_CLASSES[PlayerClass()] == true};
if not ranged and type(IsDualWielding) == "function" then
local ok, dual = pcall(IsDualWielding);
options.dualWield = ok and not (issecretvalue and issecretvalue(dual)) and dual == true;
end
local same = info.Comparison(level, skill, level, options);
local higher = info.Comparison(level, skill, level + 3, options);
if not same or not higher then return; end
local crit = ReadCrit(ranged);
AddBlock(string.format("Same-level enemy (level %d)", level), same, crit);
AddBlock(string.format(level == 60 and "Boss baseline (level %d)" or "Higher-level enemy (level %d)", level + 3), higher, crit);
local hitText = (same.hitKnown and options.hit ~= 0)
and string.format("Includes your %+.1f%% hit bonus reported by the client.", options.hit)
or "Without hit bonuses from gear, talents or effects.";
GameTooltip:AddLine(string.format("PvE estimates from %s skill %d against a standing creature of that level. %s Target effects and encounter rules can change these values.",
name or "weapon", skill, hitText), 1, 1, 1, true);
GameTooltip:Show();
end
local function EquippedWeapon(slot)
if type(GetInventoryItemID) ~= "function" then return end
local id = GetInventoryItemID("player", slot);
if not PublicNumber(id) or not C_Item or type(C_Item.GetItemInfoInstant) ~= "function" then return end
local _, _, _, _, _, class = C_Item.GetItemInfoInstant(id);
if not PublicNumber(class) or class ~= WEAPON_CLASS then return end
return id;
end
local function ReadWeapon(slot)
local id = EquippedWeapon(slot);
if not id then return end
local name = C_Item.GetItemNameByID and C_Item.GetItemNameByID(id);
if (issecretvalue and issecretvalue(name)) or type(name) ~= "string" or name == "" then
local link=GetInventoryItemLink and GetInventoryItemLink("player",slot);
name=nil;
if not (issecretvalue and issecretvalue(link)) and type(link)=="string" then name=link:match("|h%[(.-)%]|h"); end
end
return name or string.format("Item %d", id);
end
local function ReadSkill(slot)
local level = SafeNumber(UnitLevel,"player");
if not level then return end
local skill, name = SlotSkill(slot,level);
if skill then return name and string.format("%s %d", name, skill) or tostring(skill); end
end
local function ReadHit(slot)
local level = SafeNumber(UnitLevel,"player");
if not level then return end
local skill = SlotSkill(slot,level);
if not skill then return end
local ranged = slot == RANGED;
local bonus = HitBonus(ranged);
local dual = false;
if not ranged then
dual = IsDualWielding and IsDualWielding();
if (issecretvalue and issecretvalue(dual)) or type(dual) ~= "boolean" then return end
end
local compare = info.Comparison(level,skill,level,{hit=bonus,dualWield=dual,ranged=ranged});
if not compare then return end
local chance = 100 - (compare.dualMiss or compare.miss);
local detail = string.format("Auto-attacks against a standing level %d creature with %d defense. Dodge, parry and block are resolved separately.", level, level*5);
if compare.dualMiss then detail = detail .. " Special attacks pass the miss roll " .. Percent(100-compare.miss) .. " of the time."; end
if bonus and bonus ~= 0 then
detail = detail .. string.format(" Includes the client-reported %+.2f%% hit modifier. Other server-side modifiers may apply.", bonus);
return Percent(chance) .. " est.", detail;
end
return Percent(chance) .. " base", detail .. " Skill-only baseline. The client has no confirmed total for hit bonuses.";
end
local function ReadHaste(slot)
local id = EquippedWeapon(slot);
if slot == RANGED and not id then return end
local profile = GatewaySourceWeaponDamageData;
local row = id and type(profile) == "table" and profile.primary and profile.primary[id];
local effective;
if slot == RANGED then effective=SafeNumber(UnitRangedDamage,"player");
else effective=SafeNumber(UnitAttackSpeed,"player"); end
if row and PublicNumber(row.delayMS) and row.delayMS > 0 and effective and effective > 0 and
type(GetInventoryItemDurability) == "function" and not InFeralForm() then
local durability, maximum = GetInventoryItemDurability(slot);
if not (issecretvalue and (issecretvalue(durability) or issecretvalue(maximum))) and
not (PublicNumber(maximum) and maximum > 0 and durability == 0) then
local percent = (row.delayMS/(1000*effective)-1)*100;
return Percent(percent), string.format("Net haste from the equipped weapon's %.3f s base delay and the client's observed %.3f s attack time. Includes attack-speed penalties and reflects the last supplied attack time.",row.delayMS/1000,effective);
end
end
local haste;
if slot == RANGED then
if type(GetRangedHaste) == "function" then
local ok, base, ammo = pcall(GetRangedHaste);
if ok and PublicNumber(base) and PublicNumber(ammo) then haste = base + ammo; end
end
else haste = SafeNumber(GetMeleeHaste); end
-- The gateway's default multiplier is 1 when Source supplies only final
-- attack time. Its resulting API zero alone cannot verify zero haste.
if haste and haste ~= 0 then return Percent(haste),"Haste reported by the client."; end
end
-- CharacterStatsPane's value string is right anchored without a width. A
-- weapon name can therefore extend through its label and adjacent rows.
-- Measure in the actual stat font and keep the full name in the tooltip.
local function FitWeaponName(frame, name)
local value, label = frame.Value, frame.Label;
if not value or not label or type(frame.GetWidth) ~= "function" or
type(label.GetStringWidth) ~= "function" or type(value.SetText) ~= "function" or
type(value.GetStringWidth) ~= "function" then return name; end
local width, labelWidth = frame:GetWidth(), label:GetStringWidth();
if not PublicNumber(width) or not PublicNumber(labelWidth) then return name; end
-- Camelot's row is 187 px wide: label starts 11 px from the left,
-- value ends 8 px from the right. Keep another 8 px between them.
local room = width - labelWidth - 27;
if type(label.GetRight) == "function" and type(value.GetRight) == "function" then
local labelRight, valueRight = label:GetRight(), value:GetRight();
if PublicNumber(labelRight) and PublicNumber(valueRight) then
room = valueRight - labelRight - 8;
end
end
value:SetText(name);
local measured = value:GetStringWidth();
if not PublicNumber(measured) or measured <= room then return name; end
for length = #name, 0, -1 do
-- Avoid cutting in the middle of a UTF-8 item name.
local endByte = length;
while endByte > 0 do
local nextByte = name:byte(endByte + 1);
if not nextByte or nextByte < 128 or nextByte >= 192 then break; end
endByte = endByte - 1;
end
local short = name:sub(1, endByte) .. "...";
value:SetText(short);
measured = value:GetStringWidth();
if PublicNumber(measured) and measured <= room then return short; end
end
return "";
end
info.FitWeaponName = FitWeaponName;
-- Standalone read-only rows. Native PAPERDOLL_STATINFO and category tables
-- must not contain addon callbacks: native OnShow traverses them synchronously.
function info.GetStandaloneRows()
if info.CharacterRows then return info.CharacterRows; end
local rows = {};
local function Add(key,label,read,weaponName)
table.insert(rows,{key=key,label=label,read=read,weaponName=weaponName});
end
local function AddSlot(key,label,slot)
Add(key,label,function() return ReadWeapon(slot); end,true);
Add(key.."_SKILL",label.." skill",function() return ReadSkill(slot); end);
end
AddSlot("HYBRID_MAINHAND","Main hand",MAINHAND);
AddSlot("HYBRID_OFFHAND","Off hand",OFFHAND);
AddSlot("HYBRID_RANGEDWEAPON","Ranged weapon",RANGED);
Add("HYBRID_MELEEHIT","Melee hit",function() return ReadHit(MAINHAND); end);
Add("HYBRID_OFFHANDHIT","Off-hand hit",function() return ReadHit(OFFHAND); end);
Add("HYBRID_RANGEDHIT","Ranged hit",function() return ReadHit(RANGED); end);
Add("HYBRID_MELEECRIT","Melee crit",function()
local value=ReadCrit(false); if value then return Percent(value),"Melee critical strike chance reported by the client."; end
end);
Add("HYBRID_RANGEDCRIT","Ranged crit",function()
if not EquippedWeapon(RANGED) then return end
local value=ReadCrit(true); if value then return Percent(value),"Ranged critical strike chance reported by the client."; end
end);
Add("HYBRID_MELEEHASTE","Melee haste",function() return ReadHaste(MAINHAND); end);
Add("HYBRID_RANGEDHASTE","Ranged haste",function() return ReadHaste(RANGED); end);
info.CharacterRows=rows;
return rows;
end
local function Install()
if info.installed then return true; end
if type(CharacterDamageFrame_OnEnter) ~= "function" then return false; end
hooksecurefunc("CharacterDamageFrame_OnEnter", WeaponTooltip);
info.installed = true;
return true;
end
local events = CreateFrame("Frame");
events:RegisterEvent("ADDON_LOADED");
events:SetScript("OnEvent", function() if Install() then events:UnregisterEvent("ADDON_LOADED"); end end);
Install();