Advertisement
Hekili

Untitled

Feb 25th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. do
  2.     -- New TTD, hopefully more aggressive and accurate than old TTD.
  3.     Hekili.TTD = Hekili.TTD or {}
  4.     local db = Hekili.TTD
  5.  
  6.     local recycle = {}
  7.  
  8.  
  9.     local function EliminateEnemy( guid )
  10.         local enemy = db[ guid ]
  11.         if not enemy then return end
  12.  
  13.         db[ guid ] = nil
  14.         twipe( enemy )
  15.         tinsert( recycle, enemy )
  16.     end
  17.  
  18.  
  19.     local function UpdateEnemy( guid, healthPct, time )
  20.         local enemy = db[ guid ]
  21.         time = time or GetTime()
  22.  
  23.         if not enemy then
  24.             -- This is the first time we've seen the enemy.
  25.             enemy = tremove( recycle, 1 ) or {}
  26.             db[ guid ] = enemy
  27.  
  28.             enemy.firstSeen = time
  29.             enemy.firstHealth = healthPct
  30.             enemy.lastSeen = time
  31.             enemy.lastHealth = healthPct
  32.  
  33.             enemy.rate = 0
  34.             enemy.n = 0
  35.  
  36.             return
  37.         end
  38.  
  39.         local difference = enemy.lastHealth - healthPct
  40.  
  41.         -- We don't recalculate the rate when enemies heal.
  42.         if difference > 0 then
  43.             local elapsed = time - enemy.lastSeen
  44.  
  45.             -- If this is our first health difference, just store it.    
  46.             if enemy.n == 0 then
  47.                 enemy.rate = difference / elapsed
  48.                 print( "initial rate is", enemy.rate )
  49.                 enemy.n = 1
  50.             else
  51.                 local newRate = enemy.rate * enemy.n + ( difference / elapsed )
  52.                 enemy.n = enemy.n + 1
  53.                 enemy.rate = newRate / enemy.n
  54.             end
  55.  
  56.             enemy.lastSeen = time
  57.         end
  58.  
  59.         enemy.lastHealth = healthPct
  60.     end
  61.  
  62.    
  63.     local DEFAULT_TTD = 15  
  64.     local INF = ( 1 / 0 )
  65.  
  66.  
  67.     function Hekili:GetTTD( unit )
  68.         local guid = UnitExists( unit ) and UnitCanAttack( "player", unit ) and UnitGUID( unit )
  69.         if not guid then return INF end
  70.  
  71.         local enemy = db[ guid ]
  72.         if not enemy then return INF end
  73.  
  74.         -- Don't have enough data to predict yet.
  75.         if enemy.rate == 0 then return INF, enemy.n end
  76.        
  77.         local health, healthMax = UnitHealth( unit ), UnitHealthMax( unit )
  78.         local healthPct = health / healthMax
  79.  
  80.         if healthPct == 0 then return 0, enemy.n end
  81.  
  82.         return ceil( healthPct / enemy.rate ), enemy.n
  83.     end
  84.  
  85.    
  86.     function Hekili:GetGreatestTTD()
  87.         local time = 0
  88.  
  89.         for k, v in pairs( db ) do
  90.             if v.n > 0 then time = max( time, ceil( v.lastHealth / v.rate ) ) end
  91.         end
  92.  
  93.         return time
  94.     end
  95.  
  96.  
  97.     function Hekili:ExpireTTDs( all )
  98.         local now = GetTime()
  99.  
  100.         for k, v in pairs( db ) do
  101.             if all or now - v.lastSeen > 10 then EliminateEnemy( k ) end
  102.         end
  103.     end
  104.  
  105.  
  106.     local trackedUnits = { "target", "boss1", "boss2", "boss3", "boss4", "boss5", "focus" }
  107.     local seen = {}
  108.  
  109.     local UpdateTTDs
  110.  
  111.  
  112.     UpdateTTDs = function()
  113.         twipe( seen )
  114.  
  115.         local now = GetTime()
  116.  
  117.         for i, unit in ipairs( trackedUnits ) do
  118.             local guid = UnitExists( unit ) and UnitThreatSituation( "player", unit ) and not UnitIsDead( unit ) and UnitGUID( unit )
  119.  
  120.             if guid then
  121.                 local health, healthMax = UnitHealth( unit ), UnitHealthMax( unit )
  122.                 UpdateEnemy( guid, health / healthMax, now )
  123.                 seen[ guid ] = true
  124.             end
  125.         end
  126.  
  127.         for unit, guid in pairs( npGUIDs ) do
  128.             if not seen[ guid ] and UnitThreatSituation( "player", unit ) then
  129.                 local health, healthMax = UnitHealth( unit ), UnitHealthMax( unit )
  130.                 UpdateEnemy( guid, health / healthMax, now )
  131.                 seen[ guid ] = true
  132.             end
  133.         end
  134.  
  135.         C_Timer.After( 0.25, UpdateTTDs )
  136.     end
  137.  
  138.  
  139.     C_Timer.After( 0.25, UpdateTTDs )
  140.  
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement