GatherArmyBehavior.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "../Goals/RecruitHero.h"
  16. #include "../Markers/HeroExchange.h"
  17. #include "../Markers/ArmyUpgrade.h"
  18. #include "GatherArmyBehavior.h"
  19. #include "CaptureObjectsBehavior.h"
  20. #include "../AIUtility.h"
  21. #include "../Goals/ExchangeSwapTownHeroes.h"
  22. namespace NKAI
  23. {
  24. using namespace Goals;
  25. std::string GatherArmyBehavior::toString() const
  26. {
  27. return "Gather army";
  28. }
  29. Goals::TGoalVec GatherArmyBehavior::decompose(const Nullkiller * ai) const
  30. {
  31. Goals::TGoalVec tasks;
  32. auto heroes = ai->cb->getHeroesInfo();
  33. if(heroes.empty())
  34. {
  35. return tasks;
  36. }
  37. for(const CGHeroInstance * hero : heroes)
  38. {
  39. if(ai->heroManager->getHeroRole(hero) == HeroRole::MAIN)
  40. {
  41. vstd::concatenate(tasks, deliverArmyToHero(ai, hero));
  42. }
  43. }
  44. auto towns = ai->cb->getTownsInfo();
  45. for(const CGTownInstance * town : towns)
  46. {
  47. vstd::concatenate(tasks, upgradeArmy(ai, town));
  48. }
  49. return tasks;
  50. }
  51. Goals::TGoalVec GatherArmyBehavior::deliverArmyToHero(const Nullkiller * ai, const CGHeroInstance * hero) const
  52. {
  53. Goals::TGoalVec tasks;
  54. const int3 pos = hero->visitablePos();
  55. auto targetHeroScore = ai->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->pathfinder->getPathInfo(pos, ai->isObjectGraphAllowed());
  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, %s, %lld", path.toString(), path.targetHero->getObjectName(), path.heroArmy->getArmyStrength());
  67. #endif
  68. if (path.targetHero->getOwner() != ai->playerID)
  69. continue;
  70. if(path.containsHero(hero))
  71. {
  72. #if NKAI_TRACE_LEVEL >= 2
  73. logAi->trace("Selfcontaining path. Ignore");
  74. #endif
  75. continue;
  76. }
  77. if(ai->arePathHeroesLocked(path))
  78. {
  79. #if NKAI_TRACE_LEVEL >= 2
  80. logAi->trace("Ignore path because of locked hero");
  81. #endif
  82. continue;
  83. }
  84. HeroExchange heroExchange(hero, path);
  85. uint64_t armyValue = heroExchange.getReinforcementArmyStrength(ai);
  86. float armyRatio = (float)armyValue / hero->getArmyStrength();
  87. // avoid transferring very small amount of army
  88. if((armyRatio < 0.1f && armyValue < 20000) || armyValue < 500)
  89. {
  90. #if NKAI_TRACE_LEVEL >= 2
  91. logAi->trace("Army value is too small.");
  92. #endif
  93. continue;
  94. }
  95. // avoid trying to move bigger army to the weaker one.
  96. bool hasOtherMainInPath = false;
  97. for(auto node : path.nodes)
  98. {
  99. if(!node.targetHero) continue;
  100. auto heroRole = ai->heroManager->getHeroRole(node.targetHero);
  101. if(heroRole == HeroRole::MAIN)
  102. {
  103. auto score = ai->heroManager->evaluateHero(node.targetHero);
  104. if(score >= targetHeroScore)
  105. {
  106. hasOtherMainInPath = true;
  107. break;
  108. }
  109. }
  110. }
  111. if(hasOtherMainInPath)
  112. {
  113. #if NKAI_TRACE_LEVEL >= 2
  114. logAi->trace("Army value is too large.");
  115. #endif
  116. continue;
  117. }
  118. auto danger = path.getTotalDanger();
  119. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger, ai->settings->getSafeAttackRatio());
  120. #if NKAI_TRACE_LEVEL >= 2
  121. logAi->trace(
  122. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  123. isSafe ? "safe" : "not safe",
  124. hero->getObjectName(),
  125. path.targetHero->getObjectName(),
  126. path.getHeroStrength(),
  127. danger,
  128. path.getTotalArmyLoss());
  129. #endif
  130. if(isSafe)
  131. {
  132. Composition composition;
  133. ExecuteHeroChain exchangePath(path, hero);
  134. exchangePath.closestWayRatio = 1;
  135. composition.addNext(heroExchange);
  136. if(hero->inTownGarrison && path.turn() == 0)
  137. {
  138. auto lockReason = ai->getHeroLockedReason(hero);
  139. if(path.targetHero->visitedTown == hero->visitedTown)
  140. {
  141. composition.addNextSequence({
  142. sptr(ExchangeSwapTownHeroes(hero->visitedTown, hero, lockReason))});
  143. }
  144. else
  145. {
  146. composition.addNextSequence({
  147. sptr(ExchangeSwapTownHeroes(hero->visitedTown)),
  148. sptr(exchangePath),
  149. sptr(ExchangeSwapTownHeroes(hero->visitedTown, hero, lockReason))});
  150. }
  151. }
  152. else
  153. {
  154. composition.addNext(exchangePath);
  155. }
  156. auto blockedAction = path.getFirstBlockedAction();
  157. if(blockedAction)
  158. {
  159. #if NKAI_TRACE_LEVEL >= 2
  160. logAi->trace("Action is blocked. Considering decomposition.");
  161. #endif
  162. auto subGoal = blockedAction->decompose(ai, path.targetHero);
  163. if(subGoal->invalid())
  164. {
  165. #if NKAI_TRACE_LEVEL >= 1
  166. logAi->trace("Path is invalid. Skipping");
  167. #endif
  168. continue;
  169. }
  170. composition.addNext(subGoal);
  171. }
  172. tasks.push_back(sptr(composition));
  173. }
  174. }
  175. return tasks;
  176. }
  177. Goals::TGoalVec GatherArmyBehavior::upgradeArmy(const Nullkiller * ai, const CGTownInstance * upgrader) const
  178. {
  179. Goals::TGoalVec tasks;
  180. const int3 pos = upgrader->visitablePos();
  181. TResources availableResources = ai->getFreeResources();
  182. #if NKAI_TRACE_LEVEL >= 1
  183. logAi->trace("Checking ways to upgrade army in town %s, %s", upgrader->getObjectName(), pos.toString());
  184. #endif
  185. auto paths = ai->pathfinder->getPathInfo(pos, ai->isObjectGraphAllowed());
  186. auto goals = CaptureObjectsBehavior::getVisitGoals(paths, ai);
  187. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  188. #if NKAI_TRACE_LEVEL >= 1
  189. logAi->trace("Found %d paths", paths.size());
  190. #endif
  191. bool hasMainAround = false;
  192. for(const AIPath & path : paths)
  193. {
  194. auto heroRole = ai->heroManager->getHeroRole(path.targetHero);
  195. if(heroRole == HeroRole::MAIN && path.turn() < ai->settings->getScoutHeroTurnDistanceLimit())
  196. hasMainAround = true;
  197. }
  198. for(int i = 0; i < paths.size(); i++)
  199. {
  200. auto & path = paths[i];
  201. auto visitGoal = goals[i];
  202. #if NKAI_TRACE_LEVEL >= 2
  203. logAi->trace("Path found %s, %s, %lld", path.toString(), path.targetHero->getObjectName(), path.heroArmy->getArmyStrength());
  204. #endif
  205. if(visitGoal->invalid())
  206. {
  207. #if NKAI_TRACE_LEVEL >= 2
  208. logAi->trace("Ignore path. Not valid way.");
  209. #endif
  210. continue;
  211. }
  212. if(upgrader->visitingHero && (upgrader->visitingHero.get() != path.targetHero || path.exchangeCount == 1))
  213. {
  214. #if NKAI_TRACE_LEVEL >= 2
  215. logAi->trace("Ignore path. Town has visiting hero.");
  216. #endif
  217. continue;
  218. }
  219. if(ai->arePathHeroesLocked(path))
  220. {
  221. #if NKAI_TRACE_LEVEL >= 2
  222. logAi->trace("Ignore path because of locked hero");
  223. #endif
  224. continue;
  225. }
  226. if(path.getFirstBlockedAction())
  227. {
  228. #if NKAI_TRACE_LEVEL >= 2
  229. // TODO: decomposition?
  230. logAi->trace("Ignore path. Action is blocked.");
  231. #endif
  232. continue;
  233. }
  234. auto upgrade = ai->armyManager->calculateCreaturesUpgrade(path.heroArmy, upgrader, availableResources);
  235. if(!upgrader->garrisonHero
  236. && (
  237. hasMainAround
  238. || ai->heroManager->getHeroRole(path.targetHero) == HeroRole::MAIN))
  239. {
  240. ArmyUpgradeInfo armyToGetOrBuy;
  241. armyToGetOrBuy.addArmyToGet(
  242. ai->armyManager->getBestArmy(
  243. path.targetHero,
  244. path.heroArmy,
  245. upgrader->getUpperArmy(),
  246. TerrainId::NONE));
  247. armyToGetOrBuy.upgradeValue -= path.heroArmy->getArmyStrength();
  248. upgrade.upgradeValue += armyToGetOrBuy.upgradeValue;
  249. upgrade.upgradeCost += armyToGetOrBuy.upgradeCost;
  250. vstd::concatenate(upgrade.resultingArmy, armyToGetOrBuy.resultingArmy);
  251. if(!upgrade.upgradeValue
  252. && armyToGetOrBuy.upgradeValue > 20000
  253. && ai->heroManager->canRecruitHero(upgrader)
  254. && path.turn() < ai->settings->getScoutHeroTurnDistanceLimit())
  255. {
  256. for(auto hero : cb->getAvailableHeroes(upgrader))
  257. {
  258. auto scoutReinforcement = ai->armyManager->howManyReinforcementsCanGet(hero, upgrader);
  259. if(scoutReinforcement >= armyToGetOrBuy.upgradeValue
  260. && ai->getFreeGold() >20000
  261. && !ai->buildAnalyzer->isGoldPressureHigh())
  262. {
  263. Composition recruitHero;
  264. recruitHero.addNext(ArmyUpgrade(path.targetHero, town, armyToGetOrBuy)).addNext(RecruitHero(upgrader, hero));
  265. }
  266. }
  267. }
  268. }
  269. auto armyValue = (float)upgrade.upgradeValue / path.getHeroStrength();
  270. if((armyValue < 0.25f && upgrade.upgradeValue < 40000) || upgrade.upgradeValue < 2000) // avoid small upgrades
  271. {
  272. #if NKAI_TRACE_LEVEL >= 2
  273. logAi->trace("Ignore path. Army value is too small (%f)", armyValue);
  274. #endif
  275. continue;
  276. }
  277. auto danger = path.getTotalDanger();
  278. auto isSafe = isSafeToVisit(path.targetHero, path.heroArmy, danger, ai->settings->getSafeAttackRatio());
  279. #if NKAI_TRACE_LEVEL >= 2
  280. logAi->trace(
  281. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  282. isSafe ? "safe" : "not safe",
  283. upgrader->getObjectName(),
  284. path.targetHero->getObjectName(),
  285. path.getHeroStrength(),
  286. danger,
  287. path.getTotalArmyLoss());
  288. #endif
  289. if(isSafe)
  290. {
  291. tasks.push_back(sptr(Composition().addNext(ArmyUpgrade(path, upgrader, upgrade)).addNext(visitGoal)));
  292. }
  293. }
  294. return tasks;
  295. }
  296. }