GatherArmyBehavior.cpp 9.4 KB

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