GatherArmyBehavior.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * GatherArmyBehavior.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 "../VCAI.h"
  12. #include "../Engine/Nullkiller.h"
  13. #include "../Goals/ExecuteHeroChain.h"
  14. #include "GatherArmyBehavior.h"
  15. #include "../AIUtility.h"
  16. #include "lib/mapping/CMap.h" //for victory conditions
  17. #include "lib/CPathfinder.h"
  18. extern boost::thread_specific_ptr<CCallback> cb;
  19. extern boost::thread_specific_ptr<VCAI> ai;
  20. using namespace Goals;
  21. std::string GatherArmyBehavior::toString() const
  22. {
  23. return "Gather army";
  24. }
  25. Goals::TGoalVec GatherArmyBehavior::decompose() const
  26. {
  27. Goals::TGoalVec tasks;
  28. auto heroes = cb->getHeroesInfo();
  29. if(heroes.empty())
  30. {
  31. return tasks;
  32. }
  33. for(const CGHeroInstance * hero : heroes)
  34. {
  35. if(ai->nullkiller->heroManager->getHeroRole(hero) == HeroRole::MAIN
  36. && hero->getArmyStrength() >= 300)
  37. {
  38. vstd::concatenate(tasks, deliverArmyToHero(hero));
  39. }
  40. }
  41. auto towns = cb->getTownsInfo();
  42. for(const CGTownInstance * town : towns)
  43. {
  44. vstd::concatenate(tasks, upgradeArmy(town));
  45. }
  46. return tasks;
  47. }
  48. Goals::TGoalVec GatherArmyBehavior::deliverArmyToHero(const CGHeroInstance * hero) const
  49. {
  50. Goals::TGoalVec tasks;
  51. const int3 pos = hero->visitablePos();
  52. #if AI_TRACE_LEVEL >= 1
  53. logAi->trace("Checking ways to gaher army for hero %s, %s", hero->getObjectName(), pos.toString());
  54. #endif
  55. if(ai->nullkiller->isHeroLocked(hero))
  56. {
  57. #if AI_TRACE_LEVEL >= 1
  58. logAi->trace("Skipping locked hero %s, %s", hero->getObjectName(), pos.toString());
  59. #endif
  60. return tasks;
  61. }
  62. auto paths = ai->nullkiller->pathfinder->getPathInfo(pos);
  63. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  64. #if AI_TRACE_LEVEL >= 1
  65. logAi->trace("Found %d paths", paths.size());
  66. #endif
  67. for(const AIPath & path : paths)
  68. {
  69. #if AI_TRACE_LEVEL >= 2
  70. logAi->trace("Path found %s", path.toString());
  71. #endif
  72. if(path.containsHero(hero)) continue;
  73. if(path.getFirstBlockedAction())
  74. {
  75. #if AI_TRACE_LEVEL >= 2
  76. // TODO: decomposition
  77. logAi->trace("Ignore path. Action is blocked.");
  78. #endif
  79. continue;
  80. }
  81. if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
  82. {
  83. #if AI_TRACE_LEVEL >= 2
  84. logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
  85. #endif
  86. continue;
  87. }
  88. float armyValue = (float)ai->nullkiller->armyManager->howManyReinforcementsCanGet(hero, path.heroArmy) / hero->getArmyStrength();
  89. // avoid transferring very small amount of army
  90. if(armyValue < 0.1f)
  91. continue;
  92. // avoid trying to move bigger army to the weaker one.
  93. if(armyValue > 0.5f)
  94. continue;
  95. auto danger = path.getTotalDanger();
  96. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
  97. #if AI_TRACE_LEVEL >= 2
  98. logAi->trace(
  99. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  100. isSafe ? "safe" : "not safe",
  101. hero->name,
  102. path.targetHero->name,
  103. path.getHeroStrength(),
  104. danger,
  105. path.getTotalArmyLoss());
  106. #endif
  107. if(isSafe)
  108. {
  109. auto newWay = std::make_shared<ExecuteHeroChain>(path, hero);
  110. newWay->strategicalValue = armyValue;
  111. waysToVisitObj.push_back(newWay);
  112. }
  113. }
  114. if(waysToVisitObj.empty())
  115. return tasks;
  116. for(auto way : waysToVisitObj)
  117. {
  118. if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
  119. continue;
  120. way->closestWayRatio = 1;
  121. tasks.push_back(sptr(*way));
  122. }
  123. return tasks;
  124. }
  125. Goals::TGoalVec GatherArmyBehavior::upgradeArmy(const CGTownInstance * upgrader) const
  126. {
  127. Goals::TGoalVec tasks;
  128. const int3 pos = upgrader->visitablePos();
  129. TResources availableResources = cb->getResourceAmount();
  130. #if AI_TRACE_LEVEL >= 1
  131. logAi->trace("Checking ways to upgrade army in town %s, %s", upgrader->getObjectName(), pos.toString());
  132. #endif
  133. auto paths = ai->nullkiller->pathfinder->getPathInfo(pos);
  134. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  135. #if AI_TRACE_LEVEL >= 1
  136. logAi->trace("Found %d paths", paths.size());
  137. #endif
  138. for(const AIPath & path : paths)
  139. {
  140. #if AI_TRACE_LEVEL >= 2
  141. logAi->trace("Path found %s", path.toString());
  142. #endif
  143. if(upgrader->visitingHero != path.targetHero)
  144. {
  145. #if AI_TRACE_LEVEL >= 2
  146. logAi->trace("Ignore path. Town has visiting hero.");
  147. #endif
  148. continue;
  149. }
  150. if(path.getFirstBlockedAction())
  151. {
  152. #if AI_TRACE_LEVEL >= 2
  153. // TODO: decomposition?
  154. logAi->trace("Ignore path. Action is blocked.");
  155. #endif
  156. continue;
  157. }
  158. if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
  159. {
  160. #if AI_TRACE_LEVEL >= 2
  161. logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
  162. #endif
  163. continue;
  164. }
  165. auto upgrade = ai->nullkiller->armyManager->calculateCreateresUpgrade(path.heroArmy, upgrader, availableResources);
  166. auto armyValue = (float)upgrade.upgradeValue / path.getHeroStrength();
  167. if(armyValue < 0.1f || upgrade.upgradeValue < 300) // avoid small upgrades
  168. continue;
  169. auto danger = path.getTotalDanger();
  170. auto isSafe = isSafeToVisit(path.targetHero, path.heroArmy, danger);
  171. #if AI_TRACE_LEVEL >= 2
  172. logAi->trace(
  173. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  174. isSafe ? "safe" : "not safe",
  175. upgrader->getObjectName(),
  176. path.targetHero->name,
  177. path.getHeroStrength(),
  178. danger,
  179. path.getTotalArmyLoss());
  180. #endif
  181. if(isSafe)
  182. {
  183. auto newWay = std::make_shared<ExecuteHeroChain>(path, upgrader);
  184. newWay->strategicalValue = armyValue;
  185. newWay->goldCost = upgrade.upgradeCost[Res::GOLD];
  186. waysToVisitObj.push_back(newWay);
  187. }
  188. }
  189. if(waysToVisitObj.empty())
  190. return tasks;
  191. for(auto way : waysToVisitObj)
  192. {
  193. if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
  194. continue;
  195. way->closestWayRatio = 1;
  196. tasks.push_back(sptr(*way));
  197. }
  198. return tasks;
  199. }