From 5547bf37e12e73d48db3fbd6886f5b74f40324af Mon Sep 17 00:00:00 2001 From: 4n0n <4n0n@tinkr.site> Date: Mon, 30 Jan 2023 08:49:24 -0600 Subject: [PATCH] Add list registering, add InCombatOdds() --- scripts/subtlety.lua | 1 + src/ObjectManager/ObjectManager.lua | 64 ++++++++++++++++++++++++++++- src/Unit/Unit.lua | 24 ++++++----- src/_bastion.lua | 11 +++++ 4 files changed, 88 insertions(+), 12 deletions(-) diff --git a/scripts/subtlety.lua b/scripts/subtlety.lua index b95ad99..c6759c8 100644 --- a/scripts/subtlety.lua +++ b/scripts/subtlety.lua @@ -2040,6 +2040,7 @@ Stealth:CastableIf( ) SubModulue:Sync(function() + print(Player:GetEnemies(10)) DefaultAPL:Execute() end) diff --git a/src/ObjectManager/ObjectManager.lua b/src/ObjectManager/ObjectManager.lua index 7855cc8..cd0bbc4 100644 --- a/src/ObjectManager/ObjectManager.lua +++ b/src/ObjectManager/ObjectManager.lua @@ -7,7 +7,7 @@ ObjectManager.__index = ObjectManager function ObjectManager:New() local self = setmetatable({}, ObjectManager) - self._objects = {} + self._lists = {} self.enemies = Bastion.List:New() self.friends = Bastion.List:New() @@ -17,15 +17,54 @@ function ObjectManager:New() return self end +-- Register a custom list with a callback +function ObjectManager:RegisterList(name, cb) + if self._lists[name] then + return false + end + + self._lists[name] = { + list = Bastion.List:New(), + cb = cb + } + + return self._lists[name].list +end + +-- reset custom lists +function ObjectManager:ResetLists() + for _, list in pairs(self._lists) do + list.list:clear() + end +end + +-- Refresh custom lists +function ObjectManager:EnumLists(object) + for _, list in pairs(self._lists) do + local r = list.cb(object) + if r then + list.list:push(r) + end + end +end + +-- Get a list +function ObjectManager:GetList(name) + return self._lists[name].list +end + function ObjectManager:Refresh() self.enemies:clear() self.friends:clear() self.activeEnemies:clear() self.explosives:clear() + self:ResetLists() local objects = Objects() for _, object in pairs(objects) do + self:EnumLists(object) + if ObjectType(object) == 5 or ObjectType(object) == 6 then local unit = Bastion.UnitManager:GetObject(ObjectGUID(object)) if not unit then @@ -40,7 +79,7 @@ function ObjectManager:Refresh() elseif unit:IsEnemy() then self.enemies:push(unit) - if unit:IsAffectingCombat() then + if unit:InCombatOdds() > 80 then self.activeEnemies:push(unit) end end @@ -49,3 +88,24 @@ function ObjectManager:Refresh() end return ObjectManager + + +-- -- Register a list of objects that are training dummies +-- local dummies = Bastion.ObjectManager:RegisterList('dummies', function(object) +-- if ObjectType(object) == 5 or ObjectType(object) == 6 then +-- local unit = Bastion.UnitManager:GetObject(ObjectGUID(object)) + +-- if not unit then +-- unit = Bastion.Unit:New(object) +-- Bastion.UnitManager:SetObject(unit) +-- end + +-- if unit:GetID() == 198594 then +-- return unit +-- end +-- end +-- end) + +-- dummies:each(function(dummy) +-- print(dummy:GetName()) +-- end) diff --git a/src/Unit/Unit.lua b/src/Unit/Unit.lua index 63ada7f..6706b85 100644 --- a/src/Unit/Unit.lua +++ b/src/Unit/Unit.lua @@ -12,6 +12,7 @@ local Unit = { swings_since_sht = 0, last_off_attack = 0, last_main_attack = 0, + last_combat_time = 0, } function Unit:__index(k) @@ -386,7 +387,7 @@ function Unit:GetEnemies(range) Bastion.UnitManager:EnumEnemies(function(unit) if not self:IsUnit(unit) and unit:GetDistance(self) <= range and unit:IsAlive() and self:CanSee(unit) and - unit:IsEnemy() and unit:IsAffectingCombat() then + unit:IsEnemy() then count = count + 1 end end) @@ -627,17 +628,20 @@ end -- Set combat time if affecting combat and return the difference between now and the last time function Unit:GetCombatTime() - if self:IsAffectingCombat() then - self.last_combat_time = GetTime() - elseif not self:IsAffectingCombat() and self.last_combat_time then - self.last_combat_time = nil - end + return GetTime() - self.last_combat_time +end - if not self.last_combat_time then - return 0 - end +function Unit:SetLastCombatTime(time) + self.last_combat_time = time +end - return GetTime() - self.last_combat_time +-- Get combat odds (if the last combat time is less than 1 minute ago return 1 / time, else return 0) +-- the closer to 0 the more likely the unit is to be in combat (0 = 100%) 60 = 0% +function Unit:InCombatOdds() + local time = self:GetCombatTime() + local percent = 1 - (time / 60) + + return percent * 100 end -- Get units gcd time diff --git a/src/_bastion.lua b/src/_bastion.lua index 955230e..a285cd5 100644 --- a/src/_bastion.lua +++ b/src/_bastion.lua @@ -83,6 +83,17 @@ Bastion.EventManager:RegisterWoWEvent("UNIT_SPELLCAST_SUCCEEDED", function(...) end end) +Bastion.EventManager:RegisterWoWEvent("COMBAT_LOG_EVENT_UNFILTERED", function() + local _, subtype, _, sourceGUID, sourceName, _, _, destGUID, destName, destFlags, _, spellID, spellName, _, amount, interrupt, a, b, c, d, offhand, multistrike = CombatLogGetCurrentEventInfo() + + local u = Bastion.UnitManager[sourceGUID] + local u2 = Bastion.UnitManager[destGUID] + + local t = GetTime() + u:SetLastCombatTime(t) + u2:SetLastCombatTime(t) +end) + Bastion.Ticker = C_Timer.NewTicker(0.1, function() if not Bastion.CombatTimer:IsRunning() and UnitAffectingCombat("player") then Bastion.CombatTimer:Start()