2
0

RecruitHeroBehavior.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * RecruitHeroBehavior.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 "RecruitHeroBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/ExecuteHeroChain.h"
  15. #include "../Goals/RecruitHero.h"
  16. #include <algorithm>
  17. namespace NK2AI
  18. {
  19. using namespace Goals;
  20. std::string RecruitHeroBehavior::toString() const
  21. {
  22. return "Recruit hero";
  23. }
  24. Goals::TGoalVec RecruitHeroBehavior::decompose(const Nullkiller * aiNk) const
  25. {
  26. Goals::TGoalVec tasks;
  27. const auto ourTowns = aiNk->cc->getTownsInfo();
  28. const auto ourHeroes = aiNk->cc->getHeroesInfo();
  29. constexpr RecruitHeroChoice bestChoice;
  30. bool haveCapitol = false;
  31. int treasureSourcesCount = 0;
  32. // Simplification: Moved this call before getting into the decomposer
  33. // aiNk->dangerHitMap->updateHitMap();
  34. for(const auto * town : ourTowns)
  35. {
  36. // TODO: Mircea: Should consider removing it if necessary
  37. // What if under threat and there is a stronger hero available?
  38. if(town->getVisitingHero() && town->getGarrisonHero())
  39. continue;
  40. uint8_t closestThreatTurn = UINT8_MAX;
  41. for(const auto & threat : aiNk->dangerHitMap->getTownThreats(town))
  42. {
  43. closestThreatTurn = std::min(closestThreatTurn, threat.turn);
  44. }
  45. float visitabilityRatio = 0;
  46. for(const auto * const hero : ourHeroes)
  47. {
  48. if(aiNk->dangerHitMap->getClosestTown(hero->visitablePos()) == town)
  49. visitabilityRatio += 1.0f / ourHeroes.size();
  50. }
  51. // TODO: Mircea: Should check even if hero cap is reached because it should replace heroes if a stronger one appears
  52. if(aiNk->heroManager->canRecruitHero(town))
  53. {
  54. calculateTreasureSources(aiNk->objectClusterizer->getNearbyObjects(), aiNk->playerID, *aiNk->dangerHitMap, treasureSourcesCount, *town);
  55. calculateBestHero(aiNk->cc->getAvailableHeroes(town), *aiNk->heroManager, bestChoice, *town, closestThreatTurn, visitabilityRatio);
  56. }
  57. if(town->hasCapitol())
  58. haveCapitol = true;
  59. }
  60. calculateFinalDecision(*aiNk, tasks, ourHeroes, bestChoice, haveCapitol, treasureSourcesCount);
  61. return tasks;
  62. }
  63. void RecruitHeroBehavior::calculateTreasureSources(
  64. const std::vector<const CGObjectInstance *> & nearbyObjects,
  65. const PlayerColor & playerID,
  66. const DangerHitMapAnalyzer & dangerHitMap,
  67. int & treasureSourcesCount,
  68. const CGTownInstance & town
  69. )
  70. {
  71. for(const auto * obj : nearbyObjects)
  72. {
  73. if(obj->ID == Obj::RESOURCE || obj->ID == Obj::TREASURE_CHEST || obj->ID == Obj::CAMPFIRE || isWeeklyRevisitable(playerID, obj)
  74. || obj->ID == Obj::ARTIFACT)
  75. {
  76. if(&town == dangerHitMap.getClosestTown(obj->visitablePos()))
  77. treasureSourcesCount++; // TODO: Mircea: Shouldn't it be used to determine the best town?
  78. }
  79. }
  80. }
  81. void RecruitHeroBehavior::calculateBestHero(
  82. const std::vector<const CGHeroInstance *> & availableHeroes,
  83. const HeroManager & heroManager,
  84. const RecruitHeroChoice & bestChoice,
  85. const CGTownInstance & town,
  86. const uint8_t closestThreatTurn,
  87. const float visitabilityRatio
  88. )
  89. {
  90. for(const auto * const hero : availableHeroes)
  91. {
  92. if((town.getVisitingHero() || town.getGarrisonHero()) && closestThreatTurn < 1 && hero->getArmyCost() < GameConstants::HERO_GOLD_COST / 3.0)
  93. continue;
  94. const float heroScore = heroManager.evaluateHero(hero);
  95. float totalScore = heroScore + hero->getArmyCost();
  96. // TODO: Mircea: Score higher if ballista/tent/ammo cart by the cost in gold? Or should that be covered in evaluateHero?
  97. // getArtifactScoreForHero(hero, ...) ArtifactID::BALLISTA
  98. if(hero->getFactionID() == town.getFactionID())
  99. totalScore += heroScore * 1.5;
  100. // prioritize a more developed town especially if no heroes can visit it (smaller ratio, bigger score)
  101. totalScore += heroScore * town.getTownLevel() * (1 - visitabilityRatio);
  102. if(totalScore > bestChoice.score)
  103. {
  104. bestChoice.score = totalScore;
  105. bestChoice.hero = hero;
  106. bestChoice.town = &town;
  107. bestChoice.closestThreat = closestThreatTurn;
  108. }
  109. }
  110. }
  111. void RecruitHeroBehavior::calculateFinalDecision(
  112. const Nullkiller & aiNk,
  113. Goals::TGoalVec & tasks,
  114. const std::vector<const CGHeroInstance *> & ourHeroes,
  115. const RecruitHeroChoice & bestChoice,
  116. const bool haveCapitol,
  117. const int treasureSourcesCount
  118. )
  119. {
  120. if(!vstd::isAlmostZero(bestChoice.score))
  121. {
  122. if(ourHeroes.empty()
  123. || treasureSourcesCount > ourHeroes.size() * 5
  124. // TODO: Mircea: The next condition should always consider a hero if under attack especially if it has towers
  125. || (bestChoice.hero->getArmyCost() > GameConstants::HERO_GOLD_COST / 2.0
  126. && (bestChoice.closestThreat < 1 || !aiNk.buildAnalyzer->isGoldPressureOverMax()))
  127. || (aiNk.getFreeResources()[EGameResID::GOLD] > 10000 && !aiNk.buildAnalyzer->isGoldPressureOverMax() && haveCapitol)
  128. || (aiNk.getFreeResources()[EGameResID::GOLD] > 30000 && !aiNk.buildAnalyzer->isGoldPressureOverMax()))
  129. {
  130. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestChoice.town, bestChoice.hero).setpriority(3.0 / (ourHeroes.size() + 1))));
  131. }
  132. }
  133. }
  134. }