StartupBehavior.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * StartupBehavior.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 "StartupBehavior.h"
  12. #include "../VCAI.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/BuildThis.h"
  15. #include "../Goals/RecruitHero.h"
  16. #include "../Goals/ExecuteHeroChain.h"
  17. #include "../Goals/ExchangeSwapTownHeroes.h"
  18. #include "lib/mapping/CMap.h" //for victory conditions
  19. #include "lib/mapObjects/MapObjects.h" //for victory conditions
  20. #include "lib/CPathfinder.h"
  21. #include "../Engine/Nullkiller.h"
  22. extern boost::thread_specific_ptr<CCallback> cb;
  23. extern boost::thread_specific_ptr<VCAI> ai;
  24. using namespace Goals;
  25. std::string StartupBehavior::toString() const
  26. {
  27. return "Startup";
  28. }
  29. const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
  30. {
  31. auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
  32. {
  33. if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
  34. return 1;
  35. return path.movementCost();
  36. });
  37. return shortestPath;
  38. }
  39. const CGHeroInstance * getNearestHero(const CGTownInstance * town)
  40. {
  41. auto paths = ai->nullkiller->pathfinder->getPathInfo(town->visitablePos());
  42. if(paths.empty())
  43. return nullptr;
  44. auto shortestPath = getShortestPath(town, paths);
  45. if(shortestPath.nodes.size() > 1
  46. || shortestPath.turn() != 0
  47. || shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
  48. || town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get())
  49. return nullptr;
  50. return shortestPath.targetHero;
  51. }
  52. bool needToRecruitHero(const CGTownInstance * startupTown)
  53. {
  54. if(!ai->canRecruitAnyHero(startupTown))
  55. return false;
  56. if(!startupTown->garrisonHero && !startupTown->visitingHero)
  57. return false;
  58. auto heroToCheck = startupTown->garrisonHero ? startupTown->garrisonHero.get() : startupTown->visitingHero.get();
  59. auto paths = cb->getPathsInfo(heroToCheck);
  60. int treasureSourcesCount = 0;
  61. for(auto obj : ai->nullkiller->objectClusterizer->getNearbyObjects())
  62. {
  63. if(obj->ID == Obj::RESOURCE && obj->subID == Res::GOLD
  64. || obj->ID == Obj::TREASURE_CHEST
  65. || obj->ID == Obj::CAMPFIRE
  66. || obj->ID == Obj::WATER_WHEEL)
  67. {
  68. auto path = paths->getPathInfo(obj->visitablePos());
  69. if((path->accessible == CGPathNode::BLOCKVIS || path->accessible == CGPathNode::VISIT)
  70. && path->reachable())
  71. {
  72. treasureSourcesCount++;
  73. }
  74. }
  75. }
  76. auto basicCount = cb->getTownsInfo().size() + 2;
  77. auto boost = (int)std::floor(std::pow(treasureSourcesCount / 2.0, 2));
  78. logAi->trace("Startup allows %d+%d heroes", basicCount, boost);
  79. return cb->getHeroCount(ai->playerID, true) < basicCount + boost;
  80. }
  81. Goals::TGoalVec StartupBehavior::decompose() const
  82. {
  83. Goals::TGoalVec tasks;
  84. auto towns = cb->getTownsInfo();
  85. if(!towns.size())
  86. return tasks;
  87. const CGTownInstance * startupTown = towns.front();
  88. if(towns.size() > 1)
  89. {
  90. startupTown = *vstd::maxElementByFun(towns, [](const CGTownInstance * town) -> float
  91. {
  92. if(town->garrisonHero)
  93. return ai->nullkiller->heroManager->evaluateHero(town->garrisonHero.get());
  94. auto closestHero = getNearestHero(town);
  95. if(closestHero)
  96. return ai->nullkiller->heroManager->evaluateHero(closestHero);
  97. return 0;
  98. });
  99. }
  100. if(!startupTown->hasBuilt(BuildingID::TAVERN)
  101. && cb->canBuildStructure(startupTown, BuildingID::TAVERN) == EBuildingState::ALLOWED)
  102. {
  103. tasks.push_back(Goals::sptr(Goals::BuildThis(BuildingID::TAVERN, startupTown).setpriority(100)));
  104. return tasks;
  105. }
  106. bool canRecruitHero = needToRecruitHero(startupTown);
  107. auto closestHero = getNearestHero(startupTown);
  108. if(closestHero)
  109. {
  110. if(!startupTown->visitingHero)
  111. {
  112. if(ai->nullkiller->armyManager->howManyReinforcementsCanGet(startupTown->getUpperArmy(), closestHero) > 200)
  113. {
  114. auto paths = ai->nullkiller->pathfinder->getPathInfo(startupTown->visitablePos());
  115. if(paths.size())
  116. {
  117. auto path = getShortestPath(startupTown, paths);
  118. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
  119. }
  120. }
  121. }
  122. else
  123. {
  124. auto visitingHero = startupTown->visitingHero.get();
  125. auto visitingHeroScore = ai->nullkiller->heroManager->evaluateHero(visitingHero);
  126. if(startupTown->garrisonHero)
  127. {
  128. auto garrisonHero = startupTown->garrisonHero.get();
  129. auto garrisonHeroScore = ai->nullkiller->heroManager->evaluateHero(garrisonHero);
  130. if(visitingHeroScore > garrisonHeroScore
  131. || ai->nullkiller->heroManager->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->nullkiller->heroManager->getHeroRole(visitingHero) == HeroRole::MAIN)
  132. {
  133. if(canRecruitHero || ai->nullkiller->armyManager->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
  134. {
  135. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
  136. }
  137. }
  138. else if(ai->nullkiller->armyManager->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
  139. {
  140. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero, HeroLockedReason::STARTUP).setpriority(100)));
  141. }
  142. }
  143. else if(canRecruitHero)
  144. {
  145. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
  146. }
  147. }
  148. }
  149. if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
  150. {
  151. tasks.push_back(Goals::sptr(Goals::RecruitHero(startupTown)));
  152. }
  153. if(tasks.empty() && !startupTown->visitingHero)
  154. {
  155. for(auto town : towns)
  156. {
  157. if(!town->visitingHero && needToRecruitHero(town))
  158. {
  159. tasks.push_back(Goals::sptr(Goals::RecruitHero(town)));
  160. break;
  161. }
  162. }
  163. }
  164. if(tasks.empty() && towns.size())
  165. {
  166. for(const CGTownInstance * town : towns)
  167. {
  168. if(town->garrisonHero
  169. && town->garrisonHero->movement
  170. && !town->visitingHero
  171. && ai->nullkiller->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE)
  172. {
  173. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(MIN_PRIORITY)));
  174. }
  175. }
  176. }
  177. return tasks;
  178. }