GatherArmyBehavior.cpp 7.3 KB

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