StartupBehavior.cpp 4.9 KB

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