GatherArmyBehavior.cpp 6.1 KB

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