2021-05-15 19:23:05 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Nullkiller.h"
|
|
|
|
#include "../VCAI.h"
|
2021-05-15 19:23:42 +03:00
|
|
|
#include "../AIHelper.h"
|
2021-05-15 19:23:11 +03:00
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
|
|
|
#include "../Behaviors/RecruitHeroBehavior.h"
|
|
|
|
#include "../Goals/Invalid.h"
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
|
|
|
|
2021-05-15 19:23:38 +03:00
|
|
|
Nullkiller::Nullkiller()
|
|
|
|
{
|
|
|
|
priorityEvaluator.reset(new PriorityEvaluator());
|
|
|
|
}
|
|
|
|
|
|
|
|
Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec tasks)
|
2021-05-15 19:23:11 +03:00
|
|
|
{
|
|
|
|
Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float
|
|
|
|
{
|
|
|
|
return goal->priority;
|
|
|
|
});
|
|
|
|
|
|
|
|
return bestTask;
|
|
|
|
}
|
|
|
|
|
2021-05-15 19:23:38 +03:00
|
|
|
Goals::TSubgoal Nullkiller::choseBestTask(Behavior & behavior)
|
2021-05-15 19:23:11 +03:00
|
|
|
{
|
2021-05-15 21:27:22 +03:00
|
|
|
logAi->debug("Checking behavior %s", behavior.toString());
|
|
|
|
|
2021-05-15 19:23:11 +03:00
|
|
|
auto tasks = behavior.getTasks();
|
|
|
|
|
|
|
|
if(tasks.empty())
|
|
|
|
{
|
2021-05-15 19:23:38 +03:00
|
|
|
logAi->debug("Behavior %s found no tasks", behavior.toString());
|
2021-05-15 19:23:11 +03:00
|
|
|
|
|
|
|
return Goals::sptr(Goals::Invalid());
|
|
|
|
}
|
|
|
|
|
2021-05-15 19:23:38 +03:00
|
|
|
for(auto task : tasks)
|
|
|
|
{
|
|
|
|
task->setpriority(priorityEvaluator->evaluate(task));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto task = choseBestTask(tasks);
|
|
|
|
|
2021-05-15 19:23:42 +03:00
|
|
|
logAi->debug("Behavior %s returns %s(%s), priority %f", behavior.toString(), task->name(), task->tile.toString(), task->priority);
|
2021-05-15 19:23:38 +03:00
|
|
|
|
|
|
|
return task;
|
2021-05-15 19:23:11 +03:00
|
|
|
}
|
2021-05-15 19:23:05 +03:00
|
|
|
|
|
|
|
void Nullkiller::makeTurn()
|
|
|
|
{
|
2021-05-15 19:23:11 +03:00
|
|
|
while(true)
|
|
|
|
{
|
2021-05-15 21:27:22 +03:00
|
|
|
ai->ah->updatePaths(ai->getMyHeroes(), true);
|
2021-05-15 19:23:42 +03:00
|
|
|
|
2021-05-15 19:23:11 +03:00
|
|
|
Goals::TGoalVec bestTasks = {
|
|
|
|
choseBestTask(CaptureObjectsBehavior()),
|
|
|
|
choseBestTask(RecruitHeroBehavior())
|
|
|
|
};
|
|
|
|
|
|
|
|
Goals::TSubgoal bestTask = choseBestTask(bestTasks);
|
|
|
|
|
|
|
|
if(bestTask->invalid())
|
|
|
|
{
|
|
|
|
logAi->trace("No goals found. Ending turn.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-05-15 21:27:22 +03:00
|
|
|
activeHero = bestTask->hero;
|
|
|
|
|
2021-05-15 19:23:11 +03:00
|
|
|
bestTask->accept(ai.get());
|
|
|
|
}
|
|
|
|
catch(goalFulfilledException &)
|
|
|
|
{
|
|
|
|
logAi->trace(bestTask->completeMessage());
|
|
|
|
}
|
|
|
|
catch(std::exception & e)
|
|
|
|
{
|
|
|
|
logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
|
|
|
|
logAi->debug("The error message was: %s", e.what());
|
2021-05-15 19:23:05 +03:00
|
|
|
|
2021-05-15 19:23:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 19:23:05 +03:00
|
|
|
}
|