StartupBehavior.cpp 6.1 KB

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