Nullkiller.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. goals[0] = {behavior};
  57. int depth = 0;
  58. while(goals[0].size())
  59. {
  60. TSubgoal current = goals[depth].back();
  61. #if AI_TRACE_LEVEL >= 1
  62. logAi->trace("Decomposing %s, level: %d", current->toString(), depth);
  63. #endif
  64. TGoalVec subgoals = current->decompose();
  65. #if AI_TRACE_LEVEL >= 1
  66. logAi->trace("Found %d goals", subgoals.size());
  67. #endif
  68. if(depth < MAX_DEPTH)
  69. {
  70. goals[depth + 1].clear();
  71. }
  72. for(auto subgoal : subgoals)
  73. {
  74. if(subgoal->isElementar())
  75. {
  76. auto task = taskptr(*subgoal);
  77. #if AI_TRACE_LEVEL >= 1
  78. logAi->trace("Found task %s", task->toString());
  79. #endif
  80. if(task->priority <= 0)
  81. task->priority = priorityEvaluator->evaluate(subgoal);
  82. tasks.push_back(task);
  83. }
  84. else if(depth < MAX_DEPTH)
  85. {
  86. #if AI_TRACE_LEVEL >= 1
  87. logAi->trace("Found abstract goal %s", subgoal->toString());
  88. #endif
  89. goals[depth + 1].push_back(subgoal);
  90. }
  91. }
  92. if(depth < MAX_DEPTH && goals[depth + 1].size())
  93. {
  94. depth++;
  95. }
  96. else
  97. {
  98. goals[depth].pop_back();
  99. while(depth > 0 && goals[depth].empty())
  100. {
  101. depth--;
  102. goals[depth].pop_back();
  103. }
  104. }
  105. }
  106. if(tasks.empty())
  107. {
  108. logAi->debug("Behavior %s found no tasks", behavior->toString());
  109. return Goals::taskptr(Goals::Invalid());
  110. }
  111. auto task = choseBestTask(tasks);
  112. logAi->debug("Behavior %s returns %s, priority %f", behavior->toString(), task->toString(), task->priority);
  113. return task;
  114. }
  115. void Nullkiller::resetAiState()
  116. {
  117. playerID = ai->playerID;
  118. lockedHeroes.clear();
  119. dangerHitMap->reset();
  120. }
  121. void Nullkiller::updateAiState()
  122. {
  123. activeHero = nullptr;
  124. memory->removeInvisibleObjects(cb.get());
  125. dangerHitMap->updateHitMap();
  126. auto activeHeroes = cb->getHeroesInfo();
  127. vstd::erase_if(activeHeroes, [this](const CGHeroInstance * hero) -> bool
  128. {
  129. auto lockedReason = getHeroLockedReason(hero);
  130. return lockedReason == HeroLockedReason::DEFENCE;
  131. });
  132. pathfinder->updatePaths(activeHeroes, true);
  133. heroManager->update();
  134. armyManager->update();
  135. objectClusterizer->clusterize();
  136. buildAnalyzer->update();
  137. }
  138. bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
  139. {
  140. return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
  141. }
  142. bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
  143. {
  144. if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
  145. {
  146. #if AI_TRACE_LEVEL >= 1
  147. logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
  148. #endif
  149. return true;
  150. }
  151. for(auto & node : path.nodes)
  152. {
  153. auto lockReason = getHeroLockedReason(node.targetHero);
  154. if(lockReason != HeroLockedReason::NOT_LOCKED)
  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. }
  162. return false;
  163. }
  164. HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
  165. {
  166. auto found = lockedHeroes.find(hero);
  167. return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
  168. }
  169. void Nullkiller::makeTurn()
  170. {
  171. resetAiState();
  172. while(true)
  173. {
  174. updateAiState();
  175. Goals::TTaskVec bestTasks = {
  176. choseBestTask(sptr(BuyArmyBehavior())),
  177. choseBestTask(sptr(CaptureObjectsBehavior())),
  178. choseBestTask(sptr(ClusterBehavior())),
  179. choseBestTask(sptr(RecruitHeroBehavior())),
  180. choseBestTask(sptr(DefenceBehavior())),
  181. choseBestTask(sptr(BuildingBehavior())),
  182. choseBestTask(sptr(GatherArmyBehavior()))
  183. };
  184. if(cb->getDate(Date::DAY) == 1)
  185. {
  186. bestTasks.push_back(choseBestTask(sptr(StartupBehavior())));
  187. }
  188. Goals::TTask bestTask = choseBestTask(bestTasks);
  189. /*if(bestTask->invalid())
  190. {
  191. logAi->trace("No goals found. Ending turn.");
  192. return;
  193. }*/
  194. if(bestTask->priority < MIN_PRIORITY)
  195. {
  196. logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->toString());
  197. return;
  198. }
  199. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->toString(), bestTask->priority);
  200. try
  201. {
  202. bestTask->accept(ai.get());
  203. }
  204. catch(goalFulfilledException &)
  205. {
  206. logAi->trace("Task %s completed", bestTask->toString());
  207. }
  208. catch(std::exception & e)
  209. {
  210. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->toString());
  211. logAi->debug("The error message was: %s", e.what());
  212. return;
  213. }
  214. }
  215. }