GatherArmyBehavior.cpp 6.4 KB

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