GatherArmyBehavior.cpp 7.3 KB

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