GatherArmyBehavior.cpp 5.7 KB

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