mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-18 08:28:32 +01:00
Core/Objects: Implement FindNearestCreatureWithOptions helper function (#28488)
Co-authored-by: Shauren <shauren.trinity@gmail.com>
(cherry picked from commit 9ab0679781)
This commit is contained in:
@@ -2126,11 +2126,14 @@ Creature* WorldObject::FindNearestCreature(uint32 entry, float range, bool alive
|
||||
return creature;
|
||||
}
|
||||
|
||||
Creature* WorldObject::FindNearestCreatureWithAura(uint32 entry, uint32 spellId, float range, bool alive) const
|
||||
Creature* WorldObject::FindNearestCreatureWithOptions(float range, FindCreatureOptions const& options) const
|
||||
{
|
||||
Creature* creature = nullptr;
|
||||
Trinity::NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck checker(*this, entry, spellId, alive, range);
|
||||
Trinity::CreatureLastSearcher<Trinity::NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck> searcher(this, creature, checker);
|
||||
Trinity::NearestCreatureEntryWithOptionsInObjectRangeCheck checker(*this, range, options);
|
||||
Trinity::CreatureLastSearcher<Trinity::NearestCreatureEntryWithOptionsInObjectRangeCheck> searcher(this, creature, checker);
|
||||
if (options.IgnorePhases)
|
||||
searcher.i_phaseMask = PHASEMASK_ANYWHERE;
|
||||
|
||||
Cell::VisitAllObjects(this, searcher, range);
|
||||
return creature;
|
||||
}
|
||||
|
||||
@@ -296,6 +296,49 @@ class FlaggedValuesArray32
|
||||
T_FLAGS m_flags;
|
||||
};
|
||||
|
||||
struct FindCreatureOptions
|
||||
{
|
||||
FindCreatureOptions() = default;
|
||||
|
||||
FindCreatureOptions& SetCreatureId(uint32 creatureId) { CreatureId = creatureId; return *this; }
|
||||
|
||||
FindCreatureOptions& SetIsAlive(bool isAlive) { IsAlive = isAlive; return *this; }
|
||||
FindCreatureOptions& SetIsInCombat(bool isInCombat) { IsInCombat = isInCombat; return *this; }
|
||||
FindCreatureOptions& SetIsSummon(bool isSummon) { IsSummon = isSummon; return *this; }
|
||||
|
||||
FindCreatureOptions& SetIgnorePhases(bool ignorePhases) { IgnorePhases = ignorePhases; return *this; }
|
||||
FindCreatureOptions& SetIgnoreNotOwnedPrivateObjects(bool ignoreNotOwnedPrivateObjects) { IgnoreNotOwnedPrivateObjects = ignoreNotOwnedPrivateObjects; return *this; }
|
||||
FindCreatureOptions& SetIgnorePrivateObjects(bool ignorePrivateObjects) { IgnorePrivateObjects = ignorePrivateObjects; return *this; }
|
||||
|
||||
FindCreatureOptions& SetHasAura(uint32 spellId) { AuraSpellId = spellId; return *this; }
|
||||
FindCreatureOptions& SetOwner(ObjectGuid ownerGuid) { OwnerGuid = ownerGuid; return *this; }
|
||||
FindCreatureOptions& SetCharmer(ObjectGuid charmerGuid) { CharmerGuid = charmerGuid; return *this; }
|
||||
FindCreatureOptions& SetCreator(ObjectGuid creatorGuid) { CreatorGuid = creatorGuid; return *this; }
|
||||
FindCreatureOptions& SetPrivateObjectOwner(ObjectGuid privateObjectOwnerGuid) { PrivateObjectOwnerGuid = privateObjectOwnerGuid; return *this; }
|
||||
|
||||
Optional<uint32> CreatureId;
|
||||
|
||||
Optional<bool> IsAlive;
|
||||
Optional<bool> IsInCombat;
|
||||
Optional<bool> IsSummon;
|
||||
|
||||
bool IgnorePhases;
|
||||
bool IgnoreNotOwnedPrivateObjects;
|
||||
bool IgnorePrivateObjects;
|
||||
|
||||
Optional<uint32> AuraSpellId;
|
||||
Optional<ObjectGuid> OwnerGuid;
|
||||
Optional<ObjectGuid> CharmerGuid;
|
||||
Optional<ObjectGuid> CreatorGuid;
|
||||
Optional<ObjectGuid> PrivateObjectOwnerGuid;
|
||||
|
||||
FindCreatureOptions(FindCreatureOptions const&) = delete;
|
||||
FindCreatureOptions(FindCreatureOptions&&) = delete;
|
||||
|
||||
FindCreatureOptions& operator=(FindCreatureOptions const&) = delete;
|
||||
FindCreatureOptions& operator=(FindCreatureOptions&&) = delete;
|
||||
};
|
||||
|
||||
class TC_GAME_API WorldObject : public Object, public WorldLocation
|
||||
{
|
||||
protected:
|
||||
@@ -426,7 +469,7 @@ class TC_GAME_API WorldObject : public Object, public WorldLocation
|
||||
void SummonCreatureGroup(uint8 group, std::list<TempSummon*>* list = nullptr);
|
||||
|
||||
Creature* FindNearestCreature(uint32 entry, float range, bool alive = true) const;
|
||||
Creature* FindNearestCreatureWithAura(uint32 entry, uint32 spellId, float range, bool alive = true) const;
|
||||
Creature* FindNearestCreatureWithOptions(float range, FindCreatureOptions const& options) const;
|
||||
GameObject* FindNearestGameObject(uint32 entry, float range, bool spawnedOnly = true) const;
|
||||
GameObject* FindNearestUnspawnedGameObject(uint32 entry, float range) const;
|
||||
GameObject* FindNearestGameObjectOfType(GameobjectTypes type, float range) const;
|
||||
|
||||
@@ -1398,37 +1398,58 @@ namespace Trinity
|
||||
NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&) = delete;
|
||||
};
|
||||
|
||||
class NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck
|
||||
class NearestCreatureEntryWithOptionsInObjectRangeCheck
|
||||
{
|
||||
public:
|
||||
NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck(WorldObject const& obj, uint32 entry, uint32 spellId, bool alive, float range)
|
||||
: i_obj(obj), i_entry(entry), i_spellId(spellId), i_alive(alive), i_range(range) { }
|
||||
NearestCreatureEntryWithOptionsInObjectRangeCheck(WorldObject const& obj, float range, FindCreatureOptions const& args)
|
||||
: i_obj(obj), i_args(args), i_range(range) { }
|
||||
|
||||
bool operator()(Creature* u)
|
||||
{
|
||||
if (u->getDeathState() != DEAD
|
||||
&& u->GetEntry() == i_entry
|
||||
&& u->HasAura(i_spellId)
|
||||
&& u->IsAlive() == i_alive
|
||||
&& u->GetGUID() != i_obj.GetGUID()
|
||||
&& i_obj.IsWithinDistInMap(u, i_range)
|
||||
&& u->CheckPrivateObjectOwnerVisibility(&i_obj))
|
||||
bool operator()(Creature* u)
|
||||
{
|
||||
if (u->getDeathState() == DEAD) // Despawned
|
||||
return false;
|
||||
|
||||
if (u->GetGUID() == i_obj.GetGUID())
|
||||
return false;
|
||||
|
||||
if (!i_obj.IsWithinDistInMap(u, i_range))
|
||||
return false;
|
||||
|
||||
if (i_args.CreatureId && u->GetEntry() != i_args.CreatureId)
|
||||
return false;
|
||||
|
||||
if (i_args.IsAlive.has_value() && u->IsAlive() != i_args.IsAlive)
|
||||
return false;
|
||||
|
||||
if (i_args.IsSummon.has_value() && u->IsSummon() != i_args.IsSummon)
|
||||
return false;
|
||||
|
||||
if (i_args.IsInCombat.has_value() && u->IsInCombat() != i_args.IsInCombat)
|
||||
return false;
|
||||
|
||||
if ((i_args.OwnerGuid && u->GetOwnerGUID() != i_args.OwnerGuid)
|
||||
|| (i_args.CharmerGuid && u->GetCharmerGUID() != i_args.CharmerGuid)
|
||||
|| (i_args.CreatorGuid && u->GetCreatorGUID() != i_args.CreatorGuid)
|
||||
|| (i_args.PrivateObjectOwnerGuid && u->GetPrivateObjectOwner() != i_args.PrivateObjectOwnerGuid))
|
||||
return false;
|
||||
|
||||
if (i_args.IgnorePrivateObjects && u->IsPrivateObject())
|
||||
return false;
|
||||
|
||||
if (i_args.IgnoreNotOwnedPrivateObjects && !u->CheckPrivateObjectOwnerVisibility(&i_obj))
|
||||
return false;
|
||||
|
||||
if (i_args.AuraSpellId && !u->HasAura(*i_args.AuraSpellId))
|
||||
return false;
|
||||
|
||||
i_range = i_obj.GetDistance(u); // use found unit range as new range limit for next check
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
WorldObject const& i_obj;
|
||||
uint32 i_entry;
|
||||
uint32 i_spellId;
|
||||
bool i_alive;
|
||||
float i_range;
|
||||
|
||||
// prevent clone this object
|
||||
NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck(NearestCreatureEntryWithLiveStateAndAuraInObjectRangeCheck const&) = delete;
|
||||
private:
|
||||
WorldObject const& i_obj;
|
||||
FindCreatureOptions const& i_args;
|
||||
float i_range;
|
||||
};
|
||||
|
||||
class AnyPlayerInObjectRangeCheck
|
||||
|
||||
Reference in New Issue
Block a user