StartupBehavior.cpp 6.1 KB

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