HeroManager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * HeroManager.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 "HeroManager.h"
  12. #include "../../CCallback.h"
  13. #include "../../lib/mapObjects/MapObjects.h"
  14. #include "../../lib/CHeroHandler.h"
  15. SecondarySkillEvaluator HeroManager::wariorSkillsScores = SecondarySkillEvaluator(
  16. {
  17. std::make_shared<SecondarySkillScoreMap>(
  18. std::map<SecondarySkill, float>
  19. {
  20. {SecondarySkill::DIPLOMACY, 2},
  21. {SecondarySkill::LOGISTICS, 2},
  22. {SecondarySkill::EARTH_MAGIC, 2},
  23. {SecondarySkill::ARMORER, 2},
  24. {SecondarySkill::OFFENCE, 2},
  25. {SecondarySkill::AIR_MAGIC, 1},
  26. {SecondarySkill::WISDOM, 1},
  27. {SecondarySkill::LEADERSHIP, 1},
  28. {SecondarySkill::INTELLIGENCE, 1},
  29. {SecondarySkill::RESISTANCE, 1},
  30. {SecondarySkill::MYSTICISM, -1},
  31. {SecondarySkill::SORCERY, -1},
  32. {SecondarySkill::ESTATES, -1},
  33. {SecondarySkill::FIRST_AID, -1},
  34. {SecondarySkill::LEARNING, -1},
  35. {SecondarySkill::SCHOLAR, -1},
  36. {SecondarySkill::EAGLE_EYE, -1},
  37. {SecondarySkill::NAVIGATION, -1}
  38. }),
  39. std::make_shared<ExistingSkillRule>(),
  40. std::make_shared<WisdomRule>(),
  41. std::make_shared<AtLeastOneMagicRule>()
  42. });
  43. SecondarySkillEvaluator HeroManager::scountSkillsScores = SecondarySkillEvaluator(
  44. {
  45. std::make_shared<SecondarySkillScoreMap>(
  46. std::map<SecondarySkill, float>
  47. {
  48. {SecondarySkill::LOGISTICS, 2},
  49. {SecondarySkill::ESTATES, 2},
  50. {SecondarySkill::PATHFINDING, 1},
  51. {SecondarySkill::SCHOLAR, 1}
  52. }),
  53. std::make_shared<ExistingSkillRule>()
  54. });
  55. void HeroManager::init(CPlayerSpecificInfoCallback * CB)
  56. {
  57. cb = CB;
  58. }
  59. void HeroManager::setAI(VCAI * AI)
  60. {
  61. ai = AI;
  62. }
  63. float HeroManager::evaluateSecSkill(SecondarySkill skill, const CGHeroInstance * hero) const
  64. {
  65. auto role = getHeroRole(hero);
  66. if(role == HeroRole::MAIN)
  67. return wariorSkillsScores.evaluateSecSkill(hero, skill);
  68. return scountSkillsScores.evaluateSecSkill(hero, skill);
  69. }
  70. float HeroManager::evaluateSpeciality(const CGHeroInstance * hero) const
  71. {
  72. auto heroSpecial = Selector::source(Bonus::HERO_SPECIAL, hero->type->ID.getNum());
  73. auto secondarySkillBonus = Selector::type(Bonus::SECONDARY_SKILL_PREMY);
  74. auto specialSecondarySkillBonuses = hero->getBonuses(heroSpecial.And(secondarySkillBonus));
  75. float specialityScore = 0.0f;
  76. for(auto bonus : *specialSecondarySkillBonuses)
  77. {
  78. SecondarySkill bonusSkill = SecondarySkill(bonus->subtype);
  79. float bonusScore = wariorSkillsScores.evaluateSecSkill(hero, bonusSkill);
  80. if(bonusScore > 0)
  81. specialityScore += bonusScore * bonusScore * bonusScore;
  82. }
  83. return specialityScore;
  84. }
  85. float HeroManager::evaluateFightingStrength(const CGHeroInstance * hero) const
  86. {
  87. return evaluateSpeciality(hero) + wariorSkillsScores.evaluateSecSkills(hero) + hero->level * 1.5f;
  88. }
  89. void HeroManager::update()
  90. {
  91. logAi->trace("Start analysing our heroes");
  92. std::map<HeroPtr, float> scores;
  93. auto myHeroes = ai->getMyHeroes();
  94. for(auto & hero : myHeroes)
  95. {
  96. scores[hero] = evaluateFightingStrength(hero.get());
  97. }
  98. std::sort(myHeroes.begin(), myHeroes.end(), [&](const HeroPtr & h1, const HeroPtr & h2) -> bool
  99. {
  100. return scores.at(h1) > scores.at(h2);
  101. });
  102. int mainHeroCount = (myHeroes.size() + 2) / 3;
  103. for(auto & hero : myHeroes)
  104. {
  105. heroRoles[hero] = (mainHeroCount--) > 0 ? HeroRole::MAIN : HeroRole::SCOUT;
  106. logAi->trace("Hero %s has role %s", hero.name, heroRoles[hero] == HeroRole::MAIN ? "main" : "scout");
  107. }
  108. }
  109. HeroRole HeroManager::getHeroRole(const HeroPtr & hero) const
  110. {
  111. return heroRoles.at(hero);
  112. }
  113. const std::map<HeroPtr, HeroRole> & HeroManager::getHeroRoles() const
  114. {
  115. return heroRoles;
  116. }
  117. int HeroManager::selectBestSkill(const HeroPtr & hero, const std::vector<SecondarySkill> & skills) const
  118. {
  119. auto role = getHeroRole(hero);
  120. auto & evaluator = role == HeroRole::MAIN ? wariorSkillsScores : scountSkillsScores;
  121. int result = 0;
  122. float resultScore = -100;
  123. for(int i = 0; i < skills.size(); i++)
  124. {
  125. auto score = evaluator.evaluateSecSkill(hero.get(), skills[i]);
  126. if(score > resultScore)
  127. {
  128. resultScore = score;
  129. result = i;
  130. }
  131. logAi->trace(
  132. "Hero %s is proposed to learn %d with score %f",
  133. hero.name,
  134. skills[i].toEnum(),
  135. score);
  136. }
  137. return result;
  138. }
  139. float HeroManager::evaluateHero(const CGHeroInstance * hero) const
  140. {
  141. return evaluateFightingStrength(hero);
  142. }
  143. SecondarySkillScoreMap::SecondarySkillScoreMap(std::map<SecondarySkill, float> scoreMap)
  144. :scoreMap(scoreMap)
  145. {
  146. }
  147. void SecondarySkillScoreMap::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  148. {
  149. auto it = scoreMap.find(skill);
  150. if(it != scoreMap.end())
  151. {
  152. score = it->second;
  153. }
  154. }
  155. void ExistingSkillRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  156. {
  157. int upgradesLeft = 0;
  158. for(auto & heroSkill : hero->secSkills)
  159. {
  160. if(heroSkill.first == skill)
  161. return;
  162. upgradesLeft += SecSkillLevel::EXPERT - heroSkill.second;
  163. }
  164. if(score >= 2 || (score >= 1 && upgradesLeft <= 1))
  165. score += 1.5;
  166. }
  167. void WisdomRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  168. {
  169. if(skill != SecondarySkill::WISDOM)
  170. return;
  171. auto wisdomLevel = hero->getSecSkillLevel(SecondarySkill::WISDOM);
  172. if(hero->level > 10 && wisdomLevel == SecSkillLevel::NONE)
  173. score += 1.5;
  174. }
  175. std::vector<SecondarySkill> AtLeastOneMagicRule::magicSchools = {
  176. SecondarySkill::AIR_MAGIC,
  177. SecondarySkill::EARTH_MAGIC,
  178. SecondarySkill::FIRE_MAGIC,
  179. SecondarySkill::WATER_MAGIC
  180. };
  181. void AtLeastOneMagicRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  182. {
  183. if(!vstd::contains(magicSchools, skill))
  184. return;
  185. bool heroHasAnyMagic = vstd::contains_if(magicSchools, [&](SecondarySkill skill) -> bool
  186. {
  187. return hero->getSecSkillLevel(skill) > SecSkillLevel::NONE;
  188. });
  189. if(!heroHasAnyMagic)
  190. score += 1;
  191. }
  192. SecondarySkillEvaluator::SecondarySkillEvaluator(std::vector<std::shared_ptr<ISecondarySkillRule>> evaluationRules)
  193. : evaluationRules(evaluationRules)
  194. {
  195. }
  196. float SecondarySkillEvaluator::evaluateSecSkills(const CGHeroInstance * hero) const
  197. {
  198. float totalScore = 0;
  199. for(auto skill : hero->secSkills)
  200. {
  201. totalScore += skill.second * evaluateSecSkill(hero, skill.first);
  202. }
  203. return totalScore;
  204. }
  205. float SecondarySkillEvaluator::evaluateSecSkill(const CGHeroInstance * hero, SecondarySkill skill) const
  206. {
  207. float score = 0;
  208. for(auto rule : evaluationRules)
  209. rule->evaluateScore(hero, skill, score);
  210. return score;
  211. }