StartupBehavior.cpp 4.7 KB

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