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

View File

@@ -146,57 +146,51 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
bool actionExecuted = false;
ActionBasket* basket = nullptr;
time_t currentTime = time(nullptr);
// aiObjectContext->Update();
// Update triggers and push default actions
ProcessTriggers(minimal);
PushDefaultActions();
uint32 iterations = 0;
uint32 iterationsPerTick = queue.Size() * (minimal ? 2 : sPlayerbotAIConfig->iterationsPerTick);
do
while (++iterations <= iterationsPerTick)
{
basket = queue.Peek();
if (!basket)
break;
if (basket)
{
float relevance = basket->getRelevance(); // just for reference
float relevance = basket->getRelevance(); // for reference
bool skipPrerequisites = basket->isSkipPrerequisites();
if (minimal && (relevance < 100))
continue;
Event event = basket->getEvent();
// NOTE: queue.Pop() deletes basket
ActionNode* actionNode = queue.Pop();
ActionNode* actionNode = queue.Pop(); // NOTE: Pop() deletes basket
Action* action = InitializeAction(actionNode);
if (action)
action->setRelevance(relevance);
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());
}
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);
action->setRelevance(relevance);
if (!relevance)
if (relevance <= 0)
{
LogAction("Multiplier %s made action %s useless", multiplier->getName().c_str(),
action->getName().c_str());
LogAction("Multiplier %s made action %s useless", multiplier->getName().c_str(), action->getName().c_str());
break;
}
}
if (action->isPossible() && relevance)
if (action->isPossible() && relevance > 0)
{
if (!skipPrerequisites)
{
@@ -209,8 +203,7 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
}
}
PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_ACTION, action->getName(),
&aiObjectContext->performanceStack);
PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_ACTION, action->getName(), &aiObjectContext->performanceStack);
actionExecuted = ListenAndExecute(action, event);
if (pmo)
pmo->finish();
@@ -220,7 +213,7 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
LogAction("A:%s - OK", action->getName().c_str());
MultiplyAndPush(actionNode->getContinuers(), relevance, false, event, "cont");
lastRelevance = relevance;
delete actionNode;
delete actionNode; // Safe memory management
break;
}
else
@@ -231,82 +224,28 @@ bool Engine::DoNextAction(Unit* unit, uint32 depth, bool minimal)
}
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());
MultiplyAndPush(actionNode->getAlternatives(), relevance + 0.003f, false, event, "alt");
}
}
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());
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)
{
LogAction("too long execution");
LogAction("Execution time exceeded 1 second");
}
if (!actionExecuted)
LogAction("no actions executed");
queue.RemoveExpired();
LogAction("No actions executed");
queue.RemoveExpired(); // Clean up expired actions in the queue
return actionExecuted;
}
@@ -567,7 +506,7 @@ std::string const Engine::ListStrategies()
std::string s = "Strategies: ";
if (strategies.empty())
return std::move(s);
return s;
for (std::map<std::string, Strategy*>::iterator i = strategies.begin(); i != strategies.end(); i++)
{