Nullkiller.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "../Behaviors/CaptureObjectsBehavior.h"
  14. #include "../Behaviors/RecruitHeroBehavior.h"
  15. #include "../Behaviors/BuyArmyBehavior.h"
  16. #include "../Behaviors/StartupBehavior.h"
  17. #include "../Behaviors/DefenceBehavior.h"
  18. #include "../Behaviors/BuildingBehavior.h"
  19. #include "../Behaviors/GatherArmyBehavior.h"
  20. #include "../Behaviors/ClusterBehavior.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. memory.reset(new AIMemory());
  28. }
  29. void Nullkiller::init(std::shared_ptr<CCallback> cb, PlayerColor playerID)
  30. {
  31. this->cb = cb;
  32. this->playerID = playerID;
  33. priorityEvaluator.reset(new PriorityEvaluator(this));
  34. dangerHitMap.reset(new DangerHitMapAnalyzer(this));
  35. buildAnalyzer.reset(new BuildAnalyzer());
  36. objectClusterizer.reset(new ObjectClusterizer(this));
  37. dangerEvaluator.reset(new FuzzyHelper(this));
  38. pathfinder.reset(new AIPathfinder(cb.get(), this));
  39. armyManager.reset(new ArmyManager(cb.get(), this));
  40. heroManager.reset(new HeroManager(cb.get(), this));
  41. }
  42. Goals::TTask Nullkiller::choseBestTask(Goals::TTaskVec & tasks) const
  43. {
  44. Goals::TTask bestTask = *vstd::maxElementByFun(tasks, [](Goals::TTask task) -> float{
  45. return task->priority;
  46. });
  47. return bestTask;
  48. }
  49. Goals::TTask Nullkiller::choseBestTask(Goals::TSubgoal behavior) const
  50. {
  51. logAi->debug("Checking behavior %s", behavior->toString());
  52. const int MAX_DEPTH = 10;
  53. Goals::TGoalVec goals[MAX_DEPTH + 1];
  54. Goals::TTaskVec tasks;
  55. std::map<Goals::TSubgoal, Goals::TSubgoal> decompositionMap;
  56. auto start = boost::chrono::high_resolution_clock::now();
  57. goals[0] = {behavior};
  58. int depth = 0;
  59. while(goals[0].size())
  60. {
  61. TSubgoal current = goals[depth].back();
  62. #if AI_TRACE_LEVEL >= 1
  63. logAi->trace("Decomposing %s, level: %d", current->toString(), depth);
  64. #endif
  65. TGoalVec subgoals = current->decompose();
  66. #if AI_TRACE_LEVEL >= 1
  67. logAi->trace("Found %d goals", subgoals.size());
  68. #endif
  69. if(depth < MAX_DEPTH)
  70. {
  71. goals[depth + 1].clear();
  72. }
  73. for(auto subgoal : subgoals)
  74. {
  75. if(subgoal->isElementar())
  76. {
  77. auto task = taskptr(*subgoal);
  78. #if AI_TRACE_LEVEL >= 1
  79. logAi->trace("Found task %s", task->toString());
  80. #endif
  81. if(task->priority <= 0)
  82. task->priority = priorityEvaluator->evaluate(subgoal);
  83. tasks.push_back(task);
  84. }
  85. else if(depth < MAX_DEPTH)
  86. {
  87. #if AI_TRACE_LEVEL >= 1
  88. logAi->trace("Found abstract goal %s", subgoal->toString());
  89. #endif
  90. goals[depth + 1].push_back(subgoal);
  91. }
  92. }
  93. if(depth < MAX_DEPTH && goals[depth + 1].size())
  94. {
  95. depth++;
  96. }
  97. else
  98. {
  99. goals[depth].pop_back();
  100. while(depth > 0 && goals[depth].empty())
  101. {
  102. depth--;
  103. goals[depth].pop_back();
  104. }
  105. }
  106. }
  107. if(tasks.empty())
  108. {
  109. logAi->debug("Behavior %s found no tasks. Time taken %ld", behavior->toString(), timeElapsed(start));
  110. return Goals::taskptr(Goals::Invalid());
  111. }
  112. auto task = choseBestTask(tasks);
  113. logAi->debug(
  114. "Behavior %s returns %s, priority %f. Time taken %ld",
  115. behavior->toString(),
  116. task->toString(),
  117. task->priority,
  118. timeElapsed(start));
  119. return task;
  120. }
  121. void Nullkiller::resetAiState()
  122. {
  123. playerID = ai->playerID;
  124. lockedHeroes.clear();
  125. dangerHitMap->reset();
  126. }
  127. void Nullkiller::updateAiState()
  128. {
  129. auto start = boost::chrono::high_resolution_clock::now();
  130. activeHero = nullptr;
  131. memory->removeInvisibleObjects(cb.get());
  132. dangerHitMap->updateHitMap();
  133. heroManager->update();
  134. logAi->trace("Updating paths");
  135. std::map<const CGHeroInstance *, HeroRole> activeHeroes;
  136. for(auto hero : cb->getHeroesInfo())
  137. {
  138. if(getHeroLockedReason(hero) == HeroLockedReason::DEFENCE)
  139. continue;
  140. activeHeroes[hero] = heroManager->getHeroRole(hero);
  141. }
  142. pathfinder->updatePaths(activeHeroes, true);
  143. armyManager->update();
  144. objectClusterizer->clusterize();
  145. buildAnalyzer->update();
  146. logAi->debug("AI state updated in %ld", timeElapsed(start));
  147. }
  148. bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
  149. {
  150. return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
  151. }
  152. bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
  153. {
  154. if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
  155. {
  156. #if AI_TRACE_LEVEL >= 1
  157. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  158. #endif
  159. return true;
  160. }
  161. for(auto & node : path.nodes)
  162. {
  163. auto lockReason = getHeroLockedReason(node.targetHero);
  164. if(lockReason != HeroLockedReason::NOT_LOCKED)
  165. {
  166. #if AI_TRACE_LEVEL >= 1
  167. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  168. #endif
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
  175. {
  176. auto found = lockedHeroes.find(hero);
  177. return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
  178. }
  179. void Nullkiller::makeTurn()
  180. {
  181. resetAiState();
  182. while(true)
  183. {
  184. updateAiState();
  185. Goals::TTaskVec bestTasks = {
  186. choseBestTask(sptr(BuyArmyBehavior())),
  187. choseBestTask(sptr(CaptureObjectsBehavior())),
  188. choseBestTask(sptr(ClusterBehavior())),
  189. choseBestTask(sptr(RecruitHeroBehavior())),
  190. choseBestTask(sptr(DefenceBehavior())),
  191. choseBestTask(sptr(BuildingBehavior())),
  192. choseBestTask(sptr(GatherArmyBehavior()))
  193. };
  194. if(cb->getDate(Date::DAY) == 1)
  195. {
  196. bestTasks.push_back(choseBestTask(sptr(StartupBehavior())));
  197. }
  198. Goals::TTask bestTask = choseBestTask(bestTasks);
  199. if(bestTask->priority < MIN_PRIORITY)
  200. {
  201. logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->toString());
  202. return;
  203. }
  204. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->toString(), bestTask->priority);
  205. try
  206. {
  207. bestTask->accept(ai.get());
  208. }
  209. catch(goalFulfilledException &)
  210. {
  211. logAi->trace("Task %s completed", bestTask->toString());
  212. }
  213. catch(std::exception & e)
  214. {
  215. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->toString());
  216. logAi->debug("The error message was: %s", e.what());
  217. return;
  218. }
  219. }
  220. }