2
0

GatherArmyBehavior.cpp 9.8 KB

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