diff --git a/src/TimeToDie/TimeToDie.lua b/src/TimeToDie/TimeToDie.lua index a61f6d7..e00e786 100644 --- a/src/TimeToDie/TimeToDie.lua +++ b/src/TimeToDie/TimeToDie.lua @@ -20,9 +20,12 @@ local TimeToDie = { -- Max history count : min=20, max=500, default = 100 HistoryCount = 100 }, - Cache = {}, -- A cache of unused { time, value } tables to reduce garbage due to table creation - ---@type table - Units = {}, -- Used to track units, + ---@type table + Cache = {}, -- A cache of unused { time, value } tables to reduce garbage due to table creation + --@type table + --Units = {}, -- Used to track units, + ---@type table, time: number }> + Units = {}, ---@type table ExistingUnits = {}, -- Used to track GUIDs of currently existing units (to be compared with tracked units) Throttle = 0, @@ -69,33 +72,40 @@ function TimeToDie:Refresh() local healthPercentage = unit:GetHealthPercent() -- Check if it's a valid unit if healthPercentage < 100 then + --local unitTable = units[unitGUID] local unitTable = units[unitGUID] -- Check if we have seen one time this unit, if we don't then initialize it. - if not unitTable or healthPercentage > unitTable[1][1][2] then - unitTable = { {}, currentTime } + if not unitTable or healthPercentage > unitTable.history[1].percentage then + unitTable = { + history = { + { time = currentTime, percentage = healthPercentage } + }, + time = + currentTime + } units[unitGUID] = unitTable end - local values = unitTable[1] - local time = currentTime - unitTable[2] + local history = unitTable.history + local time = currentTime - unitTable.time -- Check if the % HP changed since the last check (or if there were none) - if not values or healthPercentage ~= values[2] then - local value + if not history or healthPercentage ~= history[1].percentage then + local val local lastIndex = #ttdCache -- Check if we can re-use a table from the cache if lastIndex == 0 then - value = { time, healthPercentage } + val = { time = currentTime, percentage = healthPercentage } else - value = ttdCache[lastIndex] + val = ttdCache[lastIndex] ttdCache[lastIndex] = nil - value[1] = time - value[2] = healthPercentage + val.time = currentTime + val.percentage = healthPercentage end - table.insert(values, 1, value) - local n = #values + table.insert(history, 1, val) + local n = #history -- Delete values that are no longer valid - while (n > historyCount) or (time - values[n][1] > historyTime) do - ttdCache[#Cache + 1] = values[n] - values[n] = nil + while (n > historyCount) or (time - history[n].time > historyTime) do + ttdCache[#ttdCache + 1] = history[n] + history[n] = nil n = n - 1 end end diff --git a/src/Unit/Unit.lua b/src/Unit/Unit.lua index 87f8205..a3c0042 100644 --- a/src/Unit/Unit.lua +++ b/src/Unit/Unit.lua @@ -128,7 +128,7 @@ function Unit:GetMaxHealth() return UnitHealthMax(self:GetOMToken()) end --- Get the units health percentage +-- Return Health Percent as an integer (0-100) ---@return number function Unit:GetHP() return self:GetHealth() / self:GetMaxHealth() * 100 @@ -152,7 +152,7 @@ function Unit:GetHealAbsorbedHealth() return UnitGetTotalHealAbsorbs(self:GetOMToken()) end --- Get the units health deficit +-- Return Health Percent as an integer (0-100) ---@return number function Unit:GetHealthPercent() return self:GetHP() @@ -1493,15 +1493,15 @@ function Unit:TimeToX(percentage, minSamples) -- Matrix arithmetic has been expanded and solved to make the following operation as fast as possible if unitTable then minSamples = minSamples or 3 - local values = unitTable[1] - local n = #values + local history = unitTable.history + local n = #history if n > minSamples then local a, b = 0, 0 local ex2, ex, exy, ey = 0, 0, 0, 0 for i = 1, n do - local value = values[i] - local x, y = value[1], value[2] + local value = history[i] + local x, y = value.time, value.percentage ex2 = ex2 + x * x ex = ex + x @@ -1517,7 +1517,7 @@ function Unit:TimeToX(percentage, minSamples) -- Use best fit line to calculate estimated time to reach target health seconds = (percentage - a) / b -- Subtract current time to obtain "time remaining" - seconds = seconds - (GetTime() - unitTable[2]) + seconds = seconds - (GetTime() - unitTable.time) if seconds < 0 then seconds = Bastion.TimeToDie.Enums.NEGATIVE_TTD end end end