GatherArmyBehavior.cpp 7.3 KB

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