From 26d1d2430b02d33031f805520eb10e7941d784aa Mon Sep 17 00:00:00 2001 From: 4n0n <4n0n@tinkr.site> Date: Tue, 3 Jan 2023 21:58:58 -0600 Subject: [PATCH] Update UnitManager to enum enemies more efficiently. Update resto for natures cure/aoe purge logic --- scripts/restodruid.lua | 79 ++++++++++++++++++++++++++------- src/UnitManager/UnitManager.lua | 55 +++++++++++++++++++---- 2 files changed, 109 insertions(+), 25 deletions(-) diff --git a/scripts/restodruid.lua b/scripts/restodruid.lua index ba36a5f..7746578 100644 --- a/scripts/restodruid.lua +++ b/scripts/restodruid.lua @@ -139,11 +139,12 @@ local Lowest = Bastion.UnitManager:CreateCustomUnit('lowest', function(unit) return lowest end) -local DispelTarget = Bastion.UnitManager:CreateCustomUnit('dispel', function() - local dispelTarget = nil +local DispelTarget = Bastion.UnitManager:CreateCustomUnit('rejuv', function(unit) + local lowest = nil + local lowestHP = math.huge Bastion.UnitManager:EnumFriends(function(unit) - if Player:GetDistance(unit) > 40 then + if unit:IsDead() then return false end @@ -151,24 +152,57 @@ local DispelTarget = Bastion.UnitManager:CreateCustomUnit('dispel', function() return false end - if unit:IsDead() then + if Player:GetDistance(unit) > 40 then return false end - if unit:GetAuras():HasAnyDispelableAura(NaturesCure) then - return true - end + if not unit:IsDead() and Player:CanSee(unit) and + unit:GetAuras():HasAnyDispelableAura(NaturesCure) then - return false + local hp = unit:GetHP() + if hp < lowestHP then + lowest = unit + lowestHP = hp + end + end end) - if not dispelTarget then - dispelTarget = Player + if lowest == nil then + lowest = None end - return dispelTarget + return lowest end) +-- local PurgeTarget = Bastion.UnitManager:CreateCustomUnit('purge', function(unit) +-- local purge = nil + +-- Bastion.UnitManager:EnumEnemies(function(unit) +-- if unit:IsDead() then +-- return false +-- end + +-- if not Player:CanSee(unit) then +-- return false +-- end + +-- if Player:GetDistance(unit) > 40 then +-- return false +-- end + +-- if not unit:IsDead() and Player:CanSee(unit) and +-- unit:GetAuras():HasAnyStealableAura() then +-- purge = unit +-- end +-- end) + +-- if purge == nil then +-- purge = None +-- end + +-- return purge +-- end) + local Tank = Bastion.UnitManager:CreateCustomUnit('tank', function(unit) local tank = nil @@ -277,7 +311,7 @@ local SwiftmendUnit = Bastion.UnitManager:CreateCustomUnit('swiftmend', function if lowest == nil then - lowest = Player + lowest = None end return lowest @@ -319,10 +353,17 @@ DefaultAPL:AddAction( DefaultAPL:AddSpell( NaturesCure:CastableIf(function(self) return DispelTarget:Exists() and self:IsKnownAndUsable() and not Player:IsCastingOrChanneling() and - self:IsInRange(DispelTarget) and DispelTarget:GetAuras():HasAnyDispelableAura(self) + self:IsInRange(DispelTarget) and DispelTarget:GetAuras():HasAnyDispelableAura(NaturesCure) end):SetTarget(DispelTarget) ) +DefaultAPL:AddSpell( + Soothe:CastableIf(function(self) + return Target:Exists() and self:IsKnownAndUsable() and not Player:IsCastingOrChanneling() and + self:IsInRange(Target) and Target:GetAuras():HasAnyStealableAura() + end):SetTarget(Target) +) + DefaultAPL:AddSpell( NaturesSwiftness:CastableIf(function(self) return Lowest:Exists() and self:IsKnownAndUsable() and not Player:IsCastingOrChanneling() @@ -459,7 +500,8 @@ DefaultAPL:AddSpell( and Player:CanSee(Target) and ( not Target:GetAuras():FindMy(SunfireAura):IsUp() or - Target:GetAuras():FindMy(SunfireAura):GetRemainingTime() <= 5.4) + Target:GetAuras():FindMy(SunfireAura):GetRemainingTime() <= 5.4) and Target:IsHostile() and + Target:IsAffectingCombat() end):SetTarget(Target) ) @@ -469,21 +511,24 @@ DefaultAPL:AddSpell( and Player:CanSee(Target) and ( not Target:GetAuras():FindMy(MoonfireAura):IsUp() or - Target:GetAuras():FindMy(MoonfireAura):GetRemainingTime() <= 5.4) + Target:GetAuras():FindMy(MoonfireAura):GetRemainingTime() <= 5.4) and Target:IsHostile() and + Target:IsAffectingCombat() end):SetTarget(Target) ) DefaultAPL:AddSpell( Starsurge:CastableIf(function(self) return Target:Exists() and self:IsKnownAndUsable() and not Player:IsCastingOrChanneling() - and Player:CanSee(Target) + and Player:CanSee(Target) and Target:IsHostile() and + Target:IsAffectingCombat() end):SetTarget(Target) ) DefaultAPL:AddSpell( Wrath:CastableIf(function(self) return Target:Exists() and self:IsKnownAndUsable() and not Player:IsCastingOrChanneling() - and Player:CanSee(Target) and not Player:IsMoving() + and Player:CanSee(Target) and not Player:IsMoving() and Target:IsHostile() and + Target:IsAffectingCombat() end):SetTarget(Target) ) diff --git a/src/UnitManager/UnitManager.lua b/src/UnitManager/UnitManager.lua index 11d9fb7..39b8f40 100644 --- a/src/UnitManager/UnitManager.lua +++ b/src/UnitManager/UnitManager.lua @@ -1,5 +1,7 @@ local Tinkr, Bastion = ... +local ObjectManager = Tinkr.Util.ObjectManager + local Unit = Bastion.Unit local prefixes = { @@ -49,6 +51,7 @@ end local UnitManager = { units = {}, customUnits = {}, + objects = {}, cache = {} } @@ -70,6 +73,10 @@ function UnitManager:__index(k) return self.customUnits[k].unit end + if self.objects[k] then + return self.objects[k] + end + -- if not Validate(k) then -- error("UnitManager:Get - Invalid token: " .. k) -- end @@ -78,6 +85,8 @@ function UnitManager:__index(k) self.units[k] = Unit:New(k) end + + return self.units[k] end @@ -107,6 +116,14 @@ function UnitManager:Get(token) return self.units[token] end +function UnitManager:GetObject(guid) + return self.objects[guid] +end + +function UnitManager:SetObject(unit) + self.objects[unit:GetGUID()] = unit +end + -- Create a custom unit and cache it for .5 seconds function UnitManager:CreateCustomUnit(token, cb) local unit = cb() @@ -160,14 +177,18 @@ end -- Enum Enemies (object manager) function UnitManager:EnumEnemies(cb) - local objs = Objects() - for i = 1, #objs do - local obj = objs[i] + for obj in ObjectManager:Objects() do if ObjectType(obj) == 5 or ObjectType(obj) == 6 then - local unit = Unit:New(obj) - if unit:IsHostile() and unit:IsAffectingCombat() and unit:IsAlive() and unit:CanSee(self) + local unit = UnitManager:GetObject(ObjectGUID(obj)) + if not unit then + unit = Unit:New(obj) + UnitManager:SetObject(unit) + end + if unit:IsHostile() and unit:IsAffectingCombat() and unit:IsAlive() then - cb(unit) + if cb(unit) then + break + end end end end @@ -179,8 +200,26 @@ function UnitManager:EnumUnits(cb) for i = 1, #objs do local obj = objs[i] if ObjectType(obj) == 5 or ObjectType(obj) == 6 then - local unit = Unit:New(obj) - cb(unit) + local unit = UnitManager:GetObject(ObjectGUID(obj)) + if not unit then + unit = Unit:New(obj) + UnitManager:SetObject(unit) + end + if cb(unit) then + break + end + end + end +end + +-- Enum enemies (nameplates) +function UnitManager:EnumNameplates(cb) + for i = 1, 50 do + local unit = self:Get('nameplate' .. i) + if unit:IsValid() then + if cb(unit) then + break + end end end end