Nullkiller.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "StdInc.h"
  2. #include "Nullkiller.h"
  3. #include "../VCAI.h"
  4. #include "../AIHelper.h"
  5. #include "../Behaviors/CaptureObjectsBehavior.h"
  6. #include "../Behaviors/RecruitHeroBehavior.h"
  7. #include "../Behaviors/BuyArmyBehavior.h"
  8. #include "../Goals/Invalid.h"
  9. extern boost::thread_specific_ptr<CCallback> cb;
  10. extern boost::thread_specific_ptr<VCAI> ai;
  11. Nullkiller::Nullkiller()
  12. {
  13. priorityEvaluator.reset(new PriorityEvaluator());
  14. dangerHitMap.reset(new DangerHitMapAnalyzer());
  15. }
  16. Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec & tasks) const
  17. {
  18. Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float{
  19. return goal->priority;
  20. });
  21. return bestTask;
  22. }
  23. Goals::TSubgoal Nullkiller::choseBestTask(std::shared_ptr<Behavior> behavior) const
  24. {
  25. logAi->debug("Checking behavior %s", behavior->toString());
  26. auto tasks = behavior->getTasks();
  27. if(tasks.empty())
  28. {
  29. logAi->debug("Behavior %s found no tasks", behavior->toString());
  30. return Goals::sptr(Goals::Invalid());
  31. }
  32. logAi->trace("Evaluating priorities, tasks count %d", tasks.size());
  33. for(auto task : tasks)
  34. {
  35. task->setpriority(priorityEvaluator->evaluate(task));
  36. }
  37. auto task = choseBestTask(tasks);
  38. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior->toString(), task->name(), task->tile.toString(), task->priority);
  39. return task;
  40. }
  41. void Nullkiller::resetAiState()
  42. {
  43. lockedHeroes.clear();
  44. dangerHitMap->updateHitMap();
  45. }
  46. void Nullkiller::updateAiState()
  47. {
  48. ai->validateVisitableObjs();
  49. // TODO: move to hero manager
  50. auto activeHeroes = ai->getMyHeroes();
  51. vstd::erase_if(activeHeroes, [&](const HeroPtr & hero) -> bool{
  52. return vstd::contains(lockedHeroes, hero);
  53. });
  54. ai->ah->updatePaths(activeHeroes, true);
  55. ai->ah->updateHeroRoles();
  56. }
  57. void Nullkiller::makeTurn()
  58. {
  59. resetAiState();
  60. while(true)
  61. {
  62. updateAiState();
  63. Goals::TGoalVec bestTasks = {
  64. choseBestTask(std::make_shared<BuyArmyBehavior>()),
  65. choseBestTask(std::make_shared<CaptureObjectsBehavior>()),
  66. choseBestTask(std::make_shared<RecruitHeroBehavior>())
  67. };
  68. if(cb->getDate(Date::DAY) == 1)
  69. {
  70. bestTasks.push_back(choseBestTask(std::make_shared<StartupBehavior>()));
  71. }
  72. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  73. if(bestTask->invalid())
  74. {
  75. logAi->trace("No goals found. Ending turn.");
  76. return;
  77. }
  78. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  79. try
  80. {
  81. activeHero = bestTask->hero;
  82. bestTask->accept(ai.get());
  83. }
  84. catch(goalFulfilledException &)
  85. {
  86. logAi->trace(bestTask->completeMessage());
  87. }
  88. catch(std::exception & e)
  89. {
  90. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  91. logAi->debug("The error message was: %s", e.what());
  92. return;
  93. }
  94. }
  95. }