|
|
|
@ -20,9 +20,12 @@ local TimeToDie = { |
|
|
|
|
-- Max history count : min=20, max=500, default = 100 |
|
|
|
|
HistoryCount = 100 |
|
|
|
|
}, |
|
|
|
|
---@type table<number, { time: number, percentage: number }> |
|
|
|
|
Cache = {}, -- A cache of unused { time, value } tables to reduce garbage due to table creation |
|
|
|
|
---@type table<string, {[1]: {[1]: {[1]: number, [2]: number}, [2]: number}, [2]: number }> |
|
|
|
|
Units = {}, -- Used to track units, |
|
|
|
|
--@type table<string, {[1]: {[1]: {[1]: number, [2]: number}, [2]: number}, [2]: number }> |
|
|
|
|
--Units = {}, -- Used to track units, |
|
|
|
|
---@type table<string, { history: table<number, { time: number, percentage: number }>, time: number }> |
|
|
|
|
Units = {}, |
|
|
|
|
---@type table<string, boolean> |
|
|
|
|
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 |
|
|
|
|