RecruitHeroBehavior.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <algorithm>
  13. #include "../AIGateway.h"
  14. #include "../AIUtility.h"
  15. #include "../Goals/RecruitHero.h"
  16. #include "../Goals/ExecuteHeroChain.h"
  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->heroManager->getHeroToRoleMap();
  29. 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. if(town->getVisitingHero() && town->getGarrisonHero())
  37. continue;
  38. uint8_t closestThreatTurn = UINT8_MAX;
  39. for(const auto & threat : aiNk->dangerHitMap->getTownThreats(town))
  40. {
  41. closestThreatTurn = std::min(closestThreatTurn, threat.turn);
  42. }
  43. float visitabilityRatio = 0;
  44. for(const auto & [hero, role] : ourHeroes)
  45. {
  46. if(aiNk->dangerHitMap->getClosestTown(hero.get()->visitablePos()) == town)
  47. visitabilityRatio += 1.0f / ourHeroes.size();
  48. }
  49. if(aiNk->heroManager->canRecruitHero(town))
  50. {
  51. calculateTreasureSources(aiNk->objectClusterizer->getNearbyObjects(), aiNk->playerID, *aiNk->dangerHitMap, treasureSourcesCount, town);
  52. calculateBestHero(aiNk->cc->getAvailableHeroes(town),
  53. *aiNk->heroManager,
  54. bestChoice,
  55. town,
  56. closestThreatTurn,
  57. visitabilityRatio);
  58. }
  59. if(town->hasCapitol())
  60. haveCapitol = true;
  61. }
  62. if(!vstd::isAlmostZero(bestChoice.score))
  63. {
  64. if(ourHeroes.empty()
  65. || treasureSourcesCount > ourHeroes.size() * 5
  66. // TODO: Mircea: The next condition should always consider a hero if under attack especially if it has towers
  67. || (bestChoice.hero->getArmyCost() > GameConstants::HERO_GOLD_COST / 2.0 && (
  68. bestChoice.closestThreat < 1 || !aiNk->buildAnalyzer->isGoldPressureOverMax()))
  69. || (aiNk->getFreeResources()[EGameResID::GOLD] > 10000 && !aiNk->buildAnalyzer->isGoldPressureOverMax() && haveCapitol)
  70. || (aiNk->getFreeResources()[EGameResID::GOLD] > 30000 && !aiNk->buildAnalyzer->isGoldPressureOverMax()))
  71. {
  72. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestChoice.town, bestChoice.hero).setpriority((float)3 / (ourHeroes.size() + 1))));
  73. }
  74. }
  75. return tasks;
  76. }
  77. void RecruitHeroBehavior::calculateTreasureSources(const std::vector<const CGObjectInstance *> & nearbyObjects,
  78. const PlayerColor & playerID,
  79. const DangerHitMapAnalyzer & dangerHitMap,
  80. int & treasureSourcesCount,
  81. const CGTownInstance * town)
  82. {
  83. for(const auto * obj : nearbyObjects)
  84. {
  85. if(obj->ID == Obj::RESOURCE
  86. || obj->ID == Obj::TREASURE_CHEST
  87. || obj->ID == Obj::CAMPFIRE
  88. || isWeeklyRevisitable(playerID, obj)
  89. || obj->ID == Obj::ARTIFACT)
  90. {
  91. if(town == dangerHitMap.getClosestTown(obj->visitablePos()))
  92. treasureSourcesCount++; // TODO: Mircea: Shouldn't it be used to determine the best town?
  93. }
  94. }
  95. }
  96. void RecruitHeroBehavior::calculateBestHero(const std::vector<const CGHeroInstance *> & availableHeroes,
  97. const HeroManager & heroManager,
  98. const RecruitHeroChoice & bestChoice,
  99. const CGTownInstance * town,
  100. const uint8_t closestThreatTurn,
  101. const float visitabilityRatio)
  102. {
  103. for(const auto * const hero : availableHeroes)
  104. {
  105. if((town->getVisitingHero() || town->getGarrisonHero())
  106. && closestThreatTurn < 1
  107. && hero->getArmyCost() < GameConstants::HERO_GOLD_COST / 3.0)
  108. continue;
  109. const float heroScore = heroManager.evaluateHero(hero);
  110. float totalScore = heroScore + hero->getArmyCost();
  111. // TODO: Mircea: Score higher if ballista/tent/ammo cart by the cost in gold? Or should that be covered in evaluateHero?
  112. // getArtifactScoreForHero(hero, ...) ArtifactID::BALLISTA
  113. if(hero->getFactionID() == town->getFactionID())
  114. totalScore += heroScore * 1.5;
  115. // prioritize a more developed town especially if no heroes can visit it (smaller ratio, bigger score)
  116. totalScore += heroScore * town->getTownLevel() * (1 - visitabilityRatio);
  117. if(totalScore > bestChoice.score)
  118. {
  119. bestChoice.score = totalScore;
  120. bestChoice.hero = hero;
  121. bestChoice.town = town;
  122. bestChoice.closestThreat = closestThreatTurn;
  123. }
  124. }
  125. }
  126. }