396 lines
22 KiB
Lua
396 lines
22 KiB
Lua
-- Pure Turtle data/state model. No C_Traits mutation and no optimistic server state.
|
|
GatewayTurtleTalentProvider={}
|
|
local P=GatewayTurtleTalentProvider
|
|
P.__index=P
|
|
function P.New(data)
|
|
local self=setmetatable({data=data,byID={},tabs={},staged={},listeners={},spellRecords={},talentsByTab={},talentsByClass={},childrenByPrerequisite={}},P)
|
|
for id,r in pairs(data.tooltipSpells or {})do self.spellRecords[id]=r end
|
|
for _,t in ipairs(data.talents) do
|
|
self.byID[t.id]=t
|
|
if t.tab then
|
|
local rows=self.talentsByTab[t.tab]or {};self.talentsByTab[t.tab]=rows;rows[#rows+1]=t
|
|
end
|
|
if t.prerequisite and t.prerequisite~=0 then
|
|
local children=self.childrenByPrerequisite[t.prerequisite] or {}
|
|
self.childrenByPrerequisite[t.prerequisite]=children
|
|
children[#children+1]=t
|
|
end
|
|
for _,r in ipairs(t.ranks)do self.spellRecords[r.id]=r end
|
|
end
|
|
for _,t in ipairs(data.tabs) do self.tabs[t.id]=t end
|
|
-- Catalogue indexes only: character knowledge and staged ranks stay live.
|
|
-- Preserve source order and all rows; no per-character result is cached.
|
|
for class=1,11 do
|
|
local rows={};self.talentsByClass[class]=rows
|
|
for _,t in ipairs(data.talents)do
|
|
local tab=self.tabs[t.tab]
|
|
if tab and math.floor(tab.classMask/2^(class-1))%2==1 then rows[#rows+1]=t end
|
|
end
|
|
end
|
|
return self
|
|
end
|
|
function P:ActiveContext(isInspecting)
|
|
-- This is a source snapshot, not a fabricated C_Traits configuration.
|
|
-- It belongs to the authenticated player only. Missing inspected data is
|
|
-- unavailable, never this snapshot or an invented zero-rank tree.
|
|
if isInspecting then return nil end
|
|
return self.state
|
|
end
|
|
function P:Clear()
|
|
self.state=nil;self.receiving=nil;self.staged={};self.pending=nil;self.resetOffer=nil;self.resyncSeq=nil
|
|
self:Changed()
|
|
end
|
|
local function knownTalentRank(self,id,known)
|
|
local t=self.byID[id]
|
|
if t and known then
|
|
for i=#t.ranks,1,-1 do
|
|
if self:KnowsRank(t.ranks[i].id,known)then return i end
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
function P:Rank(id,includeStaged)
|
|
-- Read-only preview (OctoSpecProfiles: a saved Device spec that is not active) is drawn by the
|
|
-- native tree itself; nothing can be staged or applied while it is shown.
|
|
if self.preview then return self.preview[id] or 0 end
|
|
return includeStaged and self.staged[id] or knownTalentRank(self,id,self.state and self.state.known)
|
|
end
|
|
function P:SetPreview(ranks)
|
|
if ranks==self.preview then return end
|
|
self.preview=ranks;if ranks then self.staged={} end;self:Changed()
|
|
end
|
|
function P:KnowsRank(spell,known)
|
|
if known[spell] then return true end
|
|
for _,higher in ipairs(self.data.rankSuccessors and self.data.rankSuccessors[spell] or {}) do
|
|
if known[higher] then return true end
|
|
end
|
|
return false
|
|
end
|
|
local function finite(value)
|
|
return type(value)=='number'and(not issecretvalue or not issecretvalue(value))and value==value and value~=math.huge and value~=-math.huge
|
|
end
|
|
local function integer(value,minimum,maximum)
|
|
return finite(value)and value==math.floor(value)and value>=minimum and value<=maximum
|
|
end
|
|
local function trunc(value)return value<0 and math.ceil(value)or math.floor(value)end
|
|
local function sourceFloat(value)
|
|
-- Source EffectRealPointsPerLevel/DicePerLevel are float, not double.
|
|
-- Round the arithmetic to binary32 before C++'s signed truncation.
|
|
if not finite(value)then return nil end
|
|
if value==0 then return 0 end
|
|
if not math.frexp or not math.ldexp then return nil end
|
|
local sign=value<0 and -1 or 1;value=math.abs(value)
|
|
local _,exponent=math.frexp(value);local quantum=math.max(exponent-24,-149)
|
|
local scaled=math.ldexp(value,-quantum);local whole=math.floor(scaled);local remainder=scaled-whole
|
|
if remainder>0.5 or(remainder==0.5 and whole%2==1)then whole=whole+1 end
|
|
return sign*math.ldexp(whole,quantum)
|
|
end
|
|
local function sourceBounds(target,effect,context)
|
|
local base,sides=effect.basePoints,effect.dieSides
|
|
if sides==nil then sides=1 end
|
|
if not integer(base,-2147483648,2147483647)or not integer(sides,-2147483648,2147483647)then return nil end
|
|
local points,dice=effect.pointsPerLevel or 0,effect.dicePerLevel or 0
|
|
if not finite(points)or not finite(dice)then return nil end
|
|
points,dice=sourceFloat(points),sourceFloat(dice)
|
|
if not points or not dice then return nil end
|
|
local baseDice=effect.baseDice
|
|
if baseDice==nil then
|
|
-- Compatibility for old pinned records; scaled records require their
|
|
-- actual Source BaseDice and level fields rather than invented data.
|
|
if points~=0 or dice~=0 or sides<0 then return nil end
|
|
baseDice=sides>1 and 1 or sides
|
|
end
|
|
if not integer(baseDice,-2147483648,2147483647)then return nil end
|
|
local delta=0
|
|
if points~=0 or dice~=0 then
|
|
local level=context and context.level
|
|
if context==nil and type(UnitLevel)=='function'then local ok,value=pcall(UnitLevel,'player');if ok then level=value end end
|
|
if not integer(level,1,255)or not integer(target.maxLevel,0,255)or not integer(target.baseLevel,0,255)or not integer(target.spellLevel,0,255)then return nil end
|
|
-- Ordered Source Object.cpp clamp: do not independently floor after
|
|
-- the maximum branch. The resulting delta may be negative.
|
|
if target.maxLevel>0 and level>target.maxLevel then level=target.maxLevel
|
|
elseif level<target.baseLevel then level=target.baseLevel end
|
|
delta=level-target.spellLevel
|
|
end
|
|
local increment=sourceFloat(delta*points);local randomProduct=sourceFloat(delta*dice)
|
|
if not increment or not randomProduct then return nil end
|
|
local nativeSides=sourceFloat(sides)
|
|
local random=nativeSides and sourceFloat(nativeSides+randomProduct)
|
|
if not random or not integer(trunc(increment),-2147483648,2147483647)or not integer(trunc(random),-2147483648,2147483647)then return nil end
|
|
base=base+trunc(increment);random=trunc(random)
|
|
if not integer(base,-2147483648,2147483647)then return nil end
|
|
local low,high=base+baseDice,base+baseDice
|
|
if random~=0 and random~=1 then low=base+math.min(baseDice,random);high=base+math.max(baseDice,random)end
|
|
if not integer(low,-2147483648,2147483647)or not integer(high,-2147483648,2147483647)then return nil end
|
|
-- The saved Source evaluator establishes absolute scalar display. Its
|
|
-- negative random-range display order is still not qualified.
|
|
if low~=high and low<0 then return nil end
|
|
return math.abs(low),math.abs(high)
|
|
end
|
|
local function lastNumber(fragment,previous)
|
|
local cursor=1
|
|
while true do
|
|
local first,last=fragment:find('[+-]?%d*%.?%d+',cursor)
|
|
if not first then return previous end
|
|
local exponent=fragment:sub(last+1):match('^[eE][+-]?%d+')
|
|
if exponent then last=last+#exponent end
|
|
local value=tonumber(fragment:sub(first,last));if finite(value)then previous=value end
|
|
cursor=last+1
|
|
end
|
|
end
|
|
function P.FormatDescription(record,references,context)
|
|
local text=type(record)=='table'and record.description
|
|
if type(text)~='string'then return '',{'missing-source-description'}end
|
|
if #text>8192 then return text,{'description-exceeds-format-bound'}end
|
|
local output,unresolved={},{};local cursor,count,previous=1,0,nil
|
|
while true do
|
|
local first=text:find('%$',cursor)
|
|
if not first then output[#output+1]=text:sub(cursor);break end
|
|
count=count+1;if count>256 then return text,{'token-count-exceeds-format-bound'}end
|
|
local literal=text:sub(cursor,first-1);output[#output+1]=literal;previous=lastNumber(literal,previous)
|
|
local tail=text:sub(first)
|
|
local token,singular,plural=tail:match('^(%$l([^:;$\r\n]*):([^;$\r\n]*);)')
|
|
local replacement
|
|
if token then
|
|
if previous~=nil then replacement=previous==1 and singular or plural end
|
|
else
|
|
local divisor,source,kind,index,operation
|
|
token,operation,divisor,source,kind,index=tail:match('^(%$([*/])([0-9]+);([0-9]*)([sSmMo])([0-9]+))')
|
|
if not token then token,source,kind,index=tail:match('^(%$([0-9]*)([sSmMtoaAqQb])([0-9]+))');divisor='1';operation='/'end
|
|
-- Octo uses the indexed proc-chance alias only before a literal percent sign.
|
|
if not token then token,source,kind,index=tail:match('^(%$([0-9]*)([hH])(1))%%')end
|
|
if not token then token,source,kind,index=tail:match('^(%$([0-9]*)([iI])(1))')end
|
|
if not token then token,source,kind=tail:match('^(%$([0-9]*)([hdnuUiIv]))')end
|
|
if token and tail:sub(#token+1,#token+1):match('[%w_]')then token=nil end
|
|
if token then
|
|
local target=record
|
|
if source~=''then
|
|
local id=tonumber(source)
|
|
target=id and id>0 and id<=2147483647 and tostring(id)==source and type(references)=='table'and references[id]or nil
|
|
if type(target)~='table'or target.id~=id then target=nil end
|
|
end
|
|
if type(target)~='table'then target={}end
|
|
if kind=='u'or kind=='U'or kind=='i'or kind=='I'or kind=='v'then
|
|
local value=target.maxTargets
|
|
if kind=='u'or kind=='U'then value=target.stackAmount end
|
|
if kind=='v'then value=target.maxTargetLevel end
|
|
if integer(value,0,2147483647)then replacement=string.format('%.0f',value)end
|
|
elseif kind=='h'or kind=='H'then
|
|
if integer(target.procChance,0,100)then replacement=string.format('%.0f',target.procChance)end
|
|
elseif kind=='n'then
|
|
if integer(target.procCharges,0,255)then replacement=string.format('%.0f',target.procCharges)end
|
|
elseif kind=='d'then
|
|
if integer(target.durationMs,1,2147483647)then replacement=string.format('%.15g sec',target.durationMs/1000)end
|
|
elseif index then
|
|
local i,n=tonumber(index),tonumber(divisor)
|
|
local effect=#index==1 and i and i>=1 and i<=3 and type(target.effects)=='table'and target.effects[i]
|
|
if type(effect)=='table'then
|
|
if kind=='a'or kind=='A'then
|
|
if finite(effect.radius)and effect.radius>=0 and effect.radius<=2147483647 then replacement=string.format('%.15g',effect.radius)end
|
|
elseif kind=='q'or kind=='Q'then
|
|
if integer(effect.misc,-2147483648,2147483647)then replacement=string.format('%.0f',effect.misc)end
|
|
elseif kind=='b'then
|
|
-- Definition coefficient only, never actual combo
|
|
-- points or a prediction of combat damage.
|
|
if finite(effect.pointsPerComboPoint)and math.abs(effect.pointsPerComboPoint)<=2147483647 then replacement=string.format('%.15g',effect.pointsPerComboPoint)end
|
|
elseif kind=='t'then
|
|
if integer(effect.amplitudeMs,1,2147483647)then replacement=string.format('%.15g',effect.amplitudeMs/1000)end
|
|
elseif integer(n,1,2147483647)then
|
|
local low,high=sourceBounds(target,effect,context);local periodic=1
|
|
if kind=='o'then
|
|
if integer(target.durationMs,1,2147483647)and integer(effect.amplitudeMs,1,2147483647)and target.durationMs%effect.amplitudeMs==0 then periodic=target.durationMs/effect.amplitudeMs else periodic=nil end
|
|
end
|
|
if low and periodic and math.max(low,high)*periodic*(operation=='*'and n or 1)<=9007199254740991 then
|
|
local function number(value)value=(operation=='*'and value*n or value/n)*periodic;return string.format(value==math.floor(value)and'%.0f'or'%.15g',value)end
|
|
if kind=='m'then replacement=number(low)
|
|
elseif kind=='M'then replacement=number(high)
|
|
elseif low==high then replacement=number(low)
|
|
else replacement=number(low)..' to '..number(high)end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if not token then token=tail:match('^%$[^%s$]+')or(tail:sub(1,2)=='$$'and '$$'or '$')end
|
|
output[#output+1]=replacement or token
|
|
if replacement then previous=lastNumber(replacement,previous)else unresolved[#unresolved+1]=token;previous=nil end
|
|
cursor=first+#token
|
|
end
|
|
return table.concat(output),unresolved
|
|
end
|
|
function P:TooltipInfo(id)
|
|
local t=self.byID[id];if not t then return nil end
|
|
local rank=self:Rank(id,true)
|
|
local record=t.ranks[math.max(1,rank)];local talentSpell=record.id
|
|
-- A one-point talent may unlock an ability subsequently trained to higher
|
|
-- ranks. Display that genuine server-known rank, without changing the
|
|
-- talent point rank, purchase identity, or known-spell snapshot.
|
|
if rank>0 and self.state then
|
|
for _,higher in ipairs(self.data.rankSuccessors and self.data.rankSuccessors[record.id]or {})do
|
|
if self.state.known[higher]and self.spellRecords[higher]then record=self.spellRecords[higher]end
|
|
end
|
|
end
|
|
return {rank=rank,committedRank=self:Rank(id,false),maxRanks=#t.ranks,
|
|
trainedAbilityRank=record.id~=talentSpell and record.rank or nil,
|
|
record=record,nextRecord=rank>0 and t.ranks[rank+1] or nil}
|
|
end
|
|
function P:ClassTabs()
|
|
local out={}
|
|
if self.state then for _,t in ipairs(self.data.tabs) do if math.floor(t.classMask/2^(self.state.class-1))%2==1 then out[#out+1]=t end end end
|
|
table.sort(out,function(a,b)return a.order<b.order end);return out
|
|
end
|
|
function P:Spent(tab,staged)
|
|
if not self.state then return 0 end
|
|
local rows
|
|
if tab then
|
|
local definition=self.tabs[tab]
|
|
if not definition or math.floor(definition.classMask/2^(self.state.class-1))%2~=1 then return 0 end
|
|
rows=self.talentsByTab[tab]
|
|
else rows=self.talentsByClass[self.state.class] end
|
|
local n=0;for _,t in ipairs(rows or {})do n=n+self:Rank(t.id,staged)end
|
|
return n
|
|
end
|
|
local function stagedPointCost(self)
|
|
-- Only staged ranks differ from the acknowledged total. Read their live
|
|
-- deltas; unchanged ranks cancel without scanning every class tree twice.
|
|
local delta=0
|
|
for id,rank in pairs(self.staged)do
|
|
local t=self.byID[id];local tab=t and self.tabs[t.tab]
|
|
if rank and tab and math.floor(tab.classMask/2^(self.state.class-1))%2==1 then
|
|
delta=delta+rank-self:Rank(id,false)
|
|
end
|
|
end
|
|
return delta
|
|
end
|
|
function P:AvailablePoints()
|
|
if not self.state or self.preview then return 0 end
|
|
-- The server's point deduction and learned rank may arrive separately.
|
|
return math.max(0,self.state.points-stagedPointCost(self))
|
|
end
|
|
function P:PurchaseExpectation(id,targetRank)
|
|
local s=self.state;local t=self.byID[id];local current=self:Rank(id,false)
|
|
if not s or not t or type(targetRank)~='number' or targetRank~=math.floor(targetRank) or targetRank~=current+1 or targetRank>#t.ranks then return nil end
|
|
local cost=targetRank-current
|
|
local ranks={};for key in pairs(self.byID)do ranks[key]=self:Rank(key,false)end
|
|
if not self:CanPurchaseAt(id,ranks,s.known,s.points)then return nil end
|
|
return {id=id,rank=targetRank,priorRank=current,priorPoints=s.points,cost=cost,points=s.points-cost,
|
|
guid=s.guid,class=s.class,level=s.level,seq=s.seq}
|
|
end
|
|
function P:PurchaseAcknowledged(expected)
|
|
local s=self.state
|
|
return s~=nil and expected~=nil and s.guid==expected.guid and s.class==expected.class and s.level==expected.level
|
|
and s.seq>expected.seq and self:Rank(expected.id,false)==expected.rank and s.points==expected.points
|
|
end
|
|
function P:CanPurchaseAt(id,ranks,known,points)
|
|
local t=self.byID[id];local tab=t and self.tabs[t.tab]
|
|
if not t or not tab or not self.state or self.state.level<10 or points<1 then return false end
|
|
if math.floor(tab.classMask/2^(self.state.class-1))%2~=1 or (ranks[id]or 0)>=#t.ranks then return false end
|
|
local spent=0;for key,rank in pairs(ranks)do if self.byID[key].tab==t.tab then spent=spent+rank end end
|
|
if spent<t.row*5 then return false end
|
|
if t.prerequisite~=0 and (ranks[t.prerequisite]or 0)<t.prerequisiteRank+1 then return false end
|
|
return t.requiredSpell==0 or self:KnowsRank(t.requiredSpell,known)
|
|
end
|
|
function P:CanPurchase(id)
|
|
if self.preview or self.pending or self.resyncSeq or not self.state then return false end
|
|
local t=self.byID[id];if not t then return false end
|
|
-- CanPurchaseAt consumes only the target tab's total and its prerequisite.
|
|
-- Keep that same authority check without evaluating unrelated classes.
|
|
local ranks={};for _,row in ipairs(self.talentsByTab[t.tab]or {})do ranks[row.id]=self:Rank(row.id,true)end
|
|
if t.prerequisite~=0 and self.byID[t.prerequisite]and ranks[t.prerequisite]==nil then
|
|
ranks[t.prerequisite]=self:Rank(t.prerequisite,true)
|
|
end
|
|
return self:CanPurchaseAt(id,ranks,self.state.known,self:AvailablePoints())
|
|
end
|
|
function P:Stage(id)
|
|
if not self:CanPurchase(id) then return false end
|
|
self.staged[id]=self:Rank(id,true)+1;self:Changed();return true
|
|
end
|
|
function P:Undo()
|
|
if self.pending then return end
|
|
self.staged={};self:Changed()
|
|
end
|
|
function P:Changed()
|
|
for _,f in ipairs(self.listeners) do f(self) end
|
|
end
|
|
local function lostKnownSpell(old,known)
|
|
for spell in pairs(old)do
|
|
if not known[spell]then return true end
|
|
end
|
|
return false
|
|
end
|
|
function P:Receive(message)
|
|
if type(message)~='string' or #message>1024 then return false end
|
|
local seq,guid,class,level,points,count=message:match('^GWTURTLE1 BEGIN (%d+) (%d+) (%d+) (%d+) (%d+) (%d+)$')
|
|
if seq then
|
|
seq,guid,class,level,points,count=tonumber(seq),tonumber(guid),tonumber(class),tonumber(level),tonumber(points),tonumber(count)
|
|
self.receiving=nil
|
|
if seq>9007199254740991 or guid<1 or class<1 or class>11 or level<1 or level>60 or points>1000 or count>4096 or (self.state and seq<=self.state.seq) then return false end
|
|
self.receiving={seq=seq,guid=guid,class=class,level=level,points=points,count=count,known={}};return false
|
|
end
|
|
local s,ids=message:match('^GWTURTLE1 SPELLS (%d+) ([%d,]+)$')
|
|
if s and self.receiving and self.receiving.seq==tonumber(s) then
|
|
for token in ids:gmatch('%d+') do local id=tonumber(token);if id<1 or id>10000000 then self.receiving=nil;return false end;self.receiving.known[id]=true end
|
|
return false
|
|
end
|
|
local e=message:match('^GWTURTLE1 END (%d+)$')
|
|
if not e or not self.receiving or self.receiving.seq~=tonumber(e) then return false end
|
|
local n=0;for _ in pairs(self.receiving.known) do n=n+1 end
|
|
if n~=self.receiving.count then self.receiving=nil;return false end
|
|
local old=self.state;self.state=self.receiving;self.receiving=nil
|
|
local resynced=self.resyncSeq and self.state.seq>self.resyncSeq
|
|
if resynced then self.resyncSeq=nil end
|
|
if old and (old.guid~=self.state.guid or old.class~=self.state.class or old.level~=self.state.level) then self.staged={};self.pending=nil end
|
|
for id,target in pairs(self.staged)do
|
|
local rank=self:Rank(id,false)
|
|
-- Losing learned ranks would turn the selected target into extra purchases.
|
|
-- The in-flight request and its acknowledgement live separately in pending.
|
|
if rank>=target or (old and rank<knownTalentRank(self,id,old.known))then
|
|
self.staged[id]=nil
|
|
end
|
|
end
|
|
if not self.pending and next(self.staged)then
|
|
if stagedPointCost(self)>self.state.points then
|
|
self.staged={}
|
|
elseif resynced or (old and lostKnownSpell(old.known,self.state.known))then
|
|
-- Resetting or switching a spec can withdraw prerequisites or tier
|
|
-- points. Recheck unsent choices, including after a stopped Apply.
|
|
local reachable=select(3,self:Purchases())
|
|
if reachable then self.staged=reachable end
|
|
end
|
|
end
|
|
self:Changed();return true
|
|
end
|
|
function P:Purchases()
|
|
if self.preview then return nil,'read-only-preview' end
|
|
if not self.state or self.resyncSeq then return nil,'state-not-ready' end
|
|
local candidates,ranks,known={},{},{}
|
|
for id in pairs(self.state.known)do known[id]=true end
|
|
for id in pairs(self.byID)do ranks[id]=self:Rank(id,false)end
|
|
for id,target in pairs(self.staged)do
|
|
if not self.byID[id]or type(target)~='number'or target~=math.floor(target)or target<ranks[id]or target>#self.byID[id].ranks then return nil,'invalid-staged-rank' end
|
|
candidates[#candidates+1]={id=id,rank=target,row=self.byID[id].row}
|
|
end
|
|
table.sort(candidates,function(a,b)if a.row==b.row then return a.id<b.id end;return a.row<b.row end)
|
|
local out,points={},self.state.points
|
|
while true do
|
|
local remaining,chosen=false,nil
|
|
for _,t in ipairs(candidates)do
|
|
if ranks[t.id]<t.rank then
|
|
remaining=true
|
|
if self:CanPurchaseAt(t.id,ranks,known,points)then chosen=t;break end
|
|
end
|
|
end
|
|
if not remaining then return out end
|
|
if not chosen then
|
|
local reachable={}
|
|
for _,step in ipairs(out)do reachable[step.id]=step.rank end
|
|
return nil,'invalid-prerequisites-or-points',reachable
|
|
end
|
|
if #out>=1000 then return nil,'invalid-prerequisites-or-points' end
|
|
local id=chosen.id;ranks[id]=ranks[id]+1;points=points-1
|
|
known[self.byID[id].ranks[ranks[id]].id]=true
|
|
out[#out+1]={id=id,rank=ranks[id],row=chosen.row}
|
|
end
|
|
end
|