Nullkiller.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "../Behaviors/GatherArmyBehavior.h"
  21. #include "../Goals/Invalid.h"
  22. extern boost::thread_specific_ptr<CCallback> cb;
  23. extern boost::thread_specific_ptr<VCAI> ai;
  24. Nullkiller::Nullkiller()
  25. {
  26. priorityEvaluator.reset(new PriorityEvaluator());
  27. dangerHitMap.reset(new DangerHitMapAnalyzer());
  28. buildAnalyzer.reset(new BuildAnalyzer());
  29. }
  30. Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec & tasks) const
  31. {
  32. Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float{
  33. return goal->priority;
  34. });
  35. return bestTask;
  36. }
  37. Goals::TSubgoal Nullkiller::choseBestTask(std::shared_ptr<Behavior> behavior) const
  38. {
  39. logAi->debug("Checking behavior %s", behavior->toString());
  40. auto tasks = behavior->getTasks();
  41. if(tasks.empty())
  42. {
  43. logAi->debug("Behavior %s found no tasks", behavior->toString());
  44. return Goals::sptr(Goals::Invalid());
  45. }
  46. logAi->trace("Evaluating priorities, tasks count %d", tasks.size());
  47. for(auto task : tasks)
  48. {
  49. task->setpriority(priorityEvaluator->evaluate(task));
  50. }
  51. auto task = choseBestTask(tasks);
  52. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior->toString(), task->name(), task->tile.toString(), task->priority);
  53. return task;
  54. }
  55. void Nullkiller::resetAiState()
  56. {
  57. lockedHeroes.clear();
  58. dangerHitMap->reset();
  59. }
  60. void Nullkiller::updateAiState()
  61. {
  62. activeHero = nullptr;
  63. ai->validateVisitableObjs();
  64. dangerHitMap->updateHitMap();
  65. // TODO: move to hero manager
  66. auto activeHeroes = ai->getMyHeroes();
  67. vstd::erase_if(activeHeroes, [this](const HeroPtr & hero) -> bool
  68. {
  69. auto lockedReason = getHeroLockedReason(hero.h);
  70. return lockedReason == HeroLockedReason::DEFENCE;
  71. });
  72. ai->ah->updatePaths(activeHeroes, true);
  73. ai->ah->update();
  74. buildAnalyzer->update();
  75. }
  76. bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
  77. {
  78. return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
  79. }
  80. bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
  81. {
  82. if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
  83. {
  84. #if AI_TRACE_LEVEL >= 1
  85. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  86. #endif
  87. return true;
  88. }
  89. for(auto & node : path.nodes)
  90. {
  91. auto lockReason = getHeroLockedReason(node.targetHero);
  92. if(lockReason != HeroLockedReason::NOT_LOCKED)
  93. {
  94. #if AI_TRACE_LEVEL >= 1
  95. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  96. #endif
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
  103. {
  104. auto found = lockedHeroes.find(hero);
  105. return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
  106. }
  107. void Nullkiller::makeTurn()
  108. {
  109. resetAiState();
  110. while(true)
  111. {
  112. updateAiState();
  113. Goals::TGoalVec bestTasks = {
  114. choseBestTask(std::make_shared<BuyArmyBehavior>()),
  115. choseBestTask(std::make_shared<CaptureObjectsBehavior>()),
  116. choseBestTask(std::make_shared<RecruitHeroBehavior>()),
  117. choseBestTask(std::make_shared<DefenceBehavior>()),
  118. choseBestTask(std::make_shared<BuildingBehavior>()),
  119. choseBestTask(std::make_shared<GatherArmyBehavior>())
  120. };
  121. if(cb->getDate(Date::DAY) == 1)
  122. {
  123. bestTasks.push_back(choseBestTask(std::make_shared<StartupBehavior>()));
  124. }
  125. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  126. if(bestTask->invalid())
  127. {
  128. logAi->trace("No goals found. Ending turn.");
  129. return;
  130. }
  131. if(bestTask->priority < MIN_PRIORITY)
  132. {
  133. logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->name());
  134. return;
  135. }
  136. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  137. try
  138. {
  139. if(bestTask->hero)
  140. {
  141. setActive(bestTask->hero.get(), bestTask->tile);
  142. }
  143. bestTask->accept(ai.get());
  144. }
  145. catch(goalFulfilledException &)
  146. {
  147. logAi->trace("Task %s completed", bestTask->name());
  148. }
  149. catch(std::exception & e)
  150. {
  151. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  152. logAi->debug("The error message was: %s", e.what());
  153. return;
  154. }
  155. }
  156. }