performance optimize

This commit is contained in:
Yunfan Li
2023-07-27 11:39:03 +08:00
parent 02521589cf
commit 6ba6bc3615
9 changed files with 50 additions and 47 deletions

View File

@@ -48,7 +48,7 @@ void PlayerbotAIBase::IncreaseNextCheckDelay(uint32 delay)
bool PlayerbotAIBase::CanUpdateAI()
{
return nextAICheckDelay < 100;
return nextAICheckDelay == 0;
}
void PlayerbotAIBase::YieldThread(bool delay)

View File

@@ -633,7 +633,7 @@ void Engine::LogAction(char const* format, ...)
if (testMode)
{
FILE* file = fopen("test.log", "a");
fprintf(file, "'{}'", buf);
fprintf(file, "'%s'", buf);
fprintf(file, "\n");
fclose(file);
}

View File

@@ -22,31 +22,37 @@ class GenericBossHelper : public AiObject {
public:
GenericBossHelper(PlayerbotAI* botAI, std::string name): AiObject(botAI), name_(name) {}
virtual bool UpdateBossAI() {
Unit* unit = AI_VALUE2(Unit*, "find target", name_);
if (!unit) {
return false;
if(unit_ && !unit_->IsInWorld()) {
unit_ = nullptr;
}
target_ = unit->ToCreature();
if (!target_) {
return false;
}
ai_ = dynamic_cast<BossAiType *>(target_->GetAI());
if (!ai_) {
return false;
}
event_map_ = &ai_->events;
if (!event_map_) {
return false;
if (!unit_) {
unit_ = AI_VALUE2(Unit*, "find target", name_);
if (!unit_) {
return false;
}
target_ = unit_->ToCreature();
if (!target_) {
return false;
}
ai_ = dynamic_cast<BossAiType *>(target_->GetAI());
if (!ai_) {
return false;
}
event_map_ = &ai_->events;
if (!event_map_) {
return false;
}
}
timer_ = event_map_->GetTimer();
return true;
}
protected:
Creature* target_;
std::string name_;
BossAiType *ai_;
EventMap* event_map_;
uint32 timer_;
Unit* unit_ = nullptr;
Creature* target_ = nullptr;
std::string name_ = nullptr;
BossAiType *ai_ = nullptr;
EventMap* event_map_ = nullptr;
uint32 timer_ = 0;
};
class KelthuzadBossHelper: public GenericBossHelper<boss_kelthuzad::boss_kelthuzadAI> {

View File

@@ -16,7 +16,7 @@ class Unit;
class AttackersValue : public ObjectGuidListCalculatedValue
{
public:
AttackersValue(PlayerbotAI* botAI) : ObjectGuidListCalculatedValue(botAI, "attackers", 1) { }
AttackersValue(PlayerbotAI* botAI) : ObjectGuidListCalculatedValue(botAI, "attackers", 2) { }
GuidVector Calculate();
static bool IsPossibleTarget(Unit* attacker, Player* bot, float range = sPlayerbotAIConfig->sightDistance);

View File

@@ -18,7 +18,9 @@ class FindLeastHpTargetStrategy : public FindTargetStrategy
if (guid && attacker->GetGUID() == guid)
return;
}
if (!attacker->IsAlive()) {
return;
}
if (!result || result->GetHealth() > attacker->GetHealth())
result = attacker;
}

View File

@@ -81,6 +81,7 @@ bool PartyMemberToHeal::Check(Unit* player)
Unit* PartyMemberToProtect::Calculate()
{
return nullptr;
Group* group = bot->GetGroup();
if (!group)
return nullptr;

View File

@@ -38,7 +38,7 @@ class PartyMemberValue : public UnitCalculatedValue
class PartyMemberMainTankValue : public PartyMemberValue
{
public:
PartyMemberMainTankValue(PlayerbotAI* botAI) : PartyMemberValue(botAI) {}
PartyMemberMainTankValue(PlayerbotAI* botAI) : PartyMemberValue(botAI, "main tank member", 2) {}
virtual Unit* Calculate();
};

View File

@@ -4,6 +4,7 @@
#include "TargetValue.h"
#include "LastMovementValue.h"
#include "ObjectGuid.h"
#include "RtiTargetValue.h"
#include "Playerbots.h"
#include "ScriptedCreature.h"
@@ -117,31 +118,24 @@ WorldPosition HomeBindValue::Calculate()
Unit* FindTargetValue::Calculate()
{
if (qualifier == "") {
return NULL;
return nullptr;
}
Group* group = bot->GetGroup();
if (!group) {
return NULL;
return nullptr;
}
for (GroupReference *gref = group->GetFirstMember(); gref; gref = gref->next()) {
Player* member = gref->GetSource();
if (!member) {
continue;
}
HostileReference *ref = member->getHostileRefMgr().getFirst();
while (ref)
{
ThreatMgr *threatManager = ref->GetSource();
Unit *unit = threatManager->GetOwner();
std::wstring wnamepart;
Utf8toWStr(unit->GetName(), wnamepart);
wstrToLower(wnamepart);
if (!qualifier.empty() && qualifier.length() == wnamepart.length() && Utf8FitTo(qualifier, wnamepart)) {
return unit;
}
assert(ref);
ref = ref->next();
HostileReference *ref = bot->getHostileRefMgr().getFirst();
while (ref)
{
ThreatMgr *threatManager = ref->GetSource();
Unit *unit = threatManager->GetOwner();
std::wstring wnamepart;
Utf8toWStr(unit->GetName(), wnamepart);
wstrToLower(wnamepart);
if (!qualifier.empty() && qualifier.length() == wnamepart.length() && Utf8FitTo(qualifier, wnamepart)) {
return unit;
}
ref = ref->next();
}
return nullptr;
}

View File

@@ -41,7 +41,7 @@ class FindNonCcTargetStrategy : public FindTargetStrategy
class TargetValue : public UnitCalculatedValue
{
public:
TargetValue(PlayerbotAI* botAI, std::string const name = "target") : UnitCalculatedValue(botAI, name) { }
TargetValue(PlayerbotAI* botAI, std::string const name = "target", int checkInterval = 1) : UnitCalculatedValue(botAI, name, checkInterval) { }
protected:
Unit* FindTarget(FindTargetStrategy* strategy);
@@ -101,7 +101,7 @@ class PullTargetValue : public ManualSetValue<ObjectGuid>
class FindTargetValue : public UnitCalculatedValue, public Qualified
{
public:
FindTargetValue(PlayerbotAI* ai) : UnitCalculatedValue(ai) {}
FindTargetValue(PlayerbotAI* ai) : UnitCalculatedValue(ai, "find target", 2) {}
public:
Unit* Calculate();
@@ -117,7 +117,7 @@ class FindBossTargetStrategy : public FindTargetStrategy
class BossTargetValue : public TargetValue, public Qualified
{
public:
BossTargetValue(PlayerbotAI* ai) : TargetValue(ai) {}
BossTargetValue(PlayerbotAI* ai) : TargetValue(ai, "boss target", 1) {}
public:
Unit* Calculate();