HeroManager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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::updateHeroRoles()
  90. {
  91. std::map<HeroPtr, float> scores;
  92. auto myHeroes = ai->getMyHeroes();
  93. for(auto & hero : myHeroes)
  94. {
  95. scores[hero] = evaluateFightingStrength(hero.get());
  96. }
  97. std::sort(myHeroes.begin(), myHeroes.end(), [&](const HeroPtr & h1, const HeroPtr & h2) -> bool
  98. {
  99. return scores.at(h1) > scores.at(h2);
  100. });
  101. int mainHeroCount = (myHeroes.size() + 2) / 3;
  102. for(auto & hero : myHeroes)
  103. {
  104. heroRoles[hero] = (mainHeroCount--) > 0 ? HeroRole::MAIN : HeroRole::SCOUT;
  105. logAi->trace("Hero %s has role %s", hero.name, heroRoles[hero] == HeroRole::MAIN ? "main" : "scout");
  106. }
  107. }
  108. HeroRole HeroManager::getHeroRole(const HeroPtr & hero) const
  109. {
  110. return heroRoles.at(hero);
  111. }
  112. const std::map<HeroPtr, HeroRole> & HeroManager::getHeroRoles() const
  113. {
  114. return heroRoles;
  115. }
  116. int HeroManager::selectBestSkill(const HeroPtr & hero, const std::vector<SecondarySkill> & skills) const
  117. {
  118. auto role = getHeroRole(hero);
  119. auto & evaluator = role == HeroRole::MAIN ? wariorSkillsScores : scountSkillsScores;
  120. int result = 0;
  121. float resultScore = -100;
  122. for(int i = 0; i < skills.size(); i++)
  123. {
  124. auto score = evaluator.evaluateSecSkill(hero.get(), skills[i]);
  125. if(score > resultScore)
  126. {
  127. resultScore = score;
  128. result = i;
  129. }
  130. logAi->trace(
  131. "Hero %s is proposed to learn %d with score %f",
  132. hero.name,
  133. skills[i].toEnum(),
  134. score);
  135. }
  136. return result;
  137. }
  138. float HeroManager::evaluateHero(const CGHeroInstance * hero) const
  139. {
  140. return evaluateFightingStrength(hero);
  141. }
  142. SecondarySkillScoreMap::SecondarySkillScoreMap(std::map<SecondarySkill, float> scoreMap)
  143. :scoreMap(scoreMap)
  144. {
  145. }
  146. void SecondarySkillScoreMap::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  147. {
  148. auto it = scoreMap.find(skill);
  149. if(it != scoreMap.end())
  150. {
  151. score = it->second;
  152. }
  153. }
  154. void ExistingSkillRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  155. {
  156. int upgradesLeft = 0;
  157. for(auto & heroSkill : hero->secSkills)
  158. {
  159. if(heroSkill.first == skill)
  160. return;
  161. upgradesLeft += SecSkillLevel::EXPERT - heroSkill.second;
  162. }
  163. if(score >= 2 || (score >= 1 && upgradesLeft <= 1))
  164. score += 1.5;
  165. }
  166. void WisdomRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  167. {
  168. if(skill != SecondarySkill::WISDOM)
  169. return;
  170. auto wisdomLevel = hero->getSecSkillLevel(SecondarySkill::WISDOM);
  171. if(hero->level > 10 && wisdomLevel == SecSkillLevel::NONE)
  172. score += 1.5;
  173. }
  174. std::vector<SecondarySkill> AtLeastOneMagicRule::magicSchools = {
  175. SecondarySkill::AIR_MAGIC,
  176. SecondarySkill::EARTH_MAGIC,
  177. SecondarySkill::FIRE_MAGIC,
  178. SecondarySkill::WATER_MAGIC
  179. };
  180. void AtLeastOneMagicRule::evaluateScore(const CGHeroInstance * hero, SecondarySkill skill, float & score) const
  181. {
  182. if(!vstd::contains(magicSchools, skill))
  183. return;
  184. bool heroHasAnyMagic = vstd::contains_if(magicSchools, [&](SecondarySkill skill) -> bool
  185. {
  186. return hero->getSecSkillLevel(skill) > SecSkillLevel::NONE;
  187. });
  188. if(!heroHasAnyMagic)
  189. score += 1;
  190. }
  191. SecondarySkillEvaluator::SecondarySkillEvaluator(std::vector<std::shared_ptr<ISecondarySkillRule>> evaluationRules)
  192. : evaluationRules(evaluationRules)
  193. {
  194. }
  195. float SecondarySkillEvaluator::evaluateSecSkills(const CGHeroInstance * hero) const
  196. {
  197. float totalScore = 0;
  198. for(auto skill : hero->secSkills)
  199. {
  200. totalScore += skill.second * evaluateSecSkill(hero, skill.first);
  201. }
  202. return totalScore;
  203. }
  204. float SecondarySkillEvaluator::evaluateSecSkill(const CGHeroInstance * hero, SecondarySkill skill) const
  205. {
  206. float score = 0;
  207. for(auto rule : evaluationRules)
  208. rule->evaluateScore(hero, skill, score);
  209. return score;
  210. }