1
0
mirror of https://github.com/vcmi/vcmi.git synced 2026-05-22 09:55:17 +02:00

Refactor goal removal from resource manager queue

This commit is contained in:
Dydzio
2019-01-01 15:41:52 +01:00
parent 456132c06b
commit 3e1b623fb6
5 changed files with 37 additions and 32 deletions
+27 -16
View File
@@ -232,22 +232,12 @@ bool ResourceManager::notifyGoalCompleted(Goals::TSubgoal goal)
if (goal->invalid())
logAi->warn("Attempt to complete Invalid goal");
bool removedGoal = false;
while (true)
{ //unfortunatelly we can't use remove_if on heap
auto it = boost::find_if(queue, [goal](const ResourceObjective & ro) -> bool
{
return ro.goal == goal || ro.goal->fulfillsMe (goal);
});
if(it != queue.end()) //removed at least one
{
logAi->debug("Removing goal %s from ResourceManager.", it->goal->name());
queue.erase(queue.s_handle_from_iterator(it));
removedGoal = true;
}
else //found nothing more to remove
break;
}
std::function<bool(const Goals::TSubgoal &)> equivalentGoalsCheck = [goal](const Goals::TSubgoal & x) -> bool
{
return x == goal || x->fulfillsMe(goal);
};
bool removedGoal = removeOutdatedObjectives(equivalentGoalsCheck);
dumpToLog();
@@ -315,6 +305,27 @@ bool ResourceManager::hasTasksLeft() const
return !queue.empty();
}
bool ResourceManager::removeOutdatedObjectives(std::function<bool(const Goals::TSubgoal &)> predicate)
{
bool removedAnything = false;
while(true)
{ //unfortunately we can't use remove_if on heap
auto it = boost::find_if(queue, [&](const ResourceObjective & ro) -> bool
{
predicate(ro.goal);
});
if(it != queue.end()) //removed at least one
{
logAi->debug("Removing goal %s from ResourceManager.", it->goal->name());
queue.erase(queue.s_handle_from_iterator(it));
removedAnything = true;
}
else //found nothing more to remove
break;
}
return removedAnything;
}
TResources ResourceManager::reservedResources() const
{
TResources res;