Nullkiller.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Nullkiller.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Nullkiller.h"
  12. #include "../VCAI.h"
  13. #include "../AIhelper.h"
  14. #include "../Behaviors/CaptureObjectsBehavior.h"
  15. #include "../Behaviors/RecruitHeroBehavior.h"
  16. #include "../Behaviors/BuyArmyBehavior.h"
  17. #include "../Behaviors/StartupBehavior.h"
  18. #include "../Behaviors/DefenceBehavior.h"
  19. #include "../Behaviors/BuildingBehavior.h"
  20. #include "../Goals/Invalid.h"
  21. extern boost::thread_specific_ptr<CCallback> cb;
  22. extern boost::thread_specific_ptr<VCAI> ai;
  23. Nullkiller::Nullkiller()
  24. {
  25. priorityEvaluator.reset(new PriorityEvaluator());
  26. dangerHitMap.reset(new DangerHitMapAnalyzer());
  27. buildAnalyzer.reset(new BuildAnalyzer());
  28. }
  29. Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec & tasks) const
  30. {
  31. Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float{
  32. return goal->priority;
  33. });
  34. return bestTask;
  35. }
  36. Goals::TSubgoal Nullkiller::choseBestTask(std::shared_ptr<Behavior> behavior) const
  37. {
  38. logAi->debug("Checking behavior %s", behavior->toString());
  39. auto tasks = behavior->getTasks();
  40. if(tasks.empty())
  41. {
  42. logAi->debug("Behavior %s found no tasks", behavior->toString());
  43. return Goals::sptr(Goals::Invalid());
  44. }
  45. logAi->trace("Evaluating priorities, tasks count %d", tasks.size());
  46. for(auto task : tasks)
  47. {
  48. task->setpriority(priorityEvaluator->evaluate(task));
  49. }
  50. auto task = choseBestTask(tasks);
  51. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior->toString(), task->name(), task->tile.toString(), task->priority);
  52. return task;
  53. }
  54. void Nullkiller::resetAiState()
  55. {
  56. lockedHeroes.clear();
  57. dangerHitMap->reset();
  58. }
  59. void Nullkiller::updateAiState()
  60. {
  61. ai->validateVisitableObjs();
  62. dangerHitMap->updateHitMap();
  63. // TODO: move to hero manager
  64. auto activeHeroes = ai->getMyHeroes();
  65. vstd::erase_if(activeHeroes, [this](const HeroPtr & hero) -> bool
  66. {
  67. auto lockedReason = getHeroLockedReason(hero.h);
  68. return lockedReason == HeroLockedReason::DEFENCE;
  69. });
  70. ai->ah->updatePaths(activeHeroes, true);
  71. ai->ah->update();
  72. buildAnalyzer->update();
  73. }
  74. bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
  75. {
  76. for(auto & node : path.nodes)
  77. {
  78. if(isHeroLocked(node.targetHero))
  79. return true;
  80. }
  81. return false;
  82. }
  83. void Nullkiller::makeTurn()
  84. {
  85. resetAiState();
  86. while(true)
  87. {
  88. updateAiState();
  89. Goals::TGoalVec bestTasks = {
  90. choseBestTask(std::make_shared<BuyArmyBehavior>()),
  91. choseBestTask(std::make_shared<CaptureObjectsBehavior>()),
  92. choseBestTask(std::make_shared<RecruitHeroBehavior>()),
  93. choseBestTask(std::make_shared<DefenceBehavior>()),
  94. choseBestTask(std::make_shared<BuildingBehavior>())
  95. };
  96. if(cb->getDate(Date::DAY) == 1)
  97. {
  98. bestTasks.push_back(choseBestTask(std::make_shared<StartupBehavior>()));
  99. }
  100. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  101. if(bestTask->invalid())
  102. {
  103. logAi->trace("No goals found. Ending turn.");
  104. return;
  105. }
  106. if(bestTask->priority < MIN_PRIORITY)
  107. {
  108. logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->name());
  109. return;
  110. }
  111. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  112. try
  113. {
  114. activeHero = bestTask->hero.get();
  115. bestTask->accept(ai.get());
  116. }
  117. catch(goalFulfilledException &)
  118. {
  119. logAi->trace(bestTask->completeMessage());
  120. }
  121. /*catch(std::exception & e)
  122. {
  123. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  124. logAi->debug("The error message was: %s", e.what());
  125. return;
  126. }*/
  127. }
  128. }