GatherArmyBehavior.cpp 9.0 KB

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