StartupBehavior.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "../AIhelper.h"
  14. #include "../AIUtility.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/CPathfinder.h"
  20. extern boost::thread_specific_ptr<CCallback> cb;
  21. extern boost::thread_specific_ptr<VCAI> ai;
  22. extern FuzzyHelper * fh;
  23. using namespace Goals;
  24. std::string StartupBehavior::toString() const
  25. {
  26. return "Startup";
  27. }
  28. const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
  29. {
  30. auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
  31. {
  32. if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
  33. return 1;
  34. return path.movementCost();
  35. });
  36. return shortestPath;
  37. }
  38. const CGHeroInstance * getNearestHero(const CGTownInstance * town)
  39. {
  40. auto paths = ai->ah->getPathsToTile(town->visitablePos());
  41. if(paths.empty())
  42. return nullptr;
  43. auto shortestPath = getShortestPath(town, paths);
  44. if(shortestPath.nodes.size() > 1
  45. || shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
  46. || town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get())
  47. return nullptr;
  48. return shortestPath.targetHero;
  49. }
  50. bool needToRecruitHero(const CGTownInstance * startupTown)
  51. {
  52. if(!ai->canRecruitAnyHero(startupTown))
  53. return false;
  54. if(!startupTown->garrisonHero && !startupTown->visitingHero)
  55. return false;
  56. auto heroToCheck = startupTown->garrisonHero ? startupTown->garrisonHero.get() : startupTown->visitingHero.get();
  57. auto paths = cb->getPathsInfo(heroToCheck);
  58. for(auto obj : ai->visitableObjs)
  59. {
  60. if(obj->ID == Obj::RESOURCE && obj->subID == Res::GOLD
  61. || obj->ID == Obj::TREASURE_CHEST
  62. || obj->ID == Obj::CAMPFIRE
  63. || obj->ID == Obj::WATER_WHEEL)
  64. {
  65. auto path = paths->getPathInfo(obj->visitablePos());
  66. if(path->accessible == CGPathNode::BLOCKVIS && path->turns != 255)
  67. {
  68. return true;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. Goals::TGoalVec StartupBehavior::getTasks()
  75. {
  76. Goals::TGoalVec tasks;
  77. auto towns = cb->getTownsInfo();
  78. if(!towns.size())
  79. return tasks;
  80. const CGTownInstance * startupTown = towns.front();
  81. bool canRecruitHero = needToRecruitHero(startupTown);
  82. if(towns.size() > 1)
  83. {
  84. startupTown = *vstd::maxElementByFun(towns, [](const CGTownInstance * town) -> float
  85. {
  86. auto closestHero = getNearestHero(town);
  87. if(!closestHero)
  88. return 0;
  89. return ai->ah->evaluateHero(closestHero);
  90. });
  91. }
  92. auto closestHero = getNearestHero(startupTown);
  93. if(closestHero)
  94. {
  95. if(!startupTown->visitingHero)
  96. {
  97. if(ai->ah->howManyReinforcementsCanGet(startupTown->getUpperArmy(), closestHero) > 200)
  98. {
  99. auto paths = ai->ah->getPathsToTile(startupTown->visitablePos());
  100. if(paths.size())
  101. {
  102. auto path = getShortestPath(startupTown, paths);
  103. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
  104. }
  105. }
  106. }
  107. else
  108. {
  109. auto visitingHero = startupTown->visitingHero.get();
  110. auto visitingHeroScore = ai->ah->evaluateHero(visitingHero);
  111. if(startupTown->garrisonHero)
  112. {
  113. auto garrisonHero = startupTown->garrisonHero.get();
  114. auto garrisonHeroScore = ai->ah->evaluateHero(garrisonHero);
  115. if(visitingHeroScore > garrisonHeroScore
  116. || ai->ah->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->ah->getHeroRole(visitingHero) == HeroRole::MAIN)
  117. {
  118. if(canRecruitHero || ai->ah->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
  119. {
  120. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero).setpriority(100)));
  121. }
  122. }
  123. else if(ai->ah->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
  124. {
  125. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero).setpriority(100)));
  126. }
  127. }
  128. else if(canRecruitHero)
  129. {
  130. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero).setpriority(100)));
  131. }
  132. }
  133. }
  134. if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
  135. {
  136. tasks.push_back(Goals::sptr(Goals::RecruitHero()));
  137. }
  138. if(tasks.empty() && towns.size())
  139. {
  140. for(const CGTownInstance * town : towns)
  141. {
  142. if(town->garrisonHero && town->garrisonHero->movement)
  143. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(0.0001f)));
  144. }
  145. }
  146. return tasks;
  147. }