Nullkiller.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. using namespace Goals;
  25. Nullkiller::Nullkiller()
  26. {
  27. priorityEvaluator.reset(new PriorityEvaluator());
  28. dangerHitMap.reset(new DangerHitMapAnalyzer());
  29. buildAnalyzer.reset(new BuildAnalyzer());
  30. }
  31. Goals::TTask Nullkiller::choseBestTask(Goals::TTaskVec & tasks) const
  32. {
  33. Goals::TTask bestTask = *vstd::maxElementByFun(tasks, [](Goals::TTask task) -> float{
  34. return task->priority;
  35. });
  36. return bestTask;
  37. }
  38. Goals::TTask Nullkiller::choseBestTask(Goals::TSubgoal behavior) const
  39. {
  40. logAi->debug("Checking behavior %s", behavior->toString());
  41. const int MAX_DEPTH = 10;
  42. Goals::TGoalVec goals[MAX_DEPTH + 1];
  43. Goals::TTaskVec tasks;
  44. goals[0] = {behavior};
  45. int depth = 0;
  46. while(goals[0].size())
  47. {
  48. TSubgoal current = goals[depth].back();
  49. #if AI_TRACE_LEVEL >= 1
  50. logAi->trace("Decomposing %s, level: %d", current->toString(), depth);
  51. #endif
  52. TGoalVec subgoals = current->decompose();
  53. #if AI_TRACE_LEVEL >= 1
  54. logAi->trace("Found %d goals", subgoals.size());
  55. #endif
  56. goals[depth + 1].clear();
  57. for(auto subgoal : subgoals)
  58. {
  59. if(subgoal->isElementar())
  60. {
  61. auto task = taskptr(*subgoal);
  62. #if AI_TRACE_LEVEL >= 1
  63. logAi->trace("Found task %s", task->toString());
  64. #endif
  65. if(task->priority <= 0)
  66. task->priority = priorityEvaluator->evaluate(subgoal);
  67. tasks.push_back(task);
  68. }
  69. else
  70. {
  71. #if AI_TRACE_LEVEL >= 1
  72. logAi->trace("Found abstract goal %s", subgoal->toString());
  73. #endif
  74. goals[depth + 1].push_back(subgoal);
  75. }
  76. }
  77. if(goals[depth + 1].size() && depth < MAX_DEPTH)
  78. {
  79. depth++;
  80. }
  81. else
  82. {
  83. goals[depth].pop_back();
  84. while(depth > 0 && goals[depth].empty())
  85. {
  86. depth--;
  87. goals[depth].pop_back();
  88. }
  89. }
  90. }
  91. if(tasks.empty())
  92. {
  93. logAi->debug("Behavior %s found no tasks", behavior->toString());
  94. return Goals::taskptr(Goals::Invalid());
  95. }
  96. auto task = choseBestTask(tasks);
  97. logAi->debug("Behavior %s returns %s, priority %f", behavior->toString(), task->toString(), task->priority);
  98. return task;
  99. }
  100. void Nullkiller::resetAiState()
  101. {
  102. lockedHeroes.clear();
  103. dangerHitMap->reset();
  104. }
  105. void Nullkiller::updateAiState()
  106. {
  107. activeHero = nullptr;
  108. ai->validateVisitableObjs();
  109. dangerHitMap->updateHitMap();
  110. // TODO: move to hero manager
  111. auto activeHeroes = ai->getMyHeroes();
  112. vstd::erase_if(activeHeroes, [this](const HeroPtr & hero) -> bool
  113. {
  114. auto lockedReason = getHeroLockedReason(hero.h);
  115. return lockedReason == HeroLockedReason::DEFENCE;
  116. });
  117. ai->ah->updatePaths(activeHeroes, true);
  118. ai->ah->update();
  119. buildAnalyzer->update();
  120. }
  121. bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
  122. {
  123. return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
  124. }
  125. bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
  126. {
  127. if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
  128. {
  129. #if AI_TRACE_LEVEL >= 1
  130. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  131. #endif
  132. return true;
  133. }
  134. for(auto & node : path.nodes)
  135. {
  136. auto lockReason = getHeroLockedReason(node.targetHero);
  137. if(lockReason != HeroLockedReason::NOT_LOCKED)
  138. {
  139. #if AI_TRACE_LEVEL >= 1
  140. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  141. #endif
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
  148. {
  149. auto found = lockedHeroes.find(hero);
  150. return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
  151. }
  152. void Nullkiller::makeTurn()
  153. {
  154. resetAiState();
  155. while(true)
  156. {
  157. updateAiState();
  158. Goals::TTaskVec bestTasks = {
  159. choseBestTask(sptr(BuyArmyBehavior())),
  160. choseBestTask(sptr(CaptureObjectsBehavior())),
  161. choseBestTask(sptr(RecruitHeroBehavior())),
  162. choseBestTask(sptr(DefenceBehavior())),
  163. choseBestTask(sptr(BuildingBehavior())),
  164. choseBestTask(sptr(GatherArmyBehavior()))
  165. };
  166. if(cb->getDate(Date::DAY) == 1)
  167. {
  168. bestTasks.push_back(choseBestTask(sptr(StartupBehavior())));
  169. }
  170. Goals::TTask bestTask = choseBestTask(bestTasks);
  171. /*if(bestTask->invalid())
  172. {
  173. logAi->trace("No goals found. Ending turn.");
  174. return;
  175. }*/
  176. if(bestTask->priority < MIN_PRIORITY)
  177. {
  178. logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->toString());
  179. return;
  180. }
  181. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->toString(), bestTask->priority);
  182. try
  183. {
  184. bestTask->accept(ai.get());
  185. }
  186. catch(goalFulfilledException &)
  187. {
  188. logAi->trace("Task %s completed", bestTask->toString());
  189. }
  190. catch(std::exception & e)
  191. {
  192. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->toString());
  193. logAi->debug("The error message was: %s", e.what());
  194. return;
  195. }
  196. }
  197. }