Reduce string overhead and totem check overhead

This commit is contained in:
Yunfan Li
2024-04-11 14:37:15 +08:00
parent b474dc44bb
commit 9d69e9263b
6 changed files with 29 additions and 6 deletions

View File

@@ -1993,7 +1993,7 @@ bool PlayerbotAI::HasAura(std::string const name, Unit* unit, bool maxStack, boo
{
SpellInfo const* spellInfo = aurEff->GetSpellInfo();
std::string const auraName = spellInfo->SpellName[0];
std::string_view const auraName = spellInfo->SpellName[0];
if (auraName.empty() || auraName.length() != wnamepart.length() || !Utf8FitTo(auraName, wnamepart))
continue;

View File

@@ -265,7 +265,7 @@ class NamedObjectFactoryList
factories.push_front(context);
}
T* GetContextObject(std::string const name, PlayerbotAI* botAI)
T* GetContextObject(std::string const &name, PlayerbotAI* botAI)
{
for (typename std::list<NamedObjectFactory<T>*>::iterator i = factories.begin(); i != factories.end(); i++)
{

View File

@@ -9,7 +9,7 @@ char* strstri(char const* str1, char const* str2);
bool HasTotemValue::Calculate()
{
GuidVector units = *context->GetValue<GuidVector>("nearest npcs");
GuidVector units = *context->GetValue<GuidVector>("nearest totems");
for (ObjectGuid const guid : units)
{
Unit* unit = botAI->GetUnit(guid);
@@ -17,14 +17,12 @@ bool HasTotemValue::Calculate()
continue;
Creature* creature = dynamic_cast<Creature*>(unit);
if (!creature || !creature->IsTotem())
continue;
if (creature->GetOwner() != bot) {
continue;
}
if (strstri(creature->GetName().c_str(), qualifier.c_str()) && bot->GetDistance(creature) <= botAI->GetRange("spell"))
if (strstri(creature->GetName().c_str(), qualifier.c_str()))
return true;
}

View File

@@ -50,4 +50,16 @@ void NearestTriggersValue::FindUnits(std::list<Unit*>& targets)
bool NearestTriggersValue::AcceptUnit(Unit* unit)
{
return !unit->IsPlayer();
}
void NearestTotemsValue::FindUnits(std::list<Unit*>& targets)
{
Acore::AnyUnitInObjectRangeCheck u_check(bot, range);
Acore::UnitListSearcher<Acore::AnyUnitInObjectRangeCheck> searcher(bot, targets, u_check);
Cell::VisitAllObjects(bot, searcher, range);
}
bool NearestTotemsValue::AcceptUnit(Unit* unit)
{
return unit->IsTotem();
}

View File

@@ -39,4 +39,15 @@ class NearestTriggersValue : public NearestUnitsValue
void FindUnits(std::list<Unit*>& targets) override;
bool AcceptUnit(Unit* unit) override;
};
class NearestTotemsValue : public NearestUnitsValue
{
public:
NearestTotemsValue(PlayerbotAI* botAI, float range = 30.0f) : NearestUnitsValue(botAI, "nearest npcs", range, true) { }
protected:
void FindUnits(std::list<Unit*>& targets) override;
bool AcceptUnit(Unit* unit) override;
};
#endif

View File

@@ -103,6 +103,7 @@ class ValueContext : public NamedObjectContext<UntypedValue>
creators["nearest game objects no los"] = &ValueContext::nearest_game_objects_no_los;
creators["closest game objects"] = &ValueContext::closest_game_objects;
creators["nearest npcs"] = &ValueContext::nearest_npcs;
creators["nearest totems"] = &ValueContext::nearest_totems;
creators["nearest vehicles"] = &ValueContext::nearest_vehicles;
creators["nearest friendly players"] = &ValueContext::nearest_friendly_players;
creators["closest friendly players"] = &ValueContext::closest_friendly_players;
@@ -367,6 +368,7 @@ class ValueContext : public NamedObjectContext<UntypedValue>
static UntypedValue* closest_game_objects(PlayerbotAI* botAI) { return new NearestGameObjects(botAI, INTERACTION_DISTANCE); }
static UntypedValue* log_level(PlayerbotAI* botAI) { return new LogLevelValue(botAI); }
static UntypedValue* nearest_npcs(PlayerbotAI* botAI) { return new NearestNpcsValue(botAI); }
static UntypedValue* nearest_totems(PlayerbotAI* botAI) { return new NearestTotemsValue(botAI); }
static UntypedValue* nearest_vehicles(PlayerbotAI* botAI) { return new NearestVehiclesValue(botAI); }
static UntypedValue* nearest_friendly_players(PlayerbotAI* botAI) { return new NearestFriendlyPlayersValue(botAI); }
static UntypedValue* closest_friendly_players(PlayerbotAI* botAI) { return new NearestFriendlyPlayersValue(botAI, INTERACTION_DISTANCE); }