Refactored Engine::DoNextAction (#854)

Update Engine.cpp
This commit is contained in:
SaW 2025-01-10 18:52:20 +01:00 committed by GitHub
parent 8f37a0cdd4
commit 72db64ba84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -146,57 +146,51 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
bool actionExecuted = false; bool actionExecuted = false;
ActionBasket* basket = nullptr; ActionBasket* basket = nullptr;
time_t currentTime = time(nullptr); time_t currentTime = time(nullptr);
// aiObjectContext->Update();
// Update triggers and push default actions
ProcessTriggers(minimal); ProcessTriggers(minimal);
PushDefaultActions(); PushDefaultActions();
uint32 iterations = 0; uint32 iterations = 0;
uint32 iterationsPerTick = queue.Size() * (minimal ? 2 : sPlayerbotAIConfig->iterationsPerTick); uint32 iterationsPerTick = queue.Size() * (minimal ? 2 : sPlayerbotAIConfig->iterationsPerTick);
do
while (++iterations <= iterationsPerTick)
{ {
basket = queue.Peek(); basket = queue.Peek();
if (!basket)
break;
if (basket) float relevance = basket->getRelevance(); // for reference
{
float relevance = basket->getRelevance(); // just for reference
bool skipPrerequisites = basket->isSkipPrerequisites(); bool skipPrerequisites = basket->isSkipPrerequisites();
if (minimal && (relevance < 100)) if (minimal && (relevance < 100))
continue; continue;
Event event = basket->getEvent(); Event event = basket->getEvent();
// NOTE: queue.Pop() deletes basket ActionNode* actionNode = queue.Pop(); // NOTE: Pop() deletes basket
ActionNode* actionNode = queue.Pop();
Action* action = InitializeAction(actionNode); Action* action = InitializeAction(actionNode);
if (action)
action->setRelevance(relevance);
if (!action) if (!action)
{ {
// LOG_ERROR("playerbots", "Action: {} - is UNKNOWN - c:{} l:{}", actionNode->getName().c_str(),
// botAI->GetBot()->getClass(), botAI->GetBot()->GetLevel());
LogAction("A:%s - UNKNOWN", actionNode->getName().c_str()); LogAction("A:%s - UNKNOWN", actionNode->getName().c_str());
} }
else if (action->isUseful()) else if (action->isUseful())
{ {
for (std::vector<Multiplier*>::iterator i = multipliers.begin(); i != multipliers.end(); i++) // Apply multipliers early to avoid unnecessary iterations
for (Multiplier* multiplier : multipliers)
{ {
Multiplier* multiplier = *i;
relevance *= multiplier->GetValue(action); relevance *= multiplier->GetValue(action);
action->setRelevance(relevance); action->setRelevance(relevance);
if (!relevance) if (relevance <= 0)
{ {
LogAction("Multiplier %s made action %s useless", multiplier->getName().c_str(), LogAction("Multiplier %s made action %s useless", multiplier->getName().c_str(), action->getName().c_str());
action->getName().c_str());
break; break;
} }
} }
if (action->isPossible() && relevance) if (action->isPossible() && relevance > 0)
{ {
if (!skipPrerequisites) if (!skipPrerequisites)
{ {
@ -209,8 +203,7 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
} }
} }
PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_ACTION, action->getName(), PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_ACTION, action->getName(), &aiObjectContext->performanceStack);
&aiObjectContext->performanceStack);
actionExecuted = ListenAndExecute(action, event); actionExecuted = ListenAndExecute(action, event);
if (pmo) if (pmo)
pmo->finish(); pmo->finish();
@ -220,7 +213,7 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
LogAction("A:%s - OK", action->getName().c_str()); LogAction("A:%s - OK", action->getName().c_str());
MultiplyAndPush(actionNode->getContinuers(), relevance, false, event, "cont"); MultiplyAndPush(actionNode->getContinuers(), relevance, false, event, "cont");
lastRelevance = relevance; lastRelevance = relevance;
delete actionNode; delete actionNode; // Safe memory management
break; break;
} }
else else
@ -231,82 +224,28 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
} }
else else
{ {
if (botAI->HasStrategy("debug", BOT_STATE_NON_COMBAT))
{
std::ostringstream out;
out << "do: ";
out << action->getName();
out << " impossible (";
out << action->getRelevance() << ")";
if (!event.GetSource().empty())
out << " [" << event.GetSource() << "]";
botAI->TellMasterNoFacing(out);
}
LogAction("A:%s - IMPOSSIBLE", action->getName().c_str()); LogAction("A:%s - IMPOSSIBLE", action->getName().c_str());
MultiplyAndPush(actionNode->getAlternatives(), relevance + 0.003f, false, event, "alt"); MultiplyAndPush(actionNode->getAlternatives(), relevance + 0.003f, false, event, "alt");
} }
} }
else else
{ {
if (botAI->HasStrategy("debug", BOT_STATE_NON_COMBAT))
{
std::ostringstream out;
out << "do: ";
out << action->getName();
out << " useless (";
out << action->getRelevance() << ")";
if (!event.GetSource().empty())
out << " [" << event.GetSource() << "]";
botAI->TellMasterNoFacing(out);
}
lastRelevance = relevance;
LogAction("A:%s - USELESS", action->getName().c_str()); LogAction("A:%s - USELESS", action->getName().c_str());
lastRelevance = relevance;
} }
delete actionNode; delete actionNode; // Always delete after processing the action node
} }
} while (basket && ++iterations <= iterationsPerTick);
// if (!basket)
// {
// lastRelevance = 0.0f;
// PushDefaultActions();
// // prevent the delay after pushing default actions
// if (queue.Peek() && depth < 1 && !minimal)
// return DoNextAction(unit, depth + 1, minimal);
// }
// MEMORY FIX TEST
/*
do
{
basket = queue.Peek();
if (basket)
{
// NOTE: queue.Pop() deletes basket
delete queue.Pop();
}
}
while (basket);
*/
if (time(nullptr) - currentTime > 1) if (time(nullptr) - currentTime > 1)
{ {
LogAction("too long execution"); LogAction("Execution time exceeded 1 second");
} }
if (!actionExecuted) if (!actionExecuted)
LogAction("no actions executed"); LogAction("No actions executed");
queue.RemoveExpired();
queue.RemoveExpired(); // Clean up expired actions in the queue
return actionExecuted; return actionExecuted;
} }
@ -567,7 +506,7 @@ std::string const Engine::ListStrategies()
std::string s = "Strategies: "; std::string s = "Strategies: ";
if (strategies.empty()) if (strategies.empty())
return std::move(s); return s;
for (std::map<std::string, Strategy*>::iterator i = strategies.begin(); i != strategies.end(); i++) for (std::map<std::string, Strategy*>::iterator i = strategies.begin(); i != strategies.end(); i++)
{ {